\documentclass[11pt]{article}
% \documentclass[11pt,twoside]{article}

\usepackage{lecnotes}
\usepackage{graphicx}
\usepackage{textcomp}
\usepackage{comment}
\input{fp-macros}

\newcommand{\lecdate}{February 22, 2012} % e.g. January 12, 2010
\newcommand{\lecnum}{11}           % e.g. 1
\newcommand{\lectitle}{Functional Computation}         % e.g. Judgments and Propositions
\newcommand{\lecturer}{Frank Pfenning}         % e.g. Frank Pfenning

\begin{document}

\maketitle

\noindent
In the linear $\lambda$-calculus from last lecture, we interpreted
proof reductions as term reductions in an underlying language of proof
terms.  This is the original insight behind the Curry-Howard
isomorphism~\cite{Howard69}, albeit on an natural deduction for
(non-linear) intuitionistic logic and simply-typed (non-linear)
$\lambda$-terms.  But there is still a significant step between proof
term reduction and an operational semantics.  For example, in
functional programming languages such as ML or Haskell, we do not
evaluate under $\lambda$-abstractions, and we impose a specific order
of evaluation for functions such as call-by-value or call-by-name.
Finding compelling logical underpinnnings for these is still an active
area of research.

In this lecture we investigate a specific possibility for analyzing
the computational content of linear natural deductions, using two
pieces already in place: the translation from natural deduction to
sequent calculus, and the interpretation of sequent proofs as
concurrent processes.  Combining these two will give us a concurrent
operational semantics for the linear $\lambda$-calculus.  More details
and additional material on this translation can be found in Toninho et
al.~\cite{Toninho12fossacs}.  This is definitely not the end of the
story, however, because other interpretations are possible.  We may
return to them later in the course.

\section{From Natural Deduction to Sequent Calculus}

In the last lecture we stated that every natural deduction can be
translated to a sequent calculus proof.  Annotating the resulting
sequent calculus proofs with their process interpretations will
therefore provide us with a compilation of linear $\lambda$-terms to
the session-typed $\pi$-calculus.  The realization of this idea is
remarkably similar to Milner's interpretation of functions as
processes~\cite{Milner92mscs}, even though Milner's work was rooted in
the untyped $\lambda$-calculus and $\pi$-calculus.

Recall that linear hypothetical judgments of natural deduction have
the form
\[
\Gamma \semi \Delta \vvdash M : A
\]
where $\Gamma$ consists of unrestricted variable declarations
$u_j{:}B_j$ while $\Delta$ consists of linear variable declarations
$x_i{:}A_i$.  Similarly, sequents annotated with process terms have
the form
\[
\Gamma \semi \Delta \seq P :: x : A
\]
where $\Gamma$ consists of shared channels $u_j{:}B_j$, $\Delta$
consists of linear channels $x_i{:}A_i$, and $x{:}A$ is also a linear
channel.

Therefore, our goal is to translate proof terms $M$ to processes $P$
that preserves typing.  We see that the processes use one additional
channel, along which they offer a service, so the translation will
have to take this as a parameter.  We write
\[ [M]^x = P \]
and expect the theorem
\begin{quote}
  \textbf{From Functions to Processes.}\newline
  If $\Gamma \semi \Delta \vvdash M : A$
  then $\Gamma \semi \Delta \seq [M]^x :: x : A$.
\end{quote}
We also expect some connection between reductions on $M$, as shown in
the last lecture, and process reduction on $[M]^x$, but we will not
try to anticipate it and instead extract it from the translation
itself.  We now proceed construct by construct to see what we obtain.

\section{Variables}

Variables are translated to channels, and even in the statement of our
desired theorem we did not formally distinguish between them.  We
restate the case in the proof, now with proof terms.
\[
\infer[\m{hyp}]{x{:}A \vvdash x : A}{\mathstrut}
\]
We construct:
\[
\infer[\m{id}_A]{x{:}A \seq [x \fwd w] :: w : A}{\mathstrut}
\]
This means
\[
\begin{array}{lcl}
[x]^w & = & [x \fwd w]
\end{array}
\]


\section{Additive Pairs}

Next, we consider additive pairs, typed by $A \with B$.  First,
the right rule:
\[
\infer[{\with}I]
  {\Delta \vvdash \pair{M}{N} : A \with B}
  {\Delta \vvdash M : A &
    \Delta \vvdash N : B}
\]
The introduction rules correspond directly to right
rules and we obtain
\[
\infer[{\with}R]
  {\Delta \seq x.\case([M]^x,[N]^x) :: x : A \with B}
  {\Delta \seq [M]^x :: x : A &
    \Delta \seq [N]^x :: x : B}
\]
So $[\pair{M}{N}]^x = x.\case([M]^x,[N]^x)$.  A pair therefore offers
a choice between its components along the channel $x$.  The client
should be able to select the first or second component.

The elimination rules use cut in the target of the translation, so we
can transport the formula to which we apply the elimination to the
left-hand side of a sequent.  The rule
\[
\infer[{\with}E_1]
  {\Delta \vvdash \pi_1 M : A}
  {\Delta \vvdash M : A \with B}
\]
therefore becomes
\[
\infer[\cut_{A\with B}]
  {\Delta \seq A}
  {\Delta \seq A \with B &
    \infer[{\with}L_1]
      {A \with B \seq A}
      {\infer[\id_A]{A \seq A}{}}}
\]
Now we annotate this with process terms:
\[
\infer[\cut_{A\with B}]
  {\Delta \seq (\nu x)([M]^x \mid x.\inl; [x\fwd w]) :: w : A}
  {\Delta \seq [M]^x :: x : A \with B &
    \infer[{\with}L_1]
      {x{:}A \with B \seq x.\inl; [x \fwd w] :: w : A}
      {\infer[\id_A]{x{:}A \seq [x \fwd w] :: w : A}{}}}
\]
So, indeed, the client of a pair $[M]^x$ selects the first component
by sending $\inl$ along $x$ and forwarding the result.  Summarizing
the three cases for pairs:
\[
\begin{array}{lcl}
[\pair{M}{N}]^x = x.\case([M]^x,[N]^x) \\ \relax
[\pi_1 M]^w = (\nu x)([M]^x \mid x.\inl; [x \fwd w]) \\ \relax
[\pi_2 M]^w = (\nu x)([M]^x \mid x.\inr; [x \fwd w]) \\ \relax
\end{array}
\]
Now we should examine the reduction that arises when a destructor
(such as $\pi_1$ or $\pi_2$) meets a constructor (such as
$\pair{-}{-}$).  We obtain:
\[
\begin{array}{lcll}
[\pi_1 \pair{M}{N}]^w & = & (\nu x)([\pair{M}{N}]^x \mid x.\inl; [x \fwd w]) & \mbox{defn. of $[]^w$} \\
& = & (\nu x)(x.\case([M]^x, [N]^x) \mid x.\inl; [x\fwd w]) & \mbox{defn. of $[]^x$} \\
& \longrightarrow & (\nu x)([M]^x \mid [x \fwd w]) & \mbox{process reduction} \\
& \longrightarrow & [M]^w & \mbox{structural reduction}
\end{array}
\]
The last reduction here comes from a cut where the first premise is
annotated with $[M]^x$ and the second premise is the identity.  This
reduces to the first premise under some renaming to make sure the
offer takes place along the correct channel.

We can see from this that the induced operational semantics on natural
deduction proof terms is lazy for pairs of type $A \with B$, since the
$x.\case({-},{-})$ prefix protects it from reduction.  As predicted,
we can project out the desired component.  It also corresponds directly
to the term reduction
\[
\pi_1 \pair{M}{N} \longrightarrow_R M
\]

\section{Multiplicative Pairs}

