-
Notifications
You must be signed in to change notification settings - Fork 6
/
raddsl_rewrite.py
247 lines (182 loc) · 4.71 KB
/
raddsl_rewrite.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
# Author: Peter Sovietov
class Env(dict):
def __getattr__(self, name):
return self[name]
class State:
def __init__(self, out, **attrs):
self.out = out
self.env = Env()
self.__dict__.update(attrs)
def is_atom(term):
return not isinstance(term, (tuple, list))
def match(s, out, pat):
if callable(pat):
out, s.out = s.out, out
res, s.out = pat(s), out
return res
return match_term(s, out, pat)
def match_term(s, out, pat):
if is_atom(pat):
return pat == out
if is_atom(out) or len(out) != len(pat):
return False
for x, y in zip(out, pat):
if not match(s, x, y):
return False
return True
def apply(s, pat):
return pat(s) if callable(pat) else match_term(s, s.out, pat)
def non(pat): return lambda s: not apply(s, pat)
def alt(*args):
def walk(s):
env = s.env
for pat in args:
if apply(s, pat):
return True
s.env = env
return False
return walk
def seq(*args):
def walk(s):
out = s.out
for pat in args:
if not apply(s, pat):
s.out = out
return False
return True
return walk
def let(**kwargs):
name, pat = list(kwargs.items())[0]
def walk(s):
if apply(s, pat):
if name in s.env:
return match(s, s.out, s.env[name])
s.env = Env(s.env)
s.env[name] = s.out
return True
return False
return walk
def rule(*args):
f = seq(*args)
def walk(s):
env = s.env
s.env = Env()
res = f(s)
s.env = env
return res
return walk
def to(f):
def walk(s):
s.out = f(s.env)
return True
return walk
def where(*args):
f = seq(*args)
def walk(s):
old = s.out
res = f(s)
s.out = old
return res
return walk
def build(f):
def walk(s):
s.out = f(s)
return True
return walk
def cons(before, after):
def walk(s):
if isinstance(s.out, list) and s.out:
res = match(s, s.out[:len(before)], before)
return res and match(s, s.out[len(before):], after)
return False
return walk
def rewrite_term(term, n, elem):
elem = (elem,) if isinstance(term, tuple) else [elem]
return term[:n] + elem + term[n + 1:]
def rewrite_rec(s, out, pat):
s.out = out
if callable(pat):
return pat(s)
if is_atom(pat):
return pat == out
if is_atom(out) or len(out) != len(pat):
return False
for i, p in enumerate(pat):
if not rewrite_rec(s, out[i], p):
return False
out = rewrite_term(out, i, s.out)
s.out = out
return True
def rewrite(pat):
def walk(s):
out = s.out
if not rewrite_rec(s, out, pat):
s.out = out
return False
return True
return walk
def to_all(pat):
def walk(s):
out = s.out
res = out
for i, term in enumerate(out):
if not is_atom(term):
s.out = term
if not apply(s, pat):
s.out = out
return False
res = rewrite_term(res, i, s.out)
s.out = res
return True
return walk
def to_one(pat):
def walk(s):
out = s.out
for i, term in enumerate(out):
if not is_atom(term):
s.out = term
if apply(s, pat):
s.out = rewrite_term(out, i, s.out)
return True
s.out = out
return False
return walk
def to_some(pat):
def walk(s):
out = s.out
res = out
ret = False
for i, term in enumerate(out):
if not is_atom(term):
s.out = term
if apply(s, pat):
res = rewrite_term(res, i, s.out)
ret = True
s.out = res
return ret
return walk
def repeat(pat):
def walk(s):
while True:
out = s.out
if not apply(s, pat):
s.out = out
return True
return walk
def Any(s): return True
def env(f): return lambda s: f(s.env)(s)
def opt(*args): return alt(seq(*args), Any)
def delay(f): return lambda s: f()(s)
def guard(f): return lambda s: f(s.env)
def topdown(pat):
f = seq(pat, to_all(delay(lambda: f)))
return f
def bottomup(pat):
f = seq(to_all(delay(lambda: f)), pat)
return f
def downup(pat1, pat2):
f = seq(pat1, to_all(delay(lambda: f)), pat2)
return f
def innermost(pat):
f = bottomup(opt(seq(pat, delay(lambda: f))))
return f