@@ -16,10 +16,12 @@
1616
1717module Language.PureScript.Errors where
1818
19+import Data.Either (lefts, rights)
1920import Data.List (intersperse, intercalate)
2021import Data.Monoid
2122
2223import Control.Monad.Error
24+import Control.Applicative ((<$>))
2325
2426import Language.PureScript.Declarations
2527import Language.PureScript.Pretty
@@ -121,3 +123,18 @@ rethrow f = flip catchError $ \e -> throwError (f e)
121123--
122124rethrowWithPosition :: (MonadError ErrorStack m) => SourcePos -> m a -> m a
123125rethrowWithPosition pos = rethrow (positionError pos <>)
126+
127+-- |
128+-- Collect errors in in parallel
129+--
130+parU :: (MonadError ErrorStack m, Functor m) => [a] -> (a -> m b) -> m [b]
131+parU xs f = forM xs (withError . f) >>= collectErrors
132+ where
133+ withError :: (MonadError ErrorStack m, Functor m) => m a -> m (Either ErrorStack a)
134+ withError u = catchError (Right <$> u) (return . Left)
135+
136+ collectErrors :: (MonadError ErrorStack m, Functor m) => [Either ErrorStack a] -> m [a]
137+ collectErrors es = case lefts es of
138+ [err] -> throwError err
139+ [] -> return $ rights es
140+ errs -> throwError $ MultipleErrors errs