-class Traversable t where
- -- | Map each element of a structure to an action, evaluate
- -- these actions from left to right, and collect the results.
- traverse :: Applicative f => (a -> f b) -> t a -> f (t b)
-
- -- | Map each element of a structure to an monadic action, evaluate
- -- these actions from left to right, and collect the results.
- mapM :: Monad m => (a -> m b) -> t a -> m (t b)
- mapM f = unwrapMonad . traverse (WrapMonad . f)
+-- The superclass instances should satisfy the following:
+--
+-- * In the 'Functor' instance, 'fmap' should be equivalent to traversal
+-- with the identity applicative functor ('fmapDefault').
+--
+-- * In the 'Foldable' instance, 'Data.Foldable.foldMap' should be
+-- equivalent to traversal with a constant applicative functor
+-- ('foldMapDefault').
+--
+class (Functor t, Foldable t) => Traversable t where
+ -- | Map each element of a structure to an action, evaluate
+ -- these actions from left to right, and collect the results.
+ traverse :: Applicative f => (a -> f b) -> t a -> f (t b)
+ traverse f = sequenceA . fmap f
+
+ -- | Evaluate each action in the structure from left to right,
+ -- and collect the results.
+ sequenceA :: Applicative f => t (f a) -> f (t a)
+ sequenceA = traverse id
+
+ -- | Map each element of a structure to a monadic action, evaluate
+ -- these actions from left to right, and collect the results.
+ mapM :: Monad m => (a -> m b) -> t a -> m (t b)
+ mapM f = unwrapMonad . traverse (WrapMonad . f)
+
+ -- | Evaluate each monadic action in the structure from left to right,
+ -- and collect the results.
+ sequence :: Monad m => t (m a) -> m (t a)
+ sequence = mapM id