{-# 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) = 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 #-}

-- | @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 (Applicative (ThrowC e m)
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)
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
$cempty :: forall e (m :: * -> *) a. (Alternative m, Monad m) => ThrowC e m a
empty :: forall 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
<|> :: forall a. ThrowC e m 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]
some :: 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]
many :: forall a. ThrowC e m a -> ThrowC e m [a]
Alternative, Functor (ThrowC e m)
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)
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
$cpure :: forall e (m :: * -> *) a. Monad m => a -> ThrowC e m a
pure :: forall a. a -> ThrowC e m a
$c<*> :: forall e (m :: * -> *) a b.
Monad m =>
ThrowC e m (a -> b) -> ThrowC e m a -> ThrowC e m b
<*> :: forall a b. ThrowC e m (a -> b) -> ThrowC e m a -> ThrowC e m b
$cliftA2 :: forall e (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> ThrowC e m a -> ThrowC e m b -> ThrowC e m c
liftA2 :: forall a b c.
(a -> b -> c) -> ThrowC e m a -> ThrowC e m b -> ThrowC e m c
$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 -> 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 a
<* :: forall a b. ThrowC e m a -> ThrowC e m b -> ThrowC e m a
Applicative, (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
$cfmap :: forall e (m :: * -> *) a b.
Functor m =>
(a -> b) -> ThrowC e m a -> ThrowC e m b
fmap :: forall a b. (a -> b) -> ThrowC e m a -> ThrowC e m b
$c<$ :: forall e (m :: * -> *) a b.
Functor m =>
a -> ThrowC e m b -> ThrowC e m a
<$ :: forall a b. a -> ThrowC e m b -> ThrowC e m a
Functor, Applicative (ThrowC e m)
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)
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
$c>>= :: forall e (m :: * -> *) a b.
Monad m =>
ThrowC e m a -> (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 -> ThrowC e m b -> ThrowC e m b
>> :: forall a b. ThrowC e m a -> ThrowC e m b -> ThrowC e m b
$creturn :: forall e (m :: * -> *) a. Monad m => a -> ThrowC e m a
return :: forall a. a -> ThrowC e m a
Monad, Monad (ThrowC e m)
Monad (ThrowC e m) =>
(forall a. String -> ThrowC e m a) -> MonadFail (ThrowC e m)
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
$cfail :: forall e (m :: * -> *) a. MonadFail m => String -> ThrowC e m a
fail :: forall a. String -> ThrowC e m a
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)
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
$cmfix :: forall e (m :: * -> *) a.
MonadFix m =>
(a -> ThrowC e m a) -> ThrowC e m a
mfix :: forall a. (a -> ThrowC e m a) -> ThrowC e m a
MonadFix, Monad (ThrowC e m)
Monad (ThrowC e m) =>
(forall a. IO a -> ThrowC e m a) -> MonadIO (ThrowC e m)
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
$cliftIO :: forall e (m :: * -> *) a. MonadIO m => IO a -> ThrowC e m a
liftIO :: forall a. IO a -> ThrowC e m a
MonadIO, Monad (ThrowC e m)
Alternative (ThrowC e m)
(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)
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
$cmzero :: forall e (m :: * -> *) a. (Alternative m, Monad m) => ThrowC e m a
mzero :: forall 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
mplus :: forall a. ThrowC e m a -> ThrowC e m a -> ThrowC e m a
MonadPlus, (forall (m :: * -> *). Monad m => Monad (ThrowC e m)) =>
(forall (m :: * -> *) a. Monad m => m a -> ThrowC e m a)
-> MonadTrans (ThrowC e)
forall e (m :: * -> *). Monad m => Monad (ThrowC e m)
forall e (m :: * -> *) a. Monad m => m a -> ThrowC e m a
forall (m :: * -> *). Monad m => Monad (ThrowC e m)
forall (m :: * -> *) a. Monad m => m a -> ThrowC 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 -> ThrowC e m a
lift :: forall (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) -> 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 (ctx :: * -> *) (n :: * -> *) a.
Functor ctx =>
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 #-}