profunctor optics and the yoneda lemma ... profunctor optics and the yoneda lemma 4 the yoneda...

Download Profunctor Optics and the Yoneda Lemma ... Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma,

Post on 18-Apr-2020

0 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Profunctor Optics and the

    Yoneda Lemma

    Jeremy Gibbons

    joint work with Guillaume Boisseau

    WG2.1#77, July 2018

  • Profunctor Optics and the Yoneda Lemma 2

    1. Nobuo Yoneda

    • 1930–1996

    • Professor of Theoretical Foundations of Information Science at University of Tokyo

    • member of IFIP WG2.1, contributor to discussions about Algol 68, major role in Algol N

    • but primarily an algebraist

  • Profunctor Optics and the Yoneda Lemma 3

    2. The Yoneda Lemma

    Formally,

    For C a locally small category, C;SetCA;�; F ’ FA, naturally in A 2 C and F 2 C;Set.

    From left to right, take nt � : CA;� :! F to �AidA 2 FA. From right to left, take element x 2 FA to nt � such that �Bf Ff x. As a special case, the Yoneda Embedding:

    The functor Y : Cop ! C; Set is full and faithful, and injective on objects.

    C;SetCA;�;CB;� ’ CB;A

    Quite fearsomely terse!

  • Profunctor Optics and the Yoneda Lemma 4

    The Yoneda Lemma, philosophically

    • roughly, ‘a thing is determined by its relationships with other things’

    • you can tell a lot about a person by the company they keep

    • in Japanese, (‘ningen’, human being) constructed from (‘nin’, person) and (‘gen’, between),

    • in Euclidean geometry, a point ‘is’ just the lines that meet there

    • in poetry, c’est l’exécution du poème qui est le poème (Valéry)

    • in music, die Idee der Interpretation gehört zur Musik selber und ist ihr nicht akzidentiell (Adorno)

    • in sculpture, need to see a work from all angles in order to understand it (Mazzola)

  • Profunctor Optics and the Yoneda Lemma 5

    The Yoneda Lemma, computationally

    Approximate category Set by Haskell:

    • objects A are types, arrows f are functions • functors are represented by the type class Functor • natural transformations are polymorphic functions.

    Then Yoneda representation Yo f a ’ f a of functorial datatypes:

    data Yo f a Yo funYo ::8x : a ! x! f xg fromYo :: Yo f a ! f a fromYo y unYo y id -- apply to identity toYo :: Functor f ) f a ! Yo f a toYo x Yo �h! fmap h x -- use functorial action

    Also kind of dual, ‘co-Yoneda’: 9x : x ! a; f x ’ f a for functor f .

  • Profunctor Optics and the Yoneda Lemma 6

    Co-Yoneda

    For functor F and object A:

    FA ’ 8X : A! X ! FX

    Now consider one half of this iso:

    8A : FA! 8X : A! X ! FX ’ 8A : 8X : FA! A! X ! FX ’ 8A : 8X : FA� A! X ! FX ’ 8X : 8A : FA� A! X ! FX ’ 8X : 9A : FA� A! X ! FX

    and indeed, for functor F and object X :

    9A : FA� A! X ’ FX

    But even for non-functor F , lhs is functorial.

  • Profunctor Optics and the Yoneda Lemma 7

    IoT toaster

    Signature of commands for remote interaction:

    data Command ::� ! � where Say :: String ! Command Toast :: Int ! Command Sense :: ! Command Int

    GADT, but not a functor; so no free monad.

    Co-Yoneda trick to the rescue:

    data Action a 9r : A Command r; r ! a

  • Profunctor Optics and the Yoneda Lemma 8

    The Yoneda Lemma, familiarly (1)

    Proof by indirect inequality or indirect order

    b 6 aa 8c : a 6 c) b 6 c

    is the Yoneda Embedding in category Pre6:

    Pre6;SetPre6a;�;Pre6b;� ’ Pre6b;�a Pre6b;a

    • homset PreA;6b;a is a ‘thin set’: singleton or empty

    • homfunctor PreA;6a;� takes c 2 A to singleton set when a 6 c, empty otherwise

    • nt � : PreA;6a;� :! PreA;6b;� is function family �c : PreA;6a; c! PreA;6b; c

    • so �c is a witness that if PreA;6a; c 6� ; then PreA;6b; c 6� ;

  • Profunctor Optics and the Yoneda Lemma 9

    The Yoneda Lemma, familiarly (2)

    • any functor F preserves isos:

    Ff � Fg id a Ff � g Fid( f � g id

    • full and faithful functor also reflects isos

    • Yoneda functor C�; is full and faithful; so

    CB;� ’ CA;�a A ’ Ba C�;A ’ C�;B

    in particular for C Pre6, the rule of indirect equality:

    b aa 8c : a 6 ca b 6 c

  • Profunctor Optics and the Yoneda Lemma 10

    The Yoneda Lemma, familiarly (3)

    Cayley’s Theorem

    every group G is isomorphic to a subgroup of the symmetric group acting on G

    and similarly

    every monoid is isomorphic to a monoid of endomorphisms

    M ;�; e ’ fm� jm 2 Mg; �; id

    and similarly,

    every poset isomorphic to a poset of principal ideals

  • Profunctor Optics and the Yoneda Lemma 11

    Monoid as category

    Monoid M ;�; e induces category M:

    • precisely one object �

    • arrows M�;� M

    • identity id� e

    • composition m � n m� n

  • Profunctor Optics and the Yoneda Lemma 12

    Yoneda embedding

    C; SetCA;�;CB;� ’ CB;A

    (left to right: apply to identity; right to left: functorial action).

    Specialize to

    M;SetM�;�;M�;� ’M�;� M

    We’ll show lhs equivalent to monoid

    �M; �; id

    where �M f�m jm 2 Mg.

  • Profunctor Optics and the Yoneda Lemma 13

    Homfunctor, natural transformations

    M has a single homset M�;�. Homfunctor M�;� is:

    M�;�� M�;� M M�;�m :� ! � m� : M ! M

    Nt � : M�;� :!M�;� is singleton family of arrows

    �� : M�;�!M�;� M ! M

    such that, for m :� ! �,

    �� �M�;m M�;m ��� a

    �� � m� m� ��� a

    8n : ��m� n m� ��n

  • Profunctor Optics and the Yoneda Lemma 14

    Bijection

    Let �p 2 �M. Then

    �p m� n

    m� n� p associativity

    m� n� p

    m� �p n

    ie �p a nt. Conversely, let � : M�;� :!M�;�; then:

    ��m �� m� e m���e

    ie �� ���e 2 �M. (Also need to check that transforms are homomorphisms, and inverses.)

  • Profunctor Optics and the Yoneda Lemma 15

    3. Optics: lenses and prisms

    • the view–update problem in databases

    • similarly in UI design, MVC architecture

    • lens combinators (Foster, Pierce)

    • compositional references (Kagawa, Oles)

    V

    V 0

    S

    S0

  • Profunctor Optics and the Yoneda Lemma 16

    Lenses

    A view onto a product type:

    data Lens a b s t Lens fview :: s ! a;

    update :: s � b ! t g

    eg projection from pair:

    fstLens :: Lens a� c b � c a b fstLens Lens vw up where

    vw a; c a up a; c;a0 a0; c

    B

    A

    T

    S view

    update

  • Profunctor Optics and the Yoneda Lemma 17

    Prisms

    A view onto a sum type:

    data Prism a b s t Prism fmatch :: s ! t a;

    build :: b ! t g

    eg onto optional value:

    the :: Prism Maybe a Maybe b a b the Prism mt bd where

    mt Just a Right a mt Nothing Left Nothing bd b Just b

    B

    A

    T

    S match

    build

  • Profunctor Optics and the Yoneda Lemma 18

    Adapters

    Common specialization, for adapting interfaces:

    data Adapter a b s t Adapter ffrom :: s ! a;

    to :: b ! t g

    eg two variants via a Boolean flag:

    swap :: Adapter a a a0 a0 Bool � a Bool � a0 swap Adapter f t where

    f Left a True;a f Right a False;a t b;a if b then Left a else Right a

    B

    A

    T

    S from

    to

  • Profunctor Optics and the Yoneda Lemma 19

    4. Profunctors

    Formally, functors Cop � C! Set. Informally, transformers, which consume and produce:

    class Profunctor p where

    dimap :: c ! a! b ! d! p a b ! p c d

    Generalizations for coherence with product and with sum:

    class Profunctor p ) Cartesian p where first :: p a b ! p a� c b � c

    class Profunctor p ) Cocartesian p where left :: p a b ! p a c b c

    Plain functions ! (homfunctors) are the canonical instance.

    A

    B

  • Profunctor Optics and the Yoneda Lemma 20

    Profunctor optics

    Profunctor optic lifts component transformer P A B to composite transformer P S T , uniformly for all profunctors P of a certain type.

    type AdapterP a b s t 8p : Profunctor p ) p a b ! p s t

    Profunctor lens works for all cartesian profunctors:

    type LensP a b s t 8p : Cartesian p ) p