{-# LANGUAGE FlexibleInstances, GeneralizedNewtypeDeriving, MultiParamTypeClasses, TypeOperators, UndecidableInstances #-}
module Control.Carrier.Throw.Either
(
runThrow
, ThrowC(..)
, module Control.Effect.Throw
) where
import Control.Algebra
import Control.Applicative (Alternative)
import Control.Carrier.Error.Either
import Control.Effect.Throw
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
runThrow :: ThrowC e m a -> m (Either e a)
runThrow :: ThrowC e m a -> m (Either e a)
runThrow (ThrowC m :: ErrorC e m a
m) = ErrorC e m a -> m (Either e a)
forall exc (m :: * -> *) a. ErrorC exc m a -> m (Either exc a)
runError ErrorC e m a
m
newtype ThrowC e m a = ThrowC (ErrorC e m a)
deriving (Applicative (ThrowC e m)
ThrowC e m a
Applicative (ThrowC e m) =>
(forall a. ThrowC e m a)
-> (forall a. ThrowC e m a -> ThrowC e m a -> ThrowC e m a)
-> (forall a. ThrowC e m a -> ThrowC e m [a])
-> (forall a. ThrowC e m a -> ThrowC e m [a])
-> Alternative (ThrowC e m)
ThrowC e m a -> ThrowC e m a -> ThrowC e m a
ThrowC e m a -> ThrowC e m [a]
ThrowC e m a -> ThrowC e m [a]
forall a. ThrowC e m a
forall a. ThrowC e m a -> ThrowC e m [a]
forall a. ThrowC e m a -> ThrowC e m a -> ThrowC e m a
forall e (m :: * -> *).
(Alternative m, Monad m) =>
Applicative (ThrowC e m)
forall e (m :: * -> *) a. (Alternative m, Monad m) => ThrowC e m a
forall e (m :: * -> *) a.
(Alternative m, Monad m) =>
ThrowC e m a -> ThrowC e m [a]
forall e (m :: * -> *) a.
(Alternative m, Monad m) =>
ThrowC e m a -> ThrowC e m a -> ThrowC e m a
forall (f :: * -> *).
Applicative f =>
(forall a. f a)
-> (forall a. f a -> f a -> f a)
-> (forall a. f a -> f [a])
-> (forall a. f a -> f [a])
-> Alternative f
many :: ThrowC e m a -> ThrowC e m [a]
$cmany :: forall e (m :: * -> *) a.
(Alternative m, Monad m) =>
ThrowC e m a -> ThrowC e m [a]
some :: ThrowC e m a -> ThrowC e m [a]
$csome :: forall e (m :: * -> *) a.
(Alternative m, Monad m) =>
ThrowC e m a -> ThrowC e m [a]
<|> :: ThrowC e m a -> ThrowC e m a -> ThrowC e m a
$c<|> :: forall e (m :: * -> *) a.
(Alternative m, Monad m) =>
ThrowC e m a -> ThrowC e m a -> ThrowC e m a
empty :: ThrowC e m a
$cempty :: forall e (m :: * -> *) a. (Alternative m, Monad m) => ThrowC e m a
$cp1Alternative :: forall e (m :: * -> *).
(Alternative m, Monad m) =>
Applicative (ThrowC e m)
Alternative, Functor (ThrowC e m)
a -> ThrowC e m a
Functor (ThrowC e m) =>
(forall a. a -> ThrowC e m a)
-> (forall a b.
ThrowC e m (a -> b) -> ThrowC e m a -> ThrowC e m b)
-> (forall a b c.
(a -> b -> c) -> ThrowC e m a -> ThrowC e m b -> ThrowC e m c)
-> (forall a b. ThrowC e m a -> ThrowC e m b -> ThrowC e m b)
-> (forall a b. ThrowC e m a -> ThrowC e m b -> ThrowC e m a)
-> Applicative (ThrowC e m)
ThrowC e m a -> ThrowC e m b -> ThrowC e m b
ThrowC e m a -> ThrowC e m b -> ThrowC e m a
ThrowC e m (a -> b) -> ThrowC e m a -> ThrowC e m b
(a -> b -> c) -> ThrowC e m a -> ThrowC e m b -> ThrowC e m c
forall a. a -> ThrowC e m a
forall a b. ThrowC e m a -> ThrowC e m b -> ThrowC e m a
forall a b. ThrowC e m a -> ThrowC e m b -> ThrowC e m b
forall a b. ThrowC e m (a -> b) -> ThrowC e m a -> ThrowC e m b
forall a b c.
(a -> b -> c) -> ThrowC e m a -> ThrowC e m b -> ThrowC e m c
forall e (m :: * -> *). Monad m => Functor (ThrowC e m)
forall e (m :: * -> *) a. Monad m => a -> ThrowC e m a
forall e (m :: * -> *) a b.
Monad m =>
ThrowC e m a -> ThrowC e m b -> ThrowC e m a
forall e (m :: * -> *) a b.
Monad m =>
ThrowC e m a -> ThrowC e m b -> ThrowC e m b
forall e (m :: * -> *) a b.
Monad m =>
ThrowC e m (a -> b) -> ThrowC e m a -> ThrowC e m b
forall e (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> ThrowC e m a -> ThrowC e m b -> ThrowC 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
<* :: ThrowC e m a -> ThrowC e m b -> ThrowC e m a
$c<* :: forall e (m :: * -> *) a b.
Monad m =>
ThrowC e m a -> ThrowC e m b -> ThrowC e m a
*> :: ThrowC e m a -> ThrowC e m b -> ThrowC e m b
$c*> :: forall e (m :: * -> *) a b.
Monad m =>
ThrowC e m a -> ThrowC e m b -> ThrowC e m b
liftA2 :: (a -> b -> c) -> ThrowC e m a -> ThrowC e m b -> ThrowC e m c
$cliftA2 :: forall e (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> ThrowC e m a -> ThrowC e m b -> ThrowC e m c
<*> :: ThrowC e m (a -> b) -> ThrowC e m a -> ThrowC e m b
$c<*> :: forall e (m :: * -> *) a b.
Monad m =>
ThrowC e m (a -> b) -> ThrowC e m a -> ThrowC e m b
pure :: a -> ThrowC e m a
$cpure :: forall e (m :: * -> *) a. Monad m => a -> ThrowC e m a
$cp1Applicative :: forall e (m :: * -> *). Monad m => Functor (ThrowC e m)
Applicative, a -> ThrowC e m b -> ThrowC e m a
(a -> b) -> ThrowC e m a -> ThrowC e m b
(forall a b. (a -> b) -> ThrowC e m a -> ThrowC e m b)
-> (forall a b. a -> ThrowC e m b -> ThrowC e m a)
-> Functor (ThrowC e m)
forall a b. a -> ThrowC e m b -> ThrowC e m a
forall a b. (a -> b) -> ThrowC e m a -> ThrowC e m b
forall e (m :: * -> *) a b.
Functor m =>
a -> ThrowC e m b -> ThrowC e m a
forall e (m :: * -> *) a b.
Functor m =>
(a -> b) -> ThrowC e m a -> ThrowC 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 -> ThrowC e m b -> ThrowC e m a
$c<$ :: forall e (m :: * -> *) a b.
Functor m =>
a -> ThrowC e m b -> ThrowC e m a
fmap :: (a -> b) -> ThrowC e m a -> ThrowC e m b
$cfmap :: forall e (m :: * -> *) a b.
Functor m =>
(a -> b) -> ThrowC e m a -> ThrowC e m b
Functor, Applicative (ThrowC e m)
a -> ThrowC e m a
Applicative (ThrowC e m) =>
(forall a b. ThrowC e m a -> (a -> ThrowC e m b) -> ThrowC e m b)
-> (forall a b. ThrowC e m a -> ThrowC e m b -> ThrowC e m b)
-> (forall a. a -> ThrowC e m a)
-> Monad (ThrowC e m)
ThrowC e m a -> (a -> ThrowC e m b) -> ThrowC e m b
ThrowC e m a -> ThrowC e m b -> ThrowC e m b
forall a. a -> ThrowC e m a
forall a b. ThrowC e m a -> ThrowC e m b -> ThrowC e m b
forall a b. ThrowC e m a -> (a -> ThrowC e m b) -> ThrowC e m b
forall e (m :: * -> *). Monad m => Applicative (ThrowC e m)
forall e (m :: * -> *) a. Monad m => a -> ThrowC e m a
forall e (m :: * -> *) a b.
Monad m =>
ThrowC e m a -> ThrowC e m b -> ThrowC e m b
forall e (m :: * -> *) a b.
Monad m =>
ThrowC e m a -> (a -> ThrowC e m b) -> ThrowC 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 -> ThrowC e m a
$creturn :: forall e (m :: * -> *) a. Monad m => a -> ThrowC e m a
>> :: ThrowC e m a -> ThrowC e m b -> ThrowC e m b
$c>> :: forall e (m :: * -> *) a b.
Monad m =>
ThrowC e m a -> ThrowC e m b -> ThrowC e m b
>>= :: ThrowC e m a -> (a -> ThrowC e m b) -> ThrowC e m b
$c>>= :: forall e (m :: * -> *) a b.
Monad m =>
ThrowC e m a -> (a -> ThrowC e m b) -> ThrowC e m b
$cp1Monad :: forall e (m :: * -> *). Monad m => Applicative (ThrowC e m)
Monad, Monad (ThrowC e m)
Monad (ThrowC e m) =>
(forall a. String -> ThrowC e m a) -> MonadFail (ThrowC e m)
String -> ThrowC e m a
forall a. String -> ThrowC e m a
forall e (m :: * -> *). MonadFail m => Monad (ThrowC e m)
forall e (m :: * -> *) a. MonadFail m => String -> ThrowC e m a
forall (m :: * -> *).
Monad m =>
(forall a. String -> m a) -> MonadFail m
fail :: String -> ThrowC e m a
$cfail :: forall e (m :: * -> *) a. MonadFail m => String -> ThrowC e m a
$cp1MonadFail :: forall e (m :: * -> *). MonadFail m => Monad (ThrowC e m)
Fail.MonadFail, Monad (ThrowC e m)
Monad (ThrowC e m) =>
(forall a. (a -> ThrowC e m a) -> ThrowC e m a)
-> MonadFix (ThrowC e m)
(a -> ThrowC e m a) -> ThrowC e m a
forall a. (a -> ThrowC e m a) -> ThrowC e m a
forall e (m :: * -> *). MonadFix m => Monad (ThrowC e m)
forall e (m :: * -> *) a.
MonadFix m =>
(a -> ThrowC e m a) -> ThrowC e m a
forall (m :: * -> *).
Monad m =>
(forall a. (a -> m a) -> m a) -> MonadFix m
mfix :: (a -> ThrowC e m a) -> ThrowC e m a
$cmfix :: forall e (m :: * -> *) a.
MonadFix m =>
(a -> ThrowC e m a) -> ThrowC e m a
$cp1MonadFix :: forall e (m :: * -> *). MonadFix m => Monad (ThrowC e m)
MonadFix, Monad (ThrowC e m)
Monad (ThrowC e m) =>
(forall a. IO a -> ThrowC e m a) -> MonadIO (ThrowC e m)
IO a -> ThrowC e m a
forall a. IO a -> ThrowC e m a
forall e (m :: * -> *). MonadIO m => Monad (ThrowC e m)
forall e (m :: * -> *) a. MonadIO m => IO a -> ThrowC e m a
forall (m :: * -> *).
Monad m =>
(forall a. IO a -> m a) -> MonadIO m
liftIO :: IO a -> ThrowC e m a
$cliftIO :: forall e (m :: * -> *) a. MonadIO m => IO a -> ThrowC e m a
$cp1MonadIO :: forall e (m :: * -> *). MonadIO m => Monad (ThrowC e m)
MonadIO, Monad (ThrowC e m)
Alternative (ThrowC e m)
ThrowC e m a
(Alternative (ThrowC e m), Monad (ThrowC e m)) =>
(forall a. ThrowC e m a)
-> (forall a. ThrowC e m a -> ThrowC e m a -> ThrowC e m a)
-> MonadPlus (ThrowC e m)
ThrowC e m a -> ThrowC e m a -> ThrowC e m a
forall a. ThrowC e m a
forall a. ThrowC e m a -> ThrowC e m a -> ThrowC e m a
forall e (m :: * -> *).
(Alternative m, Monad m) =>
Monad (ThrowC e m)
forall e (m :: * -> *).
(Alternative m, Monad m) =>
Alternative (ThrowC e m)
forall e (m :: * -> *) a. (Alternative m, Monad m) => ThrowC e m a
forall e (m :: * -> *) a.
(Alternative m, Monad m) =>
ThrowC e m a -> ThrowC e m a -> ThrowC e m a
forall (m :: * -> *).
(Alternative m, Monad m) =>
(forall a. m a) -> (forall a. m a -> m a -> m a) -> MonadPlus m
mplus :: ThrowC e m a -> ThrowC e m a -> ThrowC e m a
$cmplus :: forall e (m :: * -> *) a.
(Alternative m, Monad m) =>
ThrowC e m a -> ThrowC e m a -> ThrowC e m a
mzero :: ThrowC e m a
$cmzero :: forall e (m :: * -> *) a. (Alternative m, Monad m) => ThrowC e m a
$cp2MonadPlus :: forall e (m :: * -> *).
(Alternative m, Monad m) =>
Monad (ThrowC e m)
$cp1MonadPlus :: forall e (m :: * -> *).
(Alternative m, Monad m) =>
Alternative (ThrowC e m)
MonadPlus, m a -> ThrowC e m a
(forall (m :: * -> *) a. Monad m => m a -> ThrowC e m a)
-> MonadTrans (ThrowC e)
forall e (m :: * -> *) a. Monad m => m a -> ThrowC e m a
forall (m :: * -> *) a. Monad m => m a -> ThrowC e m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
lift :: m a -> ThrowC e m a
$clift :: forall e (m :: * -> *) a. Monad m => m a -> ThrowC e m a
MonadTrans)
instance (Algebra sig m, Effect sig) => Algebra (Throw e :+: sig) (ThrowC e m) where
alg :: (:+:) (Throw e) sig (ThrowC e m) a -> ThrowC e m a
alg (L (Throw e :: e
e)) = ErrorC e m a -> ThrowC e m a
forall e (m :: * -> *) a. ErrorC e m a -> ThrowC e m a
ThrowC (e -> ErrorC e m a
forall e (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (Throw e) sig m =>
e -> m a
throwError e
e)
alg (R other :: sig (ThrowC e m) a
other) = ErrorC e m a -> ThrowC e m a
forall e (m :: * -> *) a. ErrorC e m a -> ThrowC e m a
ThrowC ((:+:) (Error e) sig (ErrorC e m) a -> ErrorC e m a
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Algebra sig m =>
sig m a -> m a
alg (sig (ErrorC e m) a -> (:+:) (Error e) sig (ErrorC e m) a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
g m k -> (:+:) f g m k
R (sig (ThrowC e m) a -> sig (ErrorC e m) a
forall (sig :: (* -> *) -> * -> *) (f :: * -> *) (g :: * -> *) a.
(HFunctor sig, Functor f, Coercible f g) =>
sig f a -> sig g a
handleCoercible sig (ThrowC e m) a
other)))