forked from briandealwis/ubcdiss
-
Notifications
You must be signed in to change notification settings - Fork 1
/
POPL23.tex
2099 lines (1832 loc) · 97.3 KB
/
POPL23.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
%% For double-blind review submission, w/o CCS and ACM Reference (max submission space)
\documentclass[acmsmall,review,anonymous]{acmart}\settopmatter{printfolios=true,printccs=false,printacmref=false}
%% For double-blind review submission, w/ CCS and ACM Reference
%\documentclass[acmsmall,review,anonymous]{acmart}\settopmatter{printfolios=true}
%% For single-blind review submission, w/o CCS and ACM Reference (max submission space)
%\documentclass[acmsmall,review]{acmart}\settopmatter{printfolios=true,printccs=false,printacmref=false}
%% For single-blind review submission, w/ CCS and ACM Reference
%\documentclass[acmsmall,review]{acmart}\settopmatter{printfolios=true}
%% For final camera-ready submission, w/ required CCS and ACM Reference
%\documentclass[acmsmall]{acmart}\settopmatter{}
%% Journal information
%% Supplied to authors by publisher for camera-ready submission;
%% use defaults for review submission.
\acmJournal{PACMPL}
\acmVolume{7}
\acmNumber{POPL}
\acmArticle{0}
\acmYear{2023}
\acmMonth{1}
\acmDOI{} % \acmDOI{10.1145/nnnnnnn.nnnnnnn}
\startPage{1}
%% Copyright information
%% Supplied to authors (based on authors' rights management selection;
%% see authors.acm.org) by publisher for camera-ready submission;
%% use 'none' for review submission.
\setcopyright{none}
%\setcopyright{acmcopyright}
%\setcopyright{acmlicensed}
%\setcopyright{rightsretained}
%% Bibliography style
\bibliographystyle{ACM-Reference-Format}
\citestyle{acmauthoryear}
%% Packages
\usepackage{style}
%% Newbox in style is too big, so we renew the new
\newlength{\fboxsepdefault}
\setlength{\fboxsepdefault}{\fboxsep}
\renewcommand{\new}[1]{%
\setlength{\fboxsep}{3pt}%
\colorbox{newcolour}{#1}%
\setlength{\fboxsep}{\fboxsepdefault}%
}
%% Tricks for LaTeX shrinking
\setlength{\abovedisplayskip}{1pt}
\setlength{\belowdisplayskip}{1pt}
\setlength{\textfloatsep}{5pt}
\setlength{\intextsep}{5pt}
\setlist{labelindent=\parindent,leftmargin=*}
\captionsetup{skip=5pt}
%\renewcommand{\fbox}[1]{\relax}
%% Title information
\title{A Syntactic Model of Sized Dependent Types}
%% Author information
\author{Jonathan Chan}
\orcid{0000-0003-0830-3180}
\affiliation{
%\position{}
\department{Department of Computer Science}
\institution{University of British Columbia}
\streetaddress{2329 West Mall}
\city{Vancouver}
\state{British Columbia}
\postcode{V6T 1Z4}
\country{Canada}
}
\email{[email protected]}
\author{William J. Bowman}
\orcid{0000-0002-6402-4840}
\affiliation{
\position{Assistant Professor}
\department{Department of Computer Science}
\institution{University of British Columbia}
\streetaddress{2329 West Mall}
\city{Vancouver}
\state{British Columbia}
\postcode{V6T 1Z4}
\country{Canada}
}
\email{[email protected]}
%% Abstract
%% Note: \begin{abstract}...\end{abstract} environment must come
%% before \maketitle command
\begin{abstract}
Contemporary proof assistants based on dependent type theories
restrict nonterminating recursive functions to ensure logical consistency of the type theory
and decidability of type checking.
This is done via guard predicates that only allow stucturally recursive functions
recurring on syntactically smaller arguments.
However, guard predicates are restrictive and reject functions
that aren't structurally recursive but are obviously terminating,
creating extra work for the programmer to convince the guard checker.
An alternative is sized types, a type-based termination checking method
where inductively-defined types are annotated with sizes.
Type checking guarantees that functions recur only on arguments whose types have smaller sizes,
rather than on arguments that syntactically appear smaller,
accepting more terminating functions.
Some existing models of sized dependent type theories
support features for more expressive sized types,
namely higher-rank size quantification
(which allows for passing around size-preserving functions)
and bounded size quantification
(which eliminates the need for complex semi-continuity checks),
but unfortunately none support both simultaneously.
We present a sized dependent type theory with higher-rank and bounded sizes (\lang),
and prove its logical consistency with a syntactic model:
by compiling \lang into the Extensional Calculus of Inductive Constructions (\CICE),
a variant of the type theory on which Coq is based,
and showing that this translation is type preserving,
the consistency of \lang follows from the consistency of \CICE.
\end{abstract}
%% 2012 ACM Computing Classification System (CSS) concepts
%% Generate at http://dl.acm.org/ccs/ccs.cfm
\begin{CCSXML}
<ccs2012>
<concept>
<concept_id>10003752.10003790.10011740</concept_id>
<concept_desc>Theory of computation~Type theory</concept_desc>
<concept_significance>500</concept_significance>
</concept>
<concept>
<concept_id>10003752.10010124.10010125.10010130</concept_id>
<concept_desc>Theory of computation~Type structures</concept_desc>
<concept_significance>500</concept_significance>
</concept>
</ccs2012>
\end{CCSXML}
\ccsdesc[500]{Theory of computation~Type theory}
\ccsdesc[500]{Theory of computation~Type structures}
%% End of generated code
%% Keywords
%% comma separated list
\keywords{type theory, dependent types, sized types, consistency}
\begin{document}
\maketitle
\section{Introduction} \label{sec:introduction}
In proof assistants such as Coq, Agda, and Idris that are founded on the
types-as-propositions paradigm, logical inconsistency is easily derivable in the
presence of unrestricted recursion:
given the universe $\Prop$ of all propositions,
the recursive function $f = \fun{P}{\Prop}{\app{f}{P}}$ can be assigned the type $\funtype{P}{\Prop}{P}$,
enabling proving any proposition,
and furthermore reduces endlessly:
$$\fun{P}{\Prop}{\app{f}{P}} \ \rhd \ \fun{P}{\Prop}{\app{(\fun{P}{\Prop}{\app{f}{P}})}{P}} \ \rhd \ \fun{P}{\Prop}{\app{f}{P}} \ \rhd \ \dots$$
Therefore, these proof assistants impose restrictions on recursive functions
using \emph{termination checks},
since for their underlying type theories, consistency coincides with the termination of all functions\punctstack{.}%
\footnote{Some proof assistants may be consistent while having nonterminating functions,
such as Lean 3~\citep{impred-proof-irrel}, due to interactions between definitional proof irrelevance
and impredicativity.}
The usual termination checks are done syntactically using a \emph{guardedness check}:
recursion must be done on inductively-defined types,
and recursive calls must occur on syntactic subarguments.
For instance, if a recursive function on naturals is given the successor $\app{\succ*}{n}$,
then it must recur on $n$;
if a recursive function on cons lists is given a nonempty list $\app{\cons*}{\hd}{\tl}$,
then it must recur on the tail $\tl$.
While the guardedness check, being based on the elimination principles of inductive types,
yields terminating functions and a consistent type theory,
it disallows calling some other function on a subargument prior to a recursive call,
even if that function doesn't change the structure of that subargument,
because the input of the recursive call is no longer exactly the syntactic subargument.
For instance, a recursive function on lists cannot first map over the sublist,
even though the usual map function doesn't change the length of a list
and therefore presents no threat to termination.
Some proof assistants (Coq, for one) inline and even unfold recursive functions
so that the guardedness check has more information.
However, this too has disadvantages:
\begin{itemize}
\item Programs become nonmodular and noncompositional:
recursive functions that require inlining
depend on the implementation of inlined functions,
and a minor syntactic change otherwise equivalent to the original
can violate guardedness~\citep{CIC-hat-minus}.
\item As aptly summarized by \citet{coqterm},
\begin{quote}
\textit{{\rm [\ldots]} unfold{\rm [ing]} all the definitions used in the body of the function, do{\rm [ing]} reductions, e.t.c.
{\rm [\ldots]} makes typechecking extremely slow at times.
Also, the unfoldings can cause the code to bloat by orders of magnitude and become impossible to debug.}
\end{quote}
\end{itemize}
When the guardedness check fails,
the programmer can refactor the function to recur on a different, related argument,
such as the length of a list in the example above,
but this requires additional work and may bloat programs
with extra code solely for satisfying the guardedness check.
Alternatives that avoid these issues are type-based checks,
where the type system itself is augmented so that type checking
guarantees consistency without syntactically analyzing function bodies.
This paper discusses one alternative, \emph{sized typing}\punctstack{,}%
\footnote{The other common one uses \emph{guarded types}~\citep{guarded-types}.}
where inductive types are annotated with size information,
and constructors construct constructions whose size is larger than that of their arguments:
the notion of a ``smaller'' argument is then encoded within the types.
Finally, a recursive function is well typed only if it recurs on an argument
whose size is smaller than that of the original argument.
Because well typedness is the only required condition,
calling some other function before a recursive call is allowed
as long as that function's type indicates it is \emph{size preserving}.
For example, in the list mapping example,
if the sublist (whose elements have type $\tau$) has size $s$,
then the mapping function must have type $\arr*{\List{\tau}{s}}{\List{\tau}{s}}$.
The implementation of the map isn't required, merely its type, restoring
modularity and compositionality.
We introduce Sizey \textsc{mc}Type Theory (\lang),
a sized dependent type theory that we prove to be logically consistent
and therefore suitable for both recursive programming and theorem proving.
We do so using a \emph{syntactic model}:
\lang is first translated into a target type theory in whose consistency we have more confidence,
and then we prove this translation to be \emph{type preserving}.
By type preservation, consistency of the target type theory implies that of the source.
But what need is there for yet another sized type system?
Since \citet{hughes}, there has been a little over two decades' worth of past work on sized types.
\citet{flationary} notes that it has been the topic of at least five dissertations.
This paper alone names eight different type theories with sized types.
There have been many advancements along the way,
but there still remains many open problems,
especially when it comes to dependent types.
\lang does not aim to resolve all the existing problems with sized types.
Our purpose is twofold:
\begin{itemize}
\item To fill an existing gap in sized dependent type theories.
Two modern sized type features are higher-rank and bounded size quantification,
and there exist a few dependent type theories with one or the other,
but not both, to our knowledge.
On the other hand, they are currently in Agda's implementation of sized types.
By proving the consistency of \lang, a novel type theory with these two features,
we bring the theoretical state-of-the-art closer to practice,
while increasing trust in the corresponding sublanguage of sized types in Agda.
\item To demonstrate the viability of using a syntactic model to prove consistency
of a sized type theory.
Doing so from scratch through a set-theoretic model is notoriously difficult,
and often requires sacrifices to and constraints on the type theory;
see Sacchini's dissertation~\citep{CIC-hat-minus} or \CIChatstar~\citep{CIC-hat-star},
for instance.
Meanwhile, a syntactic model into a similar type theory lets us highlight
the differences unique to sized types.
\end{itemize}
\subsection{Related Work}
There are many variations on dependent type theory;
in this paper, we refer to five specific works:
\begin{itemize}
\item The Predicative Calculus of Inductive Constructions~\citep{pCIC} (pCIC)
is a dependent type theory with a cumulative universe hierarchy,
definitions, and inductive types.
\item The Predicative Calculus of Cumulative Inductive Constructions~\citep{pCuIC} (pCuIC)
further adds cumulativity between inductive types to pCIC.
It comes the closest to formally describing the underlying type theory of Coq.
\item The MetaCoq project~\citep{MetaCoq} mechanizes pCuIC
and some metatheory in Coq.
\item Martin-L\"of type theory~\citep{MLTT} (MLTT) has a noncumulative universe hierarchy
along with some basic types:
the empty type, the unit type, dependent pair types, disjoint sum types,
a propositional equality type, the naturals, and W types.
\item Extensional Type Theory~\citep{CICE} (ETT) has a noncumulative universe hierarchy,
dependent pair types, a propositional equality type,
and equality reflection.
\end{itemize}
We model \lang in the Extensional Calculus of Inductive Constructions (\CICE),
which is based on pCIC with extensionality inspired by ETT.
On the other hand, \lang is based on MetaCoq's presentation of type theory,
but with naturals and well-founded trees%
\footnote{We refer to the inhabitants of W types as well-founded trees.}
in place of inductive types,
since generalizing to inductive types provides no additional insight for a lot
more irrelevant detail.
Supporting inductive types would be straightforward, since \CICE already supports them.
Sized types have a similarly long history;
the first sized dependent type theory is \CIChat~\citep{CIC-hat}
which adds implicit and fully-inferrable sized types to pCIC.
\cref{tab:sized-types} lists the sized dependent type theories that follow \CIChat,
and the main features we study:
explicit size quantification,
higher-rank size quantification,
bounded size quantification,
and consistency.
We introduce and motivate these features in the next subsection.
\begin{table}[h]
\centering
\small
\begin{tabular}{l c c c c}
Type theory & Explicit sizes? & Higher-rank? & Bounded? & Consistent? \\
\hline
\CIChat~\citep{CIC-hat} & \crossmark & \crossmark & \crossmark & \interromark \\
\CIChatminus~\citep{CIC-hat-minus} & \crossmark & \crossmark & \crossmark & \checkmark* \\
\CChatomega~\citep{CC-hat-omega} & \crossmark & \crossmark & \crossmark & \checkmark* \\
\CIChatl~\citep{CIC-hat-l} & \crossmark & \crossmark & \crossmark & \checkmark* \\
\CIChatsub~\citep{CIC-hat-sub} & \crossmark & \crossmark & \checkmark* & \checkmark* \\
\CIChatstar~\citep{CIC-hat-star} & \crossmark & \crossmark & \crossmark & \interromark \\
\citet{NbE} & \checkmark* & \checkmark* & \crossmark & \checkmark* \\
MiniAgda~\citep{MiniAgda, flationary} & \checkmark* & \checkmark* & \checkmark* & \crossmark \\
\textbf{\lang} & \checkmark* & \checkmark* & \checkmark* & \checkmark*
\end{tabular}
\caption{Sized dependent type theories and their properties}
\label{tab:sized-types}
\end{table}
\vspace{-2ex}
\subsection{Overview}
Sized types consist of explicit size quantification $\Funtype{\alpha}{\tau}$,
size abstraction $\Fun{\alpha}{e}$, and size application $\App{e}{s}$.
Sizes consist of size variables, a base size $\circ$
and a size successor operator $\sss{s}$.
Size expressions are \emph{not} terms,
and their quantifications, abstractions, and applications
are syntactically distinct from those of terms,
similar to how, in nondependent polymorphic type systems,
types are distinct from terms.
Explicit sizes differs from prior sized type systems which,
extending the type polymorphism analogy,
support only implicit \emph{rank-1} or
\emph{prenex} size quantification:
size quantifications never appear inside a type,
and all size abstractions and applications are fully inferred.
Explicit sizes, in contrast, let us express
\emph{higher-rank} size quantification,
which is more expressive.
For instance, consider a list parametrized over some sized type $\tau$:
one could write a size-preserving mapping function over a list
of type
$\Funtype{\alpha}{\arr*{(\Funtype{\beta}{\arr*{\App{\tau}{\beta}}{\App{\tau}{\beta}}})}{\app{\List*}{(\App{\tau}{\alpha})}}{\app{\List*}{(\App{\tau}{\alpha})}}}$.
We also include \emph{bounded} size quantification $\Funtype<{\alpha}{s}{\tau}$
and abstraction $\Fun<{\alpha}{s}{e}$.
An order on sizes is induced by these bound instantiations and the successor operator;
this order has nothing to do with subtyping,
and in particular we do \emph{not} have subtyping relations between
$\N{\alpha}$ and $\N{\sss{\alpha}}$, for instance.
Bounded quantification yields a natural typing rule for fixpoint expressions,
which recur on smaller sizes according to the order:
%
\begin{mathpar}
\inferrule[]{
\check{\Phi, \alpha; \Gamma, f: \Funtype<{\beta}{\alpha}{\subst{\tau}{\alpha}{\beta}}}{e}{\tau}
}{
\infer{\Phi; \Gamma}{\fix{f}{\alpha}{\tau}{e}}{\Funtype{\alpha}{\tau}}
}
\end{mathpar}
Without bounded quantification,
occurrences of $\alpha$ in $\tau$ must be restricted by
complex \emph{semi-continuity} requirements to avoid inconsistencies; via
\citet{flationary},
\begin{quote}
\textit{A technical condition like semi-continuity can kill a system
as a candidate for the foundation of logics and programming
{\rm [\ldots]} Most systems for type-based termination replace semi-continuity by a rough approximation,
trading expressivity for simplicity}
\end{quote}
Notably, these three features are all found in Agda's implementation of sized types.
On the other hand, \lang excludes Agda's \emph{infinite size},
since its properties and its use are known to be inconsistent.
While applications of the infinite size to finitely branching
inductives such as naturals and lists can be recovered with \emph{existential size quantification},
this isn't possible for infinitely-branching inductives
such as ordinals and well-founded trees.
We give a concrete example of an inexpressible infinitary construct in \cref{subsec:examples:limitations},
and discuss where using existential sizes breaks down for well-founded trees in \cref{subsec:infinity}.
In the syntactic model, sizes and their order are translated to inductive definitions in \CICE,
and quantification over (bounded) sizes is translated
to abstractions over the translation of sizes (and their orders).
The entire translation must be \emph{type preserving}:
if some term $e$ is well typed under some environments $\Phi; \Gamma$ with some type $\tau$,
then the translated term $\compile{e}$ must also be well typed
under the translated environment $\compile{\Phi}, \compile{\Gamma}$
with the translated type $\compile{\tau}$.
By the consistency of \CICE and a type-preserving translation to it,
we can prove the consistency of \lang:
there exists no term $e$ such that
$\type{\mt \mathbin{;} \mt}{e}{\funtypeT{\PT}{\PropT}{\PT}}$.
\begin{center}
\mbox{* * *}
\end{center}
\noindent In \cref{sec:sized-dep-types}, we describe the syntax and judgements of \lang in detail
and provide examples of sized types in \lang.
In \cref{sec:model}, we describe \CICE and define the translation from \lang to \CICE.
We explain the proof architecture and metatheoretical properties of \lang and
CICE in \cref{sec:proofs}, summarizing the proofs, but explaining key cases in
detail.
We conclude in \cref{sec:discussion}, discussing some shortcomings of \lang,
particularly regarding universe levels of the inductive types and the lost
expressivity without the infinite size, and directions for future investigation.
Throughout, initial appearances of new notation are highlighted in grey.
We use ellipses \new{$\seq$} as metanotation for denoting a repeated sequence of some syntactic construct,
overlines \new{$\vec{\phantom{I}}$} for sequences of variables or terms specifically (\eg $\vec{z}$, $\vec{p}$),
and \new{$\mt$} for denoting an empty sequence (particularly for environments).
Irrelevant constructs are omitted using an underscore \new{$\any$}.
Metafunctions are introduced in the prose as needed.
\section{Sized Dependent Types} \label{sec:sized-dep-types}
\input{figures/sized.tex}
\input{figures/ind.tex}
We present first the sublanguage of \lang without any inductives
to get the preliminary details out of the way first,
and also to show that the sublanguage is independent of the inductives chosen.
After that, we add naturals and well-founded trees as representative inductive types,
followed by examples of programming with sized dependent types.
\subsection{Base \lang}
\FigSyntax{fig:syntax}
\cref{fig:syntax} gives the syntax of \lang, consisting of universes $U$, sizes $s$, and terms $e$,
the latter of which includes term functions and size abstractions.
Note that the hierarchy of universes above $\Prop$ start at $\Type{1}$, not $\Type{0}$.
Most judgements use two environments: a term environment $\Gamma$ with assumptions $\annot{x}{\tau}$
and type-annotated definitions $\define{x}{\tau}{e}$,
and a size environment $\Phi$ with unbounded and bounded size variables.
We also use the assumption environment
$\Delta$ as a shorthand when writing nested expressions with assumptions;
in particular, letting for instance $\Delta_{xy} = \annot{x}{\sigma_1}, \annot{y}{\sigma_2}$,
We use \new{$\arr*{\Delta_{xy}}{\tau}$} to mean $\funtype{x}{\sigma_1}{\funtype{y}{\sigma_2}{\tau}}$.
Similarly, we use \new{$\arr*{\sigma}{\tau}$} to mean the nondependent function type $\funtype{\any}{\sigma}{\tau}$
As a loose convention, we use $\tau, \sigma$ for type-like terms,
$P$ for the motive of eliminators,
$z$ for variables representing constructor arguments, and
$f, g$ for variables representing functions.
As mentioned in \cref{sec:introduction}, the judgement forms of \lang include
reduction, its various closures, $\alpha$-cumulativity, subtyping, and typing.
On top of those, there are also judgement forms for subsizing, sizes,
and well formedness of size and
term environments \fbox{$\wf{}{\Phi}$}, \fbox{$\wf{\Phi}{\Gamma}$};
since well formedness rules are straightforward I omit them here,
but they can be found in \cref{app:cong:red}.
\begin{figure}[h]
\centering
\begin{mathpar}
\fbox{$\red{\Phi; \Gamma}{e}{e}$} \qquad
\fbox{$\red*{\Phi; \Gamma}{e}{e}$} \hfill
\inferrule[]{
(\define{x}{\tau}{e}) \in \Gamma
}{
\red{\Phi; \Gamma}{x}{e}
}
\and \inferrule[]{~}{\red{\Phi; \Gamma}{\app{(\fun{x}{\tau}{e})}{e'}}{\subst{e}{x}{e'}}}
%\and \inferrule[]{~}{\red{\Phi; \Gamma}{\J{}{d}{\refl{}}}{d}}
\and \inferrule[]{~}{\red{\Phi; \Gamma}{\App{(\Fun{\alpha}{e})}{s}}{\subst{e}{\alpha}{s}}}
\and \inferrule[]{~}{\red{\Phi; \Gamma}{\App{(\Fun<{\alpha}{r}{e})}{s}}{\subst{e}{\alpha}{s}}}
\and \inferrule[]{~}{\red{\Phi; \Gamma}{\letin{x}{\tau}{e'}{e}}{\subst{e}{x}{e'}}}
%\and \inferrule[]{~}{\red{\Phi; \Gamma}{\unpair{\alpha}{x}{\Pair{s}{e'}}{e}}{\subst{e}{\alpha, x}{s, e'}}}
\and \inferrule*[right=\rlabel{$\rhd$-cong}{red-cong}]{
\red{\Phi'; \Gamma'}{e'}{e''}
}{
\red{\Phi; \Gamma}{\subst{e}{x}{e'}}{\subst{e}{x}{e''}}
}
\\
\inferrule*[right=\rlabel{$\rhd^*$-once}{red*-once}]{
\red{\Phi; \Gamma}{e_1}{e_2}
}{
\red*{\Phi; \Gamma}{e_1}{e_2}
}
\quad
\inferrule*[right=\rlabel{$\rhd^*$-refl}{red*-refl}]{~}{\red*{\Phi; \Gamma}{e}{e}}
\quad
\inferrule*[right=\rlabel{$\rhd^*$-trans}{red*-trans}]{
\red*{\Phi; \Gamma}{e_1}{e_2} \\
\red*{\Phi; \Gamma}{e_2}{e_3}
}{
\red*{\Phi; \Gamma}{e_1}{e_3}
}
\end{mathpar}
\caption{Reduction rules (base \lang)}
\label{fig:reduction}
\end{figure}
The reduction rules and their reflexive, transitive closure are described in \cref{fig:reduction}.
\new{$\subst{e}{x}{e'}$} denotes capture-avoiding substitution of $x$ for $e'$ within $e$,
and correspondingly \new{$\subst{e}{x_1, \seq, x_n}{e_1, \seq, e_n}$} denotes simultaneous substitution.
For every syntactic form of a composite term there is a corresponding congruence rule,
which is summarized by \rref{red-cong} using substitution;
the full set of rules can be found in \cref{app:cong:red}.
By convention, the reduction rules for function applications are also referred to as $\beta$-reduction,
for $\kw{let}$ expressions as $\zeta$-reduction,
and for defined variables as $\delta$-reduction.
\FigSubtype{fig:subtyping}
Rather than a single subtyping judgement like in \GCC,
we use the same presentation as MetaCoq
and split it into a subtyping judgement
and a separate $\alpha$-cumulativity judgement,
listed in \cref{fig:subtyping}.
This overcomes some technical proof complications that appear in
the single-judgement presentation due to the transitivity rule.
Aside from the expected cumulativity of universes,
the function type and size quantification are covariant in the codomain
(\ie are $\alpha$-cumulative when their codomains are),
while remaining invariant in the domain.
All other types are invariant, as reflected by \rref{acum-refl}.
A term is then a subtype of another if they are confluent up to $\alpha$-cumulativity.
\cref{fig:subsizing} describes a preorder on sizes such that
the successor operator is monotonic with respect to the order.
The base size $\circ$ is smaller than all sizes,
and the strict preorder $\bound{\alpha}{s}$ arising from bounded quantification or abstraction
is defined as $\sss{\alpha} \mathrel{\leqslant} s$.
An additional size judgement ensures well scopedness of sizes.
\FigSubsize{fig:subsizing}
\FigTyping[h]{fig:typing}
Finally, the typing rules for the base \lang are given in \cref{fig:typing}.
They use two metafunctions defined by
$\axioms{\Prop} = \Type{1}$ and $\axioms{\Type{i}} = \Type{i+1}$;
and by $\rules{U}{\Prop} = \Prop$, $\rules{\Prop}{U} = U$, and $\rules{\Type{i}}{\Type{j}} = \Type{\maximum{i, j}}$.
\rref{var, univ, let} are the usual rules for variables, universes, and $\kw{let}$ expressions in \GCC,
while \rref{pi, lam, app} are the usual ones for functions.
\rref{conv} uses the subtyping judgement and essentially allows casting a term
from one type to a supertype as needed.
\rref{forall, forall<, slam, slam<, sapp, sapp<} are the new rules relevant to sized types,
describing bound and unbound size quantification, abstraction, and application,
which work similarly to functions.
Of note is the bounded size application rule,
which only allows applications to smaller sizes following the subsizing judgement.
\subsection{Inductive Types: Naturals and Well-Founded Trees} \label{subsec:ind-types}
\FigSyntaxInd{fig:syntax-ind}
\cref{fig:syntax-ind} extends the grammar with sized naturals, sized well-founded trees,
$\kw{case}$ expressions, and fixpoint expressions.
Informally, borrowing syntax from the definition of general inductives,
sized naturals and well-founded trees can be thought of as being defined by the following:
\begin{align*}
&\data{\N{\alpha}}{\Type{1}} && \data{\App{\app{\W*}{(\annot{A}{\Type{i}})}{(\annot{B}{\arr*{A}{\Type{i}}})}}{\alpha}}{\Type{i+1}} \\
&\quad \annot{\zero*}{\Funtype<{\beta}{\alpha}{\N{\alpha}}} && \quad \annot{\sup*}{\Funtype<{\beta}{\alpha}{\arr{x}{A}{\arr*{(\arr*{\app{B}{x}}{\app{\App{\W*}{\beta}}{A}{B}})}{\App{\app{\W*}{A}{B}}{\alpha}}}}} \\
&\quad \annot{\succ*}{\Funtype<{\beta}{\alpha}{\arr*{\N{\beta}}{\N{\alpha}}}}
\end{align*}
The types of naturals and well-founded trees can then be considered to be (nonuniformly) parametrized by a size,
and constructing an element of that type requires providing a strictly smaller size,
which is the size%
\footnote{The ``size of'' some construction is more precisely the size by which its \emph{inductive type} is parametrized.}
of the constructor's recursive arguments.
Constructors therefore always construct elements whose sizes are larger than their arguments'.
Since constructor argument sizes are bounded by above,
the size of an inductive represents the size an element of the inductive can \emph{at most} have,
and it can always be lifted to a larger size, but never lowered to a smaller one.
In \lang, the constructors are annotated with their types
since the parameter-like sizes cannot otherwise be synthesized.
For instance, $\succ{s}{r}{e}$ constructs a natural of size $s$,
while taking as argument a natural of size $r < s$.
Although $\zero*$ has no recursive arguments,
it still takes a smaller size argument solely for uniformity with $\succ*$.
Additionally, W types have explicit binders for the first type parameter for convenience,
much like dependent function types:
the variable $x$ is bound within $\tau$ in the type $\W{x}{\sigma}{\tau}{s}$.
The expression
$\match{e}{\fun*{x}{P}}{(\app{\App{c}{\alpha}}{z_1}{\seq}{z_m} \Rightarrow e_c) \seq}$
contains three parts:
the \emph{target} $e$ it destructs,
the \emph{motive} $P$ denoting the return type of the expression abstracted over a target,
and the \emph{branches} $e_c$, one for each constructor of the target's type,
abstracted over the constructor's size and term arguments.
Its reduction rules for each constructor are given in \cref{fig:reduction-ind},
along with the reduction rule for fixpoint expressions.
By convention, the reduction rules for $\kw{case}$ expressions
are also referred to as $\iota$-reduction,
and for fixpoint expressions as $\mu$-reduction.
\FigRedInd{fig:reduction-ind}
Fixpoints reduce when applied to some size $s$ by substitution of itself into its own body.
Fixpoints' bodies are well typed when recursive applications occur only on smaller sizes,
so the substitution wraps itself in a bound size abstraction.
Most importantly, they reduce only when there exists some size strictly smaller than $s$;
intuitively, this restriction prevents fixpoints from reducing indefinitely
because subsizing is well founded, and there cannot be an infinite chain of smaller sizes.
\FigTypingInd{fig:typing-ind}
The typing rules for all new constructs are given in \cref{fig:typing-ind},
using two additional metafunctions: $\fresh{\seq}$ asserts the freshness of the given variables,
and $\FV{\mt}$ produces the free variables in the given term.
As discussed, the body of a fixpoint is only well typed
when the fixpoint is recursively applied to a smaller size,
as enforced by its type in the environment when type checking the body.
Sizes aside, the only difference from regular, unsized naturals and well-founded trees
is that their types live in a universe one level higher than they usually are.
$\N{s}$ lives in $\TypeT{1}$ rather than in $\Type{0}$,
and $\W{x}{\sigma}{\tau}{s}$ lives in $\axioms{U}$ rather than in $U$.
This is a direct consequence of the way that the translation is defined,
and is necessary to maintain its type preservation properties.
While not incorrect, inductive types living in the ``wrong'' universe is aesthetically unpleasant
and removes some of the impredicativity of their parameters
by preventing them from quantifying over the inductive types themselves.
Potential methods of circumventing this undesirable trait to varying degrees of success
is discussed later in \cref{subsec:infinity}.
\subsection{Examples} \label{subsec:examples}
This section presents examples of using \lang for programming.
Although it only has naturals and well-founded trees,
we also use other sized inductive types as examples,
informally defining them similarly to \cref{subsec:ind-types}.
We omit the type annotation of $\kw{let}$-bound expressions
when the type is evident or deducible from context.
\subsubsection{Size-preserving functions}
One of the most important uses of sized types is the ability to define
\emph{size-preserving} functions,
where the sizes of the input and output types are the same.
This guarantees that the output is never larger than the input,
and size-preserving functions can be used in recursive calls of fixpoints.
For instance, the $\monus$ function, which computes $\maximum{0, n - m}$,
can be size preserving in its first argument,
since $n - m$ is never greater than $n$.
Here, $\pred$ is another size-preserving function
$\Funtype{\alpha}{\arr*{\N{\alpha}}{\N{\alpha}}}$
that computes $\maximum{0, n - 1}$ for some number $n$.
\begin{align*}
&\Let{\monus}{\Funtype{\alpha}{\Funtype{\beta}{\arr*{\N{\beta}}{\N{\alpha}}{\N{\beta}}}}}{ \\
&\fix{\monus*}{\alpha}{\Funtype{\beta}{\arr*{\N{\beta}}{\N{\alpha}}{\N{\beta}}}}{ \\
&\quad \Fun{\beta}{\fun{n}{\N{\beta}}{\fun{m}{\N{\alpha}}{\match*{m}{ \\
&\qquad \App{\zero*}{\gamma} \Rightarrow n \\
&\qquad \app{\App{\succ*}{\gamma}}{k} \Rightarrow \app{\App{\monus*}{\gamma}{\beta}}{(\app{\App{\pred}{\beta}}{n})}{k}}}}}}}
\end{align*}
We see the benefit of size preservation with $\divv$,
which computes Euclidean division of $n$ by $m$, or $\left\lceil\frac{n}{m+1}\right\rceil$.
This is computed recursively by subtracting $m$ from the numerator using $\monus$
until $\zero*$ is reached, and counting the number of times the subtraction is performed.
The recursive call is performed on the result of $\monus$;
$\divv$ then only type checks because $\monus$ is size preserving.
Because the first argument of the recursive call to $\divv$ isn't \emph{structurally}
a subterm with the call to $\monus$ in the way,
a guardedness check wouldn't accept the corresponding unsized function.
%
\begin{align*}
&\Let{\divv}{\Funtype{\alpha}{\Funtype{\beta}{\arr*{\N{\alpha}}{\N{\beta}}{\N{\alpha}}}}}{ \\
&\fix{\divv*}{\alpha}{\Funtype{\beta}{\arr*{\N{\alpha}}{\N{\beta}}{\N{\alpha}}}}{ \\
&\quad \Fun{\beta}{\fun{n}{\N{\alpha}}{\fun{m}{\N{\beta}}{\match*{n}{ \\
&\qquad \App{\zero*}{\gamma} \Rightarrow \zero{\alpha}{\gamma} \\
&\qquad \app{\App{\succ*}{\gamma}}{k} \Rightarrow
\succ{\alpha}{\gamma}{
(\app{\App{\divv*}{\gamma}{\beta}}{
(\app{\App{\monus}{\beta}{\gamma}}{k}{
(\app{\App{\pred}{\beta}}{m})})}{m})}}}}}}}
\end{align*}
\subsubsection{Higher-rank sizes}
This example, which demonstrates higher-rank size quantification,
is a traversal of a well-founded tree with a size-preserving function,
adapted from the rose tree traversal example by \citet{NbE}.
Given some size-preserving function on a well-founded tree,
the function is applied to subtrees prior to traversal.
Suppose that $B$ here is some type operator on $A$.
%
\begin{align*}
&\Let{\traverseW}{\arr*{(\Funtype{\gamma}{\arr*{\W{x}{A}{\app{B}{x}}{\gamma}}{\W{x}{A}{\app{B}{x}}{\gamma}}})}{ \\
&\phantom{\kw{let} \: \traverseW \mathrel{:} \phantom{}} \Funtype{\alpha}{\arr*{\W{x}{A}{\app{B}{x}}{\alpha}}{\W{x}{A}{\app{B}{x}}{\alpha}}}}}{ \\
&\quad \fun{f}{\Funtype{\gamma}{\arr*{\W{x}{A}{\app{B}{x}}{\gamma}}{\W{x}{A}{\app{B}{x}}{\gamma}}}}{ \\
&\quad \fix{\traverse}{\alpha}{\arr*{\W{x}{A}{\app{B}{x}}{\alpha}}{\W{x}{A}{\app{B}{x}}{\alpha}}}{ \\
&\qquad \fun{w}{\W{x}{A}{\app{B}{x}}{\alpha}}{\match*{w}{ \\
&\qquad \quad \app{\App{\sup*}{\beta}}{a}{g} \Rightarrow \sup{x}{A}{\app{B}{x}}{\alpha}{\beta}{a}{(\fun{b}{\app{B}{a}}{\app{\App{\traverse}{\beta}}{(\app{\App{f}{\beta}}{(\app{g}{b})})}})}}}}}}
\end{align*}
\subsubsection{Limitations} \label{subsec:examples:limitations}
As expressive as bounded, higher-rank sized types are,
there still exist limitations to what can be expressed in comparison to ordinary inductive types
or to sized type theories which have an infinite size,
such as the ones listed in \cref{tab:sized-types}.
Limitations typically arise when dealing with inductive definitions where
recursive arguments appear as the return type of a function.
Consider the following inductive definition
representing the Brouwer notation for ordinals (see \eg \citet{ordinals})
with zero, successor, and limit ordinals:
%
\begin{align*}
&\data{\Ord{\alpha}}{\Type{1}} \\
&\quad \annot{\zord*}{\Funtype<{\beta}{\alpha}{\Ord{\alpha}}} \\
&\quad \annot{\sord*}{\Funtype<{\beta}{\alpha}{\arr*{\Ord{\beta}}{\Ord{\alpha}}}} \\
&\quad \annot{\lord*}{\Funtype<{\beta}{\alpha}{\arr*{(\Funtype<{\gamma}{\beta}{\arr*{\N{\gamma}}{\Ord{\beta}}})}{\Ord{\alpha}}}}
\end{align*}
The limit ordinal, taking some function on naturals returning an ordinal,
constructs an ordinal meant to be ``larger'' than any of the returned ordinals.
Such a function should be able to return an ordinal larger than any natural,
hence the bounded size quantification in its domain.
Conversely, the naturals embed quite naturally within the ordinals;
let $\natOrd$ be such an embedding function
$\Funtype{\alpha}{\arr*{\N{\alpha}}{\Ord{\alpha}}}$.
In an unsized type theory, supposing that $\const{natOrd'}$ is an unsized version of $\natOrd$,
the first limit ordinal $\const{\omega'}$ is easily defined as
$\Let{\const{\omega'}}{\const{Ord}}{\app{\lord*}{\const{natOrd'}}}$.
However, in \lang, things aren't so easy; given a function $\App{\liftO}{\alpha}{\beta}$
that lifts an ordinal $\Ord{\beta}$ to a larger-sized ordinal $\Ord{\alpha}$, we might try:
%
\begin{align*}
&\Let{\omegaOrd}{\Ord{\sss{\hole}}}{\lord{\sss{\hole}}{\hole}{(\Fun<{\gamma}{\hole}{\fun{n}{\N{\gamma}}{\app{\App{\liftO}{\hole}{\gamma}}{(\app{\App{\natOrd}{\gamma}}{n})}}})}}
\end{align*}
The crucial problem is: what size fills in the hole \new{$\hole$}?
Intuitively, this size must be larger than the size of \emph{any} natural.
Therefore, a corresponding ``limit size'' is needed;
the infinite size in other sized type theories can fill this role since it's larger than all sizes
and therefore the limit of \emph{all} sizes.
We discuss the infinite size further in \cref{subsec:infinity}.
\section{Syntactic Model of \lang} \label{sec:model}
\input{figures/cic.tex}
\input{figures/definitions.tex}
\input{figures/translation.tex}
\lang is modelled in \CICE.
The key idea is that sizes in \lang can themselves be represented as a inductive type in \CICE,
and naturals and well-founded trees are then inductives with an additional size parameter.
Sizes are represented as a (generalization of) the Brouwer notation for ordinals in type theory,
and their order as an inductive type indexed by sizes.
The order is \emph{well founded}:
there is no infinite sequence of ever-smaller sizes,
and there is always a ``smallest'' size (or many of them).
This property allows for \emph{well-founded induction},
where to prove some property on sizes, one supposes that it holds for all strictly smaller sizes.
Every fixpoint expression in \lang is modelled as an instance of well-founded induction in \CICE.
To prove well foundedness and in turn the induction principle,
we show that sizes satisfy an \emph{accessibility predicate}~\citep{accessibility}.
For the type preservation proof to go through,
\emph{definitional proof irrelevance}
of accessibility predicates is required---all proofs of accessibility
are definitionally equal.
Since the proof irrelevance holds propositionally,
We use an extensional CIC to obtain the definitional equality
from the propositional equality via equality reflection\index{equality reflection}.
We start by briefly reviewing the syntax and judgements of \CICE.
We then describe the translation from \lang to \CICE,
which is a metafunction from typing derivations of \lang to terms of \CICE.
In addition to the notation used in \cref{sec:sized-dep-types},
given variables $\vec{\xT} = \xT_1 \seq \xT_n$,
terms $\vec{\eT} = \eT_1 \seq \eT_n$,
and types $\vec{\tauT} = \tauT_1 \seq \tauT_n$,
\new{$\annotT{\vec{\xT}}{\vec{\tauT}}$} denotes the assumption environment
$\annotT*{\xT_1}{\tauT_1}, \seq, \annotT*{\xT_n}{\tauT_n}$,
\new{$\subst{\eT}{\vec{\xT}}{\vec{\eT}}$} denotes the simultaneous substitution
$\subst{\eT}{\xT_1, \seq, \xT_n}{\eT_1, \seq, \eT_n}$,
\new{$\funT{\vec{\xT}}{\vec{\tauT}}{\eT}$} denotes the $n$-ary function
$\funT{\xT_1}{\tauT_1}{\seq \funT{\xT_n}{\tauT_n}{\eT}}$, and
\new{$\type{\GammaT}{\vec{\eT}}{\vec{\tauT}}$} denotes the $n$ typing judgements
$(\type{\GammaT}{\eT_1}{\tauT_1})$, \seq, $(\type{\GammaT, \annotT{\eT_1}{\tauT_1}, \seq, \annotT{\eT_{n-1}}{\tauT_{n-1}}}{\eT_n}{\tauT_n})$.
\subsection{Target Type Theory}
\FigSyntaxCIC{fig:syntax-cic}
The syntax of \CICE is given in \cref{fig:syntax-cic};
differences from \lang include a 1-based index for the recursive argument of fixpoint expressions,
$\tg{case}$ expression motives abstracted over the target's inductive type indices,
and a homogeneous propositional equality type with the reflexivity constructor and $\JT*$ eliminator.
New inductive types are defined using data definitions $\DT$,
whose syntax resembles the informal presentation used in \cref{sec:sized-dep-types}.
Metavariable usage convention is roughly the same as for \lang,
with the addition of $\pT$ for inductive type parameters or proofs of equality
and $\aT$ for inductive type indices.
The well formedness conditions on inductive data definitions,
such as well typedness and \emph{strict positivity},
are entirely standard, so we omit them;
see \eg pCIC~\citep{pCIC} for instance for a full description.
Inductive definitions in their full generality aren't needed,
and nonmutual, nonnested inductives suffice.
Indeed, only six inductive definitions are used for the translation
for representing sizes, their order, their well foundedness,
and the empty type, naturals, and well-founded trees.
\begin{figure}[h]
\centering
\begin{mathpar}
\fbox{$\type{\GammaT}{\eT}{\tauT}$} \qquad
\fbox{$\subtype{\GammaT}{\tauT}{\tauT}$} \qquad
\fbox{$\defeq{\GammaT}{\eT}{\eT}{\tauT}$} \hfill \\
\inferrule[\rlabel*{conv*}]{
\type{\GammaT}{\eT}{\sigmaT} \\
\type{\GammaT}{\sigmaT}{\UT} \\\\
\type{\GammaT}{\tauT}{\UT} \\
\subtype{\GammaT}{\sigmaT}{\tauT}
}{
\type{\GammaT}{\eT}{\tauT}
}
\and
\inferrule[\rlabel{$\preccurlyeq$-conv}{subtype-conv}]{
\defeq{\GammaT}{\tauT_1}{\tauT_2}{\UT}
}{
\subtype{\GammaT}{\tauT_1}{\tauT_2}
}
\and
\inferrule[\rlabel{$\equiv$-refl}{equiv-refl}]{
\type{\GammaT}{\eT}{\tauT}
}{
\defeq{\GammaT}{\eT}{\eT}{\tauT}
}
\and
\inferrule[\rlabel{$\equiv$-conv}{equiv-conv}]{
\subtype{\GammaT}{\sigmaT}{\tauT} \\\\
\defeq{\GammaT}{\eT_1}{\eT_2}{\sigmaT}
}{
\defeq{\GammaT}{\eT_1}{\eT_2}{\tauT}
}
\and
\inferrule[\rlabel{$\equiv$-reflect}{equiv-reflect}]{
\type{\GammaT}{\pT}{\eqT{\eT_1}{\tauT}{\eT_2}}
}{
\defeq{\GammaT}{\eT_1}{\eT_2}{\tauT}
}
\and
\inferrule[\rlabel{$\equiv$-$\mu$}{equiv-mu}]{
\type{\GammaT}{\tauT}{\UT} \\
\defeq{\GammaT}{\tauT}{\arr*{\DeltaT}{\funtypeT{\any}{\app{\XT}{\vec{\pT}}{\vec{\aT}}}{\tauT'}}}{\UT} \\\\
\card{\DeltaT} + 1 = \nT \\
\type{\GammaT, \annotT{\fT}{\tauT}}{\eT}{\tauT}
}{
\defeq{\GammaT}{\fixT{\nT}{\fT}{\tauT}{\eT}}{\subst{\eT}{\fT}{\fixT{\nT}{\fT}{\tauT}{\eT}}}{\tauT}
}
\and
\inferrule[\rlabel*{fix*}]{
\type{\GammaT}{\tauT}{\UT} \\
\defeq{\GammaT}{\tauT}{\arr*{\DeltaT}{\funtypeT{\any}{\app{\XT}{\vec{\pT}}{\vec{\aT}}}{\tauT'}}}{\UT} \\
\card{\DeltaT} + 1 = \nT \\
\type{\GammaT, \annotT{\fT}{\tauT}}{\eT}{\tauT}
}{
\type{\GammaT}{\fixT{\nT}{\fT}{\tauT}{\eT}}{\tauT}
}
\end{mathpar}
\caption{Excerpt of typing, subtyping, and equivalence rules}
\label{fig:equiv-sub-type}
\end{figure}
The typed equivalence, subtyping, and typing judgements are defined mutually:
equivalence depends on typing and subtyping,
subtyping depends on equivalence,
and typing depends on subtyping and equivalence.
The judgement rules are all standard,
so \cref{fig:equiv-sub-type} presents only an excerpt;
the full rules are found in \cref{app:equiv}.
The first row contains the main mutual dependencies between the judgements,
followed by notable \rref{equiv-reflect, equiv-mu, fix*}.
To summarize, equivalence is, by definition, an equivalence relation,
satisfying reflexivity, symmetry, and transitivity as well as congruence.
An equivalence judgement can be converted to one annotated by a supertype via \rref{equiv-conv}.
The key rule for extensionality is equality reflection in \rref{equiv-reflect},
which definitionally equates two terms whenever there exists some proof of their propositional equality.
The remaining equivalence rules are typed versions of the usual reduction rules
for functions, $\tg{let}$ expressions, $\tg{case}$ expressions, and fixpoint expressions,
as well as $\eta$-equivalence for functions.
\rref{equiv-mu} for fixpoints is \emph{unguarded},
meaning that fixpoints are equivalent to the substitution of itself into its own body
regardless of what they are applied to.
To maintain normalization,
the usual guarded reduction rule in intensional CIC reduces fixpoints
only when applied to a literal constructor in the recursive argument position.
\vspace{-0.5\baselineskip}
\begin{mathpar}
\inferrule*[right=\rlabel{$\equiv$-$\mu$-guarded}{equiv-mu-guarded}]{\cdots \\ \card{\vec{\eT}'} + 1 = \nT}{
\defeq{\GammaT}{\app{(\fixT{\nT}{\fT}{\tauT}{\eT})}{\vec{\eT}'}{(\app{\cT}{\vec{\aT}})}}{\app{(\subst{\eT}{\fT}{\fixT{\nT}{\fT}{\tauT}{\eT}})}{\vec{\eT}'}{(\app{\cT}{\vec{\aT}})}}{\tauT}
}
\end{mathpar}
Evidently \rref{equiv-mu-guarded} can be derived from \rref{equiv-mu} by congruence.
On the other hand,
\rref{equiv-mu} can be derived from \rref{equiv-mu-guarded}
by reflecting a proof of their propositional equality,
which can be constructed using transitivity, congruence, and $\eta$-equivalence.
Since \rref{equiv-mu} and \rref{equiv-mu-guarded} are metatheoretically equivalent in \CICE,
we choose to use \rref{equiv-mu} for its simplicity.
As opposed to \lang, for \CICE we use pCIC's presentation of subtyping,
which has a typed equivalence premise in \rref{subtype-conv}.
It also has an explicit rule for transitivity of subtyping since judgements such as
$\subtype{\mt}{\app{(\funT{P}{\TypeT{\tg{1}}}{P})}{\PropT}}{\TypeT{\tg{0}}}$ would fail to hold otherwise.
Finally, the typing and environment well formedness rules are similar to those in \lang.
An additional premise to \rref{fix*} ensures that the $\nT$th argument is indeed an inductive type.
As previously mentioned, fixpoints must also be guarded:
recursive calls can only occur on structurally smaller arguments of elements of inductives.
The guard condition is well studied \citep{guard, guard-relax, Coq} and so omitted here.
To justify uses of fixpoint expressions in the translation,
we will provide either a mechanization or present a brief argument of guardedness.
\subsection{Preliminary Definitions} \label{subsec:prelim}
Before defining the translation from \lang terms to \CICE terms,
in this section we describe how the necessary \CICE terms are constructed,
which comprises the aforementioned six inductive data definitions
and well-founded induction principle
as well as the various properties which the order on sizes satisfies.
\FigData{fig:data-defns}
The inductive definitions are listed in \cref{fig:data-defns}
along with some basic definitions we treat as global.
$\botT$ is the usual empty type.
$\SizeT$ is a generalization of the $\Ord*$ type introduced in \cref{subsec:examples},
with the domain of the function passed to $\limT$ replaced by some arbitrary type.
Although limit sizes aren't strictly necessary for the translation,
as \lang only has successor sizes and size variables,
we include them so that various solutions to the problem of the infinite size
can be explored in \cref{subsec:infinity}.
Furthermore, this allows for a simplification of the definition,
since the zero size $\baseT$ can be defined as a limit size rather than as another constructor.
The order on sizes $\mt \szleT \mt$ is defined by the three properties that must hold~\citep{ordinals}:
\begin{itemize}[noitemsep]
\item $\monoT$: The successor operator $\sucT$ is monotone with respect to the order;
\item $\coconeT$: The limit operator $\limT$ constructs an upper bound, \ie
given some function $f$ returning sizes,
a size smaller than any size returned by $f$ is also smaller than the limit of $f$;
\item $\limitT$: The limit operator on $f$ constructs a \emph{least} upper bound such that
if a size is larger than \emph{all} sizes returned by $f$
then it must also be larger than the limit of $f$.
\end{itemize}
Other properties of the order can be derived by induction from these constructors alone.
A corresponding strict order $\mt \szltT \mt$ is also defined,
and an accessibility predicate $\AccT$ is specialized to sizes and the strict order.
Note that it lives in $\PropT$, as does the argument of its sole constructor $\accT$,
so accessibility predicates are intended to be interpreted as proof irrelevant,
and their large elimination is allowed.
\FigDefns{fig:defns}
Before moving on to naturals and well-founded trees,
\cref{fig:defns} lists the names and types of a number of provable definitions.
First is \emph{function extensionality},
asserting that two functions $\annot{f, g}{\funtypeT{x}{A}{\app{B}{x}}}$ are propositionally equal if they are pointwise equal.
In \CICE, they are in fact equivalent (\ie definitionally equal):
given some proof $h$ of their pointwise equality,
under the assumption $\annot{x}{A}$,
the propositional equality $\eq{\app{f}{x}}{}{\app{g}{x}}$ proven by $\app{h}{x}$
can be reflected into the corresponding definitional equality,
which by $\eta$-equivalence is then a definitional equality of $f$ and $g$.
The remaining definitions have been mechanized in either Agda and Coq in
\cref{app:mechanization:agda:prelim} and \cref{app:mechanization:coq:prelim},
respectively, under the assumption of function extensionality as an axiom
(which cannot be proven in, but is consistent with, intensional CIC).
The mechanizations don't use any additional type-theoretic features beyond CIC,
and the proofs could theoretically be written in plain CIC,
but they would be far less manageable without the ergonomics provided by the proof assistants.
As an example, the Coq proof for $\accessible$ consists of a dozen lines of tactics,
whereas the full proof term generated from the tactics is 129 lines long.
The definitions themselves describe properties of the order on sizes
and of the accessibility predicate:
\begin{itemize}[noitemsep]
\item $\baseleq$, $\reflleq$, $\transleq$, and $\sucleq$:
The order is reflexive and transitive (\ie a preorder)
such that $\baseT$ is smaller than or equal to all sizes
and the successor sizes are greater or equal.