module Control.Effects.Signal
( ResumeOrBreak(..), Signal, throwSignal, handleSignal
, Throws, handleException, handleToEither, module Control.Effects
, module Control.Monad.Trans.Except, MaybeT(..), discardAllExceptions, showAllExceptions
, Handles(..), handleToEitherRecursive, SomeSignal, signal ) where
import Import
import Control.Monad.Trans.Except
import qualified GHC.TypeLits as TL
import GHC.TypeLits (TypeError, ErrorMessage(..))
import Control.Effects
import Control.Monad.Runnable
data Signal a b = Signal
data instance Effect (Signal a b) method mr where
SignalMsg :: a -> Effect (Signal a b) 'Signal 'Msg
SignalRes :: { getSignalRes :: b } -> Effect (Signal a b) 'Signal 'Res
newtype SomeSignal = SomeSignal { getSomeSignal :: Text } deriving (Eq, Ord, Read, Show)
type family UnhandledError a b :: ErrorMessage where
UnhandledError a Void =
'TL.Text "Unhandled exception of type " ':<>: 'ShowType a
':$$: 'TL.Text "You need to handle all the exceptions before running the computation"
UnhandledError a b =
'TL.Text "Unhandled signal of type " ':<>: 'ShowType a
':<>: 'TL.Text " expecting a return value of type " ':<>: 'ShowType b
':$$: 'TL.Text "You need to handle all the signals before running the computation"
instance Monad m => MonadEffect (Signal e b) (ExceptT e m) where
effect (SignalMsg a) = throwE a
instance (Show e, Monad m) => MonadEffect (Signal e b) (ExceptT SomeSignal m) where
effect (SignalMsg a) = throwE . SomeSignal . pack . show $ a
instance Monad m => MonadEffect (Signal a b) (MaybeT m) where
effect _ = mzero
instance TypeError (UnhandledError a b)
=> MonadEffect (Signal a b) IO where
effect = undefined
instance (Monad m, b ~ c) =>
MonadEffect (Signal a c) (EffectHandler (Signal a b) m) where
effect msg = EffectHandler (ReaderT (($ msg) . getEffectWithKind))
signal :: MonadEffect (Signal a b) m => a -> m b
signal a = getSignalRes <$> effect (SignalMsg a)
type Throws e m = MonadEffect (Signal e Void) m
data ResumeOrBreak b c = Resume b
| Break c
throwSignal :: Throws a m => a -> m b
throwSignal = fmap absurd . signal
resumeOrBreak :: (b -> a) -> (c -> a) -> ResumeOrBreak b c -> a
resumeOrBreak ba _ (Resume b) = ba b
resumeOrBreak _ ca (Break c) = ca c
collapseEither :: Either a a -> a
collapseEither (Left a) = a
collapseEither (Right a) = a
handleSignal :: forall a b c m. Monad m
=> (a -> m (ResumeOrBreak b c))
-> EffectHandler (Signal a b) (ExceptT c m) c
-> m c
handleSignal f = fmap collapseEither
. runExceptT
. handleEffect h
where
h :: forall method. Effect (Signal a b) method 'Msg -> ExceptT c m (Effect (Signal a b) method 'Res)
h (SignalMsg a) = do
rb <- lift (f a)
resumeOrBreak (return . SignalRes) throwE rb
handleException :: Monad m => (a -> m c) -> ExceptT a m c -> m c
handleException f = either f return <=< runExceptT
handleToEither :: ExceptT e m a -> m (Either e a)
handleToEither = runExceptT
discardAllExceptions :: MaybeT m a -> m (Maybe a)
discardAllExceptions = runMaybeT
mapLeft :: (a -> c) -> Either a b -> Either c b
mapLeft f (Left a) = Left (f a)
mapLeft _ (Right b) = Right b
showAllExceptions :: Functor m => ExceptT SomeSignal m a -> m (Either Text a)
showAllExceptions = fmap (mapLeft getSomeSignal) . runExceptT
class Throws e m => Handles e m where
handleRecursive :: (e -> m a) -> m a -> m a
instance Monad m => Handles e (ExceptT e m) where
handleRecursive f = ExceptT . (either (runExceptT . f) (return . Right) <=< runExceptT)
instance (Monad m, Monad (t m), Handles e m, RunnableTrans t)
=> Handles e (t m) where
handleRecursive f e = do
st <- currentTransState
res <- lift (handleRecursive (\ex -> runTransformer (f ex) st) (runTransformer e st))
restoreTransState res
handleToEitherRecursive :: Handles e m => m a -> m (Either e a)
handleToEitherRecursive = handleRecursive (return . Left) . fmap Right