-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Modular type class machinery for monad transformer stacks. -- -- The layers package provides the type class machinery needed -- to make monads built out of stacks of monad transformers easy to use. -- It introduces the concept of monad layers, which are a generalisation -- of monad transformers. The type class machinery provided by and the -- design patterns suggested by layers allow for much more -- modularity than is possible with the existing type class machinery and -- design patterns. With layers it is possible to use arbitrary -- monad interfaces (monad interfaces are what we call the sort of type -- classes that you see in the mtl and similar packages) with -- arbtirary monad transformers (by monad transformers here, we are -- specifically to monad constructors, such as the ones defined in -- transformers), without ever having to explicitly define how -- to lift specific interfaces through specific transformers. -- -- layers improves upon and/or replaces, in part or in whole, -- the following list of packages: transformers, mtl, -- mmtl, transformers-base, monad-control, -- lifted-base, monad-peel, -- MonadCatchIO-transformers, MonadCatchIO-mtl, -- exception-transformers, monad-abort-fd and probably -- more too. There have been many attempts to either improve upoin or -- work around the deficiencies of the existing type class machinery for -- monad transformer stacks, but we believe layers is the most -- complete of any of these so far. -- -- A comprehensive overview of the motivation behind layers and -- an explanation of the design decisions taken is given in -- Documentation.Layers.Overview. It is highly recommended -- that you read this if you are considering using this package. The core -- type classes of the package are defined in Control.Monad.Layer -- (this can be thought of as equivalent to or a replacement of -- Control.Monad.Trans.Class from transformers). The rest -- of the modules in this package export monad interfaces, in the -- Control.Monad.Interface hierarchy, including replacements for -- all of the monad interfaces of the mtl package. @package layers @version 0.1 -- | This module is the core of the layers package. It exports: -- --
-- layerOp . withMVar :: (MonadLayerControl m, Inner m ~ IO) -- => MVar a -> (a -> m b) -> m b --layerOp :: MonadLayerControl m => ((a -> Inner m (LayerState m b)) -> Inner m (LayerState m c)) -> (a -> m b) -> m c -- | layerOp_ is a particular application of layerControl -- that allows layering control operations of type: Inner m a -- -> Inner m b to MonadLayerControl m => m -- a -> m b. -- -- For example: -- --
-- layerOp_ mask_ :: (MonadLayerControl m, Inner m ~ IO) => m a -> m a --layerOp_ :: MonadLayerControl m => (Inner m (LayerState m a) -> Inner m (LayerState m b)) -> m a -> m b -- | layerDiscard is a particular application of -- layerControl that allows layering control operations of type: -- Inner m () -> Inner m a to -- MonadLayerControl m => m () -> m a. -- -- Note that, while the argument computation m () has access to -- the captured state, all its side-effects in m are discarded. -- It is run only for its side-effects in the inner monad -- Inner m. -- -- For example: -- --
-- layerDiscard forkIO :: (MonadLayerControl m, Inner m ~ IO) -- => m () -> m ThreadId --layerDiscard :: MonadLayerControl m => (Inner m () -> Inner m a) -> m () -> m a -- | An often used composition: control f = join . -- liftControl f control :: MonadLiftControl i m => ((forall b. m b -> i (m b)) -> i (m a)) -> m a -- | liftOp is a particular application of liftControl that -- allows lifting control operations of type: (a -> i b) -> i -- b to MonadLiftControl i m => (a -> m b) -> m -- b. -- -- For example: -- --
-- liftOp . withMVar :: MonadLiftControl IO m => MVar a -> (a -> m b) -> m b --liftOp :: MonadLiftControl i m => ((a -> i (m b)) -> i (m c)) -> (a -> m b) -> m c -- | liftOp_ is a particular application of liftControl -- that allows lifting control operations of type: i a -> i b -- to MonadLiftControl i m => m a -> m b. -- -- For example: -- --
-- liftOp_ mask_ :: MonadLiftControl IO m => m a -> m a --liftOp_ :: MonadLiftControl i m => (i (m a) -> i (m b)) -> m a -> m b -- | liftDiscard is a particular application of liftControl -- that allows lifting control operations of type: i () -> i -- a to MonadLiftControl i m => m () -> m a. -- -- Note that, while the argument computation m () has access to -- the captured state, all its side-effects in m are discarded. -- It is run only for its side-effects in the inner monad i. -- -- For example: -- --
-- liftDiscard forkIO :: MonadLiftControl IO m => m () -> m ThreadId --liftDiscard :: MonadLiftControl i m => (i () -> i a) -> m () -> m a instance [overlap ok] (MonadLayerControl m, MonadLiftControl i (Inner m)) => MonadLiftControl i m instance [overlap ok] Monad m => MonadLiftControl m m instance [overlap ok] (MonadLayerFunctor m, MonadLiftFunctor i (Inner m)) => MonadLiftFunctor i m instance [overlap ok] Monad m => MonadLiftFunctor m m instance [overlap ok] (MonadLayer m, MonadLift i (Inner m)) => MonadLift i m instance [overlap ok] Monad m => MonadLift m m instance [overlap ok] (Monad m, Monoid w) => MonadTransControl (WriterT w m) instance [overlap ok] (Monad m, Monoid w) => MonadTransFunctor (WriterT w m) instance [overlap ok] (Monad m, Monoid w) => MonadTrans (WriterT w m) instance [overlap ok] (Monad m, Monoid w) => MonadLayerControl (WriterT w m) instance [overlap ok] (Monad m, Monoid w) => MonadLayerFunctor (WriterT w m) instance [overlap ok] (Monad m, Monoid w) => MonadLayer (WriterT w m) instance [overlap ok] (Monad m, Monoid w) => MonadTransControl (WriterT w m) instance [overlap ok] (Monad m, Monoid w) => MonadTransFunctor (WriterT w m) instance [overlap ok] (Monad m, Monoid w) => MonadTrans (WriterT w m) instance [overlap ok] (Monad m, Monoid w) => MonadLayerControl (WriterT w m) instance [overlap ok] (Monad m, Monoid w) => MonadLayerFunctor (WriterT w m) instance [overlap ok] (Monad m, Monoid w) => MonadLayer (WriterT w m) instance [overlap ok] Monad m => MonadTransControl (StateT s m) instance [overlap ok] Monad m => MonadTransFunctor (StateT s m) instance [overlap ok] Monad m => MonadTrans (StateT s m) instance [overlap ok] Monad m => MonadLayerControl (StateT s m) instance [overlap ok] Monad m => MonadLayerFunctor (StateT s m) instance [overlap ok] Monad m => MonadLayer (StateT s m) instance [overlap ok] Monad m => MonadTransControl (StateT s m) instance [overlap ok] Monad m => MonadTransFunctor (StateT s m) instance [overlap ok] Monad m => MonadTrans (StateT s m) instance [overlap ok] Monad m => MonadLayerControl (StateT s m) instance [overlap ok] Monad m => MonadLayerFunctor (StateT s m) instance [overlap ok] Monad m => MonadLayer (StateT s m) instance [overlap ok] (Monad m, Monoid w) => MonadTransControl (RWST r w s m) instance [overlap ok] (Monad m, Monoid w) => MonadTransFunctor (RWST r w s m) instance [overlap ok] (Monad m, Monoid w) => MonadTrans (RWST r w s m) instance [overlap ok] (Monad m, Monoid w) => MonadLayerControl (RWST r w s m) instance [overlap ok] (Monad m, Monoid w) => MonadLayerFunctor (RWST r w s m) instance [overlap ok] (Monad m, Monoid w) => MonadLayer (RWST r w s m) instance [overlap ok] (Monad m, Monoid w) => MonadTransFunctor (RWST r w s m) instance [overlap ok] (Monad m, Monoid w) => MonadTrans (RWST r w s m) instance [overlap ok] (Monad m, Monoid w) => MonadTransControl (RWST r w s m) instance [overlap ok] (Monad m, Monoid w) => MonadLayerControl (RWST r w s m) instance [overlap ok] (Monad m, Monoid w) => MonadLayerFunctor (RWST r w s m) instance [overlap ok] (Monad m, Monoid w) => MonadLayer (RWST r w s m) instance [overlap ok] Monad m => MonadTransControl (ReaderT r m) instance [overlap ok] Monad m => MonadTransFunctor (ReaderT r m) instance [overlap ok] Monad m => MonadTrans (ReaderT r m) instance [overlap ok] Monad m => MonadLayerControl (ReaderT r m) instance [overlap ok] Monad m => MonadLayerFunctor (ReaderT r m) instance [overlap ok] Monad m => MonadLayer (ReaderT r m) instance [overlap ok] Monad m => MonadTransControl (MaybeT m) instance [overlap ok] Monad m => MonadTransFunctor (MaybeT m) instance [overlap ok] Monad m => MonadTrans (MaybeT m) instance [overlap ok] Monad m => MonadLayerControl (MaybeT m) instance [overlap ok] Monad m => MonadLayerFunctor (MaybeT m) instance [overlap ok] Monad m => MonadLayer (MaybeT m) instance [overlap ok] Monad m => MonadTransControl (ListT m) instance [overlap ok] Monad m => MonadTransFunctor (ListT m) instance [overlap ok] Monad m => MonadTrans (ListT m) instance [overlap ok] Monad m => MonadLayerControl (ListT m) instance [overlap ok] Monad m => MonadLayerFunctor (ListT m) instance [overlap ok] Monad m => MonadLayer (ListT m) instance [overlap ok] Monad m => MonadTransControl (IdentityT m) instance [overlap ok] Monad m => MonadTransFunctor (IdentityT m) instance [overlap ok] Monad m => MonadTrans (IdentityT m) instance [overlap ok] Monad m => MonadLayerControl (IdentityT m) instance [overlap ok] Monad m => MonadLayerFunctor (IdentityT m) instance [overlap ok] Monad m => MonadLayer (IdentityT m) instance [overlap ok] (Error e, Monad m) => MonadTransControl (ErrorT e m) instance [overlap ok] (Error e, Monad m) => MonadTransFunctor (ErrorT e m) instance [overlap ok] (Error e, Monad m) => MonadTrans (ErrorT e m) instance [overlap ok] (Error e, Monad m) => MonadLayerControl (ErrorT e m) instance [overlap ok] (Error e, Monad m) => MonadLayerFunctor (ErrorT e m) instance [overlap ok] (Error e, Monad m) => MonadLayer (ErrorT e m) instance [overlap ok] Monad m => MonadTrans (ContT r m) instance [overlap ok] Monad m => MonadLayer (ContT r m) -- | This module exports: -- --
-- f = expr `catch` \(ex :: ArithException) -> handleArith ex -- `catch` \(ex :: IOException) -> handleIO ex ---- -- However, there are a couple of problems with this approach. The first -- is that having two exception handlers is inefficient. However, the -- more serious issue is that the second exception handler will catch -- exceptions in the first, e.g. in the example above, if -- handleArith throws an IOException then the second -- exception handler will catch it. -- -- Instead, we provide a function catches, which would be used -- thus: -- --
-- f = expr `catches` [Handler (\ (ex :: ArithException) -> handleArith ex), -- Handler (\ (ex :: IOException) -> handleIO ex)] --catches :: MonadException m => m a -> [Handler m a] -> m a -- | You need this when using catches. data Handler m a Handler :: (e -> m a) -> Handler m a -- | The function catchJust is like catch, but it takes an -- extra argument which is an exception predicate, a function -- which selects which type of exceptions we're interested in. -- --
-- catchJust (\e -> if isDoesNotExistErrorType (ioeGetErrorType e)
-- then Just ()
-- else Nothing)
-- (readFile f)
-- (\_ -> do
-- hPutStrLn stderr ("No such file: " ++ show f)
-- return "")
--
--
-- Any other exceptions which are not matched by the predicate are
-- re-raised, and may be caught by an enclosing catch,
-- catchJust, etc.
catchJust :: (MonadException m, Exception e) => (e -> Maybe b) -> m a -> (b -> m a) -> m a
-- | A version of catch with the arguments swapped around; useful in
-- situations where the code for the handler is shorter. For example:
--
-- -- do handle (\NonTermination -> exitWith (ExitFailure 1)) $ -- ... --handle :: (MonadException m, Exception e) => (e -> m a) -> m a -> m a -- | A version of catchJust with the arguments swapped around (see -- handle). handleJust :: (MonadException m, Exception e) => (e -> Maybe b) -> (b -> m a) -> m a -> m a -- | Similar to catch, but returns an Either result which is -- (Right a) if no exception of type e was -- raised, or (Left ex) if an exception of type -- e was raised and its value is ex. If any other type -- of exception is raised than it will be propogated up to the next -- enclosing exception handler. -- --
-- try a = catch (Right `liftM` a) (return . Left) --try :: (MonadException m, Exception e) => m a -> m (Either e a) -- | A variant of try that takes an exception predicate to select -- which exceptions are caught (c.f. catchJust). If the exception -- does not match the predicate, it is re-thrown. tryJust :: (MonadException m, Exception e) => (e -> Maybe b) -> m a -> m (Either b a) -- | The SomeException type is the root of the exception type -- hierarchy. When an exception of type e is thrown, behind the -- scenes it is encapsulated in a SomeException. data SomeException :: * instance [overlap ok] Error SomeException instance [overlap ok] (MonadLayerControl m, MonadException (Inner m)) => MonadException m instance [overlap ok] (MonadException f, MonadException g) => MonadException (Product f g) instance [overlap ok] (e ~ SomeException, Monad m) => MonadException (ErrorT e m) instance [overlap ok] MonadException STM instance [overlap ok] MonadException IO instance [overlap ok] e ~ SomeException => MonadException (Either e) -- | This module exports: -- --
-- mask $ \restore -> do -- x <- acquire -- restore (do_something_with x) `finally` release ---- -- This code guarantees that acquire is paired with -- release, by masking asynchronous exceptions for the critical -- parts. (Rather than write this code yourself, it would be better to -- use bracket which abstracts the general pattern). -- -- Note that the restore action passed to the argument to -- mask does not necessarily unmask asynchronous exceptions, it -- just restores the masking state to that of the enclosing context. Thus -- if asynchronous exceptions are already masked, mask cannot be -- used to unmask exceptions again. This is so that if you call a library -- function with exceptions masked, you can be sure that the library call -- will not be able to unmask exceptions again. If you are writing -- library code and need to use asynchronous exceptions, the only way is -- to create a new thread; see forkWithUnmask. -- -- Asynchronous exceptions may still be received while in the masked -- state if the masked thread blocks in certain ways; see -- Control.Exception#interruptible. -- -- Threads created by fork inherit the masked state from the -- parent; that is, to start a thread in blocked mode, use mask_ $ -- fork .... This is particularly useful if you need to establish an -- exception handler in the forked thread before any asynchronous -- exceptions are received. mask :: MonadMask m => ((forall a n. MonadMask n => n a -> n a) -> m b) -> m b -- | Like mask, but does not pass a restore action to the -- argument. mask_ :: MonadMask m => m a -> m a -- | Like mask, but the masked computation is not interruptible (see -- Control.Exception#interruptible). THIS SHOULD BE USED WITH -- GREAT CARE, because if a thread executing in -- uninterruptibleMask blocks for any reason, then the thread (and -- possibly the program, if this is the main thread) will be unresponsive -- and unkillable. This function should only be necessary if you need to -- mask exceptions around an interruptible operation, and you can -- guarantee that the interruptible operation will only block for a short -- period of time. uninterruptibleMask :: MonadMask m => ((forall a n. MonadMask n => n a -> n a) -> m b) -> m b -- | Like uninterruptibleMask, but does not pass a restore -- action to the argument. uninterruptibleMask_ :: MonadMask m => m a -> m a instance [overlap ok] (MonadLayer m, MonadMask (Inner m)) => MonadMask m instance [overlap ok] MonadMask IO instance [overlap ok] MonadMask STM instance [overlap ok] MonadMask (ST s) instance [overlap ok] MonadMask (ST s) instance [overlap ok] MonadMask ((->) r) instance [overlap ok] MonadMask [] instance [overlap ok] MonadMask (Either e) instance [overlap ok] MonadMask Maybe instance [overlap ok] (MonadMask f, MonadMask g) => MonadMask (Product f g) instance [overlap ok] MonadMask Identity -- | This module exports: -- --
-- ... mask_ $ forkIOWithUnmask $ \unmask -> -- catch (unmask ...) handler ---- -- so that the exception handler in the child thread is established with -- asynchronous exceptions masked, meanwhile the main body of the child -- thread is executed in the unmasked state. -- -- Note that the unmask function passed to the child thread -- should only be used in that thread; the behaviour is undefined if it -- is invoked in a different thread. forkWithUnmask :: MonadFork m => ((forall a n. MonadMask n => n a -> n a) -> m ()) -> m ThreadId -- | Like forkWithUnmask, but the child thread is pinned to the -- given CPU, as with forkOn. forkOnWithUnmask :: MonadFork m => Int -> ((forall a n. MonadMask n => n a -> n a) -> m ()) -> m ThreadId instance [overlap ok] (MonadLayerControl m, MonadFork (Inner m)) => MonadFork m instance [overlap ok] (MonadFork f, MonadFork g) => MonadFork (Product f g) instance [overlap ok] MonadFork IO -- | This module exports: -- --
-- ref <- newRef 0 -- replicateM_ 1000000 $ modifyRef ref (+1) -- readRef ref >>= print ---- -- To avoid this problem, use modifyRef' instead. modifyRef :: MonadMutVar ref m => ref a -> (a -> a) -> m () -- | Strict version of modifyRef. modifyRef' :: MonadMutVar ref m => ref a -> (a -> a) -> m () instance [overlap ok] (MonadLayer m, MonadMutVar ref (Inner m)) => MonadMutVar ref m instance [overlap ok] (MonadMutVar ref f, MonadMutVar ref g) => MonadMutVar ref (Product f g) instance [overlap ok] MonadMutVar TVar STM instance [overlap ok] MonadMutVar (STRef s) (ST s) instance [overlap ok] MonadMutVar (STRef s) (ST s) instance [overlap ok] MonadMutVar IORef IO -- | This module exports: -- --
-- >>> :t modify ((+1) :: Int -> Int) -- modify (...) :: (MonadState Int a) => a () ---- -- This says that modify (+1) acts over any Monad that is -- a member of the MonadState class with an Int state. modify :: MonadState s m => (s -> s) -> m () -- | Gets specific component of the state, using a projection function -- supplied. gets :: MonadState s m => (s -> a) -> m a instance [overlap ok] (MonadLayer m, MonadState s (Inner m)) => MonadState s m instance [overlap ok] (MonadState s f, MonadState s g) => MonadState s (Product f g) instance [overlap ok] (Monad m, Monoid w) => MonadState s (RWST r w s m) instance [overlap ok] (Monad m, Monoid w) => MonadState s (RWST r w s m) instance [overlap ok] Monad m => MonadState s (StateT s m) instance [overlap ok] Monad m => MonadState s (StateT s m) -- | This module exports: -- --
-- listens f m = liftM (\(~(a, w)) -> (a, f w)) (listen m) --listens :: MonadWriter w m => (w -> b) -> m a -> m (a, b) -- | censor f m is an action that executes the action -- m and applies the function f to its output, leaving -- the return value unchanged. -- --
-- censor f m = pass (liftM (\a -> (a,f)) m) --censor :: MonadWriter w m => (w -> w) -> m a -> m a instance [overlap ok] (MonadLayer m, MonadWriter w (Inner m)) => MonadWriter w m instance [overlap ok] (MonadWriter w f, MonadWriter w g) => MonadWriter w (Product f g) instance [overlap ok] (Monad m, Monoid w) => MonadWriter w (RWST r w s m) instance [overlap ok] (Monad m, Monoid w) => MonadWriter w (RWST r w s m) instance [overlap ok] (Monad m, Monoid w) => MonadWriter w (WriterT w m) instance [overlap ok] (Monad m, Monoid w) => MonadWriter w (WriterT w m) -- | This module exports: -- --
-- bracket
-- (openFile "filename" ReadMode)
-- (hClose)
-- (\fileHandle -> do { ... })
--
--
-- The arguments to bracket are in this order so that we can
-- partially apply it, e.g.:
--
-- -- withFile name mode = bracket (openFile name mode) hClose --bracket :: MonadTry m => m a -> (a -> m b) -> (a -> m c) -> m c -- | A variant of bracket where the return value from the first -- computation is not required. bracket_ :: MonadTry m => m a -> m b -> m c -> m c -- | Like bracket, but only performs the final action if the monad -- short-circuited during the in-between computation. bracketOnError :: MonadTry m => m a -> (a -> m b) -> (a -> m c) -> m c -- | A specialised variant of bracket with just a computation to run -- afterward. finally :: MonadTry m => m a -> m b -> m a -- | Like finally, but only performs the final action if the monad -- short-circuited during the computation. onException :: MonadTry m => m a -> m b -> m a instance [overlap ok] (MonadLayerControl m, MonadTry (Inner m)) => MonadTry m instance [overlap ok] MonadTry STM instance [overlap ok] MonadTry (ST s) instance [overlap ok] MonadTry (ST s) instance [overlap ok] MonadTry IO instance [overlap ok] MonadTry ((->) r) instance [overlap ok] MonadTry [] instance [overlap ok] MonadTry (Either e) instance [overlap ok] MonadTry Maybe instance [overlap ok] (MonadTry f, MonadTry g) => MonadTry (Product f g) instance [overlap ok] MonadTry Identity -- | This document discusses extensively the motivation behind the -- layers package and explains the design decisions taken in its -- construction. -- | Warning: This module exports no types, functions, classes or -- instances. It exists solely for the Haddock documentation it produces. -- You should not ever need to import it. module Documentation.Layers.Overview