{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Carrier.Error.Either
(
runError
, ErrorC(..)
, module Control.Effect.Error
) where
import Control.Algebra
import Control.Applicative (Alternative(..))
import Control.Effect.Error
import Control.Monad (MonadPlus)
import Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Control.Monad.Trans.Except
runError :: ErrorC exc m a -> m (Either exc a)
runError :: forall exc (m :: * -> *) a. ErrorC exc m a -> m (Either exc a)
runError (ErrorC ExceptT exc m a
m) = ExceptT exc m a -> m (Either exc a)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT ExceptT exc m a
m
{-# INLINE runError #-}
newtype ErrorC e m a = ErrorC (ExceptT e m a)
deriving (Algebra (Error e :+: sig), Functor (ErrorC e m)
Functor (ErrorC e m) =>
(forall a. a -> ErrorC e m a)
-> (forall a b.
ErrorC e m (a -> b) -> ErrorC e m a -> ErrorC e m b)
-> (forall a b c.
(a -> b -> c) -> ErrorC e m a -> ErrorC e m b -> ErrorC e m c)
-> (forall a b. ErrorC e m a -> ErrorC e m b -> ErrorC e m b)
-> (forall a b. ErrorC e m a -> ErrorC e m b -> ErrorC e m a)
-> Applicative (ErrorC e m)
forall a. a -> ErrorC e m a
forall a b. ErrorC e m a -> ErrorC e m b -> ErrorC e m a
forall a b. ErrorC e m a -> ErrorC e m b -> ErrorC e m b
forall a b. ErrorC e m (a -> b) -> ErrorC e m a -> ErrorC e m b
forall a b c.
(a -> b -> c) -> ErrorC e m a -> ErrorC e m b -> ErrorC e m c
forall e (m :: * -> *). Monad m => Functor (ErrorC e m)
forall e (m :: * -> *) a. Monad m => a -> ErrorC e m a
forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m a -> ErrorC e m b -> ErrorC e m a
forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m a -> ErrorC e m b -> ErrorC e m b
forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m (a -> b) -> ErrorC e m a -> ErrorC e m b
forall e (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> ErrorC e m a -> ErrorC e m b -> ErrorC e m c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
$cpure :: forall e (m :: * -> *) a. Monad m => a -> ErrorC e m a
pure :: forall a. a -> ErrorC e m a
$c<*> :: forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m (a -> b) -> ErrorC e m a -> ErrorC e m b
<*> :: forall a b. ErrorC e m (a -> b) -> ErrorC e m a -> ErrorC e m b
$cliftA2 :: forall e (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> ErrorC e m a -> ErrorC e m b -> ErrorC e m c
liftA2 :: forall a b c.
(a -> b -> c) -> ErrorC e m a -> ErrorC e m b -> ErrorC e m c
$c*> :: forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m a -> ErrorC e m b -> ErrorC e m b
*> :: forall a b. ErrorC e m a -> ErrorC e m b -> ErrorC e m b
$c<* :: forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m a -> ErrorC e m b -> ErrorC e m a
<* :: forall a b. ErrorC e m a -> ErrorC e m b -> ErrorC e m a
Applicative, (forall a b. (a -> b) -> ErrorC e m a -> ErrorC e m b)
-> (forall a b. a -> ErrorC e m b -> ErrorC e m a)
-> Functor (ErrorC e m)
forall a b. a -> ErrorC e m b -> ErrorC e m a
forall a b. (a -> b) -> ErrorC e m a -> ErrorC e m b
forall e (m :: * -> *) a b.
Functor m =>
a -> ErrorC e m b -> ErrorC e m a
forall e (m :: * -> *) a b.
Functor m =>
(a -> b) -> ErrorC e m a -> ErrorC e m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall e (m :: * -> *) a b.
Functor m =>
(a -> b) -> ErrorC e m a -> ErrorC e m b
fmap :: forall a b. (a -> b) -> ErrorC e m a -> ErrorC e m b
$c<$ :: forall e (m :: * -> *) a b.
Functor m =>
a -> ErrorC e m b -> ErrorC e m a
<$ :: forall a b. a -> ErrorC e m b -> ErrorC e m a
Functor, Applicative (ErrorC e m)
Applicative (ErrorC e m) =>
(forall a b. ErrorC e m a -> (a -> ErrorC e m b) -> ErrorC e m b)
-> (forall a b. ErrorC e m a -> ErrorC e m b -> ErrorC e m b)
-> (forall a. a -> ErrorC e m a)
-> Monad (ErrorC e m)
forall a. a -> ErrorC e m a
forall a b. ErrorC e m a -> ErrorC e m b -> ErrorC e m b
forall a b. ErrorC e m a -> (a -> ErrorC e m b) -> ErrorC e m b
forall e (m :: * -> *). Monad m => Applicative (ErrorC e m)
forall e (m :: * -> *) a. Monad m => a -> ErrorC e m a
forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m a -> ErrorC e m b -> ErrorC e m b
forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m a -> (a -> ErrorC e m b) -> ErrorC e m b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
$c>>= :: forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m a -> (a -> ErrorC e m b) -> ErrorC e m b
>>= :: forall a b. ErrorC e m a -> (a -> ErrorC e m b) -> ErrorC e m b
$c>> :: forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m a -> ErrorC e m b -> ErrorC e m b
>> :: forall a b. ErrorC e m a -> ErrorC e m b -> ErrorC e m b
$creturn :: forall e (m :: * -> *) a. Monad m => a -> ErrorC e m a
return :: forall a. a -> ErrorC e m a
Monad, Monad (ErrorC e m)
Monad (ErrorC e m) =>
(forall a. String -> ErrorC e m a) -> MonadFail (ErrorC e m)
forall a. String -> ErrorC e m a
forall e (m :: * -> *). MonadFail m => Monad (ErrorC e m)
forall e (m :: * -> *) a. MonadFail m => String -> ErrorC e m a
forall (m :: * -> *).
Monad m =>
(forall a. String -> m a) -> MonadFail m
$cfail :: forall e (m :: * -> *) a. MonadFail m => String -> ErrorC e m a
fail :: forall a. String -> ErrorC e m a
Fail.MonadFail, Monad (ErrorC e m)
Monad (ErrorC e m) =>
(forall a. (a -> ErrorC e m a) -> ErrorC e m a)
-> MonadFix (ErrorC e m)
forall a. (a -> ErrorC e m a) -> ErrorC e m a
forall e (m :: * -> *). MonadFix m => Monad (ErrorC e m)
forall e (m :: * -> *) a.
MonadFix m =>
(a -> ErrorC e m a) -> ErrorC e m a
forall (m :: * -> *).
Monad m =>
(forall a. (a -> m a) -> m a) -> MonadFix m
$cmfix :: forall e (m :: * -> *) a.
MonadFix m =>
(a -> ErrorC e m a) -> ErrorC e m a
mfix :: forall a. (a -> ErrorC e m a) -> ErrorC e m a
MonadFix, Monad (ErrorC e m)
Monad (ErrorC e m) =>
(forall a. IO a -> ErrorC e m a) -> MonadIO (ErrorC e m)
forall a. IO a -> ErrorC e m a
forall e (m :: * -> *). MonadIO m => Monad (ErrorC e m)
forall e (m :: * -> *) a. MonadIO m => IO a -> ErrorC e m a
forall (m :: * -> *).
Monad m =>
(forall a. IO a -> m a) -> MonadIO m
$cliftIO :: forall e (m :: * -> *) a. MonadIO m => IO a -> ErrorC e m a
liftIO :: forall a. IO a -> ErrorC e m a
MonadIO, (forall (m :: * -> *). Monad m => Monad (ErrorC e m)) =>
(forall (m :: * -> *) a. Monad m => m a -> ErrorC e m a)
-> MonadTrans (ErrorC e)
forall e (m :: * -> *). Monad m => Monad (ErrorC e m)
forall e (m :: * -> *) a. Monad m => m a -> ErrorC e m a
forall (m :: * -> *). Monad m => Monad (ErrorC e m)
forall (m :: * -> *) a. Monad m => m a -> ErrorC e m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *). Monad m => Monad (t m)) =>
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
$clift :: forall e (m :: * -> *) a. Monad m => m a -> ErrorC e m a
lift :: forall (m :: * -> *) a. Monad m => m a -> ErrorC e m a
MonadTrans)
instance (Alternative m, Monad m) => Alternative (ErrorC e m) where
empty :: forall a. ErrorC e m a
empty = ExceptT e m a -> ErrorC e m a
forall e (m :: * -> *) a. ExceptT e m a -> ErrorC e m a
ErrorC (m (Either e a) -> ExceptT e m a
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT m (Either e a)
forall a. m a
forall (f :: * -> *) a. Alternative f => f a
empty)
{-# INLINE empty #-}
ErrorC (ExceptT m (Either e a)
l) <|> :: forall a. ErrorC e m a -> ErrorC e m a -> ErrorC e m a
<|> ErrorC (ExceptT m (Either e a)
r) = ExceptT e m a -> ErrorC e m a
forall e (m :: * -> *) a. ExceptT e m a -> ErrorC e m a
ErrorC (m (Either e a) -> ExceptT e m a
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either e a)
l m (Either e a) -> m (Either e a) -> m (Either e a)
forall a. m a -> m a -> m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> m (Either e a)
r))
{-# INLINE (<|>) #-}
instance (Alternative m, Monad m) => MonadPlus (ErrorC e m)