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
28 areas (higher-order expressions, limited polymorphism using type
29 classes, etc.) and because Core can describe expressions that do not
30 have a direct hardware interpretation.
33 The transformations described here have a well-defined goal: to bring the
34 program in a well-defined form that is directly translatable to
35 \VHDL, while fully preserving the semantics of the program. We refer
36 to this form as the \emph{normal form} of the program. The formal
37 definition of this normal form is quite simple:
39 \placedefinition[force]{}{\startboxed A program is in \emph{normal form} if none of the
40 transformations from this chapter apply.\stopboxed}
42 Of course, this is an \quote{easy} definition of the normal form, since our
43 program will end up in normal form automatically. The more interesting part is
44 to see if this normal form actually has the properties we would like it to
47 But, before getting into more definitions and details about this normal
48 form, let us try to get a feeling for it first. The easiest way to do this
49 is by describing the things that are unwanted in the intended normal form.
52 \item Any \emph{polymorphism} must be removed. When laying down hardware, we
53 cannot generate any signals that can have multiple types. All types must be
54 completely known to generate hardware.
56 \item All \emph{higher-order} constructions must be removed. We cannot
57 generate a hardware signal that contains a function, so all values,
58 arguments and return values used must be first order.
60 \item All complex \emph{nested scopes} must be removed. In the \small{VHDL}
61 description, every signal is in a single scope. Also, full expressions are
62 not supported everywhere (in particular port maps can only map signal
63 names and constants, not complete expressions). To make the \small{VHDL}
64 generation easy, a separate binder must be bound to ever application or
69 alu :: Bit -> Word -> Word -> Word
78 \startuseMPgraphic{MulSum}
79 save a, b, c, mul, add, sum;
82 newCircle.a(btex $a$ etex) "framed(false)";
83 newCircle.b(btex $b$ etex) "framed(false)";
84 newCircle.c(btex $c$ etex) "framed(false)";
85 newCircle.sum(btex $sum$ etex) "framed(false)";
88 newCircle.mul(btex * etex);
89 newCircle.add(btex + etex);
91 a.c - b.c = (0cm, 2cm);
92 b.c - c.c = (0cm, 2cm);
93 add.c = c.c + (2cm, 0cm);
94 mul.c = midpoint(a.c, b.c) + (2cm, 0cm);
95 sum.c = add.c + (2cm, 0cm);
98 % Draw objects and lines
99 drawObj(a, b, c, mul, add, sum);
101 ncarc(a)(mul) "arcangle(15)";
102 ncarc(b)(mul) "arcangle(-15)";
108 \placeexample[][ex:MulSum]{Simple architecture consisting of a
109 multiplier and a subtractor.}
110 \startcombination[2*1]
111 {\typebufferlam{MulSum}}{Core description in normal form.}
112 {\boxedgraphic{MulSum}}{The architecture described by the normal form.}
115 \todo{Intermezzo: functions vs plain values}
117 A very simple example of a program in normal form is given in
118 \in{example}[ex:MulSum]. As you can see, all arguments to the function (which
119 will become input ports in the generated \VHDL) are at the outer level.
120 This means that the body of the inner lambda abstraction is never a
121 function, but always a plain value.
123 As the body of the inner lambda abstraction, we see a single (recursive)
124 let expression, that binds two variables (\lam{mul} and \lam{sum}). These
125 variables will be signals in the generated \VHDL, bound to the output port
126 of the \lam{*} and \lam{+} components.
128 The final line (the \quote{return value} of the function) selects the
129 \lam{sum} signal to be the output port of the function. This \quote{return
130 value} can always only be a variable reference, never a more complex
133 \todo{Add generated VHDL}
135 \in{Example}[ex:MulSum] showed a function that just applied two
136 other functions (multiplication and addition), resulting in a simple
137 architecture with two components and some connections. There is of
138 course also some mechanism for choice in the normal form. In a
139 normal Core program, the \emph{case} expression can be used in a few
140 different ways to describe choice. In normal form, this is limited
141 to a very specific form.
143 \in{Example}[ex:AddSubAlu] shows an example describing a
144 simple \small{ALU}, which chooses between two operations based on an opcode
145 bit. The main structure is similar to \in{example}[ex:MulSum], but this
146 time the \lam{res} variable is bound to a case expression. This case
147 expression scrutinizes the variable \lam{opcode} (and scrutinizing more
148 complex expressions is not supported). The case expression can select a
149 different variable based on the constructor of \lam{opcode}.
150 \refdef{case expression}
152 \startbuffer[AddSubAlu]
153 alu :: Bit -> Word -> Word -> Word
165 \startuseMPgraphic{AddSubAlu}
166 save opcode, a, b, add, sub, mux, res;
169 newCircle.opcode(btex $opcode$ etex) "framed(false)";
170 newCircle.a(btex $a$ etex) "framed(false)";
171 newCircle.b(btex $b$ etex) "framed(false)";
172 newCircle.res(btex $res$ etex) "framed(false)";
174 newCircle.add(btex + etex);
175 newCircle.sub(btex - etex);
178 opcode.c - a.c = (0cm, 2cm);
179 add.c - a.c = (4cm, 0cm);
180 sub.c - b.c = (4cm, 0cm);
181 a.c - b.c = (0cm, 3cm);
182 mux.c = midpoint(add.c, sub.c) + (1.5cm, 0cm);
183 res.c - mux.c = (1.5cm, 0cm);
186 % Draw objects and lines
187 drawObj(opcode, a, b, res, add, sub, mux);
189 ncline(a)(add) "posA(e)";
190 ncline(b)(sub) "posA(e)";
191 nccurve(a)(sub) "posA(e)", "angleA(0)";
192 nccurve(b)(add) "posA(e)", "angleA(0)";
193 nccurve(add)(mux) "posB(inpa)", "angleB(0)";
194 nccurve(sub)(mux) "posB(inpb)", "angleB(0)";
195 nccurve(opcode)(mux) "posB(n)", "angleA(0)", "angleB(-90)";
196 ncline(mux)(res) "posA(out)";
199 \placeexample[][ex:AddSubAlu]{Simple \small{ALU} supporting two operations.}
200 \startcombination[2*1]
201 {\typebufferlam{AddSubAlu}}{Core description in normal form.}
202 {\boxedgraphic{AddSubAlu}}{The architecture described by the normal form.}
205 As a more complete example, consider
206 \in{example}[ex:NormalComplete]. This example shows everything that
207 is allowed in normal form, except for built-in higher-order functions
208 (like \lam{map}). The graphical version of the architecture contains
209 a slightly simplified version, since the state tuple packing and
210 unpacking have been left out. Instead, two separate registers are
211 drawn. Most synthesis tools will further optimize this architecture by
212 removing the multiplexers at the register input and instead use the write
213 enable port of the register (when it is available), but we want to show
214 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 initial lambda
228 -- (address, data, packed state)
230 -- There are nested let expressions at top level
232 -- Unpack the state by coercion (\eg, cast from
233 -- State (Word, Word) to (Word, Word))
234 s = sp ▶ (Word, Word)
235 -- Extract both registers from the state
236 r1 = case s of (a, b) -> a
237 r2 = case s of (a, b) -> b
238 -- Calling some other user-defined function.
240 -- Conditional connections
252 -- pack the state by coercion (\eg, cast from
253 -- (Word, Word) to State (Word, Word))
254 sp' = s' ▶ State (Word, Word)
255 -- Pack our return value
262 \startuseMPgraphic{NormalComplete}
263 save a, d, r, foo, muxr, muxout, out;
266 newCircle.a(btex \lam{a} etex) "framed(false)";
267 newCircle.d(btex \lam{d} etex) "framed(false)";
268 newCircle.out(btex \lam{out} etex) "framed(false)";
270 %newCircle.add(btex + etex);
271 newBox.foo(btex \lam{foo} etex);
272 newReg.r1(btex $\lam{r1}$ etex) "dx(4mm)", "dy(6mm)";
273 newReg.r2(btex $\lam{r2}$ etex) "dx(4mm)", "dy(6mm)", "reflect(true)";
275 % Reflect over the vertical axis
276 reflectObj(muxr1)((0,0), (0,1));
279 rotateObj(muxout)(-90);
281 d.c = foo.c + (0cm, 1.5cm);
282 a.c = (xpart r2.c + 2cm, ypart d.c - 0.5cm);
283 foo.c = midpoint(muxr1.c, muxr2.c) + (0cm, 2cm);
284 muxr1.c = r1.c + (0cm, 2cm);
285 muxr2.c = r2.c + (0cm, 2cm);
286 r2.c = r1.c + (4cm, 0cm);
288 muxout.c = midpoint(r1.c, r2.c) - (0cm, 2cm);
289 out.c = muxout.c - (0cm, 1.5cm);
291 % % Draw objects and lines
292 drawObj(a, d, foo, r1, r2, muxr1, muxr2, muxout, out);
295 nccurve(foo)(muxr1) "angleA(-90)", "posB(inpa)", "angleB(180)";
296 nccurve(foo)(muxr2) "angleA(-90)", "posB(inpb)", "angleB(0)";
297 nccurve(muxr1)(r1) "posA(out)", "angleA(180)", "posB(d)", "angleB(0)";
298 nccurve(r1)(muxr1) "posA(out)", "angleA(0)", "posB(inpb)", "angleB(180)";
299 nccurve(muxr2)(r2) "posA(out)", "angleA(0)", "posB(d)", "angleB(180)";
300 nccurve(r2)(muxr2) "posA(out)", "angleA(180)", "posB(inpa)", "angleB(0)";
301 nccurve(r1)(muxout) "posA(out)", "angleA(0)", "posB(inpb)", "angleB(-90)";
302 nccurve(r2)(muxout) "posA(out)", "angleA(180)", "posB(inpa)", "angleB(-90)";
304 nccurve(a)(muxout) "angleA(-90)", "angleB(180)", "posB(sel)";
305 nccurve(a)(muxr1) "angleA(180)", "angleB(-90)", "posB(sel)";
306 nccurve(a)(muxr2) "angleA(180)", "angleB(-90)", "posB(sel)";
307 ncline(muxout)(out) "posA(out)";
310 \todo{Don't split registers in this image?}
311 \placeexample[][ex:NormalComplete]{Simple architecture consisting of an adder and a
313 \startcombination[2*1]
314 {\typebufferlam{NormalComplete}}{Core description in normal form.}
315 {\boxedgraphic{NormalComplete}}{The architecture described by the normal form.}
320 \subsection[sec:normalization:intendednormalform]{Intended normal form definition}
321 Now we have some intuition for the normal form, we can describe how we want
322 the normal form to look like in a slightly more formal manner. The
323 EBNF-like description in \in{definition}[def:IntendedNormal] captures
324 most of the intended structure (and generates a subset of \GHC's Core
327 There are two things missing from this definition: cast expressions are
328 sometimes allowed by the prototype, but not specified here and the below
329 definition allows uses of state that cannot be translated to \VHDL\
330 properly. These two problems are discussed in
331 \in{section}[sec:normalization:castproblems] and
332 \in{section}[sec:normalization:stateproblems] respectively.
334 Some clauses have an expression listed behind them in parentheses.
335 These are conditions that need to apply to the clause. The
336 predicates used there (\lam{lvar()}, \lam{representable()},
337 \lam{gvar()}) will be defined in
338 \in{section}[sec:normalization:predicates].
340 An expression is in normal form if it matches the first
341 definition, \emph{normal}.
343 \todo{Fix indentation}
344 \startbuffer[IntendedNormal]
345 \italic{normal} := \italic{lambda}
346 \italic{lambda} := λvar.\italic{lambda} (representable(var))
348 \italic{toplet} := letrec [\italic{binding}...] in var (representable(var))
349 \italic{binding} := var = \italic{rhs} (representable(rhs))
350 -- State packing and unpacking by coercion
351 | var0 = var1 ▶ State ty (lvar(var1))
352 | var0 = var1 ▶ ty (var1 :: State ty ∧ lvar(var1))
353 \italic{rhs} := \italic{userapp}
354 | \italic{builtinapp}
356 | case var of C a0 ... an -> ai (lvar(var))
358 | case var of (lvar(var))
359 [ DEFAULT -> var ] (lvar(var))
360 C0 w0,0 ... w0,n -> var0
362 Cm wm,0 ... wm,n -> varm (\forall{}i \forall{}j, wi,j \neq vari, lvar(vari))
363 \italic{userapp} := \italic{userfunc}
364 | \italic{userapp} {userarg}
365 \italic{userfunc} := var (gvar(var))
366 \italic{userarg} := var (lvar(var))
367 \italic{builtinapp} := \italic{builtinfunc}
368 | \italic{builtinapp} \italic{builtinarg}
369 \italic{built-infunc} := var (bvar(var))
370 \italic{built-inarg} := var (representable(var) ∧ lvar(var))
371 | \italic{partapp} (partapp :: a -> b)
372 | \italic{coreexpr} (¬representable(coreexpr) ∧ ¬(coreexpr :: a -> b))
373 \italic{partapp} := \italic{userapp}
374 | \italic{builtinapp}
377 \placedefinition[][def:IntendedNormal]{Definition of the intended normal form using an \small{EBNF}-like syntax.}
378 {\defref{intended normal form definition}
379 \typebufferlam{IntendedNormal}}
381 When looking at such a program from a hardware perspective, the top
382 level lambda abstractions (\italic{lambda}) define the input ports.
383 Lambda abstractions cannot appear anywhere else. The variable reference
384 in the body of the recursive let expression (\italic{toplet}) is the
385 output port. Most binders bound by the let expression define a
386 component instantiation (\italic{userapp}), where the input and output
387 ports are mapped to local signals (\italic{userarg}). Some of the others
388 use a built-in construction (\eg\ the \lam{case} expression) or call a
389 built-in function (\italic{builtinapp}) such as \lam{+} or \lam{map}.
390 For these, a hard-coded \small{VHDL} translation is available.
392 \section[sec:normalization:transformation]{Transformation notation}
393 To be able to concisely present transformations, we use a specific format
394 for them. It is a simple format, similar to one used in logic reasoning.
396 Such a transformation description looks like the following.
401 <original expression>
402 -------------------------- <expression conditions>
403 <transformed expression>
408 This format describes a transformation that applies to \lam{<original
409 expression>} and transforms it into \lam{<transformed expression>}, assuming
410 that all conditions are satisfied. In this format, there are a number of placeholders
411 in pointy brackets, most of which should be rather obvious in their meaning.
412 Nevertheless, we will more precisely specify their meaning below:
414 \startdesc{<original expression>} The expression pattern that will be matched
415 against (sub-expressions of) the expression to be transformed. We call this a
416 pattern, because it can contain \emph{placeholders} (variables), which match
417 any expression or binder. Any such placeholder is said to be \emph{bound} to
418 the expression it matches. It is convention to use an uppercase letter (\eg\
419 \lam{M} or \lam{E}) to refer to any expression (including a simple variable
420 reference) and lowercase letters (\eg\ \lam{v} or \lam{b}) to refer to
421 (references to) binders.
423 For example, the pattern \lam{a + B} will match the expression
424 \lam{v + (2 * w)} (binding \lam{a} to \lam{v} and \lam{B} to
425 \lam{(2 * w)}), but not \lam{(2 * w) + v}.
428 \startdesc{<expression conditions>}
429 These are extra conditions on the expression that is matched. These
430 conditions can be used to further limit the cases in which the
431 transformation applies, commonly to prevent a transformation from
432 causing a loop with itself or another transformation.
434 Only if these conditions are \emph{all} satisfied, the transformation
438 \startdesc{<context conditions>}
439 These are a number of extra conditions on the context of the function. In
440 particular, these conditions can require some (other) top level function to be
441 present, whose value matches the pattern given here. The format of each of
442 these conditions is: \lam{binder = <pattern>}.
444 Typically, the binder is some placeholder bound in the \lam{<original
445 expression>}, while the pattern contains some placeholders that are used in
446 the \lam{transformed expression}.
448 Only if a top level binder exists that matches each binder and pattern,
449 the transformation applies.
452 \startdesc{<transformed expression>}
453 This is the expression template that is the result of the transformation. If, looking
454 at the above three items, the transformation applies, the \lam{<original
455 expression>} is completely replaced by the \lam{<transformed expression>}.
456 We call this a template, because it can contain placeholders, referring to
457 any placeholder bound by the \lam{<original expression>} or the
458 \lam{<context conditions>}. The resulting expression will have those
459 placeholders replaced by the values bound to them.
461 Any binder (lowercase) placeholder that has no value bound to it yet will be
462 bound to (and replaced with) a fresh binder.
465 \startdesc{<context additions>}
466 These are templates for new functions to be added to the context.
467 This is a way to let a transformation create new top level
470 Each addition has the form \lam{binder = template}. As above, any
471 placeholder in the addition is replaced with the value bound to it, and any
472 binder placeholder that has no value bound to it yet will be bound to (and
473 replaced with) a fresh binder.
476 To understand this notation better, the step by step application of
477 the η-expansion transformation to a simple \small{ALU} will be
478 shown. Consider η-expansion, which is a common transformation from
479 lambda calculus, described using above notation as follows:
483 -------------- \lam{E} does not occur on a function position in an application
484 λx.E x \lam{E} is not a lambda abstraction.
487 η-expansion is a well known transformation from lambda calculus. What
488 this transformation does, is take any expression that has a function type
489 and turn it into a lambda expression (giving an explicit name to the
490 argument). There are some extra conditions that ensure that this
491 transformation does not apply infinitely (which are not necessarily part
492 of the conventional definition of η-expansion).
494 Consider the following function, in Core notation, which is a fairly obvious way to specify a
495 simple \small{ALU} (Note that it is not yet in normal form, but
496 \in{example}[ex:AddSubAlu] shows the normal form of this function).
497 The parentheses around the \lam{+} and \lam{-} operators are
498 commonly used in Haskell to show that the operators are used as
499 normal functions, instead of \emph{infix} operators (\eg, the
500 operators appear before their arguments, instead of in between).
503 alu :: Bit -> Word -> Word -> Word
504 alu = λopcode. case opcode of
509 There are a few sub-expressions in this function to which we could possibly
510 apply the transformation. Since the pattern of the transformation is only
511 the placeholder \lam{E}, any expression will match that. Whether the
512 transformation applies to an expression is thus solely decided by the
513 conditions to the right of the transformation.
515 We will look at each expression in the function in a top down manner. The
516 first expression is the entire expression the function is bound to.
519 λopcode. case opcode of
524 As said, the expression pattern matches this. The type of this expression is
525 \lam{Bit -> Word -> Word -> Word}, which matches \lam{a -> b} (Note that in
526 this case \lam{a = Bit} and \lam{b = Word -> Word -> Word}).
528 Since this expression is at top level, it does not occur at a function
529 position of an application. However, The expression is a lambda abstraction,
530 so this transformation does not apply.
532 The next expression we could apply this transformation to, is the body of
533 the lambda abstraction:
541 The type of this expression is \lam{Word -> Word -> Word}, which again
542 matches \lam{a -> b}. The expression is the body of a lambda expression, so
543 it does not occur at a function position of an application. Finally, the
544 expression is not a lambda abstraction but a case expression, so all the
545 conditions match. There are no context conditions to match, so the
546 transformation applies.
548 By now, the placeholder \lam{E} is bound to the entire expression. The
549 placeholder \lam{x}, which occurs in the replacement template, is not bound
550 yet, so we need to generate a fresh binder for that. Let us use the binder
551 \lam{a}. This results in the following replacement expression:
559 Continuing with this expression, we see that the transformation does not
560 apply again (it is a lambda expression). Next we look at the body of this
569 Here, the transformation does apply, binding \lam{E} to the entire
570 expression (which has type \lam{Word -> Word}) and binding \lam{x}
571 to the fresh binder \lam{b}, resulting in the replacement:
579 The transformation does not apply to this lambda abstraction, so we
580 look at its body. For brevity, we will put the case expression on one line from
584 (case opcode of Low -> (+); High -> (-)) a b
587 The type of this expression is \lam{Word}, so it does not match \lam{a -> b}
588 and the transformation does not apply. Next, we have two options for the
589 next expression to look at: the function position and argument position of
590 the application. The expression in the argument position is \lam{b}, which
591 has type \lam{Word}, so the transformation does not apply. The expression in
592 the function position is:
595 (case opcode of Low -> (+); High -> (-)) a
598 Obviously, the transformation does not apply here, since it occurs in
599 function position (which makes the second condition false). In the same
600 way the transformation does not apply to both components of this
601 expression (\lam{case opcode of Low -> (+); High -> (-)} and \lam{a}), so
602 we will skip to the components of the case expression: the scrutinee and
603 both alternatives. Since the opcode is not a function, it does not apply
606 The first alternative is \lam{(+)}. This expression has a function type
607 (the operator still needs two arguments). It does not occur in function
608 position of an application and it is not a lambda expression, so the
609 transformation applies.
611 We look at the \lam{<original expression>} pattern, which is \lam{E}.
612 This means we bind \lam{E} to \lam{(+)}. We then replace the expression
613 with the \lam{<transformed expression>}, replacing all occurrences of
614 \lam{E} with \lam{(+)}. In the \lam{<transformed expression>}, the This gives us the replacement expression:
615 \lam{λx.(+) x} (A lambda expression binding \lam{x}, with a body that
616 applies the addition operator to \lam{x}).
618 The complete function then becomes:
620 (case opcode of Low -> λa1.(+) a1; High -> (-)) a
623 Now the transformation no longer applies to the complete first alternative
624 (since it is a lambda expression). It does not apply to the addition
625 operator again, since it is now in function position in an application. It
626 does, however, apply to the application of the addition operator, since
627 that is neither a lambda expression nor does it occur in function
628 position. This means after one more application of the transformation, the
632 (case opcode of Low -> λa1.λb1.(+) a1 b1; High -> (-)) a
635 The other alternative is left as an exercise to the reader. The final
636 function, after applying η-expansion until it does no longer apply is:
639 alu :: Bit -> Word -> Word -> Word
640 alu = λopcode.λa.b. (case opcode of
641 Low -> λa1.λb1 (+) a1 b1
642 High -> λa2.λb2 (-) a2 b2) a b
645 \subsection{Transformation application}
646 In this chapter we define a number of transformations, but how will we apply
647 these? As stated before, our normal form is reached as soon as no
648 transformation applies anymore. This means our application strategy is to
649 simply apply any transformation that applies, and continuing to do that with
650 the result of each transformation.
652 In particular, we define no particular order of transformations. Since
653 transformation order should not influence the resulting normal form,
654 this leaves the implementation free to choose any application order that
655 results in an efficient implementation. Unfortunately this is not
656 entirely true for the current set of transformations. See
657 \in{section}[sec:normalization:non-determinism] for a discussion of this
660 When applying a single transformation, we try to apply it to every (sub)expression
661 in a function, not just the top level function body. This allows us to
662 keep the transformation descriptions concise and powerful.
664 \subsection{Definitions}
665 A \emph{global variable} is any variable (binder) that is bound at the
666 top level of a program, or an external module. A \emph{local variable} is any
667 other variable (\eg, variables local to a function, which can be bound by
668 lambda abstractions, let expressions and pattern matches of case
669 alternatives). This is a slightly different notion of global versus
670 local than what \small{GHC} uses internally, but for our purposes
671 the distinction \GHC\ makes is not useful.
672 \defref{global variable} \defref{local variable}
674 A \emph{hardware representable} (or just \emph{representable}) type or value
675 is (a value of) a type that we can generate a signal for in hardware. For
676 example, a bit, a vector of bits, a 32 bit unsigned word, etc. Values that are
677 not run-time representable notably include (but are not limited to): types,
678 dictionaries, functions.
679 \defref{representable}
681 A \emph{built-in function} is a function supplied by the Cλash
682 framework, whose implementation is not used to generate \VHDL. This is
683 either because it is no valid Cλash (like most list functions that need
684 recursion) or because a Cλash implementation would be unwanted (for the
685 addition operator, for example, we would rather use the \VHDL addition
686 operator to let the synthesis tool decide what kind of adder to use
687 instead of explicitly describing one in Cλash). \defref{built-in
690 These are functions like \lam{map}, \lam{hwor}, \lam{+} and \lam{length}.
692 For these functions, Cλash has a \emph{built-in hardware translation},
693 so calls to these functions can still be translated. Built-in functions
694 must have a valid Haskell implementation, of course, to allow
697 A \emph{user-defined} function is a function for which no built-in
698 translation is available and whose definition will thus need to be
699 translated to Cλash. \defref{user-defined function}
701 \subsubsection[sec:normalization:predicates]{Predicates}
702 Here, we define a number of predicates that can be used below to concisely
705 \emph{gvar(expr)} is true when \emph{expr} is a variable that references a
706 global variable. It is false when it references a local variable.
708 \emph{lvar(expr)} is the complement of \emph{gvar}; it is true when \emph{expr}
709 references a local variable, false when it references a global variable.
711 \emph{representable(expr)} is true when \emph{expr} is \emph{representable}.
713 \subsection[sec:normalization:uniq]{Binder uniqueness}
714 A common problem in transformation systems, is binder uniqueness. When not
715 considering this problem, it is easy to create transformations that mix up
716 bindings and cause name collisions. Take for example, the following Core
720 (λa.λb.λc. a * b * c) x c
723 By applying β-reduction (see \in{section}[sec:normalization:beta]) once,
724 we can simplify this expression to:
730 Now, we have replaced the \lam{a} binder with a reference to the \lam{x}
731 binder. No harm done here. But note that we see multiple occurrences of the
732 \lam{c} binder. The first is a binding occurrence, to which the second refers.
733 The last, however refers to \emph{another} instance of \lam{c}, which is
734 bound somewhere outside of this expression. Now, if we would apply beta
735 reduction without taking heed of binder uniqueness, we would get:
741 This is obviously not what was supposed to happen! The root of this problem is
742 the reuse of binders: identical binders can be bound in different,
743 but overlapping scopes. Any variable reference in those
744 overlapping scopes then refers to the variable bound in the inner
745 (smallest) scope. There is not way to refer to the variable in the
746 outer scope. This effect is usually referred to as
747 \emph{shadowing}: when a binder is bound in a scope where the
748 binder already had a value, the inner binding is said to
749 \emph{shadow} the outer binding. In the example above, the \lam{c}
750 binder was bound outside of the expression and in the inner lambda
751 expression. Inside that lambda expression, only the inner \lam{c}
754 There are a number of ways to solve this. \small{GHC} has isolated this
755 problem to their binder substitution code, which performs \emph{de-shadowing}
756 during its expression traversal. This means that any binding that shadows
757 another binding on a higher level is replaced by a new binder that does not
758 shadow any other binding. This non-shadowing invariant is enough to prevent
759 binder uniqueness problems in \small{GHC}.
761 In our transformation system, maintaining this non-shadowing invariant is
762 a bit harder to do (mostly due to implementation issues, the prototype
763 does not use \small{GHC}'s substitution code). Also, the following points
767 \item De-shadowing does not guarantee overall uniqueness. For example, the
768 following (slightly contrived) expression shows the identifier \lam{x} bound in
769 two separate places (and to different values), even though no shadowing
773 (let x = 1 in x) + (let x = 2 in x)
776 \item In our normal form (and the resulting \small{VHDL}), all binders
777 (signals) within the same function (entity) will end up in the same
778 scope. To allow this, all binders within the same function should be
781 \item When we know that all binders in an expression are unique, moving around
782 or removing a sub-expression will never cause any binder conflicts. If we have
783 some way to generate fresh binders, introducing new sub-expressions will not
784 cause any problems either. The only way to cause conflicts is thus to
785 duplicate an existing sub-expression.
788 Given the above, our prototype maintains a unique binder invariant. This
789 means that in any given moment during normalization, all binders \emph{within
790 a single function} must be unique. To achieve this, we apply the following
793 \todo{Define fresh binders and unique supplies}
796 \item Before starting normalization, all binders in the function are made
797 unique. This is done by generating a fresh binder for every binder used. This
798 also replaces binders that did not cause any conflict, but it does ensure that
799 all binders within the function are generated by the same unique supply.
800 \item Whenever a new binder must be generated, we generate a fresh binder that
801 is guaranteed to be different from \emph{all binders generated so far}. This
802 can thus never introduce duplication and will maintain the invariant.
803 \item Whenever (a part of) an expression is duplicated (for example when
804 inlining), all binders in the expression are replaced with fresh binders
805 (using the same method as at the start of normalization). These fresh binders
806 can never introduce duplication, so this will maintain the invariant.
807 \item Whenever we move part of an expression around within the function, there
808 is no need to do anything special. There is obviously no way to introduce
809 duplication by moving expressions around. Since we know that each of the
810 binders is already unique, there is no way to introduce (incorrect) shadowing
814 \section{Transform passes}
815 In this section we describe the actual transforms.
817 Each transformation will be described informally first, explaining
818 the need for and goal of the transformation. Then, we will formally define
819 the transformation using the syntax introduced in
820 \in{section}[sec:normalization:transformation].
822 \subsection{General cleanup}
824 \defref{substitution notation}
825 \startframedtext[width=8cm,background=box,frame=no]
826 \startalignment[center]
827 {\tfa Substitution notation}
831 In some of the transformations in this chapter, we need to perform
832 substitution on an expression. Substitution means replacing every
833 occurrence of some expression (usually a variable reference) with
836 There have been a lot of different notations used in literature for
837 specifying substitution. The notation that will be used in this report
844 This means expression \lam{E} with all occurrences of \lam{A} replaced
849 These transformations are general cleanup transformations, that aim to
850 make expressions simpler. These transformations usually clean up the
851 mess left behind by other transformations or clean up expressions to
852 expose new transformation opportunities for other transformations.
854 Most of these transformations are standard optimizations in other
855 compilers as well. However, in our compiler, most of these are not just
856 optimizations, but they are required to get our program into intended
859 \subsubsection[sec:normalization:beta]{β-reduction}
860 β-reduction is a well known transformation from lambda calculus, where it is
861 the main reduction step. It reduces applications of lambda abstractions,
862 removing both the lambda abstraction and the application.
864 In our transformation system, this step helps to remove unwanted lambda
865 abstractions (basically all but the ones at the top level). Other
866 transformations (application propagation, non-representable inlining) make
867 sure that most lambda abstractions will eventually be reducible by
870 Note that β-reduction also works on type lambda abstractions and type
871 applications as well. This means the substitution below also works on
872 type variables, in the case that the binder is a type variable and the
873 expression applied to is a type.
890 \transexample{beta}{β-reduction}{from}{to}
900 \transexample{beta-type}{β-reduction for type abstractions}{from}{to}
902 \subsubsection{Unused let binding removal}
903 This transformation removes let bindings that are never used.
904 Occasionally, \GHC's desugarer introduces some unused let bindings.
906 This normalization pass should really be not be necessary to get
907 into intended normal form (since the intended normal form
908 definition \refdef{intended normal form definition} does not
909 require that every binding is used), but in practice the
910 desugarer or simplifier emits some bindings that cannot be
911 normalized (e.g., calls to a
912 \hs{Control.Exception.Base.patError}) but are not used anywhere
913 either. To prevent the \VHDL\ generation from breaking on these
914 artifacts, this transformation removes them.
924 M \lam{ai} does not occur free in \lam{M}
925 ---------------------------- \lam{\forall j, 0 ≤ j ≤ n, j ≠ i} (\lam{ai} does not occur free in \lam{Ej})
951 \transexample{unusedlet}{Unused let binding removal}{from}{to}
953 \subsubsection{Empty let removal}
954 This transformation is simple: it removes recursive lets that have no bindings
955 (which usually occurs when unused let binding removal removes the last
958 Note that there is no need to define this transformation for
959 non-recursive lets, since they always contain exactly one binding.
978 \transexample{emptylet}{Empty let removal}{from}{to}
980 \subsubsection[sec:normalization:simplelet]{Simple let binding removal}
981 This transformation inlines simple let bindings, that bind some
982 binder to some other binder instead of a more complex expression (\ie\
985 This transformation is not needed to get an expression into intended
986 normal form (since these bindings are part of the intended normal
987 form), but makes the resulting \small{VHDL} a lot shorter.
989 \refdef{substitution notation}
999 ----------------------------- \lam{b} is a variable reference
1000 letrec \lam{ai} ≠ \lam{b}
1013 \subsubsection{Cast propagation / simplification}
1014 This transform pushes casts down into the expression as far as
1015 possible. This transformation has been added to make a few
1016 specific corner cases work, but it is not clear yet if this
1017 transformation handles cast expressions completely or in the
1018 right way. See \in{section}[sec:normalization:castproblems].
1021 (let binds in E) ▶ T
1022 -------------------------
1023 let binds in (E ▶ T)
1032 -------------------------
1039 \subsubsection{Top level binding inlining}
1040 \refdef{top level binding}
1041 This transform takes simple top level bindings generated by the
1042 \small{GHC} compiler. \small{GHC} sometimes generates very simple
1043 \quote{wrapper} bindings, which are bound to just a variable
1044 reference, or contain just a (partial) function application with
1045 the type and dictionary arguments filled in (such as the
1046 \lam{(+)} in the example below).
1048 Note that this transformation is completely optional. It is not
1049 required to get any function into intended normal form, but it does help making
1050 the resulting VHDL output easier to read (since it removes components
1051 that do not add any real structure, but do hide away operations and
1052 cause extra clutter).
1054 This transform takes any top level binding generated by \GHC,
1055 whose normalized form contains only a single let binding.
1058 x = λa0 ... λan.let y = E in y
1061 -------------------------------------- \lam{x} is generated by the compiler
1062 λa0 ... λan.let y = E in y
1066 (+) :: Word -> Word -> Word
1067 (+) = GHC.Num.(+) @Word \$dNum
1072 GHC.Num.(+) @ Alu.Word \$dNum a b
1075 \transexample{toplevelinline}{Top level binding inlining}{from}{to}
1077 \in{Example}[ex:trans:toplevelinline] shows a typical application of
1078 the addition operator generated by \GHC. The type and dictionary
1079 arguments used here are described in
1080 \in{Section}[sec:prototype:coretypes].
1082 Without this transformation, there would be a \lam{(+)} entity
1083 in the \VHDL\ which would just add its inputs. This generates a
1084 lot of overhead in the \VHDL, which is particularly annoying
1085 when browsing the generated RTL schematic (especially since most
1086 non-alphanumerics, like all characters in \lam{(+)}, are not
1087 allowed in \VHDL\ architecture names\footnote{Technically, it is
1088 allowed to use non-alphanumerics when using extended
1089 identifiers, but it seems that none of the tooling likes
1090 extended identifiers in file names, so it effectively does not
1091 work.}, so the entity would be called \quote{w7aA7f} or
1092 something similarly meaningless and auto-generated).
1094 \subsection{Program structure}
1095 These transformations are aimed at normalizing the overall structure
1096 into the intended form. This means ensuring there is a lambda abstraction
1097 at the top for every argument (input port or current state), putting all
1098 of the other value definitions in let bindings and making the final
1099 return value a simple variable reference.
1101 \subsubsection[sec:normalization:eta]{η-expansion}
1102 This transformation makes sure that all arguments of a function-typed
1103 expression are named, by introducing lambda expressions. When combined with
1104 β-reduction and non-representable binding inlining, all function-typed
1105 expressions should be lambda abstractions or global identifiers.
1109 -------------- \lam{E} does not occur on a function position in an application
1110 λx.E x \lam{E} is not a lambda abstraction.
1120 foo = λa.λx.(case a of
1125 \transexample{eta}{η-expansion}{from}{to}
1127 \subsubsection[sec:normalization:appprop]{Application propagation}
1128 This transformation is meant to propagate application expressions downwards
1129 into expressions as far as possible. This allows partial applications inside
1130 expressions to become fully applied and exposes new transformation
1131 opportunities for other transformations (like β-reduction and
1134 Since all binders in our expression are unique (see
1135 \in{section}[sec:normalization:uniq]), there is no risk that we will
1136 introduce unintended shadowing by moving an expression into a lower
1137 scope. Also, since only move expression into smaller scopes (down into
1138 our expression), there is no risk of moving a variable reference out
1139 of the scope in which it is defined.
1142 (letrec binds in E) M
1143 ------------------------
1163 \transexample{appproplet}{Application propagation for a let expression}{from}{to}
1191 \transexample{apppropcase}{Application propagation for a case expression}{from}{to}
1193 \subsubsection[sec:normalization:letrecurse]{Let recursification}
1194 This transformation makes all non-recursive lets recursive. In the
1195 end, we want a single recursive let in our normalized program, so all
1196 non-recursive lets can be converted. This also makes other
1197 transformations simpler: they only need to be specified for recursive
1198 let expressions (and simply will not apply to non-recursive let
1199 expressions until this transformation has been applied).
1206 ------------------------------------------
1213 \subsubsection{Let flattening}
1214 This transformation puts nested lets in the same scope, by lifting the
1215 binding(s) of the inner let into the outer let. Eventually, this will
1216 cause all let bindings to appear in the same scope.
1218 This transformation only applies to recursive lets, since all
1219 non-recursive lets will be made recursive (see
1220 \in{section}[sec:normalization:letrecurse]).
1222 Since we are joining two scopes together, there is no risk of moving a
1223 variable reference out of the scope where it is defined.
1229 ai = (letrec bindings in M)
1234 ------------------------------------------
1269 \transexample{letflat}{Let flattening}{from}{to}
1271 \subsubsection{Return value simplification}
1272 This transformation ensures that the return value of a function is always a
1273 simple local variable reference.
1275 The basic idea of this transformation is to take the body of a
1276 function and bind it with a let expression (so the body of that let
1277 expression becomes a variable reference that can be used as the output
1278 port). If the body of the function happens to have lambda abstractions
1279 at the top level (which is allowed by the intended normal
1280 form\refdef{intended normal form definition}), we take the body of the
1281 inner lambda instead. If that happens to be a let expression already
1282 (which is allowed by the intended normal form), we take the body of
1283 that let (which is not allowed to be anything but a variable reference
1284 according the the intended normal form).
1286 This transformation uses the context conditions in a special way.
1287 These contexts, like \lam{x = λv1 ... λvn.E}, are above the dotted
1288 line and provide a condition on the environment (\ie\ they require a
1289 certain top level binding to be present). These ensure that
1290 expressions are only transformed when they are in the functions
1291 \quote{return value} directly. This means the context conditions have
1292 to interpreted in the right way: not \quote{if there is any function
1293 \lam{x} that binds \lam{E}, any \lam{E} can be transformed}, but we
1294 mean only the \lam{E} that is bound by \lam{x}).
1296 Be careful when reading the transformations: Not the entire function
1297 from the context is transformed, just a part of it.
1299 Note that the return value is not simplified if it is not representable.
1300 Otherwise, this would cause a loop with the inlining of
1301 unrepresentable bindings in
1302 \in{section}[sec:normalization:nonrepinline]. If the return value is
1303 not representable because it has a function type, η-expansion should
1304 make sure that this transformation will eventually apply. If the
1305 value is not representable for other reasons, the function result
1306 itself is not representable, meaning this function is not translatable
1310 x = λv1 ... λvn.E \lam{n} can be zero
1311 ~ \lam{E} is representable
1312 E \lam{E} is not a lambda abstraction
1313 --------------------------- \lam{E} is not a let expression
1314 letrec y = E in y \lam{E} is not a local variable reference
1318 x = λv1 ... λvn.letrec binds in E \lam{n} can be zero
1319 ~ \lam{E} is representable
1320 letrec binds in E \lam{E} is not a local variable reference
1321 ------------------------------------
1322 letrec binds; y = E in y
1330 x = letrec y = add 1 2 in y
1333 \transexample{retvalsimpl}{Return value simplification}{from}{to}
1346 \transexample{retvalsimpllam}{Return value simplification with a lambda abstraction}{from}{to}
1363 \transexample{retvalsimpllet}{Return value simplification with a let expression}{from}{to}
1365 \subsection[sec:normalization:argsimpl]{Representable arguments simplification}
1366 This section contains just a single transformation that deals with
1367 representable arguments in applications. Non-representable arguments are
1368 handled by the transformations in
1369 \in{section}[sec:normalization:nonrep].
1371 This transformation ensures that all representable arguments will become
1372 references to local variables. This ensures they will become references
1373 to local signals in the resulting \small{VHDL}, which is required due to
1374 limitations in the component instantiation code in \VHDL\ (one can only
1375 assign a signal or constant to an input port). By ensuring that all
1376 arguments are always simple variable references, we always have a signal
1377 available to map to the input ports.
1379 To reduce a complex expression to a simple variable reference, we create
1380 a new let expression around the application, which binds the complex
1381 expression to a new variable. The original function is then applied to
1384 \refdef{global variable}
1385 Note that references to \emph{global variables} (like a top level
1386 function without arguments, but also an argumentless data-constructors
1387 like \lam{True}) are also simplified. Only local variables generate
1388 signals in the resulting architecture. Even though argumentless
1389 data-constructors generate constants in generated \VHDL\ code and could be
1390 mapped to an input port directly, they are still simplified to make the
1391 normal form more regular.
1393 \refdef{representable}
1396 -------------------- \lam{N} is representable
1397 letrec x = N in M x \lam{N} is not a local variable reference
1399 \refdef{local variable}
1406 letrec x = add a 1 in add x 1
1409 \transexample{argsimpl}{Argument simplification}{from}{to}
1411 \subsection[sec:normalization:built-ins]{Built-in functions}
1412 This section deals with (arguments to) built-in functions. In the
1413 intended normal form definition\refdef{intended normal form definition}
1414 we can see that there are three sorts of arguments a built-in function
1418 \item A representable local variable reference. This is the most
1419 common argument to any function. The argument simplification
1420 transformation described in \in{section}[sec:normalization:argsimpl]
1421 makes sure that \emph{any} representable argument to \emph{any}
1422 function (including built-in functions) is turned into a local variable
1424 \item (A partial application of) a top level function (either built-in on
1425 user-defined). The function extraction transformation described in
1426 this section takes care of turning every function-typed argument into
1427 (a partial application of) a top level function.
1428 \item Any expression that is not representable and does not have a
1429 function type. Since these can be any expression, there is no
1430 transformation needed. Note that this category is exactly all
1431 expressions that are not transformed by the transformations for the
1432 previous two categories. This means that \emph{any} Core expression
1433 that is used as an argument to a built-in function will be either
1434 transformed into one of the above categories, or end up in this
1435 category. In any case, the result is in normal form.
1438 As noted, the argument simplification will handle any representable
1439 arguments to a built-in function. The following transformation is needed
1440 to handle non-representable arguments with a function type, all other
1441 non-representable arguments do not need any special handling.
1443 \subsubsection[sec:normalization:funextract]{Function extraction}
1444 This transform deals with function-typed arguments to built-in
1446 Since built-in functions cannot be specialized (see
1447 \in{section}[sec:normalization:specialize]) to remove the arguments,
1448 these arguments are extracted into a new global function instead. In
1449 other words, we create a new top level function that has exactly the
1450 extracted argument as its body. This greatly simplifies the
1451 translation rules needed for built-in functions, since they only need
1452 to handle (partial applications of) top level functions.
1454 Any free variables occurring in the extracted arguments will become
1455 parameters to the new global function. The original argument is replaced
1456 with a reference to the new function, applied to any free variables from
1457 the original argument.
1459 This transformation is useful when applying higher-order built-in functions
1460 like \hs{map} to a lambda abstraction, for example. In this case, the code
1461 that generates \small{VHDL} for \hs{map} only needs to handle top level functions and
1462 partial applications, not any other expression (such as lambda abstractions or
1463 even more complicated expressions).
1466 M N \lam{M} is (a partial application of) a built-in function.
1467 --------------------- \lam{f0 ... fn} are all free local variables of \lam{N}
1468 M (x f0 ... fn) \lam{N :: a -> b}
1469 ~ \lam{N} is not a (partial application of) a top level function
1474 addList = λb.λxs.map (λa . add a b) xs
1478 addList = λb.λxs.map (f b) xs
1483 \transexample{funextract}{Function extraction}{from}{to}
1485 Note that the function \lam{f} will still need normalization after
1488 \subsection{Case normalization}
1489 The transformations in this section ensure that case statements end up
1492 \subsubsection{Scrutinee simplification}
1493 This transform ensures that the scrutinee of a case expression is always
1494 a simple variable reference.
1499 ----------------- \lam{E} is not a local variable reference
1518 \transexample{letflat}{Case normalization}{from}{to}
1522 \defref{wild binders}
1523 \startframedtext[width=7cm,background=box,frame=no]
1524 \startalignment[center]
1528 In a functional expression, a \emph{wild binder} refers to any
1529 binder that is never referenced. This means that even though it
1530 will be bound to a particular value, that value is never used.
1532 The Haskell syntax offers the underscore as a wild binder that
1533 cannot even be referenced (It can be seen as introducing a new,
1534 anonymous, binder every time it is used).
1536 In these transformations, the term wild binder will sometimes be
1537 used to indicate that a binder must not be referenced.
1541 \subsubsection{Scrutinee binder removal}
1542 This transformation removes (or rather, makes wild) the binder to
1543 which the scrutinee is bound after evaluation. This is done by
1544 replacing the bndr with the scrutinee in all alternatives. To prevent
1545 duplication of work, this transformation is only applied when the
1546 scrutinee is already a simple variable reference (but the previous
1547 transformation ensures this will eventually be the case). The
1548 scrutinee binder itself is replaced by a wild binder (which is no
1551 Note that one could argue that this transformation can change the
1552 meaning of the Core expression. In the regular Core semantics, a case
1553 expression forces the evaluation of its scrutinee and can be used to
1554 implement strict evaluation. However, in the generated \VHDL,
1555 evaluation is always strict. So the semantics we assign to the Core
1556 expression (which differ only at this particular point), this
1557 transformation is completely valid.
1562 ----------------- \lam{x} is a local variable reference
1579 \transexample{scrutbndrremove}{Scrutinee binder removal}{from}{to}
1581 \subsubsection{Case normalization}
1582 This transformation ensures that all case expressions get a form
1583 that is allowed by the intended normal form. This means they
1587 \item An extractor case with a single alternative that picks a field
1588 from a datatype, \eg\ \lam{case x of (a, b) ->
1589 a}.\defref{extractor case}
1590 \item A selector case with multiple alternatives and only wild binders, that
1591 makes a choice between expressions based on the constructor of another
1592 expression, \eg\ \lam{case x of Low -> a; High ->
1593 b}.\defref{selector case}
1596 For an arbitrary case, that has \lam{n} alternatives, with
1597 \lam{m} binders in each alternatives, this will result in \lam{m
1598 * n} extractor case expression to get at each variable, \lam{n}
1599 let bindings for each of the alternatives' value and a single
1600 selector case to select the right value out of these.
1602 Technically, the definition of this transformation would require
1603 that the constructor for every alternative has exactly the same
1604 amount (\lam{m}) of arguments, but of course this transformation
1605 also applies when this is not the case.
1609 C0 v0,0 ... v0,m -> E0
1611 Cn vn,0 ... vn,m -> En
1612 --------------------------------------------------- \lam{\forall i \forall j, 0 ≤ i ≤ n, 0 ≤ i < m} (\lam{wi,j} is a wild (unused) binder)
1613 letrec The case expression is not an extractor case
1614 v0,0 = case E of C0 x0,0 .. x0,m -> x0,0 The case expression is not a selector case
1616 v0,m = case E of C0 x0,0 .. x0,m -> x0,m
1618 vn,m = case E of Cn xn,0 .. xn,m -> xn,m
1624 C0 w0,0 ... w0,m -> y0
1626 Cn wn,0 ... wn,m -> yn
1629 Note that this transformation applies to case expressions with any
1630 scrutinee. If the scrutinee is a complex expression, this might
1631 result in duplication of work (hardware). An extra condition to
1632 only apply this transformation when the scrutinee is already
1633 simple (effectively causing this transformation to be only
1634 applied after the scrutinee simplification transformation) might
1653 \transexample{selcasesimpl}{Selector case simplification}{from}{to}
1661 b = case a of (,) b c -> b
1662 c = case a of (,) b c -> c
1669 \transexample{excasesimpl}{Extractor case simplification}{from}{to}
1671 \refdef{selector case}
1672 In \in{example}[ex:trans:excasesimpl] the case expression is expanded
1673 into multiple case expressions, including a pretty useless expression
1674 (that is neither a selector or extractor case). This case can be
1675 removed by the Case removal transformation in
1676 \in{section}[sec:transformation:caseremoval].
1678 \subsubsection[sec:transformation:caseremoval]{Case removal}
1679 This transform removes any case expression with a single alternative and
1680 only wild binders.\refdef{wild binders}
1682 These "useless" case expressions are usually leftovers from case simplification
1683 on extractor case (see the previous example).
1688 ---------------------- \lam{\forall i, 0 ≤ i ≤ m} (\lam{vi} does not occur free in E)
1701 \transexample{caserem}{Case removal}{from}{to}
1703 \subsection[sec:normalization:nonrep]{Removing unrepresentable values}
1704 The transformations in this section are aimed at making all the
1705 values used in our expression representable. There are two main
1706 transformations that are applied to \emph{all} unrepresentable let
1707 bindings and function arguments. These are meant to address three
1708 different kinds of unrepresentable values: polymorphic values,
1709 higher-order values and literals. The transformation are described
1710 generically: they apply to all non-representable values. However,
1711 non-representable values that do not fall into one of these three
1712 categories will be moved around by these transformations but are
1713 unlikely to completely disappear. They usually mean the program was not
1714 valid in the first place, because unsupported types were used (for
1715 example, a program using strings).
1717 Each of these three categories will be detailed below, followed by the
1718 actual transformations.
1720 \subsubsection{Removing Polymorphism}
1721 As noted in \in{section}[sec:prototype:coretypes],
1722 polymorphism is made explicit in Core through type and
1723 dictionary arguments. To remove the polymorphism from a
1724 function, we can simply specialize the polymorphic function for
1725 the particular type applied to it. The same goes for dictionary
1726 arguments. To remove polymorphism from let bound values, we
1727 simply inline the let bindings that have a polymorphic type,
1728 which should (eventually) make sure that the polymorphic
1729 expression is applied to a type and/or dictionary, which can
1730 then be removed by β-reduction (\in{section}[sec:normalization:beta]).
1732 Since both type and dictionary arguments are not representable,
1733 \refdef{representable}
1734 the non-representable argument specialization and
1735 non-representable let binding inlining transformations below
1736 take care of exactly this.
1738 There is one case where polymorphism cannot be completely
1739 removed: built-in functions are still allowed to be polymorphic
1740 (Since we have no function body that we could properly
1741 specialize). However, the code that generates \VHDL\ for built-in
1742 functions knows how to handle this, so this is not a problem.
1744 \subsubsection[sec:normalization:defunctionalization]{Defunctionalization}
1745 These transformations remove higher-order expressions from our
1746 program, making all values first-order. The approach used for
1747 defunctionalization uses a combination of specialization, inlining and
1748 some cleanup transformations, was also proposed in parallel research
1749 by Neil Mitchell \cite[mitchell09].
1751 Higher order values are always introduced by lambda abstractions, none
1752 of the other Core expression elements can introduce a function type.
1753 However, other expressions can \emph{have} a function type, when they
1754 have a lambda expression in their body.
1756 For example, the following expression is a higher-order expression
1757 that is not a lambda expression itself:
1759 \refdef{id function}
1766 The reference to the \lam{id} function shows that we can introduce a
1767 higher-order expression in our program without using a lambda
1768 expression directly. However, inside the definition of the \lam{id}
1769 function, we can be sure that a lambda expression is present.
1771 Looking closely at the definition of our normal form in
1772 \in{section}[sec:normalization:intendednormalform], we can see that
1773 there are three possibilities for higher-order values to appear in our
1774 intended normal form:
1777 \item[item:toplambda] Lambda abstractions can appear at the highest level of a
1778 top level function. These lambda abstractions introduce the
1779 arguments (input ports / current state) of the function.
1780 \item[item:built-inarg] (Partial applications of) top level functions can appear as an
1781 argument to a built-in function.
1782 \item[item:completeapp] (Partial applications of) top level functions can appear in
1783 function position of an application. Since a partial application
1784 cannot appear anywhere else (except as built-in function arguments),
1785 all partial applications are applied, meaning that all applications
1786 will become complete applications. However, since application of
1787 arguments happens one by one, in the expression:
1791 the sub-expression \lam{f 1} has a function type. But this is
1792 allowed, since it is inside a complete application.
1795 We will take a typical function with some higher-order values as an
1796 example. The following function takes two arguments: a \lam{Bit} and a
1797 list of numbers. Depending on the first argument, each number in the
1798 list is doubled, or the list is returned unmodified. For the sake of
1799 the example, no polymorphism is shown. In reality, at least map would
1803 λy.let double = λx. x + x in
1809 This example shows a number of higher-order values that we cannot
1810 translate to \VHDL\ directly. The \lam{double} binder bound in the let
1811 expression has a function type, as well as both of the alternatives of
1812 the case expression. The first alternative is a partial application of
1813 the \lam{map} built-in function, whereas the second alternative is a
1816 To reduce all higher-order values to one of the above items, a number
1817 of transformations we have already seen are used. The η-expansion
1818 transformation from \in{section}[sec:normalization:eta] ensures all
1819 function arguments are introduced by lambda abstraction on the highest
1820 level of a function. These lambda arguments are allowed because of
1821 \in{item}[item:toplambda] above. After η-expansion, our example
1822 becomes a bit bigger:
1825 λy.λq.(let double = λx. x + x in
1832 η-expansion also introduces extra applications (the application of
1833 the let expression to \lam{q} in the above example). These
1834 applications can then propagated down by the application propagation
1835 transformation (\in{section}[sec:normalization:appprop]). In our
1836 example, the \lam{q} and \lam{r} variable will be propagated into the
1837 let expression and then into the case expression:
1840 λy.λq.let double = λx. x + x in
1846 This propagation makes higher-order values become applied (in
1847 particular both of the alternatives of the case now have a
1848 representable type). Completely applied top level functions (like the
1849 first alternative) are now no longer invalid (they fall under
1850 \in{item}[item:completeapp] above). (Completely) applied lambda
1851 abstractions can be removed by β-expansion. For our example,
1852 applying β-expansion results in the following:
1855 λy.λq.let double = λx. x + x in
1861 As you can see in our example, all of this moves applications towards
1862 the higher-order values, but misses higher-order functions bound by
1863 let expressions. The applications cannot be moved towards these values
1864 (since they can be used in multiple places), so the values will have
1865 to be moved towards the applications. This is achieved by inlining all
1866 higher-order values bound by let applications, by the
1867 non-representable binding inlining transformation below. When applying
1868 it to our example, we get the following:
1872 Low -> map (λx. x + x) q
1876 We have nearly eliminated all unsupported higher-order values from this
1877 expressions. The one that is remaining is the first argument to the
1878 \lam{map} function. Having higher-order arguments to a built-in
1879 function like \lam{map} is allowed in the intended normal form, but
1880 only if the argument is a (partial application) of a top level
1881 function. This is easily done by introducing a new top level function
1882 and put the lambda abstraction inside. This is done by the function
1883 extraction transformation from
1884 \in{section}[sec:normalization:funextract].
1892 This also introduces a new function, that we have called \lam{func}:
1898 Note that this does not actually remove the lambda, but now it is a
1899 lambda at the highest level of a function, which is allowed in the
1900 intended normal form.
1902 There is one case that has not been discussed yet. What if the
1903 \lam{map} function in the example above was not a built-in function
1904 but a user-defined function? Then extracting the lambda expression
1905 into a new function would not be enough, since user-defined functions
1906 can never have higher-order arguments. For example, the following
1907 expression shows an example:
1910 twice :: (Word -> Word) -> Word -> Word
1911 twice = λf.λa.f (f a)
1913 main = λa.app (λx. x + x) a
1916 This example shows a function \lam{twice} that takes a function as a
1917 first argument and applies that function twice to the second argument.
1918 Again, we have made the function monomorphic for clarity, even though
1919 this function would be a lot more useful if it was polymorphic. The
1920 function \lam{main} uses \lam{twice} to apply a lambda expression twice.
1922 When faced with a user defined function, a body is available for that
1923 function. This means we could create a specialized version of the
1924 function that only works for this particular higher-order argument
1925 (\ie, we can just remove the argument and call the specialized
1926 function without the argument). This transformation is detailed below.
1927 Applying this transformation to the example gives:
1930 twice' :: Word -> Word
1931 twice' = λb.(λf.λa.f (f a)) (λx. x + x) b
1936 The \lam{main} function is now in normal form, since the only
1937 higher-order value there is the top level lambda expression. The new
1938 \lam{twice'} function is a bit complex, but the entire original body
1939 of the original \lam{twice} function is wrapped in a lambda
1940 abstraction and applied to the argument we have specialized for
1941 (\lam{λx. x + x}) and the other arguments. This complex expression can
1942 fortunately be effectively reduced by repeatedly applying β-reduction:
1945 twice' :: Word -> Word
1946 twice' = λb.(b + b) + (b + b)
1949 This example also shows that the resulting normal form might not be as
1950 efficient as we might hope it to be (it is calculating \lam{(b + b)}
1951 twice). This is discussed in more detail in
1952 \in{section}[sec:normalization:duplicatework].
1954 \subsubsection[sec:normalization:literals]{Literals}
1955 There are a limited number of literals available in Haskell and Core.
1956 \refdef{enumerated types} When using (enumerating) algebraic
1957 data-types, a literal is just a reference to the corresponding data
1958 constructor, which has a representable type (the algebraic datatype)
1959 and can be translated directly. This also holds for literals of the
1960 \hs{Bool} Haskell type, which is just an enumerated type.
1962 There is, however, a second type of literal that does not have a
1963 representable type: integer literals. Cλash supports using integer
1964 literals for all three integer types supported (\hs{SizedWord},
1965 \hs{SizedInt} and \hs{RangedWord}). This is implemented using
1966 Haskell's \hs{Num} type class, which offers a \hs{fromInteger} method
1967 that converts any \hs{Integer} to the Cλash data-types.
1969 When \GHC\ sees integer literals, it will automatically insert calls to
1970 the \hs{fromInteger} method in the resulting Core expression. For
1971 example, the following expression in Haskell creates a 32 bit unsigned
1972 word with the value 1. The explicit type signature is needed, since
1973 there is no context for \GHC\ to determine the type from otherwise.
1979 This Haskell code results in the following Core expression:
1982 fromInteger @(SizedWord D32) \$dNum (smallInteger 10)
1985 The literal 10 will have the type \lam{GHC.Prim.Int\#}, which is
1986 converted into an \lam{Integer} by \lam{smallInteger}. Finally, the
1987 \lam{fromInteger} function will finally convert this into a
1988 \lam{SizedWord D32}.
1990 Both the \lam{GHC.Prim.Int\#} and \lam{Integer} types are not
1991 representable, and cannot be translated directly. Fortunately, there
1992 is no need to translate them, since \lam{fromInteger} is a built-in
1993 function that knows how to handle these values. However, this does
1994 require that the \lam{fromInteger} function is directly applied to
1995 these non-representable literal values, otherwise errors will occur.
1996 For example, the following expression is not in the intended normal
1997 form, since one of the let bindings has an unrepresentable type
2001 let l = smallInteger 10 in fromInteger @(SizedWord D32) \$dNum l
2004 By inlining these let-bindings, we can ensure that unrepresentable
2005 literals bound by a let binding end up in an application of the
2006 appropriate built-in function, where they are allowed. Since it is
2007 possible that the application of that function is in a different
2008 function than the definition of the literal value, we will always need
2009 to specialize away any unrepresentable literals that are used as
2010 function arguments. The following two transformations do exactly this.
2012 \subsubsection[sec:normalization:nonrepinline]{Non-representable binding inlining}
2013 This transform inlines let bindings that are bound to a
2014 non-representable value. Since we can never generate a signal
2015 assignment for these bindings (we cannot declare a signal assignment
2016 with a non-representable type, for obvious reasons), we have no choice
2017 but to inline the binding to remove it.
2019 As we have seen in the previous sections, inlining these bindings
2020 solves (part of) the polymorphism, higher-order values and
2021 unrepresentable literals in an expression.
2023 \refdef{substitution notation}
2033 -------------------------- \lam{Ei} has a non-representable type.
2035 a0 = E0 [ai=>Ei] \vdots
2036 ai-1 = Ei-1 [ai=>Ei]
2037 ai+1 = Ei+1 [ai=>Ei]
2056 x = fromInteger (smallInteger 10)
2058 (λb -> add b 1) (add 1 x)
2061 \transexample{nonrepinline}{Non-representable binding inlining}{from}{to}
2063 \subsubsection[sec:normalization:specialize]{Function specialization}
2064 This transform removes arguments to user-defined functions that are
2065 not representable at run-time. This is done by creating a
2066 \emph{specialized} version of the function that only works for one
2067 particular value of that argument (in other words, the argument can be
2070 Specialization means to create a specialized version of the called
2071 function, with one argument already filled in. As a simple example, in
2072 the following program (this is not actual Core, since it directly uses
2073 a literal with the unrepresentable type \lam{GHC.Prim.Int\#}).
2080 We could specialize the function \lam{f} against the literal argument
2081 1, with the following result:
2088 In some way, this transformation is similar to β-reduction, but it
2089 operates across function boundaries. It is also similar to
2090 non-representable let binding inlining above, since it sort of
2091 \quote{inlines} an expression into a called function.
2093 Special care must be taken when the argument has any free variables.
2094 If this is the case, the original argument should not be removed
2095 completely, but replaced by all the free variables of the expression.
2096 In this way, the original expression can still be evaluated inside the
2099 To prevent us from propagating the same argument over and over, a
2100 simple local variable reference is not propagated (since is has
2101 exactly one free variable, itself, we would only replace that argument
2104 This shows that any free local variables that are not run-time
2105 representable cannot be brought into normal form by this transform. We
2106 rely on an inlining or β-reduction transformation to replace such a
2107 variable with an expression we can propagate again.
2112 x Y0 ... Yi ... Yn \lam{Yi} is not representable
2113 --------------------------------------------- \lam{Yi} is not a local variable reference
2114 x' Y0 ... Yi-1 f0 ... fm Yi+1 ... Yn \lam{f0 ... fm} are all free local vars of \lam{Yi}
2115 ~ \lam{T0 ... Tn} are the types of \lam{Y0 ... Yn}
2116 x' = λ(y0 :: T0) ... λ(yi-1 :: Ty-1).
2118 λ(yi+1 :: Ty+1) ... λ(yn :: Tn).
2119 E y0 ... yi-1 Yi yi+1 ... yn
2122 This is a bit of a complex transformation. It transforms an
2123 application of the function \lam{x}, where one of the arguments
2124 (\lam{Y_i}) is not representable. A new
2125 function \lam{x'} is created that wraps the body of the old function.
2126 The body of the new function becomes a number of nested lambda
2127 abstractions, one for each of the original arguments that are left
2130 The ith argument is replaced with the free variables of
2131 \lam{Y_i}. Note that we reuse the same binders as those used in
2132 \lam{Y_i}, since we can then just use \lam{Y_i} inside the new
2133 function body and have all of the variables it uses be in scope.
2135 The argument that we are specializing for, \lam{Y_i}, is put inside
2136 the new function body. The old function body is applied to it. Since
2137 we use this new function only in place of an application with that
2138 particular argument \lam{Y_i}, behavior should not change.
2140 Note that the types of the arguments of our new function are taken
2141 from the types of the \emph{actual} arguments (\lam{T0 ... Tn}). This
2142 means that any polymorphism in the arguments is removed, even when the
2143 corresponding explicit type lambda is not removed
2146 \todo{Examples. Perhaps reference the previous sections}
2148 \section{Unsolved problems}
2149 The above system of transformations has been implemented in the prototype
2150 and seems to work well to compile simple and more complex examples of
2151 hardware descriptions \cite[baaij09]. However, this normalization
2152 system has not seen enough review and work to be complete and work for
2153 every Core expression that is supplied to it. A number of problems
2154 have already been identified and are discussed in this section.
2156 \subsection[sec:normalization:duplicatework]{Work duplication}
2157 A possible problem of β-reduction is that it could duplicate work.
2158 When the expression applied is not a simple variable reference, but
2159 requires calculation and the binder the lambda abstraction binds to
2160 is used more than once, more hardware might be generated than strictly
2163 As an example, consider the expression:
2169 When applying β-reduction to this expression, we get:
2175 which of course calculates \lam{(a * b)} twice.
2177 A possible solution to this would be to use the following alternative
2178 transformation, which is of course no longer normal β-reduction. The
2179 following transformation has not been tested in the prototype, but is
2180 given here for future reference:
2188 This does not seem like much of an improvement, but it does get rid of
2189 the lambda expression (and the associated higher-order value), while
2190 at the same time introducing a new let binding. Since the result of
2191 every application or case expression must be bound by a let expression
2192 in the intended normal form anyway, this is probably not a problem. If
2193 the argument happens to be a variable reference, then simple let
2194 binding removal (\in{section}[sec:normalization:simplelet]) will
2195 remove it, making the result identical to that of the original
2196 β-reduction transformation.
2198 When also applying argument simplification to the above example, we
2199 get the following expression:
2207 Looking at this, we could imagine an alternative approach: create a
2208 transformation that removes let bindings that bind identical values.
2209 In the above expression, the \lam{y} and \lam{z} variables could be
2210 merged together, resulting in the more efficient expression:
2213 let y = (a * b) in y + y
2216 \subsection[sec:normalization:non-determinism]{Non-determinism}
2217 As an example, again consider the following expression:
2223 We can apply both β-reduction (\in{section}[sec:normalization:beta])
2224 as well as argument simplification
2225 (\in{section}[sec:normalization:argsimpl]) to this expression.
2227 When applying argument simplification first and then β-reduction, we
2228 get the following expression:
2231 let y = (a * b) in y + y
2234 When applying β-reduction first and then argument simplification, we
2235 get the following expression:
2243 As you can see, this is a different expression. This means that the
2244 order of expressions, does in fact change the resulting normal form,
2245 which is something that we would like to avoid. In this particular
2246 case one of the alternatives is even clearly more efficient, so we
2247 would of course like the more efficient form to be the normal form.
2249 For this particular problem, the solutions for duplication of work
2250 seem from the previous section seem to fix the determinism of our
2251 transformation system as well. However, it is likely that there are
2252 other occurrences of this problem.
2254 \subsection[sec:normalization:castproblems]{Casts}
2255 We do not fully understand the use of cast expressions in Core, so
2256 there are probably expressions involving cast expressions that cannot
2257 be brought into intended normal form by this transformation system.
2259 The uses of casts in the Core system should be investigated more and
2260 transformations will probably need updating to handle them in all
2263 \subsection[sec:normalization:stateproblems]{Normalization of stateful descriptions}
2264 Currently, the intended normal form definition\refdef{intended
2265 normal form definition} offers enough freedom to describe all
2266 valid stateful descriptions, but is not limiting enough. It is
2267 possible to write descriptions which are in intended normal
2268 form, but cannot be translated into \VHDL\ in a meaningful way
2269 (\eg, a function that swaps two substates in its result, or a
2270 function that changes a sub-state itself instead of passing it to
2273 It is now up to the programmer to not do anything funny with
2274 these state values, whereas the normalization just tries not to
2275 mess up the flow of state values. In practice, there are
2276 situations where a Core program that \emph{could} be a valid
2277 stateful description is not translatable by the prototype. This
2278 most often happens when statefulness is mixed with pattern
2279 matching, causing a state input to be unpacked multiple times or
2280 be unpacked and repacked only in some of the code paths.
2282 Without going into detail about the exact problems (of which
2283 there are probably more than have shown up so far), it seems
2284 unlikely that these problems can be solved entirely by just
2285 improving the \VHDL\ state generation in the final stage. The
2286 normalization stage seems the best place to apply the rewriting
2287 needed to support more complex stateful descriptions. This does
2288 of course mean that the intended normal form definition must be
2289 extended as well to be more specific about how state handling
2290 should look like in normal form.
2291 \in{Section}[sec:prototype:statelimits] already contains a
2292 tight description of the limitations on the use of state
2293 variables, which could be adapted into the intended normal form.
2295 \section[sec:normalization:properties]{Provable properties}
2296 When looking at the system of transformations outlined above, there are a
2297 number of questions that we can ask ourselves. The main question is of course:
2298 \quote{Does our system work as intended?}. We can split this question into a
2299 number of sub-questions:
2302 \item[q:termination] Does our system \emph{terminate}? Since our system will
2303 keep running as long as transformations apply, there is an obvious risk that
2304 it will keep running indefinitely. This typically happens when one
2305 transformation produces a result that is transformed back to the original
2306 by another transformation, or when one or more transformations keep
2307 expanding some expression.
2308 \item[q:soundness] Is our system \emph{sound}? Since our transformations
2309 continuously modify the expression, there is an obvious risk that the final
2310 normal form will not be equivalent to the original program: its meaning could
2312 \item[q:completeness] Is our system \emph{complete}? Since we have a complex
2313 system of transformations, there is an obvious risk that some expressions will
2314 not end up in our intended normal form, because we forgot some transformation.
2315 In other words: does our transformation system result in our intended normal
2316 form for all possible inputs?
2317 \item[q:determinism] Is our system \emph{deterministic}? Since we have defined
2318 no particular order in which the transformation should be applied, there is an
2319 obvious risk that different transformation orderings will result in
2320 \emph{different} normal forms. They might still both be intended normal forms
2321 (if our system is \emph{complete}) and describe correct hardware (if our
2322 system is \emph{sound}), so this property is less important than the previous
2323 three: the translator would still function properly without it.
2326 Unfortunately, the final transformation system has only been
2327 developed in the final part of the research, leaving no more time
2328 for verifying these properties. In fact, it is likely that the
2329 current transformation system still violates some of these
2330 properties in some cases (see
2331 \in{section}[sec:normalization:non-determinism] and
2332 \in{section}[sec:normalization:stateproblems]) and should be improved (or
2333 extra conditions on the input hardware descriptions should be formulated).
2335 This is most likely the case with the completeness and determinism
2336 properties, perhaps also the termination property. The soundness
2337 property probably holds, since it is easier to manually verify (each
2338 transformation can be reviewed separately).
2340 Even though no complete proofs have been made, some ideas for
2341 possible proof strategies are shown below.
2343 \subsection{Graph representation}
2344 Before looking into how to prove these properties, we will look at
2345 transformation systems from a graph perspective. We will first define
2346 the graph view and then illustrate it using a simple example from lambda
2347 calculus (which is a different system than the Cλash normalization
2348 system). The nodes of the graph are all possible Core expressions. The
2349 (directed) edges of the graph are transformations. When a transformation
2350 α applies to an expression \lam{A} to produce an expression \lam{B}, we
2351 add an edge from the node for \lam{A} to the node for \lam{B}, labeled
2354 \startuseMPgraphic{TransformGraph}
2358 newCircle.a(btex \lam{(λx.λy. (+) x y) 1} etex);
2359 newCircle.b(btex \lam{λy. (+) 1 y} etex);
2360 newCircle.c(btex \lam{(λx.(+) x) 1} etex);
2361 newCircle.d(btex \lam{(+) 1} etex);
2364 c.c = b.c + (4cm, 0cm);
2365 a.c = midpoint(b.c, c.c) + (0cm, 4cm);
2366 d.c = midpoint(b.c, c.c) - (0cm, 3cm);
2368 % β-conversion between a and b
2369 ncarc.a(a)(b) "name(bred)";
2370 ObjLabel.a(btex $\xrightarrow[normal]{}{β}$ etex) "labpathname(bred)", "labdir(rt)";
2371 ncarc.b(b)(a) "name(bexp)", "linestyle(dashed withdots)";
2372 ObjLabel.b(btex $\xleftarrow[normal]{}{β}$ etex) "labpathname(bexp)", "labdir(lft)";
2374 % η-conversion between a and c
2375 ncarc.a(a)(c) "name(ered)";
2376 ObjLabel.a(btex $\xrightarrow[normal]{}{η}$ etex) "labpathname(ered)", "labdir(rt)";
2377 ncarc.c(c)(a) "name(eexp)", "linestyle(dashed withdots)";
2378 ObjLabel.c(btex $\xleftarrow[normal]{}{η}$ etex) "labpathname(eexp)", "labdir(lft)";
2380 % η-conversion between b and d
2381 ncarc.b(b)(d) "name(ered)";
2382 ObjLabel.b(btex $\xrightarrow[normal]{}{η}$ etex) "labpathname(ered)", "labdir(rt)";
2383 ncarc.d(d)(b) "name(eexp)", "linestyle(dashed withdots)";
2384 ObjLabel.d(btex $\xleftarrow[normal]{}{η}$ etex) "labpathname(eexp)", "labdir(lft)";
2386 % β-conversion between c and d
2387 ncarc.c(c)(d) "name(bred)";
2388 ObjLabel.c(btex $\xrightarrow[normal]{}{β}$ etex) "labpathname(bred)", "labdir(rt)";
2389 ncarc.d(d)(c) "name(bexp)", "linestyle(dashed withdots)";
2390 ObjLabel.d(btex $\xleftarrow[normal]{}{β}$ etex) "labpathname(bexp)", "labdir(lft)";
2392 % Draw objects and lines
2393 drawObj(a, b, c, d);
2396 \placeexample[right][ex:TransformGraph]{Partial graph of a lambda calculus
2397 system with β and η reduction (solid lines) and expansion (dotted lines).}
2398 \boxedgraphic{TransformGraph}
2400 Of course the graph for Cλash is unbounded, since we can construct an
2401 infinite amount of Core expressions. Also, there might potentially be
2402 multiple edges between two given nodes (with different labels), though
2403 this seems unlikely to actually happen in our system.
2405 See \in{example}[ex:TransformGraph] for the graph representation of a very
2406 simple lambda calculus that contains just the expressions \lam{(λx.λy. (+) x
2407 y) 1}, \lam{λy. (+) 1 y}, \lam{(λx.(+) x) 1} and \lam{(+) 1}. The
2408 transformation system consists of β-reduction and η-reduction (solid edges) or
2409 β-expansion and η-expansion (dotted edges).
2411 \todo{Define β-reduction and η-reduction?}
2413 In such a graph a node (expression) is in normal form if it has no
2414 outgoing edges (meaning no transformation applies to it). The set of
2415 nodes without outgoing edges is called the \emph{normal set}. Similarly,
2416 the set of nodes containing expressions in intended normal form
2417 \refdef{intended normal form definition} is called the \emph{intended normal set}.
2419 From such a graph, we can derive some properties easily:
2421 \item A system will \emph{terminate} if there is no walk (sequence of
2422 edges, or transformations) of infinite length in the graph (this
2423 includes cycles, but can also happen without cycles).
2424 \item Soundness is not easily represented in the graph.
2425 \item A system is \emph{complete} if all of the nodes in the normal set have
2426 the intended normal form. The inverse (that all of the nodes outside of
2427 the normal set are \emph{not} in the intended normal form) is not
2428 strictly required. In other words, our normal set must be a
2429 subset of the intended normal form, but they do not need to be
2432 \item A system is deterministic if all paths starting at a particular
2433 node, which end in a node in the normal set, end at the same node.
2436 When looking at the \in{example}[ex:TransformGraph], we see that the system
2437 terminates for both the reduction and expansion systems (but note that, for
2438 expansion, this is only true because we have limited the possible
2439 expressions. In complete lambda calculus, there would be a path from
2440 \lam{(λx.λy. (+) x y) 1} to \lam{(λx.λy.(λz.(+) z) x y) 1} to
2441 \lam{(λx.λy.(λz.(λq.(+) q) z) x y) 1} etc.)
2443 If we would consider the system with both expansion and reduction, there
2444 would no longer be termination either, since there would be cycles all
2447 The reduction and expansion systems have a normal set of containing just
2448 \lam{(+) 1} or \lam{(λx.λy. (+) x y) 1} respectively. Since all paths in
2449 either system end up in these normal forms, both systems are \emph{complete}.
2450 Also, since there is only one node in the normal set, it must obviously be
2451 \emph{deterministic} as well.
2453 \subsection{Termination}
2454 In general, proving termination of an arbitrary program is a very
2455 hard problem. \todo{Ref about arbitrary termination} Fortunately,
2456 we only have to prove termination for our specific transformation
2459 A common approach for these kinds of proofs is to associate a
2460 measure with each possible expression in our system. If we can
2461 show that each transformation strictly decreases this measure
2462 (\ie, the expression transformed to has a lower measure than the
2463 expression transformed from). \todo{ref about measure-based
2464 termination proofs / analysis}
2466 A good measure for a system consisting of just β-reduction would
2467 be the number of lambda expressions in the expression. Since every
2468 application of β-reduction removes a lambda abstraction (and there
2469 is always a bounded number of lambda abstractions in every
2470 expression) we can easily see that a transformation system with
2471 just β-reduction will always terminate.
2473 For our complete system, this measure would be fairly complex
2474 (probably the sum of a lot of things). Since the (conditions on)
2475 our transformations are pretty complex, we would need to include
2476 both simple things like the number of let expressions as well as
2477 more complex things like the number of case expressions that are
2478 not yet in normal form.
2480 No real attempt has been made at finding a suitable measure for
2483 \subsection{Soundness}
2484 Soundness is a property that can be proven for each transformation
2485 separately. Since our system only runs separate transformations
2486 sequentially, if each of our transformations leaves the
2487 \emph{meaning} of the expression unchanged, then the entire system
2488 will of course leave the meaning unchanged and is thus
2491 The current prototype has only been verified in an ad hoc fashion
2492 by inspecting (the code for) each transformation. A more formal
2493 verification would be more appropriate.
2495 To be able to formally show that each transformation properly
2496 preserves the meaning of every expression, we require an exact
2497 definition of the \emph{meaning} of every expression, so we can
2498 compare them. A definition of the operational semantics of \GHC's Core
2499 language is available \cite[sulzmann07], but this does not seem
2500 sufficient for our goals (but it is a good start).
2502 It should be possible to have a single formal definition of
2503 meaning for Core for both normal Core compilation by \GHC\ and for
2504 our compilation to \VHDL. The main difference seems to be that in
2505 hardware every expression is always evaluated, while in software
2506 it is only evaluated if needed, but it should be possible to
2507 assign a meaning to Core expressions that assumes neither.
2509 Since each of the transformations can be applied to any
2510 sub-expression as well, there is a constraint on our meaning
2511 definition: the meaning of an expression should depend only on the
2512 meaning of sub-expressions, not on the expressions themselves. For
2513 example, the meaning of the application in \lam{f (let x = 4 in
2514 x)} should be the same as the meaning of the application in \lam{f
2515 4}, since the argument sub-expression has the same meaning (though
2516 the actual expression is different).
2518 \subsection{Completeness}
2519 Proving completeness is probably not hard, but it could be a lot
2520 of work. We have seen above that to prove completeness, we must
2521 show that the normal set of our graph representation is a subset
2522 of the intended normal set.
2524 However, it is hard to systematically generate or reason about the
2525 normal set, since it is defined as any nodes to which no
2526 transformation applies. To determine this set, each transformation
2527 must be considered and when a transformation is added, the entire
2528 set should be re-evaluated. This means it is hard to show that
2529 each node in the normal set is also in the intended normal set.
2530 Reasoning about our intended normal set is easier, since we know
2531 how to generate it from its definition. \refdef{intended normal
2534 Fortunately, we can also prove the complement (which is
2535 equivalent, since $A \subseteq B \Leftrightarrow \overline{B}
2536 \subseteq \overline{A}$): show that the set of nodes not in
2537 intended normal form is a subset of the set of nodes not in normal
2538 form. In other words, show that for every expression that is not
2539 in intended normal form, that there is at least one transformation
2540 that applies to it (since that means it is not in normal form
2541 either and since $A \subseteq C \Leftrightarrow \forall x (x \in A
2542 \rightarrow x \in C)$).
2544 By systematically reviewing the entire Core language definition
2545 along with the intended normal form definition (both of which have
2546 a similar structure), it should be possible to identify all
2547 possible (sets of) Core expressions that are not in intended
2548 normal form and identify a transformation that applies to it.
2550 This approach is especially useful for proving completeness of our
2551 system, since if expressions exist to which none of the
2552 transformations apply (\ie\ if the system is not yet complete), it
2553 is immediately clear which expressions these are and adding
2554 (or modifying) transformations to fix this should be relatively
2557 As observed above, applying this approach is a lot of work, since
2558 we need to check every (set of) transformation(s) separately.
2560 \todo{Perhaps do a few steps of the proofs as proof-of-concept}
2561 \subsection{Determinism}
2562 A well-known technique for proving determinism in lambda calculus
2563 and other reduction systems, is using the Church-Rosser property
2564 \cite[church36]. A reduction system has the CR property if and only if:
2566 \placedefinition[here]{Church-Rosser theorem}
2567 {\lam{\forall A, B, C \exists D (A ->> B ∧ A ->> C => B ->> D ∧ C ->> D)}}
2569 Here, \lam{A ->> B} means \lam{A} \emph{reduces to} \lam{B}. In
2570 other words, there is a set of transformations that can transform
2571 \lam{A} to \lam{B}. \lam{=>} is used to mean \emph{implies}.
2573 For a transformation system holding the Church-Rosser property, it
2574 is easy to show that it is in fact deterministic. Showing that this
2575 property actually holds is a harder problem, but has been
2576 done for some reduction systems in the lambda calculus
2577 \cite[klop80]\ \cite[barendregt84]. Doing the same for our
2578 transformation system is probably more complicated, but not
2581 % vim: set sw=2 sts=2 expandtab: