{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Carrier.Throw.Either
(
runThrow
, ThrowC(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 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 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
{-# INLINE runThrow #-}
newtype ThrowC e m a = ThrowC { ThrowC e m a -> ErrorC e m a
runThrowC :: 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 => Algebra (Throw e :+: sig) (ThrowC e m) where
alg :: Handler ctx n (ThrowC e m)
-> (:+:) (Throw e) sig n a -> ctx () -> ThrowC e m (ctx a)
alg Handler ctx n (ThrowC e m)
hdl (:+:) (Throw e) sig n a
sig ctx ()
ctx = case (:+:) (Throw e) sig n a
sig of
L (Throw e
e) -> ErrorC e m (ctx a) -> ThrowC e m (ctx a)
forall e (m :: * -> *) a. ErrorC e m a -> ThrowC e m a
ThrowC (e -> ErrorC e m (ctx a)
forall e (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (Throw e) sig m =>
e -> m a
throwError e
e)
R sig n a
other -> ErrorC e m (ctx a) -> ThrowC e m (ctx a)
forall e (m :: * -> *) a. ErrorC e m a -> ThrowC e m a
ThrowC (Handler ctx n (ErrorC e m)
-> (:+:) (Error e) sig n a -> ctx () -> ErrorC e m (ctx a)
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) (ctx :: * -> *)
(n :: * -> *) a.
(Algebra sig m, Functor ctx) =>
Handler ctx n m -> sig n a -> ctx () -> m (ctx a)
alg (ThrowC e m (ctx x) -> ErrorC e m (ctx x)
forall e (m :: * -> *) a. ThrowC e m a -> ErrorC e m a
runThrowC (ThrowC e m (ctx x) -> ErrorC e m (ctx x))
-> (ctx (n x) -> ThrowC e m (ctx x))
-> ctx (n x)
-> ErrorC e m (ctx x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ctx (n x) -> ThrowC e m (ctx x)
Handler ctx n (ThrowC e m)
hdl) (sig n a -> (:+:) (Error e) sig n a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
g m k -> (:+:) f g m k
R sig n a
other) ctx ()
ctx)
{-# INLINE alg #-}