-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSub.lam
93 lines (74 loc) · 2.38 KB
/
Sub.lam
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
module Sub
import Lambda
import LambdaCore
import LamTranslator
import Encode
-- LAMSUB
-- lambda calculus equivalent of sub
lamsub = #y #lamsubrec;
lamsubrec = Lf.Lm.Lvar.Lterm. #lamguard
(#pr term (#pr var (#pr m (#pr f #nil))))
(#pr (#pr #matchvar #varaction) (#pr (#pr #matchapp #appaction) (#pr (#pr #matchlam #lamaction) #nil)));
-- GUARDS
-- general case statement which allows for a boolean expression to
-- be in the pattern expression
-- applies subject (i) to the callback in the sequence (ps)
lamguard = #y (Lf.Li.Lps.((#frst (#car ps)) i)
((#scnd (#car ps)) i)
(f i (#cdr ps)));
-- ENCODING INFORMATION
-- Lam = 1
-- App = 2
-- Var = 3
-- Variable names in the term are labelled starting with the
-- number 0
-- EXTRACTORS
-- extracts a value from an encoded "constructor"
-- extracts x of (Var x)
extractv = #scnd;
-- extracts x of (Lam x e)
extractlname = Llam. #frst (#scnd lam);
-- extracts e of (Lam x e)
extractlexp = Llam. #scnd (#scnd lam);
-- extracts e1 of (App e1 e2)
extractaone = Lapp. #frst (#scnd app);
-- extracts e2 of (App e1 e2)
extractatwo = Lapp. #scnd (#scnd app);
-- MATCHERS
-- matching functions expect an encoded term
-- each returns a boolean
matchvar = Lterms. #eq #three (#car (#car terms));
matchlam = Lterms. #eq #one (#car (#car terms));
matchapp = Lterms. #eq #two (#car (#car terms));
-- ACTIONS
-- actions expect a list of the form [term, (Var x), m, f]
-- varaction correlates to
-- sub m (Var x) (Var v)
-- | x == v = m
-- | x /= v = (Var v)
varaction = Llist. (#eq (#extractv (#car (#cdr list))) (#extractv (#car list)))
(#car (#cdr (#cdr list)))
(#car list);
-- lamaction correlates to
-- sub m (Var x) (Lam v e)
-- | x == v = (Lam v e)
-- | otherwise = (Lam v (sub m (Var x) e))
lamaction = Llist. (#eq (#extractv (#car (#cdr list))) (#extractlname (#car list))
(#car list)
(#pr #one
(#pr (#extractlname (#car list))
((#car (#cdr (#cdr (#cdr list))))
(#car (#cdr (#cdr list)))
(#car (#cdr list))
(#extractlexp (#car list))))));
-- appaction correlates to
-- (App (sub m (Var x) e1) (sub m (Var x) e2))
appaction = Llist. #pr #two
(#pr ((#car (#cdr (#cdr (#cdr list))))
(#car (#cdr (#cdr list)))
(#car (#cdr list))
(#extractaone (#car list)))
((#car (#cdr (#cdr (#cdr list))))
(#car (#cdr (#cdr list)))
(#car (#cdr list))
(#extractatwo (#car list))));