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 |
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. context-free 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 #
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 #
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 #
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 #
Sequence actions, discarding the value of the first argument.
(<*) :: f a -> f b -> f a infixl 4 #
Sequence actions, discarding the value of the second argument.
Instances
Applicative [] # | Since: base-2.1 |
Applicative Maybe # | Since: base-2.1 |
Applicative IO # | Since: base-2.1 |
Applicative Par1 # | Since: base-4.9.0.0 |
Applicative NonEmpty # | Since: base-4.9.0.0 |
Applicative ReadP # | Since: base-4.6.0.0 |
Applicative ReadPrec # | Since: base-4.6.0.0 |
Applicative Down # | Since: base-4.11.0.0 |
Applicative Product # | Since: base-4.8.0.0 |
Applicative Sum # | Since: base-4.8.0.0 |
Applicative Dual # | Since: base-4.8.0.0 |
Applicative Last # | Since: base-4.8.0.0 |
Applicative First # | Since: base-4.8.0.0 |
Applicative STM # | Since: base-4.8.0.0 |
Applicative Identity # | Since: base-4.8.0.0 |
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: base-2.1 |
Applicative Option # | Since: base-4.9.0.0 |
Applicative Last # | Since: base-4.9.0.0 |
Applicative First # | Since: base-4.9.0.0 |
Applicative Max # | Since: base-4.9.0.0 |
Applicative Min # | Since: base-4.9.0.0 |
Applicative Complex # | Since: base-4.9.0.0 |
Applicative (Either e) # | Since: base-3.0 |
Applicative (U1 :: Type -> Type) # | Since: base-4.9.0.0 |
Monoid a => Applicative ((,) a) # | For tuples, the ("hello ", (+15)) <*> ("world!", 2002) ("hello world!",2017) Since: base-2.1 |
Applicative (ST s) # | Since: base-4.4.0.0 |
Applicative (Proxy :: Type -> Type) # | Since: base-4.7.0.0 |
Arrow a => Applicative (ArrowMonad a) # | Since: base-4.6.0.0 |
Defined in Control.Arrow pure :: a0 -> ArrowMonad a a0 # (<*>) :: ArrowMonad a (a0 -> b) -> ArrowMonad a a0 -> ArrowMonad a b # liftA2 :: (a0 -> b -> c) -> ArrowMonad a a0 -> ArrowMonad a b -> ArrowMonad a c # (*>) :: ArrowMonad a a0 -> ArrowMonad a b -> ArrowMonad a b # (<*) :: ArrowMonad a a0 -> ArrowMonad a b -> ArrowMonad a a0 # | |
Monad m => Applicative (WrappedMonad m) # | Since: base-2.1 |
Defined in Control.Applicative pure :: a -> WrappedMonad m a # (<*>) :: WrappedMonad m (a -> b) -> WrappedMonad m a -> WrappedMonad m b # liftA2 :: (a -> b -> c) -> WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m c # (*>) :: WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m b # (<*) :: WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m a # | |
Applicative (ST s) # | Since: base-2.1 |
Applicative f => Applicative (Rec1 f) # | Since: base-4.9.0.0 |
Applicative f => Applicative (Alt f) # | Since: base-4.8.0.0 |
Applicative f => Applicative (Ap f) # | Since: base-4.12.0.0 |
Monoid m => Applicative (Const m :: Type -> Type) # | Since: base-2.0.1 |
Arrow a => Applicative (WrappedArrow a b) # | Since: base-2.1 |
Defined in Control.Applicative pure :: a0 -> WrappedArrow a b a0 # (<*>) :: WrappedArrow a b (a0 -> b0) -> WrappedArrow a b a0 -> WrappedArrow a b b0 # liftA2 :: (a0 -> b0 -> c) -> WrappedArrow a b a0 -> WrappedArrow a b b0 -> WrappedArrow a b c # (*>) :: WrappedArrow a b a0 -> WrappedArrow a b b0 -> WrappedArrow a b b0 # (<*) :: WrappedArrow a b a0 -> WrappedArrow a b b0 -> WrappedArrow a b a0 # | |
Applicative ((->) a :: Type -> Type) # | Since: base-2.1 |
Monoid c => Applicative (K1 i c :: Type -> Type) # | Since: base-4.12.0.0 |
(Applicative f, Applicative g) => Applicative (f :*: g) # | Since: base-4.9.0.0 |
(Applicative f, Applicative g) => Applicative (Product f g) # | Since: base-4.9.0.0 |
Defined in Data.Functor.Product | |
Applicative f => Applicative (M1 i c f) # | Since: base-4.9.0.0 |
(Applicative f, Applicative g) => Applicative (f :.: g) # | Since: base-4.9.0.0 |
(Applicative f, Applicative g) => Applicative (Compose f g) # | Since: base-4.9.0.0 |
Defined in Data.Functor.Compose |
Alternatives
class Applicative f => Alternative f where #
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 #
An associative binary operation
One or more.
Zero or more.
Instances
Instances
The Const
functor.
Instances
Generic1 (Const a :: k -> Type) # | |
Show2 (Const :: Type -> Type -> Type) # | Since: base-4.9.0.0 |
Read2 (Const :: Type -> Type -> Type) # | Since: base-4.9.0.0 |
Defined in Data.Functor.Classes liftReadsPrec2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> Int -> ReadS (Const a b) # liftReadList2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> ReadS [Const a b] # liftReadPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec (Const a b) # liftReadListPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec [Const a b] # | |
Ord2 (Const :: Type -> Type -> Type) # | Since: base-4.9.0.0 |
Defined in Data.Functor.Classes | |
Eq2 (Const :: Type -> Type -> Type) # | Since: base-4.9.0.0 |
Bifunctor (Const :: Type -> Type -> Type) # | Since: base-4.8.0.0 |
Bifoldable (Const :: Type -> Type -> Type) # | Since: base-4.10.0.0 |
Bitraversable (Const :: Type -> Type -> Type) # | Since: base-4.10.0.0 |
Defined in Data.Bitraversable bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> Const a b -> f (Const c d) # | |
Functor (Const m :: Type -> Type) # | Since: base-2.1 |
Monoid m => Applicative (Const m :: Type -> Type) # | Since: base-2.0.1 |
Foldable (Const m :: Type -> Type) # | Since: base-4.7.0.0 |
Defined in Data.Functor.Const fold :: Monoid m0 => Const m m0 -> m0 # foldMap :: Monoid m0 => (a -> m0) -> Const m a -> m0 # foldr :: (a -> b -> b) -> b -> Const m a -> b # foldr' :: (a -> b -> b) -> b -> Const m a -> b # foldl :: (b -> a -> b) -> b -> Const m a -> b # foldl' :: (b -> a -> b) -> b -> Const m a -> b # foldr1 :: (a -> a -> a) -> Const m a -> a # foldl1 :: (a -> a -> a) -> Const m a -> a # elem :: Eq a => a -> Const m a -> Bool # maximum :: Ord a => Const m a -> a # minimum :: Ord a => Const m a -> a # | |
Traversable (Const m :: Type -> Type) # | Since: base-4.7.0.0 |
Show a => Show1 (Const a :: Type -> Type) # | Since: base-4.9.0.0 |
Read a => Read1 (Const a :: Type -> Type) # | Since: base-4.9.0.0 |
Defined in Data.Functor.Classes | |
Ord a => Ord1 (Const a :: Type -> Type) # | Since: base-4.9.0.0 |
Defined in Data.Functor.Classes | |
Eq a => Eq1 (Const a :: Type -> Type) # | Since: base-4.9.0.0 |
Contravariant (Const a :: Type -> Type) # | |
Bounded a => Bounded (Const a b) # | Since: base-4.9.0.0 |
Enum a => Enum (Const a b) # | Since: base-4.9.0.0 |
Defined in Data.Functor.Const succ :: Const a b -> Const a b # pred :: Const a b -> Const a b # fromEnum :: Const a b -> Int # enumFrom :: Const a b -> [Const a b] # enumFromThen :: Const a b -> Const a b -> [Const a b] # enumFromTo :: Const a b -> Const a b -> [Const a b] # enumFromThenTo :: Const a b -> Const a b -> Const a b -> [Const a b] # | |
Eq a => Eq (Const a b) # | Since: base-4.9.0.0 |
Floating a => Floating (Const a b) # | Since: base-4.9.0.0 |
Defined in Data.Functor.Const exp :: Const a b -> Const a b # log :: Const a b -> Const a b # sqrt :: Const a b -> Const a b # (**) :: Const a b -> Const a b -> Const a b # logBase :: Const a b -> Const a b -> Const a b # sin :: Const a b -> Const a b # cos :: Const a b -> Const a b # tan :: Const a b -> Const a b # asin :: Const a b -> Const a b # acos :: Const a b -> Const a b # atan :: Const a b -> Const a b # sinh :: Const a b -> Const a b # cosh :: Const a b -> Const a b # tanh :: Const a b -> Const a b # asinh :: Const a b -> Const a b # acosh :: Const a b -> Const a b # atanh :: Const a b -> Const a b # log1p :: Const a b -> Const a b # expm1 :: Const a b -> Const a b # | |
Fractional a => Fractional (Const a b) # | Since: base-4.9.0.0 |
Integral a => Integral (Const a b) # | Since: base-4.9.0.0 |
Defined in Data.Functor.Const | |
(Typeable k, Data a, Typeable b) => Data (Const a b) # | Since: base-4.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) # gunfold :: (forall b0 r. Data b0 => c (b0 -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Const a b) # toConstr :: Const a b -> Constr # dataTypeOf :: Const a b -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Const a b)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Const a b)) # gmapT :: (forall b0. Data b0 => b0 -> b0) -> Const a b -> Const a b # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Const a b -> r # gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Const a b -> r # gmapQ :: (forall d. Data d => d -> u) -> Const a b -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Const a b -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Const a b -> m (Const a b) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Const a b -> m (Const a b) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Const a b -> m (Const a b) # | |
Num a => Num (Const a b) # | Since: base-4.9.0.0 |
Ord a => Ord (Const a b) # | Since: base-4.9.0.0 |
Defined in Data.Functor.Const | |
Read a => Read (Const a b) # | This instance would be equivalent to the derived instances of the
Since: base-4.8.0.0 |
Real a => Real (Const a b) # | Since: base-4.9.0.0 |
Defined in Data.Functor.Const toRational :: Const a b -> Rational # | |
RealFloat a => RealFloat (Const a b) # | Since: base-4.9.0.0 |
Defined in Data.Functor.Const floatRadix :: Const a b -> Integer # floatDigits :: Const a b -> Int # floatRange :: Const a b -> (Int, Int) # decodeFloat :: Const a b -> (Integer, Int) # encodeFloat :: Integer -> Int -> Const a b # exponent :: Const a b -> Int # significand :: Const a b -> Const a b # scaleFloat :: Int -> Const a b -> Const a b # isInfinite :: Const a b -> Bool # isDenormalized :: Const a b -> Bool # isNegativeZero :: Const a b -> Bool # | |
RealFrac a => RealFrac (Const a b) # | Since: base-4.9.0.0 |
Show a => Show (Const a b) # | This instance would be equivalent to the derived instances of the
Since: base-4.8.0.0 |
Ix a => Ix (Const a b) # | Since: base-4.9.0.0 |
Defined in Data.Functor.Const range :: (Const a b, Const a b) -> [Const a b] # index :: (Const a b, Const a b) -> Const a b -> Int # unsafeIndex :: (Const a b, Const a b) -> Const a b -> Int inRange :: (Const a b, Const a b) -> Const a b -> Bool # rangeSize :: (Const a b, Const a b) -> Int # unsafeRangeSize :: (Const a b, Const a b) -> Int | |
IsString a => IsString (Const a b) # | Since: base-4.9.0.0 |
Defined in Data.String fromString :: String -> Const a b # | |
Generic (Const a b) # | |
Semigroup a => Semigroup (Const a b) # | Since: base-4.9.0.0 |
Monoid a => Monoid (Const a b) # | Since: base-4.9.0.0 |
FiniteBits a => FiniteBits (Const a b) # | Since: base-4.9.0.0 |
Defined in Data.Functor.Const finiteBitSize :: Const a b -> Int # countLeadingZeros :: Const a b -> Int # countTrailingZeros :: Const a b -> Int # | |
Bits a => Bits (Const a b) # | Since: base-4.9.0.0 |
Defined in Data.Functor.Const (.&.) :: Const a b -> Const a b -> Const a b # (.|.) :: Const a b -> Const a b -> Const a b # xor :: Const a b -> Const a b -> Const a b # complement :: Const a b -> Const a b # shift :: Const a b -> Int -> Const a b # rotate :: Const a b -> Int -> Const a b # setBit :: Const a b -> Int -> Const a b # clearBit :: Const a b -> Int -> Const a b # complementBit :: Const a b -> Int -> Const a b # testBit :: Const a b -> Int -> Bool # bitSizeMaybe :: Const a b -> Maybe Int # isSigned :: Const a b -> Bool # shiftL :: Const a b -> Int -> Const a b # unsafeShiftL :: Const a b -> Int -> Const a b # shiftR :: Const a b -> Int -> Const a b # unsafeShiftR :: Const a b -> Int -> Const a b # rotateL :: Const a b -> Int -> Const a b # | |
Storable a => Storable (Const a b) # | Since: base-4.9.0.0 |
Defined in Data.Functor.Const | |
type Rep1 (Const a :: k -> Type) # | Since: base-4.9.0.0 |
Defined in Data.Functor.Const | |
type Rep (Const a b) # | Since: base-4.9.0.0 |
Defined in Data.Functor.Const |
newtype WrappedMonad m a #
WrapMonad | |
|
Instances
newtype WrappedArrow a b c #
WrapArrow | |
|
Instances
Lists, but with an Applicative
functor based on zipping.
ZipList | |
|
Instances
Functor ZipList # | Since: base-2.1 |
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: base-2.1 |
Foldable ZipList # | Since: base-4.9.0.0 |
Defined in Control.Applicative fold :: Monoid m => ZipList m -> m # foldMap :: Monoid m => (a -> m) -> ZipList a -> m # foldr :: (a -> b -> b) -> b -> ZipList a -> b # foldr' :: (a -> b -> b) -> b -> ZipList a -> b # foldl :: (b -> a -> b) -> b -> ZipList a -> b # foldl' :: (b -> a -> b) -> b -> ZipList a -> b # foldr1 :: (a -> a -> a) -> ZipList a -> a # foldl1 :: (a -> a -> a) -> ZipList a -> a # elem :: Eq a => a -> ZipList a -> Bool # maximum :: Ord a => ZipList a -> a # minimum :: Ord a => ZipList a -> a # | |
Traversable ZipList # | Since: base-4.9.0.0 |
Alternative ZipList # | Since: base-4.11.0.0 |
Eq a => Eq (ZipList a) # | Since: base-4.7.0.0 |
Ord a => Ord (ZipList a) # | Since: base-4.7.0.0 |
Defined in Control.Applicative | |
Read a => Read (ZipList a) # | Since: base-4.7.0.0 |
Show a => Show (ZipList a) # | Since: base-4.7.0.0 |
Generic (ZipList a) # | |
Generic1 ZipList # | |
type Rep (ZipList a) # | Since: base-4.7.0.0 |
Defined in Control.Applicative | |
type Rep1 ZipList # | Since: base-4.7.0.0 |
Defined in Control.Applicative |
Utility functions
(<$>) :: Functor f => (a -> b) -> f a -> f b infixl 4 #
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 #
A variant of <*>
with the arguments reversed.
liftA :: Applicative f => (a -> b) -> f a -> f b #
liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d #
Lift a ternary function to actions.
optional :: Alternative f => f a -> f (Maybe a) #
One or none.