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

-- | A carrier for an 'Error' effect.
--
-- @since 1.0.0.0
module Control.Carrier.Error.Either
( -- * Error carrier
  runError
, ErrorC(..)
  -- * Error effect
, module Control.Effect.Error
) where

import Control.Algebra
import Control.Applicative (Alternative(..))
import Control.Effect.Error
import Control.Monad (MonadPlus)
import Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Control.Monad.Trans.Except

-- | Run an 'Error' effect, returning uncaught errors in 'Left' and successful computations’ values in 'Right'.
--
-- @
-- 'runError' ('pure' a) = 'pure' ('Right' a)
-- @
-- @
-- 'runError' ('throwError' e) = 'pure' ('Left' e)
-- @
-- @
-- 'runError' ('throwError' e \`'catchError'\` 'pure') = 'pure' ('Right' e)
-- @
--
-- @since 0.1.0.0
runError :: ErrorC exc m a -> m (Either exc a)
runError :: ErrorC exc m a -> m (Either exc a)
runError (ErrorC ExceptT exc m a
m) = ExceptT exc m a -> m (Either exc a)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT ExceptT exc m a
m
{-# INLINE runError #-}

-- | @since 0.1.0.0
newtype ErrorC e m a = ErrorC (ExceptT e m a)
  deriving (Algebra (Error e :+: sig), Functor (ErrorC e m)
a -> ErrorC e m a
Functor (ErrorC e m)
-> (forall a. a -> ErrorC e m a)
-> (forall a b.
    ErrorC e m (a -> b) -> ErrorC e m a -> ErrorC e m b)
-> (forall a b c.
    (a -> b -> c) -> ErrorC e m a -> ErrorC e m b -> ErrorC e m c)
-> (forall a b. ErrorC e m a -> ErrorC e m b -> ErrorC e m b)
-> (forall a b. ErrorC e m a -> ErrorC e m b -> ErrorC e m a)
-> Applicative (ErrorC e m)
ErrorC e m a -> ErrorC e m b -> ErrorC e m b
ErrorC e m a -> ErrorC e m b -> ErrorC e m a
ErrorC e m (a -> b) -> ErrorC e m a -> ErrorC e m b
(a -> b -> c) -> ErrorC e m a -> ErrorC e m b -> ErrorC e m c
forall a. a -> ErrorC e m a
forall a b. ErrorC e m a -> ErrorC e m b -> ErrorC e m a
forall a b. ErrorC e m a -> ErrorC e m b -> ErrorC e m b
forall a b. ErrorC e m (a -> b) -> ErrorC e m a -> ErrorC e m b
forall a b c.
(a -> b -> c) -> ErrorC e m a -> ErrorC e m b -> ErrorC e m c
forall e (m :: * -> *). Monad m => Functor (ErrorC e m)
forall e (m :: * -> *) a. Monad m => a -> ErrorC e m a
forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m a -> ErrorC e m b -> ErrorC e m a
forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m a -> ErrorC e m b -> ErrorC e m b
forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m (a -> b) -> ErrorC e m a -> ErrorC e m b
forall e (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> ErrorC e m a -> ErrorC e m b -> ErrorC 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
<* :: ErrorC e m a -> ErrorC e m b -> ErrorC e m a
$c<* :: forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m a -> ErrorC e m b -> ErrorC e m a
*> :: ErrorC e m a -> ErrorC e m b -> ErrorC e m b
$c*> :: forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m a -> ErrorC e m b -> ErrorC e m b
liftA2 :: (a -> b -> c) -> ErrorC e m a -> ErrorC e m b -> ErrorC e m c
$cliftA2 :: forall e (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> ErrorC e m a -> ErrorC e m b -> ErrorC e m c
<*> :: ErrorC e m (a -> b) -> ErrorC e m a -> ErrorC e m b
$c<*> :: forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m (a -> b) -> ErrorC e m a -> ErrorC e m b
pure :: a -> ErrorC e m a
$cpure :: forall e (m :: * -> *) a. Monad m => a -> ErrorC e m a
$cp1Applicative :: forall e (m :: * -> *). Monad m => Functor (ErrorC e m)
Applicative, a -> ErrorC e m b -> ErrorC e m a
(a -> b) -> ErrorC e m a -> ErrorC e m b
(forall a b. (a -> b) -> ErrorC e m a -> ErrorC e m b)
-> (forall a b. a -> ErrorC e m b -> ErrorC e m a)
-> Functor (ErrorC e m)
forall a b. a -> ErrorC e m b -> ErrorC e m a
forall a b. (a -> b) -> ErrorC e m a -> ErrorC e m b
forall e (m :: * -> *) a b.
Functor m =>
a -> ErrorC e m b -> ErrorC e m a
forall e (m :: * -> *) a b.
Functor m =>
(a -> b) -> ErrorC e m a -> ErrorC 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 -> ErrorC e m b -> ErrorC e m a
$c<$ :: forall e (m :: * -> *) a b.
Functor m =>
a -> ErrorC e m b -> ErrorC e m a
fmap :: (a -> b) -> ErrorC e m a -> ErrorC e m b
$cfmap :: forall e (m :: * -> *) a b.
Functor m =>
(a -> b) -> ErrorC e m a -> ErrorC e m b
Functor, Applicative (ErrorC e m)
a -> ErrorC e m a
Applicative (ErrorC e m)
-> (forall a b.
    ErrorC e m a -> (a -> ErrorC e m b) -> ErrorC e m b)
-> (forall a b. ErrorC e m a -> ErrorC e m b -> ErrorC e m b)
-> (forall a. a -> ErrorC e m a)
-> Monad (ErrorC e m)
ErrorC e m a -> (a -> ErrorC e m b) -> ErrorC e m b
ErrorC e m a -> ErrorC e m b -> ErrorC e m b
forall a. a -> ErrorC e m a
forall a b. ErrorC e m a -> ErrorC e m b -> ErrorC e m b
forall a b. ErrorC e m a -> (a -> ErrorC e m b) -> ErrorC e m b
forall e (m :: * -> *). Monad m => Applicative (ErrorC e m)
forall e (m :: * -> *) a. Monad m => a -> ErrorC e m a
forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m a -> ErrorC e m b -> ErrorC e m b
forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m a -> (a -> ErrorC e m b) -> ErrorC 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 -> ErrorC e m a
$creturn :: forall e (m :: * -> *) a. Monad m => a -> ErrorC e m a
>> :: ErrorC e m a -> ErrorC e m b -> ErrorC e m b
$c>> :: forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m a -> ErrorC e m b -> ErrorC e m b
>>= :: ErrorC e m a -> (a -> ErrorC e m b) -> ErrorC e m b
$c>>= :: forall e (m :: * -> *) a b.
Monad m =>
ErrorC e m a -> (a -> ErrorC e m b) -> ErrorC e m b
$cp1Monad :: forall e (m :: * -> *). Monad m => Applicative (ErrorC e m)
Monad, Monad (ErrorC e m)
Monad (ErrorC e m)
-> (forall a. String -> ErrorC e m a) -> MonadFail (ErrorC e m)
String -> ErrorC e m a
forall a. String -> ErrorC e m a
forall e (m :: * -> *). MonadFail m => Monad (ErrorC e m)
forall e (m :: * -> *) a. MonadFail m => String -> ErrorC e m a
forall (m :: * -> *).
Monad m -> (forall a. String -> m a) -> MonadFail m
fail :: String -> ErrorC e m a
$cfail :: forall e (m :: * -> *) a. MonadFail m => String -> ErrorC e m a
$cp1MonadFail :: forall e (m :: * -> *). MonadFail m => Monad (ErrorC e m)
Fail.MonadFail, Monad (ErrorC e m)
Monad (ErrorC e m)
-> (forall a. (a -> ErrorC e m a) -> ErrorC e m a)
-> MonadFix (ErrorC e m)
(a -> ErrorC e m a) -> ErrorC e m a
forall a. (a -> ErrorC e m a) -> ErrorC e m a
forall e (m :: * -> *). MonadFix m => Monad (ErrorC e m)
forall e (m :: * -> *) a.
MonadFix m =>
(a -> ErrorC e m a) -> ErrorC e m a
forall (m :: * -> *).
Monad m -> (forall a. (a -> m a) -> m a) -> MonadFix m
mfix :: (a -> ErrorC e m a) -> ErrorC e m a
$cmfix :: forall e (m :: * -> *) a.
MonadFix m =>
(a -> ErrorC e m a) -> ErrorC e m a
$cp1MonadFix :: forall e (m :: * -> *). MonadFix m => Monad (ErrorC e m)
MonadFix, Monad (ErrorC e m)
Monad (ErrorC e m)
-> (forall a. IO a -> ErrorC e m a) -> MonadIO (ErrorC e m)
IO a -> ErrorC e m a
forall a. IO a -> ErrorC e m a
forall e (m :: * -> *). MonadIO m => Monad (ErrorC e m)
forall e (m :: * -> *) a. MonadIO m => IO a -> ErrorC e m a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
liftIO :: IO a -> ErrorC e m a
$cliftIO :: forall e (m :: * -> *) a. MonadIO m => IO a -> ErrorC e m a
$cp1MonadIO :: forall e (m :: * -> *). MonadIO m => Monad (ErrorC e m)
MonadIO, m a -> ErrorC e m a
(forall (m :: * -> *) a. Monad m => m a -> ErrorC e m a)
-> MonadTrans (ErrorC e)
forall e (m :: * -> *) a. Monad m => m a -> ErrorC e m a
forall (m :: * -> *) a. Monad m => m a -> ErrorC e m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
lift :: m a -> ErrorC e m a
$clift :: forall e (m :: * -> *) a. Monad m => m a -> ErrorC e m a
MonadTrans)

-- | 'ErrorC' passes 'Alternative' operations along to the underlying monad @m@, rather than combining errors à la 'ExceptT'.
instance (Alternative m, Monad m) => Alternative (ErrorC e m) where
  empty :: ErrorC e m a
empty = ExceptT e m a -> ErrorC e m a
forall e (m :: * -> *) a. ExceptT e m a -> ErrorC e m a
ErrorC (m (Either e a) -> ExceptT e m a
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT m (Either e a)
forall (f :: * -> *) a. Alternative f => f a
empty)
  {-# INLINE empty #-}

  ErrorC (ExceptT m (Either e a)
l) <|> :: ErrorC e m a -> ErrorC e m a -> ErrorC e m a
<|> ErrorC (ExceptT m (Either e a)
r) = ExceptT e m a -> ErrorC e m a
forall e (m :: * -> *) a. ExceptT e m a -> ErrorC e m a
ErrorC (m (Either e a) -> ExceptT e m a
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either e a)
l m (Either e a) -> m (Either e a) -> m (Either e a)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> m (Either e a)
r))
  {-# INLINE (<|>) #-}

-- | 'ErrorC' passes 'MonadPlus' operations along to the underlying monad @m@, rather than combining errors à la 'ExceptT'.
instance (Alternative m, Monad m) => MonadPlus (ErrorC e m)