simple-effects-0.10.0.0: A simple effect system that integrates with MTL

Safe HaskellNone
LanguageHaskell2010

Control.Effects.Signal

Description

This effect allows you to "throw" a signal. For the most part signals are the same as checked exceptions. The difference here is that the handler has the option to provide the value that will be the result of calling the signal function. This effectively allows you to have recoverable exceptions at the throw site, instead of just at the handling site.

Synopsis

Documentation

data ResumeOrBreak b c Source #

The handle function will return a value of this type.

Constructors

Resume b

Give a value to the caller of signal and keep going.

Break c

Continue the execution after the handler. The handler will return this value

data Signal a b Source #

Instances

Generic (EffMethods (Signal a b) m) Source # 

Associated Types

type Rep (EffMethods (Signal a b) m) :: * -> * #

Methods

from :: EffMethods (Signal a b) m -> Rep (EffMethods (Signal a b) m) x #

to :: Rep (EffMethods (Signal a b) m) x -> EffMethods (Signal a b) m #

Effect (Signal a b) Source # 

Associated Types

data EffMethods (Signal a b) (m :: * -> *) :: * Source #

type CanLift (Signal a b) (t :: (* -> *) -> * -> *) :: Constraint Source #

Methods

liftThrough :: (CanLift (Signal a b) t, Monad m, Monad (t m)) => EffMethods (Signal a b) m -> EffMethods (Signal a b) (t m) Source #

mergeContext :: Monad m => m (EffMethods (Signal a b) m) -> EffMethods (Signal a b) m Source #

TypeError Constraint (UnhandledError a b) => MonadEffect (Signal a b) IO Source # 

Methods

effect :: EffMethods (Signal a b) IO Source #

Monad m => MonadEffect (Signal a b) (MaybeT m) Source # 

Methods

effect :: EffMethods (Signal a b) (MaybeT m) Source #

(Monad m, (~) * b c) => MonadEffect (Signal a c) (RuntimeImplemented (Signal a b) m) Source # 
(Show e, Monad m) => MonadEffect (Signal e b) (ExceptT SomeSignal m) Source # 
Monad m => MonadEffect (Signal e b) (ExceptT e m) Source # 

Methods

effect :: EffMethods (Signal e b) (ExceptT e m) Source #

type Rep (EffMethods (Signal a b) m) Source # 
type Rep (EffMethods (Signal a b) m) = D1 * (MetaData "EffMethods" "Control.Effects.Signal" "simple-effects-0.10.0.0-JYzNQJPZcR6HRHgMmvCghQ" False) (C1 * (MetaCons "SignalMethods" PrefixI True) (S1 * (MetaSel (Just Symbol "_signal") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 * (a -> m b))))
data EffMethods (Signal a b) Source # 
data EffMethods (Signal a b) = SignalMethods {}
type CanLift (Signal a b) t Source # 
type CanLift (Signal a b) t = MonadTrans t

throwSignal :: MonadEffect (Throw a) m => a -> m b Source #

Throw a signal with no possible recovery. The handler is forced to only return the Break constructor because it cannot construct a Void value.

If this function is used along with handleAsException, this module behaves like regular checked exceptions.

handleSignal :: forall a b c m. Monad m => (a -> m (ResumeOrBreak b c)) -> RuntimeImplemented (Signal a b) (ExceptT c m) c -> m c Source #

Handle signals of a computation. The handler function has the option to provide a value to the caller of signal and continue execution there, or do what regular exception handlers do and continue execution after the handler.

type Throw e = Signal e Void Source #

handleException :: forall a c m. Monad m => (a -> m c) -> ExceptT a m c -> m c Source #

This handler can only behave like a regular exception handler. If used along with throwSignal this module behaves like regular checked exceptions.

handleToEither :: forall e a m. ExceptT e m a -> m (Either e a) Source #

See documentation for handleException. This handler gives you an Either.

newtype MaybeT (m :: * -> *) a :: (* -> *) -> * -> * #

The parameterizable maybe monad, obtained by composing an arbitrary monad with the Maybe monad.

Computations are actions that may produce a value or exit.

The return function yields a computation that produces that value, while >>= sequences two subcomputations, exiting if either computation does.

Constructors

MaybeT 

Fields

Instances

MonadTrans MaybeT 

Methods

lift :: Monad m => m a -> MaybeT m a #

MonadTransControl MaybeT 

Associated Types

type StT (MaybeT :: (* -> *) -> * -> *) a :: * #

Methods

liftWith :: Monad m => (Run MaybeT -> m a) -> MaybeT m a #

restoreT :: Monad m => m (StT MaybeT a) -> MaybeT m a #

