-
Notifications
You must be signed in to change notification settings - Fork 1
/
typespp.tex
1907 lines (1698 loc) · 88.7 KB
/
typespp.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
\documentclass[a4paper,USenglish]{lipics-v2016-utf8x}
\usepackage{amsthm,amstext,amssymb,amsmath}
\usepackage{datetime}
\usepackage{fdsymbol} %% must be loaded before bbold because it interferes with numbers
\usepackage{bbold}
\usepackage{url}
\usepackage{xcolor}
\usepackage{multicol}
\usepackage{proof}
\usepackage{stmaryrd}
\usepackage{bussproofs}
\usepackage{tikz}
\usepackage{tikz-cd}
\usetikzlibrary{quotes}
\usepackage[section]{placeins}
\usepackage{braket}
\usepackage{mathtools}
\usepackage{microtype}
\newcommand{\textpi}{$\pi$}
\newcommand{\ft}{\mathbb{T}}
\newcommand{\hash}{\#}
\newcommand{\pifrac}{\ensuremath{\Pi^/}}
\newcommand{\iso}{\leftrightarrow}
\newcommand{\isotwo}{\Leftrightarrow}
\newcommand{\alt}{~|~}
\newcommand{\ag}[2]{#1 \sslash #2}
\newcommand{\agp}[3]{#1 \sslash_{#3} #2}
\newcommand{\order}[1]{\hash #1}
\newcommand{\iorder}[1]{1/\hash #1}
\newcommand{\oneg}[1]{\mathbb{1}_{#1}}
\newcommand{\divgl}[2]{\hash{#1} \div_{l} \hash{#2}}
\newcommand{\divgr}[2]{\hash{#1} \div_{r} \hash{#2}}
\newcommand{\ord}[1]{\ensuremath{\mathsf{order}(#1)}}
\newcommand{\inl}[1]{\textsf{inl}(#1)}
\newcommand{\inr}[1]{\textsf{inr}(#1)}
\newcommand{\zt}{\mathbb{0}}
\newcommand{\ot}{\mathbb{1}}
\newcommand{\G}{\mathcal{G}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Zn}{\mathbb{Z}_n}
\newcommand{\Zvn}{\mathbb{Z}^v_n}
\newcommand{\N}{\mathbb{N}}
\newcommand{\cycle}{\textsf{cycle}}
\newcommand{\twod}{\mathbb{T}}
\newcommand{\fract}[2]{#1/#2}
\newcommand{\fv}[2]{\fcolorbox{black}{white}{\strut $#1$}\fcolorbox{black}{gray!20}{$\strut #2$}}
\newcommand{\pt}[2]{\bullet[#1,#2]}
\newcommand{\sing}[1]{\textsc{Sing}(#1)}
\newcommand{\singi}[2]{\textsc{SingI}_{{#1}}(#2)}
\newcommand{\iter}[1]{\textsc{Iter}(#1)}
\newcommand{\pair}[2]{\langle #1,#2 \rangle}
\newcommand{\triple}[3]{\langle #1,#2,#3 \rangle}
\newcommand{\distiterplus}[3]{\mathsf{dist}~#1~#2~#3}
\newcommand{\looping}[1]{\mathcal{L}{#1}}
\newcommand{\delooping}[1]{\mathcal{B}{#1}}
\newcommand{\permone}{\mathit{perm}_{...}}
\newcommand{\permtwo}{\mathit{perm}_{\times\!\times.}}
\newcommand{\permthree}{\mathit{perm}_{.\times\!\times}}
\newcommand{\permfour}{\mathit{perm}_{\rightarrow}}
\newcommand{\permfive}{\mathit{perm}_{\leftarrow}}
\newcommand{\permsix}{\mathit{perm}_{\times.\times}}
\newcommand{\quant}[0]{\mbox{\ensuremath{\mathrlap{\diamond}\square}}}
\newcommand{\Rule}[4]{
\makebox{{\rm #1}
$\displaystyle
\frac{\begin{array}{l}#2\\\end{array}}
{\begin{array}{l}#3\\\end{array}}$
#4}}
\newcommand{\proves}{\vdash}
\newcommand{\jdg}[3]{{#1} #3}
\newcommand{\evalone}[2]{#1~\triangleright~#2}
\newcommand{\evaloneb}[2]{#1~\triangleleft~#2}
\newcommand{\unitv}{()}
\newcommand{\synchl}{\mathsf{{synchl}}}
\newcommand{\synchr}{\mathsf{{synchr}}}
\newcommand{\packl}{\mathsf{{packl}}}
\newcommand{\packr}{\mathsf{{packr}}}
\newcommand{\unitepl}{\mathsf{{unite_+l}}}
\newcommand{\unitipl}{\mathsf{{uniti_+l}}}
\newcommand{\unitepr}{\mathsf{{unite_+r}}}
\newcommand{\unitipr}{\mathsf{{uniti_+r}}}
\newcommand{\swapp}{\mathsf{{swap_+}}}
\newcommand{\assoclp}{\mathsf{{assocl_+}}}
\newcommand{\assocrp}{\mathsf{{assocr_+}}}
\newcommand{\unitetl}{\mathsf{{unite_{\star}l}}}
\newcommand{\unititl}{\mathsf{{uniti_{\star}l}}}
\newcommand{\unitetr}{\mathsf{{unite_{\star}r}}}
\newcommand{\unititr}{\mathsf{{uniti_{\star}r}}}
\newcommand{\swapt}{\mathsf{{swap_{\star}}}}
\newcommand{\assoclt}{\mathsf{{assocl_{\star}}}}
\newcommand{\assocrt}{\mathsf{{assocr_{\star}}}}
\newcommand{\absorbr}{\mathsf{{absorbr}}}
\newcommand{\absorbl}{\mathsf{{absorbl}}}
\newcommand{\factorzr}{\mathsf{{factorzr}}}
\newcommand{\factorzl}{\mathsf{{factorzl}}}
\newcommand{\dist}{\mathsf{{dist}}}
\newcommand{\factor}{\mathsf{{factor}}}
\newcommand{\distl}{\mathsf{{distl}}}
\newcommand{\factorl}{\mathsf{{factorl}}}
\newcommand{\idiso}{\mathsf{{id}}}
\newcommand{\assocdl}{\mathsf{{assoc_{\odot}l}}}
\newcommand{\assocdr}{\mathsf{{assoc_{\odot}r}}}
\newcommand{\idldl}{\mathsf{{idl_{\odot}l}}}
\newcommand{\idldr}{\mathsf{{idl_{\odot}r}}}
\newcommand{\idrdl}{\mathsf{{idr_{\odot}l}}}
\newcommand{\idrdr}{\mathsf{{idr_{\odot}r}}}
\newcommand{\rinvdl}{\mathsf{{rinv_{\odot}l}}}
\newcommand{\rinvdr}{\mathsf{{rinv_{\odot}r}}}
\newcommand{\linvdl}{\mathsf{{linv_{\odot}l}}}
\newcommand{\linvdr}{\mathsf{{linv_{\odot}r}}}
\newcommand{\idisotwo}{\mathsf{{id}}}
\newcommand{\transtwo}{\bullet}
\newcommand{\respstwo}{\mathsf{{\boxdot}}}
\newcommand{\respptwo}{\mathsf{{resp_{\oplus\Leftrightarrow}}}}
\newcommand{\respttwo}{\mathsf{{resp_{\otimes\Leftrightarrow}}}}
\newcommand{\sumid}{\mathsf{sumid}}
\newcommand{\splitid}{\mathsf{splitid}}
\newcommand{\homps}{\mathsf{hom_{\oplus\odot}}}
\newcommand{\homsp}{\mathsf{hom_{\odot\oplus}}}
\newcommand{\sem}[1]{\llbracket #1 \rrbracket}
\newtheorem{proposition}{Proposition}
\makeatletter
\tikzset{my loop/.style = {to path={
\pgfextra{\let\tikztotarget=\tikztostart}
[looseness=8,min distance=5mm, in=240,out=300]
\tikz@to@curve@path}
}}
\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\title{Fractional Types}
\titlerunning{Fractional Types}
\author[1]{Jacques Carette}
\author[2]{Chao-Hong Chen}
\author[3]{Vikraman Choudhury}
\author[4]{Amr Sabry}
\affil[1]{Computing and Software Department, McMaster University,
Hamilton, Ontario, Canada \\ \texttt{[email protected]}}
\affil[2]{Computer Science Department, Indiana University,
Bloomington, Indiana, USA \\ \texttt{[email protected]}}
\affil[3]{Computer Science Department, Indiana University,
Bloomington, Indiana, USA \\ \texttt{[email protected]}}
\affil[4]{Computer Science Department, Indiana University,
Bloomington, Indiana, USA \\ \texttt{[email protected]}}
\authorrunning{J. Carette, C.-H. Chen, V. Choudhury and A. Sabry}
\Copyright{Jacques Carette, Chao-Hong Chen, Vikraman Choudhury and Amr Sabry}
\maketitle
\begin{abstract}
We exhibit types whose natural cardinality is fractional. More precisely, we
show that the groupoid cardinality (as defined by Baez-Dolan) of the
denotation of the type of a singleton reversible program $p$ with exactly $k$
distinct proofs of reversibility has cardinality $1/k$. We further show that
this type is naturally a multiplicative inverse to the type of all iterates
$p ^ i$ of that reversible program. We situate this work as an extension of a
larger reversible programming language ($\Pi$), and show that this extension
is also reversible.
\end{abstract}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Introduction}
In modern treatments of type theory, types have the
structure of \emph{weak $\omega$-groupoids}. As a first approximation,
we can think of such structures as sets with points (objects) and
paths (equivalences) between the points and higher paths between these
paths and so on. Here are two simple but non-trivial examples:
\begin{center}
\begin{tabular}{c@{\qquad\qquad}c}
\begin{tikzpicture}[scale=0.7,every node/.style={scale=0.7}]
\draw[dashed] (0,0) ellipse (3cm and 1.5cm);
\node[below] (A) at (-1.7,0.3) {\texttt{a}};
\node[below] (B) at (-1.1,0.3) {\texttt{b}};
\node[below] (C) at (-0.5,0.3) {\texttt{c}};
\node[below] (D) at (0.5,0.3) {\texttt{d}};
\node[below] (E) at (1.1,0.3) {\texttt{e}};
\node[below] (F) at (1.7,0.3) {\texttt{f}};
\draw[fill] (-1.7,0.3) circle [radius=0.05];
\draw[fill] (-1.1,0.3) circle [radius=0.05];
\draw[fill] (-0.5,0.3) circle [radius=0.05];
\draw[fill] (0.5,0.3) circle [radius=0.05];
\draw[fill] (1.1,0.3) circle [radius=0.05];
\draw[fill] (1.7,0.3) circle [radius=0.05];
\draw (-1.7,0.3) -- (-1.1,0.3);
\draw (-1.1,0.3) -- (-0.5,0.3);
\draw (-1.7,0.3) to[bend left] (-0.5,0.3) ;
\draw (0.5,0.3) -- (1.1,0.3);
\draw (1.1,0.3) -- (1.7,0.3);
\draw (0.5,0.3) to[bend left] (1.7,0.3) ;
\path (A) edge [my loop] node[below] {\texttt{id}} (B);
\path (B) edge [my loop] node[below] {\texttt{id}} (B);
\path (C) edge [my loop] node[below] {\texttt{id}} (B);
\path (D) edge [my loop] node[below] {\texttt{id}} (B);
\path (E) edge [my loop] node[below] {\texttt{id}} (B);
\path (F) edge [my loop] node[below] {\texttt{id}} (B);
\end{tikzpicture}
&
\begin{tikzpicture}[scale=0.6,every node/.style={scale=0.6}]
\draw[dashed] (0,0) ellipse (2cm and 2.6cm);
\node[below] (B) at (0,-1.1) {\texttt{*}};
\path (B) edge [my loop] node[below] {\texttt{id}} (B);
\path (B) edge [loop above, looseness=15, in=48, out=132] node[above] {$p$} (B);
\path (B) edge [loop above, looseness=25, in=40, out=140] node[above] {$q$} (B);
\path (B) edge [loop above, looseness=40, in=32, out=148] node[above] {$q'$} (B);
\path (-1,0.4) edge[double, bend left] node[left] {$\alpha$} (-1,1.0);
\end{tikzpicture}
\end{tabular}
\end{center}
\medskip\noindent Baez and Dolan~\cite{groupoidcard} assign to each groupoid a
\emph{cardinality} that counts the objects up to equivalences. The groupoid on
the left has six points \texttt{a}, \texttt{b}, \texttt{c}, \texttt{d},
\texttt{e}, and \texttt{f} with two groups of three points each clustered in an
equivalence class and hence the groupoid has cardinality~2. The (2-)groupoid on
the right has one point $\ast$ with four equivalences \texttt{id}, $p$, $q$,
and~$q'$ on it. The equivalences $q$ and $q'$ are however identified by~$\alpha$
leaving only three distinct isomorphism classes and hence making the cardinality
$\frac{1}{3}$.
Both groupoids involve some notion of (semantic) ``equivalence'', which we would
like to capture as first-class entities in a programming language. In other
words, we would like to have some syntactic notion of a type, a \emph{fractional
type}, whose denotation would be a groupoid with fractional cardinality. Our
aim then is to create a language which possesses these fractional types, explore
this notion of types, their equivalence, and their associated operational semantics.
The remainder of the paper is organized as follows. We start by reviewing
necessary background material, consisting of the language $\Pi$ for programming
with isomorphisms or equivalences in a reversible information-preserving way.
Sec.~4 explains the main novel semantic ideas of using $\Pi$ programs to
generate non-trivial groupoids with fractional cardinality, as well as
translating the semantic ideas into an extension of $\Pi$ with new type
constructors denoting non-trivial groupoids and new programs that manipulate
such types. The last section puts our work in perspective and concludes.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Programming with Equivalences}
\label{sec:pi}
The main syntactic vehicle for the technical developments
is the language $\Pi$ whose only computations are isomorphisms
between finite types and equivalences between these
isomorphisms~\cite{Carette2016,James:2012:IE:2103656.2103667}. We
present the syntax and operational semantics of the parts of the
language relevant to our work.
\begin{figure}[t]
\[\begin{array}{rrcll}
\unitepl :& \zt \oplus \tau & \iso & \tau &: \unitipl \\
\unitepr :& \tau \oplus \zt & \iso & \tau &: \unitipr \\
\swapp :& \tau_1 \oplus \tau_2 & \iso & \tau_2 \oplus \tau_1 &: \swapp \\
\assoclp :& \tau_1 \oplus (\tau_2 \oplus \tau_3) & \iso & (\tau_1 \oplus \tau_2) \oplus \tau_3
&: \assocrp \\
\\
\unitetl :& \ot \otimes \tau & \iso & \tau &: \unititl \\
\unitetr :& \tau \otimes \ot & \iso & \tau &: \unititr \\
\swapt :& \tau_1 \otimes \tau_2 & \iso & \tau_2 \otimes \tau_1 &: \swapt \\
\assoclt :& \tau_1 \otimes (\tau_2 \otimes \tau_3) & \iso & (\tau_1 \otimes \tau_2) \otimes \tau_3
&: \assocrt \\
\\
\absorbr :&~ \zt \otimes \tau & \iso & \zt &: \factorzl \\
\absorbl :&~ \tau \otimes \zt & \iso & \zt &: \factorzr \\
\dist :&~ (\tau_1 \oplus \tau_2) \otimes \tau_3 &
\iso & (\tau_1 \otimes \tau_3) \oplus (\tau_2 \otimes \tau_3)~ &: \factor \\
\distl :&~ \tau_1 \otimes (\tau_2 \oplus \tau_3) &
\iso & (\tau_1 \otimes \tau_2) \oplus (\tau_1 \otimes \tau_3)~ &: \factorl
\end{array}\]
\[\begin{array}{c}
\Rule{}
{}
{\jdg{}{}{\idiso : \tau \iso \tau}}
{}
\qquad\qquad
\Rule{}
{\jdg{}{}{c_1 : \tau_1 \iso \tau_2} \quad c_2 : \tau_2 \iso \tau_3}
{\jdg{}{}{c_1 \odot c_2 : \tau_1 \iso \tau_3}}
{}
\\
\\
\Rule{}
{\jdg{}{}{c_1 : \tau_1 \iso \tau_2} \quad c_2 : \tau_3 \iso \tau_4}
{\jdg{}{}{c_1 \oplus c_2 : \tau_1 \oplus \tau_3 \iso \tau_2 \oplus \tau_4}}
{}
\qquad\qquad
\Rule{}
{\jdg{}{}{c_1 : \tau_1 \iso \tau_2} \quad c_2 : \tau_3 \iso \tau_4}
{\jdg{}{}{c_1 \otimes c_2 : \tau_1 \otimes \tau_3 \iso \tau_2 \otimes \tau_4}}
{}
\end{array}\]
Each 1-combinator $c$ has an inverse $!~c$, e.g, $!~\unitepl=\unitipl$,
$!(c_1 \odot c_2) = ~!c_2 \odot~ !c_1$, etc.
\caption{$\Pi$ 1-combinators~\cite{James:2012:IE:2103656.2103667}
\label{pi-combinators}}
\end{figure}
\begin{figure}[t]
\[\begin{array}{c}
\Rule{}
{c : \tau_1 \iso \tau_2}
{\jdg{}{}{\idisotwo : c \isotwo c}}
{}
~
\Rule{}
{c_1,c_2,c_3 : \tau_1 \iso \tau_2 \quad \alpha_1 : c_1 \isotwo c_2 \quad \alpha_2 : c_2 \isotwo c_3}
{\jdg{}{}{\alpha_1~\transtwo~\alpha_2 : c_1 \isotwo c_3}}
{}
\\
\\
\Rule{}
{c_1 : \tau_1 \iso \tau_2 \quad c_2 : \tau_2 \iso \tau_3 \quad c_3 : \tau_3 \iso \tau_4}
{\jdg{}{}{\assocdl : c_1 \odot (c_2 \odot c_3) \isotwo (c_1 \odot c_2) \odot c_3 : \assocdr}}
{}
\\
\\
\Rule{}
{c : \tau_1 \iso \tau_2}
{\jdg{}{}{\idldl : \idiso \odot c \isotwo c : \idldr}}
{}
~
\Rule{}
{c : \tau_1 \iso \tau_2}
{\jdg{}{}{\idrdl : c \odot \idiso \isotwo c : \idrdr}}
{}
\\
\\
\Rule{}
{c : \tau_1 \iso \tau_2}
{\jdg{}{}{\rinvdl : ~! c \odot c \isotwo \idiso : \rinvdr}}
{}
~
\Rule{}
{c : \tau_1 \iso \tau_2}
{\jdg{}{}{\linvdl : c ~\odot ~! c \isotwo \idiso : \linvdr}}
{}
\\
\\
\Rule{}
{}
{\jdg{}{}{\sumid : \idiso\oplus\idiso \isotwo \idiso : \splitid}}
{}
\\
\\
\Rule{}
{c_1 : \tau_5\iso\tau_1 \quad c_2 : \tau_6\iso\tau_2 \quad c_3 :
\tau_1\iso\tau_3 \quad c_4 : \tau_2\iso\tau_4}
{\jdg{}{}{\homps : (c_1\odot c_3)\oplus(c_2\odot c_4) \isotwo
(c_1\oplus c_2) \odot (c_3\oplus c_4) : \homsp }}
{}
\\
\\
\Rule{}
{c_1,c_3 : \tau_1 \iso \tau_2 \quad c_2,c_4 : \tau_2 \iso \tau_3 \quad
\alpha_1 : c_1 \isotwo c_3 \quad \alpha_2 : c_2 \isotwo c_4}
{\jdg{}{}{\alpha_1 ~\respstwo~ \alpha_2 : c_1 \odot c_2 \isotwo c_3 \odot c_4}}
{}
\\
\\
\Rule{}
{c_1,c_3 : \tau_1 \iso \tau_2 \quad c_2,c_4 : \tau_2 \iso \tau_3 \quad
\alpha_1 : c_1 \isotwo c_3 \quad \alpha_2 : c_2 \isotwo c_4}
{\jdg{}{}{\respptwo ~\alpha_1 ~\alpha_2 : c_1 \oplus c_2 \isotwo c_3 \oplus c_4}}
{}
\\
\\
\Rule{}
{c_1,c_3 : \tau_1 \iso \tau_2 \quad c_2,c_4 : \tau_2 \iso \tau_3 \quad
\alpha_1 : c_1 \isotwo c_3 \quad \alpha_2 : c_2 \isotwo c_4}
{\jdg{}{}{\respttwo ~\alpha_1 ~\alpha_2 : c_1 \otimes c_2 \isotwo c_3 \otimes c_4}}
{}
\end{array}\]
Each 2-combinator $\alpha$ has an inverse $2!~\alpha$, e.g, $2!~\assocdl=\assocdr$,
$2!(\alpha_1~\transtwo~\alpha_2) = (2!~\alpha_2)~\transtwo~(2!~\alpha_1)$, etc.
\caption{$\Pi$ 2-combinators (excerpt)~\cite{Carette2016}
\label{pi-combinators2}}
\end{figure}
\begin{figure}[t]
{\footnotesize
\[\begin{array}{cc}
\begin{array}[t]{rlcl}
\evalone{\unitepl}{&(\inr{v})} &=& v \\
\evalone{\unitipl}{&v} &=& \inr{v} \\
\evalone{\unitepr}{&(\inl{v})} &=& v \\
\evalone{\unitipr}{&v} &=& \inl{v} \\
\evalone{\swapp}{&(\inl{v})} &=& \inr{v} \\
\evalone{\swapp}{&(\inr{v})} &=& \inl{v} \\
\evalone{\assoclp}{&(\inl{v})} &=& \inl{(\inl{v})} \\
\evalone{\assoclp}{&(\inr{(\inl{v})})} &=& \inl{(\inr{v})} \\
\evalone{\assoclp}{&(\inr{(\inr{v})})} &=& \inr{v} \\
\evalone{\assocrp}{&(\inl{(\inl{v})})} &=& \inl{v} \\
\evalone{\assocrp}{&(\inl{(\inr{v})})} &=& \inr{(\inl{v})} \\
\evalone{\assocrp}{&(\inr{v})} &=& \inr{(\inr{v})}
\end{array} &
\begin{array}[t]{rlcl}
\evalone{\unitetl}{&(\unitv,v)} &=& v \\
\evalone{\unititl}{&v} &=& (\unitv,v) \\
\evalone{\unitetr}{&(v,\unitv)} &=& v \\
\evalone{\unititr}{&v} &=& (v,\unitv) \\
\evalone{\swapt}{&(v_1,v_2)} &=& (v_2,v_1) \\
\evalone{\assoclt}{&(v_1,(v_2,v_3))} &=& ((v_1,v_2),v_3) \\
\evalone{\assocrt}{&((v_1,v_2),v_3)} &=& (v_1,(v_2,v_3))
\end{array} \\
\\
\begin{array}[t]{rlcl}
\evalone{\absorbr}{&(v,\_)} &=& v \\
\evalone{\absorbl}{&(\_,v)} &=& v \\
\evalone{\dist}{&(\inl{v_1},v_3)} &=& \inl{(v_1,v_3)} \\
\evalone{\dist}{&(\inr{v_2},v_3)} &=& \inr{(v_2,v_3)} \\
\evalone{\factor}{&\inl{(v_1,v_3)}} &=& (\inl{v_1},v_3) \\
\evalone{\factor}{&\inr{(v_2,v_3)}} &=& (\inr{v_2},v_3) \\
\evalone{\distl}{&(v_1,\inl{v_3})} &=& \inl{(v_1,v_3)} \\
\evalone{\distl}{&(v_2,\inr{v_3})} &=& \inr{(v_2,v_3)} \\
\evalone{\factorl}{&\inl{(v_1,v_3)}} &=& (v_1,\inl{v_3}) \\
\evalone{\factorl}{&\inr{(v_2,v_3)}} &=& (v_2,\inr{v_3})
\end{array} &
\begin{array}[t]{rlcl}
\evalone{\idiso}{&v} &=& v \\
\evalone{(c_1 \odot c_2)}{&v} &=&
\evalone{c_2}{(\evalone{c_1}{v})} \\
\evalone{(c_1 \oplus c_2)}{&(\inl{v})} &=&
\inl{(\evalone{c_1}{v})} \\
\evalone{(c_1 \oplus c_2)}{&(\inr{v})} &=&
\inr{(\evalone{c_2}{v})} \\
\evalone{(c_1 \otimes c_2)}{&(v_1,v_2)} &=&
(\evalone{c_1}v_1, \evalone{c_2}v_2)
\end{array}
\end{array}\]
\caption{\label{fig:opsem}$\Pi$ operational semantics}
}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Syntax of $\Pi$}
\label{opsempi}
The $\Pi$ family of languages is based on type isomorphisms. In the
variant we consider, the set of types $\tau$ includes the empty
type~$\zt$, the unit type $\ot$, and sum $\oplus$ and product
$\otimes$ types. The values classified by these types are the
conventional ones: $\unitv$ of type~$\ot$, $\inl{v}$ and $\inr{v}$ for
injections into sum types, and $(v_1,v_2)$ for product types. The
language has two other syntactic categories of programs to be
described in detail.
\begin{definition}[$\Pi$]
\label{def:pi}
The syntax of $\Pi$ is given by the following categories:
\[\begin{array}{lrcl}
(\textrm{Types}) &
\tau &::=& \zt \alt \ot \alt \tau_1 \oplus \tau_2 \alt \tau_1 \otimes \tau_2 \\
(\textrm{Values}) &
v &::=& \unitv \alt \inl{v} \alt \inr{v} \alt (v_1,v_2) \\
(\textrm{1-combinators}) &
c,p &:& \tau_1 \iso \tau_2 ~ [\textit{see Fig.~\ref{pi-combinators}}] \\
(\textrm{2-combinators}) &
\alpha &:& c_1 \isotwo c_2 \mbox{~where~} c_1, c_2 : \tau_1 \iso \tau_2
~[\textit{see Fig.~\ref{pi-combinators2}}]
\end{array}\]
\end{definition}
\noindent Both classes of programs, 1-combinators $c$, and
2-combinators~$\alpha$, denote \emph{equivalences} in the Homotopy Type Theory
(HoTT) sense~\cite{hottbook}. The elements $c$ or $p$ of 1-combinators denote
type isomorphisms. The elements $\alpha$ of 2-combinators denote the set of
sound and complete equivalences between these type isomorphisms. Using the
1-combinators, it is possible to write any reversible boolean function and hence
encode arbitrary boolean functions by a technique that goes back to
Toffoli~\cite{Toffoli:1980}. The 2-combinators provide a layer of programs that
computes semantics-preserving transformations of 1-combinators. As a small
example, let us abbreviate $\ot \oplus \ot$ as the type $\mathbb{2}$ of booleans
and examine two possible implementations of boolean negation. The first directly
uses the primitive combinator
$\swapp : \tau_1 \oplus \tau_2 \iso \tau_2 \oplus \tau_1$ to exchange the two
values of type~$\mathbb{2}$; the second uses three consecutive $\swapp$s to
achieve the same effect:
\[\begin{array}{rcl}
\mathsf{not_1} &=& \swapp \\
\mathsf{not_2} &=& (\swapp \odot \swapp) \odot \swapp
\end{array}\]
We can write a 2-combinator whose \emph{type} is $\mathsf{not_2}
\isotwo \mathsf{not_1}$:
\[
(\linvdl ~\respstwo~ \idisotwo)~\transtwo~\idldl
\]
which not only shows the equivalence of the two implementations of negation but
also shows \emph{how} to transform one to the other. This rewriting focuses
on the first two occurrences of $\swapp$ and uses $\linvdl$ to reduce them to
$\idiso$ since they are inverses. It then uses $\idldl$ to simplify the
composition of $\idiso$ with $\swapp$ to just $\swapp$.
Fig.~\ref{pi-combinators} lists all the 1-combinators which consist of base
combinators (top) and compositions (bottom). Each line of the base combinators
introduces a pair of dual constants\footnote{where $\swapp$ and $\swapt$ are
self-dual.} that witness the type isomorphism in the middle. This set of
isomorphisms is known to be sound and
complete~\cite{Fiore:2004,fiore-remarks}. As the full set of 2-combinators has
$113$ entries, Fig.~\ref{pi-combinators2} lists a few of the 2-combinators that
we use in this paper. Each 2-combinator relates two 1-combinators of the same
type and witnesses their equivalence. Both 1-combinators and 2-combinators are
invertible and the 2-combinators behave as expected with respect to inverses of
1-combinators.
\begin{proposition}
For any $c : \tau_1 \iso \tau_2$, we have $c \isotwo ~!~(!~c)$.
\end{proposition}
\begin{proposition}
For any $c_1,c_2 : \tau_1 \iso \tau_2$, we have $c_1 \isotwo c_2$ implies
$!~c_1 \isotwo ~!~c_2$.
\end{proposition}
%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Semantics}
\label{sec:pisem}
We give an operational semantics for the 1-combinators of $\Pi$ which
represent the conventional layer of programs. Operationally, the
semantics consists of a pair of evaluators that
take a combinator and a value and propagate the value in the forward
direction~$\triangleright$ or in the backward
direction~$\triangleleft$. We show the complete forward evaluator in
Fig.~\ref{fig:opsem}; the backward evaluator is easy to infer.
As an example, let $\mathbb{3}$ abbreviate the type
$(\ot \oplus \ot) \oplus \ot$. There are three values of
type~$\mathbb{3}$ which are $ll=\inl{\inl{\unitv}}$,
$lr=\inl{\inr{\unitv}}$, and $r=\inr{\unitv}$. Pictorially, the type
$\mathbb{3}$ with its three inhabitants can be represented as the
left-leaning tree:
\begin{center}
\begin{tikzpicture}[level distance=0.5cm]
\node {$\cdot$}
child {node {$\cdot$}
child {node {$0$}}
child {node {$1$}}
}
child {node {$2$}} ;
\end{tikzpicture}
\end{center}
Note that the values of type $\mathbb{3}$ are the names of
the paths from the root to each of the leaves. We use
$0$, $1$ and $2$ as ordinals, to give an order to each
of the values.
There are, up to equivalence, six combinators of type
$\mathbb{3} \iso \mathbb{3}$, each representing a different
permutation of three elements that leave the \emph{shape} of the three
unchanged. The six permutations on $\mathbb{3}$ can be written as
$\Pi$-terms:
\[\begin{array}{rcl}
\permone &=& \idiso \\
\permtwo &=& \swapp \oplus \idiso \\
\permthree &=& \assocrp \odot (\idiso \oplus \swapp) \odot \assoclp \\
\permfour &=& \permtwo \odot \permthree \\
\permfive &=& \permthree \odot \permtwo \\
\permsix &=& \permfour \odot \permtwo
\end{array}\]
Tracing the evaluation of $\permtwo$ on each of the possible inputs yields:
\[\begin{array}{rcl}
\evalone{(\swapp\oplus\idiso)}{\inl{\inl{\unitv}}} &=& \inl{\evalone{\swapp}{\inl{\unitv}}} \\
&=& \inl{\inr{\unitv}} \\
\\
\evalone{(\swapp\oplus\idiso)}{\inl{\inr{\unitv}}} &=& \inl{\evalone{\swapp}{\inr{\unitv}}} \\
&=& \inl{\inl{\unitv}} \\
\\
\evalone{(\swapp\oplus\idiso)}{\inr{\unitv}} &=& \inr{\evalone{\idiso}{\unitv}} \\
&=& \inr{\unitv}
\end{array}\]
Thus the effect of combinator $\permtwo$ is to swap the values
$\inl{\inl{\unitv}}$ and $\inl{\inr{\unitv}}$ leaving the value
$\inr{\unitv}$ intact. In other words, the effect of $\permtwo$
can be visualized as giving the tree:
\begin{center}
\begin{tikzpicture}[level distance=0.5cm]
\node {$\cdot$}
child {node {$\cdot$}
child {node {$1$}}
child {node {$0$}}
}
child {node {$2$}} ;
\end{tikzpicture}
\end{center}
These trees should also make it clear why mathematicians shorten
their notation to
$\begin{pmatrix}
0 & 1 & 2 \\
1 & 0 & 2 \\
\end{pmatrix}$
for the same permutation. We will not do so, as this
notation is \emph{untyped}, as it does not enforce that
the shape of the tree is preserved.
Iterating $\permtwo$ again is equivalent to the identity permutation, which can
be verified using 2-combinators: \[\begin{array}{rcl} \permtwo \odot \permtwo
&=& (\swapp \oplus \idiso) \odot (\swapp \oplus \idiso) \\ &\isotwo& (\swapp
\odot \swapp) \oplus (\idiso \odot \idiso) \\ &\isotwo& \idiso \oplus \idiso \\
&=& \idiso \end{array}\]
More generally we can iterate 1-combinators to produce different
reversible functions between finite sets, eventually wrapping around
at some number which represents the \emph{order} of the underlying
permutation.
\begin{definition}[Iterated Power of a 1-combinator]
The $k^{\mbox{th}}$ iterated power of a 1-combinator $p : \tau \iso \tau$, for
$k \in \Z$ is
\[
p^k =
\begin{cases}
\idiso & k = 0 \\
p \odot p^{k - 1} & k > 0 \\
(!~p) \odot p^{k + 1} & k < 0 \\
\end{cases}
\]
\end{definition}
\begin{definition}[Order of a 1-combinator]
\label{def:order}
The order of a 1-combinator $p : \tau \iso \tau$, $\ord{p}$, is the
least postitive natural number $k \in \N^+$ such that
$p^k \isotwo \idiso$.
\end{definition}
For our example combinators on the type $\mathbb{3}$, simple traces using the
operational semantics show the combinator $\permone$ is the identity
permutation; the combinators $\permthree$ and $\permsix$ swap two of the three
elements leaving the third intact; and the combinators $\permfour$ and
$\permfive$ rotate the three elements. We therefore have:
\[\begin{array}{rcl}
\mathit{order}(\permone) &=& 1 \\
\mathit{order}(\permtwo) = \mathit{order}(\permthree) = \mathit{order}(\permsix) &=& 2 \\
\mathit{order}(\permfour) = \mathit{order}(\permfive) &=& 3
\end{array}\]
We should note that the above definition is the only one in this paper which is not \emph{effective}. While there is an obvious method to compute it using the action of a 1-combinator on the elements of the type it acts on, this is extremely inefficient. We do not have an effective algorithm for computing it that works on the syntax of combinators. The (only) difficulty is $\odot$, which can have an arbitrary effect on the order.
The 2-combinators, being complete equivalences between
1-combinators~\cite{Carette2016}, also capture equivalences regarding
power of combinators and their order.
\begin{lemma}
\label{lem:distiterplus}
For $p : \tau\iso\tau$, $m,n\in\Z$, we have a 2-combinator
$\distiterplus{p}{m}{n} : (p^m \odot p^n) \isotwo p ^{m + n}$.
\end{lemma}
\begin{lemma}
\label{lem:ordercancel}
For $p : \tau \iso \tau$, $n \in \Z$, $p^{k + n} \isotwo p^n$ where
$k = \ord{p}$.
\end{lemma}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{From Sets to Groupoids}
\label{sec:groupoids}
From a denotational perspective, a $\Pi$ type $\tau$ denotes a finite set, a $\Pi$ 1-combinator denotes a permutation on finite sets, and the 2-combinators denote coherence conditions on these permutations~\cite{Carette2016}. Formally, the language $\Pi$ is a \emph{categorification}~\cite{math/9802029} of the natural numbers as a \emph{symmetric rig groupoid}~\cite{nlabrig}. This structure is a \emph{symmetric bimonoidal category} or a \emph{commutative rig category} in which every morphism is invertible. The underlying category consists of two symmetric monoidal structures~\cite{nla.cat-vn1051288} separately induced by the properties of addition and multiplication of the natural numbers. The monoidal structures are then augmented with distributivity and absorption natural isomorphisms~\cite{laplaza} to model the full commutative semiring (aka, commutative rig) of the natural numbers. Despite this rich structure, the individual objects in the category for $\Pi$ are just plain sets with no interesting structure. In this section we introduce, in the denotation of $\Pi$, some non-trivial groupoids which we call \emph{iteration groupoids} and \emph{symmetry groupoids}. Products of these groupoids behave as expected which ensures that a sensible compositional programming language can be designed around them.
%%%%%
\subsection{$\Pi$ Types as Sets (Discrete Groupoids)}
Each $\Pi$ type $\tau$ denotes a (structured) finite set $\sem{\tau}$ as follows:
\[\begin{array}{rcl}
\sem{\zt} &=& \bot \\
\sem{\ot} &=& \top \\
\sem{\tau_1 \oplus \tau_2} &=& \sem{\tau_1} \uplus \sem{\tau_2} \\
\sem{\tau_1 \otimes \tau_2} &=& \sem{\tau_1} \times \sem{\tau_2}
\end{array}\]
\noindent where we use $\bot$ to denote the empty set, $\top$ to denote a set
with one element, and $\uplus$ and~$\times$ to denote the disjoint union of sets
and the cartesian product of sets respectively. Each set can be viewed as a
groupoid whose objects are the set elements and with only identity morphisms on
each object. Nevertheless, the denotations of $\ot \oplus (\ot \oplus \ot)$ of
$(\ot \oplus \ot) \oplus \ot$ are not in fact equal, although they are
trivially isomorphic.
By only being able to express types whose denotations are trivial
groupoids, $\Pi$ leaves untapped an enormous amount of combinatorial structure
that is expressible in type theory. We show that with a small but deep technical
insight, it is possible to extend~$\Pi$ with types whose denotations are
not discrete.
%%%%%
\subsection{Groupoids and Groupoid Cardinality}
There are many definitions of groupoids that provide complementary perspectives
and insights. Perhaps the simplest definition to state, and the one which is
most immediately useful for our work, is that a groupoid is a category
in which every morphism has an inverse. Intuitively, such a category consists of
clusters of connected objects where each cluster is equivalent (in the
category-theoretic sense) to a group, viewed as a $1$-object category. Thus an
alternative definition of a groupoid is as a generalization of a group that
allows for individual elements
to have ``internal symmetries''~\cite{groupoidintro}. Baez et
al.~\cite{2009arXiv0908.4305B} associate with each groupoid a cardinality that
counts the elements up to these ``internal symmetries''.
\begin{definition}[Groupoid cardinality~\cite{2009arXiv0908.4305B}]
The cardinality of a groupoid $G$ is the (positive) real number:
\[
|G| = \sum_{[x]} \frac{1}{|\textsf{Aut}(x)|}
\]
provided the sum converges. The summation is over \emph{isomorphism classes}
$[x]$ of objects $x$ and $|\textsf{Aut}(x)|$ is the number of \emph{distinct}
automorphisms of $x$.
\end{definition}
\begin{figure}[t]
\begin{center}
\begin{tikzpicture}[scale=0.6,every node/.style={scale=0.8}]
\draw[dashed] (0,-0.3) ellipse (1.5cm and 2.1cm);
\node[below] (A) at (-0.5,0) {$a$};
\node[below] (C) at (0.5,0) {$c$};
\path (A) edge [my loop] node[below] {\texttt{id}} (A);
\path (C) edge [my loop] node[below] {\texttt{id}} (C);
\path (C) edge [out=140, in=40, looseness=4] (C);
\end{tikzpicture}
\qquad \qquad \qquad
\begin{tikzpicture}[scale=0.6,every node/.style={scale=0.8}]
\draw[dashed] (0,-0.3) ellipse (3cm and 2.5cm);
\node[below] (A) at (-2,0) {$a$};
\node[below] (B) at (0,0) {$b$};
\node[below] (C) at (1.5,0) {$c$};
\path (A) edge [bend left=50] (B);
\path (C) edge [out=140, in=40, looseness=4] (C);
\path (A) edge [my loop] node[below] {\texttt{id}} (A);
\path (B) edge [my loop] node[below] {\texttt{id}} (B);
\path (C) edge [my loop] node[below] {\texttt{id}} (C);
\end{tikzpicture}
\qquad \qquad \qquad
\begin{tikzpicture}[scale=0.6,every node/.style={scale=0.8}]
\draw[dashed] (0,-0.3) ellipse (2.8cm and 2.5cm);
\node[below] (A) at (-1.6,0) {$a$};
\node[below] (B) at (0,0) {$b$};
\node[below] (C) at (1.6,0) {$c$};
\path (A) edge [my loop] node[below] {\texttt{id}} (A);
\path (B) edge [my loop] node[below] {\texttt{id}} (B);
\path (C) edge [my loop] node[below] {\texttt{id}} (C);
\path (A) edge [loop above, looseness=3, in=48, out=132] (A);
\path (B) edge [loop above, looseness=3, in=48, out=132] (B);
\path (C) edge [loop above, looseness=3, in=48, out=132] (C);
\end{tikzpicture}
\end{center}
\caption{\label{fig:groupoids2}Example groupoids $G_1$, $G_2$, and $G_3$.}
\end{figure}
For plain sets, the definition just counts the elements as each element is its
own equivalence class and has exactly one automorphism (the identity). Without
quite formalizing them and relying on the informal diagrams until the next
section, we argue that each of the groupoids $G_1$, $G_2$, and $G_3$ in
Fig.~\ref{fig:groupoids2} has cardinality $\frac{3}{2}$. Groupoid~$G_1$ consists
of two isomorphism classes: class~$a$ has one object with one automorphism (the
identity) and class~$c$ has one object with two distinct automorphisms; the
cardinality is $\frac{1}{1} + \frac{1}{2} = \frac{3}{2}$. For groupoid~$G_2$, we
also have two isomorphism classes with representatives $a$ and $c$; the class
containing $a$ has two automorphisms starting from $a$: the identity and the
loop going from $a$ to~$b$ and back. By the groupoid axioms, this loop is
equivalent to the identity which means that the class containing $a$ has just
one automorphism. The isomorphism class of $c$ has two non-equivalent
automorphisms on it and hence the cardinality of $G_2$ is also
$\frac{1}{1} + \frac{1}{2} = \frac{3}{2}$. For~$G_3$, we have three isomorphism
classes, each with two non-equivalent automorphisms, and hence the cardinality
of $G_3$ is $\frac{1}{2} + \frac{1}{2} + \frac{1}{2} = \frac{3}{2}$. It is
important to note that $G_1$ and $G_2$ are categorically equivalent groupoids,
but that $G_3$ is not categorically equivalent to either $G_1$ or $G_2$. Roughly speaking
this is because the number of connected components is also a categorical
invariant of a groupoid, and here $G_1$ and $G_2$ have $2$ whilst $G_3$ has $3$.
%%%%%%%%%%%%%%%%%%%%%%%
\subsection{$\Pi$-Combinators as Automorphism Classes}
To formalize the counting above, we need, in the context of $\Pi$, a precise
definition of what it means for automorphisms to be ``distinct''. We start with
an example. Recall the type $\mathbb{3}$ with its three elements
$ll=\inl{\inl{\unitv}}$, $lr=\inl{\inr{\unitv}}$, and $r=\inr{\unitv}$. One of
the combinators of type $\mathbb{3} \iso \mathbb{3}$ is $\permtwo$. Observing
the results of applying the iterates $(\permtwo)^k$ for $k \in \Z$ on the three
elements we find:
\[\begin{array}{c@{\qquad\qquad}c}
\begin{array}{rcl}
\evalone{(\permtwo)^{2k}}{ll} &=& ll \\
\evalone{(\permtwo)^{2k}}{lr} &=& lr \\
\evalone{(\permtwo)^{2k}}{r} &=& r
\end{array} &
\begin{array}{rcl}
\evalone{(\permtwo)^{2k+1}}{ll} &=& lr \\
\evalone{(\permtwo)^{2k+1}}{lr} &=& ll \\
\evalone{(\permtwo)^{2k+1}}{r} &=& r
\end{array}
\end{array}\]
Furthermore, Lem.~\ref{lem:ordercancel} gives us the following families of
2-combinators $\alpha_{2k} : \idiso \isotwo (\permtwo)^{2k}$ and
$\alpha_{2k+1} : \permtwo \isotwo (\permtwo)^{2k+1}$. We can put these facts together to
construct a groupoid whose objects are the elements of $\mathbb{3}$, whose
1-morphisms relate $v_i$ and~$v_j$ if $\evalone{(\permtwo)^k}{v_i} = v_j$ for some
$k \in \Z$, and whose 2-morphisms are the families $\alpha_{2k}$ and
$\alpha_{2k+1}$ above. Such a construction produces the following groupoid where
each family of 1-morphisms that are identified by a family of 2-morphisms is
drawn using a thick line:
\begin{center}
\begin{tikzpicture}[scale=0.8,every node/.style={scale=0.8}]
\draw[dashed] (0,-0.3) ellipse (3cm and 2.5cm);
\node[below] (A) at (-2,0) {$ll$};
\node[below] (B) at (0,0) {$lr$};
\node[below] (C) at (1.5,0) {$r$};
\path[ultra thick] (A) edge [bend left=50] node[above] {$\permtwo$} (B);
\path[ultra thick] (C) edge [out=140, in=40, looseness=4] node[above] {$\permtwo$} (C);
\path[ultra thick] (A) edge [my loop] node[below] {\texttt{id}} (A);
\path[ultra thick] (B) edge [my loop] node[below] {\texttt{id}} (B);
\path[ultra thick] (C) edge [my loop] node[below] {\texttt{id}} (C);
\end{tikzpicture}
\end{center}
Clearly, the resulting groupoid is a reconstruction of $G_2$ in
Fig.~\ref{fig:groupoids2} using $\Pi$ types and combinators. As analyzed
earlier, this groupoid has cardinality $\frac{3}{2}$. From the perspective
of~$\Pi$, this cardinality corresponds to the number of elements in the
underlying set which is $3$ divided by the order of the combinator $\permtwo$
which is 2. It is important to note that, as Def.~\ref{def:order} states, the
calculation of the order of a 1-combinator is defined up to the equivalence
induced by 2-combinators.
%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Iteration Groupoids $\order{p}$}
The previous construction known in the literature as an \emph{action
groupoid}~\cite{groupoidintro} is quite useful: it allows us to take a set of
cardinality $N$ and a permutation on that set of order $P$ to construct a
groupoid of cardinality $\frac{N}{P}$. Although this idea allows us to construct
a groupoid of cardinality $\frac{3}{2}$ as shown above, it is not expressive
enough to construct a groupoid of cardinality, say, $\frac{1}{3}$. Indeed, if the
underlying set has only one element ($N=1$) the only permutation is the identity
and $P$ must be 1.
The construction, however, already contains the main ingredient needed for the construction of more general groupoids with fractional cardinality. This key piece is the set of iterates of a combinator which we formally define as follows.
\begin{definition}[$\iter{p}$]
For each 1-combinator $p : \tau\iso\tau$, we form the set $\iter{p}$ whose elements are triples consisting of an integer $k$, a 1-combinator $q : \tau\iso\tau$ and a 2-combinator $\alpha : q \isotwo p^k$.
\end{definition}
Each triple encodes our knowledge that we have some (arbitrary) iterate $q$ of $p$; we do not have any a priori knowledge of the actual syntactic structure of $q$, but we do know that it is equivalent to $p^k$. For example, we have:
\[\begin{array}{rcl}
\iter{\permtwo} &=& \{ \triple{0}{\idiso}{\idisotwo},\triple{1}{\permtwo}{\idrdr}, \triple{-1}{\permtwo}{\idisotwo}, \ldots \}
\end{array}\]
The idea is that $\iter{\permtwo}$ is, up to equivalence, the set of all distinct iterates $(\permtwo)^k$ of $\permtwo$. Because of the underlying group structure of automorphisms, there are, up to equivalence, only $\ord{\permtwo}$ distinct iterates in $\iter{\permtwo}$. In a proof-irrelevant setting, $\iter{\permtwo}$ is simply $\{ \idiso, \permtwo \}$. In this section and the next, we will use the elements of $\iter{p}$ in two groupoid constructions as either objects (emphasizing their ``data'' aspect) or morphisms (emphasizing their ``symmetry'' aspect).
Given a 1-combinator $p : \tau\iso\tau$, we define the groupoid $\order{p}$ as
follows. The objects are the elements of $\iter{p}$, i.e., the triples
$\triple{k}{q}{\alpha}$ indexed by integers $k$, 1-combinators
$q : \tau\iso\tau$, and 2-combinators $\alpha : q \isotwo p^k$. We then add
(reversible) morphisms between any iterates related by 2-combinators;
categorically, this will make any such objects equivalent. If $p$ has order
$o$, Lem.~\ref{lem:ordercancel} gives us a 2-combinator $\alpha$ which witnesses
that $p^i \isotwo p^{i+o}$. Thus given two iterates $\triple{i}{q_i}{\alpha_i}$
and $\triple{i+o}{q_j}{\alpha_j}$, they must be equivalent since
$\alpha_i~\bullet~\alpha~\bullet~!\,\alpha_j$ shows that $q_i \isotwo q_j$. In
other words $p^j$ will be equivalent to $p^k$ exactly when $j$ and $k$ differ by
$o$. This informal description formalizes straightforwardly.
\begin{definition}[$\order{p}$] For each 1-combinator $p : \tau\iso\tau$, we form the groupoid $\order{p}$ as follows:
\begin{itemize}
\item objects are the elements $\triple{k}{q}{\alpha}$ of $\iter{p}$;
\item there is a morphism between $\triple{k_1}{q_1}{\alpha_1}$ and $\triple{k_2}{q_2}{\alpha_2}$ for each $\alpha : q_1 \isotwo q_2$
\end{itemize}
\end{definition}
Despite its involved internal structure, the groupoid $\order{p}$ is essentially
a set of cardinality $\ord{p}$.
\begin{lemma}
$|\order{p}| = \ord{p}$
\end{lemma}
\begin{proof}
Let $o = \ord{p}$. There are $o$ isomorphism classes of
objects. Consider an object $x = \triple{k}{q}{\alpha}$, its
isomorphism class $[x] = \triple{k+io}{q_i}{\alpha_i}$ where
$i \in \Z$. The group $\textsf{Aut}(x)$ is the group generated by
$\idisotwo$ and has order 1. Hence
$|\order{p}| = \sum\limits_{1}^{o}\frac{1}{1} = o$.
\end{proof}
As an example, the groupoid $\order{(\permtwo)}$ can be represented as follows. Up to equivalence, this groupoid is indeed equivalent to a set with two elements $\idiso$ and $\permtwo$.
\begin{center}
\begin{tikzpicture}[scale=0.6,every node/.style={scale=0.6}]
\draw[dashed] (0,-0.3) ellipse (9cm and 2.5cm);
\node[below] at (-8,0) {$\ldots$};
\node[below] (A) at (-6,0) {$< -2 , \idiso , \ldots >$};
\node[below] (B) at (-3,0) {$< -1 , \permtwo , \ldots >$};
\node[below] (C) at (0,0) {$< 0, \idiso, \idisotwo >$};
\node[below] (D) at (3,0) {$< 1 , \permtwo , \ldots >$};
\node[below] (E) at (6,0) {$< 2 , \idiso , \ldots >$};
\node[below] at (8,0) {$\ldots$};
\path[ultra thick] (A) edge [my loop] node[below] {\texttt{id}} (A);
\path[ultra thick] (B) edge [my loop] node[below] {\texttt{id}} (B);
\path[ultra thick] (C) edge [my loop] node[below] {\texttt{id}} (C);
\path[ultra thick] (D) edge [my loop] node[below] {\texttt{id}} (D);
\path[ultra thick] (E) edge [my loop] node[below] {\texttt{id}} (E);
\path[ultra thick] (A) edge [bend left=50] node[above] {$\alpha_{-2,0}$} (C);
\path[ultra thick] (C) edge [bend left=50] node[above] {$\alpha_{0,2}$} (E);
\path[ultra thick] (B) edge [bend left=-50] node[below] {$\alpha_{-1,1}$} (D);
\end{tikzpicture}
\end{center}
%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Symmetry Groupoids $\iorder{p}$}\label{sec:symmetryg}
The elements of $\iter{p}$ form a group under the following operation:
\[
\triple{k_1}{p_1}{\alpha_1} ~\circ~ \triple{k_2}{p_2}{\alpha_2} =
\triple{k_1+k_2}{p_1 \odot p_2}{(\alpha_1 ~\respstwo~
\alpha_2)~\transtwo~(\distiterplus{p}{k_1}{k_2})}
\]
where $\distiterplus{p}{k_1}{k_2}$ is defined in
Lem.~\ref{lem:distiterplus}. The common categorical representation of a group is
a category with one trivial object and the group elements as morphisms on that
trivial object. Our construction of the groupoid $\iorder{p}$ is essentially the
same.
\begin{definition}[$\iorder{p}$] For each 1-combinator $p : \tau\iso\tau$, we form the 2-groupoid $\iorder{p}$ as follows:
\begin{itemize}
\item the objects are the iterates of the identity combinator on $\tau$;
\item the morphisms between every pair of objects are the elements of $\iter{p}$;
\item there is a 2-morphism between 1-morphisms $\triple{k_1}{q_1}{\alpha_1}$ and $\triple{k_2}{q_2}{\alpha_2}$ for every $\alpha : q_1 \isotwo q_2$.
\end{itemize}
\end{definition}
Note that for each power $p ^ i$ of $p$, there is a morphism
$\triple{k}{q}{\alpha}$ in $\iter{p}$ such that $q$ annihilates $p^i$ to the
identity.
\paragraph*{Remark.} Note also that everything is well-defined even if we choose
$p : \zt\iso\zt$. In that case, the cardinality is 1.
\begin{lemma}
$|\iorder{p}| = \frac{1}{\ord{p}}$
\end{lemma}
\begin{proof}
Let $o = \ord{p}$. The objects form one isomorphism class
$[p]$. There are, up to equivalence, exactly $\ord{p}$ distinct
morphisms on this equivalence class. Hence, the group
$\textsf{Aut}([p])$ is the group generated by
$p^0, p^1 \dots p^{o-1}$, and the cardinality $|\iorder{p}|$ is
$\frac{1}{o}$.
\end{proof}
As an example, the groupoid $\iorder{(\permtwo)}$ can be represented as follows.
\begin{center}
\begin{tikzpicture}[scale=0.6,every node/.style={scale=0.6}]
\draw[dashed] (0,-0.3) ellipse (9cm and 3cm);
\node[below] at (-8,0) {$\ldots$};
\node[below] (A) at (-6,0) {$< -2 , \idiso , \ldots >$};
\node[below] (B) at (-3,0) {$< -1 , \idiso , \ldots >$};
\node[below] (C) at (0,0) {$< 0, \idiso, \idisotwo >$};
\node[below] (D) at (3,0) {$< 1 , \idiso , \ldots >$};
\node[below] (E) at (6,0) {$< 2 , \idiso , \ldots >$};
\node[below] at (8,0) {$\ldots$};
\path[ultra thick] (A) edge [my loop] node[below] {$\idiso$} (A);
\path[ultra thick] (B) edge [my loop] node[below] {$\idiso$} (B);
\path[ultra thick] (C) edge [my loop] node[below] {$\idiso$} (C);
\path[ultra thick] (D) edge [my loop] node[below] {$\idiso$} (D);
\path[ultra thick] (E) edge [my loop] node[below] {$\idiso$} (E);
\path[ultra thick] (A) edge [bend left=50] node[above] {$\alpha_{-2,0}$} (B);
\path[ultra thick] (B) edge [bend left=50] node[above] {$\alpha_{0,2}$} (C);
\path[ultra thick] (C) edge [bend left=50] node[above] {$\alpha_{0,2}$} (D);
\path[ultra thick] (D) edge [bend left=50] node[above] {$\alpha_{0,2}$} (E);
\path[ultra thick] (A) edge [out=-140, in=-40, looseness=10] node[below] {$\permtwo$} (A);
\path[ultra thick] (B) edge [out=-140, in=-40, looseness=10] node[below] {$\permtwo$} (B);
\path[ultra thick] (C) edge [out=-140, in=-40, looseness=10] node[below] {$\permtwo$} (C);
\path[ultra thick] (D) edge [out=-140, in=-40, looseness=10] node[below] {$\permtwo$} (D);
\path[ultra thick] (E) edge [out=-140, in=-40, looseness=10] node[below] {$\permtwo$} (E);
\end{tikzpicture}