@@ -7,9 +7,10 @@
77-- |
88-- This module implements the type checker
99--
10−module Language.PureScript.TypeChecker.Types (
11− typesOf
12−) where
10+module Language.PureScript.TypeChecker.Types
11+ ( BindingGroupType(..)
12+ , typesOf
13+ ) where
1314
1415{-
1516 The following functions represent the corresponding type checking judgements:
@@ -59,14 +60,20 @@ import Language.PureScript.TypeChecker.Unify
5960import Language.PureScript.TypeClassDictionaries
6061import Language.PureScript.Types
6162
63+data BindingGroupType
64+ = RecursiveBindingGroup
65+ | NonRecursiveBindingGroup
66+ deriving (Show, Eq, Ord)
67+
6268-- | Infer the types of multiple mutually-recursive values, and return elaborated values including
6369-- type class dictionaries and type annotations.
6470typesOf ::
6571 (MonadSupply m, MonadState CheckState m, MonadError MultipleErrors m, MonadWriter MultipleErrors m) =>
72+ BindingGroupType ->
6673 ModuleName ->
6774 [(Ident, Expr)] ->
6875 m [(Ident, (Expr, Type))]
69−typesOf moduleName vals = do
76+typesOf bindingGroupType moduleName vals = do
7077 tys <- fmap tidyUp . liftUnifyWarnings replace $ do
7178 (untyped, typed, dict, untypedDict) <- typeDictionaryForBindingGroup moduleName vals
7279 ds1 <- parU typed $ \e -> checkTypedBindingGroupElement moduleName e dict
@@ -79,14 +86,23 @@ typesOf moduleName vals = do
7986 let unsolvedTypeVars = nub $ unknownsInType ty
8087 -- Generalize and constrain the type
8188 let generalized = generalize unsolved ty
82− -- Make sure any unsolved type constraints only use type variables which appear
83− -- unknown in the inferred type.
89+
8490 when shouldGeneralize $ do
91+ -- Show the inferred type in a warning
8592 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))
96+ . throwError
97+ . errorMessage
98+ $ CannotGeneralizeRecursiveFunction ident generalized
99+ -- Make sure any unsolved type constraints only use type variables which appear
100+ -- unknown in the inferred type.
86101 forM_ unsolved $ \(_, (className, classTys)) -> do
87102 let constraintTypeVars = nub $ foldMap unknownsInType classTys
88103 when (any (`notElem` unsolvedTypeVars) constraintTypeVars) $
89104 throwError . errorMessage $ NoInstanceFound className classTys
105+
90106 -- Check skolem variables did not escape their scope
91107 skolemEscapeCheck val'
92108 -- Check rows do not contain duplicate labels