module Control.Eff.Exception( Exc (..)
, throwExc
, runExc
, catchExc
) where
import Data.Typeable
import Control.Eff
newtype Exc e v = Exc e
deriving (Functor, Typeable)
throwExc :: (Typeable e, Member (Exc e) r) => e -> Eff r a
throwExc e = send (\_ -> inj $ Exc e)
runExc :: Typeable e => Eff (Exc e :> r) a -> Eff r (Either e a)
runExc m = loop (admin m)
where
loop (Val x) = return (Right x)
loop (E u) = handleRelay u loop (\(Exc e) -> return (Left e))
catchExc :: (Typeable e, Member (Exc e) r)
=> Eff r a
-> (e -> Eff r a)
-> Eff r a
catchExc m handle = loop (admin m)
where
loop (Val x) = return x
loop (E u) = interpose u loop (\(Exc e) -> handle e)