Control.Monad.Compat

base-compat-0.14.1: A compatibility layer for base

Safe Haskell | Safe-Inferred
Language | Haskell2010

Control.Monad.Compat

Synopsis

- module Control.Monad
- module Control.Monad.Fail
- class Applicative m => Monad (m :: Type -> Type)
- class Monad m => MonadFail (m :: Type -> Type)
- fail :: MonadFail m => String -> m a
- class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) where
  - mzero :: m a
  - mplus :: m a -> m a -> m a

# Documentation

module Control.Monad

module Control.Monad.Fail

class Applicative m => Monad (m :: Type -> Type) #

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:

Left identity
  ``return` a `>>=` k = k a`
Right identity
  `m `>>=` `return` = m`
Associativity
  `m `>>=` (\x -> k x `>>=` h) = (m `>>=` k) `>>=` h`

Furthermore, the `Monad` and `Applicative` operations should relate as follows:

-     pure = return

-     m1 <*> m2 = m1 >>= (\x1 -> m2 >>= (\x2 -> return (x1 x2)))

The above laws imply:

-     fmap f xs  =  xs >>= return . f

-     (>>) = (*>)

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

The instances of `Monad` for `List`, `Maybe` and `IO` defined in the Prelude satisfy these laws.

Minimal complete definition

(>>=)

#### Instances

Instances details

