Control.Applicative (original) (raw)
Contents
Description
This module describes a structure intermediate between a functor and a monad (technically, a strong lax monoidal functor). Compared with monads, this interface lacks the full power of the binding operation[>>=](Control-Monad.html#v:-62--62--61-)
, but
- it has more instances.
- it is sufficient for many uses, e.g. context-free parsing, or the
[Traversable](Data-Traversable.html#t:Traversable)
class. - instances can perform analysis of computations before they are executed, and thus produce shared optimizations.
This interface was introduced for parsers by Niklas Röjemo, because it admits more sharing than the monadic interface. The names here are mostly based on parsing work by Doaitse Swierstra.
For more details, seeApplicative Programming with Effects, by Conor McBride and Ross Paterson.
Synopsis
- class Functor f => Applicative f where
- class Applicative f => Alternative f where
- newtype Const a b = Const {
- getConst :: a
}
- getConst :: a
- newtype WrappedMonad m a = WrapMonad {
- unwrapMonad :: m a
}
- unwrapMonad :: m a
- newtype WrappedArrow a b c = WrapArrow {
- unwrapArrow :: a b c
}
- unwrapArrow :: a b c
- newtype ZipList a = ZipList {
- getZipList :: [a]
}
- getZipList :: [a]
- (<$>) :: Functor f => (a -> b) -> f a -> f b
- (<$) :: Functor f => a -> f b -> f a
- (<**>) :: Applicative f => f a -> f (a -> b) -> f b
- liftA :: Applicative f => (a -> b) -> f a -> f b
- liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
- liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
- optional :: Alternative f => f a -> f (Maybe a)
Applicative functors
class Functor f => Applicative f where Source
A functor with application, providing operations to
- embed pure expressions (
[pure](Control-Applicative.html#v:pure)
), and - sequence computations and combine their results (
[<*>](Control-Applicative.html#v:-60--42--62-)
).
A minimal complete definition must include implementations of these functions satisfying the following laws:
identity
[pure](Control-Applicative.html#v:pure)
[id](Control-Category.html#v:id)
[<*>](Control-Applicative.html#v:-60--42--62-)
v = v
composition
[pure](Control-Applicative.html#v:pure)
(.) [<*>](Control-Applicative.html#v:-60--42--62-)
u [<*>](Control-Applicative.html#v:-60--42--62-)
v [<*>](Control-Applicative.html#v:-60--42--62-)
w = u [<*>](Control-Applicative.html#v:-60--42--62-)
(v [<*>](Control-Applicative.html#v:-60--42--62-)
w)
homomorphism
[pure](Control-Applicative.html#v:pure)
f [<*>](Control-Applicative.html#v:-60--42--62-)
[pure](Control-Applicative.html#v:pure)
x = [pure](Control-Applicative.html#v:pure)
(f x)
interchange
u [<*>](Control-Applicative.html#v:-60--42--62-)
[pure](Control-Applicative.html#v:pure)
y = [pure](Control-Applicative.html#v:pure)
([$](Prelude.html#v:-36-)
y) [<*>](Control-Applicative.html#v:-60--42--62-)
u
The other methods have the following default definitions, which may be overridden with equivalent specialized implementations:
- u
[*>](Control-Applicative.html#v:-42--62-)
v =[pure](Control-Applicative.html#v:pure)
([const](Prelude.html#v:const)
[id](Control-Category.html#v:id)
)[<*>](Control-Applicative.html#v:-60--42--62-)
u[<*>](Control-Applicative.html#v:-60--42--62-)
v - u
[<*](Control-Applicative.html#v:-60--42-)
v =[pure](Control-Applicative.html#v:pure)
[const](Prelude.html#v:const)
[<*>](Control-Applicative.html#v:-60--42--62-)
u[<*>](Control-Applicative.html#v:-60--42--62-)
v
As a consequence of these laws, the [Functor](Control-Monad.html#t:Functor)
instance for f
will satisfy
[fmap](Control-Monad.html#v:fmap)
f x =[pure](Control-Applicative.html#v:pure)
f[<*>](Control-Applicative.html#v:-60--42--62-)
x
If f
is also a [Monad](Control-Monad.html#t:Monad)
, it should satisfy
[pure](Control-Applicative.html#v:pure)
=[return](Control-Monad.html#v:return)
- (
[<*>](Control-Applicative.html#v:-60--42--62-)
) =[ap](Control-Monad.html#v:ap)
(which implies that [pure](Control-Applicative.html#v:pure)
and [<*>](Control-Applicative.html#v:-60--42--62-)
satisfy the applicative functor laws).
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.
Alternatives
Instances
Utility functions
(<$) :: Functor f => a -> f b -> f a Source
Replace all locations in the input with the same value. The default definition is `[fmap](Control-Monad.html#v:fmap)` . `[const](Prelude.html#v:const)`
, but this may be overridden with a more efficient version.
liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d Source
Lift a ternary function to actions.