-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSpadTypeEvaluator.spad
360 lines (285 loc) · 11.6 KB
/
SpadTypeEvaluator.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
)abbrev domain STEVAL SpadTypeEvaluator
SpadTypeEvaluator(EC : SpadEnvCategory) : Exports == Implementation where
)include SpadTypeDefs.inc
Exports ==> CoercibleTo(PF) with
fetchFunctorType : (Symbol, EC) -> MT
getTypeInfo : (APP, EC) -> TI
makeFunctorType : (FT, EC) -> MT
Implementation ==> add
import Logger('TypeEval)
import Printer
import SpadNode
import SpadNodeFactory
import SpadNodeTools
import SpadTypeDatabase
import SpadTypeUnifier
import SpadLogic(EC)
apply : (APP, MT, EC) -> TI
flatten : (Symbol, TI, EC) -> TI
evalTypeGuards : (APP, TI, EC) -> TI
eraseConstraints : (APP, TI, EC) -> TI
fetchFunctorType(name : Symbol, env : EC) : MT ==
n? := search(name, env)
n? case EVAL => (n? :: EVAL).type :: MT
info ["getTypeInfo :" :: PF, bold(name :: PF)]
mt := fetchType name
ti := flatten(name, mt.result :: TI, env)
mt := [mt.args, [ti]]
env(name) := [[mt], []]
mt
getTypeInfo(app : APP, env : EC) : TI ==
n? := search(app, env)
n? case EVAL => (n? :: EVAL).type :: TI
name := app.function :: Symbol
ti := apply(app, fetchFunctorType(name, env), env)
env(app) := [[ti], []]
ti := evalTypeGuards(app, ti, env)
hasFreeVars? [app] => ti
eraseConstraints(app, ti, env)
makeFunctorType(ft : FT, env : EC) : MT ==
mt := makeType ft
ti := flatten(ft.name, mt.result :: TI, env)
mt := [mt.args, [ti]]
env(ft.name) := [[mt], []]
mt
apply(app : APP, mt : MT, env : EC) : TI ==
#app.args ~= #mt.args =>
fail ["wrong number of arguments for:" :: PF, app :: PF]
error "apply $ SpadTypeEvaluator"
empty? app.args => mt.result :: TI
margs := [(arg :: TD).expr for arg in mt.args]
n1 := nodeTuple margs
n2 := nodeTuple app.args
subs := unifyType(n1, setMajor(n2, 1))
subs case "failed" =>
fail [paren [a :: PF for a in margs], "~" :: PF,
paren [a :: PF for a in app.args]]
error "apply $ SpadTypeEvaluator"
subs case SUBS =>
ti := substitute(mt.result, subs) :: TI
tds := [substitute(arg, subs) :: TD for arg in mt.args]
-- constraints have "X has Y $ Z" format, so in case of failure
-- we can report which type requires the property to be met
cs := [nodeTypeOrigin(nodeTypeHas(td.expr, td.type), [app])
for td in tds]
cs := concat(cs, ti.constraints)
setMajor(nodeTypeInfo(ti.body, ti.hasList, cs, ti.superType), 0) :: TI
flatten(name : Symbol, ti : TI, env : EC) : TI ==
info ["Flatten" :: PF, bold(name :: PF)]
origBody := ti.body.list
flatBody : List(N) := []
hasLst : AssociationList(APP, N) := [[]]
constraints := ti.constraints
while not empty? origBody repeat
(n, origBody) := (first origBody, rest origBody)
cond := ['true]
if typeGuard? n then
tg := n :: TG
if apply? tg.expr then
-- encountered a functor with constraints
(n, cond) := (tg.expr, tg.type)
-- merge functor
apply? n =>
app := n :: APP
-- check if we have already merged a functor
key?(app, hasLst) =>
debug ["Skipping:" :: PF, bold(app :: PF), "with" :: PF, bold(hasLst(app) :: PF)]
-- if true then erase previously remembered constraints
true? cond =>
hasLst(app) := cond
-- if remembered constraints are true then ignore new constraints
true? hasLst(app) => "iterate"
hasLstVal := hasLst(app)
-- BUG: hasLstVal may be a complex logic expression, checks below
-- assume it's a flat list of statements
condLst : List(N) :=
apply? hasLstVal =>
(hasLstVal :: APP).args
[hasLstVal]
member?(cond, condLst) => "iterate"
hasLst(app) := nodeApp(['or], concat(cond, condLst))
debug(["Merge" :: PF, bold(true? cond => n :: PF; nodeTypeGuard(n, cond) :: PF),
"with" :: PF, name :: PF])
hasLst(app) := cond
ti' := apply(app, fetchType(app.function :: Symbol), env)
-- TODO: does it make sense?
constraints := concat(ti'.constraints, constraints)
-- extend original functor's body with included constructor type
snippet : List(N) := []
-- TODO: merge both branches
if true? cond then
-- case 1: merged functor has no constraints
for e in ti'.body.list repeat
e' :=
cond' := ['true]
if typeGuard? e then
tg := e :: TG
(e, cond') := (tg.expr, tg.type)
apply? e => -- add constraint to merged functor
true? cond' => e
nodeTypeGuard(e, cond')
typeDecl? e => -- add origin to function signature
e := nodeTypeOrigin(e, n)
true? cond' => e
nodeTypeGuard(e, cond')
error "Not handled!"
snippet := [e', :snippet]
else
-- case 2: merged functor has some constraints
-- INFO: When we merge functor A with %, we know that all what is
-- within A belongs to % only when "% has A". Hence for each
-- component of A we remember simple constraint "% has A".
-- More complex constraints are remembered in hasList.
--
-- This allows to add extra options to condition "% has A" in case
-- we encounter some other (less restrictive) constraints later.
for e in ti'.body.list repeat
e' :=
cond' := nodeTypeHas(typeVar(), [app])
if typeGuard? e then
tg := e :: TG
-- add extra constraint to merged functor
(e, cond') := (tg.expr, nodeApp(['and], [cond', tg.type]))
apply? e => -- add constraint to merged functor
nodeTypeGuard(e, cond')
typeDecl? e => -- add origin to function signature
e := nodeTypeOrigin(e, n)
nodeTypeGuard(e, cond')
error "Not handled!"
snippet := [e', :snippet]
origBody := concat(reverse snippet, origBody)
-- merge everything else
typeDecl? n or typeGuard? n or typeOrigin? n =>
debug ["Copy" :: PF, bold(n :: PF), "to" :: PF, name :: PF]
flatBody := [n, :flatBody]
aggregate? n =>
origBody := concat(n :: List(N), origBody)
fail ["Flatten" :: PF, bold(n :: PF)]
error "Not handled!"
hasLst' : List(N) := []
for he in entries hasLst repeat
h :=
true? he.entry => [he.key]
nodeTypeGuard([he.key], he.entry)
hasLst' := [h, :hasLst']
[[ti.body.kind, reverse flatBody],
hasLst', removeDuplicates constraints, ti.superType]
evaluate'(n : N, self : APP, env : EC) : N ==
evaluate(substitute(n, typeVar() :: TV, [self]), env)
eraseConstraints(name : APP, ti : TI, env : EC) : TI ==
info ["Check type constraints for" :: PF, bold(name :: PF)]
appliesCleanly(n : N) : Boolean ==
true? evaluate'((n :: TO).expr, name, env)
ti := [ti.body, ti.hasList, remove(appliesCleanly, ti.constraints),
ti.superType]
env(name) := [[ti], []]
empty? ti.constraints => ti
fail ["Could not construct type" :: PF, bold(name :: PF)]
for c in ti.constraints repeat
to := c :: TO
te := to.expr :: TEH
fail [bold blue(to.type :: PF), "requires" :: PF, bold(te.expr :: PF),
"to have" :: PF, bold(te.type :: PF)]
error "eraseConstraints $ SpadTypeEvaluator"
evalTypeGuards(name : APP, ti : TI, env : EC) : TI ==
info ["Evaluate type guards for" :: PF, bold(name :: PF)]
-- evaluate type guards on HasList
continue? := true
while continue? repeat
newHasList : List(N) := []
continue? := false
for n in ti.hasList repeat
n' :=
not typeGuard? n => n
tg := n :: TG
type' := evaluate'(tg.type, name, env)
true? type' => (continue? := true; tg.expr)
nodeTypeGuard(tg.expr, type')
newHasList := [n', :newHasList]
ti := [ti.body, reverse newHasList, ti.constraints, ti.superType]
env(name) := [[ti], []]
-- with new HasList evaluate type guards in TypeInfo's body
eval(n : N) : N ==
not typeGuard? n => n
tg := n :: TG
type' := evaluate'(tg.type, name, env)
true? type' => tg.expr
nodeTypeGuard(tg.expr, type')
ti := [[ti.body.kind, map(eval, ti.body.list)],
ti.hasList, ti.constraints, ti.superType]
env(name) := [[ti], []]
ti
)abbrev package TEVALT SpadTypeEvaluatorTest
SpadTypeEvaluatorTest(EC : SpadEnvCategory) : Exports == Implementation where
)include SpadTypeDefs.inc
Exports ==> with
test1 : () -> Void
test2 : () -> Void
test3 : () -> Void
test4 : () -> Void
test5 : () -> Void
test6 : () -> Void
fail1 : () -> Void
fail2 : () -> Void
Implementation ==> add
import Printer
import SpadNodeFactory
import SpadTypeEvaluator(EC)
import MainLogger
initTest : () -> EC
initTest ==
loggerDefaultLevel "info"
loggerLevel('Parser, "notice")
resetTime()
new()
test1 ==
env := initTest()
symbols : List(Symbol) := (['Integer, 'List, 'Fraction, 'Expression,
'UnivariateTaylorSeries, 'Matrix])
for s in symbols repeat
println [bold blue(s :: PF), "is" :: PF, fetchFunctorType(s, env) :: PF]
println (env :: PF)
test2 ==
env := initTest()
-- type flattening, type contrains evaluation
types : List(APP) := (
[[['NonNegativeInteger], []],
[['Integer], []],
[['Symbol], []],
[['Float], []],
[['List], [integerType]],
[['Fraction], [integerType]],
[['Matrix], [integerType]],
[['Expression], [integerType]]])
for t in types repeat
println [bold blue(t :: PF), "is" :: PF, getTypeInfo(t, env) :: PF]
println (env :: PF)
test3 ==
env := initTest()
-- check if values as accepted as functor arguments (instead of types)
println (fetchFunctorType('UnivariateTaylorSeries, env) :: PF)
t := [['UnivariateTaylorSeries], [integerType(), ['x]$N, [2]$N]]$APP
println (getTypeInfo(t, env) :: PF)
println (env :: PF)
test4 ==
env := initTest()
-- TODO: some extremely slow cases to be improved
println (fetchFunctorType('UnivariateLaurentSeries, env) :: PF)
--println (fetchFunctorType('UnivariatePuiseuxSeries, env) :: PF)
--println (fetchFunctorType('ExponentialExpansion, env) :: PF)
println (env :: PF)
-- Should I consider following case:
-- undefinedType() "%undef" passed as a parameter to a functor prevents
-- contraint evaluation that involves the parameter
test5 ==
env := initTest()
LI := nodeApp(['List], [['Integer]$N])$N
println (getTypeInfo([['List], [LI]], env) :: PF)
fail1 ==
env := initTest()
-- wrong number of arguments => should fail!
println (getTypeInfo([['List], []], env) :: PF)
fail2 ==
env := initTest()
-- wrong type parameter => should fail!
println (getTypeInfo([['List], [nodeApp(['BasicType], [])]], env) :: PF)