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