Monad Complex | Since: base-4.9.0.0
Instance details Defined in Data.Complex Methods (>>=) :: Complex a -> (a -> Complex b) -> Complex b # (>>) :: Complex a -> Complex b -> Complex b # return :: a -> Complex a #
Monad First | Since: base-4.9.0.0
Instance details Defined in Data.Semigroup Methods (>>=) :: First a -> (a -> First b) -> First b # (>>) :: First a -> First b -> First b # return :: a -> First a #
Monad Last | Since: base-4.9.0.0
Instance details Defined in Data.Semigroup Methods (>>=) :: Last a -> (a -> Last b) -> Last b # (>>) :: Last a -> Last b -> Last b # return :: a -> Last a #
Monad Max | Since: base-4.9.0.0
Instance details Defined in Data.Semigroup Methods (>>=) :: Max a -> (a -> Max b) -> Max b # (>>) :: Max a -> Max b -> Max b # return :: a -> Max a #
Monad Min | Since: base-4.9.0.0
Instance details Defined in Data.Semigroup Methods (>>=) :: Min a -> (a -> Min b) -> Min b # (>>) :: Min a -> Min b -> Min b # return :: a -> Min a #
Monad NonEmpty | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Base Methods (>>=) :: NonEmpty a -> (a -> NonEmpty b) -> NonEmpty b # (>>) :: NonEmpty a -> NonEmpty b -> NonEmpty b # return :: a -> NonEmpty a #
Monad STM | @since base-4.3.0.0
Instance details Defined in GHC.Internal.Conc.Sync Methods (>>=) :: STM a -> (a -> STM b) -> STM b # (>>) :: STM a -> STM b -> STM b # return :: a -> STM a #
Monad Identity | @since base-4.8.0.0
Instance details Defined in GHC.Internal.Data.Functor.Identity Methods (>>=) :: Identity a -> (a -> Identity b) -> Identity b # (>>) :: Identity a -> Identity b -> Identity b # return :: a -> Identity a #
Monad First | @since base-4.8.0.0
Instance details Defined in GHC.Internal.Data.Monoid Methods (>>=) :: First a -> (a -> First b) -> First b # (>>) :: First a -> First b -> First b # return :: a -> First a #
Monad Last | @since base-4.8.0.0
Instance details Defined in GHC.Internal.Data.Monoid Methods (>>=) :: Last a -> (a -> Last b) -> Last b # (>>) :: Last a -> Last b -> Last b # return :: a -> Last a #
Monad Dual | @since base-4.8.0.0
Instance details Defined in GHC.Internal.Data.Semigroup.Internal Methods (>>=) :: Dual a -> (a -> Dual b) -> Dual b # (>>) :: Dual a -> Dual b -> Dual b # return :: a -> Dual a #
Monad Product | @since base-4.8.0.0
Instance details Defined in GHC.Internal.Data.Semigroup.Internal Methods (>>=) :: Product a -> (a -> Product b) -> Product b # (>>) :: Product a -> Product b -> Product b # return :: a -> Product a #
Monad Sum | @since base-4.8.0.0
Instance details Defined in GHC.Internal.Data.Semigroup.Internal Methods (>>=) :: Sum a -> (a -> Sum b) -> Sum b # (>>) :: Sum a -> Sum b -> Sum b # return :: a -> Sum a #
Monad Par1 | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Generics Methods (>>=) :: Par1 a -> (a -> Par1 b) -> Par1 b # (>>) :: Par1 a -> Par1 b -> Par1 b # return :: a -> Par1 a #
Monad P | @since base-2.01
Instance details Defined in GHC.Internal.Text.ParserCombinators.ReadP Methods (>>=) :: P a -> (a -> P b) -> P b # (>>) :: P a -> P b -> P b # return :: a -> P a #
Monad ReadP | @since base-2.01
Instance details Defined in GHC.Internal.Text.ParserCombinators.ReadP Methods (>>=) :: ReadP a -> (a -> ReadP b) -> ReadP b # (>>) :: ReadP a -> ReadP b -> ReadP b # return :: a -> ReadP a #
Monad ReadPrec | @since base-2.01
Instance details Defined in GHC.Internal.Text.ParserCombinators.ReadPrec Methods (>>=) :: ReadPrec a -> (a -> ReadPrec b) -> ReadPrec b # (>>) :: ReadPrec a -> ReadPrec b -> ReadPrec b # return :: a -> ReadPrec a #
Monad IO | @since base-2.01
Instance details Defined in GHC.Internal.Base Methods (>>=) :: IO a -> (a -> IO b) -> IO b # (>>) :: IO a -> IO b -> IO b # return :: a -> IO a #
Monad Maybe | @since base-2.01
Instance details Defined in GHC.Internal.Base Methods (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b # (>>) :: Maybe a -> Maybe b -> Maybe b # return :: a -> Maybe a #
Monad Solo | @since base-4.15
Instance details Defined in GHC.Internal.Base Methods (>>=) :: Solo a -> (a -> Solo b) -> Solo b # (>>) :: Solo a -> Solo b -> Solo b # return :: a -> Solo a #
Monad [] | @since base-2.01
Instance details Defined in GHC.Internal.Base Methods (>>=) :: [a] -> (a -> [b]) -> [b] # (>>) :: [a] -> [b] -> [b] # return :: a -> [a] #
Monad m => Monad (WrappedMonad m) | Since: base-4.7.0.0
Instance details 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 #
ArrowApply a => Monad (ArrowMonad a) | @since base-2.01
Instance details Defined in GHC.Internal.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 #
Monad (ST s) | @since base-2.01
Instance details Defined in GHC.Internal.Control.Monad.ST.Lazy.Imp Methods (>>=) :: ST s a -> (a -> ST s b) -> ST s b # (>>) :: ST s a -> ST s b -> ST s b # return :: a -> ST s a #
Monad (Either e) | @since base-4.4.0.0
Instance details Defined in GHC.Internal.Data.Either Methods (>>=) :: Either e a -> (a -> Either e b) -> Either e b # (>>) :: Either e a -> Either e b -> Either e b # return :: a -> Either e a #
Monad (Proxy :: Type -> Type) | @since base-4.7.0.0
Instance details Defined in GHC.Internal.Data.Proxy Methods (>>=) :: Proxy a -> (a -> Proxy b) -> Proxy b # (>>) :: Proxy a -> Proxy b -> Proxy b # return :: a -> Proxy a #
Monad (U1 :: Type -> Type) | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Generics Methods (>>=) :: U1 a -> (a -> U1 b) -> U1 b # (>>) :: U1 a -> U1 b -> U1 b # return :: a -> U1 a #
Monad (ST s) | @since base-2.01
Instance details Defined in GHC.Internal.ST Methods (>>=) :: ST s a -> (a -> ST s b) -> ST s b # (>>) :: ST s a -> ST s b -> ST s b # return :: a -> ST s a #
Monoid a => Monad ((,) a) | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Base Methods (>>=) :: (a, a0) -> (a0 -> (a, b)) -> (a, b) # (>>) :: (a, a0) -> (a, b) -> (a, b) # return :: a0 -> (a, a0) #
Monad m => Monad (Kleisli m a) | @since base-4.14.0.0
Instance details Defined in GHC.Internal.Control.Arrow Methods (>>=) :: Kleisli m a a0 -> (a0 -> Kleisli m a b) -> Kleisli m a b # (>>) :: Kleisli m a a0 -> Kleisli m a b -> Kleisli m a b # return :: a0 -> Kleisli m a a0 #
Monad f => Monad (Ap f) | @since base-4.12.0.0
Instance details Defined in GHC.Internal.Data.Monoid Methods (>>=) :: Ap f a -> (a -> Ap f b) -> Ap f b # (>>) :: Ap f a -> Ap f b -> Ap f b # return :: a -> Ap f a #
Monad f => Monad (Alt f) | @since base-4.8.0.0
Instance details Defined in GHC.Internal.Data.Semigroup.Internal Methods (>>=) :: Alt f a -> (a -> Alt f b) -> Alt f b # (>>) :: Alt f a -> Alt f b -> Alt f b # return :: a -> Alt f a #
Monad f => Monad (Rec1 f) | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Generics Methods (>>=) :: Rec1 f a -> (a -> Rec1 f b) -> Rec1 f b # (>>) :: Rec1 f a -> Rec1 f b -> Rec1 f b # return :: a -> Rec1 f a #
(Monoid a, Monoid b) => Monad ((,,) a b) | @since base-4.14.0.0
Instance details Defined in GHC.Internal.Base Methods (>>=) :: (a, b, a0) -> (a0 -> (a, b, b0)) -> (a, b, b0) # (>>) :: (a, b, a0) -> (a, b, b0) -> (a, b, b0) # return :: a0 -> (a, b, a0) #
(Monad f, Monad g) => Monad (Product f g) | Since: base-4.9.0.0
Instance details Defined in Data.Functor.Product Methods (>>=) :: Product f g a -> (a -> Product f g b) -> Product f g b # (>>) :: Product f g a -> Product f g b -> Product f g b # return :: a -> Product f g a #
(Monad f, Monad g) => Monad (f :*: g) | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Generics Methods (>>=) :: (f :*: g) a -> (a -> (f :*: g) b) -> (f :*: g) b # (>>) :: (f :*: g) a -> (f :*: g) b -> (f :*: g) b # return :: a -> (f :*: g) a #
(Monoid a, Monoid b, Monoid c) => Monad ((,,,) a b c) | @since base-4.14.0.0
Instance details Defined in GHC.Internal.Base Methods (>>=) :: (a, b, c, a0) -> (a0 -> (a, b, c, b0)) -> (a, b, c, b0) # (>>) :: (a, b, c, a0) -> (a, b, c, b0) -> (a, b, c, b0) # return :: a0 -> (a, b, c, a0) #
Monad ((->) r) | @since base-2.01
Instance details Defined in GHC.Internal.Base Methods (>>=) :: (r -> a) -> (a -> r -> b) -> r -> b # (>>) :: (r -> a) -> (r -> b) -> r -> b # return :: a -> r -> a #
Monad f => Monad (M1 i c f) | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Generics Methods (>>=) :: M1 i c f a -> (a -> M1 i c f b) -> M1 i c f b # (>>) :: M1 i c f a -> M1 i c f b -> M1 i c f b # return :: a -> M1 i c f a #

class Monad m => MonadFail (m :: Type -> Type) #

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

`fail s` should be an action that runs in the monad itself, not an exception (except in instances of `MonadIO`). In particular, `fail` should not be implemented in terms of `error`.

@since base-4.9.0.0

Minimal complete definition

fail

#### Instances

Instances details

MonadFail P | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Text.ParserCombinators.ReadP Methods fail :: String -> P a #
MonadFail ReadP | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Text.ParserCombinators.ReadP Methods fail :: String -> ReadP a #
MonadFail ReadPrec | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Text.ParserCombinators.ReadPrec Methods fail :: String -> ReadPrec a #
MonadFail IO | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Control.Monad.Fail Methods fail :: String -> IO a #
MonadFail Maybe | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Control.Monad.Fail Methods fail :: String -> Maybe a #
MonadFail [] | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Control.Monad.Fail Methods fail :: String -> [a] #
MonadFail f => MonadFail (Ap f) | @since base-4.12.0.0
Instance details Defined in GHC.Internal.Data.Monoid Methods fail :: String -> Ap f a #

fail :: MonadFail m => String -> m a #

class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) where #

