# mathlib3documentation

control.traversable.basic

# Traversable type class #

THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.

Type classes for traversing collections. The concepts and laws are taken from http://hackage.haskell.org/package/base-4.11.1.0/docs/Data-Traversable.html

Traversable collections are a generalization of functors. Whereas functors (such as list) allow us to apply a function to every element, it does not allow functions which external effects encoded in a monad. Consider for instance a functor invite : email → io response that takes an email address, sends an email and waits for a response. If we have a list guests : list email, using calling invite using map gives us the following: map invite guests : list (io response). It is not what we need. We need something of type io (list response). Instead of using map, we can use traverse to send all the invites: traverse invite guests : io (list response). traverse applies invite to every element of guests and combines all the resulting effects. In the example, the effect is encoded in the monad io but any applicative functor is accepted by traverse.

For more on how to use traversable, consider the Haskell tutorial: https://en.wikibooks.org/wiki/Haskell/Traversable

## Main definitions #

• traversable type class - exposes the traverse function
• sequence - based on traverse, turns a collection of effects into an effect returning a collection
• is_lawful_traversable - laws for a traversable functor
• applicative_transformation - the notion of a natural transformation for applicative functors

## Tags #

traversable iterator functor applicative

## References #

structure applicative_transformation (F : Type u Type v) [applicative F] (G : Type u Type w) [applicative G]  :
Type (max (u+1) v w)

A transformation between applicative functors. It is a natural transformation such that app preserves the has_pure.pure and functor.map (<*>) operations. See applicative_transformation.preserves_map for naturality.

Instances for applicative_transformation
@[protected, instance]
def applicative_transformation.has_coe_to_fun (F : Type u Type v) [applicative F] (G : Type u Type w) [applicative G]  :
(λ (_x : , Π {α : Type u}, F α G α)
Equations
@[simp]
theorem applicative_transformation.app_eq_coe {F : Type u Type v} [applicative F] {G : Type u Type w} [applicative G] (η : G) :
η.app = η
@[simp]
theorem applicative_transformation.coe_mk {F : Type u Type v} [applicative F] {G : Type u Type w} [applicative G] (f : Π (α : Type u), F α G α) (pp : {α : Type u} (x : α), f α = ) (ps : {α β : Type u} (x : F β)) (y : F α), f β (x <*> y) = f β) x <*> f α y) :
{app := f, preserves_pure' := pp, preserves_seq' := ps} = f
@[protected]
theorem applicative_transformation.congr_fun {F : Type u Type v} [applicative F] {G : Type u Type w} [applicative G] (η η' : G) (h : η = η') {α : Type u} (x : F α) :
η x = η' x
@[protected]
theorem applicative_transformation.congr_arg {F : Type u Type v} [applicative F] {G : Type u Type w} [applicative G] (η : G) {α : Type u} {x y : F α} (h : x = y) :
η x = η y
theorem applicative_transformation.coe_inj {F : Type u Type v} [applicative F] {G : Type u Type w} [applicative G] ⦃η η' : G⦄ (h : η = η') :
η = η'
@[ext]
theorem applicative_transformation.ext {F : Type u Type v} [applicative F] {G : Type u Type w} [applicative G] ⦃η η' : G⦄ (h : (α : Type u) (x : F α), η x = η' x) :
η = η'
theorem applicative_transformation.ext_iff {F : Type u Type v} [applicative F] {G : Type u Type w} [applicative G] {η η' : G} :
η = η' (α : Type u) (x : F α), η x = η' x
theorem applicative_transformation.preserves_pure {F : Type u Type v} [applicative F] {G : Type u Type w} [applicative G] (η : G) {α : Type u} (x : α) :
η =
theorem applicative_transformation.preserves_seq {F : Type u Type v} [applicative F] {G : Type u Type w} [applicative G] (η : G) {α β : Type u} (x : F β)) (y : F α) :
η (x <*> y) = η x <*> η y
theorem applicative_transformation.preserves_map {F : Type u Type v} [applicative F] {G : Type u Type w} [applicative G] (η : G) {α β : Type u} (x : α β) (y : F α) :
η (x <$> y) = x <$> η y
theorem applicative_transformation.preserves_map' {F : Type u Type v} [applicative F] {G : Type u Type w} [applicative G] (η : G) {α β : Type u} (x : α β) :
=

