import Foreign.Storable.Traversable as Store data Stereo a = Stereo {left, right :: a} instance Functor Stereo where fmap = Trav.fmapDefault instance Foldable Stereo where foldMap = Trav.foldMapDefault instance Traversable Stereo where sequenceA ~(Stereo l r) = liftA2 Stereo l r instance (Storable a) => Storable (Stereo a) where sizeOf = Store.sizeOf alignment = Store.alignment peek = Store.peek (error "instance Traversable Stereo is lazy, so we do not provide a real value here") poke = Store.pokeYou would certainly not define the Traversable and according instances just for the implementation of the Storable instance, but there are usually similar applications where the Traversable instance is useful.
t :: (Applicative f, Applicative g) => f a -> g apreserving the Applicative operations, i.e. and the identity functor Identity and composition of functors Compose are defined as
newtype Identity a = Identity a instance Functor Identity where fmap f (Identity x) = Identity (f x) instance Applicative Identity where pure x = Identity x 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 x = Compose (pure (pure x)) Compose f <*> Compose x = Compose ((<*>) <$> f <*> x)(The naturality law is implied by parametricity.) Instances are similar to Functor, e.g. given a data type
data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)a suitable instance would be
instance Traversable Tree where traverse f Empty = pure Empty traverse f (Leaf x) = Leaf <$> f x traverse f (Node l k r) = Node <$> traverse f l <*> f k <*> traverse f rThis is suitable even for abstract types, as the laws for <*> imply a form of associativity. The superclass instances should satisfy the following:
t :: (Applicative f, Applicative g) => f a -> g apreserving the Applicative operations, i.e.
t (pure x) = pure x t (f <*> x) = t f <*> t xand the identity functor Identity and composition functors Compose are from Data.Functor.Identity and Data.Functor.Compose. A result of the naturality law is a purity law for traverse
traverse pure = pure(The naturality law is implied by parametricity and thus so is the purity law [1, p15].) Instances are similar to Functor, e.g. given a data type
data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)a suitable instance would be
instance Traversable Tree where traverse f Empty = pure Empty traverse f (Leaf x) = Leaf <$> f x traverse f (Node l k r) = Node <$> traverse f l <*> f k <*> traverse f rThis is suitable even for abstract types, as the laws for <*> imply a form of associativity. The superclass instances should satisfy the following:
gmap @Show f x everywhere @Typeable f xetc. For more information, see:
t . btraverse f = btraverse (t . f) -- naturality btraverse Identity = Identity -- identity btraverse (Compose . fmap g . f) = Compose . fmap (btraverse g) . btraverse f -- compositionThere is a default btraverse implementation for Generic types, so instances can derived automatically.