{-# LANGUAGE DeriveFunctor, ExistentialQuantification, FlexibleContexts, FlexibleInstances, MultiParamTypeClasses, StandaloneDeriving, TypeOperators, UndecidableInstances #-}
module Control.Effect.Error
( -- * Error effect
  Error(..)
, throwError
, catchError
  -- * Error carrier
, runError
, ErrorC(..)
  -- * Re-exports
, Carrier
, Member
, run
) where

import Control.Applicative (Alternative(..), liftA2)
import Control.Effect.Carrier
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

data Error exc m k
  = Throw exc
  | forall b . Catch (m b) (exc -> m b) (b -> m k)

deriving instance Functor m => Functor (Error exc m)

instance HFunctor (Error exc) where
  hmap :: (forall x. m x -> n x) -> Error exc m a -> Error exc n a
hmap _ (Throw exc :: exc
exc)   = exc -> Error exc n a
forall exc (m :: * -> *) k. exc -> Error exc m k
Throw exc
exc
  hmap f :: forall x. m x -> n x
f (Catch m :: m b
m h :: exc -> m b
h k :: b -> m a
k) = n b -> (exc -> n b) -> (b -> n a) -> Error exc n a
forall exc (m :: * -> *) k b.
m b -> (exc -> m b) -> (b -> m k) -> Error exc m k
Catch (m b -> n b
forall x. m x -> n x
f m b
m) (m b -> n b
forall x. m x -> n x
f (m b -> n b) -> (exc -> m b) -> exc -> n b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. exc -> m b
h) (m a -> n a
forall x. m x -> n x
f (m a -> n a) -> (b -> m a) -> b -> n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> m a
k)

instance Effect (Error exc) where
  handle :: f ()
-> (forall x. f (m x) -> n (f x))
-> Error exc m a
-> Error exc n (f a)
handle _     _       (Throw exc :: exc
exc)   = exc -> Error exc n (f a)
forall exc (m :: * -> *) k. exc -> Error exc m k
Throw exc
exc
  handle state :: f ()
state handler :: forall x. f (m x) -> n (f x)
handler (Catch m :: m b
m h :: exc -> m b
h k :: b -> m a
k) = n (f b)
-> (exc -> n (f b)) -> (f b -> n (f a)) -> Error exc n (f a)
forall exc (m :: * -> *) k b.
m b -> (exc -> m b) -> (b -> m k) -> Error exc m k
Catch (f (m b) -> n (f b)
forall x. f (m x) -> n (f x)
handler (m b
m m b -> f () -> f (m b)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f ()
state)) (f (m b) -> n (f b)
forall x. f (m x) -> n (f x)
handler (f (m b) -> n (f b)) -> (exc -> f (m b)) -> exc -> n (f b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (m b -> f () -> f (m b)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f ()
state) (m b -> f (m b)) -> (exc -> m b) -> exc -> f (m b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. exc -> m b
h) (f (m a) -> n (f a)
forall x. f (m x) -> n (f x)
handler (f (m a) -> n (f a)) -> (f b -> f (m a)) -> f b -> n (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b -> m a) -> f b -> f (m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> m a
k)

-- | Throw an error, escaping the current computation up to the nearest 'catchError' (if any).
--
--   prop> run (runError (throwError a)) === Left @Int @Int a
throwError :: (Member (Error exc) sig, Carrier sig m) => exc -> m a
throwError :: exc -> m a
throwError = Error exc m a -> m a
forall (effect :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
       (m :: * -> *) a.
(Member effect sig, Carrier sig m) =>
effect m a -> m a
send (Error exc m a -> m a) -> (exc -> Error exc m a) -> exc -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. exc -> Error exc m a
forall exc (m :: * -> *) k. exc -> Error exc m k
Throw

-- | Run a computation which can throw errors with a handler to run on error.
--
-- Errors thrown by the handler will escape up to the nearest enclosing 'catchError' (if any).
-- Note that this effect does /not/ handle errors thrown from impure contexts such as IO,
-- nor will it handle exceptions thrown from pure code. If you need to handle IO-based errors,
-- consider if 'Control.Effect.Resource' fits your use case; if not, use 'liftIO' with
-- 'Control.Exception.try' or use 'Control.Exception.Catch' from outside the effect invocation.
--
--   prop> run (runError (pure a `catchError` pure)) === Right a
--   prop> run (runError (throwError a `catchError` pure)) === Right @Int @Int a
--   prop> run (runError (throwError a `catchError` (throwError @Int))) === Left @Int @Int a
catchError :: (Member (Error exc) sig, Carrier sig m) => m a -> (exc -> m a) -> m a
catchError :: m a -> (exc -> m a) -> m a
catchError m :: m a
m h :: exc -> m a
h = Error exc m a -> m a
forall (effect :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
       (m :: * -> *) a.
(Member effect sig, Carrier sig m) =>
effect m a -> m a
send (m a -> (exc -> m a) -> (a -> m a) -> Error exc m a
forall exc (m :: * -> *) k b.
m b -> (exc -> m b) -> (b -> m k) -> Error exc m k
Catch m a
m exc -> m a
h a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure)


-- | Run an 'Error' effect, returning uncaught errors in 'Left' and successful computations’ values in 'Right'.
--
--   prop> run (runError (pure a)) === Right @Int @Int a
runError :: ErrorC exc m a -> m (Either exc a)
runError :: ErrorC exc m a -> m (Either exc a)
runError = ErrorC exc m a -> m (Either exc a)
forall e (m :: * -> *) a. ErrorC e m a -> m (Either e a)
runErrorC

newtype ErrorC e m a = ErrorC { ErrorC e m a -> m (Either e a)
runErrorC :: m (Either e a) }
  deriving (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)

instance Applicative m => Applicative (ErrorC e m) where
  pure :: a -> ErrorC e m a
pure a :: a
a = m (Either e a) -> ErrorC e m a
forall e (m :: * -> *) a. m (Either e a) -> ErrorC e m a
ErrorC (Either e a -> m (Either e a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Either e a
forall a b. b -> Either a b
Right a
a))
  {-# INLINE pure #-}
  ErrorC f :: m (Either e (a -> b))
f <*> :: ErrorC e m (a -> b) -> ErrorC e m a -> ErrorC e m b
<*> ErrorC a :: m (Either e a)
a = m (Either e b) -> ErrorC e m b
forall e (m :: * -> *) a. m (Either e a) -> ErrorC e m a
ErrorC ((Either e (a -> b) -> Either e a -> Either e b)
-> m (Either e (a -> b)) -> m (Either e a) -> m (Either e b)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 Either e (a -> b) -> Either e a -> Either e b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>) m (Either e (a -> b))
f m (Either e a)
a)
  {-# INLINE (<*>) #-}

instance Alternative m => Alternative (ErrorC e m) where
  empty :: ErrorC e m a
empty = m (Either e a) -> ErrorC e m a
forall e (m :: * -> *) a. m (Either e a) -> ErrorC e m a
ErrorC m (Either e a)
forall (f :: * -> *) a. Alternative f => f a
empty
  {-# INLINE empty #-}
  ErrorC l :: m (Either e a)
l <|> :: ErrorC e m a -> ErrorC e m a -> ErrorC e m a
<|> ErrorC r :: m (Either e a)
r = m (Either e a) -> ErrorC e m a
forall e (m :: * -> *) a. m (Either e a) -> ErrorC e m a
ErrorC (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 (<|>) #-}

instance Monad m => Monad (ErrorC e m) where
  ErrorC a :: m (Either e a)
a >>= :: ErrorC e m a -> (a -> ErrorC e m b) -> ErrorC e m b
>>= f :: a -> ErrorC e m b
f = m (Either e b) -> ErrorC e m b
forall e (m :: * -> *) a. m (Either e a) -> ErrorC e m a
ErrorC (m (Either e a)
a m (Either e a) -> (Either e a -> m (Either e b)) -> m (Either e b)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (e -> m (Either e b))
-> (a -> m (Either e b)) -> Either e a -> m (Either e b)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either e b -> m (Either e b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either e b -> m (Either e b))
-> (e -> Either e b) -> e -> m (Either e b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> Either e b
forall a b. a -> Either a b
Left) (ErrorC e m b -> m (Either e b)
forall e (m :: * -> *) a. ErrorC e m a -> m (Either e a)
runError (ErrorC e m b -> m (Either e b))
-> (a -> ErrorC e m b) -> a -> m (Either e b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ErrorC e m b
f))
  {-# INLINE (>>=) #-}

instance MonadFix m => MonadFix (ErrorC e m) where
  mfix :: (a -> ErrorC e m a) -> ErrorC e m a
mfix f :: a -> ErrorC e m a
f = m (Either e a) -> ErrorC e m a
forall e (m :: * -> *) a. m (Either e a) -> ErrorC e m a
ErrorC ((Either e a -> m (Either e a)) -> m (Either e a)
forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix (ErrorC e m a -> m (Either e a)
forall e (m :: * -> *) a. ErrorC e m a -> m (Either e a)
runError (ErrorC e m a -> m (Either e a))
-> (Either e a -> ErrorC e m a) -> Either e a -> m (Either e a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e -> ErrorC e m a)
-> (a -> ErrorC e m a) -> Either e a -> ErrorC e m a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ([Char] -> e -> ErrorC e m a
forall a. HasCallStack => [Char] -> a
error "mfix (ErrorC): function returned failure") a -> ErrorC e m a
f))
  {-# INLINE mfix #-}

instance MonadIO m => MonadIO (ErrorC e m) where
  liftIO :: IO a -> ErrorC e m a
liftIO io :: IO a
io = m (Either e a) -> ErrorC e m a
forall e (m :: * -> *) a. m (Either e a) -> ErrorC e m a
ErrorC (a -> Either e a
forall a b. b -> Either a b
Right (a -> Either e a) -> m a -> m (Either e a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO a
io)
  {-# INLINE liftIO #-}

instance Fail.MonadFail m => Fail.MonadFail (ErrorC e m) where
  fail :: [Char] -> ErrorC e m a
fail s :: [Char]
s = m (Either e a) -> ErrorC e m a
forall e (m :: * -> *) a. m (Either e a) -> ErrorC e m a
ErrorC ([Char] -> m (Either e a)
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
Fail.fail [Char]
s)
  {-# INLINE fail #-}

instance (Alternative m, Monad m) => MonadPlus (ErrorC e m)

instance MonadTrans (ErrorC e) where
  lift :: m a -> ErrorC e m a
lift = m (Either e a) -> ErrorC e m a
forall e (m :: * -> *) a. m (Either e a) -> ErrorC e m a
ErrorC (m (Either e a) -> ErrorC e m a)
-> (m a -> m (Either e a)) -> m a -> ErrorC e m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Either e a) -> m a -> m (Either e a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Either e a
forall a b. b -> Either a b
Right
  {-# INLINE lift #-}

instance (Carrier sig m, Effect sig) => Carrier (Error e :+: sig) (ErrorC e m) where
  eff :: (:+:) (Error e) sig (ErrorC e m) a -> ErrorC e m a
eff (L (Throw e :: e
e))     = m (Either e a) -> ErrorC e m a
forall e (m :: * -> *) a. m (Either e a) -> ErrorC e m a
ErrorC (Either e a -> m (Either e a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (e -> Either e a
forall a b. a -> Either a b
Left e
e))
  eff (L (Catch m :: ErrorC e m b
m h :: e -> ErrorC e m b
h k :: b -> ErrorC e m a
k)) = m (Either e a) -> ErrorC e m a
forall e (m :: * -> *) a. m (Either e a) -> ErrorC e m a
ErrorC (ErrorC e m b -> m (Either e b)
forall e (m :: * -> *) a. ErrorC e m a -> m (Either e a)
runError ErrorC e m b
m m (Either e b) -> (Either e b -> m (Either e a)) -> m (Either e a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (e -> m (Either e a))
-> (b -> m (Either e a)) -> Either e b -> m (Either e a)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ((e -> m (Either e a))
-> (b -> m (Either e a)) -> Either e b -> m (Either e a)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either e a -> m (Either e a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either e a -> m (Either e a))
-> (e -> Either e a) -> e -> m (Either e a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> Either e a
forall a b. a -> Either a b
Left) (ErrorC e m a -> m (Either e a)
forall e (m :: * -> *) a. ErrorC e m a -> m (Either e a)
runError (ErrorC e m a -> m (Either e a))
-> (b -> ErrorC e m a) -> b -> m (Either e a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> ErrorC e m a
k) (Either e b -> m (Either e a))
-> (e -> m (Either e b)) -> e -> m (Either e a)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< ErrorC e m b -> m (Either e b)
forall e (m :: * -> *) a. ErrorC e m a -> m (Either e a)
runError (ErrorC e m b -> m (Either e b))
-> (e -> ErrorC e m b) -> e -> m (Either e b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> ErrorC e m b
h) (ErrorC e m a -> m (Either e a)
forall e (m :: * -> *) a. ErrorC e m a -> m (Either e a)
runError (ErrorC e m a -> m (Either e a))
-> (b -> ErrorC e m a) -> b -> m (Either e a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> ErrorC e m a
k))
  eff (R other :: sig (ErrorC e m) a
other)         = m (Either e a) -> ErrorC e m a
forall e (m :: * -> *) a. m (Either e a) -> ErrorC e m a
ErrorC (sig m (Either e a) -> m (Either e a)
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Carrier sig m =>
sig m a -> m a
eff (Either e ()
-> (forall x. Either e (ErrorC e m x) -> m (Either e x))
-> sig (ErrorC e m) a
-> sig m (Either e a)
forall (sig :: (* -> *) -> * -> *) (f :: * -> *) (m :: * -> *)
       (n :: * -> *) a.
(Effect sig, Functor f, Monad m) =>
f () -> (forall x. f (m x) -> n (f x)) -> sig m a -> sig n (f a)
handle (() -> Either e ()
forall a b. b -> Either a b
Right ()) ((e -> m (Either e x))
-> (ErrorC e m x -> m (Either e x))
-> Either e (ErrorC e m x)
-> m (Either e x)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either e x -> m (Either e x)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either e x -> m (Either e x))
-> (e -> Either e x) -> e -> m (Either e x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> Either e x
forall a b. a -> Either a b
Left) ErrorC e m x -> m (Either e x)
forall e (m :: * -> *) a. ErrorC e m a -> m (Either e a)
runError) sig (ErrorC e m) a
other))
  {-# INLINE eff #-}


-- $setup
-- >>> :seti -XFlexibleContexts
-- >>> :seti -XTypeApplications
-- >>> import Test.QuickCheck
-- >>> import Control.Effect.Pure