-
Notifications
You must be signed in to change notification settings - Fork 1
/
lec03.tex
280 lines (258 loc) · 10.1 KB
/
lec03.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
\sectionwithdate{Explicit substitution}{1/23/2018}
In this lecture, we introduce a representation of substitutions as mathematical objects
rather than resorting to meta-mathematical reasoning. We adopt the presentation of de
Bruijn indices that will be used in the first project.
\subsection{Intuition and Examples}
Throughout, we use an \emph{ordered context}. As a substitution is being performed,
variables are bound to this context. There may still be free variables whose index
exceeds the number of items in the context; we will have to decrement the indices
of these.
Given the term (in de Bruijn indices)
\[ (\lambda. \lambda. 2 + 0 + 3) [M/0]~~~~\text{,} \]
it would be quite nice if this reduced to
\[ (\lambda. \lambda. M + 0 + 2)~~~~\text{.} \]
Notice again how we talk about substitution as being part of the \emph{term} and of
being \emph{reduced}---this is the first sign that we're modeling substitution
explicitly.
The other context explored today is \emph{shifting} (or \text{lifting}), which involves
yet more terms to the ordered context. Let's give some examples of familiar terms and
their de Bruijn equivalents.
\begin{align}
w &\vdash \lambda y. \lambda z. z+w && \Longrightarrow && \lambda.\lambda. 0 + 2\\
w, x & \vdash \lambda y. \lambda z. z + w && \Longrightarrow && \lambda. \lambda. 0 + 3
\end{align}
In (2), we must shift the index of $w$ by 1 to refer to the correct position in
the ordered context.
\subsection{Modelling substitutions explicitly}
We model our approach after \url{http://www.hpl.hp.com/techreports/Compaq-DEC/SRC-RR-54.pdf},
taking some liberties, like indexing from 0.
First, our grammar of terms:
\begin{bnf}
M \bnfeq i \alt \lambda.M \alt M~M \alt M[\sigma]
\end{bnf}
$i$ denotes de Bruijn indices, and postfix brackets indicate substitution. Now, our grammar
of substitutions $\sigma$.
\begin{bnf}
\sigma \bnfeq M \cdot \sigma \alt \uparrow^n
\end{bnf}
$\cdot$ functions as cons\footnote{In lecture, we used a period ``.'', but this notation is misleadingly
suggestive of binding.}. We usefully abbreviate $\uparrow^0$ as $\id$, and $\uparrow^1$ as
$\uparrow$.
Substitution is given meaning by equations:
\begin{align*}
0[M \cdot \sigma] &= M\\
(i+1)[M \cdot \sigma] &= i[\sigma]\\
i[\uparrow^n] &= i+n\\
(M_1~M_2)[\sigma] &= M_1[\sigma]~M_2[\sigma]\\
(\lambda.M)[\sigma] &= \lambda.M[0 \cdot (\sigma \circ \uparrow)]
\end{align*}
Unlike the reference, we don't model $\circ$ as primitive but rather define it as a binary operator
over substitutions. Our goal is for $M[\sigma \circ \sigma'] = M[\sigma][\sigma']$.
Here's how you compute it, assuming $\cdot$ to bind tighter than $\circ$.
\begin{align*}
\uparrow^m &\circ \uparrow^n &&= \uparrow^{m+n}\\
\id &\circ M \cdot \sigma &&= M \cdot \sigma\\
\uparrow^{m+1} &\circ M \cdot \sigma &&= \uparrow^n \cdot \sigma\\
M \cdot \sigma &\circ \sigma' &&= M[\sigma'] \cdot (\sigma \circ \sigma')
\end{align*}
Composition here can be pronounced ``before.'' The last rule is the only interesting one, indicating
that later substitutions may depend on prior ones, since we must perform $M[\sigma']$.
For example, we would like the following to hold:
\begin{align*}
(0+1) [M \cdot \uparrow \circ \uparrow^4] &= (0+1) [M \cdot \uparrow][\uparrow^4]\\
&= (M + 1)[\uparrow^4]\\
&= M[\uparrow^4] + 5
\end{align*}
And by our equations, it in fact does.
\begin{align*}
(0 + 1) [M \cdot \uparrow \circ \uparrow^4] &= (0 + 1) [M[\uparrow^4] \cdot (\uparrow \circ \uparrow^4)]\\
&= (0 + 1) [M[\uparrow^4] \cdot \uparrow^5]\\
&= M[\uparrow^4] + 5
\end{align*}
\subsection{Rule conversions}
With these notions of indices and binding, we convert some rules from
from \thref{weak} and \thref{algequiv}. Importantly, the context becomes ordered with this conversion.
\begin{judgment}[Rule conversions with de Bruijn indices]
\[
\vcenter{
\infer{(\lambda (\alpha : \kappa). c)~c' \leadsto [c'/\alpha] c}{}
}
\qquad
\Longrightarrow_\emph{de Bruijn}
\qquad
\vcenter{
\infer{(\lambda \kappa.c)~c' \leadsto c[ c' \cdot \id]}{}
}
\]
\[
\vcenter{
\infer
{\Gamma \vdash c \Leftrightarrow c' : \kappa_1 \rightarrow \kappa_2}
{\Gamma, \alpha : \kappa_1 \vdash c~\alpha \Leftrightarrow c'~\alpha : \kappa_2}
}
\qquad
\Longrightarrow_\emph{de Bruijn}
\qquad
\vcenter{
\infer
{\Gamma \vdash c \Leftrightarrow c' : \kappa_1 \rightarrow \kappa_2}
{\Gamma, \kappa_1 \vdash c[\uparrow]~0 \Leftrightarrow c'[\uparrow]~0 : \kappa_2}
}
\]
\end{judgment}
\subsection{Substitutions we'll use}
Rather than using substitutions in their full generality, we are really concerned with substitutions
of the form $[ 0 \cdot 1 \cdots n-1 \cdot M_1[\uparrow^n] \cdots M_k[\uparrow^n] \cdot \uparrow^{n+\ell}]$.
This is still pretty general:
\begin{enumerate}[1.]
\item $[M \cdot \id]$ has $n = 0, k = 1, \ell = 0$.
\item $[ \uparrow^\ell ]$ has $n = 0, k = 0$.
\item For $\sigma$ of the desired form, the substitution $[0 \cdot (\sigma \circ \uparrow)]$ retains the form:
$$0 \cdot (( 0 \cdot 1 \cdots n-1 \cdot M_1[\uparrow^n] \cdots M_k[\uparrow^n] \cdot \uparrow^{n+\ell}) \circ \uparrow)$$
$$\Downarrow$$
$$0 \cdot 1 \cdots n \cdot M_1[\uparrow^{n+1}] \cdots M_k[\uparrow^{n+1}] \cdot \uparrow^{n+\ell+1}$$
\subsection{The type theory of explicit substitution}
Assuming the type theory of the underlying language, we want to show that:
\[ \textit{If} \quad \Gamma \vdash \sigma : \Gamma'
\quad \textit{and} \quad \Gamma' \vdash M : A, \quad \textit{then} \quad \Gamma \vdash M[\sigma] : A[\sigma].
\]
There are a few things to note. One is that the type of a substitution $\sigma$ can be described be
an ordered list of types; in other words, the type of $\sigma$ is a context $\Gamma'$. Next,
the use of $A[\sigma]$ indicates that the type $A$ may refer to variables in the context $\Gamma$.
This will require some thought about dependent types. Finally, this framework applies
equally well to $M$ indicating a term and $A$ a type as it does to $M$ indicating a type
and $A$ a kind.
Below, we assume $\Gamma$ to be an ordered context of the form $\varepsilon$ or $\Gamma, A$.
\begin{judgment}[Substitution typing---general rules]
\[
\infer{\Gamma \vdash M \cdot \sigma : \Gamma', A}
{\Gamma \vdash \sigma : \Gamma' & \Gamma \vdash M : A[\sigma]}
\]
\[
\infer{\Gamma \vdash \uparrow^k : \Gamma'}
{\Gamma = \Gamma', A_1, \ldots, A_k}
\qquad
\infer{\varepsilon \vdash \id : \varepsilon}{}
\]
\end{judgment}
We can use these rules to perform a simple derivation after converting to de Bruijn indices.
We wish to derive the RHS of:
\[
y : \int \vdash [\int/\alpha, y/x] : \alpha : \T, x : \alpha
\qquad \Longrightarrow_\textnormal{deBruijn} \qquad
\int \vdash \int \cdot 0 \cdot \uparrow : \varepsilon, \T, 0
\]
We show this, explicitly including $\varepsilon$ as part of the types of substitutions
(so that it is slightly easier to distinguish judgments on the types of substitutions
and judgments on the kinds of types).
\[
\infer
{\int \vdash 0 \cdot \int \cdot \uparrow : \varepsilon, \T, 0}
{\infer
{\int \vdash \int \cdot \uparrow : \varepsilon, \T}
{\infer
{\int \vdash \uparrow : \varepsilon}
{}
&\infer
{\int \vdash \int : \T[\uparrow]}
{}
}
&\infer
{\int \vdash 0 : 0[\int.\uparrow]}
{\infer
{\int \vdash 0 : \int}
{}
}
}
\]
We perform transformations on the rules of \thref{kind}, now, in this type-theoretic setting.
\begin{judgment}[Type kind with substitutions]
\[
\vcenter{ \infer{\Gamma \vdash \alpha : \kappa}{\Gamma(\alpha) : \kappa} }
\qquad
\Longrightarrow
\qquad
\vcenter{ \infer{\Gamma \vdash i : \kappa[\uparrow^{i+1}]}{\Gamma(i) : \kappa} }
\]
\[
\vcenter{
\infer{\Gamma \vdash \lambda(\alpha : \kappa).c : \kappa \to \kappa'}
{\Gamma, \alpha : \kappa \vdash c : \kappa'}
}
\qquad
\Longrightarrow
\qquad
\vcenter{
\infer{\Gamma \vdash \lambda \kappa. c : \kappa \rightarrow \kappa'}
{\Gamma, \kappa \vdash c : \kappa'[\uparrow]}
}
\]
Unchanged rules:
\[
\infer{\Gamma \vdash c_1 ~ c_2 : \kappa'}
{\Gamma \vdash c_1 : \kappa \rightarrow \kappa'
&\Gamma \vdash c_2 : \kappa
}
\qquad
\infer{\Gamma \vdash \forall(\alpha : \kappa).c : \T}
{\Gamma, \alpha : \kappa \vdash c : \T}
\]
\end{judgment}
The $\forall$ rule may be surprising, since there are no shifts involved, but this is only
because there's no need to shift $\T$ up as in the lambda rule; $\T$ has no variables.
Finally, we give the same sort of transformations for our term language, transforming
rules from \thref{term}.
\begin{judgment}[Term type with substitution]
\[
\vcenter{ \infer{\Gamma \vdash x : \tau}{\Gamma(x) : \tau} }
\qquad
\Longrightarrow
\qquad
\vcenter{ \infer{\Gamma \vdash i : \tau[\uparrow^{i+1}]}{\Gamma(i) : \tau} }
\]
\[
\vcenter{
\infer{\Gamma \vdash e[\tau] : [\tau / \alpha]\tau'}
{\Gamma \vdash e : \forall(\alpha : \kappa).\tau'
&\Gamma \vdash \tau : \kappa
}
}
\qquad
\Longrightarrow
\qquad
\vcenter{
\infer{\Gamma \vdash e[c \cdot \id] : \tau [c \cdot \id]}
{\Gamma \vdash e : \forall \kappa. \tau
& \Gamma \vdash c : \kappa
}
}
\]
\[
\vcenter{
\infer{\Gamma \vdash \lambda(x : \tau).e : \tau \rightarrow \tau'}
{\Gamma, x : \tau \vdash e : \tau'
&\Gamma \vdash \tau : \kappa
}
}
\qquad
\Longrightarrow
\qquad
\vcenter{
\infer{\Gamma \vdash \lambda \tau. e : \tau \rightarrow \tau'}
{\Gamma, \tau \vdash e : \tau'[\uparrow]}
}
\]
Unchanged rules:
\[
\infer{\Gamma \vdash \Lambda (\alpha : \kappa).e : \forall (\alpha : \kappa).\tau}
{\Gamma, \alpha : \kappa \vdash e : \tau}
\qquad
\infer{\Gamma \vdash e_1 ~ e_2 : \tau'}
{\Gamma \vdash e_1 : \tau \rightarrow \tau'
&\Gamma \vdash e_2 : \tau
}
\]
\end{judgment}
Note that the lambda rule above was extrapolated by myself and not specifically covered
in lecture. Mistakes are mine. It is known.
\end{enumerate}