-
Notifications
You must be signed in to change notification settings - Fork 0
/
styping.go
274 lines (235 loc) · 5.96 KB
/
styping.go
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
/*
* Inference for a simply typed extended λ-calculus.
*
* By comparison with the incomplete typing.go / typing_test.go,
* which targets a polymorphic (à la System F) λ-calculus.
*
* Essentially, the idea is that we don't have to deal with VarType.
*/
package main
import (
"fmt"
"reflect"
)
// map a bound variable name to its type
type Ctx map[string]Type
// Infer the types for a given expression and perform
// typechecking when relevant.
//
// We modify (and return) the expression in place
// so that it contains the relevant typing data.
func inferSType(x Expr) (Expr, error) {
var aux func(Expr, Ctx) (Expr, error)
aux = func(x Expr, ctx Ctx) (Expr, error) {
var err error
switch x.(type) {
// Those four cases have already been typed
// during the parsing.
case *IntExpr:
case *FloatExpr:
case *BoolExpr:
case *UnitExpr:
// We may need some typechecking here
case *UnaryExpr:
r := x.(*UnaryExpr).right
if r, err = aux(r, ctx); err != nil {
return nil, err
}
switch x.(*UnaryExpr).op {
// Right must be int
case tokenMinus:
fallthrough
case tokenPlus:
if _, rok := r.getType().(*IntType); !rok {
return nil, fmt.Errorf("%s : int → int; got %s",
x.(*UnaryExpr).op, r.getType(),
)
}
x.setType(&IntType{typ{}})
// Right must be float
case tokenFMinus:
fallthrough
case tokenFPlus:
if _, rok := r.getType().(*FloatType); !rok {
return nil, fmt.Errorf("%s : float → float; got %s",
x.(*UnaryExpr).op, r.getType(),
)
}
x.setType(&FloatType{typ{}})
// Right must be bool
case tokenExcl:
if _, rok := r.getType().(*BoolType); !rok {
return nil, fmt.Errorf("%s : bool → bool; got %s",
x.(*UnaryExpr).op, r.getType(),
)
}
x.setType(&BoolType{typ{}})
default:
panic("assert")
}
// Again, we may need to typecheck things here
case *BinaryExpr:
l := x.(*BinaryExpr).left
r := x.(*BinaryExpr).right
if l, err = aux(l, ctx); err != nil {
return nil, err
}
if r, err = aux(r, ctx); err != nil {
return nil, err
}
// NOTE/TODO: maybe generics can help here
// (quick test yields an issue with the setType(T{typ{}}))
switch x.(*BinaryExpr).op {
// (int×int) → int
case tokenMinus:
fallthrough
case tokenPlus:
fallthrough
case tokenStar:
fallthrough
case tokenSlash:
_, lok := l.getType().(*IntType)
_, rok := r.getType().(*IntType)
if !lok || !rok {
return nil, fmt.Errorf("%s : (int×int) → int; got (%s×%s)",
x.(*BinaryExpr).op, l.getType(), r.getType(),
)
}
x.setType(&IntType{typ{}})
// (int×int) → bool
case tokenLessEq:
fallthrough
case tokenMoreEq:
fallthrough
case tokenLess:
fallthrough
case tokenMore:
_, lok := l.getType().(*IntType)
_, rok := r.getType().(*IntType)
if !lok || !rok {
return nil, fmt.Errorf("%s : (int×int) → bool; got (%s×%s)",
x.(*BinaryExpr).op, l.getType(), r.getType(),
)
}
x.setType(&BoolType{typ{}})
// (float×float) → float
case tokenFMinus:
fallthrough
case tokenFPlus:
fallthrough
case tokenFStar:
fallthrough
case tokenFSlash:
_, lok := l.getType().(*FloatType)
_, rok := r.getType().(*FloatType)
if !lok || !rok {
return nil, fmt.Errorf("%s : (float×float) → float; got (%s×%s)",
x.(*BinaryExpr).op, l.getType(), r.getType(),
)
}
x.setType(&FloatType{typ{}})
// (float×float) → bool
case tokenFLessEq:
fallthrough
case tokenFMoreEq:
fallthrough
case tokenFLess:
fallthrough
case tokenFMore:
_, lok := l.getType().(*FloatType)
_, rok := r.getType().(*FloatType)
if !lok || !rok {
return nil, fmt.Errorf("%s : (float×float) → float; got (%s×%s)",
x.(*BinaryExpr).op, l.getType(), r.getType(),
)
}
x.setType(&BoolType{typ{}})
// Left/right must be bools
case tokenOrOr:
fallthrough
case tokenAndAnd:
_, lok := l.getType().(*BoolType)
_, rok := r.getType().(*BoolType)
if !lok || !rok {
return nil, fmt.Errorf("%s : (bool×bool) → bool; got (%s×%s)",
x.(*BinaryExpr).op, l.getType(), r.getType(),
)
}
x.setType(&BoolType{typ{}})
default:
panic("assert")
}
x.(*BinaryExpr).left = l
x.(*BinaryExpr).right = r
case *AbsExpr:
n := x.(*AbsExpr).name
t := x.(*AbsExpr).typ
r := x.(*AbsExpr).right
// save previous ctx[n] if any
t2, ok := ctx[n]
// new var in env
ctx[n] = t
if r, err = aux(r, ctx); err != nil {
return nil, err
}
x.setType(&ArrowType{typ{}, t, r.getType()})
x.(*AbsExpr).right = r
if ok {
ctx[n] = t2
} else {
delete(ctx, n)
}
case *AppExpr:
l := x.(*AppExpr).left
r := x.(*AppExpr).right
if l, err = aux(l, ctx); err != nil {
return nil, err
}
if r, err = aux(r, ctx); err != nil {
return nil, err
}
tl, ok := l.getType().(*ArrowType)
if !ok {
return nil, fmt.Errorf("Trying to apply to non-arrow: '%s'", l.getType())
}
if reflect.TypeOf(tl.left) != reflect.TypeOf(r.getType()) {
return nil, fmt.Errorf("Can't apply '%s' to '%s'", r.getType(), l.getType())
}
x.setType(l.getType().(*ArrowType).right)
x.(*AppExpr).left = l
x.(*AppExpr).right = r
case *VarExpr:
n := x.(*VarExpr).name
t, ok := ctx[n]
if !ok {
return nil, fmt.Errorf("'%s' isn't bounded!", n)
}
x.setType(t)
return x, nil
case *ProductExpr:
l := x.(*ProductExpr).left
r := x.(*ProductExpr).right
if l, err = aux(l, ctx); err != nil {
return nil, err
}
if r, err = aux(r, ctx); err != nil {
return nil, err
}
x.setType(&ProductType{typ{}, l.getType(), r.getType()})
x.(*ProductExpr).left = l
x.(*ProductExpr).right = r
default:
panic("assert")
}
return x, nil
}
return aux(x, Ctx{})
}
// To ease tests so far
func mustSType(x Expr) Expr {
y, err := inferSType(x)
if err != nil {
panic(err)
}
return y
}