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