Documentation

Mathlib.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→ 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
• IsLawfulTraversable - laws for a traversable functor
• ApplicativeTransformation - the notion of a natural transformation for applicative functors

Tags #

traversable iterator functor applicative

References #

structure ApplicativeTransformation (F : Type u → Type v) [inst : ] (G : Type u → Type w) [inst : ] :
Type (max(u+1)vw)
• The function on objects defined by an ApplicativeTransformation.

app : (α : Type u) → F αG α
• An ApplicativeTransformation preserves pure.

preserves_pure' : ∀ {α : Type u} (x : α), app α (pure x) = pure x
• An ApplicativeTransformation intertwines seq.

preserves_seq' : ∀ {α β : Type u} (x : F (αβ)) (y : F α), app β (Seq.seq x fun x => y) = Seq.seq (app (αβ) x) fun x => app α y

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

Instances For
instance ApplicativeTransformation.instCoeFunApplicativeTransformationForAllType (F : Type u → Type v) [inst : ] (G : Type u → Type w) [inst : ] :
CoeFun () fun x => {α : Type u} → F αG α
Equations
• = { coe := fun η {α} => }
@[simp]
theorem ApplicativeTransformation.app_eq_coe {F : Type u → Type v} [inst : ] {G : Type u → Type w} [inst : ] (η : ) :
η.app = fun {α} =>
@[simp]
theorem ApplicativeTransformation.coe_mk {F : Type u → Type v} [inst : ] {G : Type u → Type w} [inst : ] (f : (α : Type u) → F αG α) (pp : ∀ {α : Type u} (x : α), f α (pure x) = pure x) (ps : ∀ {α β : Type u} (x : F (αβ)) (y : F α), f β (Seq.seq x fun x => y) = Seq.seq (f (αβ) x) fun x => f α y) :
(fun {α} => ApplicativeTransformation.app { app := f, preserves_pure' := pp, preserves_seq' := ps } α) = f
theorem ApplicativeTransformation.congr_fun {F : Type u → Type v} [inst : ] {G : Type u → Type w} [inst : ] (η : ) (η' : ) (h : η = η') {α : Type u} (x : F α) :
(fun {α} => ) α x = (fun {α} => ) α x
theorem ApplicativeTransformation.congr_arg {F : Type u → Type v} [inst : ] {G : Type u → Type w} [inst : ] (η : ) {α : Type u} {x : F α} {y : F α} (h : x = y) :
(fun {α} => ) α x = (fun {α} => ) α y
theorem ApplicativeTransformation.coe_inj {F : Type u → Type v} [inst : ] {G : Type u → Type w} [inst : ] ⦃η : ⦃η' : (h : (fun {α} => ) = fun {α} => ) :
η = η'
theorem ApplicativeTransformation.ext {F : Type u → Type v} [inst : ] {G : Type u → Type w} [inst : ] ⦃η : ⦃η' : (h : ∀ (α : Type u) (x : F α), (fun {α} => ) α x = (fun {α} => ) α x) :
η = η'
theorem ApplicativeTransformation.ext_iff {F : Type u → Type v} [inst : ] {G : Type u → Type w} [inst : ] {η : } {η' : } :
η = η' ∀ (α : Type u) (x : F α), (fun {α} => ) α x = (fun {α} => ) α x
theorem ApplicativeTransformation.preserves_pure {F : Type u → Type v} [inst : ] {G : Type u → Type w} [inst : ] (η : ) {α : Type u} (x : α) :
(fun {α} => ) α (pure x) = pure x
theorem ApplicativeTransformation.preserves_seq {F : Type u → Type v} [inst : ] {G : Type u → Type w} [inst : ] (η : ) {α : Type u} {β : Type u} (x : F (αβ)) (y : F α) :
(fun {α} => ) β (Seq.seq x fun x => y) = Seq.seq ((fun {α} => ) (αβ) x) fun x => (fun {α} => ) α y
theorem ApplicativeTransformation.preserves_map {F : Type u → Type v} [inst : ] [inst : ] {G : Type u → Type w} [inst : ] [inst : ] (η : ) {α : Type u} {β : Type u} (x : αβ) (y : F α) :
(fun {α} => ) β (x <$> y) = x <$> (fun {α} => ) α y
theorem ApplicativeTransformation.preserves_map' {F : Type u → Type v} [inst : ] [inst : ] {G : Type u → Type w} [inst : ] [inst : ] (η : ) {α : Type u} {β : Type u} (x : αβ) :
(fun {α} => ) β = (fun {α} => ) α

The identity applicative transformation from an applicative functor to itself.

Equations
• One or more equations did not get rendered due to their size.
Equations
• ApplicativeTransformation.instInhabitedApplicativeTransformation = { default := ApplicativeTransformation.idTransformation }
def ApplicativeTransformation.comp {F : Type u → Type v} [inst : ] {G : Type u → Type w} [inst : ] {H : Type u → Type s} [inst : ] (η' : ) (η : ) :

The composition of applicative transformations.

Equations
• One or more equations did not get rendered due to their size.
@[simp]
theorem ApplicativeTransformation.comp_apply {F : Type u → Type v} [inst : ] {G : Type u → Type w} [inst : ] {H : Type u → Type s} [inst : ] (η' : ) (η : ) {α : Type u} (x : F α) :
(fun {α} => ) α x = (fun {α} => ) α ((fun {α} => ) α x)
theorem ApplicativeTransformation.comp_assoc {F : Type u → Type v} [inst : ] {G : Type u → Type w} [inst : ] {H : Type u → Type s} [inst : ] {I : Type u → Type t} [inst : ] (η'' : ) (η' : ) (η : ) :
@[simp]
theorem ApplicativeTransformation.comp_id {F : Type u → Type v} [inst : ] {G : Type u → Type w} [inst : ] (η : ) :
ApplicativeTransformation.comp η ApplicativeTransformation.idTransformation = η
@[simp]
theorem ApplicativeTransformation.id_comp {F : Type u → Type v} [inst : ] {G : Type u → Type w} [inst : ] (η : ) :
ApplicativeTransformation.comp ApplicativeTransformation.idTransformation η = η
class Traversable (t : Type u → Type u) extends :
Type (u+1)
• The function commuting a traversable functor t with an arbitrary applicative functor m.

traverse : {m : Type u → Type u} → [inst : ] → {α β : Type u} → (αm β) → t αm (t β)

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 β→ IO β, the function Functor.map f is List α → List (IO β)→ List (IO β), but traverse f is List α → IO (List β)→ IO (List β).

Instances
def sequence {t : Type u → Type u} {α : Type u} {f : Type u → Type u} [inst : ] [inst : ] :
t (f α)f (t α)

A traversable functor commutes with all applicative functors.

Equations
class IsLawfulTraversable (t : Type u → Type u) [inst : ] extends :
Type (u+1)
• traverse plays well with pure of the identity monad

id_traverse : ∀ {α : Type u} (x : t α), traverse pure x = x
• traverse plays well with composition of applicative functors.

comp_traverse : ∀ {F G : Type u → Type u} [inst : ] [inst_1 : ] [inst_2 : ] [inst_3 : ] {α β γ : Type u} (f : βF γ) (g : αG β) (x : t α), traverse (Functor.Comp.mk g) x = Functor.Comp.mk ( <$> traverse g x) • An axiom for traverse involving pure : β → Id β→ Id β. traverse_eq_map_id : ∀ {α β : Type u} (f : αβ) (x : t α), traverse (pure f) x = id.mk (f <$> x)
• The naturality axiom explaining how lawful traversable functors should play with lawful applicative functors.

naturality : ∀ {F G : Type u → Type u} [inst : ] [inst_1 : ] [inst_2 : ] [inst_3 : ] (η : ) {α β : Type u} (f : αF β) (x : t α), (fun {α} => ) (traverse f x) = traverse ((fun {α} => ) f) x

A traversable functor is lawful if its traverse satisfies a number of additional properties. It must send pure : α → Id α→ Id α to pure, 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 Equations Equations • One or more equations did not get rendered due to their size. Equations def Sum.traverse {σ : Type u} {F : Type u → Type u} [inst : ] {α : 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 • = match x with | => pure () | => Sum.inr <$> f x
instance instTraversableSum {σ : Type u} :
Equations
• instTraversableSum =