-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtypecheckertests.js
147 lines (137 loc) · 3.19 KB
/
typecheckertests.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
var ast = haskell.ast;
var typechecker = haskell.typechecker;
var astt = new ast.Module(
[
new ast.Variable(
new ast.VariableBinding("inc"),
new ast.Lambda(
new ast.VariableBinding("x"),
new ast.Application(
new ast.Application(
new ast.VariableLookup("+"),
new ast.VariableLookup("x")),
new ast.Constant(new ast.Num(1))))),
new ast.Variable(
new ast.VariableBinding("inc2"),
new ast.Lambda(
new ast.VariableBinding("x"),
new ast.Application(
new ast.VariableLookup("inc"),
new ast.Application(
new ast.VariableLookup("inc"),
new ast.VariableLookup("x"))))),
new ast.Variable(
new ast.VariableBinding("main"),
new ast.Application(
new ast.VariableLookup("alert"),
new ast.Application(
new ast.VariableLookup("inc2"),
new ast.Constant(new ast.Num(2)))))
]);
var asttt = new ast.Application(
new ast.Application(
new ast.VariableLookup("+"),
new ast.VariableLookup("x")),
new ast.Constant(new ast.Num(1))); // ((x + :: (Num -> Num)) 1 :: Num)
/*
* Kinds
*
*/
(function() {
fireunit.compare(
new typechecker.Star().toString(),
"*",
"Star is *");
fireunit.compare(
new typechecker.Kfun(
new typechecker.Star(),
new typechecker.Star()).toString(),
"*->*",
"Kfun Star Star is *->*");
fireunit.compare(
new typechecker.Kfun(
new typechecker.Kfun(
new typechecker.Star(),
new typechecker.Star()),
new typechecker.Star()).toString(),
"(*->*)->*",
"Kfun Kfun Star Star Star is (*->*)->*");
fireunit.compare(
new typechecker.Kfun(
new typechecker.Star(),
new typechecker.Kfun(
new typechecker.Star(),
new typechecker.Star())).toString(),
"*->(*->*)",
"Kfun Star Kfun Star Star is *->(*->*)");
}) ();
/*
* Num
*
*/
(function() {
fireunit.compare(
new ast.Num(1).infer(typechecker.emptyEnv()).toString(),
"Num a1",
"1 is Num a1"
);
fireunit.compare(
new ast.Num(1).infer(typechecker.emptyEnv()).class(),
"Num",
"1 is in Num");
fireunit.compare(
new ast.Num(1).infer(typechecker.emptyEnv()).type().id(),
"a1",
"1 is a typevariable named a1");
}) ();
/*
* VariableLookup
* For example, using
* the Qual and Pred datatypes, the type (Num a) => a -> Int
* can be represented by:
* [IsIn "Num" (TVar (Tyvar "a" Star))] :=> (TVar (Tyvar "a" Star ) `fn` tInt)
*
*/
(function() {
fireunit.compare(
new ast.VariableLookup("x").infer(
new typechecker.Environment(
{x: new typechecker.Scheme(
[new typechecker.Pred(
"Num",
new typechecker.TVar("a3", new typechecker.Star()))],
new typechecker.TVar("a3", new typechecker.Star()))})).toString(),
"Num a3",
"x is Num a3");
})();
/*
* Const
*
*/
(function() {
})();
/*
* Type schemes
*
*/
(function() {
}) ();
/*
* NameGen
*
*/
(function() {
fireunit.compare(
new typechecker.NameGen(2).next({}),
"a2",
"should generate next free var");
fireunit.compare(
new typechecker.NameGen(2).next({"a2":true}),
"a3",
"should get first free varname");
fireunit.compare(
typechecker.emptyEnv().nextName(),
"a1",
"an environment has an associated name generator");
}) ();
fireunit.testDone();