forked from daweibalong/interpreters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
r2-typeinference.ss
216 lines (171 loc) · 4.21 KB
/
r2-typeinference.ss
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
#lang racket
;; ----- data structures -----
(struct Closure (fun env))
;; ----- main code -----
(define r2i
(lambda (exp)
(infer exp env0)))
(define infer
(lambda (exp env)
(match exp
[(? symbol? x)
(let ([v (lookup x env)])
(cond
[(not v)
(error "undefined variable" x)]
[else v]))]
[(? number? x) 'int]
[(? string? x) 'string]
[(? boolean? x) 'bool]
[`(lambda (,x) ,e)
(Closure exp env)]
[`(let ([,x ,e1]) ,e2)
(let ([t1 (infer e1 env)])
(infer e2 (ext-env x t1 env)))]
[`(if ,t ,e1 ,e2)
(let ([tt (infer t env)])
(cond
[(eq? tt 'bool)
(let ([t1 (infer e1 env)]
[t2 (infer e2 env)])
(cond
[(eq? t1 t2) t1]
[else
(failure 'infer
"Return types don't match for branches. \n"
"branch1: " t1 "\n"
"branch2: " t2 "\n")]))]))]
[`(,e1 ,e2)
(let ([t1 (infer e1 env)]
[t2 (infer e2 env)])
(match t1
[(Closure `(lambda (,x) ,e) env-save)
(infer e (ext-env x t2 env-save))]))]
[`(,op ,e1 ,e2)
(let ([t1 (infer e1 env)]
[t2 (infer e2 env)])
(cond
[(memq op '(+ - * /))
(cond
[(not (eq? t1 'int))
(failure 'infer "first operand to operator " op " must be int" "\n"
"but got: " t1)]
[(not (eq? t2 'int))
(failure 'infer "first operand to operator " op " must be int" "\n"
"but got: " t2)]
[else
'int])]
[(memq op '(= < >))
(cond
[(not (eq? t1 'int))
(failure 'infer "first operand to operator " op " must be int" "\n"
"but got: " t1)]
[(not (eq? t2 'int))
(failure 'infer "first operand to operator " op " must be int" "\n"
"but got: " t2)]
[else
'bool])]))])))
;; ----- environment -----
(define env0 '())
(define ext-env
(lambda (x v env)
(cons `(,x . ,v) env)))
(define lookup
(lambda (x env)
(let ([p (assq x env)])
(cond
[(not p) #f]
[else (cdr p)]))))
;; ----- utility -----
(define failure
(lambda (who . args)
(display who) (display ": ")
(for-each display args)
(display "\n")
(error "type check failed")))
;; ----- examples -----
(r2i '(+ 1 2))
;; => 'int
(r2i '(* 2 3))
;; => 'int
(r2i '(* 2 (+ 3 4)))
;; => 'int
(r2i '(* (+ 1 2) (+ 3 4)))
;; => 'int
(r2i '((lambda (x) (* 2 x)) 3))
;; => 'int
(r2i
'(let ([x 2])
(let ([f (lambda (y) (* x y))])
(f 3))))
;; => 'int
(r2i
'(let ([x 2])
(let ([f (lambda (y) (* x y))])
(let ([x "hi"])
(f 3)))))
;; => 'int
(r2i
'(let ([x "hi"])
(let ([f (lambda (y) (* x y))])
(let ([x 4])
(f 3)))))
;; infer: first operand to operator * must be int
;; but got: string
;; type check failed
(r2i
'(let ([f (lambda (x) x)])
(f "hi")))
;; => 'string
(r2i
'(let ([f (lambda (x) x)])
(+ (f 1) (f "hi"))))
;; infer: first operand to operator + must be int
;; but got: string
(r2i
'(let ([f (lambda (x) x)])
(((f f) (f f)) (f 1))))
;; => 'int
(r2i
'(if (< 1 2)
42
"hi"))
;; infer: Return types don't match for branches.
;; branch1: int
;; branch2: string
(r2i
'(if (< 1 2)
"hi"
(if (< 4 3)
100
20)))
;; infer: Return types don't match for branches.
;; branch1: string
;; branch2: int
(r2i
'(let ([f (lambda (x)
(lambda (y)
(if (< x 5)
(+ y 1)
(* y 2))))])
((f 1) 2)))
;; => 'int
(r2i
'(let ([f (lambda (x)
(lambda (y)
(if (< x 5)
(+ y 1)
(* y 2))))])
((f 1) "hi")))
;; infer: first operand to operator + must be int
;; but got: string
(r2i
'(let ([f (lambda (x)
(lambda (y)
(if (< x 5)
(+ y 1)
(> y 2))))])
((f 1) 2)))
;; infer: Return types don't match for branches.
;; branch1: int
;; branch2: bool