d2899162bc4179ff105e1e099341e5fee6c843f6
[ghc-base.git] / Control / Applicative.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  Control.Applicative
4 -- Copyright   :  Conor McBride and Ross Paterson 2005
5 -- License     :  BSD-style (see the LICENSE file in the distribution)
6 --
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  experimental
9 -- Portability :  portable
10 --
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>.
17 --
18 -- This interface was introduced for parsers by Niklas R&#xF6;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.
21 --
22 -- This class is also useful with instances of the
23 -- 'Data.Traversable.Traversable' class.
24
25 module Control.Applicative (
26         -- * Applicative functors
27         Applicative(..),
28         -- * Alternatives
29         Alternative(..),
30         -- * Instances
31         Const(..), WrappedMonad(..), WrappedArrow(..), ZipList(..),
32         -- * Utility functions
33         (<$>), (<$), (*>), (<*), (<**>),
34         liftA, liftA2, liftA3,
35         optional, some, many
36         ) where
37
38 import Prelude hiding (id,(.))
39 import qualified Prelude
40
41 import Control.Category
42 import Control.Arrow
43         (Arrow(arr, (&&&)), ArrowZero(zeroArrow), ArrowPlus((<+>)))
44 import Control.Monad (liftM, ap, MonadPlus(..))
45 import Control.Monad.Instances ()
46 import Data.Monoid (Monoid(..))
47
48 infixl 3 <|>
49 infixl 4 <$>, <$
50 infixl 4 <*>, <*, *>, <**>
51
52 -- | A functor with application.
53 --
54 -- Instances should satisfy the following laws:
55 --
56 -- [/identity/]
57 --      @'pure' 'id' '<*>' v = v@
58 --
59 -- [/composition/]
60 --      @'pure' (.) '<*>' u '<*>' v '<*>' w = u '<*>' (v '<*>' w)@
61 --
62 -- [/homomorphism/]
63 --      @'pure' f '<*>' 'pure' x = 'pure' (f x)@
64 --
65 -- [/interchange/]
66 --      @u '<*>' 'pure' y = 'pure' ('$' y) '<*>' u@
67 --
68 -- The 'Functor' instance should satisfy
69 --
70 -- @
71 --      'fmap' f x = 'pure' f '<*>' x
72 -- @
73 --
74 -- If @f@ is also a 'Monad', define @'pure' = 'return'@ and @('<*>') = 'ap'@.
75
76 class Functor f => Applicative f where
77         -- | Lift a value.
78         pure :: a -> f a
79
80         -- | Sequential application.
81         (<*>) :: f (a -> b) -> f a -> f b
82
83 -- | A monoid on applicative functors.
84 class Applicative f => Alternative f where
85         -- | The identity of '<|>'
86         empty :: f a
87         -- | An associative binary operation
88         (<|>) :: f a -> f a -> f a
89
90 -- instances for Prelude types
91
92 instance Applicative Maybe where
93         pure = return
94         (<*>) = ap
95
96 instance Alternative Maybe where
97         empty = Nothing
98         Nothing <|> p = p
99         Just x <|> _ = Just x
100
101 instance Applicative [] where
102         pure = return
103         (<*>) = ap
104
105 instance Alternative [] where
106         empty = []
107         (<|>) = (++)
108
109 instance Applicative IO where
110         pure = return
111         (<*>) = ap
112
113 instance Applicative ((->) a) where
114         pure = const
115         (<*>) f g x = f x (g x)
116
117 instance Monoid a => Applicative ((,) a) where
118         pure x = (mempty, x)
119         (u, f) <*> (v, x) = (u `mappend` v, f x)
120
121 -- new instances
122
123 newtype Const a b = Const { getConst :: a }
124
125 instance Functor (Const m) where
126         fmap _ (Const v) = Const v
127
128 instance Monoid m => Applicative (Const m) where
129         pure _ = Const mempty
130         Const f <*> Const v = Const (f `mappend` v)
131
132 newtype WrappedMonad m a = WrapMonad { unwrapMonad :: m a }
133
134 instance Monad m => Functor (WrappedMonad m) where
135         fmap f (WrapMonad v) = WrapMonad (liftM f v)
136
137 instance Monad m => Applicative (WrappedMonad m) where
138         pure = WrapMonad . return
139         WrapMonad f <*> WrapMonad v = WrapMonad (f `ap` v)
140
141 instance MonadPlus m => Alternative (WrappedMonad m) where
142         empty = WrapMonad mzero
143         WrapMonad u <|> WrapMonad v = WrapMonad (u `mplus` v)
144
145 newtype WrappedArrow a b c = WrapArrow { unwrapArrow :: a b c }
146
147 instance Arrow a => Functor (WrappedArrow a b) where
148         fmap f (WrapArrow a) = WrapArrow (a >>> arr f)
149
150 instance Arrow a => Applicative (WrappedArrow a b) where
151         pure x = WrapArrow (arr (const x))
152         WrapArrow f <*> WrapArrow v = WrapArrow (f &&& v >>> arr (uncurry id))
153
154 instance (ArrowZero a, ArrowPlus a) => Alternative (WrappedArrow a b) where
155         empty = WrapArrow zeroArrow
156         WrapArrow u <|> WrapArrow v = WrapArrow (u <+> v)
157
158 -- | Lists, but with an 'Applicative' functor based on zipping, so that
159 --
160 -- @f '<$>' 'ZipList' xs1 '<*>' ... '<*>' 'ZipList' xsn = 'ZipList' (zipWithn f xs1 ... xsn)@
161 --
162 newtype ZipList a = ZipList { getZipList :: [a] }
163
164 instance Functor ZipList where
165         fmap f (ZipList xs) = ZipList (map f xs)
166
167 instance Applicative ZipList where
168         pure x = ZipList (repeat x)
169         ZipList fs <*> ZipList xs = ZipList (zipWith id fs xs)
170
171 -- extra functions
172
173 -- | A synonym for 'fmap'.
174 (<$>) :: Functor f => (a -> b) -> f a -> f b
175 f <$> a = fmap f a
176
177 -- | Replace the value.
178 (<$) :: Functor f => a -> f b -> f a
179 (<$) = (<$>) . const
180  
181 -- | Sequence actions, discarding the value of the first argument.
182 (*>) :: Applicative f => f a -> f b -> f b
183 (*>) = liftA2 (const id)
184  
185 -- | Sequence actions, discarding the value of the second argument.
186 (<*) :: Applicative f => f a -> f b -> f a
187 (<*) = liftA2 const
188  
189 -- | A variant of '<*>' with the arguments reversed.
190 (<**>) :: Applicative f => f a -> f (a -> b) -> f b
191 (<**>) = liftA2 (flip ($))
192
193 -- | Lift a function to actions.
194 -- This function may be used as a value for `fmap` in a `Functor` instance.
195 liftA :: Applicative f => (a -> b) -> f a -> f b
196 liftA f a = pure f <*> a
197
198 -- | Lift a binary function to actions.
199 liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
200 liftA2 f a b = f <$> a <*> b
201
202 -- | Lift a ternary function to actions.
203 liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
204 liftA3 f a b c = f <$> a <*> b <*> c
205
206 -- | One or none.
207 optional :: Alternative f => f a -> f (Maybe a)
208 optional v = Just <$> v <|> pure Nothing
209
210 -- | One or more.
211 some :: Alternative f => f a -> f [a]
212 some v = some_v
213   where many_v = some_v <|> pure []
214         some_v = (:) <$> v <*> many_v
215
216 -- | Zero or more.
217 many :: Alternative f => f a -> f [a]
218 many v = many_v
219   where many_v = some_v <|> pure []
220         some_v = (:) <$> v <*> many_v