-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
349 lines (242 loc) · 6.86 KB
/
example.py
File metadata and controls
349 lines (242 loc) · 6.86 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
from abc import ABC, abstractmethod
from objalg import algebra_impl, Union
class Expr:
pass
class Eval(ABC):
@abstractmethod
def eval(self):
pass
class Show(ABC):
@abstractmethod
def show(self):
pass
class Pair:
def A(self):
self.a.__class__
def B(self):
self.b.__class__
def __init__(self, a, b):
self.a = a
self.b = b
class LiteralExpr(Expr):
def __init__(self, x):
self.x = x
class AddExpr(Expr):
def __init__(self, lhs, rhs):
self.lhs = lhs
self.rhs = rhs
class BooleanExpr(Expr):
def __init__(self, b):
if not isinstance(b, bool):
raise TypeError(b)
self.b = b
class IfExpr(Expr):
def __init__(self, pred, if_expr, else_expr):
if not isinstance(pred, BooleanExpr):
raise TypeError(pred)
self.pred = pred
self.if_expr = if_expr
self.else_expr = else_expr
class VarExpr(Expr):
def __init__(self, name):
self.name = name
class Stmt:
pass
class AssignStmt(Stmt):
def __init__(self, name, value):
self.name = name
self.value = value
class ExprStmt(Stmt):
def __init__(self, e):
self.e = e
class BlockStmt(Stmt):
def __init__(self, *exprs):
self.exprs = exprs
# ######## #
# ALGEBRAS #
# ######## #
class Algebra(ABC):
@abstractmethod
def T(self):
pass
class IntAlg(Algebra):
@abstractmethod
def literal(self, x):
pass
@abstractmethod
def add(self, lhs, rhs):
pass
class IntBoolAlg(IntAlg):
@abstractmethod
def boolean(self, b):
pass
@abstractmethod
def iff(self, e1, e2):
pass
class StmtAlg(Algebra):
@abstractmethod
def var(self, name):
pass
@abstractmethod
def assign(self, name, value):
pass
@abstractmethod
def expr(self, e):
pass
@abstractmethod
def block(self, *exprs):
pass
# ################# #
# Algebra Instances #
# ################# #
class IntFactory(IntAlg):
def T(self):
return Expr
def literal(self, x):
return LiteralExpr(x)
def add(self, lhs, rhs):
return AddExpr(lhs, rhs)
class IntEval(IntAlg):
'''Eval with manual instance creation.
'''
def T(self):
return Eval
def literal(self, x):
class EvalLiteral(LiteralExpr, Eval):
def eval(self):
return self.x
return EvalLiteral(x)
def add(self, lhs, rhs):
class EvalAdd(AddExpr, Eval):
def eval(self):
return self.lhs.eval() + self.rhs.eval()
return EvalAdd(lhs, rhs)
class IntShow(IntAlg):
'''Show implementation using the decerator to remove boilerplate
'''
def T(self):
return Show
@algebra_impl(LiteralExpr)
def literal(self, this):
return str(this.x)
@algebra_impl(AddExpr)
def add(self, this):
return "%s + %s" % (this.lhs.show(), this.rhs.show())
class IntBoolEval(IntEval):
@algebra_impl(BooleanExpr)
def boolean(self, this):
return bool(this.b)
@algebra_impl(IfExpr)
def iff(self, this):
if this.pred.eval():
return this.if_expr.eval()
else:
return this.else_expr.eval()
class IntBoolShow(IntShow):
@algebra_impl(BooleanExpr)
def boolean(self, this):
return str(this.b)
@algebra_impl(IfExpr)
def iff(self, this):
return "if %s then %s else %s" % \
(this.pred.show(),
this.if_expr.show(),
this.else_expr.show())
class StmtEval(StmtAlg):
def T(self):
return Eval
def __init__(self):
super().__init__()
self.variables = {}
@algebra_impl(VarExpr)
def var(self, this):
return self.variables[this.name]
@algebra_impl(ExprStmt)
def expr(self, this):
return this.e.eval()
@algebra_impl(AssignStmt)
def assign(self, this):
self.variables[this.name] = this.value.eval()
@algebra_impl(BlockStmt)
def block(self, this):
if len(this.exprs) == 0:
return None
elif len(this.exprs) == 1:
return this.exprs[0].eval()
else:
for i in range(len(this.exprs) - 1):
this.exprs[i].eval()
return this.exprs[-1].eval()
class StmtShow(StmtAlg):
def T(self):
return Show
@algebra_impl(VarExpr)
def var(self, this):
return this.name
@algebra_impl(ExprStmt)
def expr(self, this):
return this.e.show()
@algebra_impl(AssignStmt)
def assign(self, this):
return "%s = %s" % (this.name, this.value.show())
@algebra_impl(BlockStmt)
def block(self, this):
return "; ".join(e.show() for e in this.exprs)
class Combine(IntAlg):
def A(self):
return self.alg1.__class__
def B(self):
return self.alg2.__class__
def T(self):
class CombinedPair(Pair):
def A(_):
return self.A()
def B(_):
return self.B()
return CombinedPair
def __init__(self, alg1, alg2):
self.alg1 = alg1
self.alg2 = alg2
def literal(self, x):
return self.T()(self.alg1.literal(x), self.alg2.literal(x))
def add(self, lhs, rhs):
return self.T()(
self.alg1.add(lhs.a, rhs.a),
self.alg2.add(lhs.b, rhs.b),
)
class Debug(Combine):
def __init__(self):
super().__init__(IntEval(), IntShow())
def add(self, lhs, rhs):
print("First expression", lhs.b.show(), "evaluated to", lhs.a.eval())
print("Second expression", rhs.b.show(), "evaluated to", rhs.a.eval())
return super().add(lhs, rhs)
def expr(alg):
return alg.add(alg.literal(1), alg.literal(2))
def expr1(alg):
return alg.add(alg.literal(1), alg.add(alg.literal(2), alg.literal(2)))
def expr2(alg):
return alg.iff(alg.boolean(True), alg.literal(10), alg.literal(20))
def expr3(alg):
return alg.block(
alg.assign('x', expr(alg)),
alg.assign('y', alg.literal(3)),
alg.add(alg.var('x'), alg.var('y')),
)
def main():
eval_alg = IntBoolEval()
stmt_alg = Union(IntBoolEval, StmtEval)()
show_alg = Union(IntBoolShow, StmtShow)()
combine_alg = Combine(IntEval(), IntShow())
debug_alg = Debug()
alg_expr = expr(eval_alg)
assert alg_expr.eval() == 3
assert expr2(eval_alg).eval() == 10
assert expr3(stmt_alg).eval() == 6
print(expr1(combine_alg).b.show(), '=>', expr(combine_alg).a.eval())
print(expr1(debug_alg).b.show(), '=>', expr(debug_alg).a.eval())
print(expr(show_alg).show(), '=>', expr(stmt_alg).eval())
print(expr2(show_alg).show(), '=>', expr2(stmt_alg).eval())
print(expr3(show_alg).show(), '=>', expr3(stmt_alg).eval())
if __name__ == '__main__':
main()