fixed some inference bugs

This commit is contained in:
pca006132 2020-12-17 17:09:21 +08:00 committed by pca006132
parent 5f2eb1c10c
commit f056f76a76
2 changed files with 36 additions and 15 deletions

View File

@ -36,7 +36,10 @@ def find_subst(ctx: dict[str, Type],
return sub return sub
if isinstance(a, TypeVariable): if isinstance(a, TypeVariable):
return f"{a} can take values other than {b}" if a == b:
return sub
else:
return f"{a} can take values other than {b}"
if isinstance(a, BotType): if isinstance(a, BotType):
return sub return sub

View File

@ -7,13 +7,23 @@ types = {
'str': PrimitiveType('str'), 'str': PrimitiveType('str'),
} }
i32 = types['int32']
i64 = types['int64']
s = types['str']
variables = { variables = {
'X': TypeVariable('X', []), 'X': TypeVariable('X', []),
'Y': TypeVariable('Y', []), 'Y': TypeVariable('Y', []),
'I': TypeVariable('I', [types['int32'], types['int64']]), 'I': TypeVariable('I', [i32, i64]),
'A': TypeVariable('A', [types['int32'], types['int64'], types['str']]), 'A': TypeVariable('A', [i32, i64, s]),
} }
X = variables['X']
Y = variables['Y']
I = variables['I']
A = variables['A']
def stringify_subst(subst): def stringify_subst(subst):
if isinstance(subst, str): if isinstance(subst, str):
return subst return subst
@ -25,16 +35,24 @@ def try_case(a, b, ctx):
print(f"{a} <- {b} w.r.t. {stringify_subst(ctx)}\n {stringify_subst(result)}\n") print(f"{a} <- {b} w.r.t. {stringify_subst(ctx)}\n {stringify_subst(result)}\n")
try_case(types['int32'], types['int32'], {}) try_case(i32, i32, {})
try_case(types['int32'], types['int64'], {}) try_case(i32, i64, {})
try_case(types['int32'], variables['X'], {}) try_case(i32, X, {})
try_case(types['int32'], variables['X'], {'X': types['int32']}) try_case(i32, X, {'X': i32})
try_case(types['int32'], variables['X'], {'X': types['int64']}) try_case(i32, X, {'X': i64})
try_case(variables['X'], variables['X'], {'X': types['int64']}) try_case(X, X, {'X': i64})
try_case(variables['X'], variables['Y'], {'Y': types['int64']}) try_case(X, Y, {'Y': i64})
try_case(variables['X'], variables['Y'], {'X': types['int64']}) try_case(X, Y, {'X': i64})
try_case(variables['I'], variables['X'], {}) try_case(I, X, {})
try_case(variables['I'], variables['A'], {}) try_case(I, A, {})
try_case(variables['A'], variables['I'], {}) try_case(A, I, {})
try_case(variables['X'], variables['I'], {}) try_case(X, I, {})
try_case(ListType(i32), TupleType([i32]), {})
try_case(TupleType([i32]), ListType(i32), {})
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]), {})
try_case(TupleType([X, Y]), X, {})