profunctor optics a categorical update
play

Profunctor optics, a categorical update (Extended abstract) Mario - PowerPoint PPT Presentation

Profunctor optics, a categorical update (Extended abstract) Mario Romn , Bryce Clarke, Fosco Loregian, Emily Pillmore, Derek Elkins, Bartosz Milewski and Jeremy Gibbons September 5, 2019 NWPT19 , T altech Motivation Part 1: Motivation


  1. Profunctor optics, a categorical update (Extended abstract) Mario Román , Bryce Clarke, Fosco Loregian, Emily Pillmore, Derek Elkins, Bartosz Milewski and Jeremy Gibbons September 5, 2019 NWPT’19 , T altech

  2. Motivation Part 1: Motivation

  3. Optics Optics are composable data accessors. They allow us to access and modify nested data structures. • Each family of optics encodes a data accessing pattern. • Lenses access subfields. • Prisms pattern match. • Traversals transform lists. • Two optics (of any two families!) can be directly composed.

  4. Lenses Definition (Oles, 1982) Lens ( A, S ) := ( S → A ) × ( S × A → S ) .

  5. Lenses Definition (Oles, 1982) �� � � �� A S := ( S → A ) × ( S × B → T ) . Lens , B T

  6. Lenses

  7. Prisms Definition �� � � �� A S Prism , = ( S → T + A ) × ( B → T ) . B T

  8. Prisms Adapted from Penner’s @opticsbyexample

  9. Traversals Definition �� � � �� A S � n A n × ( B n → T ) � � Traversal , = S → . B T

  10. The problem of modularity • How to compose any two optics? • Even from different families of optics (lens+prism+traversal). • Simple but tedious code. • Every pair of families needs special attention.

  11. Profunctor optics

  12. Profunctor optics A Tambara module is a profunctor endowed with a natural transformation p ( A, B ) → p ( C ⊗ A, C ⊗ B ) subject to some conditions. Every optic can be written as a function polymorphic on these Tambara modules. Why is this?

  13. Outline • Existential optics: a definition of optic. • Profunctor optics: on optics as parametric functions. • Composing optics: on how composition works. • Case study: on how to invent an optic. • Further work: and implementations.

  14. Preliminaries Part 2: Existential optics

  15. Parametricity as ends • W e write ∀ to denote polymorphism ( actually, ends ). • We write ∃ to denote existential types ( coends ). Parametricity ( Yoneda lemma ) implies the following rules. • ∀ X. (( A → X ) → GX ) ∼ = GA • ∃ X. (( X → A ) × FX ) ∼ = FA Continuity implies the following. • (( ∃ C.FC ) → D ) ∼ = ( ∀ C.FC → D ) • ( D → ( ∀ C.PC )) ∼ = ( ∀ C.D → PC ) These are rules from (co)end calculus . See Loregian’s "Coend calculus".

  16. Parametricity as ends • W e write ∀ to denote polymorphism ( actually, ends ). • We write ∃ to denote existential types ( coends ). Parametricity ( Yoneda lemma ) implies the following rules. X = A • ∀ X. (( A → X ) → GX ) ∼ = GA X = A • ∃ X. (( X → A ) × FX ) ∼ = FA Continuity implies the following. • (( ∃ C.FC ) → D ) ∼ = ( ∀ C.FC → D ) • ( D → ( ∀ C.PC )) ∼ = ( ∀ C.D → PC ) These are rules from (co)end calculus . See Loregian’s "Coend calculus".

  17. A definition of "optic" Definition (Milewski, Boisseau/Gibbons, Riley, simplified) Fix a monoidal class of endofunctors M (that is, a constraint satisfied by the identity and closed under composition, such as Applicative or Traversable ). An optic from ( S, T ) with focus on ( A, B ) is an element of the following type. �� � � �� A S Optic , := ∃ M ∈ M . ( S → MA ) × ( MB → T ) . B T Intuition: The optic splits into some focus A and some context M . We cannot access that context, but we can use it to update.

  18. Lenses are optics Proposition (from Milewski, 2017) Lenses are optics for the product. Proof. ( Product ) ∃ C. ( S → C × A ) × ( C × B → T ) ∼ = ∃ C. ( S → C ) × ( S → A ) × ( C × B → T ) ∼ ( Yoneda ) = ( S → A ) × ( S × B → T )

  19. Lenses are optics Proposition (from Milewski, 2017) Lenses are optics for the product. Proof. ( Product ) ∃ C. ( S → C × A ) × ( C × B → T ) ∼ = S = C ( Yoneda ) × ( S → A ) × ( C × B → T ) ∼ ∃ C. ( S → C ) = ( S → A ) × ( S × B → T )

  20. Prisms are optics Proposition (Milewski, 2017) Prisms are optics for the coproduct. Proof. ( Coproduct ) ∃ M. ( S → M + A ) × ( M + B → T ) ∼ = ∃ M. ( S → M + A ) × ( M → T ) × ( B → T ) ∼ ( Yoneda ) = ( S → T + A ) × ( B → T )

  21. Prisms are optics Proposition (Milewski, 2017) Prisms are optics for the coproduct. Proof. ( Coproduct ) ∃ M. ( S → M + A ) × ( M + B → T ) ∼ = M = T ( Yoneda ) × ( B → T ) ∼ ∃ M. ( S → M + A ) × ( M → T ) = ( S → T + A ) × ( B → T )

  22. Traversals are optics Proposition Traversals are optics for the action of polynomial functors � n C n × � n . That is, � n C n × A n � ��� n C n × B n � � � n A n × ( B n → T ) � � ∼ � ∃ C. S → × → T = S → .

  23. Traversals are optics: proof Again by the Yoneda lemma, this time for functors C : N → Sets . � n C n × A n � �� � � ( cocontinuity ) ∼ ∃ C. S → n C n × B n , T × = � n C n × A n � ( C n × B n → T ) ∼ ( prod/exp adjunction ) � � ∃ C. S → × = n � n C n × A n � ( C n → ( B n → T )) ∼ ( natural transformation ) � � ∃ C. S → × = n � n C n × A n � � C � , ( B � → T ) � � ( Yoneda lemma ) ∼ × Nat ∃ C. S, = n A n × ( B n → T ) � S → Programming libraries use traversable functors to describe traversals. Polynomials are related to these traversable functors by the work of Jaskelioff and O’Connor.

  24. Traversals are optics: proof Again by the Yoneda lemma, this time for functors C : N → Sets . � n C n × A n � �� � � ( cocontinuity ) ∼ ∃ C. S → n C n × B n , T × = � n C n × A n � ( C n × B n → T ) ∼ ( prod/exp adjunction ) � � ∃ C. S → × = n � n C n × A n � ( C n → ( B n → T )) ∼ ( natural transformation ) � � ∃ C. S → × = n � C = B � → T � n C n × A n � � � C � , ( B � → T ) ( Yoneda lemma ) ∼ × Nat ∃ C. S, = n A n × ( B n → T ) � S → Programming libraries use traversable functors to describe traversals. Polynomials are related to these traversable functors by the work of Jaskelioff and O’Connor.

  25. Unification of optics All the usual optics are of this form. Some new ones arise naturally. Name Concrete Action Adapter Identity ( S → A ) × ( B → T ) Lens Product ( S → A ) × ( B × S → T ) Prism Coproduct ( S → T + A ) × ( B → T ) Grate Exponential (( S → A ) → B ) → T Affine Traversal Product and coproduct S → T + A × ( B → T ) Glass Product and exponential (( S → A ) → B ) → S → T Traversal S → Σ n.A n × ( B n → T ) Polynomials Setter Any functor ( A → B ) → ( S → T )

  26. Unification of optics

  27. Profunctor representation Part 3: the Profunctor representation theorem

  28. Tambara modules Definition (from Pastro/Street) A Tambara module is a profunctor P together with a family of morphisms satisfying some coherence conditions. P ( A, B ) → P ( MA, MB ) , M ∈ M . Pastro and Street showed they are algebras for a monad. Ψ Q ( X, Y ) = ∃ M, A, B. Q ( A, B ) × ( MA → X ) × ( Y → MB ) We call Tmb to the Eilenberg-Moore category for the monad.

  29. Profunctor representation Theorem (Boisseau/Gibbons) Optics are functions parametric over Tambara modules. Optic (( A, B ) , ( S, T )) ∼ = ∀ P ∈ Tmb . P ( A, B ) → P ( S, T )

  30. Composition of optics Part 4: Composition of optics

  31. Composing optics via coproducts When we compose two optics in Haskell, the compiler joins the constraints. Is this an optic according to the definition? If so, for which action? • In other words, P has a bialgebra structure. • This is the same as P having algebra structure for the coproduct monad (Kelly, Adamek). • We prove the coproduct monad is the monad for the coproduct action.

  32. Composing optics via coproducts When we compose two optics in Haskell, the compiler joins the constraints. Is this an optic according to the definition? If so, for which action? • In other words, P has a bialgebra structure. • This is the same as P having algebra structure for the coproduct monad (Kelly, Adamek). • We prove the coproduct monad is the monad for the coproduct action.

  33. Composing optics via distributive laws • The folklore is that lenses and prisms compose into the optic for the action of a single sum and product. • Haskell actually composes lenses and prisms into the optic for the action of multiple sums and products. In which sense is folklore right?

  34. Composing optics via distributive laws • The folklore is that lenses and prisms compose into the optic for the action of a single sum and product. • Haskell actually composes lenses and prisms into the optic for the action of multiple sums and products. In which sense is folklore right? We show that the fact that ( × ) distributes over (+) induces a distributive law between the Pastro-Street monads.

  35. Composing optics via distributive laws Monads can be joined in two ways. • Taking their coproduct monad S ⊕ T ; and • using a distributive law ST ⇒ TS to induce a monad structure on the composition TS .

Recommend


More recommend