1 -----------------------------------------------------------------------------
3 -- Module : Data.Traversable
4 -- Copyright : Conor McBride and Ross Paterson 2005
5 -- License : BSD-style (see the LICENSE file in the distribution)
7 -- Maintainer : ross@soi.city.ac.uk
8 -- Stability : experimental
9 -- Portability : portable
11 -- Class of data structures that can be traversed from left to right.
13 -- See also <http://www.soi.city.ac.uk/~ross/papers/Applicative.html>.
15 module Data.Traversable (
23 import Prelude hiding (mapM, sequence)
24 import qualified Prelude (mapM)
25 import Control.Applicative
26 import Data.Monoid (Monoid)
29 -- | Functors representing data structures that can be traversed from
32 -- Minimal complete definition: 'traverse'.
34 -- Instances are similar to 'Functor', e.g. given a data type
36 -- > data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)
38 -- a suitable instance would be
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
45 -- This is suitable even for abstract types, as the laws for '<*>'
46 -- imply a form of associativity.
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)
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)
58 -- instances for Prelude types
60 instance Traversable Maybe where
61 traverse f Nothing = pure Nothing
62 traverse f (Just x) = Just <$> f x
64 instance Traversable [] where
65 traverse f = foldr cons_f (pure [])
66 where cons_f x ys = (:) <$> f x <*> ys
70 instance Ix i => Traversable (Array i) where
71 traverse f arr = listArray (bounds arr) <$> traverse f (elems arr)
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
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)
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)
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)
98 newtype Id a = Id { getId :: a }
100 instance Applicative Id where
102 Id f <*> Id x = Id (f x)