module Control.Monad.Freer.Error
( Error(..)
, throwError
, runError
, catchError
, handleError
) where
import Control.Monad.Freer (Eff, Member, interposeWith, interpretWith, send)
import Control.Monad.Freer.Internal (handleRelay)
newtype Error e r where
Error :: e -> Error e r
throwError :: forall e effs a. Member (Error e) effs => e -> Eff effs a
throwError :: e -> Eff effs a
throwError e
e = Error e a -> Eff effs a
forall (eff :: * -> *) (effs :: [* -> *]) a.
Member eff effs =>
eff a -> Eff effs a
send (e -> Error e a
forall e r. e -> Error e r
Error e
e)
runError :: forall e effs a. Eff (Error e ': effs) a -> Eff effs (Either e a)
runError :: Eff (Error e : effs) a -> Eff effs (Either e a)
runError = (a -> Eff effs (Either e a))
-> (forall v.
Error e v -> Arr effs v (Either e a) -> Eff effs (Either e a))
-> Eff (Error e : effs) a
-> Eff effs (Either e a)
forall a (effs :: [* -> *]) b (eff :: * -> *).
(a -> Eff effs b)
-> (forall v. eff v -> Arr effs v b -> Eff effs b)
-> Eff (eff : effs) a
-> Eff effs b
handleRelay (Either e a -> Eff effs (Either e a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either e a -> Eff effs (Either e a))
-> (a -> Either e a) -> a -> Eff effs (Either e a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Either e a
forall a b. b -> Either a b
Right) (\(Error e) Arr effs v (Either e a)
_ -> Either e a -> Eff effs (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))
catchError
:: forall e effs a
. Member (Error e) effs
=> Eff effs a
-> (e -> Eff effs a)
-> Eff effs a
catchError :: Eff effs a -> (e -> Eff effs a) -> Eff effs a
catchError Eff effs a
m e -> Eff effs a
handle = (forall v. Error e v -> (v -> Eff effs a) -> Eff effs a)
-> Eff effs a -> Eff effs a
forall (eff :: * -> *) (effs :: [* -> *]) b.
Member eff effs =>
(forall v. eff v -> (v -> Eff effs b) -> Eff effs b)
-> Eff effs b -> Eff effs b
interposeWith (\(Error e) v -> Eff effs a
_ -> e -> Eff effs a
handle e
e) Eff effs a
m
handleError
:: forall e effs a
. Eff (Error e ': effs) a
-> (e -> Eff effs a)
-> Eff effs a
handleError :: Eff (Error e : effs) a -> (e -> Eff effs a) -> Eff effs a
handleError Eff (Error e : effs) a
m e -> Eff effs a
handle = (forall v. Error e v -> (v -> Eff effs a) -> Eff effs a)
-> Eff (Error e : effs) a -> Eff effs a
forall (eff :: * -> *) (effs :: [* -> *]) b.
(forall v. eff v -> (v -> Eff effs b) -> Eff effs b)
-> Eff (eff : effs) b -> Eff effs b
interpretWith (\(Error e) v -> Eff effs a
_ -> e -> Eff effs a
handle e
e) Eff (Error e : effs) a
m