-
Notifications
You must be signed in to change notification settings - Fork 9
/
haskell.interpreter.js
279 lines (255 loc) · 9.26 KB
/
haskell.interpreter.js
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
(function(interpreter, ast, primitives, utilities){
interpreter.loadDeclarations = function(declrs, env) {
var lastfunname = null;
var lastfunenv = [];
// TODO: Remove duplication of Function addition.
for (var i in declrs) {
var decl = declrs[i];
if (decl.type=="Function") {
if (lastfunname == decl.identifier) {
lastfunenv.push(decl);
} else {
if (lastfunenv.length > 0) {
// perhaps the intial HeapPtr shouldn't point to a weakhead, but here it does...
env.bind(lastfunname, new interpreter.HeapPtr(new interpreter.DelayedApplication(env, lastfunenv[0].patterns.length, lastfunenv, [])));
}
lastfunname = decl.identifier;
lastfunenv = [decl];
}
continue;
}
if (lastfunenv.length > 0) {
// perhaps the intial HeapPtr shouldn't point to a weakhead, but here it does...
env.bind(lastfunname, new interpreter.HeapPtr(new interpreter.DelayedApplication(env, lastfunenv[0].patterns.length, lastfunenv, [])));
lastfunname = "";
lastfunenv = [];
}
if (decl.type=="Variable") {
env.patternBind(decl.pattern, new interpreter.HeapPtr(new interpreter.Closure(env, decl.expression)));
}
else if (decl.type=="Data") {
for (var i in decl.constructors) {
constr = decl.constructors[i];
env.bind(constr.identifier, primitives.createDataConstructorKludge(env, constr.identifier, constr.types.length));
}
}
}
if (lastfunenv.length > 0) {
// perhaps the intial HeapPtr shouldn't point to a weakhead, but here it does...
env.bind(lastfunname, new interpreter.HeapPtr(new interpreter.DelayedApplication(env, lastfunenv[0].patterns.length, lastfunenv, [])));
}
return env;
};
// Creates env from an ast and returns it !
interpreter.prepare = function(astt, env) {
return interpreter.loadDeclarations(astt.declarations, env);
};
// Executes a haskell program
interpreter.execute = function(astt) {
var env = new interpreter.RootEnv();
// Only fun defs atm
interpreter.prepare(astt, env);
primitives.init(env);
return env.lookup("main").dereference();
};
// Evaluates an expression under an env
interpreter.eval = function(astt, env) {
return (new interpreter.HeapPtr(new interpreter.Closure(env, astt))).dereference();
};
// Live data
/*
data Env = RootEnv [(Identifier, Pattern, HeapPtr)|(Identifier, HeapPtr)]
| ChildEnv [(Identifier, Pattern, HeapPtr)|(Identifier, HeapPtr)] Env
*/
interpreter.Env = function() {
};
interpreter.Env.prototype.substitute = function(pattern, expression) {
var newEnv = this.derive();
newEnv.patternBind(pattern, expression);
return newEnv;
};
interpreter.Env.prototype.derive = function() {
return new interpreter.ChildEnv(this);
};
interpreter.Env.prototype.patternBind = function(pattern, expression) {
var vars = pattern.vars();
for (var i in vars) {
this.env[vars[i]] = [pattern, expression];
this.env[vars[i]].type = "unforced";
}
};
interpreter.Env.prototype.bind = function(identifier, expr) {
utilities.expectType(expr, interpreter.HeapPtr);
this.env[identifier] = expr;
};
interpreter.Env.prototype.lookup = function(identifier) {
if (this.env[identifier]==undefined) {
return this.onUndefined(identifier);
};
var found = this.env[identifier];
if (found.type == "unforced") {
if (!found[0].match(this, found[1])) {
throw new Error("Unrefutable pattern failed");
};
};
return this.env[identifier];
};
interpreter.Env.prototype.onUndefined = function(identifier) {
throw new Error("Identifier not in env " + identifier );
};
interpreter.RootEnv = function() {
this.env = {};
this.type = "RootEnv";
};
interpreter.RootEnv.prototype = new interpreter.Env();
var childId = 0;
interpreter.ChildEnv = function(parent) {
this.env = {};
this.type = "ChildEnv";
this.parent = parent;
this.id = childId++;
};
interpreter.ChildEnv.prototype = new interpreter.Env();
interpreter.ChildEnv.prototype.onUndefined = function(identifier) {
return this.parent.lookup(identifier);
};
/* data HeapPtr = HeapPtr Thunk WeakHead */
// There's no actuall heap pointer here
// The name is just used to highlight that it's
// use is the same as the heap pointer in a
// standard haskell implementation.
// "dereferencing" a HeapPtr returns the forced
// version of the thunk it points to as well
// as updates the pointer to the whnf.
interpreter.HeapPtr = function(thunk) {
this.thunk = thunk;
this.weakHead = undefined;
// Dereferencing a HeapPtr returns a whnf
this.dereference = function() {
if (this.weakHead == undefined) {
// We'll drive the execution here instead of recursing in the force method
var continuation = this.thunk;
while (continuation instanceof interpreter.Thunk) {
continuation = continuation.force();
}
this.weakHead = continuation;
this.thunk = null;
}
return this.weakHead;
};
this.stringify = function() {
if (this.weakHead == undefined) {
return this.thunk.stringify();
}
if (this.weakHead.stringify == undefined) {
return this.weakHead;
}
return this.weakHead.stringify();
};
};
/*
data Thunk = Closure Env Expression
| ConstantThunk Value
| Data Identifier [HeapPtr]
| Primitive Env Function
| PrimData -- Javascript data
*/
interpreter.Thunk = function() {};
interpreter.Closure = function(env, expression) {
utilities.expectType(env, interpreter.Env);
utilities.expectType(expression, ast.Expression);
this.type = "Closure";
this.env = env;
this.expression = expression;
// force returns the whnf
this.force = function() {
// Forcing a closure is the same as evaluating its expression under the closures env
return this.expression.eval(this.env);
};
this.stringify = function() {
return this.expression.stringify();
};
};
interpreter.ConstantThunk = function(value) {
this.type = "ConstantThunk";
this.value = value;
this.force = function() {
return this;
};
};
interpreter.Closure.prototype = new interpreter.Thunk();
interpreter.ConstantThunk.prototype = new interpreter.Thunk();
/*
data WeakHead = Data Identifier [HeapPtr]
| LambdaAbstraction Env Pattern Expression
| Primitive
*/
interpreter.WeakHead = function() {};
interpreter.Data = function(identifier, ptrs) {
utilities.expectTypeArray(ptrs, interpreter.HeapPtr);
this.type = "Data";
this.identifier = identifier;
this.ptrs = ptrs;
this.getPtrs = function() {
return this.ptrs;
};
this.stringify = function() {
return "(" + this.identifier + " " + this.ptrs.map(function(ptr) {
return ptr.stringify();
}).join(" ") + ")";
};
};
interpreter.Data.prototype = new interpreter.WeakHead();
interpreter.LambdaAbstraction = function(env, pattern, expression)
{
this.type="LambdaAbstraction";
this.env = env;
this.pattern = pattern;
this.expression = expression;
this.apply = function(argument) {
var newEnv = this.env.substitute(this.pattern, argument);
return new interpreter.Closure(newEnv, this.expression);
};
this.stringify = function() {
return "(\\" + this.pattern.stringify() + " -> " + this.expression.stringify() + ")"
};
};
interpreter.LambdaAbstraction.prototype = new interpreter.WeakHead();
// A delayed application represents pattern/guard matches which aren't
// desugered. Eg: f 1 = 2; f 2 = 3;
interpreter.DelayedApplication = function(env, numArgs, funcs, givenArgs) {
this.type="DelayedApplication";
this.env = env;
this.funcs = funcs;
this.numArgs = numArgs;
this.givenArgs = givenArgs;
this.apply = function(argument) {
var givenArgs = this.givenArgs.concat();
givenArgs.push(argument);
if (this.numArgs == 1) {
for (var i in this.funcs) {
var newEnv = this.env.derive();
if (this.funcs[i].patternMatch(newEnv, givenArgs)) {
var matchedFunc = this.funcs[i];
if (matchedFunc.expression instanceof Array) {
for (var j in matchedFunc.expression) {
var guard = matchedFunc.expression[j][0];
var expression = matchedFunc.expression[j][1];
var guardResult = (new interpreter.HeapPtr(new interpreter.Closure(newEnv, guard))).dereference();
if (guardResult.identifier == "True") {
return new interpreter.Closure(newEnv, expression);
}
}
} else {
return new interpreter.Closure(newEnv, matchedFunc.expression);
}
}
}
throw new Error("Failed pattern match");
} else {
return new interpreter.DelayedApplication(this.env, this.numArgs - 1, this.funcs, givenArgs);
}
};
};
interpreter.DelayedApplication.prototype = new interpreter.WeakHead();
})(haskell.interpreter, haskell.ast, haskell.primitives, haskell.utilities);