{-# 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 qualified 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 :: ErrorC exc m a -> m (Either exc a)
runError (ErrorC m :: 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
newtype ErrorC e m a = ErrorC (ExceptT e m a)
deriving (Functor (ErrorC e m)
a -> ErrorC e m a
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)
ErrorC e m a -> ErrorC e m b -> ErrorC e m b
ErrorC e m a -> ErrorC e m b -> ErrorC e m a
ErrorC e m (a -> b) -> ErrorC e m a -> ErrorC e m b
(a -> b -> c) -> ErrorC e m a -> ErrorC e m b -> ErrorC e m c
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
<* :: ErrorC e m a -> ErrorC e m b -> ErrorC e m a
$c<* :: forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m a -> ErrorC e m b -> ErrorC e m a
*> :: 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 b
liftA2 :: (a -> b -> c) -> ErrorC e m a -> ErrorC e m b -> ErrorC e m c
$cliftA2 :: forall e (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> ErrorC e m a -> ErrorC e m b -> ErrorC e m c
<*> :: ErrorC e m (a -> b) -> ErrorC e m a -> ErrorC e m b
$c<*> :: forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m (a -> b) -> ErrorC e m a -> ErrorC e m b
pure :: a -> ErrorC e m a
$cpure :: forall e (m :: * -> *) a. Monad m => a -> ErrorC e m a
$cp1Applicative :: forall e (m :: * -> *). Monad m => Functor (ErrorC e m)
Applicative, a -> ErrorC e m b -> ErrorC e m a
(a -> b) -> ErrorC e m a -> ErrorC e m b
(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
<$ :: a -> ErrorC e m b -> ErrorC e m a
$c<$ :: forall e (m :: * -> *) a b.
Functor m =>
a -> ErrorC e m b -> ErrorC e m a
fmap :: (a -> b) -> ErrorC e m a -> ErrorC e m b
$cfmap :: forall e (m :: * -> *) a b.
Functor m =>
(a -> b) -> ErrorC e m a -> ErrorC e m b
Functor, Applicative (ErrorC e m)
a -> ErrorC e m a
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)
ErrorC e m a -> (a -> ErrorC e m b) -> ErrorC e m b
ErrorC e m a -> ErrorC e m b -> ErrorC e m b
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
return :: a -> ErrorC e m a
$creturn :: forall e (m :: * -> *) a. Monad m => a -> ErrorC e m a
>> :: 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 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 -> (a -> ErrorC e m b) -> ErrorC e m b
$cp1Monad :: forall e (m :: * -> *). Monad m => Applicative (ErrorC e m)
Monad, Monad (ErrorC e m)
Monad (ErrorC e m) =>
(forall a. String -> ErrorC e m a) -> MonadFail (ErrorC e m)
String -> ErrorC e m a
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
fail :: String -> ErrorC e m a
$cfail :: forall e (m :: * -> *) a. MonadFail m => String -> ErrorC e m a
$cp1MonadFail :: forall e (m :: * -> *). MonadFail m => Monad (ErrorC e m)
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)
(a -> ErrorC e m a) -> ErrorC e m a
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
mfix :: (a -> ErrorC e m a) -> ErrorC e m a
$cmfix :: forall e (m :: * -> *) a.
MonadFix m =>
(a -> ErrorC e m a) -> ErrorC e m a
$cp1MonadFix :: forall e (m :: * -> *). MonadFix m => Monad (ErrorC e m)
MonadFix, Monad (ErrorC e m)
Monad (ErrorC e m) =>
(forall a. IO a -> ErrorC e m a) -> MonadIO (ErrorC e m)
IO a -> ErrorC e m a
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
liftIO :: IO a -> ErrorC e m a
$cliftIO :: forall e (m :: * -> *) a. MonadIO m => IO a -> ErrorC e m a
$cp1MonadIO :: forall e (m :: * -> *). MonadIO m => Monad (ErrorC e m)
MonadIO, m a -> ErrorC e m a
(forall (m :: * -> *) a. Monad m => m a -> ErrorC e m a)
-> MonadTrans (ErrorC e)
forall e (m :: * -> *) a. Monad m => m a -> ErrorC e m a
forall (m :: * -> *) a. Monad m => m a -> ErrorC e m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
lift :: m a -> ErrorC e m a
$clift :: forall e (m :: * -> *) a. Monad m => m a -> ErrorC e m a
MonadTrans)
instance (Alternative m, Monad m) => Alternative (ErrorC e m) where
empty :: 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 (f :: * -> *) a. Alternative f => f a
empty)
{-# INLINE empty #-}
ErrorC (ExceptT l :: m (Either e a)
l) <|> :: ErrorC e m a -> ErrorC e m a -> ErrorC e m a
<|> ErrorC (ExceptT r :: 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 (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)
instance (Algebra sig m, Effect sig) => Algebra (Error e :+: sig) (ErrorC e m) where
alg :: (:+:) (Error e) sig (ErrorC e m) a -> ErrorC e m a
alg = ExceptT e m a -> ErrorC e m a
forall e (m :: * -> *) a. ExceptT e m a -> ErrorC e m a
ErrorC (ExceptT e m a -> ErrorC e m a)
-> ((:+:) (Error e) sig (ErrorC e m) a -> ExceptT e m a)
-> (:+:) (Error e) sig (ErrorC e m) a
-> ErrorC e m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:+:) (Error e) sig (ExceptT e m) a -> ExceptT e m a
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Algebra sig m =>
sig m a -> m a
alg ((:+:) (Error e) sig (ExceptT e m) a -> ExceptT e m a)
-> ((:+:) (Error e) sig (ErrorC e m) a
-> (:+:) (Error e) sig (ExceptT e m) a)
-> (:+:) (Error e) sig (ErrorC e m) a
-> ExceptT e m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:+:) (Error e) sig (ErrorC e m) a
-> (:+:) (Error e) sig (ExceptT e m) a
forall (sig :: (* -> *) -> * -> *) (f :: * -> *) (g :: * -> *) a.
(HFunctor sig, Functor f, Coercible f g) =>
sig f a -> sig g a
handleCoercible
{-# INLINE alg #-}