The identity applicative transformation from an applicative functor to itself.

Equations
@[protected, instance]
Equations
def applicative_transformation.comp {F : Type u Type v} [applicative F] {G : Type u Type w} [applicative G] {H : Type u Type s} [applicative H] (η' : H) (η : G) :

The composition of applicative transformations.

Equations
@[simp]
theorem applicative_transformation.comp_apply {F : Type u Type v} [applicative F] {G : Type u Type w} [applicative G] {H : Type u Type s} [applicative H] (η' : H) (η : G) {α : Type u} (x : F α) :
(η'.comp η) x = η' (η x)
theorem applicative_transformation.comp_assoc {F : Type u Type v} [applicative F] {G : Type u Type w} [applicative G] {H : Type u Type s} [applicative H] {I : Type u Type t} [applicative I] (η'' : I) (η' : H) (η : G) :
(η''.comp η').comp η = η''.comp (η'.comp η)
@[simp]
theorem applicative_transformation.comp_id {F : Type u Type v} [applicative F] {G : Type u Type w} [applicative G] (η : G) :
@[simp]
theorem applicative_transformation.id_comp {F : Type u Type v} [applicative F] {G : Type u Type w} [applicative G] (η : G) :
@[class]
structure traversable (t : Type u Type u) :
Type (u+1)

A traversable functor is a functor along with a way to commute with all applicative functors (see sequence). For example, if t is the traversable functor list and m is the applicative functor io, then given a function f : α → io β, the function functor.map f is list α → list (io β), but traverse f is list α → io (list β).

Instances of this typeclass
Instances of other typeclasses for traversable
• traversable.has_sizeof_inst
def sequence {t : Type u Type u} {α : Type u} {f : Type u Type u} [applicative f] [traversable t] :
t (f α) f (t α)

A traversable functor commutes with all applicative functors.

Equations
@[class]
structure is_lawful_traversable (t : Type u Type u) [traversable t] :
Type (u+1)
• to_is_lawful_functor :
• id_traverse : {α : Type ?} (x : t α),
• comp_traverse : {F G : Type ? Type ?} [_inst_1_1 : [_inst_2 : [_inst_3 : [_inst_4 : {α β γ : Type ?} (f : β F γ) (g : α G β) (x : t α),
• traverse_eq_map_id : {α β : Type ?} (f : α β) (x : t α), x = id.mk (f <$> x) • naturality : {F G : Type ? Type ?} [_inst_1_1 : [_inst_2 : [_inst_3 : [_inst_4 : (η : {α β : Type ?} (f : α F β) (x : t α), η x) = traversable.traverse (η f) x A traversable functor is lawful if its traverse satisfies a number of additional properties. It must send id.mk to id.mk, send the composition of applicative functors to the composition of the traverse of each, send each function f to λ x, f <$> x, and satisfy a naturality condition with respect to applicative transformations.

Instances of this typeclass
Instances of other typeclasses for is_lawful_traversable
• is_lawful_traversable.has_sizeof_inst
@[protected, instance]
Equations
@[protected, instance]
Equations
@[protected, instance]
Equations
@[protected, instance]
Equations
@[protected]
def sum.traverse {σ : Type u} {F : Type u Type u} [applicative F] {α : Type u_1} {β : Type u} (f : α F β) :
σ α F β)

Defines a traverse function on the second component of a sum type. This is used to give a traversable instance for the functor σ ⊕ -.

Equations
@[protected, instance]
def sum.traversable {σ : Type u} :
Equations