-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
lsf.flx
429 lines (362 loc) · 9.29 KB
/
lsf.flx
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
macro val testing = true;
/////////////////////////////////////////////////////////////////////////////////////
// A CRUDE LEXER
chip lex (s:string)
connector io
pin out: %>string
{
var n = s.len.int;
variant state_t = Skip | Grab;
var token = "";
proc emit() {
if token != "" perform write (io.out,token);
token = "";
state = Skip;
}
var state = Skip;
nextch: for ch in s do
match state with
| Skip =>
if ch in "()." do
write (io.out,string ch);
elif ch > char ' ' do
token += ch;
state = Grab;
done
| Grab =>
if ch in "()." do
emit;
write (io.out,string ch);
elif ch <= char ' ' do
emit;
else
token += ch;
done
endmatch;
done
emit;
write (io.out, "End");
}
if testing do
var testlsf =
"lam x. lam y. S F (lam z. F a z) x"
;
proc testlex(s:string) {
proc printstring (s:string) { println$ "Lexeme= " + s; }
#(lex s |-> procedure printstring);
}
testlex(testlsf);
done
/////////////////////////////////////////////////////////////////////////////////////
// TOKEN DEFINITION
variant token_t =
| TVar of string
| TokS
| TokF
| Left
| Right
| TLam
| Dot
| TEnd
;
instance Str[token_t] {
fun str: token_t -> string =
| TVar s => s
| TokS => "S"
| TokF => "F"
| Left => "("
| Right => ")"
| TLam => "Lam"
| Dot => "."
| TEnd => "End"
;
}
/////////////////////////////////////////////////////////////////////////////////////
// TOKENISER
chip tokeniser
connector io
pin inp: %<string
pin out: %>token_t
{
while true do
var x = read io.inp;
write (io.out,
match x with
| "(" => Left
| ")" => Right
| "S" => TokS
| "F" => TokF
| "." => Dot
| "lam" => TLam
| "End" => TEnd
| _ => TVar x
endmatch
);
done
}
if testing do
proc testtok (s:string) {
var counter = 0;
proc printtok (t:token_t) { ++counter; println$ "Token #" + counter.str + "=" + t.str; }
#(lex s |-> tokeniser |-> procedure printtok);
}
testtok (testlsf);
done
/////////////////////////////////////////////////////////////////////////////////////
// TERM DEFINITION
variant term_t =
| Var of string
| Lam of string * term_t
| Node of term_t * term_t
| S
| F
| Error of string
;
instance Str[term_t] {
fun str: term_t -> string =
| Var s => s
| Lam (s,t) => "{lam " + s + "." + t.str+"}"
| Node (t1, t2) => "(" + t1.str + " " +t2.str + ")"
| S => "\\S"
| F => "\\F"
| Error s => "ERROR " + s
;
}
// We can write N M in Felix code to mean Node (N,M)
// but only in expressions, not in patterns unfortunately
fun apply (l:term_t,r:term_t) => Node (l,r);
/////////////////////////////////////////////////////////////////////////////////////
// PARSER
//
// term = | var | lam var . term | term term | S | F | ( term )
// constraint: lam must be preceded by either . or ( or be the first symbol
chip lsfparser
connector io
pin inp: %<token_t
pin out: %>term_t
{
var counter = 0;
var tok : token_t;
proc fetch () { tok = read io.inp; ++counter; }
proc fail () {
write (io.out, Error ("Syntax Error on token #" + counter.str + "=" + tok.str + "\nStack=" + stack.str));
goto terminate;
}
variant elt_t =
| Tok of token_t
| Term of term_t
;
instance Str[elt_t] {
fun str : elt_t -> string =
| Tok t => "Tok " + t.str
| Term t => "Term " + t.str
;
}
var stack = Empty[elt_t];
proc push () { stack = Tok tok ! stack; }
proc maybe_node () {
match stack with
| Term a ! Term b ! tail => stack = Term (Node (b,a)) ! tail;
| _ => ;
endmatch;
}
while true do
maybe_node;
fetch();
match tok with
| TEnd =>
reduce_lam1:>
match stack with
| ([Term term]) => write (io.out, term);
| Term term ! Tok Dot ! Tok (TVar v) ! Tok TLam ! tail =>
stack = Term (Lam (v,term)) ! tail;
maybe_node;
goto reduce_lam1;
| Empty => write (io.out, Error "No input");
| _ => fail;
endmatch;
| Left =>
push;
| Right =>
reduce_lam2:>
match stack with
| Term t ! Tok Left ! tail => stack = Term t ! tail;
| Term term ! Tok Dot ! Tok (TVar v) ! Tok TLam ! tail =>
stack = Term (Lam (v,term)) ! tail;
maybe_node;
goto reduce_lam2;
| _ => fail;
endmatch;
| TokS =>
match stack with
| Term t ! tail => stack = Term (Node (t, S)) ! tail;
| _ => stack = Term S ! stack;
endmatch;
| TokF =>
match stack with
| Term t ! tail => stack = Term (Node (t, F)) ! tail;
| _ => stack = Term F ! stack;
endmatch;
| TLam =>
match stack with
| (Empty | ((Tok Dot | Tok Left) ! _)) => push;
| _ => fail;
endmatch;
| TVar v =>
match stack with
| Tok TLam ! _ => push;
| Term t ! tail => stack = Term (Node (t, Var v)) ! tail;
| _ => stack = Term (Var v) ! stack;
endmatch;
| Dot =>
match stack with
| Tok (TVar v) ! Tok TLam ! tail => push;
| _ => fail;
endmatch;
endmatch;
done
terminate:> ;
}
if testing do
proc testlsfparser (s:string) {
proc printterm (t:term_t) { println$ "Term=" + t.str; }
#(lex s |-> tokeniser |-> lsfparser |-> procedure printterm);
}
var testexpr = "lam x. lam y. S F (lam z. F a z) x";
testlsfparser(testexpr);
done
fun lsfparse (s:string) : term_t = {
var t: term_t;
proc saveterm (x:term_t) { t = x; }
run (lex s |-> tokeniser |-> lsfparser |-> procedure saveterm);
return t;
}
if testing do
println$ "Parsed again = " + (lsfparse testexpr).str;
done
/////////////////////////////////////////////////////////////////////////////////////
// ALPHA CONVERSION
// fresh integer generator for alpha conversion
gen freshint_gen () : int = { for i in 100.. yield i; }
var freshint = freshint_gen;
// alpha conversion
fun alpha (t:term_t) : term_t =>
let fun aux (env:list[string * string]) (t:term_t) : term_t =>
match t with
| Var s' as t =>
match find env s' with
| None => t
| Some x => Var x
endmatch
| Lam (s', t') =>
let r = "_" + #freshint.str in
Lam (r, aux ((s',r) ! env) t')
| Node (a,b) => Node (aux env a, aux env b)
| x => x // S or F
in aux Empty[string * string] t
;
/////////////////////////////////////////////////////////////////////////////////////
// BETA REDUCTION
fun beta (s: string, bdy: term_t, r: term_t) : term_t =>
let b = alpha bdy in
let fun aux (t:term_t) =>
match t with
| Var v when v == s => r
| Lam (s,t) => Lam (s, aux t)
| Node (a,b) => Node (aux a, aux b)
| x => x
endmatch
in aux b
;
if testing do
proc checkbeta (s:string, b: term_t, r: term_t) {
var a = Node (Lam (s,b),r);
println$ "To reduce = " + a.str;
println$ "Reduced = " + (beta (s,b,r)).str;
}
var testexpr2 = "x y";
var testexpr3 = "z";
checkbeta ("x", lsfparse testexpr2, lsfparse testexpr3);
done
/////////////////////////////////////////////////////////////////////////////////////
// DEFINE ACTIVE VARIABLE
fun active: term_t -> opt[string] =
| Var x => Some x
| S => None[string]
| F => None[string]
| Lam (s,t) =>
match active t with // active t - {s}
| Some s' as a => if s == s' then None[string] else a
| None => None[string]
endmatch
| Node ((S|F),m) => None[string]
| Node (Node ((S|F),m),n) => None[string]
| Node (Node(Node(S,m),n),p) => None[string]
| Node (Node(Node(F,m),n),p) => active m
| Node (m,n) => active m
;
fun has_active (t:term_t): bool =>
match active t with
| Some _ => true
| None => false
;
/////////////////////////////////////////////////////////////////////////////////////
// DEFINE STAR REDUCTION
//
// Eliminates lambda terms
var K = F F;
var I = S K K;
var abs_left = S K F;
fun star (s:string, t:term_t) => match t with
| Var x when x == s => I
| Var y => K (Var y)
| (S|F) as O => K O
| Lam (y,m) => Lam (s, star (y,m))
| Node (m,n) => S (Lam(s,m)) (Lam(s,n))
;
if testing do
var teststar = "lam x. lam y. y";
println$ lsfparse teststar;
println$ star("x",lsfparse "lam y.y");
done
/////////////////////////////////////////////////////////////////////////////////////
// DEFINE LEFT AND RIGHT COMPONENTS
fun left : term_t -> term_t =
| Node (m,n) => m
| _ => S K F
;
fun right : term_t -> term_t =
| Node (m,n) => n
| Lam (s,t) => star (s,t)
| m => m
;
fun split (t:term_t) : term_t => t.left t.right;
/////////////////////////////////////////////////////////////////////////////////////
// DEFINE NORMAL FORM
fun normal: term_t -> bool =
| (S|F|Var _) => true
| Lam (_,t) => normal t
| Node (m,n) as t =>
normal m and normal n and (compound t or has_active t)
;
/////////////////////////////////////////////////////////////////////////////////////
// DEFINE COMPOUND
fun compound : term_t -> bool =
| (S|F) => false
| _ => true
;
/////////////////////////////////////////////////////////////////////////////////////
// REDUCTION
fun sfreduce: term_t -> term_t =
| Node (f,x) =>
match f,x with
| Lam (s,f), x => beta (s,f,x)
| S,Node (m,Node (n,p)) => (m n) (n p)
| F,Node(Node (p,m),n) =>
match p with
| (S|F) => m
| _ => n p.split
endmatch
endmatch
| x => x
;