-
Notifications
You must be signed in to change notification settings - Fork 9
/
haskell.ast.js
719 lines (648 loc) · 21.7 KB
/
haskell.ast.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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
(function(ast, interpreter, utilities) {
expectType = utilities.expectType;
expectTypeOf = utilities.expectTypeOf;
expectTypeArray = utilities.expectTypeArray;
/*
data Module = Module VisibleNames [Import] [Declaration]
*/
ast.Module = function(declarations) {
expectTypeArray(declarations, ast.Declaration);
this.declarations = declarations;
};
/*
data VisibleNames = Hiding [NameDef]
| Showing [NameDef]
*/
/*
data NameDef = Name Identifier | ConstructorName Identifer [NameDef] | NoName
// NoName = ...
*/
/*
Import = Import Identifier VisibleNames
| ImportQualified Identifer VisibleNames Identifier
*/
/*
data Type = TypeVariable Identifier
| TypeConstructor Identifier
| TypeApplication Type Type
| TypeTupple Int
*/
ast.Type = function() {};
ast.TypeVariable = function(name) {
expectTypeOf(name, "string");
this.name = name;
};
ast.TypeVariable.prototype = new ast.Type();
ast.TypeVariable.prototype.stringify = function() {
return this.name;
};
ast.TypeConstructor = function(name) {
expectTypeOf(name, "string");
this.name = name;
};
ast.TypeConstructor.prototype = new ast.Type();
ast.TypeConstructor.prototype.stringify = function() {
return this.name;
};
ast.TypeApplication = function(t1, t2) {
expectType(t1, ast.Type);
expectType(t2, ast.Type);
this.t1 = t1;
this.t2 = t2;
};
ast.TypeApplication.prototype = new ast.Type();
ast.TypeApplication.prototype.stringify = function() {
return this.t1.stringify() + " " + this.t2.stringify();
};
ast.TypeTupple = function(size) {
expectTypeOf(size, "number");
this.size = size;
};
ast.TypeTupple.prototype = new ast.Type();
ast.TypeTupple.prototype.stringify = function() {
return "(" + (new Array(this.size)).join(",") + ")";
};
/*
data Constraint = Constraint Identifier Identifier
*/
ast.Constraint = function(typeclass, typevar) {
expectTypeOf(typeclass, "string");
expectTypeOf(typevar, "string");
this.typeclass = typeclass;
this.typevar = typevar;
};
ast.Constraint.prototype.stringify = function() {
return this.typeclass + " " + this.typevar;
};
/*
data TypeConstraint = TypeConstraint [Constraint] Type
*/
ast.TypeConstraint = function(constraints, type) {
expectTypeArray(constraints, ast.Constraint);
expectType(type, ast.Type);
this.constraints = constraints;
this.type = type;
};
ast.Constraint.prototype.stringify = function() {
var constraints = this.constraints.map(function(p) { return p.stringify(); }).join(", ");
if (constraints.length > 0) {
constraints = "(" + constraints + ") => ";
}
return predsString + this.type.stringify();
};
/*
data Expression = Constant Value
| Lambda Pattern Expression
| Application Expression Expression
| Let Pattern Expression Expression
| Case Expression [(Pattern, Expression)]
| VariableLookup Identifier
| ExpressionTypeConstraint Expression TypeConstraint
| If Expression Expression Expression
| Do [DoNotation]
| List [Expression]
| ArithmeticSequence Expression (Maybe Expression) (Maybe Expression)
| ListComprehension Expression [ListNotation]
| Primitive Function
*/
// Eval returns a whnf
ast.Expression = function(){};
ast.Expression.prototype = new Object();
// Either desugar or the other needs overriding in child objects
ast.Expression.prototype.eval = function(env) {
return this.desugar().eval(env);
};
ast.Expression.prototype.stringify = function() {
return this.desugar().stringify();
};
ast.Expression.prototype.desugar = function() {
return undefined;
};
ast.Constant = function(value) {
expectType(value, ast.Value);
this.type ="Constant";
this.value = value;
};
ast.Constant.prototype = new ast.Expression();
ast.Constant.prototype.eval = function(env) {
return this.value.eval(env);
};
ast.Constant.prototype.stringify = function() {
return this.value.stringify();
};
ast.Lambda = function(pattern, expression) {
expectType(pattern, ast.Pattern);
expectType(expression, ast.Expression);
this.type = "Lambda";
this.pattern = pattern;
this.expression = expression;
this.eval = function(env) {
return new interpreter.LambdaAbstraction(env, this.pattern, this.expression);
};
this.stringify = function() {
return "(\\" + this.pattern.stringify() + " -> " + this.expression.stringify() + ")";
};
};
ast.Application = function(func, arg) {
expectType(func, ast.Expression);
expectType(arg, ast.Expression);
this.type = "Application";
this.func = func;
this.arg = arg;
this.eval = function(env) {
var continuation = this.func.eval(env);
while (!(continuation instanceof interpreter.WeakHead)) {
continuation = continuation.force();
};
return continuation.apply(new interpreter.HeapPtr(new interpreter.Closure(env, this.arg)));
};
this.stringify = function() {
return "(" + this.func.stringify() + ") (" + this.arg.stringify() + ")";
};
};
ast.Let = function(declr, expr) {
expectTypeArray(declr, ast.Declaration);
expectType(expr, ast.Expression);
this.type = "Let";
this.declr = declr;
this.expr = expr;
this.eval = function(env) {
var newEnv = interpreter.loadDeclarations(this.declr, env.derive());
return new interpreter.Closure(newEnv, this.expr);
};
this.stringify = function() {
return "let {" + this.declr.map(function (d) {
return d.stringify();
}).join(";") + "} in " + this.expr.stringify();
};
};
ast.Case = function(expr, cases) {
expectType(expr, ast.Expression);
// TODO: Expect cases [(Pattern, Expression)]
this.type = "Case";
this.expr = expr;
this.cases = cases;
this.eval = function(env) {
var expr = new interpreter.HeapPtr(new interpreter.Closure(env, this.expr));
for (var i in this.cases) {
var newEnv = env.derive();
if (this.cases[i][0].match(newEnv, expr)) {
return new interpreter.Closure(newEnv, this.cases[i][1]);
};
};
throw new Error("No matching clause");
};
this.stringify = function() {
return "case " + this.expr.stringify() + " of {" +
this.cases.map(function(c) {
return c[0].stringify() + " -> " + c[1].stringify() + ";";
}).join("") + "}";
};
};
ast.VariableLookup = function(identifier) {
expectTypeOf(identifier, "string");
this.type = "VariableLookup";
this.identifier = identifier;
this.eval = function(env) {
return env.lookup(this.identifier).dereference();
};
this.stringify = function() {
return this.identifier;
};
};
ast.ExpressionTypeConstraint = function(expr, type) {
expectType(expr, ast.Expression);
expectType(type, ast.TypeConstraint);
this.expr = expr;
this.type = type;
this.eval = function(env) {
return this.expr.eval(env);
};
this.stringify = function() {
return this.expr.stringify() + " :: " + this.type.stringify();
};
};
ast.ExpressionTypeConstraint.prototype = new ast.Expression();
ast.If = function(ifExpr, thenExpr, elseExpr) {
expectType(ifExpr, ast.Expression);
expectType(thenExpr, ast.Expression);
expectType(elseExpr, ast.Expression);
this.ifExpr = ifExpr;
this.thenExpr = thenExpr;
this.elseExpr = elseExpr;
this.eval = function(env) {
};
this.stringify = function() {
return "if " + this.ifExpr.stringify() + " then " + this.thenExpr.stringify() + " else " + this.elseExpr.stringify();
};
this.eval = function(env) {
var expr = new interpreter.HeapPtr(new interpreter.Closure(env, this.ifExpr));
var res = expr.dereference();
if (new ast.PatternConstructor("True", []).match(env, expr)) {
return this.thenExpr;
} else {
return this.elseExpr;
}
};
};
ast.If.prototype = new ast.Expression();
ast.Do = function(notations) {
expectTypeArray(notations, ast.DoNotation);
this.type="Do";
this.notations = notations;
};
ast.Do.prototype = new ast.Expression();
ast.Do.prototype.desugar = function() {
var rest = this.notations.concat();
var first = rest.shift();
return (first.partDesugar(rest));
};
ast.List = function(expressions) {
expectTypeArray(expressions, ast.Expression);
this.type="List";
this.expressions = expressions;
};
ast.List.prototype = new ast.Expression();
ast.List.prototype.desugar = function() {
// [] = []
// [1] = 1:[]
// [1,2] = 1:[2]
if (this.expressions.length == 0) {
return new ast.VariableLookup("[]");
};
var first = this.expressions[0];
return new ast.Application(new ast.Application(new ast.VariableLookup(":"),
first),
new ast.List(this.expressions.slice(1))
);
};
ast.ArithmeticSequence = function(e1, e2, e3) {
expectType(e1, ast.Expression);
if (e2) expectType(e2, ast.Expression);
if (e3) expectType(e3, ast.Expression);
this.type="ArithmeticSequence";
this.e1 = e1;
this.e2 = e2;
this.e3 = e3;
};
ast.ArithmeticSequence.prototype = new ast.Expression();
ast.ArithmeticSequence.prototype.desugar = function() {
// [e1..] = enumFrom e1
// [e1,e2..] = enumFromThen e1 e2
// [e1..e3] = enumFromTo e1 e3
// [e1,e2..e3] = enumFromThenTo e1 e2 e3
var funname = 'enumFrom';
if (this.e2) funname = funname + 'Then';
if (this.e3) funname = funname + 'To';
var application = new ast.Application(new ast.VariableLookup(funname), this.e1);
if (this.e2) application = new ast.Application(application, this.e2);
if (this.e3) application = new ast.Application(application, this.e3);
return application;
};
ast.ListComprehension = function(ret, notations) {
expectType(ret, ast.Expression);
expectTypeArray(notations, ast.ListNotation);
this.type="ListComprehensions";
this.ret = ret;
this.notations = notations;
};
ast.ListComprehension.prototype = new ast.Expression();
ast.ListComprehension.prototype.desugar = function() {
if (this.notations.length == 0) {
return (new ast.Application(new ast.Application(new ast.VariableLookup(":"), this.ret), new ast.VariableLookup("[]")));
}
var first = this.notations[0];
return first.partDesugar(new ast.ListComprehension(this.ret,
this.notations.slice(1)));
};
ast.Primitive = function(func) {
expectTypeOf(func, "function");
this.type="Primitive";
this.func = func;
this.eval = function(env) {
return this.func(env);
};
this.stringify = function() {
return "{primitive function}";
};
};
ast.Lambda.prototype = new ast.Expression();
ast.Application.prototype = new ast.Expression();
ast.Let.prototype = new ast.Expression();
ast.Case.prototype = new ast.Expression();
ast.VariableLookup.prototype = new ast.Expression();
ast.Primitive.prototype = new ast.Expression();
/* ast.Constant.prototype.constructor = ast.Constant;
ast.Lambda.prototype.constructor = ast.Lambda;
ast.Application.prototype.constructor = ast.Application;
ast.Let.prototype.constructor = ast.Let;
ast.Case.prototype.constructor = ast.Case;
ast.VariableLookup.prototype.constructor = ast.VariableLookup;
ast.Primitive.prototype.constructor = ast.Primitive; */
/*
data DoNotation = DoLet [Declaration]
| DoBind Pattern Expression
| DoExpr Expression
*/
ast.DoNotation = function(){};
ast.DoLet = function(declrs) {
expectTypeArray(declrs, ast.Declaration);
this.type="DoLet";
this.declrs = declrs;
};
ast.DoLet.prototype = new ast.DoNotation();
ast.DoLet.prototype.partDesugar = function(rest) {
// let declr ; do ==> let declr in do
return new ast.Let(this.declrs, new ast.Do(rest));
};
ast.DoBind = function(pattern, expression) {
expectType(pattern, ast.Pattern);
expectType(expression, ast.Expression);
this.type="DoBind";
this.pattern = pattern;
this.expression = expression;
};
ast.DoBind.prototype = new ast.DoNotation();
ast.DoBind.prototype.partDesugar = function(rest) {
// x <- expr ; do ==> expr >>= (a -> case a of x -> do; _ -> fail undefined)
return new ast.Application(
new ast.Application(
new ast.VariableLookup(">>="),
this.expression
),
new ast.Lambda(new ast.VariableBinding("__todoGenerateunique"),
new ast.Case(new ast.VariableLookup("__todoGenerateunique"),
[[this.pattern, new ast.Do(rest)],
[new ast.Wildcard(),new ast.Application(new ast.VariableLookup("fail"), new ast.VariableLookup("undefined"))]])
)
);
};
ast.DoExpr = function(expr) {
expectType(expr, ast.Expression);
this.type="DoExpr";
this.expr = expr;
};
ast.DoExpr.prototype = new ast.DoNotation();
ast.DoExpr.prototype.partDesugar = function(rest) {
if (rest.length == 0) {
return this.expr;
};
// expr ; do ==> expr >> do
return new ast.Application(new ast.Application(new ast.VariableLookup(">>"), this.expr), new ast.Do(rest));
};
/*
data ListNotation = ListGuard Expression
| ListBind Pattern Expression
| ListLet Pattern Expression
*/
ast.ListNotation = function() {};
ast.ListGuard = function(expr) {
expectType(expr, ast.Expression);
this.type="ListGuard";
this.expr = expr;
};
ast.ListGuard.prototype = new ast.ListNotation();
ast.ListGuard.prototype.partDesugar = function(rest) {
return new ast.Application(new ast.Application(new ast.VariableLookup("concatMap"),
new ast.Lambda(new ast.Wildcard(), rest)),
new ast.Application(new ast.VariableLookup("guard"),
this.expr));
};
ast.ListBind = function(pattern, expr) {
expectType(pattern, ast.Pattern);
expectType(expr, ast.Expression);
this.type="ListBind";
this.pattern = pattern;
this.expr = expr;
};
ast.ListBind.prototype = new ast.ListNotation();
ast.ListBind.prototype.partDesugar = function(rest) {
return new ast.Application(new ast.Application(new ast.VariableLookup("concatMap"),
new ast.Lambda(this.pattern, rest)),
this.expr);
};
ast.ListLet = function(decls) {
expectType(decls, ast.Declaration);
this.type="ListLet";
this.decls = decls
};
ast.ListLet.prototype = new ast.ListNotation();
ast.ListLet.prototype.partDesugar = function() {
// let p = expr ==> let p = expr ...
return new ast.DoLet(this.decls);
};
/*
data Value = Num Int
*/
ast.Value = function() {};
ast.Num = function(num) {
expectTypeOf(num, "number");
this.type = "Num";
this.num = num;
this.match = function(env, n) {
return (new ast.PatternConstructor("I#", [new ast.ConstantPattern(new ast.PrimitiveValue(this.num))])).match(env, n);
};
this.eval = function(env) {
return new interpreter.Data("I#", [new interpreter.HeapPtr(new interpreter.Closure(env, new ast.Constant(new ast.PrimitiveValue(this.num))))]);
};
this.stringify = function() {
return this.num;
};
};
ast.PrimitiveValue = function(value) {
this.type="PrimitiveValue";
this.value = value;
this.eval = function(env) {
return this.value;
};
this.match = function(env, v) {
return this.value == v.dereference();
};
this.stringify = function()
{
return this.value + "#";
};
};
ast.Num.prototype = new ast.Value();
ast.PrimitiveValue.prototype = new ast.Value();
/*
data Declaration = Variable Pattern Expression
| Function Identifier [Pattern] [(Guard, Expression)]|Expression
| Data Identifier [TVar] [Constructor]
| TypeConstraintDeclaration [Identifier] TypeConstraint
*/
ast.Declaration = function(){};
ast.Variable = function(pattern, expression) {
expectType(pattern, ast.Pattern);
expectType(expression, ast.Expression);
this.type = "Variable";
this.pattern = pattern;
this.expression = expression;
this.stringify = function() {
return this.pattern.stringify() + " = " + this.expression.stringify();
};
};
ast.Data = function(identifier, tvars, constructors) {
expectTypeOf(identifier, "string");
expectTypeArray(tvars, ast.TypeVariable);
expectTypeArray(constructors, ast.Constructor);
this.type = "Data";
this.identifier = identifier;
this.tvars = tvars;
this.constructors = constructors;
};
// expression can either be an expression or a list of [guard, expression]
// where guard is an expression of type Boolean.
ast.Function = function(identifier, patterns, expression) {
expectTypeOf(identifier, "string");
expectTypeArray(patterns, ast.Pattern);
// expression can be two different kinds... TODO: fix this?
this.type = "Function";
this.identifier = identifier;
this.patterns = patterns;
this.expression = expression;
this.patternMatch = function(env, givenArgs) {
for (var i in this.patterns) {
if (!this.patterns[i].match(env, givenArgs[i])) {
return false;
}
};
return true;
};
this.stringify = function() {
var exprstr;
if (this.expression instanceof Array) {
exprstr = this.expression.map(function(e) {
return e[0].stringify() + " -> " + e[1].stringify();
}).join("|");
} else {
exprstr = this.expression.stringify();
};
return this.identifier + this.patterns.map(function(p) {
return p.stringify();
}).join(" ") + " = " + exprstr;
};
};
ast.TypeConstraintDeclaration = function(funs, type) {
// expectTypeArrayOf(funs, "string");
expectType(type, ast.TypeConstraint);
this.funs = funs;
this.type = type;
};
ast.TypeConstraintDeclaration.prototype = new ast.Declaration();
ast.Variable.prototype = new ast.Declaration();
ast.Data.prototype = new ast.Declaration();
ast.Function.prototype = new ast.Declaration();
/*
data Constructor = Constructor Identifier [Type]
*/
ast.Constructor = function(identifier, types) {
expectTypeOf(identifier, "string");
expectTypeArray(types, ast.Type);
this.type = "Constructor";
this.identifier = identifier;
this.types = types;
};
/*
Pattern = Constructor Identifier [Pattern]
| VariableBinding Identifier
| Combined Identifier Pattern
| ConstantPattern Value
| Wildcard
*/
ast.Pattern = function(){};
ast.PatternConstructor = function(identifier, patterns) {
expectTypeOf(identifier, "string");
expectTypeArray(patterns, ast.Pattern);
this.type = "PatternConstructor";
this.identifier = identifier;
this.patterns = patterns;
this.match = function(env, expr) {
var weakHead = expr.dereference();
if (this.identifier!=weakHead.identifier) {
return false;
};
for (var i in this.patterns) {
if (!this.patterns[i].match(env, weakHead.ptrs[i])) {
return false;
};
};
return true;
};
this.vars = function() {
var vars=[];
for (var i in this.patterns) {
vars=vars.concat(this.patterns[i].vars());
};
return vars;
};
this.stringify = function() {
return this.identifier + " " + this.patterns.map(function(p) {
return p.stringify();
}).join(" ");
};
};
ast.VariableBinding = function(identifier) {
expectTypeOf(identifier, "string");
this.type = "VariableBinding";
this.identifier = identifier;
this.match = function(env, expr) {
env.bind(this.identifier, expr);
return true;
};
this.vars = function() {
return [this.identifier];
};
this.stringify = function() {
return this.identifier;
};
};
ast.Combined = function(identifier, pattern) {
expectTypeOf(identifier, "string");
expectType(pattern, ast.Pattern);
this.type = "Combined";
this.identifier = identifier;
this.pattern = pattern;
this.match = function(env, expr) {
env.bind(this.identifier, expr);
return this.pattern.match(env, expr);
};
this.vars = function() {
return [this.identifier].concat(this.pattern.vars());
};
this.stringify = function() {
return this.identifier + "@(" + this.pattern.stringify() + ")";
};
};
ast.ConstantPattern = function(value) {
expectType(value, ast.Value);
this.type = "ConstantPattern";
this.value = value;
this.match = function(env, expr) {
return (this.value.match(env, expr));
};
this.vars = function() {
return [];
};
this.stringify = function() {
return this.value.stringify();
};
};
ast.Wildcard = function() {
this.type = "Wildcard";
this.match = function(env, expr) {
return true;
};
this.vars = function() {
return [];
};
this.stringify = function() {
return "_";
};
};
ast.PatternConstructor.prototype = new ast.Pattern();
ast.VariableBinding.prototype = new ast.Pattern();
ast.Combined.prototype = new ast.Pattern();
ast.ConstantPattern.prototype = new ast.Pattern();
ast.Wildcard.prototype = new ast.Pattern();
})(haskell.ast,haskell.interpreter, haskell.utilities);