{-# LANGUAGE FlexibleInstances, GeneralizedNewtypeDeriving, MultiParamTypeClasses, TypeOperators, UndecidableInstances #-}

-- | A carrier for a 'Control.Effect.Fail.Fail' effect, returning the result as an 'Either' 'String'. Failed computations will return a 'Left' containing the 'String' value passed to 'Fail.fail'.
--
-- @since 1.0.0.0
module Control.Carrier.Fail.Either
( -- * Fail carrier
  runFail
, FailC(..)
  -- * Fail effect
, module Control.Effect.Fail
) where

import Control.Algebra
import Control.Applicative (Alternative(..))
import Control.Carrier.Throw.Either
import Control.Effect.Fail
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

-- | Run a 'Control.Effect.Fail.Fail' effect, returning failure messages in 'Left' and successful computations’ results in 'Right'.
--
-- @
-- 'runFail' ('pure' a) = 'pure' ('Right' a)
-- @
-- @
-- 'runFail' ('Fail.fail' s) = 'pure' ('Left' s)
-- @
--
-- @since 1.0.0.0
runFail :: FailC m a -> m (Either String a)
runFail :: FailC m a -> m (Either String a)
runFail (FailC m :: ThrowC String m a
m) = ThrowC String m a -> m (Either String a)
forall e (m :: * -> *) a. ThrowC e m a -> m (Either e a)
runThrow ThrowC String m a
m

-- | @since 1.0.0.0
newtype FailC m a = FailC (ThrowC String m a)
  deriving (Applicative (FailC m)
FailC m a
Applicative (FailC m) =>
(forall a. FailC m a)
-> (forall a. FailC m a -> FailC m a -> FailC m a)
-> (forall a. FailC m a -> FailC m [a])
-> (forall a. FailC m a -> FailC m [a])
-> Alternative (FailC m)
FailC m a -> FailC m a -> FailC m a
FailC m a -> FailC m [a]
FailC m a -> FailC m [a]
forall a. FailC m a
forall a. FailC m a -> FailC m [a]
forall a. FailC m a -> FailC m a -> FailC 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
forall (m :: * -> *).
(Alternative m, Monad m) =>
Applicative (FailC m)
forall (m :: * -> *) a. (Alternative m, Monad m) => FailC m a
forall (m :: * -> *) a.
(Alternative m, Monad m) =>
FailC m a -> FailC m [a]
forall (m :: * -> *) a.
(Alternative m, Monad m) =>
FailC m a -> FailC m a -> FailC m a
many :: FailC m a -> FailC m [a]
$cmany :: forall (m :: * -> *) a.
(Alternative m, Monad m) =>
FailC m a -> FailC m [a]
some :: FailC m a -> FailC m [a]
$csome :: forall (m :: * -> *) a.
(Alternative m, Monad m) =>
FailC m a -> FailC m [a]
<|> :: FailC m a -> FailC m a -> FailC m a
$c<|> :: forall (m :: * -> *) a.
(Alternative m, Monad m) =>
FailC m a -> FailC m a -> FailC m a
empty :: FailC m a
$cempty :: forall (m :: * -> *) a. (Alternative m, Monad m) => FailC m a
$cp1Alternative :: forall (m :: * -> *).
(Alternative m, Monad m) =>
Applicative (FailC m)
Alternative, Functor (FailC m)
a -> FailC m a
Functor (FailC m) =>
(forall a. a -> FailC m a)
-> (forall a b. FailC m (a -> b) -> FailC m a -> FailC m b)
-> (forall a b c.
    (a -> b -> c) -> FailC m a -> FailC m b -> FailC m c)
-> (forall a b. FailC m a -> FailC m b -> FailC m b)
-> (forall a b. FailC m a -> FailC m b -> FailC m a)
-> Applicative (FailC m)
FailC m a -> FailC m b -> FailC m b
FailC m a -> FailC m b -> FailC m a
FailC m (a -> b) -> FailC m a -> FailC m b
(a -> b -> c) -> FailC m a -> FailC m b -> FailC m c
forall a. a -> FailC m a
forall a b. FailC m a -> FailC m b -> FailC m a
forall a b. FailC m a -> FailC m b -> FailC m b
forall a b. FailC m (a -> b) -> FailC m a -> FailC m b
forall a b c. (a -> b -> c) -> FailC m a -> FailC m b -> FailC m c
forall (m :: * -> *). Monad m => Functor (FailC m)
forall (m :: * -> *) a. Monad m => a -> FailC m a
forall (m :: * -> *) a b.
Monad m =>
FailC m a -> FailC m b -> FailC m a
forall (m :: * -> *) a b.
Monad m =>
FailC m a -> FailC m b -> FailC m b
forall (m :: * -> *) a b.
Monad m =>
FailC m (a -> b) -> FailC m a -> FailC m b
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> FailC m a -> FailC m b -> FailC 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
<* :: FailC m a -> FailC m b -> FailC m a
$c<* :: forall (m :: * -> *) a b.
Monad m =>
FailC m a -> FailC m b -> FailC m a
*> :: FailC m a -> FailC m b -> FailC m b
$c*> :: forall (m :: * -> *) a b.
Monad m =>
FailC m a -> FailC m b -> FailC m b
liftA2 :: (a -> b -> c) -> FailC m a -> FailC m b -> FailC m c
$cliftA2 :: forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> FailC m a -> FailC m b -> FailC m c
<*> :: FailC m (a -> b) -> FailC m a -> FailC m b
$c<*> :: forall (m :: * -> *) a b.
Monad m =>
FailC m (a -> b) -> FailC m a -> FailC m b
pure :: a -> FailC m a
$cpure :: forall (m :: * -> *) a. Monad m => a -> FailC m a
$cp1Applicative :: forall (m :: * -> *). Monad m => Functor (FailC m)
Applicative, a -> FailC m b -> FailC m a
(a -> b) -> FailC m a -> FailC m b
(forall a b. (a -> b) -> FailC m a -> FailC m b)
-> (forall a b. a -> FailC m b -> FailC m a) -> Functor (FailC m)
forall a b. a -> FailC m b -> FailC m a
forall a b. (a -> b) -> FailC m a -> FailC m b
forall (m :: * -> *) a b. Functor m => a -> FailC m b -> FailC m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> FailC m a -> FailC m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> FailC m b -> FailC m a
$c<$ :: forall (m :: * -> *) a b. Functor m => a -> FailC m b -> FailC m a
fmap :: (a -> b) -> FailC m a -> FailC m b
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> FailC m a -> FailC m b
Functor, Applicative (FailC m)
a -> FailC m a
Applicative (FailC m) =>
(forall a b. FailC m a -> (a -> FailC m b) -> FailC m b)
-> (forall a b. FailC m a -> FailC m b -> FailC m b)
-> (forall a. a -> FailC m a)
-> Monad (FailC m)
FailC m a -> (a -> FailC m b) -> FailC m b
FailC m a -> FailC m b -> FailC m b
forall a. a -> FailC m a
forall a b. FailC m a -> FailC m b -> FailC m b
forall a b. FailC m a -> (a -> FailC m b) -> FailC m b
forall (m :: * -> *). Monad m => Applicative (FailC m)
forall (m :: * -> *) a. Monad m => a -> FailC m a
forall (m :: * -> *) a b.
Monad m =>
FailC m a -> FailC m b -> FailC m b
forall (m :: * -> *) a b.
Monad m =>
FailC m a -> (a -> FailC m b) -> FailC 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 -> FailC m a
$creturn :: forall (m :: * -> *) a. Monad m => a -> FailC m a
>> :: FailC m a -> FailC m b -> FailC m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
FailC m a -> FailC m b -> FailC m b
>>= :: FailC m a -> (a -> FailC m b) -> FailC m b
$c>>= :: forall (m :: * -> *) a b.
Monad m =>
FailC m a -> (a -> FailC m b) -> FailC m b
$cp1Monad :: forall (m :: * -> *). Monad m => Applicative (FailC m)
Monad, Monad (FailC m)
Monad (FailC m) =>
(forall a. (a -> FailC m a) -> FailC m a) -> MonadFix (FailC m)
(a -> FailC m a) -> FailC m a
forall a. (a -> FailC m a) -> FailC m a
forall (m :: * -> *).
Monad m =>
(forall a. (a -> m a) -> m a) -> MonadFix m
forall (m :: * -> *). MonadFix m => Monad (FailC m)
forall (m :: * -> *) a. MonadFix m => (a -> FailC m a) -> FailC m a
mfix :: (a -> FailC m a) -> FailC m a
$cmfix :: forall (m :: * -> *) a. MonadFix m => (a -> FailC m a) -> FailC m a
$cp1MonadFix :: forall (m :: * -> *). MonadFix m => Monad (FailC m)
MonadFix, Monad (FailC m)
Monad (FailC m) =>
(forall a. IO a -> FailC m a) -> MonadIO (FailC m)
IO a -> FailC m a
forall a. IO a -> FailC m a
forall (m :: * -> *).
Monad m =>
(forall a. IO a -> m a) -> MonadIO m
forall (m :: * -> *). MonadIO m => Monad (FailC m)
forall (m :: * -> *) a. MonadIO m => IO a -> FailC m a
liftIO :: IO a -> FailC m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> FailC m a
$cp1MonadIO :: forall (m :: * -> *). MonadIO m => Monad (FailC m)
MonadIO, Monad (FailC m)
Alternative (FailC m)
FailC m a
(Alternative (FailC m), Monad (FailC m)) =>
(forall a. FailC m a)
-> (forall a. FailC m a -> FailC m a -> FailC m a)
-> MonadPlus (FailC m)
FailC m a -> FailC m a -> FailC m a
forall a. FailC m a
forall a. FailC m a -> FailC m a -> FailC m a
forall (m :: * -> *). (Alternative m, Monad m) => Monad (FailC m)
forall (m :: * -> *).
(Alternative m, Monad m) =>
Alternative (FailC m)
forall (m :: * -> *).
(Alternative m, Monad m) =>
(forall a. m a) -> (forall a. m a -> m a -> m a) -> MonadPlus m
forall (m :: * -> *) a. (Alternative m, Monad m) => FailC m a
forall (m :: * -> *) a.
(Alternative m, Monad m) =>
FailC m a -> FailC m a -> FailC m a
mplus :: FailC m a -> FailC m a -> FailC m a
$cmplus :: forall (m :: * -> *) a.
(Alternative m, Monad m) =>
FailC m a -> FailC m a -> FailC m a
mzero :: FailC m a
$cmzero :: forall (m :: * -> *) a. (Alternative m, Monad m) => FailC m a
$cp2MonadPlus :: forall (m :: * -> *). (Alternative m, Monad m) => Monad (FailC m)
$cp1MonadPlus :: forall (m :: * -> *).
(Alternative m, Monad m) =>
Alternative (FailC m)
MonadPlus, m a -> FailC m a
(forall (m :: * -> *) a. Monad m => m a -> FailC m a)
-> MonadTrans FailC
forall (m :: * -> *) a. Monad m => m a -> FailC m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
lift :: m a -> FailC m a
$clift :: forall (m :: * -> *) a. Monad m => m a -> FailC m a
MonadTrans)

instance (Algebra sig m, Effect sig) => Fail.MonadFail (FailC m) where
  fail :: String -> FailC m a
fail = Throw String (FailC m) a -> FailC m a
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
       (m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (Throw String (FailC m) a -> FailC m a)
-> (String -> Throw String (FailC m) a) -> String -> FailC m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Throw String (FailC m) a
forall (m :: * -> *) k. String -> Fail m k
Fail
  {-# INLINE fail #-}

instance (Algebra sig m, Effect sig) => Algebra (Fail :+: sig) (FailC m) where
  alg :: (:+:) Fail sig (FailC m) a -> FailC m a
alg = ThrowC String m a -> FailC m a
forall (m :: * -> *) a. ThrowC String m a -> FailC m a
FailC (ThrowC String m a -> FailC m a)
-> ((:+:) Fail sig (FailC m) a -> ThrowC String m a)
-> (:+:) Fail sig (FailC m) a
-> FailC m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:+:) Fail sig (ThrowC String m) a -> ThrowC String m a
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Algebra sig m =>
sig m a -> m a
alg ((:+:) Fail sig (ThrowC String m) a -> ThrowC String m a)
-> ((:+:) Fail sig (FailC m) a
    -> (:+:) Fail sig (ThrowC String m) a)
-> (:+:) Fail sig (FailC m) a
-> ThrowC String m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:+:) Fail sig (FailC m) a -> (:+:) Fail sig (ThrowC String m) a
forall (sig :: (* -> *) -> * -> *) (f :: * -> *) (g :: * -> *) a.
(HFunctor sig, Functor f, Coercible f g) =>
sig f a -> sig g a
handleCoercible
  {-# INLINE alg #-}