1414--
1515-----------------------------------------------------------------------------
1616
17+ {-# LANGUAGE FlexibleContexts #-}
18+
1719module Language.PureScript.Sugar.BindingGroups (
1820 createBindingGroups ,
1921 createBindingGroupsModule ,
@@ -24,8 +26,9 @@ module Language.PureScript.Sugar.BindingGroups (
2426import Data.Graph
2527import Data.List (nub , intersect )
2628import Data.Maybe (isJust , mapMaybe )
27- import Control.Applicative ( (<$>) , (<*>) , pure )
29+ import Control.Applicative
2830import Control.Monad ((<=<) )
31+ import Control.Monad.Error.Class
2932
3033import qualified Data.Set as S
3134
@@ -38,7 +41,7 @@ import Language.PureScript.Errors
3841-- |
3942-- Replace all sets of mutually-recursive declarations in a module with binding groups
4043--
41- createBindingGroupsModule :: [Module ] -> Either ErrorStack [Module ]
44+ createBindingGroupsModule :: ( Functor m , Applicative m , MonadError ErrorStack m ) => [Module ] -> m [Module ]
4245createBindingGroupsModule = mapM $ \ (Module name ds exps) -> Module name <$> createBindingGroups name ds <*> pure exps
4346
4447-- |
@@ -47,20 +50,20 @@ createBindingGroupsModule = mapM $ \(Module name ds exps) -> Module name <$> cre
4750collapseBindingGroupsModule :: [Module ] -> [Module ]
4851collapseBindingGroupsModule = map $ \ (Module name ds exps) -> Module name (collapseBindingGroups ds) exps
4952
50- createBindingGroups :: ModuleName -> [Declaration ] -> Either ErrorStack [Declaration ]
53+ createBindingGroups :: ( Functor m , Applicative m , MonadError ErrorStack m ) => ModuleName -> [Declaration ] -> m [Declaration ]
5154createBindingGroups moduleName = mapM f <=< handleDecls
5255
5356 where
5457 (f, _, _) = everywhereOnValuesTopDownM return handleExprs return
5558
56- handleExprs :: Expr -> Either ErrorStack Expr
59+ handleExprs :: ( Functor m , MonadError ErrorStack m ) => Expr -> m Expr
5760 handleExprs (Let ds val) = flip Let val <$> handleDecls ds
5861 handleExprs other = return other
5962
6063 -- |
6164 -- Replace all sets of mutually-recursive declarations with binding groups
6265 --
63- handleDecls :: [Declaration ] -> Either ErrorStack [Declaration ]
66+ handleDecls :: ( Functor m , MonadError ErrorStack m ) => [Declaration ] -> m [Declaration ]
6467 handleDecls ds = do
6568 let values = filter isValueDecl ds
6669 dataDecls = filter isDataDecl ds
@@ -151,7 +154,7 @@ getProperName _ = error "Expected DataDeclaration"
151154-- Convert a group of mutually-recursive dependencies into a BindingGroupDeclaration (or simple ValueDeclaration).
152155--
153156--
154- toBindingGroup :: ModuleName -> SCC Declaration -> Either ErrorStack Declaration
157+ toBindingGroup :: ( Functor m , MonadError ErrorStack m ) => ModuleName -> SCC Declaration -> m Declaration
155158toBindingGroup _ (AcyclicSCC d) = return d
156159toBindingGroup _ (CyclicSCC [d]) = return d
157160toBindingGroup moduleName (CyclicSCC ds') =
@@ -172,24 +175,24 @@ toBindingGroup moduleName (CyclicSCC ds') =
172175 valueVerts :: [(Declaration , Ident , [Ident ])]
173176 valueVerts = map (\ d -> (d, getIdent d, usedImmediateIdents moduleName d `intersect` idents)) ds'
174177
175- toBinding :: SCC Declaration -> Either ErrorStack (Ident , NameKind , Expr )
178+ toBinding :: ( MonadError ErrorStack m ) => SCC Declaration -> m (Ident , NameKind , Expr )
176179 toBinding (AcyclicSCC d) = return $ fromValueDecl d
177180 toBinding (CyclicSCC ~ (d: ds)) = cycleError d ds
178181
179- cycleError :: Declaration -> [Declaration ] -> Either ErrorStack a
182+ cycleError :: ( MonadError ErrorStack m ) => Declaration -> [Declaration ] -> m a
180183 cycleError (PositionedDeclaration p _ d) ds = rethrowWithPosition p $ cycleError d ds
181- cycleError (ValueDeclaration n _ _ (Right e)) [] = Left $
184+ cycleError (ValueDeclaration n _ _ (Right e)) [] = throwError $
182185 mkErrorStack (" Cycle in definition of " ++ show n) (Just (ExprError e))
183186 cycleError d ds@ (_: _) = rethrow (mkCompileError (" The following are not yet defined here: " ++ unwords (map (show . getIdent) ds)) Nothing `combineErrors` ) $ cycleError d []
184187 cycleError _ _ = error " Expected ValueDeclaration"
185188
186- toDataBindingGroup :: SCC Declaration -> Either ErrorStack Declaration
189+ toDataBindingGroup :: ( MonadError ErrorStack m ) => SCC Declaration -> m Declaration
187190toDataBindingGroup (AcyclicSCC d) = return d
188191toDataBindingGroup (CyclicSCC [d]) = case isTypeSynonym d of
189- Just pn -> Left $ mkErrorStack (" Cycle in type synonym " ++ show pn) Nothing
192+ Just pn -> throwError $ mkErrorStack (" Cycle in type synonym " ++ show pn) Nothing
190193 _ -> return d
191194toDataBindingGroup (CyclicSCC ds')
192- | all (isJust . isTypeSynonym) ds' = Left $ mkErrorStack " Cycle in type synonyms" Nothing
195+ | all (isJust . isTypeSynonym) ds' = throwError $ mkErrorStack " Cycle in type synonyms" Nothing
193196 | otherwise = return $ DataBindingGroupDeclaration ds'
194197
195198isTypeSynonym :: Declaration -> Maybe ProperName
0 commit comments