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.
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.
9 \framed[offset=1mm,align=right,strut=no,background=box,frame=off]{
10 \setuptyping[option=LAM,style=sans,before=,after=,strip=auto]
12 \setuptyping[option=none,style=\tttf,strip=auto]
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}
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
31 \todo{Describe core properties not supported in \VHDL, and describe how the
32 \VHDL we want to generate should look like.}
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
42 \placedefinition{}{A program is in \emph{normal form} if none of the
43 transformations from this chapter apply.}
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
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.
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.
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.
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
71 \todo{Intermezzo: functions vs plain values}
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.
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.
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
89 \todo{Add generated VHDL}
92 alu :: Bit -> Word -> Word -> Word
101 \startuseMPgraphic{MulSum}
102 save a, b, c, mul, add, sum;
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)";
111 newCircle.mul(btex * etex);
112 newCircle.add(btex + etex);
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);
121 % Draw objects and lines
122 drawObj(a, b, c, mul, add, sum);
124 ncarc(a)(mul) "arcangle(15)";
125 ncarc(b)(mul) "arcangle(-15)";
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.}
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
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}.
153 \startbuffer[AddSubAlu]
154 alu :: Bit -> Word -> Word -> Word
166 \startuseMPgraphic{AddSubAlu}
167 save opcode, a, b, add, sub, mux, res;
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)";
175 newCircle.add(btex + etex);
176 newCircle.sub(btex - etex);
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);
187 % Draw objects and lines
188 drawObj(opcode, a, b, res, add, sub, mux);
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)";
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.}
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.
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.
221 \startbuffer[NormalComplete]
224 -> State (Word, Word)
225 -> (State (Word, Word), Word)
227 -- All arguments are an inital lambda (address, data, packed state)
229 -- There are nested let expressions at top level
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.
239 -- Conditional connections
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
261 \startuseMPgraphic{NormalComplete}
262 save a, d, r, foo, muxr, muxout, out;
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)";
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)";
274 % Reflect over the vertical axis
275 reflectObj(muxr1)((0,0), (0,1));
278 rotateObj(muxout)(-90);
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);
287 muxout.c = midpoint(r1.c, r2.c) - (0cm, 2cm);
288 out.c = muxout.c - (0cm, 1.5cm);
290 % % Draw objects and lines
291 drawObj(a, d, foo, r1, r2, muxr1, muxr2, muxout, out);
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)";
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)";
309 \todo{Don't split registers in this image?}
310 \placeexample[here][ex:NormalComplete]{Simple architecture consisting of an adder and a
312 \startcombination[2*1]
313 {\typebufferlam{NormalComplete}}{Core description in normal form.}
314 {\boxedgraphic{NormalComplete}}{The architecture described by the normal form.}
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).
325 Some clauses have an expression listed in parentheses. These are conditions
326 that need to apply to the clause.
328 \defref{intended normal form definition}
329 \todo{Fix indentation}
331 \italic{normal} = \italic{lambda}
332 \italic{lambda} = λvar.\italic{lambda} (representable(var))
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
342 | case var of C a0 ... an -> ai (lvar(var))
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}
357 \todo{Limit builtinarg further}
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}
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
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
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.
380 Such a transformation description looks like the following.
385 <original expression>
386 -------------------------- <expression conditions>
387 <transformed expresssion>
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:
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.
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}.
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.
418 Only if these conditions are \emph{all} true, the transformation
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>}.
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}.
432 Only if a top level binder exists that matches each binder and pattern,
433 the transformation applies.
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.
445 Any binder (lowercase) placeholder that has no value bound to it yet will be
446 bound to (and replaced with) a fresh binder.
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.
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.
459 As an example, we'll look at η-abstraction:
463 -------------- \lam{E} does not occur on a function position in an application
464 λx.E x \lam{E} is not a lambda abstraction.
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).
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).
482 alu :: Bit -> Word -> Word -> Word
483 alu = λopcode. case opcode of
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.
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.
498 λopcode. case opcode of
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}).
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.
511 The next expression we could apply this transformation to, is the body of
512 the lambda abstraction:
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.
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:
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
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
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
563 (case opcode of Low -> (+); High -> (-)) a b
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:
574 (case opcode of Low -> (+); High -> (-)) a
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
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.
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}).
597 The complete function then becomes:
599 (case opcode of Low -> λa1.(+) a1; High -> (-)) a
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
611 (case opcode of Low -> λa1.λb1.(+) a1 b1; High -> (-)) a
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:
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
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.
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.
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.
641 \subsection{Definitions}
642 In the following sections, we will be using a number of functions and
643 notations, which we will define here.
645 \todo{Define substitution (notation)}
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}
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}
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}
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}.
672 A \emph{user-defined} function is a function for which we do have a Cλash
673 implementation available.
675 \subsubsection{Predicates}
676 Here, we define a number of predicates that can be used below to concisely
677 specify conditions.\refdef{global variable}
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.
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.
685 \refdef{representable}\emph{representable(expr)} or \emph{representable(var)} is true when
686 \emph{expr} or \emph{var} is \emph{representable}.
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
695 (λa.λb.λc. a * b * c) x c
698 By applying β-reduction (see \in{section}[sec:normalization:beta]) once,
699 we can simplify this expression to:
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:
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
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}.
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
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
742 (let x = 1 in x) + (let x = 2 in x)
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
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.
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
762 \todo{Define fresh binders and unique supplies}
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
784 \section{Transform passes}
785 In this section we describe the actual transforms.
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].
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.
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
803 \subsubsection[sec:normalization:beta]{β-reduction}
804 β-reduction is a well known transformation from lambda calculus, where it is
805 the main reduction step. It reduces applications of lambda abstractions,
806 removing both the lambda abstraction and the application.
808 In our transformation system, this step helps to remove unwanted lambda
809 abstractions (basically all but the ones at the top level). Other
810 transformations (application propagation, non-representable inlining) make
811 sure that most lambda abstractions will eventually be reducable by
829 \transexample{beta}{β-reduction}{from}{to}
831 \subsubsection{Empty let removal}
832 This transformation is simple: It removes recursive lets that have no bindings
833 (which usually occurs when unused let binding removal removes the last
836 Note that there is no need to define this transformation for
837 non-recursive lets, since they always contain exactly one binding.
847 \subsubsection{Simple let binding removal}
848 This transformation inlines simple let bindings, that bind some
849 binder to some other binder instead of a more complex expression (\ie
852 This transformation is not needed to get an expression into intended
853 normal form (since these bindings are part of the intended normal
854 form), but makes the resulting \small{VHDL} a lot shorter.
865 ----------------------------- \lam{b} is a variable reference
866 letrec \lam{ai} ≠ \lam{b}
879 \subsubsection{Unused let binding removal}
880 This transformation removes let bindings that are never used.
881 Occasionally, \GHC's desugarer introduces some unused let bindings.
883 This normalization pass should really be unneeded to get into intended normal form
884 (since unused bindings are not forbidden by the normal form), but in practice
885 the desugarer or simplifier emits some unused bindings that cannot be
886 normalized (e.g., calls to a \type{PatError}\todo{Check this name}). Also,
887 this transformation makes the resulting \small{VHDL} a lot shorter.
889 \todo{Don't use old-style numerals in transformations}
898 M \lam{ai} does not occur free in \lam{M}
899 ---------------------------- \forall j, 0 ≤ j ≤ n, j ≠ i (\lam{ai} does not occur free in \lam{Ej})
913 \subsubsection{Cast propagation / simplification}
914 This transform pushes casts down into the expression as far as possible.
915 Since its exact role and need is not clear yet, this transformation is
918 \todo{Cast propagation}
920 \subsubsection{Top level binding inlining}
921 This transform takes simple top level bindings generated by the
922 \small{GHC} compiler. \small{GHC} sometimes generates very simple
923 \quote{wrapper} bindings, which are bound to just a variable
924 reference, or a partial application to constants or other variable
927 Note that this transformation is completely optional. It is not
928 required to get any function into intended normal form, but it does help making
929 the resulting VHDL output easier to read (since it removes a bunch of
930 components that are really boring).
932 This transform takes any top level binding generated by the compiler,
933 whose normalized form contains only a single let binding.
936 x = λa0 ... λan.let y = E in y
939 -------------------------------------- \lam{x} is generated by the compiler
940 λa0 ... λan.let y = E in y
944 (+) :: Word -> Word -> Word
945 (+) = GHC.Num.(+) @Word $dNum
950 GHC.Num.(+) @ Alu.Word $dNum a b
953 \transexample{toplevelinline}{Top level binding inlining}{from}{to}
955 \in{Example}[ex:trans:toplevelinline] shows a typical application of
956 the addition operator generated by \GHC. The type and dictionary
957 arguments used here are described in
958 \in{Section}[section:prototype:polymorphism].
960 Without this transformation, there would be a \lam{(+)} entity
961 in the \VHDL which would just add its inputs. This generates a
962 lot of overhead in the \VHDL, which is particularly annoying
963 when browsing the generated RTL schematic (especially since most
964 non-alphanumerics, like all characters in \lam{(+)}, are not
965 allowed in \VHDL architecture names\footnote{Technically, it is
966 allowed to use non-alphanumerics when using extended
967 identifiers, but it seems that none of the tooling likes
968 extended identifiers in filenames, so it effectively doesn't
969 work.}, so the entity would be called \quote{w7aA7f} or
970 something similarly unreadable and autogenerated).
972 \subsection{Program structure}
973 These transformations are aimed at normalizing the overall structure
974 into the intended form. This means ensuring there is a lambda abstraction
975 at the top for every argument (input port or current state), putting all
976 of the other value definitions in let bindings and making the final
977 return value a simple variable reference.
979 \subsubsection{η-abstraction}
980 This transformation makes sure that all arguments of a function-typed
981 expression are named, by introducing lambda expressions. When combined with
982 β-reduction and non-representable binding inlining, all function-typed
983 expressions should be lambda abstractions or global identifiers.
987 -------------- \lam{E} is not the first argument of an application.
988 λx.E x \lam{E} is not a lambda abstraction.
989 \lam{x} is a variable that does not occur free in \lam{E}.
999 foo = λa.λx.(case a of
1004 \transexample{eta}{η-abstraction}{from}{to}
1006 \subsubsection{Application propagation}
1007 This transformation is meant to propagate application expressions downwards
1008 into expressions as far as possible. This allows partial applications inside
1009 expressions to become fully applied and exposes new transformation
1010 opportunities for other transformations (like β-reduction and
1013 Since all binders in our expression are unique (see
1014 \in{section}[sec:normalization:uniq]), there is no risk that we will
1015 introduce unintended shadowing by moving an expression into a lower
1016 scope. Also, since only move expression into smaller scopes (down into
1017 our expression), there is no risk of moving a variable reference out
1018 of the scope in which it is defined.
1021 (letrec binds in E) M
1022 ------------------------
1042 \transexample{appproplet}{Application propagation for a let expression}{from}{to}
1070 \transexample{apppropcase}{Application propagation for a case expression}{from}{to}
1072 \subsubsection{Let recursification}
1073 This transformation makes all non-recursive lets recursive. In the
1074 end, we want a single recursive let in our normalized program, so all
1075 non-recursive lets can be converted. This also makes other
1076 transformations simpler: They can simply assume all lets are
1084 ------------------------------------------
1091 \subsubsection{Let flattening}
1092 This transformation puts nested lets in the same scope, by lifting the
1093 binding(s) of the inner let into the outer let. Eventually, this will
1094 cause all let bindings to appear in the same scope.
1096 This transformation only applies to recursive lets, since all
1097 non-recursive lets will be made recursive (see
1098 \in{section}[sec:normalization:letrecurse]).
1100 Since we are joining two scopes together, there is no risk of moving a
1101 variable reference out of the scope where it is defined.
1107 ai = (letrec bindings in M)
1112 ------------------------------------------
1147 \transexample{letflat}{Let flattening}{from}{to}
1149 \subsubsection{Return value simplification}
1150 This transformation ensures that the return value of a function is always a
1151 simple local variable reference.
1153 Currently implemented using lambda simplification, let simplification, and
1154 top simplification. Should change into something like the following, which
1155 works only on the result of a function instead of any subexpression. This is
1156 achieved by the contexts, like \lam{x = E}, though this is strictly not
1157 correct (you could read this as "if there is any function \lam{x} that binds
1158 \lam{E}, any \lam{E} can be transformed, while we only mean the \lam{E} that
1159 is bound by \lam{x}. This might need some extra notes or something).
1161 Note that the return value is not simplified if its not representable.
1162 Otherwise, this would cause a direct loop with the inlining of
1163 unrepresentable bindings. If the return value is not
1164 representable because it has a function type, η-abstraction should
1165 make sure that this transformation will eventually apply. If the value
1166 is not representable for other reasons, the function result itself is
1167 not representable, meaning this function is not translatable anyway.
1170 x = E \lam{E} is representable
1171 ~ \lam{E} is not a lambda abstraction
1172 E \lam{E} is not a let expression
1173 --------------------------- \lam{E} is not a local variable reference
1179 ~ \lam{E} is representable
1180 E \lam{E} is not a let expression
1181 --------------------------- \lam{E} is not a local variable reference
1186 x = λv0 ... λvn.let ... in E
1187 ~ \lam{E} is representable
1188 E \lam{E} is not a local variable reference
1189 -----------------------------
1198 x = letrec x = add 1 2 in x
1201 \transexample{retvalsimpl}{Return value simplification}{from}{to}
1203 \todo{More examples}
1205 \subsection{Argument simplification}
1206 The transforms in this section deal with simplifying application
1207 arguments into normal form. The goal here is to:
1209 \todo{This section should only talk about representable arguments. Non
1210 representable arguments are treated by specialization.}
1213 \item Make all arguments of user-defined functions (\eg, of which
1214 we have a function body) simple variable references of a runtime
1215 representable type. This is needed, since these applications will be turned
1216 into component instantiations.
1217 \item Make all arguments of builtin functions one of:
1219 \item A type argument.
1220 \item A dictionary argument.
1221 \item A type level expression.
1222 \item A variable reference of a runtime representable type.
1223 \item A variable reference or partial application of a function type.
1227 When looking at the arguments of a user-defined function, we can
1228 divide them into two categories:
1230 \item Arguments of a runtime representable type (\eg bits or vectors).
1232 These arguments can be preserved in the program, since they can
1233 be translated to input ports later on. However, since we can
1234 only connect signals to input ports, these arguments must be
1235 reduced to simple variables (for which signals will be
1236 produced). This is taken care of by the argument extraction
1238 \item Non-runtime representable typed arguments. \todo{Move this
1239 bullet to specialization}
1241 These arguments cannot be preserved in the program, since we
1242 cannot represent them as input or output ports in the resulting
1243 \small{VHDL}. To remove them, we create a specialized version of the
1244 called function with these arguments filled in. This is done by
1245 the argument propagation transform.
1247 Typically, these arguments are type and dictionary arguments that are
1248 used to make functions polymorphic. By propagating these arguments, we
1249 are essentially doing the same which GHC does when it specializes
1250 functions: Creating multiple variants of the same function, one for
1251 each type for which it is used. Other common non-representable
1252 arguments are functions, e.g. when calling a higher order function
1253 with another function or a lambda abstraction as an argument.
1255 The reason for doing this is similar to the reasoning provided for
1256 the inlining of non-representable let bindings above. In fact, this
1257 argument propagation could be viewed as a form of cross-function
1261 \todo{Move this itemization into a new section about builtin functions}
1262 When looking at the arguments of a builtin function, we can divide them
1266 \item Arguments of a runtime representable type.
1268 As we have seen with user-defined functions, these arguments can
1269 always be reduced to a simple variable reference, by the
1270 argument extraction transform. Performing this transform for
1271 builtin functions as well, means that the translation of builtin
1272 functions can be limited to signal references, instead of
1273 needing to support all possible expressions.
1275 \item Arguments of a function type.
1277 These arguments are functions passed to higher order builtins,
1278 like \lam{map} and \lam{foldl}. Since implementing these
1279 functions for arbitrary function-typed expressions (\eg, lambda
1280 expressions) is rather comlex, we reduce these arguments to
1281 (partial applications of) global functions.
1283 We can still support arbitrary expressions from the user code,
1284 by creating a new global function containing that expression.
1285 This way, we can simply replace the argument with a reference to
1286 that new function. However, since the expression can contain any
1287 number of free variables we also have to include partial
1288 applications in our normal form.
1290 This category of arguments is handled by the function extraction
1292 \item Other unrepresentable arguments.
1294 These arguments can take a few different forms:
1295 \startdesc{Type arguments}
1296 In the core language, type arguments can only take a single
1297 form: A type wrapped in the Type constructor. Also, there is
1298 nothing that can be done with type expressions, except for
1299 applying functions to them, so we can simply leave type
1300 arguments as they are.
1302 \startdesc{Dictionary arguments}
1303 In the core language, dictionary arguments are used to find
1304 operations operating on one of the type arguments (mostly for
1305 finding class methods). Since we will not actually evaluatie
1306 the function body for builtin functions and can generate
1307 code for builtin functions by just looking at the type
1308 arguments, these arguments can be ignored and left as they
1311 \startdesc{Type level arguments}
1312 Sometimes, we want to pass a value to a builtin function, but
1313 we need to know the value at compile time. Additionally, the
1314 value has an impact on the type of the function. This is
1315 encoded using type-level values, where the actual value of the
1316 argument is not important, but the type encodes some integer,
1317 for example. Since the value is not important, the actual form
1318 of the expression does not matter either and we can leave
1319 these arguments as they are.
1321 \startdesc{Other arguments}
1322 Technically, there is still a wide array of arguments that can
1323 be passed, but does not fall into any of the above categories.
1324 However, none of the supported builtin functions requires such
1325 an argument. This leaves use with passing unsupported types to
1326 a function, such as calling \lam{head} on a list of functions.
1328 In these cases, it would be impossible to generate hardware
1329 for such a function call anyway, so we can ignore these
1332 The only way to generate hardware for builtin functions with
1333 arguments like these, is to expand the function call into an
1334 equivalent core expression (\eg, expand map into a series of
1335 function applications). But for now, we choose to simply not
1336 support expressions like these.
1339 From the above, we can conclude that we can simply ignore these
1340 other unrepresentable arguments and focus on the first two
1344 \subsubsection{Argument simplification}
1345 This transform deals with arguments to functions that
1346 are of a runtime representable type. It ensures that they will all become
1347 references to global variables, or local signals in the resulting
1348 \small{VHDL}, which is required due to limitations in the component
1349 instantiation code in \VHDL (one can only assign a signal or constant
1350 to an input port). By ensuring that all arguments are always simple
1351 variable references, we always have a signal available to assign to
1354 \todo{Say something about dataconstructors (without arguments, like True
1355 or False), which are variable references of a runtime representable
1356 type, but do not result in a signal.}
1358 To reduce a complex expression to a simple variable reference, we create
1359 a new let expression around the application, which binds the complex
1360 expression to a new variable. The original function is then applied to
1363 Note that a reference to a \emph{global variable} (like a top level
1364 function without arguments, but also an argumentless dataconstructors
1365 like \lam{True}) is also simplified. Only local variables generate
1366 signals in the resulting architecture.
1368 \refdef{representable}
1371 -------------------- \lam{N} is representable
1372 letrec x = N in M x \lam{N} is not a local variable reference
1374 \refdef{local variable}
1381 letrec x = add a 1 in add x 1
1384 \transexample{argextract}{Argument extraction}{from}{to}
1386 \subsubsection{Function extraction}
1387 \todo{Move to section about builtin functions}
1388 This transform deals with function-typed arguments to builtin
1389 functions. Since builtin functions cannot be specialized to remove
1390 the arguments, we choose to extract these arguments into a new global
1391 function instead. This greatly simplifies the translation rules needed
1392 for builtin functions. \todo{Should we talk about these? Reference
1395 Any free variables occuring in the extracted arguments will become
1396 parameters to the new global function. The original argument is replaced
1397 with a reference to the new function, applied to any free variables from
1398 the original argument.
1400 This transformation is useful when applying higher order builtin functions
1401 like \hs{map} to a lambda abstraction, for example. In this case, the code
1402 that generates \small{VHDL} for \hs{map} only needs to handle top level functions and
1403 partial applications, not any other expression (such as lambda abstractions or
1404 even more complicated expressions).
1407 M N \lam{M} is (a partial aplication of) a builtin function.
1408 --------------------- \lam{f0 ... fn} are all free local variables of \lam{N}
1409 M (x f0 ... fn) \lam{N :: a -> b}
1410 ~ \lam{N} is not a (partial application of) a top level function
1414 \todo{Split this example}
1416 map (λa . add a b) xs
1430 \transexample{funextract}{Function extraction}{from}{to}
1432 Note that \lam{x0} and {x1} will still need normalization after this.
1434 \subsubsection{Argument propagation}
1435 \todo{Rename this section to specialization and move it into a
1438 This transform deals with arguments to user-defined functions that are
1439 not representable at runtime. This means these arguments cannot be
1440 preserved in the final form and most be {\em propagated}.
1442 Propagation means to create a specialized version of the called
1443 function, with the propagated argument already filled in. As a simple
1444 example, in the following program:
1451 We could {\em propagate} the constant argument 1, with the following
1459 Special care must be taken when the to-be-propagated expression has any
1460 free variables. If this is the case, the original argument should not be
1461 removed completely, but replaced by all the free variables of the
1462 expression. In this way, the original expression can still be evaluated
1463 inside the new function. Also, this brings us closer to our goal: All
1464 these free variables will be simple variable references.
1466 To prevent us from propagating the same argument over and over, a simple
1467 local variable reference is not propagated (since is has exactly one
1468 free variable, itself, we would only replace that argument with itself).
1470 This shows that any free local variables that are not runtime representable
1471 cannot be brought into normal form by this transform. We rely on an
1472 inlining transformation to replace such a variable with an expression we
1473 can propagate again.
1478 x Y0 ... Yi ... Yn \lam{Yi} is not of a runtime representable type
1479 --------------------------------------------- \lam{Yi} is not a local variable reference
1480 x' y0 ... yi-1 f0 ... fm Yi+1 ... Yn \lam{f0 ... fm} are all free local vars of \lam{Yi}
1482 x' = λy0 ... λyi-1. λf0 ... λfm. λyi+1 ... λyn .
1483 E y0 ... yi-1 Yi yi+1 ... yn
1486 \todo{Describe what the formal specification means}
1487 \todo{Note that we don't change the sepcialised function body, only
1493 \subsection{Case normalisation}
1494 \subsubsection{Scrutinee simplification}
1495 This transform ensures that the scrutinee of a case expression is always
1496 a simple variable reference.
1501 ----------------- \lam{E} is not a local variable reference
1520 \transexample{letflat}{Let flattening}{from}{to}
1523 \subsubsection{Case simplification}
1524 This transformation ensures that all case expressions become normal form. This
1525 means they will become one of:
1527 \item An extractor case with a single alternative that picks a single field
1528 from a datatype, \eg \lam{case x of (a, b) -> a}.
1529 \item A selector case with multiple alternatives and only wild binders, that
1530 makes a choice between expressions based on the constructor of another
1531 expression, \eg \lam{case x of Low -> a; High -> b}.
1534 \defref{wild binder}
1537 C0 v0,0 ... v0,m -> E0
1539 Cn vn,0 ... vn,m -> En
1540 --------------------------------------------------- \forall i \forall j, 0 ≤ i ≤ n, 0 ≤ i < m (\lam{wi,j} is a wild (unused) binder)
1542 v0,0 = case E of C0 v0,0 .. v0,m -> v0,0
1544 v0,m = case E of C0 v0,0 .. v0,m -> v0,m
1546 vn,m = case E of Cn vn,0 .. vn,m -> vn,m
1552 C0 w0,0 ... w0,m -> x0
1554 Cn wn,0 ... wn,m -> xn
1556 \todo{Check the subscripts of this transformation}
1558 Note that this transformation applies to case statements with any
1559 scrutinee. If the scrutinee is a complex expression, this might result
1560 in duplicate hardware. An extra condition to only apply this
1561 transformation when the scrutinee is already simple (effectively
1562 causing this transformation to be only applied after the scrutinee
1563 simplification transformation) might be in order.
1565 \fxnote{This transformation specified like this is complicated and misses
1566 conditions to prevent looping with itself. Perhaps it should be split here for
1585 \transexample{selcasesimpl}{Selector case simplification}{from}{to}
1593 b = case a of (,) b c -> b
1594 c = case a of (,) b c -> c
1601 \transexample{excasesimpl}{Extractor case simplification}{from}{to}
1603 \refdef{selector case}
1604 In \in{example}[ex:trans:excasesimpl] the case expression is expanded
1605 into multiple case expressions, including a pretty useless expression
1606 (that is neither a selector or extractor case). This case can be
1607 removed by the Case removal transformation in
1608 \in{section}[sec:transformation:caseremoval].
1610 \subsubsection[sec:transformation:caseremoval]{Case removal}
1611 This transform removes any case statements with a single alternative and
1614 These "useless" case statements are usually leftovers from case simplification
1615 on extractor case (see the previous example).
1620 ---------------------- \lam{\forall i, 0 ≤ i ≤ m} (\lam{vi} does not occur free in E)
1633 \transexample{caserem}{Case removal}{from}{to}
1635 \todo{Move these two sections somewhere? Perhaps not?}
1636 \subsection{Removing polymorphism}
1637 Reference type-specialization (== argument propagation)
1639 Reference polymporphic binding inlining (== non-representable binding
1642 \subsection{Defunctionalization}
1643 These transformations remove most higher order expressions from our
1644 program, making it completely first-order (the only exception here is for
1645 arguments to builtin functions, since we can't specialize builtin
1646 function. \todo{Talk more about this somewhere}
1648 Reference higher-order-specialization (== argument propagation)
1650 \subsubsection{Non-representable binding inlining}
1651 \todo{Move this section into a new section (together with
1653 This transform inlines let bindings that are bound to a
1654 non-representable value. Since we can never generate a signal
1655 assignment for these bindings (we cannot declare a signal assignment
1656 with a non-representable type, for obvious reasons), we have no choice
1657 but to inline the binding to remove it.
1659 If the binding is non-representable because it is a lambda abstraction, it is
1660 likely that it will inlined into an application and β-reduction will remove
1661 the lambda abstraction and turn it into a representable expression at the
1662 inline site. The same holds for partial applications, which can be turned into
1663 full applications by inlining.
1665 Other cases of non-representable bindings we see in practice are primitive
1666 Haskell types. In most cases, these will not result in a valid normalized
1667 output, but then the input would have been invalid to start with. There is one
1668 exception to this: When a builtin function is applied to a non-representable
1669 expression, things might work out in some cases. For example, when you write a
1670 literal \hs{SizedInt} in Haskell, like \hs{1 :: SizedInt D8}, this results in
1671 the following core: \lam{fromInteger (smallInteger 10)}, where for example
1672 \lam{10 :: GHC.Prim.Int\#} and \lam{smallInteger 10 :: Integer} have
1673 non-representable types. \todo{Expand on this. This/these paragraph(s)
1674 should probably become a separate discussion somewhere else}
1676 \todo{Can this duplicate work?}
1687 -------------------------- \lam{Ei} has a non-representable type.
1689 a0 = E0 [ai=>Ei] \vdots
1690 ai-1 = Ei-1 [ai=>Ei]
1691 ai+1 = Ei+1 [ai=>Ei]
1710 x = fromInteger (smallInteger 10)
1712 (λb -> add b 1) (add 1 x)
1715 \transexample{nonrepinline}{Nonrepresentable binding inlining}{from}{to}
1718 \section[sec:normalization:properties]{Provable properties}
1719 When looking at the system of transformations outlined above, there are a
1720 number of questions that we can ask ourselves. The main question is of course:
1721 \quote{Does our system work as intended?}. We can split this question into a
1722 number of subquestions:
1725 \item[q:termination] Does our system \emph{terminate}? Since our system will
1726 keep running as long as transformations apply, there is an obvious risk that
1727 it will keep running indefinitely. This typically happens when one
1728 transformation produces a result that is transformed back to the original
1729 by another transformation, or when one or more transformations keep
1730 expanding some expression.
1731 \item[q:soundness] Is our system \emph{sound}? Since our transformations
1732 continuously modify the expression, there is an obvious risk that the final
1733 normal form will not be equivalent to the original program: Its meaning could
1735 \item[q:completeness] Is our system \emph{complete}? Since we have a complex
1736 system of transformations, there is an obvious risk that some expressions will
1737 not end up in our intended normal form, because we forgot some transformation.
1738 In other words: Does our transformation system result in our intended normal
1739 form for all possible inputs?
1740 \item[q:determinism] Is our system \emph{deterministic}? Since we have defined
1741 no particular order in which the transformation should be applied, there is an
1742 obvious risk that different transformation orderings will result in
1743 \emph{different} normal forms. They might still both be intended normal forms
1744 (if our system is \emph{complete}) and describe correct hardware (if our
1745 system is \emph{sound}), so this property is less important than the previous
1746 three: The translator would still function properly without it.
1749 Unfortunately, the final transformation system has only been
1750 developed in the final part of the research, leaving no more time
1751 for verifying these properties. In fact, it is likely that the
1752 current transformation system still violates some of these
1753 properties in some cases and should be improved (or extra conditions
1754 on the input hardware descriptions should be formulated).
1756 This is most likely the case with the completeness and determinism
1757 properties, perhaps als the termination property. The soundness
1758 property probably holds, since it is easier to manually verify (each
1759 transformation can be reviewed separately).
1761 Even though no complete proofs have been made, some ideas for
1762 possible proof strategies are shown below.
1764 \subsection{Graph representation}
1765 Before looking into how to prove these properties, we'll look at our
1766 transformation system from a graph perspective. The nodes of the graph are
1767 all possible Core expressions. The (directed) edges of the graph are
1768 transformations. When a transformation α applies to an expression \lam{A} to
1769 produce an expression \lam{B}, we add an edge from the node for \lam{A} to the
1770 node for \lam{B}, labeled α.
1772 \startuseMPgraphic{TransformGraph}
1776 newCircle.a(btex \lam{(λx.λy. (+) x y) 1} etex);
1777 newCircle.b(btex \lam{λy. (+) 1 y} etex);
1778 newCircle.c(btex \lam{(λx.(+) x) 1} etex);
1779 newCircle.d(btex \lam{(+) 1} etex);
1782 c.c = b.c + (4cm, 0cm);
1783 a.c = midpoint(b.c, c.c) + (0cm, 4cm);
1784 d.c = midpoint(b.c, c.c) - (0cm, 3cm);
1786 % β-conversion between a and b
1787 ncarc.a(a)(b) "name(bred)";
1788 ObjLabel.a(btex $\xrightarrow[normal]{}{β}$ etex) "labpathname(bred)", "labdir(rt)";
1789 ncarc.b(b)(a) "name(bexp)", "linestyle(dashed withdots)";
1790 ObjLabel.b(btex $\xleftarrow[normal]{}{β}$ etex) "labpathname(bexp)", "labdir(lft)";
1792 % η-conversion between a and c
1793 ncarc.a(a)(c) "name(ered)";
1794 ObjLabel.a(btex $\xrightarrow[normal]{}{η}$ etex) "labpathname(ered)", "labdir(rt)";
1795 ncarc.c(c)(a) "name(eexp)", "linestyle(dashed withdots)";
1796 ObjLabel.c(btex $\xleftarrow[normal]{}{η}$ etex) "labpathname(eexp)", "labdir(lft)";
1798 % η-conversion between b and d
1799 ncarc.b(b)(d) "name(ered)";
1800 ObjLabel.b(btex $\xrightarrow[normal]{}{η}$ etex) "labpathname(ered)", "labdir(rt)";
1801 ncarc.d(d)(b) "name(eexp)", "linestyle(dashed withdots)";
1802 ObjLabel.d(btex $\xleftarrow[normal]{}{η}$ etex) "labpathname(eexp)", "labdir(lft)";
1804 % β-conversion between c and d
1805 ncarc.c(c)(d) "name(bred)";
1806 ObjLabel.c(btex $\xrightarrow[normal]{}{β}$ etex) "labpathname(bred)", "labdir(rt)";
1807 ncarc.d(d)(c) "name(bexp)", "linestyle(dashed withdots)";
1808 ObjLabel.d(btex $\xleftarrow[normal]{}{β}$ etex) "labpathname(bexp)", "labdir(lft)";
1810 % Draw objects and lines
1811 drawObj(a, b, c, d);
1814 \placeexample[right][ex:TransformGraph]{Partial graph of a lambda calculus
1815 system with β and η reduction (solid lines) and expansion (dotted lines).}
1816 \boxedgraphic{TransformGraph}
1818 Of course our graph is unbounded, since we can construct an infinite amount of
1819 Core expressions. Also, there might potentially be multiple edges between two
1820 given nodes (with different labels), though seems unlikely to actually happen
1823 See \in{example}[ex:TransformGraph] for the graph representation of a very
1824 simple lambda calculus that contains just the expressions \lam{(λx.λy. (+) x
1825 y) 1}, \lam{λy. (+) 1 y}, \lam{(λx.(+) x) 1} and \lam{(+) 1}. The
1826 transformation system consists of β-reduction and η-reduction (solid edges) or
1827 β-expansion and η-expansion (dotted edges).
1829 \todo{Define β-reduction and η-reduction?}
1831 Note that the normal form of such a system consists of the set of nodes
1832 (expressions) without outgoing edges, since those are the expression to which
1833 no transformation applies anymore. We call this set of nodes the \emph{normal
1834 set}. The set of nodes containing expressions in intended normal
1835 form \refdef{intended normal form} is called the \emph{intended
1838 From such a graph, we can derive some properties easily:
1840 \item A system will \emph{terminate} if there is no path of infinite length
1841 in the graph (this includes cycles, but can also happen without cycles).
1842 \item Soundness is not easily represented in the graph.
1843 \item A system is \emph{complete} if all of the nodes in the normal set have
1844 the intended normal form. The inverse (that all of the nodes outside of
1845 the normal set are \emph{not} in the intended normal form) is not
1846 strictly required. In other words, our normal set must be a
1847 subset of the intended normal form, but they do not need to be
1850 \item A system is deterministic if all paths starting at a particular
1851 node, which end in a node in the normal set, end at the same node.
1854 When looking at the \in{example}[ex:TransformGraph], we see that the system
1855 terminates for both the reduction and expansion systems (but note that, for
1856 expansion, this is only true because we've limited the possible
1857 expressions. In comlete lambda calculus, there would be a path from
1858 \lam{(λx.λy. (+) x y) 1} to \lam{(λx.λy.(λz.(+) z) x y) 1} to
1859 \lam{(λx.λy.(λz.(λq.(+) q) z) x y) 1} etc.)
1861 If we would consider the system with both expansion and reduction, there
1862 would no longer be termination either, since there would be cycles all
1865 The reduction and expansion systems have a normal set of containing just
1866 \lam{(+) 1} or \lam{(λx.λy. (+) x y) 1} respectively. Since all paths in
1867 either system end up in these normal forms, both systems are \emph{complete}.
1868 Also, since there is only one node in the normal set, it must obviously be
1869 \emph{deterministic} as well.
1871 \todo{Add content to these sections}
1872 \subsection{Termination}
1873 In general, proving termination of an arbitrary program is a very
1874 hard problem. \todo{Ref about arbitrary termination} Fortunately,
1875 we only have to prove termination for our specific transformation
1878 A common approach for these kinds of proofs is to associate a
1879 measure with each possible expression in our system. If we can
1880 show that each transformation strictly decreases this measure
1881 (\ie, the expression transformed to has a lower measure than the
1882 expression transformed from). \todo{ref about measure-based
1883 termination proofs / analysis}
1885 A good measure for a system consisting of just β-reduction would
1886 be the number of lambda expressions in the expression. Since every
1887 application of β-reduction removes a lambda abstraction (and there
1888 is always a bounded number of lambda abstractions in every
1889 expression) we can easily see that a transformation system with
1890 just β-reduction will always terminate.
1892 For our complete system, this measure would be fairly complex
1893 (probably the sum of a lot of things). Since the (conditions on)
1894 our transformations are pretty complex, we would need to include
1895 both simple things like the number of let expressions as well as
1896 more complex things like the number of case expressions that are
1897 not yet in normal form.
1899 No real attempt has been made at finding a suitable measure for
1902 \subsection{Soundness}
1903 Soundness is a property that can be proven for each transformation
1904 separately. Since our system only runs separate transformations
1905 sequentially, if each of our transformations leaves the
1906 \emph{meaning} of the expression unchanged, then the entire system
1907 will of course leave the meaning unchanged and is thus
1910 The current prototype has only been verified in an ad-hoc fashion
1911 by inspecting (the code for) each transformation. A more formal
1912 verification would be more appropriate.
1914 To be able to formally show that each transformation properly
1915 preserves the meaning of every expression, we require an exact
1916 definition of the \emph{meaning} of every expression, so we can
1917 compare them. Currently there seems to be no formal definition of
1918 the meaning or semantics of \GHC's core language, only informal
1919 descriptions are available.
1921 It should be possible to have a single formal definition of
1922 meaning for Core for both normal Core compilation by \GHC and for
1923 our compilation to \VHDL. The main difference seems to be that in
1924 hardware every expression is always evaluated, while in software
1925 it is only evaluated if needed, but it should be possible to
1926 assign a meaning to core expressions that assumes neither.
1928 Since each of the transformations can be applied to any
1929 subexpression as well, there is a constraint on our meaning
1930 definition: The meaning of an expression should depend only on the
1931 meaning of subexpressions, not on the expressions themselves. For
1932 example, the meaning of the application in \lam{f (let x = 4 in
1933 x)} should be the same as the meaning of the application in \lam{f
1934 4}, since the argument subexpression has the same meaning (though
1935 the actual expression is different).
1937 \subsection{Completeness}
1938 Proving completeness is probably not hard, but it could be a lot
1939 of work. We have seen above that to prove completeness, we must
1940 show that the normal set of our graph representation is a subset
1941 of the intended normal set.
1943 However, it is hard to systematically generate or reason about the
1944 normal set, since it is defined as any nodes to which no
1945 transformation applies. To determine this set, each transformation
1946 must be considered and when a transformation is added, the entire
1947 set should be re-evaluated. This means it is hard to show that
1948 each node in the normal set is also in the intended normal set.
1949 Reasoning about our intended normal set is easier, since we know
1950 how to generate it from its definition. \refdef{intended normal
1953 Fortunately, we can also prove the complement (which is
1954 equivalent, since $A \subseteq B \Leftrightarrow \overline{B}
1955 \subseteq \overline{A}$): Show that the set of nodes not in
1956 intended normal form is a subset of the set of nodes not in normal
1957 form. In other words, show that for every expression that is not
1958 in intended normal form, that there is at least one transformation
1959 that applies to it (since that means it is not in normal form
1960 either and since $A \subseteq C \Leftrightarrow \forall x (x \in A
1961 \rightarrow x \in C)$).
1963 By systematically reviewing the entire Core language definition
1964 along with the intended normal form definition (both of which have
1965 a similar structure), it should be possible to identify all
1966 possible (sets of) core expressions that are not in intended
1967 normal form and identify a transformation that applies to it.
1969 This approach is especially useful for proving completeness of our
1970 system, since if expressions exist to which none of the
1971 transformations apply (\ie if the system is not yet complete), it
1972 is immediately clear which expressions these are and adding
1973 (or modifying) transformations to fix this should be relatively
1976 As observed above, applying this approach is a lot of work, since
1977 we need to check every (set of) transformation(s) separately.
1979 \todo{Perhaps do a few steps of the proofs as proof-of-concept}
1981 % vim: set sw=2 sts=2 expandtab: