forked from blynn/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern.lhs
More file actions
329 lines (261 loc) · 9.33 KB
/
pattern.lhs
File metadata and controls
329 lines (261 loc) · 9.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
= Patterns and Guards =
++++++++++
<script>
function hideshow(s) {
var x = document.getElementById(s);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
++++++++++
Patterns and guards make programming much more pleasant.
== Patty ==
We support definitions spread across multiple equations, and patterns in
lambdas, case expressions, and the arguments of function definitions.
We leave supporting patterns as the left-hand side of an equation for another
day. We also ignore fixity declarations for pattern infix operators.
*Definitions with multiple equations*
Consider a top-level function defined with multiple equations:
\begin{code}
f (Left (Right x)) y z@(_, 42) = expr1:
f (Right a) "foo" y = expr2;
\end{code}
This is parsed as:
\begin{code}
f = Pa [ ([(Left (Right x)), y, z@(_, 42)], expr1)
, ([(Right a) , "foo", y ], expr2)
]
\end{code}
where `Pa` is a data constructor for holding a collection of lists of patterns
and their corresponding expressions.
We rewrite this as a lambda expression. Since there are 3 parameters, we
generate 3 variable names and begin defining `f` as:
\begin{code}
f = \1# 2# 3# -> ...
\end{code}
In our example, we start numbering the generated variable names from 1, but in
general they start from the value of a carefully maintained counter.
We bind a `join#` variable that represents a join point which we later
construct from the other defining equations.
\begin{code}
f = \1# 2# 3# -> \join# -> ...
\end{code}
The first pattern of the first equation is `(Left (Right x))`, so we add:
\begin{code}
f = \1# 2# 3# -> \join# -> case 1# of
{ Left 4# -> case 4# of
{ Left _ -> join#
; Right 5# -> ...
}
; Right _ -> join#
}
\end{code}
We encounter a variable `x` so we replace all free occurrences of `x` in
`expr1` with `5#` which we denote `expr1[5#/x]`.
The second pattern is `y`, so we replace all free occurrences of this
variable in `expr1` with `2#` to get `expr1[5#/x,2#/y]`.
For the third pattern, we start by replacing all free occurrences of `z` in
with `3#`. We have finished the first equation so we apply what we have so far
to the expression we will obtain from rewriting the other definitions.
\begin{code}
f = \1# 2# 3# -> (\join# -> case 1# of
{ Left 4# -> case 4# of
{ Left _ -> join#
; Right 5# -> case 3# of
{ (6#, 7#) -> if 7# == 42 then expr1[5#/x,2#/y,3#/z] else join#
}
}
; Right _ -> join#
}) $ ...
\end{code}
The first pattern of the second equation is `Right a`.
\begin{code}
f = \1# 2# 3# -> (\join# -> case 1# of
{ Left 4# -> case 4# of
{ Left _ -> join#
; Right 5# -> case 3# of
{ (6#, 7#) -> if 7# == 42 then expr1[5#/x,2#/y,3#/z] else join#
}
}
; Right _ -> join#
}) $ \join# -> case 1# of
{ Left _ -> join#
; Right 8# -> ...
}
\end{code}
We replace all free occurrences of `a` in `expr2` with `8#`, which we denote
`expr2[8#/a]`.
Continuing in this fashion, by the end of the second equation we arrive at:
\begin{code}
f = \1# 2# 3# -> (\join# -> case 1# of
{ Left 4# -> case 4# of
{ Left _ -> join#
; Right 5# -> case 3# of
{ (6#, 7#) -> if 7# == 42 then expr1[5#/x,2#/y,3#/z] else join#
}
}
; Right _ -> join#
}) $ (\join# -> case 1# of
{ Left _ -> join#
; Right 8# -> if 2# == "foo" then expr2[8#/a,3#/y] else join#
}) $ ...
\end{code}
As there are no more equations, we finish off with `fail#`, which causes
program termination on execution:
\begin{code}
f = \1# 2# 3# -> (\join# -> case 1# of
{ Left 4# -> case 4# of
{ Left _ -> join#
; Right 5# -> case 3# of
{ (6#, 7#) -> if 7# == 42 then expr1[5#/x,2#/y,3#/z] else join#
}
}
; Right _ -> join#
}) $ (\join# -> case 1# of
{ Left _ -> join#
; Right 8# -> if 2# == "foo" then expr2[8#/a,3#/y] else join#
}) $ fail#
\end{code}
*Case expressions*
We could apply the above to rewrite case expressions, but then we'd lose
efficiency from performing a series of binary decisions instead of a single
multi-way decision.
Instead, suppose we have:
\begin{code}
case scrutinee of
Foo (Left 42) -> expr1
Baz -> expr2
Foo (Right a) -> expr3
Bar x "bar" -> expr4
z -> expr5
w -> expr6
Baz -> expr7
Bar x y -> expr8
x -> expr9
\end{code}
Conceptually, we combine contiguous data constructor alternatives into maps,
where the keys are the data constructors, and the values are the corresponding
expressions appended in the order they appear.
\begin{code}
[ (Foo, [(Left 42) -> expr1, (Right a) -> expr3])
, (Bar, [x "bar" -> expr4])
, (Baz, [ -> expr2])
]
z -> expr5
w -> expr6
[ (Bar, [x y -> expr8])
, (Baz, [ -> expr7])
]
x -> expr9
\end{code}
We rewrite this to:
\begin{code}
(\v -> (\cjoin# -> case v of
Foo 1# -> Pa [(Left 42) -> expr1, (Right a) -> expr3]
Bar -> Pa [x "bar" -> expr4]
Baz -> Pa [ -> expr2]
) $ (\pjoin# -> expr5[v/z]
) $ (\pjoin# -> expr6[v/z]
) $ (\cjoin# -> case v of
Foo _ -> cjoin#
Bar -> [x y -> expr8]
Baz -> Pa [ -> expr7]
) $ (V "fail#")
) scrutinee
\end{code}
We then apply the first rewrite algorithm to get:
\begin{code}
(\v -> (\cjoin# -> case v of
Foo 1# -> case 1# of
Left 2# -> if 2# == 42 then expr1 else cjoin#
Right 3# -> expr3[3#/a]
Bar 4# 5# -> if 5# == "bar" then expr 4 else cjoin#
Baz -> expr2
) $ (\pjoin# -> expr5[v/z]
) $ (\pjoin# -> expr6[v/z]
) $ (\cjoin# -> case v of
Foo 8# -> cjoin#
Bar 9# 10# -> expr8[9#/x 10#/y]
Baz -> expr7
) $ (V "fail#")
) scrutinee
\end{code}
Our pattern rewriting algorithm sets `pjoin#` to `fail#`, that is, if none of
the given patterns match, then the program exits. Our case rewriting algorithm
subverts this by inserting a catch-all case that calls `cjoin#` before calling
the pattern rewriting algorithm, so that instead of exiting, we examine the
next batch of case patterns. We can probably refactor so that only one type of
join point is needed, but for now we press on.
We try to avoid dead code with the `optiApp` helper which beta-reduces
applications of lambdas where the bound variable appears at most once in the
body, but this is imperfect because of the `Pa` value that may appear during
`Ca` rewrites: we look for the bound variable before rewriting the `Pa` value,
thus our count is wrong if the variable is later eliminated when rewriting the
`Pa` value.
++++++++++
<p><a onclick='hideshow("patty");'>▶ Toggle `patty.hs`</a></p>
<div id='patty' style='display:none'>
++++++++++
------------------------------------------------------------------------
include::patty.hs[]
------------------------------------------------------------------------
++++++++++
</div>
++++++++++
We predefine the `Bool` type, as our next compiler will handle guards, which
translate to expressions involving booleans.
== Guardedly ==
Our last compiler passed an unfortunate milestone: it's over 1000 lines long.
We use language features we just added to shrink the code. At the same time, we
add support for guards.
Before, the right-hand sides of lambdas, equations, and case alternatives were
simply `Ast` values. We change to the type `[(Ast, Ast)]`, that is, a list of
pairs of expressions. During parsing, the guard condition becomes the first
element of a pair, and the corresponding expression is the second element. We
use a list because there can be multiple guards.
We rewrite guards as chains of if-then-else expressions, where the last else
branch is the pattern join point.
Our previous compiler defined `charEq` and `charLE` which we use in this
compiler to define the typeclass instance for `Eq Char`. This prepares for
treating `Int` and `Char` as distinct types in our next compiler.
Doing so will correct a subtle bug. Up until now, a hack treats `Int` and
`Char` as equal during type checking, but it fails to treat them as equals in
dictionaries; for example, `Eq Char` differs to `Eq Int`. We could have fixed
this by treating `Char` as a type synonym for `Int` in the same way `String` is
a type synonym for `[Char]`, but this breaks FFI typing.
++++++++++
<p><a onclick='hideshow("guardedly");'>▶ Toggle `guardedly.hs`</a></p>
<div id='guardedly' style='display:none'>
++++++++++
------------------------------------------------------------------------
include::guardedly.hs[]
------------------------------------------------------------------------
++++++++++
</div>
++++++++++
== Assembly ==
We split off rewriting cases and patterns into a separate function, and change
it from top-down to bottom-up.
We split off and delay address lookup for symbols from bracket abstraction, and
also delay converting literals to combinators as late as possible.
All this slows our compiler and adds more lines of code, but it disentangles
various phases of the pipeline.
The refactoring makes it easy to dump the output of bracket abstraction on the
source code, which is somewhat analogous to a typical compiler printing the
assembly it generates.
We add support for quasiquoted raw strings; see
http://hackage.haskell.org/package/raw-strings-qq/docs/Text-RawString-QQ.html[the
raw-strings-qq package].
++++++++++
<p><a onclick='hideshow("assembly");'>▶ Toggle `assembly.hs`</a></p>
<div id='assembly' style='display:none'>
++++++++++
------------------------------------------------------------------------
include::assembly.hs[]
------------------------------------------------------------------------
++++++++++
</div>
++++++++++