From 1ac7ba99fc809958ab59ed9b81df6fda7f2dbf05 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Mon, 27 Jul 2020 08:35:57 +0200 Subject: Correct type variable rigidity (I think) --- typecheck/CC/Typecheck.hs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'typecheck/CC/Typecheck.hs') diff --git a/typecheck/CC/Typecheck.hs b/typecheck/CC/Typecheck.hs index 824a714..292eaeb 100644 --- a/typecheck/CC/Typecheck.hs +++ b/typecheck/CC/Typecheck.hs @@ -146,6 +146,14 @@ instantiate (T.TypeScheme bnds ty) = do freshenFrees :: Env -> T.Type -> TM T.Type freshenFrees env = instantiate . generalise env +replaceRigid :: T.Type -> T.Type +replaceRigid (T.TFun t1 t2) = T.TFun (replaceRigid t1) (replaceRigid t2) +replaceRigid T.TInt = T.TInt +replaceRigid (T.TTup ts) = T.TTup (map (replaceRigid) ts) +replaceRigid (T.TNamed n ts) = T.TNamed n (map replaceRigid ts) +replaceRigid (T.TUnion ts) = T.TUnion (Set.map replaceRigid ts) +replaceRigid (T.TyVar _ v) = T.TyVar T.Rigid v + data UnifyContext = UnifyContext SourceRange T.Type T.Type unify :: SourceRange -> T.Type -> T.Type -> TM Subst @@ -207,6 +215,10 @@ infer env expr = case expr of S.Annot sr subex ty -> do (theta1, subex') <- infer env subex ty' <- convertType (envAliases env) sr ty + -- Make sure the type of the subexpression matches the type with rigid + -- variables, then make it instantiable variables instead for the rest + -- of the code. + void $ unify sr (T.exprType subex') (replaceRigid ty') theta2 <- unify sr (T.exprType subex') ty' return (theta2 <> theta1, theta2 >>! subex') -- TODO: quadratic complexity -- cgit v1.2.3-70-g09d2