\documentclass{article}
\usepackage{homework}

\homework{1}
\due{11am, 11 Mar 2025}


%% Packages
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{xspace}
\usepackage{fullpage}
\usepackage{amsmath,amsthm,amssymb}
\usepackage{mathpartir}
\usepackage{latexsym}
\usepackage{verbatim}
\usepackage{stmaryrd}


%% Local definitions
\newcommand{\hide}[1]{}
\theoremstyle{definition}
\newtheorem*{lem}{Lemma}
\newtheorem{exercise}{Exercise}
\newtheorem*{thm}{Theorem}
\newcommand{\case}{\item \textbf{Case:~}}
\newenvironment{pf}[1][]{%
\noindent%
\textbf{Proof}%
\begin{list}{}{}\item[]}{%
\qed
\end{list}}
\newenvironment{proofE}[1][]{%
\noindent%
\textbf{Proof}%
\begin{list}{\arabic{prfi}.}{\usecounter{prfi}}}{%
\qed
\end{list}}

\newcommand{\namedrule}[3]{\inferrule{#1}{#2}\ \text{({\sc #3})}}
\newcommand{\rulename}[1]{\textsc{#1}}
\newcommand{\lit}[1]{\textsf{#1}}
\newcommand{\FV}{\mathit{FV}}
\newcommand{\dom}{\mathop{\mathrm{dom}}}
\newcommand{\var}[1]{\mathit{#1}}

\newcommand{\decl}[2]{#1{:}#2}
\newcommand{\rdecl}[2]{#1\,{:}\,#2}
\newcommand{\rbind}[2]{#1 = #2}
\newcommand{\bind}[2]{#1.\, #2}
\newcommand{\tbind}[3]{\bind{\decl{#1}{#2}}{#3}}

\newcommand{\step}[2]{#1 \longrightarrow #2}
\newcommand{\stepstar}[2]{#1 \longrightarrow^* #2}

\newcommand{\etrue}{\lit{true}}
\newcommand{\efalse}{\lit{false}}
\newcommand{\eif}[4]{\lit{if}_{#1}\,#2\,\lit{then}\,#3\,\lit{else}\,#4}
\newcommand{\enot}{\lit{not}}
\newcommand{\eand}{\lit{and}}
\newcommand{\eor}{\lit{or}}
\newcommand{\ezero}{\lit{zero}}
\newcommand{\esucc}[1]{\lit{succ}\,#1}
\newcommand{\enil}{\lit{nil}}
\newcommand{\econs}{\lit{cons}}
\newcommand{\eleaf}{\lit{leaf}}
\newcommand{\enode}{\lit{node}}
\newcommand{\erec}[5]{\kwd{rec}(#1,\, #2,\, \tbind{#3}{#4}{#5})}
\newcommand{\eapp}[2]{#1\ #2}
\newcommand{\eApp}[2]{#1\,[#2]}
\newcommand{\eunit}{\langle \rangle}
\newcommand{\epair}[2]{\langle #1, #2 \rangle}
\newcommand{\efst}[1]{\lit{fst}\,#1}
\newcommand{\esnd}[1]{\lit{snd}\,#1}
\newcommand{\einl}[1]{\lit{inl}\,#1}
\newcommand{\einr}[1]{\lit{inr}\,#1}
\newcommand{\ecase}[5]{\lit{case}\,#1\,\lit{of}\,\einl{#2}\Rightarrow\,#3
  \mid\mid\,\einr{#4}\Rightarrow\,#5}
\newcommand{\epack}[4]{\lit{pack}\,\langle #1,#2 \rangle \,\lit{as}\:{\texist #3 #4}}
\newcommand{\eunpack}[4]{\lit{unpack}\,\langle #2,#3 \rangle = #1\:\lit{in}\:#4}
%\newcommand{\elet}[3]{\lit{let}\,#1 = #2\,\lit{in}\,#3}

\newcommand{\eproj}[2]{#1.#2}
\newcommand{\eunroll}[1]{\kwd{unroll}\,#1}
\newcommand{\eannot}[2]{(#1 : #2)}
%\newcommand{\elam}[2]{\lambda \bind{#1}{#2}}
\newcommand{\elam}[3]{\lambda \tbind{#1}{#2}{#3}}
\newcommand{\eLam}[2]{\Lambda \bind{#1}{#2}}
\newcommand{\erecord}[1]{\{\, #1 \,\}}
\newcommand{\einj}[2]{[\, \rbind{#1}{#2} \,]}
%\newcommand{\ecase}[4]{\kwd{case}\,#1\,\{\,\einj{#2}{#3} \Rightarrow #4\,\}}
\newcommand{\enatcase}[4]{\kwd{natcase}\,#1\,\kwd{of}\,\{\,\lzero \Rightarrow #2,%
                                                 \lsucc\,#3 \Rightarrow #4\,\}}
\newcommand{\elistcase}[6]{\kwd{listcase}_#1\,#2\,%
                            \{\,\lnil \Rightarrow #3,%
                                \lcons(#4, #5) \Rightarrow #6\,\}}
\newcommand{\eroll}[1]{\lit{roll}\,#1}
%\newcommand{\efix}[2]{\kwd{fix}\,\bind{#1}{#2}}

\newcommand{\efix}[3]{\kwd{fix}\,\tbind{#1}{#2}{#3}}
\newcommand{\etroll}[2]{\lit{roll}_{#1}\,{#2}}
\newcommand{\etinj}[3]{\einj{#1}{#2}_{#3}}
\newcommand{\etcase}[3]{\kwd{case}_{#1}\,#2\,\{\,#3\,\}}
\newcommand{\casecase}[3]{\einj{#1}{#2} \Rightarrow #3}
\newcommand{\etcasen}[5]{\kwd{case}_{#1}\,#2\,\{\,\casecase{#3}{#4}{#5}\,\}}

% \newcommand{\steval}[2]{#1 > #2}
% \newcommand{\stret}[2]{#1 < #2}
% \newcommand{\stfail}[2]{#1 < \lit{fail}}

\newcommand{\tbool}{\lit{bool}}
\newcommand{\tnat}{\lit{nat}}
\newcommand{\tstr}{\lit{string}}
\newcommand{\tint}{\lit{int}}
\newcommand{\tnatlist}{\lit{natlist}}
\newcommand{\tlist}[1]{#1\,\lit{list}}
\newcommand{\ttree}[1]{#1\,\lit{tree}}
\newcommand{\tarrow}[2]{#1 \rightarrow #2}
\newcommand{\tunit}{\lit{unit}}
\newcommand{\tprod}[2]{#1 \times #2}
\newcommand{\tsum}[2]{#1 + #2}
\newcommand{\trecord}[1]{\{\, #1 \,\}}
\newcommand{\tvariant}[1]{[\, #1 \,]}
\newcommand{\tmu}[2]{\mu \bind{#1}{#2}}
\newcommand{\tforall}[2]{\forall \bind{#1}{#2}}
\newcommand{\texist}[2]{\exists \bind{#1}{#2}}

\newcommand{\rlabel}[1]{\lit{#1}}
\newcommand{\lzero}{\rlabel{zero}}
\newcommand{\lsucc}{\rlabel{succ}}
\newcommand{\lnil}{\rlabel{nil}}
\newcommand{\lcons}{\rlabel{cons}}
\newcommand{\lconstwo}{\rlabel{cons2}}

% Stack formatting
\newenvironment{stackAux}[2]{%
\setlength{\arraycolsep}{0pt}
\begin{array}[#1]{#2}}{
\end{array}}
\newenvironment{stackCC}{
\begin{stackAux}{c}{c}}{\end{stackAux}}
\newenvironment{stackCL}{
\begin{stackAux}{c}{l}}{\end{stackAux}}
\newenvironment{stackTL}{
\begin{stackAux}{t}{l}}{\end{stackAux}}
\newenvironment{stackTR}{
\begin{stackAux}{t}{r}}{\end{stackAux}}
\newenvironment{stackBC}{
\begin{stackAux}{b}{c}}{\end{stackAux}}
\newenvironment{stackBL}{
\begin{stackAux}{b}{l}}{\end{stackAux}}

% logical relation
\newcommand{\rel}[3]{#1:#2 \leftrightarrow #3}
\newcommand{\valeq}[4]{#1 \approx #2 : #3 \mid #4}
\newcommand{\expeq}[4]{#1 \sim #2 : #3 \mid #4}
\newcommand{\substeq}[4]{#1 \approx #2 : #3 \mid #4}
\newcommand{\dsat}[2]{#1 \models #2}
%\newcommand{\oexpeq}[5]{#1;#2 \vdash #3 \sim #4 : #5}
\renewcommand{\iff}{\mathrm{iff}}

\newcommand{\lsem}{\left\llbracket}
\newcommand{\rsem}{\right\rrbracket}
\newcommand{\sembrace}[1]{\lsem{#1}\rsem}

\newcommand{\Rel}[2]{\mathrm{Rel}[#1,#2]}
\newcommand{\Atom}[2]{\mathrm{Atom}[#1]#2}
% Logical Relation
\newcommand{\Vrel}[1]{\mathcal{V}\sembrace{#1}}
\newcommand{\Erel}[1]{\mathcal{E}\sembrace{#1}}
\newcommand{\Drel}[1]{\mathcal{D}\sembrace{#1}}
\newcommand{\Grel}[1]{\mathcal{G}\sembrace{#1}}
\newcommand{\lrjudg}[5]{#1;#2 \vdash #3 \approx #4 : #5}

\begin{document}

\maketitle

You may turn in handwritten solutions to this assignment---but
make sure you write clearly and leave lots of whitespace.
If you choose to typeset your solutions, the LaTeX sources are
available for your use at the course website.  (Look in
hw1.tex for macros you can use.)  A \textsf{pdf} file containing
the solutions can be submitted via email (use subject: cs7470 hw1 submission).

\vspace{1ex}

This homework is worth 140 points.  

\vspace{4ex}



\section*{Parametricity}

Recall from class the following \emph{logical relation} for System F
(where we assume a call-by-value operational semantics for System F).  

\paragraph{Notation} Below, the metavariable $R$ ranges over relations
on closed values (i.e., values with no free type or term variables). 
The metavariable $\rho$ ranges over finite maps from type variables
$\alpha$ to triples $(\tau_1,\tau_2,R)$, where $\tau_1$ and $\tau_2$
are closed types and $R$ is a value relation.  Finally, the
metavariable $\gamma$ ranges over finite maps from term variables $x$
to pairs of closed values. 

If $\rho = \{\alpha_1 \mapsto (\tau_{11},\tau_{12},R_1), \ldots,
  \alpha_n \mapsto (\tau_{n1},\tau_{n2},R_n)\}$ and 
$\gamma = \{x_1 \mapsto (v_{11},v_{12}), \ldots,
  x_m \mapsto (v_{m1},v_{m2})\}$, then
\begin{itemize}
\item
$\rho_1(\tau)$ denotes $\tau[\tau_{11}/\alpha_1,\ldots,\tau_{n1}/\alpha_n]$ 
and $\rho_2(\tau)$ denotes $\tau[\tau_{12}/\alpha_1,\ldots,\tau_{n2}/\alpha_n]$
\item
$\rho_1(e)$ denotes $e[\tau_{11}/\alpha_1,\ldots,\tau_{n1}/\alpha_n]$ 
and $\rho_2(e)$ denotes
$e[\tau_{12}/\alpha_1,\ldots,\tau_{n2}/\alpha_n]$
\item 
$\gamma_1(e)$ denotes $e[v_{11}/x_1,\ldots,v_{m1}/x_m]$ 
and $\gamma_2(e)$ denotes
$e[v_{12}/x_1,\ldots,v_{m2}/x_m]$

\end{itemize}

\[
\begin{array}{lcl}
\Rel {\tau_1} {\tau_2} & = & 
\{\, R \subseteq \mathit{Val}\times\mathit{Val} \mid~
 \forall(v_1,v_2) \in R.~ \vdash v_1 : \tau_1 ~\land~ \vdash v_2 :
                             \tau_2 \,\} \\[4pt] 
\Atom \tau \rho & = & 
\{\, (e_1,e_2) \mid~ \vdash e_1 : \rho_1(\tau) 
         ~\land~ \vdash e_2 : \rho_2(\tau) \,\} \\
& & \\
\Vrel \alpha \rho & = & \{\,(v_1,v_2) \mid~
\rho(\alpha) = (\tau_1,\tau_2,R) ~\land~ (v_1,v_2) \in R \,\}\\[4pt] 
\Vrel {\tarrow{\tau}{\tau'}} \rho  & = &  
\{\,(\elam x {\rho_1(\tau)} {e_1}, \elam x {\rho_2(\tau)} {e_2}) \in
                                         \Atom {\tarrow{\tau}{\tau'}} \rho
\mid~\\
& & \qquad 
\forall v_1,v_2.~ (v_1,v_2) \in \Vrel {\tau} \rho ~\implies~
(e_1[v_1/x], e_2[v_2/x]) \in \Erel {\tau'} \rho \,\}\\[4pt] 
\Vrel {\tforall \alpha \tau} \rho & = & 
\{\, (\eLam \alpha e_1, \eLam \alpha e_2) \in \Atom {\tforall \alpha \tau} \rho
\mid~ \\
& & \qquad
\forall \tau_1,\tau_2,R.~ 
R \in \Rel{\tau_1}{\tau_2} ~\implies~
(e_1[\tau_1/\alpha], e_2[\tau_2/\alpha]) \in \Erel \tau {\rho[\alpha
  \mapsto (\tau_1,\tau_2,R)]} \,\}\\
& & \\
\Erel \tau \rho & = &
\{\, (e_1,e_2) \in \Atom \tau \rho \mid~
\exists v_1, v_2 .~\stepstar {e_1} {v_1} ~\land~ 
                   \stepstar {e_2} {v_2} ~\land~ 
                   (v_1,v_2) \in \Vrel \tau \rho \,\} \\
& & \\
\Drel{\cdot} & = & \{\, \emptyset \,\}\\
\Drel{\Delta,\alpha} & = & \{\, \rho[\alpha
  \mapsto (\tau_1,\tau_2,R)] \mid~ \rho \in \Drel \Delta ~\land~
R \in \Rel{\tau_1}{\tau_2} \,\}\\[4pt]

\Grel{\cdot} \rho & = & \{\, \emptyset \,\}\\
\Grel{\Gamma,x:\tau} \rho & = & \{\, \gamma[x \mapsto (v_1,v_2)] \mid~ 
\gamma \in \Grel \Gamma \rho ~\land~
(v_1,v_2) \in \Vrel{\tau}\rho \,\}\\
& & \\
\lrjudg \Delta \Gamma {e_1} {e_2} \tau & \defeq & 
\forall \rho, \gamma.~
\rho \in \Drel \Delta ~\land~ \gamma \in \Grel \Gamma \rho
~\implies~
(\rho_1(\gamma_1(e_1)), \rho_2(\gamma_2(e_2))) \in \Erel \tau \rho
\end{array}
\]

\newpage

\begin{thm}[Parametricity] 
If $\Delta;\Gamma \vdash e : \tau$ then $\lrjudg \Delta \Gamma e e
\tau$.
\end{thm}
The proof is by induction on the typing derivation $\Delta;\Gamma
\vdash e : \tau$.  \\

\begin{itemize}
\case
\namedrule 
{\Delta;\Gamma,x:\tau \vdash e : \tau'}
{\Delta;\Gamma \vdash {\elam x {\tau} e} : \tarrow {\tau} {\tau'}}
{Abs} 

\begin{pf}
We are required to show that 
$\lrjudg \Delta \Gamma 
{\elam x {\tau} e} {\elam x {\tau} e} {\tarrow {\tau} {\tau'}}$. 
\par
Consider arbitrary $\rho$ and $\gamma$ such that
\begin{itemize}
\item $\rho \in \Drel \Delta$, and
\item $\gamma \in \Grel \Gamma \rho$. 
\end{itemize}
We are required to show that 
$\begin{stackTL}
(\rho_1(\gamma_1({\elam x {\tau} e})), 
\rho_2(\gamma_2({\elam x {\tau} e}))) \in 
\Erel {\tarrow {\tau} {\tau'}} \rho \\
\quad \equiv
(\elam x {\rho_1(\tau)} {\rho_1(\gamma_1(e))},
\elam x {\rho_2(\tau)} {\rho_2(\gamma_2(e))}) \in 
\Erel {\tarrow {\tau} {\tau'}} \rho\mbox{.}
\end{stackTL}$
\par
Take $v_1 = {\elam x {\rho_1(\tau)} {\rho_1(\gamma_1(e))}}$. \\
Take $v_2 = {\elam x {\rho_2(\tau)} {\rho_2(\gamma_2(e))}}$. \\
We are required to show that 
\begin{itemize}
\item 
$\stepstar {\elam x {\rho_1(\tau)} {\rho_1(\gamma_1(e))}} 
           {\elam x {\rho_1(\tau)} {\rho_1(\gamma_1(e))}}$, \\
which is immediate since ${\elam x {\rho_1(\tau)}
  {\rho_1(\gamma_1(e))}}$ is a value,
\item 
$\stepstar {\elam x {\rho_2(\tau)} {\rho_2(\gamma_2(e))}} 
           {\elam x {\rho_2(\tau)} {\rho_2(\gamma_2(e))}}$, \\
which is immediate since ${\elam x {\rho_2(\tau)}
  {\rho_2(\gamma_2(e))}}$ is a value, and 
\item 
$(\elam x {\rho_1(\tau)} {\rho_1(\gamma_1(e))}, 
 \elam x {\rho_2(\tau)} {\rho_2(\gamma_2(e))}) \in \Vrel  
        {\tarrow {\tau} {\tau'}} \rho$, \\
which we conclude as follows:\\
\par
Consider arbitrary $v_1$ and $v_2$ such that 
\begin{itemize}
\item 
$(v_1,v_2) \in \Vrel \tau \rho$.
\end{itemize} 
We are required to show 
$(\rho_1(\gamma_1(e))[v_1/x],
  \rho_2(\gamma_2(e))[v_2/x]) \in \Erel {\tau'} \rho$. 
\par
Applying the induction hypothesis to 
$\Delta;\Gamma,x:\tau \vdash e : \tau'$, we have that
$\lrjudg \Delta {\Gamma,x:\tau} e e {\tau'}$. 
\par
Instantiate the latter with $\rho$ and $\gamma[x \mapsto (v_1,v_2)]$.
Note that 
\begin{itemize}
\item $\rho \in \Drel \Delta$, \\
which follows from above, and 
\item 
$\gamma[x \mapsto (v_1,v_2)] \in \Grel {\Gamma,x:\tau} \rho$, \\
which follows from 
\begin{itemize}
\item 
$\gamma \in \Grel \Gamma \rho$ (follows from above), and 
\item 
$(v_1,v_2) \in \Vrel {\tau} \rho$ (follows from above). 
\end{itemize}
\end{itemize}
Hence, $(\rho_1((\gamma[x \mapsto (v_1,v_2)])_1(e)), 
\rho_2((\gamma[x \mapsto (v_1,v_2)])_2(e))) \in \Erel {\tau'} \rho$.
\par
Thus, $(\rho_1(\gamma_1(e))[v_1/x], \rho_2(\gamma_2(e))[v_2/x]) \in 
\Erel {\tau'} \rho$, as we were required to show.
\end{itemize}

\end{pf}

\case
\namedrule 
{\Delta;\Gamma \vdash e_1 : {\tarrow {\tau_1} {\tau_2}} \qquad 
 \Delta;\Gamma \vdash e_2 : \tau_1}
{\Delta;\Gamma \vdash {\eapp {e_1} {e_2}} : \tau_2}
{App} 

\begin{pf}
We are required to show that 
$\lrjudg \Delta \Gamma 
{\eapp {e_1} {e_2}} {\eapp {e_1} {e_2}} {\tau_2}$. 
\par
Consider arbitrary $\rho$ and $\gamma$ such that 
\begin{itemize}
\item $\rho \in \Drel \Delta$, and
\item $\gamma \in \Grel \Gamma \rho$. 
\end{itemize}
We are required to show that 
$\begin{stackTL}
(\rho_1(\gamma_1({\eapp {e_1} {e_2}})),
 \rho_2(\gamma_1({\eapp {e_1} {e_2}}))) \in \Erel {\tau_2} \rho \\
\quad \equiv
(\eapp {\rho_1(\gamma_1(e_1))} {\rho_1(\gamma_1(e_2))},
 \eapp {\rho_2(\gamma_2(e_1))} {\rho_2(\gamma_2(e_2))}) 
\in \Erel {\tau_2} \rho\mbox{.}
\end{stackTL}$
\par
Applying the induction hypothesis to $\Delta;\Gamma \vdash e_1 :
{\tarrow {\tau_1} {\tau_2}}$, we have that 
$\lrjudg \Delta \Gamma {e_1} {e_1} {\tarrow {\tau_1} {\tau_2}}$.  
\par
Instantiate the latter with $\rho$ and $\gamma$.  Note
that 
\begin{itemize}
\item $\rho \in \Drel \Delta$, and 
\item $\gamma \in \Grel \Gamma \rho$. 
\end{itemize}
Hence, $(\rho_1(\gamma_1(e_1)), \rho_2(\gamma_2(e_1))) \in 
\Erel {\tarrow {\tau_1} {\tau_2}} \rho$. 
\par
From the latter, there exist $v_1$ and $v_2$ such that 
\begin{itemize}
\item $\stepstar {\rho_1(\gamma_1(e_1))} {v_{11}}$, 
\item $\stepstar {\rho_2(\gamma_2(e_1))} {v_{12}}$, and
\item $(v_{11},v_{12}) \in \Vrel {\tarrow {\tau_1}{\tau_2}} \rho$. 
\end{itemize}
From the latter it follows that $v_{1}1 = \elam x {\rho_1(\tau_1)} {e_{11}}$ and 
$v_{12} = \elam x {\rho_2(\tau_1)} {e_{12}}$. \\
\par
Applying the induction hypothesis to $\Delta;\Gamma \vdash e_2 :
{\tau_1}$, we have that 
$\lrjudg \Delta \Gamma {e_2} {e_2} {\tau_1}$.  
\par
Instantiate the latter with $\rho$ and $\gamma$.  Note
that 
\begin{itemize}
\item $\rho \in \Drel \Delta$, and 
\item $\gamma \in \Grel \Gamma \rho$. 
\end{itemize}
Hence, $(\rho_1(\gamma_1(e_2)),\rho_2(\gamma_2(e_2))) \in \Erel {\tau_1} \rho$. 
\par
From the latter, there exist $v_{21}$ and $v_{22}$ such that 
\begin{itemize}
\item $\stepstar {\rho_1(\gamma_1(e_2))} {v_{21}}$, 
\item $\stepstar {\rho_2(\gamma_2(e_2))} {v_{22}}$, and
\item $(v_{21},v_{22}) \in \Vrel {\tau_1} \rho$. 
\end{itemize}
Instantiate $(\elam x {\rho_1(\tau_1)} {e_{11}}, \elam x
  {\rho_2(\tau_1)} {e_{12}}) \in \Vrel {\tarrow {\tau_1}{\tau_2}} \rho$ (from
above) with $v_{21}$ and $v_{22}$. Note that
\begin{itemize}
\item $(v_{21},v_{22}) \in \Vrel {\tau_1} \rho$. 
\end{itemize}
Hence, $(e_{11}[v_{21}/x], e_{12}[v_{22}/x]) \in \Erel {\tau_2} \rho$. 
\par
From the latter, there exist $v_1$ and $v_2$ such that 
\begin{itemize}
\item $\stepstar {e_{11}[v_{21}/x]} v_1$, 
\item $\stepstar {e_{12}[v_{22}/x]} v_2$, and
\item $(v_1,v_2) \in \Vrel {\tau_2} \rho$. 
\end{itemize}
\par
Thus, there exist $v_1$ and $v_2$ such that
\begin{itemize}
\item $\stepstar {\eapp {\rho_1(\gamma_1(e_1))} {\rho_1(\gamma_1(e_2))}}
  v_1$ (from above by operational semantics), 
\item $\stepstar {\eapp {\rho_2(\gamma_2(e_1))} {\rho_2(\gamma_2(e_2))}}
  v_2$ (from above by operational semantics), and 
\item $(v_1,v_2) \in \Vrel {\tau_2} \rho$, which follows from above.
\end{itemize}
\end{pf}
\end{itemize}

\newpage
\begin{exercise} (25 pts)
Do the \rulename{Var}, \rulename{Tabs}, and \rulename{Tapp} cases of the proof:

\renewcommand{\labelenumi}{(\alph{enumi})}
\begin{enumerate}

\case
\namedrule 
{ x:\tau \in \Gamma}
{\Delta;\Gamma \vdash {x} : {\tau}}
{Var}

\case
\namedrule 
{\Delta,\alpha;\Gamma \vdash e : \tau}
{\Delta;\Gamma \vdash {\eLam \alpha e} : {\tforall \alpha \tau}}
{Tabs}

\case
\namedrule 
{\Delta;\Gamma \vdash e : {\tforall \alpha {\tau}} \qquad 
 \Delta \vdash \sigma}
{\Delta;\Gamma \vdash {\eApp e {\sigma}} : \tau[\sigma/\alpha]}
{Tapp}

\end{enumerate}

\noindent You may make use of the following lemmas: 
\begin{lem}[Compositionality, or ``syntactic substitution equals
  semantic substitution''] \hfill\\
If $\rho \in \Drel \Delta$ and $\Delta \vdash \tau'$ and $R = \{~(v_1',v_2')
  \mid~ (v_1',v_2') \in \Vrel {\tau'} \rho~\}$, \\
then~ $(v_1,v_2) \in \Vrel {\tau[\tau'/\alpha]} \rho$ ~~\mbox{iff}~~ 
$(v_1,v_2) \in \Vrel {\tau} 
     \rho[\alpha \mapsto (\rho_1(\tau'),\rho_2(\tau'),R)]$.
\end{lem}

\begin{lem}[Well-typed inhabitants]
If $(v_1,v_2) \in \Vrel \tau \rho$, 
then $\vdash v_1 : \rho_1(\tau)$ and $\vdash v_2 : \rho_2(\tau)$. 
\end{lem}

\end{exercise}

\vspace{2ex}

\hrule

\vspace{2ex}

Let us add product types, sum types, existential types, and record types to
System F.

\[
\begin{array}{lcl}
  \tau & \bnfdef & \ldots \sep \tprod {\tau_1}{\tau_2} \sep
\tsum {\tau_1}{\tau_2} \sep
\texist \alpha \tau \sep 
\{\ell_1 : \tau_1, \ldots, \ell_n : \tau_n\}\\
e & \bnfdef & \ldots \sep 
\epair {e_1} {e_2} \sep \efst e \sep \esnd e \sep \\ 
& & \einl {e} \sep \einr{e} \sep \ecase{e}{x}{e_1}{y}{e_2} \sep \\ 
& &  \epack {\tau'} e \alpha \tau \sep \eunpack {e_1} \alpha x {e_2} \sep \\ 
& & \{\ell_1 = e_1, \ldots, \ell_n = e_n\} \sep \eproj e \ell\\
v & \bnfdef & \ldots \sep
\epair {v_1} {v_2} \sep \einl{v} \sep \einr{v} \sep \epack {\tau'} v \alpha \tau \sep 
\{\ell_1 = v_1, \ldots, \ell_n = v_n\} \\
E & \bnfdef & \ldots \sep
\epair {E} {e_2} \sep \epair {v_1} {E} \sep \efst E \sep \esnd E \sep
\einl {E} \sep \einr {E} \sep \ecase{E}{x}{e_1}{y}{e_2} \sep \\ 
& & \epack {\tau'} E \alpha \tau \sep \eunpack {E} \alpha x {e_2} ~\mid
\{\ell_1 = v_1, \ldots, \ell_i = E, \ldots \ell_n = e_n\} \sep   \eproj E \ell
\end{array}
\]

\fbox{$\step e e'$} 
\[
\begin{array}{lcl}
\efst \epair {v_1} {v_2} & \step{}{} & v_1 \\
\esnd \epair {v_1} {v_2} & \step{}{} & v_2 \\
\ecase{\einl{v}}{x}{e_1}{y}{e_2} & \step{}{} & e_1[v/x] \\
\ecase{\einr{v}}{x}{e_1}{y}{e_2} & \step{}{} & e_2[v/y] \\
\eunpack {(\epack {\tau'} v \alpha \tau)} \alpha x {e_2} & \step{}{} & 
e_2[\tau'/\alpha][v/x]\\
\eproj {(\{\ell_1 = v_1, \ldots, \ell_i = v_i, \ldots \ell_n = v_n\})}
  \ell_i & \step{}{} & v_i
\end{array}
\]

\newpage
\fbox{$\Delta;\Gamma \vdash e : \tau$}

\begin{mathpar}
\namedrule 
{\Delta;\Gamma \vdash e_1 : \tau_1 \qquad 
 \Delta;\Gamma \vdash e_2 : \tau_2}  
{\Delta;\Gamma \vdash {\epair {e_1} {e_2}} : \tprod {\tau_1} {\tau_2}}
{Pair}
\and
\namedrule 
{\Delta;\Gamma \vdash e : \tprod {\tau_1} {\tau_2}}
{\Delta;\Gamma \vdash {\efst e} : \tau_1}
{Fst}
\and
\namedrule 
{\Delta;\Gamma \vdash e : \tprod {\tau_1} {\tau_2}}
{\Delta;\Gamma \vdash {\esnd e} : \tau_2}
{Snd}
\\
\namedrule 
{\Delta;\Gamma \vdash e : \tau_1}
{\Delta;\Gamma \vdash {\einl e} : \tsum {\tau_1} {\tau_2}}
{Inl}
\and
\namedrule 
{\Delta;\Gamma \vdash e : \tau_2}
{\Delta;\Gamma \vdash {\einr e} : \tsum {\tau_1} {\tau_2}}
{Inl}
\and
\namedrule 
{\Delta;\Gamma \vdash e : \tsum {\tau_1} {\tau_2} \qquad 
 \Delta;\Gamma,x:\tau_1 \vdash e_1 : \tau \qquad 
 \Delta;\Gamma,y:\tau_2 \vdash e_2 : \tau}
{\Delta;\Gamma \vdash {\ecase e x {e_1} y {e_2}} : \tau}
{Case}
\\
\namedrule 
{\Delta \vdash \sigma \qquad 
 \Delta;\Gamma \vdash e : \tau[\sigma/\alpha]}
{\Delta;\Gamma \vdash {\epack \sigma e \alpha \tau} :
  \texist \alpha \tau}
{Pack}
\and
\namedrule 
{\Delta;\Gamma \vdash e_1 : {\texist \alpha \tau} \qquad
 \Delta \vdash \tau_2 \qquad
 \Delta,\alpha;\Gamma,x:\tau \vdash e_2 : \tau_2}
{\Delta;\Gamma \vdash {\eunpack {e_1} \alpha x {e_2}} : \tau_2}
{Unpack}
\\
\namedrule 
{\Delta;\Gamma \vdash e_i : \tau_i \quad \mathrm{forall~}i\in\{1,\ldots,n\}}  
{\Delta;\Gamma \vdash {\{\ell_1 = e_1, \ldots, \ell_n = e_n\}} : \{\ell_1 : \tau_1, \ldots, \ell_n : \tau_n\}}
{Record}
\and
\namedrule 
{\Delta;\Gamma \vdash e : \{\ell_1 : \tau_1, \ldots, \ell_i : \tau_i,
  \ldots, \ell_n : \tau_n\}}
{\Delta;\Gamma \vdash {\eproj e \ell_i} : \tau_i}
{Proj}

\end{mathpar}

\vspace{4ex}

The logical relation for the extended language is exactly as above,
except that we must also define when values of product, sum, existential,
and record types are related.  

The value relation for products and existential types is defined as follows.
\[
\begin{array}{lcl}
\Vrel {\tprod {\tau_1}{\tau_2}} \rho & = & 
\{\,(\epair {v_1} {v_2}, \epair {v_1'} {v_2'}) \in 
    \Atom {\tprod{\tau_1}{\tau_2}} \rho \mid~
(v_1,v_1') \in \Vrel {\tau_1} \rho ~\land~
(v_2,v_2') \in \Vrel {\tau_2} \rho \,\}\\[4pt]

\Vrel {\texist \alpha \tau} \rho & = & 
\{\, (\epack {\tau_1} {v_1} \alpha {\rho_1(\tau)}, 
  \epack {\tau_2} {v_2} \alpha {\rho_2(\tau)}) \in 
\Atom {\texist \alpha \tau} \rho \mid~ \\
& & \qquad
\exists R.~ R \in \Rel{\tau_1}{\tau_2} ~\land~
(v_1,v_2) \in \Vrel \tau {\rho[\alpha
  \mapsto (\tau_1,\tau_2,R)]} \,\}
\end{array}
\]

\begin{exercise} (10 pts)
Define the value relation for sum types and record types. 
\end{exercise}

\begin{exercise} (30 pts)
Complete the following cases of the parametricity proof: 

\renewcommand{\labelenumi}{(\alph{enumi})}
\begin{enumerate}

\case
\namedrule 
{\Delta;\Gamma \vdash e : \tau_1}
{\Delta;\Gamma \vdash {\einl e} : \tsum {\tau_1} {\tau_2}}
{Inl}

\case
\namedrule 
{\Delta;\Gamma \vdash e : \tsum {\tau_1} {\tau_2} \qquad 
 \Delta;\Gamma,x:\tau_1 \vdash e_1 : \tau \qquad 
 \Delta;\Gamma,y:\tau_2 \vdash e_2 : \tau}
{\Delta;\Gamma \vdash {\ecase e x {e_1} y {e_2}} : \tau}
{Case}

\case
\namedrule 
{\Delta;\Gamma \vdash e_1 : {\texist \alpha.\tau} \qquad
 \Delta \vdash \tau_2 
 \Delta,\alpha;\Gamma,x:\tau \vdash e_2 : \tau_2}
{\Delta;\Gamma \vdash {\eunpack {e_1} \alpha x {e_2}} : \tau_2}
{Unpack}

\end{enumerate}
\end{exercise}

\vspace{2ex}

\hrule
\hrule

\vspace{2ex}

\section*{Free Theorems}

\begin{exercise} (15 pts)
Let $\vdash e : \tforall \alpha
{\tarrow{\tarrow{\alpha}{\alpha}}{\alpha}}$, ~$\vdash \tau$, ~$\vdash
v_1 : \tau$,  and $\vdash v_2 : \tau$. \\
Prove the following:~~ 
If  $\stepstar {\eapp {\eapp {\eApp e \tau} {v_1}} {v_2}} v$ then
either $v = v_1$ or $v = v_2$. 
\end{exercise}

\begin{exercise} (30 pts)
Let $\vdash e : \tforall \alpha {\tarrow{\alpha}{\alpha}}$, ~$\vdash f
: \tarrow{\tau}{\tau'}$, and $\vdash v : \tau$. \\
Prove the following:~~ 
$\lrjudg \cdot \cdot 
      {\eapp f {(\eapp {\eApp e \tau} v})} 
      {\eapp {\eApp e {\tau'}} {(\eapp f v)}} 
      {\tau'}$.
\end{exercise}

\vspace{2ex}

\hrule
\hrule

\vspace{2ex}

\section*{Representation Independence}
Parametricity is the essence of \emph{representation independence} for
abstract data types. 

A relation $R \in \Rel {\tau_1}{\tau_2}$ captures the invariant
relationship between two packages of type $\texist \alpha \tau$:
$\epack {\tau_1} {v_1} \alpha \tau$ and  $\epack {\tau_2} {v_2} \alpha
\tau$ if and only if: 
\[ 
(v_1,v_2) \in \Vrel \tau {\emptyset[\alpha \mapsto (\tau_1,\tau_2,R)]}
\]

When two packages $e_1$ and $e_2$ are logically related, they are
indistinguishable by any client --- that is, the execution of
a client $e_c$ cannot depend on whether $e_1$ or $e_2$ is the provided 
package.  
In other words, if $\alpha;x:\tau \vdash e_c : \tau_c$, then
\[
\lrjudg \cdot \cdot {\eunpack {e_1} \alpha x {e_c}} 
       {\eunpack {e_2} \alpha x {e_c}} {\tau_c} 
\]

For the exercises that follow, we must define when two values of
type $\tint$ are related. We use the metavariable $n$ for integers
and extend the logical relation to integer types as follows:
\[
\Vrel{\tint}\rho ~~=~~ \{\, (n,n) \in \Atom \tint \rho \,\}
\]


\vspace{2ex}

\begin{exercise} (15 pts)
For each of the following pairs of packages $e_1$ and $e_2$, say
whether they are logically related (contextually equivalent).  If so,
provide a proof.  If not, provide a program context that can tell them
apart.  (You may assume that the logical relation defined earlier in
this document is sound with respect to contextual equivalence.)

\renewcommand{\labelenumi}{(\alph{enumi})}
\begin{enumerate}
\item 
\[
\begin{array}{lcl}
\tau & = & \texist \alpha {\tprod \alpha {(\tarrow \alpha \tbool)}}\\
e_1 & = & 
\lit{pack}\:\langle \tint, {\epair {0} {\elam x \tint {(\lit{eq?}\:x\:1)}}} \rangle~
\lit{as}\:\tau \\
e_2 & = & 
\lit{pack}\:\langle \tbool, {\epair {\etrue} {\elam x \tbool
          {\lit{not}\:x}}} \rangle~\lit{as}\:\tau 
\end{array}
\]


\item 
\[
\begin{array}{lcl}
\tau & = & \texist \alpha {\tprod \alpha {(\tarrow \alpha \tint)}}\\
e_1 & = & 
\lit{pack}\:\langle \tstr, {\epair {\lit{``hello''}} {\elam x \tstr {(\lit{string-length}\:x)}}} \rangle~
\lit{as}\:\tau \\
e_2 & = & 
\lit{pack}\:\langle \tint, {\epair {4} {\elam x \tint x}} \rangle~\lit{as}\:\tau 
\end{array}
\]

\end{enumerate}
\end{exercise}
\vspace{1ex}

\newpage
\begin{exercise} (15 pts)
Show that the following two implementations of counters are
logically related.

\[
\begin{array}{lcl}
\lit{Counter} & = & \texist \alpha \{\begin{stackTL}
\lit{new} : \alpha, \\
\lit{inc} : \tarrow \alpha \alpha, \\
\lit{get} : \tarrow \alpha \tint \}
\end{stackTL} \\
& & \\
& & \\
\lit{c1} & = & \{\begin{stackTL}
\lit{new} = 0, \\
\lit{inc} = \lambda x:\tint.~x+1, \\
\lit{get} = \lambda x:\tint.~x\}
\end{stackTL} \\
& & \\
\lit{cntr1} & = & 
\lit{pack}\:\begin{stackTL}\langle \tint, \lit{c1} \rangle~ 
\lit{as}\: \lit{Counter}
\end{stackTL} \\ 
& & \\
& & \\
\lit{c2} & = & \{\begin{stackTL}
\lit{new} = 0, \\
\lit{inc} = \lambda x:\tint.~x-1, \\
\lit{get} = \lambda x:\tint.~0-x\} 
\end{stackTL} \\
& & \\
\lit{cntr2} & = & 
\lit{pack}\:\begin{stackTL}\langle \tint, \lit{c2} \rangle~
\lit{as}\: {\lit{Counter}}
\end{stackTL} 

\end{array}
\]

\vspace{1ex}

Prove the following:~~
$\lrjudg \cdot \cdot {\lit{cntr1}} {\lit{cntr2}} {\lit{Counter}}$.

\vspace{1ex}

You will need to use the value relation for record types from Exercise 2.
\end{exercise}



\end{document}

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% End: 
