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