Restructure some of the normalization chapter.
[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         \defref{beta-reduction}
805         β-reduction is a well known transformation from lambda calculus, where it is
806         the main reduction step. It reduces applications of lambda abstractions,
807         removing both the lambda abstraction and the application.
808
809         In our transformation system, this step helps to remove unwanted lambda
810         abstractions (basically all but the ones at the top level). Other
811         transformations (application propagation, non-representable inlining) make
812         sure that most lambda abstractions will eventually be reducable by
813         β-reduction.
814
815         \starttrans
816         (λx.E) M
817         -----------------
818         E[x=>M]
819         \stoptrans
820
821         % And an example
822         \startbuffer[from]
823         (λa. 2 * a) (2 * b)
824         \stopbuffer
825
826         \startbuffer[to]
827         2 * (2 * b)
828         \stopbuffer
829
830         \transexample{beta}{β-reduction}{from}{to}
831
832       \subsubsection{Empty let removal}
833         This transformation is simple: It removes recursive lets that have no bindings
834         (which usually occurs when unused let binding removal removes the last
835         binding from it).
836
837         Note that there is no need to define this transformation for
838         non-recursive lets, since they always contain exactly one binding.
839
840         \starttrans
841         letrec in M
842         --------------
843         M
844         \stoptrans
845
846         \todo{Example}
847
848       \subsubsection{Simple let binding removal}
849         This transformation inlines simple let bindings, that bind some
850         binder to some other binder instead of a more complex expression (\ie
851         a = b).
852
853         This transformation is not needed to get an expression into intended
854         normal form (since these bindings are part of the intended normal
855         form), but makes the resulting \small{VHDL} a lot shorter.
856         
857         \starttrans
858         letrec
859           a0 = E0
860           \vdots
861           ai = b
862           \vdots
863           an = En
864         in
865           M
866         -----------------------------  \lam{b} is a variable reference
867         letrec                         \lam{ai} ≠ \lam{b}
868           a0 = E0 [ai=>b]
869           \vdots
870           ai-1 = Ei-1 [ai=>b]
871           ai+1 = Ei+1 [ai=>b]
872           \vdots
873           an = En [ai=>b]
874         in
875           M[ai=>b]
876         \stoptrans
877
878         \todo{example}
879
880       \subsubsection{Unused let binding removal}
881         This transformation removes let bindings that are never used.
882         Occasionally, \GHC's desugarer introduces some unused let bindings.
883
884         This normalization pass should really be unneeded to get into intended normal form
885         (since unused bindings are not forbidden by the normal form), but in practice
886         the desugarer or simplifier emits some unused bindings that cannot be
887         normalized (e.g., calls to a \type{PatError}\todo{Check this name}). Also,
888         this transformation makes the resulting \small{VHDL} a lot shorter.
889
890         \todo{Don't use old-style numerals in transformations}
891         \starttrans
892         letrec
893           a0 = E0
894           \vdots
895           ai = Ei
896           \vdots
897           an = En
898         in
899           M                             \lam{ai} does not occur free in \lam{M}
900         ----------------------------    \forall j, 0 ≤ j ≤ n, j ≠ i (\lam{ai} does not occur free in \lam{Ej})
901         letrec
902           a0 = E0
903           \vdots
904           ai-1 = Ei-1
905           ai+1 = Ei+1
906           \vdots
907           an = En
908         in
909           M
910         \stoptrans
911
912         \todo{Example}
913
914       \subsubsection{Cast propagation / simplification}
915         This transform pushes casts down into the expression as far as possible.
916         Since its exact role and need is not clear yet, this transformation is
917         not yet specified.
918
919         \todo{Cast propagation}
920
921       \subsubsection{Top level binding inlining}
922         This transform takes simple top level bindings generated by the
923         \small{GHC} compiler. \small{GHC} sometimes generates very simple
924         \quote{wrapper} bindings, which are bound to just a variable
925         reference, or a partial application to constants or other variable
926         references.
927
928         Note that this transformation is completely optional. It is not
929         required to get any function into intended normal form, but it does help making
930         the resulting VHDL output easier to read (since it removes a bunch of
931         components that are really boring).
932
933         This transform takes any top level binding generated by the compiler,
934         whose normalized form contains only a single let binding.
935
936         \starttrans
937         x = λa0 ... λan.let y = E in y
938         ~
939         x
940         --------------------------------------         \lam{x} is generated by the compiler
941         λa0 ... λan.let y = E in y
942         \stoptrans
943
944         \startbuffer[from]
945         (+) :: Word -> Word -> Word
946         (+) = GHC.Num.(+) @Word $dNum
947         ~
948         (+) a b
949         \stopbuffer
950         \startbuffer[to]
951         GHC.Num.(+) @ Alu.Word $dNum a b
952         \stopbuffer
953
954         \transexample{toplevelinline}{Top level binding inlining}{from}{to}
955        
956         \in{Example}[ex:trans:toplevelinline] shows a typical application of
957         the addition operator generated by \GHC. The type and dictionary
958         arguments used here are described in
959         \in{Section}[section:prototype:polymorphism].
960
961         Without this transformation, there would be a \lam{(+)} entity
962         in the \VHDL which would just add its inputs. This generates a
963         lot of overhead in the \VHDL, which is particularly annoying
964         when browsing the generated RTL schematic (especially since most
965         non-alphanumerics, like all characters in \lam{(+)}, are not
966         allowed in \VHDL architecture names\footnote{Technically, it is
967         allowed to use non-alphanumerics when using extended
968         identifiers, but it seems that none of the tooling likes
969         extended identifiers in filenames, so it effectively doesn't
970         work.}, so the entity would be called \quote{w7aA7f} or
971         something similarly unreadable and autogenerated).
972
973     \subsection{Program structure}
974       These transformations are aimed at normalizing the overall structure
975       into the intended form. This means ensuring there is a lambda abstraction
976       at the top for every argument (input port or current state), putting all
977       of the other value definitions in let bindings and making the final
978       return value a simple variable reference.
979
980       \subsubsection{η-abstraction}
981         This transformation makes sure that all arguments of a function-typed
982         expression are named, by introducing lambda expressions. When combined with
983         β-reduction and non-representable binding inlining, all function-typed
984         expressions should be lambda abstractions or global identifiers.
985
986         \starttrans
987         E                 \lam{E :: a -> b}
988         --------------    \lam{E} is not the first argument of an application.
989         λx.E x            \lam{E} is not a lambda abstraction.
990                           \lam{x} is a variable that does not occur free in \lam{E}.
991         \stoptrans
992
993         \startbuffer[from]
994         foo = λa.case a of 
995           True -> λb.mul b b
996           False -> id
997         \stopbuffer
998
999         \startbuffer[to]
1000         foo = λa.λx.(case a of 
1001             True -> λb.mul b b
1002             False -> λy.id y) x
1003         \stopbuffer
1004
1005         \transexample{eta}{η-abstraction}{from}{to}
1006
1007       \subsubsection{Application propagation}
1008         This transformation is meant to propagate application expressions downwards
1009         into expressions as far as possible. This allows partial applications inside
1010         expressions to become fully applied and exposes new transformation
1011         opportunities for other transformations (like β-reduction and
1012         specialization).
1013
1014         Since all binders in our expression are unique (see
1015         \in{section}[sec:normalization:uniq]), there is no risk that we will
1016         introduce unintended shadowing by moving an expression into a lower
1017         scope. Also, since only move expression into smaller scopes (down into
1018         our expression), there is no risk of moving a variable reference out
1019         of the scope in which it is defined.
1020
1021         \starttrans
1022         (letrec binds in E) M
1023         ------------------------
1024         letrec binds in E M
1025         \stoptrans
1026
1027         % And an example
1028         \startbuffer[from]
1029         ( letrec
1030             val = 1
1031           in 
1032             add val
1033         ) 3
1034         \stopbuffer
1035
1036         \startbuffer[to]
1037         letrec
1038           val = 1
1039         in 
1040           add val 3
1041         \stopbuffer
1042
1043         \transexample{appproplet}{Application propagation for a let expression}{from}{to}
1044
1045         \starttrans
1046         (case x of
1047           p1 -> E1
1048           \vdots
1049           pn -> En) M
1050         -----------------
1051         case x of
1052           p1 -> E1 M
1053           \vdots
1054           pn -> En M
1055         \stoptrans
1056
1057         % And an example
1058         \startbuffer[from]
1059         ( case x of 
1060             True -> id
1061             False -> neg
1062         ) 1
1063         \stopbuffer
1064
1065         \startbuffer[to]
1066         case x of 
1067           True -> id 1
1068           False -> neg 1
1069         \stopbuffer
1070
1071         \transexample{apppropcase}{Application propagation for a case expression}{from}{to}
1072
1073       \subsubsection{Let recursification}
1074         This transformation makes all non-recursive lets recursive. In the
1075         end, we want a single recursive let in our normalized program, so all
1076         non-recursive lets can be converted. This also makes other
1077         transformations simpler: They can simply assume all lets are
1078         recursive.
1079
1080         \starttrans
1081         let
1082           a = E
1083         in
1084           M
1085         ------------------------------------------
1086         letrec
1087           a = E
1088         in
1089           M
1090         \stoptrans
1091
1092       \subsubsection{Let flattening}
1093         This transformation puts nested lets in the same scope, by lifting the
1094         binding(s) of the inner let into the outer let. Eventually, this will
1095         cause all let bindings to appear in the same scope.
1096
1097         This transformation only applies to recursive lets, since all
1098         non-recursive lets will be made recursive (see
1099         \in{section}[sec:normalization:letrecurse]).
1100
1101         Since we are joining two scopes together, there is no risk of moving a
1102         variable reference out of the scope where it is defined.
1103
1104         \starttrans
1105         letrec 
1106           a0 = E0
1107           \vdots
1108           ai = (letrec bindings in M)
1109           \vdots
1110           an = En
1111         in
1112           N
1113         ------------------------------------------
1114         letrec
1115           a0 = E0
1116           \vdots
1117           ai = M
1118           \vdots
1119           an = En
1120           bindings
1121         in
1122           N
1123         \stoptrans
1124
1125         \startbuffer[from]
1126         letrec
1127           a = 1
1128           b = letrec
1129             x = a
1130             y = c
1131           in
1132             x + y
1133           c = 2
1134         in
1135           b
1136         \stopbuffer
1137         \startbuffer[to]
1138         letrec
1139           a = 1
1140           b = x + y
1141           c = 2
1142           x = a
1143           y = c
1144         in
1145           b
1146         \stopbuffer
1147
1148         \transexample{letflat}{Let flattening}{from}{to}
1149
1150       \subsubsection{Return value simplification}
1151         This transformation ensures that the return value of a function is always a
1152         simple local variable reference.
1153
1154         Currently implemented using lambda simplification, let simplification, and
1155         top simplification. Should change into something like the following, which
1156         works only on the result of a function instead of any subexpression. This is
1157         achieved by the contexts, like \lam{x = E}, though this is strictly not
1158         correct (you could read this as "if there is any function \lam{x} that binds
1159         \lam{E}, any \lam{E} can be transformed, while we only mean the \lam{E} that
1160         is bound by \lam{x}. This might need some extra notes or something).
1161
1162         Note that the return value is not simplified if its not representable.
1163         Otherwise, this would cause a direct loop with the inlining of
1164         unrepresentable bindings. If the return value is not
1165         representable because it has a function type, η-abstraction should
1166         make sure that this transformation will eventually apply. If the value
1167         is not representable for other reasons, the function result itself is
1168         not representable, meaning this function is not translatable anyway.
1169
1170         \starttrans
1171         x = E                            \lam{E} is representable
1172         ~                                \lam{E} is not a lambda abstraction
1173         E                                \lam{E} is not a let expression
1174         ---------------------------      \lam{E} is not a local variable reference
1175         letrec x = E in x
1176         \stoptrans
1177
1178         \starttrans
1179         x = λv0 ... λvn.E
1180         ~                                \lam{E} is representable
1181         E                                \lam{E} is not a let expression
1182         ---------------------------      \lam{E} is not a local variable reference
1183         letrec x = E in x
1184         \stoptrans
1185
1186         \starttrans
1187         x = λv0 ... λvn.let ... in E
1188         ~                                \lam{E} is representable
1189         E                                \lam{E} is not a local variable reference
1190         -----------------------------
1191         letrec x = E in x
1192         \stoptrans
1193
1194         \startbuffer[from]
1195         x = add 1 2
1196         \stopbuffer
1197
1198         \startbuffer[to]
1199         x = letrec x = add 1 2 in x
1200         \stopbuffer
1201
1202         \transexample{retvalsimpl}{Return value simplification}{from}{to}
1203         
1204         \todo{More examples}
1205
1206     \subsection{Argument simplification}
1207       The transforms in this section deal with simplifying application
1208       arguments into normal form. The goal here is to:
1209
1210       \todo{This section should only talk about representable arguments. Non
1211       representable arguments are treated by specialization.}
1212
1213       \startitemize
1214        \item Make all arguments of user-defined functions (\eg, of which
1215        we have a function body) simple variable references of a runtime
1216        representable type. This is needed, since these applications will be turned
1217        into component instantiations.
1218        \item Make all arguments of builtin functions one of:
1219          \startitemize
1220           \item A type argument.
1221           \item A dictionary argument.
1222           \item A type level expression.
1223           \item A variable reference of a runtime representable type.
1224           \item A variable reference or partial application of a function type.
1225          \stopitemize
1226       \stopitemize
1227
1228       When looking at the arguments of a user-defined function, we can
1229       divide them into two categories:
1230       \startitemize
1231         \item Arguments of a runtime representable type (\eg bits or vectors).
1232
1233               These arguments can be preserved in the program, since they can
1234               be translated to input ports later on.  However, since we can
1235               only connect signals to input ports, these arguments must be
1236               reduced to simple variables (for which signals will be
1237               produced). This is taken care of by the argument extraction
1238               transform.
1239         \item Non-runtime representable typed arguments. \todo{Move this
1240         bullet to specialization}
1241               
1242               These arguments cannot be preserved in the program, since we
1243               cannot represent them as input or output ports in the resulting
1244               \small{VHDL}. To remove them, we create a specialized version of the
1245               called function with these arguments filled in. This is done by
1246               the argument propagation transform.
1247
1248               Typically, these arguments are type and dictionary arguments that are
1249               used to make functions polymorphic. By propagating these arguments, we
1250               are essentially doing the same which GHC does when it specializes
1251               functions: Creating multiple variants of the same function, one for
1252               each type for which it is used. Other common non-representable
1253               arguments are functions, e.g. when calling a higher order function
1254               with another function or a lambda abstraction as an argument.
1255
1256               The reason for doing this is similar to the reasoning provided for
1257               the inlining of non-representable let bindings above. In fact, this
1258               argument propagation could be viewed as a form of cross-function
1259               inlining.
1260       \stopitemize
1261
1262       \todo{Move this itemization into a new section about builtin functions}
1263       When looking at the arguments of a builtin function, we can divide them
1264       into categories: 
1265
1266       \startitemize
1267         \item Arguments of a runtime representable type.
1268               
1269               As we have seen with user-defined functions, these arguments can
1270               always be reduced to a simple variable reference, by the
1271               argument extraction transform. Performing this transform for
1272               builtin functions as well, means that the translation of builtin
1273               functions can be limited to signal references, instead of
1274               needing to support all possible expressions.
1275
1276         \item Arguments of a function type.
1277               
1278               These arguments are functions passed to higher order builtins,
1279               like \lam{map} and \lam{foldl}. Since implementing these
1280               functions for arbitrary function-typed expressions (\eg, lambda
1281               expressions) is rather comlex, we reduce these arguments to
1282               (partial applications of) global functions.
1283               
1284               We can still support arbitrary expressions from the user code,
1285               by creating a new global function containing that expression.
1286               This way, we can simply replace the argument with a reference to
1287               that new function. However, since the expression can contain any
1288               number of free variables we also have to include partial
1289               applications in our normal form.
1290
1291               This category of arguments is handled by the function extraction
1292               transform.
1293         \item Other unrepresentable arguments.
1294               
1295               These arguments can take a few different forms:
1296               \startdesc{Type arguments}
1297                 In the core language, type arguments can only take a single
1298                 form: A type wrapped in the Type constructor. Also, there is
1299                 nothing that can be done with type expressions, except for
1300                 applying functions to them, so we can simply leave type
1301                 arguments as they are.
1302               \stopdesc
1303               \startdesc{Dictionary arguments}
1304                 In the core language, dictionary arguments are used to find
1305                 operations operating on one of the type arguments (mostly for
1306                 finding class methods). Since we will not actually evaluatie
1307                 the function body for builtin functions and can generate
1308                 code for builtin functions by just looking at the type
1309                 arguments, these arguments can be ignored and left as they
1310                 are.
1311               \stopdesc
1312               \startdesc{Type level arguments}
1313                 Sometimes, we want to pass a value to a builtin function, but
1314                 we need to know the value at compile time. Additionally, the
1315                 value has an impact on the type of the function. This is
1316                 encoded using type-level values, where the actual value of the
1317                 argument is not important, but the type encodes some integer,
1318                 for example. Since the value is not important, the actual form
1319                 of the expression does not matter either and we can leave
1320                 these arguments as they are.
1321               \stopdesc
1322               \startdesc{Other arguments}
1323                 Technically, there is still a wide array of arguments that can
1324                 be passed, but does not fall into any of the above categories.
1325                 However, none of the supported builtin functions requires such
1326                 an argument. This leaves use with passing unsupported types to
1327                 a function, such as calling \lam{head} on a list of functions.
1328
1329                 In these cases, it would be impossible to generate hardware
1330                 for such a function call anyway, so we can ignore these
1331                 arguments.
1332
1333                 The only way to generate hardware for builtin functions with
1334                 arguments like these, is to expand the function call into an
1335                 equivalent core expression (\eg, expand map into a series of
1336                 function applications). But for now, we choose to simply not
1337                 support expressions like these.
1338               \stopdesc
1339
1340               From the above, we can conclude that we can simply ignore these
1341               other unrepresentable arguments and focus on the first two
1342               categories instead.
1343       \stopitemize
1344
1345       \subsubsection{Argument simplification}
1346         This transform deals with arguments to functions that
1347         are of a runtime representable type. It ensures that they will all become
1348         references to global variables, or local signals in the resulting
1349         \small{VHDL}, which is required due to limitations in the component
1350         instantiation code in \VHDL (one can only assign a signal or constant
1351         to an input port). By ensuring that all arguments are always simple
1352         variable references, we always have a signal available to assign to
1353         input ports.
1354
1355         \todo{Say something about dataconstructors (without arguments, like True
1356         or False), which are variable references of a runtime representable
1357         type, but do not result in a signal.}
1358
1359         To reduce a complex expression to a simple variable reference, we create
1360         a new let expression around the application, which binds the complex
1361         expression to a new variable. The original function is then applied to
1362         this variable.
1363
1364         Note that a reference to a \emph{global variable} (like a top level
1365         function without arguments, but also an argumentless dataconstructors
1366         like \lam{True}) is also simplified. Only local variables generate
1367         signals in the resulting architecture. 
1368
1369         \refdef{representable}
1370         \starttrans
1371         M N
1372         --------------------    \lam{N} is representable
1373         letrec x = N in M x     \lam{N} is not a local variable reference
1374         \stoptrans
1375         \refdef{local variable}
1376
1377         \startbuffer[from]
1378         add (add a 1) 1
1379         \stopbuffer
1380
1381         \startbuffer[to]
1382         letrec x = add a 1 in add x 1
1383         \stopbuffer
1384
1385         \transexample{argextract}{Argument extraction}{from}{to}
1386       
1387       \subsubsection[sec:normalization:funextract]{Function extraction}
1388         \todo{Move to section about builtin functions}
1389         This transform deals with function-typed arguments to builtin
1390         functions.  Since builtin functions cannot be specialized to remove
1391         the arguments, we choose to extract these arguments into a new global
1392         function instead. This greatly simplifies the translation rules needed
1393         for builtin functions. \todo{Should we talk about these? Reference
1394         Christiaan?}
1395
1396         Any free variables occuring in the extracted arguments will become
1397         parameters to the new global function. The original argument is replaced
1398         with a reference to the new function, applied to any free variables from
1399         the original argument.
1400
1401         This transformation is useful when applying higher order builtin functions
1402         like \hs{map} to a lambda abstraction, for example. In this case, the code
1403         that generates \small{VHDL} for \hs{map} only needs to handle top level functions and
1404         partial applications, not any other expression (such as lambda abstractions or
1405         even more complicated expressions).
1406
1407         \starttrans
1408         M N                     \lam{M} is (a partial aplication of) a builtin function.
1409         ---------------------   \lam{f0 ... fn} are all free local variables of \lam{N}
1410         M (x f0 ... fn)         \lam{N :: a -> b}
1411         ~                       \lam{N} is not a (partial application of) a top level function
1412         x = λf0 ... λfn.N
1413         \stoptrans
1414
1415         \todo{Split this example}
1416         \startbuffer[from]
1417         map (λa . add a b) xs
1418
1419         map (add b) ys
1420         \stopbuffer
1421
1422         \startbuffer[to]
1423         map (x0 b) xs
1424
1425         map x1 ys
1426         ~
1427         x0 = λb.λa.add a b
1428         x1 = λb.add b
1429         \stopbuffer
1430
1431         \transexample{funextract}{Function extraction}{from}{to}
1432
1433         Note that \lam{x0} and {x1} will still need normalization after this.
1434
1435       \todo{Fill the gap left by moving argument propagation away}
1436
1437     \subsection{Case normalisation}
1438       \subsubsection{Scrutinee simplification}
1439         This transform ensures that the scrutinee of a case expression is always
1440         a simple variable reference.
1441
1442         \starttrans
1443         case E of
1444           alts
1445         -----------------        \lam{E} is not a local variable reference
1446         letrec x = E in 
1447           case E of
1448             alts
1449         \stoptrans
1450
1451         \startbuffer[from]
1452         case (foo a) of
1453           True -> a
1454           False -> b
1455         \stopbuffer
1456
1457         \startbuffer[to]
1458         letrec x = foo a in
1459           case x of
1460             True -> a
1461             False -> b
1462         \stopbuffer
1463
1464         \transexample{letflat}{Let flattening}{from}{to}
1465
1466
1467       \subsubsection{Case simplification}
1468         This transformation ensures that all case expressions become normal form. This
1469         means they will become one of:
1470         \startitemize
1471         \item An extractor case with a single alternative that picks a single field
1472         from a datatype, \eg \lam{case x of (a, b) -> a}.
1473         \item A selector case with multiple alternatives and only wild binders, that
1474         makes a choice between expressions based on the constructor of another
1475         expression, \eg \lam{case x of Low -> a; High -> b}.
1476         \stopitemize
1477         
1478         \defref{wild binder}
1479         \starttrans
1480         case E of
1481           C0 v0,0 ... v0,m -> E0
1482           \vdots
1483           Cn vn,0 ... vn,m -> En
1484         --------------------------------------------------- \forall i \forall j, 0 ≤ i ≤ n, 0 ≤ i < m (\lam{wi,j} is a wild (unused) binder)
1485         letrec
1486           v0,0 = case E of C0 v0,0 .. v0,m -> v0,0
1487           \vdots
1488           v0,m = case E of C0 v0,0 .. v0,m -> v0,m
1489           \vdots
1490           vn,m = case E of Cn vn,0 .. vn,m -> vn,m
1491           x0 = E0
1492           \vdots
1493           xn = En
1494         in
1495           case E of
1496             C0 w0,0 ... w0,m -> x0
1497             \vdots
1498             Cn wn,0 ... wn,m -> xn
1499         \stoptrans
1500         \todo{Check the subscripts of this transformation}
1501
1502         Note that this transformation applies to case statements with any
1503         scrutinee. If the scrutinee is a complex expression, this might result
1504         in duplicate hardware. An extra condition to only apply this
1505         transformation when the scrutinee is already simple (effectively
1506         causing this transformation to be only applied after the scrutinee
1507         simplification transformation) might be in order. 
1508
1509         \fxnote{This transformation specified like this is complicated and misses
1510         conditions to prevent looping with itself. Perhaps it should be split here for
1511         discussion?}
1512
1513         \startbuffer[from]
1514         case a of
1515           True -> add b 1
1516           False -> add b 2
1517         \stopbuffer
1518
1519         \startbuffer[to]
1520         letnonrec
1521           x0 = add b 1
1522           x1 = add b 2
1523         in
1524           case a of
1525             True -> x0
1526             False -> x1
1527         \stopbuffer
1528
1529         \transexample{selcasesimpl}{Selector case simplification}{from}{to}
1530
1531         \startbuffer[from]
1532         case a of
1533           (,) b c -> add b c
1534         \stopbuffer
1535         \startbuffer[to]
1536         letrec
1537           b = case a of (,) b c -> b
1538           c = case a of (,) b c -> c
1539           x0 = add b c
1540         in
1541           case a of
1542             (,) w0 w1 -> x0
1543         \stopbuffer
1544
1545         \transexample{excasesimpl}{Extractor case simplification}{from}{to}
1546
1547         \refdef{selector case}
1548         In \in{example}[ex:trans:excasesimpl] the case expression is expanded
1549         into multiple case expressions, including a pretty useless expression
1550         (that is neither a selector or extractor case). This case can be
1551         removed by the Case removal transformation in
1552         \in{section}[sec:transformation:caseremoval].
1553
1554       \subsubsection[sec:transformation:caseremoval]{Case removal}
1555         This transform removes any case statements with a single alternative and
1556         only wild binders.
1557
1558         These "useless" case statements are usually leftovers from case simplification
1559         on extractor case (see the previous example).
1560
1561         \starttrans
1562         case x of
1563           C v0 ... vm -> E
1564         ----------------------     \lam{\forall i, 0 ≤ i ≤ m} (\lam{vi} does not occur free in E)
1565         E
1566         \stoptrans
1567
1568         \startbuffer[from]
1569         case a of
1570           (,) w0 w1 -> x0
1571         \stopbuffer
1572
1573         \startbuffer[to]
1574         x0
1575         \stopbuffer
1576
1577         \transexample{caserem}{Case removal}{from}{to}
1578
1579     \subsection{Removing unrepresentable values}
1580       The transformations in this section are aimed at making all the
1581       values used in our expression representable. There are two main
1582       transformations that are applied to \emph{all} unrepresentable let
1583       bindings and function arguments, but these are really meant to
1584       address three different kinds of unrepresentable values:
1585       Polymorphic values, higher order values and literals. Each of these
1586       will be detailed below, followed by the actual transformations.
1587
1588       \subsubsection{Removing Polymorphism}
1589         As noted in \in{section}[sec:prototype:polymporphism],
1590         polymorphism is made explicit in Core through type and
1591         dictionary arguments. To remove the polymorphism from a
1592         function, we can simply specialize the polymorphic function for
1593         the particular type applied to it. The same goes for dictionary
1594         arguments. To remove polymorphism from let bound values, we
1595         simply inline the let bindings that have a polymorphic type,
1596         which should (eventually) make sure that the polymorphic
1597         expression is applied to a type and/or dictionary, which can
1598         \refdef{beta-reduction}
1599         then be removed by β-reduction.
1600
1601         Since both type and dictionary arguments are not representable,
1602         \refdef{representable}
1603         the non-representable argument specialization and
1604         non-representable let binding inlining transformations below
1605         take care of exactly this.
1606
1607         There is one case where polymorphism cannot be completely
1608         removed: Builtin functions are still allowed to be polymorphic
1609         (Since we have no function body that we could properly
1610         specialize). However, the code that generates \VHDL for builtin
1611         functions knows how to handle this, so this is not a problem.
1612
1613       \subsubsection{Defunctionalization}
1614         These transformations remove higher order expressions from our
1615         program, making all values first-order.
1616
1617         \todo{Finish this section}
1618         
1619         There is one case where higher order values cannot be completely
1620         removed: Builtin functions are still allowed to have higher
1621         order arguments (Since we have no function body that we could
1622         properly specialize). These are limited to (partial applications
1623         of) top level functions, however, which is handled by the
1624         top-level function extraction (see
1625         \in{section}[sec:normalization:funextract]). However, the code
1626         that generates \VHDL for builtin functions knows how to handle
1627         these, so this is not a problem.
1628
1629       \subsubsection{Literals}
1630         \todo{Fill this section}
1631
1632       \subsubsection{Non-representable binding inlining}
1633         \todo{Move this section into a new section (together with
1634         specialization?)}
1635         This transform inlines let bindings that are bound to a
1636         non-representable value. Since we can never generate a signal
1637         assignment for these bindings (we cannot declare a signal assignment
1638         with a non-representable type, for obvious reasons), we have no choice
1639         but to inline the binding to remove it.
1640
1641         If the binding is non-representable because it is a lambda abstraction, it is
1642         likely that it will inlined into an application and β-reduction will remove
1643         the lambda abstraction and turn it into a representable expression at the
1644         inline site. The same holds for partial applications, which can be turned into
1645         full applications by inlining.
1646
1647         Other cases of non-representable bindings we see in practice are primitive
1648         Haskell types. In most cases, these will not result in a valid normalized
1649         output, but then the input would have been invalid to start with. There is one
1650         exception to this: When a builtin function is applied to a non-representable
1651         expression, things might work out in some cases. For example, when you write a
1652         literal \hs{SizedInt} in Haskell, like \hs{1 :: SizedInt D8}, this results in
1653         the following core: \lam{fromInteger (smallInteger 10)}, where for example
1654         \lam{10 :: GHC.Prim.Int\#} and \lam{smallInteger 10 :: Integer} have
1655         non-representable types. \todo{Expand on this. This/these paragraph(s)
1656         should probably become a separate discussion somewhere else}
1657
1658         \todo{Can this duplicate work? -- For polymorphism probably, for
1659         higher order expressions only if they are inlined before they
1660         are themselves normalized.}
1661
1662         \starttrans
1663         letrec 
1664           a0 = E0
1665           \vdots
1666           ai = Ei
1667           \vdots
1668           an = En
1669         in
1670           M
1671         --------------------------    \lam{Ei} has a non-representable type.
1672         letrec
1673           a0 = E0 [ai=>Ei] \vdots
1674           ai-1 = Ei-1 [ai=>Ei]
1675           ai+1 = Ei+1 [ai=>Ei]
1676           \vdots
1677           an = En [ai=>Ei]
1678         in
1679           M[ai=>Ei]
1680         \stoptrans
1681
1682         \startbuffer[from]
1683         letrec
1684           a = smallInteger 10
1685           inc = λb -> add b 1
1686           inc' = add 1
1687           x = fromInteger a 
1688         in
1689           inc (inc' x)
1690         \stopbuffer
1691
1692         \startbuffer[to]
1693         letrec
1694           x = fromInteger (smallInteger 10)
1695         in
1696           (λb -> add b 1) (add 1 x)
1697         \stopbuffer
1698
1699         \transexample{nonrepinline}{Nonrepresentable binding inlining}{from}{to}
1700
1701       \subsubsection{Argument propagation}
1702         \todo{Rename this section to specialization}
1703
1704         This transform deals with arguments to user-defined functions that are
1705         not representable at runtime. This means these arguments cannot be
1706         preserved in the final form and most be {\em propagated}.
1707
1708         Propagation means to create a specialized version of the called
1709         function, with the propagated argument already filled in. As a simple
1710         example, in the following program:
1711
1712         \startlambda
1713         f = λa.λb.a + b
1714         inc = λa.f a 1
1715         \stoplambda
1716
1717         We could {\em propagate} the constant argument 1, with the following
1718         result:
1719
1720         \startlambda
1721         f' = λa.a + 1
1722         inc = λa.f' a
1723         \stoplambda
1724
1725         Special care must be taken when the to-be-propagated expression has any
1726         free variables. If this is the case, the original argument should not be
1727         removed completely, but replaced by all the free variables of the
1728         expression. In this way, the original expression can still be evaluated
1729         inside the new function. Also, this brings us closer to our goal: All
1730         these free variables will be simple variable references.
1731
1732         To prevent us from propagating the same argument over and over, a simple
1733         local variable reference is not propagated (since is has exactly one
1734         free variable, itself, we would only replace that argument with itself).
1735
1736         This shows that any free local variables that are not runtime representable
1737         cannot be brought into normal form by this transform. We rely on an
1738         inlining transformation to replace such a variable with an expression we
1739         can propagate again.
1740
1741         \starttrans
1742         x = E
1743         ~
1744         x Y0 ... Yi ... Yn                               \lam{Yi} is not of a runtime representable type
1745         ---------------------------------------------    \lam{Yi} is not a local variable reference
1746         x' y0 ... yi-1 f0 ...  fm Yi+1 ... Yn            \lam{f0 ... fm} are all free local vars of \lam{Yi}
1747         ~
1748         x' = λy0 ... λyi-1. λf0 ... λfm. λyi+1 ... λyn .       
1749               E y0 ... yi-1 Yi yi+1 ... yn   
1750         \stoptrans
1751
1752         \todo{Describe what the formal specification means}
1753         \todo{Note that we don't change the sepcialised function body, only
1754         wrap it}
1755         \todo{This does not take care of updating the types of y0 ...
1756         yn. The code uses the types of Y0 ... Yn for this, regardless of
1757         whether the type arguments were properly propagated...}
1758
1759         \todo{Example}
1760
1761
1762
1763
1764   \section[sec:normalization:properties]{Provable properties}
1765     When looking at the system of transformations outlined above, there are a
1766     number of questions that we can ask ourselves. The main question is of course:
1767     \quote{Does our system work as intended?}. We can split this question into a
1768     number of subquestions:
1769
1770     \startitemize[KR]
1771     \item[q:termination] Does our system \emph{terminate}? Since our system will
1772     keep running as long as transformations apply, there is an obvious risk that
1773     it will keep running indefinitely. This typically happens when one
1774     transformation produces a result that is transformed back to the original
1775     by another transformation, or when one or more transformations keep
1776     expanding some expression.
1777     \item[q:soundness] Is our system \emph{sound}? Since our transformations
1778     continuously modify the expression, there is an obvious risk that the final
1779     normal form will not be equivalent to the original program: Its meaning could
1780     have changed.
1781     \item[q:completeness] Is our system \emph{complete}? Since we have a complex
1782     system of transformations, there is an obvious risk that some expressions will
1783     not end up in our intended normal form, because we forgot some transformation.
1784     In other words: Does our transformation system result in our intended normal
1785     form for all possible inputs?
1786     \item[q:determinism] Is our system \emph{deterministic}? Since we have defined
1787     no particular order in which the transformation should be applied, there is an
1788     obvious risk that different transformation orderings will result in
1789     \emph{different} normal forms. They might still both be intended normal forms
1790     (if our system is \emph{complete}) and describe correct hardware (if our
1791     system is \emph{sound}), so this property is less important than the previous
1792     three: The translator would still function properly without it.
1793     \stopitemize
1794
1795     Unfortunately, the final transformation system has only been
1796     developed in the final part of the research, leaving no more time
1797     for verifying these properties. In fact, it is likely that the
1798     current transformation system still violates some of these
1799     properties in some cases and should be improved (or extra conditions
1800     on the input hardware descriptions should be formulated).
1801
1802     This is most likely the case with the completeness and determinism
1803     properties, perhaps als the termination property. The soundness
1804     property probably holds, since it is easier to manually verify (each
1805     transformation can be reviewed separately).
1806
1807     Even though no complete proofs have been made, some ideas for
1808     possible proof strategies are shown below.
1809
1810     \subsection{Graph representation}
1811       Before looking into how to prove these properties, we'll look at our
1812       transformation system from a graph perspective. The nodes of the graph are
1813       all possible Core expressions. The (directed) edges of the graph are
1814       transformations. When a transformation α applies to an expression \lam{A} to
1815       produce an expression \lam{B}, we add an edge from the node for \lam{A} to the
1816       node for \lam{B}, labeled α.
1817
1818       \startuseMPgraphic{TransformGraph}
1819         save a, b, c, d;
1820
1821         % Nodes
1822         newCircle.a(btex \lam{(λx.λy. (+) x y) 1} etex);
1823         newCircle.b(btex \lam{λy. (+) 1 y} etex);
1824         newCircle.c(btex \lam{(λx.(+) x) 1} etex);
1825         newCircle.d(btex \lam{(+) 1} etex);
1826
1827         b.c = origin;
1828         c.c = b.c + (4cm, 0cm);
1829         a.c = midpoint(b.c, c.c) + (0cm, 4cm);
1830         d.c = midpoint(b.c, c.c) - (0cm, 3cm);
1831
1832         % β-conversion between a and b
1833         ncarc.a(a)(b) "name(bred)";
1834         ObjLabel.a(btex $\xrightarrow[normal]{}{β}$ etex) "labpathname(bred)", "labdir(rt)";
1835         ncarc.b(b)(a) "name(bexp)", "linestyle(dashed withdots)";
1836         ObjLabel.b(btex $\xleftarrow[normal]{}{β}$ etex) "labpathname(bexp)", "labdir(lft)";
1837
1838         % η-conversion between a and c
1839         ncarc.a(a)(c) "name(ered)";
1840         ObjLabel.a(btex $\xrightarrow[normal]{}{η}$ etex) "labpathname(ered)", "labdir(rt)";
1841         ncarc.c(c)(a) "name(eexp)", "linestyle(dashed withdots)";
1842         ObjLabel.c(btex $\xleftarrow[normal]{}{η}$ etex) "labpathname(eexp)", "labdir(lft)";
1843
1844         % η-conversion between b and d
1845         ncarc.b(b)(d) "name(ered)";
1846         ObjLabel.b(btex $\xrightarrow[normal]{}{η}$ etex) "labpathname(ered)", "labdir(rt)";
1847         ncarc.d(d)(b) "name(eexp)", "linestyle(dashed withdots)";
1848         ObjLabel.d(btex $\xleftarrow[normal]{}{η}$ etex) "labpathname(eexp)", "labdir(lft)";
1849
1850         % β-conversion between c and d
1851         ncarc.c(c)(d) "name(bred)";
1852         ObjLabel.c(btex $\xrightarrow[normal]{}{β}$ etex) "labpathname(bred)", "labdir(rt)";
1853         ncarc.d(d)(c) "name(bexp)", "linestyle(dashed withdots)";
1854         ObjLabel.d(btex $\xleftarrow[normal]{}{β}$ etex) "labpathname(bexp)", "labdir(lft)";
1855
1856         % Draw objects and lines
1857         drawObj(a, b, c, d);
1858       \stopuseMPgraphic
1859
1860       \placeexample[right][ex:TransformGraph]{Partial graph of a lambda calculus
1861       system with β and η reduction (solid lines) and expansion (dotted lines).}
1862           \boxedgraphic{TransformGraph}
1863
1864       Of course our graph is unbounded, since we can construct an infinite amount of
1865       Core expressions. Also, there might potentially be multiple edges between two
1866       given nodes (with different labels), though seems unlikely to actually happen
1867       in our system.
1868
1869       See \in{example}[ex:TransformGraph] for the graph representation of a very
1870       simple lambda calculus that contains just the expressions \lam{(λx.λy. (+) x
1871       y) 1}, \lam{λy. (+) 1 y}, \lam{(λx.(+) x) 1} and \lam{(+) 1}. The
1872       transformation system consists of β-reduction and η-reduction (solid edges) or
1873       β-expansion and η-expansion (dotted edges).
1874
1875       \todo{Define β-reduction and η-reduction?}
1876
1877       Note that the normal form of such a system consists of the set of nodes
1878       (expressions) without outgoing edges, since those are the expression to which
1879       no transformation applies anymore. We call this set of nodes the \emph{normal
1880       set}. The set of nodes containing expressions in intended normal
1881       form \refdef{intended normal form} is called the \emph{intended
1882       normal set}.
1883
1884       From such a graph, we can derive some properties easily:
1885       \startitemize[KR]
1886         \item A system will \emph{terminate} if there is no path of infinite length
1887         in the graph (this includes cycles, but can also happen without cycles).
1888         \item Soundness is not easily represented in the graph.
1889         \item A system is \emph{complete} if all of the nodes in the normal set have
1890         the intended normal form. The inverse (that all of the nodes outside of
1891         the normal set are \emph{not} in the intended normal form) is not
1892         strictly required. In other words, our normal set must be a
1893         subset of the intended normal form, but they do not need to be
1894         the same set.
1895         form.
1896         \item A system is deterministic if all paths starting at a particular
1897         node, which end in a node in the normal set, end at the same node.
1898       \stopitemize
1899
1900       When looking at the \in{example}[ex:TransformGraph], we see that the system
1901       terminates for both the reduction and expansion systems (but note that, for
1902       expansion, this is only true because we've limited the possible
1903       expressions.  In comlete lambda calculus, there would be a path from
1904       \lam{(λx.λy. (+) x y) 1} to \lam{(λx.λy.(λz.(+) z) x y) 1} to
1905       \lam{(λx.λy.(λz.(λq.(+) q) z) x y) 1} etc.)
1906
1907       If we would consider the system with both expansion and reduction, there
1908       would no longer be termination either, since there would be cycles all
1909       over the place.
1910
1911       The reduction and expansion systems have a normal set of containing just
1912       \lam{(+) 1} or \lam{(λx.λy. (+) x y) 1} respectively. Since all paths in
1913       either system end up in these normal forms, both systems are \emph{complete}.
1914       Also, since there is only one node in the normal set, it must obviously be
1915       \emph{deterministic} as well.
1916
1917     \todo{Add content to these sections}
1918     \subsection{Termination}
1919       In general, proving termination of an arbitrary program is a very
1920       hard problem. \todo{Ref about arbitrary termination} Fortunately,
1921       we only have to prove termination for our specific transformation
1922       system.
1923
1924       A common approach for these kinds of proofs is to associate a
1925       measure with each possible expression in our system. If we can
1926       show that each transformation strictly decreases this measure
1927       (\ie, the expression transformed to has a lower measure than the
1928       expression transformed from).  \todo{ref about measure-based
1929       termination proofs / analysis}
1930       
1931       A good measure for a system consisting of just β-reduction would
1932       be the number of lambda expressions in the expression. Since every
1933       application of β-reduction removes a lambda abstraction (and there
1934       is always a bounded number of lambda abstractions in every
1935       expression) we can easily see that a transformation system with
1936       just β-reduction will always terminate.
1937
1938       For our complete system, this measure would be fairly complex
1939       (probably the sum of a lot of things). Since the (conditions on)
1940       our transformations are pretty complex, we would need to include
1941       both simple things like the number of let expressions as well as
1942       more complex things like the number of case expressions that are
1943       not yet in normal form.
1944
1945       No real attempt has been made at finding a suitable measure for
1946       our system yet.
1947
1948     \subsection{Soundness}
1949       Soundness is a property that can be proven for each transformation
1950       separately. Since our system only runs separate transformations
1951       sequentially, if each of our transformations leaves the
1952       \emph{meaning} of the expression unchanged, then the entire system
1953       will of course leave the meaning unchanged and is thus
1954       \emph{sound}.
1955
1956       The current prototype has only been verified in an ad-hoc fashion
1957       by inspecting (the code for) each transformation. A more formal
1958       verification would be more appropriate.
1959
1960       To be able to formally show that each transformation properly
1961       preserves the meaning of every expression, we require an exact
1962       definition of the \emph{meaning} of every expression, so we can
1963       compare them. Currently there seems to be no formal definition of
1964       the meaning or semantics of \GHC's core language, only informal
1965       descriptions are available.
1966
1967       It should be possible to have a single formal definition of
1968       meaning for Core for both normal Core compilation by \GHC and for
1969       our compilation to \VHDL. The main difference seems to be that in
1970       hardware every expression is always evaluated, while in software
1971       it is only evaluated if needed, but it should be possible to
1972       assign a meaning to core expressions that assumes neither.
1973       
1974       Since each of the transformations can be applied to any
1975       subexpression as well, there is a constraint on our meaning
1976       definition: The meaning of an expression should depend only on the
1977       meaning of subexpressions, not on the expressions themselves. For
1978       example, the meaning of the application in \lam{f (let x = 4 in
1979       x)} should be the same as the meaning of the application in \lam{f
1980       4}, since the argument subexpression has the same meaning (though
1981       the actual expression is different).
1982       
1983     \subsection{Completeness}
1984       Proving completeness is probably not hard, but it could be a lot
1985       of work. We have seen above that to prove completeness, we must
1986       show that the normal set of our graph representation is a subset
1987       of the intended normal set.
1988
1989       However, it is hard to systematically generate or reason about the
1990       normal set, since it is defined as any nodes to which no
1991       transformation applies. To determine this set, each transformation
1992       must be considered and when a transformation is added, the entire
1993       set should be re-evaluated. This means it is hard to show that
1994       each node in the normal set is also in the intended normal set.
1995       Reasoning about our intended normal set is easier, since we know
1996       how to generate it from its definition. \refdef{intended normal
1997       form definition}.
1998
1999       Fortunately, we can also prove the complement (which is
2000       equivalent, since $A \subseteq B \Leftrightarrow \overline{B}
2001       \subseteq \overline{A}$): Show that the set of nodes not in
2002       intended normal form is a subset of the set of nodes not in normal
2003       form. In other words, show that for every expression that is not
2004       in intended normal form, that there is at least one transformation
2005       that applies to it (since that means it is not in normal form
2006       either and since $A \subseteq C \Leftrightarrow \forall x (x \in A
2007       \rightarrow x \in C)$).
2008
2009       By systematically reviewing the entire Core language definition
2010       along with the intended normal form definition (both of which have
2011       a similar structure), it should be possible to identify all
2012       possible (sets of) core expressions that are not in intended
2013       normal form and identify a transformation that applies to it.
2014       
2015       This approach is especially useful for proving completeness of our
2016       system, since if expressions exist to which none of the
2017       transformations apply (\ie if the system is not yet complete), it
2018       is immediately clear which expressions these are and adding
2019       (or modifying) transformations to fix this should be relatively
2020       easy.
2021
2022       As observed above, applying this approach is a lot of work, since
2023       we need to check every (set of) transformation(s) separately.
2024
2025       \todo{Perhaps do a few steps of the proofs as proof-of-concept}
2026
2027 % vim: set sw=2 sts=2 expandtab: