\documentclass[11pt]{article}

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

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

\begin{document}
\maketitle

\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}

\begin{task}[5 points]
  Please take a few minutes to complete the Faculty Course Evaluation and TA evaluations!
  You can complete the FCEs at \url{https://www.cmu.edu/hub/fce/}.  We are particularly
  interested in suggestions for improving the course for future iterations.
  You can complete the two parts of the TA evaluation here:
  \begin{itemize}
  \item \url{https://www.ugrad.cs.cmu.edu/ta/F18/feedback/}
  \item \url{https://www.ugrad.cs.cmu.edu/ta/F18/freeform/}
  \end{itemize}
  Once you have completed all three evaluations, you can claim your five points by affirming that you have done so.
\end{task}
\begin{solution}
\end{solution}

  \begin{align}
  &\stevnc{(e_1\,e_2)}{d} \;\;\step\;\; \stevnc{e_1}{d_1}, \stcont{d_1}{(\uscore\,e_2)}{d}\qquad(d_1\text{ fresh})\label[rule]{eq:2}\\
  &\stcell{d_1}{c_1}, \stcont{d_1}{(\uscore\,e_2)}{d} \;\;\step\;\; \stevnc{e_2}{d_2}, \stcont{d_2}{(d_1\,\uscore)}{d}\qquad(d_2\text{ fresh})\label[rule]{eq:3}\\
  &\stcell{d_1}{(\lambda x.\, e_1')}, \stcell{d_2}{c_2}, \stcont{d_2}{(d_1\,\uscore)}{d} \;\;\step\;\; \stevnc{([d_2/x]e_1')}{d}\label[rule]{eq:4}\\
  &\stcell{d_1}{c}, \stevnc{d_1}{d} \;\;\step\;\; \stcell{d}{c}\label[rule]{eq:5} \\
  &\stevnc{(\lambda x.\, e)}{d}\;\;\step\;\; \stcell{d}{(\lambda x.\, e)}\label[rule]{eq:7}
  \end{align}
\begin{task}[20 points, WB]
  Implement call-by-need evaluation of function application on the S machine,
  replacing the rules above.
  If you keep any of the \cref{eq:2,eq:3,eq:4,eq:5,eq:7}, state which ones.

  Feel free to define a new kind of semantic object besides
  $\ms{eval}$, $\ms{cont}$, and ${!}\ms{cell}$ to track references to
  function arguments, but consider carefully if it should be ephemeral
  or persistent.
\end{task}
\begin{solution}
\end{solution}

\begin{task}[5 points, WB]
  Verify by showing the key steps of computation in each case that
  \begin{enumerate}
  \item $\ms{eval}\; ((\lambda x.\, \eunit)\, (\mb{fix}\, y.\, y))\; d_0$ terminates, and
  \item $\ms{eval}\; ((\lambda x.\, \langle x,x\rangle)\, e)\; d_0$ evaluates $e$ only once, where you should assume evaluation of $e$ terminates.
  \end{enumerate}
\end{task}
\begin{solution}
\end{solution}

\begin{task}[5 points, WB]
  Implement a process $s : \str \Vdash \ms{flush} :: (o : \Tunit)$ that discards all of the messages on $s$.
\end{task}
\begin{solution}
\end{solution}

\begin{task}[10 points, WB]
  Implement a process $s_1 : \str, s_2 : \str \Vdash \ms{eq} :: (r : \Tbool)$.
  It should send the message $\btrue$ over $r$ and terminate if and only if the strings $s_1$ and $s_2$ produce the same sequence of characters (labels); otherwise, it should send $\bfalse$ and terminate.
  \label{task:eq}
\end{task}
\begin{solution}
\end{solution}

\begin{task}[15 points, WB]
  Implement the string reversal process
  \[ s : \str \Vdash \ms{rev} :: (r : \str). \]
  You may define auxiliary processes as you see fit.  You do
  not need to be concerned about efficiency.

  As an example, the output on the channel $e$ of the following
  process should be $\btrue$:
  \[
    \cdot \Vdash s_1 \leftarrow \ulcorner ab \urcorner; s_2 \leftarrow \ulcorner ba \urcorner; r \leftarrow \ms{rev} \leftarrow s_2; e \leftarrow \ms{eq} \leftarrow s_1, r :: (e : \Tbool).
  \]
\end{task}
\begin{solution}
\end{solution}

\begin{task}[5 points, WB]
  Implement a process
  \[
    e : \exp \Vdash \ms{flush} :: (o : \Tunit)
  \]
  that discards all of the messages on $e$.
  Using this, implement a process
  \[
    e' : \exp \Vdash \ms{abort} :: (e : \exp)
  \]
  that aborts the computation by sending the label $E$ along $e$ and terminating.
\end{task}
\begin{solution}
\end{solution}

\begin{task}[10 points, WB]
  Implement the processes
  \begin{tabbing}
    \quad $e' : \exp \Vdash \plu :: (e : \exp)$\\
    \quad $e' : \exp \Vdash \tim :: (e : \exp)$
  \end{tabbing}
  The process $\plu$ should behave as follows: if the next two labels on $e'$ are integers, $\plu$ should output their sum on $e$ and then forward.
  Otherwise, $\plu$ should abort by sending the error label $E$ and terminating.
  The process $\tim$ should behave similarly, except by implementing multiplication.
\end{task}
\begin{solution}
\end{solution}

\begin{task}[10 points, WB]
  Implement the process $e' : \exp \Vdash \eval :: (e : \exp)$.
  Whenever it encounters a malformed expression, it should abort with an error.
  The process
  \[
    \cdot \Vdash e' \leftarrow \ulcorner + 1 \times 1\ 1 \urcorner; e \leftarrow \eval \leftarrow e' :: (e : \exp)
  \]
  should be indistinguishable from the process $\ulcorner 0 \urcorner$ to any process using $e$.
\end{task}
\begin{solution}
\end{solution}

\end{document}