Add a section on binder uniqueness.
[matthijs/master-project/report.git] / Chapters / Normalization.tex
1 \chapter[chap:normalization]{Normalization}
2
3 % A helper to print a single example in the half the page width. The example
4 % text should be in a buffer whose name is given in an argument.
5 %
6 % The align=right option really does left-alignment, but without the program
7 % will end up on a single line. The strut=no option prevents a bunch of empty
8 % space at the start of the frame.
9 \define[1]\example{
10   \framed[offset=1mm,align=right,strut=no,background=box,frame=off]{
11     \setuptyping[option=LAM,style=sans,before=,after=]
12     \typebuffer[#1]
13     \setuptyping[option=none,style=\tttf]
14   }
15 }
16
17
18 % A transformation example
19 \definefloat[example][examples]
20 \setupcaption[example][location=top] % Put captions on top
21
22 \define[3]\transexample{
23   \placeexample[here]{#1}
24   \startcombination[2*1]
25     {\example{#2}}{Original program}
26     {\example{#3}}{Transformed program}
27   \stopcombination
28 }
29 %
30 %\define[3]\transexampleh{
31 %%  \placeexample[here]{#1}
32 %%  \startcombination[1*2]
33 %%    {\example{#2}}{Original program}
34 %%    {\example{#3}}{Transformed program}
35 %%  \stopcombination
36 %}
37
38 The first step in the core to \small{VHDL} translation process, is normalization. We
39 aim to bring the core description into a simpler form, which we can
40 subsequently translate into \small{VHDL} easily. This normal form is needed because
41 the full core language is more expressive than \small{VHDL} in some areas and because
42 core can describe expressions that do not have a direct hardware
43 interpretation.
44
45 TODO: Describe core properties not supported in \small{VHDL}, and describe how the
46 \small{VHDL} we want to generate should look like.
47
48 \section{Normal form}
49 The transformations described here have a well-defined goal: To bring the
50 program in a well-defined form that is directly translatable to hardware,
51 while fully preserving the semantics of the program. We refer to this form as
52 the \emph{normal form} of the program. The formal definition of this normal
53 form is quite simple:
54
55 \placedefinition{}{A program is in \emph{normal form} if none of the
56 transformations from this chapter apply.}
57
58 Of course, this is an \quote{easy} definition of the normal form, since our
59 program will end up in normal form automatically. The more interesting part is
60 to see if this normal form actually has the properties we would like it to
61 have.
62
63 But, before getting into more definitions and details about this normal form,
64 let's try to get a feeling for it first. The easiest way to do this is by
65 describing the things we want to not have in a normal form.
66
67 \startitemize
68   \item Any \emph{polymorphism} must be removed. When laying down hardware, we
69   can't generate any signals that can have multiple types. All types must be
70   completely known to generate hardware.
71   
72   \item Any \emph{higher order} constructions must be removed. We can't
73   generate a hardware signal that contains a function, so all values,
74   arguments and returns values used must be first order.
75
76   \item Any complex \emph{nested scopes} must be removed. In the \small{VHDL}
77   description, every signal is in a single scope. Also, full expressions are
78   not supported everywhere (in particular port maps can only map signal names,
79   not expressions). To make the \small{VHDL} generation easy, all values must be bound
80   on the \quote{top level}.
81 \stopitemize
82
83 TODO: Intermezzo: functions vs plain values
84
85 A very simple example of a program in normal form is given in
86 \in{example}[ex:MulSum]. As you can see, all arguments to the function (which
87 will become input ports in the final hardware) are at the top. This means that
88 the body of the final lambda abstraction is never a function, but always a
89 plain value.
90
91 After the lambda abstractions, we see a single let expression, that binds two
92 variables (\lam{mul} and \lam{sum}). These variables will be signals in the
93 final hardware, bound to the output port of the \lam{*} and \lam{+}
94 components.
95
96 The final line (the \quote{return value} of the function) selects the
97 \lam{sum} signal to be the output port of the function. This \quote{return
98 value} can always only be a variable reference, never a more complex
99 expression.
100
101 \startbuffer[MulSum]
102 alu :: Bit -> Word -> Word -> Word
103 alu = λa.λb.λc.
104     let
105       mul = (*) a b
106       sum = (+) mul c
107     in
108       sum
109 \stopbuffer
110
111 \startuseMPgraphic{MulSum}
112   save a, b, c, mul, add, sum;
113
114   % I/O ports
115   newCircle.a(btex $a$ etex) "framed(false)";
116   newCircle.b(btex $b$ etex) "framed(false)";
117   newCircle.c(btex $c$ etex) "framed(false)";
118   newCircle.sum(btex $res$ etex) "framed(false)";
119
120   % Components
121   newCircle.mul(btex - etex);
122   newCircle.add(btex + etex);
123
124   a.c      - b.c   = (0cm, 2cm);
125   b.c      - c.c   = (0cm, 2cm);
126   add.c            = c.c + (2cm, 0cm);
127   mul.c            = midpoint(a.c, b.c) + (2cm, 0cm);
128   sum.c            = add.c + (2cm, 0cm);
129   c.c              = origin;
130
131   % Draw objects and lines
132   drawObj(a, b, c, mul, add, sum);
133
134   ncarc(a)(mul) "arcangle(15)";
135   ncarc(b)(mul) "arcangle(-15)";
136   ncline(c)(add);
137   ncline(mul)(add);
138   ncline(add)(sum);
139 \stopuseMPgraphic
140
141 \placeexample[here][ex:MulSum]{Simple architecture consisting of an adder and a
142 subtractor.}
143   \startcombination[2*1]
144     {\typebufferlam{MulSum}}{Core description in normal form.}
145     {\boxedgraphic{MulSum}}{The architecture described by the normal form.}
146   \stopcombination
147
148 The previous example described composing an architecture by calling other
149 functions (operators), resulting in a simple architecture with component and
150 connection. There is of course also some mechanism for choice in the normal
151 form. In a normal Core program, the \emph{case} expression can be used in a
152 few different ways to describe choice. In normal form, this is limited to a
153 very specific form.
154
155 \in{Example}[ex:AddSubAlu] shows an example describing a
156 simple \small{ALU}, which chooses between two operations based on an opcode
157 bit. The main structure is the same as in \in{example}[ex:MulSum], but this
158 time the \lam{res} variable is bound to a case expression. This case
159 expression scrutinizes the variable \lam{opcode} (and scrutinizing more
160 complex expressions is not supported). The case expression can select a
161 different variable based on the constructor of \lam{opcode}.
162
163 \startbuffer[AddSubAlu]
164 alu :: Bit -> Word -> Word -> Word
165 alu = λopcode.λa.λb.
166     let
167       res1 = (+) a b
168       res2 = (-) a b
169       res = case opcode of
170         Low -> res1
171         High -> res2
172     in
173       res
174 \stopbuffer
175
176 \startuseMPgraphic{AddSubAlu}
177   save opcode, a, b, add, sub, mux, res;
178
179   % I/O ports
180   newCircle.opcode(btex $opcode$ etex) "framed(false)";
181   newCircle.a(btex $a$ etex) "framed(false)";
182   newCircle.b(btex $b$ etex) "framed(false)";
183   newCircle.res(btex $res$ etex) "framed(false)";
184   % Components
185   newCircle.add(btex + etex);
186   newCircle.sub(btex - etex);
187   newMux.mux;
188
189   opcode.c - a.c   = (0cm, 2cm);
190   add.c    - a.c   = (4cm, 0cm);
191   sub.c    - b.c   = (4cm, 0cm);
192   a.c      - b.c   = (0cm, 3cm);
193   mux.c            = midpoint(add.c, sub.c) + (1.5cm, 0cm);
194   res.c    - mux.c = (1.5cm, 0cm);
195   b.c              = origin;
196
197   % Draw objects and lines
198   drawObj(opcode, a, b, res, add, sub, mux);
199
200   ncline(a)(add) "posA(e)";
201   ncline(b)(sub) "posA(e)";
202   nccurve(a)(sub) "posA(e)", "angleA(0)";
203   nccurve(b)(add) "posA(e)", "angleA(0)";
204   nccurve(add)(mux) "posB(inpa)", "angleB(0)";
205   nccurve(sub)(mux) "posB(inpb)", "angleB(0)";
206   nccurve(opcode)(mux) "posB(n)", "angleA(0)", "angleB(-90)";
207   ncline(mux)(res) "posA(out)";
208 \stopuseMPgraphic
209
210 \placeexample[here][ex:AddSubAlu]{Simple \small{ALU} supporting two operations.}
211   \startcombination[2*1]
212     {\typebufferlam{AddSubAlu}}{Core description in normal form.}
213     {\boxedgraphic{AddSubAlu}}{The architecture described by the normal form.}
214   \stopcombination
215
216 As a more complete example, consider \in{example}[ex:NormalComplete]. This
217 example contains everything that is supported in normal form, with the
218 exception of builtin higher order functions. The graphical version of the
219 architecture contains a slightly simplified version, since the state tuple
220 packing and unpacking have been left out. Instead, two seperate registers are
221 drawn. Also note that most synthesis tools will further optimize this
222 architecture by removing the multiplexers at the register input and replace
223 them with some logic in the clock inputs, but we want to show the architecture
224 as close to the description as possible.
225
226 \startbuffer[NormalComplete]
227   regbank :: Bit 
228              -> Word 
229              -> State (Word, Word) 
230              -> (State (Word, Word), Word)
231
232   -- All arguments are an inital lambda
233   regbank = λa.λd.λsp.
234   -- There are nested let expressions at top level
235   let
236     -- Unpack the state by coercion (\eg, cast from
237     -- State (Word, Word) to (Word, Word))
238     s = sp :: (Word, Word)
239     -- Extract both registers from the state
240     r1 = case s of (fst, snd) -> fst
241     r2 = case s of (fst, snd) -> snd
242     -- Calling some other user-defined function.
243     d' = foo d
244     -- Conditional connections
245     out = case a of
246       High -> r1
247       Low -> r2
248     r1' = case a of
249       High -> d'
250       Low -> r1
251     r2' = case a of
252       High -> r2
253       Low -> d'
254     -- Packing a tuple
255     s' = (,) r1' r2'
256     -- pack the state by coercion (\eg, cast from
257     -- (Word, Word) to State (Word, Word))
258     sp' = s' :: State (Word, Word)
259     -- Pack our return value
260     res = (,) sp' out
261   in
262     -- The actual result
263     res
264 \stopbuffer
265
266 \startuseMPgraphic{NormalComplete}
267   save a, d, r, foo, muxr, muxout, out;
268
269   % I/O ports
270   newCircle.a(btex \lam{a} etex) "framed(false)";
271   newCircle.d(btex \lam{d} etex) "framed(false)";
272   newCircle.out(btex \lam{out} etex) "framed(false)";
273   % Components
274   %newCircle.add(btex + etex);
275   newBox.foo(btex \lam{foo} etex);
276   newReg.r1(btex $\lam{r1}$ etex) "dx(4mm)", "dy(6mm)";
277   newReg.r2(btex $\lam{r2}$ etex) "dx(4mm)", "dy(6mm)", "reflect(true)";
278   newMux.muxr1;
279   % Reflect over the vertical axis
280   reflectObj(muxr1)((0,0), (0,1));
281   newMux.muxr2;
282   newMux.muxout;
283   rotateObj(muxout)(-90);
284
285   d.c               = foo.c + (0cm, 1.5cm); 
286   a.c               = (xpart r2.c + 2cm, ypart d.c - 0.5cm);
287   foo.c             = midpoint(muxr1.c, muxr2.c) + (0cm, 2cm);
288   muxr1.c           = r1.c + (0cm, 2cm);
289   muxr2.c           = r2.c + (0cm, 2cm);
290   r2.c              = r1.c + (4cm, 0cm);
291   r1.c              = origin;
292   muxout.c          = midpoint(r1.c, r2.c) - (0cm, 2cm);
293   out.c             = muxout.c - (0cm, 1.5cm);
294
295 %  % Draw objects and lines
296   drawObj(a, d, foo, r1, r2, muxr1, muxr2, muxout, out);
297   
298   ncline(d)(foo);
299   nccurve(foo)(muxr1) "angleA(-90)", "posB(inpa)", "angleB(180)";
300   nccurve(foo)(muxr2) "angleA(-90)", "posB(inpb)", "angleB(0)";
301   nccurve(muxr1)(r1) "posA(out)", "angleA(180)", "posB(d)", "angleB(0)";
302   nccurve(r1)(muxr1) "posA(out)", "angleA(0)", "posB(inpb)", "angleB(180)";
303   nccurve(muxr2)(r2) "posA(out)", "angleA(0)", "posB(d)", "angleB(180)";
304   nccurve(r2)(muxr2) "posA(out)", "angleA(180)", "posB(inpa)", "angleB(0)";
305   nccurve(r1)(muxout) "posA(out)", "angleA(0)", "posB(inpb)", "angleB(-90)";
306   nccurve(r2)(muxout) "posA(out)", "angleA(180)", "posB(inpa)", "angleB(-90)";
307   % Connect port a
308   nccurve(a)(muxout) "angleA(-90)", "angleB(180)", "posB(sel)";
309   nccurve(a)(muxr1) "angleA(180)", "angleB(-90)", "posB(sel)";
310   nccurve(a)(muxr2) "angleA(180)", "angleB(-90)", "posB(sel)";
311   ncline(muxout)(out) "posA(out)";
312 \stopuseMPgraphic
313
314 \placeexample[here][ex:NormalComplete]{Simple architecture consisting of an adder and a
315 subtractor.}
316   \startcombination[2*1]
317     {\typebufferlam{NormalComplete}}{Core description in normal form.}
318     {\boxedgraphic{NormalComplete}}{The architecture described by the normal form.}
319   \stopcombination
320
321 \subsection{Normal form definition}
322 Now we have some intuition for the normal form, we can describe how we want
323 the normal form to look like in a slightly more formal manner. The following
324 EBNF-like description completely captures the intended structure (and
325 generates a subset of GHC's core format).
326
327 Some clauses have an expression listed in parentheses. These are conditions
328 that need to apply to the clause.
329
330 \startlambda
331 \italic{normal} = \italic{lambda}
332 \italic{lambda} = λvar.\italic{lambda} (representable(var))
333                 | \italic{toplet} 
334 \italic{toplet} = let \italic{binding} in \italic{toplet} 
335                 | letrec [\italic{binding}] in \italic{toplet}
336                 | var (representable(varvar))
337 \italic{binding} = var = \italic{rhs} (representable(rhs))
338                  -- State packing and unpacking by coercion
339                  | var0 = var1 :: State ty (lvar(var1))
340                  | var0 = var1 :: ty (var0 :: State ty) (lvar(var1))
341 \italic{rhs} = userapp
342              | builtinapp
343              -- Extractor case
344              | case var of C a0 ... an -> ai (lvar(var))
345              -- Selector case
346              | case var of (lvar(var))
347                 DEFAULT -> var0 (lvar(var0))
348                 C w0 ... wn -> resvar (\forall{}i, wi \neq resvar, lvar(resvar))
349 \italic{userapp} = \italic{userfunc}
350                  | \italic{userapp} {userarg}
351 \italic{userfunc} = var (gvar(var))
352 \italic{userarg} = var (lvar(var))
353 \italic{builtinapp} = \italic{builtinfunc}
354                     | \italic{builtinapp} \italic{builtinarg}
355 \italic{builtinfunc} = var (bvar(var))
356 \italic{builtinarg} = \italic{coreexpr}
357 \stoplambda
358
359 -- TODO: Limit builtinarg further
360
361 -- TODO: There can still be other casts around (which the code can handle,
362 e.g., ignore), which still need to be documented here.
363
364 -- TODO: Note about the selector case. It just supports Bit and Bool
365 currently, perhaps it should be generalized in the normal form?
366
367 When looking at such a program from a hardware perspective, the top level
368 lambda's define the input ports. The value produced by the let expression is
369 the output port. Most function applications bound by the let expression
370 define a component instantiation, where the input and output ports are mapped
371 to local signals or arguments. Some of the others use a builtin
372 construction (\eg the \lam{case} statement) or call a builtin function
373 (\eg \lam{add} or \lam{sub}). For these, a hardcoded \small{VHDL} translation is
374 available.
375
376 \subsection{Definitions}
377 In the following sections, we will be using a number of functions and
378 notations, which we will define here.
379
380 \subsubsection{Transformations}
381 The most important notation is the one for transformation, which looks like
382 the following:
383
384 \starttrans
385 context conditions
386 ~
387 from
388 ------------------------            expression conditions
389 to
390 ~
391 context additions
392 \stoptrans
393
394 Here, we describe a transformation. The most import parts are \lam{from} and
395 \lam{to}, which describe the Core expresssion that should be matched and the
396 expression that it should be replaced with. This matching can occur anywhere
397 in function that is being normalized, so it applies to any subexpression as
398 well.
399
400 The \lam{expression conditions} list a number of conditions on the \lam{from}
401 expression that must hold for the transformation to apply.
402
403 Furthermore, there is some way to look into the environment (\eg, other top
404 level bindings).  The \lam{context conditions} part specifies any number of
405 top level bindings that must be present for the transformation to apply.
406 Usually, this lists a top level binding that binds an identfier that is also
407 used in the \lam{from} expression, allowing us to "access" the value of a top
408 level binding in the \lam{to} expression (\eg, for inlining).
409
410 Finally, there is a way to influence the environment. The \lam{context
411 additions} part lists any number of new top level bindings that should be
412 added.
413
414 If there are no \lam{context conditions} or \lam{context additions}, they can
415 be left out alltogether, along with the separator \lam{~}.
416
417 TODO: Example
418
419 \subsubsection{Other concepts}
420 A \emph{global variable} is any variable that is bound at the
421 top level of a program, or an external module. A local variable is any other
422 variable (\eg, variables local to a function, which can be bound by lambda
423 abstractions, let expressions and case expressions).
424
425 A \emph{hardware representable} type is a type that we can generate
426 a signal for in hardware. For example, a bit, a vector of bits, a 32 bit
427 unsigned word, etc. Types that are not runtime representable notably
428 include (but are not limited to): Types, dictionaries, functions.
429
430 A \emph{builtin function} is a function for which a builtin
431 hardware translation is available, because its actual definition is not
432 translatable. A user-defined function is any other function.
433
434 \subsubsection{Functions}
435 Here, we define a number of functions that can be used below to concisely
436 specify conditions.
437
438 \emph{gvar(expr)} is true when \emph{expr} is a variable that references a
439 global variable. It is false when it references a local variable.
440
441 \emph{lvar(expr)} is the inverse of \emph{gvar}; it is true when \emph{expr}
442 references a local variable, false when it references a global variable.
443
444 \emph{representable(expr)} or \emph{representable(var)} is true when
445 \emph{expr} or \emph{var} has a type that is representable at runtime.
446
447 \section{Transform passes}
448 In this section we describe the actual transforms. Here we're using
449 the core language in a notation that resembles lambda calculus.
450
451 Each of these transforms is meant to be applied to every (sub)expression
452 in a program, for as long as it applies. Only when none of the
453 transformations can be applied anymore, the program is in normal form (by
454 definition). We hope to be able to prove that this form will obey all of the
455 constraints defined above, but this has yet to happen (though it seems likely
456 that it will).
457
458 Each of the transforms will be described informally first, explaining
459 the need for and goal of the transform. Then, a formal definition is
460 given, using a familiar syntax from the world of logic. Each transform
461 is specified as a number of conditions (above the horizontal line) and a
462 number of conclusions (below the horizontal line). The details of using
463 this notation are still a bit fuzzy, so comments are welcom.
464
465 TODO: Formally describe the "apply to every (sub)expression" in terms of
466 rules with full transformations in the conditions.
467
468 \subsection{Binder uniqueness}
469 A common problem in transformation systems, is binder uniqueness. When not
470 considering this problem, it is easy to create transformations that mix up
471 bindings and cause name collisions. Take for example, the following core
472 expression:
473
474 \startlambda
475 (λa.λb.λc. a * b * c) x c
476 \stoplambda
477
478 By applying β-reduction (see below) once, we can simplify this expression to:
479
480 \startlambda
481 (λb.λc. x * b * c) c
482 \stoplambda
483
484 Now, we have replaced the \lam{a} binder with a reference to the \lam{x}
485 binder. No harm done here. But note that we see multiple occurences of the
486 \lam{c} binder. The first is a binding occurence, to which the second refers.
487 The last, however refers to \emph{another} instance of \lam{c}, which is
488 bound somewhere outside of this expression. Now, if we would apply beta
489 reduction without taking heed of binder uniqueness, we would get:
490
491 \startlambda
492 λc. x * c * c
493 \stoplambda
494
495 This is obviously not what was supposed to happen! The root of this problem is
496 the reuse of binders: Identical binders can be bound in different scopes, such
497 that only the inner one is \quote{visible} in the inner expression. In the example
498 above, the \lam{c} binder was bound outside of the expression and in the inner
499 lambda expression. Inside that lambda expression, only the inner \lam{c} is
500 visible.
501
502 There are a number of ways to solve this. \small{GHC} has isolated this
503 problem to their binder substitution code, which performs \emph{deshadowing}
504 during its expression traversal. This means that any binding that shadows
505 another binding on a higher level is replaced by a new binder that does not
506 shadow any other binding. This non-shadowing invariant is enough to prevent
507 binder uniqueness problems in \small{GHC}.
508
509 In our transformation system, maintaining this non-shadowing invariant is
510 a bit harder to do (mostly due to implementation issues, the prototype doesn't
511 use \small{GHC}'s subsitution code). Also, we can observe the following
512 points.
513
514 \startitemize
515 \item Deshadowing does not guarantee overall uniqueness. For example, the
516 following (slightly contrived) expression shows the identifier \lam{x} bound in
517 two seperate places (and to different values), even though no shadowing
518 occurs.
519
520 \startlambda
521 (let x = 1 in x) + (let x = 2 in x)
522 \stoplambda
523
524 \item In our normal form (and the resulting \small{VHDL}), all binders
525 (signals) will end up in the same scope. To allow this, all binders within the
526 same function should be unique.
527
528 \item When we know that all binders in an expression are unique, moving around
529 or removing a subexpression will never cause any binder conflicts. If we have
530 some way to generate fresh binders, introducing new subexpressions will not
531 cause any problems either. The only way to cause conflicts is thus to
532 duplicate an existing subexpression.
533 \stopitemize
534
535 Given the above, our prototype maintains a unique binder invariant. This
536 meanst that in any given moment during normalization, all binders \emph{within
537 a single function} must be unique. To achieve this, we apply the following
538 technique.
539
540 TODO: Define fresh binders and unique supplies
541
542 \startitemize
543 \item Before starting normalization, all binders in the function are made
544 unique. This is done by generating a fresh binder for every binder used. This
545 also replaces binders that did not pose any conflict, but it does ensure that
546 all binders within the function are generated by the same unique supply. See
547 (TODO: ref fresh binder).
548 \item Whenever a new binder must be generated, we generate a fresh binder that
549 is guaranteed to be different from \emph{all binders generated so far}. This
550 can thus never introduce duplication and will maintain the invariant.
551 \item Whenever (part of) an expression is duplicated (for example when
552 inlining), all binders in the expression are replaced with fresh binders
553 (using the same method as at the start of normalization). These fresh binders
554 can never introduce duplication, so this will maintain the invariant.
555 \item Whenever we move part of an expression around within the function, there
556 is no need to do anything special. There is obviously no way to introduce
557 duplication by moving expressions around. Since we know that each of the
558 binders is already unique, there is no way to introduce (incorrect) shadowing
559 either.
560 \stopitemize
561
562 \subsection{η-abstraction}
563 This transformation makes sure that all arguments of a function-typed
564 expression are named, by introducing lambda expressions. When combined with
565 β-reduction and function inlining below, all function-typed expressions should
566 be lambda abstractions or global identifiers.
567
568 \starttrans
569 E                 \lam{E :: * -> *}
570 --------------    \lam{E} is not the first argument of an application.
571 λx.E x            \lam{E} is not a lambda abstraction.
572                   \lam{x} is a variable that does not occur free in \lam{E}.
573 \stoptrans
574
575 \startbuffer[from]
576 foo = λa.case a of 
577   True -> λb.mul b b
578   False -> id
579 \stopbuffer
580
581 \startbuffer[to]
582 foo = λa.λx.(case a of 
583     True -> λb.mul b b
584     False -> λy.id y) x
585 \stopbuffer
586
587 \transexample{η-abstraction}{from}{to}
588
589 \subsection{Extended β-reduction}
590 This transformation is meant to propagate application expressions downwards
591 into expressions as far as possible. In lambda calculus, this reduction
592 is known as β-reduction, but it is of course only defined for
593 applications of lambda abstractions. We extend this reduction to also
594 work for the rest of core (case and let expressions).
595
596 For let expressions:
597 \starttrans
598 let binds in E) M
599 -----------------
600 let binds in E M
601 \stoptrans
602
603 For case statements:
604 \starttrans
605 (case x of
606   p1 -> E1
607   \vdots
608   pn -> En) M
609 -----------------
610 case x of
611   p1 -> E1 M
612   \vdots
613   pn -> En M
614 \stoptrans
615
616 For lambda expressions:
617 \starttrans
618 (λx.E) M
619 -----------------
620 E[M/x]
621 \stoptrans
622
623 % And an example
624 \startbuffer[from]
625 ( let a = (case x of 
626             True -> id
627             False -> neg
628           ) 1
629       b = (let y = 3 in add y) 2
630   in
631     (λz.add 1 z)
632 ) 3
633 \stopbuffer
634
635 \startbuffer[to]
636 let a = case x of 
637            True -> id 1
638            False -> neg 1
639     b = let y = 3 in add y 2
640 in
641   add 1 3
642 \stopbuffer
643
644 \transexample{Extended β-reduction}{from}{to}
645
646 \subsection{Let derecursification}
647 This transformation is meant to make lets non-recursive whenever possible.
648 This might allow other optimizations to do their work better. TODO: Why is
649 this needed exactly?
650
651 \subsection{Let flattening}
652 This transformation puts nested lets in the same scope, by lifting the
653 binding(s) of the inner let into a new let around the outer let. Eventually,
654 this will cause all let bindings to appear in the same scope (they will all be
655 in scope for the function return value).
656
657 Note that this transformation does not try to be smart when faced with
658 recursive lets, it will just leave the lets recursive (possibly joining a
659 recursive and non-recursive let into a single recursive let). The let
660 rederursification transformation will do this instead.
661
662 \starttrans
663 letnonrec x = (let bindings in M) in N
664 ------------------------------------------
665 let bindings in (letnonrec x = M) in N
666 \stoptrans
667
668 \starttrans
669 letrec 
670   \vdots
671   x = (let bindings in M)
672   \vdots
673 in
674   N
675 ------------------------------------------
676 letrec
677   \vdots
678   bindings
679   x = M
680   \vdots
681 in
682   N
683 \stoptrans
684
685 \startbuffer[from]
686 let
687   a = letrec
688     x = 1
689     y = 2
690   in
691     x + y
692 in
693   letrec
694     b = let c = 3 in a + c
695     d = 4
696   in
697     d + b
698 \stopbuffer
699 \startbuffer[to]
700 letrec
701   x = 1
702   y = 2
703 in
704   let
705     a = x + y
706   in
707     letrec
708       c = 3
709       b = a + c
710       d = 4
711     in
712       d + b
713 \stopbuffer
714
715 \transexample{Let flattening}{from}{to}
716
717 \subsection{Empty let removal}
718 This transformation is simple: It removes recursive lets that have no bindings
719 (which usually occurs when let derecursification removes the last binding from
720 it).
721
722 \starttrans
723 letrec in M
724 --------------
725 M
726 \stoptrans
727
728 \subsection{Simple let binding removal}
729 This transformation inlines simple let bindings (\eg a = b).
730
731 This transformation is not needed to get into normal form, but makes the
732 resulting \small{VHDL} a lot shorter.
733
734 \starttrans
735 letnonrec
736   a = b
737 in
738   M
739 -----------------
740 M[b/a]
741 \stoptrans
742
743 \starttrans
744 letrec
745   \vdots
746   a = b
747   \vdots
748 in
749   M
750 -----------------
751 let
752   \vdots [b/a]
753   \vdots [b/a]
754 in
755   M[b/a]
756 \stoptrans
757
758 \subsection{Unused let binding removal}
759 This transformation removes let bindings that are never used. Usually,
760 the desugarer introduces some unused let bindings.
761
762 This normalization pass should really be unneeded to get into normal form
763 (since ununsed bindings are not forbidden by the normal form), but in practice
764 the desugarer or simplifier emits some unused bindings that cannot be
765 normalized (e.g., calls to a \type{PatError} (TODO: Check this name)). Also,
766 this transformation makes the resulting \small{VHDL} a lot shorter.
767
768 \starttrans
769 let a = E in M
770 ----------------------------    \lam{a} does not occur free in \lam{M}
771 M
772 \stoptrans
773
774 \starttrans
775 letrec
776   \vdots
777   a = E
778   \vdots
779 in
780   M
781 ----------------------------    \lam{a} does not occur free in \lam{M}
782 letrec
783   \vdots
784   \vdots
785 in
786   M
787 \stoptrans
788
789 \subsection{Non-representable binding inlining}
790 This transform inlines let bindings that have a non-representable type. Since
791 we can never generate a signal assignment for these bindings (we cannot
792 declare a signal assignment with a non-representable type, for obvious
793 reasons), we have no choice but to inline the binding to remove it.
794
795 If the binding is non-representable because it is a lambda abstraction, it is
796 likely that it will inlined into an application and β-reduction will remove
797 the lambda abstraction and turn it into a representable expression at the
798 inline site. The same holds for partial applications, which can be turned into
799 full applications by inlining.
800
801 Other cases of non-representable bindings we see in practice are primitive
802 Haskell types. In most cases, these will not result in a valid normalized
803 output, but then the input would have been invalid to start with. There is one
804 exception to this: When a builtin function is applied to a non-representable
805 expression, things might work out in some cases. For example, when you write a
806 literal \hs{SizedInt} in Haskell, like \hs{1 :: SizedInt D8}, this results in
807 the following core: \lam{fromInteger (smallInteger 10)}, where for example
808 \lam{10 :: GHC.Prim.Int\#} and \lam{smallInteger 10 :: Integer} have
809 non-representable types. TODO: This/these paragraph(s) should probably become a
810 separate discussion somewhere else.
811
812 \starttrans
813 letnonrec a = E in M
814 --------------------------    \lam{E} has a non-representable type.
815 M[E/a]
816 \stoptrans
817
818 \starttrans
819 letrec 
820   \vdots
821   a = E
822   \vdots
823 in
824   M
825 --------------------------    \lam{E} has a non-representable type.
826 letrec
827   \vdots [E/a]
828   \vdots [E/a]
829 in
830   M[E/a]
831 \stoptrans
832
833 \startbuffer[from]
834 letrec
835   a = smallInteger 10
836   inc = λa -> add a 1
837   inc' = add 1
838   x = fromInteger a 
839 in
840   inc (inc' x)
841 \stopbuffer
842
843 \startbuffer[to]
844 letrec
845   x = fromInteger (smallInteger 10)
846 in
847   (λa -> add a 1) (add 1 x)
848 \stopbuffer
849
850 \transexample{Let flattening}{from}{to}
851
852 \subsection{Compiler generated top level binding inlining}
853 TODO
854
855 \subsection{Scrutinee simplification}
856 This transform ensures that the scrutinee of a case expression is always
857 a simple variable reference.
858
859 \starttrans
860 case E of
861   alts
862 -----------------        \lam{E} is not a local variable reference
863 let x = E in 
864   case E of
865     alts
866 \stoptrans
867
868 \startbuffer[from]
869 case (foo a) of
870   True -> a
871   False -> b
872 \stopbuffer
873
874 \startbuffer[to]
875 let x = foo a in
876   case x of
877     True -> a
878     False -> b
879 \stopbuffer
880
881 \transexample{Let flattening}{from}{to}
882
883
884 \subsection{Case simplification}
885 This transformation ensures that all case expressions become normal form. This
886 means they will become one of:
887 \startitemize
888 \item An extractor case with a single alternative that picks a single field
889 from a datatype, \eg \lam{case x of (a, b) -> a}.
890 \item A selector case with multiple alternatives and only wild binders, that
891 makes a choice between expressions based on the constructor of another
892 expression, \eg \lam{case x of Low -> a; High -> b}.
893 \stopitemize
894
895 \starttrans
896 case E of
897   C0 v0,0 ... v0,m -> E0
898   \vdots
899   Cn vn,0 ... vn,m -> En
900 --------------------------------------------------- \forall i \forall j, 0 <= i <= n, 0 <= i < m (\lam{wi,j} is a wild (unused) binder)
901 letnonrec
902   v0,0 = case x of C0 v0,0 .. v0,m -> v0,0
903   \vdots
904   v0,m = case x of C0 v0,0 .. v0,m -> v0,m
905   x0 = E0
906   \dots
907   vn,m = case x of Cn vn,0 .. vn,m -> vn,m
908   xn = En
909 in
910   case E of
911     C0 w0,0 ... w0,m -> x0
912     \vdots
913     Cn wn,0 ... wn,m -> xn
914 \stoptrans
915
916 TODO: This transformation specified like this is complicated and misses
917 conditions to prevent looping with itself. Perhaps we should split it here for
918 discussion?
919
920 \startbuffer[from]
921 case a of
922   True -> add b 1
923   False -> add b 2
924 \stopbuffer
925
926 \startbuffer[to]
927 letnonrec
928   x0 = add b 1
929   x1 = add b 2
930 in
931   case a of
932     True -> x0
933     False -> x1
934 \stopbuffer
935
936 \transexample{Selector case simplification}{from}{to}
937
938 \startbuffer[from]
939 case a of
940   (,) b c -> add b c
941 \stopbuffer
942 \startbuffer[to]
943 letnonrec
944   b = case a of (,) b c -> b
945   c = case a of (,) b c -> c
946   x0 = add b c
947 in
948   case a of
949     (,) w0 w1 -> x0
950 \stopbuffer
951
952 \transexample{Extractor case simplification}{from}{to}
953
954 \subsection{Case removal}
955 This transform removes any case statements with a single alternative and
956 only wild binders.
957
958 These "useless" case statements are usually leftovers from case simplification
959 on extractor case (see the previous example).
960
961 \starttrans
962 case x of
963   C v0 ... vm -> E
964 ----------------------     \lam{\forall i, 0 <= i <= m} (\lam{vi} does not occur free in E)
965 E
966 \stoptrans
967
968 \startbuffer[from]
969 case a of
970   (,) w0 w1 -> x0
971 \stopbuffer
972
973 \startbuffer[to]
974 x0
975 \stopbuffer
976
977 \transexample{Case removal}{from}{to}
978
979 \subsection{Argument simplification}
980 The transforms in this section deal with simplifying application
981 arguments into normal form. The goal here is to:
982
983 \startitemize
984  \item Make all arguments of user-defined functions (\eg, of which
985  we have a function body) simple variable references of a runtime
986  representable type. This is needed, since these applications will be turned
987  into component instantiations.
988  \item Make all arguments of builtin functions one of:
989    \startitemize
990     \item A type argument.
991     \item A dictionary argument.
992     \item A type level expression.
993     \item A variable reference of a runtime representable type.
994     \item A variable reference or partial application of a function type.
995    \stopitemize
996 \stopitemize
997
998 When looking at the arguments of a user-defined function, we can
999 divide them into two categories:
1000 \startitemize
1001   \item Arguments of a runtime representable type (\eg bits or vectors).
1002
1003         These arguments can be preserved in the program, since they can
1004         be translated to input ports later on.  However, since we can
1005         only connect signals to input ports, these arguments must be
1006         reduced to simple variables (for which signals will be
1007         produced). This is taken care of by the argument extraction
1008         transform.
1009   \item Non-runtime representable typed arguments.
1010         
1011         These arguments cannot be preserved in the program, since we
1012         cannot represent them as input or output ports in the resulting
1013         \small{VHDL}. To remove them, we create a specialized version of the
1014         called function with these arguments filled in. This is done by
1015         the argument propagation transform.
1016
1017         Typically, these arguments are type and dictionary arguments that are
1018         used to make functions polymorphic. By propagating these arguments, we
1019         are essentially doing the same which GHC does when it specializes
1020         functions: Creating multiple variants of the same function, one for
1021         each type for which it is used. Other common non-representable
1022         arguments are functions, e.g. when calling a higher order function
1023         with another function or a lambda abstraction as an argument.
1024
1025         The reason for doing this is similar to the reasoning provided for
1026         the inlining of non-representable let bindings above. In fact, this
1027         argument propagation could be viewed as a form of cross-function
1028         inlining.
1029 \stopitemize
1030
1031 TODO: Check the following itemization.
1032
1033 When looking at the arguments of a builtin function, we can divide them
1034 into categories: 
1035
1036 \startitemize
1037   \item Arguments of a runtime representable type.
1038         
1039         As we have seen with user-defined functions, these arguments can
1040         always be reduced to a simple variable reference, by the
1041         argument extraction transform. Performing this transform for
1042         builtin functions as well, means that the translation of builtin
1043         functions can be limited to signal references, instead of
1044         needing to support all possible expressions.
1045
1046   \item Arguments of a function type.
1047         
1048         These arguments are functions passed to higher order builtins,
1049         like \lam{map} and \lam{foldl}. Since implementing these
1050         functions for arbitrary function-typed expressions (\eg, lambda
1051         expressions) is rather comlex, we reduce these arguments to
1052         (partial applications of) global functions.
1053         
1054         We can still support arbitrary expressions from the user code,
1055         by creating a new global function containing that expression.
1056         This way, we can simply replace the argument with a reference to
1057         that new function. However, since the expression can contain any
1058         number of free variables we also have to include partial
1059         applications in our normal form.
1060
1061         This category of arguments is handled by the function extraction
1062         transform.
1063   \item Other unrepresentable arguments.
1064         
1065         These arguments can take a few different forms:
1066         \startdesc{Type arguments}
1067           In the core language, type arguments can only take a single
1068           form: A type wrapped in the Type constructor. Also, there is
1069           nothing that can be done with type expressions, except for
1070           applying functions to them, so we can simply leave type
1071           arguments as they are.
1072         \stopdesc
1073         \startdesc{Dictionary arguments}
1074           In the core language, dictionary arguments are used to find
1075           operations operating on one of the type arguments (mostly for
1076           finding class methods). Since we will not actually evaluatie
1077           the function body for builtin functions and can generate
1078           code for builtin functions by just looking at the type
1079           arguments, these arguments can be ignored and left as they
1080           are.
1081         \stopdesc
1082         \startdesc{Type level arguments}
1083           Sometimes, we want to pass a value to a builtin function, but
1084           we need to know the value at compile time. Additionally, the
1085           value has an impact on the type of the function. This is
1086           encoded using type-level values, where the actual value of the
1087           argument is not important, but the type encodes some integer,
1088           for example. Since the value is not important, the actual form
1089           of the expression does not matter either and we can leave
1090           these arguments as they are.
1091         \stopdesc
1092         \startdesc{Other arguments}
1093           Technically, there is still a wide array of arguments that can
1094           be passed, but does not fall into any of the above categories.
1095           However, none of the supported builtin functions requires such
1096           an argument. This leaves use with passing unsupported types to
1097           a function, such as calling \lam{head} on a list of functions.
1098
1099           In these cases, it would be impossible to generate hardware
1100           for such a function call anyway, so we can ignore these
1101           arguments.
1102
1103           The only way to generate hardware for builtin functions with
1104           arguments like these, is to expand the function call into an
1105           equivalent core expression (\eg, expand map into a series of
1106           function applications). But for now, we choose to simply not
1107           support expressions like these.
1108         \stopdesc
1109
1110         From the above, we can conclude that we can simply ignore these
1111         other unrepresentable arguments and focus on the first two
1112         categories instead.
1113 \stopitemize
1114
1115 \subsubsection{Argument simplification}
1116 This transform deals with arguments to functions that
1117 are of a runtime representable type. It ensures that they will all become
1118 references to global variables, or local signals in the resulting \small{VHDL}. 
1119
1120 TODO: It seems we can map an expression to a port, not only a signal.
1121 Perhaps this makes this transformation not needed?
1122 TODO: Say something about dataconstructors (without arguments, like True
1123 or False), which are variable references of a runtime representable
1124 type, but do not result in a signal.
1125
1126 To reduce a complex expression to a simple variable reference, we create
1127 a new let expression around the application, which binds the complex
1128 expression to a new variable. The original function is then applied to
1129 this variable.
1130
1131 \starttrans
1132 M N
1133 --------------------    \lam{N} is of a representable type
1134 let x = N in M x        \lam{N} is not a local variable reference
1135 \stoptrans
1136
1137 \startbuffer[from]
1138 add (add a 1) 1
1139 \stopbuffer
1140
1141 \startbuffer[to]
1142 let x = add a 1 in add x 1
1143 \stopbuffer
1144
1145 \transexample{Argument extraction}{from}{to}
1146
1147 \subsubsection{Function extraction}
1148 This transform deals with function-typed arguments to builtin functions.
1149 Since these arguments cannot be propagated, we choose to extract them
1150 into a new global function instead.
1151
1152 Any free variables occuring in the extracted arguments will become
1153 parameters to the new global function. The original argument is replaced
1154 with a reference to the new function, applied to any free variables from
1155 the original argument.
1156
1157 This transformation is useful when applying higher order builtin functions
1158 like \hs{map} to a lambda abstraction, for example. In this case, the code
1159 that generates \small{VHDL} for \hs{map} only needs to handle top level functions and
1160 partial applications, not any other expression (such as lambda abstractions or
1161 even more complicated expressions).
1162
1163 \starttrans
1164 M N                     \lam{M} is a (partial aplication of a) builtin function.
1165 ---------------------   \lam{f0 ... fn} = free local variables of \lam{N}
1166 M x f0 ... fn           \lam{N :: a -> b}
1167 ~                       \lam{N} is not a (partial application of) a top level function
1168 x = λf0 ... λfn.N
1169 \stoptrans
1170
1171 \startbuffer[from]
1172 map (λa . add a b) xs
1173
1174 map (add b) ys
1175 \stopbuffer
1176
1177 \startbuffer[to]
1178 x0 = λb.λa.add a b
1179 ~
1180 map x0 xs
1181
1182 x1 = λb.add b
1183 map x1 ys
1184 \stopbuffer
1185
1186 \transexample{Function extraction}{from}{to}
1187
1188 \subsubsection{Argument propagation}
1189 This transform deals with arguments to user-defined functions that are
1190 not representable at runtime. This means these arguments cannot be
1191 preserved in the final form and most be {\em propagated}.
1192
1193 Propagation means to create a specialized version of the called
1194 function, with the propagated argument already filled in. As a simple
1195 example, in the following program:
1196
1197 \startlambda
1198 f = λa.λb.a + b
1199 inc = λa.f a 1
1200 \stoplambda
1201
1202 we could {\em propagate} the constant argument 1, with the following
1203 result:
1204
1205 \startlambda
1206 f' = λa.a + 1
1207 inc = λa.f' a
1208 \stoplambda
1209
1210 Special care must be taken when the to-be-propagated expression has any
1211 free variables. If this is the case, the original argument should not be
1212 removed alltogether, but replaced by all the free variables of the
1213 expression. In this way, the original expression can still be evaluated
1214 inside the new function. Also, this brings us closer to our goal: All
1215 these free variables will be simple variable references.
1216
1217 To prevent us from propagating the same argument over and over, a simple
1218 local variable reference is not propagated (since is has exactly one
1219 free variable, itself, we would only replace that argument with itself).
1220
1221 This shows that any free local variables that are not runtime representable
1222 cannot be brought into normal form by this transform. We rely on an
1223 inlining transformation to replace such a variable with an expression we
1224 can propagate again.
1225
1226 \starttrans
1227 x = E
1228 ~
1229 x Y0 ... Yi ... Yn                               \lam{Yi} is not of a runtime representable type
1230 ---------------------------------------------    \lam{Yi} is not a local variable reference
1231 x' y0 ... yi-1 f0 ...  fm Yi+1 ... Yn            \lam{f0 ... fm} = free local vars of \lam{Yi}
1232 ~
1233 x' = λy0 ... yi-1 f0 ... fm yi+1 ... yn .       
1234       E y0 ... yi-1 Yi yi+1 ... yn   
1235
1236 \stoptrans
1237
1238 TODO: Example
1239
1240 \subsection{Cast propagation / simplification}
1241 This transform pushes casts down into the expression as far as possible. Since
1242 its exact role and need is not clear yet, this transformation is not yet
1243 specified.
1244
1245 \subsection{Return value simplification}
1246 This transformation ensures that the return value of a function is always a
1247 simple local variable reference.
1248
1249 Currently implemented using lambda simplification, let simplification, and
1250 top simplification. Should change into something like the following, which
1251 works only on the result of a function instead of any subexpression. This is
1252 achieved by the contexts, like \lam{x = E}, though this is strictly not
1253 correct (you could read this as "if there is any function \lam{x} that binds
1254 \lam{E}, any \lam{E} can be transformed, while we only mean the \lam{E} that
1255 is bound by \lam{x}. This might need some extra notes or something).
1256
1257 \starttrans
1258 x = E                            \lam{E} is representable
1259 ~                                \lam{E} is not a lambda abstraction
1260 E                                \lam{E} is not a let expression
1261 ---------------------------      \lam{E} is not a local variable reference
1262 let x = E in x
1263 \stoptrans
1264
1265 \starttrans
1266 x = λv0 ... λvn.E
1267 ~                                \lam{E} is representable
1268 E                                \lam{E} is not a let expression
1269 ---------------------------      \lam{E} is not a local variable reference
1270 let x = E in x
1271 \stoptrans
1272
1273 \starttrans
1274 x = λv0 ... λvn.let ... in E
1275 ~                                \lam{E} is representable
1276 E                                \lam{E} is not a local variable reference
1277 ---------------------------
1278 let x = E in x
1279 \stoptrans
1280
1281 \startbuffer[from]
1282 x = add 1 2
1283 \stopbuffer
1284
1285 \startbuffer[to]
1286 x = let x = add 1 2 in x
1287 \stopbuffer
1288
1289 \transexample{Return value simplification}{from}{to}