-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypearithmeticyl.tex
1486 lines (1190 loc) · 41.8 KB
/
typearithmeticyl.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% !TeX TXS-program:compile = txs:///pdflatex/[--shell-escape]
\documentclass[tikz]{beamer}
\usepackage{minted}
\usepackage[parfill]{parskip}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{bm}
\usepackage{tikz-cd}
\usepackage{enumerate}
\usepackage{xfrac}
\usepackage{hyperref}
\usepackage{graphicx} \graphicspath{ {./} }
\usepackage{xcolor}
\usepackage{bbold}
\newcommand{\cat}[1]{\bm{ \mathsf{#1} }}
\newcommand{\functor}[3]{#1 : \cat{#2} \to \cat{#3}}
\newcommand{\functordef}{\functor{F}{C}{D}}
\newcommand{\cc}{\cat{C}}
\newcommand{\dd}{\cat{D}}
\newcommand{\ee}{\cat{E}}
\newcommand{\subcat}[2]{\bm{ \mathsf{#1}}_{\bm{ \mathsf{#2}}}}
\newcommand{\op}[1]{#1^{\text{op}}}
\newcommand{\opc}{\op{\cc}}
\newcommand{\opd}{\op{\dd}}
\newcommand{\ope}{\op{\ee}}
\newcommand{\mono}{\rightarrowtail}
\newcommand{\epi}{\twoheadrightarrow}
\newcommand{\zero}{\bm{\mathbb{0}}}
\newcommand{\one}{\bm{\mathbb{1}}}
\newcommand{\two}{\bm{\mathbb{2}}}
\newcommand{\three}{\bm{\mathbb{3}}}
\newcommand{\bg}{\cat{BG}}
\newcommand{\bgg}{\cat{BG'}}
\newcommand{\nt}{\Rightarrow}
\newcommand{\ant}[2]{\alpha : F \nt G}
\newcommand{\bnt}[2]{\beta : F \nt G}
\newcommand{\anti}[2]{\alpha : F \cong G}
\newcommand{\bnti}[2]{\beta : F \cong G}
\newcommand{\red}[1]{\textcolor{red}{#1}}
\newcommand{\mred}[1]{\textcolor{red}{$#1$}}
\newcommand{\blue}[1]{\textcolor{blue}{#1}}
\newcommand{\mblue}[1]{\textcolor{blue}{$#1$}}
\setbeamertemplate{navigation symbols}{\insertframenumber{}}
\colorlet{shadecolor}{gray!15}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\newcommand{\propnumber}{} % initialize
\newtheorem*{prop}{Proposition \propnumber}
\theoremstyle{definition}
\newtheorem{defn}{Definition}[section]
\AtBeginSection[]{
\begin{frame}
\vfill
\centering
\begin{beamercolorbox}[sep=8pt,center,shadow=true,rounded=true]{title}
\usebeamerfont{title}\insertsectionhead\par%
\end{beamercolorbox}
\vfill
\end{frame}
}
\title{Type Arithmetic and the Yoneda Perspective}
\author{Emily Pillmore}
\begin{document}
\frame{\titlepage}
\section[Outline]{}
\frame{\tableofcontents[hideallsubsections]}
\section{Introduction}
\subsection{Who am I?}
\frame
{
My name is Emily Pillmore.
I am a hopeful mathematician, and a moonlight programmer.
}
\frame
{
\begin{itemize}
\item Twitter (\href{https://twitter.com/emi1ypi}{@emi1ypi})
\item Meetups in NYC: NY Homotopy Type Theory, NY Category Theory, and the NY Haskell User Group.
\item Discord: Haskell $\cap$ Dank Memes: \href{https://discord.gg/2x2fYSK}{https://discord.gg/2x2fYSK}.
\item Personal: All of my slides and meetup content are hosted at \href{https://github.com/cohomolo-gy}{cohomolo.gy}.
\end{itemize}
If you ever want to talk category theory or programming, I'm around to talk, help, or mentor (within reason).
}
\frame
{
I got my start in Scala, with Runar's \textit{Functional Programming in Scala} (the \textcolor{red}{red one} - there is now a \textcolor{blue}{blue one})
}
\frame
{
\begin{center}
\includegraphics[scale=0.7]{screed}
\end{center}
}
\frame
{
I now work at a company called \textbf{Kadena}, designing a language and a couple of cool blockchains
\begin{center}
\includegraphics[scale=0.3]{kadena}
\end{center}
}
\subsection{Why are we here?}
\frame
{
Today, we're going to learn to \textbf{count}. Then we will learn to \textbf{add}, \textbf{multiple}, and even \textbf{take powers}.
}
\frame
{
...and we will do this with the language of Type Theory, Category Theory, and Constructive Logic. Hopefully you will be converted to Computational Trinitarianism by the time we're done.
}
\frame
{
Many of you know these topics already without knowing the details. Much of it is folklore within the community. But there are new ways of thinking about it that I guarantee you haven't seen before, and some of it even translates into our work on Profunctor Optics!
}
\subsection{What and How?}
\frame
{
First, we'll start off with some exercises to illustrate the point, proving free theorems and basic facts about types (e.g. reasoning through why \red{$\forall a. a \to a$} has precisely 1 inhabitant), and then draw direct lines with the Lambek correspondence.
}
\frame
{
Second, we will try to understand the Leibniz perspective and how it informs the Curry-Howard correspondence.
}
\frame
{
Third, we will begin to build the requisite knowledge for understanding the Yoneda perspective, and build some categorical constructs, and an understanding of representation.
}
\frame
{
Then, we will tie it all together.
}
\frame
{
Special thanks to Jon Pretty (\href{https://twitter.com/propensive}{@propensive})
}
\section{Some Type Arithmetic}
\subsection{Ground Rules}
\frame
{
We will use the following conventions:
\begin{itemize}
\item the type \red{()} with precisely 1 inhabitant (called \blue{Unit}) will be denoted by the numeral \mred{\one}.
\item the type with no inhabitants, \blue{Nothing} or \red{Void} will be denoted \mred{\zero}.
\item Function types will be denoted by \red{$a \to b$} or by the notation \red{$b^a$}.
\item Universal quantification is given by \red{forall} or \red{$\forall$}.
\item The notation $|\cdot| : \cat{Type} \to \mathbb{N}$ will denote the function taking a type to its number of type inhabitants
\item The notation $[\cdot] : \mathbb{N} \to \cat{Type} $ will denote the function taking a natural number $n$ and constructing a type with precisely $n$ inhabitants.
\end{itemize}
}
\frame
{
\begin{itemize}
\item Our calculus assumes that we have all finite coproducts and will denote them by the plus sign \red{+}. In Scala and Haskell, the binary coproduct is \red{Either}.
\item Our calculus assumes that we have all finite products, and we will denote they by the times sign \red{*}. In Scala and Haskell, the binary product is \red{(,)} or (\red{(,)} is an alias for \blue{Tuple2} in Scala.
\end{itemize}
}
\frame
{
We also assume our types are sufficiently set-like as to be usefully enumerable.
}
\frame
{
\begin{definition}[Isomorphism]
An \textit{isomorphism} is a pair of functions \mred{to : a \to b} and \mblue{from : b \to a} such that $\red{to} \cdot \blue{from} = \blue{from} \cdot \red{to} = \cat{id}$.
The existence of an isomorphism between types will be denoted $\red{a} \sim \blue{b}$.
\end{definition}
}
\frame
{
There might be some confusion regarding what exactly we mean by “inhabitants”, e.g. are $1 + 1$ and $2$ different inhabitants of type $\cat{Int}$? For now we will treat two expressions of type \mred{a} to be separate inhabitants of \mred{a} if and only if there is some predicate $f : \red{a} \to \cat{Bool}$ that distinguishes between then.
}
\frame
{
Proposition: Suppose we were to treat a polymorphic type like your standard high school arithmetic. What would this arithmetic look like?
}
\subsection{Addition}
\frame
{
Does \mred{1 + 1 = 2}? How can we tell?
}
\begin{frame}[fragile]
By simple counting. Axiomatically, \mred{\one} is the type with 1 inhabitant, so
\begin{minted}[escapeinside=||,mathescape]{haskell}
|\mred{\one}| + |\mred{\one}|
~ () + ()
~ Either () ()
-- [|Either () ()|]
~ |\mred{\two}|
\end{minted}
\end{frame}
\frame
{
By inspection, we see that $\cat{Either}$ has precisely two cases - \blue{Left} and \red{Right} containing a single type. Thus, two cases, with one inhabitant in each case. Therefore we have 2 inhabitants! Note also that this is isomorphic to $\cat{Bool}$, since $\cat{Bool}$ has precisely 2 inhabitants.
}
\frame
{
Instead of referencing these types by their names, we'll just represent them as numerals $\one, \two, \three, \ldots$.
}
\frame
{
How about \mred{\cat{Maybe}} containing values of type \mblue{\two}?
}
\begin{frame}[fragile]
Again, we can apply some natural deduction, nothing that \mblue{\two} has precisely two inhabitants
\begin{minted}[escapeinside=||,mathescape]{haskell}
Maybe | \mblue{\two}|
~ Nothing + Just | \mblue{\two}|
~ |\mblue{\one}| + | \mblue{\two}|
~ |\mblue{\three}|
\end{minted}
\end{frame}
\frame
{
Thus, the number of inhabitants of \mred{\cat{Maybe}} can be deduced to be $[1 + |\red{a}|]$
}
\subsection{Multiplication}
\frame
{
Multiplication operates in the similar way. Using products, we can count \mred{\cat{Bool} * \cat{Bool}}. We have precisely four cases:
\begin{itemize}
\item (\blue{True}, \blue{True})
\item(\blue{True}, \red{False})
\item (\red{False}, \blue{True})
\item (\red{False}, \red{False})
\end{itemize}
}
\frame
{
This works out as well! How about exponents?
}
\subsection{Exponentiation}
\begin{frame}[fragile]
Let's consider a function \mred{\cat{Bool} \to \cat{Bool}}. How do we count the number of functions we have? Well, we can list them out:
\begin{minted}{haskell}
k1 :: Bool -> Bool
k1 b
| True = True
| False = False
k2 :: Bool -> Bool
k2 b
| True = True
| False = True
-- and so on
\end{minted}
\end{frame}
\begin{frame}[fragile]
tl;dr there are exactly 4 inabitants. Now, consider the functions
\begin{minted}{haskell}
f :: Maybe Bool -> Bool
f ...
\end{minted}
\end{frame}
\frame
{
Upon inspection you might find that there are precisely 8 such functions. Inductively, we see that there are \mred{|b|^{|a|}}-many inhabitants of the type \mred{a \to b}. The cardinality of this type behaves like exponentiation in $\mathbb{N}$!
}
\frame
{
As we will see later, \mred{a \to b} is a little bit different from coproducts and products in the way it interacts with \red{forall a} when we have (parametric) type polymorphism. The above formula can be used only if \red{a} and \red{b} are free from any universally quantified variables.
}
\subsection{$\top$ and $\bot$}
\frame
{
Considering all we have just discussed, there are caveats to this calculus. For example, how many inhabitants are there in the type \mred{\zero \to \one}? How about \mred{\one \to \zero}? \mred{\zero \to \zero}?
}
\subsection{A correspondence Pt. 1}
\frame
{
The analogy so far:
\begin{center}
\begin{tabular}{|c|c|c|c|}
\hline
Types & \# of Inhabitants & Sets & Categories \\
\hline a & $|A|$ & ? & ? \\
(a, b) & $|A| \times |B| $ & ? & ? \\
Either a b & $|A| + |B|$ & ? & ? \\
$a \to b$ & $|B|^{|A|}$ & ? & ? \\
() & 1 & ? & ? \\
Void & 0 & ? & ? \\ \hline
\end{tabular}
\end{center}
}
\frame
{
\textbf{Claim}: we can fill in the rest of the table with direct analogies from Set theory and Category theory.
}
\begin{frame}[fragile]
What does this look like?
\begin{center}
\begin{minted}[escapeinside=||,mathescape]{haskell}
leftUnitP :: (|$\one$| |$\times$| a) = a
commuteP :: (a |$\times$| b) = (b |$\times$| a)
associateP :: ((a |$\times$| b) |$\times$| c) = (a |$\times$| (b |$\times$| c))
leftUnitC :: (|$\zero$| + a) = a
commuteC :: (a + b) = (b + a)
associateC :: ((a + b) + c) = (a + (b + c))
currying :: |$c^{a \times b}$| = |$(c^b)^a$|
leftUnitF :: |$a^{\one}$| = a
rightUnitF :: |$\one^a$| = |$\one$|
\end{minted}
\end{center}
\end{frame}
\frame
{
Why do we care in the first place?
}
\frame
{
Because proving that a type \mblue{T} has inhabitants is equivalent to proving the theorem corresponding with \mblue{T} due to the Curry-Howard-Lambek correspondence.
}
\section{The Leibniz Perspective}
\subsection{When are things equal?}
\frame
{
When are two things equal? You may have noticed our use of $=$ in the previous slides, but that was a convenient lie.
}
\frame
{
Are these things equal?
\begin{itemize}
\item \blue{1729}
\item \mblue{12^3 + 1^3}
\item "The first number expressible by the sum of two cubes in two different ways"
\end{itemize}
}
\frame
{
When viewed as expressions, \blue{1729} and \mblue{12^3 + 1^3} are different.
}
\frame
{
When viewed as integers, they are the same.
}
\subsection{Isomorphisms}
\frame
{
Enumeration in this sense is an association of a representative of an equivalence class of \textbf{isomorphisms} of a type with a finite set.
}
\begin{frame}[fragile]
\begin{minted}{haskell}
data Iso a b = Iso
{ to :: a -> b
, from :: b -> a
}
-- fromTo :: (x :: a) -> (from . to) x = x
-- toFrom :: (x :: b) -> (to . from) x = x
\end{minted}
\end{frame}
\frame
{
We have been suffering from an abuse of notation - what we've really been saying is that two things are equal if we can exhibit an isomorphism between the two.
}
\frame
{
\begin{prop}[Equality Preservation]
if \red{A} and \blue{B} are isomorphic, and \mred{a_1, a_2 : A}, and \mblue{b_1, b_2 : B}, then \mred{a_1 = a_2} $\Leftrightarrow$ \mblue{b_1 = b_2}.
\end{prop}
}
\subsection{Identity of Indiscernibles}
\frame
{
\begin{prop}[Gottfried Wilhelm Leibniz, 1646-1716]
"For any x and y, if x is identical to y, then x and y have all the same properties. For any x and y, if x and y have all the same properties, then x is identical to y."
\end{prop}
}
\frame
{
In symbols, $\blue{x} = \red{y} \Leftrightarrow \forall P. P \blue{x} = P \red{y}$ where $P$ is quantified over all properties of \blue{x}.
}
\subsection{Putting it to work}
\begin{frame}[fragile]
Let's see some interesting isomorphisms.
\begin{minted}{haskell}
leftUnitP :: Iso ((), a) a
leftUnitP = Iso (\((), a) -> a)
(\a -> ((), a))
commuteP :: Iso (a, b) (b, a)
commuteP = Iso (\(a, b) -> (b, a))
(\(b, a) -> (a, b))
associateP :: Iso ((a, b), c) (a, (b, c))
associateP = Iso (\((a, b), c) -> (a, (b, c)))
(\(a, (b, c)) -> ((a, b), c))
\end{minted}
\end{frame}
\begin{frame}[fragile]
What does this remind you of?
\begin{minted}{haskell}
rightUnitP :: Iso (a, ()) a
leftUnitP :: Iso ((), a) a
associateP :: Iso ((a, b), c) (a, (b, c))
\end{minted}
\end{frame}
\begin{frame}[fragile]
\begin{minted}{haskell}
leftUnitC :: Iso (Either Void a) a
leftUnitC = ...
commuteC :: Iso (Either a b) (Either b a)
commuteC = ...
associateC :: Iso (Either (Either a b) c)
(Either a (Either b c))
associateC =
Iso (\case Left (Left a) -> Left a;
Left (Right b) -> Right (Left b);
Right c -> Right (Right c))
(\case Left a -> Left (Left a);
Right (Left b) -> Left (Right b);
Right (Right c) -> Right c)
\end{minted}
\end{frame}
\frame
{
So what do our arithmetic rules \textit{really} say about our types?
}
\begin{frame}[fragile]
\begin{minted}[escapeinside=||,mathescape]{haskell}
leftUnitP :: (|$\one$| |$\times$| a) |$\cong$| a
commuteP :: (a |$\times$| b) |$\cong$| (b |$\times$| a)
associateP :: ((a |$\times$| b) |$\times$| c) |$\cong$| (a |$\times$| (b |$\times$| c))
leftUnitC :: (|$\zero$| + a) |$\cong$| a
commuteC :: (a + b) |$\cong$| (b + a)
associateC :: ((a + b) + c) |$\cong$| (a + (b + c))
currying :: |$c^{a \times b}$| |$\cong$| |$(c^b)^a$|
leftUnitF :: |$a^{\one}$| |$\cong$| a
rightUnitF :: |$\one^a$| |$\cong$| |$\one$|
\end{minted}
\end{frame}
\subsection{The million dollar question}
\frame
{
By inspection, we can show that there is a correspondence between sets and types by looking at their cardinalities and inhabitants respectively.
}
\frame
{
What are the sets with cardinality $\red{a} \times \blue{b}$, $\red{a} + \blue{b}$, $1$, $0$, and $\blue{b}^{\red{a}}$-many inhabitants?
}
\frame
{
We continue to build the picture.
}
\begin{frame}
There is a correspondence between types, sets, natural numbers.
\begin{center}
\begin{tabular}{|c|c|c|c|}
\hline
Types & Sets & \# of Inhabitants & Categories \\
\hline
a & $A$ & $|A|$ & ? \\
(a, b) & $A \times B$ & $|A| \times |B|$ & ? \\
Either a b & $A \sqcup B$ & $|A| + |B|$ & ? \\
$a \to b$ & set functions & $|B|^{|A|}$ & ? \\
() & $\{*\}$ & 1 & ? \\
Void & $\emptyset$ & 0 & ? \\ \hline
\end{tabular}
\end{center}
\end{frame}
\section{Some Category Theory}
\subsection{The Basics}
\frame
{
\textbf{Claim}: the same correspondence can be made within the language of a bicartesian-closed category.
}
\frame
{
Lets elaborate on what that means. But first, some definitions.
}
\frame
{
\begin{definition}[Category]
A \textbf{category} $\cc$ consists of the following data:
\begin{itemize}
\item a collection of \textbf{objects} \mred{x, y, z,...}
\item a collection of \textbf{morphisms} \mred{f,g,h,...}
\end{itemize}
Such that
\begin{itemize}
\item each morphism has specified \textbf{domain} and \textbf{codomain} objects so that the notation \mred{f : x \to y} signifies a morphism with domain \mred{x} and codomain \mred{y}.
\item each object has an associated \textbf{identity} morphism \mblue{1_x : x \to x} which acts as a two-sided unital element for composition.
\item to each pair of morphisms \mred{f : x \to y}, \mblue{g : y \to z}, there exists a \textbf{composite} arrow $h : \red{x} \to \blue{z}$ where $h :\equiv \blue{g}\red{f}$.
\end{itemize}
\end{definition}
}
\frame
{
For every category we may speak of its dual notion, $\opc$, the \textbf{opposite} category of $\cc$, consisting of the same object data, but with the domain and codomain of each morphism reversed.
}
\frame
{
This data is subject to some axioms.
\begin{itemize}
\item For any $f : \blue{x} \to \red{y}$, $f1_{\blue{x}} = f = 1_{\red{y}} f$.
\item For any composable triple $f,g,h$, $h(gf) = (hg)f$, and will simply be denoted $hgf$.
\item Between any two objects $\blue{x}, \red{y}$ in a category $\cc$, there exists an object $\cc(\red{x},\red{y})$ consisting of all morphisms with domain $\blue{x}$ and codomain $\red{y}$. This is usually called $Hom_{\cc}(\blue{x}, \red{y})$.
\end{itemize}
}
\frame
{
Indeed, we are familiar with morphisms, identities, and composition already. In haskell, these translate into types \mred{a \to b}, the identity function \mred{id}, and composition \mred{(.)}.
In scala, these are roughly the same, modulo naming conventions - \mblue{A \Rightarrow B}, \mblue{identity}, \mblue{compose}.
}
\frame
{
Isomorphisms in this context become a pair of morphisms \mred{ f : c \to d} and \mblue{g : d \to c} such that $\blue{g}\red{f} = 1_c$ and $\red{f}\blue{g} = 1_d$.
If \mred{x} and \mblue{y} are isomorphic, we will denote this by $\red{x} \cong \blue{y}$.
}
\frame
{
We can also speak of the "size" of a category in a precise sense
}
\frame
{
\begin{definition}[Size matters]
A category $\cc$ is said to be...
\begin{itemize}
\item \textbf{small} if there are at most a set's worth of morphisms and objects in the category
\item \textbf{locally small} if for all $\blue{c}, \red{d}$, $\cc(\blue{c}, \red{d})$ is a set
\item \textbf{large} if it is not small
\end{itemize}
\end{definition}
}
\frame
{
For our purposes, everything will be small (set-sized)
}
\frame
{
\begin{definition}[Functor]
A functor $F : \cc \to \dd$ between categories consists of the following data:
\begin{itemize}
\item an object $F\blue{c}$ in $\dd$ for every object \mblue{c} of $\cc$
\item a morphism $Ff : F\blue{c} \to F\red{d}$ in $\dd$ for each morphism $f : \blue{c} \to \red{d}$ of $\cc$.
\end{itemize}
Additionally, we require that $Fg \cdot Ff = F(gf)$ when $g$ and $f$ are a composable pair, and that for every object \mblue{c} in $\cc$, $F1_{\blue{c}} = 1_{F\blue{c}}$.
\end{definition}
}
\begin{frame}[fragile]
In Haskell, we represent this data as a typeclass
\begin{minted}{haskell}
class Functor f where
fmap :: (a -> b) -> f a -> f b
-- fmap id fa = fa
-- fmap g . fmap f = fmap (g . f)
\end{minted}
\end{frame}
\frame
{
Functors that act uniformly on arrows are called \textbf{covariant}. However, if a functor is mapping to or from an opposite category to a normal category, then one calls these functors \textbf{contravariant}.
Operationally, this can be seen as "flipping the direction of arrows" in the target category.
}
\frame
{
Saying "$F$ is a contravariant functor from $\cc$ to $\dd$" is saying that one has a covariant functor $F : \opc \to \dd$. When it matters, I'll just tell you which one that is.
}
\frame
{
Example: what do we mean by cardinality?
}
\frame
{
Recall that I mentioned this was an association of an isomorphism class of types with a finite set.
}
\frame
{
Enumeration defines a cardinality functor $|\cdot|: \cat{Fin_{Iso}} \to \cat{Set}$.
}
\frame
{
As an important example, for any object \mblue{c} of $\cc$, we have the \textbf{covariant represented functor of \blue{c}}, $\cc(\blue{c}, -)$, which is a functor $\cc \to \cat{Set}$.
}
\frame
{
LIkewise, we have the \textbf{contravariant functor represented by \red{c}}: $\cc(-, \red{c}) : \opc \to \cat{Set}$.
}
\frame
{
We know these well! They're our old friends \textbf{Reader} and \textbf{Coreader}.
}
\begin{frame}[fragile]
\begin{minted}{haskell}
newtype Reader t a = Reader (t -> a)
instance Functor (Reader t) where
-- fmap :: (a -> b) -> Reader t a -> Reader t b
fmap f (Reader t) = Reader (f . t)
newtype Coreader t a = Coreader (a -> t)
instance Contravariant (Coreader t) where
contramap f (Coreader k) = Coreader (k . f)
\end{minted}
\end{frame}
\frame
{
To each functor $F : \cc \to \dd$ is associated data - a family of maps from each object $c$ of $\cc$ to objects $Fc$ of $\dd$ and likewise between morphisms. But this begs the question - how can we translate the data of one functor into the data of another?
}
\frame
{
For one, this entails we have two functors $F, G : \cc \to \dd$ with the same domain and codomain. Second, we need additional data such that to each $c$ of $\cc$ and $Fc$ of $\dd$, we can translate $Fc$ to $Gc$ in a natural way.
}
\begin{frame}[fragile]
\begin{definition}[Natural Transformation]
Let $F,G : \cc \to \dd$ be functors. A \textbf{natural transformation} $\alpha$ consists of the following data: to each $c$ of $\cc$, a \textbf{component} map $\alpha_c : Fc \Rightarrow Gc$ such that the following square commutes for any $f : c \to d$ of $\cc$:
\begin{center}
\begin{tikzcd}
Fc \ar[r, "\alpha_c"] \ar[d, "Ff", swap] & Gc \ar[d, "Gf"]
\\ Fd \ar[r, "\alpha_d", swap] & Gd
\end{tikzcd}
\end{center}
\end{definition}
\end{frame}
\frame
{
I.e., $\alpha_d \cdot Ff = Gf \cdot \alpha_c$.
}
\begin{frame}[fragile]
In haskell, this is rather simple to implement:
\begin{minted}{haskell}
type f ~> g = forall a. f a -> g a
-- given f : a -> b, natural transf. t,
-- t . fmap f = fmap f . t
\end{minted}
\end{frame}
\frame
{
A \textbf{natural isomorphism} is a natural transformation where each component map is an isomorphism. We will denote this $\alpha : F \cong G$.
}
\frame
{
One form of category that we will look at later is that of a \textbf{functor category}, denoted $[\cc, \dd]$, whose objects are functors $\cc \to \dd$, and whose morphisms are natural transformations between these functors.
}
\subsection{Initial and Terminal Objects}
\frame
{
"Special" objects are determined by how morphisms to or from them act in relation to other objects.
}
\frame
{
\begin{definition}[Terminal and Initial objects]
An object $c$ of $\cc$ is called \textbf{terminal} if there is exactly 1 unique morphism from any other object in $\cc$ to $c$.
Dually, an object $c$ of $\cc$ is \textbf{initial} if there is exactly 1 unique morphism to any other object in $\cc$.
\end{definition}
}
\frame
{
We have seen two such objects that act like terminal and initial objects already: \mred{()} and \mblue{Void}
}
\frame
{
Indeed for every type $a$, the types \mred{a \to ()} and \mblue{Void \to a} has precisely one inhabitant .
}
\begin{frame}[fragile]
Namely, we have the following functions:
\begin{minted}[escapeinside=||,mathescape]{haskell}
-- a -> ()
k :: a -> ()
k = const ()
-- |$\bot$| -> a
t :: Void -> a
t = absurd
\end{minted}
\end{frame}
\subsection{Products and Coproducts}
\frame
{
In the same way that we have terminal and initial objects, constructions for products and coproducts in category theory follow suit.
}
\frame
{
\begin{definition}[Product]
A \textbf{product} in a category $\cc$ is an object $P$ together with morphisms $p_c : P \to c$ and $p_d : P \to d$ for $c,d$ in $\cc$ such that for any other object $N$ with morphisms $f_c : N \to c$ and $f_d : N \to d$, then each $f_{(-)}$ can be factored through a unique function $f : N \to P$ such that $f_c = p_cf$ and $f_d = p_df$.
\end{definition}
}
\begin{frame}[fragile]
This is diagram gives this data succinctly:
\begin{center}
\begin{tikzcd}
& & c
\\ N \ar[r, "f" description, dotted] \ar[urr, "f_c", bend left] \ar[drr, "f_d", bend right, swap] & P \ar[ur, "p_c"] \ar[dr, "p_d", swap]
\\ & & d
\end{tikzcd}
\end{center}
\end{frame}
\begin{frame}[fragile]
We are all familar with these: in Haskell and Scala, we have the type \mred{(a, b)}. Its defined by having two operators:
\begin{minted}{haskell}
fst :: (a,b) -> a
fst (a,_) = a
snd :: (a,b) -> b
snd (_,b) = b
\end{minted}
\end{frame}
\frame
{
\begin{definition}[Coproduct]
A \textbf{coproduct} in a category $\cc$ is an object $P$ together with morphisms $i_c : c \to C $ and $i_d : C \to d$ for $c,d$ in $\cc$ such that for any other object $N$ with morphisms $f_c : c \to N$ and $f_d : d \to N$, then each $f_{(-)}$ can be factored through a unique function $f : C \to N$ such that $f_c = fi_c$ and $f_d = fi_d$.
\end{definition}
}
\begin{frame}[fragile]
This is diagram gives this data succinctly:
\begin{center}
\begin{tikzcd}
c \ar[dr, "i_c", swap] \ar[drr, "f_c", bend left]
\\ & C \ar[r, "f" description, dotted] & N
\\ d \ar[ur, "i_d"] \ar[urr, "f_d", bend right, swap]
\end{tikzcd}
\end{center}
\end{frame}
\begin{frame}[fragile]
We are all familar with these: in Haskell and Scala, we have the type \blue{Either} \blue{a} \blue{b}. Its defined by having two operators:
\begin{minted}{haskell}
inl :: a -> Either a b
inl = Left
inr :: b -> Either a b
inr = Right
\end{minted}
\end{frame}
\subsection{Universal Properties}
\frame
{
The diagrams for products and coproducts define their \textbf{universal properties}. In an imprecise sense, this means that these constructions are initial themselves in the appropriate category.
}
\frame
{
Let's now consider morphisms to and from products and coproducts, beginning with products first.
}
\frame
{
Consider $f \in \cc(\red{a \times b}, \blue{c})$. What data is necessary to produce \mblue{c}?
}
\frame
{
$f$ specifies an element $f \red{(a,b)}$ of $\cc$. Therefore, we require that $f$ have the additional data that when partially applied to \mred{b}, $f \red{(-,b)}$ specifies a function $\phi : \red{a} \to \blue{c}$.
}
\frame
{
Thus, to specify a function $\cc(\red{a \times b}, \blue{c})$ is to specify an equivalent function $\cc(\red{b}, \blue{c}^{\red{a}})$ - hence, an isomorphism.
}
\begin{frame}[fragile]
We are familiar with this function!
\begin{minted}{haskell}
curry :: ((a,b) -> c) -> a -> b -> c
curry k a b = k (a,b)
uncurry :: (a -> b -> c) -> (a,b) -> c
uncurry k (a,b) = k a b
\end{minted}
\end{frame}
\frame
{
The same deduction can be applied to coproducts. What data can we glean from $f \in \cc(\blue{a + b}, \red{c})$?
}
\frame
{
When \mblue{a + b} is introduced, we have either the left or the right hand side.
Therefore, the data equivalent to $f$ consists of a pair of function $\blue{a} \to \red{c}$ and $\blue{b} \to \red{c}$ - one for each side.
}
\frame
{
In symbols, $\cc(\blue{a + b}, \red{c}) \cong \cc(\blue{a}, \red{c}) \times \cc(\blue{b}, \red{c})$
}
\begin{frame}[fragile]
\begin{minted}{haskell}
either
:: Either a b -> (a -> c) -> (b -> c) -> c
either (Left a) f _ = f a
either (Right b) _ g = g b
pair :: (a -> c, b -> c) -> Either a b -> c
pair (f,g) (Left a) = f a
pair (f,g) (Right b) = g b
\end{minted}
\end{frame}
\frame
{
Similarly, how would you deduce that $(\red{a \times b})^{\blue{c}} \cong \red{a}^{\blue{c}} \times \red{b}^{\blue{c}}$?
}
\section{The Yoneda Perspective}
\begin{frame}