[project @ 2005-11-29 14:31:59 by ross]
[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 <http://www.soi.city.ac.uk/~ross/papers/Applicative.html>.
14
15 module Data.Traversable (
16         Traversable(..),
17         sequenceA,
18         sequence,
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.Monoid (Monoid)
27 import Data.Array
28
29 -- | Functors representing data structures that can be traversed from
30 -- left to right.
31 --
32 -- Minimal complete definition: 'traverse'.
33 --
34 -- Instances are similar to 'Functor', e.g. given a data type
35 --
36 -- > data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)
37 --
38 -- a suitable instance would be
39 --
40 -- > instance Traversable Tree
41 -- >    traverse f Empty = pure Empty
42 -- >    traverse f (Leaf x) = Leaf <$> f x
43 -- >    traverse f (Node l k r) = Node <$> traverse f l <*> f k <*> traverse f r
44 --
45 -- This is suitable even for abstract types, as the laws for '<*>'
46 -- imply a form of associativity.
47 --
48 class Traversable t where
49         -- | Map each element of a structure to an action, evaluate
50         -- these actions from left to right, and collect the results.
51         traverse :: Applicative f => (a -> f b) -> t a -> f (t b)
52
53         -- | Map each element of a structure to an monadic action, evaluate
54         -- these actions from left to right, and collect the results.
55         mapM :: Monad m => (a -> m b) -> t a -> m (t b)
56         mapM f = unwrapMonad . traverse (WrapMonad . f)
57
58 -- instances for Prelude types
59
60 instance Traversable Maybe where
61         traverse f Nothing = pure Nothing
62         traverse f (Just x) = Just <$> f x
63
64 instance Traversable [] where
65         traverse f = foldr cons_f (pure [])
66           where cons_f x ys = (:) <$> f x <*> ys
67
68         mapM = Prelude.mapM
69
70 instance Ix i => Traversable (Array i) where
71         traverse f arr = listArray (bounds arr) <$> traverse f (elems arr)
72
73 -- general functions
74
75 -- | Evaluate each action in the structure from left to right,
76 -- and collect the results.
77 sequenceA :: (Traversable t, Applicative f) => t (f a) -> f (t a)
78 sequenceA = traverse id
79
80 -- | Evaluate each monadic action in the structure from left to right,
81 -- and collect the results.
82 sequence :: (Traversable t, Monad m) => t (m a) -> m (t a)
83 sequence = mapM id
84
85 -- | Any 'Traversable' can also be made an instance of 'Functor' by
86 -- defining 'fmap' as 'fmapDefault'.
87 fmapDefault :: Traversable t => (a -> b) -> t a -> t b
88 fmapDefault f = getId . traverse (Id . f)
89
90 -- | Any 'Traversable' can also be made an instance of
91 -- 'Data.Foldable.Foldable' by defining 'Data.Foldable.foldMap'
92 -- as 'foldMapDefault'.
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 Applicative Id where
101         pure = Id
102         Id f <*> Id x = Id (f x)