-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransforms.py
432 lines (362 loc) · 13.3 KB
/
transforms.py
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
from CoreParser import PROGRAM, COMBINATOR, APPLICATION, ID, LET, DEFINITION, LAMBDA
from common import Visitor, CompositeVisitor
from collections import defaultdict
from ast import *
class FreeVariables(Visitor):
''' An occurrence of a variable v in an expression e is said to be free in e if the occurrence is not bound by an enclosing lambda or let(rec) expression in e but there is no need to treat
other top-level (combinators) functions as extra parameters. '''
def __init__(self, symtab):
self.symtab = symtab
self.bound = {}
for c in symtab:
self.bound[c] = True
self.vars = []
def variables(self):
return list(set(self.vars))
def visit_CombinatorNode(self, node, **data):
for p in node.parameters():
self.bound[str(p)] = True
self.visit(node.body(), **data)
def visit_LetNode(self, node, **data):
for d in node.definitions():
self.bound[d.name()] = True
self.visit(d.body(), **data)
self.visit(node.body(), **data)
def visit_LetRecNode(self, node, **data):
for d in node.definitions():
self.bound[d.name()] = True
self.visit(d.body(), **data)
self.visit(node.body(), **data)
def visit_LambdaNode(self, node, **data):
for p in node.parameters():
self.bound[str(p)] = True
self.visit(node.body(), **data)
def visit_CaseNode(self, node, **data):
self.visit(node.condition(), **data)
for a in node.alternatives():
self.visit(a, **data)
def visit_AlternativeNode(self, node, **data):
for p in node.parameters():
self.bound[str(p)] = True
self.visit(node.body(), **data)
def visit_IdentifierNode(self, node):
if not str(node) in self.bound:
self.vars.append(str(node))
class Transformer(Visitor):
'base class for all transforming visitors'
def __init__(self):
super(Transformer, self).__init__()
self.names = defaultdict(int)
def fresh(self, stub):
"generate a new fresh name"
self.names[stub] += 1
return "$%s%s" % (stub, self.names[stub])
def token(self, spelling, kind = ID):
token = CommonToken(
type = kind,
text = spelling
)
return token
def id(self, name):
return IdentifierNode(self.token(str(name), ID))
class TransformationScheme(Transformer):
'base class for transformers that are composed of multiple mutually recursive sub-transformers'
def __init__(self, facade):
super(TransformationScheme, self).__init__()
self.facade = facade
def select(self, scheme):
self.facade.select(scheme)
def visit(self, scheme, node, **data):
if scheme != None:
active = self.facade.active
self.select(scheme)
result = self.facade.visit(node, **data)
if scheme != None:
self.facade.active = active
return result
def fallback(self, node, **data):
"define a fallback that is aware of the scheme parameter."
if hasattr(node, 'children'):
# call and collect results for all children
result = []
for child in node.children:
ans = self.visit(None, child, **data)
result.append(ans)
return result
class CaseLifter(CompositeVisitor):
''' Some legal expressions cannot be compiled. They fall into 2 classes: 1. Occurrences of case in non-strict contexts; i.e. in expressions compiled by the C scheme. 2. Occurrences of constructors in expressions where
it is applied to too few arguments. Both problems can be solved by using program transformation techniques. The solution for ECase is to make the offending expressions into supercombinators which are then applied to
their free variables. The trick for constructors is to create a supercombinator for each constructor; this will be generated with enough free variables to saturate the constructor'''
def __init__(self, symtab):
super(CaseLifter, self).__init__()
self.symtab = symtab
self['S'] = CaseLifterS(self)
self['E'] = CaseLifterE(self)
self['C'] = CaseLifterC(self)
self['A'] = CaseLifterA(self)
self.select('S')
class CaseLifterS(TransformationScheme):
def visit_ProgramNode(self, node, **data):
for node in node.combinators():
self.visit('E', node.body(), **data)
class CaseLifterE(TransformationScheme):
def visit_LetNode(self, node, **data):
for definition in node.definitions():
self.visit('E', definition.body(), **data)
self.visit('E', node.body(), **data)
def visit_LetRecNode(self, node, **data):
for definition in node.definitions():
self.visit('E', definition.body(), **data)
self.visit('E', node.body(), **data)
def visit_CaseNode(self, node, **data):
self.visit('E', node.condition(), **data)
for alt in node.alternatives():
self.visit('A', alt, **data)
def visit_ConstructorNode(self, node, **data):
for n in node.expressions():
self.visit('C', n, **data)
def visit_AndNode(self, node, **data):
self.visit('E', node.right(), **data)
self.visit('E', node.left(), **data)
def visit_OrNode(self, node, **data):
self.visit('E', node.right(), **data)
self.visit('E', node.left(), **data)
def visit_AddNode(self, node, **data):
self.visit('E', node.right(), **data)
self.visit('E', node.left(), **data)
def visit_MinNode(self, node, **data):
self.visit('E', node.right(), **data)
self.visit('E', node.left(), **data)
def visit_MulNode(self, node, **data):
self.visit('E', node.right(), **data)
self.visit('E', node.left(), **data)
def visit_DivNode(self, node, **data):
self.visit('E', node.right(), **data)
self.visit('E', node.left(), **data)
def visit_EqualNode(self, node, **data):
self.visit('E', node.right(), **data)
self.visit('E', node.left(), **data)
def visit_NotEqualNode(self, node, **data):
self.visit('E', node.right(), **data)
self.visit('E', node.left(), **data)
def visit_NegateNode(self, node, **data):
self.visit('E', node.left(), **data)
def fallback(self, node, **data):
self.visit('C', node, **data)
class CaseLifterC(TransformationScheme):
def visit_LetNode(self, node, **data):
for definition in node.definitions():
self.visit('C', definition.body(), **data)
self.visit('C', node.body(), **data)
def visit_LetRecNode(self, node, **data):
for definition in node.definitions():
self.visit('C', definition.body(), **data)
self.visit('C', node.body(), **data)
def visit_CaseNode(self, node, **data):
"lift case node in non-strict context to the top level as a new combinator"
parent = node.getParent()
program = node.getAncestor(PROGRAM)
index = parent.children.index(node)
parent.children.remove(node)
name = self.fresh('combinator')
sc = CombinatorNode(self.token("COMBINATOR", COMBINATOR))
sc.addChild(self.id(name))
fv = FreeVariables(self.facade.symtab)
fv.visit(node)
for v in fv.variables():
sc.addChild(self.id(v))
sc.addChild(node)
program.addChild(sc)
vs = fv.variables()
vs.reverse()
ap = ApplicationNode(APPLICATION)
ap.addChild(self.id(name))
ap.addChild(self.id(vs.pop()))
while len(vs) > 0:
a = ApplicationNode(APPLICATION)
a.addChild(ap)
a.addChild(self.id(vs.pop()))
ap = a
parent.children.insert(index, ap)
def visit_ConstructorNode(self, node, **data):
if not node.saturated():
parent = node.getParent()
index = parent.children.index(node)
parent.children.remove(node)
program = node.getAncestor(PROGRAM)
name = self.fresh('combinator')
sc = CombinatorNode(self.token("COMBINATOR", COMBINATOR))
sc.addChild(self.id(name))
fv = FreeVariables(self.facade.symtab)
fv.visit(node)
for v in fv.variables():
sc.addChild(self.id(v))
for i in range(node.arity() - len(node.expressions())):
id = self.id(self.fresh('id'))
sc.addChild(id)
node.children.append(id)
sc.addChild(node)
program.addChild(sc)
if node.arity() > 1:
vs = fv.variables()
vs.reverse()
ap = ApplicationNode(APPLICATION)
ap.addChild(self.id(name))
ap.addChild(self.id(vs.pop()))
while len(vs) > 0:
a = ApplicationNode(APPLICATION)
a.addChild(ap)
a.addChild(self.id(vs.pop()))
ap = a
parent.children.insert(index, ap)
else:
parent.children.insert(index, self.id(name))
else:
for n in node.expressions():
self.visit('C', n, **data)
def visit_ApplicationNode(self, node, **data):
self.visit('C', node.right(), **data)
self.visit('C', node.left(), **data)
def visit_AddNode(self, node, **data):
self.visit('C', node.right(), **data)
self.visit('C', node.left(), **data)
def visit_MinNode(self, node, **data):
self.visit('C', node.right(), **data)
self.visit('C', node.left(), **data)
def visit_MulNode(self, node, **data):
self.visit('C', node.right(), **data)
self.visit('C', node.left(), **data)
def visit_DivNode(self, node, **data):
self.visit('C', node.right(), **data)
self.visit('C', node.left(), **data)
def visit_EqualNode(self, node, **data):
self.visit('C', node.right(), **data)
self.visit('C', node.left(), **data)
def visit_NotEqualNode(self, node, **data):
self.visit('C', node.right(), **data)
self.visit('C', node.left(), **data)
def visit_LessThanNode(self, node, **data):
self.visit('C', node.right(), **data)
self.visit('C', node.left(), **data)
def visit_LessThanEqualNode(self, node, **data):
self.visit('C', node.right(), **data)
self.visit('C', node.left(), **data)
def visit_GreaterThanNode(self, node, **data):
self.visit('C', node.right(), **data)
self.visit('C', node.left(), **data)
def visit_GreaterThanEqualNode(self, node, **data):
self.visit('C', node.right(), **data)
self.visit('C', node.left(), **data)
def visit_IdentifierNode(self, identifier, **kwargs):
pass
def visit_NegateNode(self, node, **data):
self.visit('C', node.left(), **data)
class CaseLifterA(TransformationScheme):
def visit_AlternativeNode(self, node, **data):
self.visit('E', node.body(), **data)
class Renamer(Transformer):
def __init__(self, source, target):
super(Renamer, self).__init__()
self.source = str(source)
self.target = target
def visit_IdentifierNode(self, node):
if str(node) == self.source:
parent = node.getParent()
index = parent.children.index(node)
parent.children.remove(node)
parent.children.insert(index, self.target)
class LambdaSplitter(Transformer):
'split all multi parameter lambdas into nested single parameter lambdas as preparation for the full-laziness transform'
def visit_LambdaNode(self, node, **data):
tree = None
root = None
if len(node.parameters()) > 1:
for p in node.parameters():
tmp = LambdaNode(self.token("LAMBDA", LAMBDA))
tmp.addChild(p)
if tree == None:
root = tmp
tree = tmp
else:
tree.addChild(tmp)
tree = tmp
tree.addChild(node.body())
index = node.getParent().children.index(node)
node.getParent().children[index] = root
class LambdaLifter(Transformer):
'transform the program so that all local function definitions are transformed to functions defined as supercombinators.'
def __init__(self, symtab):
super(LambdaLifter, self).__init__()
self.symtab = symtab
def visit_ProgramNode(self, node, **data):
self.program = node
for node in node.combinators():
self.visit(node, **data)
def visit_CombinatorNode(self, node, **data):
if node.body().__class__ == LambdaNode:
# special case: do not introduce redundant supercombinator definitions by reusing the current combinator
lambda_node = node.body()
index = node.children.index(lambda_node)
node.children.remove(lambda_node)
# move lambda to the top level as combinator
fv = FreeVariables(self.symtab)
fv.visit(lambda_node)
for v in fv.variables():
node.children.insert(1, self.id(v))
for n in lambda_node.parameters():
node.children.insert(1, n)
node.addChild(lambda_node.body())
self.visit(node.body(), **data)
def visit_LetNode(self, node, **data):
for definition in node.definitions():
if definition.body().__class__ == LambdaNode:
# special case to avoid introducing too many useless let definitions
self.visit(definition.body(), **data)
fv = FreeVariables(self.symtab)
fv.visit(node.getAncestor(COMBINATOR))
if len(fv.variables()) == 1 and fv.variables()[0] == definition.name():
# if the name of the definition is the only free variable and we
rn = Renamer(definition.name(), definition.body())
rn.visit(node.body())
parent = node.getParent()
index = parent.children.index(node)
parent.children.remove(node)
parent.children.insert(index, node.body())
else:
self.visit(definition.body(), **data)
self.visit(node.body(), **data)
def visit_LambdaNode(self, node, **data):
parent = node.getParent()
index = parent.children.index(node)
parent.children.remove(node)
# move lambda to the top level as combinator
fv = FreeVariables(self.symtab)
fv.visit(node)
for v in fv.variables():
node.children.insert(0, self.id(v))
# now make the combinator
name = self.fresh('sc')
sc = CombinatorNode(self.token("COMBINATOR", COMBINATOR))
sc.addChild(self.id(name))
self.symtab[name] = sc
for n in node.parameters():
sc.addChild(n)
sc.addChild(node.body())
self.program.addChild(sc)
# replace the original expression
vs = fv.variables()
if len(vs) > 0:
# build application spine
vs.reverse()
ap = ApplicationNode(APPLICATION)
ap.addChild(self.id(name))
ap.addChild(self.id(vs.pop()))
while len(vs) > 0:
a = ApplicationNode(APPLICATION)
a.addChild(ap)
a.addChild(self.id(vs.pop()))
ap = a
parent.children.insert(index, ap)
else:
# if there is only 1 argument, add it as simple identifier
parent.children.insert(index, self.id(name))