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