-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlambda.nix
212 lines (184 loc) · 5.07 KB
/
lambda.nix
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
#!/usr/bin/env -S nix-instantiate --eval
with builtins;
with (import ./string/strings.nix);
with (import ./string/ascii.nix);
rec {
/*
* "Types"/constructors
*/
# (m n)
mkApply = m: n: { type = "apply"; left = m; right = n; };
# (λx. m)
mkLambda = x: m: { type = "lambda"; bound = x; expr = m; };
# x
mkVar = x: { type = "var"; name = x; };
/*
* Auxiliary
*/
# isPunct could have been in string/ascii.nix
isPunct = c: hasAttr c {
"(" = true;
")" = true;
"." = true;
};
# skipBlacks could have been in string/strings.nix
skipBlacks = s: n: if !isWhite (charAt s n) && !isPunct(charAt s n) && charAt s n != ""
then skipBlacks s (n + 1) else n;
toString = m:
if m.type == "var" then
m.name
else if m.type == "lambda" then
"(λ"+m.bound+"."+" "+(toString m.expr)+")"
else
"("+(toString m.left)+" "+(toString m.right)+")"
;
# prettier toString
pretty = m: let aux = m: inLambda: inApp:
if m.type == "var" then
m.name
else if m.type == "lambda" then
if inLambda then
m.bound+"."+" "+(aux m.expr true false)
else
"(λ"+m.bound+"."+" "+(aux m.expr true false)+")"
else if inApp then
(aux m.left false true)+" "+(aux m.right false false)
else
"("+(aux m.left false true)+" "+(aux m.right false false)+")"
; in aux m false false;
freeVars = m:
if m.type == "var" then { ${m.name} = true; }
else if m.type == "apply" then
(freeVars m.left) // (freeVars m.right)
else
removeAttrs (freeVars m.expr) [m.bound];
allVars = m:
if m.type == "var" then { ${m.name} = true; }
else if m.type == "apply" then
(allVars m.left) // (allVars m.right)
else
{ ${m.bound} = true; } // (allVars m.expr);
# Computes a relatively fresh variable name from a set of
# used names
getFresh = xs:
let aux = n:
if hasAttr "x${builtins.toString n}" xs then
aux (n + 1)
else "x${builtins.toString n}"
; in aux 0;
isFree = m: x: hasAttr x (freeVars m);
/*
* Parsing
*/
start = {
# input buffer
s = "";
# pointer to input buffer
p = 0;
err = null;
# parsed expression
expr = {};
};
atEOF = s: s.p == (stringLength s.s);
hasErr = s: s.err != null;
peek1 = s: charAt s.s (skipWhites s.s s.p);
next1 = s: s // { p = (skipWhites s.s s.p) + 1; };
peekw = s: let q = skipWhites s.s s.p; in
substring q ((skipBlacks s.s q) - q) s.s;
nextw = s: s // {
p = skipBlacks s.s (skipWhites s.s s.p);
};
parseUnary = s:
if atEOF s then s // { err = "unexpected EOF"; }
else if (peek1 s) == "(" then let
t = parseExpr (next1 s);
in if hasErr t then t
else if (peek1 t) != ")" then
t // { err = "expecting ')'"; }
else
next1 t
else let w = peekw s; in
if w == "" then s // { err = "word expected"; }
else nextw s // { expr = mkVar w; }
;
# no utf-8 support; "λ" is two bytes long
hasLambda = s: p: substring (skipWhites s p) 2 s == "λ";
skipLambda = s: next1 (next1 s);
# the lambda is optional, i.e (f. x) <=> (λf. x)
parseLambda = s:
let
b = hasLambda s.s s.p;
t = if b then skipLambda s else s;
in let
w = peekw t;
u = nextw t;
in if peek1 u == "." then let v = parseExpr (next1 u); in
if hasErr v then v
else v // { expr = mkLambda w v.expr; }
else if b then
u // { err = ". expected after λ"; }
else parseUnary s;
hasMore = s:
let
q = skipWhites s.s s.p;
c = charAt s.s q;
in q != stringLength s.s &&
(c == "(" || (hasLambda s.s q) || !(isPunct c));
parseApply = s:
let aux = acc: s: let t = parseLambda s; in
if hasErr t then t
else let
a = if acc == {} then t.expr else mkApply acc t.expr;
in if hasMore t then aux a (t // { expr = {}; })
else t // { expr = a; }
; in aux {} (s // { expr = {}; });
parseExpr = s: parseApply s;
parse = s: parseExpr (start // { s = s; });
/*
* Interpretation/evaluation
*/
# α-renaming, M{y,x} (renaming x as y in M)
rename = m: y: x:
if m.type == "var" then m // {
name = if m.name == x then y else m.name;
} else if m.type == "apply" then m // {
left = rename m.left y x;
right = rename m.right y x;
} else m // {
bound = if m.bound == x then y else m.bound;
expr = rename m.expr y x;
};
# β-substitution, M[N/x] (substituing x for N in M)
substitute = m: n: x:
if m.type == "var" then
if m.name == x then n else m
else if m.type == "apply" then m // {
left = substitute m.left n x;
right = substitute m.right n x;
} else
if m.bound == x then m
else if ! (isFree n m.bound) then m // {
expr = substitute m.expr n x;
} else let
y = getFresh (
(allVars m.expr) //
(allVars n) //
{ ${x} = true; /* ?? "${m.bound}" = true; */ }
);
in m // {
bound = y;
expr = substitute (rename m.expr y m.bound) n x;
};
reduce = m: /* trace(pretty m) */ (
if m.type == "lambda" then
m // { expr = reduce m.expr; }
else if m.type == "var" then
m
else if m.left.type == "lambda" then
substitute m.left.expr m.right m.left.bound
else
m // { left = reduce m.left; right = reduce m.right; }
);
eval = m: let n = reduce m; in
if n == m then n else eval n;
}