forked from sampsyo/minisynth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex2.py
203 lines (161 loc) · 5.17 KB
/
ex2.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
import lark
import z3
import sys
# A language based on a Lark example from:
# https://github.com/lark-parser/lark/wiki/Examples
GRAMMAR = """
?start: sum
| sum "?" sum ":" sum -> if
?sum: term
| sum "+" term -> add
| sum "-" term -> sub
?term: item
| term "*" item -> mul
| term "/" item -> div
| term ">>" item -> shr
| term "<<" item -> shl
?item: NUMBER -> num
| "-" item -> neg
| CNAME -> var
| "(" start ")"
%import common.NUMBER
%import common.WS
%import common.CNAME
%ignore WS
""".strip()
def interp(tree, lookup):
"""Evaluate the arithmetic expression.
Pass a tree as a Lark `Tree` object for the parsed expression. For
`lookup`, provide a function for mapping variable names to values.
"""
op = tree.data
if op in ('add', 'sub', 'mul', 'div', 'shl', 'shr'): # Binary operators.
lhs = interp(tree.children[0], lookup)
rhs = interp(tree.children[1], lookup)
if op == 'add':
return lhs + rhs
elif op == 'sub':
return lhs - rhs
elif op == 'mul':
return lhs * rhs
elif op == 'div':
return lhs / rhs
elif op == 'shl':
return lhs << rhs
elif op == 'shr':
return lhs >> rhs
elif op == 'neg': # Negation.
sub = interp(tree.children[0], lookup)
return -sub
elif op == 'num': # Literal number.
return int(tree.children[0])
elif op == 'var': # Variable lookup.
return lookup(tree.children[0])
elif op == 'if': # Conditional.
cond = interp(tree.children[0], lookup)
true = interp(tree.children[1], lookup)
false = interp(tree.children[2], lookup)
return (cond != 0) * true + (cond == 0) * false
def pretty(tree, subst={}, paren=False):
"""Pretty-print a tree, with optional substitutions applied.
If `paren` is true, then loose-binding expressions are
parenthesized. We simplify boolean expressions "on the fly."
"""
# Add parentheses?
if paren:
def par(s):
return '({})'.format(s)
else:
def par(s):
return s
op = tree.data
if op in ('add', 'sub', 'mul', 'div', 'shl', 'shr'):
lhs = pretty(tree.children[0], subst, True)
rhs = pretty(tree.children[1], subst, True)
c = {
'add': '+',
'sub': '-',
'mul': '*',
'div': '/',
'shl': '<<',
'shr': '>>',
}[op]
return par('{} {} {}'.format(lhs, c, rhs))
elif op == 'neg':
sub = pretty(tree.children[0], subst)
return '-{}'.format(sub, True)
elif op == 'num':
return tree.children[0]
elif op == 'var':
name = tree.children[0]
return str(subst.get(name, name))
elif op == 'if':
cond = pretty(tree.children[0], subst)
true = pretty(tree.children[1], subst)
false = pretty(tree.children[2], subst)
return par('{} ? {} : {}'.format(cond, true, false))
def run(tree, env):
"""Ordinary expression evaluation.
`env` is a mapping from variable names to values.
"""
return interp(tree, lambda n: env[n])
def z3_expr(tree, vars=None):
"""Create a Z3 expression from a tree.
Return the Z3 expression and a dict mapping variable names to all
free variables occurring in the expression. All variables are
represented as BitVecs of width 8. Optionally, `vars` can be an
initial set of variables.
"""
vars = dict(vars) if vars else {}
# Lazily construct a mapping from names to variables.
def get_var(name):
if name in vars:
return vars[name]
else:
v = z3.BitVec(name, 8)
vars[name] = v
return v
return interp(tree, get_var), vars
def solve(phi):
"""Solve a Z3 expression, returning the model.
"""
s = z3.Solver()
s.add(phi)
s.check()
return s.model()
def model_values(model):
"""Get the values out of a Z3 model.
"""
return {
d.name(): model[d]
for d in model.decls()
}
def synthesize(tree1, tree2):
"""Given two programs, synthesize the values for holes that make
them equal.
`tree1` has no holes. In `tree2`, every variable beginning with the
letter "h" is considered a hole.
"""
expr1, vars1 = z3_expr(tree1)
expr2, vars2 = z3_expr(tree2, vars1)
# Filter out the variables starting with "h" to get the non-hole
# variables.
plain_vars = {k: v for k, v in vars1.items()
if not k.startswith('h')}
# Formulate the constraint for Z3.
goal = z3.ForAll(
list(plain_vars.values()), # For every valuation of variables...
expr1 == expr2, # ...the two expressions produce equal results.
)
# Solve the constraint.
return solve(goal)
def ex2(source):
src1, src2 = source.strip().split('\n')
parser = lark.Lark(GRAMMAR)
tree1 = parser.parse(src1)
tree2 = parser.parse(src2)
model = synthesize(tree1, tree2)
print(pretty(tree1))
print(pretty(tree2, model_values(model)))
if __name__ == '__main__':
ex2(sys.stdin.read())