Skip to content

Commit ada8a30

Browse files
authored
Merge pull request #76181 from slavapestov/more-generics-docs
docs: Update generics.tex
2 parents 25a8885 + b769332 commit ada8a30

21 files changed

+973
-829
lines changed

docs/Generics/README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Compiling Swift Generics
2+
3+
This is a book about the *implementation* of generic programming--also known as parametric polymorphism--in the Swift compiler. The first four chapters also give an overview of the Swift compiler architecture in general.
4+
5+
## Downloading the PDF
6+
7+
A periodically-updated PDF is available here:
8+
9+
> https://download.swift.org/docs/assets/generics.pdf
10+
11+
## Typesetting the PDF
12+
13+
It's written in TeX, so to typeset the PDF yourself, you need a TeX distribution:
14+
15+
- [MacTeX](https://www.tug.org/mactex/mactex-download.html): macOS
16+
- [TeX Live](https://www.tug.org/texlive/): Linux, Windows
17+
- [MikTeX](https://miktex.org): another alternative for macOS, Linux, Windows
18+
19+
### Using `make`
20+
21+
Running `make` in `docs/Generics/` will run `pdflatex` and `bibtex` in the right order to generate the final document with bibliography, index and cross-references:
22+
23+
```
24+
cd docs/Generics/
25+
make
26+
```
27+
28+
### Using `latexmk`
29+
30+
A more modern alternative is to use `latexmk`, which runs `pdflatex` and `bibtex` until fixed point:
31+
32+
```
33+
cd docs/Generics/
34+
latexmk -pdf generics.tex
35+
```
36+
37+
### Manually
38+
39+
You can also just do this:
40+
41+
```
42+
cd docs/Generics/
43+
pdflatex generics
44+
bibtex generics
45+
pdflatex generics
46+
pdflatex generics
47+
```
48+
49+
## Reading the PDF
50+
51+
The book makes use of internal hyperlinks so it is is best to use PDF reader with support for PDF bookmarks and back/forward history:
52+
53+
- Preview.app on macOS fits the bill; you can add Back/Forward buttons to the toolbar with **View** > **Customize Toolbar**.
54+
- [Skim.app](https://skim-app.sourceforge.io) is a BSD-licensed open source PDF reader for macOS.
55+
56+
The font size and link targets are probably too small for a smartphone display, so I recommend using something bigger.
57+
58+
## Current Status
59+
60+
This is a work in progress.
61+
62+
The following chapters need some editing:
63+
64+
- Part II:
65+
- Substitution Maps
66+
- Conformances
67+
- Part III:
68+
- Conformance Paths
69+
- Part V:
70+
- Symbols, Terms, and Rules
71+
- Completion
72+
- Type Substitution Summary
73+
74+
The following chapters are not yet written:
75+
76+
- Part IV:
77+
- Opaque Return Types
78+
- Existential Types
79+
- Subclassing
80+
- Part V:
81+
- The Property Map
82+
- Concrete Conformances
83+
- Rule Minimization

docs/Generics/chapters/archetypes.tex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,16 @@ \section{Local Requirements}\label{local requirements}
155155
Recall that with a type parameter, global conformance lookup returns an abstract conformance without being able to check if the conformance actually holds (\SecRef{abstract conformances}). With archetypes, we implement a more useful behavior that distinguishes several cases. Let $\archetype{T}$ be an archetype with generic signature $G$, and let \texttt{P} be a protocol:
156156
\begin{enumerate}
157157
\item If $G\vdash\TC$ for some class type~\texttt{C}, and \texttt{C} conforms to \texttt{P}, then the archetype \emph{conforms concretely} to \texttt{P}. The class type \texttt{C} may contain type parameters, so global conformance lookup recursively calls itself on~$\MapIn(\texttt{C})$:
158-
\[\protosym{P}\otimes\archetype{T}=\protosym{P}\otimes\MapIn(\texttt{C})=\ConfReq{$\MapIn(\texttt{C})$}{P}\]
158+
\[\pP \otimes \archetype{T}=\pP \otimes \MapIn(\texttt{C})=\ConfReq{$\MapIn(\texttt{C})$}{P}\]
159159
The result is actually wrapped in an \index{inherited conformance}\emph{inherited conformance}, which doesn't do much except report the conforming type as $\archetype{T}$ rather than $\MapIn(\texttt{C})$ (\SecRef{inheritedconformance}).
160160
\item If $G\vdash\TP$, the archetype \emph{conforms abstractly}, and thus global conformance lookup returns an abstract conformance:
161-
\[\protosym{P}\otimes\archetype{T}=\ConfReq{$\archetype{T}$}{P}\]
161+
\[\pP\otimes\archetype{T}=\ConfReq{$\archetype{T}$}{P}\]
162162
\item Otherwise, $\archetype{T}$ does not conform to \texttt{P}, and global conformance lookup returns an invalid conformance.
163163
\end{enumerate}
164164
In (2), we return an abstract conformance whose subject type is an archetype. We need to explain what this means. To recap, when the original type of an abstract conformance is a type parameter~\tT, the type witness for \texttt{[P]A} is a dependent member type~\texttt{T.[P]A}. Similarly, the associated conformance for $\AssocConfReq{Self.U}{Q}{P}$ is the abstract conformance $\ConfReq{T.U}{Q}$. So, when the original type is an \index{abstract conformance!with archetype}archetype $\archetype{T}$, we must also map this \index{type witness!of abstract conformance}type witness or \index{associated conformance!of abstract conformance}associated conformance into the environment:
165165
\begin{align*}
166166
\AssocType{[P]A}\otimes \ConfReq{$\archetype{T}$}{P} &=\archetype{T.[P]A}= \MapIn(\texttt{T.[P]A})\\
167-
\AssocConf{Self.U}{Q}\otimes \ConfReq{$\archetype{T}$}{P} &=\ConfReq{$\archetype{T.U}$}{Q}= \protosym{Q}\otimes\MapIn(\texttt{T.U})
167+
\AssocConf{Self.U}{Q}\otimes \ConfReq{$\archetype{T}$}{P} &=\ConfReq{$\archetype{T.U}$}{Q}= \pQ\otimes\MapIn(\texttt{T.U})
168168
\end{align*}
169169

170170
\section{Archetype Substitution}\label{archetypesubst}
@@ -173,7 +173,7 @@ \section{Archetype Substitution}\label{archetypesubst}
173173
\[
174174
\TypeObj{\EquivClass{G}}\otimes\SubMapObj{G}{H}\longrightarrow\TypeObj{H}
175175
\]
176-
To apply a substitution map $\Sigma$ to a contextual type $\tT^\prime\in\TypeObj{\EquivClass{G}}$, we first map the the contextual type out of its environment, and then apply the substitution map to this interface type:
176+
To apply a substitution map $\Sigma$ to a contextual type $\tT^\prime\in\TypeObj{\EquivClass{G}}$, we first map the contextual type out of its environment, and then apply the substitution map to this interface type:
177177
\[\tT^\prime\otimes \Sigma = \MapOut(\tT^\prime)\otimes \Sigma\]
178178
If the replacement types of $\Sigma$ are interface types, we always get an interface type back, regardless of whether the original type was an interface type or a contextual type.
179179

@@ -415,7 +415,7 @@ \section{The Type Parameter Graph}\label{type parameter graph}
415415
\node (TA) [interior, right=of T] {\texttt{\rT.A}};
416416
\node (TAA) [interior, right=of TA] {\texttt{\rT.A.A}};
417417
\node (TAAA) [interior, right=of TAA] {\texttt{\rT.A.A.A}};
418-
\node (Rest) [interior, right=of TAAA] {\texttt{\vphantom{Ty}...}};
418+
\node (Rest) [right=of TAAA] {$\cdots$};
419419

420420
\path [arrow] (T) edge [above] node {\tiny{\texttt{.A}}} (TA);
421421
\path [arrow] (TA) edge [above] node {\tiny{\texttt{.A}}} (TAA);
@@ -597,14 +597,14 @@ \section{The Archetype Builder}\label{archetype builder}
597597
\item If $\PAForward(t)$ is non-null, load $t\leftarrow\PAForward(t)$, and repeat if necessary.
598598
\item If $\texttt{P}\in\PAConforms(t)$, return.
599599
\item Otherwise, set $\PAConforms(t)\leftarrow \PAConforms(t)\cup\{\texttt{P}\}$.
600-
\item For each protocol~\texttt{Q} appearing in the inheritance clause of the declaration of \texttt{P}, recursively apply this algorithm to $t$ and \texttt{Q}.
600+
\item For each protocol~\tQ\ appearing in the inheritance clause of the declaration of \texttt{P}, recursively apply this algorithm to $t$ and \tQ.
601601
\item Let $\tT:=\PAType(t)$.
602602
\item For each associated type declaration \texttt{A} of \texttt{P}:
603603
\begin{enumerate}
604604
\item If $\PAMembers(t)$ does not have an entry for \texttt{A}, create a new potential archetype~$v$ with $\PAType(v)=\texttt{T.A}$. Add the ordered pair $(\texttt{A}, v)$ to $\PAMembers(t)$.
605605

606606
Otherwise, $\PAMembers(t)$ contains an existing entry~$(\texttt{A}, v)$ with $\PAType(v)=\texttt{T.A}$.
607-
\item For each protocol $\texttt{Q}$ listed in the inheritance clause of the declaration of \texttt{A}, first check if \texttt{Q} already appears among the set of all protocols we've visited in prior recursive calls; if so, emit a diagnostic. Otherwise, add \texttt{Q} to the visited set, and recursively apply this algorithm to $v$ and \texttt{Q}.
607+
\item For each protocol \tQ\ listed in the inheritance clause of the declaration of \texttt{A}, first check if \tQ\ already appears among the set of all protocols we've visited in prior recursive calls; if so, emit a diagnostic. Otherwise, add \tQ\ to the visited set, and recursively apply this algorithm to $v$ and \tQ.
608608
\end{enumerate}
609609
\end{enumerate}
610610
\end{algorithm}

0 commit comments

Comments
 (0)