-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_affine_checker.py
executable file
·363 lines (274 loc) · 12.5 KB
/
test_affine_checker.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
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
import pytest
import language as lang
import typecheck_errors as tc_err
from affine_checker import AffineTypeChecker as ATC
import dsl_types as dslT
from env import TypeCheckEnv, deepcopy_env
from dsl_parser import dsl_parse
def base_tcheck_env():
return TypeCheckEnv(defaults=lang.builtin_fn_types)
def test_reference():
prog = dsl_parse("("
"(defvar x (un val int) 0)"
"(defvar xref (un ref (un val int)) (mkref x))"
"xref"
")")
env = base_tcheck_env()
T = ATC.type_check(env, prog)
assert isinstance(T, dslT.RefType)
assert T.referenced_type() == lang.T_INT
def test_reference_no_effect():
prog = dsl_parse("("
"(defvar x (un val int) 0)"
"(mkref x)"
")")
env = base_tcheck_env()
with pytest.raises(tc_err.ReferenceNoEffectError):
ATC.type_check(env, prog)
def test_env_copy_works():
prog = dsl_parse("( "
" (defvar x (un val int) 0) "
" (defvar y (un val int) 0) "
" (while (apply < x 3) 0 ((set x (apply + x 1)) x))"
")")
env = base_tcheck_env()
ATC.type_check(env, prog, descope=False)
env_cp = deepcopy_env(env)
assert env == env_cp
def test_unrestricted_variables():
# Declaring an unrestricted value works...
prog = dsl_parse("(defvar x (un val int) 3)")
env = base_tcheck_env()
ret_type = ATC.type_check(env, prog)
assert ret_type == lang.T_UNIT
assert env.get_bind_val('x') == dslT.tparse(dsl_parse("(un val int)"))
# But you're not allowed to defvar a name twice, even with same type
prog = dsl_parse("((defvar x (un val int) 3) (defvar x (un val int) 4))")
env = base_tcheck_env()
with pytest.raises(tc_err.BindingRedefinitionError):
ATC.type_check(env, prog)
# Unrestricted values can be used multiple times
prog = dsl_parse("((defvar x (un val int) 3) x x)")
env = base_tcheck_env()
ret_type = ATC.type_check(env, prog)
assert ret_type == lang.T_INT
# Can turn an unrestricted value into an affine one
prog = dsl_parse("((defvar x (aff val int) 3))")
env = base_tcheck_env()
ret_type = ATC.type_check(env, prog)
assert ret_type == lang.T_UNIT
# But you can't make an affine value unrestricted
prog = dsl_parse("((defvar x (aff val int) 3) (defvar y (un val int) x))")
env = base_tcheck_env()
with pytest.raises(tc_err.TypeMismatchError):
ATC.type_check(env, prog)
def test_affine_variables():
# An affine value can be used once
prog = dsl_parse("((defvar x (aff val int) 3) x)")
env = base_tcheck_env()
ret_type = ATC.type_check(env, prog)
assert dslT.Tmod.less_restrictive(lang.T_INT.tmod, ret_type.tmod)
# ...but not twice
prog = dsl_parse("((defvar x (aff val int) 3) x x)")
env = base_tcheck_env()
with pytest.raises(tc_err.LinAffineVariableReuseError):
ATC.type_check(env, prog)
# Can't circumvent this through use of variables either...
prog = dsl_parse("((defvar x (aff val int) 3) (defvar y (aff val int) x) x)")
env = base_tcheck_env()
with pytest.raises(tc_err.LinAffineVariableReuseError):
ATC.type_check(env, prog)
def test_linear_variables():
################
# Linear stuff #
################
# Linear variables must be used *exactly* once, so just declaring it won't work
prog = dsl_parse("((defvar x (lin val int) 3))")
env = base_tcheck_env()
with pytest.raises(tc_err.UnusedLinVariableError):
# We have to make sure that the type-checker knows top-level environment will be descoped
ATC.type_check(env, prog, descope=True)
# But declaring and then using it actually will
prog = dsl_parse("((defvar x (lin val int) 3) (apply + 42 x))")
env = base_tcheck_env()
ATC.type_check(env, prog, descope=True)
# Declaring and then using it twice, on the other hand, is a big no-no
prog = dsl_parse("((defvar x (lin val int) 3) (apply + 42 x) (apply + 42 x))")
env = base_tcheck_env()
with pytest.raises(tc_err.LinAffineVariableReuseError):
ATC.type_check(env, prog, descope=True)
def test_function_typechecking_correct_args():
prog = dsl_parse("(apply + 3 3)")
ret_type = ATC.type_check(base_tcheck_env(), prog)
assert ret_type == lang.T_INT
def test_plus_type_checks_incorrect_args():
prog = dsl_parse("(apply + true 3)")
with pytest.raises(tc_err.TypeMismatchError):
ATC.type_check(base_tcheck_env(), prog)
def test_variables_redeclare():
with pytest.raises(tc_err.BindingRedefinitionError):
prog = dsl_parse("((defvar x (un val int) 3) (defvar x (un val int) 3) )")
ATC.type_check(base_tcheck_env(), prog)
def test_decl_var1():
prog = dsl_parse("((defvar x (un val int) 3) (defvar y (un val int) 3) )")
ATC.type_check(base_tcheck_env(), prog)
def test_decl_var2():
prog = dsl_parse("((defvar x (un val int) 3) (set x 3))")
t = ATC.type_check(base_tcheck_env(), prog)
assert t == lang.T_UNIT
def test_decl_var3():
prog = dsl_parse("((defvar x (un val int) 3) (set x 3) (set x 4))")
t = ATC.type_check(base_tcheck_env(), prog)
assert t == lang.T_UNIT
# assert ctx['x'] == lang.tparse(parse("(aff ref (un val int))"))
def test_decl_var4():
prog = dsl_parse("((defvar x (un val int) 3) (set x 3) (set x (+ x 1)))")
t = ATC.type_check(base_tcheck_env(), prog)
assert t == lang.T_UNIT
# assert ctx['x'] == tparse(parse("(aff ref (un val int))"))
def test_unused_linint_errs():
prog = dsl_parse("(defvar x (lin val int) 3)")
with pytest.raises(tc_err.UnusedLinVariableError):
ATC.type_check(base_tcheck_env(), prog, descope=True)
def test_unused_affint_fine():
prog = dsl_parse("(defvar x (aff val int) 3)")
ATC.type_check(base_tcheck_env(), prog, descope=True)
def test_oneuse_affint_fine():
prog = dsl_parse("((defvar x (aff val int) 3) x)")
ATC.type_check(base_tcheck_env(), prog, descope=True)
def test_twouse_affint_errs():
prog = dsl_parse("((defvar x (aff val int) 3) x x)")
with pytest.raises(tc_err.LinAffineVariableReuseError):
ATC.type_check(base_tcheck_env(), prog, descope=True)
def test_if1():
prog = dsl_parse("(if true (defvar x (un val int) 3) x)")
with pytest.raises(tc_err.BindingUndefinedError):
ATC.type_check(base_tcheck_env(), prog, descope=True)
def test_if2():
prog = dsl_parse("((defvar x (un val int) 3) (if true (set x (apply + x 1)) (set x (apply - x 1))))")
ATC.type_check(base_tcheck_env(), prog, descope=True)
def test_if_lin():
prog = dsl_parse("((defvar x (lin val int) 3) (if true (set x (apply + x 1)) (set x (apply + x 0))) (apply + x 1))")
ATC.type_check(base_tcheck_env(), prog, descope=True)
def test_if_lin2():
prog = dsl_parse("((defvar x (lin val int) 3) (if true ((apply + x 1) (apply + x 1)) (apply + x 2)))")
with pytest.raises(tc_err.LinAffineVariableReuseError):
ATC.type_check(base_tcheck_env(), prog)
def test_if_lin3():
prog = dsl_parse("((defvar x (lin val int) 3) (if true (3) (apply + x 2)))")
with pytest.raises(tc_err.EnvironmentMismatchError):
ATC.type_check(base_tcheck_env(), prog)
def test_while():
prog = dsl_parse("((defvar x (un val int) 0) (while (apply < x 3) -2 ((set x (apply + x 1)) x)))")
ATC.type_check(base_tcheck_env(), prog, descope=False)
def test_fun():
prog = dsl_parse("((defvar x (un val int) 3)"
"(defun foo (un val bool) ((y (un val bool))) y )"
"(apply foo x)"
"x)")
with pytest.raises(tc_err.TypeMismatchError):
ATC.type_check(base_tcheck_env(), prog)
def test_nested_fun():
prog = dsl_parse("( (defun add-3 (un val int) ((x (un val int))) (apply + x 3)) "
"(defun add-4 (un val int) ((x (un val int))) (apply add-3 (apply + x 1)))"
"(apply add-4 6) )")
ATC.type_check(base_tcheck_env(), prog)
def test_ref_fun():
prog = dsl_parse("((defvar x (un val int) 3)"
"(defvar xref (un ref (un val int)) (mkref x)) "
"(defun foo (un val unit) ((y (un ref (un val int)))) (setrefval y (apply + (deref y) 1)))) )"
"(apply foo xref)"
"x)")
ATC.type_check(base_tcheck_env(), prog)
def test_scope_descopes():
prog = dsl_parse("(3"
"(scope (defvar x (lin val int) 3)))"
")")
with pytest.raises(tc_err.UnusedLinVariableError):
ATC.type_check(base_tcheck_env(), prog)
def test_reference_borrows():
prog = dsl_parse("("
"(defvar x (un val int) 3)"
"(defvar xref (un ref (un val int)) (mkref x))"
"x"
")")
with pytest.raises(tc_err.LinAffineVariableReuseError):
ATC.type_check(base_tcheck_env(), prog)
def test_reference_borrow_under_scope():
prog = dsl_parse("("
"(defvar x (un val int) 3)"
"(scope "
" (defvar xref (un ref (un val int)) (mkref x))"
")"
"x"
")")
ATC.type_check(base_tcheck_env(), prog)
def test_ref_borrow_lin():
prog = dsl_parse("( (defvar x (lin val int) 3)"
"(scope "
" (defvar xref (un ref (lin val int)) (mkref x))"
"))")
with pytest.raises(tc_err.UnusedLinVariableError):
ATC.type_check(base_tcheck_env(), prog, descope=True)
def test_ref_borrow_lin2():
prog = dsl_parse("( (defvar x (lin val int) 3)"
" (defvar xref (un ref (lin val int)) (mkref x))"
")")
with pytest.raises(tc_err.UnusedLinVariableError):
ATC.type_check(base_tcheck_env(), prog, descope=True)
def test_ref_borrow3():
prog = dsl_parse("( (defvar x (lin val int) 3)"
" (defvar xref (un ref (lin val int)) (mkref x))"
" (defvar xref2 (un ref (lin val int)) xref)"
")")
with pytest.raises(tc_err.UnusedLinVariableError):
ATC.type_check(base_tcheck_env(), prog, descope=True)
def test_ref_borrow4():
prog = dsl_parse("( (defvar x (lin val int) 3)"
" (scope (defvar xref (un ref (lin val int)) (mkref x))"
" (defvar xref2 (un ref (lin val int)) xref))"
"(apply + 3 x))")
ATC.type_check(base_tcheck_env(), prog)
def test_nonexistent_ref():
prog = dsl_parse("((defvar x (lin val int) 3) (defvar badref (un ref (un val int)) (mkref y))))")
with pytest.raises(tc_err.BindingUndefinedError):
ATC.type_check(base_tcheck_env(), prog)
def test_working_with_files():
prog = dsl_parse("("
"(defvar f (lin val file) (apply fopen 123))"
")")
with pytest.raises(tc_err.UnusedLinVariableError):
ATC.type_check(base_tcheck_env(), prog, descope=True)
prog = dsl_parse("("
"(defvar f (lin val file) (apply fopen 123))"
"(apply fclose f)"
")")
ATC.type_check(base_tcheck_env(), prog, descope=True)
prog = dsl_parse("("
"(defvar f (lin val file) (apply fopen 123))"
"(scope (defvar fref (un ref (lin val file)) (mkref f))"
" (apply fwrite fref 100)"
")"
"(apply fclose f)"
")")
ATC.type_check(base_tcheck_env(), prog, descope=True)
prog = dsl_parse("("
"(defvar f (lin val file) (apply fopen 123))"
"(scope (defvar fref (un ref (lin val file)) (mkref f))"
" (apply fwrite fref 100)"
")"
"(apply fclose f)"
")")
ATC.type_check(base_tcheck_env(), prog, descope=True)
def test_recursive_fun():
prog = dsl_parse("("
"(defun fib (un val int) ((n (un val int))) "
" (defvar ret (un val int) 0) "
" (if (apply = 0 n) (set ret 0) "
" (if (apply = 1 n) (set ret 1) "
" (set ret (apply + (apply fib (apply - n 1)) (apply fib (apply - n 2)) ))))"
" ret"
")"
"(apply fib 0)"
")")
ATC.type_check(base_tcheck_env(), prog)