mathlib documentation

control.​traversable.​basic

control.​traversable.​basic

Traversable type class

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 uType v) [applicative F] [is_lawful_applicative F] (G : Type uType w) [applicative G] [is_lawful_applicative G] :
Type (max (u+1) v w)
  • app : Π (α : Type ?), F αG α
  • preserves_pure' : ∀ {α : Type ?} (x : α), c.app α (has_pure.pure x) = has_pure.pure x
  • preserves_seq' : ∀ {α β : Type ?} (x : F (α → β)) (y : F α), c.app β (x <*> y) = c.app (α → β) x <*> c.app α y

@[instance]

Equations
theorem applicative_transformation.​preserves_pure {F : Type uType v} [applicative F] [is_lawful_applicative F] {G : Type uType w} [applicative G] [is_lawful_applicative G] (η : applicative_transformation F G) {α : Type u} (x : α) :

theorem applicative_transformation.​preserves_seq {F : Type uType v} [applicative F] [is_lawful_applicative F] {G : Type uType 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 uType v} [applicative F] [is_lawful_applicative F] {G : Type uType w} [applicative G] [is_lawful_applicative G] (η : applicative_transformation F G) {α β : Type u} (x : α → β) (y : F α) :
η (x <$> y) = x <$> η y

def sequence {t : Type uType u} {α : Type u} {f : Type uType u} [applicative f] [traversable t] :
t (f α)f (t α)

Equations
@[class]
structure is_lawful_traversable (t : Type uType u) [traversable t] :
Type (u+1)

Instances
@[instance]

Equations
@[instance]

Equations
def sum.​traverse {σ : Type u} {F : Type uType u} [applicative F] {α : Type u_1} {β : Type u} :
(α → F β)σ αF β)

Equations