module Control.Effects.Early
( module Control.Effects, Early
, earlyReturn, handleEarly, onlyDo, ifNothingEarlyReturn, ifNothingDo ) where
import Interlude
import Control.Monad.Trans.Except
import Control.Effects
data Early a
type instance EffectMsg (Early a) = a
type instance EffectRes (Early a) = Void
instance Monad m => MonadEffect (Early a) (ExceptT a m) where
effect _ = throwE
earlyReturn :: forall a b m. MonadEffect (Early a) m => a -> m b
earlyReturn = fmap absurd . effect (Proxy :: Proxy (Early a))
collapseEither :: Either a a -> a
collapseEither (Left a) = a
collapseEither (Right a) = a
handleEarly :: Monad m => ExceptT a m a -> m a
handleEarly = fmap collapseEither
. runExceptT
onlyDo :: MonadEffect (Early a) m => m a -> m b
onlyDo m = m >>= earlyReturn
ifNothingEarlyReturn :: MonadEffect (Early a) m => a -> Maybe b -> m b
ifNothingEarlyReturn a = maybe (earlyReturn a) return
ifNothingDo :: MonadEffect (Early a) m => m a -> Maybe b -> m b
ifNothingDo m = maybe (onlyDo m) return