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