/
CatTypeReconstructor.cs
232 lines (200 loc) · 7.64 KB
/
CatTypeReconstructor.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace Cat
{
class CatTypeReconstructor : ConstraintSolver
{
public Constraint KindVarToConstraintVar(CatKind k)
{
Trace.Assert(k.IsKindVar());
return CreateVar(k.ToString().Substring(1));
}
public Vector TypeVectorToConstraintVector(CatTypeVector x)
{
Vector vec = new Vector();
foreach (CatKind k in x.GetKinds())
vec.Insert(0, CatKindToConstraint(k));
return vec;
}
public Relation FxnTypeToRelation(CatFxnType ft)
{
Vector cons = TypeVectorToConstraintVector(ft.GetCons());
Vector prod = TypeVectorToConstraintVector(ft.GetProd());
Relation r = new Relation(cons, prod);
return r;
}
public Constraint CatKindToConstraint(CatKind k)
{
if (k is CatTypeVector)
{
return TypeVectorToConstraintVector(k as CatTypeVector);
}
else if (k is CatFxnType)
{
return FxnTypeToRelation(k as CatFxnType);
}
else if (k.IsKindVar())
{
return KindVarToConstraintVar(k);
}
else
{
return new Constant(k.ToIdString());
}
}
public CatTypeVector CatTypeVectorFromVec(Vector vec)
{
CatTypeVector ret = new CatTypeVector();
foreach (Constraint c in vec)
ret.PushKindBottom(ConstraintToCatKind(c));
return ret;
}
public CatFxnType CatFxnTypeFromRelation(Relation rel)
{
CatTypeVector cons = CatTypeVectorFromVec(rel.GetLeft());
CatTypeVector prod = CatTypeVectorFromVec(rel.GetRight());
// TODO: add the boolean as a third value in the vector.
// it becomes a variable when unknown, and is resolved otherwise.
return new CatFxnType(cons, prod, false);
}
public CatKind ConstraintToCatKind(Constraint c)
{
if (c is ScalarVar)
{
return new CatTypeVar(c.ToString());
}
else if (c is VectorVar)
{
return new CatStackVar(c.ToString());
}
else if (c is Vector)
{
return CatTypeVectorFromVec(c as Vector);
}
else if (c is Relation)
{
return CatFxnTypeFromRelation(c as Relation);
}
else if (c is RecursiveRelation)
{
return new CatRecursiveType();
}
else if (c is Constant)
{
// TODO: deal with CatCustomKinds
return new CatSimpleTypeKind(c.ToString());
}
else
{
throw new Exception("unhandled constraint " + c.ToString());
}
}
public CatFxnType LocalComposeTypes(CatFxnType left, CatFxnType right)
{
// Make sure that the variables on the left function and the variables
// on the right function are different
CatVarRenamer renamer = new CatVarRenamer();
left = renamer.Rename(left.AddImplicitRhoVariables());
renamer.ResetNames();
right = renamer.Rename(right.AddImplicitRhoVariables());
Log("==");
Log("Composing : " + left.ToString());
Log("with : " + right.ToString());
Log("Adding constraints");
Relation rLeft = FxnTypeToRelation(left);
Relation rRight = FxnTypeToRelation(right);
//TODO: remove
//rLeft.UnrollRecursiveRelations();
//rRight.UnrollRecursiveRelations();
Relation result = new Relation(rLeft.GetLeft(), rRight.GetRight());
AddTopLevelConstraints(rLeft.GetRight(), rRight.GetLeft());
AddConstraint(CreateVar("result$"), result);
Log("Constraints");
ComputeConstraintLists();
LogConstraints();
Log("Unifiers");
ComputeUnifiers();
foreach (string sVar in GetConstrainedVars())
{
Constraint u = GetUnifierFor(sVar);
Log("var: " + sVar + " = " + u);
}
Log("Composed Type");
Constraint c = GetResolvedUnifierFor("result$");
if (!(c is Relation))
throw new Exception("Resolved type is not a relation");
// TODO: remove
// Relation r = c as Relation;
// r.RollupRecursiveRelations();
CatKind k = ConstraintToCatKind(c);
CatFxnType ft = k as CatFxnType;
Log("raw type : " + ft.ToString());
Log("pretty type : " + ft.ToPrettyString());
Log("==");
CheckConstraintQueueEmpty();
// Check if the relation was valid, and thus the function type
if (!ft.IsValid())
throw new Exception("invalid function type: " + ft.ToString());
return ft;
}
public static void OutputInferredType(CatFxnType ft)
{
Log("After rewriting");
Log(ft.ToPrettyString());
Log("");
}
public static CatFxnType Infer(CatExpr f)
{
if (!Config.gbTypeChecking)
return null;
if (f.Count == 0)
{
if (Config.gbVerboseInference)
Log("type is ( -> )");
return CatFxnType.Create("( -> )");
}
else if (f.Count == 1)
{
Function x = f[0];
if (Config.gbVerboseInference)
OutputInferredType(x.GetFxnType());
return x.GetFxnType();
}
else
{
Function x = f[0];
CatFxnType ft = x.GetFxnType();
if (Config.gbVerboseInference)
Log("initial term = " + x.GetName() + " : " + x.GetFxnTypeString());
for (int i = 1; i < f.Count; ++i)
{
if (ft == null)
return ft;
Function y = f[i];
if (Config.gbVerboseInference)
{
Log("Composing accumulated terms with next term");
string s = "previous terms = { ";
for (int j = 0; j < i; ++j)
s += f[j].GetName() + " ";
Log(s + "} : " + ft.ToString());
Log("next term = " + y.GetName() + " : " + y.GetFxnTypeString());
}
ft = ComposeTypes(ft, y.GetFxnType());
if (ft == null)
return null;
}
return ft;
}
}
public static CatFxnType ComposeTypes(CatFxnType left, CatFxnType right)
{
if (!Config.gbTypeChecking)
return null;
CatTypeReconstructor inferer = new CatTypeReconstructor();
return inferer.LocalComposeTypes(left, right);
}
}
}