blob: e2ff375dead379e97f87a8e2a2ae57e8712004fc (
plain)
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
|
(use-modules (ice-9 format)
(ice-9 match)
(srfi srfi-1)
(srfi srfi-9)
(srfi srfi-9 gnu))
(define-syntax-rule (define-matcher (name rules ...) body ...)
(define name
(match-lambda*
((rules ...) body ...)
(_ 'unmatched))))
(define-syntax-rule (compose-matchers matcher ...)
(lambda args
(let loop ((matchers (list matcher ...)))
(match matchers
(() (error "unmatched" args))
((m . rest)
(let ((result (apply m args)))
(if (eq? result 'unmatched)
(loop rest)
result)))))))
;;;
;;; Typed expression annotation
;;;
(define-record-type <primitive-type>
(make-primitive-type name)
primitive-type?
(name primitive-type-name))
(define (display-primitive-type type port)
(format port "#<primitive ~a>" (primitive-type-name type)))
(set-record-type-printer! <primitive-type> display-primitive-type)
(define int-type (make-primitive-type 'int))
(define float-type (make-primitive-type 'float))
(define bool-type (make-primitive-type 'bool))
(define-record-type <type-variable>
(make-type-variable name)
type-variable?
(name type-variable-name))
(define (display-type-variable type port)
(format port "#<tvar ~a>" (type-variable-name type)))
(set-record-type-printer! <type-variable> display-type-variable)
(define unique-counter (make-parameter 0))
(define (unique-number)
(let ((n (unique-counter)))
(unique-counter (+ n 1))
n))
(define* (unique-identifier #:optional (prefix 'T))
(string->symbol
(format #f "~a~a" prefix (unique-number))))
(define (fresh-type-variable)
(make-type-variable (unique-identifier)))
(define-record-type <procedure-type>
(make-procedure-type arg-types return-type)
procedure-type?
(arg-types procedure-type-arg-types)
(return-type procedure-type-return-type))
(define (display-procedure-type type port)
(format port "#<proc-type ~a → ~a>"
(procedure-type-arg-types type)
(procedure-type-return-type type)))
(set-record-type-printer! <procedure-type> display-procedure-type)
(define (type? obj)
(or (primitive-type? obj)
(type-variable? obj)
(procedure-type? obj)))
(define (top-level-env)
'())
(define (lookup var env)
(or (assq-ref env var)
(error "unbound variable" var)))
(define (make-texp type exp)
`(t ,type ,exp))
(define (texp? obj)
(match obj
(('t _ _) #t)
(_ #f)))
(define (texp-type texp)
(match texp
(('t type _) type)))
(define (texp-exp texp)
(match texp
(('t _ exp) exp)))
(define (display-typed-expression texp port)
(format port "#<texp ~a ~a>" (texp-type texp) (texp-exp texp)))
(set-record-type-printer! <typed-expression> display-typed-expression)
(define-matcher (annotate:bool (? boolean? b) env)
(make-texp bool-type b))
(define-matcher (annotate:int (and (? number?) (? exact-integer? n)) env)
(make-texp int-type n))
(define-matcher (annotate:float (and (? number?) (? inexact? n)) env)
(make-texp float-type n))
(define-matcher (annotate:var (? symbol? var) env)
(make-texp (lookup var env) var))
(define-matcher (annotate:if ('if predicate consequent alternate) env)
(make-texp (fresh-type-variable)
`(if ,(annotate-exp predicate env)
,(annotate-exp consequent env)
,(annotate-exp alternate env))))
(define-matcher (annotate:lambda ('lambda ((? symbol? args) ...) body) env)
(define arg-types (map (lambda (_name) (fresh-type-variable)) args))
(define env* (append (map cons args arg-types) env))
(define return-type (fresh-type-variable))
(make-texp (make-procedure-type arg-types return-type)
`(lambda ,args ,(annotate-exp body env*))))
(define-matcher (annotate:call (operator args ...) env)
(make-texp (fresh-type-variable)
(cons (annotate-exp operator env)
(map (lambda (arg) (annotate-exp arg env)) args))))
(define annotate-exp
(compose-matchers annotate:bool
annotate:int
annotate:float
annotate:var
annotate:if
annotate:lambda
annotate:call))
(define (annotate-exp* exp)
(parameterize ((unique-counter 0))
(annotate-exp exp (top-level-env))))
;;;
;;; Constraints
;;;
(define-record-type <constraint>
(constrain lhs rhs)
constraint?
(lhs constraint-lhs)
(rhs constraint-rhs))
(define (display-constraint constraint port)
(format port "#<constraint ~a = ~a>"
(constraint-lhs constraint)
(constraint-rhs constraint)))
(set-record-type-printer! <constraint> display-constraint)
(define-matcher (constrain:other (? type? _) _) '())
(define-matcher (constrain:if (? type? type)
('if (? texp? predicate)
(? texp? consequent)
(? texp? alternate)))
(append (list (constrain bool-type (texp-type predicate))
(constrain type (texp-type consequent))
(constrain type (texp-type alternate)))
(program-constraints predicate)
(program-constraints consequent)
(program-constraints alternate)))
(define-matcher (constrain:lambda (? procedure-type? type)
('lambda ((? symbol? args) ...)
(? texp? body)))
(cons (constrain (procedure-type-return-type type)
(texp-type body))
(program-constraints body)))
(define-matcher (constrain:call (? type? type)
((? texp? operator) (? texp? operands) ...))
(cons (constrain (texp-type operator)
(make-procedure-type (map texp-type operands)
type))
(append (program-constraints operator)
(append-map program-constraints operands))))
(define %program-constraints
(compose-matchers constrain:if
constrain:lambda
constrain:call
constrain:other))
(define (program-constraints texp)
(%program-constraints (texp-type texp) (texp-exp texp)))
;;;
;;; Unification
;;;
(define-matcher (occurs:default _ _)
#f)
(define-matcher (occurs:variable (? type-variable? a) (? type-variable? b))
(eq? a b))
(define-matcher (occurs:procedure (? type-variable? v) (? procedure-type? p))
(or (any (lambda (arg-type)
(occurs-in? v arg-type))
(procedure-type-arg-types p))
(occurs-in? v (procedure-type-return-type p))))
(define occurs-in?
(compose-matchers occurs:variable
occurs:procedure
occurs:default))
(define (substitute-term term dict)
(match dict
(() term)
(((from . to) . rest)
(if (eq? term from)
to
(substitute-term term rest)))))
(define (substitute-dict var term dict)
(map (match-lambda
((a . b)
(cons (if (eq? a var) term a)
(if (eq? b var) term b))))
dict))
(define (substitute var term dict)
(let ((term* (substitute-term term dict)))
(and (not (occurs-in? var term*))
(alist-cons var term* (substitute-dict var term* dict)))))
(define (maybe-substitute var-first terms)
(lambda (dict succeed fail)
(match var-first
((var . rest-vars)
(match terms
((term . rest-terms)
(cond
;; Tautology: matching 2 vars that are the same.
((and (type-variable? term) (eq? var term))
(succeed dict fail rest-vars rest-terms))
;; Variable has been bound to some other value, recursively
;; follow it and unify.
((assq-ref dict var) =>
(lambda (type)
((unify:dispatch (cons type rest-vars) terms)
dict succeed fail)))
;; Substitute variable for value.
(else
(let ((dict* (substitute var term dict)))
(if dict*
(succeed dict* fail rest-vars rest-terms)
(fail)))))))))))
(define (constant? obj)
(and (not (type-variable? obj))
(not (procedure-type? obj))
(not (list? obj))))
(define-matcher (unify:fail a b)
(define (fail-unifier dict succeed fail)
(fail))
fail-unifier)
(define-matcher (unify:constants ((? constant? a) . rest-a) ((? constant? b) . rest-b))
(define (constant-unifier dict succeed fail)
(if (eqv? a b)
(begin
(succeed dict fail rest-a rest-b))
(fail)))
constant-unifier)
(define-matcher (unify:lists (a . rest-a) (b . rest-b))
(define (list-unifier dict succeed fail)
((unify:dispatch a b)
dict
(lambda (dict* fail* _null-a _null-b)
(succeed dict* fail* rest-a rest-b))
fail))
list-unifier)
(define-matcher (unify:variable-left (and ((? type-variable? _) . _) a) b)
(maybe-substitute a b))
(define-matcher (unify:variable-right a (and ((? type-variable? _) . _) b))
(maybe-substitute b a))
(define-matcher (unify:procedures ((? procedure-type? a) . rest-a)
((? procedure-type? b) . rest-b))
(define (procedure-unifier dict succeed fail)
((unify:dispatch (cons (procedure-type-return-type a)
(procedure-type-arg-types a))
(cons (procedure-type-return-type b)
(procedure-type-arg-types b)))
dict
(lambda (dict* fail* _null-a _null-b)
(succeed dict* fail* rest-a rest-b))
fail))
procedure-unifier)
(define %unify:dispatch
(compose-matchers unify:constants
unify:variable-left
unify:variable-right
unify:procedures
unify:lists
unify:fail))
(define (unify:dispatch a b)
(define (dispatcher dict succeed fail)
(if (and (null? a) (null? b))
(succeed dict fail a b)
((%unify:dispatch a b)
dict
(lambda (dict* fail* rest-a rest-b)
((unify:dispatch rest-a rest-b)
dict* succeed fail*))
fail)))
dispatcher)
(define (%unify a b dict succeed)
((unify:dispatch (list a) (list b))
dict
(lambda (dict fail rest-a rest-b)
(or (and (null? rest-a) (null? rest-b) (succeed dict))
(fail)))
(lambda ()
#f)))
(define (unify a b)
(%unify a b '() identity))
(define (unify-constraints constraints)
(unify (map constraint-lhs constraints)
(map constraint-rhs constraints)))
;;;
;;; Type Resolution
;;;
(define (primitive? x)
(or (number? x)
(boolean? x)
(symbol? x)))
(define-matcher (resolve:primitive (? primitive? x) dict)
x)
(define-matcher (resolve:primitive-type (? primitive-type? type) dict)
type)
(define-matcher (resolve:type-variable (? type-variable? var) dict)
(let ((type (assq-ref dict var)))
(if (or (not type) (type-variable? type))
(error "cannot determine type" var)
type)))
(define-matcher (resolve:procedure-type (? procedure-type? type) dict)
(make-procedure-type (resolve-types (procedure-type-arg-types type) dict)
(resolve-types (procedure-type-return-type type) dict)))
(define-matcher (resolve:list (exps ...) dict)
(map (lambda (texp) (resolve-types texp dict)) exps))
(define resolve-types
(compose-matchers resolve:primitive
resolve:primitive-type
resolve:type-variable
resolve:procedure-type
resolve:list))
(define (infer-types exp)
(let* ((texp (annotate-exp* exp))
(constraints (program-constraints texp))
(substitutions (unify-constraints constraints)))
(unless substitutions
(error "type mismatch" texp))
(resolve-types texp substitutions)))
(infer-types #t)
(infer-types 6)
(infer-types 6.5)
(infer-types '(if #t 1 2))
(infer-types '((lambda (x) x) 1))
(infer-types '((lambda (f) (if (f #t) (f 1) (f 2))) (lambda (x) x)))
|