@@ -109,7 +109,7 @@ unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 +
109 unifyTypes' (TypeConstructor c1) (TypeConstructor c2) = do
109 unifyTypes' (TypeConstructor c1) (TypeConstructor c2) = do
110 env <- getEnv
110 env <- getEnv
111 Just moduleName <- checkCurrentModule <$> get
111 Just moduleName <- checkCurrentModule <$> get
112− guardWith ("Cannot unify " ++ show c1 ++ " with " ++ show c2 ++ ".") (typeConstructorsAreEqual env moduleName c1 c2)
112+ guardWith ("Cannot unify " ++ show c1 ++ " with " ++ show c2 ++ ".") (c1 == c2)
113 unifyTypes' (TypeApp t3 t4) (TypeApp t5 t6) = do
113 unifyTypes' (TypeApp t3 t4) (TypeApp t5 t6) = do
114 t3 `unifyTypes` t5
114 t3 `unifyTypes` t5
115 t4 `unifyTypes` t6
115 t4 `unifyTypes` t6
@@ -153,12 +153,6 @@ unifyRows r1 r2 =
153 unifyRows' [] (Skolem s1 _) [] (Skolem s2 _) | s1 == s2 = return ()
153 unifyRows' [] (Skolem s1 _) [] (Skolem s2 _) | s1 == s2 = return ()
154 unifyRows' sd3 r3 sd4 r4 = throwError $ "Cannot unify (" ++ prettyPrintRow (rowFromList (sd3, r3)) ++ ") with (" ++ prettyPrintRow (rowFromList (sd4, r4)) ++ ")."
154 unifyRows' sd3 r3 sd4 r4 = throwError $ "Cannot unify (" ++ prettyPrintRow (rowFromList (sd3, r3)) ++ ") with (" ++ prettyPrintRow (rowFromList (sd4, r4)) ++ ")."
157−-- Ensure type constructors are equal after canonicalization
159−typeConstructorsAreEqual :: Environment -> ModuleName -> Qualified ProperName -> Qualified ProperName -> Bool
160−typeConstructorsAreEqual env moduleName = (==) `on` canonicalizeType moduleName env
163-- Infer the types of multiple mutually-recursive values, and return elaborated values including
157-- Infer the types of multiple mutually-recursive values, and return elaborated values including
164-- type class dictionaries and type annotations.
158-- type class dictionaries and type annotations.
@@ -271,7 +265,7 @@ entails moduleName context goal@(className, tys) = do
271 -- Choose type class dictionaries in scope in the current module
265 -- Choose type class dictionaries in scope in the current module
272 , filterModule tcd
266 , filterModule tcd
273 -- Make sure the type class name matches the one we are trying to satisfy
267 -- Make sure the type class name matches the one we are trying to satisfy
274− , typeConstructorsAreEqual env moduleName className' (tcdClassName tcd)
268+ , className' == tcdClassName tcd
275 -- Make sure the type unifies with the type in the type instance definition
269 -- Make sure the type unifies with the type in the type instance definition
276 , subst <- maybeToList . (>>= verifySubstitution) . fmap concat $ zipWithM (typeHeadsAreEqual moduleName env) tys' (tcdInstanceTypes tcd)
270 , subst <- maybeToList . (>>= verifySubstitution) . fmap concat $ zipWithM (typeHeadsAreEqual moduleName env) tys' (tcdInstanceTypes tcd)
277 -- Solve any necessary subgoals
271 -- Solve any necessary subgoals
@@ -294,8 +288,6 @@ entails moduleName context goal@(className, tys) = do
294 filterModule (TypeClassDictionaryInScope { tcdName = Qualified (Just mn) _ }) | mn == moduleName = True
288 filterModule (TypeClassDictionaryInScope { tcdName = Qualified (Just mn) _ }) | mn == moduleName = True
295 filterModule (TypeClassDictionaryInScope { tcdName = Qualified Nothing _ }) = True
289 filterModule (TypeClassDictionaryInScope { tcdName = Qualified Nothing _ }) = True
296 filterModule _ = False
290 filterModule _ = False
297− -- Resolve a type class dictionary in scope to an actual value by following any (TCDAlias) pointers
298− -- which originated from module imports
299 canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
291 canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
300 canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
292 canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
301 canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm
293 canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm
@@ -314,7 +306,7 @@ typeHeadsAreEqual :: ModuleName -> Environment -> Type -> Type -> Maybe [(String
314typeHeadsAreEqual _ _ (Skolem s1 _) (Skolem s2 _) | s1 == s2 = Just []
306typeHeadsAreEqual _ _ (Skolem s1 _) (Skolem s2 _) | s1 == s2 = Just []
315typeHeadsAreEqual _ _ (TypeVar v) t = Just [(v, t)]
307typeHeadsAreEqual _ _ (TypeVar v) t = Just [(v, t)]
316typeHeadsAreEqual _ _ t (TypeVar v) = Just [(v, t)]
308typeHeadsAreEqual _ _ t (TypeVar v) = Just [(v, t)]
317−typeHeadsAreEqual m e (TypeConstructor c1) (TypeConstructor c2) | typeConstructorsAreEqual e m c1 c2 = Just []
309+typeHeadsAreEqual m e (TypeConstructor c1) (TypeConstructor c2) | c1 == c2 = Just []
318typeHeadsAreEqual m e (TypeApp h1 (TypeVar v)) (TypeApp h2 arg) = (:) (v, arg) <$> typeHeadsAreEqual m e h1 h2
310typeHeadsAreEqual m e (TypeApp h1 (TypeVar v)) (TypeApp h2 arg) = (:) (v, arg) <$> typeHeadsAreEqual m e h1 h2
319typeHeadsAreEqual m e t1@(TypeApp _ _) t2@(TypeApp _ (TypeVar _)) = typeHeadsAreEqual m e t2 t1
311typeHeadsAreEqual m e t1@(TypeApp _ _) t2@(TypeApp _ (TypeVar _)) = typeHeadsAreEqual m e t2 t1
320typeHeadsAreEqual m e (SaturatedTypeSynonym name args) t2 = case expandTypeSynonym' e m name args of
312typeHeadsAreEqual m e (SaturatedTypeSynonym name args) t2 = case expandTypeSynonym' e m name args of
@@ -419,7 +411,7 @@ instantiatePolyTypeWithUnknowns val (ConstrainedType constraints ty) = do
419 Just moduleName <- checkCurrentModule <$> get
411 Just moduleName <- checkCurrentModule <$> get
420 dicts <- getTypeClassDictionaries
412 dicts <- getTypeClassDictionaries
421 (_, ty') <- instantiatePolyTypeWithUnknowns (error "Types under a constraint cannot themselves be constrained") ty
413 (_, ty') <- instantiatePolyTypeWithUnknowns (error "Types under a constraint cannot themselves be constrained") ty
422− return (foldl App val (map (flip TypeClassDictionary dicts) (qualifyAllUnqualifiedNames moduleName env constraints)), ty')
414+ return (foldl App val (map (flip TypeClassDictionary dicts) constraints), ty')
423instantiatePolyTypeWithUnknowns val ty = return (val, ty)
415instantiatePolyTypeWithUnknowns val ty = return (val, ty)
@@ -437,7 +429,7 @@ replaceVarWithUnknown ident ty = do
437replaceAllTypeSynonyms' :: (D.Data d) => Environment -> ModuleName -> d -> Either String d
429replaceAllTypeSynonyms' :: (D.Data d) => Environment -> ModuleName -> d -> Either String d
438replaceAllTypeSynonyms' env moduleName d =
430replaceAllTypeSynonyms' env moduleName d =
440− syns = map (\((path, name), (args, _)) -> ((path, name), length args)) . M.toList $ typeSynonyms env
432+ syns = map (\(name, (args, _)) -> (name, length args)) . M.toList $ typeSynonyms env
442 saturateAllTypeSynonyms env moduleName syns d
434 saturateAllTypeSynonyms env moduleName syns d
@@ -461,7 +453,7 @@ desaturateAllTypeSynonyms = everywhere (mkT replaceSaturatedTypeSynonym)
462expandTypeSynonym' :: Environment -> ModuleName -> Qualified ProperName -> [Type] -> Either String Type
454expandTypeSynonym' :: Environment -> ModuleName -> Qualified ProperName -> [Type] -> Either String Type
463expandTypeSynonym' env moduleName name args =
455expandTypeSynonym' env moduleName name args =
464− case M.lookup (canonicalizeType moduleName env name) (typeSynonyms env) of
456+ case M.lookup name (typeSynonyms env) of
465 Just (synArgs, body) -> do
457 Just (synArgs, body) -> do
466 let repl = replaceAllTypeVars (zip synArgs args) body
458 let repl = replaceAllTypeVars (zip synArgs args) body
467 replaceAllTypeSynonyms' env moduleName repl
459 replaceAllTypeSynonyms' env moduleName repl
@@ -540,15 +532,15 @@ infer' (Var var) = do
540 ConstrainedType constraints ty' -> do
532 ConstrainedType constraints ty' -> do
541 env <- getEnv
533 env <- getEnv
542 dicts <- getTypeClassDictionaries
534 dicts <- getTypeClassDictionaries
543− return $ TypedValue True (foldl App (Var var) (map (flip TypeClassDictionary dicts) (qualifyAllUnqualifiedNames moduleName env constraints))) ty'
535+ return $ TypedValue True (foldl App (Var var) (map (flip TypeClassDictionary dicts) constraints)) ty'
544 _ -> return $ TypedValue True (Var var) ty
536 _ -> return $ TypedValue True (Var var) ty
545infer' v@(Constructor c) = do
537infer' v@(Constructor c) = do
546 env <- getEnv
538 env <- getEnv
547 Just moduleName <- checkCurrentModule <$> get
539 Just moduleName <- checkCurrentModule <$> get
548− case M.lookup (qualify moduleName c) (dataConstructors env) of
540+ case M.lookup c (dataConstructors env) of
549 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
541 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
550− Just (ty, _) -> do ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty
542+ Just ty -> do ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty
551− return $ TypedValue True v ty'
543+ return $ TypedValue True v ty'
552infer' (Case vals binders) = do
544infer' (Case vals binders) = do
553 ts <- mapM infer vals
545 ts <- mapM infer vals
554 ret <- fresh
546 ret <- fresh
@@ -596,8 +588,8 @@ inferBinder val (VarBinder name) = return $ M.singleton name val
596inferBinder val (ConstructorBinder ctor binders) = do
588inferBinder val (ConstructorBinder ctor binders) = do
597 env <- getEnv
589 env <- getEnv
598 Just moduleName <- checkCurrentModule <$> get
590 Just moduleName <- checkCurrentModule <$> get
599− case M.lookup (qualify moduleName ctor) (dataConstructors env) of
591+ case M.lookup ctor (dataConstructors env) of
600− Just (ty, _) -> do
592+ Just ty -> do
601 (_, fn) <- instantiatePolyTypeWithUnknowns (error "Data constructor types cannot contains constraints") ty
593 (_, fn) <- instantiatePolyTypeWithUnknowns (error "Data constructor types cannot contains constraints") ty
602 go binders fn
594 go binders fn
@@ -640,18 +632,18 @@ inferBinder val (NamedBinder name binder) = do
641-- Check the types of the return values in a set of binders in a case statement
633-- Check the types of the return values in a set of binders in a case statement
643−checkBinders :: [Type] -> Type -> [([Binder], Maybe Guard, Value)] -> UnifyT Type Check [([Binder], Maybe Guard, Value)]
635+checkBinders :: [Type] -> Type -> [CaseAlternative] -> UnifyT Type Check [CaseAlternative]
644checkBinders _ _ [] = return []
636checkBinders _ _ [] = return []
645−checkBinders nvals ret ((binders, grd, val):bs) = do
637+checkBinders nvals ret (CaseAlternative binders grd val : bs) = do
646 Just moduleName <- checkCurrentModule <$> get
638 Just moduleName <- checkCurrentModule <$> get
647 m1 <- M.unions <$> zipWithM inferBinder nvals binders
639 m1 <- M.unions <$> zipWithM inferBinder nvals binders
648 r <- bindLocalVariables moduleName (M.toList m1) $ do
640 r <- bindLocalVariables moduleName (M.toList m1) $ do
649 val' <- TypedValue True <$> check val ret <*> pure ret
641 val' <- TypedValue True <$> check val ret <*> pure ret
650 case grd of
642 case grd of
651− Nothing -> return (binders, Nothing, val')
643+ Nothing -> return $ CaseAlternative binders Nothing val'
652 Just g -> do
644 Just g -> do
653 g' <- check g tyBoolean
645 g' <- check g tyBoolean
654− return (binders, Just g', val')
646+ return $ CaseAlternative binders (Just g') val'
655 rs <- checkBinders nvals ret bs
647 rs <- checkBinders nvals ret bs
656 return $ r : rs
648 return $ r : rs
@@ -725,8 +717,7 @@ check' val t@(ConstrainedType constraints ty) = do
725 return $ Ident $ "__dict_" ++ className ++ "_" ++ show n
717 return $ Ident $ "__dict_" ++ className ++ "_" ++ show n
726 val' <- withTypeClassDictionaries (zipWith (\name (className, instanceTy) ->
718 val' <- withTypeClassDictionaries (zipWith (\name (className, instanceTy) ->
727 TypeClassDictionaryInScope name className instanceTy Nothing TCDRegular) (map (Qualified Nothing) dictNames)
719 TypeClassDictionaryInScope name className instanceTy Nothing TCDRegular) (map (Qualified Nothing) dictNames)
728− (qualifyAllUnqualifiedNames moduleName env constraints)) $
729− check val ty
720+ constraints) $ check val ty
730 return $ TypedValue True (foldr (Abs . Left) val' dictNames) t
721 return $ TypedValue True (foldr (Abs . Left) val' dictNames) t
731check' val (SaturatedTypeSynonym name args) = do
722check' val (SaturatedTypeSynonym name args) = do
732 ty <- introduceSkolemScope <=< expandTypeSynonym name $ args
723 ty <- introduceSkolemScope <=< expandTypeSynonym name $ args
@@ -806,9 +797,9 @@ check' (Accessor prop val) ty = do
806check' (Constructor c) ty = do
797check' (Constructor c) ty = do
807 env <- getEnv
798 env <- getEnv
808 Just moduleName <- checkCurrentModule <$> get
799 Just moduleName <- checkCurrentModule <$> get
809− case M.lookup (qualify moduleName c) (dataConstructors env) of
800+ case M.lookup c (dataConstructors env) of
810 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
801 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
811− Just (ty1, _) -> do
802+ Just ty1 -> do
812 repl <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty1
803 repl <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty1
813 _ <- subsumes Nothing repl ty
804 _ <- subsumes Nothing repl ty
814 return $ TypedValue True (Constructor c) ty
805 return $ TypedValue True (Constructor c) ty
@@ -886,7 +877,7 @@ checkFunctionApplication' fn (ConstrainedType constraints fnTy) arg = do
886 env <- getEnv
877 env <- getEnv
887 dicts <- getTypeClassDictionaries
878 dicts <- getTypeClassDictionaries
888 Just moduleName <- checkCurrentModule <$> get
879 Just moduleName <- checkCurrentModule <$> get
889− checkFunctionApplication' (foldl App fn (map (flip TypeClassDictionary dicts) (qualifyAllUnqualifiedNames moduleName env constraints))) fnTy arg
880+ checkFunctionApplication' (foldl App fn (map (flip TypeClassDictionary dicts) constraints)) fnTy arg
890checkFunctionApplication' _ fnTy arg = throwError $ "Cannot apply a function of type "
881checkFunctionApplication' _ fnTy arg = throwError $ "Cannot apply a function of type "
891 ++ prettyPrintType fnTy
882 ++ prettyPrintType fnTy
892 ++ " to argument " ++ prettyPrintValue arg
883 ++ " to argument " ++ prettyPrintValue arg
@@ -932,7 +923,7 @@ subsumes' (Just val) (ConstrainedType constraints ty1) ty2 = do
932 dicts <- getTypeClassDictionaries
923 dicts <- getTypeClassDictionaries
933 Just moduleName <- checkCurrentModule <$> get
924 Just moduleName <- checkCurrentModule <$> get
934 _ <- subsumes' Nothing ty1 ty2
925 _ <- subsumes' Nothing ty1 ty2
935− return . Just $ foldl App val (map (flip TypeClassDictionary dicts) (qualifyAllUnqualifiedNames moduleName env constraints))
926+ return . Just $ foldl App val (map (flip TypeClassDictionary dicts) constraints)
936subsumes' val (Object r1) (Object r2) = do
927subsumes' val (Object r1) (Object r2) = do
938 (ts1, r1') = rowToList r1
929 (ts1, r1') = rowToList r1