File: generics.tex

package info (click to toggle)
swiftlang 6.1.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,644 kB
  • sloc: cpp: 9,901,738; ansic: 2,201,433; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (742 lines) | stat: -rw-r--r-- 26,401 bytes parent folder | download
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This source file is part of the Swift.org open source project
%
% Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors
% Licensed under Apache License v2.0 with Runtime Library Exception
%
% See https://swift.org/LICENSE.txt for license information
% See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% KOMA-Script 'scrbook' document class
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% From left to right, each option does the following:
% - A4 page size. Do not change!
% - Draw line under chapter/section heading at top of page
% - Next three options force use of serif fonts for part, chapter and section headings
% - Put Bibliography in Table of Contents
% - Use 'flat' ToC style because it looks better with double-digit chapter numbers
% - Use 'fleqn' to left-align display math to make it consistent with the 'quote' environment which I use for tables and diagrams and stuff
%
% - Changing `twoside=semi' to `twoside' is safe. The default mode is meant for reading on-screen. If you make this change, even pages have a narrower left margin, and odd pages have a wider left margin, which looks silly when scrolling vertically, but makes sense if you actually print and bind the book.
\documentclass[a4paper,headsepline,headings=standardclasses,headings=big,chapterprefix=false,bibliography=totoc,toc=flat,fleqn,twoside=semi]{scrbook}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Packages
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Fancy drop caps at the start of each chapter.
\usepackage{Zallman,lettrine}
\renewcommand\LettrineFontHook{\Zallmanfamily}

% Multiple indices.
\usepackage{imakeidx}

% Theorem-like environments: proposition, example, algorithm, etc.
\usepackage{amsthm}

% For the 'List of Algorithms'. Must load before hyperref.
\usepackage{thmtools}

