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

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

\begin{document}
\maketitle

\section{Termination in System \langt}
\begin{task}
  Prove closure under head expansion.
\end{task}
\begin{sol}
\end{sol}

\begin{task}
  Prove the remaining cases of Theorem B.
\end{task}
\begin{sol}
\end{sol}

\section{Programming with nats and streams}
\[\altrec{\zero}{e_0}{x}{y}{e_1} \step e_0\]
\[\altrec{\suc{e}}{e_0}{x}{y}{e_1} \step
         [\altrec{e}{e_0}{x}{y}{e_1}, e/x, y]e_1\]
\begin{task}~
\begin{enumerate}
\item Define $\mathtt{plus}$, where $\mathtt{plus}\; \natof{m}\; \natof{n}
\step^* \natof{m + n}$.\footnote{We will write $\natof{n}$ to indicate the
representation of a natural number $n$ as an element of type $\tnat$.}
\item Define $\mathtt{minus}$, where $\mathtt{minus}\; \natof{m}\; \natof{n}
\step^* \natof{m - n}$ if $m > n$. It should produce $0$ otherwise.
\item Define $\mathtt{leq}$, where
$\mathtt{leq}\; \natof{m}\; \natof{n} \step^* \inl{\triv}$ if $m \leq n$ and
$\mathtt{leq}\; \natof{m}\; \natof{n} \step^* \inr{\triv}$ otherwise.
\item Define $\mathtt{mod}$, where $\mathtt{mod}\; \natof{m}\; \natof{n} =
\natof{m \mod n}$.
\end{enumerate}
\end{task}
\begin{sol}
\end{sol}

\begin{task}\label{task:str-def}~
\begin{enumerate}
\item Define a function $\mathtt{delay}$, which takes a nat and a nat
  stream and produces a stream whose head is the given nat and whose
  tail is the given stream; if we think of the input stream as a
  signal, this function delays the input signal by one clock tick and
  maintains the delayed value in a buffer.  For example,
  $\mathtt{delay}\; 0\; \mathtt{nats}$ should return the stream $0, 0,
  1, 2, 3, \dots$.
\item Define a function $\mathtt{csum}$, which takes a nat stream and
  produces a nat stream that is the cumulative sum of the input
  stream; $\mathtt{csum}\; \mathtt{nats}$ should be $0, 1, 3, 6,
  \dots$.
\end{enumerate}
\end{task}
\begin{sol}
\end{sol}

\begin{task}\label{task:fun-def}
Rewrite the definitions of Task~\ref{task:str-def}, using
$\tarrow{\tnat}{\tnat}$ functions anywhere nat streams appear
(i.e. the stream arguments should be functions as described above, as
should the result.)
\end{task}
\begin{sol}
\end{sol}

\section{Inductive and Coinductive Types}
\subsection{Generic Programming} \label{sub:generic}
\begin{task}~
\begin{enumerate}
\item Write a function $\mathtt{crecord} :
  \tarrow{\mathtt{record}}{\mathtt{record}}$ which uses a given
  capitalization function $c :
  \tarrow{\mathtt{string}}{\mathtt{string}}$ to capitalize the first,
  middle, and last name entries, {\em without} using the $\fmapop$
  operator. (Assume that $\times$ associates to the right, so $\tau_1
  \times \tau_2 \times \tau_3$ is shorthand for $\tau_1 \times (\tau_2
  \times \tau_3)$.)
\item Now define $\mathtt{crecord}$ as an application of $\fmapop$.
\end{enumerate}
\end{task}
\begin{sol}
\end{sol}

\begin{task} Suppose we add a new rule
\[
\infer
  {t.\tstream{\tau} \pos}
  {t.\tau \pos}
\]
Give a new dynamics rule for $\fmapop$ to cover this case. Prove the
case of preservation corresponding to your dynamics rule.
\end{task}
\begin{sol}
\end{sol}

\subsection{Trees}
\begin{task}~
\begin{enumerate}
\item Define a type $\mathtt{btree}$ of binary trees with values of
  type $\tnat$ at each node (excluding leaves).
\item Define the empty tree $\mathtt{bemp} : \mathtt{btree}$.
\item Define the function $\mathtt{val}:
  \tarrow{\mathtt{btree}}{(\tsum{\tnat}{\tunit})}$, which returns the
  value at the root node or returns $\inr{\triv}$ on the empty tree.
\item Define the function
  $\mathtt{sum}: \tarrow{\mathtt{btree}}{\tnat}$ which returns the sum of all of the
  numbers in a tree. It should return $\zero$ on the empty tree.
\end{enumerate}
\end{task}
\begin{sol}
\end{sol}

\begin{task}~
\begin{enumerate}
\item Define a type $\mathtt{itree}$ of {\em infinite} binary trees with values
  of type $\tnat$ at every node. Every node has two children; there are no
  leaves and there is no empty tree.
\item Define the infinite tree containing zero at every node.
\item Define the function $\mathtt{val} : \mathtt{btree} \to
  \mathtt{nat}$, which returns the value at the root node.
\item Define the function $\mathtt{embed} : \mathtt{btree} \to
  \mathtt{itree}$, which embeds an finite tree in an infinite one by
  extending with zeroes.
\end{enumerate}
\end{task}
\begin{sol}
\end{sol}

\begin{task}
Assume we generalize all of our definitions to handle nested type operators
(e.g. $\tind{t}{\tind{s}{\tau}}$ where $\tau$ may contain both $s$ and $t$.)
This allows us to combine inductive and coinductive types.

\begin{enumerate}
\item Define a type of nat-valued tree with finite depth but infinite branching
  factor. A tree of this type is either empty or is a node with a value of type
  $\tnat$ and a stream of children.
\item Define the dual of the above type: a nat-valued tree with (potentially)
  infinite depth and finite but variable branching factor. A tree of this type
  is a node with a value of type $\tnat$ and a {\em finite list} of children.
\end{enumerate}
In both cases, you need only define the type. You do not need to define any
operations over it.
\end{task}
\begin{sol}
\end{sol}

\section{\systemf}
\subsection{Church Encoding}
\begin{task}
  Using the definitions above, we are able to define a
  \systemf\ equivalent for any type operator $t.\tau \pos$ as defined
  in Section \ref{sub:generic}. Since we have also translated the
  constructors and recursors for these types, we can define $\fmapop$
  as well. Use these to define, for any $t.\tau \pos$, the
  \systemf\ equivalent of $\tind{t}{\tau}$ along with $\foldop$ and
  $\grecop$. (You may want to try encoding some specific cases first
  to develop some intuition.)
\end{task}
\begin{sol}
\end{sol}

\begin{task}
  Define $\tcoi{t}{\tau}$ for any $t.\tau \pos$, along with $\genop$
  and $\unfoldop$.
  \begin{hint}
    Recall that coinductive types use hidden internal state in order
    to respond to $\unfoldop$. As such, you may find it useful to use
    existential types, which are definable in \systemf\ per PFPL 17.3.
  \end{hint}
\end{task}
\begin{sol}
\end{sol}

\section{System \langt} \label{app:system-t}
\subsection{Statics}
\subsection{Dynamics}
\section{Streams} \label{app:streams}
\subsection{Statics}
\subsection{Dynamics}
\end{document}