yukigo-haskell-parser 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.mocharc.json +4 -0
- package/CHANGELOG.md +19 -0
- package/README.md +10 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +54 -0
- package/dist/index.js.map +1 -0
- package/dist/parser/grammar.d.ts +28 -0
- package/dist/parser/grammar.js +404 -0
- package/dist/parser/grammar.js.map +1 -0
- package/dist/parser/layoutPreprocessor.d.ts +1 -0
- package/dist/parser/layoutPreprocessor.js +22 -0
- package/dist/parser/layoutPreprocessor.js.map +1 -0
- package/dist/parser/lexer.d.ts +42 -0
- package/dist/parser/lexer.js +75 -0
- package/dist/parser/lexer.js.map +1 -0
- package/dist/parser/preprocessor.d.ts +28 -0
- package/dist/parser/preprocessor.js +453 -0
- package/dist/parser/preprocessor.js.map +1 -0
- package/dist/prelude.d.ts +1 -0
- package/dist/prelude.js +205 -0
- package/dist/prelude.js.map +1 -0
- package/dist/typechecker/DeclarationCollector.d.ts +15 -0
- package/dist/typechecker/DeclarationCollector.js +80 -0
- package/dist/typechecker/DeclarationCollector.js.map +1 -0
- package/dist/typechecker/TypeBuilder.d.ts +12 -0
- package/dist/typechecker/TypeBuilder.js +113 -0
- package/dist/typechecker/TypeBuilder.js.map +1 -0
- package/dist/typechecker/checker.d.ts +80 -0
- package/dist/typechecker/checker.js +269 -0
- package/dist/typechecker/checker.js.map +1 -0
- package/dist/typechecker/core.d.ts +14 -0
- package/dist/typechecker/core.js +142 -0
- package/dist/typechecker/core.js.map +1 -0
- package/dist/typechecker/inference.d.ts +69 -0
- package/dist/typechecker/inference.js +984 -0
- package/dist/typechecker/inference.js.map +1 -0
- package/dist/utils/helpers.d.ts +2 -0
- package/dist/utils/helpers.js +25 -0
- package/dist/utils/helpers.js.map +1 -0
- package/dist/utils/types.d.ts +6 -0
- package/dist/utils/types.js +53 -0
- package/dist/utils/types.js.map +1 -0
- package/package.json +36 -0
- package/src/index.ts +55 -0
- package/src/parser/grammar.ne +463 -0
- package/src/parser/grammar.ts +512 -0
- package/src/parser/layoutPreprocessor.ts +21 -0
- package/src/parser/lexer.ts +77 -0
- package/src/parser/preprocessor.ne +375 -0
- package/src/parser/preprocessor.ts +502 -0
- package/src/prelude.ts +206 -0
- package/src/typechecker/DeclarationCollector.ts +104 -0
- package/src/typechecker/TypeBuilder.ts +148 -0
- package/src/typechecker/checker.ts +395 -0
- package/src/typechecker/core.ts +162 -0
- package/src/typechecker/inference.ts +1327 -0
- package/src/utils/helpers.ts +29 -0
- package/src/utils/types.ts +56 -0
- package/tests/parser.spec.ts +823 -0
- package/tests/prelude.spec.ts +22 -0
- package/tests/preprocessor.spec.ts +69 -0
- package/tests/typechecker.spec.ts +310 -0
- package/tsconfig.json +17 -0
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AST,
|
|
3
|
+
Function,
|
|
4
|
+
UnguardedBody,
|
|
5
|
+
GuardedBody,
|
|
6
|
+
Visitor,
|
|
7
|
+
Return,
|
|
8
|
+
} from "yukigo-ast";
|
|
9
|
+
import { typeMappings } from "../utils/types.js";
|
|
10
|
+
import { InferenceEngine, PatternVisitor } from "./inference.js";
|
|
11
|
+
import { CoreHM } from "./core.js";
|
|
12
|
+
import { inspect } from "util";
|
|
13
|
+
import { DeclarationCollectorVisitor } from "./DeclarationCollector.js";
|
|
14
|
+
|
|
15
|
+
export interface TypeVar {
|
|
16
|
+
type: "TypeVar";
|
|
17
|
+
id: number;
|
|
18
|
+
name?: string;
|
|
19
|
+
constraints: string[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface TypeConstructor {
|
|
23
|
+
type: "TypeConstructor";
|
|
24
|
+
name: string;
|
|
25
|
+
args: Type[];
|
|
26
|
+
}
|
|
27
|
+
export interface FunctionType {
|
|
28
|
+
type: "TypeConstructor";
|
|
29
|
+
name: "->";
|
|
30
|
+
args: [Type, Type];
|
|
31
|
+
}
|
|
32
|
+
export interface ListType {
|
|
33
|
+
type: "TypeConstructor";
|
|
34
|
+
name: "List";
|
|
35
|
+
args: [Type];
|
|
36
|
+
}
|
|
37
|
+
export interface TupleType {
|
|
38
|
+
type: "TypeConstructor";
|
|
39
|
+
name: "Tuple";
|
|
40
|
+
args: Type[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface TypeScheme {
|
|
44
|
+
type: "TypeScheme";
|
|
45
|
+
quantifiers: number[];
|
|
46
|
+
body: Type;
|
|
47
|
+
constraints: Map<number, string[]>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type Type = TypeVar | TypeConstructor;
|
|
51
|
+
|
|
52
|
+
export type Environment = Map<string, TypeScheme>;
|
|
53
|
+
|
|
54
|
+
export type Substitution = Map<number, Type>;
|
|
55
|
+
|
|
56
|
+
export type Result<T> =
|
|
57
|
+
| { success: true; value: T }
|
|
58
|
+
| { success: false; error: string };
|
|
59
|
+
|
|
60
|
+
export const booleanType: TypeConstructor = {
|
|
61
|
+
type: "TypeConstructor",
|
|
62
|
+
name: "YuBoolean",
|
|
63
|
+
args: [],
|
|
64
|
+
};
|
|
65
|
+
export const numberType: TypeConstructor = {
|
|
66
|
+
type: "TypeConstructor",
|
|
67
|
+
name: "YuNumber",
|
|
68
|
+
args: [],
|
|
69
|
+
};
|
|
70
|
+
export const stringType: TypeConstructor = {
|
|
71
|
+
type: "TypeConstructor",
|
|
72
|
+
name: "YuString",
|
|
73
|
+
args: [],
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export class FunctionRegistrarVisitor implements Visitor<void> {
|
|
77
|
+
constructor(
|
|
78
|
+
private env: Environment,
|
|
79
|
+
private signatureMap: Map<string, TypeScheme>,
|
|
80
|
+
private coreHM: CoreHM
|
|
81
|
+
) {}
|
|
82
|
+
visitFunction(node: Function): void {
|
|
83
|
+
const functionName = node.identifier.value;
|
|
84
|
+
let funcScheme = this.signatureMap.get(functionName);
|
|
85
|
+
if (!funcScheme) {
|
|
86
|
+
const funcTypeVar = this.coreHM.freshVar();
|
|
87
|
+
funcScheme = {
|
|
88
|
+
type: "TypeScheme",
|
|
89
|
+
quantifiers: [],
|
|
90
|
+
body: funcTypeVar,
|
|
91
|
+
constraints: new Map(),
|
|
92
|
+
};
|
|
93
|
+
this.env.set(functionName, funcScheme);
|
|
94
|
+
}
|
|
95
|
+
for (const equation of node.equations) {
|
|
96
|
+
if (isUnguardedBody(equation.body)) {
|
|
97
|
+
const { statements } = equation.body.sequence;
|
|
98
|
+
statements
|
|
99
|
+
.filter((stmt) => stmt instanceof Function)
|
|
100
|
+
.forEach((func) => {
|
|
101
|
+
this.env.set(func.identifier.value, funcScheme);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
export class FunctionCheckerVisitor implements Visitor<void> {
|
|
108
|
+
constructor(
|
|
109
|
+
private environments: Environment[],
|
|
110
|
+
private signatureMap: Map<string, TypeScheme>,
|
|
111
|
+
private coreHM: CoreHM,
|
|
112
|
+
private errors: string[]
|
|
113
|
+
) {}
|
|
114
|
+
visitFunction(node: Function): void {
|
|
115
|
+
const functionName = node.identifier.value;
|
|
116
|
+
let funcScheme = this.signatureMap.get(functionName);
|
|
117
|
+
// Handle function without signature
|
|
118
|
+
if (!funcScheme) {
|
|
119
|
+
const firstEq = node.equations[0];
|
|
120
|
+
this.environments.unshift(new Map());
|
|
121
|
+
const inferenceEngine = new InferenceEngine(
|
|
122
|
+
this.signatureMap,
|
|
123
|
+
this.coreHM,
|
|
124
|
+
this.environments
|
|
125
|
+
);
|
|
126
|
+
const paramTypes = firstEq.patterns.map(() => this.coreHM.freshVar());
|
|
127
|
+
|
|
128
|
+
paramTypes.forEach((type, i) => {
|
|
129
|
+
try {
|
|
130
|
+
new PatternVisitor(
|
|
131
|
+
this.coreHM,
|
|
132
|
+
this.signatureMap,
|
|
133
|
+
type,
|
|
134
|
+
this.environments,
|
|
135
|
+
inferenceEngine
|
|
136
|
+
).visit(firstEq.patterns[i]);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
this.errors.push(`Type error in '${functionName}': ${error.message}`);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
let inferredBodyType: Type;
|
|
143
|
+
if (isUnguardedBody(firstEq.body)) {
|
|
144
|
+
const equationStatements = firstEq.body.sequence.statements;
|
|
145
|
+
equationStatements.forEach((stmt) =>
|
|
146
|
+
stmt.accept(
|
|
147
|
+
new FunctionCheckerVisitor(
|
|
148
|
+
this.environments,
|
|
149
|
+
this.signatureMap,
|
|
150
|
+
this.coreHM,
|
|
151
|
+
this.errors
|
|
152
|
+
)
|
|
153
|
+
)
|
|
154
|
+
);
|
|
155
|
+
const returnResult = equationStatements
|
|
156
|
+
.find((stmt) => stmt instanceof Return)
|
|
157
|
+
.accept(inferenceEngine);
|
|
158
|
+
if (returnResult.success === false) {
|
|
159
|
+
this.errors.push(
|
|
160
|
+
`Type error in '${functionName}': ${returnResult.error}`
|
|
161
|
+
);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
inferredBodyType = returnResult.value;
|
|
165
|
+
} else {
|
|
166
|
+
// Handle guarded body inference if necessary
|
|
167
|
+
inferredBodyType = this.coreHM.freshVar(); // Placeholder
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const fullFuncType = paramTypes.reduceRight(
|
|
171
|
+
(acc, param) => functionType(param, acc),
|
|
172
|
+
inferredBodyType
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
// Generalize the inferred type to create a polymorphic type scheme
|
|
176
|
+
this.environments.shift();
|
|
177
|
+
funcScheme = this.coreHM.generalize(this.environments[0], fullFuncType);
|
|
178
|
+
this.signatureMap.set(functionName, funcScheme);
|
|
179
|
+
this.environments[0].set(functionName, funcScheme);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const expectedArity = getArity(this.coreHM.instantiate(funcScheme));
|
|
183
|
+
for (const [index, equation] of node.equations.entries()) {
|
|
184
|
+
if (equation.patterns.length !== expectedArity) {
|
|
185
|
+
this.errors.push(`Type error in '${functionName}': Arity mismatch in equation number ${index}`);
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
try {
|
|
189
|
+
const funcType = this.coreHM.instantiate(funcScheme);
|
|
190
|
+
|
|
191
|
+
const returnType = getReturnType(funcType);
|
|
192
|
+
const patternTypes = getArgumentTypes(funcType);
|
|
193
|
+
|
|
194
|
+
this.environments.unshift(new Map());
|
|
195
|
+
const inferenceEngine = new InferenceEngine(
|
|
196
|
+
this.signatureMap,
|
|
197
|
+
this.coreHM,
|
|
198
|
+
this.environments
|
|
199
|
+
);
|
|
200
|
+
patternTypes.forEach((argType, i) => {
|
|
201
|
+
try {
|
|
202
|
+
equation.patterns[i].accept(
|
|
203
|
+
new PatternVisitor(
|
|
204
|
+
this.coreHM,
|
|
205
|
+
this.signatureMap,
|
|
206
|
+
argType,
|
|
207
|
+
this.environments,
|
|
208
|
+
inferenceEngine
|
|
209
|
+
)
|
|
210
|
+
);
|
|
211
|
+
} catch (error) {
|
|
212
|
+
this.errors.push(
|
|
213
|
+
`Type error in '${functionName}': ${error.message}`
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
if (isUnguardedBody(equation.body)) {
|
|
218
|
+
const equationStatements = equation.body.sequence.statements;
|
|
219
|
+
equationStatements.forEach((stmt) =>
|
|
220
|
+
stmt.accept(
|
|
221
|
+
new FunctionCheckerVisitor(
|
|
222
|
+
this.environments,
|
|
223
|
+
this.signatureMap,
|
|
224
|
+
this.coreHM,
|
|
225
|
+
this.errors
|
|
226
|
+
)
|
|
227
|
+
)
|
|
228
|
+
);
|
|
229
|
+
const returnResult = equationStatements
|
|
230
|
+
.find((stmt) => stmt instanceof Return)
|
|
231
|
+
.accept(inferenceEngine);
|
|
232
|
+
if (returnResult.success === false) {
|
|
233
|
+
this.errors.push(
|
|
234
|
+
`Type error in '${functionName}': ${returnResult.error}`
|
|
235
|
+
);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const sub = this.coreHM.unify(returnResult.value, returnType);
|
|
240
|
+
if (sub.success === false) throw Error(sub.error);
|
|
241
|
+
} else {
|
|
242
|
+
// Handles GuardedBody case
|
|
243
|
+
for (const guard of equation.body) {
|
|
244
|
+
// checks if condition expression in guard is a resolves to YuBoolean
|
|
245
|
+
|
|
246
|
+
const condition = guard.condition.accept(inferenceEngine);
|
|
247
|
+
if (condition.success === false) throw Error(condition.error);
|
|
248
|
+
|
|
249
|
+
const conditionSub = this.coreHM.unify(
|
|
250
|
+
condition.value,
|
|
251
|
+
booleanType
|
|
252
|
+
);
|
|
253
|
+
if (conditionSub.success === false) throw Error(conditionSub.error);
|
|
254
|
+
|
|
255
|
+
const bodyResult = guard.body.accept(inferenceEngine);
|
|
256
|
+
if (bodyResult.success === false) throw Error(bodyResult.error);
|
|
257
|
+
|
|
258
|
+
const sub = this.coreHM.unify(bodyResult.value, returnType);
|
|
259
|
+
if (sub.success === false) throw Error(sub.error);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
this.environments.shift();
|
|
263
|
+
} catch (error: any) {
|
|
264
|
+
this.errors.push(`Type error in '${functionName}': ${error.message}`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export class TypeChecker {
|
|
271
|
+
private signatureMap: Map<string, TypeScheme>;
|
|
272
|
+
private coreHM: CoreHM;
|
|
273
|
+
private errors: string[];
|
|
274
|
+
|
|
275
|
+
constructor() {
|
|
276
|
+
this.signatureMap = new Map<string, TypeScheme>();
|
|
277
|
+
this.coreHM = new CoreHM();
|
|
278
|
+
this.errors = [];
|
|
279
|
+
}
|
|
280
|
+
check(ast: AST): string[] {
|
|
281
|
+
const typeAliasMap = new Map<string, Type>();
|
|
282
|
+
const recordMap = new Map<string, Type>();
|
|
283
|
+
|
|
284
|
+
// Phase 1: Collect declarations
|
|
285
|
+
const collector = new DeclarationCollectorVisitor(
|
|
286
|
+
this.errors,
|
|
287
|
+
typeAliasMap,
|
|
288
|
+
recordMap,
|
|
289
|
+
this.signatureMap,
|
|
290
|
+
this.coreHM
|
|
291
|
+
);
|
|
292
|
+
for (const node of ast) {
|
|
293
|
+
try {
|
|
294
|
+
node.accept(collector);
|
|
295
|
+
} catch (error) {
|
|
296
|
+
this.errors.push(error);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (this.errors.length > 0) return this.errors;
|
|
301
|
+
|
|
302
|
+
// Phase 2: Infer and check functions
|
|
303
|
+
this.inferencePass(ast);
|
|
304
|
+
|
|
305
|
+
return this.errors;
|
|
306
|
+
}
|
|
307
|
+
inferencePass(ast: AST): void {
|
|
308
|
+
// Step 1: Register all functions in env
|
|
309
|
+
const globalEnv = new Map<string, TypeScheme>(this.signatureMap);
|
|
310
|
+
const visitor1 = new FunctionRegistrarVisitor(
|
|
311
|
+
globalEnv,
|
|
312
|
+
this.signatureMap,
|
|
313
|
+
this.coreHM
|
|
314
|
+
);
|
|
315
|
+
for (const node of ast) {
|
|
316
|
+
node.accept(visitor1);
|
|
317
|
+
}
|
|
318
|
+
// Step 2: Infer and check each function
|
|
319
|
+
const visitor2 = new FunctionCheckerVisitor(
|
|
320
|
+
[globalEnv],
|
|
321
|
+
this.signatureMap,
|
|
322
|
+
this.coreHM,
|
|
323
|
+
this.errors
|
|
324
|
+
);
|
|
325
|
+
for (const node of ast) {
|
|
326
|
+
node.accept(visitor2);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export function showType(t: Type): string {
|
|
332
|
+
if (t.type === "TypeVar") return t.name ?? `t${t.id}`;
|
|
333
|
+
|
|
334
|
+
if (isFunctionType(t)) {
|
|
335
|
+
const a = showType(t.args[0]);
|
|
336
|
+
const b = showType(t.args[1]);
|
|
337
|
+
const aDisp = isFunctionType(t.args[0]) ? `(${a})` : a;
|
|
338
|
+
return `${aDisp} -> ${b}`;
|
|
339
|
+
}
|
|
340
|
+
if (isListType(t)) return `[${showType(t.args[0])}]`;
|
|
341
|
+
if (isTupleType(t)) return `(${t.args.map(showType.bind(this)).join(", ")})`;
|
|
342
|
+
|
|
343
|
+
return t.args.length
|
|
344
|
+
? `${t.name} ${t.args.map(showType.bind(this)).join(" ")}`
|
|
345
|
+
: t.name;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export function getReturnType(type: Type): Type {
|
|
349
|
+
let t: Type = type;
|
|
350
|
+
while (isFunctionType(t)) t = t.args[1];
|
|
351
|
+
return t;
|
|
352
|
+
}
|
|
353
|
+
export function getArgumentTypes(type: Type): Type[] {
|
|
354
|
+
const args: Type[] = [];
|
|
355
|
+
let t: Type = type;
|
|
356
|
+
while (isFunctionType(t)) {
|
|
357
|
+
args.push(t.args[0]);
|
|
358
|
+
t = t.args[1];
|
|
359
|
+
}
|
|
360
|
+
return args;
|
|
361
|
+
}
|
|
362
|
+
export function getArity(type: Type): number {
|
|
363
|
+
return getArgumentTypes(type).length;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
export function isUnguardedBody(
|
|
367
|
+
body: UnguardedBody | GuardedBody[]
|
|
368
|
+
): body is UnguardedBody {
|
|
369
|
+
return !Array.isArray(body);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
export function isFunctionType(t: Type): t is FunctionType {
|
|
373
|
+
return t.type === "TypeConstructor" && t.name === "->" && t.args.length === 2;
|
|
374
|
+
}
|
|
375
|
+
export function isListType(t: Type): t is ListType {
|
|
376
|
+
return t.name === "List";
|
|
377
|
+
}
|
|
378
|
+
export function isTupleType(t: Type): t is TupleType {
|
|
379
|
+
return t.name === "Tuple";
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export function functionType(params: Type, returnType: Type): FunctionType {
|
|
383
|
+
return {
|
|
384
|
+
type: "TypeConstructor",
|
|
385
|
+
name: "->",
|
|
386
|
+
args: [params, returnType],
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
export function listType(elementsType: Type): ListType {
|
|
390
|
+
return {
|
|
391
|
+
type: "TypeConstructor",
|
|
392
|
+
name: "List",
|
|
393
|
+
args: [elementsType],
|
|
394
|
+
};
|
|
395
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { typeClasses } from "../utils/types.js";
|
|
2
|
+
import {
|
|
3
|
+
Environment,
|
|
4
|
+
Result,
|
|
5
|
+
showType,
|
|
6
|
+
Substitution,
|
|
7
|
+
Type,
|
|
8
|
+
TypeConstructor,
|
|
9
|
+
TypeScheme,
|
|
10
|
+
TypeVar,
|
|
11
|
+
} from "./checker.js";
|
|
12
|
+
|
|
13
|
+
export class CoreHM {
|
|
14
|
+
private nextVarId = 0;
|
|
15
|
+
|
|
16
|
+
public freshVar(constraints: string[] = []): TypeVar {
|
|
17
|
+
return { type: "TypeVar", id: this.nextVarId++, constraints };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public freeTypeVars(t: Type): Map<number, string[]> {
|
|
21
|
+
const freeVars = new Map<number, string[]>();
|
|
22
|
+
const collect = (type: Type) => {
|
|
23
|
+
if (type.type === "TypeVar") {
|
|
24
|
+
freeVars.set(type.id, type.constraints);
|
|
25
|
+
} else if (type.type === "TypeConstructor") {
|
|
26
|
+
type.args.forEach(collect);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
collect(t);
|
|
30
|
+
return freeVars;
|
|
31
|
+
}
|
|
32
|
+
public generalize(env: Environment, t: Type): TypeScheme {
|
|
33
|
+
const envFreeVars = new Set<number>();
|
|
34
|
+
for (const scheme of env.values()) {
|
|
35
|
+
const schemeFreeVars = this.freeTypeVars(scheme.body);
|
|
36
|
+
for (const id of schemeFreeVars.keys()) {
|
|
37
|
+
if (!scheme.quantifiers.includes(id)) {
|
|
38
|
+
envFreeVars.add(id);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const typeFreeVars = this.freeTypeVars(t);
|
|
44
|
+
|
|
45
|
+
const quantifiers: number[] = [];
|
|
46
|
+
const constraints = new Map<number, string[]>();
|
|
47
|
+
|
|
48
|
+
for (const [id, consts] of typeFreeVars.entries()) {
|
|
49
|
+
if (!envFreeVars.has(id)) {
|
|
50
|
+
quantifiers.push(id);
|
|
51
|
+
if (consts.length > 0) {
|
|
52
|
+
constraints.set(id, consts);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return { type: "TypeScheme", quantifiers, body: t, constraints };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public instantiate(scheme: TypeScheme): Type {
|
|
61
|
+
const substitutions = new Map<number, Type>();
|
|
62
|
+
scheme.quantifiers.forEach((id) => {
|
|
63
|
+
const constraints = scheme.constraints.get(id) || [];
|
|
64
|
+
substitutions.set(id, this.freshVar(constraints));
|
|
65
|
+
});
|
|
66
|
+
return this.applySubst(substitutions, scheme.body);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public applySubst(subst: Substitution, t: Type): TypeVar | TypeConstructor {
|
|
70
|
+
if (t.type === "TypeVar") {
|
|
71
|
+
const replacement = subst.get(t.id);
|
|
72
|
+
return replacement ? replacement : t;
|
|
73
|
+
} else if (t.type === "TypeConstructor") {
|
|
74
|
+
return {
|
|
75
|
+
type: "TypeConstructor",
|
|
76
|
+
name: t.name,
|
|
77
|
+
args: t.args.map((arg) => this.applySubst(subst, arg)),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
throw new Error("Unexpected TypeScheme in applySubst");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public composeSubst(s1: Substitution, s2: Substitution): Substitution {
|
|
85
|
+
const result = new Map(s2);
|
|
86
|
+
for (const [id, type] of s1) {
|
|
87
|
+
result.set(id, this.applySubst(s2, type));
|
|
88
|
+
}
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
public unify(t1: Type, t2: Type): Result<Substitution> {
|
|
93
|
+
if (t1.type === "TypeVar") return this.unifyVar(t1, t2);
|
|
94
|
+
if (t2.type === "TypeVar") return this.unifyVar(t2, t1);
|
|
95
|
+
if (t1.type === "TypeConstructor" && t2.type === "TypeConstructor") {
|
|
96
|
+
if (t1.name !== t2.name || t1.args.length !== t2.args.length) {
|
|
97
|
+
return {
|
|
98
|
+
success: false,
|
|
99
|
+
error: `Cannot unify ${showType(t1)} with ${showType(t2)}`,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
let sub: Substitution = new Map();
|
|
103
|
+
for (let i = 0; i < t1.args.length; i++) {
|
|
104
|
+
const arg1 = this.applySubst(sub, t1.args[i]);
|
|
105
|
+
const arg2 = this.applySubst(sub, t2.args[i]);
|
|
106
|
+
const argSubRes = this.unify(arg1, arg2);
|
|
107
|
+
if (!argSubRes.success) return argSubRes;
|
|
108
|
+
sub = this.composeSubst(sub, argSubRes.value);
|
|
109
|
+
}
|
|
110
|
+
return { success: true, value: sub };
|
|
111
|
+
}
|
|
112
|
+
return { success: false, error: `Cannot unify non-types` };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public unifyVar(v: TypeVar, t: Type): Result<Substitution> {
|
|
116
|
+
if (t.type === "TypeVar" && t.id === v.id) {
|
|
117
|
+
return { success: true, value: new Map() };
|
|
118
|
+
}
|
|
119
|
+
if (this.occurs(v.id, t)) {
|
|
120
|
+
return { success: false, error: `Occurs check failed` };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
for (const constraint of v.constraints) {
|
|
124
|
+
this.checkConstraint(constraint, t);
|
|
125
|
+
}
|
|
126
|
+
if (t.type === "TypeVar") {
|
|
127
|
+
for (const constraint of t.constraints) {
|
|
128
|
+
this.checkConstraint(constraint, v);
|
|
129
|
+
}
|
|
130
|
+
// Merge constraints
|
|
131
|
+
const mergedConstraints = [
|
|
132
|
+
...new Set([...v.constraints, ...t.constraints]),
|
|
133
|
+
];
|
|
134
|
+
v.constraints = mergedConstraints;
|
|
135
|
+
t.constraints = mergedConstraints;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return { success: true, value: new Map([[v.id, t]]) };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public checkConstraint(constraintName: string, t: Type) {
|
|
142
|
+
if (t.type === "TypeVar") {
|
|
143
|
+
if (!t.constraints.includes(constraintName)) {
|
|
144
|
+
t.constraints.push(constraintName);
|
|
145
|
+
}
|
|
146
|
+
} else if (t.type === "TypeConstructor") {
|
|
147
|
+
const instances = typeClasses.get(constraintName);
|
|
148
|
+
if (!instances || !instances.includes(t.name)) {
|
|
149
|
+
throw new Error(
|
|
150
|
+
`Type '${showType(t)}' is not an instance of '${constraintName}'`
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
public occurs(varId: number, t: Type): boolean {
|
|
157
|
+
if (t.type === "TypeVar") return t.id === varId;
|
|
158
|
+
if (t.type === "TypeConstructor")
|
|
159
|
+
return t.args.some((a) => this.occurs(varId, a));
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
}
|