RunnableTrans MaybeT Source # 

Associated Types

type TransformerState (MaybeT :: (* -> *) -> * -> *) (m :: * -> *) :: * Source #

type TransformerResult (MaybeT :: (* -> *) -> * -> *) (m :: * -> *) a :: * Source #

MonadBase b m => MonadBase b (MaybeT m) 

Methods

liftBase :: b α -> MaybeT m α #

MonadBaseControl b m => MonadBaseControl b (MaybeT m) 

Associated Types

type StM (MaybeT m :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (MaybeT m) b -> b a) -> MaybeT m a #

restoreM :: StM (MaybeT m) a -> MaybeT m a #

MonadState s m => MonadState s (MaybeT m) 

Methods

get :: MaybeT m s #

put :: s -> MaybeT m () #

state :: (s -> (a, s)) -> MaybeT m a #

MonadReader r m => MonadReader r (MaybeT m) 

Methods

ask :: MaybeT m r #

local :: (r -> r) -> MaybeT m a -> MaybeT m a #

reader :: (r -> a) -> MaybeT m a #

Monad m => Monad (MaybeT m) 

Methods

(>>=) :: MaybeT m a -> (a -> MaybeT m b) -> MaybeT m b #

(>>) :: MaybeT m a -> MaybeT m b -> MaybeT m b #

return :: a -> MaybeT m a #

fail :: String -> MaybeT m a #

Functor m => Functor (MaybeT m) 

Methods

fmap :: (a -> b) -> MaybeT m a -> MaybeT m b #

(<$) :: a -> MaybeT m b -> MaybeT m a #

MonadFix m => MonadFix (MaybeT m) 

Methods

mfix :: (a -> MaybeT m a) -> MaybeT m a #

Monad m => MonadFail (MaybeT m) 

Methods

fail :: String -> MaybeT m a #

(Functor m, Monad m) => Applicative (MaybeT m) 

Methods

pure :: a -> MaybeT m a #

(<*>) :: MaybeT m (a -> b) -> MaybeT m a -> MaybeT m b #

liftA2 :: (a -> b -> c) -> MaybeT m a -> MaybeT m b -> MaybeT m c #

(*>) :: MaybeT m a -> MaybeT m b -> MaybeT m b #

(<*) :: MaybeT m a -> MaybeT m b -> MaybeT m a #

Foldable f => Foldable (MaybeT f) 

Methods

fold :: Monoid m => MaybeT f m -> m #

foldMap :: Monoid m => (a -> m) -> MaybeT f a -> m #

foldr :: (a -> b -> b) -> b -> MaybeT f a -> b #

foldr' :: (a -> b -> b) -> b -> MaybeT f a -> b #

foldl :: (b -> a -> b) -> b -> MaybeT f a -> b #

foldl' :: (b -> a -> b) -> b -> MaybeT f a -> b #

foldr1 :: (a -> a -> a) -> MaybeT f a -> a #

foldl1 :: (a -> a -> a) -> MaybeT f a -> a #

toList :: MaybeT f a -> [a] #

null :: MaybeT f a -> Bool #

length :: MaybeT f a -> Int #

elem :: Eq a => a -> MaybeT f a -> Bool #

maximum :: Ord a => MaybeT f a -> a #

minimum :: Ord a => MaybeT f a -> a #

sum :: Num a => MaybeT f a -> a #

product :: Num a => MaybeT f a -> a #

Traversable f => Traversable (MaybeT f) 

Methods

traverse :: Applicative f => (a -> f b) -> MaybeT f a -> f (MaybeT f b) #

sequenceA :: Applicative f => MaybeT f (f a) -> f (MaybeT f a) #

mapM :: Monad m => (a -> m b) -> MaybeT f a -> m (MaybeT f b) #

sequence :: Monad m => MaybeT f (m a) -> m (MaybeT f a) #

Eq1 m => Eq1 (MaybeT m) 

Methods

liftEq :: (a -> b -> Bool) -> MaybeT m a -> MaybeT m b -> Bool #

Ord1 m => Ord1 (MaybeT m) 

Methods

liftCompare :: (a -> b -> Ordering) -> MaybeT m a -> MaybeT m b -> Ordering #

Read1 m => Read1 (MaybeT m) 

Methods

liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (MaybeT m a) #

liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [MaybeT m a] #

liftReadPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec (MaybeT m a) #

liftReadListPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec [MaybeT m a] #

Show1 m => Show1 (MaybeT m) 

Methods

liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> MaybeT m a -> ShowS #

liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [MaybeT m a] -> ShowS #

MonadZip m => MonadZip (MaybeT m) 

Methods

