| Copyright | Conor McBride and Ross Paterson 2005 |
|---|---|
| License | BSD-style (see the LICENSE file in the distribution) |
| Maintainer | libraries@haskell.org |
| Stability | experimental |
| Portability | portable |
| Safe Haskell | Trustworthy |
| Language | Haskell2010 |
Control.Applicative
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>>=, but
Traversable class.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.
classFunctor f =>Applicative fwhereSource#
A functor with application, providing operations to
A minimal complete definition must include implementations ofpure and of either<*> orliftA2. If it defines both, then they must behave the same as their default definitions:
(<*>) =liftA2id
liftA2f x y = f<$>x<*>y
Further, any definition must satisfy the following:
pureid<*>v = v
pure(.)<*>u<*>v<*>w = u<*>(v<*>w)
puref<*>purex =pure(f x)
u<*>purey =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, theFunctor instance forf 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
liftA2p (liftA2q u v) =liftA2f u .liftA2g v
Iff is also aMonad, it should satisfy
(which implies thatpure and<*> satisfy the applicative functor laws).
Methods
Lift a value.
(<*>) :: f (a -> b) -> f a -> f binfixl 4Source#
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 cSource#
Lift a binary function to actions.
Some functors support an implementation ofliftA2 that is more efficient than the default one. In particular, iffmap is an expensive operation, it is likely better to useliftA2 than tofmap over the structure and then use<*>.
(*>) :: f a -> f b -> f binfixl 4Source#
Sequence actions, discarding the value of the first argument.
(<*) :: f a -> f b -> f ainfixl 4Source#
Sequence actions, discarding the value of the second argument.
| Applicative []Source# | Since: 2.1 |
| ApplicativeMaybeSource# | Since: 2.1 |
| ApplicativeIOSource# | Since: 2.1 |
| ApplicativePar1Source# | Since: 4.9.0.0 |
| ApplicativeNonEmptySource# | Since: 4.9.0.0 |
| ApplicativeReadPSource# | Since: 4.6.0.0 |
| ApplicativeReadPrecSource# | Since: 4.6.0.0 |
Instance detailsDefined inText.ParserCombinators.ReadPrec | |
| ApplicativeDownSource# | Since: 4.11.0.0 |
| ApplicativeProductSource# | Since: 4.8.0.0 |
| ApplicativeSumSource# | Since: 4.8.0.0 |
| ApplicativeDualSource# | Since: 4.8.0.0 |
| ApplicativeLastSource# | Since: 4.8.0.0 |
| ApplicativeFirstSource# | Since: 4.8.0.0 |
| ApplicativeSTMSource# | Since: 4.8.0.0 |
| ApplicativeIdentitySource# | Since: 4.8.0.0 |
Instance detailsDefined inData.Functor.Identity | |
| ApplicativeZipListSource# | 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: 2.1 |
| ApplicativeOptionSource# | Since: 4.9.0.0 |
| ApplicativeLastSource# | Since: 4.9.0.0 |
| ApplicativeFirstSource# | Since: 4.9.0.0 |
| ApplicativeMaxSource# | Since: 4.9.0.0 |
| ApplicativeMinSource# | Since: 4.9.0.0 |
| ApplicativeComplexSource# | Since: 4.9.0.0 |
| Applicative (Either e)Source# | Since: 3.0 |
Instance detailsDefined inData.Either | |
| Applicative (U1 ::Type ->Type)Source# | Since: 4.9.0.0 |
| Monoid a =>Applicative ((,) a)Source# | For tuples, the ("hello ", (+15)) <*> ("world!", 2002)("hello world!",2017)Since: 2.1 |
| Applicative (ST s)Source# | Since: 4.4.0.0 |
| Applicative (Proxy ::Type ->Type)Source# | Since: 4.7.0.0 |
| Arrow a =>Applicative (ArrowMonad a)Source# | Since: 4.6.0.0 |
Instance detailsDefined inControl.Arrow Methods pure :: a0 ->ArrowMonad a a0Source# (<*>) ::ArrowMonad a (a0 -> b) ->ArrowMonad a a0 ->ArrowMonad a bSource# liftA2 :: (a0 -> b -> c) ->ArrowMonad a a0 ->ArrowMonad a b ->ArrowMonad a cSource# (*>) ::ArrowMonad a a0 ->ArrowMonad a b ->ArrowMonad a bSource# (<*) ::ArrowMonad a a0 ->ArrowMonad a b ->ArrowMonad a a0Source# | |
| Monad m =>Applicative (WrappedMonad m)Source# | Since: 2.1 |
Instance detailsDefined inControl.Applicative Methods pure :: a ->WrappedMonad m aSource# (<*>) ::WrappedMonad m (a -> b) ->WrappedMonad m a ->WrappedMonad m bSource# liftA2 :: (a -> b -> c) ->WrappedMonad m a ->WrappedMonad m b ->WrappedMonad m cSource# (*>) ::WrappedMonad m a ->WrappedMonad m b ->WrappedMonad m bSource# (<*) ::WrappedMonad m a ->WrappedMonad m b ->WrappedMonad m aSource# | |
| Applicative (ST s)Source# | Since: 2.1 |
| Applicative f =>Applicative (Rec1 f)Source# | Since: 4.9.0.0 |
| Applicative f =>Applicative (Alt f)Source# | Since: 4.8.0.0 |
| Applicative f =>Applicative (Ap f)Source# | Since: 4.12.0.0 |
| Monoid m =>Applicative (Const m ::Type ->Type)Source# | Since: 2.0.1 |
| Arrow a =>Applicative (WrappedArrow a b)Source# | Since: 2.1 |
Instance detailsDefined inControl.Applicative Methods pure :: a0 ->WrappedArrow a b a0Source# (<*>) ::WrappedArrow a b (a0 -> b0) ->WrappedArrow a b a0 ->WrappedArrow a b b0Source# liftA2 :: (a0 -> b0 -> c) ->WrappedArrow a b a0 ->WrappedArrow a b b0 ->WrappedArrow a b cSource# (*>) ::WrappedArrow a b a0 ->WrappedArrow a b b0 ->WrappedArrow a b b0Source# (<*) ::WrappedArrow a b a0 ->WrappedArrow a b b0 ->WrappedArrow a b a0Source# | |
| Applicative ((->) a ::Type ->Type)Source# | Since: 2.1 |
| Monoid c =>Applicative (K1 i c ::Type ->Type)Source# | Since: 4.12.0.0 |
| (Applicative f,Applicative g) =>Applicative (f:*: g)Source# | Since: 4.9.0.0 |
Instance detailsDefined inGHC.Generics | |
| (Applicative f,Applicative g) =>Applicative (Product f g)Source# | Since: 4.9.0.0 |
| Applicative f =>Applicative (M1 i c f)Source# | Since: 4.9.0.0 |
Instance detailsDefined inGHC.Generics | |
| (Applicative f,Applicative g) =>Applicative (f:.: g)Source# | Since: 4.9.0.0 |
Instance detailsDefined inGHC.Generics | |
| (Applicative f,Applicative g) =>Applicative (Compose f g)Source# | Since: 4.9.0.0 |
classApplicative f =>Alternative fwhereSource#
A monoid on applicative functors.
If defined,some andmany should be the least solutions of the equations:
Methods
The identity of<|>
(<|>) :: f a -> f a -> f ainfixl 3Source#
An associative binary operation
One or more.
Zero or more.
TheConst functor.
newtypeWrappedMonad m aSource#
Constructors
| WrapMonad | |
Fields
| |
newtypeWrappedArrow a b cSource#
Constructors
| WrapArrow | |
Fields
| |
Lists, but with anApplicative functor based on zipping.
Constructors
| ZipList | |
Fields
| |
| FunctorZipListSource# | Since: 2.1 |
| ApplicativeZipListSource# | 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: 2.1 |
| FoldableZipListSource# | Since: 4.9.0.0 |
Instance detailsDefined inControl.Applicative Methods fold ::Monoid m =>ZipList m -> mSource# foldMap ::Monoid m => (a -> m) ->ZipList a -> mSource# foldr :: (a -> b -> b) -> b ->ZipList a -> bSource# foldr' :: (a -> b -> b) -> b ->ZipList a -> bSource# foldl :: (b -> a -> b) -> b ->ZipList a -> bSource# foldl' :: (b -> a -> b) -> b ->ZipList a -> bSource# foldr1 :: (a -> a -> a) ->ZipList a -> aSource# foldl1 :: (a -> a -> a) ->ZipList a -> aSource# toList ::ZipList a -> [a]Source# null ::ZipList a ->BoolSource# length ::ZipList a ->IntSource# elem ::Eq a => a ->ZipList a ->BoolSource# maximum ::Ord a =>ZipList a -> aSource# minimum ::Ord a =>ZipList a -> aSource# | |
| TraversableZipListSource# | Since: 4.9.0.0 |
Instance detailsDefined inData.Traversable | |
| AlternativeZipListSource# | Since: 4.11.0.0 |
| Eq a =>Eq (ZipList a)Source# | Since: 4.7.0.0 |
| Ord a =>Ord (ZipList a)Source# | Since: 4.7.0.0 |
Instance detailsDefined inControl.Applicative | |
| Read a =>Read (ZipList a)Source# | Since: 4.7.0.0 |
| Show a =>Show (ZipList a)Source# | Since: 4.7.0.0 |
| Generic (ZipList a)Source# | |
| Generic1ZipListSource# | |
| typeRep (ZipList a)Source# | Since: 4.7.0.0 |
Instance detailsDefined inControl.Applicative | |
| typeRep1ZipListSource# | Since: 4.7.0.0 |
Instance detailsDefined inControl.Applicative | |
(<$>) ::Functor f => (a -> b) -> f a -> f binfixl 4Source#
An infix synonym forfmap.
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 aFunctor.
Convert from a to aMaybeInt usingMaybeStringshow:
>>>show <$> NothingNothing>>>show <$> Just 3Just "3"
Convert from an to anEitherIntIntEitherIntString usingshow:
>>>show <$> Left 17Left 17>>>show <$> Right 17Right "17"
Double each element of a list:
>>>(*2) <$> [1,2,3][2,4,6]
Applyeven to the second element of a pair:
>>>even <$> (2,2)(2,True)
(<**>) ::Applicative f => f a -> f (a -> b) -> f binfixl 4Source#
A variant of<*> with the arguments reversed.
liftA ::Applicative f => (a -> b) -> f a -> f bSource#
liftA3 ::Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f dSource#
Lift a ternary function to actions.
optional ::Alternative f => f a -> f (Maybe a)Source#
One or none.
Produced byHaddock version 2.20.0