mathlib3 documentation

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 #

Tags #

traversable iterator functor applicative

References #

structure applicative_transformation (F : Type u Type v) [applicative F] [is_lawful_applicative F] (G : Type u Type w) [applicative G] [is_lawful_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
@[simp]
theorem applicative_transformation.coe_mk {F : Type u Type v} [applicative F] [is_lawful_applicative F] {G : Type u Type w} [applicative G] [is_lawful_applicative G] (f : Π (α : Type u), F α G α) (pp : {α : Type u} (x : α), f α (has_pure.pure x) = has_pure.pure x) (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] [is_lawful_applicative F] {G : Type u Type w} [applicative G] [is_lawful_applicative G] (η η' : applicative_transformation F G) (h : η = η') {α : Type u} (x : F α) :
η x = η' x
@[protected]
theorem applicative_transformation.congr_arg {F : Type u Type v} [applicative F] [is_lawful_applicative F] {G : Type u Type w} [applicative G] [is_lawful_applicative G] (η : applicative_transformation F G) {α : Type u} {x y : F α} (h : x = y) :
η x = η y
@[ext]
theorem applicative_transformation.ext {F : Type u Type v} [applicative F] [is_lawful_applicative F] {G : Type u Type w} [applicative G] [is_lawful_applicative G] ⦃η η' : applicative_transformation F G⦄ (h : (α : Type u) (x : F α), η x = η' x) :
η = η'
theorem applicative_transformation.ext_iff {F : Type u Type v} [applicative F] [is_lawful_applicative F] {G : Type u Type w} [applicative G] [is_lawful_applicative G] {η η' : applicative_transformation F G} :
η = η' (α : Type u) (x : F α), η x = η' x
theorem applicative_transformation.preserves_seq {F : Type u Type v} [applicative F] [is_lawful_applicative F] {G : Type u Type w} [applicative G] [is_lawful_applicative G] (η : applicative_transformation F G) {α β : Type u} (x : F β)) (y : F α) :
η (x <*> y) = η x <*> η y
theorem applicative_transformation.preserves_map {F : Type u Type v} [applicative F] [is_lawful_applicative F] {G : Type u Type w} [applicative G] [is_lawful_applicative G] (η : applicative_transformation F G) {α β : Type u} (x : α β) (y : F α) :
η (x <$> y) = x <$> η y

The identity applicative transformation from an applicative functor to itself.

Equations

The composition of applicative transformations.

Equations
@[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)

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]
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