mzip :: MaybeT m a -> MaybeT m b -> MaybeT m (a, b) #

mzipWith :: (a -> b -> c) -> MaybeT m a -> MaybeT m b -> MaybeT m c #

munzip :: MaybeT m (a, b) -> (MaybeT m a, MaybeT m b) #

MonadIO m => MonadIO (MaybeT m) 

Methods

liftIO :: IO a -> MaybeT m a #

(Functor m, Monad m) => Alternative (MaybeT m) 

Methods

empty :: MaybeT m a #

(<|>) :: MaybeT m a -> MaybeT m a -> MaybeT m a #

some :: MaybeT m a -> MaybeT m [a] #

many :: MaybeT m a -> MaybeT m [a] #

Monad m => MonadPlus (MaybeT m) 

Methods

mzero :: MaybeT m a #

mplus :: MaybeT m a -> MaybeT m a -> MaybeT m a #

MonadThrow m => MonadThrow (MaybeT m)

Throws exceptions into the base monad.

Methods

throwM :: Exception e => e -> MaybeT m a #

MonadCatch m => MonadCatch (MaybeT m)

Catches exceptions from the base monad.

Methods

catch :: Exception e => MaybeT m a -> (e -> MaybeT m a) -> MaybeT m a #

(Eq1 m, Eq a) => Eq (MaybeT m a) 

Methods

(==) :: MaybeT m a -> MaybeT m a -> Bool #

(/=) :: MaybeT m a -> MaybeT m a -> Bool #

(Ord1 m, Ord a) => Ord (MaybeT m a) 

Methods

compare :: MaybeT m a -> MaybeT m a -> Ordering #

(<) :: MaybeT m a -> MaybeT m a -> Bool #

(<=) :: MaybeT m a -> MaybeT m a -> Bool #

(>) :: MaybeT m a -> MaybeT m a -> Bool #

(>=) :: MaybeT m a -> MaybeT m a -> Bool #

max :: MaybeT m a -> MaybeT m a -> MaybeT m a #

min :: MaybeT m a -> MaybeT m a -> MaybeT m a #

(Read1 m, Read a) => Read (MaybeT m a) 
(Show1 m, Show a) => Show (MaybeT m a) 

Methods

showsPrec :: Int -> MaybeT m a -> ShowS #

show :: MaybeT m a -> String #

showList :: [MaybeT m a] -> ShowS #

Monad m => MonadEffect (Signal a b) (MaybeT m) Source # 

Methods

effect :: EffMethods (Signal a b) (MaybeT m) Source #

type StT MaybeT a 
type StT MaybeT a = Maybe a
type TransformerState MaybeT m Source # 
type TransformerResult MaybeT m a Source # 
type StM (MaybeT m) a 
type StM (MaybeT m) a = ComposeSt MaybeT m a

discardAllExceptions :: MaybeT m a -> m (Maybe a) Source #

Discard all the Throw and Signal effects. If any exception was thrown the result will be Nothing.

showAllExceptions :: Functor m => ExceptT SomeSignal m a -> m (Either Text a) Source #

Satisfies all the Throw and Signal constraints if they all throw Showable exceptions. The first thrown exception will be shown and returned as a Left result.

handleWithoutDiscarding :: forall e m a. MonadEffect (HandleException e) m => (e -> m a) -> m a -> m a Source #

Use this function to handle exceptions without discarding the Throw effect. You'll want to use this if you're writing a recursive function. Using the regular handlers in that case will result with infinite types.

Since this function doesn't discard constraints, you still need to handle the exception on the whole computation.

Here's a slightly contrived example.

  data NotFound = NotFound
  data Tree a = Leaf a | Node (Tree a) (Tree a)
  data Step = GoLeft | GoRight
  findIndex :: (Handles NotFound m, Eq a) => a -> Tree a -> m [Step]
  findIndex x (Leaf a) | x == a    = return []
                       | otherwise = throwSignal NotFound
  findIndex x (Node l r) = ((GoLeft :) $ findIndex x l)
      & handleWithoutDiscarding (NotFound -> (GoRight :) $ findIndex x r)

Note: When you finally handle the exception effect, the order in which you handle it and other effects determines whether handleWithoutDiscarding rolls back other effects if an exception occured or it preserves all of them up to the point of the exception. Handling exceptions last and handling them first will produce the former and latter behaviour respectively.

handleToEitherRecursive :: MonadEffect (HandleException e) m => m a -> m (Either e a) Source #

handleToEither that doesn't discard Throws constraints. See documentation for handleWithoutDiscarding.

signal :: forall a b m. MonadEffect (Signal a b) m => a -> m b Source #