Remove unused defref.
[matthijs/master-project/report.git] / Chapters / Normalization.tex
1 \chapter[chap:normalization]{Normalization}
2   % A helper to print a single example in the half the page width. The example
3   % text should be in a buffer whose name is given in an argument.
4   %
5   % The align=right option really does left-alignment, but without the program
6   % will end up on a single line. The strut=no option prevents a bunch of empty
7   % space at the start of the frame.
8   \define[1]\example{
9     \framed[offset=1mm,align=right,strut=no,background=box,frame=off]{
10       \setuptyping[option=LAM,style=sans,before=,after=,strip=auto]
11       \typebuffer[#1]
12       \setuptyping[option=none,style=\tttf,strip=auto]
13     }
14   }
15
16   \define[4]\transexample{
17     \placeexample[here][ex:trans:#1]{#2}
18     \startcombination[2*1]
19       {\example{#3}}{Original program}
20       {\example{#4}}{Transformed program}
21     \stopcombination
22   }
23
24   The first step in the core to \small{VHDL} translation process, is normalization. We
25   aim to bring the core description into a simpler form, which we can
26   subsequently translate into \small{VHDL} easily. This normal form is needed because
27   the full core language is more expressive than \small{VHDL} in some areas and because
28   core can describe expressions that do not have a direct hardware
29   interpretation.
30
31   \section{Normal form}
32     The transformations described here have a well-defined goal: To bring the
33     program in a well-defined form that is directly translatable to hardware,
34     while fully preserving the semantics of the program. We refer to this form as
35     the \emph{normal form} of the program. The formal definition of this normal
36     form is quite simple:
37
38     \placedefinition{}{A program is in \emph{normal form} if none of the
39     transformations from this chapter apply.}
40
41     Of course, this is an \quote{easy} definition of the normal form, since our
42     program will end up in normal form automatically. The more interesting part is
43     to see if this normal form actually has the properties we would like it to
44     have.
45
46     But, before getting into more definitions and details about this normal form,
47     let's try to get a feeling for it first. The easiest way to do this is by
48     describing the things we want to not have in a normal form.
49
50     \startitemize
51       \item Any \emph{polymorphism} must be removed. When laying down hardware, we
52       can't generate any signals that can have multiple types. All types must be
53       completely known to generate hardware.
54       
55       \item Any \emph{higher order} constructions must be removed. We can't
56       generate a hardware signal that contains a function, so all values,
57       arguments and returns values used must be first order.
58
59       \item Any complex \emph{nested scopes} must be removed. In the \small{VHDL}
60       description, every signal is in a single scope. Also, full expressions are
61       not supported everywhere (in particular port maps can only map signal
62       names and constants, not complete expressions). To make the \small{VHDL}
63       generation easy, a separate binder must be bound to ever application or
64       other expression.
65     \stopitemize
66
67     \todo{Intermezzo: functions vs plain values}
68
69     A very simple example of a program in normal form is given in
70     \in{example}[ex:MulSum]. As you can see, all arguments to the function (which
71     will become input ports in the final hardware) are at the outer level.
72     This means that the body of the inner lambda abstraction is never a
73     function, but always a plain value.
74
75     As the body of the inner lambda abstraction, we see a single (recursive)
76     let expression, that binds two variables (\lam{mul} and \lam{sum}). These
77     variables will be signals in the final hardware, bound to the output port
78     of the \lam{*} and \lam{+} components.
79
80     The final line (the \quote{return value} of the function) selects the
81     \lam{sum} signal to be the output port of the function. This \quote{return
82     value} can always only be a variable reference, never a more complex
83     expression.
84
85     \todo{Add generated VHDL}
86
87     \startbuffer[MulSum]
88     alu :: Bit -> Word -> Word -> Word
89     alu = λa.λb.λc.
90         let
91           mul = (*) a b
92           sum = (+) mul c
93         in
94           sum
95     \stopbuffer
96
97     \startuseMPgraphic{MulSum}
98       save a, b, c, mul, add, sum;
99
100       % I/O ports
101       newCircle.a(btex $a$ etex) "framed(false)";
102       newCircle.b(btex $b$ etex) "framed(false)";
103       newCircle.c(btex $c$ etex) "framed(false)";
104       newCircle.sum(btex $res$ etex) "framed(false)";
105
106       % Components
107       newCircle.mul(btex * etex);
108       newCircle.add(btex + etex);
109
110       a.c      - b.c   = (0cm, 2cm);
111       b.c      - c.c   = (0cm, 2cm);
112       add.c            = c.c + (2cm, 0cm);
113       mul.c            = midpoint(a.c, b.c) + (2cm, 0cm);
114       sum.c            = add.c + (2cm, 0cm);
115       c.c              = origin;
116
117       % Draw objects and lines
118       drawObj(a, b, c, mul, add, sum);
119
120       ncarc(a)(mul) "arcangle(15)";
121       ncarc(b)(mul) "arcangle(-15)";
122       ncline(c)(add);
123       ncline(mul)(add);
124       ncline(add)(sum);
125     \stopuseMPgraphic
126
127     \placeexample[here][ex:MulSum]{Simple architecture consisting of a
128     multiplier and a subtractor.}
129       \startcombination[2*1]
130         {\typebufferlam{MulSum}}{Core description in normal form.}
131         {\boxedgraphic{MulSum}}{The architecture described by the normal form.}
132       \stopcombination
133
134     The previous example described composing an architecture by calling other
135     functions (operators), resulting in a simple architecture with components and
136     connections. There is of course also some mechanism for choice in the normal
137     form. In a normal Core program, the \emph{case} expression can be used in a
138     few different ways to describe choice. In normal form, this is limited to a
139     very specific form.
140
141     \in{Example}[ex:AddSubAlu] shows an example describing a
142     simple \small{ALU}, which chooses between two operations based on an opcode
143     bit. The main structure is similar to \in{example}[ex:MulSum], but this
144     time the \lam{res} variable is bound to a case expression. This case
145     expression scrutinizes the variable \lam{opcode} (and scrutinizing more
146     complex expressions is not supported). The case expression can select a
147     different variable based on the constructor of \lam{opcode}.
148
149     \startbuffer[AddSubAlu]
150     alu :: Bit -> Word -> Word -> Word
151     alu = λopcode.λa.λb.
152         let
153           res1 = (+) a b
154           res2 = (-) a b
155           res = case opcode of
156             Low -> res1
157             High -> res2
158         in
159           res
160     \stopbuffer
161
162     \startuseMPgraphic{AddSubAlu}
163       save opcode, a, b, add, sub, mux, res;
164
165       % I/O ports
166       newCircle.opcode(btex $opcode$ etex) "framed(false)";
167       newCircle.a(btex $a$ etex) "framed(false)";
168       newCircle.b(btex $b$ etex) "framed(false)";
169       newCircle.res(btex $res$ etex) "framed(false)";
170       % Components
171       newCircle.add(btex + etex);
172       newCircle.sub(btex - etex);
173       newMux.mux;
174
175       opcode.c - a.c   = (0cm, 2cm);
176       add.c    - a.c   = (4cm, 0cm);
177       sub.c    - b.c   = (4cm, 0cm);
178       a.c      - b.c   = (0cm, 3cm);
179       mux.c            = midpoint(add.c, sub.c) + (1.5cm, 0cm);
180       res.c    - mux.c = (1.5cm, 0cm);
181       b.c              = origin;
182
183       % Draw objects and lines
184       drawObj(opcode, a, b, res, add, sub, mux);
185
186       ncline(a)(add) "posA(e)";
187       ncline(b)(sub) "posA(e)";
188       nccurve(a)(sub) "posA(e)", "angleA(0)";
189       nccurve(b)(add) "posA(e)", "angleA(0)";
190       nccurve(add)(mux) "posB(inpa)", "angleB(0)";
191       nccurve(sub)(mux) "posB(inpb)", "angleB(0)";
192       nccurve(opcode)(mux) "posB(n)", "angleA(0)", "angleB(-90)";
193       ncline(mux)(res) "posA(out)";
194     \stopuseMPgraphic
195
196     \placeexample[here][ex:AddSubAlu]{Simple \small{ALU} supporting two operations.}
197       \startcombination[2*1]
198         {\typebufferlam{AddSubAlu}}{Core description in normal form.}
199         {\boxedgraphic{AddSubAlu}}{The architecture described by the normal form.}
200       \stopcombination
201
202     As a more complete example, consider \in{example}[ex:NormalComplete]. This
203     example contains everything that is supported in normal form, with the
204     exception of builtin higher order functions. The graphical version of the
205     architecture contains a slightly simplified version, since the state tuple
206     packing and unpacking have been left out. Instead, two seperate registers are
207     drawn. Also note that most synthesis tools will further optimize this
208     architecture by removing the multiplexers at the register input and
209     instead put some gates in front of the register's clock input, but we want
210     to show the architecture as close to the description as possible.
211
212     As you can see from the previous examples, the generation of the final
213     architecture from the normal form is straightforward. In each of the
214     examples, there is a direct match between the normal form structure,
215     the generated VHDL and the architecture shown in the images.
216
217     \startbuffer[NormalComplete]
218       regbank :: Bit 
219                  -> Word 
220                  -> State (Word, Word) 
221                  -> (State (Word, Word), Word)
222
223       -- All arguments are an inital lambda (address, data, packed state)
224       regbank = λa.λd.λsp.
225       -- There are nested let expressions at top level
226       let
227         -- Unpack the state by coercion (\eg, cast from
228         -- State (Word, Word) to (Word, Word))
229         s = sp ▶ (Word, Word)
230         -- Extract both registers from the state
231         r1 = case s of (a, b) -> a
232         r2 = case s of (a, b) -> b
233         -- Calling some other user-defined function.
234         d' = foo d
235         -- Conditional connections
236         out = case a of
237           High -> r1
238           Low -> r2
239         r1' = case a of
240           High -> d'
241           Low -> r1
242         r2' = case a of
243           High -> r2
244           Low -> d'
245         -- Packing a tuple
246         s' = (,) r1' r2'
247         -- pack the state by coercion (\eg, cast from
248         -- (Word, Word) to State (Word, Word))
249         sp' = s' ▶ State (Word, Word)
250         -- Pack our return value
251         res = (,) sp' out
252       in
253         -- The actual result
254         res
255     \stopbuffer
256
257     \startuseMPgraphic{NormalComplete}
258       save a, d, r, foo, muxr, muxout, out;
259
260       % I/O ports
261       newCircle.a(btex \lam{a} etex) "framed(false)";
262       newCircle.d(btex \lam{d} etex) "framed(false)";
263       newCircle.out(btex \lam{out} etex) "framed(false)";
264       % Components
265       %newCircle.add(btex + etex);
266       newBox.foo(btex \lam{foo} etex);
267       newReg.r1(btex $\lam{r1}$ etex) "dx(4mm)", "dy(6mm)";
268       newReg.r2(btex $\lam{r2}$ etex) "dx(4mm)", "dy(6mm)", "reflect(true)";
269       newMux.muxr1;
270       % Reflect over the vertical axis
271       reflectObj(muxr1)((0,0), (0,1));
272       newMux.muxr2;
273       newMux.muxout;
274       rotateObj(muxout)(-90);
275
276       d.c               = foo.c + (0cm, 1.5cm); 
277       a.c               = (xpart r2.c + 2cm, ypart d.c - 0.5cm);
278       foo.c             = midpoint(muxr1.c, muxr2.c) + (0cm, 2cm);
279       muxr1.c           = r1.c + (0cm, 2cm);
280       muxr2.c           = r2.c + (0cm, 2cm);
281       r2.c              = r1.c + (4cm, 0cm);
282       r1.c              = origin;
283       muxout.c          = midpoint(r1.c, r2.c) - (0cm, 2cm);
284       out.c             = muxout.c - (0cm, 1.5cm);
285
286     %  % Draw objects and lines
287       drawObj(a, d, foo, r1, r2, muxr1, muxr2, muxout, out);
288       
289       ncline(d)(foo);
290       nccurve(foo)(muxr1) "angleA(-90)", "posB(inpa)", "angleB(180)";
291       nccurve(foo)(muxr2) "angleA(-90)", "posB(inpb)", "angleB(0)";
292       nccurve(muxr1)(r1) "posA(out)", "angleA(180)", "posB(d)", "angleB(0)";
293       nccurve(r1)(muxr1) "posA(out)", "angleA(0)", "posB(inpb)", "angleB(180)";
294       nccurve(muxr2)(r2) "posA(out)", "angleA(0)", "posB(d)", "angleB(180)";
295       nccurve(r2)(muxr2) "posA(out)", "angleA(180)", "posB(inpa)", "angleB(0)";
296       nccurve(r1)(muxout) "posA(out)", "angleA(0)", "posB(inpb)", "angleB(-90)";
297       nccurve(r2)(muxout) "posA(out)", "angleA(180)", "posB(inpa)", "angleB(-90)";
298       % Connect port a
299       nccurve(a)(muxout) "angleA(-90)", "angleB(180)", "posB(sel)";
300       nccurve(a)(muxr1) "angleA(180)", "angleB(-90)", "posB(sel)";
301       nccurve(a)(muxr2) "angleA(180)", "angleB(-90)", "posB(sel)";
302       ncline(muxout)(out) "posA(out)";
303     \stopuseMPgraphic
304
305     \todo{Don't split registers in this image?}
306     \placeexample[here][ex:NormalComplete]{Simple architecture consisting of an adder and a
307     subtractor.}
308       \startcombination[2*1]
309         {\typebufferlam{NormalComplete}}{Core description in normal form.}
310         {\boxedgraphic{NormalComplete}}{The architecture described by the normal form.}
311       \stopcombination
312     
313
314
315     \subsection[sec:normalization:intendednormalform]{Intended normal form definition}
316       Now we have some intuition for the normal form, we can describe how we want
317       the normal form to look like in a slightly more formal manner. The following
318       EBNF-like description completely captures the intended structure (and
319       generates a subset of GHC's core format).
320
321       Some clauses have an expression listed in parentheses. These are conditions
322       that need to apply to the clause.
323
324       \defref{intended normal form definition}
325       \todo{Fix indentation}
326       \startlambda
327       \italic{normal} := \italic{lambda}
328       \italic{lambda} := λvar.\italic{lambda} (representable(var))
329                       | \italic{toplet} 
330       \italic{toplet} := letrec [\italic{binding}...] in var (representable(var))
331       \italic{binding} := var = \italic{rhs} (representable(rhs))
332                        -- State packing and unpacking by coercion
333                        | var0 = var1 ▶ State ty (lvar(var1))
334                        | var0 = var1 ▶ ty (var1 :: State ty ∧ lvar(var1))
335       \italic{rhs} := userapp
336                    | builtinapp
337                    -- Extractor case
338                    | case var of C a0 ... an -> ai (lvar(var))
339                    -- Selector case
340                    | case var of (lvar(var))
341                       [ DEFAULT -> var ]  (lvar(var))
342                       C0 w0,0 ... w0,n -> var0
343                       \vdots
344                       Cm wm,0 ... wm,n -> varm       (\forall{}i \forall{}j, wi,j \neq vari, lvar(vari))
345       \italic{userapp} := \italic{userfunc}
346                        | \italic{userapp} {userarg}
347       \italic{userfunc} := var (gvar(var))
348       \italic{userarg} := var (lvar(var))
349       \italic{builtinapp} := \italic{builtinfunc}
350                           | \italic{builtinapp} \italic{builtinarg}
351       \italic{builtinfunc} := var (bvar(var))
352       \italic{builtinarg} := var (representable(var) ∧ lvar(var))
353                           | \italic{partapp} (partapp :: a -> b)
354                           | \italic{coreexpr} (¬representable(coreexpr) ∧ ¬(coreexpr :: a -> b))
355       \italic{partapp} := \italic{userapp} | \italic{builtinapp}
356       \stoplambda
357
358       \todo{There can still be other casts around (which the code can handle,
359       e.g., ignore), which still need to be documented here}
360
361       When looking at such a program from a hardware perspective, the top level
362       lambda's define the input ports. The variable reference in the body of
363       the recursive let expression is the output port. Most function
364       applications bound by the let expression define a component
365       instantiation, where the input and output ports are mapped to local
366       signals or arguments. Some of the others use a builtin construction (\eg
367       the \lam{case} expression) or call a builtin function (\eg \lam{+} or
368       \lam{map}). For these, a hardcoded \small{VHDL} translation is
369       available.
370
371   \section[sec:normalization:transformation]{Transformation notation}
372     To be able to concisely present transformations, we use a specific format
373     for them. It is a simple format, similar to one used in logic reasoning.
374
375     Such a transformation description looks like the following.
376
377     \starttrans
378     <context conditions>
379     ~
380     <original expression>
381     --------------------------          <expression conditions>
382     <transformed expresssion>
383     ~
384     <context additions>
385     \stoptrans
386
387     This format desribes a transformation that applies to \lam{<original
388     expresssion>} and transforms it into \lam{<transformed expression>}, assuming
389     that all conditions apply. In this format, there are a number of placeholders
390     in pointy brackets, most of which should be rather obvious in their meaning.
391     Nevertheless, we will more precisely specify their meaning below:
392
393       \startdesc{<original expression>} The expression pattern that will be matched
394       against (subexpressions of) the expression to be transformed. We call this a
395       pattern, because it can contain \emph{placeholders} (variables), which match
396       any expression or binder. Any such placeholder is said to be \emph{bound} to
397       the expression it matches. It is convention to use an uppercase letter (\eg
398       \lam{M} or \lam{E}) to refer to any expression (including a simple variable
399       reference) and lowercase letters (\eg \lam{v} or \lam{b}) to refer to
400       (references to) binders.
401
402       For example, the pattern \lam{a + B} will match the expression 
403       \lam{v + (2 * w)} (binding \lam{a} to \lam{v} and \lam{B} to 
404       \lam{(2 * w)}), but not \lam{(2 * w) + v}.
405       \stopdesc
406
407       \startdesc{<expression conditions>}
408       These are extra conditions on the expression that is matched. These
409       conditions can be used to further limit the cases in which the
410       transformation applies, commonly to prevent a transformation from
411       causing a loop with itself or another transformation.
412
413       Only if these conditions are \emph{all} true, the transformation
414       applies.
415       \stopdesc
416
417       \startdesc{<context conditions>}
418       These are a number of extra conditions on the context of the function. In
419       particular, these conditions can require some (other) top level function to be
420       present, whose value matches the pattern given here. The format of each of
421       these conditions is: \lam{binder = <pattern>}.
422
423       Typically, the binder is some placeholder bound in the \lam{<original
424       expression>}, while the pattern contains some placeholders that are used in
425       the \lam{transformed expression}.
426       
427       Only if a top level binder exists that matches each binder and pattern,
428       the transformation applies.
429       \stopdesc
430
431       \startdesc{<transformed expression>}
432       This is the expression template that is the result of the transformation. If, looking
433       at the above three items, the transformation applies, the \lam{<original
434       expression>} is completely replaced with the \lam{<transformed expression>}.
435       We call this a template, because it can contain placeholders, referring to
436       any placeholder bound by the \lam{<original expression>} or the
437       \lam{<context conditions>}. The resulting expression will have those
438       placeholders replaced by the values bound to them.
439
440       Any binder (lowercase) placeholder that has no value bound to it yet will be
441       bound to (and replaced with) a fresh binder.
442       \stopdesc
443
444       \startdesc{<context additions>}
445       These are templates for new functions to add to the context. This is a way
446       to have a transformation create new top level functions.
447
448       Each addition has the form \lam{binder = template}. As above, any
449       placeholder in the addition is replaced with the value bound to it, and any
450       binder placeholder that has no value bound to it yet will be bound to (and
451       replaced with) a fresh binder.
452       \stopdesc
453
454     As an example, we'll look at η-abstraction:
455
456     \starttrans
457     E                 \lam{E :: a -> b}
458     --------------    \lam{E} does not occur on a function position in an application
459     λx.E x            \lam{E} is not a lambda abstraction.
460     \stoptrans
461
462     η-abstraction is a well known transformation from lambda calculus. What
463     this transformation does, is take any expression that has a function type
464     and turn it into a lambda expression (giving an explicit name to the
465     argument). There are some extra conditions that ensure that this
466     transformation does not apply infinitely (which are not necessarily part
467     of the conventional definition of η-abstraction).
468
469     Consider the following function, which is a fairly obvious way to specify a
470     simple ALU (Note that \in{example}[ex:AddSubAlu] shows the normal form of this
471     function). The parentheses around the \lam{+} and \lam{-} operators are
472     commonly used in Haskell to show that the operators are used as normal
473     functions, instead of \emph{infix} operators (\eg, the operators appear
474     before their arguments, instead of in between).
475
476     \startlambda 
477     alu :: Bit -> Word -> Word -> Word
478     alu = λopcode. case opcode of
479       Low -> (+)
480       High -> (-)
481     \stoplambda
482
483     There are a few subexpressions in this function to which we could possibly
484     apply the transformation. Since the pattern of the transformation is only
485     the placeholder \lam{E}, any expression will match that. Whether the
486     transformation applies to an expression is thus solely decided by the
487     conditions to the right of the transformation.
488
489     We will look at each expression in the function in a top down manner. The
490     first expression is the entire expression the function is bound to.
491
492     \startlambda
493     λopcode. case opcode of
494       Low -> (+)
495       High -> (-)
496     \stoplambda
497
498     As said, the expression pattern matches this. The type of this expression is
499     \lam{Bit -> Word -> Word -> Word}, which matches \lam{a -> b} (Note that in
500     this case \lam{a = Bit} and \lam{b = Word -> Word -> Word}).
501
502     Since this expression is at top level, it does not occur at a function
503     position of an application. However, The expression is a lambda abstraction,
504     so this transformation does not apply.
505
506     The next expression we could apply this transformation to, is the body of
507     the lambda abstraction:
508
509     \startlambda
510     case opcode of
511       Low -> (+)
512       High -> (-)
513     \stoplambda
514
515     The type of this expression is \lam{Word -> Word -> Word}, which again
516     matches \lam{a -> b}. The expression is the body of a lambda expression, so
517     it does not occur at a function position of an application. Finally, the
518     expression is not a lambda abstraction but a case expression, so all the
519     conditions match. There are no context conditions to match, so the
520     transformation applies.
521
522     By now, the placeholder \lam{E} is bound to the entire expression. The
523     placeholder \lam{x}, which occurs in the replacement template, is not bound
524     yet, so we need to generate a fresh binder for that. Let's use the binder
525     \lam{a}. This results in the following replacement expression:
526
527     \startlambda
528     λa.(case opcode of
529       Low -> (+)
530       High -> (-)) a
531     \stoplambda
532
533     Continuing with this expression, we see that the transformation does not
534     apply again (it is a lambda expression). Next we look at the body of this
535     lambda abstraction:
536
537     \startlambda
538     (case opcode of
539       Low -> (+)
540       High -> (-)) a
541     \stoplambda
542     
543     Here, the transformation does apply, binding \lam{E} to the entire
544     expression and \lam{x} to the fresh binder \lam{b}, resulting in the
545     replacement:
546
547     \startlambda
548     λb.(case opcode of
549       Low -> (+)
550       High -> (-)) a b
551     \stoplambda
552
553     Again, the transformation does not apply to this lambda abstraction, so we
554     look at its body. For brevity, we'll put the case expression on one line from
555     now on.
556
557     \startlambda
558     (case opcode of Low -> (+); High -> (-)) a b
559     \stoplambda
560
561     The type of this expression is \lam{Word}, so it does not match \lam{a -> b}
562     and the transformation does not apply. Next, we have two options for the
563     next expression to look at: The function position and argument position of
564     the application. The expression in the argument position is \lam{b}, which
565     has type \lam{Word}, so the transformation does not apply. The expression in
566     the function position is:
567
568     \startlambda
569     (case opcode of Low -> (+); High -> (-)) a
570     \stoplambda
571
572     Obviously, the transformation does not apply here, since it occurs in
573     function position (which makes the second condition false). In the same
574     way the transformation does not apply to both components of this
575     expression (\lam{case opcode of Low -> (+); High -> (-)} and \lam{a}), so
576     we'll skip to the components of the case expression: The scrutinee and
577     both alternatives. Since the opcode is not a function, it does not apply
578     here.
579
580     The first alternative is \lam{(+)}. This expression has a function type
581     (the operator still needs two arguments). It does not occur in function
582     position of an application and it is not a lambda expression, so the
583     transformation applies.
584
585     We look at the \lam{<original expression>} pattern, which is \lam{E}.
586     This means we bind \lam{E} to \lam{(+)}. We then replace the expression
587     with the \lam{<transformed expression>}, replacing all occurences of
588     \lam{E} with \lam{(+)}. In the \lam{<transformed expression>}, the This gives us the replacement expression:
589     \lam{λx.(+) x} (A lambda expression binding \lam{x}, with a body that
590     applies the addition operator to \lam{x}).
591
592     The complete function then becomes:
593     \startlambda
594     (case opcode of Low -> λa1.(+) a1; High -> (-)) a
595     \stoplambda
596
597     Now the transformation no longer applies to the complete first alternative
598     (since it is a lambda expression). It does not apply to the addition
599     operator again, since it is now in function position in an application. It
600     does, however, apply to the application of the addition operator, since
601     that is neither a lambda expression nor does it occur in function
602     position. This means after one more application of the transformation, the
603     function becomes:
604
605     \startlambda
606     (case opcode of Low -> λa1.λb1.(+) a1 b1; High -> (-)) a
607     \stoplambda
608
609     The other alternative is left as an exercise to the reader. The final
610     function, after applying η-abstraction until it does no longer apply is:
611
612     \startlambda 
613     alu :: Bit -> Word -> Word -> Word
614     alu = λopcode.λa.b. (case opcode of
615       Low -> λa1.λb1 (+) a1 b1
616       High -> λa2.λb2 (-) a2 b2) a b
617     \stoplambda
618
619     \subsection{Transformation application}
620       In this chapter we define a number of transformations, but how will we apply
621       these? As stated before, our normal form is reached as soon as no
622       transformation applies anymore. This means our application strategy is to
623       simply apply any transformation that applies, and continuing to do that with
624       the result of each transformation.
625
626       In particular, we define no particular order of transformations. Since
627       transformation order should not influence the resulting normal form,
628       this leaves the implementation free to choose any application order that
629       results in an efficient implementation. Unfortunately this is not
630       entirely true for the current set of transformations. See
631       \in{section}[sec:normalization:non-determinism] for a discussion of this
632       problem.
633
634       When applying a single transformation, we try to apply it to every (sub)expression
635       in a function, not just the top level function body. This allows us to
636       keep the transformation descriptions concise and powerful.
637
638     \subsection{Definitions}
639       In the following sections, we will be using a number of functions and
640       notations, which we will define here.
641
642       \subsubsection{Concepts}
643         A \emph{global variable} is any variable (binder) that is bound at the
644         top level of a program, or an external module. A \emph{local variable} is any
645         other variable (\eg, variables local to a function, which can be bound by
646         lambda abstractions, let expressions and pattern matches of case
647         alternatives).  Note that this is a slightly different notion of global versus
648         local than what \small{GHC} uses internally.
649         \defref{global variable} \defref{local variable}
650
651         A \emph{hardware representable} (or just \emph{representable}) type or value
652         is (a value of) a type that we can generate a signal for in hardware. For
653         example, a bit, a vector of bits, a 32 bit unsigned word, etc. Values that are
654         not runtime representable notably include (but are not limited to): Types,
655         dictionaries, functions.
656         \defref{representable}
657
658         A \emph{builtin function} is a function supplied by the Cλash framework, whose
659         implementation is not valid Cλash. The implementation is of course valid
660         Haskell, for simulation, but it is not expressable in Cλash.
661         \defref{builtin function} \defref{user-defined function}
662
663       For these functions, Cλash has a \emph{builtin hardware translation}, so calls
664       to these functions can still be translated. These are functions like
665       \lam{map}, \lam{hwor} and \lam{length}.
666
667       A \emph{user-defined} function is a function for which we do have a Cλash
668       implementation available.
669
670       \subsubsection{Predicates}
671         Here, we define a number of predicates that can be used below to concisely
672         specify conditions.\refdef{global variable}
673
674         \emph{gvar(expr)} is true when \emph{expr} is a variable that references a
675         global variable. It is false when it references a local variable.
676
677         \refdef{local variable}\emph{lvar(expr)} is the complement of \emph{gvar}; it is true when \emph{expr}
678         references a local variable, false when it references a global variable.
679
680         \refdef{representable}\emph{representable(expr)} or \emph{representable(var)} is true when
681         \emph{expr} or \emph{var} is \emph{representable}.
682
683     \subsection[sec:normalization:uniq]{Binder uniqueness}
684       A common problem in transformation systems, is binder uniqueness. When not
685       considering this problem, it is easy to create transformations that mix up
686       bindings and cause name collisions. Take for example, the following core
687       expression:
688
689       \startlambda
690       (λa.λb.λc. a * b * c) x c
691       \stoplambda
692
693       By applying β-reduction (see \in{section}[sec:normalization:beta]) once,
694       we can simplify this expression to:
695
696       \startlambda
697       (λb.λc. x * b * c) c
698       \stoplambda
699
700       Now, we have replaced the \lam{a} binder with a reference to the \lam{x}
701       binder. No harm done here. But note that we see multiple occurences of the
702       \lam{c} binder. The first is a binding occurence, to which the second refers.
703       The last, however refers to \emph{another} instance of \lam{c}, which is
704       bound somewhere outside of this expression. Now, if we would apply beta
705       reduction without taking heed of binder uniqueness, we would get:
706
707       \startlambda
708       λc. x * c * c
709       \stoplambda
710
711       This is obviously not what was supposed to happen! The root of this problem is
712       the reuse of binders: Identical binders can be bound in different scopes, such
713       that only the inner one is \quote{visible} in the inner expression. In the example
714       above, the \lam{c} binder was bound outside of the expression and in the inner
715       lambda expression. Inside that lambda expression, only the inner \lam{c} is
716       visible.
717
718       There are a number of ways to solve this. \small{GHC} has isolated this
719       problem to their binder substitution code, which performs \emph{deshadowing}
720       during its expression traversal. This means that any binding that shadows
721       another binding on a higher level is replaced by a new binder that does not
722       shadow any other binding. This non-shadowing invariant is enough to prevent
723       binder uniqueness problems in \small{GHC}.
724
725       In our transformation system, maintaining this non-shadowing invariant is
726       a bit harder to do (mostly due to implementation issues, the prototype doesn't
727       use \small{GHC}'s subsitution code). Also, the following points can be
728       observed.
729
730       \startitemize
731       \item Deshadowing does not guarantee overall uniqueness. For example, the
732       following (slightly contrived) expression shows the identifier \lam{x} bound in
733       two seperate places (and to different values), even though no shadowing
734       occurs.
735
736       \startlambda
737       (let x = 1 in x) + (let x = 2 in x)
738       \stoplambda
739
740       \item In our normal form (and the resulting \small{VHDL}), all binders
741       (signals) within the same function (entity) will end up in the same
742       scope. To allow this, all binders within the same function should be
743       unique.
744
745       \item When we know that all binders in an expression are unique, moving around
746       or removing a subexpression will never cause any binder conflicts. If we have
747       some way to generate fresh binders, introducing new subexpressions will not
748       cause any problems either. The only way to cause conflicts is thus to
749       duplicate an existing subexpression.
750       \stopitemize
751
752       Given the above, our prototype maintains a unique binder invariant. This
753       means that in any given moment during normalization, all binders \emph{within
754       a single function} must be unique. To achieve this, we apply the following
755       technique.
756
757       \todo{Define fresh binders and unique supplies}
758
759       \startitemize
760       \item Before starting normalization, all binders in the function are made
761       unique. This is done by generating a fresh binder for every binder used. This
762       also replaces binders that did not cause any conflict, but it does ensure that
763       all binders within the function are generated by the same unique supply.
764       \refdef{fresh binder}
765       \item Whenever a new binder must be generated, we generate a fresh binder that
766       is guaranteed to be different from \emph{all binders generated so far}. This
767       can thus never introduce duplication and will maintain the invariant.
768       \item Whenever (a part of) an expression is duplicated (for example when
769       inlining), all binders in the expression are replaced with fresh binders
770       (using the same method as at the start of normalization). These fresh binders
771       can never introduce duplication, so this will maintain the invariant.
772       \item Whenever we move part of an expression around within the function, there
773       is no need to do anything special. There is obviously no way to introduce
774       duplication by moving expressions around. Since we know that each of the
775       binders is already unique, there is no way to introduce (incorrect) shadowing
776       either.
777       \stopitemize
778
779   \section{Transform passes}
780     In this section we describe the actual transforms.
781
782     Each transformation will be described informally first, explaining
783     the need for and goal of the transformation. Then, we will formally define
784     the transformation using the syntax introduced in
785     \in{section}[sec:normalization:transformation].
786
787     \subsection{General cleanup}
788       These transformations are general cleanup transformations, that aim to
789       make expressions simpler. These transformations usually clean up the
790        mess left behind by other transformations or clean up expressions to
791        expose new transformation opportunities for other transformations.
792
793        Most of these transformations are standard optimizations in other
794        compilers as well. However, in our compiler, most of these are not just
795        optimizations, but they are required to get our program into intended
796        normal form.
797
798         \placeintermezzo{}{
799           \defref{substitution notation}
800           \startframedtext[width=8cm,background=box,frame=no]
801           \startalignment[center]
802             {\tfa Substitution notation}
803           \stopalignment
804           \blank[medium]
805
806           In some of the transformations in this chapter, we need to perform
807           substitution on an expression. Substitution means replacing every
808           occurence of some expression (usually a variable reference) with
809           another expression.
810
811           There have been a lot of different notations used in literature for
812           specifying substitution. The notation that will be used in this report
813           is the following:
814
815           \startlambda
816             E[A=>B]
817           \stoplambda
818
819           This means expression \lam{E} with all occurences of \lam{A} replaced
820           with \lam{B}.
821           \stopframedtext
822         }
823
824       \subsubsection[sec:normalization:beta]{β-reduction}
825         β-reduction is a well known transformation from lambda calculus, where it is
826         the main reduction step. It reduces applications of lambda abstractions,
827         removing both the lambda abstraction and the application.
828
829         In our transformation system, this step helps to remove unwanted lambda
830         abstractions (basically all but the ones at the top level). Other
831         transformations (application propagation, non-representable inlining) make
832         sure that most lambda abstractions will eventually be reducable by
833         β-reduction.
834
835         Note that β-reduction also works on type lambda abstractions and type
836         applications as well. This means the substitution below also works on
837         type variables, in the case that the binder is a type variable and teh
838         expression applied to is a type.
839
840         \starttrans
841         (λx.E) M
842         -----------------
843         E[x=>M]
844         \stoptrans
845
846         % And an example
847         \startbuffer[from]
848         (λa. 2 * a) (2 * b)
849         \stopbuffer
850
851         \startbuffer[to]
852         2 * (2 * b)
853         \stopbuffer
854
855         \transexample{beta}{β-reduction}{from}{to}
856
857         \startbuffer[from]
858         (λt.λa::t. a) @Int
859         \stopbuffer
860
861         \startbuffer[to]
862         (λa::Int. a)
863         \stopbuffer
864
865         \transexample{beta-type}{β-reduction for type abstractions}{from}{to}
866        
867       \subsubsection{Empty let removal}
868         This transformation is simple: It removes recursive lets that have no bindings
869         (which usually occurs when unused let binding removal removes the last
870         binding from it).
871
872         Note that there is no need to define this transformation for
873         non-recursive lets, since they always contain exactly one binding.
874
875         \starttrans
876         letrec in M
877         --------------
878         M
879         \stoptrans
880
881         \todo{Example}
882
883       \subsubsection[sec:normalization:simplelet]{Simple let binding removal}
884         This transformation inlines simple let bindings, that bind some
885         binder to some other binder instead of a more complex expression (\ie
886         a = b).
887
888         This transformation is not needed to get an expression into intended
889         normal form (since these bindings are part of the intended normal
890         form), but makes the resulting \small{VHDL} a lot shorter.
891        
892         \refdef{substitution notation}
893         \starttrans
894         letrec
895           a0 = E0
896           \vdots
897           ai = b
898           \vdots
899           an = En
900         in
901           M
902         -----------------------------  \lam{b} is a variable reference
903         letrec                         \lam{ai} ≠ \lam{b}
904           a0 = E0 [ai=>b]
905           \vdots
906           ai-1 = Ei-1 [ai=>b]
907           ai+1 = Ei+1 [ai=>b]
908           \vdots
909           an = En [ai=>b]
910         in
911           M[ai=>b]
912         \stoptrans
913
914         \todo{example}
915
916       \subsubsection{Unused let binding removal}
917         This transformation removes let bindings that are never used.
918         Occasionally, \GHC's desugarer introduces some unused let bindings.
919
920         This normalization pass should really be unneeded to get into intended normal form
921         (since unused bindings are not forbidden by the normal form), but in practice
922         the desugarer or simplifier emits some unused bindings that cannot be
923         normalized (e.g., calls to a \type{PatError}\todo{Check this name}). Also,
924         this transformation makes the resulting \small{VHDL} a lot shorter.
925
926         \todo{Don't use old-style numerals in transformations}
927         \starttrans
928         letrec
929           a0 = E0
930           \vdots
931           ai = Ei
932           \vdots
933           an = En
934         in
935           M                             \lam{ai} does not occur free in \lam{M}
936         ----------------------------    \forall j, 0 ≤ j ≤ n, j ≠ i (\lam{ai} does not occur free in \lam{Ej})
937         letrec
938           a0 = E0
939           \vdots
940           ai-1 = Ei-1
941           ai+1 = Ei+1
942           \vdots
943           an = En
944         in
945           M
946         \stoptrans
947
948         \todo{Example}
949
950       \subsubsection{Cast propagation / simplification}
951         This transform pushes casts down into the expression as far as possible.
952         Since its exact role and need is not clear yet, this transformation is
953         not yet specified.
954
955         \todo{Cast propagation}
956
957       \subsubsection{Top level binding inlining}
958         This transform takes simple top level bindings generated by the
959         \small{GHC} compiler. \small{GHC} sometimes generates very simple
960         \quote{wrapper} bindings, which are bound to just a variable
961         reference, or a partial application to constants or other variable
962         references.
963
964         Note that this transformation is completely optional. It is not
965         required to get any function into intended normal form, but it does help making
966         the resulting VHDL output easier to read (since it removes a bunch of
967         components that are really boring).
968
969         This transform takes any top level binding generated by the compiler,
970         whose normalized form contains only a single let binding.
971
972         \starttrans
973         x = λa0 ... λan.let y = E in y
974         ~
975         x
976         --------------------------------------         \lam{x} is generated by the compiler
977         λa0 ... λan.let y = E in y
978         \stoptrans
979
980         \startbuffer[from]
981         (+) :: Word -> Word -> Word
982         (+) = GHC.Num.(+) @Word \$dNum
983         ~
984         (+) a b
985         \stopbuffer
986         \startbuffer[to]
987         GHC.Num.(+) @ Alu.Word \$dNum a b
988         \stopbuffer
989
990         \transexample{toplevelinline}{Top level binding inlining}{from}{to}
991        
992         \in{Example}[ex:trans:toplevelinline] shows a typical application of
993         the addition operator generated by \GHC. The type and dictionary
994         arguments used here are described in
995         \in{Section}[section:prototype:polymorphism].
996
997         Without this transformation, there would be a \lam{(+)} entity
998         in the \VHDL which would just add its inputs. This generates a
999         lot of overhead in the \VHDL, which is particularly annoying
1000         when browsing the generated RTL schematic (especially since most
1001         non-alphanumerics, like all characters in \lam{(+)}, are not
1002         allowed in \VHDL architecture names\footnote{Technically, it is
1003         allowed to use non-alphanumerics when using extended
1004         identifiers, but it seems that none of the tooling likes
1005         extended identifiers in filenames, so it effectively doesn't
1006         work.}, so the entity would be called \quote{w7aA7f} or
1007         something similarly unreadable and autogenerated).
1008
1009     \subsection{Program structure}
1010       These transformations are aimed at normalizing the overall structure
1011       into the intended form. This means ensuring there is a lambda abstraction
1012       at the top for every argument (input port or current state), putting all
1013       of the other value definitions in let bindings and making the final
1014       return value a simple variable reference.
1015
1016       \subsubsection[sec:normalization:eta]{η-abstraction}
1017         This transformation makes sure that all arguments of a function-typed
1018         expression are named, by introducing lambda expressions. When combined with
1019         β-reduction and non-representable binding inlining, all function-typed
1020         expressions should be lambda abstractions or global identifiers.
1021
1022         \starttrans
1023         E                 \lam{E :: a -> b}
1024         --------------    \lam{E} is not the first argument of an application.
1025         λx.E x            \lam{E} is not a lambda abstraction.
1026                           \lam{x} is a variable that does not occur free in \lam{E}.
1027         \stoptrans
1028
1029         \startbuffer[from]
1030         foo = λa.case a of 
1031           True -> λb.mul b b
1032           False -> id
1033         \stopbuffer
1034
1035         \startbuffer[to]
1036         foo = λa.λx.(case a of 
1037             True -> λb.mul b b
1038             False -> λy.id y) x
1039         \stopbuffer
1040
1041         \transexample{eta}{η-abstraction}{from}{to}
1042
1043       \subsubsection[sec:normalization:appprop]{Application propagation}
1044         This transformation is meant to propagate application expressions downwards
1045         into expressions as far as possible. This allows partial applications inside
1046         expressions to become fully applied and exposes new transformation
1047         opportunities for other transformations (like β-reduction and
1048         specialization).
1049
1050         Since all binders in our expression are unique (see
1051         \in{section}[sec:normalization:uniq]), there is no risk that we will
1052         introduce unintended shadowing by moving an expression into a lower
1053         scope. Also, since only move expression into smaller scopes (down into
1054         our expression), there is no risk of moving a variable reference out
1055         of the scope in which it is defined.
1056
1057         \starttrans
1058         (letrec binds in E) M
1059         ------------------------
1060         letrec binds in E M
1061         \stoptrans
1062
1063         % And an example
1064         \startbuffer[from]
1065         ( letrec
1066             val = 1
1067           in 
1068             add val
1069         ) 3
1070         \stopbuffer
1071
1072         \startbuffer[to]
1073         letrec
1074           val = 1
1075         in 
1076           add val 3
1077         \stopbuffer
1078
1079         \transexample{appproplet}{Application propagation for a let expression}{from}{to}
1080
1081         \starttrans
1082         (case x of
1083           p1 -> E1
1084           \vdots
1085           pn -> En) M
1086         -----------------
1087         case x of
1088           p1 -> E1 M
1089           \vdots
1090           pn -> En M
1091         \stoptrans
1092
1093         % And an example
1094         \startbuffer[from]
1095         ( case x of 
1096             True -> id
1097             False -> neg
1098         ) 1
1099         \stopbuffer
1100
1101         \startbuffer[to]
1102         case x of 
1103           True -> id 1
1104           False -> neg 1
1105         \stopbuffer
1106
1107         \transexample{apppropcase}{Application propagation for a case expression}{from}{to}
1108
1109       \subsubsection[sec:normalization:letrecurse]{Let recursification}
1110         This transformation makes all non-recursive lets recursive. In the
1111         end, we want a single recursive let in our normalized program, so all
1112         non-recursive lets can be converted. This also makes other
1113         transformations simpler: They can simply assume all lets are
1114         recursive.
1115
1116         \starttrans
1117         let
1118           a = E
1119         in
1120           M
1121         ------------------------------------------
1122         letrec
1123           a = E
1124         in
1125           M
1126         \stoptrans
1127
1128       \subsubsection{Let flattening}
1129         This transformation puts nested lets in the same scope, by lifting the
1130         binding(s) of the inner let into the outer let. Eventually, this will
1131         cause all let bindings to appear in the same scope.
1132
1133         This transformation only applies to recursive lets, since all
1134         non-recursive lets will be made recursive (see
1135         \in{section}[sec:normalization:letrecurse]).
1136
1137         Since we are joining two scopes together, there is no risk of moving a
1138         variable reference out of the scope where it is defined.
1139
1140         \starttrans
1141         letrec 
1142           a0 = E0
1143           \vdots
1144           ai = (letrec bindings in M)
1145           \vdots
1146           an = En
1147         in
1148           N
1149         ------------------------------------------
1150         letrec
1151           a0 = E0
1152           \vdots
1153           ai = M
1154           \vdots
1155           an = En
1156           bindings
1157         in
1158           N
1159         \stoptrans
1160
1161         \startbuffer[from]
1162         letrec
1163           a = 1
1164           b = letrec
1165             x = a
1166             y = c
1167           in
1168             x + y
1169           c = 2
1170         in
1171           b
1172         \stopbuffer
1173         \startbuffer[to]
1174         letrec
1175           a = 1
1176           b = x + y
1177           c = 2
1178           x = a
1179           y = c
1180         in
1181           b
1182         \stopbuffer
1183
1184         \transexample{letflat}{Let flattening}{from}{to}
1185
1186       \subsubsection{Return value simplification}
1187         This transformation ensures that the return value of a function is always a
1188         simple local variable reference.
1189
1190         Currently implemented using lambda simplification, let simplification, and
1191         top simplification. Should change into something like the following, which
1192         works only on the result of a function instead of any subexpression. This is
1193         achieved by the contexts, like \lam{x = E}, though this is strictly not
1194         correct (you could read this as "if there is any function \lam{x} that binds
1195         \lam{E}, any \lam{E} can be transformed, while we only mean the \lam{E} that
1196         is bound by \lam{x}. This might need some extra notes or something).
1197
1198         Note that the return value is not simplified if its not representable.
1199         Otherwise, this would cause a direct loop with the inlining of
1200         unrepresentable bindings. If the return value is not
1201         representable because it has a function type, η-abstraction should
1202         make sure that this transformation will eventually apply. If the value
1203         is not representable for other reasons, the function result itself is
1204         not representable, meaning this function is not translatable anyway.
1205
1206         \starttrans
1207         x = E                            \lam{E} is representable
1208         ~                                \lam{E} is not a lambda abstraction
1209         E                                \lam{E} is not a let expression
1210         ---------------------------      \lam{E} is not a local variable reference
1211         letrec x = E in x
1212         \stoptrans
1213
1214         \starttrans
1215         x = λv0 ... λvn.E
1216         ~                                \lam{E} is representable
1217         E                                \lam{E} is not a let expression
1218         ---------------------------      \lam{E} is not a local variable reference
1219         letrec x = E in x
1220         \stoptrans
1221
1222         \starttrans
1223         x = λv0 ... λvn.let ... in E
1224         ~                                \lam{E} is representable
1225         E                                \lam{E} is not a local variable reference
1226         -----------------------------
1227         letrec x = E in x
1228         \stoptrans
1229
1230         \startbuffer[from]
1231         x = add 1 2
1232         \stopbuffer
1233
1234         \startbuffer[to]
1235         x = letrec x = add 1 2 in x
1236         \stopbuffer
1237
1238         \transexample{retvalsimpl}{Return value simplification}{from}{to}
1239         
1240         \todo{More examples}
1241
1242     \subsection[sec:normalization:argsimpl]{Representable arguments simplification}
1243       This section contains just a single transformation that deals with
1244       representable arguments in applications. Non-representable arguments are
1245       handled by the transformations in
1246       \in{section}[sec:normalization:nonrep]. 
1247       
1248       This transformation ensures that all representable arguments will become
1249       references to local variables. This ensures they will become references
1250       to local signals in the resulting \small{VHDL}, which is required due to
1251       limitations in the component instantiation code in \VHDL (one can only
1252       assign a signal or constant to an input port). By ensuring that all
1253       arguments are always simple variable references, we always have a signal
1254       available to map to the input ports.
1255
1256       To reduce a complex expression to a simple variable reference, we create
1257       a new let expression around the application, which binds the complex
1258       expression to a new variable. The original function is then applied to
1259       this variable.
1260
1261       \refdef{global variable}
1262       Note that references to \emph{global variables} (like a top level
1263       function without arguments, but also an argumentless dataconstructors
1264       like \lam{True}) are also simplified. Only local variables generate
1265       signals in the resulting architecture. Even though argumentless
1266       dataconstructors generate constants in generated \VHDL code and could be
1267       mapped to an input port directly, they are still simplified to make the
1268       normal form more regular.
1269
1270       \refdef{representable}
1271       \starttrans
1272       M N
1273       --------------------    \lam{N} is representable
1274       letrec x = N in M x     \lam{N} is not a local variable reference
1275       \stoptrans
1276       \refdef{local variable}
1277
1278       \startbuffer[from]
1279       add (add a 1) 1
1280       \stopbuffer
1281
1282       \startbuffer[to]
1283       letrec x = add a 1 in add x 1
1284       \stopbuffer
1285
1286       \transexample{argsimpl}{Argument simplification}{from}{to}
1287
1288     \subsection[sec:normalization:builtins]{Builtin functions}
1289       This section deals with (arguments to) builtin functions.  In the
1290       intended normal form definition\refdef{intended normal form definition}
1291       we can see that there are three sorts of arguments a builtin function
1292       can receive.
1293       
1294       \startitemize[KR]
1295         \item A representable local variable reference. This is the most
1296         common argument to any function. The argument simplification
1297         transformation described in \in{section}[sec:normalization:argsimpl]
1298         makes sure that \emph{any} representable argument to \emph{any}
1299         function (including builtin functions) is turned into a local variable
1300         reference.
1301         \item (A partial application of) a top level function (either builtin on
1302         user-defined). The function extraction transformation described in
1303         this section takes care of turning every functiontyped argument into
1304         (a partial application of) a top level function.
1305         \item Any expression that is not representable and does not have a
1306         function type. Since these can be any expression, there is no
1307         transformation needed. Note that this category is exactly all
1308         expressions that are not transformed by the transformations for the
1309         previous two categories. This means that \emph{any} core expression
1310         that is used as an argument to a builtin function will be either
1311         transformed into one of the above categories, or end up in this
1312         categorie. In any case, the result is in normal form.
1313       \stopitemize
1314
1315       As noted, the argument simplification will handle any representable
1316       arguments to a builtin function. The following transformation is needed
1317       to handle non-representable arguments with a function type, all other
1318       non-representable arguments don't need any special handling.
1319
1320       \subsubsection[sec:normalization:funextract]{Function extraction}
1321         This transform deals with function-typed arguments to builtin
1322         functions. 
1323         Since builtin functions cannot be specialized (see
1324         \in{section}[sec:normalization:specialize]) to remove the arguments,
1325         these arguments are extracted into a new global function instead. In
1326         other words, we create a new top level function that has exactly the
1327         extracted argument as its body. This greatly simplifies the
1328         translation rules needed for builtin functions, since they only need
1329         to handle (partial applications of) top level functions.
1330
1331         Any free variables occuring in the extracted arguments will become
1332         parameters to the new global function. The original argument is replaced
1333         with a reference to the new function, applied to any free variables from
1334         the original argument.
1335
1336         This transformation is useful when applying higher order builtin functions
1337         like \hs{map} to a lambda abstraction, for example. In this case, the code
1338         that generates \small{VHDL} for \hs{map} only needs to handle top level functions and
1339         partial applications, not any other expression (such as lambda abstractions or
1340         even more complicated expressions).
1341
1342         \starttrans
1343         M N                     \lam{M} is (a partial aplication of) a builtin function.
1344         ---------------------   \lam{f0 ... fn} are all free local variables of \lam{N}
1345         M (x f0 ... fn)         \lam{N :: a -> b}
1346         ~                       \lam{N} is not a (partial application of) a top level function
1347         x = λf0 ... λfn.N
1348         \stoptrans
1349
1350         \startbuffer[from]
1351         addList = λb.λxs.map (λa . add a b) xs
1352         \stopbuffer
1353
1354         \startbuffer[to]
1355         addList = λb.λxs.map (f b) xs
1356         ~
1357         f = λb.λa.add a b
1358         \stopbuffer
1359
1360         \transexample{funextract}{Function extraction}{from}{to}
1361
1362         Note that the function \lam{f} will still need normalization after
1363         this.
1364
1365     \subsection{Case normalisation}
1366       \subsubsection{Scrutinee simplification}
1367         This transform ensures that the scrutinee of a case expression is always
1368         a simple variable reference.
1369
1370         \starttrans
1371         case E of
1372           alts
1373         -----------------        \lam{E} is not a local variable reference
1374         letrec x = E in 
1375           case E of
1376             alts
1377         \stoptrans
1378
1379         \startbuffer[from]
1380         case (foo a) of
1381           True -> a
1382           False -> b
1383         \stopbuffer
1384
1385         \startbuffer[to]
1386         letrec x = foo a in
1387           case x of
1388             True -> a
1389             False -> b
1390         \stopbuffer
1391
1392         \transexample{letflat}{Case normalisation}{from}{to}
1393
1394
1395       \subsubsection{Case simplification}
1396         This transformation ensures that all case expressions become normal form. This
1397         means they will become one of:
1398         \startitemize
1399         \item An extractor case with a single alternative that picks a single field
1400         from a datatype, \eg \lam{case x of (a, b) -> a}.
1401         \item A selector case with multiple alternatives and only wild binders, that
1402         makes a choice between expressions based on the constructor of another
1403         expression, \eg \lam{case x of Low -> a; High -> b}.
1404         \stopitemize
1405         
1406         \defref{wild binder}
1407         \starttrans
1408         case E of
1409           C0 v0,0 ... v0,m -> E0
1410           \vdots
1411           Cn vn,0 ... vn,m -> En
1412         --------------------------------------------------- \forall i \forall j, 0 ≤ i ≤ n, 0 ≤ i < m (\lam{wi,j} is a wild (unused) binder)
1413         letrec
1414           v0,0 = case E of C0 v0,0 .. v0,m -> v0,0
1415           \vdots
1416           v0,m = case E of C0 v0,0 .. v0,m -> v0,m
1417           \vdots
1418           vn,m = case E of Cn vn,0 .. vn,m -> vn,m
1419           x0 = E0
1420           \vdots
1421           xn = En
1422         in
1423           case E of
1424             C0 w0,0 ... w0,m -> x0
1425             \vdots
1426             Cn wn,0 ... wn,m -> xn
1427         \stoptrans
1428         \todo{Check the subscripts of this transformation}
1429
1430         Note that this transformation applies to case expressions with any
1431         scrutinee. If the scrutinee is a complex expression, this might result
1432         in duplicate hardware. An extra condition to only apply this
1433         transformation when the scrutinee is already simple (effectively
1434         causing this transformation to be only applied after the scrutinee
1435         simplification transformation) might be in order. 
1436
1437         \fxnote{This transformation specified like this is complicated and misses
1438         conditions to prevent looping with itself. Perhaps it should be split here for
1439         discussion?}
1440
1441         \startbuffer[from]
1442         case a of
1443           True -> add b 1
1444           False -> add b 2
1445         \stopbuffer
1446
1447         \startbuffer[to]
1448         letnonrec
1449           x0 = add b 1
1450           x1 = add b 2
1451         in
1452           case a of
1453             True -> x0
1454             False -> x1
1455         \stopbuffer
1456
1457         \transexample{selcasesimpl}{Selector case simplification}{from}{to}
1458
1459         \startbuffer[from]
1460         case a of
1461           (,) b c -> add b c
1462         \stopbuffer
1463         \startbuffer[to]
1464         letrec
1465           b = case a of (,) b c -> b
1466           c = case a of (,) b c -> c
1467           x0 = add b c
1468         in
1469           case a of
1470             (,) w0 w1 -> x0
1471         \stopbuffer
1472
1473         \transexample{excasesimpl}{Extractor case simplification}{from}{to}
1474
1475         \refdef{selector case}
1476         In \in{example}[ex:trans:excasesimpl] the case expression is expanded
1477         into multiple case expressions, including a pretty useless expression
1478         (that is neither a selector or extractor case). This case can be
1479         removed by the Case removal transformation in
1480         \in{section}[sec:transformation:caseremoval].
1481
1482       \subsubsection[sec:transformation:caseremoval]{Case removal}
1483         This transform removes any case expression with a single alternative and
1484         only wild binders.
1485
1486         These "useless" case expressions are usually leftovers from case simplification
1487         on extractor case (see the previous example).
1488
1489         \starttrans
1490         case x of
1491           C v0 ... vm -> E
1492         ----------------------     \lam{\forall i, 0 ≤ i ≤ m} (\lam{vi} does not occur free in E)
1493         E
1494         \stoptrans
1495
1496         \startbuffer[from]
1497         case a of
1498           (,) w0 w1 -> x0
1499         \stopbuffer
1500
1501         \startbuffer[to]
1502         x0
1503         \stopbuffer
1504
1505         \transexample{caserem}{Case removal}{from}{to}
1506
1507     \subsection[sec:normalization:nonrep]{Removing unrepresentable values}
1508       The transformations in this section are aimed at making all the
1509       values used in our expression representable. There are two main
1510       transformations that are applied to \emph{all} unrepresentable let
1511       bindings and function arguments. These are meant to address three
1512       different kinds of unrepresentable values: Polymorphic values, higher
1513       order values and literals. The transformation are described generically:
1514       They apply to all non-representable values. However, non-representable
1515       values that don't fall into one of these three categories will be moved
1516       around by these transformations but are unlikely to completely
1517       disappear. They usually mean the program was not valid in the first
1518       place, because unsupported types were used (for example, a program using
1519       strings).
1520      
1521       Each of these three categories will be detailed below, followed by the
1522       actual transformations.
1523
1524       \subsubsection{Removing Polymorphism}
1525         As noted in \in{section}[sec:prototype:polymporphism],
1526         polymorphism is made explicit in Core through type and
1527         dictionary arguments. To remove the polymorphism from a
1528         function, we can simply specialize the polymorphic function for
1529         the particular type applied to it. The same goes for dictionary
1530         arguments. To remove polymorphism from let bound values, we
1531         simply inline the let bindings that have a polymorphic type,
1532         which should (eventually) make sure that the polymorphic
1533         expression is applied to a type and/or dictionary, which can
1534         then be removed by β-reduction (\in{section}[sec:normalization:beta]).
1535
1536         Since both type and dictionary arguments are not representable,
1537         \refdef{representable}
1538         the non-representable argument specialization and
1539         non-representable let binding inlining transformations below
1540         take care of exactly this.
1541
1542         There is one case where polymorphism cannot be completely
1543         removed: Builtin functions are still allowed to be polymorphic
1544         (Since we have no function body that we could properly
1545         specialize). However, the code that generates \VHDL for builtin
1546         functions knows how to handle this, so this is not a problem.
1547
1548       \subsubsection{Defunctionalization}
1549         These transformations remove higher order expressions from our
1550         program, making all values first-order.
1551       
1552         Higher order values are always introduced by lambda abstractions, none
1553         of the other Core expression elements can introduce a function type.
1554         However, other expressions can \emph{have} a function type, when they
1555         have a lambda expression in their body. 
1556         
1557         For example, the following expression is a higher order expression
1558         that is not a lambda expression itself:
1559         
1560         \refdef{id function}
1561         \startlambda
1562           case x of
1563             High -> id
1564             Low -> λx.x
1565         \stoplambda
1566
1567         The reference to the \lam{id} function shows that we can introduce a
1568         higher order expression in our program without using a lambda
1569         expression directly. However, inside the definition of the \lam{id}
1570         function, we can be sure that a lambda expression is present.
1571         
1572         Looking closely at the definition of our normal form in
1573         \in{section}[sec:normalization:intendednormalform], we can see that
1574         there are three possibilities for higher order values to appear in our
1575         intended normal form:
1576
1577         \startitemize[KR]
1578           \item[item:toplambda] Lambda abstractions can appear at the highest level of a
1579           top level function. These lambda abstractions introduce the
1580           arguments (input ports / current state) of the function.
1581           \item[item:builtinarg] (Partial applications of) top level functions can appear as an
1582           argument to a builtin function.
1583           \item[item:completeapp] (Partial applications of) top level functions can appear in
1584           function position of an application. Since a partial application
1585           cannot appear anywhere else (except as builtin function arguments),
1586           all partial applications are applied, meaning that all applications
1587           will become complete applications. However, since application of
1588           arguments happens one by one, in the expression:
1589           \startlambda
1590             f 1 2
1591           \stoplambda
1592           the subexpression \lam{f 1} has a function type. But this is
1593           allowed, since it is inside a complete application.
1594         \stopitemize
1595
1596         We will take a typical function with some higher order values as an
1597         example. The following function takes two arguments: a \lam{Bit} and a
1598         list of numbers. Depending on the first argument, each number in the
1599         list is doubled, or the list is returned unmodified. For the sake of
1600         the example, no polymorphism is shown. In reality, at least map would
1601         be polymorphic.
1602
1603         \startlambda
1604         λy.let double = λx. x + x in
1605              case y of
1606                 Low -> map double
1607                 High -> λz. z
1608         \stoplambda
1609
1610         This example shows a number of higher order values that we cannot
1611         translate to \VHDL directly. The \lam{double} binder bound in the let
1612         expression has a function type, as well as both of the alternatives of
1613         the case expression. The first alternative is a partial application of
1614         the \lam{map} builtin function, whereas the second alternative is a
1615         lambda abstraction.
1616
1617         To reduce all higher order values to one of the above items, a number
1618         of transformations we've already seen are used. The η-abstraction
1619         transformation from \in{section}[sec:normalization:eta] ensures all
1620         function arguments are introduced by lambda abstraction on the highest
1621         level of a function. These lambda arguments are allowed because of
1622         \in{item}[item:toplambda] above. After η-abstraction, our example
1623         becomes a bit bigger:
1624
1625         \startlambda
1626         λy.λq.(let double = λx. x + x in
1627                  case y of
1628                    Low -> map double
1629                    High -> λz. z
1630               ) q
1631         \stoplambda
1632
1633         η-abstraction also introduces extra applications (the application of
1634         the let expression to \lam{q} in the above example). These
1635         applications can then propagated down by the application propagation
1636         transformation (\in{section}[sec:normalization:appprop]). In our
1637         example, the \lam{q} and \lam{r} variable will be propagated into the
1638         let expression and then into the case expression:
1639
1640         \startlambda
1641         λy.λq.let double = λx. x + x in
1642                 case y of
1643                   Low -> map double q
1644                   High -> (λz. z) q
1645         \stoplambda
1646         
1647         This propagation makes higher order values become applied (in
1648         particular both of the alternatives of the case now have a
1649         representable type). Completely applied top level functions (like the
1650         first alternative) are now no longer invalid (they fall under
1651         \in{item}[item:completeapp] above). (Completely) applied lambda
1652         abstractions can be removed by β-abstraction. For our example,
1653         applying β-abstraction results in the following:
1654
1655         \startlambda
1656         λy.λq.let double = λx. x + x in
1657                 case y of
1658                   Low -> map double q
1659                   High -> q
1660         \stoplambda
1661
1662         As you can see in our example, all of this moves applications towards
1663         the higher order values, but misses higher order functions bound by
1664         let expressions. The applications cannot be moved towards these values
1665         (since they can be used in multiple places), so the values will have
1666         to be moved towards the applications. This is achieved by inlining all
1667         higher order values bound by let applications, by the
1668         non-representable binding inlining transformation below. When applying
1669         it to our example, we get the following:
1670         
1671         \startlambda
1672         λy.λq.case y of
1673                 Low -> map (λx. x + x) q
1674                 High -> q
1675         \stoplambda
1676
1677         We've nearly eliminated all unsupported higher order values from this
1678         expressions. The one that's remaining is the first argument to the
1679         \lam{map} function. Having higher order arguments to a builtin
1680         function like \lam{map} is allowed in the intended normal form, but
1681         only if the argument is a (partial application) of a top level
1682         function. This is easily done by introducing a new top level function
1683         and put the lambda abstraction inside. This is done by the function
1684         extraction transformation from
1685         \in{section}[sec:normalization:funextract].
1686
1687         \startlambda
1688         λy.λq.case y of
1689                 Low -> map func q
1690                 High -> q
1691         \stoplambda
1692
1693         This also introduces a new function, that we have called \lam{func}:
1694
1695         \startlambda
1696         func = λx. x + x
1697         \stoplambda
1698
1699         Note that this does not actually remove the lambda, but now it is a
1700         lambda at the highest level of a function, which is allowed in the
1701         intended normal form.
1702
1703         There is one case that has not been discussed yet. What if the
1704         \lam{map} function in the example above was not a builtin function
1705         but a user-defined function? Then extracting the lambda expression
1706         into a new function would not be enough, since user-defined functions
1707         can never have higher order arguments. For example, the following
1708         expression shows an example:
1709
1710         \startlambda
1711         twice :: (Word -> Word) -> Word -> Word
1712         twice = λf.λa.f (f a)
1713
1714         main = λa.app (λx. x + x) a
1715         \stoplambda
1716
1717         This example shows a function \lam{twice} that takes a function as a
1718         first argument and applies that function twice to the second argument.
1719         Again, we've made the function monomorphic for clarity, even though
1720         this function would be a lot more useful if it was polymorphic. The
1721         function \lam{main} uses \lam{twice} to apply a lambda epression twice.
1722
1723         When faced with a user defined function, a body is available for that
1724         function. This means we could create a specialized version of the
1725         function that only works for this particular higher order argument
1726         (\ie, we can just remove the argument and call the specialized
1727         function without the argument). This transformation is detailed below.
1728         Applying this transformation to the example gives:
1729
1730         \startlambda
1731         twice' :: Word -> Word
1732         twice' = λb.(λf.λa.f (f a)) (λx. x + x) b
1733
1734         main = λa.app' a
1735         \stoplambda
1736
1737         The \lam{main} function is now in normal form, since the only higher
1738         order value there is the top level lambda expression. The new
1739         \lam{twice'} function is a bit complex, but the entire original body of
1740         the original \lam{twice} function is wrapped in a lambda abstraction
1741         and applied to the argument we've specialized for (\lam{λx. x + x})
1742         and the other arguments. This complex expression can fortunately be
1743         effectively reduced by repeatedly applying β-reduction: 
1744
1745         \startlambda
1746         twice' :: Word -> Word
1747         twice' = λb.(b + b) + (b + b)
1748         \stoplambda
1749
1750         This example also shows that the resulting normal form might not be as
1751         efficient as we might hope it to be (it is calculating \lam{(b + b)}
1752         twice). This is discussed in more detail in
1753         \in{section}[sec:normalization:duplicatework].
1754
1755       \subsubsection{Literals}
1756         There are a limited number of literals available in Haskell and Core.
1757         \refdef{enumerated types} When using (enumerating) algebraic
1758         datatypes, a literal is just a reference to the corresponding data
1759         constructor, which has a representable type (the algebraic datatype)
1760         and can be translated directly. This also holds for literals of the
1761         \hs{Bool} Haskell type, which is just an enumerated type.
1762
1763         There is, however, a second type of literal that does not have a
1764         representable type: Integer literals. Cλash supports using integer
1765         literals for all three integer types supported (\hs{SizedWord},
1766         \hs{SizedInt} and \hs{RangedWord}). This is implemented using
1767         Haskell's \hs{Num} typeclass, which offers a \hs{fromInteger} method
1768         that converts any \hs{Integer} to the Cλash datatypes.
1769
1770         When \GHC sees integer literals, it will automatically insert calls to
1771         the \hs{fromInteger} method in the resulting Core expression. For
1772         example, the following expression in Haskell creates a 32 bit unsigned
1773         word with the value 1. The explicit type signature is needed, since
1774         there is no context for \GHC to determine the type from otherwise.
1775
1776         \starthaskell
1777         1 :: SizedWord D32
1778         \stophaskell
1779
1780         This Haskell code results in the following Core expression:
1781
1782         \startlambda
1783         fromInteger @(SizedWord D32) \$dNum (smallInteger 10)
1784         \stoplambda
1785
1786         The literal 10 will have the type \lam{GHC.Prim.Int\#}, which is
1787         converted into an \lam{Integer} by \lam{smallInteger}. Finally, the
1788         \lam{fromInteger} function will finally convert this into a
1789         \lam{SizedWord D32}.
1790
1791         Both the \lam{GHC.Prim.Int\#} and \lam{Integer} types are not
1792         representable, and cannot be translated directly. Fortunately, there
1793         is no need to translate them, since \lam{fromInteger} is a builtin
1794         function that knows how to handle these values. However, this does
1795         require that the \lam{fromInteger} function is directly applied to
1796         these non-representable literal values, otherwise errors will occur.
1797         For example, the following expression is not in the intended normal
1798         form, since one of the let bindings has an unrepresentable type
1799         (\lam{Integer}):
1800
1801         \startlambda
1802         let l = smallInteger 10 in fromInteger @(SizedWord D32) \$dNum l
1803         \stoplambda
1804
1805         By inlining these let-bindings, we can ensure that unrepresentable
1806         literals bound by a let binding end up in an application of the
1807         appropriate builtin function, where they are allowed. Since it is
1808         possible that the application of that function is in a different
1809         function than the definition of the literal value, we will always need
1810         to specialize away any unrepresentable literals that are used as
1811         function arguments. The following two transformations do exactly this.
1812
1813       \subsubsection{Non-representable binding inlining}
1814         This transform inlines let bindings that are bound to a
1815         non-representable value. Since we can never generate a signal
1816         assignment for these bindings (we cannot declare a signal assignment
1817         with a non-representable type, for obvious reasons), we have no choice
1818         but to inline the binding to remove it.
1819
1820         As we have seen in the previous sections, inlining these bindings
1821         solves (part of) the polymorphism, higher order values and
1822         unrepresentable literals in an expression.
1823
1824         \refdef{substitution notation}
1825         \starttrans
1826         letrec 
1827           a0 = E0
1828           \vdots
1829           ai = Ei
1830           \vdots
1831           an = En
1832         in
1833           M
1834         --------------------------    \lam{Ei} has a non-representable type.
1835         letrec
1836           a0 = E0 [ai=>Ei] \vdots
1837           ai-1 = Ei-1 [ai=>Ei]
1838           ai+1 = Ei+1 [ai=>Ei]
1839           \vdots
1840           an = En [ai=>Ei]
1841         in
1842           M[ai=>Ei]
1843         \stoptrans
1844
1845         \startbuffer[from]
1846         letrec
1847           a = smallInteger 10
1848           inc = λb -> add b 1
1849           inc' = add 1
1850           x = fromInteger a 
1851         in
1852           inc (inc' x)
1853         \stopbuffer
1854
1855         \startbuffer[to]
1856         letrec
1857           x = fromInteger (smallInteger 10)
1858         in
1859           (λb -> add b 1) (add 1 x)
1860         \stopbuffer
1861
1862         \transexample{nonrepinline}{Nonrepresentable binding inlining}{from}{to}
1863
1864       \subsubsection[sec:normalization:specialize]{Function specialization}
1865         This transform removes arguments to user-defined functions that are
1866         not representable at runtime. This is done by creating a
1867         \emph{specialized} version of the function that only works for one
1868         particular value of that argument (in other words, the argument can be
1869         removed).
1870
1871         Specialization means to create a specialized version of the called
1872         function, with one argument already filled in. As a simple example, in
1873         the following program (this is not actual Core, since it directly uses
1874         a literal with the unrepresentable type \lam{GHC.Prim.Int\#}).
1875
1876         \startlambda
1877         f = λa.λb.a + b
1878         inc = λa.f a 1
1879         \stoplambda
1880
1881         We could specialize the function \lam{f} against the literal argument
1882         1, with the following result:
1883
1884         \startlambda
1885         f' = λa.a + 1
1886         inc = λa.f' a
1887         \stoplambda
1888
1889         In some way, this transformation is similar to β-reduction, but it
1890         operates across function boundaries. It is also similar to
1891         non-representable let binding inlining above, since it sort of
1892         \quote{inlines} an expression into a called function.
1893
1894         Special care must be taken when the argument has any free variables.
1895         If this is the case, the original argument should not be removed
1896         completely, but replaced by all the free variables of the expression.
1897         In this way, the original expression can still be evaluated inside the
1898         new function.
1899
1900         To prevent us from propagating the same argument over and over, a
1901         simple local variable reference is not propagated (since is has
1902         exactly one free variable, itself, we would only replace that argument
1903         with itself).
1904
1905         This shows that any free local variables that are not runtime
1906         representable cannot be brought into normal form by this transform. We
1907         rely on an inlining or β-reduction transformation to replace such a
1908         variable with an expression we can propagate again.
1909
1910         \starttrans
1911         x = E
1912         ~
1913         x Y0 ... Yi ... Yn                               \lam{Yi} is not representable
1914         ---------------------------------------------    \lam{Yi} is not a local variable reference
1915         x' y0 ... yi-1 f0 ...  fm Yi+1 ... Yn            \lam{f0 ... fm} are all free local vars of \lam{Yi}
1916         ~                                                \lam{T0 ... Tn} are the types of \lam{Y0 ... Yn}
1917         x' = λ(y0 :: T0) ... λ(yi-1 :: Ty-1). λf0 ... λfm. λ(yi+1 :: Ty+1) ...  λ(yn :: Tn).       
1918               E y0 ... yi-1 Yi yi+1 ... yn   
1919         \stoptrans
1920
1921         This is a bit of a complex transformation. It transforms an
1922         application of the function \lam{x}, where one of the arguments
1923         (\lam{Y_i}) is not representable. A new
1924         function \lam{x'} is created that wraps the body of the old function.
1925         The body of the new function becomes a number of nested lambda
1926         abstractions, one for each of the original arguments that are left
1927         unchanged.
1928         
1929         The ith argument is replaced with the free variables of
1930         \lam{Y_i}. Note that we reuse the same binders as those used in
1931         \lam{Y_i}, since we can then just use \lam{Y_i} inside the new
1932         function body and have all of the variables it uses be in scope.
1933
1934         The argument that we are specializing for, \lam{Y_i}, is put inside
1935         the new function body. The old function body is applied to it. Since
1936         we use this new function only in place of an application with that
1937         particular argument \lam{Y_i}, behaviour should not change.
1938         
1939         Note that the types of the arguments of our new function are taken
1940         from the types of the \emph{actual} arguments (\lam{T0 ... Tn}). This
1941         means that any polymorphism in the arguments is removed, even when the
1942         corresponding explicit type lambda is not removed
1943         yet.\refdef{type lambda}
1944
1945         \todo{Examples. Perhaps reference the previous sections}
1946
1947   \section{Unsolved problems}
1948     The above system of transformations has been implemented in the prototype
1949     and seems to work well to compile simple and more complex examples of
1950     hardware descriptions. \todo{Ref christiaan?} However, this normalization
1951     system has not seen enough review and work to be complete and work for
1952     every Core expression that is supplied to it. A number of problems
1953     have already been identified and are discussed in this section.
1954
1955     \subsection[sec:normalization:duplicatework]{Work duplication}
1956         A possible problem of β-reduction is that it could duplicate work.
1957         When the expression applied is not a simple variable reference, but
1958         requires calculation and the binder the lambda abstraction binds to
1959         is used more than once, more hardware might be generated than strictly
1960         needed. 
1961
1962         As an example, consider the expression:
1963
1964         \startlambda
1965         (λx. x + x) (a * b)
1966         \stoplambda
1967
1968         When applying β-reduction to this expression, we get:
1969
1970         \startlambda
1971         (a * b) + (a * b)
1972         \stoplambda
1973
1974         which of course calculates \lam{(a * b)} twice.
1975         
1976         A possible solution to this would be to use the following alternative
1977         transformation, which is of course no longer normal β-reduction. The
1978         followin transformation has not been tested in the prototype, but is
1979         given here for future reference:
1980
1981         \starttrans
1982         (λx.E) M
1983         -----------------
1984         letrec x = M in E
1985         \stoptrans
1986         
1987         This doesn't seem like much of an improvement, but it does get rid of
1988         the lambda expression (and the associated higher order value), while
1989         at the same time introducing a new let binding. Since the result of
1990         every application or case expression must be bound by a let expression
1991         in the intended normal form anyway, this is probably not a problem. If
1992         the argument happens to be a variable reference, then simple let
1993         binding removal (\in{section}[sec:normalization:simplelet]) will
1994         remove it, making the result identical to that of the original
1995         β-reduction transformation.
1996
1997         When also applying argument simplification to the above example, we
1998         get the following expression:
1999
2000         \startlambda
2001         let y = (a * b)
2002             z = (a * b)
2003         in y + z
2004         \stoplambda
2005
2006         Looking at this, we could imagine an alternative approach: Create a
2007         transformation that removes let bindings that bind identical values.
2008         In the above expression, the \lam{y} and \lam{z} variables could be
2009         merged together, resulting in the more efficient expression:
2010
2011         \startlambda
2012         let y = (a * b) in y + y
2013         \stoplambda
2014
2015       \subsection[sec:normalization:non-determinism]{Non-determinism}
2016         As an example, again consider the following expression:
2017
2018         \startlambda
2019         (λx. x + x) (a * b)
2020         \stoplambda
2021
2022         We can apply both β-reduction (\in{section}[sec:normalization:beta])
2023         as well as argument simplification
2024         (\in{section}[sec:normalization:argsimpl]) to this expression.
2025
2026         When applying argument simplification first and then β-reduction, we
2027         get the following expression:
2028
2029         \startlambda
2030         let y = (a * b) in y + y
2031         \stoplambda
2032
2033         When applying β-reduction first and then argument simplification, we
2034         get the following expression:
2035
2036         \startlambda
2037         let y = (a * b)
2038             z = (a * b)
2039         in y + z
2040         \stoplambda
2041
2042         As you can see, this is a different expression. This means that the
2043         order of expressions, does in fact change the resulting normal form,
2044         which is something that we would like to avoid. In this particular
2045         case one of the alternatives is even clearly more efficient, so we
2046         would of course like the more efficient form to be the normal form.
2047
2048         For this particular problem, the solutions for duplication of work
2049         seem from the previous section seem to fix the determinism of our
2050         transformation system as well. However, it is likely that there are
2051         other occurences of this problem.
2052
2053       \subsection{Casts}
2054         We do not fully understand the use of cast expressions in Core, so
2055         there are probably expressions involving cast expressions that cannot
2056         be brought into intended normal form by this transformation system.
2057
2058         The uses of casts in the core system should be investigated more and
2059         transformations will probably need updating to handle them in all
2060         cases.
2061
2062       \subsection[sec:normalization:stateproblems]{Normalization of stateful descriptions}
2063         Currently, the intended normal form definition\refdef{intended
2064         normal form definition} offers enough freedom to describe all
2065         valid stateful descriptions, but is not limiting enough. It is
2066         possible to write descriptions which are in intended normal
2067         form, but cannot be translated into \VHDL in a meaningful way
2068         (\eg, a function that swaps two substates in its result, or a
2069         function that changes a substate itself instead of passing it to
2070         a subfunction).
2071
2072         It is now up to the programmer to not do anything funny with
2073         these state values, whereas the normalization just tries not to
2074         mess up the flow of state values. In practice, there are
2075         situations where a Core program that \emph{could} be a valid
2076         stateful description is not translateable by the prototype. This
2077         most often happens when statefulness is mixed with pattern
2078         matching, causing a state input to be unpacked multiple times or
2079         be unpacked and repacked only in some of the code paths.
2080
2081         Without going into detail about the exact problems (of which
2082         there are probably more than have shown up so far), it seems
2083         unlikely that these problems can be solved entirely by just
2084         improving the \VHDL state generation in the final stage. The
2085         normalization stage seems the best place to apply the rewriting
2086         needed to support more complex stateful descriptions. This does
2087         of course mean that the intended normal form definition must be
2088         extended as well to be more specific about how state handling
2089         should look like in normal form.
2090         \in{Section}[sec:prototype:statelimits] already contains a
2091         tight description of the limitations on the use of state
2092         variables, which could be adapted into the intended normal form.
2093
2094   \section[sec:normalization:properties]{Provable properties}
2095     When looking at the system of transformations outlined above, there are a
2096     number of questions that we can ask ourselves. The main question is of course:
2097     \quote{Does our system work as intended?}. We can split this question into a
2098     number of subquestions:
2099
2100     \startitemize[KR]
2101     \item[q:termination] Does our system \emph{terminate}? Since our system will
2102     keep running as long as transformations apply, there is an obvious risk that
2103     it will keep running indefinitely. This typically happens when one
2104     transformation produces a result that is transformed back to the original
2105     by another transformation, or when one or more transformations keep
2106     expanding some expression.
2107     \item[q:soundness] Is our system \emph{sound}? Since our transformations
2108     continuously modify the expression, there is an obvious risk that the final
2109     normal form will not be equivalent to the original program: Its meaning could
2110     have changed.
2111     \item[q:completeness] Is our system \emph{complete}? Since we have a complex
2112     system of transformations, there is an obvious risk that some expressions will
2113     not end up in our intended normal form, because we forgot some transformation.
2114     In other words: Does our transformation system result in our intended normal
2115     form for all possible inputs?
2116     \item[q:determinism] Is our system \emph{deterministic}? Since we have defined
2117     no particular order in which the transformation should be applied, there is an
2118     obvious risk that different transformation orderings will result in
2119     \emph{different} normal forms. They might still both be intended normal forms
2120     (if our system is \emph{complete}) and describe correct hardware (if our
2121     system is \emph{sound}), so this property is less important than the previous
2122     three: The translator would still function properly without it.
2123     \stopitemize
2124
2125     Unfortunately, the final transformation system has only been
2126     developed in the final part of the research, leaving no more time
2127     for verifying these properties. In fact, it is likely that the
2128     current transformation system still violates some of these
2129     properties in some cases and should be improved (or extra conditions
2130     on the input hardware descriptions should be formulated).
2131
2132     This is most likely the case with the completeness and determinism
2133     properties, perhaps als the termination property. The soundness
2134     property probably holds, since it is easier to manually verify (each
2135     transformation can be reviewed separately).
2136
2137     Even though no complete proofs have been made, some ideas for
2138     possible proof strategies are shown below.
2139
2140     \subsection{Graph representation}
2141       Before looking into how to prove these properties, we'll look at
2142       transformation systems from a graph perspective. We will first define
2143       the graph view and then illustrate it using a simple example from lambda
2144       calculus (which is a different system than the Cλash normalization
2145       system). The nodes of the graph are all possible Core expressions. The
2146       (directed) edges of the graph are transformations. When a transformation
2147       α applies to an expression \lam{A} to produce an expression \lam{B}, we
2148       add an edge from the node for \lam{A} to the node for \lam{B}, labeled
2149       α.
2150
2151       \startuseMPgraphic{TransformGraph}
2152         save a, b, c, d;
2153
2154         % Nodes
2155         newCircle.a(btex \lam{(λx.λy. (+) x y) 1} etex);
2156         newCircle.b(btex \lam{λy. (+) 1 y} etex);
2157         newCircle.c(btex \lam{(λx.(+) x) 1} etex);
2158         newCircle.d(btex \lam{(+) 1} etex);
2159
2160         b.c = origin;
2161         c.c = b.c + (4cm, 0cm);
2162         a.c = midpoint(b.c, c.c) + (0cm, 4cm);
2163         d.c = midpoint(b.c, c.c) - (0cm, 3cm);
2164
2165         % β-conversion between a and b
2166         ncarc.a(a)(b) "name(bred)";
2167         ObjLabel.a(btex $\xrightarrow[normal]{}{β}$ etex) "labpathname(bred)", "labdir(rt)";
2168         ncarc.b(b)(a) "name(bexp)", "linestyle(dashed withdots)";
2169         ObjLabel.b(btex $\xleftarrow[normal]{}{β}$ etex) "labpathname(bexp)", "labdir(lft)";
2170
2171         % η-conversion between a and c
2172         ncarc.a(a)(c) "name(ered)";
2173         ObjLabel.a(btex $\xrightarrow[normal]{}{η}$ etex) "labpathname(ered)", "labdir(rt)";
2174         ncarc.c(c)(a) "name(eexp)", "linestyle(dashed withdots)";
2175         ObjLabel.c(btex $\xleftarrow[normal]{}{η}$ etex) "labpathname(eexp)", "labdir(lft)";
2176
2177         % η-conversion between b and d
2178         ncarc.b(b)(d) "name(ered)";
2179         ObjLabel.b(btex $\xrightarrow[normal]{}{η}$ etex) "labpathname(ered)", "labdir(rt)";
2180         ncarc.d(d)(b) "name(eexp)", "linestyle(dashed withdots)";
2181         ObjLabel.d(btex $\xleftarrow[normal]{}{η}$ etex) "labpathname(eexp)", "labdir(lft)";
2182
2183         % β-conversion between c and d
2184         ncarc.c(c)(d) "name(bred)";
2185         ObjLabel.c(btex $\xrightarrow[normal]{}{β}$ etex) "labpathname(bred)", "labdir(rt)";
2186         ncarc.d(d)(c) "name(bexp)", "linestyle(dashed withdots)";
2187         ObjLabel.d(btex $\xleftarrow[normal]{}{β}$ etex) "labpathname(bexp)", "labdir(lft)";
2188
2189         % Draw objects and lines
2190         drawObj(a, b, c, d);
2191       \stopuseMPgraphic
2192
2193       \placeexample[right][ex:TransformGraph]{Partial graph of a lambda calculus
2194       system with β and η reduction (solid lines) and expansion (dotted lines).}
2195           \boxedgraphic{TransformGraph}
2196
2197       Of course the graph for Cλash is unbounded, since we can construct an
2198       infinite amount of Core expressions. Also, there might potentially be
2199       multiple edges between two given nodes (with different labels), though
2200       seems unlikely to actually happen in our system.
2201
2202       See \in{example}[ex:TransformGraph] for the graph representation of a very
2203       simple lambda calculus that contains just the expressions \lam{(λx.λy. (+) x
2204       y) 1}, \lam{λy. (+) 1 y}, \lam{(λx.(+) x) 1} and \lam{(+) 1}. The
2205       transformation system consists of β-reduction and η-reduction (solid edges) or
2206       β-expansion and η-expansion (dotted edges).
2207
2208       \todo{Define β-reduction and η-reduction?}
2209
2210       Note that the normal form of such a system consists of the set of nodes
2211       (expressions) without outgoing edges, since those are the expression to which
2212       no transformation applies anymore. We call this set of nodes the \emph{normal
2213       set}. The set of nodes containing expressions in intended normal
2214       form \refdef{intended normal form} is called the \emph{intended
2215       normal set}.
2216
2217       From such a graph, we can derive some properties easily:
2218       \startitemize[KR]
2219         \item A system will \emph{terminate} if there is no path of infinite length
2220         in the graph (this includes cycles, but can also happen without cycles).
2221         \item Soundness is not easily represented in the graph.
2222         \item A system is \emph{complete} if all of the nodes in the normal set have
2223         the intended normal form. The inverse (that all of the nodes outside of
2224         the normal set are \emph{not} in the intended normal form) is not
2225         strictly required. In other words, our normal set must be a
2226         subset of the intended normal form, but they do not need to be
2227         the same set.
2228         form.
2229         \item A system is deterministic if all paths starting at a particular
2230         node, which end in a node in the normal set, end at the same node.
2231       \stopitemize
2232
2233       When looking at the \in{example}[ex:TransformGraph], we see that the system
2234       terminates for both the reduction and expansion systems (but note that, for
2235       expansion, this is only true because we've limited the possible
2236       expressions.  In comlete lambda calculus, there would be a path from
2237       \lam{(λx.λy. (+) x y) 1} to \lam{(λx.λy.(λz.(+) z) x y) 1} to
2238       \lam{(λx.λy.(λz.(λq.(+) q) z) x y) 1} etc.)
2239
2240       If we would consider the system with both expansion and reduction, there
2241       would no longer be termination either, since there would be cycles all
2242       over the place.
2243
2244       The reduction and expansion systems have a normal set of containing just
2245       \lam{(+) 1} or \lam{(λx.λy. (+) x y) 1} respectively. Since all paths in
2246       either system end up in these normal forms, both systems are \emph{complete}.
2247       Also, since there is only one node in the normal set, it must obviously be
2248       \emph{deterministic} as well.
2249
2250     \subsection{Termination}
2251       In general, proving termination of an arbitrary program is a very
2252       hard problem. \todo{Ref about arbitrary termination} Fortunately,
2253       we only have to prove termination for our specific transformation
2254       system.
2255
2256       A common approach for these kinds of proofs is to associate a
2257       measure with each possible expression in our system. If we can
2258       show that each transformation strictly decreases this measure
2259       (\ie, the expression transformed to has a lower measure than the
2260       expression transformed from).  \todo{ref about measure-based
2261       termination proofs / analysis}
2262       
2263       A good measure for a system consisting of just β-reduction would
2264       be the number of lambda expressions in the expression. Since every
2265       application of β-reduction removes a lambda abstraction (and there
2266       is always a bounded number of lambda abstractions in every
2267       expression) we can easily see that a transformation system with
2268       just β-reduction will always terminate.
2269
2270       For our complete system, this measure would be fairly complex
2271       (probably the sum of a lot of things). Since the (conditions on)
2272       our transformations are pretty complex, we would need to include
2273       both simple things like the number of let expressions as well as
2274       more complex things like the number of case expressions that are
2275       not yet in normal form.
2276
2277       No real attempt has been made at finding a suitable measure for
2278       our system yet.
2279
2280     \subsection{Soundness}
2281       Soundness is a property that can be proven for each transformation
2282       separately. Since our system only runs separate transformations
2283       sequentially, if each of our transformations leaves the
2284       \emph{meaning} of the expression unchanged, then the entire system
2285       will of course leave the meaning unchanged and is thus
2286       \emph{sound}.
2287
2288       The current prototype has only been verified in an ad-hoc fashion
2289       by inspecting (the code for) each transformation. A more formal
2290       verification would be more appropriate.
2291
2292       To be able to formally show that each transformation properly
2293       preserves the meaning of every expression, we require an exact
2294       definition of the \emph{meaning} of every expression, so we can
2295       compare them. Currently there seems to be no formal definition of
2296       the meaning or semantics of \GHC's core language, only informal
2297       descriptions are available.
2298
2299       It should be possible to have a single formal definition of
2300       meaning for Core for both normal Core compilation by \GHC and for
2301       our compilation to \VHDL. The main difference seems to be that in
2302       hardware every expression is always evaluated, while in software
2303       it is only evaluated if needed, but it should be possible to
2304       assign a meaning to core expressions that assumes neither.
2305       
2306       Since each of the transformations can be applied to any
2307       subexpression as well, there is a constraint on our meaning
2308       definition: The meaning of an expression should depend only on the
2309       meaning of subexpressions, not on the expressions themselves. For
2310       example, the meaning of the application in \lam{f (let x = 4 in
2311       x)} should be the same as the meaning of the application in \lam{f
2312       4}, since the argument subexpression has the same meaning (though
2313       the actual expression is different).
2314       
2315     \subsection{Completeness}
2316       Proving completeness is probably not hard, but it could be a lot
2317       of work. We have seen above that to prove completeness, we must
2318       show that the normal set of our graph representation is a subset
2319       of the intended normal set.
2320
2321       However, it is hard to systematically generate or reason about the
2322       normal set, since it is defined as any nodes to which no
2323       transformation applies. To determine this set, each transformation
2324       must be considered and when a transformation is added, the entire
2325       set should be re-evaluated. This means it is hard to show that
2326       each node in the normal set is also in the intended normal set.
2327       Reasoning about our intended normal set is easier, since we know
2328       how to generate it from its definition. \refdef{intended normal
2329       form definition}.
2330
2331       Fortunately, we can also prove the complement (which is
2332       equivalent, since $A \subseteq B \Leftrightarrow \overline{B}
2333       \subseteq \overline{A}$): Show that the set of nodes not in
2334       intended normal form is a subset of the set of nodes not in normal
2335       form. In other words, show that for every expression that is not
2336       in intended normal form, that there is at least one transformation
2337       that applies to it (since that means it is not in normal form
2338       either and since $A \subseteq C \Leftrightarrow \forall x (x \in A
2339       \rightarrow x \in C)$).
2340
2341       By systematically reviewing the entire Core language definition
2342       along with the intended normal form definition (both of which have
2343       a similar structure), it should be possible to identify all
2344       possible (sets of) core expressions that are not in intended
2345       normal form and identify a transformation that applies to it.
2346       
2347       This approach is especially useful for proving completeness of our
2348       system, since if expressions exist to which none of the
2349       transformations apply (\ie if the system is not yet complete), it
2350       is immediately clear which expressions these are and adding
2351       (or modifying) transformations to fix this should be relatively
2352       easy.
2353
2354       As observed above, applying this approach is a lot of work, since
2355       we need to check every (set of) transformation(s) separately.
2356
2357       \todo{Perhaps do a few steps of the proofs as proof-of-concept}
2358
2359 % vim: set sw=2 sts=2 expandtab: