Movatterモバイル変換
[0]ホーム
{-# LANGUAGE Trustworthy #-}{-# LANGUAGE ScopedTypeVariables #-}------------------------------------------------------------------------------- |-- Module : Data.Bitraversable-- Copyright : (C) 2011-2016 Edward Kmett-- License : BSD-style (see the file LICENSE)---- Maintainer : libraries@haskell.org-- Stability : provisional-- Portability : portable---- @since 4.10.0.0----------------------------------------------------------------------------moduleData.Bitraversable(Bitraversable(..),bisequenceA,bisequence,bimapM,bifor,biforM,bimapAccumL,bimapAccumR,bimapDefault,bifoldMapDefault)whereimportControl.ApplicativeimportData.BifunctorimportData.BifoldableimportData.CoerceimportData.Functor.Identity(Identity(..))importData.Functor.Utils(StateL(..),StateR(..))importGHC.Generics(K1(..))-- | 'Bitraversable' identifies bifunctorial data structures whose elements can-- be traversed in order, performing 'Applicative' or 'Monad' actions at each-- element, and collecting a result structure with the same shape.---- As opposed to 'Traversable' data structures, which have one variety of-- element on which an action can be performed, 'Bitraversable' data structures-- have two such varieties of elements.---- A definition of 'bitraverse' must satisfy the following laws:---- [/naturality/]-- @'bitraverse' (t . f) (t . g) ≡ t . 'bitraverse' f g@-- for every applicative transformation @t@---- [/identity/]-- @'bitraverse' 'Identity' 'Identity' ≡ 'Identity'@---- [/composition/]-- @'Compose' . 'fmap' ('bitraverse' g1 g2) . 'bitraverse' f1 f2-- ≡ 'traverse' ('Compose' . 'fmap' g1 . f1) ('Compose' . 'fmap' g2 . f2)@---- where an /applicative transformation/ is a function---- @t :: ('Applicative' f, 'Applicative' g) => f a -> g a@---- preserving the 'Applicative' operations:---- @-- t ('pure' x) = 'pure' x-- t (f '<*>' x) = t f '<*>' t x-- @---- and the identity functor 'Identity' and composition functors 'Compose' are-- defined as---- > newtype Identity a = Identity { runIdentity :: a }-- >-- > instance Functor Identity where-- > fmap f (Identity x) = Identity (f x)-- >-- > instance Applicative Identity where-- > pure = Identity-- > Identity f <*> Identity x = Identity (f x)-- >-- > newtype Compose f g a = Compose (f (g a))-- >-- > instance (Functor f, Functor g) => Functor (Compose f g) where-- > fmap f (Compose x) = Compose (fmap (fmap f) x)-- >-- > instance (Applicative f, Applicative g) => Applicative (Compose f g) where-- > pure = Compose . pure . pure-- > Compose f <*> Compose x = Compose ((<*>) <$> f <*> x)---- Some simple examples are 'Either' and '(,)':---- > instance Bitraversable Either where-- > bitraverse f _ (Left x) = Left <$> f x-- > bitraverse _ g (Right y) = Right <$> g y-- >-- > instance Bitraversable (,) where-- > bitraverse f g (x, y) = (,) <$> f x <*> g y---- 'Bitraversable' relates to its superclasses in the following ways:---- @-- 'bimap' f g ≡ 'runIdentity' . 'bitraverse' ('Identity' . f) ('Identity' . g)-- 'bifoldMap' f g = 'getConst' . 'bitraverse' ('Const' . f) ('Const' . g)-- @---- These are available as 'bimapDefault' and 'bifoldMapDefault' respectively.---- @since 4.10.0.0class(Bifunctort,Bifoldablet)=>Bitraversabletwhere-- | Evaluates the relevant functions at each element in the structure,-- running the action, and builds a new structure with the same shape, using-- the results produced from sequencing the actions.---- @'bitraverse' f g ≡ 'bisequenceA' . 'bimap' f g@---- For a version that ignores the results, see 'bitraverse_'.---- @since 4.10.0.0bitraverse::Applicativef=>(a->fc)->(b->fd)->tab->f(tcd)bitraversefg=bisequenceA.bimapfg-- | Alias for 'bisequence'.---- @since 4.10.0.0bisequenceA::(Bitraversablet,Applicativef)=>t(fa)(fb)->f(tab)bisequenceA=bisequence-- | Alias for 'bitraverse'.---- @since 4.10.0.0bimapM::(Bitraversablet,Applicativef)=>(a->fc)->(b->fd)->tab->f(tcd)bimapM=bitraverse-- | Sequences all the actions in a structure, building a new structure with-- the same shape using the results of the actions. For a version that ignores-- the results, see 'bisequence_'.---- @'bisequence' ≡ 'bitraverse' 'id' 'id'@---- @since 4.10.0.0bisequence::(Bitraversablet,Applicativef)=>t(fa)(fb)->f(tab)bisequence=bitraverseidid-- | @since 4.10.0.0instanceBitraversable(,)wherebitraversefg~(a,b)=liftA2(,)(fa)(gb)-- | @since 4.10.0.0instanceBitraversable((,,)x)wherebitraversefg~(x,a,b)=liftA2((,,)x)(fa)(gb)-- | @since 4.10.0.0instanceBitraversable((,,,)xy)wherebitraversefg~(x,y,a,b)=liftA2((,,,)xy)(fa)(gb)-- | @since 4.10.0.0instanceBitraversable((,,,,)xyz)wherebitraversefg~(x,y,z,a,b)=liftA2((,,,,)xyz)(fa)(gb)-- | @since 4.10.0.0instanceBitraversable((,,,,,)xyzw)wherebitraversefg~(x,y,z,w,a,b)=liftA2((,,,,,)xyzw)(fa)(gb)-- | @since 4.10.0.0instanceBitraversable((,,,,,,)xyzwv)wherebitraversefg~(x,y,z,w,v,a,b)=liftA2((,,,,,,)xyzwv)(fa)(gb)-- | @since 4.10.0.0instanceBitraversableEitherwherebitraversef_(Lefta)=Left<$>fabitraverse_g(Rightb)=Right<$>gb-- | @since 4.10.0.0instanceBitraversableConstwherebitraversef_(Consta)=Const<$>fa-- | @since 4.10.0.0instanceBitraversable(K1i)wherebitraversef_(K1c)=K1<$>fc-- | 'bifor' is 'bitraverse' with the structure as the first argument. For a-- version that ignores the results, see 'bifor_'.---- @since 4.10.0.0bifor::(Bitraversablet,Applicativef)=>tab->(a->fc)->(b->fd)->f(tcd)bifortfg=bitraversefgt-- | Alias for 'bifor'.---- @since 4.10.0.0biforM::(Bitraversablet,Applicativef)=>tab->(a->fc)->(b->fd)->f(tcd)biforM=bifor-- | The 'bimapAccumL' function behaves like a combination of 'bimap' and-- 'bifoldl'; it traverses a structure from left to right, threading a state-- of type @a@ and using the given actions to compute new elements for the-- structure.---- @since 4.10.0.0bimapAccumL::Bitraversablet=>(a->b->(a,c))->(a->d->(a,e))->a->tbd->(a,tce)bimapAccumLfgst=runStateL(bitraverse(StateL.flipf)(StateL.flipg)t)s-- | The 'bimapAccumR' function behaves like a combination of 'bimap' and-- 'bifoldl'; it traverses a structure from right to left, threading a state-- of type @a@ and using the given actions to compute new elements for the-- structure.---- @since 4.10.0.0bimapAccumR::Bitraversablet=>(a->b->(a,c))->(a->d->(a,e))->a->tbd->(a,tce)bimapAccumRfgst=runStateR(bitraverse(StateR.flipf)(StateR.flipg)t)s-- | A default definition of 'bimap' in terms of the 'Bitraversable'-- operations.---- @'bimapDefault' f g ≡-- 'runIdentity' . 'bitraverse' ('Identity' . f) ('Identity' . g)@---- @since 4.10.0.0bimapDefault::foralltabcd.Bitraversablet=>(a->b)->(c->d)->tac->tbd-- See Note [Function coercion] in Data.Functor.Utils.bimapDefault=coerce(bitraverse::(a->Identityb)->(c->Identityd)->tac->Identity(tbd)){-# INLINEbimapDefault#-}-- | A default definition of 'bifoldMap' in terms of the 'Bitraversable'-- operations.---- @'bifoldMapDefault' f g ≡-- 'getConst' . 'bitraverse' ('Const' . f) ('Const' . g)@---- @since 4.10.0.0bifoldMapDefault::foralltmab.(Bitraversablet,Monoidm)=>(a->m)->(b->m)->tab->m-- See Note [Function coercion] in Data.Functor.Utils.bifoldMapDefault=coerce(bitraverse::(a->Constm())->(b->Constm())->tab->Constm(t()())){-# INLINEbifoldMapDefault#-}
[8]ページ先頭