From 4c9e6c7dc4468c05553b04ba0c1aca6964dc0f82 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Mon, 27 Jul 2020 10:32:49 +0200 Subject: Fix unification and make union types work --- typecheck/CC/Typecheck/Types.hs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'typecheck/CC/Typecheck') diff --git a/typecheck/CC/Typecheck/Types.hs b/typecheck/CC/Typecheck/Types.hs index 3009ca1..dc0740d 100644 --- a/typecheck/CC/Typecheck/Types.hs +++ b/typecheck/CC/Typecheck/Types.hs @@ -14,17 +14,22 @@ import CC.Pretty import CC.Types -data TCError = TypeError SourceRange T.Type T.Type +data TCError = UnifyError SourceRange T.Type T.Type T.Type T.Type (Maybe UnifyReason) | RefError SourceRange Name | TypeArityError SourceRange Name Int Int | DupTypeError Name deriving (Show) +data UnifyReason = URNotInUnion | URAmbiguousWeakening + deriving (Show) + instance Pretty TCError where - pretty (TypeError sr real expect) = + pretty (UnifyError sr real expect unifyt1 unifyt2 mreason) = "Type error: Expression at " ++ pretty sr ++ " has type " ++ pretty real ++ - ", but should have type " ++ pretty expect + ", but should have type " ++ pretty expect ++ + " (when unifying " ++ pretty unifyt1 ++ " and " ++ pretty unifyt2 ++ ")" ++ + maybe "" (\r -> " (reason: " ++ pretty r ++ ")") mreason pretty (RefError sr name) = "Reference error: Variable '" ++ name ++ "' out of scope at " ++ pretty sr pretty (TypeArityError sr name wanted got) = @@ -33,6 +38,10 @@ instance Pretty TCError where pretty (DupTypeError name) = "Duplicate types: Type '" ++ name ++ "' defined multiple times" +instance Pretty UnifyReason where + pretty URNotInUnion = "type not found in union" + pretty URAmbiguousWeakening = "type unifies with multiple items in the union" + type TM a = ExceptT TCError (State Int) a genId :: TM Int -- cgit v1.2.3-70-g09d2