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

27
Profunctor Optics and the Yoneda Lemma Jeremy Gibbons joint work with Guillaume Boisseau WG2.1#77, July 2018

Upload: others

Post on 18-Apr-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

Profunctor Optics and the

Yoneda Lemma

Jeremy Gibbons

joint work with Guillaume Boisseau

WG2.1#77, July 2018

Page 2: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

Profunctor Optics and the Yoneda Lemma 2

1. Nobuo Yoneda

• 1930–1996

• Professor of TheoreticalFoundations of InformationScience at University ofTokyo

• member of IFIP WG2.1,contributor to discussionsabout Algol 68, major rolein Algol N

• but primarily an algebraist

Page 3: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

Profunctor Optics and the Yoneda Lemma 3

2. The Yoneda Lemma

Formally,

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

From left to right, take nt � : C�A;�� :! F to �A�idA� 2 F�A�.From right to left, take element x 2 F�A� to nt � such that �B�f � � F�f ��x�.

As a special case, the Yoneda Embedding:

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

�C;Set��C�A;��;C�B;��� ’ C�B;A�

Quite fearsomely terse!

Page 4: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

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’execution du poeme qui est le poeme (Valery)

• in music, die Idee der Interpretation gehort zur Musik selber und istihr nicht akzidentiell (Adorno)

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

Page 5: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

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 xgfromYo :: 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 .

Page 6: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

Profunctor Optics and the Yoneda Lemma 6

Co-Yoneda

For functor F and object A:

F�A� ’ �8X : �A! X �! F�X ��

Now consider one half of this iso:

8A : F�A�! �8X : �A! X �! F�X ��’ 8A : 8X : F�A�! �A! X �! F�X �’ 8A : 8X : �F�A�� �A! X ��! F�X �’ 8X : 8A : �F�A�� �A! X ��! F�X �’ 8X : �9A : F�A�� �A! X ��! F�X �

and indeed, for functor F and object X :

�9A : F�A�� �A! X �� ’ F�X �

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

Page 7: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

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�

Page 8: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

Profunctor Optics and the Yoneda Lemma 8

The Yoneda Lemma, familiarly (1)

Proof by indirect inequality or indirect order

�b 6 a�a �8c : �a 6 c�) �b 6 c��

is the Yoneda Embedding in category Pre�6�:

�Pre�6�;Set��Pre�6��a;��;Pre�6��b;��� ’ Pre�6��b;���a�� Pre�6��b;a�

• homset Pre�A;6��b;a� is a ‘thin set’: singleton or empty

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

• nt � : Pre�A;6��a;�� :! Pre�A;6��b;�� isfunction family �c : Pre�A;6��a; c�! Pre�A;6��b; c�

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

Page 9: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

Profunctor Optics and the Yoneda Lemma 9

The Yoneda Lemma, familiarly (2)

• any functor F preserves isos:

F�f � � F�g� � id a F�f � g� � F�id�( f � g � id

• full and faithful functor also reflects isos

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

�C�B;�� ’ C�A;���a �A ’ B�a �C��;A� ’ C��;B��

in particular for C � Pre�6�, the rule of indirect equality:

�b � a�a �8c : �a 6 c�a �b 6 c��

Page 10: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

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 symmetricgroup acting on G

and similarly

every monoid is isomorphic to a monoid of endomorphisms

�M ;�; e� ’ �f�m�� jm 2 Mg; ���; id�

and similarly,

every poset isomorphic to a poset of principal ideals

Page 11: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

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

Page 12: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

Profunctor Optics and the Yoneda Lemma 12

Yoneda embedding

�C; Set��C�A;��;C�B;��� ’ C�B;A�

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

Specialize to

�M;Set��M��;��;M��;��� ’M��;�� � M

We’ll show lhs equivalent to monoid

���M�; ���; id�

where ��M� � f��m� jm 2 Mg.

Page 13: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

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��

Page 14: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

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

Page 15: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

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

Page 16: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

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

Sview

update

Page 17: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

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

Smatch

build

Page 18: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

Profunctor Optics and the Yoneda Lemma 18

Adapters

Common specialization, foradapting 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

Sfrom

to

Page 19: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

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

Page 20: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

Profunctor Optics and the Yoneda Lemma 20

Profunctor optics

Profunctor optic liftscomponent transformer P A B tocomposite transformer P S T , uniformlyfor 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 a b ! p s t

Profunctor prism works for all cocartesian profunctors:

type PrismP a b s t � 8p : Cocartesian p ) p a b ! p s t

Optics are now just ordinary functions, so compose ordinarily.

