% flag to use fancy fonts / graphics
\newif\ifjnf\jnftrue

\documentclass[11pt]{article}

\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{cmtt}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{stmaryrd}
\usepackage{url}
\usepackage[margin=1in]{geometry}

\usepackage{csnotes}

% local definitions for this lecture
\newcommand{\lam}[2]{\ensuremath{\lambda #1.\,#2}}
\newcommand{\definedas}{\ensuremath{\triangleq}}
\newcommand{\subst}[3]{\ensuremath{#1[#2/#3]}}
\newcommand{\cdef}[2]{\ensuremath{\mathrm{def}~#1~\text{in}~#2}}
% \newcommand{\vec}[1]{\ensuremath{\tilde{#1}}}
\newcommand{\join}{\vartriangleright}
\newcommand{\react}{\rightarrow}
\newcommand{\trans}[1]{\stackrel{#1}{\rightarrow}}
\newcommand{\dom}{\mathop{\rm dom}}
\newcommand{\rng}{\mathop{\rm rng}}
\newcommand{\new}[2]{\ensuremath{\mathrm{new}\;#1\;\mathrm{in}\;#2}}
\newtheorem{thm}{Theorem}

\begin{document}

\header{13 October 2011}{Mark Reitblatt}

\section{Chemical Abstraction Machine}

The Chemical Abstract Machine (CHAM) is a model of computation for concurrent calculi.

``Intuitively, the state of a system is like a chemical solution in which the floating molecules can interact with each other according to reaction rules; a magical mechanism stirs the solution, allowing for possible contacts between molecules''

Reaction rules are static, specified before computation. The reaction rules specify the signature of concurrent calculus we are modeling. We can think of the rules as \textit{catalysts} of reactions, and also as \textit{places} where molecules must travel to react.

Unfortunately, specifying a fixed set of reaction rules at the beginning of the computation limits how we can model calculi. 

\begin{itemize}
\item All communication is constrained to the fixed set of reaction sites. ``Catalysts are bottlenecks''
\item All pattern matching in the core calculus must be in the fixed set of rules, which may complicate dynamic elements of matching. ``Catalysts clog up''
\end{itemize}
 
\subsection{Reflexive CHAM}

To solve both of these problems, we permit molecules to add new reactions to their environment. This gives us the Reflexive CHAM.

Reflexive CHAM processes $P$ are either:
\begin{itemize}
\item An emission of an asynchronous polyadic message $x\langle\vec{v}\rangle$
\item A definition of new names $\cdef{D}{P}$
\item A parallel composition of processes $P \mid P$
\end{itemize}

A join-pattern $J$ consists of:
\begin{itemize}
\item Receipt of an asynchronous polyadic message $x\langle\vec{v}\rangle$
\item Parallel composition of joins $J\mid J$
\end{itemize}

A definition $D$ consists of:
\begin{itemize}
\item An elementary definition that match patterns to guarded processes $J\join P$
\item A conjunction of definitions $D \wedge D$
\end{itemize}

Names that appear in a process P may be captured by an enclosing definition. The only binder is the join pattern $J$, but the scope of its names depeds on their position in messages. The formal parameters that are received are bound in the correspond guarded process. The defined port names are bound in the whole defining process, that is, the main process and recursively all the guarded processes. Received variables $rv(J)$, defined variables $dv(D)$, and free variables $fv(D)$ and $fv(P)$ are specified by structural induction. Notice the syntactic restriction for processes: No received variable may appear twice in the same pattern $J$. This rules out any comparison on names, and guarantess that join patterns are linear.

\begin{align*}
  rv( x\langle\vec{v}\rangle) &\definedas \{u\in \vec{v}\} 
& {rv}(J\mid J') &\definedas {rv}(J) \uplus {rv}(J') 
\end{align*}

\begin{align*}
{dv}( x\langle\vec{v}\rangle) &\definedas \{x\}
& {dv}( J \mid J') &\definedas {dv}(J) \cup {dv}(J')
\end{align*}

\begin{align*}
{dv}( J \join P) &\definedas \{{dv}(J)
& {dv}( D \wedge D') &\definedas {dv}(D) \cup {dv}(D')
\end{align*}

\begin{align*}
{fv}( J \join P) &\definedas {dv}(J)\cup ({fv}(P) - {rv}(J))
& {fv}( D \wedge D') &\definedas {fv}(D) \cup {fv}(D')
\end{align*}

\begin{align*}
  {fv}( x\langle\vec{v}\rangle) &\definedas \{x\} \cup \{u\in \vec{v}\} 
& {fv}(\cdef{D}{P}) &\definedas ({fv}(P) \cup {fv}(D)) - {dv}(D)
& {fv}(P\mid P') &\definedas {fv}(P) \cup {fv}(P') 
\end{align*}

Rules operate on solutions $\mathcal{R}\vdash\mathcal{M}$ consisting of a multiset $\mathcal{M}$ of molecules (active processes) and a multiset $\mathcal{R}$ of reactions (active definitions). There are reversible structural ``heating/cooling'' rules ($\rightleftharpoons$) and one irreversible reaction rule ($\react$).

\begin{align*}
\mathcal{R}\vdash\mathcal{M},P\mid Q &\rightleftharpoons& \mathcal{R} \vdash \mathcal{M},P,Q\\
\mathcal{R},D\vdash E\vdash \mathcal{M} &\rightleftharpoons& \mathcal{R},D,E \vdash \mathcal{M}\\
\mathcal{R}\vdash \mathcal{M},\cdef{D}{P} &\rightleftharpoons& \mathcal{R},D\sigma_{dv}\vdash \mathcal{M},P\sigma_{dv}\\
\mathcal{R},J\join P\vdash \mathcal{M},J\sigma_{rv} &\react& \mathcal{R},J\join P\vdash \mathcal{M},P\sigma_{rv}
\end{align*}

Side Conditions:

\begin{itemize}
\item $\dom(\sigma_{dv})\subseteq dv(D)$ and substitutes for these variables distinct, fresh names. Fresh means $\rng(\sigma_{dv})\cap (fv(\mathcal{R}\cup fv(\mathcal{M}\cup fv(\cdef{D}{P})) = \empty$
\item $\dom(\sigma_{Rv})\subseteq rv(J)$
\end{itemize}

\section{Join-Calculus}

We can think of the CHAM as a computational model derived from process calculi like the $\pi$-calculus. We can also go in the opposite direction and produce a process calculus, the join-calculus, from the reflecive CHAM. The terms of the join calculus are just the molecules of the reflexive CHAM, and the structural equivalence and transition rules correspond to the reaction rules of the CHAM.

However, we're going to look at a smaller version of the full join calculus, the core join calculus. It turns out that we lose no expressive power.

\begin{align*}
P \ \ &::=\ x\langle u\rangle \mid (P_1|P_2) \mid \cdef{(x\langle u\rangle|y\langle v\rangle \join P_1)}{P_2}
\end{align*}

The join calculus includes a structural congruence relation $(\equiv)$

\begin{align*}
P|Q &\equiv Q|P\\
P|(Q|R) &\equiv (P|Q)|R\\
P|\cdef{D}{Q} &\equiv  \cdef{D}{P|Q}\\
\cdef{D}{\cdef{D'}{P}} &\equiv  \cdef{D'}{\cdef{D}{P}}\\
P \equiv_{\alpha} P' &\implies P\equiv P'\\
P \equiv Q &\implies (P|R) \equiv (Q|R)\\
R\equiv S, P\equiv Q &\implies \cdef{J\join R}{P} \equiv \cdef{J\join S}{Q}
\end{align*}

Semantics are given by a labelled transition relation $\trans{\delta}$ where $\delta$ ranges over $D\cup \{\tau\}$. The relation is the smallest such that

\begin{itemize}
\item Forall $D = x\langle u \rangle |y\langle v\rangle \join R$, we have $x\langle s\rangle|y\langle t\rangle \trans{D} R[s/x,t/y]$
\item If $P\trans{\delta}P'$, then
  \begin{itemize}
    \item $P|Q\trans{\delta}P'|Q$
    \item $\cdef{D}{P}\trans{\delta}\cdef{D}{P'}$ (if $fv(D)\cap dv(\delta)=\empty$)
    \item $\cdef{\delta}{P}\trans{\tau}\cdef{\delta}{P'}$ (if $\delta \neq \tau$)
    \item $Q\trans{\delta}{Q'}$ (if $P\equiv Q$ and $P'\equiv Q'$)
  \end{itemize}
\end{itemize}

\subsection{The Join-Calculus and the Reflexive CHAM}

\begin{lemma}
  The structurual congruence $\equiv$ is the smallest congruence that contains all pairs of processes $P,Q$ such that $\vdash P\rightleftharpoons^*\vdash Q$. The silent transition relation $\trans{\tau}$ contains exactly the pairs of processes $P,Q$ up to $\equiv$ such that $\vdash P\react \vdash Q$
\end{lemma}

\subsection{Encodings in the Join-Calculus}

\subsubsection{CBN Lambda Calculus}

\begin{align*}
\llbracket x\rrbracket_v & :=  x\langle v\rangle\\
\llbracket \lambda x.T\rrbracket_v &:= \cdef{\kappa\langle x,w\rangle \join \llbracket T\rrbracket_w}{v\langle \kappa\rangle}\\
\llbracket T U \rrbracket_v &:= \cdef{x\langle u \rangle \join \llbracket U\rrbracket_u}{\cdef{w\langle \kappa\rangle \join \kappa\langle x,v\rangle}{\llbracket T\rrbracket_w}}
\end{align*}

The interpretation $\llbracket T\rrbracket_v$ means that $T$ should send its value on channel $v$. A value is a channel $\kappa$ which, if sent $\langle x,w\rangle$, uses $x$ to look up an argument and sends the result of applying itself to the argument on $w$. To lookup the value of $x$, send $z$ to $x$, and the value of $x$ will be sent on $z$.

\subsubsection{Parallel CBV Lambda Calculus}

\begin{align*}
\llbracket x\rrbracket_v & :=  v\langle x\rangle\\
\llbracket \lambda x.T\rrbracket_v &:= \cdef{\kappa\langle x,w\rangle \join \llbracket T\rrbracket_w}{v\langle \kappa\rangle}\\
\llbracket T U \rrbracket_v &:= \cdef{t\langle \kappa \rangle |u\langle w\rangle \join \kappa\langle w,v\rangle}{\llbracket T\rrbracket_t|\llbracket U\rrbracket_u}
\end{align*}

This differs in that instead of sending a channel which serves up the unreduced argument, we send the actual argument in the 'application request'. We can do this in the definition of application because we wait until both of the terms in the application have converged to a value.

\section{Join-Calculus and the $\pi$-calculus}

We can think of the join calculus as an asynchronous version of the $\pi$-calculus, except with restrictions:

\begin{itemize}
\item All binding happens with one construct, the defintion.
\item Synchronization only happens with defined names; processes cannot pass messages over free names. Even though one can write on a channel with a free name, communication only occurs when a defined rule executes.
\item For every defined name, there is exactly one replicated read. If we think of the calculus in a distributed setting, this means that for every defined name, there is exactly one place here synchronization can occur for that name.
\end{itemize}

\subsection{Asynchronous $\pi$-calculus in Join-Calculus}

\begin{align*}
P \ \ &::=\ (P|Q)\mid  \new{u}{P} \mid \bar{x}\langle u\rangle\mid x(u).P\mid !x(u).P
\end{align*}

Naive representation of the $\pi$-calculus

\begin{align*}
\llbracket P|Q \rrbracket_\pi &:= \llbracket P\rrbracket_\pi |\llbracket Q\rrbracket_\pi\\
\llbracket \new{x}{P}\rrbracket_\pi &:= \cdef{x_o\langle v_o,v_i\rangle |x_i\langle\kappa\rangle \join \kappa\langle v_o,v_i\rangle}{\llbracket P\rrbracket\pi}\\
\llbracket\bar{x}\langle v\rangle\rrbracket_\pi &:= x_o\langle v_o,v_i\rangle\\
\llbracket x(v).P\rrbracket_\pi &:= \cdef{\kappa\langle v_o,v_i\rangle\join \llbracket P\rrbracket_\pi}{x_i\langle\kappa\rangle}\\
\llbracket !x(v).P\rrbracket_\pi &:= \cdef{\kappa \langle v_o, v_i\rangle \join x_i\langle\kappa\rangle |\llbracket P\rrbracket_\pi}{x_i\langle \kappa\rangle}
\end{align*}

But this translation doesn't quite work. For instance, $\llbracket \bar{x}\langle a\rangle|\bar{x}\langle b\rangle|x(u).\bar{y}\langle u\rangle\rrbracket_\pi$ is stuck, even though $\bar{x}\langle a\rangle|\bar{x}\langle b\rangle|x(u).\bar{y}\langle u\rangle$ is not stuck in the $\pi$-calculus. This is because we have no enclosing $\new{x}{\ldots}$ to provide the definition that allows synchronization to occur.

Additionally, this encoding is not fully abstract in the sense that it is not robust given an arbitrary concurrent context that the encoded process lives in. Even if we make sure all of our free names have been appropriately defined, a channel we are reading on might be written to by some malicious process, with the message being a free name. Again, if we try to do anything to that free name, we become stuck.

We use a tool called the \textit{equator} to fix this problem.

\begin{align*}
  M^\pi_{x,y} &:= !x(u).\bar{y}\langle u\rangle |!y(u).\bar{x}\langle u\rangle
\end{align*}

This is an asynchronous $\pi$-calculus program which conflates the two channels $x$ and $y$. If $M^\pi_{x,y}$ is in the 'solution', then no process can tell the difference between them.

To protect the translation from hostile contexts, the encoding must set-up a ``firewall'' that enforces the protocol. We refine our first approach: each channel $x$ is now represented as several pairs $x_o,x_i$ from the naive encoding that cannot be distinguished from the outside. Two pairs are merged by repeatedly communicating their pending messages to one another. New pairs are defined at run-time according to the following secure protocol:

\begin{itemize}
  \item Whenever a pair of names is received from the outside, the firewall defines a new, correct proxy pair, merges it to the external pair, and transmits the new pair instead
  \item Whenever a pair of names is sent to the outside, a new firewall is inserted to set up proxies for future messages on this pair
\end{itemize}

We use the following contexts to build the firewall on top of the naive translation:

\begin{align*}
\mathcal{P}_x[P] & := \cdef{x_i\langle v_o,v_i\rangle|x_i\langle\kappa\rangle \join \kappa \langle v_o,v_i\rangle}{\cdef{x_o\langle v_o,v_i\rangle\join p\langle v_o,v_o,x_i\rangle}{P}}\\
\mathcal{E}_x[P] &:= \mathcal{P}_x[x_e\langle x_o,x_i\rangle|P]\\
\mathcal{M}[P] &:= \cdef{p(x_o,x_i,\kappa)\join \mathcal{P}_y[\kappa\langle y_o,y_i\rangle|\llbracket M^\pi_{x,y}\rrbracket_\pi]}{P}\\
\mathcal{E}[P] &:= \mathcal{M}[\mathcal{E}_{x_1}[\ldots \mathcal{E}_{x_n}[P]\ldots]] \;\;\; \text{for $x_1,\ldots,x_n = fv(P)$}
\end{align*}

For every free name $x$, $\mathcal{P}_x$ encodes the creation of a new proxy for its output. $\mathcal{E}_x$ does the same, and also exports the proxy on a conventional free name $x_e$. Finally, $\mathcal{M}$ defines the proxy creator $p$ for the whole translation.

Full Abstraction theorem:

\begin{thm}
$Q\approx_\pi R \iff \mathcal{E}[\llbracket Q\rrbracket_\pi] \approx \mathcal{E}[\llbracket R\rrbracket_\pi] $
\end{thm}

\subsection{Join-Calculus in $\pi$-Calculus}

Naive representation of the Join-Calculus

\begin{align*}
\llbracket P|Q\rrbracket_j &:= \llbracket P \rrbracket_j |\llbracket Q\rrbracket_j\\
\llbracket x\langle v\rangle\rrbracket_j &:= \bar{x}\langle v\rangle\\
\llbracket\cdef{x\langle u\rangle|y\langle v\rangle\join P}{Q}\rrbracket_k &:= \new{x,y}{((!x(u).y(v).\llbracket P\rrbracket_j)|\llbracket Q \rrbracket_j)}
\end{align*}

But this translation, like the previous one, is not robust up to arbitray contexts. We use a similar trick as before. We define a relay process and firewall definitions

\begin{align*}
R_{x,y} &:= !x(v).\new{v_e}{\bar{r}\langle v_e,v\rangle|\bar{y}\langle v_e\rangle}\\
\mathcal{R}[P] &:= \new{r}{(!r(x,x_e).R_{x,x_e}|P)}\\
\mathcal{E}^\pi_x[P] &:= \new{x}{(R_{x,x_e}|P)}\\
\mathcal{E}^\pi[P] &:= \mathcal{R}[\mathcal{E}^\pi_{x_1}[\ldots \mathcal{E}^\pi_{x_n}[P]\ldots]] \;\;\; \text{for $x_1,\ldots,x_n = fv(P)$}
\end{align*}

Full Abstraction theorem:

\begin{thm}
$P\approx Q\iff \mathcal{E}^\pi[\llbracket P\rrbracket_j]\approx_\pi \mathcal{E}^\pi[\llbracket Q\rrbracket_j]$
\end{thm}


\end{document}