| 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
- it has more instances.
- it is sufficient for many uses, e.g. context-free parsing, or the
Traversableclass. - 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
- 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
A minimal complete definition must include implementations of these functions satisfying the following laws:
- identity
pureid<*>v = v- composition
pure(.)<*>u<*>v<*>w = u<*>(v<*>w)- homomorphism
puref<*>purex =pure(f x)- interchange
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, the Functor instance for f will satisfy
If f is also a Monad , it should satisfy
(which implies that pure and <*> satisfy the applicative functor laws).
Methods
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.
Instances
Methods
pure :: a -> ArrowMonad a a Source #
(<*>) :: ArrowMonad a (a -> b) -> ArrowMonad a a -> ArrowMonad a b Source #
(*>) :: ArrowMonad a a -> ArrowMonad a b -> ArrowMonad a b Source #
(<*) :: ArrowMonad a a -> ArrowMonad a b -> ArrowMonad a a Source #
Methods
pure :: a -> WrappedMonad m a Source #
(<*>) :: WrappedMonad m (a -> b) -> WrappedMonad m a -> WrappedMonad m b Source #
(*>) :: WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m b Source #
(<*) :: WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m a Source #
Methods
pure :: a -> WrappedArrow a b a Source #
(<*>) :: WrappedArrow a b (a -> b) -> WrappedArrow a b a -> WrappedArrow a b b Source #
(*>) :: WrappedArrow a b a -> WrappedArrow a b b -> WrappedArrow a b b Source #
(<*) :: WrappedArrow a b a -> WrappedArrow a b b -> WrappedArrow a b 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:
Methods
The identity of <|>
(<|>) :: f a -> f a -> f a infixl 3 Source #
An associative binary operation
One or more.
Zero or more.
Instances
Methods
empty :: ArrowMonad a a Source #
(<|>) :: ArrowMonad a a -> ArrowMonad a a -> ArrowMonad a a Source #
some :: ArrowMonad a a -> ArrowMonad a [a] Source #
many :: ArrowMonad a a -> ArrowMonad a [a] Source #
Methods
empty :: WrappedMonad m a Source #
(<|>) :: WrappedMonad m a -> WrappedMonad m a -> WrappedMonad m a Source #
some :: WrappedMonad m a -> WrappedMonad m [a] Source #
many :: WrappedMonad m a -> WrappedMonad m [a] Source #
Methods
empty :: WrappedArrow a b a Source #
(<|>) :: WrappedArrow a b a -> WrappedArrow a b a -> WrappedArrow a b a Source #
some :: WrappedArrow a b a -> WrappedArrow a b [a] Source #
many :: WrappedArrow a b a -> WrappedArrow a b [a] Source #
Instances
The Const functor.
Instances
Methods
fold :: Monoid m => Const * m m -> m Source #
foldMap :: Monoid m => (a -> m) -> Const * m a -> m 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 #
Methods
traverse :: Applicative f => (a -> f b) -> Const * m a -> f (Const * m b) Source #
sequenceA :: Applicative f => Const * m (f a) -> f (Const * m a) Source #
mapM :: Monad m => (a -> m b) -> Const * m a -> m (Const * m b) Source #
sequence :: Monad m => Const * m (m a) -> m (Const * m a) Source #
Methods
succ :: Const k a b -> Const k a b Source #
pred :: Const k a b -> Const k a b Source #
toEnum :: Int -> Const k a b Source #
fromEnum :: Const k a b -> Int Source #
enumFrom :: Const k a b -> [Const k a b] Source #
enumFromThen :: Const k a b -> Const k a b -> [Const k a b] Source #
enumFromTo :: Const k a b -> Const k a b -> [Const k a b] Source #
enumFromThenTo :: Const k a b -> Const k a b -> Const k a b -> [Const k a b] Source #
Methods
exp :: Const k a b -> Const k a b Source #
log :: Const k a b -> Const k a b Source #
sqrt :: Const k a b -> Const k a b Source #
(**) :: Const k a b -> Const k a b -> Const k a b Source #
logBase :: Const k a b -> Const k a b -> Const k a b Source #
sin :: Const k a b -> Const k a b Source #
cos :: Const k a b -> Const k a b Source #
tan :: Const k a b -> Const k a b Source #
asin :: Const k a b -> Const k a b Source #
acos :: Const k a b -> Const k a b Source #
atan :: Const k a b -> Const k a b Source #
sinh :: Const k a b -> Const k a b Source #
cosh :: Const k a b -> Const k a b Source #
tanh :: Const k a b -> Const k a b Source #
asinh :: Const k a b -> Const k a b Source #
acosh :: Const k a b -> Const k a b Source #
atanh :: Const k a b -> Const k a b Source #
log1p :: Const k a b -> Const k a b Source #
expm1 :: Const k a b -> Const k a b Source #
Methods
quot :: Const k a b -> Const k a b -> Const k a b Source #
rem :: Const k a b -> Const k a b -> Const k a b Source #
div :: Const k a b -> Const k a b -> Const k a b Source #
mod :: Const k a b -> Const k a b -> Const k a b Source #
quotRem :: Const k a b -> Const k a b -> (Const k a b, Const k a b) Source #
divMod :: Const k a b -> Const k a b -> (Const k a b, Const k a b) Source #
Methods
(+) :: Const k a b -> Const k a b -> Const k a b Source #
(-) :: Const k a b -> Const k a b -> Const k a b Source #
(*) :: Const k a b -> Const k a b -> Const k a b Source #
negate :: Const k a b -> Const k a b Source #
abs :: Const k a b -> Const k a b Source #
signum :: Const k a b -> Const k a b Source #
fromInteger :: Integer -> Const k a b Source #
This instance would be equivalent to the derived instances of the
Const newtype if the runConst field were removed
Methods
toRational :: Const k a b -> Rational Source #
Methods
floatRadix :: Const k a b -> Integer Source #
floatDigits :: Const k a b -> Int Source #
floatRange :: Const k a b -> (Int, Int) Source #
decodeFloat :: Const k a b -> (Integer, Int) Source #
encodeFloat :: Integer -> Int -> Const k a b Source #
exponent :: Const k a b -> Int Source #
significand :: Const k a b -> Const k a b Source #
scaleFloat :: Int -> Const k a b -> Const k a b Source #
isNaN :: Const k a b -> Bool Source #
isInfinite :: Const k a b -> Bool Source #
isDenormalized :: Const k a b -> Bool Source #
isNegativeZero :: Const k a b -> Bool Source #
This instance would be equivalent to the derived instances of the
Const newtype if the runConst field were removed
Methods
range :: (Const k a b, Const k a b) -> [Const k a b] Source #
index :: (Const k a b, Const k a b) -> Const k a b -> Int Source #
unsafeIndex :: (Const k a b, Const k a b) -> Const k a b -> Int
inRange :: (Const k a b, Const k a b) -> Const k a b -> Bool Source #
rangeSize :: (Const k a b, Const k a b) -> Int Source #
unsafeRangeSize :: (Const k a b, Const k a b) -> Int
Methods
finiteBitSize :: Const k a b -> Int Source #
countLeadingZeros :: Const k a b -> Int Source #
countTrailingZeros :: Const k a b -> Int Source #
Methods
(.&.) :: Const k a b -> Const k a b -> Const k a b Source #
(.|.) :: Const k a b -> Const k a b -> Const k a b Source #
xor :: Const k a b -> Const k a b -> Const k a b Source #
complement :: Const k a b -> Const k a b Source #
shift :: Const k a b -> Int -> Const k a b Source #
rotate :: Const k a b -> Int -> Const k a b Source #
zeroBits :: Const k a b Source #
bit :: Int -> Const k a b Source #
setBit :: Const k a b -> Int -> Const k a b Source #
clearBit :: Const k a b -> Int -> Const k a b Source #
complementBit :: Const k a b -> Int -> Const k a b Source #
testBit :: Const k a b -> Int -> Bool Source #
bitSizeMaybe :: Const k a b -> Maybe Int Source #
bitSize :: Const k a b -> Int Source #
isSigned :: Const k a b -> Bool Source #
shiftL :: Const k a b -> Int -> Const k a b Source #
unsafeShiftL :: Const k a b -> Int -> Const k a b Source #
shiftR :: Const k a b -> Int -> Const k a b Source #
unsafeShiftR :: Const k a b -> Int -> Const k a b Source #
rotateL :: Const k a b -> Int -> Const k a b Source #
Methods
sizeOf :: Const k a b -> Int Source #
alignment :: Const k a b -> Int Source #
peekElemOff :: Ptr (Const k a b) -> Int -> IO (Const k a b) Source #
pokeElemOff :: Ptr (Const k a b) -> Int -> Const k a b -> IO () Source #
peekByteOff :: Ptr b -> Int -> IO (Const k a b) Source #
pokeByteOff :: Ptr b -> Int -> Const k a b -> IO () Source #
newtype WrappedMonad m a Source #
Instances
Methods
(>>=) :: WrappedMonad m a -> (a -> WrappedMonad m b) -> WrappedMonad m b Source #
(>>) :: WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m b Source #
return :: a -> WrappedMonad m a Source #
fail :: String -> WrappedMonad m a Source #
Methods
fmap :: (a -> b) -> WrappedMonad m a -> WrappedMonad m b Source #
(<$) :: a -> WrappedMonad m b -> WrappedMonad m a Source #
Methods
pure :: a -> WrappedMonad m a Source #
(<*>) :: WrappedMonad m (a -> b) -> WrappedMonad m a -> WrappedMonad m b Source #
(*>) :: WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m b Source #
(<*) :: WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m a Source #
Methods
from1 :: WrappedMonad m a -> Rep1 (WrappedMonad m) a Source #
to1 :: Rep1 (WrappedMonad m) a -> WrappedMonad m a Source #
Methods
empty :: WrappedMonad m a Source #
(<|>) :: WrappedMonad m a -> WrappedMonad m a -> WrappedMonad m a Source #
some :: WrappedMonad m a -> WrappedMonad m [a] Source #
many :: WrappedMonad m a -> WrappedMonad m [a] Source #
Methods
from :: WrappedMonad m a -> Rep (WrappedMonad m a) x Source #
to :: Rep (WrappedMonad m a) x -> WrappedMonad m a Source #
newtype WrappedArrow a b c Source #
Instances
Methods
fmap :: (a -> b) -> WrappedArrow a b a -> WrappedArrow a b b Source #
(<$) :: a -> WrappedArrow a b b -> WrappedArrow a b a Source #
Methods
pure :: a -> WrappedArrow a b a Source #
(<*>) :: WrappedArrow a b (a -> b) -> WrappedArrow a b a -> WrappedArrow a b b Source #
(*>) :: WrappedArrow a b a -> WrappedArrow a b b -> WrappedArrow a b b Source #
(<*) :: WrappedArrow a b a -> WrappedArrow a b b -> WrappedArrow a b a Source #
Methods
from1 :: WrappedArrow a b a -> Rep1 (WrappedArrow a b) a Source #
to1 :: Rep1 (WrappedArrow a b) a -> WrappedArrow a b a Source #
Methods
empty :: WrappedArrow a b a Source #
(<|>) :: WrappedArrow a b a -> WrappedArrow a b a -> WrappedArrow a b a Source #
some :: WrappedArrow a b a -> WrappedArrow a b [a] Source #
many :: WrappedArrow a b a -> WrappedArrow a b [a] Source #
Methods
from :: WrappedArrow a b c -> Rep (WrappedArrow a b c) x Source #
to :: Rep (WrappedArrow a b c) x -> WrappedArrow a b c Source #
Lists, but with an Applicative functor based on zipping, so that
f<$>ZipListxs1<*>...<*>ZipListxsn =ZipList(zipWithn f xs1 ... xsn)
Instances
Methods
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 #
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 Stringshow:
>>>show <$> NothingNothing>>>show <$> Just 3Just "3"
Convert from an to an Either Int IntEither IntString using show:
>>>show <$> Left 17Left 17>>>show <$> Right 17Right "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 #
liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c Source #
Lift a binary function to actions.
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.