SafeHaskell: Added SafeHaskell to base
[ghc.git] / libraries / base / Control / Applicative.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE CPP #-}
3
4 -----------------------------------------------------------------------------
5 -- |
6 -- Module : Control.Applicative
7 -- Copyright : Conor McBride and Ross Paterson 2005
8 -- License : BSD-style (see the LICENSE file in the distribution)
9 --
10 -- Maintainer : libraries@haskell.org
11 -- Stability : experimental
12 -- Portability : portable
13 --
14 -- This module describes a structure intermediate between a functor and
15 -- a monad (technically, a strong lax monoidal functor). Compared with
16 -- monads, this interface lacks the full power of the binding operation
17 -- '>>=', but
18 --
19 -- * it has more instances.
20 --
21 -- * it is sufficient for many uses, e.g. context-free parsing, or the
22 -- 'Data.Traversable.Traversable' class.
23 --
24 -- * instances can perform analysis of computations before they are
25 -- executed, and thus produce shared optimizations.
26 --
27 -- This interface was introduced for parsers by Niklas Röjemo, because
28 -- it admits more sharing than the monadic interface. The names here are
29 -- mostly based on parsing work by Doaitse Swierstra.
30 --
31 -- For more details, see /Applicative Programming with Effects/,
32 -- by Conor McBride and Ross Paterson, online at
33 -- <http://www.soi.city.ac.uk/~ross/papers/Applicative.html>.
34
35 module Control.Applicative (
36 -- * Applicative functors
37 Applicative(..),
38 -- * Alternatives
39 Alternative(..),
40 -- * Instances
41 Const(..), WrappedMonad(..), WrappedArrow(..), ZipList(..),
42 -- * Utility functions
43 (<$>), (<$), (<**>),
44 liftA, liftA2, liftA3,
45 optional,
46 ) where
47
48 import Prelude hiding (id,(.))
49
50 import Control.Category
51 import Control.Arrow (Arrow(arr, (&&&)), ArrowZero(zeroArrow), ArrowPlus((<+>)))
52 import Control.Monad (liftM, ap, MonadPlus(..))
53 import Control.Monad.Instances ()
54 #ifndef __NHC__
55 import Control.Monad.ST.Safe (ST)
56 import qualified Control.Monad.ST.Lazy.Safe as Lazy (ST)
57 #endif
58 import Data.Functor ((<$>), (<$))
59 import Data.Monoid (Monoid(..))
60
61 #ifdef __GLASGOW_HASKELL__
62 import GHC.Conc (STM, retry, orElse)
63 #endif
64
65 infixl 3 <|>
66 infixl 4 <*>, <*, *>, <**>
67
68 -- | A functor with application, providing operations to
69 --
70 -- * embed pure expressions ('pure'), and
71 --
72 -- * sequence computations and combine their results ('<*>').
73 --
74 -- A minimal complete definition must include implementations of these
75 -- functions satisfying the following laws:
76 --
77 -- [/identity/]
78 -- @'pure' 'id' '<*>' v = v@
79 --
80 -- [/composition/]
81 -- @'pure' (.) '<*>' u '<*>' v '<*>' w = u '<*>' (v '<*>' w)@
82 --
83 -- [/homomorphism/]
84 -- @'pure' f '<*>' 'pure' x = 'pure' (f x)@
85 --
86 -- [/interchange/]
87 -- @u '<*>' 'pure' y = 'pure' ('$' y) '<*>' u@
88 --
89 -- The other methods have the following default definitions, which may
90 -- be overridden with equivalent specialized implementations:
91 --
92 -- @
93 -- u '*>' v = 'pure' ('const' 'id') '<*>' u '<*>' v
94 -- u '<*' v = 'pure' 'const' '<*>' u '<*>' v
95 -- @
96 --
97 -- As a consequence of these laws, the 'Functor' instance for @f@ will satisfy
98 --
99 -- @
100 -- 'fmap' f x = 'pure' f '<*>' x
101 -- @
102 --
103 -- If @f@ is also a 'Monad', it should satisfy @'pure' = 'return'@ and
104 -- @('<*>') = 'ap'@ (which implies that 'pure' and '<*>' satisfy the
105 -- applicative functor laws).
106
107 class Functor f => Applicative f where
108 -- | Lift a value.
109 pure :: a -> f a
110
111 -- | Sequential application.
112 (<*>) :: f (a -> b) -> f a -> f b
113
114 -- | Sequence actions, discarding the value of the first argument.
115 (*>) :: f a -> f b -> f b
116 (*>) = liftA2 (const id)
117
118 -- | Sequence actions, discarding the value of the second argument.
119 (<*) :: f a -> f b -> f a
120 (<*) = liftA2 const
121
122 -- | A monoid on applicative functors.
123 --
124 -- Minimal complete definition: 'empty' and '<|>'.
125 --
126 -- If defined, 'some' and 'many' should be the least solutions
127 -- of the equations:
128 --
129 -- * @some v = (:) '<$>' v '<*>' many v@
130 --
131 -- * @many v = some v '<|>' 'pure' []@
132 class Applicative f => Alternative f where
133 -- | The identity of '<|>'
134 empty :: f a
135 -- | An associative binary operation
136 (<|>) :: f a -> f a -> f a
137
138 -- | One or more.
139 some :: f a -> f [a]
140 some v = some_v
141 where
142 many_v = some_v <|> pure []
143 some_v = (:) <$> v <*> many_v
144
145 -- | Zero or more.
146 many :: f a -> f [a]
147 many v = many_v
148 where
149 many_v = some_v <|> pure []
150 some_v = (:) <$> v <*> many_v
151
152 -- instances for Prelude types
153
154 instance Applicative Maybe where
155 pure = return
156 (<*>) = ap
157
158 instance Alternative Maybe where
159 empty = Nothing
160 Nothing <|> p = p
161 Just x <|> _ = Just x
162
163 instance Applicative [] where
164 pure = return
165 (<*>) = ap
166
167 instance Alternative [] where
168 empty = []
169 (<|>) = (++)
170
171 instance Applicative IO where
172 pure = return
173 (<*>) = ap
174
175 #ifndef __NHC__
176 instance Applicative (ST s) where
177 pure = return
178 (<*>) = ap
179
180 instance Applicative (Lazy.ST s) where
181 pure = return
182 (<*>) = ap
183 #endif
184
185 #ifdef __GLASGOW_HASKELL__
186 instance Applicative STM where
187 pure = return
188 (<*>) = ap
189
190 instance Alternative STM where
191 empty = retry
192 (<|>) = orElse
193 #endif
194
195 instance Applicative ((->) a) where
196 pure = const
197 (<*>) f g x = f x (g x)
198
199 instance Monoid a => Applicative ((,) a) where
200 pure x = (mempty, x)
201 (u, f) <*> (v, x) = (u `mappend` v, f x)
202
203 instance Applicative (Either e) where
204 pure = Right
205 Left e <*> _ = Left e
206 Right f <*> r = fmap f r
207
208 -- new instances
209
210 newtype Const a b = Const { getConst :: a }
211
212 instance Functor (Const m) where
213 fmap _ (Const v) = Const v
214
215 instance Monoid m => Applicative (Const m) where
216 pure _ = Const mempty
217 Const f <*> Const v = Const (f `mappend` v)
218
219 newtype WrappedMonad m a = WrapMonad { unwrapMonad :: m a }
220
221 instance Monad m => Functor (WrappedMonad m) where
222 fmap f (WrapMonad v) = WrapMonad (liftM f v)
223
224 instance Monad m => Applicative (WrappedMonad m) where
225 pure = WrapMonad . return
226 WrapMonad f <*> WrapMonad v = WrapMonad (f `ap` v)
227
228 instance MonadPlus m => Alternative (WrappedMonad m) where
229 empty = WrapMonad mzero
230 WrapMonad u <|> WrapMonad v = WrapMonad (u `mplus` v)
231
232 newtype WrappedArrow a b c = WrapArrow { unwrapArrow :: a b c }
233
234 instance Arrow a => Functor (WrappedArrow a b) where
235 fmap f (WrapArrow a) = WrapArrow (a >>> arr f)
236
237 instance Arrow a => Applicative (WrappedArrow a b) where
238 pure x = WrapArrow (arr (const x))
239 WrapArrow f <*> WrapArrow v = WrapArrow (f &&& v >>> arr (uncurry id))
240
241 instance (ArrowZero a, ArrowPlus a) => Alternative (WrappedArrow a b) where
242 empty = WrapArrow zeroArrow
243 WrapArrow u <|> WrapArrow v = WrapArrow (u <+> v)
244
245 -- | Lists, but with an 'Applicative' functor based on zipping, so that
246 --
247 -- @f '<$>' 'ZipList' xs1 '<*>' ... '<*>' 'ZipList' xsn = 'ZipList' (zipWithn f xs1 ... xsn)@
248 --
249 newtype ZipList a = ZipList { getZipList :: [a] }
250
251 instance Functor ZipList where
252 fmap f (ZipList xs) = ZipList (map f xs)
253
254 instance Applicative ZipList where
255 pure x = ZipList (repeat x)
256 ZipList fs <*> ZipList xs = ZipList (zipWith id fs xs)
257
258 -- extra functions
259
260 -- | A variant of '<*>' with the arguments reversed.
261 (<**>) :: Applicative f => f a -> f (a -> b) -> f b
262 (<**>) = liftA2 (flip ($))
263
264 -- | Lift a function to actions.
265 -- This function may be used as a value for `fmap` in a `Functor` instance.
266 liftA :: Applicative f => (a -> b) -> f a -> f b
267 liftA f a = pure f <*> a
268
269 -- | Lift a binary function to actions.
270 liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
271 liftA2 f a b = f <$> a <*> b
272
273 -- | Lift a ternary function to actions.
274 liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
275 liftA3 f a b c = f <$> a <*> b <*> c
276
277 -- | One or none.
278 optional :: Alternative f => f a -> f (Maybe a)
279 optional v = Just <$> v <|> pure Nothing