\documentclass[11pt]{article}

\input{hw2-macros.tex}
\toggletrue{sol}

\title{15-814 Homework 2}
\author{YOUR NAME HERE}

\begin{document}
\maketitle

\begin{task}[10 points, WB]
  Give rules capturing the intended dynamics for lazy products.
  That is, give rules defining the judgments $\val{e}$ and $e \step e'$ for terms $e$ of the form $\lpair{e_1}{e_2}$, $\outl{e_0}$, and $\outr{e_0}$.
  They should satisfy $\outl{\lpair{e_1}{e_2}} \steps e_1$ and $\outr{\lpair{e_1}{e_2}} \steps e_2$ and capture the intuitions we gave above.
  \label{task:proddyn}
\end{task}
\begin{solution}
\end{solution}

\begin{task}[10 points, WB]
  State and prove the canonical forms theorem for lazy pairs.
  The statement should be of the following form: ``if $\cdot \vdash e : \Tlprod{\tau_1}{\tau_2}$ and $\val{e}$, then $e$ is a term of the form(s) \ldots''.
  \label{task:cft}
\end{task}
\begin{solution}
\end{solution}

\begin{task}[10 points, WB]
  Show that the progress property still holds after the addition of lazy pairs.
  In particular, prove if $\cdot \vdash e : \tau$ by \rn{I-$\Tlprod{}{}$}, \rn{E-$\Tlprod{}{}$-L}, or \rn{E-$\Tlprod{}{}$-R}, then either $\val{e}$ or there exists an $e'$ such that $e \step e'$.
\end{task}
\begin{solution}
\end{solution}

\begin{task}[5 points, WB]
  Show that the preservation property still holds after the addition of lazy pairs.
  In particular, show that if $e \step e'$ by one of the rules you gave in task~\ref{task:proddyn} and $\cdot \vdash e : \tau$, then $\cdot \vdash e' : \tau$.
\end{task}
\begin{solution}
\end{solution}

\begin{task}[5 points, WB]
  Write down terms $\xi$ and $\xi^{-1}$ such that
  \begin{align*}
    \cdot &\vdash \xi : \Teprod{\tau}{\sigma} \to \Tlprod{\tau}{\sigma}\\
    \cdot &\vdash \xi^{-1} : \Tlprod{\tau}{\sigma} \to \Teprod{\tau}{\sigma}
  \end{align*}
  and for all values $v$ of type $\Teprod{\tau}{\sigma}$, $\xi^{-1}(\xi(v)) \steps v$ (you do not need to prove this).
  Do these terms witness the isomorphism?
  If so, state this.
  If not, briefly explain why.
\end{task}
\begin{solution}
\end{solution}

\begin{task}[10 points, WB, {\cite[Ex.~11.2]{pfpl}}]
  Informally show that we cannot identify $\Teprod{\Tbool}{\tau}$ and $\Topt{\tau}$ for arbitrary $\tau$.
  Do so by attempting to give an implementation of $\Topt{\tau}$ in terms of $\Teprod{\Tbool}{\tau}$ by sensibly completing the following chart:
  \begin{align*}
    \onone &= {?}\\
    \osome e &= {?}\\
    \ocase{e_1}{x}{e_2}{e_3} &= {?}
  \end{align*}
  Where do you get stuck if you do not additionally assume the existence of a ``null'' value $\tnull_\tau$ for each type $\tau$?
  Argue that even by artificially assuming the existence of such values, you cannot complete the chart in a manner that captures the semantics of $\Topt{\tau}$ in their absence.
  (Please be brief: your answer must contain at most eight lines of prose.)
\end{task}
\begin{solution}
\end{solution}

\begin{task}[5 points, WB]
  Find terms $e$ and $e'$ and types $\tau$ and $\tau'$ such that $\cdot \vdash e : \tau$, $e \step e'$, and $\cdot \vdash e' : \tau'$, but not $\cdot \vdash e: \tau'$.
\end{task}
\begin{solution}
\end{solution}

\begin{task}[5 points, WB]
  Is the nullary sum type $\Tzero$ is the zero for $\Teprod{}{}$?
  That is, prove or disprove that for all $\tau$,
  \[
    \Teprod{\tau}{\Tvoid} \cong \Tvoid.
  \]
  In the affirmative case, we only ask that you write down the two terms witnessing the isomorphism.
  In the negative case, you must provide a counter-example and explain why it is a counter-example.