Recall
\[
\infer[{\tensor}I]
  {\Delta, \Delta' \vvdash M \tensor N : A \tensor B}
  {\Delta \vvdash M : A & \Delta' \vvdash N : B}
\]
Under the translation, and writing in process terms immediately, we
obtain
\[
\infer[{\tensor}R]
  {\Delta, \Delta' \seq (\nu y) \send{x}{y}.([M]^y \mid [N]^x) :: x : A \tensor B}
  {\Delta \seq [M]^y :: y : A &
    \Delta' \seq [N]^x :: x : B}
\]
At this point it is not clear what to make of this.  In the
synchronous $\pi$-calculus the output prefix means that neither
$[M]^y$ nor $[N]^x$ can reduce.  So this form of pair also appears to
be lazy.

Perhaps the elimination rule will shed more light on the situation.
We index terms here with the explicitly mentioned variables or
channels that they may depend on, because it clarifies the
communication patterns.  We have:
\[
\infer[{\tensor}E]
  {\Delta, \Delta' \vvdash \llet y \tensor x = M \iin N_{y,x} : C}
  {\Delta \vvdash M : A \tensor B &
    \Delta', y{:}A, x{:}B \vvdash N_{y,x} : C}
\]
Here is the translation, first without process terms:
\[
\infer[\cut_{A \tensor B}]
  {\Delta, \Delta' \seq C}
  {\Delta \seq A \tensor B &
    \infer[{\tensor}L]
      {\Delta', A \tensor B \seq C}
      {\Delta', A, B \seq C}}
\]
Annotating this now with process terms we get:
\[
\infer[\cut_{A \tensor B}]
  {\Delta, \Delta' \seq (\nu x)([M]^x \mid \recv{x}{y}.[N]^w_{y,x}) :: w : C}
  {\Delta \seq [M]^x :: x : A \tensor B &
    \infer[{\tensor}L]
      {\Delta', x{:}A \tensor B \seq \recv{x}{y}. [N]^w_{y,x} :: w : C}
      {\Delta', y{:}A, x{:}B \seq [N]^w_{y,x} :: w : C}}
\]
In summary:
\[
\begin{array}{lcl}
[M \tensor N]^x & = & (\nu y) \send{x}{y}.([M]^y \mid [N]^x) \\ \relax
[\llet y \tensor x = M \iin N]^w & = &
(\nu x)([M]^x \mid \recv{x}{y}.[N]^w_{y,x})
\end{array}
\]
Checking out the reduction between the two:
\[
\begin{array}{lcll}
[\llet y \tensor x = M_1 \tensor M_2 \iin N]^w & = &
(\nu x)([M_1 \tensor M_2]^x \mid \recv{x}{y}.[N]^w_{y,x}) \\
& = & (\nu x)((\nu y) \send{x}{y}.([M_1]^y \mid [M_2]^x) \mid
     \recv{x}{y}.[N]^w_{y,x}) \\
& \longrightarrow &
(\nu x)(\nu y)([M_1]^y \mid [M_2]^x \mid [N]^w_{y,x})
\end{array}
\]
This reduction results in three parallel processes: $[M_1]^y$,
communicating along $y$, $[M_2]^x$, communicating along $x$, and the
body of the $\m{let}$-term $[N]^w_{y,x}$ which depends on both $y$ and
$x$.  Even though the multiplicative pairs themselves have some aspect
of lazy evaluation, they actually evaluate in parallel with the body,
synchronizing on the variables $y$ and $x$ along which they
communicate.  For example, when the variable $y$ is needed during the
computation of $N$, it will have to wait until $M_1$ offers
communication (either input or output, depending on the type of $y$).

\section{Functions}

Now we turn our attention to functions, keeping in mind that they must
use their argument exactly once.  First, the introduction rule
corresponds to a right rule, as usual.
\[
\infer[{\lolli}I]
  {\Delta \vvdash \lambda y.\, M_y : A \lolli B}
  {\Delta, y{:}A \vvdash M_y : B}
\]
Translated (already with the proof terms):
\[
\infer[{\lolli}R]
  {\Delta \seq \recv{x}{y}.[M]^x_y :: x : A \lolli B}
  {\Delta, y{:}A \seq [M]^x_y :: x : B}
\]
We see that the the translation of a function $[\lambda y.M]^x =
\recv{x}{y}.[M]^x_y$ first receives the function argument along $x$ and
then evaluates the body.  However, the ``function argument'' here is
not a value, but the name of a channel along which we can communicate
with the argument.  Let's look at the evaluation of function
application.
\[
\infer[{\lolli}E]
  {\Delta, \Delta' \vvdash M\, N : B}
  {\Delta \vvdash M : A \lolli B &
    \Delta' \vvdash N : A}
\]
Here is what we construct, first without process terms.
\[
\infer[\cut_{A \lolli B}]
  {\Delta, \Delta' \seq B}
  {\Delta \seq A \lolli B &
    \infer[{\lolli}L]
      {\Delta', A \lolli B \seq B}
      {\Delta' \seq A &
       \infer[\id_B]{B \seq B}{\mathstrut}}}
\]
Filling in process terms, we get:
\[
\infer[\cut_{A \lolli B}]
  {\Delta, \Delta' \seq (\nu x)([M]^x \mid (\nu y)\send{x}{y}.([N]^y \mid [x \fwd w])) :: w : B}
  {\Delta \seq [M]^x :: x : A \lolli B &
    \infer[{\lolli}L]
      {\Delta', x{:}A \lolli B \seq (\nu y)\send{x}{y}.([N]^y \mid [x \fwd w]) :: w : B}
      {\Delta' \seq [N]^y :: y : A &
       \infer[\id_B]{x{:}B \seq [x \fwd w] :: w : B}{\mathstrut}}}
\]
We can see that the evaluation of the argument $N$ is blocked until
$M$ is ready to receive an input.  This will be the case when it has
reduced to the translation of a function.  Let's calculate:
\[
\begin{array}{lcl}
[(\lambda y.\, M)\, N]^w & = &
(\nu x)([\lambda y.\, M]^x \mid (\nu y)\send{x}{y}.([N]^y \mid [x \fwd w])) \\
& = & (\nu x)(\recv{x}{y}.[M]^x_y \mid (\nu y)\send{x}{y}.([N]^y \mid [x \fwd w])) \\
& \longrightarrow & (\nu x)(\nu y)([M]^x_y \mid [N]^y \mid [x \fwd w]) \\
& \longrightarrow & (\nu y)([M]^w_y \mid [N]^y)
\end{array}
\]
We see that the function body is evaluated in parallel with the
function argument, synchronizing on the uses of the variable $y$.
Writing the natural deduction reduction
\[
(\lambda y.\, M)\, N \longrightarrow [N/y]M
\]
we see that instead of carrying out a substitution over the term $M$
we tie the computation of $N$ to $y$, now viewed as a channel.
In general, when a term is typed as
\[
\Delta \vvdash M : A
\]
then $[M]^x$ will compute in an environment where each channel
$x_i{:}A_i$ declared in $\Delta$ will be tied to process communicating
along it, implementing the concurrent evaluation of a process
$[N_i]^{x_i}$.

To relate reduction to the translation, we expect some relation
\[
[[N/y]M]^w \simeq (\nu y)([N]^y \mid [M]^w_y)
\]
Of course, this does not hold has an equality, since $N$ may be deeply
embedded in $[N/y]M$ while it is available at the top-level of the
process on the right.  Still, the process term on the right (logically
a cut) can be seen as an implementation of the term on the left
(logically a substitution).

The evaluation strategy that this corresponds to is
\emph{futures}~\cite{Halstead85}: we evaluate the argument at the same
time as the function body, synchronizing on accesses to variables.
This can be specialized into call-by-value and call-by-need.  If we
always schedule reduction on the argument (called $N$ above) first,
then it becomes call-by-value.  If we always schedule reduction on the
body first, then it becomes call-by-need.  Linearity of the variable
ensures not only that it will be needed, but that its value is needed
only once.

\section{Persistence}

So far, all variables and corresponding channels have been linear.
How do unrestricted variables (which correspond to shared channels)
fit into the picture?

First, the unrestricted hypothesis rule.
\[
\infer[\m{uhyp}]
 {\Gamma \semi \cdot \vvdash u : A}
 {u{:}A \in \Gamma}
\]
Without process terms, we construct the sequent
proof as follows:
\[
\infer[\m{copy}]
 {\Gamma \semi \cdot \seq A}
 {\infer[\id]{\Gamma \semi A \seq A}{\mathstrut} 
   & A \in \Gamma}
\]
Assigning proof terms:
\[
\infer[\m{copy}]
 {\Gamma \semi \cdot \seq (\nu y)\send{u}{y}. [y \fwd x] :: x : A}
 {\infer[\id]{\Gamma \semi y{:}A \seq [y \fwd x] :: x : A}{\mathstrut}
   & u{:}A \in \Gamma}
\]
So the translation of an unrestricted variable appearing as a term,
$[u]^x$, creates a new channel and sends it to $u$, tying the result
to $x$.

Translating the ${\bang}I$ rule should tell us which form of process
is listening on this channel.
\[
\infer[{\bang}I]
  {\Gamma \semi \cdot \seq \bang M : \bang A}
  {\Gamma \semi \cdot \seq M : A}
\]
Translating the proof
\[
\infer[{\bang}R]
  {\Gamma \semi \cdot \seq \bang \recv{x}{y}. [M]^y :: x : \bang A}
  {\Gamma \semi \cdot \seq [M]^y :: y : A}
\]
reveals that the process is a replicating input that can create
arbitrarily many copies of a term $[M]^y$.

The elimination rule will set up the communcation channel
along $u$.
\[
\infer[{\bang}E]
  {\Gamma \semi \Delta, \Delta' \vvdash
    \llet \bang u = M \iin N : C}
  {\Gamma \semi \Delta \vvdash M : \bang A &
    \Gamma, u{:}A \semi \Delta' \vvdash N : C}
\]
Translated as
\[
\infer[\cut_{\bang A}]
  {\Gamma \semi \Delta, \Delta' \vvdash C}
  {\Gamma \semi \Delta \vvdash \bang A &
    \infer[{\bang}L]
      {\Gamma \semi \Delta', \bang A \vvdash C}
      {\Gamma, A \semi \Delta' \vvdash C}}
\]
and with process terms:
\[
\infer[\cut_{\bang A}]
  {\Gamma \semi \Delta, \Delta' \vvdash 
    (\nu x)([M]^x \mid x/u.[N]^w_u) :: w : C}
  {\Gamma \semi \Delta \vvdash [M]^x :: x : \bang A &
    \infer[{\bang}L]
      {\Gamma \semi \Delta', x{:}\bang A \vvdash x/u.[N]^w_u :: w : C}
      {\Gamma, u{:}A \semi \Delta' \vvdash [N]^w_u :: w : C}}
\]
Let's summarize these translations:
\[
\begin{array}{lcl}
[u]^x & = & (\nu y)\send{u}{y}. [y \fwd x] \\ \relax
[\bang M]^ x & = & \recv{x}{y}. [M]^y \\ \relax
[\llet \bang u = M \iin N]^w & = & (\nu x)([M]^x \mid x/u.[N]^w_u)
\end{array}
\]
Again, we calculate the reductions to obtain a reading for the
operational semantics under the process interpretation.
\[
\begin{array}{lcl}
[\llet \bang u = \bang M \iin N]^w
 & = & (\nu x)([\bang M]^x \mid x/u.[N]^w_u) \\
 & = & (\nu x)(\bang \recv{x}{y}. [M]^y \mid x/u.[N]^w_u) \\
 & \longrightarrow & (\nu u)(\bang \recv{u}{y}. [M]^y \mid [N]^w_u)
\end{array}
\]
We see that $u$ is tied to a replicating input along $u$, as expected,
and that the body of the let will evaluate.  This will happen as soon
as the argument was prepared to receive along $x$, that is, had been
reduced to a replicating input along $x$.

Comparing this to the term reduction
\[
\begin{array}{lcl}
\llet \bang u = \bang M \iin N & \longrightarrow & [M/u]N
\end{array}
\]
we see that substitution for unrestricted variables is
implemented differently than linear substitution.  We
expect some relation
\[
[[M/u]N]^w \simeq (\nu u)(\bang \recv{u}{y}. [M]^y \mid [N]^w_u)
\]
where the left-hand side is unrestricted substitution and the
right-hand side is $\cutbang$.

Together, these reductions give us a copying interpretation for $\bang
A$.  We compute a value $\bang M$, where a fresh copy of $M$ is
evaluated every time it is used.  This is like a call-by-name
interpretation of function calls, except that here it is tied to a
$\m{let}$-elimination (see Exercise~\ref{exc:intfun}).

For now, we just have the preservation of types across the
translation.
\begin{theorem}[From Functions to Processes]
  \mbox{}\newline
  If $\Gamma \semi \Delta \vvdash M : A$ then $\Gamma \semi \Delta
  \seq [M]^x :: x : A$.
\end{theorem}
\begin{proof}
  By induction on the structure of the given deduction.  The reasoning
  in each case is contained in the proof translations constructed in
  this lecture.
\end{proof}

\clearpage
\phantomsection
\addcontentsline{toc}{section}{Exercises}
\section*{Exercises}

\begin{exercise}\rm
  Play through the computational interpretation of disjunction $A
  \oplus B$ via an interpretation into the session-typed
  $\pi$-calculus in the manner of this lecture.
\end{exercise}

\begin{exercise}\rm
  Play through the computation interpretation of the multiplicative
  unit $\one$ via an interpretation into the session-typed
  $\pi$-calculus in the manner of this lecture.
\end{exercise}

\begin{exercise}\rm
  \label{exc:intfun}
  A simple embedding of (unrestricted) functions into the linear
  functional language is by defining $A \supset B = (!A) \lolli B$.
  Explore this definition.  Specifically:
  \begin{enumerate}
    \item[(i)] Give derived term assignments for introduction
      and elimination rules for $A \supset B$ under the
      above definition.
    \item[(ii)] Translate the derived term assignments into process
      terms for the sequent calculus.
    \item[(iii)] Explain the evaluation strategy that corresponds to
      this embedding of unrestricted functions into the linear
      $\lambda$-calculus.
  \end{enumerate}
\end{exercise}

\clearpage
\phantomsection
\addcontentsline{toc}{section}{References}
\bibliographystyle{alpha}
\bibliography{fp,lfs}

% \cleardoublepage
\end{document}
