@@ -67,24 +67,17 @@ import Language.PureScript.TypeChecker.Synonyms
67import Language.PureScript.TypeChecker.Unify
67import Language.PureScript.TypeChecker.Unify
68import Language.PureScript.TypeClassDictionaries
68import Language.PureScript.TypeClassDictionaries
69import Language.PureScript.Types
69import Language.PureScript.Types
70−import qualified Language.PureScript.Constants as C
73-- Infer the types of multiple mutually-recursive values, and return elaborated values including
72-- Infer the types of multiple mutually-recursive values, and return elaborated values including
74-- type class dictionaries and type annotations.
73-- type class dictionaries and type annotations.
76−typesOf :: Maybe ModuleName -> ModuleName -> [(Ident, Expr)] -> Check [(Ident, (Expr, Type))]
75+typesOf :: ModuleName -> [(Ident, Expr)] -> Check [(Ident, (Expr, Type))]
77−typesOf mainModuleName moduleName vals = do
76+typesOf moduleName vals = do
78 tys <- fmap tidyUp . liftUnifyWarnings replace $ do
77 tys <- fmap tidyUp . liftUnifyWarnings replace $ do
79 (untyped, typed, dict, untypedDict) <- typeDictionaryForBindingGroup moduleName vals
78 (untyped, typed, dict, untypedDict) <- typeDictionaryForBindingGroup moduleName vals
80− ds1 <- parU typed $ \e -> do
81− triple@(_, (_, ty)) <- checkTypedBindingGroupElement moduleName e dict
79+ ds1 <- parU typed $ \e -> checkTypedBindingGroupElement moduleName e dict
84− ds2 <- forM untyped $ \e -> do
85− triple@(_, (_, ty)) <- typeForBindingGroupElement e dict untypedDict
80+ ds2 <- forM untyped $ \e -> typeForBindingGroupElement e dict untypedDict
88 return $ ds1 ++ ds2
81 return $ ds1 ++ ds2
90 forM tys $ \(ident, (val, ty)) -> do
83 forM tys $ \(ident, (val, ty)) -> do
@@ -94,21 +87,13 @@ typesOf mainModuleName moduleName vals = do
94 skolemEscapeCheck val'
87 skolemEscapeCheck val'
95 -- Check rows do not contain duplicate labels
88 -- Check rows do not contain duplicate labels
96 checkDuplicateLabels val'
89 checkDuplicateLabels val'
97− -- Remove type synonyms placeholders, and replace
98− -- top-level unification variables with named type variables.
99− let val'' = overTypes desaturateAllTypeSynonyms val'
100− ty' = varIfUnknown . desaturateAllTypeSynonyms $ ty
101− return (ident, (val'', ty'))
90+ return (ident, (val', varIfUnknown ty))
103 -- Apply the substitution that was returned from runUnify to both types and (type-annotated) values
92 -- Apply the substitution that was returned from runUnify to both types and (type-annotated) values
104 tidyUp (ts, sub) = map (\(i, (val, ty)) -> (i, (overTypes (sub $?) val, sub $? ty))) ts
93 tidyUp (ts, sub) = map (\(i, (val, ty)) -> (i, (overTypes (sub $?) val, sub $? ty))) ts
105 -- Replace all the wildcards types with their inferred types
94 -- Replace all the wildcards types with their inferred types
106 replace sub (SimpleErrorWrapper (WildcardInferredType ty)) = SimpleErrorWrapper $ WildcardInferredType (sub $? ty)
95 replace sub (SimpleErrorWrapper (WildcardInferredType ty)) = SimpleErrorWrapper $ WildcardInferredType (sub $? ty)
107 replace _ em = em
96 replace _ em = em
108− -- If --main is enabled, need to check that `main` has type Eff eff a for some eff, a
109− checkMain nm ty = when (Just moduleName == mainModuleName && nm == Ident C.main) $ do
110− [eff, a] <- replicateM 2 fresh
111− ty =?= TypeApp (TypeApp (TypeConstructor (Qualified (Just (ModuleName [ProperName "Control", ProperName "Monad", ProperName "Eff"])) (ProperName "Eff"))) eff) a
113type TypeData = M.Map (ModuleName, Ident) (Type, NameKind, NameVisibility)
98type TypeData = M.Map (ModuleName, Ident) (Type, NameKind, NameVisibility)
@@ -182,9 +167,7 @@ replaceTypeClassDictionaries mn =
182 let (_, f, _) = everywhereOnValuesTopDownM return go return
167 let (_, f, _) = everywhereOnValuesTopDownM return go return
185− go (TypeClassDictionary constraint dicts) = do
170+ go (TypeClassDictionary constraint dicts) = entails mn dicts constraint
187− entails env mn dicts constraint
188 go other = return other
171 go other = return other
@@ -346,9 +329,6 @@ inferProperty :: Type -> String -> UnifyT Type Check (Maybe Type)
346inferProperty (TypeApp obj row) prop | obj == tyObject = do
329inferProperty (TypeApp obj row) prop | obj == tyObject = do
347 let (props, _) = rowToList row
330 let (props, _) = rowToList row
348 return $ lookup prop props
331 return $ lookup prop props
349−inferProperty (SaturatedTypeSynonym name args) prop = do
350− replaced <- introduceSkolemScope <=< expandTypeSynonym name $ args
351− inferProperty replaced prop
352inferProperty (ForAll ident ty _) prop = do
332inferProperty (ForAll ident ty _) prop = do
353 replaced <- replaceVarWithUnknown ident ty
333 replaced <- replaceVarWithUnknown ident ty
354 inferProperty replaced prop
334 inferProperty replaced prop
@@ -493,9 +473,6 @@ check' val t@(ConstrainedType constraints ty) = do
494 instantiateSuperclass :: [String] -> [Type] -> [Type] -> [Type]
474 instantiateSuperclass :: [String] -> [Type] -> [Type] -> [Type]
495 instantiateSuperclass args supArgs tys = map (replaceAllTypeVars (zip args tys)) supArgs
475 instantiateSuperclass args supArgs tys = map (replaceAllTypeVars (zip args tys)) supArgs
496−check' val (SaturatedTypeSynonym name args) = do
497− ty <- introduceSkolemScope <=< expandTypeSynonym name $ args
499check' val u@(TUnknown _) = do
476check' val u@(TUnknown _) = do
500 val'@(TypedValue _ _ ty) <- infer val
477 val'@(TypedValue _ _ ty) <- infer val
501 -- Don't unify an unknown with an inferred polytype
478 -- Don't unify an unknown with an inferred polytype
@@ -599,9 +576,6 @@ check' v@(Constructor c) ty = do
599check' (Let ds val) ty = do
576check' (Let ds val) ty = do
600 (ds', val') <- inferLetBinding [] ds val (`check` ty)
577 (ds', val') <- inferLetBinding [] ds val (`check` ty)
601 return $ TypedValue True (Let ds' val') ty
578 return $ TypedValue True (Let ds' val') ty
602−check' val ty | containsTypeSynonyms ty = do
603− ty' <- introduceSkolemScope <=< expandAllTypeSynonyms <=< replaceTypeWildcards $ ty
605check' val kt@(KindedType ty kind) = do
579check' val kt@(KindedType ty kind) = do
606 checkTypeKind ty kind
580 checkTypeKind ty kind
607 val' <- check' val ty
581 val' <- check' val ty
@@ -615,12 +589,6 @@ check' val ty = do
615 Nothing -> throwError . errorMessage $ SubsumptionCheckFailed
589 Nothing -> throwError . errorMessage $ SubsumptionCheckFailed
616 Just v' -> return $ TypedValue True v' ty
590 Just v' -> return $ TypedValue True v' ty
618−containsTypeSynonyms :: Type -> Bool
619−containsTypeSynonyms = everythingOnTypes (||) go where
620− go (SaturatedTypeSynonym _ _) = True
625-- Check the type of a collection of named record fields
593-- Check the type of a collection of named record fields
@@ -683,9 +651,6 @@ checkFunctionApplication' fn u@(TUnknown _) arg ret = do
683 ret' <- maybe fresh return ret
651 ret' <- maybe fresh return ret
684 u =?= function ty ret'
652 u =?= function ty ret'
685 return (ret', App fn arg')
653 return (ret', App fn arg')
686−checkFunctionApplication' fn (SaturatedTypeSynonym name tyArgs) arg ret = do
687− ty <- introduceSkolemScope <=< expandTypeSynonym name $ tyArgs
688− checkFunctionApplication fn ty arg ret
689checkFunctionApplication' fn (KindedType ty _) arg ret =
654checkFunctionApplication' fn (KindedType ty _) arg ret =
690 checkFunctionApplication fn ty arg ret
655 checkFunctionApplication fn ty arg ret
691checkFunctionApplication' fn (ConstrainedType constraints fnTy) arg ret = do
656checkFunctionApplication' fn (ConstrainedType constraints fnTy) arg ret = do