1 -----------------------------------------------------------------------------
3 -- Module : Control.Applicative
4 -- Copyright : Conor McBride and Ross Paterson 2005
5 -- License : BSD-style (see the LICENSE file in the distribution)
7 -- Maintainer : ross@soi.city.ac.uk
8 -- Stability : experimental
9 -- Portability : portable
11 -- This module describes a structure intermediate between a functor and
12 -- a monad: it provides pure expressions and sequencing, but no binding.
13 -- (Technically, a strong lax monoidal functor.) For more details, see
14 -- /Applicative Programming with Effects/,
15 -- by Conor McBride and Ross Paterson, online at
16 -- <http://www.soi.city.ac.uk/~ross/papers/Applicative.html>.
18 -- This interface was introduced for parsers by Niklas Röjemo, because
19 -- it admits more sharing than the monadic interface. The names here are
20 -- mostly based on recent parsing work by Doaitse Swierstra.
22 -- This class is also useful with instances of the
23 -- 'Data.Traversable.Traversable' class.
25 module Control.Applicative (
26 -- * Applicative functors
29 WrappedMonad(..), Const(..), ZipList(..),
30 -- * Utility functions
31 (<$>), (<$), (*>), (<*), (<**>),
39 import Control.Monad (liftM, ap)
40 import Control.Monad.Instances ()
41 import Data.Monoid (Monoid(..))
44 infixl 4 <*>, <*, *>, <**>
46 -- | A functor with application.
48 -- Instances should satisfy the following laws:
51 -- @'pure' 'id' '<*>' v = v@
54 -- @'pure' (.) '<*>' u '<*>' v '<*>' w = u '<*>' (v '<*>' w)@
57 -- @'pure' f '<*>' 'pure' x = 'pure' (f x)@
60 -- @u '<*>' 'pure' y = 'pure' ('$' y) '<*>' u@
62 -- If @f@ is also a 'Monad', define @'pure' = 'return'@ and @('<*>') = 'ap'@.
64 class Functor f => Applicative f where
68 -- | Sequential application.
69 (<*>) :: f (a -> b) -> f a -> f b
71 -- instances for Prelude types
73 instance Applicative Maybe where
77 instance Applicative [] where
81 instance Applicative IO where
85 instance Applicative ((->) a) where
87 (<*>) f g x = f x (g x)
89 instance Monoid a => Applicative ((,) a) where
91 (u, f) <*> (v, x) = (u `mappend` v, f x)
95 newtype WrappedMonad m a = WrapMonad { unwrapMonad :: m a }
97 instance Monad m => Functor (WrappedMonad m) where
98 fmap f (WrapMonad v) = WrapMonad (liftM f v)
100 instance Monad m => Applicative (WrappedMonad m) where
101 pure = WrapMonad . return
102 WrapMonad f <*> WrapMonad v = WrapMonad (f `ap` v)
104 newtype Const a b = Const { getConst :: a }
106 instance Functor (Const m) where
107 fmap _ (Const v) = Const v
109 instance Monoid m => Applicative (Const m) where
110 pure _ = Const mempty
111 Const f <*> Const v = Const (f `mappend` v)
113 -- | Lists, but with an 'Applicative' functor based on zipping, so that
115 -- @f '<$>' 'ZipList' xs1 '<*>' ... '<*>' 'ZipList' xsn = 'ZipList' (zipWithn f xs1 ... xsn)@
117 newtype ZipList a = ZipList { getZipList :: [a] }
119 instance Functor ZipList where
120 fmap f (ZipList xs) = ZipList (map f xs)
122 instance Applicative ZipList where
123 pure x = ZipList (repeat x)
124 ZipList fs <*> ZipList xs = ZipList (zipWith id fs xs)
128 -- | A synonym for 'fmap'.
129 (<$>) :: Functor f => (a -> b) -> f a -> f b
132 -- | Replace the value.
133 (<$) :: Functor f => a -> f b -> f a
136 -- | Sequence actions, discarding the value of the first argument.
137 (*>) :: Applicative f => f a -> f b -> f b
138 (*>) = liftA2 (const id)
140 -- | Sequence actions, discarding the value of the second argument.
141 (<*) :: Applicative f => f a -> f b -> f a
144 -- | A variant of '<*>' with the arguments reversed.
145 (<**>) :: Applicative f => f a -> f (a -> b) -> f b
146 (<**>) = liftA2 (flip ($))
148 -- | Lift a function to actions.
149 -- This function may be used as a value for `fmap` in a `Functor` instance.
150 liftA :: Applicative f => (a -> b) -> f a -> f b
151 liftA f a = pure f <*> a
153 -- | Lift a binary function to actions.
154 liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
155 liftA2 f a b = f <$> a <*> b
157 -- | Lift a ternary function to actions.
158 liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
159 liftA3 f a b c = f <$> a <*> b <*> c