The areas / V33. Data structures and computation
Mathlib.Control.Traversable.Basic
46 declarations. Mathlib docs · Source
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
Traversabletype class - exposes thetraversefunctionsequence- based ontraverse, turns a collection of effects into an effect returning a collectionLawfulTraversable- laws for a traversable functorApplicativeTransformation- the notion of a natural transformation for applicative functors
Tags
traversable iterator functor applicative
References
- "Applicative Programming with Effects", by Conor McBride and Ross Paterson, Journal of Functional Programming 18:1 (2008) 1-13, online at http://www.soi.city.ac.uk/~ross/papers/Applicative.html
- "The Essence of the Iterator Pattern", by Jeremy Gibbons and Bruno Oliveira, in Mathematically-Structured Functional Programming, 2006, online at http://web.comlab.ox.ac.uk/oucl/work/jeremy.gibbons/publications/#iterator
- "An Investigation of the Laws of Traversals", by Mauro Jaskelioff and Ondrej Rypacek, in Mathematically-Structured Functional Programming, 2012, online at http://arxiv.org/pdf/1202.2919
theorem ApplicativeTransformation.mk.inj E1 #
∀ {F : Type u → Type v} {inst : Applicative F} {G : Type u → Type w} {inst_1 : Applicative G}
{app : (α : Type u) → F α → G α} {preserves_pure' : ∀ {α : Type u} (x : α), app α (pure x) = pure x}
{preserves_seq' : ∀ {α β : Type u} (x : F (α → β)) (y : F α), app β (x <*> y) = app (α → β) x <*> app α y}
{app_1 : (α : Type u) → F α → G α} {preserves_pure'_1 : ∀ {α : Type u} (x : α), app_1 α (pure x) = pure x}
{preserves_seq'_1 : ∀ {α β : Type u} (x : F (α → β)) (y : F α), app_1 β (x <*> y) = app_1 (α → β) x <*> app_1 α y},
{ app := app, preserves_pure' := preserves_pure', preserves_seq' := preserves_seq' } =
{ app := app_1, preserves_pure' := preserves_pure'_1, preserves_seq' := preserves_seq'_1 } →
app = app_1
def Traversable.ctorIdx E1 #
{t : Type u → Type u} → Traversable t → ℕ
def ApplicativeTransformation.ctorIdx E1 #
{F : Type u → Type v} →
{inst : Applicative F} → {G : Type u → Type w} → {inst_1 : Applicative G} → ApplicativeTransformation F G → ℕ
theorem ApplicativeTransformation.mk.injEq E0 #
∀ {F : Type u → Type v} [inst : Applicative F] {G : Type u → Type w} [inst_1 : Applicative G]
(app : (α : Type u) → F α → G α) (preserves_pure' : ∀ {α : Type u} (x : α), app α (pure x) = pure x)
(preserves_seq' : ∀ {α β : Type u} (x : F (α → β)) (y : F α), app β (x <*> y) = app (α → β) x <*> app α y)
(app_1 : (α : Type u) → F α → G α) (preserves_pure'_1 : ∀ {α : Type u} (x : α), app_1 α (pure x) = pure x)
(preserves_seq'_1 : ∀ {α β : Type u} (x : F (α → β)) (y : F α), app_1 β (x <*> y) = app_1 (α → β) x <*> app_1 α y),
({ app := app, preserves_pure' := preserves_pure', preserves_seq' := preserves_seq' } =
{ app := app_1, preserves_pure' := preserves_pure'_1, preserves_seq' := preserves_seq'_1 }) =
(app = app_1)
structure ApplicativeTransformation E3 #
(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 Pure.pure and
Functor.map (<*>) operations. See
ApplicativeTransformation.preserves_map for naturality.
ctor ApplicativeTransformation.mk E0 #
{F : Type u → Type v} →
[inst : Applicative F] →
{G : Type u → Type w} →
[inst_1 : Applicative G] →
(app : (α : Type u) → F α → G α) →
(∀ {α : Type u} (x : α), app α (pure x) = pure x) →
(∀ {α β : Type u} (x : F (α → β)) (y : F α), app β (x <*> y) = app (α → β) x <*> app α y) →
ApplicativeTransformation F G
abbrev ApplicativeTransformation.app E1 #
{F : Type u → Type v} →
[inst : Applicative F] →
{G : Type u → Type w} → [inst_1 : Applicative G] → ApplicativeTransformation F G → (α : Type u) → F α → G α
The function on objects defined by an ApplicativeTransformation.
theorem ApplicativeTransformation.preserves_pure' E1 #
∀ {F : Type u → Type v} [inst : Applicative F] {G : Type u → Type w} [inst_1 : Applicative G]
(self : ApplicativeTransformation F G) {α : Type u} (x : α), self.app α (pure x) = pure x
An ApplicativeTransformation preserves pure.
theorem ApplicativeTransformation.preserves_seq' E1 #
∀ {F : Type u → Type v} [inst : Applicative F] {G : Type u → Type w} [inst_1 : Applicative G]
(self : ApplicativeTransformation F G) {α β : Type u} (x : F (α → β)) (y : F α),
self.app β (x <*> y) = self.app (α → β) x <*> self.app α y
An ApplicativeTransformation intertwines seq.
instance ApplicativeTransformation.instCoeFunForallForall E0 #
(F : Type u → Type v) →
[inst : Applicative F] →
(G : Type u → Type w) →
[inst_1 : Applicative G] → CoeFun (ApplicativeTransformation F G) fun x ↦ {α : Type u} → F α → G α
theorem ApplicativeTransformation.app_eq_coe E0 #
∀ {F : Type u → Type v} [inst : Applicative F] {G : Type u → Type w} [inst_1 : Applicative G]
(η : ApplicativeTransformation F G), η.app = fun {α} ↦ η.app α
theorem ApplicativeTransformation.coe_mk E0 #
∀ {F : Type u → Type v} [inst : Applicative F] {G : Type u → Type w} [inst_1 : Applicative G]
(f : (α : Type u) → F α → G α) (pp : ∀ {α : Type u} (x : α), f α (pure x) = pure x)
(ps : ∀ {α β : Type u} (x : F (α → β)) (y : F α), f β (x <*> y) = f (α → β) x <*> f α y),
(fun {α} ↦ { app := f, preserves_pure' := pp, preserves_seq' := ps }.app α) = f
theorem ApplicativeTransformation.congr_fun E1 #
∀ {F : Type u → Type v} [inst : Applicative F] {G : Type u → Type w} [inst_1 : Applicative G]
(η η' : ApplicativeTransformation F G),
η = η' → ∀ {α : Type u} (x : F α), (fun {α} ↦ η.app α) x = (fun {α} ↦ η'.app α) x
theorem ApplicativeTransformation.congr_arg E1 #
∀ {F : Type u → Type v} [inst : Applicative F] {G : Type u → Type w} [inst_1 : Applicative G]
(η : ApplicativeTransformation F G) {α : Type u} {x y : F α}, x = y → (fun {α} ↦ η.app α) x = (fun {α} ↦ η.app α) y
theorem ApplicativeTransformation.coe_inj E0 #
∀ {F : Type u → Type v} [inst : Applicative F] {G : Type u → Type w} [inst_1 : Applicative G]
⦃η η' : ApplicativeTransformation F G⦄, ((fun {α} ↦ η.app α) = fun {α} ↦ η'.app α) → η = η'
theorem ApplicativeTransformation.ext_iff E0 #
∀ {F : Type u → Type v} [inst : Applicative F] {G : Type u → Type w} [inst_1 : Applicative G]
{η η' : ApplicativeTransformation F G},
η = η' ↔ ∀ (α : Type u) (x : F α), (fun {α} ↦ η.app α) x = (fun {α} ↦ η'.app α) x
theorem ApplicativeTransformation.ext E1 #
∀ {F : Type u → Type v} [inst : Applicative F] {G : Type u → Type w} [inst_1 : Applicative G]
⦃η η' : ApplicativeTransformation F G⦄,
(∀ (α : Type u) (x : F α), (fun {α} ↦ η.app α) x = (fun {α} ↦ η'.app α) x) → η = η'
theorem ApplicativeTransformation.preserves_pure E1 #
∀ {F : Type u → Type v} [inst : Applicative F] {G : Type u → Type w} [inst_1 : Applicative G]
(η : ApplicativeTransformation F G) {α : Type u} (x : α), (fun {α} ↦ η.app α) (pure x) = pure x
theorem ApplicativeTransformation.preserves_seq E1 #
∀ {F : Type u → Type v} [inst : Applicative F] {G : Type u → Type w} [inst_1 : Applicative G]
(η : ApplicativeTransformation F G) {α β : Type u} (x : F (α → β)) (y : F α),
(fun {α} ↦ η.app α) (x <*> y) = (fun {α} ↦ η.app α) x <*> (fun {α} ↦ η.app α) y
theorem ApplicativeTransformation.preserves_map E1 #
∀ {F : Type u → Type v} [inst : Applicative F] {G : Type u → Type w} [inst_1 : Applicative G]
(η : ApplicativeTransformation F G) [LawfulApplicative F] [LawfulApplicative G] {α β : Type u} (x : α → β) (y : F α),
(fun {α} ↦ η.app α) (x <$> y) = x <$> (fun {α} ↦ η.app α) y
theorem ApplicativeTransformation.preserves_map' E1 #
∀ {F : Type u → Type v} [inst : Applicative F] {G : Type u → Type w} [inst_1 : Applicative G]
(η : ApplicativeTransformation F G) [LawfulApplicative F] [LawfulApplicative G] {α β : Type u} (x : α → β),
(fun {α} ↦ η.app α) ∘ Functor.map x = Functor.map x ∘ fun {α} ↦ η.app α
def ApplicativeTransformation.idTransformation E1 #
{F : Type u → Type v} → [inst : Applicative F] → ApplicativeTransformation F F
The identity applicative transformation from an applicative functor to itself.
instance ApplicativeTransformation.instInhabited E0 #
{F : Type u → Type v} → [inst : Applicative F] → Inhabited (ApplicativeTransformation F F)
def ApplicativeTransformation.comp E1 #
{F : Type u → Type v} →
[inst : Applicative F] →
{G : Type u → Type w} →
[inst_1 : Applicative G] →
{H : Type u → Type s} →
[inst_2 : Applicative H] →
ApplicativeTransformation G H → ApplicativeTransformation F G → ApplicativeTransformation F H
The composition of applicative transformations.
theorem ApplicativeTransformation.comp_apply E0 #
∀ {F : Type u → Type v} [inst : Applicative F] {G : Type u → Type w} [inst_1 : Applicative G] {H : Type u → Type s}
[inst_2 : Applicative H] (η' : ApplicativeTransformation G H) (η : ApplicativeTransformation F G) {α : Type u}
(x : F α), (fun {α} ↦ (η'.comp η).app α) x = (fun {α} ↦ η'.app α) ((fun {α} ↦ η.app α) x)
theorem ApplicativeTransformation.comp_assoc E1 #
∀ {F : Type u → Type v} [inst : Applicative F] {G : Type u → Type w} [inst_1 : Applicative G] {H : Type u → Type s}
[inst_2 : Applicative H] {I : Type u → Type t} [inst_3 : Applicative I] (η'' : ApplicativeTransformation H I)
(η' : ApplicativeTransformation G H) (η : ApplicativeTransformation F G), (η''.comp η').comp η = η''.comp (η'.comp η)
theorem ApplicativeTransformation.comp_id E0 #
∀ {F : Type u → Type v} [inst : Applicative F] {G : Type u → Type w} [inst_1 : Applicative G]
(η : ApplicativeTransformation F G), η.comp ApplicativeTransformation.idTransformation = η
theorem ApplicativeTransformation.id_comp E0 #
∀ {F : Type u → Type v} [inst : Applicative F] {G : Type u → Type w} [inst_1 : Applicative G]
(η : ApplicativeTransformation F G), ApplicativeTransformation.idTransformation.comp η = η
class Traversable E3 #
(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 β).
instance Traversable.toFunctor E0 #
{t : Type u → Type u} → [self : Traversable t] → Functor t
ctor Traversable.mk E0 #
{t : Type u → Type u} →
[toFunctor : Functor t] →
({m : Type u → Type u} → [Applicative m] → {α β : Type u} → (α → m β) → t α → m (t β)) → Traversable t
abbrev Traversable.traverse E3 #
{t : Type u → Type u} →
[self : Traversable t] → {m : Type u → Type u} → [Applicative m] → {α β : Type u} → (α → m β) → t α → m (t β)
The function commuting a traversable functor t with an arbitrary applicative functor m.
def sequence E3 #
{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.
class LawfulTraversable E3 #
(t : Type u → Type u) → [Traversable t] → Prop
A traversable functor is lawful if its traverse satisfies a
number of additional properties. It must send pure : α → Id α to pure,
send the composition of applicative functors to the composition of the
traverse of each, send each function f to fun x ↦ f <$> x, and
satisfy a naturality condition with respect to applicative
transformations.
ctor LawfulTraversable.mk E0 #
∀ {t : Type u → Type u} [inst : Traversable t] [toLawfulFunctor : LawfulFunctor t],
(∀ {α : Type u} (x : t α), traverse pure x = pure x) →
(∀ {F G : Type u → Type u} [inst_1 : Applicative F] [inst_2 : Applicative G] [LawfulApplicative F]
[LawfulApplicative G] {α β γ : Type u} (f : β → F γ) (g : α → G β) (x : t α),
traverse (Functor.Comp.mk ∘ Functor.map f ∘ g) x = Functor.Comp.mk (traverse f <$> traverse g x)) →
(∀ {α β : Type u} (f : α → β) (x : t α), traverse (pure ∘ f) x = pure (f <$> x)) →
(∀ {F G : Type u → Type u} [inst_1 : Applicative F] [inst_2 : Applicative G] [LawfulApplicative F]
[LawfulApplicative G] (η : ApplicativeTransformation F G) {α β : Type u} (f : α → F β) (x : t α),
(fun {α} ↦ η.app α) (traverse f x) = traverse ((fun {α} ↦ η.app α) ∘ f) x) →
LawfulTraversable t
instance LawfulTraversable.toLawfulFunctor E0 #
∀ {t : Type u → Type u} {inst : Traversable t} [self : LawfulTraversable t], LawfulFunctor t
theorem LawfulTraversable.id_traverse E0 #
∀ {t : Type u → Type u} {inst : Traversable t} [self : LawfulTraversable t] {α : Type u} (x : t α),
traverse pure x = pure x
traverse plays well with pure of the identity monad
theorem LawfulTraversable.comp_traverse E1 #
∀ {t : Type u → Type u} {inst : Traversable t} [self : LawfulTraversable t] {F G : Type u → Type u}
[inst_1 : Applicative F] [inst_2 : Applicative G] [LawfulApplicative F] [LawfulApplicative G] {α β γ : Type u}
(f : β → F γ) (g : α → G β) (x : t α),
traverse (Functor.Comp.mk ∘ Functor.map f ∘ g) x = Functor.Comp.mk (traverse f <$> traverse g x)
traverse plays well with composition of applicative functors.
theorem LawfulTraversable.traverse_eq_map_id E1 #
∀ {t : Type u → Type u} {inst : Traversable t} [self : LawfulTraversable t] {α β : Type u} (f : α → β) (x : t α),
traverse (pure ∘ f) x = pure (f <$> x)
An axiom for traverse involving pure : β → Id β.
theorem LawfulTraversable.naturality E1 #
∀ {t : Type u → Type u} {inst : Traversable t} [self : LawfulTraversable t] {F G : Type u → Type u}
[inst_1 : Applicative F] [inst_2 : Applicative G] [LawfulApplicative F] [LawfulApplicative G]
(η : ApplicativeTransformation F G) {α β : Type u} (f : α → F β) (x : t α),
(fun {α} ↦ η.app α) (traverse f x) = traverse ((fun {α} ↦ η.app α) ∘ f) x
The naturality axiom explaining how lawful traversable functors should play with lawful applicative functors.
instance instTraversableId E0 #
Traversable Id
instance instLawfulTraversableId E0 #
LawfulTraversable Id
instance instTraversableOption E0 #
Traversable Option
instance instTraversableList E0 #
Traversable List
def Sum.traverse E3 #
{σ : Type u} → {F : Type u → Type u} → [Applicative F] → {α : Type u_1} → {β : Type u} → (α → 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 σ ⊕ -.
instance instTraversableSum E0 #
{σ : Type u} → Traversable (Sum σ)