@@ -110,14 +110,8 @@ unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 +
110110 unifyTypes' (SaturatedTypeSynonym name1 args1) (SaturatedTypeSynonym name2 args2)
111111 | name1 == name2 = zipWithM_ unifyTypes args1 args2
112112 unifyTypes' (SaturatedTypeSynonym name args) ty = do
113− env <- lift getEnv
114− modulePath <- checkModulePath `fmap` lift get
115− case M.lookup (qualify modulePath name) (typeSynonyms env) of
116− Just (synArgs, body) -> do
117− let m = M.fromList $ zip synArgs args
118− let replaced = replaceTypeVars m body
119− replaced `unifyTypes` ty
120− Nothing -> error "Type synonym was not defined"
113+ ty1 <- expandTypeSynonym name args
114+ ty1 `unifyTypes` ty
121115 unifyTypes' ty s@(SaturatedTypeSynonym _ _) = s `unifyTypes` ty
122116 unifyTypes' (ForAll ident1 ty1) (ForAll ident2 ty2) = do
123117 sk <- skolemize ident1 ty1
@@ -247,6 +241,16 @@ desaturateAllTypeSynonyms = everywhere (mkT replace)
247241 replace (SaturatedTypeSynonym name args) = foldl TypeApp (TypeConstructor name) args
248242 replace t = t
249243
244+expandTypeSynonym :: Qualified ProperName -> [Type] -> Subst Check Type
245+expandTypeSynonym name args = do
246+ env <- lift getEnv
247+ modulePath <- checkModulePath `fmap` lift get
248+ case M.lookup (qualify modulePath name) (typeSynonyms env) of
249+ Just (synArgs, body) -> do
250+ let m = M.fromList $ zip synArgs args
251+ return $ replaceTypeVars m body
252+ Nothing -> error "Type synonym was not defined"
253+
250254ensureNoDuplicateProperties :: [(String, Value)] -> Check ()
251255ensureNoDuplicateProperties ps = guardWith "Duplicate property names" $ length (nub . map fst $ ps) == length ps
252256
@@ -573,14 +577,6 @@ check' :: M.Map Ident Type -> Value -> Type -> Subst Check ()
573577check' m val (ForAll idents ty) = do
574578 sk <- skolemize idents ty
575579 check m val sk
576−check' m val (SaturatedTypeSynonym name args) = do
577− env <- lift getEnv
578− modulePath <- checkModulePath `fmap` lift get
579− case M.lookup (qualify modulePath name) (typeSynonyms env) of
580− Just (synArgs, body) -> do
581− let replaced = replaceTypeVars (M.fromList (zip synArgs args)) body
582− check m val replaced
583− Nothing -> error "Type synonym was not defined"
584580check' m val u@(TUnknown _) = do
585581 ty <- infer m val
586582 -- Don't unify an unknown with an inferred polytype
@@ -652,6 +648,9 @@ check' m (Constructor c) ty = do
652648 Just ty1 -> do
653649 repl <- lift $ replaceAllTypeSynonyms ty1
654650 repl `subsumes` ty
651+check' m val (SaturatedTypeSynonym name args) = do
652+ ty <- expandTypeSynonym name args
653+ check m val ty
655654check' _ val ty = throwError $ prettyPrintValue val ++ " does not have type " ++ prettyPrintType ty
656655
657656checkProperties :: M.Map Ident Type -> [(String, Value)] -> Row -> Bool -> Subst Check ()
@@ -713,6 +712,9 @@ checkFunctionApplication' m (ForAll ident ty) args ret = do
713712checkFunctionApplication' m u@(TUnknown _) args ret = do
714713 tyArgs <- mapM (\arg -> infer m arg >>= replaceAllVarsWithUnknowns) args
715714 u ~~ Function tyArgs ret
715+checkFunctionApplication' m (SaturatedTypeSynonym name tyArgs) args ret = do
716+ ty <- expandTypeSynonym name tyArgs
717+ checkFunctionApplication' m ty args ret
716718checkFunctionApplication' _ fnTy args ret = throwError $ "Cannot apply function of type "
717719 ++ prettyPrintType fnTy
718720 ++ " to arguments " ++ intercalate ", " (map prettyPrintValue args)