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