-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper.ts
180 lines (146 loc) · 4.69 KB
/
helper.ts
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
import { Context, isContext, makeContext, MonoType, PolyType, TypeVariable } from "./models";
// substitutions
export type Substitution = {
type: 'substitution',
(m: MonoType): MonoType;
(t: PolyType): PolyType;
(c: Context): Context;
(s: Substitution): Substitution;
raw: { [typeVariables: string]: MonoType }
}
export const makeSubstitution = (raw: Substitution["raw"]): Substitution => {
const fn = ((arg: MonoType | PolyType | Context | Substitution) => {
if (arg.type === "substitution") return combine(fn, arg)
return apply(fn, arg);
}) as Substitution
fn.type = 'substitution';
fn.raw = raw;
return fn;
}
function apply<T extends MonoType | PolyType | Context>(substitution: Substitution, value: T): T;
function apply(s: Substitution, value: MonoType | PolyType | Context): MonoType | PolyType | Context {
if (isContext(value)) {
return makeContext(Object.fromEntries(
Object.entries(value)
.map(([k, v]) => [k, apply(s, v)])
))
}
if (value.type === "ty-var") {
if (s.raw[value.a]) return s.raw[value.a];
return value;
}
if (value.type === "ty-app") {
return { ...value, mus: value.mus.map((m) => apply(s, m)) };
}
if (value.type === "ty-quantifier") {
// If the quantifier variable conflicts with any substitution...
if (s.raw[value.a] || Object.values(s.raw).some(t => freeVars(t).includes(value.a))) {
// Rename the quantifier variable
const aPrime = newTypeVar();
const renamedSigma = apply(makeSubstitution({ [value.a]: aPrime }), value.sigma);
// Apply the original substitution to the renamed sigma
return {
...value,
a: aPrime.a,
sigma: apply(s, renamedSigma)
};
}
return { ...value, sigma: apply(s, value.sigma) };
}
throw new Error('Unknown argument passed to substitution')
}
const combine = (s1: Substitution, s2: Substitution): Substitution => {
return makeSubstitution({
...s1.raw,
...Object.fromEntries(Object.entries(s2.raw).map(([k, v]) => [k, s1(v)]))
})
}
// new type variable
let currentTypeVar = 0;
export const newTypeVar = (): TypeVariable => ({
type: 'ty-var',
a: `t${currentTypeVar++}`
})
// instantiate
// mappings = { a |-> t0, b |-> t1 }
// Va. Vb. a -> b
// t0 -> t1
export const instantiate = (
type: PolyType,
mappings: Map<string, TypeVariable> = new Map()
): MonoType => {
if (type.type === "ty-var") {
return mappings.get(type.a) ?? type;
}
if (type.type === "ty-app") {
return { ...type, mus: type.mus.map((m) => instantiate(m, mappings)) };
}
if (type.type === "ty-quantifier") {
mappings.set(type.a, newTypeVar());
return instantiate(type.sigma, mappings);
}
throw new Error('Unknown type passed to instantiate')
}
// generalise
export const generalise = (ctx: Context, type: MonoType): PolyType => {
const quantifiers = diff(freeVars(type), freeVars(ctx));
let t: PolyType = type;
quantifiers.forEach(q => {
t = { type: 'ty-quantifier', a: q, sigma: t }
})
return t;
}
const diff = <T>(a: T[], b: T[]): T[] => {
const bset = new Set(b);
return a.filter(v => !bset.has(v))
}
const freeVars = (value: PolyType | Context): string[] => {
if (isContext(value)) {
return Object.values(value).flatMap(freeVars)
}
if (value.type === "ty-var") {
return [value.a];
}
if (value.type === "ty-app") {
return value.mus.flatMap(freeVars)
}
if (value.type === "ty-quantifier") {
return freeVars(value.sigma).filter(v => v !== value.a)
}
throw new Error('Unknown argument passed to substitution')
}
// unify
export const unify = (type1: MonoType, type2: MonoType): Substitution => {
if (type1.type === "ty-var" && type2.type === "ty-var" && type1.a === type2.a) {
return makeSubstitution({})
}
if (type1.type === "ty-var") {
if (contains(type2, type1)) throw new Error('Infinite type detected')
return makeSubstitution({
[type1.a]: type2
})
}
if (type2.type === "ty-var") {
return unify(type2, type1)
}
if (type1.C !== type2.C) {
throw new Error(`Could not unify types (different type functions): ${type1.C} and ${type2.C}`)
}
if (type1.mus.length !== type2.mus.length) {
throw new Error(`Could not unify types (different argument lengths): ${type1} and ${type2}`)
}
let s: Substitution = makeSubstitution({})
for (let i = 0; i < type1.mus.length; i++) {
s = (unify(s(type1.mus[i]), s(type2.mus[i])))(s)
}
return s;
}
const contains = (value: MonoType, type2: TypeVariable): boolean => {
if (value.type === "ty-var") {
return value.a === type2.a;
}
if (value.type === "ty-app") {
return value.mus.some((t) => contains(t, type2))
}
throw new Error('Unknown argument passed to substitution')
}