nac3-spec/toy-impl/test_subst.py

41 lines
1.3 KiB
Python
Raw Normal View History

2020-12-17 17:01:31 +08:00
from type_def import *
from inference import *
types = {
'int32': PrimitiveType('int32'),
'int64': PrimitiveType('int64'),
'str': PrimitiveType('str'),
}
variables = {
'X': TypeVariable('X', []),
'Y': TypeVariable('Y', []),
'I': TypeVariable('I', [types['int32'], types['int64']]),
'A': TypeVariable('A', [types['int32'], types['int64'], types['str']]),
}
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")
try_case(types['int32'], types['int32'], {})
try_case(types['int32'], types['int64'], {})
try_case(types['int32'], variables['X'], {})
try_case(types['int32'], variables['X'], {'X': types['int32']})
try_case(types['int32'], variables['X'], {'X': types['int64']})
try_case(variables['X'], variables['X'], {'X': types['int64']})
try_case(variables['X'], variables['Y'], {'Y': types['int64']})
try_case(variables['X'], variables['Y'], {'X': types['int64']})
try_case(variables['I'], variables['X'], {})
try_case(variables['I'], variables['A'], {})
try_case(variables['A'], variables['I'], {})
try_case(variables['X'], variables['I'], {})