From f056f76a7693534a26c352781423cbe005e95a4c Mon Sep 17 00:00:00 2001 From: pca006132 Date: Thu, 17 Dec 2020 17:09:21 +0800 Subject: [PATCH] fixed some inference bugs --- toy-impl/inference.py | 5 ++++- toy-impl/test_subst.py | 46 +++++++++++++++++++++++++++++------------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/toy-impl/inference.py b/toy-impl/inference.py index 8159201..b77f3fa 100644 --- a/toy-impl/inference.py +++ b/toy-impl/inference.py @@ -36,7 +36,10 @@ def find_subst(ctx: dict[str, Type], return sub 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): return sub diff --git a/toy-impl/test_subst.py b/toy-impl/test_subst.py index 451d07f..7662195 100644 --- a/toy-impl/test_subst.py +++ b/toy-impl/test_subst.py @@ -7,13 +7,23 @@ types = { 'str': PrimitiveType('str'), } +i32 = types['int32'] +i64 = types['int64'] +s = types['str'] + variables = { 'X': TypeVariable('X', []), 'Y': TypeVariable('Y', []), - 'I': TypeVariable('I', [types['int32'], types['int64']]), - 'A': TypeVariable('A', [types['int32'], types['int64'], types['str']]), + 'I': TypeVariable('I', [i32, i64]), + 'A': TypeVariable('A', [i32, i64, s]), } +X = variables['X'] +Y = variables['Y'] +I = variables['I'] +A = variables['A'] + + def stringify_subst(subst): if isinstance(subst, str): 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") -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'], {}) +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), {}) +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, {}) +