B

A

T

S

Page 21: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

Profunctor Optics and the Yoneda Lemma 21

5. Double Yoneda Embedding

Key insight, by Bartosz Milewski:

��C; Set�;Set�����A; ���B� ’ C�A;B� ���

since by three applications of Yoneda we have:

��C;Set�; Set�����A; ���B�’ �� Yoneda Lemma: F�X � ’ �C;Set��C�X ;��; F� ����C;Set�; Set���C; Set��C�A;��;��; �C;Set��C�B;��;���’ �� Yoneda Embedding ���C; Set��C�B;��;C�A;���’ �� Yoneda Embedding ��C�A;B�

Page 22: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

Profunctor Optics and the Yoneda Lemma 22

Double Yoneda Embedding, computationally

In Haskell, taking C � Set, this says:

�8f : Functor f ) f a ! f b� ’ �a ! b�

Indeed,

fromFun :: �8f : Functor f ) f a ! f b�! �a ! b�fromFun � � unId �� � Id -- apply to identity

toFun :: �a ! b�! �8f : Functor f ) f a ! f b�toFun � fmap -- use functorial action

are inverses.

But the result holds in any category.

Page 23: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

Profunctor Optics and the Yoneda Lemma 23

Deriving profunctor adapters using Yoneda

Concrete adapters over C are parallel but opposite arrows:

A Soo

B // T

Introduce synonym Ada for category Cop � C.Then a :: Adapter A B S T is an arrow a 2 Ada��A;B�; �S;T��.

Profunctors are functors Cop � C! Set, ie Ada ! Set.

Double Yoneda ��� gives:

�8P : Profunctor P ) P A B ! P S T� ’ Ada��A;B�; �S;T��

—that is, AdapterP A B S T ’ Adapter A B S T !

Page 24: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

Profunctor Optics and the Yoneda Lemma 24

Deriving profunctor lenses using Yoneda

Category Lens over C has same objects as Cop � C, but arrows

Lens��A;B�; �S;T�� � 9C 2 C : C�S;A� C�� C�B � C;T�

(formally, a co-end). These are equivalent to concrete lenses (1).

Moreover, functors P : Lens ! Set are Cartesian profunctors (2).

Double Yoneda ��� again gives:

�8P : Cartesian P ) P A B ! P S T� ’ Lens��A;B�; �S;T��

—that is, LensP A B S T ’ Lens A B S T .

And dually for prisms.

Page 25: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

Profunctor Optics and the Yoneda Lemma 25

Proof obligation (1)

Arrows in Lens are equivalent to concrete lenses:

9C 2 C : C�S;A� C�� C�B � C;T�’ �� product ��9C 2 C : C�S;A�� C�S;C�� C�B � C;T�’ �� distributing C�S;A��� through existential ��C�S;A�� �9C 2 C : C�S;C�� C�B � C;T��’ �� contra-co-Yoneda: �9C : C�X ;C�� F�C�� ’ F�X � ��C�S;A�� C�B � S;T�

Page 26: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

Profunctor Optics and the Yoneda Lemma 26

Proof obligation (2)

Functors P : Lens ! Set are Cartesian profunctors:

8A B S T : �9C : C�S;A� C�� C�B � C;T��! P�A;B�! P�S;T�’ �� distribute � ! P�A;B�! P�S;T� through existential ��8A B S T : 8C : C�S;A� C�� C�B � C;T�! P�A;B�! P�S;T�’ �� currying; distribute 8T : � ��8A B S C : C�S;A� C�! 8T : C�B � C;T�! P�A;B�! P�S;T�’ �� Yoneda: �8T : C�X ;T�! F�T�� ’ F�X � ��8A B S C : C�S;A� C�! P�A;B�! P�S;B � C�’ �� contra-Yoneda: �8S : C�S;X �! F�S�� ’ F�X � ��8A B C : P�A;B�! P�A� C;B � C�

ie we get the first method from the functorial action.

Page 27: Profunctor Optics and the Yoneda Lemma...Profunctor Optics and the Yoneda Lemma 4 The Yoneda Lemma, philosophically roughly, ‘a thing is determined by its relationships with other

Profunctor Optics and the Yoneda Lemma 27

6. Conclusions

• profunctor optics are practical and powerful, but mysterious

• previous proofs of equivalence (ours and Milewski’s) convoluted

• Yoneda simplifies everything considerably

• some of these ideas due to Ed Kmett, Russell O’Connor,Matthew Pickering, Bartosz Milewski

• paper to appear at ICFP 2018

• GB funded by UK EPSRC