-
Notifications
You must be signed in to change notification settings - Fork 2
/
SpadTypeUnifier.spad
374 lines (328 loc) · 12.5 KB
/
SpadTypeUnifier.spad
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
)abbrev package STUNIFY SpadTypeUnifier
SpadTypeUnifier() : Exports == Implementation where
)include SpadTypeDefs.inc
P ==> Record(t1 : N, t2 : N)
Exports ==> with
unify? : (N, N) -> Boolean
unifyType : (N, N) -> URES
unifyType : (N, List(N)) -> URES
findMatches : (List(N), List(N)) -> Record(terms : List(N), subs : SUBS)
isSubType : (N, N) -> Union(N, "true", "false")
findSubTypes : (List(N), List(N)) -> List(N)
dropSuperTypes : List(N) -> List(N)
dropSubTypes : List(N) -> List(N)
Implementation ==> add
import Printer
import Logger('Unify)
import SpadNode
import SpadNodeFactory
import SpadNodeTools
import DaaseDatabase
unifyType' : (N, N) -> URES
unifyTypeList(pairs : List(P)) : URES ==
subs : SUBS := empty()
while not empty? pairs repeat
pair := first pairs
ures := unifyType'(pair.t1, pair.t2)
ures case "failed" => return "failed"
subs := concat (subs, ures :: SUBS)
pairs := [[substitute(pair.t1, subs), substitute(pair.t2, subs)]$P for pair in rest pairs]
subs
unifyType'(n1 : N, n2 : N) : URES ==
-- left or right node is a type variable => just generate a substitution
typeVar? n1 and typeVar? n2 =>
tv1 := n1 :: TV
tv2 := n2 :: TV
tv1 = tv2 => [[]]
-- always substitute newer variable with older one to avoid (?) cycles
if tv1 < tv2
then [[[tv1, [n2]]]]
else [[[tv2, [n1]]]]
typeVar? n1 =>
occurs? (n1 :: TV, n2) => "failed"
[[[n1 :: TV, [n2]]]]
typeVar? n2 =>
occurs? (n2 :: TV, n1) => "failed"
[[[n2 :: TV, [n1]]]]
-- use type value instead
typeValue? n1 =>
unifyType'((n1 :: TVL).value, n2)
typeValue? n2 =>
unifyType'(n1, (n2 :: TVL).value)
-- both nodes can be represented as lists of terms and variables
-- so convert them to a pair of lists and perform unification
mappingType? n1 and mappingType? n2 =>
mt1 := n1 :: MT
mt2 := n2 :: MT
#mt1.args ~= #mt2.args => "failed"
unifyTypeList ([[t1, t2]$P for t1 in [mt1.result, :mt1.args]
for t2 in [mt2.result, :mt2.args]])
apply? n1 and apply? n2 =>
app1 := n1 :: APP
app2 := n2 :: APP
#app1.args ~= #app2.args => "failed"
unifyTypeList ([[t1, t2]$P for t1 in [app1.function, :app1.args]
for t2 in [app2.function, :app2.args]])
recordType? n1 and recordType? n2 =>
rt1 := n1 :: RT
rt2 := n2 :: RT
#(fields rt1) ~= #(fields rt2) => "failed"
for f1 in fields rt1 for f2 in fields rt2 repeat
if f1.expr ~= f2.expr then
return "failed"
unifyTypeList ([[f1.type, f2.type]$P
for f1 in fields rt1 for f2 in fields rt2])
unionType? n1 and unionType? n2 =>
ut1 := n1 :: UT
ut2 := n2 :: UT
#(variants ut1) ~= #(variants ut2) => "failed"
unifyTypeList ([[v1, v2]$P for v1 in variants ut1
for v2 in variants ut2])
typeDecl? n1 and typeDecl? n2 =>
td1 := n1 :: TD
td2 := n2 :: TD
td1.expr ~= td2.expr => "failed"
unifyType'(td1.type, td2.type)
typeOrigin? n1 and typeOrigin? n2 =>
to1 := n1 :: TO
to2 := n2 :: TO
unifyTypeList [[to1.type, to2.type]$P, [to1.expr, to2.expr]$P]
typeOrigin? n1 =>
to1 := n1 :: TO
unifyType'(to1.expr, n2)
typeOrigin? n2 =>
to2 := n2 :: TO
unifyType'(n1, to2.expr)
aggregate? n1 and aggregate? n2 =>
s1 := n1 :: AGG
s2 := n2 :: AGG
s1.kind ~= s1.kind => "failed"
#s1.list ~= #s2.list => "failed"
unifyTypeList [[e1, e2]$P for e1 in s1.list for e2 in s2.list]
-- constants
symbol? n1 and symbol? n2 and (n1 :: Symbol) = (n2 :: Symbol) => [[]]
integer? n1 and integer? n2 and (n1 :: Integer) = (n2 :: Integer) => [[]]
string? n1 and string? n2 and (n1 :: String) = (n2 :: String) => [[]]
"failed"
unifyType(n1 : N , n2 : N) : URES ==
ures := unifyType'(n1, n2)
summary : PF :=
ures case "failed" =>
paren bold red("no" :: PF)
ures case SUBS =>
spaces [paren bold green("yes" :: PF), (ures :: SUBS) :: PF]
-- debug([brace bold(n1 :: PF), bold yellow("~" :: PF),
-- brace bold(n2 :: PF), summary])
ures
unifyType(type : N, terms : List(N)) : URES ==
ures : URES :=
subsList : List(SUBS) := []
for term in terms repeat
ures := unifyType'(term, type)
ures case "failed" => "iterate"
subsList := [ures :: SUBS, :subsList]
empty? subsList => "failed"
merge subsList
summary : PF :=
ures case "failed" =>
paren bold red("no" :: PF)
ures case SUBS =>
spaces [paren bold green("yes" :: PF), (ures :: SUBS) :: PF]
-- debug([brace bold(type :: PF), bold yellow("~" :: PF),
-- bracket [bold(t :: PF) for t in terms], summary])
ures
unify?(n1, n2) ==
unifyType(n1, n2) case SUBS
findMatches (terms1, terms2) ==
terms : List(N) := []
subsList : List(SUBS) := []
for t1 in terms1 repeat
for t2 in terms2 repeat
ures := unifyType'(t1, t2)
ures case "failed" => "iterate"
++ prefer concrete type instead of wildcard
t := if containsWildcard? t1 then t2 else t1
++ Avoid wildcard propagation by removing all substitutions with
++ wildcards in them.
ss : SUBS := [[]]
for s in entries(ures :: SUBS) repeat
es := remove(containsWildcard?, s.entry)
empty? es => "iterate"
ss(s.key) := es
t := substitute(t, ss)
++ Sometimes, by applying substitutions, unions will become
++ ill-formed (i.e. contain two fields of same type),
++ filter out such cases.
null? t => "iterate"
terms := [t, :terms]
subsList := [ss, :subsList]
++ Remove wildcard if we have more than one match.
if #terms > 1 then
terms := [t for t in removeDuplicates terms | not containsWildcard? t]
debug spaces(
[bracket [bold(t1 :: PF) for t1 in terms1], bold yellow("~" :: PF),
bracket [bold(t2 :: PF) for t2 in terms2],
paren bold(empty? terms => red("no" :: PF); green("yes" :: PF))])
[terms, merge subsList]
isSubType' : (N, N) -> Union(N, "true", "false")
isUnionSubType(ut1 : UT, ut2 : UT) : Union(N, "true", "false") ==
-- {Union(T1, T2, ..., Tn)} <: {Union(S1, S2, ..., Sm)} and n <= m
-- i.e. T1 <: S1 and T2 <: S2 and ... and Tn <: Sn
#(variants ut1) > #(variants ut2) => "false"
ts : List(N) := []
for v1 in variants ut1 for v2 in variants ut2 repeat
r := isSubType'(v1, v2)
r case "false" => return "false"
r case "true" => "iterate"
ts := [r :: N, :ts]
empty? ts => "true"
#ts = 1 => ts.1
nodeApp(['and], removeDuplicates ts)
++ INFO: Given a subtyping relation X <: Y, that reads as X is safe to use
++ in context where Y is used, we say that:
++ X <: Y => F(X) <: F(Y), then F is covariant
++ X <: Y => F(Y) <: F(X), then F is contravariant
++ otherwise F is invariant
isSubType'(n1 : N, n2 : N) : Union(N, "true", "false") ==
typeOrigin? n1 =>
to1 := n1 :: TO
isSubType'(to1.expr, n2)
typeOrigin? n2 =>
to2 := n2 :: TO
isSubType'(n1, to2.expr)
typeValue? n1 =>
tv := n1 :: TVL
isSubType'(tv.value, n2)
typeValue? n2 =>
tv := n2 :: TVL
isSubType'(n1, tv.value)
undefinedType? n1 or undefinedType? n2 => "false"
n2 = nodeApp(['Type], []) => "true"
mappingType? n1 and mappingType? n2 =>
++ {(T1, T2, ..., Tn) -> T0} <: (S1, S2, ..., Sn) -> S0
++ Mapping type is contravariant for argument types, and covariant for
++ the output type, i.e. S1 <: T1 and S2 <: T2 and ... and T0 <: S0
mt1 := n1 :: MT
mt2 := n2 :: MT
#mt1.args ~= #mt2.args => "false"
mt1.result ~= mt2.result => "false"
ts : List(N) := []
for s in mt2.args for t in mt1.args repeat
r := isSubType'(s, t)
r case "false" => return "false"
r case "true" => "iterate"
ts := [r :: N, :ts]
empty? ts => "true"
#ts = 1 => ts.1
nodeApp(['and], removeDuplicates ts)
apply? n1 and apply? n2 =>
-- {T0(T1, T2, ..., Tk)} <: {S0(S1, S2, ..., Sk)}
-- AFAIK functors in SPAD are invariant (!) for the time being.
app1 := n1 :: APP
app2 := n2 :: APP
fn1 := app1.function :: Symbol
fn2 := app2.function :: Symbol
#app1.args = 0 and #app2.args = 0 and isSubDomain?(fn1, fn2) => "true"
fn1 ~= fn2 => "false"
#app1.args ~= #app2.args => "false"
ts : List(N) := []
for arg1 in app1.args for arg2 in app2.args repeat
r := isSubType'(arg1, arg2)
r case "false" => return "false"
r case "true" => "iterate"
ts := [r :: N, :ts]
empty? ts => "true"
#ts = 1 => ts.1
nodeApp(['and], removeDuplicates ts)
recordType? n1 and recordType? n2 =>
-- {Record(F1 : T1, F2 : T2, ..., Fn : Tn)}
-- <: {Record(F1 : S1, F2 : S2, ..., Fm : Sm)} and n = m
-- i.e. T1 <: S1 and T2 <: S2 and ... and Tn <: Sm
rt1 := n1 :: RT
rt2 := n2 :: RT
#(fields rt1) ~= #(fields rt2) => "false"
ts : List(N) := []
for f1 in fields rt1 for f2 in fields rt2 repeat
f1.expr ~= f2.expr => return "false"
r := isSubType'(f1.type, f2.type)
r case "false" => return "false"
r case "true" => "iterate"
ts := [r :: N, :ts]
empty? ts => "true"
#ts = 1 => ts.1
nodeApp(['and], removeDuplicates ts)
unionType? n2 =>
if unionType? n1 then
r := isUnionSubType(n1 :: UT, n2 :: UT)
r ~= "false" => return r
-- {S} <: {Union(T1, T2, ..., Tn)}
-- i.e. foreach i such that S <: Ti. {S <: Ti or ...}
ts : List(N) := []
for v in variants(n2 :: UT) repeat
r := isSubType'(n1, v)
r case "false" => "iterate"
r case "true" => return "true"
ts := [r :: N, :ts]
empty? ts => "false"
#ts = 1 => ts.1
nodeApp(['or], removeDuplicates ts)
typeDecl? n1 and typeDecl? n2 =>
td1 := n1 :: TD
td2 := n2 :: TD
td1.expr ~= td2.expr => "false"
isSubType'(td1.type, td2.type)
aggregate? n1 and aggregate? n2 =>
s1 := n1 :: AGG
s2 := n2 :: AGG
s1.kind ~= s2.kind => "false"
l1 := s1.list
l2 := s2.list
#l1 < #l2 => "false"
for i2 in l2 repeat
"and"/[isSubType'(i1, i2) case "false" for i1 in l1] =>
return "false"
"true"
typeVar? n1 =>
occurs?(n1 :: TV, n2) => "false"
nodeSubType(n1, n2)
typeVar? n2 =>
occurs?(n2 :: TV, n1) => "false"
nodeSubType(n1, n2)
n1 = n2 => "true"
"false"
isSubType(n1 : N, n2 : N) : Union(N, "true", "false") ==
res := isSubType'(n1, n2)
pf :=
res case "true" => bold green("yes" :: PF)
res case "false" => bold red("no" :: PF)
spaces [bold magenta ("when" :: PF), (res :: N) :: PF]
-- debug ([brace(bold(n1 :: PF)), bold yellow("<:" :: PF),
-- brace(bold(n2 :: PF)), pf])
res
findSubTypes(ts1 : List(N), ts2 : List(N)) : List(N) ==
ts : List(N) := []
for t1 in ts1 repeat
for t2 in ts2 repeat
isSubType'(t1, t2) case "false" => "iterate"
ts := [t1, :ts]
removeDuplicates ts
dropSuperTypes(sl : List(N)) : List(N) ==
tl : List(N) := []
for s in sl repeat
subTypeExists? := false
for t in sl | s ~= t and not subTypeExists? repeat
if isSubType'(t, s) case "true" then
subTypeExists? := true
if not subTypeExists? then
tl := [s, :tl]
tl
dropSubTypes(sl : List(N)) : List(N) ==
tl : List(N) := []
for s in sl repeat
superTypeExists? := false
for t in sl | s ~= t and not superTypeExists? repeat
if isSubType'(s, t) case "true" then
superTypeExists? := true
if not superTypeExists? then
tl := [s, :tl]
tl