-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.scm
601 lines (544 loc) · 18.3 KB
/
types.scm
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
;; types.scm
;; TO DO:
;; * limited list typecheck caching:
;; Want to cache most specific derived list type informatio about lists.
;; When a list value passes a list-of or list-with predicate, that fact
;; should be recorded. Later, when cons, set-car, set-cdr! is done,
;; the type of the resulting list can be calculated by doing a lub with types
;; of new value(s).
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; TOP TYPE
;;
(define-record-type top-type :top-type
(make-top-type)
top-type?)
(define <top> (make-top-type))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; BOTTOM TYPE
;;
(define-record-type bottom-type :bottom-type
(make-bottom-type)
bottom-type?)
(define-record-discloser :bottom-type
(lambda (v) `(bottom-type)))
(define <bottom> bottom-type?)
;; canonical undefined object
(define *undefined* (make-bottom-type))
(define (undefined? obj) (eq? obj *undefined*))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; EQ TYPES
;;
(define-record-type eq-type :eq-type
(make-eq-type val)
eq-type?
(val eq-type-val))
(define == make-eq-type)
(define <eq-type> eq-type?)
(define-record-discloser :eq-type
(lambda (v) `(eq-type ,(eq-type-val v))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; AND TYPES
;;
(define-record-type and-type :and-type
(really-make-and-type conjuncts)
and-type?
(conjuncts and-type-types))
(define-record-discloser :and-type
(lambda (v) `(and-type ,@(and-type-types v))))
(define <and-type> and-type?)
;; simple normalization of and-types:
;; (1) (and x (and y z)) => (and x y z)
;; (2) (and x y z), (subtype? x y) => (and x z)
;; (3) (and x) => x
(define (make-and-type . types)
;; first get flat list of conjuncts (in reverse order)
(let ((types1 (fold (lambda (type out)
(if (and-type? type)
(append (reverse (and-type-types type)) out)
(cons type out)))
'() types)))
(dbg 'types "make-and-type, types1 = ~a" types1)
;; next see if can merge any conjuncts
(let ((types2 (fold (lambda (type1 out)
;; if any other types are subtypes of type, ignore type
(if (any (lambda (type2)
(and (not (eq? type1 type2))
(subtype? type2 type1)))
types1)
out
(cons type1 out)))
'() types1)))
(dbg 'types "make-and-type, types2 = ~a" types2)
(if (= 1 (length types2))
(car types2)
(really-make-and-type types2)))))
(define and? make-and-type)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; OR TYPES
;;
(define-record-type or-type :or-type
(really-make-or-type disjuncts)
or-type?
(disjuncts or-type-types))
(define-record-discloser :or-type
(lambda (v) `(or-type ,@(or-type-types v))))
(define <or-type> or-type?)
;; simple normalization of or-types:
;; (1) (or x (or y z)) => (or x y z)
;; (2) (or x y z), (subtype? x y) => (or y z)
;; (3) (or x) => x
(define (make-or-type . types)
;; first get flat list of disjuncts
(let ((types1 (fold (lambda (type out)
(if (or-type? type)
(append (reverse (or-type-types type)) out)
(cons type out)))
'() types)))
(dbg 'types "make-or-type, types1 = ~a" types1)
;; next see if can merge any disjuncts
(let ((types2 (fold (lambda (type1 out)
;; if this is a subtype of any other type, ignore this
(if (any (lambda (type2)
(and (not (eq? type1 type2))
(subtype? type1 type2)))
types1)
out
(cons type1 out)))
'() types1)))
(dbg 'types "make-or-type, types2 = ~a" types2)
(if (= 1 (length types2))
(car types2)
(really-make-or-type types2)))))
(define or? make-or-type)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; SIGNATURE-TYPES
;;
;; rest-type/f is element type of each remaining element.
;; - if rest-type/f is #f, must have exactly (length types) elt.s
(define-record-type signature-type :signature-type
(really-make-signature-type types rest-type/f)
signature-type?
(types signature-type-types)
(rest-type/f signature-type-rest-type/f))
(define-record-discloser :signature-type
(lambda (v) `(signature-type ,(signature-type-types v) ':
,(signature-type-rest-type/f v))))
(define (make-signature-type rest-type/f . types)
(really-make-signature-type types rest-type/f))
(define <signature-type> signature-type?)
;; *covariant*
;; true if any (list) value satisfying sig1 will also satisfy sig2
(define (signature-subtype? sig1 sig2)
;; look at arg-types first
(let ((rest1 (signature-type-rest-type/f sig1))
(rest2 (signature-type-rest-type/f sig2)))
(let loop ((arg-types1 (signature-type-types sig1))
(arg-types2 (signature-type-types sig2)))
(cond
((null? arg-types1) ; done with arg-types1
(if (null? arg-types2)
;; same # of arg-types
(if rest1
(if rest2
(subtype? rest1 rest2)
#f) ; sig1 has rest, sig2 doesn't
#t) ; sig1 has no rest.
;; more arg-types2 (but done with arg-types1)
(if rest1
(let loop2 ((arg-types2 arg-types2))
(if (null? arg-types2)
(if rest2
(subtype? rest1 rest2)
#f) ; sig1 has rest, sig2 doesn't
(if (subtype? rest1 (car arg-types2))
(loop2 (cdr arg-types2))
#f)))
#f))) ; more arg-types2 but no more rest1
((null? arg-types2) ; more arg1's than arg2's
(if rest2
(let loop2 ((arg-types1 arg-types1))
(if (null? arg-types1)
(if rest1
(subtype? rest1 rest2)
#t)
(if (subtype? (car arg-types1) rest2)
(loop2 (cdr arg-types1))
#f)))
#f)) ; more arg-types1 but no rest2
(else ; more of both
(if (subtype? (car arg-types1) (car arg-types2))
(loop (cdr arg-types1) (cdr arg-types2))
#f))))))
(define (vals-match-signature? orig-vals sig)
(let ((arg-types (signature-type-types sig))
(rest-type/f (signature-type-rest-type/f sig)))
; (format #t " arg-types(~a)=~a, orig-vals(~a)=~a.~%"
; (length arg-types) arg-types (length orig-vals) orig-vals)
; (format #t "too few: ~a. too many: ~a~%"
; (< (length orig-vals) (length arg-types))
; (and (not rest-type/f)
; (> (length orig-vals) (length arg-types))))
;; quick check 1st:
(if (or (< (length orig-vals) (length arg-types)) ; not enuf
(and (not rest-type/f)
(> (length orig-vals) (length arg-types)))) ; too many
#f
;; check types
(let loop ((vals orig-vals)
(types arg-types)
(in-rest? (null? arg-types)))
(cond
((null? vals) ; we already know there are the right #
#t)
(in-rest?
(and (isa? (car vals) rest-type/f)
(loop (cdr vals) '() in-rest?)))
((isa? (car vals) (car types))
(loop (cdr vals) (cdr types)
(null? (cdr types))))
(else
#f))))))
;; TO DO: make signature-equal? a method
(define (signature-equal? s1 s2)
;; (method ((s1 <signature>) (s2 <signature>)) => <boolean>
(list= eq?
(cons (signature-type-rest-type/f s1)
(signature-type-types s1))
(cons (signature-type-rest-type/f s2)
(signature-type-types s2))))
;; try to merge (and together) signature types
(define (glb-signature-types sig-types)
(assert (not (null? sig-types)) "null sig-types to glb-signature-types")
(if (= 1 (length sig-types))
(car sig-types)
(fold glb-signature-type2 (car sig-types) (cdr sig-types))))
;; think "and" ...
(define (glb-signature-type2 sig-type1 sig-type2)
(if (or (bottom-type? sig-type1)
(bottom-type? sig-type2))
<bottom>
;; shorter first (just to avoid duplicating logic later)
;; since glb is symmetric
(let* ((type1 (if (<= (length (signature-type-types sig-type1))
(length (signature-type-types sig-type2)))
sig-type1
sig-type2))
(type2 (if (<= (length (signature-type-types sig-type1))
(length (signature-type-types sig-type2)))
sig-type2
sig-type1))
(types1 (signature-type-types type1))
(types2 (signature-type-types type2))
(rest1 (signature-type-rest-type/f type1))
(rest2 (signature-type-rest-type/f type2)))
(let loop ((types1 types1) (types2 types2) (out-types '()))
(if (null? types1) ; done with arg-types1
(let loop2 ((types2 types2) (out-types out-types))
(if (null? types2)
;; if either has rest-type = #f, then glb has rest-type = #f
(if (or (not rest1) (not rest2))
(really-make-signature-type out-types #f)
;; otherwise, rest is glb of rest types
(let ((rest-glb (types-glb rest1 rest2)))
(if (bottom-type? rest-glb)
<bottom>
(really-make-signature-type out-types rest-glb))))
;; more types in types2
(if (not rest1) ; problem
<bottom>
(let ((glbtype (types-glb (car types2) rest1)))
(if (bottom-type? glbtype)
<bottom>
(loop2 (cdr types2) (cons glbtype out-types)))))))
;; more types1
(let ((glbtype (types-glb (car types1) (car types2))))
(if (bottom-type? glbtype)
<bottom>
(loop (cdr types1) (cdr types2) (cons glbtype out-types)))))))))
;; try to merge (or together) signature types
(define (lub-signature-types sig-types)
(assert (not (null? sig-types)) "null sig-types to lub-signature-types")
(if (= 1 (length sig-types))
sig-types
(fold lub-signature-type2 (car sig-types) (cdr sig-types))))
;; think "or" ...
(define (lub-signature-type2 sig-type1 sig-type2)
(if (or (top-type? sig-type1)
(top-type? sig-type2))
<top>
;; shorter first (just to avoid duplicating logic later)
;; since lub is symmetric
(let* ((type1 (if (<= (length (signature-type-types sig-type1))
(length (signature-type-types sig-type2)))
sig-type1
sig-type2))
(type2 (if (<= (length (signature-type-types sig-type1))
(length (signature-type-types sig-type2)))
sig-type2
sig-type1))
(types1 (signature-type-types type1))
(types2 (signature-type-types type2))
(rest1 (signature-type-rest-type/f type1))
(rest2 (signature-type-rest-type/f type2)))
(let loop ((types1 types1) (types2 types2) (out-types '()))
(if (null? types1) ; done with arg-types1
(let loop2 ((types2 types2) (out-types out-types))
(if (null? types2)
(let ((rest-lub (if rest1
(if rest2
(types-lub rest1 rest2)
rest1)
(if rest2
rest2
#f))))
(really-make-signature-type out-types rest-lub))
;; more types in types2
(loop2 (cdr types2)
(cons (if rest1
(types-lub rest1 (car types2))
(car types2))
out-types))))
;; more types1
(loop (cdr types1) (cdr types2)
(cons (types-lub (car types1) (car types2))
out-types)))))))
;; throw an exception if check fails
(define (check-applicable! type args)
(if (isa? args type)
#t
(error "ck-app!: args ~a don't satisfy method type ~a" args type)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; METHOD TYPES
;;
(define-record-type method-type :method-type
(really-make-method-type args-type result-type)
method-type?
(args-type method-type-args-type)
(result-type method-type-result-type))
;; common case
(define (make-method-type rest-type/f result-type . arg-types)
(really-make-method-type
(really-make-signature-type arg-types rest-type/f)
result-type))
(define-record-discloser :method-type
(lambda (v) `(method-type ,(method-type-args-type v) ->
,(method-type-result-type v))))
(define <method-type> method-type?)
(define-syntax ->
(syntax-rules ()
((-> (?arg-type ...) ?result-type)
(handle-arrow-args
(?arg-type ...) ?result-type ()))))
(define-syntax handle-arrow-args
(syntax-rules (:rest)
((handle-arrow-args (:rest ?rest-type) ?result-type (?arg-type ...))
(really-make-method-type
(really-make-signature-type (reverse (list ?arg-type ...)) ?rest-type)
?result-type))
((handle-arrow-args () ?result-type (?arg-type ...))
(really-make-method-type
(really-make-signature-type (reverse (list ?arg-type ...)) #f)
?result-type))
((handle-arrow-args (?arg-type . ?more-arg-types) ?result-type ?arg-types)
(handle-arrow-args ?more-arg-types ?result-type (?arg-type . ?arg-types)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; CELL TYPES
;;
(define-record-type cell-type :cell-type
(cell-of type)
cell-type?
(type cell-type-type))
(define-record-discloser :cell-type
(lambda (v) `(cell-type ,(cell-type-type v))))
(define <cell-type> cell-type?)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; (subtype-of t)
;;
(define (subtype-of t)
(rcurry subtype? t))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; ISA?
;;
(define (isa? val type)
(cond
((eq? <top> type) #t)
((eq? val *undefined*) #t)
((eq? <bottom> type) #f)
; ((predicate-subtype? type)
; ((predicate-subtype-pred type) val))
((glos-record-type? type)
(and (instance? val)
(or ((glos-record-type-predicate type) val)
(subtype? (instance-type val) type))))
((and-type? type) ; conjunction
(assert (not (null? (and-type-types type))) "Empty and-type in isa?")
(every (curry isa? val) (and-type-types type)))
((or-type? type) ; disjunction
(assert (not (null? (or-type-types type))) "Empty or-type in is-a?-internal")
(any (curry isa? val) (or-type-types type)))
((eq-type? type)
(eq? val (eq-type-val type)))
((signature-type? type)
(cond ((list? val)
(vals-match-signature? val type))
((vector? val)
(vals-match-signature? (vector->list val) type))
(else
#f)))
((method-type? type)
(cond ((generic? val)
(every (rcurry isa? type) (generic-methods val)))
((method? val)
(and
;; use contravariant comparison of arg-types:
(subtype? (method-type-args-type type)
(method-args-type val))
;; and covariant comparison of result-types:
(subtype? (method-result-type val)
(method-type-result-type type))))
(else
#f)))
((cell-type? type)
(cond ((cell? val)
(isa? (cell-ref val) (cell-type-type type)))
(else
#f)))
((callable? type)
(type val))
(else
#f)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; SUBTYPE?
;;
;; true if any instance of t1 can be used in a context requiring a t2
;; that is, true if for every value v, (isa? v t1) => (isa? v t2)
;; - note that predicate types are not comparable.
(define (subtype? t1 t2)
(cond
((eq? t1 t2) #t)
((eq? t2 <top>) #t)
((eq? t1 <top>) #f)
((eq? t1 <bottom>) #t)
((eq? t2 <bottom>) #f)
; ((predicate-subtype? t1)
; (any (rcurry subtype? t2) ; will recurse through all ancestors
; (predicate-subtype-supertypes t1)))
;; (and t1_1 ... t1_n) <= t2 iff
;; 1. if t2 = (and t2_1 ... t2_m), t1 <= t2_i for all t2_i.
;; 2. else some t1_i <= t2.
((and-type? t1)
(if (and-type? t2)
(every (curry subtype? t1) (and-type-types t2))
(any (rcurry subtype? t2) (and-type-types t1))))
((or-type? t1);; (or t1_1 ... t1_n) <= t2 iff all t1_i <= t2
(every (rcurry subtype? t2) (or-type-types t1)))
((eq-type? t1);; (eq v) <= t2 iff v : t2 or t2 : eq(v)
(or (isa? (eq-type-val t1) t2)
(and (eq-type? t2) (equal? (eq-type-val t1) (eq-type-val t2)))))
((and-type? t2);; t1 <= (and t2_1 ... t2_n) iff t1 <= t2_i for all i
(every (curry subtype? t1) (and-type-types t2)))
((or-type? t2);; t1 <= (or t2_1 ... t2_n) iff t1 <= t2_i for some i
(any (curry subtype? t1) (or-type-types t2)))
((signature-type? t1)
(and (signature-type? t2)
(signature-subtype? t1 t2))) ; covariant
((method-type? t1)
(and (method-type? t2)
;; contravariant in the arg types
(subtype? (method-type-args-type t2) (method-type-args-type t1))
;; covariant in result types
(subtype? (method-type-result-type t1)
(method-type-result-type t2))))
((glos-record-type? t1)
(any (rcurry subtype? t2)
(glos-record-type-supers t1)))
((cell-type? t1)
(and (cell-type? t2)
(subtype? (cell-type-type t1)
(cell-type-type t2))))
(else
#f)))
(define <type>
(make-or-type <bottom> <eq-type> <and-type> <or-type>
<signature-type> <cell-type> <method-type>
(lambda (v) (eq? v <top>))))
; predicate-subtype?))
;; TO DO: throw an exception on error
;; returns val on success
(define (check-type! val type)
(if (isa? val type)
val
(error "check-type! failed: ~a ~a" val type)))
(define <false> (== #f))
(define (false-or t)
(make-or-type <false> t))
(define (type-equal? t1 t2)
(or (eq? t1 t2)
(cond
((eq? <top> t1) (eq? <top> t2))
((eq? <bottom> t1) (eq? <bottom> t2))
; ((predicate-subtype? t1) (eq? t1 t2))
((glos-record-type? t1) (eq? t1 t2))
((and-type? t1)
(and (and-type? t2)
(= (length (and-type-types t1)) (length (and-type-types t2)))
(every type-equal? (and-type-types t1) (and-type-types t2))))
((or-type? t1) ; disjunction
(and (or-type? t2)
(= (length (or-type-types t1)) (length (or-type-types t2)))
(every type-equal? (or-type-types t1) (or-type-types t2))))
((eq-type? t1)
(and (eq-type? t2)
(equal? (eq-type-val t1) (eq-type-val t2))))
((signature-type? t1)
(and (signature-type? t2)
(= (length (signature-type-types t1)) (length (signature-type-types t2)))
(every type-equal? (signature-type-types t1) (signature-type-types t2))
;; rest-types are either both #f or both equal types
(or (and (eq? #f (signature-type-rest-type/f t1))
(eq? #f (signature-type-rest-type/f t2)))
(type-equal? (signature-type-rest-type/f t1)
(signature-type-rest-type/f t2)))))
((method-type? t1)
(and (method-type? t2)
(type-equal? (method-type-args-type t1) (method-type-args-type t2))
(type-equal? (method-type-result-type t1) (method-type-result-type t2))))
((cell-type? t1)
(and (cell-type? t2)
(type-equal? (cell-type-type t1) (cell-type-type t2))))
(else
#f))))
(define (types-glb . types)
(assert (not (null? types)) "null types in types-glb")
(fold type2-glb <top> types))
(define (type2-glb t glb)
(cond
((subtype? glb t)
glb)
((subtype? t glb)
t)
(else
(make-and-type t glb))))
(define (types-lub . types)
(assert (not (null? types)) "null types in types-lub")
(fold type2-lub <bottom> types))
(define (type2-lub t lub)
(cond
((subtype? lub t)
t)
((subtype? t lub)
lub)
(else
(make-or-type t lub))))
; eof