------------------------------------------------------------------------------- |-- Module : Data.Traversable-- Copyright : Conor McBride and Ross Paterson 2005-- License : BSD-style (see the LICENSE file in the distribution)---- Maintainer : libraries@haskell.org-- Stability : stable-- Portability : portable---- Class of data structures that can be traversed from left to right,-- performing an action on each element. Instances are expected to satisfy-- the listed [laws](#laws).-----------------------------------------------------------------------------module Data.Traversable ( -- * The 'Traversable' class Traversable(..), -- * Utility functions for, forM, forAccumM, mapAccumL, mapAccumR, mapAccumM, -- * General definitions for superclass methods fmapDefault, foldMapDefault, ) whereimport qualified Prelude() -- do not import Preludeimport Primitivesimport Control.Applicativeimport Control.Errorimport Control.Monad(Monad(..), MonadPlus(..), liftM)--import Data.Coerceimport Data.Eitherimport Data.Foldableimport Data.Foldable.Internal(StateL(..), runStateL, StateR(..), runStateR, StateT(..), runStateT)import Data.Functionimport Data.Functorimport Data.Functor.Constimport Data.Functor.Identityimport Data.List_Typeimport qualified Data.List as Listimport Data.Maybeimport Data.Monoid.Internalimport Data.Ord (Down(..))import Data.Proxyimport Data.Tuple (Solo(..))import {-# SOURCE #-} Data.Typeableclass (Functor t, Foldable t) => Traversable (t :: Type -> Type) where traverse :: forall (f :: Type -> Type) a b . Applicative f => (a -> f b) -> t a -> f (t b) traverse f = sequenceA . fmap f sequenceA :: forall (f :: Type -> Type) a . Applicative f => t (f a) -> f (t a) sequenceA = traverse id mapM :: forall (m :: Type -> Type) a b . Monad m => (a -> m b) -> t a -> m (t b) mapM = traverse sequence :: forall (m :: Type -> Type) a . Monad m => t (m a) -> m (t a) sequence = sequenceAinstance Traversable Maybe where traverse _ Nothing = pure Nothing traverse f (Just x) = Just <$> f xinstance Traversable [] where traverse f = List.foldr cons_f (pure []) where cons_f x ys = liftA2 (:) (f x) ysinstance Traversable (Either a) where traverse _ (Left x) = pure (Left x) traverse f (Right y) = Right <$> f yinstance Traversable Identity where traverse f (Identity a) = Identity <$> f ainstance Traversable (Const m) where traverse _ (Const m) = pure $ Const m-- | @since 4.15deriving instance Traversable Solo-- | @since 4.7.0.0instance Traversable Proxy where traverse _ _ = pure Proxy {-# INLINE traverse #-} sequenceA _ = pure Proxy {-# INLINE sequenceA #-} mapM _ _ = pure Proxy {-# INLINE mapM #-} sequence _ = pure Proxy {-# INLINE sequence #-}{--- | @since 4.7.0.0instance Traversable (Const m) where traverse _ (Const m) = pure $ Const m-- | @since 4.8.0.0instance Traversable Dual where traverse f (Dual x) = Dual <$> f x-- | @since 4.8.0.0instance Traversable Sum where traverse f (Sum x) = Sum <$> f x-- | @since 4.8.0.0instance Traversable Product where traverse f (Product x) = Product <$> f x-- | @since 4.8.0.0instance Traversable First where traverse f (First x) = First <$> traverse f x-- | @since 4.8.0.0instance Traversable Last where traverse f (Last x) = Last <$> traverse f x-- | @since 4.12.0.0instance (Traversable f) => Traversable (Alt f) where traverse f (Alt x) = Alt <$> traverse f x-- | @since 4.12.0.0instance (Traversable f) => Traversable (Ap f) where traverse f (Ap x) = Ap <$> traverse f x-- | @since 4.9.0.0instance Traversable ZipList where traverse f (ZipList x) = ZipList <$> traverse f xinstance Traversable (Arg a) where traverse f (Arg x a) = Arg x `fmap` f a-- Instances for GHC.Generics-- | @since 4.9.0.0instance Traversable U1 where traverse _ _ = pure U1 {-# INLINE traverse #-} sequenceA _ = pure U1 {-# INLINE sequenceA #-} mapM _ _ = pure U1 {-# INLINE mapM #-} sequence _ = pure U1 {-# INLINE sequence #-}-- | @since 4.9.0.0deriving instance Traversable V1-- | @since 4.9.0.0deriving instance Traversable Par1-- | @since 4.9.0.0deriving instance Traversable f => Traversable (Rec1 f)-- | @since 4.9.0.0deriving instance Traversable (K1 i c)-- | @since 4.9.0.0deriving instance Traversable f => Traversable (M1 i c f)-- | @since 4.9.0.0deriving instance (Traversable f, Traversable g) => Traversable (f :+: g)-- | @since 4.9.0.0deriving instance (Traversable f, Traversable g) => Traversable (f :*: g)-- | @since 4.9.0.0deriving instance (Traversable f, Traversable g) => Traversable (f :.: g)-- | @since 4.9.0.0deriving instance Traversable UAddr-- | @since 4.9.0.0deriving instance Traversable UChar-- | @since 4.9.0.0deriving instance Traversable UDouble-- | @since 4.9.0.0deriving instance Traversable UFloat-- | @since 4.9.0.0deriving instance Traversable UInt-- | @since 4.9.0.0deriving instance Traversable UWord-}-- Instance for Data.Ord-- | @since 4.12.0.0deriving instance Traversable Down-- general functions-- | 'for' is 'traverse' with its arguments flipped. For a version-- that ignores the results see 'Data.Foldable.for_'.for :: forall (t :: Type -> Type) (f :: Type -> Type) a b . (Traversable t, Applicative f) => t a -> (a -> f b) -> f (t b)for = flip traverseforM :: forall (t :: Type -> Type) (m :: Type -> Type) a b . (Traversable t, Monad m) => t a -> (a -> m b) -> m (t b)forM = flip mapMmapAccumL :: forall t s a b. Traversable t => (s -> a -> (s, b)) -> s -> t a -> (s, t b)mapAccumL f s t = runStateL (traverse (StateL . flip f) t) s --coerce (traverse @t @(StateL s) @a @b) (flip f) t smapAccumR :: forall t s a b. Traversable t => (s -> a -> (s, b)) -> s -> t a -> (s, t b)mapAccumR f s t = runStateR (traverse (StateR . flip f) t) s --coerce (traverse @t @(StateR s) @a @b) (flip f) t smapAccumM :: forall m t s a b. (Monad m, Traversable t) => (s -> a -> m (s, b)) -> s -> t a -> m (s, t b)mapAccumM f s t = runStateT (traverse (StateT . flip f) t) s -- coerce (mapM @t @(StateT s m) @a @b) (StateT #. flip f) t sforAccumM :: (Monad m, Traversable t) => s -> t a -> (s -> a -> m (s, b)) -> m (s, t b)forAccumM s t f = mapAccumM f s tfmapDefault :: forall t a b . Traversable t => (a -> b) -> t a -> t bfmapDefault f = runIdentity . traverse (Identity . f)foldMapDefault :: forall t m a . (Traversable t, Monoid m) => (a -> m) -> t a -> mfoldMapDefault f = getConst . traverse (Const . f)-----------------------