-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpolytypic_walk.py
More file actions
54 lines (43 loc) · 1.53 KB
/
polytypic_walk.py
File metadata and controls
54 lines (43 loc) · 1.53 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
"""
Polytypic traversals -- postorder and preorder.
"""
def visit(node, visitor):
return node.accept(lambda x: x, visitor)
def walk_postorder(node, visitor):
def walk(node): return node.accept(walk, visitor)
return walk(node)
def walk_preorder(node, visitor):
# The logical symmetry with walk_postorder here is obscured
# because fmap and visit are generic functions, while accept
# is a method.
def walk(node): return fmap(visit(node, visitor), walk)
return walk(node)
# The name 'fmap' is the usual one in Haskell.
def fmap(node, f):
return node.accept(f, identity_visitor)
# An example type and traversal
class Literal:
def __init__(self, value):
self.value = value
def accept(self, f, visitor):
return visitor.visit_literal(self, self.value)
class BinaryApp:
def __init__(self, operator, left, right):
self.operator = operator
self.left = left
self.right = right
def accept(self, f, visitor):
return visitor.visit_binary_app(self,
self.operator,
f(self.left),
f(self.right))
def evaluate(node):
return walk_postorder(node, EvalVisitor())
class EvalVisitor:
def visit_literal(self, node, value):
return value
def visit_binary_app(self, node, operator, left, right):
assert operator == '+'
return left + right
## evaluate(BinaryApp('+', BinaryApp('+', Literal(4), Literal(2)), Literal(3)))
#. 9