aboutsummaryrefslogtreecommitdiff
path: root/typecheck/CC/Typecheck.hs
diff options
context:
space:
mode:
Diffstat (limited to 'typecheck/CC/Typecheck.hs')
-rw-r--r--typecheck/CC/Typecheck.hs12
1 files changed, 12 insertions, 0 deletions
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