-
Notifications
You must be signed in to change notification settings - Fork 1
/
lec07.tex
241 lines (224 loc) · 8.85 KB
/
lec07.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
\sectionwithdate{Introduction to CPS Conversion}{2/6/2018}
First, let's tie up a loose end: structural equivalence of paths.
\begin{judgment}[Structural equivalence]
$\Gamma \vdash p \leftrightarrow p' : \kappa$
\[
\infer{\Gamma \vdash \alpha \leftrightarrow \alpha : \kappa}
{\Gamma(\alpha) = \kappa}
\]
\[
\infer[\star]{\Gamma \vdash p~c \leftrightarrow p'~c' : [c/\alpha]\kappa_2}
{\Gamma \vdash p \leftrightarrow p' : \Pi(\alpha : \kappa_1).\kappa_2
&\Gamma \vdash c \Leftrightarrow c' : \kappa_1
}
\]
\[
\infer{\Gamma \vdash \pi_1 p \leftrightarrow \pi_1 p' : \kappa_1}
{\Gamma \vdash p \leftrightarrow p' : \Sigma(\alpha : \kappa_1).\kappa_2}
\qquad
\infer{\Gamma \vdash \pi_2p \leftrightarrow \pi_2p' : [\pi p/\alpha]\kappa_2}
{\Gamma \vdash p \leftrightarrow p' : \Sigma(\alpha : \kappa_1).\kappa_2}
\]
\[ \infer{\Gamma \vdash (c_1 \rightarrow c_2) \leftrightarrow (c_1' \rightarrow c_2') : \T}
{\Gamma \vdash c_1 \Leftrightarrow c_2' : \T
&\Gamma \vdash c_2 \Leftrightarrow c_2' : \T
}
\qquad
\infer{\Gamma \vdash \forall(\alpha : \kappa).c \leftrightarrow
\forall(\alpha : \kappa').c' : \T}
{\Gamma \vdash \kappa \Leftrightarrow \kappa' : \kind
&\Gamma, \alpha : \kappa \vdash c \Leftrightarrow c' : \T
}
\]
\end{judgment}
In the starred rule, we could have either substituted $c$ or $c'$ for $\alpha$; either
way, the proof is made annoying. We introduced one more judgment along the way:
\begin{judgment}[Kind equivalence]
$\Gamma \vdash \kappa \Leftrightarrow \kappa' : \kind$
\[
\infer{\Gamma \vdash \T \Leftrightarrow \T : \kind}{}
\qquad
\infer{\Gamma \vdash \S(c) \Leftrightarrow \S(c') : \kind}
{\Gamma \vdash c \Leftrightarrow c' : \T}
\]
\[
\infer
{\deduce
{\Gamma \vdash \Sigma(\alpha : \kappa_1).\kappa_2 \Leftrightarrow
\Sigma(\alpha : \kappa_1').\kappa_2' : \kind}
{\Gamma \vdash \Pi(\alpha : \kappa_1).\kappa_2 \Leftrightarrow
\Pi(\alpha : \kappa_1').\kappa_2' : \kind}
}
{\Gamma \vdash \kappa_1 \Leftrightarrow \kappa_1' : \kind
&\Gamma, \alpha : \kappa_1 \vdash \kappa_2 \Leftrightarrow \kappa_2' : \kind
}
\]
\end{judgment}
Done!
\subsection{CPS conversion}
IL-Direct is roughly the core ML language. In this phase, we will translate IL-Direct
to IL-CPS using, well, CPS conversion.
\paragraph{IL-Direct}
\begin{bnf}
\tau \bnfeq
\alpha
\alt \tau \rightarrow \tau
\alt \forall(\alpha : \kappa). \tau
\alt \tau \times \tau
\alt \exists(\alpha : \kappa).\tau\\
e \bnfeq x \alt \lambda(x:\tau).e \alt \cdots
\end{bnf}
\paragraph{IL-CPS}
\begin{bnf}
\kappa \bnfeq \textit{(same as SKC)}\\
c \bnfeq \textit{(same as SKC)}
\alt c \times c
\alt \exists(\alpha : \kappa).c
\alt \neg c
\alt \unit\\
v \bnfeq x \alt \langle v, v \rangle \alt
\pack{c}{v}{\exists (\alpha : \kappa).\tau}
\alt \lambda(x : \tau).e \alt \ast\\
e \bnfeq
\letv{x}{v}{e}
\alt \letv{x}{\pi_i v}{e}
\alt \unpack{\alpha}{x}{v}{e}
\alt v~v
\alt \halt
\end{bnf}
Two typing judgments are our main concern at the term level.
\begin{judgment}[Value typing]
$\Gamma \vdash v : \tau$
\[
\infer{\Gamma \vdash x : \tau}{\Gamma(x) = \tau}
\qquad
\infer{\Gamma \vdash \langle v_1, v_2 \rangle : \tau_1 \times \tau_2}
{\Gamma \vdash v_1 : \tau_1
&\Gamma \vdash v_2 : \tau_2
}
\qquad
\infer{\Gamma \vdash \ast : \unit}{}
\]
\[
\infer{\Gamma \vdash \pack{c}{v}{\exists(\alpha : \kappa).\tau}
: \exists(\alpha : \kappa).\tau}
{\Gamma \vdash c : \kappa
&\Gamma \vdash v : [c/\alpha]\tau
&\Gamma, \alpha : \kappa \vdash \tau : \T
}
\qquad
\infer{\Gamma \vdash \lambda(x : \tau).e : \neg \tau}
{\Gamma, x : \tau \vdash e : \mathbf 0
&\Gamma \vdash \tau : \T
}
\]
\end{judgment}
\begin{judgment}[Expression typing]
$\Gamma \vdash e : \mathbf 0$
\[
\infer{\Gamma \vdash \letv x v e : \mathbf{0}}
{\Gamma \vdash v : \tau
&\Gamma, x : \tau \vdash e : \mathbf 0
}
\qquad
\infer
{\Gamma \vdash \letv x {\pi_i v} e: \mathbf 0}
{\Gamma \vdash v : \tau_1 \times \tau_2
&\Gamma \vdash x : \tau_I \vdash e : \mathbf 0
}
\]
\[
\infer
{\Gamma \vdash \unpack \alpha x v e : \mathbf 0}
{\Gamma \vdash v : \exists (\alpha : \kappa).\tau
&\Gamma, \alpha : \kappa, x : \tau \vdash e : \mathbf{0}
}
\]
\[
\infer
{\Gamma \vdash v_1~v_2 : \mathbf{0}}
{\Gamma \vdash v_1 : \neg \tau
& \Gamma \vdash v_2 : \tau
}
\qquad
\infer{\Gamma \vdash \halt : \mathbf{0}}{}
\]
\end{judgment}
John Reynolds remarked in his paper \emph{Definitional interpreters for higher-order
programming languages} that CPS conversion resolves any ambiguities in control flow. For example,
it's immediately apparent whether the semantics is call-by-value or call-by-name. We can remark
on additional aspects of CPS conversion. It:
\begin{enumerate}[1.]
\item names all intermediate computations,
\item makes control flow explicit, and
\item reifies continuations.
\end{enumerate}
Many compilers do 1 and 2 in what's known as A-normal form, not to be confused with anormal
form or a normal form. A-normal form is, formally, ``A it until you can't A it anymore.''
A more descriptive name might be monadic form, or two-thirds CPS.
\subsection{Typing derivation--directed translation}
We don't want to do syntax-directed translation, since this would involve putting type
information everywhere in IL-Direct. Here's an example of a type-directed translation
to warm up to IL-CPS.
We define $\overline{\cdot} : \text{IL-$X$ types} \to \text{IL-$Y$ types}$ so that
\begin{align*}
\text{if}\quad &\tau \quad \text{is an IL-$X$ type,}\\
\text{then}\quad &\taubar \quad \text{is an IL-$Y$ type.}
\end{align*}
Under this definition, we want to define a judgment
\[ \Gamma \vdash e : \tau \leadsto \ebar \]
such that, if $\Gamma \vdash e : \tau$, then $\Gamma\vdash : \tau \leadsto \ebar$ such that
(roughly) $\Gammabar \vdash \ebar : \taubar$. Just to reiterate: the translation
of $\tau$ to $\taubar$ is syntax-directed; $\ebar$ is just a meta-variable that we will give
meaning to with the translation judgment.
\subsection{Stupidification}
In this section, we define a stupid translation that does nothing meaningful.
\begin{align*}
\overline{\alpha} &= \alpha &\overline{\T} &= \T\\
\overline{\tau_1 \to \tau_2} &= \taubar_1 \to \taubar_2 &\overline{\S(c)} &= \S(c)\\
\overline{\tau_1 \times \tau_2} &= \taubar_1 \times (\taubar_1 \to \taubar_2) &\overline{\Pi(\alpha : \kappa_1).\kappa_2} &= \Pi(\alpha : \kappabar_1).\kappabar_2\\
&&\overline{\Sigma(\alpha : \kappa_1).\kappa_2} &= \Sigma(\alpha : \kappabar_1).\kappabar_2
\end{align*}
\begin{align*}
\overline{\varepsilon} &= \varepsilon\\
\overline{\Gamma, \alpha : \kappa} &= \Gammabar, \alpha : \kappabar\\
\overline{\Gamma, x : \tau} &= \Gammabar, x : \taubar
\end{align*}
I ellide the rules defining the translation judgment that just recursively translate the
constituent parts. Here are some of the more interesting rules:
\[
\infer
{\Gamma \vdash \langle e_1, e_2 \rangle : \tau_1 \times \tau_2
\leadsto \langle \ebar_1, \lambda(\_ : \taubar_1) : \ebar_2}
{\Gamma \vdash e_1 : \tau_1 \leadsto \ebar_1
&\Gamma \vdash e_2 : \tau_2 \leadsto \ebar_2}
\qquad
\infer
{\Gamma \vdash \pi_2e : \tau_2 \leadsto \letv{x}{\ebar}{\pi_2 x(\pi_1 x)}}
{\Gamma \vdash e : \tau_1 \times \tau_2 \leadsto \ebar}
\]
Now you see why it's important that the translation be type-directed instead of syntax-directed:
otherwise, we wouldn't have known that $\taubar_1$ in the pair introduction rule.
What are some properties we'd like?
\paragraph{(Effectiveness.)}
\begin{enumerate}[1.]
\item If $c$ is a constructor, then $\overline{c}$ is defined.
(Likewise for $\kappa$ and $\Gamma$.)
\item $\Gamma \vdash e : \tau$ is derivable iff $\Gamma \vdash e : \tau \leadsto \ebar$
for some $\ebar$.
\end{enumerate}
\paragraph{(Static correctness.)}
\begin{enumerate}[1.]
\item If $\vdash \Gamma~\mathsf{ok}$, then $\vdash \Gammabar~\mathsf{ok}$.
\item If $\Gamma \vdash \kappa : \kind$, then $\Gammabar \vdash \kappabar : \kind$.
\item If $\Gamma \vdash c : \kappa$, then $\Gammabar \vdash \overline{c} : \kappabar$ \ldots
\item \ldots and so on, until:
If $\Gamma \vdash e : \tau \leadsto \ebar$, then $\Gammabar \vdash \ebar : \taubar$.
\end{enumerate}
We might want dynamic correctness. But what is it? Dunno.
We might also want coherence: if $\Gamma \vdash e : \tau \leadsto \ebar$ and
$\Gamma \vdash e : \tau \leadsto \ebar'$, then $\Gammabar \vdash \ebar \cong \ebar' : \taubar$
for some suitable $\cong$.
But unfortunately ``$\cong$,'' like the origin of the Donkey Kong Wikia page that claims
that Funky Kong canonically fought in the Great Ape War,
remains shrouded in mystery.