Copyright  Conor McBride and Ross Paterson 2005 

License  BSDstyle (see the LICENSE file in the distribution) 
Maintainer  libraries@haskell.org 
Stability  experimental 
Portability  portable 
Safe Haskell  Trustworthy 
Language  Haskell2010 
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
>>=
, but
 it has more instances.
 it is sufficient for many uses, e.g. contextfree parsing, or the
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, see Applicative 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
 newtype WrappedMonad m a = WrapMonad {
 unwrapMonad :: m a
 newtype WrappedArrow a b c = WrapArrow {
 unwrapArrow :: a b c
 newtype ZipList a = ZipList {
 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
 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
A minimal complete definition must include implementations of pure
and of either <*>
or liftA2
. If it defines both, then they must behave
the same as their default definitions:
(<*>
) =liftA2
id
liftA2
f x y = f<$>
x<*>
y
Further, any definition must satisfy the following:
 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:
As a consequence of these laws, the Functor
instance for f
will satisfy
It may be useful to note that supposing
forall x y. p (q x y) = f x . g y
it follows from the above that
liftA2
p (liftA2
q u v) =liftA2
f u .liftA2
g v
If f
is also a Monad
, it should satisfy
(which implies that pure
and <*>
satisfy the applicative functor laws).
Lift a value.
(<*>) :: f (a > b) > f a > f b infixl 4 Source #
Sequential application.
A few functors support an implementation of <*>
that is more
efficient than the default one.
liftA2 :: (a > b > c) > f a > f b > f c Source #
Lift a binary function to actions.
Some functors support an implementation of liftA2
that is more
efficient than the default one. In particular, if fmap
is an
expensive operation, it is likely better to use liftA2
than to
fmap
over the structure and then use <*>
.
(*>) :: 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 [] #  Since: base2.1 
Applicative Maybe #  Since: base2.1 
Applicative IO #  Since: base2.1 
Applicative Par1 #  Since: base4.9.0.0 
Applicative NonEmpty #  Since: base4.9.0.0 
Applicative ReadP #  Since: base4.6.0.0 
Applicative ReadPrec #  Since: base4.6.0.0 
Defined in Text.ParserCombinators.ReadPrec  
Applicative Down #  Since: base4.11.0.0 
Applicative Product #  Since: base4.8.0.0 
Defined in Data.Semigroup.Internal  
Applicative Sum #  Since: base4.8.0.0 
Applicative Dual #  Since: base4.8.0.0 
Applicative Last #  
Applicative First #  
Applicative STM #  Since: base4.8.0.0 
Applicative Identity #  Since: base4.8.0.0 
Defined in Data.Functor.Identity  
Applicative ZipList #  f '<$>' 'ZipList' xs1 '<*>' ... '<*>' 'ZipList' xsN = 'ZipList' (zipWithN f xs1 ... xsN) where (\a b c > stimes c [a, b]) <$> ZipList "abcd" <*> ZipList "567" <*> ZipList [1..] = ZipList (zipWith3 (\a b c > stimes c [a, b]) "abcd" "567" [1..]) = ZipList {getZipList = ["a5","b6b6","c7c7c7"]} Since: base2.1 
Applicative Option #  Since: base4.9.0.0 
Applicative Last #  Since: base4.9.0.0 
Applicative First #  Since: base4.9.0.0 
Applicative Max #  Since: base4.9.0.0 
Applicative Min #  Since: base4.9.0.0 
Applicative Complex #  Since: base4.9.0.0 
Applicative (Either e) #  Since: base3.0 
Defined in Data.Either  
Applicative (U1 :: * > *) #  Since: base4.9.0.0 
Monoid a => Applicative ((,) a) #  For tuples, the ("hello ", (+15)) <*> ("world!", 2002) ("hello world!",2017) Since: base2.1 
Applicative (ST s) #  Since: base4.4.0.0 
Applicative (Proxy :: * > *) #  Since: base4.7.0.0 
Arrow a => Applicative (ArrowMonad a) #  Since: base4.6.0.0 
Defined in Control.Arrow pure :: a0 > ArrowMonad a a0 Source # (<*>) :: ArrowMonad a (a0 > b) > ArrowMonad a a0 > ArrowMonad a b Source # liftA2 :: (a0 > b > c) > ArrowMonad a a0 > ArrowMonad a b > ArrowMonad a c Source # (*>) :: ArrowMonad a a0 > ArrowMonad a b > ArrowMonad a b Source # (<*) :: ArrowMonad a a0 > ArrowMonad a b > ArrowMonad a a0 Source #  
Monad m => Applicative (WrappedMonad m) #  Since: base2.1 
Defined in Control.Applicative pure :: a > WrappedMonad m a Source # (<*>) :: WrappedMonad m (a > b) > WrappedMonad m a > WrappedMonad m b Source # liftA2 :: (a > b > c) > WrappedMonad m a > WrappedMonad m b > WrappedMonad m c Source # (*>) :: WrappedMonad m a > WrappedMonad m b > WrappedMonad m b Source # (<*) :: WrappedMonad m a > WrappedMonad m b > WrappedMonad m a Source #  
Applicative (ST s) #  Since: base2.1 
Applicative f => Applicative (Rec1 f) #  Since: base4.9.0.0 
Applicative f => Applicative (Alt f) #  
Monoid m => Applicative (Const m :: * > *) #  Since: base2.0.1 
Arrow a => Applicative (WrappedArrow a b) #  Since: base2.1 
Defined in Control.Applicative pure :: a0 > WrappedArrow a b a0 Source # (<*>) :: WrappedArrow a b (a0 > b0) > WrappedArrow a b a0 > WrappedArrow a b b0 Source # liftA2 :: (a0 > b0 > c) > WrappedArrow a b a0 > WrappedArrow a b b0 > WrappedArrow a b c Source # (*>) :: WrappedArrow a b a0 > WrappedArrow a b b0 > WrappedArrow a b b0 Source # (<*) :: WrappedArrow a b a0 > WrappedArrow a b b0 > WrappedArrow a b a0 Source #  
Applicative ((>) a :: * > *) #  Since: base2.1 
(Applicative f, Applicative g) => Applicative (f :*: g) #  Since: base4.9.0.0 
Defined in GHC.Generics  
(Applicative f, Applicative g) => Applicative (Product f g) #  Since: base4.9.0.0 
Defined in Data.Functor.Product pure :: a > Product f g a Source # (<*>) :: Product f g (a > b) > Product f g a > Product f g b Source # liftA2 :: (a > b > c) > Product f g a > Product f g b > Product f g c Source # (*>) :: Product f g a > Product f g b > Product f g b Source # (<*) :: Product f g a > Product f g b > Product f g a Source #  
Applicative f => Applicative (M1 i c f) #  Since: base4.9.0.0 
Defined in GHC.Generics  
(Applicative f, Applicative g) => Applicative (f :.: g) #  Since: base4.9.0.0 
Defined in GHC.Generics  
(Applicative f, Applicative g) => Applicative (Compose f g) #  Since: base4.9.0.0 
Defined in Data.Functor.Compose pure :: a > Compose f g a Source # (<*>) :: Compose f g (a > b) > Compose f g a > Compose f g b Source # liftA2 :: (a > b > c) > Compose f g a > Compose f g b > Compose f g c Source # (*>) :: Compose f g a > Compose f g b > Compose f g b Source # (<*) :: Compose f g a > Compose f g b > Compose f g a Source # 
Alternatives
class Applicative f => Alternative f where Source #
A monoid on applicative functors.
If defined, some
and many
should be the least solutions
of the equations:
The identity of <>
(<>) :: f a > f a > f a infixl 3 Source #
An associative binary operation
One or more.
Zero or more.
Instances
Instances
The Const
functor.
Instances
Generic1 (Const a :: k > *) #  
Show2 (Const :: * > * > *) #  Since: base4.9.0.0 
Read2 (Const :: * > * > *) #  Since: base4.9.0.0 
Defined in Data.Functor.Classes liftReadsPrec2 :: (Int > ReadS a) > ReadS [a] > (Int > ReadS b) > ReadS [b] > Int > ReadS (Const a b) Source # liftReadList2 :: (Int > ReadS a) > ReadS [a] > (Int > ReadS b) > ReadS [b] > ReadS [Const a b] Source # liftReadPrec2 :: ReadPrec a > ReadPrec [a] > ReadPrec b > ReadPrec [b] > ReadPrec (Const a b) Source # liftReadListPrec2 :: ReadPrec a > ReadPrec [a] > ReadPrec b > ReadPrec [b] > ReadPrec [Const a b] Source #  
Ord2 (Const :: * > * > *) #  Since: base4.9.0.0 
Defined in Data.Functor.Classes  
Eq2 (Const :: * > * > *) #  Since: base4.9.0.0 
Bifunctor (Const :: * > * > *) #  Since: base4.8.0.0 
Bifoldable (Const :: * > * > *) #  Since: base4.10.0.0 
Bitraversable (Const :: * > * > *) #  Since: base4.10.0.0 
Defined in Data.Bitraversable bitraverse :: Applicative f => (a > f c) > (b > f d) > Const a b > f (Const c d) Source #  
Functor (Const m :: * > *) #  Since: base2.1 
Monoid m => Applicative (Const m :: * > *) #  Since: base2.0.1 
Foldable (Const m :: * > *) #  Since: base4.7.0.0 
Defined in Data.Functor.Const fold :: Monoid m0 => Const m m0 > m0 Source # foldMap :: Monoid m0 => (a > m0) > Const m a > m0 Source # foldr :: (a > b > b) > b > Const m a > b Source # foldr' :: (a > b > b) > b > Const m a > b Source # foldl :: (b > a > b) > b > Const m a > b Source # foldl' :: (b > a > b) > b > Const m a > b Source # foldr1 :: (a > a > a) > Const m a > a Source # foldl1 :: (a > a > a) > Const m a > a Source # toList :: Const m a > [a] Source # null :: Const m a > Bool Source # length :: Const m a > Int Source # elem :: Eq a => a > Const m a > Bool Source # maximum :: Ord a => Const m a > a Source # minimum :: Ord a => Const m a > a Source #  
Traversable (Const m :: * > *) #  Since: base4.7.0.0 
Defined in Data.Traversable  
Show a => Show1 (Const a :: * > *) #  Since: base4.9.0.0 
Read a => Read1 (Const a :: * > *) #  Since: base4.9.0.0 
Defined in Data.Functor.Classes liftReadsPrec :: (Int > ReadS a0) > ReadS [a0] > Int > ReadS (Const a a0) Source # liftReadList :: (Int > ReadS a0) > ReadS [a0] > ReadS [Const a a0] Source # liftReadPrec :: ReadPrec a0 > ReadPrec [a0] > ReadPrec (Const a a0) Source # liftReadListPrec :: ReadPrec a0 > ReadPrec [a0] > ReadPrec [Const a a0] Source #  
Ord a => Ord1 (Const a :: * > *) #  Since: base4.9.0.0 
Defined in Data.Functor.Classes  
Eq a => Eq1 (Const a :: * > *) #  Since: base4.9.0.0 
Bounded a => Bounded (Const a b) #  
Enum a => Enum (Const a b) #  
Defined in Data.Functor.Const succ :: Const a b > Const a b Source # pred :: Const a b > Const a b Source # toEnum :: Int > Const a b Source # fromEnum :: Const a b > Int Source # enumFrom :: Const a b > [Const a b] Source # enumFromThen :: Const a b > Const a b > [Const a b] Source # enumFromTo :: Const a b > Const a b > [Const a b] Source # enumFromThenTo :: Const a b > Const a b > Const a b > [Const a b] Source #  
Eq a => Eq (Const a b) #  
Floating a => Floating (Const a b) #  
Defined in Data.Functor.Const exp :: Const a b > Const a b Source # log :: Const a b > Const a b Source # sqrt :: Const a b > Const a b Source # (**) :: Const a b > Const a b > Const a b Source # logBase :: Const a b > Const a b > Const a b Source # sin :: Const a b > Const a b Source # cos :: Const a b > Const a b Source # tan :: Const a b > Const a b Source # asin :: Const a b > Const a b Source # acos :: Const a b > Const a b Source # atan :: Const a b > Const a b Source # sinh :: Const a b > Const a b Source # cosh :: Const a b > Const a b Source # tanh :: Const a b > Const a b Source # asinh :: Const a b > Const a b Source # acosh :: Const a b > Const a b Source # atanh :: Const a b > Const a b Source # log1p :: Const a b > Const a b Source # expm1 :: Const a b > Const a b Source #  
Fractional a => Fractional (Const a b) #  
Integral a => Integral (Const a b) #  
Defined in Data.Functor.Const quot :: Const a b > Const a b > Const a b Source # rem :: Const a b > Const a b > Const a b Source # div :: Const a b > Const a b > Const a b Source # mod :: Const a b > Const a b > Const a b Source # quotRem :: Const a b > Const a b > (Const a b, Const a b) Source # divMod :: Const a b > Const a b > (Const a b, Const a b) Source #  
(Typeable k, Data a, Typeable b) => Data (Const a b) #  Since: base4.10.0.0 
Defined in Data.Data gfoldl :: (forall d b0. Data d => c (d > b0) > d > c b0) > (forall g. g > c g) > Const a b > c (Const a b) Source # gunfold :: (forall b0 r. Data b0 => c (b0 > r) > c r) > (forall r. r > c r) > Constr > c (Const a b) Source # toConstr :: Const a b > Constr Source # dataTypeOf :: Const a b > DataType Source # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) > Maybe (c (Const a b)) Source # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) > Maybe (c (Const a b)) Source # gmapT :: (forall b0. Data b0 => b0 > b0) > Const a b > Const a b Source # gmapQl :: (r > r' > r) > r > (forall d. Data d => d > r') > Const a b > r Source # gmapQr :: (r' > r > r) > r > (forall d. Data d => d > r') > Const a b > r Source # gmapQ :: (forall d. Data d => d > u) > Const a b > [u] Source # gmapQi :: Int > (forall d. Data d => d > u) > Const a b > u Source # gmapM :: Monad m => (forall d. Data d => d > m d) > Const a b > m (Const a b) Source # gmapMp :: MonadPlus m => (forall d. Data d => d > m d) > Const a b > m (Const a b) Source # gmapMo :: MonadPlus m => (forall d. Data d => d > m d) > Const a b > m (Const a b) Source #  
Num a => Num (Const a b) #  
Defined in Data.Functor.Const (+) :: Const a b > Const a b > Const a b Source # () :: Const a b > Const a b > Const a b Source # (*) :: Const a b > Const a b > Const a b Source # negate :: Const a b > Const a b Source # abs :: Const a b > Const a b Source # signum :: Const a b > Const a b Source # fromInteger :: Integer > Const a b Source #  
Ord a => Ord (Const a b) #  
Defined in Data.Functor.Const  
Read a => Read (Const a b) #  This instance would be equivalent to the derived instances of the
Since: base4.8.0.0 
Real a => Real (Const a b) #  
Defined in Data.Functor.Const toRational :: Const a b > Rational Source #  
RealFloat a => RealFloat (Const a b) #  
Defined in Data.Functor.Const floatRadix :: Const a b > Integer Source # floatDigits :: Const a b > Int Source # floatRange :: Const a b > (Int, Int) Source # decodeFloat :: Const a b > (Integer, Int) Source # encodeFloat :: Integer > Int > Const a b Source # exponent :: Const a b > Int Source # significand :: Const a b > Const a b Source # scaleFloat :: Int > Const a b > Const a b Source # isNaN :: Const a b > Bool Source # isInfinite :: Const a b > Bool Source # isDenormalized :: Const a b > Bool Source # isNegativeZero :: Const a b > Bool Source #  
RealFrac a => RealFrac (Const a b) #  
Show a => Show (Const a b) #  This instance would be equivalent to the derived instances of the
Since: base4.8.0.0 
Ix a => Ix (Const a b) #  
Defined in Data.Functor.Const range :: (Const a b, Const a b) > [Const a b] Source # index :: (Const a b, Const a b) > Const a b > Int Source # unsafeIndex :: (Const a b, Const a b) > Const a b > Int inRange :: (Const a b, Const a b) > Const a b > Bool Source # rangeSize :: (Const a b, Const a b) > Int Source # unsafeRangeSize :: (Const a b, Const a b) > Int  
IsString a => IsString (Const a b) #  Since: base4.9.0.0 
Defined in Data.String fromString :: String > Const a b Source #  
Generic (Const a b) #  
Semigroup a => Semigroup (Const a b) #  
Monoid a => Monoid (Const a b) #  
FiniteBits a => FiniteBits (Const a b) #  
Defined in Data.Functor.Const finiteBitSize :: Const a b > Int Source # countLeadingZeros :: Const a b > Int Source # countTrailingZeros :: Const a b > Int Source #  
Bits a => Bits (Const a b) #  
Defined in Data.Functor.Const (.&.) :: Const a b > Const a b > Const a b Source # (..) :: Const a b > Const a b > Const a b Source # xor :: Const a b > Const a b > Const a b Source # complement :: Const a b > Const a b Source # shift :: Const a b > Int > Const a b Source # rotate :: Const a b > Int > Const a b Source # zeroBits :: Const a b Source # bit :: Int > Const a b Source # setBit :: Const a b > Int > Const a b Source # clearBit :: Const a b > Int > Const a b Source # complementBit :: Const a b > Int > Const a b Source # testBit :: Const a b > Int > Bool Source # bitSizeMaybe :: Const a b > Maybe Int Source # bitSize :: Const a b > Int Source # isSigned :: Const a b > Bool Source # shiftL :: Const a b > Int > Const a b Source # unsafeShiftL :: Const a b > Int > Const a b Source # shiftR :: Const a b > Int > Const a b Source # unsafeShiftR :: Const a b > Int > Const a b Source # rotateL :: Const a b > Int > Const a b Source #  
Storable a => Storable (Const a b) #  
Defined in Data.Functor.Const sizeOf :: Const a b > Int Source # alignment :: Const a b > Int Source # peekElemOff :: Ptr (Const a b) > Int > IO (Const a b) Source # pokeElemOff :: Ptr (Const a b) > Int > Const a b > IO () Source # peekByteOff :: Ptr b0 > Int > IO (Const a b) Source # pokeByteOff :: Ptr b0 > Int > Const a b > IO () Source #  
type Rep1 (Const a :: k > *) #  
Defined in Data.Functor.Const  
type Rep (Const a b) #  
Defined in Data.Functor.Const 
newtype WrappedMonad m a Source #
WrapMonad  

Instances
newtype WrappedArrow a b c Source #
WrapArrow  

Instances
Lists, but with an Applicative
functor based on zipping.
ZipList  

Instances
Functor ZipList #  
Applicative ZipList #  f '<$>' 'ZipList' xs1 '<*>' ... '<*>' 'ZipList' xsN = 'ZipList' (zipWithN f xs1 ... xsN) where (\a b c > stimes c [a, b]) <$> ZipList "abcd" <*> ZipList "567" <*> ZipList [1..] = ZipList (zipWith3 (\a b c > stimes c [a, b]) "abcd" "567" [1..]) = ZipList {getZipList = ["a5","b6b6","c7c7c7"]} Since: base2.1 
Foldable ZipList #  
Defined in Control.Applicative fold :: Monoid m => ZipList m > m Source # foldMap :: Monoid m => (a > m) > ZipList a > m Source # foldr :: (a > b > b) > b > ZipList a > b Source # foldr' :: (a > b > b) > b > ZipList a > b Source # foldl :: (b > a > b) > b > ZipList a > b Source # foldl' :: (b > a > b) > b > ZipList a > b Source # foldr1 :: (a > a > a) > ZipList a > a Source # foldl1 :: (a > a > a) > ZipList a > a Source # toList :: ZipList a > [a] Source # null :: ZipList a > Bool Source # length :: ZipList a > Int Source # elem :: Eq a => a > ZipList a > Bool Source # maximum :: Ord a => ZipList a > a Source # minimum :: Ord a => ZipList a > a Source #  
Traversable ZipList #  Since: base4.9.0.0 
Defined in Data.Traversable  
Alternative ZipList #  Since: base4.11.0.0 
Eq a => Eq (ZipList a) #  
Ord a => Ord (ZipList a) #  
Defined in Control.Applicative  
Read a => Read (ZipList a) #  
Show a => Show (ZipList a) #  
Generic (ZipList a) #  
Generic1 ZipList #  
type Rep (ZipList a) #  
Defined in Control.Applicative  
type Rep1 ZipList #  
Defined in Control.Applicative 
Utility functions
(<$>) :: Functor f => (a > b) > f a > f b infixl 4 Source #
An infix synonym for fmap
.
The name of this operator is an allusion to $
.
Note the similarities between their types:
($) :: (a > b) > a > b (<$>) :: Functor f => (a > b) > f a > f b
Whereas $
is function application, <$>
is function
application lifted over a Functor
.
Examples
Convert from a
to a Maybe
Int
using Maybe
String
show
:
>>>
show <$> Nothing
Nothing>>>
show <$> Just 3
Just "3"
Convert from an
to an Either
Int
Int
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)
(<**>) :: Applicative f => f a > f (a > b) > f b infixl 4 Source #
A variant of <*>
with the arguments reversed.
liftA :: Applicative f => (a > b) > f a > f b Source #
liftA3 :: Applicative f => (a > b > c > d) > f a > f b > f c > f d Source #
Lift a ternary function to actions.
optional :: Alternative f => f a > f (Maybe a) Source #
One or none.