Allow literal $'s to be used in lambda expressions.
[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=]
11       \typebuffer[#1]
12       \setuptyping[option=none,style=\tttf]
13     }
14   }
15
16   \define[3]\transexample{
17     \placeexample[here]{#1}
18     \startcombination[2*1]
19       {\example{#2}}{Original program}
20       {\example{#3}}{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   TODO: Describe core properties not supported in \small{VHDL}, and describe how the
32   \small{VHDL} we want to generate should look like.
33
34   \section{Normal form}
35     The transformations described here have a well-defined goal: To bring the
36     program in a well-defined form that is directly translatable to hardware,
37     while fully preserving the semantics of the program. We refer to this form as
38     the \emph{normal form} of the program. The formal definition of this normal
39     form is quite simple:
40
41     \placedefinition{}{A program is in \emph{normal form} if none of the
42     transformations from this chapter apply.}
43
44     Of course, this is an \quote{easy} definition of the normal form, since our
45     program will end up in normal form automatically. The more interesting part is
46     to see if this normal form actually has the properties we would like it to
47     have.
48
49     But, before getting into more definitions and details about this normal form,
50     let's try to get a feeling for it first. The easiest way to do this is by
51     describing the things we want to not have in a normal form.
52
53     \startitemize
54       \item Any \emph{polymorphism} must be removed. When laying down hardware, we
55       can't generate any signals that can have multiple types. All types must be
56       completely known to generate hardware.
57       
58       \item Any \emph{higher order} constructions must be removed. We can't
59       generate a hardware signal that contains a function, so all values,
60       arguments and returns values used must be first order.
61
62       \item Any complex \emph{nested scopes} must be removed. In the \small{VHDL}
63       description, every signal is in a single scope. Also, full expressions are
64       not supported everywhere (in particular port maps can only map signal names,
65       not expressions). To make the \small{VHDL} generation easy, all values must be bound
66       on the \quote{top level}.
67     \stopitemize
68
69     TODO: Intermezzo: functions vs plain values
70
71     A very simple example of a program in normal form is given in
72     \in{example}[ex:MulSum]. As you can see, all arguments to the function (which
73     will become input ports in the final hardware) are at the top. This means that
74     the body of the final lambda abstraction is never a function, but always a
75     plain value.
76
77     After the lambda abstractions, we see a single let expression, that binds two
78     variables (\lam{mul} and \lam{sum}). These variables will be signals in the
79     final hardware, bound to the output port of the \lam{*} and \lam{+}
80     components.
81
82     The final line (the \quote{return value} of the function) selects the
83     \lam{sum} signal to be the output port of the function. This \quote{return
84     value} can always only be a variable reference, never a more complex
85     expression.
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 an adder and a
128     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 component and
136     connection. 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 the same as in \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 replace
209     them with some logic in the clock inputs, but we want to show the architecture
210     as close to the description as possible.
211
212     \startbuffer[NormalComplete]
213       regbank :: Bit 
214                  -> Word 
215                  -> State (Word, Word) 
216                  -> (State (Word, Word), Word)
217
218       -- All arguments are an inital lambda
219       regbank = λa.λd.λsp.
220       -- There are nested let expressions at top level
221       let
222         -- Unpack the state by coercion (\eg, cast from
223         -- State (Word, Word) to (Word, Word))
224         s = sp :: (Word, Word)
225         -- Extract both registers from the state
226         r1 = case s of (fst, snd) -> fst
227         r2 = case s of (fst, snd) -> snd
228         -- Calling some other user-defined function.
229         d' = foo d
230         -- Conditional connections
231         out = case a of
232           High -> r1
233           Low -> r2
234         r1' = case a of
235           High -> d'
236           Low -> r1
237         r2' = case a of
238           High -> r2
239           Low -> d'
240         -- Packing a tuple
241         s' = (,) r1' r2'
242         -- pack the state by coercion (\eg, cast from
243         -- (Word, Word) to State (Word, Word))
244         sp' = s' :: State (Word, Word)
245         -- Pack our return value
246         res = (,) sp' out
247       in
248         -- The actual result
249         res
250     \stopbuffer
251
252     \startuseMPgraphic{NormalComplete}
253       save a, d, r, foo, muxr, muxout, out;
254
255       % I/O ports
256       newCircle.a(btex \lam{a} etex) "framed(false)";
257       newCircle.d(btex \lam{d} etex) "framed(false)";
258       newCircle.out(btex \lam{out} etex) "framed(false)";
259       % Components
260       %newCircle.add(btex + etex);
261       newBox.foo(btex \lam{foo} etex);
262       newReg.r1(btex $\lam{r1}$ etex) "dx(4mm)", "dy(6mm)";
263       newReg.r2(btex $\lam{r2}$ etex) "dx(4mm)", "dy(6mm)", "reflect(true)";
264       newMux.muxr1;
265       % Reflect over the vertical axis
266       reflectObj(muxr1)((0,0), (0,1));
267       newMux.muxr2;
268       newMux.muxout;
269       rotateObj(muxout)(-90);
270
271       d.c               = foo.c + (0cm, 1.5cm); 
272       a.c               = (xpart r2.c + 2cm, ypart d.c - 0.5cm);
273       foo.c             = midpoint(muxr1.c, muxr2.c) + (0cm, 2cm);
274       muxr1.c           = r1.c + (0cm, 2cm);
275       muxr2.c           = r2.c + (0cm, 2cm);
276       r2.c              = r1.c + (4cm, 0cm);
277       r1.c              = origin;
278       muxout.c          = midpoint(r1.c, r2.c) - (0cm, 2cm);
279       out.c             = muxout.c - (0cm, 1.5cm);
280
281     %  % Draw objects and lines
282       drawObj(a, d, foo, r1, r2, muxr1, muxr2, muxout, out);
283       
284       ncline(d)(foo);
285       nccurve(foo)(muxr1) "angleA(-90)", "posB(inpa)", "angleB(180)";
286       nccurve(foo)(muxr2) "angleA(-90)", "posB(inpb)", "angleB(0)";
287       nccurve(muxr1)(r1) "posA(out)", "angleA(180)", "posB(d)", "angleB(0)";
288       nccurve(r1)(muxr1) "posA(out)", "angleA(0)", "posB(inpb)", "angleB(180)";
289       nccurve(muxr2)(r2) "posA(out)", "angleA(0)", "posB(d)", "angleB(180)";
290       nccurve(r2)(muxr2) "posA(out)", "angleA(180)", "posB(inpa)", "angleB(0)";
291       nccurve(r1)(muxout) "posA(out)", "angleA(0)", "posB(inpb)", "angleB(-90)";
292       nccurve(r2)(muxout) "posA(out)", "angleA(180)", "posB(inpa)", "angleB(-90)";
293       % Connect port a
294       nccurve(a)(muxout) "angleA(-90)", "angleB(180)", "posB(sel)";
295       nccurve(a)(muxr1) "angleA(180)", "angleB(-90)", "posB(sel)";
296       nccurve(a)(muxr2) "angleA(180)", "angleB(-90)", "posB(sel)";
297       ncline(muxout)(out) "posA(out)";
298     \stopuseMPgraphic
299
300     \placeexample[here][ex:NormalComplete]{Simple architecture consisting of an adder and a
301     subtractor.}
302       \startcombination[2*1]
303         {\typebufferlam{NormalComplete}}{Core description in normal form.}
304         {\boxedgraphic{NormalComplete}}{The architecture described by the normal form.}
305       \stopcombination
306
307     \subsection{Intended normal form definition}
308       Now we have some intuition for the normal form, we can describe how we want
309       the normal form to look like in a slightly more formal manner. The following
310       EBNF-like description completely captures the intended structure (and
311       generates a subset of GHC's core format).
312
313       Some clauses have an expression listed in parentheses. These are conditions
314       that need to apply to the clause.
315
316       \startlambda
317       \italic{normal} = \italic{lambda}
318       \italic{lambda} = λvar.\italic{lambda} (representable(var))
319                       | \italic{toplet} 
320       \italic{toplet} = letrec [\italic{binding}...] in var (representable(varvar))
321       \italic{binding} = var = \italic{rhs} (representable(rhs))
322                        -- State packing and unpacking by coercion
323                        | var0 = var1 :: State ty (lvar(var1))
324                        | var0 = var1 :: ty (var0 :: State ty) (lvar(var1))
325       \italic{rhs} = userapp
326                    | builtinapp
327                    -- Extractor case
328                    | case var of C a0 ... an -> ai (lvar(var))
329                    -- Selector case
330                    | case var of (lvar(var))
331                       DEFAULT -> var0 (lvar(var0))
332                       C w0 ... wn -> resvar (\forall{}i, wi \neq resvar, lvar(resvar))
333       \italic{userapp} = \italic{userfunc}
334                        | \italic{userapp} {userarg}
335       \italic{userfunc} = var (gvar(var))
336       \italic{userarg} = var (lvar(var))
337       \italic{builtinapp} = \italic{builtinfunc}
338                           | \italic{builtinapp} \italic{builtinarg}
339       \italic{builtinfunc} = var (bvar(var))
340       \italic{builtinarg} = \italic{coreexpr}
341       \stoplambda
342
343       -- TODO: Limit builtinarg further
344
345       -- TODO: There can still be other casts around (which the code can handle,
346       e.g., ignore), which still need to be documented here.
347
348       -- TODO: Note about the selector case. It just supports Bit and Bool
349       currently, perhaps it should be generalized in the normal form?
350
351       When looking at such a program from a hardware perspective, the top level
352       lambda's define the input ports. The value produced by the let expression is
353       the output port. Most function applications bound by the let expression
354       define a component instantiation, where the input and output ports are mapped
355       to local signals or arguments. Some of the others use a builtin
356       construction (\eg the \lam{case} statement) or call a builtin function
357       (\eg \lam{add} or \lam{sub}). For these, a hardcoded \small{VHDL} translation is
358       available.
359
360   \section{Transformation notation}
361     To be able to concisely present transformations, we use a specific format to
362     them. It is a simple format, similar to one used in logic reasoning.
363
364     Such a transformation description looks like the following.
365
366     \starttrans
367     <context conditions>
368     ~
369     <original expression>
370     --------------------------          <expression conditions>
371     <transformed expresssion>
372     ~
373     <context additions>
374     \stoptrans
375
376     This format desribes a transformation that applies to \lam{original
377     expresssion} and transforms it into \lam{transformed expression}, assuming
378     that all conditions apply. In this format, there are a number of placeholders
379     in pointy brackets, most of which should be rather obvious in their meaning.
380     Nevertheless, we will more precisely specify their meaning below:
381
382       \startdesc{<original expression>} The expression pattern that will be matched
383       against (subexpressions of) the expression to be transformed. We call this a
384       pattern, because it can contain \emph{placeholders} (variables), which match
385       any expression or binder. Any such placeholder is said to be \emph{bound} to
386       the expression it matches. It is convention to use an uppercase latter (\eg
387       \lam{M} or \lam{E} to refer to any expression (including a simple variable
388       reference) and lowercase letters (\eg \lam{v} or \lam{b}) to refer to
389       (references to) binders.
390
391       For example, the pattern \lam{a + B} will match the expression 
392       \lam{v + (2 * w)} (and bind \lam{a} to \lam{v} and \lam{B} to 
393       \lam{(2 * 2)}), but not \lam{v + (2 * w)}.
394       \stopdesc
395
396       \startdesc{<expression conditions>}
397       These are extra conditions on the expression that is matched. These
398       conditions can be used to further limit the cases in which the
399       transformation applies, in particular to prevent a transformation from
400       causing a loop with itself or another transformation.
401
402       Only if these if these conditions are \emph{all} true, this transformation
403       applies.
404       \stopdesc
405
406       \startdesc{<context conditions>}
407       These are a number of extra conditions on the context of the function. In
408       particular, these conditions can require some other top level function to be
409       present, whose value matches the pattern given here. The format of each of
410       these conditions is: \lam{binder = <pattern>}.
411
412       Typically, the binder is some placeholder bound in the \lam{<original
413       expression>}, while the pattern contains some placeholders that are used in
414       the \lam{transformed expression}.
415       
416       Only if a top level binder exists that matches each binder and pattern, this
417       transformation applies.
418       \stopdesc
419
420       \startdesc{<transformed expression>}
421       This is the expression template that is the result of the transformation. If, looking
422       at the above three items, the transformation applies, the \lam{original
423       expression} is completely replaced with the \lam{<transformed expression>}.
424       We call this a template, because it can contain placeholders, referring to
425       any placeholder bound by the \lam{<original expression>} or the
426       \lam{<context conditions>}. The resulting expression will have those
427       placeholders replaced by the values bound to them.
428
429       Any binder (lowercase) placeholder that has no value bound to it yet will be
430       bound to (and replaced with) a fresh binder.
431       \stopdesc
432
433       \startdesc{<context additions>}
434       These are templates for new functions to add to the context. This is a way
435       to have a transformation create new top level functiosn.
436
437       Each addition has the form \lam{binder = template}. As above, any
438       placeholder in the addition is replaced with the value bound to it, and any
439       binder placeholder that has no value bound to it yet will be bound to (and
440       replaced with) a fresh binder.
441       \stopdesc
442
443     As an example, we'll look at η-abstraction:
444
445     \starttrans
446     E                 \lam{E :: a -> b}
447     --------------    \lam{E} does not occur on a function position in an application
448     λx.E x            \lam{E} is not a lambda abstraction.
449     \stoptrans
450
451     Consider the following function, which is a fairly obvious way to specify a
452     simple ALU (Note \at{example}[ex:AddSubAlu] is the normal form of this
453     function):
454
455     \startlambda 
456     alu :: Bit -> Word -> Word -> Word
457     alu = λopcode. case opcode of
458       Low -> (+)
459       High -> (-)
460     \stoplambda
461
462     There are a few subexpressions in this function to which we could possibly
463     apply the transformation. Since the pattern of the transformation is only
464     the placeholder \lam{E}, any expression will match that. Whether the
465     transformation applies to an expression is thus solely decided by the
466     conditions to the right of the transformation.
467
468     We will look at each expression in the function in a top down manner. The
469     first expression is the entire expression the function is bound to.
470
471     \startlambda
472     λopcode. case opcode of
473       Low -> (+)
474       High -> (-)
475     \stoplambda
476
477     As said, the expression pattern matches this. The type of this expression is
478     \lam{Bit -> Word -> Word -> Word}, which matches \lam{a -> b} (Note that in
479     this case \lam{a = Bit} and \lam{b = Word -> Word -> Word}).
480
481     Since this expression is at top level, it does not occur at a function
482     position of an application. However, The expression is a lambda abstraction,
483     so this transformation does not apply.
484
485     The next expression we could apply this transformation to, is the body of
486     the lambda abstraction:
487
488     \startlambda
489     case opcode of
490       Low -> (+)
491       High -> (-)
492     \stoplambda
493
494     The type of this expression is \lam{Word -> Word -> Word}, which again
495     matches \lam{a -> b}. The expression is the body of a lambda expression, so
496     it does not occur at a function position of an application. Finally, the
497     expression is not a lambda abstraction but a case expression, so all the
498     conditions match. There are no context conditions to match, so the
499     transformation applies.
500
501     By now, the placeholder \lam{E} is bound to the entire expression. The
502     placeholder \lam{x}, which occurs in the replacement template, is not bound
503     yet, so we need to generate a fresh binder for that. Let's use the binder
504     \lam{a}. This results in the following replacement expression:
505
506     \startlambda
507     λa.(case opcode of
508       Low -> (+)
509       High -> (-)) a
510     \stoplambda
511
512     Continuing with this expression, we see that the transformation does not
513     apply again (it is a lambda expression). Next we look at the body of this
514     labmda abstraction:
515
516     \startlambda
517     (case opcode of
518       Low -> (+)
519       High -> (-)) a
520     \stoplambda
521     
522     Here, the transformation does apply, binding \lam{E} to the entire
523     expression and \lam{x} to the fresh binder \lam{b}, resulting in the
524     replacement:
525
526     \startlambda
527     λb.(case opcode of
528       Low -> (+)
529       High -> (-)) a b
530     \stoplambda
531
532     Again, the transformation does not apply to this lambda abstraction, so we
533     look at its body. For brevity, we'll put the case statement on one line from
534     now on.
535
536     \startlambda
537     (case opcode of Low -> (+); High -> (-)) a b
538     \stoplambda
539
540     The type of this expression is \lam{Word}, so it does not match \lam{a -> b}
541     and the transformation does not apply. Next, we have two options for the
542     next expression to look at: The function position and argument position of
543     the application. The expression in the argument position is \lam{b}, which
544     has type \lam{Word}, so the transformation does not apply. The expression in
545     the function position is:
546
547     \startlambda
548     (case opcode of Low -> (+); High -> (-)) a
549     \stoplambda
550
551     Obviously, the transformation does not apply here, since it occurs in
552     function position. In the same way the transformation does not apply to both
553     components of this expression (\lam{case opcode of Low -> (+); High -> (-)}
554     and \lam{a}), so we'll skip to the components of the case expression: The
555     scrutinee and both alternatives. Since the opcode is not a function, it does
556     not apply here, and we'll leave both alternatives as an exercise to the
557     reader. The final function, after all these transformations becomes:
558
559     \startlambda 
560     alu :: Bit -> Word -> Word -> Word
561     alu = λopcode.λa.b. (case opcode of
562       Low -> λa1.λb1 (+) a1 b1
563       High -> λa2.λb2 (-) a2 b2) a b
564     \stoplambda
565
566     In this case, the transformation does not apply anymore, though this might
567     not always be the case (e.g., the application of a transformation on a
568     subexpression might open up possibilities to apply the transformation
569     further up in the expression).
570
571     \subsection{Transformation application}
572       In this chapter we define a number of transformations, but how will we apply
573       these? As stated before, our normal form is reached as soon as no
574       transformation applies anymore. This means our application strategy is to
575       simply apply any transformation that applies, and continuing to do that with
576       the result of each transformation.
577
578       In particular, we define no particular order of transformations. Since
579       transformation order should not influence the resulting normal form (see TODO
580       ref), this leaves the implementation free to choose any application order that
581       results in an efficient implementation.
582
583       When applying a single transformation, we try to apply it to every (sub)expression
584       in a function, not just the top level function. This allows us to keep the
585       transformation descriptions concise and powerful.
586
587     \subsection{Definitions}
588       In the following sections, we will be using a number of functions and
589       notations, which we will define here.
590
591       TODO: Define substitution
592
593       \subsubsection{Other concepts}
594         A \emph{global variable} is any variable that is bound at the
595         top level of a program, or an external module. A \emph{local variable} is any
596         other variable (\eg, variables local to a function, which can be bound by
597         lambda abstractions, let expressions and pattern matches of case
598         alternatives).  Note that this is a slightly different notion of global versus
599         local than what \small{GHC} uses internally.
600         \defref{global variable} \defref{local variable}
601
602         A \emph{hardware representable} (or just \emph{representable}) type or value
603         is (a value of) a type that we can generate a signal for in hardware. For
604         example, a bit, a vector of bits, a 32 bit unsigned word, etc. Types that are
605         not runtime representable notably include (but are not limited to): Types,
606         dictionaries, functions.
607         \defref{representable}
608
609         A \emph{builtin function} is a function supplied by the Cλash framework, whose
610         implementation is not valid Cλash. The implementation is of course valid
611         Haskell, for simulation, but it is not expressable in Cλash.
612         \defref{builtin function} \defref{user-defined function}
613
614       For these functions, Cλash has a \emph{builtin hardware translation}, so calls
615       to these functions can still be translated. These are functions like
616       \lam{map}, \lam{hwor} and \lam{length}.
617
618       A \emph{user-defined} function is a function for which we do have a Cλash
619       implementation available.
620
621       \subsubsection{Functions}
622         Here, we define a number of functions that can be used below to concisely
623         specify conditions.
624
625         \refdef{global variable}\emph{gvar(expr)} is true when \emph{expr} is a variable that references a
626         global variable. It is false when it references a local variable.
627
628         \refdef{local variable}\emph{lvar(expr)} is the complement of \emph{gvar}; it is true when \emph{expr}
629         references a local variable, false when it references a global variable.
630
631         \refdef{representable}\emph{representable(expr)} or \emph{representable(var)} is true when
632         \emph{expr} or \emph{var} is \emph{representable}.
633
634     \subsection{Binder uniqueness}
635       A common problem in transformation systems, is binder uniqueness. When not
636       considering this problem, it is easy to create transformations that mix up
637       bindings and cause name collisions. Take for example, the following core
638       expression:
639
640       \startlambda
641       (λa.λb.λc. a * b * c) x c
642       \stoplambda
643
644       By applying β-reduction (see below) once, we can simplify this expression to:
645
646       \startlambda
647       (λb.λc. x * b * c) c
648       \stoplambda
649
650       Now, we have replaced the \lam{a} binder with a reference to the \lam{x}
651       binder. No harm done here. But note that we see multiple occurences of the
652       \lam{c} binder. The first is a binding occurence, to which the second refers.
653       The last, however refers to \emph{another} instance of \lam{c}, which is
654       bound somewhere outside of this expression. Now, if we would apply beta
655       reduction without taking heed of binder uniqueness, we would get:
656
657       \startlambda
658       λc. x * c * c
659       \stoplambda
660
661       This is obviously not what was supposed to happen! The root of this problem is
662       the reuse of binders: Identical binders can be bound in different scopes, such
663       that only the inner one is \quote{visible} in the inner expression. In the example
664       above, the \lam{c} binder was bound outside of the expression and in the inner
665       lambda expression. Inside that lambda expression, only the inner \lam{c} is
666       visible.
667
668       There are a number of ways to solve this. \small{GHC} has isolated this
669       problem to their binder substitution code, which performs \emph{deshadowing}
670       during its expression traversal. This means that any binding that shadows
671       another binding on a higher level is replaced by a new binder that does not
672       shadow any other binding. This non-shadowing invariant is enough to prevent
673       binder uniqueness problems in \small{GHC}.
674
675       In our transformation system, maintaining this non-shadowing invariant is
676       a bit harder to do (mostly due to implementation issues, the prototype doesn't
677       use \small{GHC}'s subsitution code). Also, we can observe the following
678       points.
679
680       \startitemize
681       \item Deshadowing does not guarantee overall uniqueness. For example, the
682       following (slightly contrived) expression shows the identifier \lam{x} bound in
683       two seperate places (and to different values), even though no shadowing
684       occurs.
685
686       \startlambda
687       (let x = 1 in x) + (let x = 2 in x)
688       \stoplambda
689
690       \item In our normal form (and the resulting \small{VHDL}), all binders
691       (signals) will end up in the same scope. To allow this, all binders within the
692       same function should be unique.
693
694       \item When we know that all binders in an expression are unique, moving around
695       or removing a subexpression will never cause any binder conflicts. If we have
696       some way to generate fresh binders, introducing new subexpressions will not
697       cause any problems either. The only way to cause conflicts is thus to
698       duplicate an existing subexpression.
699       \stopitemize
700
701       Given the above, our prototype maintains a unique binder invariant. This
702       meanst that in any given moment during normalization, all binders \emph{within
703       a single function} must be unique. To achieve this, we apply the following
704       technique.
705
706       TODO: Define fresh binders and unique supplies
707
708       \startitemize
709       \item Before starting normalization, all binders in the function are made
710       unique. This is done by generating a fresh binder for every binder used. This
711       also replaces binders that did not pose any conflict, but it does ensure that
712       all binders within the function are generated by the same unique supply. See
713       (TODO: ref fresh binder).
714       \item Whenever a new binder must be generated, we generate a fresh binder that
715       is guaranteed to be different from \emph{all binders generated so far}. This
716       can thus never introduce duplication and will maintain the invariant.
717       \item Whenever (part of) an expression is duplicated (for example when
718       inlining), all binders in the expression are replaced with fresh binders
719       (using the same method as at the start of normalization). These fresh binders
720       can never introduce duplication, so this will maintain the invariant.
721       \item Whenever we move part of an expression around within the function, there
722       is no need to do anything special. There is obviously no way to introduce
723       duplication by moving expressions around. Since we know that each of the
724       binders is already unique, there is no way to introduce (incorrect) shadowing
725       either.
726       \stopitemize
727
728   \section{Transform passes}
729     In this section we describe the actual transforms. Here we're using
730     the core language in a notation that resembles lambda calculus.
731
732     Each of these transforms is meant to be applied to every (sub)expression
733     in a program, for as long as it applies. Only when none of the
734     transformations can be applied anymore, the program is in normal form (by
735     definition). We hope to be able to prove that this form will obey all of the
736     constraints defined above, but this has yet to happen (though it seems likely
737     that it will).
738
739     Each of the transforms will be described informally first, explaining
740     the need for and goal of the transform. Then, a formal definition is
741     given, using a familiar syntax from the world of logic. Each transform
742     is specified as a number of conditions (above the horizontal line) and a
743     number of conclusions (below the horizontal line). The details of using
744     this notation are still a bit fuzzy, so comments are welcom.
745
746     \subsection{General cleanup}
747       These transformations are general cleanup transformations, that aim to
748       make expressions simpler. These transformations usually clean up the
749        mess left behind by other transformations or clean up expressions to
750        expose new transformation opportunities for other transformations.
751
752        Most of these transformations are standard optimizations in other
753        compilers as well. However, in our compiler, most of these are not just
754        optimizations, but they are required to get our program into normal
755        form.
756
757       \subsubsection{β-reduction}
758         β-reduction is a well known transformation from lambda calculus, where it is
759         the main reduction step. It reduces applications of labmda abstractions,
760         removing both the lambda abstraction and the application.
761
762         In our transformation system, this step helps to remove unwanted lambda
763         abstractions (basically all but the ones at the top level). Other
764         transformations (application propagation, non-representable inlining) make
765         sure that most lambda abstractions will eventually be reducable by
766         β-reduction.
767
768         \starttrans
769         (λx.E) M
770         -----------------
771         E[M/x]
772         \stoptrans
773
774         % And an example
775         \startbuffer[from]
776         (λa. 2 * a) (2 * b)
777         \stopbuffer
778
779         \startbuffer[to]
780         2 * (2 * b)
781         \stopbuffer
782
783         \transexample{β-reduction}{from}{to}
784
785       \subsubsection{Empty let removal}
786         This transformation is simple: It removes recursive lets that have no bindings
787         (which usually occurs when unused let binding removal removes the last
788         binding from it).
789
790         \starttrans
791         letrec in M
792         --------------
793         M
794         \stoptrans
795
796         TODO: Example
797
798       \subsubsection{Simple let binding removal}
799         This transformation inlines simple let bindings (\eg a = b).
800
801         This transformation is not needed to get into normal form, but makes the
802         resulting \small{VHDL} a lot shorter.
803
804         \starttrans
805         letrec
806           a0 = E0
807           \vdots
808           ai = b
809           \vdots
810           an = En
811         in
812           M
813         -----------------------------  \lam{b} is a variable reference
814         letrec
815           a0 = E0 [b/ai]
816           \vdots
817           ai-1 = Ei-1 [b/ai]
818           ai+1 = Ei+1 [b/ai]
819           \vdots
820           an = En [b/ai]
821         in
822           M[b/ai]
823         \stoptrans
824
825         TODO: Example
826
827       \subsubsection{Unused let binding removal}
828         This transformation removes let bindings that are never used. Usually,
829         the desugarer introduces some unused let bindings.
830
831         This normalization pass should really be unneeded to get into normal form
832         (since unused bindings are not forbidden by the normal form), but in practice
833         the desugarer or simplifier emits some unused bindings that cannot be
834         normalized (e.g., calls to a \type{PatError} (TODO: Check this name)). Also,
835         this transformation makes the resulting \small{VHDL} a lot shorter.
836
837         \starttrans
838         letrec
839           a0 = E0
840           \vdots
841           ai = Ei
842           \vdots
843           an = En
844         in
845           M                             \lam{a} does not occur free in \lam{M}
846         ----------------------------    \forall j, 0 <= j <= n, j ≠ i (\lam{a} does not occur free in \lam{Ej})
847         letrec
848           a0 = E0
849           \vdots
850           ai-1 = Ei-1
851           ai+1 = Ei+1
852           \vdots
853           an = En
854         in
855           M
856         \stoptrans
857
858         TODO: Example
859
860       \subsubsection{Cast propagation / simplification}
861         This transform pushes casts down into the expression as far as possible.
862         Since its exact role and need is not clear yet, this transformation is
863         not yet specified.
864
865       \subsubsection{Compiler generated top level binding inlining}
866         TODO
867
868     \subsection{Program structure}
869       These transformations are aimed at normalizing the overall structure
870       into the intended form. This means ensuring there is a lambda abstraction
871       at the top for every argument (input port), putting all of the other
872       value definitions in let bindings and making the final return value a
873       simple variable reference.
874
875       \subsubsection{η-abstraction}
876         This transformation makes sure that all arguments of a function-typed
877         expression are named, by introducing lambda expressions. When combined with
878         β-reduction and non-representable binding inlining, all function-typed
879         expressions should be lambda abstractions or global identifiers.
880
881         \starttrans
882         E                 \lam{E :: a -> b}
883         --------------    \lam{E} is not the first argument of an application.
884         λx.E x            \lam{E} is not a lambda abstraction.
885                           \lam{x} is a variable that does not occur free in \lam{E}.
886         \stoptrans
887
888         \startbuffer[from]
889         foo = λa.case a of 
890           True -> λb.mul b b
891           False -> id
892         \stopbuffer
893
894         \startbuffer[to]
895         foo = λa.λx.(case a of 
896             True -> λb.mul b b
897             False -> λy.id y) x
898         \stopbuffer
899
900         \transexample{η-abstraction}{from}{to}
901
902       \subsubsection{Application propagation}
903         This transformation is meant to propagate application expressions downwards
904         into expressions as far as possible. This allows partial applications inside
905         expressions to become fully applied and exposes new transformation
906         opportunities for other transformations (like β-reduction and
907         specialization).
908
909         \starttrans
910         (letrec binds in E) M
911         -----------------
912         letrec binds in E M
913         \stoptrans
914
915         % And an example
916         \startbuffer[from]
917         ( letrec
918             val = 1
919           in 
920             add val
921         ) 3
922         \stopbuffer
923
924         \startbuffer[to]
925         letrec
926           val = 1
927         in 
928           add val 3
929         \stopbuffer
930
931         \transexample{Application propagation for a let expression}{from}{to}
932
933         \starttrans
934         (case x of
935           p1 -> E1
936           \vdots
937           pn -> En) M
938         -----------------
939         case x of
940           p1 -> E1 M
941           \vdots
942           pn -> En M
943         \stoptrans
944
945         % And an example
946         \startbuffer[from]
947         ( case x of 
948             True -> id
949             False -> neg
950         ) 1
951         \stopbuffer
952
953         \startbuffer[to]
954         case x of 
955           True -> id 1
956           False -> neg 1
957         \stopbuffer
958
959         \transexample{Application propagation for a case expression}{from}{to}
960
961       \subsubsection{Let recursification}
962         This transformation makes all non-recursive lets recursive. In the
963         end, we want a single recursive let in our normalized program, so all
964         non-recursive lets can be converted. This also makes other
965         transformations simpler: They can simply assume all lets are
966         recursive.
967
968         \starttrans
969         let
970           a = E
971         in
972           M
973         ------------------------------------------
974         letrec
975           a = E
976         in
977           M
978         \stoptrans
979
980       \subsubsection{Let flattening}
981         This transformation puts nested lets in the same scope, by lifting the
982         binding(s) of the inner let into a new let around the outer let. Eventually,
983         this will cause all let bindings to appear in the same scope (they will all be
984         in scope for the function return value).
985
986         \starttrans
987         letrec 
988           \vdots
989           x = (letrec bindings in M)
990           \vdots
991         in
992           N
993         ------------------------------------------
994         letrec
995           \vdots
996           bindings
997           x = M
998           \vdots
999         in
1000           N
1001         \stoptrans
1002
1003         \startbuffer[from]
1004         letrec
1005           a = letrec
1006             x = 1
1007             y = 2
1008           in
1009             x + y
1010         in
1011           a
1012         \stopbuffer
1013         \startbuffer[to]
1014         letrec
1015           x = 1
1016           y = 2
1017           a = x + y
1018         in
1019           a
1020         \stopbuffer
1021
1022         \transexample{Let flattening}{from}{to}
1023
1024       \subsubsection{Return value simplification}
1025         This transformation ensures that the return value of a function is always a
1026         simple local variable reference.
1027
1028         Currently implemented using lambda simplification, let simplification, and
1029         top simplification. Should change into something like the following, which
1030         works only on the result of a function instead of any subexpression. This is
1031         achieved by the contexts, like \lam{x = E}, though this is strictly not
1032         correct (you could read this as "if there is any function \lam{x} that binds
1033         \lam{E}, any \lam{E} can be transformed, while we only mean the \lam{E} that
1034         is bound by \lam{x}. This might need some extra notes or something).
1035
1036         Note that the return value is not simplified if its not representable.
1037         Otherwise, this would cause a direct loop with the inlining of
1038         unrepresentable bindings, of course. If the return value is not
1039         representable because it has a function type, η-abstraction should
1040         make sure that this transformation will eventually apply. If the value
1041         is not representable for other reasons, the function result itself is
1042         not representable, meaning this function is not representable anyway!
1043
1044         \starttrans
1045         x = E                            \lam{E} is representable
1046         ~                                \lam{E} is not a lambda abstraction
1047         E                                \lam{E} is not a let expression
1048         ---------------------------      \lam{E} is not a local variable reference
1049         letrec x = E in x
1050         \stoptrans
1051
1052         \starttrans
1053         x = λv0 ... λvn.E
1054         ~                                \lam{E} is representable
1055         E                                \lam{E} is not a let expression
1056         ---------------------------      \lam{E} is not a local variable reference
1057         letrec x = E in x
1058         \stoptrans
1059
1060         \starttrans
1061         x = λv0 ... λvn.let ... in E
1062         ~                                \lam{E} is representable
1063         E                                \lam{E} is not a local variable reference
1064         ---------------------------
1065         letrec x = E in x
1066         \stoptrans
1067
1068         \startbuffer[from]
1069         x = add 1 2
1070         \stopbuffer
1071
1072         \startbuffer[to]
1073         x = letrec x = add 1 2 in x
1074         \stopbuffer
1075
1076         \transexample{Return value simplification}{from}{to}
1077
1078     \subsection{Argument simplification}
1079       The transforms in this section deal with simplifying application
1080       arguments into normal form. The goal here is to:
1081
1082       \startitemize
1083        \item Make all arguments of user-defined functions (\eg, of which
1084        we have a function body) simple variable references of a runtime
1085        representable type. This is needed, since these applications will be turned
1086        into component instantiations.
1087        \item Make all arguments of builtin functions one of:
1088          \startitemize
1089           \item A type argument.
1090           \item A dictionary argument.
1091           \item A type level expression.
1092           \item A variable reference of a runtime representable type.
1093           \item A variable reference or partial application of a function type.
1094          \stopitemize
1095       \stopitemize
1096
1097       When looking at the arguments of a user-defined function, we can
1098       divide them into two categories:
1099       \startitemize
1100         \item Arguments of a runtime representable type (\eg bits or vectors).
1101
1102               These arguments can be preserved in the program, since they can
1103               be translated to input ports later on.  However, since we can
1104               only connect signals to input ports, these arguments must be
1105               reduced to simple variables (for which signals will be
1106               produced). This is taken care of by the argument extraction
1107               transform.
1108         \item Non-runtime representable typed arguments.
1109               
1110               These arguments cannot be preserved in the program, since we
1111               cannot represent them as input or output ports in the resulting
1112               \small{VHDL}. To remove them, we create a specialized version of the
1113               called function with these arguments filled in. This is done by
1114               the argument propagation transform.
1115
1116               Typically, these arguments are type and dictionary arguments that are
1117               used to make functions polymorphic. By propagating these arguments, we
1118               are essentially doing the same which GHC does when it specializes
1119               functions: Creating multiple variants of the same function, one for
1120               each type for which it is used. Other common non-representable
1121               arguments are functions, e.g. when calling a higher order function
1122               with another function or a lambda abstraction as an argument.
1123
1124               The reason for doing this is similar to the reasoning provided for
1125               the inlining of non-representable let bindings above. In fact, this
1126               argument propagation could be viewed as a form of cross-function
1127               inlining.
1128       \stopitemize
1129
1130       TODO: Check the following itemization.
1131
1132       When looking at the arguments of a builtin function, we can divide them
1133       into categories: 
1134
1135       \startitemize
1136         \item Arguments of a runtime representable type.
1137               
1138               As we have seen with user-defined functions, these arguments can
1139               always be reduced to a simple variable reference, by the
1140               argument extraction transform. Performing this transform for
1141               builtin functions as well, means that the translation of builtin
1142               functions can be limited to signal references, instead of
1143               needing to support all possible expressions.
1144
1145         \item Arguments of a function type.
1146               
1147               These arguments are functions passed to higher order builtins,
1148               like \lam{map} and \lam{foldl}. Since implementing these
1149               functions for arbitrary function-typed expressions (\eg, lambda
1150               expressions) is rather comlex, we reduce these arguments to
1151               (partial applications of) global functions.
1152               
1153               We can still support arbitrary expressions from the user code,
1154               by creating a new global function containing that expression.
1155               This way, we can simply replace the argument with a reference to
1156               that new function. However, since the expression can contain any
1157               number of free variables we also have to include partial
1158               applications in our normal form.
1159
1160               This category of arguments is handled by the function extraction
1161               transform.
1162         \item Other unrepresentable arguments.
1163               
1164               These arguments can take a few different forms:
1165               \startdesc{Type arguments}
1166                 In the core language, type arguments can only take a single
1167                 form: A type wrapped in the Type constructor. Also, there is
1168                 nothing that can be done with type expressions, except for
1169                 applying functions to them, so we can simply leave type
1170                 arguments as they are.
1171               \stopdesc
1172               \startdesc{Dictionary arguments}
1173                 In the core language, dictionary arguments are used to find
1174                 operations operating on one of the type arguments (mostly for
1175                 finding class methods). Since we will not actually evaluatie
1176                 the function body for builtin functions and can generate
1177                 code for builtin functions by just looking at the type
1178                 arguments, these arguments can be ignored and left as they
1179                 are.
1180               \stopdesc
1181               \startdesc{Type level arguments}
1182                 Sometimes, we want to pass a value to a builtin function, but
1183                 we need to know the value at compile time. Additionally, the
1184                 value has an impact on the type of the function. This is
1185                 encoded using type-level values, where the actual value of the
1186                 argument is not important, but the type encodes some integer,
1187                 for example. Since the value is not important, the actual form
1188                 of the expression does not matter either and we can leave
1189                 these arguments as they are.
1190               \stopdesc
1191               \startdesc{Other arguments}
1192                 Technically, there is still a wide array of arguments that can
1193                 be passed, but does not fall into any of the above categories.
1194                 However, none of the supported builtin functions requires such
1195                 an argument. This leaves use with passing unsupported types to
1196                 a function, such as calling \lam{head} on a list of functions.
1197
1198                 In these cases, it would be impossible to generate hardware
1199                 for such a function call anyway, so we can ignore these
1200                 arguments.
1201
1202                 The only way to generate hardware for builtin functions with
1203                 arguments like these, is to expand the function call into an
1204                 equivalent core expression (\eg, expand map into a series of
1205                 function applications). But for now, we choose to simply not
1206                 support expressions like these.
1207               \stopdesc
1208
1209               From the above, we can conclude that we can simply ignore these
1210               other unrepresentable arguments and focus on the first two
1211               categories instead.
1212       \stopitemize
1213
1214       \subsubsection{Argument simplification}
1215         This transform deals with arguments to functions that
1216         are of a runtime representable type. It ensures that they will all become
1217         references to global variables, or local signals in the resulting \small{VHDL}. 
1218
1219         TODO: It seems we can map an expression to a port, not only a signal.
1220         Perhaps this makes this transformation not needed?
1221         TODO: Say something about dataconstructors (without arguments, like True
1222         or False), which are variable references of a runtime representable
1223         type, but do not result in a signal.
1224
1225         To reduce a complex expression to a simple variable reference, we create
1226         a new let expression around the application, which binds the complex
1227         expression to a new variable. The original function is then applied to
1228         this variable.
1229
1230         \starttrans
1231         M N
1232         --------------------    \lam{N} is of a representable type
1233         letrec x = N in M x     \lam{N} is not a local variable reference
1234         \stoptrans
1235
1236         \startbuffer[from]
1237         add (add a 1) 1
1238         \stopbuffer
1239
1240         \startbuffer[to]
1241         letrec x = add a 1 in add x 1
1242         \stopbuffer
1243
1244         \transexample{Argument extraction}{from}{to}
1245
1246       \subsubsection{Function extraction}
1247         This transform deals with function-typed arguments to builtin functions.
1248         Since these arguments cannot be propagated, we choose to extract them
1249         into a new global function instead.
1250
1251         Any free variables occuring in the extracted arguments will become
1252         parameters to the new global function. The original argument is replaced
1253         with a reference to the new function, applied to any free variables from
1254         the original argument.
1255
1256         This transformation is useful when applying higher order builtin functions
1257         like \hs{map} to a lambda abstraction, for example. In this case, the code
1258         that generates \small{VHDL} for \hs{map} only needs to handle top level functions and
1259         partial applications, not any other expression (such as lambda abstractions or
1260         even more complicated expressions).
1261
1262         \starttrans
1263         M N                     \lam{M} is a (partial aplication of a) builtin function.
1264         ---------------------   \lam{f0 ... fn} = free local variables of \lam{N}
1265         M (x f0 ... fn)         \lam{N :: a -> b}
1266         ~                       \lam{N} is not a (partial application of) a top level function
1267         x = λf0 ... λfn.N
1268         \stoptrans
1269
1270         \startbuffer[from]
1271         map (λa . add a b) xs
1272
1273         map (add b) ys
1274         \stopbuffer
1275
1276         \startbuffer[to]
1277         map (x0 b) xs
1278
1279         map x1 ys
1280         ~
1281         x0 = λb.λa.add a b
1282         x1 = λb.add b
1283         \stopbuffer
1284
1285         \transexample{Function extraction}{from}{to}
1286
1287         Note that \lam{x0} and {x1} will still need normalization after this.
1288
1289       \subsubsection{Argument propagation}
1290         This transform deals with arguments to user-defined functions that are
1291         not representable at runtime. This means these arguments cannot be
1292         preserved in the final form and most be {\em propagated}.
1293
1294         Propagation means to create a specialized version of the called
1295         function, with the propagated argument already filled in. As a simple
1296         example, in the following program:
1297
1298         \startlambda
1299         f = λa.λb.a + b
1300         inc = λa.f a 1
1301         \stoplambda
1302
1303         We could {\em propagate} the constant argument 1, with the following
1304         result:
1305
1306         \startlambda
1307         f' = λa.a + 1
1308         inc = λa.f' a
1309         \stoplambda
1310
1311         Special care must be taken when the to-be-propagated expression has any
1312         free variables. If this is the case, the original argument should not be
1313         removed alltogether, but replaced by all the free variables of the
1314         expression. In this way, the original expression can still be evaluated
1315         inside the new function. Also, this brings us closer to our goal: All
1316         these free variables will be simple variable references.
1317
1318         To prevent us from propagating the same argument over and over, a simple
1319         local variable reference is not propagated (since is has exactly one
1320         free variable, itself, we would only replace that argument with itself).
1321
1322         This shows that any free local variables that are not runtime representable
1323         cannot be brought into normal form by this transform. We rely on an
1324         inlining transformation to replace such a variable with an expression we
1325         can propagate again.
1326
1327         \starttrans
1328         x = E
1329         ~
1330         x Y0 ... Yi ... Yn                               \lam{Yi} is not of a runtime representable type
1331         ---------------------------------------------    \lam{Yi} is not a local variable reference
1332         x' y0 ... yi-1 f0 ...  fm Yi+1 ... Yn            \lam{f0 ... fm} = free local vars of \lam{Yi}
1333         ~
1334         x' = λy0 ... yi-1 f0 ... fm yi+1 ... yn .       
1335               E y0 ... yi-1 Yi yi+1 ... yn   
1336
1337         \stoptrans
1338
1339         TODO: Example
1340
1341     \subsection{Case simplification}
1342       \subsubsection{Scrutinee simplification}
1343         This transform ensures that the scrutinee of a case expression is always
1344         a simple variable reference.
1345
1346         \starttrans
1347         case E of
1348           alts
1349         -----------------        \lam{E} is not a local variable reference
1350         letrec x = E in 
1351           case E of
1352             alts
1353         \stoptrans
1354
1355         \startbuffer[from]
1356         case (foo a) of
1357           True -> a
1358           False -> b
1359         \stopbuffer
1360
1361         \startbuffer[to]
1362         letrec x = foo a in
1363           case x of
1364             True -> a
1365             False -> b
1366         \stopbuffer
1367
1368         \transexample{Let flattening}{from}{to}
1369
1370
1371       \subsubsection{Case simplification}
1372         This transformation ensures that all case expressions become normal form. This
1373         means they will become one of:
1374         \startitemize
1375         \item An extractor case with a single alternative that picks a single field
1376         from a datatype, \eg \lam{case x of (a, b) -> a}.
1377         \item A selector case with multiple alternatives and only wild binders, that
1378         makes a choice between expressions based on the constructor of another
1379         expression, \eg \lam{case x of Low -> a; High -> b}.
1380         \stopitemize
1381
1382         \starttrans
1383         case E of
1384           C0 v0,0 ... v0,m -> E0
1385           \vdots
1386           Cn vn,0 ... vn,m -> En
1387         --------------------------------------------------- \forall i \forall j, 0 <= i <= n, 0 <= i < m (\lam{wi,j} is a wild (unused) binder)
1388         letrec
1389           v0,0 = case x of C0 v0,0 .. v0,m -> v0,0
1390           \vdots
1391           v0,m = case x of C0 v0,0 .. v0,m -> v0,m
1392           x0 = E0
1393           \dots
1394           vn,m = case x of Cn vn,0 .. vn,m -> vn,m
1395           xn = En
1396         in
1397           case E of
1398             C0 w0,0 ... w0,m -> x0
1399             \vdots
1400             Cn wn,0 ... wn,m -> xn
1401         \stoptrans
1402
1403         TODO: This transformation specified like this is complicated and misses
1404         conditions to prevent looping with itself. Perhaps we should split it here for
1405         discussion?
1406
1407         \startbuffer[from]
1408         case a of
1409           True -> add b 1
1410           False -> add b 2
1411         \stopbuffer
1412
1413         \startbuffer[to]
1414         letnonrec
1415           x0 = add b 1
1416           x1 = add b 2
1417         in
1418           case a of
1419             True -> x0
1420             False -> x1
1421         \stopbuffer
1422
1423         \transexample{Selector case simplification}{from}{to}
1424
1425         \startbuffer[from]
1426         case a of
1427           (,) b c -> add b c
1428         \stopbuffer
1429         \startbuffer[to]
1430         letrec
1431           b = case a of (,) b c -> b
1432           c = case a of (,) b c -> c
1433           x0 = add b c
1434         in
1435           case a of
1436             (,) w0 w1 -> x0
1437         \stopbuffer
1438
1439         \transexample{Extractor case simplification}{from}{to}
1440
1441       \subsubsection{Case removal}
1442         This transform removes any case statements with a single alternative and
1443         only wild binders.
1444
1445         These "useless" case statements are usually leftovers from case simplification
1446         on extractor case (see the previous example).
1447
1448         \starttrans
1449         case x of
1450           C v0 ... vm -> E
1451         ----------------------     \lam{\forall i, 0 <= i <= m} (\lam{vi} does not occur free in E)
1452         E
1453         \stoptrans
1454
1455         \startbuffer[from]
1456         case a of
1457           (,) w0 w1 -> x0
1458         \stopbuffer
1459
1460         \startbuffer[to]
1461         x0
1462         \stopbuffer
1463
1464         \transexample{Case removal}{from}{to}
1465
1466   \subsection{Removing polymorphism}
1467     Reference type-specialization (== argument propagation)
1468
1469     Reference polymporphic binding inlining (== non-representable binding
1470     inlining).
1471
1472   \subsection{Defunctionalization}
1473     These transformations remove most higher order expressions from our
1474     program, making it completely first-order (the only exception here is for
1475     arguments to builtin functions, since we can't specialize builtin
1476     function. TODO: Talk more about this somewhere).
1477
1478     Reference higher-order-specialization (== argument propagation)
1479
1480       \subsubsection{Non-representable binding inlining}
1481         This transform inlines let bindings that have a non-representable type. Since
1482         we can never generate a signal assignment for these bindings (we cannot
1483         declare a signal assignment with a non-representable type, for obvious
1484         reasons), we have no choice but to inline the binding to remove it.
1485
1486         If the binding is non-representable because it is a lambda abstraction, it is
1487         likely that it will inlined into an application and β-reduction will remove
1488         the lambda abstraction and turn it into a representable expression at the
1489         inline site. The same holds for partial applications, which can be turned into
1490         full applications by inlining.
1491
1492         Other cases of non-representable bindings we see in practice are primitive
1493         Haskell types. In most cases, these will not result in a valid normalized
1494         output, but then the input would have been invalid to start with. There is one
1495         exception to this: When a builtin function is applied to a non-representable
1496         expression, things might work out in some cases. For example, when you write a
1497         literal \hs{SizedInt} in Haskell, like \hs{1 :: SizedInt D8}, this results in
1498         the following core: \lam{fromInteger (smallInteger 10)}, where for example
1499         \lam{10 :: GHC.Prim.Int\#} and \lam{smallInteger 10 :: Integer} have
1500         non-representable types. TODO: This/these paragraph(s) should probably become a
1501         separate discussion somewhere else.
1502
1503
1504         \starttrans
1505         letrec 
1506           a0 = E0
1507           \vdots
1508           ai = Ei
1509           \vdots
1510           an = En
1511         in
1512           M
1513         --------------------------    \lam{Ei} has a non-representable type.
1514         letrec
1515           a0 = E0 [Ei/ai]
1516           \vdots
1517           ai-1 = Ei-1 [Ei/ai]
1518           ai+1 = Ei+1 [Ei/ai]
1519           \vdots
1520           an = En [Ei/ai]
1521         in
1522           M[Ei/ai]
1523         \stoptrans
1524
1525         \startbuffer[from]
1526         letrec
1527           a = smallInteger 10
1528           inc = λb -> add b 1
1529           inc' = add 1
1530           x = fromInteger a 
1531         in
1532           inc (inc' x)
1533         \stopbuffer
1534
1535         \startbuffer[to]
1536         letrec
1537           x = fromInteger (smallInteger 10)
1538         in
1539           (λb -> add b 1) (add 1 x)
1540         \stopbuffer
1541
1542         \transexample{None representable binding inlining}{from}{to}
1543
1544
1545   \section{Provable properties}
1546     When looking at the system of transformations outlined above, there are a
1547     number of questions that we can ask ourselves. The main question is of course:
1548     \quote{Does our system work as intended?}. We can split this question into a
1549     number of subquestions:
1550
1551     \startitemize[KR]
1552     \item[q:termination] Does our system \emph{terminate}? Since our system will
1553     keep running as long as transformations apply, there is an obvious risk that
1554     it will keep running indefinitely. One transformation produces a result that
1555     is transformed back to the original by another transformation, for example.
1556     \item[q:soundness] Is our system \emph{sound}? Since our transformations
1557     continuously modify the expression, there is an obvious risk that the final
1558     normal form will not be equivalent to the original program: Its meaning could
1559     have changed.
1560     \item[q:completeness] Is our system \emph{complete}? Since we have a complex
1561     system of transformations, there is an obvious risk that some expressions will
1562     not end up in our intended normal form, because we forgot some transformation.
1563     In other words: Does our transformation system result in our intended normal
1564     form for all possible inputs?
1565     \item[q:determinism] Is our system \emph{deterministic}? Since we have defined
1566     no particular order in which the transformation should be applied, there is an
1567     obvious risk that different transformation orderings will result in
1568     \emph{different} normal forms. They might still both be intended normal forms
1569     (if our system is \emph{complete}) and describe correct hardware (if our
1570     system is \emph{sound}), so this property is less important than the previous
1571     three: The translator would still function properly without it.
1572     \stopitemize
1573
1574     \subsection{Graph representation}
1575       Before looking into how to prove these properties, we'll look at our
1576       transformation system from a graph perspective. The nodes of the graph are
1577       all possible Core expressions. The (directed) edges of the graph are
1578       transformations. When a transformation α applies to an expression \lam{A} to
1579       produce an expression \lam{B}, we add an edge from the node for \lam{A} to the
1580       node for \lam{B}, labeled α.
1581
1582       \startuseMPgraphic{TransformGraph}
1583         save a, b, c, d;
1584
1585         % Nodes
1586         newCircle.a(btex \lam{(λx.λy. (+) x y) 1} etex);
1587         newCircle.b(btex \lam{λy. (+) 1 y} etex);
1588         newCircle.c(btex \lam{(λx.(+) x) 1} etex);
1589         newCircle.d(btex \lam{(+) 1} etex);
1590
1591         b.c = origin;
1592         c.c = b.c + (4cm, 0cm);
1593         a.c = midpoint(b.c, c.c) + (0cm, 4cm);
1594         d.c = midpoint(b.c, c.c) - (0cm, 3cm);
1595
1596         % β-conversion between a and b
1597         ncarc.a(a)(b) "name(bred)";
1598         ObjLabel.a(btex $\xrightarrow[normal]{}{β}$ etex) "labpathname(bred)", "labdir(rt)";
1599         ncarc.b(b)(a) "name(bexp)", "linestyle(dashed withdots)";
1600         ObjLabel.b(btex $\xleftarrow[normal]{}{β}$ etex) "labpathname(bexp)", "labdir(lft)";
1601
1602         % η-conversion between a and c
1603         ncarc.a(a)(c) "name(ered)";
1604         ObjLabel.a(btex $\xrightarrow[normal]{}{η}$ etex) "labpathname(ered)", "labdir(rt)";
1605         ncarc.c(c)(a) "name(eexp)", "linestyle(dashed withdots)";
1606         ObjLabel.c(btex $\xleftarrow[normal]{}{η}$ etex) "labpathname(eexp)", "labdir(lft)";
1607
1608         % η-conversion between b and d
1609         ncarc.b(b)(d) "name(ered)";
1610         ObjLabel.b(btex $\xrightarrow[normal]{}{η}$ etex) "labpathname(ered)", "labdir(rt)";
1611         ncarc.d(d)(b) "name(eexp)", "linestyle(dashed withdots)";
1612         ObjLabel.d(btex $\xleftarrow[normal]{}{η}$ etex) "labpathname(eexp)", "labdir(lft)";
1613
1614         % β-conversion between c and d
1615         ncarc.c(c)(d) "name(bred)";
1616         ObjLabel.c(btex $\xrightarrow[normal]{}{β}$ etex) "labpathname(bred)", "labdir(rt)";
1617         ncarc.d(d)(c) "name(bexp)", "linestyle(dashed withdots)";
1618         ObjLabel.d(btex $\xleftarrow[normal]{}{β}$ etex) "labpathname(bexp)", "labdir(lft)";
1619
1620         % Draw objects and lines
1621         drawObj(a, b, c, d);
1622       \stopuseMPgraphic
1623
1624       \placeexample[right][ex:TransformGraph]{Partial graph of a labmda calculus
1625       system with β and η reduction (solid lines) and expansion (dotted lines).}
1626           \boxedgraphic{TransformGraph}
1627
1628       Of course our graph is unbounded, since we can construct an infinite amount of
1629       Core expressions. Also, there might potentially be multiple edges between two
1630       given nodes (with different labels), though seems unlikely to actually happen
1631       in our system.
1632
1633       See \in{example}[ex:TransformGraph] for the graph representation of a very
1634       simple lambda calculus that contains just the expressions \lam{(λx.λy. (+) x
1635       y) 1}, \lam{λy. (+) 1 y}, \lam{(λx.(+) x) 1} and \lam{(+) 1}. The
1636       transformation system consists of β-reduction and η-reduction (solid edges) or
1637       β-reduction and η-reduction (dotted edges).
1638
1639       TODO: Define β-reduction and η-reduction?
1640
1641       Note that the normal form of such a system consists of the set of nodes
1642       (expressions) without outgoing edges, since those are the expression to which
1643       no transformation applies anymore. We call this set of nodes the \emph{normal
1644       set}.
1645
1646       From such a graph, we can derive some properties easily:
1647       \startitemize[KR]
1648         \item A system will \emph{terminate} if there is no path of infinite length
1649         in the graph (this includes cycles).
1650         \item Soundness is not easily represented in the graph.
1651         \item A system is \emph{complete} if all of the nodes in the normal set have
1652         the intended normal form. The inverse (that all of the nodes outside of
1653         the normal set are \emph{not} in the intended normal form) is not
1654         strictly required.
1655         \item A system is deterministic if all paths from a node, which end in a node
1656         in the normal set, end at the same node.
1657       \stopitemize
1658
1659       When looking at the \in{example}[ex:TransformGraph], we see that the system
1660       terminates for both the reduction and expansion systems (but note that, for
1661       expansion, this is only true because we've limited the possible expressions!
1662       In comlete lambda calculus, there would be a path from \lam{(λx.λy. (+) x y)
1663       1} to \lam{(λx.λy.(λz.(+) z) x y) 1} to \lam{(λx.λy.(λz.(λq.(+) q) z) x y) 1}
1664       etc.)
1665
1666       If we would consider the system with both expansion and reduction, there would
1667       no longer be termination, since there would be cycles all over the place.
1668
1669       The reduction and expansion systems have a normal set of containing just
1670       \lam{(+) 1} or \lam{(λx.λy. (+) x y) 1} respectively. Since all paths in
1671       either system end up in these normal forms, both systems are \emph{complete}.
1672       Also, since there is only one normal form, it must obviously be
1673       \emph{deterministic} as well.
1674
1675     \subsection{Termination}
1676       Approach: Counting.
1677
1678       Church-Rosser?
1679
1680     \subsection{Soundness}
1681       Needs formal definition of semantics.
1682       Prove for each transformation seperately, implies soundness of the system.
1683      
1684     \subsection{Completeness}
1685       Show that any transformation applies to every Core expression that is not
1686       in normal form. To prove: no transformation applies => in intended form.
1687       Show the reverse: Not in intended form => transformation applies.
1688
1689     \subsection{Determinism}
1690       How to prove this?