-
Notifications
You must be signed in to change notification settings - Fork 0
/
lec4-systemf.tex
157 lines (122 loc) · 7.81 KB
/
lec4-systemf.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
\documentclass{notes}
\coursename{Logical Relations Mini-Course}
\lecnumber{4}
\title{System F and parametricity}
\author{Andrey Yao}
\newcommand{\unit}{()}
\newcommand{\Unit}{\texttt{unit}}
\newcommand{\V}{\mathcal{V}}
\newcommand{\E}{\mathcal{E}}
\newcommand{\bequiv}{\equiv_{\beta}}
\begin{document}
\maketitle
\section{Introduction}
\textit{[begins with a fable from Reynold on representation invariance and construction of the complex numberss]}
In this lecture, we talked about the idea of parametricity. In system F, the polymorphism is \textit{parametric}, in the sense that a generic function has to work ``the same way'' uniformly across all types. This surprisingly allows us to talk about properties of all inhabitants of certain polymorphic types, such as the type $\forall X.\;X\to X$. We developed a binary logical relation for system F, based on the idea of a ``compatible relation'', which are relations that respect beta-equivalence. The parametricity theorem follows from the fundamental theorem of logical relations in this case, and as a corollary we can prove some ``free theorems'', such as some of those from the Wadler paper ``Theorems for free!''. Some examples include that the type $\forall X.\;X\to X$ has only one inhabitant up to beta-eta equivalence, etc.
\section{System F}
The syntax of the language is STLC extended with two new expression constructs: type abstraction $\Lambda X.\;e$ and type application $e[\tau]$. The type level syntax is augmented with a new ``universal type'', $\forall X.\;\tau$. We don't introduce product types here for simplicity. Also, later we will see products are encodeable in System F.
%
\begin{syntax}
\category[Types]{\tau}
\alternative{\Unit}
\alternative{\tau_1 \rightarrow \tau_2}
\alternative{X}
\alternative{\forall X.\; \tau}
\category[Expressions]{e}
\alternative{\unit}
\alternative{x}
\alternative{\lambda x.\,e}
\alternative{e_1 \; e_2}
\alternative{\Lambda X.\; e}
\alternative{e[\tau]}
\end{syntax}
%
For the typing judgements, we need to keep track of another context $\Delta$, which contains a set of free type variables that one is allowed to use in the context $\Gamma$:
\begin{mathpar}
\infer{\;}{\Delta \vdash \Unit} \quad (\textsc{Unit}) \and
\infer{X \in \Delta}{\Delta \vdash X} \quad (\textsc{TypeVar}) \and
\infer{\Delta \vdash \tau_1 \\ \Delta \vdash \tau_2}{\Delta \vdash \tau_1 \to \tau_2} \quad (\textsc{Arrow}) \and
\infer{\Delta,X \vdash \tau}{\Delta \vdash \forall X.\;\tau} \quad (\textsc{Forall}) \and
\end{mathpar}
We can check well-formedness of contexts as well:
\begin{mathpar}
\infer{ }{\Delta \vdash \cdot} \and
\infer{\Delta \vdash \Gamma\\\Delta \vdash \tau}{\Delta \vdash \Gamma, x : \tau}
\end{mathpar}
To type check expressions, we need both $\Delta;\Gamma$:
\begin{mathpar}
\infer{\Delta \vdash \Gamma}{\Delta;\Gamma \vdash \unit:\Unit} (\textsc{Unit}) \and
\infer{\Delta \vdash \Gamma \\ x : \tau \in \Gamma}{\Delta;\Gamma \vdash x : \tau} \quad (\textsc{Axiom}) \and
\infer{\Delta; \Gamma, x : \tau_1 \vdash e : \tau_2}{\Delta;\Gamma \vdash \lambda x:\tau_1.\, e : \tau_1 \rightarrow \tau_2} \quad (\rightarrow_I) \and
\infer{\Delta;\Gamma \vdash e_1 : \tau_1 \to \tau_2 \\ \Delta;\Gamma \vdash e_2 : \tau_1}{\Delta;\Gamma \vdash e_1 \; e_2 : \tau_2} \quad (\rightarrow_E) \and
\infer{\Delta, X; \Gamma \vdash e : \tau}{\Delta;\Gamma \vdash \Lambda X.\, e : \forall X.\;\tau} \quad (\forall_I) \and
\infer{\Delta;\Gamma \vdash e : \forall X.\;\tau}{\Delta;\Gamma \vdash e[\tau'] : \tau[X\mapsto \tau']} \quad (\forall_E)
\end{mathpar}
\section{Operational semantics}
It's really just a CBV semantics similar to that of STLC. We add an additional rule to deal with concretizing the type variables used in the type annotations in $e$:
\begin{mathpar}
\infer{\;}{(\Lambda X.\;e)[\tau]\to_\beta e[X \mapsto \tau]}
\end{mathpar}
\section{Logical relations}
Before defining a logical relations on system F, we need to develop the following notion:
\begin{defn}[Compatible relation (Old)]
A binary relation $R$ on the set of expressions is called compatible if:
\[\textbf{forall}(e_1,e_2,e_1',e_2'), e_1\bequiv e_1'\land e_2\bequiv e_2'\land R(e_1, e_2)\implies R(e_1',e_2')\]
More informally, a compatible relation is one that is invariant under beta-redution, or beta-equivalence.
\end{defn}
\begin{defn}[Compatible Relation (CBV)]
A binary relation $R$ on the set of expressions is called \emph{compatible} if:
$$\forall e_1, e_2, R(e_1, e_2) \implies e_1~\text{is a value} \land e_2~\text{is a value}$$
\end{defn}
We also use $\rho$ to denote a partial mapping from type variables to compatible relations.
Now we can define the logical relations:
\begin{align*}
\V_\rho(X)(v_1,v_2) &\triangleq \rho(X)(v_1,v_2)\\
\V_\rho(\Unit)(v_1,v_2) &\triangleq v_1=v_2=\unit\\
\V_\rho(\tau_1\to\tau_2)(v_1,v_2) &\triangleq \textbf{exists}(x,y,e_1e_2),\;v_1=\lambda x.\;e_1\land v_2=\lambda y.\;e_2\land\\&\quad\;\textbf{forall}(v_1',v_2'),\,V_\rho(\tau_1)(v_1',v_2')\implies \E_\rho(\tau_2)(e_1[x\mapsto v_1'],e_2[y\mapsto v_2'])\\
\V_\rho(\forall X.\tau)(v_1,v_2) &\triangleq \textbf{exists}(X,e_1,e_2),\;v_1=\Lambda X.\;e_1\land v_2=\Lambda X.\;e_2\land \textbf{forall}(\text{compatible}\;R),\,\E_{\rho[X\mapsto R]}(e_1,e_2)\\
\noalign{\smallskip} \hline \noalign{\smallskip}
\E_\rho(\tau)(e_1,e_2) &\triangleq \textbf{exists}(v_1,v_2),\;e_1\to^*v_1\land e_2\to^*v_2\land \V_\rho(\tau)(v_1,v_2)
\end{align*}
As a sanity check, we can try to show this following result:
\begin{prop}[Polymorphic identity related to itself]
\[\V_\rho(\forall X.\;X\to X)(\Lambda X.\;\lambda x:X.\;x,\;\;\Lambda X.\;\lambda x:X.\;x)\]
\end{prop}
\begin{proof}
Just unfold the definitions, and you will find that eventually you want to prove that: given any $\rho$ and compatible relation $R$, $\V_{\rho[X\mapsto R]}(X\to X)(\lambda x:X.x,\;\lambda x:X.x)$ holds. The rest is straightforward.
\end{proof}
\begin{defn}[Good relation substitution]
\[\Delta\vDash \rho \triangleq \Delta\subseteq \operatorname{dom}(\rho)\]
\end{defn}
\begin{defn}[Semantically related expression substitution]
Two parallel substitution $\gamma_1,\gamma_2$ (partial maps from expression variables to expressions) are semantically related w.r.t. a context $\Gamma$ and a relation substitution $\rho$ when:
\[\Gamma\vDash_\rho \gamma_1\sim \gamma_2 \triangleq \textbf{forall} (x:\tau)\in \Gamma, \V_\rho(\tau)(\gamma_1(x),\gamma_2(x))\]
\end{defn}
\begin{defn}[Semantically related (in type $\tau$)]
\[\Delta;\Gamma\vDash e_1\sim e_2:\tau \triangleq \textbf{forall}(\rho,\gamma_1,\gamma_2),\;\Delta\vDash \rho\land \Gamma\vDash_\rho \gamma_1\sim\gamma_2\implies \E_\rho(\tau)(e_1[\gamma_1], e_2[\gamma_2])\]
\end{defn}
\begin{thm}[Parametricity]
\[\Delta;\Gamma\vdash e:\tau \implies \Delta;\Gamma\vDash e \sim e:\tau\]
This reads ``well-typed expressions are related to itself''
\end{thm}
\section{Homework}
\subsection{Problem 1)}
We can encode product types and pairs, projections into system F as follows:
\begin{align*}
\tau_1\times \tau_2 &\triangleq \forall Z.\;(\tau_1\to\tau_2\to Z)\to Z\\
\texttt{pair} :\forall X\;Y.\;X\to Y\to X\times Y &\triangleq \Lambda X\;Y.\;\lambda x:X.\;\lambda y:Y.\;\Lambda Z.\;\lambda f:X\to Y\to Z.\;f\,x\,y\\
\texttt{fst}: \forall X\;Y.\;X\times Y\to X &\triangleq \Lambda X\;Y.\lambda p:X\times Y.\;p[X](\lambda x.\;\lambda y.x)
\end{align*}
\textit{Exercise:} Define the \texttt{snd} operator.
\subsection{Problem 2)}
Show that for any types $\tau_1,\tau_2$ and expressions $e_1,e_2$:
\[\E_\rho(\tau_1\times \tau_2)(e_1,e_2) \iff (\E_\rho(\tau_1)(\texttt{fst}~e_1\sim \texttt{fst}~e_2)\land \E_\rho(\tau_2)(\texttt{snd}~e_1\sim \texttt{snd}~e_2))\]
\section{Further readings}
\begin{enumerate}
\item {Wadler: Theorems for free!}\;{https://dl.acm.org/doi/10.1145/99370.99404}
\end{enumerate}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End: