# Haskell/Traversable

* Traversable (Solutions)
* Advanced Haskell
* Monoids
* Applicative functors
* Foldable
* Traversable
* Arrow tutorial
* Understanding arrows
* Continuation passing style
* Zippers
* Lenses
* Comonads
* Value recursion (MonadFix)
* Effectful streaming
* Mutable objects
* Concurrency
* Template Haskell
* Type Families
We already have studied four of the five type classes in the Prelude that can be used for data structure manipulation: Functor, Applicative, Monad and Foldable. The fifth one is Traversable. To traverse means to walk across, and that is exactly what Traversable generalises: walking across a structure, collecting results at each stop.

## Functors made for walking

If traversing means walking across, though, we have been performing traversals for a long time already. Consider the following plausible Functor and Foldable instances for lists:
fmap f walks across the list, applies f to each element and collects the results by rebuilding the list. Similarly, foldMap f walks across the list, applies f to each element and collects the results by combining them with mappend. Functor and Foldable, however, are not enough to express all useful ways of traversing. For instance, suppose we have the following Maybe-encoded test for negative numbers...
... and we want to use it to implement...
... which gives back the original list wrapped in Just if there are no negative elements in it, and Nothing otherwise. Neither Foldable nor Functor on their own would help. Using Foldable would replace the structure of the original list with that of whatever Monoid we pick for folding, and there is no way of twisting that into giving either the original list or Nothing. As for Functor, fmap might be attractive at first...
... but then we would need a way to turn a list of Maybe into Maybe a list. If you squint hard enough, that looks somewhat like a fold. Instead, however, of merely combining the values and destroying the list, we need to combine the Maybe contexts of the values and recreate the list structure within the combined context. Fortunately, there is a type class which is essentially about combining Functor contexts: Applicative. Applicative, in turn, leads us to the class we need: Traversable.
Traversable is to Applicative contexts what Foldable is to Monoid values. From that point of view, sequenceA is analogous to fold − it creates an applicative summary of the contexts within a structure, and then rebuilds the structure in the new context. sequenceA is the function we were looking for:
These are the methods of Traversable:
If sequenceA is analogous to fold, traverse is analogous to foldMap. They can be defined in terms of each other, and therefore a minimal implementation of Traversable just needs to supply one of them:
Rewriting the list instance using traverse makes the parallels with Functor and Foldable obvious:
In general, it is better to write traverse when implementing Traversable, as the default definition of traverse performs, in principle, two runs across the structure (one for fmap and another for sequenceA).
We can cleanly define rejectWithNegatives directly in terms of traverse:
* Exercises
* Give the Tree from Other data structures a Traversable instance. The definition of Tree is:
* data Tree a = Leaf a | Branch (Tree a) (Tree a)

## Interpretations of Traversable

Traversable structures can be walked over using the applicative functor of your choice. The type of traverse...
... resembles that of mapping functions we have seen in other classes. Rather than using its function argument to insert functorial contexts under the original structure (as might be done with fmap) or to modify the structure itself (as (>>=) does), traverse adds an extra layer of context on the top of the structure. Said in another way, traverse allows for effectful traversals − traversals which produce an overall effect (i.e. the new outer layer of context).
If the structure below the new layer is recoverable at all, it will match the original structure (the values might have changed, of course). Here is an example involving nested lists:
To understand what is going on here, let's break this down step by step.
The inner lists retain the structure of the original list − all of them have three elements. The outer list is the new layer, corresponding to the introduction of nondeterminism through allowing each element to vary from zero to its (original) value.
We can also understand Traversable by focusing on sequenceA and how it distributes context.
In this example, sequenceA can be seen distributing the old outer structure into the new outer structure, and so the new inner lists have two elements, just like the old outer list. The new outer structure is a list of twelve elements, which is exactly what you would expect from combining with (<*>) one list of four elements with another of three elements. One interesting aspect of the distribution perspective is how it helps making sense of why certain functors cannot possibly have instances of Traversable (how would one distribute an IO action? Or a function?).
* Exercises
* Having the applicative functors chapter fresh in memory can help with the following exercises.
* Consider a representation of matrices as nested lists, with the inner lists being the rows. Use Traversable to implement
* transpose :: [[a]] -> [[a]]
* which transposes a matrix (i.e. changes columns into rows and vice-versa). For the purposes of this exercise, we don't care about how fake "matrices" with rows of different sizes are handled.
* Explain what traverse mappend does.
* Time for a round of Spot The Applicative Functor. Consider:
* mapAccumL :: Traversable t => ‌(a -> b -> (a, c)) -> a -> t b -> (a, t c)
* Does its type remind you of anything? Use the appropriate Applicative to implement it with Traversable. As further guidance, here is the description of mapAccumL in the Data.Traversable documentation:
* The mapAccumL function behaves like a combination of fmap and foldl; it applies a function to each element of a structure, passing an accumulating parameter from left to right, and returning a final value of this accumulator together with the new structure.

## The Traversable laws

Sensible instances of Traversable have a set of laws to follow. There are the following two laws:
Plus a bonus law, which is guaranteed to hold:
Those laws are not exactly self-explanatory, so let's have a closer look at them. Starting from the last one: an applicative homomorphism is a function which preserves the Applicative operations, so that:
Note that not only this definition is analogous to the one of monoid homomorphisms which we have seen earlier on but also that the naturality law mirrors exactly the property about foldMap and monoid homomorphisms seen in the chapter about Foldable.
The identity law involves Identity, the dummy functor:
The law says that all traversing with the Identity constructor does is wrap the structure with Identity, which amounts to doing nothing (as the original structure can be trivially recovered with runIdentity). The Identity constructor is thus the identity traversal, which is very reasonable indeed.
The composition law, in turn, is stated in terms of the Compose functor:
Compose performs composition of functors. Composing two Functors results in a Functor, and composing two Applicatives results in an Applicative. The instances are the obvious ones, threading the methods one further functorial layer down.
The composition law states that it doesn't matter whether we perform two traversals separately (right side of the equation) or compose them in order to walk across the structure only once (left side). It is analogous, for instance, to the second functor law. The fmaps are needed because the second traversal (or the second part of the traversal, for the left side of the equation) happens below the layer of structure added by the first (part). Compose is needed so that the composed traversal is applied to the correct layer.
Identity and Compose are available from Data.Functor.Identity and Data.Functor.Compose respectively.
The laws can also be formulated in terms of sequenceA:
Though it's not immediately obvious, several desirable characteristics of traversals follow from the laws, including:
* Traversals do not skip elements.
* Traversals do not visit elements more than once.
* traverse pure = pure
* Traversals cannot modify the original structure (it is either preserved or fully destroyed).

## Recovering fmap and foldMap

We still have not justified the Functor and Foldable class constraints of Traversable. The reason for them is very simple: as long as the Traversable instance follows the laws traverse is enough to implement both fmap and foldMap. For fmap, all we need is to use Identity to make a traversal out of an arbitrary function:
To recover foldMap, we need to introduce a third utility functor: Const from Control-Applicative:
Const is a constant functor. A value of type Const a b does not actually contain a b value. Rather, it holds an a value which is unaffected by fmap. For our current purposes, the truly interesting instance is the Applicative one
(<*>) simply combines the values in each context with mappend. We can exploit that to make a traversal out of any Monoid m => a -> m function that we might pass to foldMap. Thanks to the instance above, the traversal then becomes a fold:
We have just recovered from traverse two functions which on the surface appear to be entirely different, and all we had to do was pick two different functors. That is a taste of how powerful an abstraction functors are.

## Notes

* ↑ Strictly speaking, we should refer to the five classes in the GHC Prelude, as Applicative, Foldable and Traversable aren't officially part of the Prelude yet according to the Haskell Report. It is just a matter of time for them to be included, though.
* ↑ One thing to attempt would be exploiting the Monoid a => Monoid (Maybe a) instance from Data.Monoid. If you try that, however, you will see it can't possibly give the desired results.
* ↑ The monoidal presentation of Applicative makes that very clear.
* ↑ Remarkably, however, composing two Monads does not necessarily result in a Monad.
* ↑ For technical details, check the papers cited by the Data.Traversable documentation.
* ↑ This is a great illustration of how Applicative combines contexts monoidally. If we remove the values within the context, the applicative laws in monoidal presentation match the monoid laws exactly.
* ↑ A prime example, and one of clear practical relevance at that, is that great ode to functors, the lens library.
* Traversable
* Solutions to exercises
* Advanced Haskell
* Monoids
* >> Applicative functors
* >> Foldable
* >> Traversable
* >> Arrow tutorial
* >> Understanding arrows
* >> Continuation passing style
* >> Zippers
* >> Lenses
* >> Comonads
* >> Value recursion (MonadFix)
* >> Effectful streaming
* >> Mutable objects
* >> Concurrency
* >> Template Haskell
* >> Type Families
* Haskell
* Haskell Basics
* >> Elementary Haskell
* >> Intermediate Haskell
* >> Monads
* Advanced Haskell
* >> Fun with Types
* >> Wider Theory
* >> Haskell Performance
* Libraries Reference
* >> General Practices
* >> Specialised Tasks