-
Notifications
You must be signed in to change notification settings - Fork 2
/
Expr.mag
314 lines (272 loc) · 7.82 KB
/
Expr.mag
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
declare type PGGExpr;
declare attributes PGGExpr: free_vars;
declare type PGGExpr_FreeVar: PGGExpr;
declare attributes PGGExpr_FreeVar: name;
declare type PGGExpr_Unop: PGGExpr;
declare attributes PGGExpr_Unop: op, arg1, name, prefix, format;
declare type PGGExpr_Binop: PGGExpr;
declare attributes PGGExpr_Binop: op, arg1, arg2, name, infix, format;
declare type PGGExpr_Seqop: PGGExpr;
declare attributes PGGExpr_Seqop: op, args, name, infix;
declare type PGGExpr_Const: PGGExpr;
declare attributes PGGExpr_Const: val;
declare type PGGExpr_All: PGGExpr;
declare attributes PGGExpr_All: args;
declare type PGGExpr_Any: PGGExpr;
declare attributes PGGExpr_Any: args;
intrinsic Print(x :: PGGExpr_FreeVar)
{Print.}
printf "%o", x`name;
end intrinsic;
INTRNAME := func<f | Type(f) eq Intrinsic select GetIntrinsicName(f) else "<unknown>">;
intrinsic Print(x :: PGGExpr_Unop)
{"}
if assigned x`format then
printf x`format, x`arg1;
elif x`prefix then
printf "(%o %o)", x`name, x`arg1;
else
printf "%o(%o)", x`name, x`arg1;
end if;
end intrinsic;
intrinsic Print(x :: PGGExpr_Binop)
{"}
if assigned x`format then
printf x`format, x`arg1, x`arg2;
elif x`infix then
printf "(%o %o %o)", x`arg1, x`name, x`arg2;
else
printf "%o(%o, %o)", x`name, x`arg1, x`arg2;
end if;
end intrinsic;
intrinsic Print(x :: PGGExpr_Seqop)
{"}
if x`infix and #x`args ge 1 then
if #x`args eq 1 then
Print(x`args[1]);
else
printf "(%o)", Join([Sprint(a) : a in x`args], " " cat x`name cat " ");
end if;
else
printf "%o([%o])", x`name, Join([Sprint(a) : a in x`args], ", ");
end if;
end intrinsic;
intrinsic Print(x :: PGGExpr_Const)
{"}
printf "%o", x`val;
end intrinsic;
intrinsic Print(x :: PGGExpr_All)
{"}
case #x`args:
when 0:
printf "true";
when 1:
Print(x`args[1]);
else
printf "(%o)", Join([Sprint(y) : y in x`args], " and ");
end case;
end intrinsic;
intrinsic Print(x :: PGGExpr_Any)
{"}
case #x`args:
when 0:
printf "false";
when 1:
Print(x`args[1]);
else
printf "(%o)", Join([Sprint(y) : y in x`args], " or ");
end case;
end intrinsic;
intrinsic FreeVariables(x :: PGGExpr) -> {}
{The free variables in the expression.}
if not assigned x`free_vars then
x`free_vars := PowerSet(Strings()) ! _FreeVariables(x);
end if;
return x`free_vars;
end intrinsic;
intrinsic PGG_Expression_FreeVariable(name :: MonStgElt) -> PGGExpr_FreeVar
{The free variable with the given name.}
x := New(PGGExpr_FreeVar);
x`name := name;
return x;
end intrinsic;
intrinsic PGG_Expression_UnOp(op, arg1 :: PGGExpr : Prefix:=false, Name:=false, Format:=false) -> PGGExpr_Unop
{The unary operation op (a function or intrinsic) with argument arg1.}
x := New(PGGExpr_Unop);
x`op := op;
x`arg1 := arg1;
if Format cmpeq false then
x`name := Name cmpeq false select INTRNAME(op) else Name;
x`prefix := Prefix;
else
x`format := Format;
end if;
return x;
end intrinsic;
intrinsic PGG_Expression_BinOp(op, arg1 :: PGGExpr, arg2 :: PGGExpr : Infix:=false, Name:=false, Format:=false) -> PGGExpr_Binop
{The binary operation op (a function or intrinsic) with arguments arg1 and arg2.}
x := New(PGGExpr_Binop);
x`op := op;
x`arg1 := arg1;
x`arg2 := arg2;
if Format cmpeq false then
x`infix := Infix;
x`name := Name cmpeq false select INTRNAME(op) else Name;
else
x`format := Format;
end if;
return x;
end intrinsic;
intrinsic PGG_Expression_SeqOp(op, args : Infix:=false, Name:=false) -> PGGExpr_Seqop
{The operation op with one argument, the sequence args.}
x := New(PGGExpr_Seqop);
x`op := op;
x`args := args;
x`infix := Infix;
x`name := Name cmpeq false select INTRNAME(op) else Name;
return x;
end intrinsic;
intrinsic PGG_Expression_Const(val) -> PGGExpr_Const
{The constant expression val.}
x := New(PGGExpr_Const);
x`val := val;
return x;
end intrinsic;
intrinsic PGG_Expression_All(args) -> PGGExpr_All
{The expression evaluating to true if all its arguments are true.}
x := New(PGGExpr_All);
x`args := args;
return x;
end intrinsic;
intrinsic PGG_Expression_Any(args) -> PGGExpr_Any
{The expression evaluating to true if any of its arguments are true.}
x := New(PGGExpr_Any);
x`args := args;
return x;
end intrinsic;
intrinsic _FreeVariables(x :: PGGExpr_FreeVar) -> {}
{"}
return {x`name};
end intrinsic;
intrinsic _FreeVariables(x :: PGGExpr_Unop) -> {}
{"}
return FreeVariables(x`arg1);
end intrinsic;
intrinsic _FreeVariables(x :: PGGExpr_Binop) -> {}
{"}
return FreeVariables(x`arg1) join FreeVariables(x`arg2);
end intrinsic;
intrinsic _FreeVariables(x :: PGGExpr_Const) -> {}
{"}
return {};
end intrinsic;
intrinsic _FreeVariables(x :: PGGExpr_Seqop) -> {}
{"}
return #x`args eq 0 select {} else &join[FreeVariables(arg) : arg in x`args];
end intrinsic;
intrinsic _FreeVariables(x :: PGGExpr_All) -> {}
{"}
return #x`args eq 0 select {} else &join[FreeVariables(arg) : arg in x`args];
end intrinsic;
intrinsic _FreeVariables(x :: PGGExpr_Any) -> {}
{"}
return #x`args eq 0 select {} else &join[FreeVariables(arg) : arg in x`args];
end intrinsic;
intrinsic Evaluate(x :: PGGExpr, vars :: UserProgram) -> .
{Evaluates x.}
return _Evaluate(x, vars);
end intrinsic;
intrinsic Evaluate(x :: PGGExpr, vars :: Assoc) -> .
{Evaluates x.}
return _Evaluate(x, func<n | vars[n]>);
end intrinsic;
intrinsic EvaluateLazy(x :: PGGExpr, lazy_vars :: Assoc : Recursive:=false) -> ., .
{Evaluates x.}
store := NewStore();
if Recursive then
function vars(name)
ok, x := StoreIsDefined(store, name);
if ok then
return x;
end if;
x := lazy_vars[name](vars);
StoreSet(store, name, x);
return x;
end function;
else
function vars(name)
ok, x := StoreIsDefined(store, name);
if ok then
return x;
end if;
x := lazy_vars[name]();
StoreSet(store, name, x);
return x;
end function;
end if;
return _Evaluate(x, vars), vars;
end intrinsic;
intrinsic EvaluateLazy(x :: PGGExpr, lazy_vars :: UserProgram : Recursive:=false) -> ., .
{Evaluates x.}
store := NewStore();
if Recursive then
function vars(name)
ok, x := StoreIsDefined(store, name);
if ok then
return x;
end if;
x := lazy_vars(name, vars);
StoreSet(store, name, x);
return x;
end function;
else
function vars(name)
ok, x := StoreIsDefined(store, name);
if ok then
return x;
end if;
x := lazy_vars(name);
StoreSet(store, name, x);
return x;
end function;
end if;
return _Evaluate(x, vars), vars;
end intrinsic;
intrinsic _Evaluate(x :: PGGExpr_FreeVar, vars) -> .
{Evaluates x.}
return vars(x`name);
end intrinsic;
intrinsic _Evaluate(x :: PGGExpr_Unop, vars) -> .
{"}
return x`op(Evaluate(x`arg1, vars));
end intrinsic;
intrinsic _Evaluate(x :: PGGExpr_Binop, vars) -> .
{"}
return x`op(Evaluate(x`arg1, vars), Evaluate(x`arg2, vars));
end intrinsic;
intrinsic _Evaluate(x :: PGGExpr_Const, vars) -> .
{"}
return x`val;
end intrinsic;
intrinsic _Evaluate(x :: PGGExpr_Seqop, vars) -> .
{"}
return x`op([Evaluate(arg, vars) : arg in x`args]);
end intrinsic;
intrinsic _Evaluate(x :: PGGExpr_All, vars) -> .
{"}
for arg in x`args do
if not Evaluate(arg, vars) then
return false;
end if;
end for;
return true;
end intrinsic;
intrinsic _Evaluate(x :: PGGExpr_Any, vars) -> .
{"}
for arg in x`args do
if Evaluate(arg, vars) then
return true;
end if;
end for;
return false;
end intrinsic;