nac3-spec/toy-impl/test_subst.py

62 lines
1.6 KiB
Python
Raw Permalink Normal View History

2020-12-17 17:01:31 +08:00
from type_def import *
from inference import *
2020-12-18 13:03:36 +08:00
from helper import *
2020-12-17 17:01:31 +08:00
types = {
'int32': PrimitiveType('int32'),
'int64': PrimitiveType('int64'),
'str': PrimitiveType('str'),
}
2020-12-17 17:09:21 +08:00
i32 = types['int32']
i64 = types['int64']
s = types['str']
2020-12-17 17:01:31 +08:00
variables = {
'X': TypeVariable('X', []),
'Y': TypeVariable('Y', []),
2020-12-17 17:09:21 +08:00
'I': TypeVariable('I', [i32, i64]),
'A': TypeVariable('A', [i32, i64, s]),
2020-12-17 17:01:31 +08:00
}
2020-12-17 17:09:21 +08:00
X = variables['X']
Y = variables['Y']
I = variables['I']
A = variables['A']
2020-12-17 17:01:31 +08:00
def stringify_subst(subst):
if isinstance(subst, str):
return subst
elements = [f"{key}: {str(value)}" for key, value in subst.items()]
return "{" + ', '.join(elements) + "}"
def try_case(a, b, ctx):
result = find_subst(ctx, {}, a, b)
print(f"{a} <- {b} w.r.t. {stringify_subst(ctx)}\n {stringify_subst(result)}\n")
2020-12-17 17:09:21 +08:00
try_case(i32, i32, {})
try_case(i32, i64, {})
try_case(i32, X, {})
try_case(i32, X, {'X': i32})
try_case(i32, X, {'X': i64})
try_case(X, X, {'X': i64})
try_case(X, Y, {'Y': i64})
try_case(X, Y, {'X': i64})
try_case(I, X, {})
try_case(I, A, {})
try_case(A, I, {})
try_case(X, I, {})
try_case(ListType(i32), TupleType([i32]), {})
try_case(TupleType([i32]), ListType(i32), {})
2020-12-18 13:03:36 +08:00
try_case(TupleType([i32, i32]), TupleType([i32]), {})
2020-12-17 17:09:21 +08:00
try_case(ListType(i32), ListType(i32), {})
try_case(TupleType([X, X]), TupleType([X, Y]), {})
try_case(TupleType([X, X]), TupleType([Y, Y]), {})
try_case(TupleType([X, Y]), TupleType([X, X]), {})
2020-12-17 17:21:51 +08:00
try_case(TupleType([X, X]), TupleType([X, X]), {})
2020-12-17 17:09:21 +08:00
try_case(TupleType([X, Y]), X, {})
2020-12-17 17:15:00 +08:00
try_case(TupleType([i32, Y]), X, {})
2020-12-17 17:09:21 +08:00