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 /Applicative Programming with Effects/,
14 -- by Conor McBride and Ross Paterson, online at
15 -- <http://www.soi.city.ac.uk/~ross/papers/Applicative.html>.
17 module Data.Traversable (
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)
30 -- | Functors representing data structures that can be traversed from
33 -- Minimal complete definition: 'traverse' or 'sequenceA'.
35 -- Instances are similar to 'Functor', e.g. given a data type
37 -- > data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)
39 -- a suitable instance would be
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
46 -- This is suitable even for abstract types, as the laws for '<*>'
47 -- imply a form of associativity.
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
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
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)
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)
70 -- instances for Prelude types
72 instance Traversable Maybe where
73 traverse f Nothing = pure Nothing
74 traverse f (Just x) = Just <$> f x
76 instance Traversable [] where
77 traverse f = foldr cons_f (pure [])
78 where cons_f x ys = (:) <$> f x <*> ys
82 instance Ix i => Traversable (Array i) where
83 traverse f arr = listArray (bounds arr) <$> traverse f (elems arr)
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)
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)
98 newtype Id a = Id { getId :: a }
100 instance Functor Id where
101 fmap f (Id x) = Id (f x)
103 instance Applicative Id where
105 Id f <*> Id x = Id (f x)