\end{task}
\begin{solution}
\end{solution}

\begin{task}[0 points]
  How long did you spend on this assignment?
  Please list the questions that you discussed with classmates using the whiteboard policy.
\end{task}
\begin{solution}
\end{solution}

\iftoggle{sol}{%
  % Don't show the appendices if we are presenting solutions.
}{
  % We have provided you with the macros for your reference.
  % Please don't include these pages in your submission PDF!
\appendix
\clearpage
\section{Syntax}
\label{sec:syntax}
Types $\tau$ and terms $e$ are given by the grammars:
\begin{align*}
  \tau &\Coloneqq \alpha \mid \tau_1 \to \tau_2 \mid \Tvoid \mid \tau_1 + \tau_2 \mid \Tunit \mid \Teprod{\tau_1}{\tau_2} \mid \Tlprod{\tau_1}{\tau_2}\\
  e &\Coloneqq x\\
       &\mid \lambda x.e \mid e_1e_2\\
       &\mid \zcase{e}\\
       &\mid \inl{e} \mid \inr{e} \mid \bscase{e}{x}{e_x}{y}{y_}\\
       &\mid \eunit \mid \epcase{e_1}{\eunit}{e_2}\\
       &\mid \epair{e_1}{e_2} \mid \epcase{e_1}{\epair{x_1}{x_2}}{e_2}\\
       &\mid \lpair{e_1}{e_2} \mid \outl{e} \mid \outr{e}
\end{align*}

\section{Statics}
\label{sec:statics}

\[
  \infer[\rn{Var}]{
    \Gamma \vdash x : \tau
  }{
    x : \tau \in \Gamma
  }
  \qquad
  \infer[\rn{Lam}]{
    \Gamma \vdash \lambda x.e : \tau \to \tau'
  }{
    \Gamma, x : \tau \vdash e : \tau'
  }
\]
\[
  \infer[\rn{App}]{
    \Gamma \vdash e_1e_2 : \tau'
  }{
    \Gamma \vdash e_1 : \tau \to \tau'
    &
    \Gamma \vdash e_2 : \tau
  }
\]
\[
  \infer[\rn{I-+-L}]{
    \Gamma \vdash \inl e : \tau + \tau'
  }{
    \Gamma \vdash e : \tau
  }
  \qquad
  \infer[\rn{I-+-R}]{
    \Gamma \vdash \inr e : \tau + \tau'
  }{
    \Gamma \vdash e : \tau'
  }
\]
\[
  \infer[\rn{E-+}]{
    \Gamma \vdash \bscase{e}{x}{e_1}{x}{e_2} : \tau
  }{
    \Gamma \vdash e : \tau_1 + \tau_2
    &
    \Gamma, x : \tau_1 \vdash e_1 : \tau
    &
    \Gamma, x : \tau_2 \vdash e_2 : \tau
  }
\]
\[
  \text{No \rn{I-$\Tvoid$} rule.}
  \qquad
  \infer[\rn{E-$\Tvoid$}]{
    \Gamma \vdash \zcase{e} : \tau
  }{
    \Gamma \vdash e : \Tvoid
  }
\]
\[
  \infer[\rn{I-$\Tunit$}]{\Gamma \vdash \eunit : \Tunit}{\mathstrut}
  \qquad
  \infer[\rn{E-$\Tunit$}]{%
    \Gamma\vdash \epcase{e_0}{\eunit}{e_1} : \tau
  }{%
    \Gamma \vdash e_0 : \Tunit
    &
    \Gamma \vdash e_1 : \tau
  }
\]
\[
  \infer[\rn{I-$\Teprod{}{}$}]{%
    \Gamma \vdash \epair{e_1}{e_2} : \Teprod{\tau_1}{\tau_2}
  }{%
    \Gamma \vdash e_1 : \tau_1
    &
    \Gamma \vdash e_2 : \tau_2
  }
\]
\[
  \infer[\rn{E-$\Teprod{}{}$}]{%
    \Gamma \vdash \epcase{e_0}{\epair{x_1}{x_2}}{e_1} : \tau
  }{%
    \Gamma \vdash e_0 : \Teprod{\tau_1}{\tau_2}
    &
    \Gamma, x_1 : \tau_1, x_2 : \tau_2 \vdash e_1 : \tau
  }
\]
\[
  \infer[\rn{I-$\Tlprod{}{}$}]{
    \Gamma \vdash \lpair{e_1}{e_2} : \Tlprod{\tau_1}{\tau_2}
  }{
    \Gamma \vdash e_1 : \tau_1
    &
    \Gamma \vdash e_2 : \tau_2
  }
\]
\[
  \infer[\rn{E-$\Tlprod{}{}$-L}]{
    \Gamma \vdash \outl{e} : \tau_1
  }{
    \Gamma \vdash e : \Tlprod{\tau_1}{\tau_2}
  }
  \qquad
  \infer[\rn{E-$\Tlprod{}{}$-R}]{
    \Gamma \vdash \outr{e} : \tau_2
  }{
    \Gamma \vdash e : \Tlprod{\tau_1}{\tau_2}
  }
\]


\section{Dynamics}
\label{sec:dynamics}

\[
  \infer[\rn{$\to$-Val}]{\val{\lambda x.e}}{\mathstrut}
  \qquad
  \infer[\rn{App-Red}]{(\lambda x.e_1)e_2 \step [e_2/x]e_1}{\mathstrut}
\]
\[
  \infer[\rn{App-Step-L}]{
    e_1e_2 \step e_1'e_2
  }{
    e_1 \step e_1'
  }
  \qquad
  \infer[\rn{App-Step-R}]{
    e_1e_2 \step e_1e_2'
  }{
    \val{e_1}
    &
    e_2 \step e_2'
  }
\]
\[
  \infer[\rn{val/l}]{\val{\inl{e}}}{\val e}
  \qquad
  \infer[\rn{val/r}]{\val{\inr{e}}}{\val e}
\]
\[
  \infer[\rn{$\step$/l}]{
    \inl e \step \inl{e'}
  }{
    e \step e'
  }
  \qquad
  \infer[\rn{$\step$/r}]{
    \inr e \step \inr{e'}
  }{
    e \step e'
  }
\]
\[
  \infer[\rn{$\step/\ccase_0$}]{
    \zcase{e} \step \zcase{e'}
  }{
    e \step e'
  }
\]
\[
  \infer[\rn{$\step/\ccase_1$}]{
    \bscase{e}{x}{e_1}{y}{e_2}
    \step
    \bscase{e'}{x}{e_1}{y}{e_2}
  }{
    e \step e'
  }
\]
\[
  \infer[\rn{$\step/\ccase/l$}]{
    \bscase{\inl{v_1}}{x}{e_1}{y}{e_2}
    \step
    [v_1/x]e_1
  }{
    \val{v_1}
  }
\]
\[
  \infer[\rn{$\step/\ccase/r$}]{
    \bscase{\inr{v_2}}{x}{e_1}{y}{e_2}
    \step
    [v_2/y]e_2
  }{
    \val{v_2}
  }
\]
\[
  \infer[\rn{$\eunit$-val}]{\val\eunit}{}
  \qquad
  \infer[\rn{Pair-val}]{
    \val{\epair{v_1}{v_2}}
  }{
    \val{v_1}
    &
    \val{v_2}
  }
\]
\[
  \infer[\rn{Step-L}]{
    \epair{e_1}{e_2} \step \epair{e_1'}{e_2}
  }{
    e_1 \step e_1'
  }
  \qquad
  \infer[\rn{Step-R}]{
    \epair{v_1}{e_2} \step \epair{v_1}{e_2'}
  }{
    \val{v_1}
    &
    e_2 \step e_2'
  }
\]
\[
  \infer[\rn{Step-Subj-1}]{
    \epcase{e_0}{\eunit}{e_1}
    \step
    \epcase{e_0'}{\eunit}{e_1}
  }{
    e_0 \step e_0'
  }
\]
\[
  \infer[\rn{Step-Subj-2}]{
    \epcase{e_0}{\epair{x_1}{x_2}}{e_1}
    \step
    \epcase{e_0'}{\epair{x_1}{x_2}}{e_1}
  }{
    e_0 \step e_0'
  }
\]
\[
  \infer[\rn{Step-Case-1}]{
    \epcase{\eunit}{\eunit}{e_1}
    \step
    e_1
  }{
    \mathstrut
  }
\]
\[
  \infer[\rn{Step-Case-2}]{
    \epcase{\epair{v_1}{v_2}}{\epair{x_1}{x_2}}{e_1}
    \step
    [v_1,v_2/x_1,x_2]e_1
  }{
    \val{\epair{v_1}{v_2}}
  }
\]
}
\end{document}