| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Foundation.Monad
Synopsis
- class Monad m => MonadIO (m :: Type -> Type) where
- class Monad m => MonadFailure (m :: Type -> Type) where
- class Monad m => MonadThrow (m :: Type -> Type) where
- class MonadThrow m => MonadCatch (m :: Type -> Type) where
- class MonadCatch m => MonadBracket (m :: Type -> Type) where
- generalBracket :: m a -> (a -> b -> m ignored1) -> (a -> SomeException -> m ignored2) -> (a -> m b) -> m b
- class MonadTrans (trans :: (Type -> Type) -> Type -> Type) where
- newtype Identity a = Identity {
- runIdentity :: a
- replicateM :: Applicative m => CountOf a -> m a -> m [a]
Documentation
class Monad m => MonadIO (m :: Type -> Type) where #
Monads in which IO computations may be embedded.
Any monad built by applying a sequence of monad transformers to the
IO monad will be an instance of this class.
Instances should satisfy the following laws, which state that liftIO
is a transformer of monads:
Methods
Lift a computation from the IO monad.
This allows us to run IO computations in any monadic stack, so long as it supports these kinds of operations
(i.e. IO is the base monad for the stack).
Example
import Control.Monad.Trans.State -- from the "transformers" library printState :: Show s => StateT s IO () printState = do state <- get liftIO $ print state
Had we omitted , we would have ended up with this error:liftIO
• Couldn't match type ‘IO’ with ‘StateT s IO’ Expected type: StateT s IO () Actual type: IO ()
The important part here is the mismatch between StateT s IO () and .IO ()
Luckily, we know of a function that takes an and returns an IO a(m a): ,
enabling us to run the program and see the expected results:liftIO
> evalStateT printState "hello" "hello" > evalStateT printState 3 3
Instances
| MonadIO IO | Since: base-4.9.0.0 |
Defined in Control.Monad.IO.Class | |
| MonadIO m => MonadIO (ResourceT m) # | |
Defined in Foundation.Conduit.Internal | |
| MonadIO m => MonadIO (ExceptT e m) # | |
Defined in Foundation.Monad.Except | |
| MonadIO m => MonadIO (ReaderT r m) # | |
Defined in Foundation.Monad.Reader | |
| (Functor m, MonadIO m) => MonadIO (StateT s m) # | |
Defined in Foundation.Monad.State | |
| MonadIO m => MonadIO (Conduit i o m) # | |
Defined in Foundation.Conduit.Internal | |
class Monad m => MonadFailure (m :: Type -> Type) where #
Monad that can represent failure
Similar to MonadFail but with a parametrized Failure linked to the Monad
Associated Types
type Failure (m :: Type -> Type) #
The associated type with the MonadFailure, representing what failure can be encoded in this monad
Instances
| MonadFailure Maybe | |||||
| MonadFailure (Either a) | |||||
| Monad m => MonadFailure (ExceptT e m) # | |||||
| MonadFailure m => MonadFailure (ReaderT r m) # | |||||
| (Functor m, MonadFailure m) => MonadFailure (StateT s m) # | |||||
| MonadFailure m => MonadFailure (Conduit i o m) # | |||||
| Monad state => MonadFailure (Builder collection mutCollection step state err) | |||||
Defined in Basement.MutableBuilder Associated Types
| |||||
class Monad m => MonadThrow (m :: Type -> Type) where #
Monad that can throw exception
Methods
throw :: Exception e => e -> m a #
Throw immediatity an exception.
Only a MonadCatch monad will be able to catch the exception using catch
Instances
| MonadThrow IO # | |
Defined in Foundation.Monad.Exception | |
| MonadThrow m => MonadThrow (ResourceT m) # | |
Defined in Foundation.Conduit.Internal | |
| MonadThrow m => MonadThrow (ReaderT r m) # | |
Defined in Foundation.Monad.Reader | |
| (Functor m, MonadThrow m) => MonadThrow (StateT s m) # | |
Defined in Foundation.Monad.State | |
| MonadThrow m => MonadThrow (Conduit i o m) # | |
Defined in Foundation.Conduit.Internal | |
class MonadThrow m => MonadCatch (m :: Type -> Type) where #
Monad that can catch exception
Instances
| MonadCatch IO # | |
| MonadCatch m => MonadCatch (ResourceT m) # | |
| MonadCatch m => MonadCatch (ReaderT r m) # | |
| (Functor m, MonadCatch m) => MonadCatch (StateT s m) # | |
| MonadCatch m => MonadCatch (Conduit i o m) # | |
class MonadCatch m => MonadBracket (m :: Type -> Type) where #
Monad that can ensure cleanup actions are performed even in the case of exceptions, both synchronous and asynchronous. This usually excludes continuation-based monads.
Methods
Arguments
| :: m a | acquire some resource |
| -> (a -> b -> m ignored1) | cleanup, no exception thrown |
| -> (a -> SomeException -> m ignored2) | cleanup, some exception thrown. The exception will be rethrown |
| -> (a -> m b) | inner action to perform with the resource |
| -> m b |
A generalized version of the standard bracket function which allows distinguishing different exit cases.
Instances
| MonadBracket IO # | |
Defined in Foundation.Monad.Exception Methods generalBracket :: IO a -> (a -> b -> IO ignored1) -> (a -> SomeException -> IO ignored2) -> (a -> IO b) -> IO b # | |
| MonadBracket m => MonadBracket (ResourceT m) # | |
Defined in Foundation.Conduit.Internal Methods generalBracket :: ResourceT m a -> (a -> b -> ResourceT m ignored1) -> (a -> SomeException -> ResourceT m ignored2) -> (a -> ResourceT m b) -> ResourceT m b # | |
| MonadBracket m => MonadBracket (ReaderT r m) # | |
Defined in Foundation.Monad.Reader Methods generalBracket :: ReaderT r m a -> (a -> b -> ReaderT r m ignored1) -> (a -> SomeException -> ReaderT r m ignored2) -> (a -> ReaderT r m b) -> ReaderT r m b # | |
class MonadTrans (trans :: (Type -> Type) -> Type -> Type) where #
Basic Transformer class
Methods
lift :: Monad m => m a -> trans m a #
Lift a computation from an inner monad to the current transformer monad
Instances
| MonadTrans ResourceT # | |
Defined in Foundation.Conduit.Internal | |
| MonadTrans (ExceptT e) # | |
Defined in Foundation.Monad.Except | |
| MonadTrans (ReaderT r) # | |
Defined in Foundation.Monad.Reader | |
| MonadTrans (StateT s) # | |
Defined in Foundation.Monad.State | |
| MonadTrans (Conduit i o) # | |
Defined in Foundation.Conduit.Internal | |
Identity functor and monad. (a non-strict monad)
Examples
>>>fmap (+1) (Identity 0)Identity 1
>>>Identity [1, 2, 3] <> Identity [4, 5, 6]Identity [1,2,3,4,5,6]
>>> do
x <- Identity 10
y <- Identity (x + 5)
pure (x + y)
Identity 25
@since base-4.8.0.0
Constructors
| Identity | |
Fields
| |
Instances
replicateM :: Applicative m => CountOf a -> m a -> m [a] #
performs the action replicateM n actn times,
gathering the results.