\renewcommand\thmtformatoptarg[1]{#1}

% thmtools doesn't offer enough customization options, so hack it up to not print "Algorithm" in each line of the "List of Algorithms"
\makeatletter

\renewcommand\thmt@mklistcmd{%
  \thmtlo@newentry
    \@xa\def\csname ll@\thmt@envname\endcsname{%
      \protect\numberline{\csname the\thmt@envname\endcsname}%
      \ifx\@empty\thmt@shortoptarg\else\protect\thmtformatoptarg{\thmt@shortoptarg}\fi
    }%
  \@xa\gdef\csname thmt@contentsline@\thmt@envname\endcsname{%
    \thmt@contentslineShow% default:show
  }%
}

% Fix page header to say "Index" instead of "INDEX"
\renewcommand\imki@indexheaders{\@mkboth{\indexname}{\indexname}}

% We use fleqn to left-align display math by default, but sometimes we want to center
% something
\newenvironment{ceqn}{\@fleqnfalse
  \@mathmargin\@centering \ignorespaces
}{%
  \ignorespacesafterend
}

\makeatother

% Hyperlinks in PDF files. We enable numbering in PDF bookmark titles, and backreferences from bibliography entries, and make the section title in the TOC clickable.
\usepackage[pdfborder={0 0 0},linktocpage=true,colorlinks=true,bookmarksnumbered,bookmarksopen=true,bookmarksopenlevel=0,pagebackref,linktoc=all]{hyperref}

% More control over PDF bookmarks.
\usepackage{bookmark}

% \uptau for generic parameter notation
\usepackage{upgreek}

% rcases, etc
\usepackage{mathtools}

% Nice horizontal rules in tables: \toprule, \midrule, \bottomrule
\usepackage{booktabs}

% AMS math symbols: \varnothing, \mathbb, etc
\usepackage{amssymb}

% UTF-8 greek letters directly in TeX for verbatim environment
\usepackage{textgreek}

% TikZ for figures
\usepackage{tikz}
\usepackage{tikz-cd}
\usetikzlibrary{positioning,shapes,arrows,trees,calc,fit,backgrounds,shapes.multipart}
\tikzstyle{class}=[rounded corners,draw=black,thick,anchor=west, outer sep=0.2em]
\tikzstyle{stage}=[rounded corners, draw=darkgray, thick, fill=lightgray, text centered, outer sep=0.2em]
\tikzstyle{data}=[draw=darkgray, thick, fill=light-gray, text centered, outer sep=0.2em]
\tikzstyle{arrow}=[->,>=stealth]
\tikzstyle{genericenv}=[inner sep=12pt, rounded corners, draw=black, very thick]
\tikzstyle{genericenvlabel}=[fill=white, draw=black, very thick, rounded corners]
\tikzstyle{genericenvmatrix}=[matrix, row sep=3pt, column sep=5pt]
\tikzstyle{genericenvpart}=[rounded corners, fill=light-gray]
\tikzstyle{abstractvertex}=[circle, fill=gray, outer sep=0.1em]
\tikzstyle{abstractvertex2}=[circle, fill=lightgray, outer sep=0.1em]
\tikzstyle{SourceFile}=[draw=black,thick,fill=lightgray, outer sep=0.2em]
\tikzstyle{decl}=[draw=black,thick, outer sep=0.2em]
\tikzstyle{type}=[rounded corners,draw=black,thick,fill=lighter-gray,outer sep=0.2em]
\tikzstyle{OtherEntity}=[draw=black,thick,fill=lighter-gray, outer sep=0.2em]

% \DeclareMathOperator, \boxed, etc
\usepackage{mathtools}

% Verbatim environment with border and line numbers
\usepackage{fancyvrb}

% Extended array environment
\usepackage{array}

% Colors for shaded frames
\usepackage{xcolor}

% Shaded frames for 'More details' boxes in Introduction
\usepackage{mdframed}

% To customize bibliography with \bstctlcite
\usepackage{IEEEtrantools}

% Each chapter is its own file.
\usepackage{subfiles}

% Strikethrough (\st)
\usepackage[normalem]{ulem}

% Line breaks in URLs in bibliography
\usepackage{url}
\def\UrlBreaks{\do\/\do-}

% Text flowing around figures
\usepackage{wrapfig}

% Fancier tables
\usepackage{tabularx}

% Set up hyperref link colors.
\hypersetup{linkcolor=[rgb]{0,0.2,0.6},citecolor=[rgb]{0.5,0,0.5},urlcolor=[rgb]{0,0.4,0.2}}

% For printing in black and white:
%\hypersetup{linkcolor=[rgb]{0,0,0},citecolor=[rgb]{0,0,0},urlcolor=[rgb]{0,0,0}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Environments
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Various theorem-like environments
\theoremstyle{definition}
\newtheorem{definition}{Definition}[chapter]

% Algorithm 12A, 12B, etc.
\newtheorem{algorithm}{Algorithm}[chapter]
\renewcommand\thealgorithm{\thechapter\Alph{algorithm}}

% Use the same counter for all three so you get Definition 12.1, Proposition 12.2, Example 12.3 etc.
\newtheorem{example}[definition]{Example}

% We don't want the statement of the theorem to use italics...
% \theoremstyle{theorem}
\newtheorem{proposition}[definition]{Proposition}
\newtheorem{lemma}[definition]{Lemma}
\newtheorem{corollary}[definition]{Corollary}
\newtheorem{theorem}[definition]{Theorem}

% 'listing' floating environment for source listings
\DeclareNewTOC[
  type = listing,
  counterwithin=chapter,
  float
]{listing}

% 'Verbatim' environment for fancy source listings
\RecustomVerbatimEnvironment{Verbatim}{Verbatim}{frame=single}

% 'More Details' boxes in Introduction
\newenvironment{MoreDetails}{\medskip\begin{mdframed}[rightline=true,frametitlerule=true,frametitlerulecolor=gray,frametitlebackgroundcolor=light-gray,frametitlerulewidth=2pt,backgroundcolor=light-gray,linecolor=gray,frametitle={More details}]\begin{itemize}}{\end{itemize}\end{mdframed}}

% Derived requirements
\newenvironment{derivation}{\begin{gather*}}{\end{gather*}}

% Generic environments in Archetypes

\newenvironment{tightcenter}{%
\setlength\topsep{2pt}%
\setlength\parskip{0pt}\begin{center}%
}{%
\end{center}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Macros
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Cross-references.
\newcommand{\PartRef}[1]{\hyperref[#1]{Part~\ref*{#1}}}
\newcommand{\ChapRef}[1]{\hyperref[#1]{Chapter~\ref*{#1}}}
\newcommand{\SecRef}[1]{\hyperref[#1]{Section~\ref*{#1}}}
\newcommand{\AppendixRef}[1]{\hyperref[#1]{Appendix~\ref*{#1}}}
\newcommand{\DefRef}[1]{\hyperref[#1]{Definition~\ref*{#1}}}
\newcommand{\PropRef}[1]{\hyperref[#1]{Proposition~\ref*{#1}}}
\newcommand{\ThmRef}[1]{\hyperref[#1]{Theorem~\ref*{#1}}}
\newcommand{\CorollaryRef}[1]{\hyperref[#1]{Corollary~\ref*{#1}}}
\newcommand{\LemmaRef}[1]{\hyperref[#1]{Lemma~\ref*{#1}}}
\newcommand{\AlgRef}[1]{\hyperref[#1]{Algorithm~\ref*{#1}}}
\newcommand{\ExRef}[1]{\hyperref[#1]{Example~\ref*{#1}}}
\newcommand{\FigRef}[1]{\hyperref[#1]{Figure~\ref*{#1}}}
\newcommand{\ListingRef}[1]{\hyperref[#1]{Listing~\ref*{#1}}}

% I use this in a few places.
\definecolor{light-gray}{gray}{0.90}
\definecolor{lighter-gray}{gray}{0.97}

% Zero-tolerance policy for overfull hboxes.
\overfullrule=10pt

% Comment this out and uncomment the next line to see the work-in-progress sections.
\newcommand{\ifWIP}{\iffalse}
%\newcommand{\ifWIP}{\iftrue}

% By default KOMA-script lays out footnotes so that the second line is offset by the width of an opening quotation mark. This looks ugly if your footnote text is not actually a quotation, because the second line is indented too far relative to the first line.
\deffootnote[1em]{1em}{1em}{\textsuperscript{\thefootnotemark}}

% Bibliography backreferences pages with citations
\renewcommand*{\backrefalt}[4]{%
       \ifcase #1 %
         \\*(No citations.)%
       \or
         \\*(Cited on page #2.)%
       \else
         \\*(Cited on pages #2.)%
\fi }

% Request evalator requests
\newcommand{\Request}[1]{\textbf{#1}}

% Generic parameters
\newcommand{\ttgp}[2]{\texttt{$\uptau$\char`_#1\char`_#2}}
\newcommand{\rT}{\ttgp{0}{0}}
\newcommand{\rU}{\ttgp{0}{1}}
\newcommand{\rV}{\ttgp{0}{2}}

% Identifiers
\newcommand{\tT}{\texttt{T}}
\newcommand{\tU}{\texttt{U}}
\newcommand{\tV}{\texttt{V}}
\newcommand{\tP}{\texttt{P}}
\newcommand{\tQ}{\texttt{Q}}
\newcommand{\tR}{\texttt{R}}
\newcommand{\tX}{\texttt{X}}
\newcommand{\tY}{\texttt{Y}}
\newcommand{\tC}{\texttt{C}}
\newcommand{\tSelf}{\texttt{Self}}
\newcommand{\SelfU}{\texttt{Self.U}}
\newcommand{\tCollection}{\texttt{Collection}}
\newcommand{\tSequence}{\texttt{Sequence}}
\newcommand{\tIterator}{\texttt{IteratorProtocol}}
\newcommand{\tInt}{\texttt{Int}}
\newcommand{\tN}{\texttt{N}}

\newcommand{\nA}{\texttt{A}}
\newcommand{\nB}{\texttt{B}}
\newcommand{\nC}{\texttt{C}}
\newcommand{\nElement}{\texttt{Element}}
\newcommand{\nIterator}{\texttt{Iterator}}

% Identifiers with '
\newcommand{\tTp}{\tT^\prime}
\newcommand{\tUp}{\tU^\prime}

% Declared interface type
\newcommand{\tXd}{\texttt{X}_d}

% Protocol generic signature
\newcommand{\GP}{G_\tP}
\newcommand{\GN}{G_\tN}

%%%
% Derived requirements
%%%

\newcommand{\SameReq}[2]{[\texttt{#1 == #2}]}
\newcommand{\ConfReq}[2]{[\texttt{#1:~#2}]}

\newcommand{\AssocSameReq}[3]{\SameReq{#1}{#2}_{\texttt{#3}}}
\newcommand{\AssocConfReq}[3]{\ConfReq{#1}{#2}_{\texttt{#3}}}

\newcommand{\TP}{\ConfReq{T}{P}}
\newcommand{\TC}{\ConfReq{T}{C}}
\newcommand{\TAnyObject}{\ConfReq{T}{AnyObject}}
\newcommand{\TU}{\SameReq{T}{U}}
\newcommand{\TX}{\SameReq{T}{X}}

\newcommand{\Tprime}{\texttt{T}^\prime}
\newcommand{\Xprime}{\texttt{X}^\prime}
\newcommand{\Cprime}{\texttt{C}^\prime}

% Derivation step definitions for induction proofs and Appendix B
\newcommand{\GenericStepDef}{\ttgp{d}{i}\tag{\textsc{Generic}}}

\newcommand{\ConfStepDef}{\TP\tag{\textsc{Conf}}}
\newcommand{\SameStepDef}{\TU\tag{\textsc{Same}}}
\newcommand{\ConcreteStepDef}{\SameReq{T}{X}\tag{\textsc{Concrete}}}
\newcommand{\SuperStepDef}{\ConfReq{T}{C}\tag{\textsc{Super}}}
\newcommand{\LayoutStepDef}{\ConfReq{T}{AnyObject}\tag{\textsc{Layout}}}

\newcommand{\AssocNameStepDef}{\texttt{T.A}\tag{\textsc{AssocName} $\TP$}}
\newcommand{\AssocDeclStepDef}{\texttt{T.[P]A}\tag{\textsc{AssocDecl} $\TP$}}
\newcommand{\AssocBindStepDef}{\SameReq{T.[P]A}{T.A}\tag{\textsc{AssocBind} $\TP$}}

\newcommand{\AssocConfStepDef}{\ConfReq{T.U}{Q}\tag{\textsc{AssocConf} $\AssocConfReq{Self.U}{Q}{P}$ $\TP$}}
\newcommand{\AssocSameStepDef}{\SameReq{T.U}{T.V}\tag{\textsc{AssocSame} $\AssocSameReq{Self.U}{Self.V}{P}$ $\TP$}}
\newcommand{\AssocConcreteStepDef}{\SameReq{T.U}{$\Xprime$}\tag{\textsc{AssocConcrete} $\AssocSameReq{Self.U}{X}{P}$ $\TP$}}
\newcommand{\AssocSuperStepDef}{\ConfReq{T.U}{$\Cprime$}\tag{\textsc{AssocSuper} $\AssocConfReq{Self.U}{C}{P}$ $\TP$}}
\newcommand{\AssocLayoutStepDef}{\ConfReq{T.U}{AnyObject}\tag{\textsc{AssocLayout} $\AssocConfReq{Self.U}{AnyObject}{P}$ $\TP$}}

\newcommand{\ReflexStepDef}{\SameReq{T}{T}\tag{\textsc{Reflex} $\texttt{T}$}}
\newcommand{\SymStepDef}{\SameReq{U}{T}\tag{\textsc{Sym} $\TU$}}
\newcommand{\TransStepDef}{\SameReq{T}{V}\tag{\textsc{Trans} $\TU$ $\SameReq{U}{V}$}}

\newcommand{\SameConfStepDef}{\TP\tag{\textsc{SameConf} $\ConfReq{U}{P}$ $\TU$}}
\newcommand{\SameConcreteStepDef}{\SameReq{T}{X}\tag{\textsc{SameConcrete} $\SameReq{U}{X}$ $\TU$}}
\newcommand{\SameSuperStepDef}{\ConfReq{T}{C}\tag{\textsc{SameSuper} $\ConfReq{U}{C}$ $\TU$}}
\newcommand{\SameLayoutStepDef}{\ConfReq{T}{AnyObject}\tag{\textsc{SameLayout} $\ConfReq{U}{AnyObject}$ $\TU$}}

\newcommand{\SameNameStepDef}{\SameReq{T.A}{U.A}\tag{\textsc{SameName} $\ConfReq{U}{P}$ $\TU$}}
\newcommand{\SameDeclStepDef}{\SameReq{T.[P]A}{U.[P]A}\tag{\textsc{SameDecl} $\ConfReq{U}{P}$ $\TU$}}

% Derivation steps in concrete derivations
\newcommand{\AnyStep}[2]{{#2}.\ #1\tag{$\ldots$}}

\newcommand{\GenericStep}[2]{{#2}.\ \texttt{#1}\tag{\textsc{Generic}}}

\newcommand{\ConfStep}[3]{{#3}.\ \ConfReq{#1}{#2}\tag{\textsc{Conf}}}
\newcommand{\SameStep}[3]{{#3}.\ \SameReq{#1}{#2}\tag{\textsc{Same}}}
\newcommand{\ConcreteStep}[3]{{#3}.\ \SameReq{#1}{#2}\tag{\textsc{Concrete}}}

\newcommand{\AssocNameStep}[3]{{#3}.\ \texttt{#2}\tag{\textsc{AssocName} {#1}}}
\newcommand{\AssocDeclStep}[3]{{#3}.\ \texttt{#2}\tag{\textsc{AssocDecl} {#1}}}
\newcommand{\AssocBindStep}[4]{{#4}.\ \SameReq{#2}{#3}\tag{\textsc{AssocBind} {#1}}}

\newcommand{\AssocConfStep}[4]{{#4}.\ \ConfReq{#2}{#3}\tag{\textsc{AssocConf} {#1}}}
\newcommand{\AssocSameStep}[4]{{#4}.\ \SameReq{#2}{#3}\tag{\textsc{AssocSame} {#1}}}
\newcommand{\AssocConcreteStep}[4]{{#4}.\ \SameReq{#2}{#3}\tag{\textsc{AssocConcrete} {#1}}}
\newcommand{\AssocSuperStep}[4]{{#4}.\ \ConfReq{#2}{#3}\tag{\textsc{AssocSuper} {#1}}}
\newcommand{\AssocLayoutStep}[3]{{#3}.\ \ConfReq{#2}{AnyObject}\tag{\textsc{AssocLayout} {#1}}}

\newcommand{\SameConfStep}[5]{{#5}.\ \ConfReq{#3}{#4}\tag{\textsc{SameConf} {#1} {#2}}}

\newcommand{\ReflexStep}[3]{{#3}.\ \SameReq{#2}{#2}\tag{\textsc{Reflex} {#1}}}
\newcommand{\SymStep}[4]{{#4}.\ \SameReq{#2}{#3}\tag{\textsc{Sym} {#1}}}
\newcommand{\TransStep}[5]{{#5}.\ \SameReq{#3}{#4}\tag{\textsc{Trans} {#1} {#2}}}

\newcommand{\SameNameStep}[5]{{#5}.\ \SameReq{#3}{#4}\tag{\textsc{SameName} {#1} {#2}}}
\newcommand{\SameDeclStep}[5]{{#5}.\ \SameReq{#3}{#4}\tag{\textsc{SameDecl} {#1} {#2}}}

%%%
% Generic signature queries
%%%

\newcommand{\Query}[2]{\texttt{#1}(#2)}
\newcommand{\QueryDef}[5]{
\item \textbf{Query:} \IndexQueryDef{#1}$\texttt{#1}(#2)$

\textbf{Input:} #3

\textbf{Output:} #4

#5
}

%%%
% Type substitution algebra
%%%

\newcommand{\SubstMap}[1]{\{#1\}}
\newcommand{\SubstMapC}[2]{\{#1;\,#2\}}

\newcommand{\SubstType}[2]{\texttt{#1} \mapsto \texttt{#2}}
\newcommand{\SubstConf}[3]{\ConfReq{#1}{#3} \mapsto \ConfReq{#2}{#3}}

\newcommand{\SubMapInt}{\SubstMap{\SubstType{\rT}{Int}}}

% Protocols
\newcommand{\Proto}[1]{\langle\texttt{#1}]}
\newcommand{\PP}{\Proto{P}}
\newcommand{\PQ}{\Proto{Q}}

% Conformances
\newcommand{\XP}{\ConfReq{X}{P}}
\newcommand{\NormalConf}{\ConfReq{$\tXd$}{P}}
\newcommand{\ArraySequence}{\ConfReq{Array<\rT>}{Sequence}}
\newcommand{\ArrayCollection}{\ConfReq{Array<\rT>}{Collection}}
\newcommand{\ArrayIntSequence}{\ConfReq{Array<Int>}{Sequence}}
\newcommand{\ArrayIntCollection}{\ConfReq{Array<Int>}{Collection}}
\newcommand{\rTSequence}{\ConfReq{\rT}{Sequence}}
\newcommand{\rTCollection}{\ConfReq{\rT}{Collection}}
\newcommand{\rUSequence}{\ConfReq{\rU}{Sequence}}
\newcommand{\rUCollection}{\ConfReq{\rU}{Collection}}

\newcommand{\AssocType}[2]{\langle\texttt{#1}|\texttt{#2}}
\newcommand{\AssocConf}[2]{\langle\texttt{#1:~#2}]}

\newcommand{\PSequence}{\Proto{Sequence}}
\newcommand{\PIterator}{\Proto{IteratorProtocol}}
\newcommand{\AElement}{\AssocType{Sequence}{Element}}
\newcommand{\AIElement}{\AssocType{IteratorProtocol}{Element}}
\newcommand{\AIterator}{\AssocType{Sequence}{Iterator}}
\newcommand{\ASubSequence}{\AssocType{Collection}{SubSequence}}
\newcommand{\ANA}{\AssocType{N}{A}}
\newcommand{\APA}{\AssocType{P}{A}}
\newcommand{\SelfUQ}{\AssocConf{Self.U}{Q}}
\newcommand{\SelfIterator}{\AssocConf{Self.Iterator}{IteratorProtocol}}
\newcommand{\SelfSequence}{\AssocConf{Self}{Sequence}}
\newcommand{\SelfSubSequence}{\AssocConf{Self.SubSequence}{Collection}}

\newcommand{\SigObj}{\textsc{Sig}}
\newcommand{\ProtoObj}{\textsc{Proto}}
\newcommand{\TypeObj}[1]{\textsc{Type}({#1})}
\newcommand{\SubMapObj}[2]{\textsc{Sub}({#1, #2})}
\newcommand{\ReqObj}[1]{\textsc{Req}({#1})}
\newcommand{\ConfObj}[1]{\textsc{Conf}({#1})}
\newcommand{\ConfPObj}[2]{\textsc{Conf}_{\texttt{#1}}({#2})}
\newcommand{\AssocTypeObj}[1]{\textsc{AssocType}_{\texttt{#1}}}
\newcommand{\AssocConfObj}[1]{\textsc{AssocConf}_{\texttt{#1}}}

% Archetypes print [[like this]]. Must be in math mode
\newcommand{\archetype}[1]{[\![\texttt{#1}]\!]}

% Generic environment operations
\newcommand{\MapIn}{\mathsf{in}_G}
\newcommand{\MapOut}{\mathsf{out}_G}

%%%
% Monoids, etc
%%%

\newcommand{\Pres}[2]{\langle #1\,|\,#2\rangle}
\newcommand{\AR}{\Pres{A}{R}}
\newcommand{\PhiInv}{\varphi^{-1}}
\newcommand{\NN}{\mathbb{N}}

\newcommand{\WL}{\triangleleft}
\newcommand{\WR}{\triangleright}

% Requirement Machine
\newcommand{\protosym}[1]{[\texttt{#1}]}
\newcommand{\assocsym}[2]{[\texttt{#1}|\texttt{#2}]}
\newcommand{\layoutsym}[1]{[\mathsf{layout}\colon\texttt{#1}]}
\newcommand{\supersym}[1]{[\mathsf{superclass}\colon #1]}
\newcommand{\concretesym}[1]{[\mathsf{concrete}\colon #1]}

\newcommand{\Term}{\mathsf{term}}
\newcommand{\Rule}{\mathsf{rule}}

\newcommand{\TermP}[1]{\mathsf{term}_{\texttt{#1}}}
\newcommand{\RuleP}[1]{\mathsf{rule}_{\texttt{#1}}}

\newcommand{\Type}{\mathsf{type}}

\newcommand{\pP}{\protosym{P}}
\newcommand{\pQ}{\protosym{Q}}
\newcommand{\pR}{\protosym{R}}

\newcommand{\aPA}{\assocsym{P}{A}}
\newcommand{\aPB}{\assocsym{P}{B}}
\newcommand{\aPC}{\assocsym{P}{C}}
\newcommand{\aPD}{\assocsym{P}{D}}

\newcommand{\aNA}{\assocsym{N}{A}}

\newcommand{\pSequence}{\protosym{Sequence}}
\newcommand{\pCollection}{\protosym{Collection}}
\newcommand{\pIterator}{\protosym{IteratorProtocol}}
\newcommand{\pEquatable}{\protosym{Equatable}}
\newcommand{\pComparable}{\protosym{Comparable}}

\newcommand{\ConfRule}[2]{#1 \cdot #2 \sim #1}
\newcommand{\SameRule}[2]{#1 \sim #2}
\newcommand{\AssocRule}[2]{\protosym{#1}\cdot\texttt{#2} \sim \assocsym{#1}{#2}}

\newcommand{\CRule}[1]{$\diamond\, #1$}

%%%
% Math helpers
%%%
\newcommand{\Case}[1]{\smallskip\par($\bigstar$ Case #1.)}
\newcommand{\BaseCase}{\smallskip\par\emph{Base case.}\ }
\newcommand{\InductiveStep}{\smallskip\par\emph{Inductive step.}\ }

\newcommand{\Tight}{\hspace{-0.08em}}

\newcommand{\EquivClass}[1]{[\![#1]\!]}

\DeclareMathOperator{\Src}{src}
\DeclareMathOperator{\Dst}{dst}

% Old substitution algebra notation
\newcommand{\mathboxed}[1]{\boxed{\mbox{\vphantom{pI\texttt{pI}}#1}}}
\newcommand{\ttbox}[1]{\boxed{\mbox{\vphantom{pI\texttt{pI}}\texttt{#1}}}}

% GitHub links in 'Source Code Reference' sections
\newcommand{\SourceFile}[1]{\href{https://github.com/swiftlang/swift/tree/main/#1}{\texttt{#1}}}

% Note: \apiref{foo}{bar} must be followed by a newline, because of a quirk with \noindent
\def\apiref#1#2
{\bigskip\hrule\nobreak\vskip 3pt\noindent\texttt{\vphantom{p}#1}\hfill\textsl{#2}\par\nobreak\vskip 3pt\hrule\vskip 3pt\noindent}

% Type parameter and conformance path graph
\tikzstyle{root} = [rounded corners, fill=lightgray, outer sep=0.1em]
\tikzstyle{interior} = [rounded corners, fill=light-gray, outer sep=0.1em]

% Protocol dependency graph
\tikzstyle{protocol} = [rounded corners, fill=light-gray]

% Title page logo

\newcommand{\GenericsLogo}{
\begin{center}
\begin{tikzpicture}[x=2cm, y=2cm]
\foreach \a in {0,1,2,3,4,5} {
  \foreach \b in {0,1,2,3,4,5} {
    \node (A\a B\b) [circle, draw, very thick, outer sep=0.4em] at (\a,\b) {};
  }
}

\foreach \a in {0,1,2,3,4,5} {
  \path [arrow, very thick] (A\a B4) edge (A\a B3);
  \path [arrow, very thick] (A\a B2) edge (A\a B1);
  \path [arrow, very thick] (A\a B0) edge (A\a B1);
}

\foreach \a in {0,1,2,3,4} {
  \pgfmathtruncatemacro\aa{\a+1}
  \foreach \b in {0,2,3,4,5} {
    \path [arrow, very thick] (A\a B\b) edge (A\aa B\b);
  }
}
\end{tikzpicture}
\end{center}
}

% I don't like boldface page numbers in the table of contents.
\RedeclareSectionCommands[tocpagenumberformat=]{chapter}
\RedeclareSectionCommands[tocpagenumberformat=]{part}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Index
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\indexsetup{othercode=\footnotesize}

% We have a main index and one for command line flags.

% generics.ist configures letter headings in the index.
\makeindex[intoc,options=-s generics]
\makeindex[intoc,name=flags,title=Command Line Flags,columns=1,options=-s generics]

% \Index is used instead of \index for entries with custom formatting, eg
%
%   \Index{foo bar@foo \texttt{bar}}
%
% because for some reason, a mix of \IndexDefinition or \IndexSource with \index doesn't get collated correctly.
\newcommand{\Index}[1]{\index{#1|textrm}}

\newcommand{\IndexDefinition}[1]{\index{#1|textbf}}
\newcommand{\IndexSource}[1]{\index{#1|textit}}

% Add an entry to the command line flag index with the right formatting
\newcommand{\IndexFlag}[1]{\index[flags]{#1@\texttt{-#1}}}

% Used for -debug-requirement-machine=... flags
\newcommand{\IndexTwoFlag}[2]{\index[flags]{#1@\texttt{-#1}!#2@\texttt{#2}}}

% Derivation steps
\newcommand{\IndexStep}[1]{\index{#1 derivation step@\textsc{#1} derivation step|textrm}}
\newcommand{\IndexStepTwo}[2]{\index{#1 derivation step@\textsc{#1} derivation step!#2}}
\newcommand{\IndexStepDefinition}[1]{\index{#1 derivation step@\textsc{#1} derivation step|textbf}}

% Sets in the type substitution algebra
\newcommand{\IndexSet}[2]{\index{#1@$#2$|textrm}}
\newcommand{\IndexSetDefinition}[2]{\index{#1@$#2$|textbf}}

% Swift version history
\newcommand{\IndexSwift}[1]{\index{Swift!#1}\index{history}}

% Generic signature queries
\newcommand{\IndexQueryDef}[1]{\IndexDefinition{#1()@$\texttt{#1}()$}}
\newcommand{\IndexQuerySource}[1]{\IndexSource{#1()@$\texttt{#1}()$}}
\newcommand{\IndexQuery}[1]{\Index{#1()@$\texttt{#1}()$}}

\newcommand{\IndexSelf}{\Index{protocol Self type@protocol \tSelf\ type}}

% Normally a seealso index entry elides the page number, but sometimes we want index entries that look like this:
%
% foo, 123
%    see also bar
%
% The trick is to make a fake subentry and format it with this macro to prevent it from showing up.
%
% The above example would be generated by: \index{foo}\index{foo!z@\igobble|seealso{bar}}
%
% The 'z' should sort after every other 'real' subentry, if any.
\def\igobble#1 {}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title Page
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\titlehead{\GenericsLogo}
\title{Compiling Swift Generics}
\author{Slava Pestov}

% Put chapter/section headings at the top of each page
\pagestyle{headings}

\begin{document}

\maketitle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Let's Go!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Table of Contents
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% PDF bookmark for Table of Contents
\cleardoublepage
\pdfbookmark{Contents}{toc}
\tableofcontents

% List of Algorithms
\cleardoublepage
\pdfbookmark{List of Algorithms}{loa}
\listoftheorems[ignoreall,onlynamed={algorithm},title={List of Algorithms}]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The Good Stuff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subfile{chapters/preface}

\part{Syntax}\label{part syntax}

\subfile{chapters/introduction}

\subfile{chapters/compilation-model}

\subfile{chapters/types}

\subfile{chapters/declarations}

\part{Semantics}\label{part semantics}

\subfile{chapters/generic-signatures}

\subfile{chapters/substitution-maps}

\subfile{chapters/conformances}

\subfile{chapters/archetypes}

\subfile{chapters/type-resolution}

\part{Specialties}\label{part specialties}

\subfile{chapters/extensions}

\subfile{chapters/building-generic-signatures}

\subfile{chapters/conformance-paths}

\subfile{chapters/opaque-return-types}

\subfile{chapters/existential-types}

\part{The Requirement Machine}\label{part rqm}

\subfile{chapters/basic-operation}

\subfile{chapters/monoids}

\subfile{chapters/symbols-terms-and-rules}

\subfile{chapters/completion}

\subfile{chapters/property-map}

\subfile{chapters/concrete-conformances}

\subfile{chapters/rule-minimization}

% Move subsequent PDF bookmarks to the top level since the Appendix, Bibliography and Index are not logically contained in Part V
\bookmarksetup{startatroot}

\appendix

\subfile{chapters/math-summary}

\subfile{chapters/derived-requirements-summary}

\subfile{chapters/type-substitution-summary}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Postmatter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Customize bibliography some more
\begingroup
\scriptsize
% Don't justify paragraphs since it looks silly
\raggedright
% Don't break bibliography entries across pages
\raggedbottom
\widowpenalties 1 10000
\bibliographystyle{IEEEtran}
\bibliography{generics}
\endgroup

% Index
\indexprologue{\noindent A \textbf{bold} page number is where the concept is first defined; an \textit{italic} page number is where the implementation is shown in a ``Source Code Reference'' section. A plain page number is where the concept is used.\par}
\printindex

% Command Line Flags
\setindexpreamble{}
\printindex[flags]

% Goodbye!
\end{document}