3 -----------------------------------------------------------------------------
5 -- Module : Control.Applicative
6 -- Copyright : Conor McBride and Ross Paterson 2005
7 -- License : BSD-style (see the LICENSE file in the distribution)
9 -- Maintainer : libraries@haskell.org
10 -- Stability : experimental
11 -- Portability : portable
13 -- This module describes a structure intermediate between a functor and
14 -- a monad: it provides pure expressions and sequencing, but no binding.
15 -- (Technically, a strong lax monoidal functor.) For more details, see
16 -- /Applicative Programming with Effects/,
17 -- by Conor McBride and Ross Paterson, online at
18 -- <http://www.soi.city.ac.uk/~ross/papers/Applicative.html>.
20 -- This interface was introduced for parsers by Niklas Röjemo, because
21 -- it admits more sharing than the monadic interface. The names here are
22 -- mostly based on recent parsing work by Doaitse Swierstra.
24 -- This class is also useful with instances of the
25 -- 'Data.Traversable.Traversable' class.
27 module Control.Applicative (
28 -- * Applicative functors
33 Const(..), WrappedMonad(..), WrappedArrow(..), ZipList(..),
34 -- * Utility functions
36 liftA, liftA2, liftA3,
40 import Prelude hiding (id,(.))
42 import Control.Category
43 import Control.Arrow (Arrow(arr, (&&&)), ArrowZero(zeroArrow), ArrowPlus((<+>)))
44 import Control.Monad (liftM, ap, MonadPlus(..))
45 import Control.Monad.Instances ()
47 import Control.Monad.ST (ST)
48 import qualified Control.Monad.ST.Lazy as Lazy (ST)
50 import Data.Functor ((<$>), (<$))
51 import Data.Monoid (Monoid(..))
53 #ifdef __GLASGOW_HASKELL__
54 import GHC.Conc (STM, retry, orElse)
58 infixl 4 <*>, <*, *>, <**>
60 -- | A functor with application.
62 -- Instances should satisfy the following laws:
65 -- @'pure' 'id' '<*>' v = v@
68 -- @'pure' (.) '<*>' u '<*>' v '<*>' w = u '<*>' (v '<*>' w)@
71 -- @'pure' f '<*>' 'pure' x = 'pure' (f x)@
74 -- @u '<*>' 'pure' y = 'pure' ('$' y) '<*>' u@
76 -- [/ignore left value/]
77 -- @u '*>' v = 'pure' ('const' 'id') '<*>' u '<*>' v@
79 -- [/ignore right value/]
80 -- @u '<*' v = 'pure' 'const' '<*>' u '<*>' v@
82 -- The 'Functor' instance should satisfy
85 -- 'fmap' f x = 'pure' f '<*>' x
88 -- If @f@ is also a 'Monad', define @'pure' = 'return'@ and @('<*>') = 'ap'@.
90 -- Minimal complete definition: 'pure' and '<*>'.
92 class Functor f => Applicative f where
96 -- | Sequential application.
97 (<*>) :: f (a -> b) -> f a -> f b
99 -- | Sequence actions, discarding the value of the first argument.
100 (*>) :: f a -> f b -> f b
101 (*>) = liftA2 (const id)
103 -- | Sequence actions, discarding the value of the second argument.
104 (<*) :: f a -> f b -> f a
107 -- | A monoid on applicative functors.
109 -- Minimal complete definition: 'empty' and '<|>'.
111 -- 'some' and 'many' should be the least solutions of the equations:
113 -- * @some v = (:) '<$>' v '<*>' many v@
115 -- * @many v = some v '<|>' 'pure' []@
116 class Applicative f => Alternative f where
117 -- | The identity of '<|>'
119 -- | An associative binary operation
120 (<|>) :: f a -> f a -> f a
126 many_v = some_v <|> pure []
127 some_v = (:) <$> v <*> many_v
133 many_v = some_v <|> pure []
134 some_v = (:) <$> v <*> many_v
136 -- instances for Prelude types
138 instance Applicative Maybe where
142 instance Alternative Maybe where
145 Just x <|> _ = Just x
147 instance Applicative [] where
151 instance Alternative [] where
155 instance Applicative IO where
160 instance Applicative (ST s) where
164 instance Applicative (Lazy.ST s) where
169 #ifdef __GLASGOW_HASKELL__
170 instance Applicative STM where
174 instance Alternative STM where
179 instance Applicative ((->) a) where
181 (<*>) f g x = f x (g x)
183 instance Monoid a => Applicative ((,) a) where
185 (u, f) <*> (v, x) = (u `mappend` v, f x)
187 instance Applicative (Either e) where
189 Left e <*> _ = Left e
190 Right f <*> r = fmap f r
194 newtype Const a b = Const { getConst :: a }
196 instance Functor (Const m) where
197 fmap _ (Const v) = Const v
199 instance Monoid m => Applicative (Const m) where
200 pure _ = Const mempty
201 Const f <*> Const v = Const (f `mappend` v)
203 newtype WrappedMonad m a = WrapMonad { unwrapMonad :: m a }
205 instance Monad m => Functor (WrappedMonad m) where
206 fmap f (WrapMonad v) = WrapMonad (liftM f v)
208 instance Monad m => Applicative (WrappedMonad m) where
209 pure = WrapMonad . return
210 WrapMonad f <*> WrapMonad v = WrapMonad (f `ap` v)
212 instance MonadPlus m => Alternative (WrappedMonad m) where
213 empty = WrapMonad mzero
214 WrapMonad u <|> WrapMonad v = WrapMonad (u `mplus` v)
216 newtype WrappedArrow a b c = WrapArrow { unwrapArrow :: a b c }
218 instance Arrow a => Functor (WrappedArrow a b) where
219 fmap f (WrapArrow a) = WrapArrow (a >>> arr f)
221 instance Arrow a => Applicative (WrappedArrow a b) where
222 pure x = WrapArrow (arr (const x))
223 WrapArrow f <*> WrapArrow v = WrapArrow (f &&& v >>> arr (uncurry id))
225 instance (ArrowZero a, ArrowPlus a) => Alternative (WrappedArrow a b) where
226 empty = WrapArrow zeroArrow
227 WrapArrow u <|> WrapArrow v = WrapArrow (u <+> v)
229 -- | Lists, but with an 'Applicative' functor based on zipping, so that
231 -- @f '<$>' 'ZipList' xs1 '<*>' ... '<*>' 'ZipList' xsn = 'ZipList' (zipWithn f xs1 ... xsn)@
233 newtype ZipList a = ZipList { getZipList :: [a] }
235 instance Functor ZipList where
236 fmap f (ZipList xs) = ZipList (map f xs)
238 instance Applicative ZipList where
239 pure x = ZipList (repeat x)
240 ZipList fs <*> ZipList xs = ZipList (zipWith id fs xs)
244 -- | A variant of '<*>' with the arguments reversed.
245 (<**>) :: Applicative f => f a -> f (a -> b) -> f b
246 (<**>) = liftA2 (flip ($))
248 -- | Lift a function to actions.
249 -- This function may be used as a value for `fmap` in a `Functor` instance.
250 liftA :: Applicative f => (a -> b) -> f a -> f b
251 liftA f a = pure f <*> a
253 -- | Lift a binary function to actions.
254 liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
255 liftA2 f a b = f <$> a <*> b
257 -- | Lift a ternary function to actions.
258 liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
259 liftA3 f a b c = f <$> a <*> b <*> c
262 optional :: Alternative f => f a -> f (Maybe a)
263 optional v = Just <$> v <|> pure Nothing