profunctor optics a categorical update
play

Profunctor optics: a categorical update Mario Romn , Bryce Clarke, - PowerPoint PPT Presentation

Profunctor optics: a categorical update Mario Romn , Bryce Clarke, Fosco Loregian, Emily Pillmore, Derek Elkins, Bartosz Milewski and Jeremy Gibbons September 5, 2019 SYCO 5, University of Birmingham Motivation Part 1: Motivation Lenses


  1. Profunctor optics: a categorical update Mario Román , Bryce Clarke, Fosco Loregian, Emily Pillmore, Derek Elkins, Bartosz Milewski and Jeremy Gibbons September 5, 2019 SYCO 5, University of Birmingham

  2. Motivation Part 1: Motivation

  3. Lenses Definition (Oles, 1982) �� � � �� a s , = Sets ( s, a ) × Sets ( s × b, t ) . Lens b t ( example ) view: Postal → Street update: Postal × Street → Postal

  4. Prisms (alternatives) Definition �� � � �� a s Prism , = Sets ( s, t + a ) × Sets ( b, t ) . b t ( example ) match: String → String + Postal build: Postal → String

  5. Traversals (and multiple foci) Definition �� � � �� s a � n a n × ( b n → t ) � � Traversal , = Sets s, . t b Email n × ( Email n → MailList ) � ( example ) extract: MailList → n ∈ N

  6. This is not modular How to compose two lenses? How to compose a Prism with a Lens? � � � � � � s a x m,b v,u t prism b lens y

  7. This is not modular How to compose two lenses? How to compose a Prism with a Lens? � � � � � � s a x m,b v,u t prism b lens y Every case (Prism+Lens, Lens+Prism, Traversal+Prism+Other...) needs special attention. For instance, a lens and a prism v ∈ Sets ( s, t + a ) m ∈ Sets ( a, x ) u ∈ Sets ( b, t ) b ∈ Sets ( a × y, b ) can be composed into the following morphism, which is neither a lens nor a prism. m ◦ [id t , v × Λ( b ◦ u )] ∈ Sets ( s, t + x × ( y → t )) .

  8. This is not modular (in code) -- Given a lens and a prism, viewStreet :: Postal -> Street updateStreet :: Postal -> Street -> Postal matchAddress :: String -> Either String Postal buildAddress :: Postal -> String -- the composition is neither a lens nor a prism. parseStreet :: String -> Either String ( Street , Street -> Postal ) parseStreet s = case matchAddress s of Left addr -> Left addr Right post -> Right (viewStreet post, updateStreet post)

  9. Profunctor optics Perhaps surprisingly, some optics are equivalent to parametric functions over profunctors. • Lenses are parametric functions. ∼ Sets ( s, a ) × Sets ( s × b, t ) ∀ p ∈ Tmb( × ) .p ( a, b ) → p ( s, t ) = • Prisms are parametric functions. ∼ Sets ( a, a + x ) × Sets ( y, b ) ∀ p ∈ Tmb(+) .p ( x, y ) → p ( a, b ) = Where p ∈ Tmb( ⊗ ) is called a Tambara module ; this means we have a natural transformation p ( a, b ) → p ( c ⊗ a, c ⊗ b ) subject to some conditions

  10. Profunctor optics Perhaps surprisingly, some optics are equivalent to parametric functions over profunctors. • Lenses are parametric functions. ∼ Sets ( s, a ) × Sets ( s × b, t ) ∀ p ∈ Tmb( × ) .p ( a, b ) → p ( s, t ) = • Prisms are parametric functions. ∼ Sets ( a, a + x ) × Sets ( y, b ) ∀ p ∈ Tmb(+) .p ( x, y ) → p ( a, b ) = Where p ∈ Tmb( ⊗ ) is called a Tambara module ; this means we have a natural transformation p ( a, b ) → p ( c ⊗ a, c ⊗ b ) subject to some conditions This solves composition Now composition of optics is just function composition . From p ( a, b ) → p ( s, t ) and p ( x, y ) → p ( a, b ) we can get p ( x, y ) → p ( s, t ) .

  11. An example in Haskell -- Haskell code -- let address = "15 Parks Rd, OX1 3QD, Oxford" address^.postal -- Street: 15 Parks Rd -- Code: OX1 3QD -- City: Oxford address^.postal.street -- "15 Parks Rd" address^.postal.street <~ "7 Banbury Rd" -- "7 Banbury Rd, OX1 3QD, Oxford" -------------------

  12. 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.

  13. Preliminaries Part 2: Existential optics

  14. (co)Ends Ends and Coends over a profunctor p : C op × C → Sets are special kinds of (co)limits , (co)equalizing its right and left mapping. p (id ,f ) � � � p ( x, x ) p ( x, x ) p ( a, b ) p ( f, id) x ∈ C x ∈ C f : a → b � x ∈ C p (id ,f ) � � p ( a, b ) p ( x, x ) p ( x, x ) p ( f, id) f : b → a x ∈ C Intuitively, a natural universal quantifier (ends) and existential quantifier (coends). Fosco Loregian. “This is the (co)end, my only (co)friend”. In: arXiv preprint arXiv:1501.02503 (2015).

  15. (Co)end calculus Natural transformations can be rewritten in terms of ends. For any F, G : C → D , � Nat( F, G ) = D ( Fx, Gx ) . x ∈ C We can compute (co)ends using the Yoneda lemma. � Sets ( C ( x, a ) , Gx ) ∼ = Ga, x ∈ C � x ∈ C Fx × C ( a, x ) ∼ = Fa. Continuity of the hom functor takes the following form. �� c ∈ C � � ∼ D p ( c, c ) , d = D ( p ( c, c ) , d ) , c ∈ C � � � � ∼ p ( c, c ) D ( d, p ( c, c )) . D d, = c ∈ C c ∈ C

  16. (Co)end calculus Natural transformations can be rewritten in terms of ends. For any F, G : C → D , � Nat( F, G ) = D ( Fx, Gx ) . x ∈ C We can compute (co)ends using the Yoneda lemma. � x = a , Gx ) ∼ Sets ( C ( x, a ) = Ga, x ∈ C � x ∈ C x = a ∼ Fx × C ( a, x ) = Fa. Continuity of the hom functor takes the following form. �� c ∈ C � � ∼ D p ( c, c ) , d = D ( p ( c, c ) , d ) , c ∈ C � � � � ∼ p ( c, c ) D ( d, p ( c, c )) . D d, = c ∈ C c ∈ C

  17. A definition of "optic" Definition (Milewski, Boisseau/Gibbons, Riley, generalized) Fix a monoidal category M with a strong monoidal functor ( ): M → [ C , C ] . Let s, t, a, b ∈ C ; an optic from ( s, t ) with focus on ( a, b ) is an element of the following set. � m ∈ M �� � � �� a s Optic , = C ( s, ma ) × C ( 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. � c ∈ Sets ( Product ) Sets ( s, c × a ) × Sets ( c × b, t ) ∼ = � c ∈ Sets Sets ( s, c ) × Sets ( s, a ) × Sets ( c × b, t ) ∼ ( Yoneda ) = Sets ( s, a ) × Sets ( s × b, t )

  19. Lenses are optics Proposition (from Milewski, 2017) Lenses are optics for the product.                   ∼ =                     Proof. � c ∈ Sets ( Product ) Sets ( s, c × a ) × Sets ( c × b, t ) ∼ = � c ∈ Sets c = s × Sets ( s, a ) × Sets ( c × b, t ) ∼ ( Yoneda ) Sets ( s, c ) = Sets ( s, a ) × Sets ( s × b, t )

  20. Prisms are optics Proposition (Milewski, 2017) Dually, prisms are optics for the coproduct.                                   ∼ =                                       Proof. � m ∈ Sets ( Coproduct ) Sets ( s, m + a ) × Sets ( m + b, t ) ∼ = � m ∈ Sets ( Yoneda ) Sets ( s, m + a ) × Sets ( m, t ) × Sets ( b, t ) ∼ = Sets ( s, t + a ) × Sets ( b, t )

  21. Prisms are optics Proposition (Milewski, 2017) Dually, prisms are optics for the coproduct.                                   ∼ =                                       Proof. � m ∈ Sets ( Coproduct ) Sets ( s, m + a ) × Sets ( m + b, t ) ∼ = � m ∈ Sets m = t ( Yoneda ) × Sets ( b, t ) ∼ Sets ( s, m + a ) × Sets ( m, t ) = Sets ( s, t + a ) × Sets ( b, t )

  22. Traversals are optics Theorem Traversals are optics for the action of polynomial functors � n c n × � n .                           ∼ =                             That is, � c = Sets ( s, Σ n a n × ( b n → t )) . Sets ( s, Σ n ( c n × a n )) × Sets (Σ n ( c n × b n ) , t ) ∼

  23. Traversals are optics: proof Again by the Yoneda lemma, this time for functors c : N → Sets . � c � n c n × a n � �� � � ( cocontinuity ) n c n × b n , t ∼ Sets s, × Sets = � c � n c n × a n � � � Sets ( c n × b n , t ) ∼ ( prod/exp adjunction ) Sets s, × = n � c � n c n × a n � Sets ( c n , b n → t ) ∼ � � ( natural transf. as an end ) s, Sets × = n � c � c � , b � → t � � ( Yoneda lemma ) n c n × a n ) × [ N , Sets ] ∼ Sets ( s, = � n a n × ( b n → t ) � � Sets s, Programming libraries use traversable functors to describe traversals. Polynomials are related to these traversable functors by a result of Jaskelioff/O’Connor.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend