@@ -7,9 +7,10 @@
8-- This module implements the type checker
8-- This module implements the type checker
10−module Language.PureScript.TypeChecker.Types (
10+module Language.PureScript.TypeChecker.Types
11+ ( BindingGroupType(..)
15 The following functions represent the corresponding type checking judgements:
16 The following functions represent the corresponding type checking judgements:
@@ -59,14 +60,20 @@ import Language.PureScript.TypeChecker.Unify
59import Language.PureScript.TypeClassDictionaries
60import Language.PureScript.TypeClassDictionaries
60import Language.PureScript.Types
61import Language.PureScript.Types
64+ = RecursiveBindingGroup
65+ | NonRecursiveBindingGroup
66+ deriving (Show, Eq, Ord)
62-- | Infer the types of multiple mutually-recursive values, and return elaborated values including
68-- | Infer the types of multiple mutually-recursive values, and return elaborated values including
63-- type class dictionaries and type annotations.
69-- type class dictionaries and type annotations.
65 (MonadSupply m, MonadState CheckState m, MonadError MultipleErrors m, MonadWriter MultipleErrors m) =>
71 (MonadSupply m, MonadState CheckState m, MonadError MultipleErrors m, MonadWriter MultipleErrors m) =>
66 ModuleName ->
73 ModuleName ->
67 [(Ident, Expr)] ->
74 [(Ident, Expr)] ->
68 m [(Ident, (Expr, Type))]
75 m [(Ident, (Expr, Type))]
69−typesOf moduleName vals = do
76+typesOf bindingGroupType moduleName vals = do
70 tys <- fmap tidyUp . liftUnifyWarnings replace $ do
77 tys <- fmap tidyUp . liftUnifyWarnings replace $ do
71 (untyped, typed, dict, untypedDict) <- typeDictionaryForBindingGroup moduleName vals
78 (untyped, typed, dict, untypedDict) <- typeDictionaryForBindingGroup moduleName vals
72 ds1 <- parU typed $ \e -> checkTypedBindingGroupElement moduleName e dict
79 ds1 <- parU typed $ \e -> checkTypedBindingGroupElement moduleName e dict
@@ -79,14 +86,23 @@ typesOf moduleName vals = do
79 let unsolvedTypeVars = nub $ unknownsInType ty
86 let unsolvedTypeVars = nub $ unknownsInType ty
80 -- Generalize and constrain the type
87 -- Generalize and constrain the type
81 let generalized = generalize unsolved ty
88 let generalized = generalize unsolved ty
82− -- Make sure any unsolved type constraints only use type variables which appear
89+
83− -- unknown in the inferred type.
84 when shouldGeneralize $ do
90 when shouldGeneralize $ do
91+ -- Show the inferred type in a warning
85 tell . errorMessage $ MissingTypeDeclaration ident generalized
92 tell . errorMessage $ MissingTypeDeclaration ident generalized
93+ -- For non-recursive binding groups, can generalize over constraints.
94+ -- For recursive binding groups, we throw an error here for now.
95+ when (bindingGroupType == RecursiveBindingGroup && not (null unsolved))
98+ $ CannotGeneralizeRecursiveFunction ident generalized
99+ -- Make sure any unsolved type constraints only use type variables which appear
100+ -- unknown in the inferred type.
86 forM_ unsolved $ \(_, (className, classTys)) -> do
101 forM_ unsolved $ \(_, (className, classTys)) -> do
87 let constraintTypeVars = nub $ foldMap unknownsInType classTys
102 let constraintTypeVars = nub $ foldMap unknownsInType classTys
88 when (any (`notElem` unsolvedTypeVars) constraintTypeVars) $
103 when (any (`notElem` unsolvedTypeVars) constraintTypeVars) $
89 throwError . errorMessage $ NoInstanceFound className classTys
104 throwError . errorMessage $ NoInstanceFound className classTys
90 -- Check skolem variables did not escape their scope
106 -- Check skolem variables did not escape their scope
91 skolemEscapeCheck val'
107 skolemEscapeCheck val'
92 -- Check rows do not contain duplicate labels
108 -- Check rows do not contain duplicate labels