1 \chapter[chap:normalization]{Normalization}
3 % A helper to print a single example in the half the page width. The example
4 % text should be in a buffer whose name is given in an argument.
6 % The align=right option really does left-alignment, but without the program
7 % will end up on a single line. The strut=no option prevents a bunch of empty
8 % space at the start of the frame.
10 \framed[offset=1mm,align=right,strut=no,background=box,frame=off]{
11 \setuptyping[option=LAM,style=sans,before=,after=]
13 \setuptyping[option=none,style=\tttf]
18 % A transformation example
19 \definefloat[example][examples]
20 \setupcaption[example][location=top] % Put captions on top
22 \define[3]\transexample{
23 \placeexample[here]{#1}
24 \startcombination[2*1]
25 {\example{#2}}{Original program}
26 {\example{#3}}{Transformed program}
30 %\define[3]\transexampleh{
31 %% \placeexample[here]{#1}
32 %% \startcombination[1*2]
33 %% {\example{#2}}{Original program}
34 %% {\example{#3}}{Transformed program}
38 The first step in the core to \small{VHDL} translation process, is normalization. We
39 aim to bring the core description into a simpler form, which we can
40 subsequently translate into \small{VHDL} easily. This normal form is needed because
41 the full core language is more expressive than \small{VHDL} in some areas and because
42 core can describe expressions that do not have a direct hardware
45 TODO: Describe core properties not supported in \small{VHDL}, and describe how the
46 \small{VHDL} we want to generate should look like.
49 The transformations described here have a well-defined goal: To bring the
50 program in a well-defined form that is directly translatable to hardware,
51 while fully preserving the semantics of the program. We refer to this form as
52 the \emph{normal form} of the program. The formal definition of this normal
55 \placedefinition{}{A program is in \emph{normal form} if none of the
56 transformations from this chapter apply.}
58 Of course, this is an \quote{easy} definition of the normal form, since our
59 program will end up in normal form automatically. The more interesting part is
60 to see if this normal form actually has the properties we would like it to
63 But, before getting into more definitions and details about this normal form,
64 let's try to get a feeling for it first. The easiest way to do this is by
65 describing the things we want to not have in a normal form.
68 \item Any \emph{polymorphism} must be removed. When laying down hardware, we
69 can't generate any signals that can have multiple types. All types must be
70 completely known to generate hardware.
72 \item Any \emph{higher order} constructions must be removed. We can't
73 generate a hardware signal that contains a function, so all values,
74 arguments and returns values used must be first order.
76 \item Any complex \emph{nested scopes} must be removed. In the \small{VHDL}
77 description, every signal is in a single scope. Also, full expressions are
78 not supported everywhere (in particular port maps can only map signal names,
79 not expressions). To make the \small{VHDL} generation easy, all values must be bound
80 on the \quote{top level}.
83 TODO: Intermezzo: functions vs plain values
85 A very simple example of a program in normal form is given in
86 \in{example}[ex:MulSum]. As you can see, all arguments to the function (which
87 will become input ports in the final hardware) are at the top. This means that
88 the body of the final lambda abstraction is never a function, but always a
91 After the lambda abstractions, we see a single let expression, that binds two
92 variables (\lam{mul} and \lam{sum}). These variables will be signals in the
93 final hardware, bound to the output port of the \lam{*} and \lam{+}
96 The final line (the \quote{return value} of the function) selects the
97 \lam{sum} signal to be the output port of the function. This \quote{return
98 value} can always only be a variable reference, never a more complex
102 alu :: Bit -> Word -> Word -> Word
111 \startuseMPgraphic{MulSum}
112 save a, b, c, mul, add, sum;
115 newCircle.a(btex $a$ etex) "framed(false)";
116 newCircle.b(btex $b$ etex) "framed(false)";
117 newCircle.c(btex $c$ etex) "framed(false)";
118 newCircle.sum(btex $res$ etex) "framed(false)";
121 newCircle.mul(btex - etex);
122 newCircle.add(btex + etex);
124 a.c - b.c = (0cm, 2cm);
125 b.c - c.c = (0cm, 2cm);
126 add.c = c.c + (2cm, 0cm);
127 mul.c = midpoint(a.c, b.c) + (2cm, 0cm);
128 sum.c = add.c + (2cm, 0cm);
131 % Draw objects and lines
132 drawObj(a, b, c, mul, add, sum);
134 ncarc(a)(mul) "arcangle(15)";
135 ncarc(b)(mul) "arcangle(-15)";
141 \placeexample[here][ex:MulSum]{Simple architecture consisting of an adder and a
143 \startcombination[2*1]
144 {\typebufferlam{MulSum}}{Core description in normal form.}
145 {\boxedgraphic{MulSum}}{The architecture described by the normal form.}
148 The previous example described composing an architecture by calling other
149 functions (operators), resulting in a simple architecture with component and
150 connection. There is of course also some mechanism for choice in the normal
151 form. In a normal Core program, the \emph{case} expression can be used in a
152 few different ways to describe choice. In normal form, this is limited to a
155 \in{Example}[ex:AddSubAlu] shows an example describing a
156 simple \small{ALU}, which chooses between two operations based on an opcode
157 bit. The main structure is the same as in \in{example}[ex:MulSum], but this
158 time the \lam{res} variable is bound to a case expression. This case
159 expression scrutinizes the variable \lam{opcode} (and scrutinizing more
160 complex expressions is not supported). The case expression can select a
161 different variable based on the constructor of \lam{opcode}.
163 \startbuffer[AddSubAlu]
164 alu :: Bit -> Word -> Word -> Word
176 \startuseMPgraphic{AddSubAlu}
177 save opcode, a, b, add, sub, mux, res;
180 newCircle.opcode(btex $opcode$ etex) "framed(false)";
181 newCircle.a(btex $a$ etex) "framed(false)";
182 newCircle.b(btex $b$ etex) "framed(false)";
183 newCircle.res(btex $res$ etex) "framed(false)";
185 newCircle.add(btex + etex);
186 newCircle.sub(btex - etex);
189 opcode.c - a.c = (0cm, 2cm);
190 add.c - a.c = (4cm, 0cm);
191 sub.c - b.c = (4cm, 0cm);
192 a.c - b.c = (0cm, 3cm);
193 mux.c = midpoint(add.c, sub.c) + (1.5cm, 0cm);
194 res.c - mux.c = (1.5cm, 0cm);
197 % Draw objects and lines
198 drawObj(opcode, a, b, res, add, sub, mux);
200 ncline(a)(add) "posA(e)";
201 ncline(b)(sub) "posA(e)";
202 nccurve(a)(sub) "posA(e)", "angleA(0)";
203 nccurve(b)(add) "posA(e)", "angleA(0)";
204 nccurve(add)(mux) "posB(inpa)", "angleB(0)";
205 nccurve(sub)(mux) "posB(inpb)", "angleB(0)";
206 nccurve(opcode)(mux) "posB(n)", "angleA(0)", "angleB(-90)";
207 ncline(mux)(res) "posA(out)";
210 \placeexample[here][ex:AddSubAlu]{Simple \small{ALU} supporting two operations.}
211 \startcombination[2*1]
212 {\typebufferlam{AddSubAlu}}{Core description in normal form.}
213 {\boxedgraphic{AddSubAlu}}{The architecture described by the normal form.}
216 As a more complete example, consider \in{example}[ex:NormalComplete]. This
217 example contains everything that is supported in normal form, with the
218 exception of builtin higher order functions. The graphical version of the
219 architecture contains a slightly simplified version, since the state tuple
220 packing and unpacking have been left out. Instead, two seperate registers are
221 drawn. Also note that most synthesis tools will further optimize this
222 architecture by removing the multiplexers at the register input and replace
223 them with some logic in the clock inputs, but we want to show the architecture
224 as close to the description as possible.
226 \startbuffer[NormalComplete]
229 -> State (Word, Word)
230 -> (State (Word, Word), Word)
232 -- All arguments are an inital lambda
234 -- There are nested let expressions at top level
236 -- Unpack the state by coercion (\eg, cast from
237 -- State (Word, Word) to (Word, Word))
238 s = sp :: (Word, Word)
239 -- Extract both registers from the state
240 r1 = case s of (fst, snd) -> fst
241 r2 = case s of (fst, snd) -> snd
242 -- Calling some other user-defined function.
244 -- Conditional connections
256 -- pack the state by coercion (\eg, cast from
257 -- (Word, Word) to State (Word, Word))
258 sp' = s' :: State (Word, Word)
259 -- Pack our return value
266 \startuseMPgraphic{NormalComplete}
267 save a, d, r, foo, muxr, muxout, out;
270 newCircle.a(btex \lam{a} etex) "framed(false)";
271 newCircle.d(btex \lam{d} etex) "framed(false)";
272 newCircle.out(btex \lam{out} etex) "framed(false)";
274 %newCircle.add(btex + etex);
275 newBox.foo(btex \lam{foo} etex);
276 newReg.r1(btex $\lam{r1}$ etex) "dx(4mm)", "dy(6mm)";
277 newReg.r2(btex $\lam{r2}$ etex) "dx(4mm)", "dy(6mm)", "reflect(true)";
279 % Reflect over the vertical axis
280 reflectObj(muxr1)((0,0), (0,1));
283 rotateObj(muxout)(-90);
285 d.c = foo.c + (0cm, 1.5cm);
286 a.c = (xpart r2.c + 2cm, ypart d.c - 0.5cm);
287 foo.c = midpoint(muxr1.c, muxr2.c) + (0cm, 2cm);
288 muxr1.c = r1.c + (0cm, 2cm);
289 muxr2.c = r2.c + (0cm, 2cm);
290 r2.c = r1.c + (4cm, 0cm);
292 muxout.c = midpoint(r1.c, r2.c) - (0cm, 2cm);
293 out.c = muxout.c - (0cm, 1.5cm);
295 % % Draw objects and lines
296 drawObj(a, d, foo, r1, r2, muxr1, muxr2, muxout, out);
299 nccurve(foo)(muxr1) "angleA(-90)", "posB(inpa)", "angleB(180)";
300 nccurve(foo)(muxr2) "angleA(-90)", "posB(inpb)", "angleB(0)";
301 nccurve(muxr1)(r1) "posA(out)", "angleA(180)", "posB(d)", "angleB(0)";
302 nccurve(r1)(muxr1) "posA(out)", "angleA(0)", "posB(inpb)", "angleB(180)";
303 nccurve(muxr2)(r2) "posA(out)", "angleA(0)", "posB(d)", "angleB(180)";
304 nccurve(r2)(muxr2) "posA(out)", "angleA(180)", "posB(inpa)", "angleB(0)";
305 nccurve(r1)(muxout) "posA(out)", "angleA(0)", "posB(inpb)", "angleB(-90)";
306 nccurve(r2)(muxout) "posA(out)", "angleA(180)", "posB(inpa)", "angleB(-90)";
308 nccurve(a)(muxout) "angleA(-90)", "angleB(180)", "posB(sel)";
309 nccurve(a)(muxr1) "angleA(180)", "angleB(-90)", "posB(sel)";
310 nccurve(a)(muxr2) "angleA(180)", "angleB(-90)", "posB(sel)";
311 ncline(muxout)(out) "posA(out)";
314 \placeexample[here][ex:NormalComplete]{Simple architecture consisting of an adder and a
316 \startcombination[2*1]
317 {\typebufferlam{NormalComplete}}{Core description in normal form.}
318 {\boxedgraphic{NormalComplete}}{The architecture described by the normal form.}
321 \subsection{Normal form definition}
322 Now we have some intuition for the normal form, we can describe how we want
323 the normal form to look like in a slightly more formal manner. The following
324 EBNF-like description completely captures the intended structure (and
325 generates a subset of GHC's core format).
327 Some clauses have an expression listed in parentheses. These are conditions
328 that need to apply to the clause.
331 \italic{normal} = \italic{lambda}
332 \italic{lambda} = λvar.\italic{lambda} (representable(var))
334 \italic{toplet} = let \italic{binding} in \italic{toplet}
335 | letrec [\italic{binding}] in \italic{toplet}
336 | var (representable(varvar))
337 \italic{binding} = var = \italic{rhs} (representable(rhs))
338 -- State packing and unpacking by coercion
339 | var0 = var1 :: State ty (lvar(var1))
340 | var0 = var1 :: ty (var0 :: State ty) (lvar(var1))
341 \italic{rhs} = userapp
344 | case var of C a0 ... an -> ai (lvar(var))
346 | case var of (lvar(var))
347 DEFAULT -> var0 (lvar(var0))
348 C w0 ... wn -> resvar (\forall{}i, wi \neq resvar, lvar(resvar))
349 \italic{userapp} = \italic{userfunc}
350 | \italic{userapp} {userarg}
351 \italic{userfunc} = var (gvar(var))
352 \italic{userarg} = var (lvar(var))
353 \italic{builtinapp} = \italic{builtinfunc}
354 | \italic{builtinapp} \italic{builtinarg}
355 \italic{builtinfunc} = var (bvar(var))
356 \italic{builtinarg} = \italic{coreexpr}
359 -- TODO: Limit builtinarg further
361 -- TODO: There can still be other casts around (which the code can handle,
362 e.g., ignore), which still need to be documented here.
364 -- TODO: Note about the selector case. It just supports Bit and Bool
365 currently, perhaps it should be generalized in the normal form?
367 When looking at such a program from a hardware perspective, the top level
368 lambda's define the input ports. The value produced by the let expression is
369 the output port. Most function applications bound by the let expression
370 define a component instantiation, where the input and output ports are mapped
371 to local signals or arguments. Some of the others use a builtin
372 construction (\eg the \lam{case} statement) or call a builtin function
373 (\eg \lam{add} or \lam{sub}). For these, a hardcoded \small{VHDL} translation is
376 \subsection{Definitions}
377 In the following sections, we will be using a number of functions and
378 notations, which we will define here.
380 \subsubsection{Transformations}
381 The most important notation is the one for transformation, which looks like
388 ------------------------ expression conditions
394 Here, we describe a transformation. The most import parts are \lam{from} and
395 \lam{to}, which describe the Core expresssion that should be matched and the
396 expression that it should be replaced with. This matching can occur anywhere
397 in function that is being normalized, so it applies to any subexpression as
400 The \lam{expression conditions} list a number of conditions on the \lam{from}
401 expression that must hold for the transformation to apply.
403 Furthermore, there is some way to look into the environment (\eg, other top
404 level bindings). The \lam{context conditions} part specifies any number of
405 top level bindings that must be present for the transformation to apply.
406 Usually, this lists a top level binding that binds an identfier that is also
407 used in the \lam{from} expression, allowing us to "access" the value of a top
408 level binding in the \lam{to} expression (\eg, for inlining).
410 Finally, there is a way to influence the environment. The \lam{context
411 additions} part lists any number of new top level bindings that should be
414 If there are no \lam{context conditions} or \lam{context additions}, they can
415 be left out alltogether, along with the separator \lam{~}.
419 \subsubsection{Other concepts}
420 A \emph{global variable} is any variable that is bound at the
421 top level of a program, or an external module. A local variable is any other
422 variable (\eg, variables local to a function, which can be bound by lambda
423 abstractions, let expressions and case expressions).
425 A \emph{hardware representable} type is a type that we can generate
426 a signal for in hardware. For example, a bit, a vector of bits, a 32 bit
427 unsigned word, etc. Types that are not runtime representable notably
428 include (but are not limited to): Types, dictionaries, functions.
430 A \emph{builtin function} is a function for which a builtin
431 hardware translation is available, because its actual definition is not
432 translatable. A user-defined function is any other function.
434 \subsubsection{Functions}
435 Here, we define a number of functions that can be used below to concisely
438 \emph{gvar(expr)} is true when \emph{expr} is a variable that references a
439 global variable. It is false when it references a local variable.
441 \emph{lvar(expr)} is the inverse of \emph{gvar}; it is true when \emph{expr}
442 references a local variable, false when it references a global variable.
444 \emph{representable(expr)} or \emph{representable(var)} is true when
445 \emph{expr} or \emph{var} has a type that is representable at runtime.
447 \section{Transform passes}
448 In this section we describe the actual transforms. Here we're using
449 the core language in a notation that resembles lambda calculus.
451 Each of these transforms is meant to be applied to every (sub)expression
452 in a program, for as long as it applies. Only when none of the
453 transformations can be applied anymore, the program is in normal form (by
454 definition). We hope to be able to prove that this form will obey all of the
455 constraints defined above, but this has yet to happen (though it seems likely
458 Each of the transforms will be described informally first, explaining
459 the need for and goal of the transform. Then, a formal definition is
460 given, using a familiar syntax from the world of logic. Each transform
461 is specified as a number of conditions (above the horizontal line) and a
462 number of conclusions (below the horizontal line). The details of using
463 this notation are still a bit fuzzy, so comments are welcom.
465 TODO: Formally describe the "apply to every (sub)expression" in terms of
466 rules with full transformations in the conditions.
468 \subsection{η-abstraction}
469 This transformation makes sure that all arguments of a function-typed
470 expression are named, by introducing lambda expressions. When combined with
471 β-reduction and function inlining below, all function-typed expressions should
472 be lambda abstractions or global identifiers.
476 -------------- \lam{E} is not the first argument of an application.
477 λx.E x \lam{E} is not a lambda abstraction.
478 \lam{x} is a variable that does not occur free in \lam{E}.
488 foo = λa.λx.(case a of
493 \transexample{η-abstraction}{from}{to}
495 \subsection{Extended β-reduction}
496 This transformation is meant to propagate application expressions downwards
497 into expressions as far as possible. In lambda calculus, this reduction
498 is known as β-reduction, but it is of course only defined for
499 applications of lambda abstractions. We extend this reduction to also
500 work for the rest of core (case and let expressions).
522 For lambda expressions:
535 b = (let y = 3 in add y) 2
545 b = let y = 3 in add y 2
550 \transexample{Extended β-reduction}{from}{to}
552 \subsection{Let derecursification}
553 This transformation is meant to make lets non-recursive whenever possible.
554 This might allow other optimizations to do their work better. TODO: Why is
557 \subsection{Let flattening}
558 This transformation puts nested lets in the same scope, by lifting the
559 binding(s) of the inner let into a new let around the outer let. Eventually,
560 this will cause all let bindings to appear in the same scope (they will all be
561 in scope for the function return value).
563 Note that this transformation does not try to be smart when faced with
564 recursive lets, it will just leave the lets recursive (possibly joining a
565 recursive and non-recursive let into a single recursive let). The let
566 rederursification transformation will do this instead.
569 letnonrec x = (let bindings in M) in N
570 ------------------------------------------
571 let bindings in (letnonrec x = M) in N
577 x = (let bindings in M)
581 ------------------------------------------
600 b = let c = 3 in a + c
621 \transexample{Let flattening}{from}{to}
623 \subsection{Empty let removal}
624 This transformation is simple: It removes recursive lets that have no bindings
625 (which usually occurs when let derecursification removes the last binding from
634 \subsection{Simple let binding removal}
635 This transformation inlines simple let bindings (\eg a = b).
637 This transformation is not needed to get into normal form, but makes the
638 resulting \small{VHDL} a lot shorter.
664 \subsection{Unused let binding removal}
665 This transformation removes let bindings that are never used. Usually,
666 the desugarer introduces some unused let bindings.
668 This normalization pass should really be unneeded to get into normal form
669 (since ununsed bindings are not forbidden by the normal form), but in practice
670 the desugarer or simplifier emits some unused bindings that cannot be
671 normalized (e.g., calls to a \type{PatError} (TODO: Check this name)). Also,
672 this transformation makes the resulting \small{VHDL} a lot shorter.
676 ---------------------------- \lam{a} does not occur free in \lam{M}
687 ---------------------------- \lam{a} does not occur free in \lam{M}
695 \subsection{Non-representable binding inlining}
696 This transform inlines let bindings that have a non-representable type. Since
697 we can never generate a signal assignment for these bindings (we cannot
698 declare a signal assignment with a non-representable type, for obvious
699 reasons), we have no choice but to inline the binding to remove it.
701 If the binding is non-representable because it is a lambda abstraction, it is
702 likely that it will inlined into an application and β-reduction will remove
703 the lambda abstraction and turn it into a representable expression at the
704 inline site. The same holds for partial applications, which can be turned into
705 full applications by inlining.
707 Other cases of non-representable bindings we see in practice are primitive
708 Haskell types. In most cases, these will not result in a valid normalized
709 output, but then the input would have been invalid to start with. There is one
710 exception to this: When a builtin function is applied to a non-representable
711 expression, things might work out in some cases. For example, when you write a
712 literal \hs{SizedInt} in Haskell, like \hs{1 :: SizedInt D8}, this results in
713 the following core: \lam{fromInteger (smallInteger 10)}, where for example
714 \lam{10 :: GHC.Prim.Int\#} and \lam{smallInteger 10 :: Integer} have
715 non-representable types. TODO: This/these paragraph(s) should probably become a
716 separate discussion somewhere else.
720 -------------------------- \lam{E} has a non-representable type.
731 -------------------------- \lam{E} has a non-representable type.
751 x = fromInteger (smallInteger 10)
753 (λa -> add a 1) (add 1 x)
756 \transexample{Let flattening}{from}{to}
758 \subsection{Compiler generated top level binding inlining}
761 \subsection{Scrutinee simplification}
762 This transform ensures that the scrutinee of a case expression is always
763 a simple variable reference.
768 ----------------- \lam{E} is not a local variable reference
787 \transexample{Let flattening}{from}{to}
790 \subsection{Case simplification}
791 This transformation ensures that all case expressions become normal form. This
792 means they will become one of:
794 \item An extractor case with a single alternative that picks a single field
795 from a datatype, \eg \lam{case x of (a, b) -> a}.
796 \item A selector case with multiple alternatives and only wild binders, that
797 makes a choice between expressions based on the constructor of another
798 expression, \eg \lam{case x of Low -> a; High -> b}.
803 C0 v0,0 ... v0,m -> E0
805 Cn vn,0 ... vn,m -> En
806 --------------------------------------------------- \forall i \forall j, 0 <= i <= n, 0 <= i < m (\lam{wi,j} is a wild (unused) binder)
808 v0,0 = case x of C0 v0,0 .. v0,m -> v0,0
810 v0,m = case x of C0 v0,0 .. v0,m -> v0,m
813 vn,m = case x of Cn vn,0 .. vn,m -> vn,m
817 C0 w0,0 ... w0,m -> x0
819 Cn wn,0 ... wn,m -> xn
822 TODO: This transformation specified like this is complicated and misses
823 conditions to prevent looping with itself. Perhaps we should split it here for
842 \transexample{Selector case simplification}{from}{to}
850 b = case a of (,) b c -> b
851 c = case a of (,) b c -> c
858 \transexample{Extractor case simplification}{from}{to}
860 \subsection{Case removal}
861 This transform removes any case statements with a single alternative and
864 These "useless" case statements are usually leftovers from case simplification
865 on extractor case (see the previous example).
870 ---------------------- \lam{\forall i, 0 <= i <= m} (\lam{vi} does not occur free in E)
883 \transexample{Case removal}{from}{to}
885 \subsection{Argument simplification}
886 The transforms in this section deal with simplifying application
887 arguments into normal form. The goal here is to:
890 \item Make all arguments of user-defined functions (\eg, of which
891 we have a function body) simple variable references of a runtime
892 representable type. This is needed, since these applications will be turned
893 into component instantiations.
894 \item Make all arguments of builtin functions one of:
896 \item A type argument.
897 \item A dictionary argument.
898 \item A type level expression.
899 \item A variable reference of a runtime representable type.
900 \item A variable reference or partial application of a function type.
904 When looking at the arguments of a user-defined function, we can
905 divide them into two categories:
907 \item Arguments of a runtime representable type (\eg bits or vectors).
909 These arguments can be preserved in the program, since they can
910 be translated to input ports later on. However, since we can
911 only connect signals to input ports, these arguments must be
912 reduced to simple variables (for which signals will be
913 produced). This is taken care of by the argument extraction
915 \item Non-runtime representable typed arguments.
917 These arguments cannot be preserved in the program, since we
918 cannot represent them as input or output ports in the resulting
919 \small{VHDL}. To remove them, we create a specialized version of the
920 called function with these arguments filled in. This is done by
921 the argument propagation transform.
923 Typically, these arguments are type and dictionary arguments that are
924 used to make functions polymorphic. By propagating these arguments, we
925 are essentially doing the same which GHC does when it specializes
926 functions: Creating multiple variants of the same function, one for
927 each type for which it is used. Other common non-representable
928 arguments are functions, e.g. when calling a higher order function
929 with another function or a lambda abstraction as an argument.
931 The reason for doing this is similar to the reasoning provided for
932 the inlining of non-representable let bindings above. In fact, this
933 argument propagation could be viewed as a form of cross-function
937 TODO: Check the following itemization.
939 When looking at the arguments of a builtin function, we can divide them
943 \item Arguments of a runtime representable type.
945 As we have seen with user-defined functions, these arguments can
946 always be reduced to a simple variable reference, by the
947 argument extraction transform. Performing this transform for
948 builtin functions as well, means that the translation of builtin
949 functions can be limited to signal references, instead of
950 needing to support all possible expressions.
952 \item Arguments of a function type.
954 These arguments are functions passed to higher order builtins,
955 like \lam{map} and \lam{foldl}. Since implementing these
956 functions for arbitrary function-typed expressions (\eg, lambda
957 expressions) is rather comlex, we reduce these arguments to
958 (partial applications of) global functions.
960 We can still support arbitrary expressions from the user code,
961 by creating a new global function containing that expression.
962 This way, we can simply replace the argument with a reference to
963 that new function. However, since the expression can contain any
964 number of free variables we also have to include partial
965 applications in our normal form.
967 This category of arguments is handled by the function extraction
969 \item Other unrepresentable arguments.
971 These arguments can take a few different forms:
972 \startdesc{Type arguments}
973 In the core language, type arguments can only take a single
974 form: A type wrapped in the Type constructor. Also, there is
975 nothing that can be done with type expressions, except for
976 applying functions to them, so we can simply leave type
977 arguments as they are.
979 \startdesc{Dictionary arguments}
980 In the core language, dictionary arguments are used to find
981 operations operating on one of the type arguments (mostly for
982 finding class methods). Since we will not actually evaluatie
983 the function body for builtin functions and can generate
984 code for builtin functions by just looking at the type
985 arguments, these arguments can be ignored and left as they
988 \startdesc{Type level arguments}
989 Sometimes, we want to pass a value to a builtin function, but
990 we need to know the value at compile time. Additionally, the
991 value has an impact on the type of the function. This is
992 encoded using type-level values, where the actual value of the
993 argument is not important, but the type encodes some integer,
994 for example. Since the value is not important, the actual form
995 of the expression does not matter either and we can leave
996 these arguments as they are.
998 \startdesc{Other arguments}
999 Technically, there is still a wide array of arguments that can
1000 be passed, but does not fall into any of the above categories.
1001 However, none of the supported builtin functions requires such
1002 an argument. This leaves use with passing unsupported types to
1003 a function, such as calling \lam{head} on a list of functions.
1005 In these cases, it would be impossible to generate hardware
1006 for such a function call anyway, so we can ignore these
1009 The only way to generate hardware for builtin functions with
1010 arguments like these, is to expand the function call into an
1011 equivalent core expression (\eg, expand map into a series of
1012 function applications). But for now, we choose to simply not
1013 support expressions like these.
1016 From the above, we can conclude that we can simply ignore these
1017 other unrepresentable arguments and focus on the first two
1021 \subsubsection{Argument simplification}
1022 This transform deals with arguments to functions that
1023 are of a runtime representable type. It ensures that they will all become
1024 references to global variables, or local signals in the resulting \small{VHDL}.
1026 TODO: It seems we can map an expression to a port, not only a signal.
1027 Perhaps this makes this transformation not needed?
1028 TODO: Say something about dataconstructors (without arguments, like True
1029 or False), which are variable references of a runtime representable
1030 type, but do not result in a signal.
1032 To reduce a complex expression to a simple variable reference, we create
1033 a new let expression around the application, which binds the complex
1034 expression to a new variable. The original function is then applied to
1039 -------------------- \lam{N} is of a representable type
1040 let x = N in M x \lam{N} is not a local variable reference
1048 let x = add a 1 in add x 1
1051 \transexample{Argument extraction}{from}{to}
1053 \subsubsection{Function extraction}
1054 This transform deals with function-typed arguments to builtin functions.
1055 Since these arguments cannot be propagated, we choose to extract them
1056 into a new global function instead.
1058 Any free variables occuring in the extracted arguments will become
1059 parameters to the new global function. The original argument is replaced
1060 with a reference to the new function, applied to any free variables from
1061 the original argument.
1063 This transformation is useful when applying higher order builtin functions
1064 like \hs{map} to a lambda abstraction, for example. In this case, the code
1065 that generates \small{VHDL} for \hs{map} only needs to handle top level functions and
1066 partial applications, not any other expression (such as lambda abstractions or
1067 even more complicated expressions).
1070 M N \lam{M} is a (partial aplication of a) builtin function.
1071 --------------------- \lam{f0 ... fn} = free local variables of \lam{N}
1072 M x f0 ... fn \lam{N :: a -> b}
1073 ~ \lam{N} is not a (partial application of) a top level function
1078 map (λa . add a b) xs
1092 \transexample{Function extraction}{from}{to}
1094 \subsubsection{Argument propagation}
1095 This transform deals with arguments to user-defined functions that are
1096 not representable at runtime. This means these arguments cannot be
1097 preserved in the final form and most be {\em propagated}.
1099 Propagation means to create a specialized version of the called
1100 function, with the propagated argument already filled in. As a simple
1101 example, in the following program:
1108 we could {\em propagate} the constant argument 1, with the following
1116 Special care must be taken when the to-be-propagated expression has any
1117 free variables. If this is the case, the original argument should not be
1118 removed alltogether, but replaced by all the free variables of the
1119 expression. In this way, the original expression can still be evaluated
1120 inside the new function. Also, this brings us closer to our goal: All
1121 these free variables will be simple variable references.
1123 To prevent us from propagating the same argument over and over, a simple
1124 local variable reference is not propagated (since is has exactly one
1125 free variable, itself, we would only replace that argument with itself).
1127 This shows that any free local variables that are not runtime representable
1128 cannot be brought into normal form by this transform. We rely on an
1129 inlining transformation to replace such a variable with an expression we
1130 can propagate again.
1135 x Y0 ... Yi ... Yn \lam{Yi} is not of a runtime representable type
1136 --------------------------------------------- \lam{Yi} is not a local variable reference
1137 x' y0 ... yi-1 f0 ... fm Yi+1 ... Yn \lam{f0 ... fm} = free local vars of \lam{Yi}
1139 x' = λy0 ... yi-1 f0 ... fm yi+1 ... yn .
1140 E y0 ... yi-1 Yi yi+1 ... yn
1146 \subsection{Cast propagation / simplification}
1147 This transform pushes casts down into the expression as far as possible. Since
1148 its exact role and need is not clear yet, this transformation is not yet
1151 \subsection{Return value simplification}
1152 This transformation ensures that the return value of a function is always a
1153 simple local variable reference.
1155 Currently implemented using lambda simplification, let simplification, and
1156 top simplification. Should change into something like the following, which
1157 works only on the result of a function instead of any subexpression. This is
1158 achieved by the contexts, like \lam{x = E}, though this is strictly not
1159 correct (you could read this as "if there is any function \lam{x} that binds
1160 \lam{E}, any \lam{E} can be transformed, while we only mean the \lam{E} that
1161 is bound by \lam{x}. This might need some extra notes or something).
1164 x = E \lam{E} is representable
1165 ~ \lam{E} is not a lambda abstraction
1166 E \lam{E} is not a let expression
1167 --------------------------- \lam{E} is not a local variable reference
1173 ~ \lam{E} is representable
1174 E \lam{E} is not a let expression
1175 --------------------------- \lam{E} is not a local variable reference
1180 x = λv0 ... λvn.let ... in E
1181 ~ \lam{E} is representable
1182 E \lam{E} is not a local variable reference
1183 ---------------------------
1192 x = let x = add 1 2 in x
1195 \transexample{Return value simplification}{from}{to}