-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
60 lines (53 loc) · 1.74 KB
/
index.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
const {
Var,
App,
Abs,
AbsT,
Let,
Ann,
If,
Lit,
showTerm,
} = require('./terms');
const {
TCon,
TApp,
TFun,
TVar,
TForall,
showTy,
tBool,
} = require('./types');
const { kType, KFun } = require('./kinds');
const { infer } = require('./inference');
const { Env, initialEnv } = require('./env');
function kfun() { return Array.prototype.slice.call(arguments).reduceRight((x, y) => KFun(y, x)) }
const tv = TVar;
function tfun() { return Array.prototype.slice.call(arguments).reduceRight((x, y) => TFun(y, x)) }
function tapp() { return Array.prototype.slice.call(arguments).reduce(TApp) }
const tforall = (tvs, ty) => TForall(tvs, null, ty);
const v = Var;
function app() { return Array.prototype.slice.call(arguments).reduce(App) }
const abs = (ns, body) => ns.reduceRight((x, y) => Array.isArray(y) ? AbsT(y[0], y[1], x) : Abs(y, x), body);
const tid = TForall(['t'], [kType], tfun(tv('t'), tv('t')));
const ListC = TCon('List');
const List = t => tapp(ListC, t);
const env = initialEnv;
env.vars.id = tid;
env.vars.singleton = TForall(['t'], [kType], TFun(tv('t'), List(tv('t'))));
env.vars.refl = TForall(['f', 'a', 'b'], [KFun(kType, kType), kType, kType], TFun(tapp(tv('f'), tv('a')), tapp(tv('f'), tv('b'))));
env.tcons.List = kfun(kType, kType);
const t1 = Ann(abs(['x', 'y', 'z'], v('z')), tfun(tBool, TForall(['a'], [], tfun(tBool, tv('a'), tv('a')))));
const t2 = Ann(abs(['x', 'y', 'z'], v('z')), tfun(tBool, tBool, TForall(['a'], [], tfun(tv('a'), tv('a')))));
const term = If(v('True'), t1, t2);
try {
console.log(showTerm(term));
time = Date.now();
const ty = infer(env, term);
time = Date.now() - time;
console.log(`${time}`);
console.log(showTy(ty));
} catch (err) {
console.log(`${err}`);
console.log(err);
}