add superclasses to Applicative and Traversable
[ghc-base.git] / Data / Traversable.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  Data.Traversable
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 -- Class of data structures that can be traversed from left to right.
12 --
13 -- See also /Applicative Programming with Effects/,
14 -- by Conor McBride and Ross Paterson, online at
15 -- <http://www.soi.city.ac.uk/~ross/papers/Applicative.html>.
16
17 module Data.Traversable (
18         Traversable(..),
19         fmapDefault,
20         foldMapDefault,
21         ) where
22
23 import Prelude hiding (mapM, sequence)
24 import qualified Prelude (mapM)
25 import Control.Applicative
26 import Data.Foldable (Foldable)
27 import Data.Monoid (Monoid)
28 import Data.Array
29
30 -- | Functors representing data structures that can be traversed from
31 -- left to right.
32 --
33 -- Minimal complete definition: 'traverse' or 'sequenceA'.
34 --
35 -- Instances are similar to 'Functor', e.g. given a data type
36 --
37 -- > data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)
38 --
39 -- a suitable instance would be
40 --
41 -- > instance Traversable Tree
42 -- >    traverse f Empty = pure Empty
43 -- >    traverse f (Leaf x) = Leaf <$> f x
44 -- >    traverse f (Node l k r) = Node <$> traverse f l <*> f k <*> traverse f r
45 --
46 -- This is suitable even for abstract types, as the laws for '<*>'
47 -- imply a form of associativity.
48 --
49 class (Functor t, Foldable t) => Traversable t where
50         -- | Map each element of a structure to an action, evaluate
51         -- these actions from left to right, and collect the results.
52         traverse :: Applicative f => (a -> f b) -> t a -> f (t b)
53         traverse f = sequenceA . fmap f
54
55         -- | Evaluate each action in the structure from left to right,
56         -- and collect the results.
57         sequenceA :: Applicative f => t (f a) -> f (t a)
58         sequenceA = traverse id
59
60         -- | Map each element of a structure to an monadic action, evaluate
61         -- these actions from left to right, and collect the results.
62         mapM :: Monad m => (a -> m b) -> t a -> m (t b)
63         mapM f = unwrapMonad . traverse (WrapMonad . f)
64
65         -- | Evaluate each monadic action in the structure from left to right,
66         -- and collect the results.
67         sequence :: Monad m => t (m a) -> m (t a)
68         sequence = mapM id
69
70 -- instances for Prelude types
71
72 instance Traversable Maybe where
73         traverse f Nothing = pure Nothing
74         traverse f (Just x) = Just <$> f x
75
76 instance Traversable [] where
77         traverse f = foldr cons_f (pure [])
78           where cons_f x ys = (:) <$> f x <*> ys
79
80         mapM = Prelude.mapM
81
82 instance Ix i => Traversable (Array i) where
83         traverse f arr = listArray (bounds arr) <$> traverse f (elems arr)
84
85 -- general functions
86
87 -- | This function may be used as a value for `fmap` in a `Functor` instance.
88 fmapDefault :: Traversable t => (a -> b) -> t a -> t b
89 fmapDefault f = getId . traverse (Id . f)
90
91 -- | This function may be used as a value for `Data.Foldable.foldMap`
92 -- in a `Foldable` instance.
93 foldMapDefault :: (Traversable t, Monoid m) => (a -> m) -> t a -> m
94 foldMapDefault f = getConst . traverse (Const . f)
95
96 -- local instances
97
98 newtype Id a = Id { getId :: a }
99
100 instance Functor Id where
101         fmap f (Id x) = Id (f x)
102
103 instance Applicative Id where
104         pure = Id
105         Id f <*> Id x = Id (f x)