indentation tweaks (whitespace only)
[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,
36     ) where
37
38 import Prelude hiding (id,(.))
39
40 import Control.Category
41 import Control.Arrow (Arrow(arr, (&&&)), ArrowZero(zeroArrow), ArrowPlus((<+>)))
42 import Control.Monad (liftM, ap, MonadPlus(..))
43 import Control.Monad.Instances ()
44 import Control.Monad.ST (ST)
45 import qualified Control.Monad.ST.Lazy as Lazy (ST)
46 import Data.Functor ((<$>), (<$))
47 import Data.Monoid (Monoid(..))
48
49 #ifdef __GLASGOW_HASKELL__
50 import GHC.Conc (STM, retry, orElse)
51 #endif
52
53 infixl 3 <|>
54 infixl 4 <*>, <*, *>, <**>
55
56 -- | A functor with application.
57 --
58 -- Instances should satisfy the following laws:
59 --
60 -- [/identity/]
61 --      @'pure' 'id' '<*>' v = v@
62 --
63 -- [/composition/]
64 --      @'pure' (.) '<*>' u '<*>' v '<*>' w = u '<*>' (v '<*>' w)@
65 --
66 -- [/homomorphism/]
67 --      @'pure' f '<*>' 'pure' x = 'pure' (f x)@
68 --
69 -- [/interchange/]
70 --      @u '<*>' 'pure' y = 'pure' ('$' y) '<*>' u@
71 --
72 -- [/ignore left value/]
73 --      @u '*>' v = 'pure' ('const' 'id') '<*>' u '<*>' v@
74 --
75 -- [/ignore right value/]
76 --      @u '<*' v = 'pure' 'const' '<*>' u '<*>' v@
77 --
78 -- The 'Functor' instance should satisfy
79 --
80 -- @
81 --      'fmap' f x = 'pure' f '<*>' x
82 -- @
83 --
84 -- If @f@ is also a 'Monad', define @'pure' = 'return'@ and @('<*>') = 'ap'@.
85 --
86 -- Minimal complete definition: 'pure' and '<*>'.
87
88 class Functor f => Applicative f where
89     -- | Lift a value.
90     pure :: a -> f a
91
92     -- | Sequential application.
93     (<*>) :: f (a -> b) -> f a -> f b
94
95     -- | Sequence actions, discarding the value of the first argument.
96     (*>) :: f a -> f b -> f b
97     (*>) = liftA2 (const id)
98
99     -- | Sequence actions, discarding the value of the second argument.
100     (<*) :: f a -> f b -> f a
101     (<*) = liftA2 const
102
103 -- | A monoid on applicative functors.
104 --
105 -- Minimal complete definition: 'empty' and '<|>'.
106 --
107 -- 'some' and 'many' should be the least solutions of the equations:
108 --
109 -- * @some v = (:) '<$>' v '<*>' many v@
110 --
111 -- * @many v = some v '<|>' 'pure' []@
112 class Applicative f => Alternative f where
113     -- | The identity of '<|>'
114     empty :: f a
115     -- | An associative binary operation
116     (<|>) :: f a -> f a -> f a
117
118     -- | One or more.
119     some :: f a -> f [a]
120     some v = some_v
121       where
122         many_v = some_v <|> pure []
123         some_v = (:) <$> v <*> many_v
124
125     -- | Zero or more.
126     many :: f a -> f [a]
127     many v = many_v
128       where
129         many_v = some_v <|> pure []
130         some_v = (:) <$> v <*> many_v
131
132 -- instances for Prelude types
133
134 instance Applicative Maybe where
135     pure = return
136     (<*>) = ap
137
138 instance Alternative Maybe where
139     empty = Nothing
140     Nothing <|> p = p
141     Just x <|> _ = Just x
142
143 instance Applicative [] where
144     pure = return
145     (<*>) = ap
146
147 instance Alternative [] where
148     empty = []
149     (<|>) = (++)
150
151 instance Applicative IO where
152     pure = return
153     (<*>) = ap
154
155 instance Applicative (ST s) where
156     pure = return
157     (<*>) = ap
158
159 instance Applicative (Lazy.ST s) where
160     pure = return
161     (<*>) = ap
162
163 #ifdef __GLASGOW_HASKELL__
164 instance Applicative STM where
165     pure = return
166     (<*>) = ap
167
168 instance Alternative STM where
169     empty = retry
170     (<|>) = orElse
171 #endif
172
173 instance Applicative ((->) a) where
174     pure = const
175     (<*>) f g x = f x (g x)
176
177 instance Monoid a => Applicative ((,) a) where
178     pure x = (mempty, x)
179     (u, f) <*> (v, x) = (u `mappend` v, f x)
180
181 instance Applicative (Either e) where
182     pure          = Right
183     Left  e <*> _ = Left e
184     Right f <*> r = fmap f r
185
186 -- new instances
187
188 newtype Const a b = Const { getConst :: a }
189
190 instance Functor (Const m) where
191     fmap _ (Const v) = Const v
192
193 instance Monoid m => Applicative (Const m) where
194     pure _ = Const mempty
195     Const f <*> Const v = Const (f `mappend` v)
196
197 newtype WrappedMonad m a = WrapMonad { unwrapMonad :: m a }
198
199 instance Monad m => Functor (WrappedMonad m) where
200     fmap f (WrapMonad v) = WrapMonad (liftM f v)
201
202 instance Monad m => Applicative (WrappedMonad m) where
203     pure = WrapMonad . return
204     WrapMonad f <*> WrapMonad v = WrapMonad (f `ap` v)
205
206 instance MonadPlus m => Alternative (WrappedMonad m) where
207     empty = WrapMonad mzero
208     WrapMonad u <|> WrapMonad v = WrapMonad (u `mplus` v)
209
210 newtype WrappedArrow a b c = WrapArrow { unwrapArrow :: a b c }
211
212 instance Arrow a => Functor (WrappedArrow a b) where
213     fmap f (WrapArrow a) = WrapArrow (a >>> arr f)
214
215 instance Arrow a => Applicative (WrappedArrow a b) where
216     pure x = WrapArrow (arr (const x))
217     WrapArrow f <*> WrapArrow v = WrapArrow (f &&& v >>> arr (uncurry id))
218
219 instance (ArrowZero a, ArrowPlus a) => Alternative (WrappedArrow a b) where
220     empty = WrapArrow zeroArrow
221     WrapArrow u <|> WrapArrow v = WrapArrow (u <+> v)
222
223 -- | Lists, but with an 'Applicative' functor based on zipping, so that
224 --
225 -- @f '<$>' 'ZipList' xs1 '<*>' ... '<*>' 'ZipList' xsn = 'ZipList' (zipWithn f xs1 ... xsn)@
226 --
227 newtype ZipList a = ZipList { getZipList :: [a] }
228
229 instance Functor ZipList where
230     fmap f (ZipList xs) = ZipList (map f xs)
231
232 instance Applicative ZipList where
233     pure x = ZipList (repeat x)
234     ZipList fs <*> ZipList xs = ZipList (zipWith id fs xs)
235
236 -- extra functions
237
238 -- | A variant of '<*>' with the arguments reversed.
239 (<**>) :: Applicative f => f a -> f (a -> b) -> f b
240 (<**>) = liftA2 (flip ($))
241
242 -- | Lift a function to actions.
243 -- This function may be used as a value for `fmap` in a `Functor` instance.
244 liftA :: Applicative f => (a -> b) -> f a -> f b
245 liftA f a = pure f <*> a
246
247 -- | Lift a binary function to actions.
248 liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
249 liftA2 f a b = f <$> a <*> b
250
251 -- | Lift a ternary function to actions.
252 liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
253 liftA3 f a b c = f <$> a <*> b <*> c
254
255 -- | One or none.
256 optional :: Alternative f => f a -> f (Maybe a)
257 optional v = Just <$> v <|> pure Nothing