MonadUtils

ghc-7.10.3: The GHC API

Safe Haskell | Safe
Language | Haskell2010

MonadUtils

Description

Utilities related to Monad and Applicative classes Mostly for backwards compatability.

Synopsis

- class Functor f => Applicative f where
  - pure :: a -> f a
  - (<*>) :: f (a -> b) -> f a -> f b
  - (*>) :: f a -> f b -> f b
  - (<*) :: f a -> f b -> f a
- (<$>) :: Functor f => (a -> b) -> f a -> f b
- class Monad m => MonadFix m where
  - mfix :: (a -> m a) -> m a
- class Monad m => MonadIO m where
  - liftIO :: IO a -> m a
- liftIO1 :: MonadIO m => (a -> IO b) -> a -> m b
- liftIO2 :: MonadIO m => (a -> b -> IO c) -> a -> b -> m c
- liftIO3 :: MonadIO m => (a -> b -> c -> IO d) -> a -> b -> c -> m d
- liftIO4 :: MonadIO m => (a -> b -> c -> d -> IO e) -> a -> b -> c -> d -> m e
- zipWith3M :: Monad m => (a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
- zipWith3M_ :: Monad m => (a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m ()
- zipWithAndUnzipM :: Monad m => (a -> b -> m (c, d)) -> [a] -> [b] -> m ([c], [d])
- mapAndUnzipM :: Monad m => (a -> m (b, c)) -> [a] -> m ([b], [c])
- mapAndUnzip3M :: Monad m => (a -> m (b, c, d)) -> [a] -> m ([b], [c], [d])
- mapAndUnzip4M :: Monad m => (a -> m (b, c, d, e)) -> [a] -> m ([b], [c], [d], [e])
- mapAccumLM :: Monad m => (acc -> x -> m (acc, y)) -> acc -> [x] -> m (acc, [y])
- mapSndM :: Monad m => (b -> m c) -> [(a, b)] -> m [(a, c)]
- concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
- mapMaybeM :: Monad m => (a -> m (Maybe b)) -> [a] -> m [b]
- fmapMaybeM :: Monad m => (a -> m b) -> Maybe a -> m (Maybe b)
- fmapEitherM :: Monad m => (a -> m b) -> (c -> m d) -> Either a c -> m (Either b d)
- anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
- allM :: Monad m => (a -> m Bool) -> [a] -> m Bool
- foldlM :: Monad m => (a -> b -> m a) -> a -> [b] -> m a
- foldlM_ :: Monad m => (a -> b -> m a) -> a -> [b] -> m ()
- foldrM :: Monad m => (b -> a -> m a) -> a -> [b] -> m a
- maybeMapM :: Monad m => (a -> m b) -> Maybe a -> m (Maybe b)
- whenM :: Monad m => m Bool -> m () -> m ()

# Documentation

class Functor f => Applicative f where Source

A functor with application, providing operations to

- embed pure expressions (`pure`), and
- sequence computations and combine their results (`<*>`).

A minimal complete definition must include implementations of these functions satisfying the following laws:

identity

      pure id <*> v = v

composition

      pure (.) <*> u <*> v <*> w = u <*> (v <*> w)

homomorphism

      pure f <*> pure x = pure (f x)

interchange

      u <*> pure y = pure ($ y) <*> u

The other methods have the following default definitions, which may be overridden with equivalent specialized implementations:

-     u *> v = pure (const id) <*> u <*> v

-     u <* v = pure const <*> u <*> v

As a consequence of these laws, the `Functor` instance for `f` will satisfy

-     fmap f x = pure f <*> x

If `f` is also a `Monad`, it should satisfy

-     pure = return

-     (<*>) = ap

(which implies that `pure` and `<*>` satisfy the applicative functor laws).

Minimal complete definition

pure, (<*>)

Methods

pure :: a -> f a Source

Lift a value.

(<*>) :: f (a -> b) -> f a -> f b infixl 4 Source

Sequential application.

(*>) :: f a -> f b -> f b infixl 4 Source

Sequence actions, discarding the value of the first argument.

(<*) :: f a -> f b -> f a infixl 4 Source

Sequence actions, discarding the value of the second argument.

Instances

Applicative [] |
Applicative IO |
Applicative Q |
Applicative Id |
Applicative P |
Applicative Identity |
Applicative ZipList |
Applicative STM |
Applicative First |
Applicative Last |
Applicative ReadPrec |
Applicative ReadP |
Applicative Maybe |
Applicative Seq |
Applicative VM |
Applicative SimpleUniqueMonad |
Applicative Pair |
Applicative UniqSM |
Applicative P |
Applicative LlvmM |
Applicative NatM |
Applicative OccCheckResult |
Applicative FCode |
Applicative CmmParse |
Applicative Hsc |
Applicative Ghc |
Applicative CompPipeline |
Applicative TcPluginM |
Applicative CoreM |
Applicative SimplM |
Applicative CpsRn |
Applicative VM |
Applicative TcS |
Applicative ((->) a) |
Applicative (Either e) |
Monoid a => Applicative ((,) a) |
Applicative (ST s) |
Applicative (StateL s) |
Applicative (StateR s) |
Applicative (ST s) |
Monoid m => Applicative (Const m) |
Monad m => Applicative (WrappedMonad m) |
Arrow a => Applicative (ArrowMonad a) |
Applicative (Proxy *) |
Applicative (State s) |
Monad m => Applicative (CheckingFuelMonad m) |
Monad m => Applicative (InfiniteFuelMonad m) |
Monad m => Applicative (UniqueMonadT m) |
Applicative (MaybeErr err) |
(Monad m, Functor m) => Applicative (MaybeT m) |
Applicative (State s) |
Applicative (CmdLineP s) |
Monad m => Applicative (EwM m) |
Applicative (IOEnv m) |
Applicative (RegM freeRegs) |
Applicative m => Applicative (GhcT m) |
Arrow a => Applicative (WrappedArrow a b) |
Applicative f => Applicative (Alt * f) |
(Functor m, Monad m) => Applicative (StateT s m) |
Monad m => Applicative (Stream m a) |

(<$>) :: Functor f => (a -> b) -> f a -> f b infixl 4 Source

An infix synonym for `fmap`.

#### Examples

Convert from a ``Maybe` `Int`` to a ``Maybe` `String`` using `show`:

    >>> show <$> Nothing
    Nothing
    >>> show <$> Just 3
    Just "3"

Convert from an ``Either` `Int` `Int`` to an ``Either` `Int`` `String` using `show`:

    >>> show <$> Left 17
    Left 17
    >>> show <$> Right 17
    Right "17"

Double each element of a list:

    >>> (*2) <$> [1,2,3]
    [2,4,6]

Apply `even` to the second element of a pair:

    >>> even <$> (2,2)
    (2,True)

class Monad m => MonadFix m where Source

Monads having fixed points with a 'knot-tying' semantics. Instances of `MonadFix` should satisfy the following laws:

purity
  ``mfix` (`return` . h) = `return` (`fix` h)`
left shrinking (or tightening)
  ``mfix` (\x -> a >>= \y -> f x y) = a >>= \y -> `mfix` (\x -> f x y)`
sliding
  ``mfix` (`liftM` h . f) = `liftM` h (`mfix` (f . h))`, for strict `h`.
nesting
  ``mfix` (\x -> `mfix` (\y -> f x y)) = `mfix` (\x -> f x x)`

This class is used in the translation of the recursive `do` notation supported by GHC and Hugs.

Methods

mfix :: (a -> m a) -> m a Source

The fixed point of a monadic computation. ``mfix` f` executes the action `f` only once, with the eventual output fed back as the input. Hence `f` should not be strict, for then ``mfix` f` would diverge.

Instances

MonadFix [] |
MonadFix IO |
MonadFix Identity |
MonadFix Maybe |
MonadFix UniqSM |
MonadFix Ghc |
MonadFix ((->) r) |
MonadFix (Either e) |
MonadFix (ST s) |
MonadFix (ST s) |
MonadFix m => MonadFix (StateT s m) |

class Monad m => MonadIO m where Source

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:

-     liftIO . return = return

-     liftIO (m >>= f) = liftIO m >>= (liftIO . f)

Methods

liftIO :: IO a -> m a Source

Lift a computation from the `IO` monad.

Instances

MonadIO IO |
MonadIO Hsc |
MonadIO Ghc |
MonadIO CompPipeline |
MonadIO CoreM |
MonadIO SimplM |
MonadIO VM |
MonadIO (IOEnv env) |
MonadIO m => MonadIO (GhcT m) |
MonadIO m => MonadIO (StateT s m) |

liftIO1 :: MonadIO m => (a -> IO b) -> a -> m b

Lift an `IO` operation with 1 argument into another monad

liftIO2 :: MonadIO m => (a -> b -> IO c) -> a -> b -> m c

Lift an `IO` operation with 2 arguments into another monad

liftIO3 :: MonadIO m => (a -> b -> c -> IO d) -> a -> b -> c -> m d

Lift an `IO` operation with 3 arguments into another monad

liftIO4 :: MonadIO m => (a -> b -> c -> d -> IO e) -> a -> b -> c -> d -> m e

Lift an `IO` operation with 4 arguments into another monad

zipWith3M :: Monad m => (a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]

zipWith3M_ :: Monad m => (a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m ()

zipWithAndUnzipM :: Monad m => (a -> b -> m (c, d)) -> [a] -> [b] -> m ([c], [d])

mapAndUnzipM :: Monad m => (a -> m (b, c)) -> [a] -> m ([b], [c]) Source

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.

mapAndUnzip3M :: Monad m => (a -> m (b, c, d)) -> [a] -> m ([b], [c], [d])

mapAndUnzipM for triples

mapAndUnzip4M :: Monad m => (a -> m (b, c, d, e)) -> [a] -> m ([b], [c], [d], [e])

mapAccumLM

Arguments

:: Monad m |
=> (acc -> x -> m (acc, y)) | combining funcction
-> acc | initial state
-> [x] | inputs
-> m (acc, [y]) | final state, outputs

Monadic version of mapAccumL

mapSndM :: Monad m => (b -> m c) -> [(a, b)] -> m [(a, c)]

Monadic version of mapSnd

concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]

Monadic version of concatMap

mapMaybeM :: Monad m => (a -> m (Maybe b)) -> [a] -> m [b]

Monadic version of mapMaybe

fmapMaybeM :: Monad m => (a -> m b) -> Maybe a -> m (Maybe b)

Monadic version of fmap

fmapEitherM :: Monad m => (a -> m b) -> (c -> m d) -> Either a c -> m (Either b d)

Monadic version of fmap

anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool

Monadic version of `any`, aborts the computation at the first `True` value

allM :: Monad m => (a -> m Bool) -> [a] -> m Bool

Monad version of `all`, aborts the computation at the first `False` value

foldlM :: Monad m => (a -> b -> m a) -> a -> [b] -> m a

Monadic version of foldl

foldlM_ :: Monad m => (a -> b -> m a) -> a -> [b] -> m ()

Monadic version of foldl that discards its result

foldrM :: Monad m => (b -> a -> m a) -> a -> [b] -> m a

Monadic version of foldr

maybeMapM :: Monad m => (a -> m b) -> Maybe a -> m (Maybe b)

Monadic version of fmap specialised for Maybe

whenM :: Monad m => m Bool -> m () -> m ()

Monadic version of `when`, taking the condition in the monad