add superclasses to Applicative and Traversable
[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  :  ross@soi.city.ac.uk
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         -- * Instances
29         WrappedMonad(..), Const(..), ZipList(..),
30         -- * Utility functions
31         (<$>), (<$), (*>), (<*), (<**>),
32         liftA, liftA2, liftA3
33         ) where
34
35 #ifdef __HADDOCK__
36 import Prelude
37 #endif
38
39 import Control.Monad (liftM, ap)
40 import Control.Monad.Instances ()
41 import Data.Monoid (Monoid(..))
42
43 infixl 4 <$>, <$
44 infixl 4 <*>, <*, *>, <**>
45
46 -- | A functor with application.
47 --
48 -- Instances should satisfy the following laws:
49 --
50 -- [/identity/]
51 --      @'pure' 'id' '<*>' v = v@
52 --
53 -- [/composition/]
54 --      @'pure' (.) '<*>' u '<*>' v '<*>' w = u '<*>' (v '<*>' w)@
55 --
56 -- [/homomorphism/]
57 --      @'pure' f '<*>' 'pure' x = 'pure' (f x)@
58 --
59 -- [/interchange/]
60 --      @u '<*>' 'pure' y = 'pure' ('$' y) '<*>' u@
61 --
62 -- If @f@ is also a 'Monad', define @'pure' = 'return'@ and @('<*>') = 'ap'@.
63
64 class Functor f => Applicative f where
65         -- | Lift a value.
66         pure :: a -> f a
67
68         -- | Sequential application.
69         (<*>) :: f (a -> b) -> f a -> f b
70
71 -- instances for Prelude types
72
73 instance Applicative Maybe where
74         pure = return
75         (<*>) = ap
76
77 instance Applicative [] where
78         pure = return
79         (<*>) = ap
80
81 instance Applicative IO where
82         pure = return
83         (<*>) = ap
84
85 instance Applicative ((->) a) where
86         pure = const
87         (<*>) f g x = f x (g x)
88
89 instance Monoid a => Applicative ((,) a) where
90         pure x = (mempty, x)
91         (u, f) <*> (v, x) = (u `mappend` v, f x)
92
93 -- new instances
94
95 newtype WrappedMonad m a = WrapMonad { unwrapMonad :: m a }
96
97 instance Monad m => Functor (WrappedMonad m) where
98         fmap f (WrapMonad v) = WrapMonad (liftM f v)
99
100 instance Monad m => Applicative (WrappedMonad m) where
101         pure = WrapMonad . return
102         WrapMonad f <*> WrapMonad v = WrapMonad (f `ap` v)
103
104 newtype Const a b = Const { getConst :: a }
105
106 instance Functor (Const m) where
107         fmap _ (Const v) = Const v
108
109 instance Monoid m => Applicative (Const m) where
110         pure _ = Const mempty
111         Const f <*> Const v = Const (f `mappend` v)
112
113 -- | Lists, but with an 'Applicative' functor based on zipping, so that
114 --
115 -- @f '<$>' 'ZipList' xs1 '<*>' ... '<*>' 'ZipList' xsn = 'ZipList' (zipWithn f xs1 ... xsn)@
116 --
117 newtype ZipList a = ZipList { getZipList :: [a] }
118
119 instance Functor ZipList where
120         fmap f (ZipList xs) = ZipList (map f xs)
121
122 instance Applicative ZipList where
123         pure x = ZipList (repeat x)
124         ZipList fs <*> ZipList xs = ZipList (zipWith id fs xs)
125
126 -- extra functions
127
128 -- | A synonym for 'fmap'.
129 (<$>) :: Functor f => (a -> b) -> f a -> f b
130 f <$> a = fmap f a
131
132 -- | Replace the value.
133 (<$) :: Functor f => a -> f b -> f a
134 (<$) = (<$>) . const
135  
136 -- | Sequence actions, discarding the value of the first argument.
137 (*>) :: Applicative f => f a -> f b -> f b
138 (*>) = liftA2 (const id)
139  
140 -- | Sequence actions, discarding the value of the second argument.
141 (<*) :: Applicative f => f a -> f b -> f a
142 (<*) = liftA2 const
143  
144 -- | A variant of '<*>' with the arguments reversed.
145 (<**>) :: Applicative f => f a -> f (a -> b) -> f b
146 (<**>) = liftA2 (flip ($))
147
148 -- | Lift a function to actions.
149 -- This function may be used as a value for `fmap` in a `Functor` instance.
150 liftA :: Applicative f => (a -> b) -> f a -> f b
151 liftA f a = pure f <*> a
152
153 -- | Lift a binary function to actions.
154 liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
155 liftA2 f a b = f <$> a <*> b
156
157 -- | Lift a ternary function to actions.
158 liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
159 liftA3 f a b c = f <$> a <*> b <*> c