\documentclass{article}
\usepackage[left=3cm,top=3cm,right=4cm,nohead,bottom=3cm]{geometry}
\input{hwhw3-macros.tex}

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

\begin{document}
\maketitle

\section{Parametricity}
\begin{task}
  Show that for any expression $e$ of type
  $\tforall{t}{\tarrow{t}{t}}$ and any expression $e_0 : \rho$, the
  terms $e[\rho](e_0) : \rho$ and $e_0 : \rho$ are observationally
  equivalent (that is, $e[\rho](e_0) \cong e_0$). (Hint: You know that
  $e \sim_{\tforall{t}{\tarrow{t}{t}}} e$. Also, observe that the
  relation $S : \rho \leftrightarrow \rho$ defined by
  \begin{quote}
    $S(e_1,e_2)$ holds iff $e_1 \cong e_2 \cong e_0$
  \end{quote}
  is admissible.)
\end{task}
\begin{sol}
\end{sol}

\begin{task}
  Using the previous task, show that any expression $e$ of type
  $\tforall{t}{\tarrow{t}{t}}$ is equivalent to the identity function.
  (Hint: Unroll the definition of $\tforall{t}{\tarrow{t}{t}}$ and use
  the properties of admissible relations.)
\end{task}
\begin{sol}
\end{sol}

\begin{task}
  Show that for any expression $e$ of type
  $\tforall{t}{\tarrow{\tarrow{t}{t}}{t}}$ and any $e_1 : \rho$ and $e_2 : \rho$,
  either $e[\rho](e_1)(e_2) \cong e_1$ or $e[\rho](e_1)(e_2) \cong e_2$.

\end{task}
\begin{sol}
\end{sol}

\section{Defining Inductive and Coinductive Types}
\subsection{Inductive Types}
\begin{task}\taskpts{5}
Encode the {\langic} expression $\grec{t. \tau'}{x}{e_1}{e_2}$ in the extension
of {\langfpc} described above. For convenience, abbreviate $\tau \eqdef
\trec{t}{\tau'}$.

\begin{hint}You will want to use generic programming. It may help to think
about how to do this for $\tnat$ and then generalize.\end{hint}
\end{task}
\begin{sol}
\end{sol}

\subsection{Laziness and Streams}
\begin{task}\taskpts{3}
We could add laziness to {\langfpc} in other ways. Consider taking the eager
variant and replacing the value rule for pairs with this rule:

\[ \infer{\eprod{e_1}{e_2} \val}{} \]

In this setting, $\foldop$ isn't lazy, but pairs are; all pairs are values.
\begin{enumerate}
\item Does the above encoding of streams work with this dynamics?
If so, give an example of a canonical form of stream type. If not, explain why.
\item Same question, but imagine pairing is only lazy in the second component,
i.e.
\[ \infer{\eprod{v_1}{e_2} \val}{v_1 \val} \]
\item Same question, where pairing is only lazy in the first component.
\end{enumerate}
\end{task}
\begin{sol}
\end{sol}

\begin{task}\taskpts{3}
Consider the type $\trec{t}{\tsum{\tunit}{t}}$, which would be our encoding of
$\tnat$ in {\langfpc}. Prove the following statement or give a counterexample.

\begin{prop}
In {\em the lazy variant} of {\langfpc} (the version where $\foldop$ is lazy),
every closed value of type $\trec{t}{\tsum{\tunit}{t}}$ is extensionally
equivalent to (a suitable encoding of) $\natof{n}$ for some $n$.
\end{prop}

You can use the constructors $\z$ and $\sucop$ without defining their encodings.
Your reasoning can be fairly informal, but should be convincing.
\end{task}
\begin{sol}
\end{sol}

\subsection{Simulating Laziness}
\begin{task}\taskpts{5}
Lest you forget how to do a type safety proof, show that progress and
preservation remain true with the addition of suspensions. You need
only consider the cases related to suspensions, i.e. the new rules
above. There will be three cases for progress and four for
preservation. You may assume that appropriately extended canonical
forms and substitution lemmas hold.
\end{task}
\begin{sol}
\end{sol}

\begin{task}\taskpts{4}
Define an alternate encoding, $\tstream_2$ (different from $\tstream_1$ above),
of the stream type in the eager variant of {\langfpc}.  As in the above
encoding, this will require using suspensions, but in a different part of the
type. Define the operations $\hdop$, $\tlop$ and $\strgenop$ on your type.

\begin{hint}Adding laziness where we did corresponds to when we added lazy pairs
(but not folds) to the language. What about the version where $\foldop$ is lazy?
\end{hint}
\end{task}
\begin{sol}
\end{sol}

\begin{task}\taskpts{5}
\begin{enumerate}
\item Define an encoding of the coinductive type $\tcoi{t}{\tau'}$ in
{\langfpc}. You will need to use suspended computations.
\item Define $\unfold{t. \tau'}{e}$ for your encoding of coinductive types.
\item Fill in the dots in the below implementation of $\genop$.

\[\gen{t. \tau'}{x}{e_1}{e_2} \eqdef
  \ap{(\efix{\tparr{\rho}{\tau}}{f}
    {\lam{y}{\rho}{\foldnp
      {\dots
        \fmap{t. \tau'}{x.fx;\dots} \dots }}})}{e_2}\]
\end{enumerate}
\end{task}
\begin{sol}
\end{sol}

\section{Halting Problem in PCF}
Consider a term $H : \tparr{(\tparr{\tnat}{\tnat})}{\tnat}$ with the
following properties:
\begin{enumerate}
\item For all $f : \tparr{\tnat}{\tnat}$, either
  $\ap{H}{f} \step^* \z$ or $\ap{H}{f} \step^* \suc{\z}$
  (i.e. $H$ always terminates and evaluates to either $\natof{0}$ or
  $\natof{1}$.)
\item $\ap{H}{f} \step^* \z$ iff there exists $n$ such that
  $\ap{f}{\z} \step^* \natof{n}$ (i.e. $\ap{f}{\z}$ converges to a value.)
\item $\ap{H}{f} \step^* \suc{\z}$ iff $\ap{f}{\z}$ diverges.
\end{enumerate}
\begin{task}\taskpts{5}
Prove that $H$ is not definable in {\langpcf}.

\begin{hint}
Suppose $H$ exists. Define a term $D$ (which may refer to $H$)
such that $D$ diverges iff
$\ap{H}{D} \step^* \z$ (to make a term diverge, you can easily write an
infinite loop.) Then consider to what $\ap{H}{D}$ should evaluate.
\end{hint}
\end{task}
\begin{sol}
\end{sol}

\begin{bonus}
Prove that this more general $H$ is not definable in {\langpcf}.
\end{bonus}
\begin{sol}
\end{sol}

\end{document}