| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
Relude.Monad.Reexport
Description
This module reexports functions to work with monads.
Synopsis
- newtype ExceptT e (m :: * -> *) a = ExceptT (m (Either e a))
- runExceptT :: ExceptT e m a -> m (Either e a)
- asks :: MonadReader r m => (r -> a) -> m a
- class Monad m => MonadReader r (m :: * -> *) | m -> r where
- newtype ReaderT r (m :: k -> *) (a :: k) :: forall k. * -> (k -> *) -> k -> * = ReaderT {
- runReaderT :: r -> m a
- type Reader r = ReaderT r Identity
- runReader :: Reader r a -> r -> a
- gets :: MonadState s m => (s -> a) -> m a
- modify' :: MonadState s m => (s -> s) -> m ()
- modify :: MonadState s m => (s -> s) -> m ()
- class Monad m => MonadState s (m :: * -> *) | m -> s where
- newtype StateT s (m :: * -> *) a = StateT {
- runStateT :: s -> m (a, s)
- type State s = StateT s Identity
- runState :: State s a -> s -> (a, s)
- evalState :: State s a -> s -> a
- execState :: State s a -> s -> s
- withState :: (s -> s) -> State s a -> State s a
- evalStateT :: Monad m => StateT s m a -> s -> m a
- execStateT :: Monad m => StateT s m a -> s -> m s
- class Monad m => MonadIO (m :: * -> *) where
- class MonadTrans (t :: (* -> *) -> * -> *) where
- data IdentityT (f :: k -> *) (a :: k) :: forall k. (k -> *) -> k -> *
- exceptToMaybeT :: Functor m => ExceptT e m a -> MaybeT m a
- maybeToExceptT :: Functor m => e -> MaybeT m a -> ExceptT e m a
- newtype MaybeT (m :: * -> *) a = MaybeT {}
- join :: Monad m => m (m a) -> m a
- class Applicative m => Monad (m :: * -> *) where
- mfilter :: MonadPlus m => (a -> Bool) -> m a -> m a
- (<$!>) :: Monad m => (a -> b) -> m a -> m b
- replicateM_ :: Applicative m => Int -> m a -> m ()
- replicateM :: Applicative m => Int -> m a -> m [a]
- zipWithM_ :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m ()
- zipWithM :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m [c]
- mapAndUnzipM :: Applicative m => (a -> m (b, c)) -> [a] -> m ([b], [c])
- forever :: Applicative f => f a -> f b
- (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c
- (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
- filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a]
- (=<<) :: Monad m => (a -> m b) -> m a -> m b
- class (Alternative m, Monad m) => MonadPlus (m :: * -> *) where
- class Monad m => MonadFail (m :: * -> *) where
- data Maybe a
- mapMaybe :: (a -> Maybe b) -> [a] -> [b]
- catMaybes :: [Maybe a] -> [a]
- listToMaybe :: [a] -> Maybe a
- maybeToList :: Maybe a -> [a]
- fromMaybe :: a -> Maybe a -> a
- isNothing :: Maybe a -> Bool
- isJust :: Maybe a -> Bool
- maybe :: b -> (a -> b) -> Maybe a -> b
- data Either a b
- isRight :: Either a b -> Bool
- isLeft :: Either a b -> Bool
- partitionEithers :: [Either a b] -> ([a], [b])
- rights :: [Either a b] -> [b]
- lefts :: [Either a b] -> [a]
- either :: (a -> c) -> (b -> c) -> Either a b -> c
Reexport transformers
newtype ExceptT e (m :: * -> *) a #
A monad transformer that adds exceptions to other monads.
ExceptT constructs a monad parameterized over two things:
- e - The exception type.
- m - The inner monad.
The return function yields a computation that produces the given
value, while >>= sequences two subcomputations, exiting on the
first exception.
Instances
runExceptT :: ExceptT e m a -> m (Either e a) #
The inverse of ExceptT.
Arguments
| :: MonadReader r m | |
| => (r -> a) | The selector function to apply to the environment. |
| -> m a |
Retrieves a function of the current environment.
class Monad m => MonadReader r (m :: * -> *) | m -> r where #
See examples in Control.Monad.Reader.
Note, the partially applied function type (->) r is a simple reader monad.
See the instance declaration below.
Methods
Retrieves the monad environment.
Arguments
| :: (r -> r) | The function to modify the environment. |
| -> m a |
|
| -> m a |
Executes a computation in a modified environment.
Arguments
| :: (r -> a) | The selector function to apply to the environment. |
| -> m a |
Retrieves a function of the current environment.
Instances
| MonadReader r m => MonadReader r (MaybeT m) | |
| MonadReader r m => MonadReader r (ListT m) | |
| (Monoid w, MonadReader r m) => MonadReader r (WriterT w m) | |
| (Monoid w, MonadReader r m) => MonadReader r (WriterT w m) | |
| MonadReader r m => MonadReader r (StateT s m) | |
| MonadReader r m => MonadReader r (StateT s m) | |
| MonadReader r m => MonadReader r (IdentityT m) | |
| MonadReader r m => MonadReader r (ExceptT e m) | Since: mtl-2.2 |
| (Error e, MonadReader r m) => MonadReader r (ErrorT e m) | |
| Monad m => MonadReader r (ReaderT r m) | |
| MonadReader r ((->) r :: * -> *) | |
| MonadReader r' m => MonadReader r' (ContT r m) | |
| (Monad m, Monoid w) => MonadReader r (RWST r w s m) | |
| (Monad m, Monoid w) => MonadReader r (RWST r w s m) | |
newtype ReaderT r (m :: k -> *) (a :: k) :: forall k. * -> (k -> *) -> k -> * #
The reader monad transformer, which adds a read-only environment to the given monad.
The return function ignores the environment, while >>= passes
the inherited environment to both subcomputations.
Constructors
| ReaderT | |
Fields
| |
Instances
| MonadState s m => MonadState s (ReaderT r m) | |
| Monad m => MonadReader r (ReaderT r m) | |
| MonadTrans (ReaderT r :: (* -> *) -> * -> *) | |
Defined in Control.Monad.Trans.Reader | |
| Monad m => Monad (ReaderT r m) | |
| Functor m => Functor (ReaderT r m) | |
| MonadFix m => MonadFix (ReaderT r m) | |
Defined in Control.Monad.Trans.Reader | |
| MonadFail m => MonadFail (ReaderT r m) | |
Defined in Control.Monad.Trans.Reader | |
| Applicative m => Applicative (ReaderT r m) | |
Defined in Control.Monad.Trans.Reader | |
| MonadZip m => MonadZip (ReaderT r m) | |
| MonadIO m => MonadIO (ReaderT r m) | |
Defined in Control.Monad.Trans.Reader | |
| Alternative m => Alternative (ReaderT r m) | |
| MonadPlus m => MonadPlus (ReaderT r m) | |
type Reader r = ReaderT r Identity #
The parameterizable reader monad.
Computations are functions of a shared environment.
The return function ignores the environment, while >>= passes
the inherited environment to both subcomputations.
Arguments
| :: Reader r a | A |
| -> r | An initial environment. |
| -> a |
Runs a Reader and extracts the final value from it.
(The inverse of reader.)
gets :: MonadState s m => (s -> a) -> m a #
Gets specific component of the state, using a projection function supplied.
modify' :: MonadState s m => (s -> s) -> m () #
A variant of modify in which the computation is strict in the
new state.
Since: mtl-2.2
modify :: MonadState s m => (s -> s) -> m () #
Monadic state transformer.
Maps an old state to a new state inside a state monad. The old state is thrown away.
Main> :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.
class Monad m => MonadState s (m :: * -> *) | m -> s where #
Minimal definition is either both of get and put or just state
Methods
Return the state from the internals of the monad.
Replace the state inside the monad.
state :: (s -> (a, s)) -> m a #
Embed a simple state action into the monad.
Instances
| MonadState s m => MonadState s (MaybeT m) | |
| MonadState s m => MonadState s (ListT m) | |
| (Monoid w, MonadState s m) => MonadState s (WriterT w m) | |
| (Monoid w, MonadState s m) => MonadState s (WriterT w m) | |
| Monad m => MonadState s (StateT s m) | |
| Monad m => MonadState s (StateT s m) | |
| MonadState s m => MonadState s (IdentityT m) | |
| MonadState s m => MonadState s (ExceptT e m) | Since: mtl-2.2 |
| (Error e, MonadState s m) => MonadState s (ErrorT e m) | |
| MonadState s m => MonadState s (ReaderT r m) | |
| MonadState s m => MonadState s (ContT r m) | |
| (Monad m, Monoid w) => MonadState s (RWST r w s m) | |
| (Monad m, Monoid w) => MonadState s (RWST r w s m) | |
newtype StateT s (m :: * -> *) a #
A state transformer monad parameterized by:
s- The state.m- The inner monad.
The return function leaves the state unchanged, while >>= uses
the final state of the first computation as the initial state of
the second.
Instances
| Monad m => MonadState s (StateT s m) | |
| MonadReader r m => MonadReader r (StateT s m) | |
| MonadTrans (StateT s) | |
Defined in Control.Monad.Trans.State.Strict | |
| Monad m => Monad (StateT s m) | |
| Functor m => Functor (StateT s m) | |
| MonadFix m => MonadFix (StateT s m) | |
Defined in Control.Monad.Trans.State.Strict | |
| MonadFail m => MonadFail (StateT s m) | |
Defined in Control.Monad.Trans.State.Strict | |
| (Functor m, Monad m) => Applicative (StateT s m) | |
Defined in Control.Monad.Trans.State.Strict | |
| MonadIO m => MonadIO (StateT s m) | |
Defined in Control.Monad.Trans.State.Strict | |
| (Functor m, MonadPlus m) => Alternative (StateT s m) | |
| MonadPlus m => MonadPlus (StateT s m) | |
type State s = StateT s Identity #
A state monad parameterized by the type s of the state to carry.
The return function leaves the state unchanged, while >>= uses
the final state of the first computation as the initial state of
the second.
Arguments
| :: State s a | state-passing computation to execute |
| -> s | initial state |
| -> (a, s) | return value and final state |
Unwrap a state monad computation as a function.
(The inverse of state.)
Arguments
| :: State s a | state-passing computation to execute |
| -> s | initial value |
| -> a | return value of the state computation |
Arguments
| :: State s a | state-passing computation to execute |
| -> s | initial value |
| -> s | final state |
evalStateT :: Monad m => StateT s m a -> s -> m a #
Evaluate a state computation with the given initial state and return the final value, discarding the final state.
evalStateTm s =liftMfst(runStateTm s)
execStateT :: Monad m => StateT s m a -> s -> m s #
Evaluate a state computation with the given initial state and return the final state, discarding the final value.
execStateTm s =liftMsnd(runStateTm s)
class Monad m => MonadIO (m :: * -> *) 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:
Minimal complete definition
Instances
| MonadIO IO | Since: base-4.9.0.0 |
Defined in Control.Monad.IO.Class | |
| MonadIO m => MonadIO (MaybeT m) | |
Defined in Control.Monad.Trans.Maybe | |
| MonadIO m => MonadIO (IdentityT m) | |
Defined in Control.Monad.Trans.Identity | |
| (Error e, MonadIO m) => MonadIO (ErrorT e m) | |
Defined in Control.Monad.Trans.Error | |
| MonadIO m => MonadIO (ExceptT e m) | |
Defined in Control.Monad.Trans.Except | |
| MonadIO m => MonadIO (StateT s m) | |
Defined in Control.Monad.Trans.State.Strict | |
| MonadIO m => MonadIO (ReaderT r m) | |
Defined in Control.Monad.Trans.Reader | |
class MonadTrans (t :: (* -> *) -> * -> *) where #
The class of monad transformers. Instances should satisfy the
following laws, which state that lift is a monad transformation:
Minimal complete definition
Methods
lift :: Monad m => m a -> t m a #
Lift a computation from the argument monad to the constructed monad.
Instances
| MonadTrans MaybeT | |
Defined in Control.Monad.Trans.Maybe | |
| MonadTrans (IdentityT :: (* -> *) -> * -> *) | |
Defined in Control.Monad.Trans.Identity | |
| MonadTrans (ErrorT e) | |
Defined in Control.Monad.Trans.Error | |
| MonadTrans (ExceptT e) | |
Defined in Control.Monad.Trans.Except | |
| MonadTrans (StateT s) | |
Defined in Control.Monad.Trans.State.Strict | |
| MonadTrans (ReaderT r :: (* -> *) -> * -> *) | |
Defined in Control.Monad.Trans.Reader | |
data IdentityT (f :: k -> *) (a :: k) :: forall k. (k -> *) -> k -> * #
The trivial monad transformer, which maps a monad to an equivalent monad.
Instances
exceptToMaybeT :: Functor m => ExceptT e m a -> MaybeT m a #
maybeToExceptT :: Functor m => e -> MaybeT m a -> ExceptT e m a #
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.
Instances
Reexport monadic functions
join :: Monad m => m (m a) -> m a #
The join function is the conventional monad join operator. It
is used to remove one level of monadic structure, projecting its
bound argument into the outer level.
class Applicative m => Monad (m :: * -> *) where #
The Monad class defines the basic operations over a monad,
a concept from a branch of mathematics known as category theory.
From the perspective of a Haskell programmer, however, it is best to
think of a monad as an abstract datatype of actions.
Haskell's do expressions provide a convenient syntax for writing
monadic expressions.
Instances of Monad should satisfy the following laws:
Furthermore, the Monad and Applicative operations should relate as follows:
The above laws imply:
and that pure and (<*>) satisfy the applicative functor laws.
The instances of Monad for lists, Maybe and IO
defined in the Prelude satisfy these laws.
Minimal complete definition
Methods
(>>=) :: m a -> (a -> m b) -> m b infixl 1 #
Sequentially compose two actions, passing any value produced by the first as an argument to the second.
(>>) :: m a -> m b -> m b infixl 1 #
Sequentially compose two actions, discarding any value produced by the first, like sequencing operators (such as the semicolon) in imperative languages.
Inject a value into the monadic type.
Instances
| Monad [] | Since: base-2.1 |
| Monad Maybe | Since: base-2.1 |
| Monad IO | Since: base-2.1 |
| Monad Par1 | Since: base-4.9.0.0 |
| Monad Complex | Since: base-4.9.0.0 |
| Monad Min | Since: base-4.9.0.0 |
| Monad Max | Since: base-4.9.0.0 |
| Monad First | Since: base-4.9.0.0 |
| Monad Last | Since: base-4.9.0.0 |
| Monad Option | Since: base-4.9.0.0 |
| Monad Identity | Since: base-4.8.0.0 |
| Monad STM | Since: base-4.3.0.0 |
| Monad First | |
| Monad Last | |
| Monad Dual | Since: base-4.8.0.0 |
| Monad Sum | Since: base-4.8.0.0 |
| Monad Product | Since: base-4.8.0.0 |
| Monad Down | Since: base-4.11.0.0 |
| Monad ReadPrec | Since: base-2.1 |
| Monad ReadP | Since: base-2.1 |
| Monad NonEmpty | Since: base-4.9.0.0 |
| Monad Put | |
| Monad Tree | |
| Monad Seq | |
| Monad P | Since: base-2.1 |
| Monad (Either e) | Since: base-4.4.0.0 |
| Monad (U1 :: * -> *) | Since: base-4.9.0.0 |
| Monoid a => Monad ((,) a) | Since: base-4.9.0.0 |
| Monad m => Monad (WrappedMonad m) | |
Defined in Control.Applicative Methods (>>=) :: WrappedMonad m a -> (a -> WrappedMonad m b) -> WrappedMonad m b # (>>) :: WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m b # return :: a -> WrappedMonad m a # fail :: String -> WrappedMonad m a # | |
| ArrowApply a => Monad (ArrowMonad a) | Since: base-2.1 |
Defined in Control.Arrow Methods (>>=) :: ArrowMonad a a0 -> (a0 -> ArrowMonad a b) -> ArrowMonad a b # (>>) :: ArrowMonad a a0 -> ArrowMonad a b -> ArrowMonad a b # return :: a0 -> ArrowMonad a a0 # fail :: String -> ArrowMonad a a0 # | |
| Monad (Proxy :: * -> *) | Since: base-4.7.0.0 |
| Monad m => Monad (MaybeT m) | |
| Monad f => Monad (Rec1 f) | Since: base-4.9.0.0 |
| Monad f => Monad (Alt f) | |
| (Applicative f, Monad f) => Monad (WhenMissing f x) | Equivalent to Since: containers-0.5.9 |
Defined in Data.IntMap.Internal Methods (>>=) :: WhenMissing f x a -> (a -> WhenMissing f x b) -> WhenMissing f x b # (>>) :: WhenMissing f x a -> WhenMissing f x b -> WhenMissing f x b # return :: a -> WhenMissing f x a # fail :: String -> WhenMissing f x a # | |
| Monad m => Monad (IdentityT m) | |
| (Monad m, Error e) => Monad (ErrorT e m) | |
| Monad m => Monad (ExceptT e m) | |
| Monad m => Monad (StateT s m) | |
| Monad ((->) r :: * -> *) | Since: base-2.1 |
| (Monad f, Monad g) => Monad (f :*: g) | Since: base-4.9.0.0 |
| (Monad f, Monad g) => Monad (Product f g) | Since: base-4.9.0.0 |
| (Monad f, Applicative f) => Monad (WhenMatched f x y) | Equivalent to Since: containers-0.5.9 |
Defined in Data.IntMap.Internal Methods (>>=) :: WhenMatched f x y a -> (a -> WhenMatched f x y b) -> WhenMatched f x y b # (>>) :: WhenMatched f x y a -> WhenMatched f x y b -> WhenMatched f x y b # return :: a -> WhenMatched f x y a # fail :: String -> WhenMatched f x y a # | |
| (Applicative f, Monad f) => Monad (WhenMissing f k x) | Equivalent to Since: containers-0.5.9 |
Defined in Data.Map.Internal Methods (>>=) :: WhenMissing f k x a -> (a -> WhenMissing f k x b) -> WhenMissing f k x b # (>>) :: WhenMissing f k x a -> WhenMissing f k x b -> WhenMissing f k x b # return :: a -> WhenMissing f k x a # fail :: String -> WhenMissing f k x a # | |
| Monad m => Monad (ReaderT r m) | |
| Monad f => Monad (M1 i c f) | Since: base-4.9.0.0 |
| (Monad f, Applicative f) => Monad (WhenMatched f k x y) | Equivalent to Since: containers-0.5.9 |
Defined in Data.Map.Internal Methods (>>=) :: WhenMatched f k x y a -> (a -> WhenMatched f k x y b) -> WhenMatched f k x y b # (>>) :: WhenMatched f k x y a -> WhenMatched f k x y b -> WhenMatched f k x y b # return :: a -> WhenMatched f k x y a # fail :: String -> WhenMatched f k x y a # | |
replicateM_ :: Applicative m => Int -> m a -> m () #
Like replicateM, but discards the result.
replicateM :: Applicative m => Int -> m a -> m [a] #
performs the action replicateM n actn times,
gathering the results.
zipWithM_ :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m () #
zipWithM :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m [c] #
mapAndUnzipM :: Applicative m => (a -> m (b, c)) -> [a] -> m ([b], [c]) #
The mapAndUnzipM function maps its first argument over a list, returning
the result as a pair of lists. This function is mainly used with complicated
data structures or a state-transforming monad.
forever :: Applicative f => f a -> f b #
repeats the action infinitely.forever act
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c infixr 1 #
Left-to-right Kleisli composition of monads.
filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a] #
This generalizes the list-based filter function.
(=<<) :: Monad m => (a -> m b) -> m a -> m b infixr 1 #
Same as >>=, but with the arguments interchanged.
class (Alternative m, Monad m) => MonadPlus (m :: * -> *) where #
Monads that also support choice and failure.
Methods
The identity of mplus. It should also satisfy the equations
mzero >>= f = mzero v >> mzero = mzero
The default definition is
mzero = empty
An associative operation. The default definition is
mplus = (<|>)
Instances
| MonadPlus [] | Since: base-2.1 |
| MonadPlus Maybe | Since: base-2.1 |
| MonadPlus IO | Since: base-4.9.0.0 |
| MonadPlus Option | Since: base-4.9.0.0 |
| MonadPlus STM | Since: base-4.3.0.0 |
| MonadPlus ReadPrec | Since: base-2.1 |
| MonadPlus ReadP | Since: base-2.1 |
| MonadPlus Seq | |
| MonadPlus P | Since: base-2.1 |
Defined in Text.ParserCombinators.ReadP | |
| MonadPlus (U1 :: * -> *) | Since: base-4.9.0.0 |
| (ArrowApply a, ArrowPlus a) => MonadPlus (ArrowMonad a) | Since: base-4.6.0.0 |
Defined in Control.Arrow | |
| MonadPlus (Proxy :: * -> *) | Since: base-4.9.0.0 |
| Monad m => MonadPlus (MaybeT m) | |
| MonadPlus f => MonadPlus (Rec1 f) | Since: base-4.9.0.0 |
| MonadPlus f => MonadPlus (Alt f) | |
| MonadPlus m => MonadPlus (IdentityT m) | |
| (Monad m, Error e) => MonadPlus (ErrorT e m) | |
| (Monad m, Monoid e) => MonadPlus (ExceptT e m) | |
| MonadPlus m => MonadPlus (StateT s m) | |
| (MonadPlus f, MonadPlus g) => MonadPlus (f :*: g) | Since: base-4.9.0.0 |
| (MonadPlus f, MonadPlus g) => MonadPlus (Product f g) | Since: base-4.9.0.0 |
| MonadPlus m => MonadPlus (ReaderT r m) | |
| MonadPlus f => MonadPlus (M1 i c f) | Since: base-4.9.0.0 |
class Monad m => MonadFail (m :: * -> *) where #
When a value is bound in do-notation, the pattern on the left
hand side of <- might not match. In this case, this class
provides a function to recover.
A Monad without a MonadFail instance may only be used in conjunction
with pattern that always match, such as newtypes, tuples, data types with
only a single data constructor, and irrefutable patterns (~pat).
Instances of MonadFail should satisfy the following law: fail s should
be a left zero for >>=,
fail s >>= f = fail s
If your Monad is also MonadPlus, a popular definition is
fail _ = mzero
Since: base-4.9.0.0
Minimal complete definition
Instances
| MonadFail [] | Since: base-4.9.0.0 |
Defined in Control.Monad.Fail | |
| MonadFail Maybe | Since: base-4.9.0.0 |
Defined in Control.Monad.Fail | |
| MonadFail IO | Since: base-4.9.0.0 |
Defined in Control.Monad.Fail | |
| MonadFail ReadPrec | Since: base-4.9.0.0 |
Defined in Text.ParserCombinators.ReadPrec | |
| MonadFail ReadP | Since: base-4.9.0.0 |
Defined in Text.ParserCombinators.ReadP | |
| MonadFail P | Since: base-4.9.0.0 |
Defined in Text.ParserCombinators.ReadP | |
| IsString str => MonadFail (Either str) # | |
Defined in Relude.Monad.Either | |
| Monad m => MonadFail (MaybeT m) | |
Defined in Control.Monad.Trans.Maybe | |
| MonadFail m => MonadFail (IdentityT m) | |
Defined in Control.Monad.Trans.Identity | |
| (Monad m, Error e) => MonadFail (ErrorT e m) | |
Defined in Control.Monad.Trans.Error | |
| MonadFail m => MonadFail (ExceptT e m) | |
Defined in Control.Monad.Trans.Except | |
| MonadFail m => MonadFail (StateT s m) | |
Defined in Control.Monad.Trans.State.Strict | |
| MonadFail m => MonadFail (ReaderT r m) | |
Defined in Control.Monad.Trans.Reader | |
Reexport Maybe
The Maybe type encapsulates an optional value. A value of type
either contains a value of type Maybe aa (represented as ),
or it is empty (represented as Just aNothing). Using Maybe is a good way to
deal with errors or exceptional cases without resorting to drastic
measures such as error.
The Maybe type is also a monad. It is a simple kind of error
monad, where all errors are represented by Nothing. A richer
error monad can be built using the Either type.
Instances
| Monad Maybe | Since: base-2.1 |
| Functor Maybe | Since: base-2.1 |
| MonadFail Maybe | Since: base-4.9.0.0 |
Defined in Control.Monad.Fail | |
| Applicative Maybe | Since: base-2.1 |
| Foldable Maybe | Since: base-2.1 |
Defined in Data.Foldable Methods fold :: Monoid m => Maybe m -> m # foldMap :: Monoid m => (a -> m) -> Maybe a -> m # foldr :: (a -> b -> b) -> b -> Maybe a -> b # foldr' :: (a -> b -> b) -> b -> Maybe a -> b # foldl :: (b -> a -> b) -> b -> Maybe a -> b # foldl' :: (b -> a -> b) -> b -> Maybe a -> b # foldr1 :: (a -> a -> a) -> Maybe a -> a # foldl1 :: (a -> a -> a) -> Maybe a -> a # elem :: Eq a => a -> Maybe a -> Bool # maximum :: Ord a => Maybe a -> a # minimum :: Ord a => Maybe a -> a # | |
| Traversable Maybe | Since: base-2.1 |
| Eq1 Maybe | Since: base-4.9.0.0 |
| Ord1 Maybe | Since: base-4.9.0.0 |
Defined in Data.Functor.Classes | |
| Read1 Maybe | Since: base-4.9.0.0 |
Defined in Data.Functor.Classes | |
| Show1 Maybe | Since: base-4.9.0.0 |
| Alternative Maybe | Since: base-2.1 |
| MonadPlus Maybe | Since: base-2.1 |
| NFData1 Maybe | Since: deepseq-1.4.3.0 |
Defined in Control.DeepSeq | |
| Hashable1 Maybe | |
Defined in Data.Hashable.Class | |
| Eq a => Eq (Maybe a) | |
| Data a => Data (Maybe a) | Since: base-4.0.0.0 |
Defined in Data.Data Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Maybe a -> c (Maybe a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Maybe a) # toConstr :: Maybe a -> Constr # dataTypeOf :: Maybe a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Maybe a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Maybe a)) # gmapT :: (forall b. Data b => b -> b) -> Maybe a -> Maybe a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Maybe a -> r # gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Maybe a -> r # gmapQ :: (forall d. Data d => d -> u) -> Maybe a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Maybe a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a) # | |
| Ord a => Ord (Maybe a) | |
| Read a => Read (Maybe a) | Since: base-2.1 |
| Show a => Show (Maybe a) | |
| Generic (Maybe a) | |
| Semigroup a => Semigroup (Maybe a) | Since: base-4.9.0.0 |
| Semigroup a => Monoid (Maybe a) | Lift a semigroup into Since 4.11.0: constraint on inner Since: base-2.1 |
| SingKind a => SingKind (Maybe a) | Since: base-4.9.0.0 |
Defined in GHC.Generics | |
| NFData a => NFData (Maybe a) | |
Defined in Control.DeepSeq | |
| Hashable a => Hashable (Maybe a) | |
Defined in Data.Hashable.Class | |
| Generic1 Maybe | |
| SingI (Nothing :: Maybe a) | Since: base-4.9.0.0 |
Defined in GHC.Generics | |
| SingI a2 => SingI (Just a2 :: Maybe a1) | Since: base-4.9.0.0 |
Defined in GHC.Generics | |
| type Rep (Maybe a) | |
| data Sing (b :: Maybe a) | |
| type DemoteRep (Maybe a) | |
Defined in GHC.Generics | |
| type Rep1 Maybe | |
mapMaybe :: (a -> Maybe b) -> [a] -> [b] #
The mapMaybe function is a version of map which can throw
out elements. In particular, the functional argument returns
something of type . If this is Maybe bNothing, no element
is added on to the result list. If it is , then Just bb is
included in the result list.
Examples
Using is a shortcut for mapMaybe f x
in most cases:catMaybes $ map f x
>>>import Text.Read ( readMaybe )>>>let readMaybeInt = readMaybe :: String -> Maybe Int>>>mapMaybe readMaybeInt ["1", "Foo", "3"][1,3]>>>catMaybes $ map readMaybeInt ["1", "Foo", "3"][1,3]
If we map the Just constructor, the entire list should be returned:
>>>mapMaybe Just [1,2,3][1,2,3]
catMaybes :: [Maybe a] -> [a] #
The catMaybes function takes a list of Maybes and returns
a list of all the Just values.
Examples
Basic usage:
>>>catMaybes [Just 1, Nothing, Just 3][1,3]
When constructing a list of Maybe values, catMaybes can be used
to return all of the "success" results (if the list is the result
of a map, then mapMaybe would be more appropriate):
>>>import Text.Read ( readMaybe )>>>[readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ][Just 1,Nothing,Just 3]>>>catMaybes $ [readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ][1,3]
listToMaybe :: [a] -> Maybe a #
The listToMaybe function returns Nothing on an empty list
or where Just aa is the first element of the list.
Examples
Basic usage:
>>>listToMaybe []Nothing
>>>listToMaybe [9]Just 9
>>>listToMaybe [1,2,3]Just 1
Composing maybeToList with listToMaybe should be the identity
on singleton/empty lists:
>>>maybeToList $ listToMaybe [5][5]>>>maybeToList $ listToMaybe [][]
But not on lists with more than one element:
>>>maybeToList $ listToMaybe [1,2,3][1]
maybeToList :: Maybe a -> [a] #
The maybeToList function returns an empty list when given
Nothing or a singleton list when not given Nothing.
Examples
Basic usage:
>>>maybeToList (Just 7)[7]
>>>maybeToList Nothing[]
One can use maybeToList to avoid pattern matching when combined
with a function that (safely) works on lists:
>>>import Text.Read ( readMaybe )>>>sum $ maybeToList (readMaybe "3")3>>>sum $ maybeToList (readMaybe "")0
fromMaybe :: a -> Maybe a -> a #
The fromMaybe function takes a default value and and Maybe
value. If the Maybe is Nothing, it returns the default values;
otherwise, it returns the value contained in the Maybe.
Examples
Basic usage:
>>>fromMaybe "" (Just "Hello, World!")"Hello, World!"
>>>fromMaybe "" Nothing""
Read an integer from a string using readMaybe. If we fail to
parse an integer, we want to return 0 by default:
>>>import Text.Read ( readMaybe )>>>fromMaybe 0 (readMaybe "5")5>>>fromMaybe 0 (readMaybe "")0
maybe :: b -> (a -> b) -> Maybe a -> b #
The maybe function takes a default value, a function, and a Maybe
value. If the Maybe value is Nothing, the function returns the
default value. Otherwise, it applies the function to the value inside
the Just and returns the result.
Examples
Basic usage:
>>>maybe False odd (Just 3)True
>>>maybe False odd NothingFalse
Read an integer from a string using readMaybe. If we succeed,
return twice the integer; that is, apply (*2) to it. If instead
we fail to parse an integer, return 0 by default:
>>>import Text.Read ( readMaybe )>>>maybe 0 (*2) (readMaybe "5")10>>>maybe 0 (*2) (readMaybe "")0
Apply show to a Maybe Int. If we have Just n, we want to show
the underlying Int n. But if we have Nothing, we return the
empty string instead of (for example) "Nothing":
>>>maybe "" show (Just 5)"5">>>maybe "" show Nothing""
Reexport Either
The Either type represents values with two possibilities: a value of
type is either Either a b or Left a.Right b
The Either type is sometimes used to represent a value which is
either correct or an error; by convention, the Left constructor is
used to hold an error value and the Right constructor is used to
hold a correct value (mnemonic: "right" also means "correct").
Examples
The type is the type of values which can be either
a Either String IntString or an Int. The Left constructor can be used only on
Strings, and the Right constructor can be used only on Ints:
>>>let s = Left "foo" :: Either String Int>>>sLeft "foo">>>let n = Right 3 :: Either String Int>>>nRight 3>>>:type ss :: Either String Int>>>:type nn :: Either String Int
The fmap from our Functor instance will ignore Left values, but
will apply the supplied function to values contained in a Right:
>>>let s = Left "foo" :: Either String Int>>>let n = Right 3 :: Either String Int>>>fmap (*2) sLeft "foo">>>fmap (*2) nRight 6
The Monad instance for Either allows us to chain together multiple
actions which may fail, and fail overall if any of the individual
steps failed. First we'll write a function that can either parse an
Int from a Char, or fail.
>>>import Data.Char ( digitToInt, isDigit )>>>:{let parseEither :: Char -> Either String Int parseEither c | isDigit c = Right (digitToInt c) | otherwise = Left "parse error">>>:}
The following should work, since both '1' and '2' can be
parsed as Ints.
>>>:{let parseMultiple :: Either String Int parseMultiple = do x <- parseEither '1' y <- parseEither '2' return (x + y)>>>:}
>>>parseMultipleRight 3
But the following should fail overall, since the first operation where
we attempt to parse 'm' as an Int will fail:
>>>:{let parseMultiple :: Either String Int parseMultiple = do x <- parseEither 'm' y <- parseEither '2' return (x + y)>>>:}
>>>parseMultipleLeft "parse error"
Instances
| Bifunctor Either | Since: base-4.8.0.0 |
| Eq2 Either | Since: base-4.9.0.0 |
| Ord2 Either | Since: base-4.9.0.0 |
Defined in Data.Functor.Classes | |
| Read2 Either | Since: base-4.9.0.0 |
Defined in Data.Functor.Classes Methods liftReadsPrec2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> Int -> ReadS (Either a b) # liftReadList2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> ReadS [Either a b] # liftReadPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec (Either a b) # liftReadListPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec [Either a b] # | |
| Show2 Either | Since: base-4.9.0.0 |
| NFData2 Either | Since: deepseq-1.4.3.0 |
Defined in Control.DeepSeq | |
| Hashable2 Either | |
Defined in Data.Hashable.Class | |
| Monad (Either e) | Since: base-4.4.0.0 |
| Functor (Either a) | Since: base-3.0 |
| IsString str => MonadFail (Either str) # | |
Defined in Relude.Monad.Either | |
| Applicative (Either e) | Since: base-3.0 |
| Foldable (Either a) | Since: base-4.7.0.0 |
Defined in Data.Foldable Methods fold :: Monoid m => Either a m -> m # foldMap :: Monoid m => (a0 -> m) -> Either a a0 -> m # foldr :: (a0 -> b -> b) -> b -> Either a a0 -> b # foldr' :: (a0 -> b -> b) -> b -> Either a a0 -> b # foldl :: (b -> a0 -> b) -> b -> Either a a0 -> b # foldl' :: (b -> a0 -> b) -> b -> Either a a0 -> b # foldr1 :: (a0 -> a0 -> a0) -> Either a a0 -> a0 # foldl1 :: (a0 -> a0 -> a0) -> Either a a0 -> a0 # toList :: Either a a0 -> [a0] # length :: Either a a0 -> Int # elem :: Eq a0 => a0 -> Either a a0 -> Bool # maximum :: Ord a0 => Either a a0 -> a0 # minimum :: Ord a0 => Either a a0 -> a0 # | |
| Traversable (Either a) | Since: base-4.7.0.0 |
Defined in Data.Traversable | |
| Eq a => Eq1 (Either a) | Since: base-4.9.0.0 |
| Ord a => Ord1 (Either a) | Since: base-4.9.0.0 |
Defined in Data.Functor.Classes | |
| Read a => Read1 (Either a) | Since: base-4.9.0.0 |
Defined in Data.Functor.Classes Methods liftReadsPrec :: (Int -> ReadS a0) -> ReadS [a0] -> Int -> ReadS (Either a a0) # liftReadList :: (Int -> ReadS a0) -> ReadS [a0] -> ReadS [Either a a0] # liftReadPrec :: ReadPrec a0 -> ReadPrec [a0] -> ReadPrec (Either a a0) # liftReadListPrec :: ReadPrec a0 -> ReadPrec [a0] -> ReadPrec [Either a a0] # | |
| Show a => Show1 (Either a) | Since: base-4.9.0.0 |
| NFData a => NFData1 (Either a) | Since: deepseq-1.4.3.0 |
Defined in Control.DeepSeq | |
| Hashable a => Hashable1 (Either a) | |
Defined in Data.Hashable.Class | |
| Generic1 (Either a :: * -> *) | |
| (Eq a, Eq b) => Eq (Either a b) | |
| (Data a, Data b) => Data (Either a b) | Since: base-4.0.0.0 |
Defined in Data.Data Methods gfoldl :: (forall d b0. Data d => c (d -> b0) -> d -> c b0) -> (forall g. g -> c g) -> Either a b -> c (Either a b) # gunfold :: (forall b0 r. Data b0 => c (b0 -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Either a b) # toConstr :: Either a b -> Constr # dataTypeOf :: Either a b -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Either a b)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Either a b)) # gmapT :: (forall b0. Data b0 => b0 -> b0) -> Either a b -> Either a b # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Either a b -> r # gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Either a b -> r # gmapQ :: (forall d. Data d => d -> u) -> Either a b -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Either a b -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Either a b -> m (Either a b) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Either a b -> m (Either a b) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Either a b -> m (Either a b) # | |
| (Ord a, Ord b) => Ord (Either a b) | |
| (Read a, Read b) => Read (Either a b) | |
| (Show a, Show b) => Show (Either a b) | |
| Generic (Either a b) | |
| Semigroup (Either a b) | Since: base-4.9.0.0 |
| (NFData a, NFData b) => NFData (Either a b) | |
Defined in Control.DeepSeq | |
| (Hashable a, Hashable b) => Hashable (Either a b) | |
Defined in Data.Hashable.Class | |
| type Rep1 (Either a :: * -> *) | |
Defined in GHC.Generics type Rep1 (Either a :: * -> *) = D1 (MetaData "Either" "Data.Either" "base" False) (C1 (MetaCons "Left" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a)) :+: C1 (MetaCons "Right" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) Par1)) | |
| type Rep (Either a b) | |
Defined in GHC.Generics type Rep (Either a b) = D1 (MetaData "Either" "Data.Either" "base" False) (C1 (MetaCons "Left" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a)) :+: C1 (MetaCons "Right" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 b))) | |
isRight :: Either a b -> Bool #
Return True if the given value is a Right-value, False otherwise.
Examples
Basic usage:
>>>isRight (Left "foo")False>>>isRight (Right 3)True
Assuming a Left value signifies some sort of error, we can use
isRight to write a very simple reporting function that only
outputs "SUCCESS" when a computation has succeeded.
This example shows how isRight might be used to avoid pattern
matching when one does not care about the value contained in the
constructor:
>>>import Control.Monad ( when )>>>let report e = when (isRight e) $ putStrLn "SUCCESS">>>report (Left "parse error")>>>report (Right 1)SUCCESS
Since: base-4.7.0.0
isLeft :: Either a b -> Bool #
Return True if the given value is a Left-value, False otherwise.
Examples
Basic usage:
>>>isLeft (Left "foo")True>>>isLeft (Right 3)False
Assuming a Left value signifies some sort of error, we can use
isLeft to write a very simple error-reporting function that does
absolutely nothing in the case of success, and outputs "ERROR" if
any error occurred.
This example shows how isLeft might be used to avoid pattern
matching when one does not care about the value contained in the
constructor:
>>>import Control.Monad ( when )>>>let report e = when (isLeft e) $ putStrLn "ERROR">>>report (Right 1)>>>report (Left "parse error")ERROR
Since: base-4.7.0.0
partitionEithers :: [Either a b] -> ([a], [b]) #
Partitions a list of Either into two lists.
All the Left elements are extracted, in order, to the first
component of the output. Similarly the Right elements are extracted
to the second component of the output.
Examples
Basic usage:
>>>let list = [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]>>>partitionEithers list(["foo","bar","baz"],[3,7])
The pair returned by should be the same
pair as partitionEithers x(:lefts x, rights x)
>>>let list = [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]>>>partitionEithers list == (lefts list, rights list)True
either :: (a -> c) -> (b -> c) -> Either a b -> c #
Case analysis for the Either type.
If the value is , apply the first function to Left aa;
if it is , apply the second function to Right bb.
Examples
We create two values of type , one using the
Either String IntLeft constructor and another using the Right constructor. Then
we apply "either" the length function (if we have a String)
or the "times-two" function (if we have an Int):
>>>let s = Left "foo" :: Either String Int>>>let n = Right 3 :: Either String Int>>>either length (*2) s3>>>either length (*2) n6