-
Notifications
You must be signed in to change notification settings - Fork 1
/
type_check_Lany.py
207 lines (196 loc) · 7.17 KB
/
type_check_Lany.py
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
import ast
from ast import *
from type_check_Llambda import TypeCheckLlambda
from utils import *
import typing
class TypeCheckLany(TypeCheckLlambda):
def check_type_equal(self, t1, t2, e):
if t1 == Bottom() or t2 == Bottom():
return
match t1:
case AnyType():
return
case FunctionType(ps1, rt1):
match t2:
case FunctionType(ps2, rt2):
for (p1, p2) in zip(ps1, ps2):
self.check_type_equal(p1, p2, e)
self.check_type_equal(rt1, rt2, e)
case _:
if isinstance(t2, AnyType):
# t2 was AnyType we can match any
return
raise Exception('error: ' + repr(t1) + ' != ' + repr(t2) \
+ ' in ' + repr(e))
case _:
if isinstance(t2, AnyType):
return
super().check_type_equal(t1, t2, e)
def type_check_exp(self, e, env):
match e:
case Inject(value, typ):
self.check_exp(value, typ, env)
return AnyType()
case Project(value, typ):
self.check_exp(value, AnyType(), env)
return typ
case Call(Name('any_tuple_load'), [tup, index]):
self.check_exp(tup, AnyType(), env)
return AnyType()
case Call(Name('any_len'), [tup]):
self.check_exp(tup, AnyType(), env)
return IntType()
case Call(Name('arity'), [fun]):
ty = self.type_check_exp(fun, env)
match ty:
case FunctionType(ps, rt):
return IntType()
case TupleType([FunctionType(ps,rs)]):
return IntType()
case _:
raise Exception('type_check_exp arity unexpected ' + repr(ty))
case Call(Name('make_any'), [value, tag]):
self.type_check_exp(value, env)
self.check_exp(tag, IntType(), env)
return AnyType()
case ValueOf(value, typ):
self.check_exp(value, AnyType(), env)
return typ
case TagOf(value):
self.check_exp(value, AnyType(), env)
return IntType()
case Call(Name('exit'), []):
return Bottom()
case AnnLambda(params, returns, body):
new_env = {x:t for (x,t) in env.items()}
for (x,t) in params:
new_env[x] = t
return_t = self.type_check_exp(body, new_env)
self.check_type_equal(returns, return_t, e)
return FunctionType([t for (x,t) in params], return_t)
case Constant(value) if isinstance(value, TagOf):
# return self.type_check_exp(value, env)
return IntType()
case _:
return super().type_check_exp(e, env)
def type_check_stmts(self, ss, env):
if len(ss) == 0:
return
match ss[0]:
case Assign([v], value) if isinstance(v, Name):
t = self.type_check_exp(value, env)
if v.id in env:
self.check_type_equal(env[v.id], t, value)
else:
env[v.id] = t
v.has_type = env[v.id]
return self.type_check_stmts(ss[1:], env)
case Expr(Call(Name('print'), [arg])):
t = self.type_check_exp(arg, env)
self.check_type_equal(t, AnyType(), arg) # we make all return is AnyType now
return self.type_check_stmts(ss[1:], env)
case _:
return super().type_check_stmts(ss, env)
def check_exp(self, e, ty, env):
match e:
case Lambda(params, body):
e.has_type = ty
if isinstance(params, ast.arguments):
new_params = [a.arg for a in params.args]
e.args = new_params
else:
new_params = params
match ty:
case FunctionType(params_t, return_t):
new_env = {x: t for (x, t) in env.items()}
for (p, t) in zip(new_params, params_t):
new_env[p] = t
self.check_exp(body, return_t, new_env)
case Bottom():
pass
case _:
raise Exception('lambda does not have type ' + str(ty))
case _:
t = self.type_check_exp(e, env)
self.check_type_equal(t, ty, e)
# t = self.type_check_exp(e, env)
# self.check_type_equal(t, ty, e)
def type_check(self, p):
#trace('*** type check Llambda')
match p:
case Module(body):
env = {}
for s in body:
match s:
case FunctionDef(name, params, bod, dl, returns, comment):
if isinstance(params, ast.arguments):
params_t = [self.parse_type_annot(p.annotation) \
for p in params.args]
returns_t = self.parse_type_annot(returns)
else:
params_t = [t for (x,t) in params]
returns_t = returns
env[name] = FunctionType(params_t, returns_t)
self.check_stmts(body, AnyType(), env)
case _:
raise Exception('type_check: unexpected ' + repr(p))
def check_stmts(self, ss, return_ty, env):
if len(ss) == 0:
return
#trace('*** check_stmts ' + repr(ss[0]) + '\n')
match ss[0]:
case FunctionDef(name, params, body, dl, returns, comment):
#trace('*** tc_check ' + name)
new_env = {x: t for (x,t) in env.items()}
if isinstance(params, ast.arguments):
new_params = [(p.arg, self.parse_type_annot(p.annotation)) for p in params.args]
ss[0].args = new_params
new_returns = self.parse_type_annot(returns)
ss[0].returns = new_returns
else:
new_params = params
new_returns = returns
for (x,t) in new_params:
new_env[x] = t
rt = self.check_stmts(body, new_returns, new_env)
self.check_stmts(ss[1:], return_ty, env)
case Return(value):
#trace('** tc_check return ' + repr(value))
self.check_exp(value, return_ty, env)
case Assign([v], value) if isinstance(v, Name):
if v.id in env:
self.check_exp(value, env[v.id], env)
else:
t = self.type_check_exp(value, env)
env[v.id] = t
if isinstance(value, Inject) and isinstance(value.typ, FunctionType):
env[v.id] = value.typ #
elif isinstance(value, Call):
if isinstance(value.args[0], AnnLambda):
env[v.id] = value.args[0].convert_to_typ()# pass
v.has_type = env[v.id]
# trace(env)
self.check_stmts(ss[1:], return_ty, env)
case Assign([Subscript(tup, Constant(index), Store())], value):
tup_t = self.type_check_exp(tup, env)
match tup_t:
case TupleType(ts):
self.check_exp(value, ts[index], env)
case Bottom():
pass
case _:
raise Exception('check_stmts: expected a tuple, not ' \
+ repr(tup_t))
self.check_stmts(ss[1:], return_ty, env)
case AnnAssign(v, ty, value, simple) if isinstance(v, Name):
ty_annot = self.parse_type_annot(ty)
ss[0].annotation = ty_annot
if v.id in env:
self.check_type_equal(env[v.id], ty_annot)
else:
env[v.id] = ty_annot
v.has_type = env[v.id]
self.check_exp(value, ty_annot, env)
self.check_stmts(ss[1:], return_ty, env)
case _:
self.type_check_stmts(ss, env)