Monads that also support choice and failure.

Minimal complete definition

Nothing

Methods

mzero :: m a #

The identity of `mplus`. It should also satisfy the equations

    mzero >>= f  =  mzero
    v >> mzero   =  mzero

The default definition is

    mzero = empty

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

An associative operation. The default definition is

    mplus = (<|>)

#### Instances

Instances details

MonadPlus STM | Takes the first non-`retry`ing `STM` action. @since base-4.3.0.0
Instance details Defined in GHC.Internal.Conc.Sync Methods mzero :: STM a # mplus :: STM a -> STM a -> STM a #
MonadPlus P | @since base-2.01
Instance details Defined in GHC.Internal.Text.ParserCombinators.ReadP Methods mzero :: P a # mplus :: P a -> P a -> P a #
MonadPlus ReadP | @since base-2.01
Instance details Defined in GHC.Internal.Text.ParserCombinators.ReadP Methods mzero :: ReadP a # mplus :: ReadP a -> ReadP a -> ReadP a #
MonadPlus ReadPrec | @since base-2.01
Instance details Defined in GHC.Internal.Text.ParserCombinators.ReadPrec Methods mzero :: ReadPrec a # mplus :: ReadPrec a -> ReadPrec a -> ReadPrec a #
MonadPlus IO | Takes the first non-throwing `IO` action's result. `mzero` throws an exception. @since base-4.9.0.0
Instance details Defined in GHC.Internal.Base Methods mzero :: IO a # mplus :: IO a -> IO a -> IO a #
MonadPlus Maybe | Picks the leftmost `Just` value, or, alternatively, `Nothing`. @since base-2.01
Instance details Defined in GHC.Internal.Base Methods mzero :: Maybe a # mplus :: Maybe a -> Maybe a -> Maybe a #
MonadPlus [] | Combines lists by concatenation, starting from the empty list. @since base-2.01
Instance details Defined in GHC.Internal.Base Methods mzero :: [a] # mplus :: [a] -> [a] -> [a] #
(ArrowApply a, ArrowPlus a) => MonadPlus (ArrowMonad a) | @since base-4.6.0.0
Instance details Defined in GHC.Internal.Control.Arrow Methods mzero :: ArrowMonad a a0 # mplus :: ArrowMonad a a0 -> ArrowMonad a a0 -> ArrowMonad a a0 #
MonadPlus (Proxy :: Type -> Type) | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Data.Proxy Methods mzero :: Proxy a # mplus :: Proxy a -> Proxy a -> Proxy a #
MonadPlus (U1 :: Type -> Type) | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Generics Methods mzero :: U1 a # mplus :: U1 a -> U1 a -> U1 a #
MonadPlus m => MonadPlus (Kleisli m a) | @since base-4.14.0.0
Instance details Defined in GHC.Internal.Control.Arrow Methods mzero :: Kleisli m a a0 # mplus :: Kleisli m a a0 -> Kleisli m a a0 -> Kleisli m a a0 #
MonadPlus f => MonadPlus (Ap f) | @since base-4.12.0.0
Instance details Defined in GHC.Internal.Data.Monoid Methods mzero :: Ap f a # mplus :: Ap f a -> Ap f a -> Ap f a #
MonadPlus f => MonadPlus (Alt f) | @since base-4.8.0.0
Instance details Defined in GHC.Internal.Data.Semigroup.Internal Methods mzero :: Alt f a # mplus :: Alt f a -> Alt f a -> Alt f a #
MonadPlus f => MonadPlus (Rec1 f) | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Generics Methods mzero :: Rec1 f a # mplus :: Rec1 f a -> Rec1 f a -> Rec1 f a #
(MonadPlus f, MonadPlus g) => MonadPlus (Product f g) | Since: base-4.9.0.0
Instance details Defined in Data.Functor.Product Methods mzero :: Product f g a # mplus :: Product f g a -> Product f g a -> Product f g a #
(MonadPlus f, MonadPlus g) => MonadPlus (f :*: g) | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Generics Methods mzero :: (f :*: g) a # mplus :: (f :*: g) a -> (f :*: g) a -> (f :*: g) a #
MonadPlus f => MonadPlus (M1 i c f) | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Generics Methods mzero :: M1 i c f a # mplus :: M1 i c f a -> M1 i c f a -> M1 i c f a #