{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{- | A carrier for a 'Throw' effect.

@since 1.0.0.0
-}
module Control.Carrier.Throw.Either
( -- * Throw carrier
  runThrow
, ThrowC(ThrowC)
  -- * Throw effect
, 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

-- | Run a 'Throw' effect, returning failures in 'Left' and successful computations’ results in 'Right'.
runThrow :: ThrowC e m a -> m (Either e a)
runThrow :: forall e (m :: * -> *) a. ThrowC e m a -> m (Either e a)
runThrow (ThrowC ErrorC e m a
m) = forall exc (m :: * -> *) a. ErrorC exc m a -> m (Either exc a)
runError ErrorC e m a
m
{-# INLINE runThrow #-}

-- | @since 1.0.0.0
newtype ThrowC e m a = ThrowC { forall e (m :: * -> *) a. ThrowC e m a -> ErrorC e m a
runThrowC :: ErrorC e m a }
  deriving (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 :: forall a. 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 :: forall a. 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]
<|> :: forall 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 :: forall a. ThrowC e m a
$cempty :: forall e (m :: * -> *) a. (Alternative m, Monad m) => ThrowC e m a
Alternative, 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
<* :: forall a b. 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
*> :: forall a b. 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 :: forall a b c.
(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
<*> :: forall a b. 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 :: forall a. a -> ThrowC e m a
$cpure :: forall e (m :: * -> *) a. Monad m => a -> ThrowC e m a
Applicative, 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
<$ :: forall a b. 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 :: forall a b. (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, 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 :: forall a. a -> ThrowC e m a
$creturn :: forall e (m :: * -> *) a. Monad m => a -> ThrowC e m a
>> :: forall a b. 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
>>= :: forall a 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
Monad, 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 :: forall a. String -> ThrowC e m a
$cfail :: forall e (m :: * -> *) a. MonadFail m => String -> ThrowC e m a
Fail.MonadFail, 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 :: forall a. (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
MonadFix, 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 :: forall a. IO a -> ThrowC e m a
$cliftIO :: forall e (m :: * -> *) a. MonadIO m => IO a -> ThrowC e m a
MonadIO, 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 :: forall a. 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 :: forall a. ThrowC e m a
$cmzero :: forall e (m :: * -> *) a. (Alternative m, Monad m) => ThrowC e m a
MonadPlus, 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 :: forall (m :: * -> *) a. Monad m => 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 :: forall (ctx :: * -> *) (n :: * -> *) a.
Functor ctx =>
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) -> forall e (m :: * -> *) a. ErrorC e m a -> ThrowC e m a
ThrowC (forall e (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (Throw e) sig m =>
e -> m a
throwError e
e)
    R sig n a
other     -> forall e (m :: * -> *) a. ErrorC e m a -> ThrowC e m a
ThrowC (forall (sig :: (* -> *) -> * -> *) (m :: * -> *) (ctx :: * -> *)
       (n :: * -> *) a.
(Algebra sig m, Functor ctx) =>
Handler ctx n m -> sig n a -> ctx () -> m (ctx a)
alg (forall e (m :: * -> *) a. ThrowC e m a -> ErrorC e m a
runThrowC forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handler ctx n (ThrowC e m)
hdl) (forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
       (m :: * -> *) k.
g m k -> (:+:) f g m k
R sig n a
other) ctx ()
ctx)
  {-# INLINE alg #-}