\section{Lecture 3 - SAT-Solving}

\begin{frame}
  \frametitle{SAT-Solving}

  SAT-Solving is (the art of) finding the assignment satisfying all clauses
  \vfill
  The assignment evolves {\bf incrementally} by taking {\bf Decisions},
  starting from empty $\{\ \}$, but it can be {\bf backtracked} if found wrong
  \vfill
  The evolution of the assignment can be represented as a {\bf tree}
  \vfill
  \begin{tabular}{ccc}
    \begin{minipage}{.4\textwidth}
    $$
      \begin{array}{l}
      (\coltwoat{\neg a_1}{3-5} \vee a_3 \vee a_9) \\
      (\coloneat{\coltwoat{\neg a_2}{4}}{5} \vee \neg a_3 \vee a_4) \\
      (\coloneat{a_1}{3-5} \vee \coltwoat{\coloneat{a_2}{4}}{5}) \\
      (\neg a_4 \vee a_5 \vee a_{10})
      \end{array}
    $$
    \begin{overlayarea}{\textwidth}{.3cm}
      \only<1|handout:0>{$\{\ \}$}
      \only<2|handout:0>{$\{ \neg a_1 \}$}
      \only<3|handout:0>{$\{ \neg a_1, \neg a_2 \}$}
      \only<4|handout:0>{$\{ \neg a_1, a_2 \}$}
      \only<5>{$\{ \ldots \}$}
    \end{overlayarea}
    \end{minipage}
    & ~~~~ &
    \begin{minipage}{.4\textwidth}
    \begin{overlayarea}{\textwidth}{5cm}
      %\only<1|handout:0>{\scalebox{.7}{\input{sat_search_1.pdf_t}}}
      \only<1|handout:0>{\scalebox{.7}{\input{sat_search_1_1.pdf_t}}}
      \only<2|handout:0>{\scalebox{.7}{\input{sat_search_1_2.pdf_t}}}
      \only<3|handout:0>{\scalebox{.7}{\input{sat_search_1_3.pdf_t}}}
      \only<4|handout:0>{\scalebox{.7}{\input{sat_search_1_4.pdf_t}}}
      \only<5>{\scalebox{.7}{\input{sat_search_1_5.pdf_t}}}
    \end{overlayarea}
    \end{minipage}
  \end{tabular}

\end{frame}

\begin{frame}
  \frametitle{SAT-Solving}

  There are assignments which can be trivially driven towards the right direction.
  In the example below, given the current assignment, the third clause can be satisfied
  only by setting $a_2 \mapsto \top$
  \vfill
  \begin{tabular}{ccc}
    \begin{minipage}{.4\textwidth}
    $$
      \begin{array}{l}
      (\coltwoat{\neg a_1}{1-} \vee a_3 \vee a_9) \\
      (\coloneat{\neg a_2}{2-} \vee \neg a_3 \vee a_4) \\
      (\coloneat{a_1}{1-} \vee \coltwoat{a_2}{2-}) \\
      (\neg a_4 \vee a_5 \vee a_{10})
      \end{array}
    $$
    \begin{overlayarea}{\textwidth}{.3cm}
      \only<1|handout:0>{$\{ \neg a_1 \}$}
      \only<2->{$\{ \neg a_1, a_2 \}$}
    \end{overlayarea}
    \end{minipage}
    & ~~~~ &
    \begin{minipage}{.4\textwidth}
    \begin{overlayarea}{\textwidth}{4cm}
      \only<1|handout:0>{\scalebox{.7}{\input{sat_search_2_1.pdf_t}}}
      \only<2->{\scalebox{.7}{\input{sat_search_2_2.pdf_t}}}
    \end{overlayarea}
    \end{minipage}
  \end{tabular}
  \vfill
  \pause
  \pause
  This step is called {\bf Boolean Constraint Propagation} (BCP). It represents
  a {\bf forced} deduction. It triggers whenever all literals but one are assigned $\bot$
  $$
  (\colone{\neg a_1} \vee \colone{a_2} \vee \colone{\neg a_3} \vee \colone{a_4} \vee \neg a_5)
  $$

\end{frame}

\begin{frame}[fragile]
  \frametitle{SAT-Solving}

  {\bf Decision-level}: in an assignment, it is the number of decisions taken
  \vfill
  Clearly, BCPs do not contribute to increase the decision level
  \vfill
  When necessary, we will indicate decision level on top of literals $\dec{a}{5}$ in the assignment
  \vfill
  Example:
  \begin{center}
  \begin{tabular}{ccc}
    \begin{minipage}{.4\textwidth}
      \vfill
      $\{ \dec{a_{10}}{0}, \dec{\neg a_{1}}{1}, \dec{a_3}{2}, \dec{\neg a_4}{3}, \dec{a_5}{3} \}$
      \vfill
    \end{minipage}
    & &
    \begin{minipage}{.4\textwidth}
      \scalebox{.4}{\input{sat_search_4.pdf_t}}
    \end{minipage}
  \end{tabular}
  \end{center}

\end{frame}

\begin{frame}
  \frametitle{SAT-Solving}

  The process of finding the satisfying assignment is called {\bf search}, and
  in its basic version it evolves with {\bf Decisions}, {\bf BCPs}, and
  {\bf backtracking}

  \begin{center}
  \scalebox{.5}{\input{sat_search_3.pdf_t}}
  \end{center}

\end{frame}

\begin{frame}
  \frametitle{SAT-Solving}

  \scriptsize

  Consider the following scenario before and after BCP
  \vfill
  \begin{tabular}{ccc}
  \begin{minipage}{.4\textwidth}
  $$
  \begin{array}{l}
    \ldots \\
    (\colone{\neg a_{10}} \vee \colone{\neg a_1} \vee a_4) \\
    (\colone{a_3} \vee \colone{\neg a_1} \vee a_5) \\
    (\neg a_4 \vee a_6) \\
    (\neg a_5 \vee \neg a_6) \\
    \ldots \\
    \\
    \{ \dec{a_{10}}{0}, \dec{\neg a_3}{1}, \dec{a_7}{2}, \dec{\neg a_2}{3}, \dec{a_1}{4} \}
  \end{array}
  $$
  \end{minipage}
  & ~~~~ &
  \begin{minipage}{.5\textwidth}
  $$
  \begin{array}{l}
    \ldots \\
    (\colone{\neg a_{10}} \vee \colone{\neg a_1} \vee \coltwo{a_4}) \\
    (\colone{a_3} \vee \colone{\neg a_1} \vee \coltwo{a_5}) \\
    (\colone{\neg a_4} \vee \coltwo{a_6}) \\
    (\colone{\neg a_5} \vee \colone{\neg a_6}) \\
    \ldots \\
    \\
    \{ \dec{a_{10}}{0}, \dec{\neg a_3}{1}, \dec{a_7}{2}, \dec{\neg a_2}{3}, \dec{a_1}{4}, \dec{a_4}{4}, \dec{a_5}{4}, \dec{a_6}{4}\}
  \end{array}
  $$
  \end{minipage}
  \end{tabular}
  \vfill
  BCP leads to a conflict, but, what is the {\bf reason} for it ?
  Do $a_7$ and $a_2$ play any role ?
  \vfill
  Does it make sense to consider the assignments (which backtracking would produce) ?\\
  $\{ \dec{a_{10}}{0}, \dec{\neg a_3}{1}, \dec{\neg a_7}{2}, \dec{\neg a_2}{3}, \dec{a_1}{4} \}$ ---
  $\{ \dec{a_{10}}{0}, \dec{\neg a_3}{1}, \dec{a_7}{2}, \dec{\neg a_2}{3}, \dec{a_1}{4} \}$ ---
  $\{ \dec{a_{10}}{0}, \dec{\neg a_3}{1}, \dec{a_7}{2}, \dec{a_2}{3}, \dec{a_1}{4} \}$ \\
  \vfill
  No, because {\em whenever $a_{10}$, $\neg a_3$ is assigned, then $a_1$ must not be set to $\top$}.
  This translates to an additional clause, which can be {\em learnt}, i.e., it can be added to the formula
  $$(\neg a_{10} \vee a_3 \vee \neg a_1)$$

\end{frame}

\begin{frame}
  \frametitle{SAT-Solving}

  \scriptsize

  In practice, we use {\bf resolution} steps to compute the learnt clause:
  \begin{itemize}
    \item we start from the clause with all literals to $\bot$ (conflicting clause)
    \item iteratively, we take the clause that propagated the last literal on the trail
	  and we apply resolution
    \item we stop when only one literal from the current decision level is left in the clause
  \end{itemize}
  \vfill
  \begin{tabular}{ccc}
    \begin{minipage}{.4\textwidth}
      \scalebox{.4}{\input{ca_7.pdf_t}}
    \end{minipage}
    & ~~~~~~~~~~ &
    \begin{minipage}{.4\textwidth}
      \begin{tabular}{ccl}
	\hline
	Trail & dl & Reason \\
	\hline
	$a_{10}$   & 0 & $( a_{10} )$ \\
	$\neg a_3$ & 1 & Decision \\
	$a_7$      & 2 & Decision \\
	$\neg a_2$ & 3 & Decision \\
	$a_1$      & 4 & Decision \\
	$a_4$      & 4 & $(\neg a_{10} \vee \neg a_1 \vee a_4)$ \\
	$a_5$      & 4 & $(a_3 \vee \neg a_1 \vee a_5)$ \\
	$a_6$      & 4 & $(\neg a_4 \vee a_6)$ \\
	\hline
      \end{tabular}
      \medskip \\
      $\{ \dec{a_{10}}{0}, \dec{\neg a_3}{1}, \dec{a_7}{2}, \dec{\neg a_2}{3}, \dec{a_1}{4}, \dec{a_4}{4}, \dec{a_5}{4}, \dec{a_6}{4}\}$
    \end{minipage}
  \end{tabular}
  \vfill
  We say that $(\neg a_{10} \vee a_3 \vee \neg a_1)$ is the {\bf conflict clause} and it is the one we learn

\end{frame}

\begin{frame}
  \frametitle{Lecture 3 - Exercize 1}

  \scriptsize

  Consider the following clause set and assignment
  $$
  \begin{array}{l}
  (\neg a_1 \vee a_2) \\
  (\neg a_1 \vee a_3 \vee a_9) \\
  (\neg a_2 \vee \neg a_3 \vee a_4) \\
  (\neg a_4 \vee a_5 \vee a_{10}) \\
  (\neg a_4 \vee a_6 \vee a_{11}) \\
  (\neg a_5 \vee \neg a_6) \\
  (a_1 \vee a_7 \vee \neg a_{12}) \\
  (a_1 \vee a_8) \\
  (\neg a_7 \vee \neg a_8 \vee \neg a_{13}) \\
  \ldots \\ \\
  \{ \dec{\neg a_9}{1}, \dec{a_{12}}{2}, \dec{a_{13}}{2}, \dec{\neg a_{10}}{3}, \dec{\neg a_{11}}{3}, \ldots, \dec{a_1}{6}\}
  \end{array}
  $$
  \vfill
  \begin{itemize}
    \item Find the correct conflict clause
    \item Find the correct decision level to backtrack
  \end{itemize}

\end{frame}

\begin{frame}
  \frametitle{Lecture 3 - Exercize 1}

  \scriptsize

  \scalebox{.8}{
  \begin{tabular}{ccc}
    \begin{minipage}{.4\textwidth}
     $$
     \begin{array}{l}
     (\neg a_1 \vee a_2) \\
     (\neg a_1 \vee a_3 \vee a_9) \\
     (\neg a_2 \vee \neg a_3 \vee a_4) \\
     (\neg a_4 \vee a_5 \vee a_{10}) \\
     (\neg a_4 \vee a_6 \vee a_{11}) \\
     (\neg a_5 \vee \neg a_6) \\
     (a_1 \vee a_7 \vee \neg a_{12}) \\
     (a_1 \vee a_8) \\
     (\neg a_7 \vee \neg a_8 \vee \neg a_{13})
     \end{array}
     $$
    \end{minipage}
    & ~~~~~~ &
    \begin{minipage}{.4\textwidth}
      \begin{tabular}{ccl}
	\hline
	Trail & dl & Reason \\
	\hline
	$\neg a_{9}$  & 1 & Decision \\
	$a_{12}$      & 2 & Decision \\
	$a_{13}$      & 2 & (some clause) \\
	$\neg a_{10}$ & 3 & Decision \\
	$\neg a_{11}$ & 3 & (some clause) \\
	\ldots \\
	$a_1$         & 6 & Decision \\
	\hline
	$a_2$         & 6 & $(\neg a_1 \vee a_2)$ \\
	$a_3$         & 6 & $(\neg a_1 \vee a_3 \vee a_9)$ \\
	$a_4$         & 6 & $(\neg a_2 \vee \neg a_3 \vee a_4)$ \\
	$a_5$         & 6 & $(\neg a_4 \vee a_5 \vee a_{10})$ \\
	$a_6$         & 6 & $(\neg a_4 \vee a_6 \vee a_{11})$ \\
	\hline
      \end{tabular}
      \bigskip \\
      $\{ \dec{\neg a_9}{1}, \dec{a_{12}}{2}, \dec{a_{13}}{2}, \dec{\neg a_{10}}{3}, \dec{\neg a_{11}}{3}, \ldots, \dec{a_1}{6} \}$
    \end{minipage}
  \end{tabular}}

  \vfill
  \pause

  \scalebox{.7}{
  \begin{minipage}{\textwidth}
    \begin{prooftree}
    \AxiomC{$(\neg a_5 \vee \neg a_6)$}
    \AxiomC{$(\neg a_4 \vee a_6 \vee a_{11})$}
    \BinaryInfC{$(\neg a_5 \vee \neg a_4 \vee a_{11})$}
    \AxiomC{$(\neg a_4 \vee a_5 \vee a_{10})$}
    \BinaryInfC{$(\neg a_4 \vee a_{10} \vee a_{11})$}
    \AxiomC{$(\neg a_2 \vee a_3 \vee a_4)$}
    \BinaryInfC{$(\neg a_2 \vee \neg a_3 \vee a_{10} \vee a_{11})$}
    \AxiomC{$(\neg a_2 \vee a_3 \vee a_9)$}
    \BinaryInfC{$(\neg a_2 \vee a_9 \vee a_{10} \vee a_{11})$}
    \AxiomC{$(\neg a_1 \vee a_2)$}
    \BinaryInfC{$(\neg a_1 \vee a_9 \vee a_{10} \vee a_{11})$}
    \end{prooftree}
  \end{minipage}
  }

  \vfill
  \pause
  Conflict clause: $(\neg a_1 \vee a_9 \vee a_{10} \vee a_{11})$ \pause \\
  Backtracking level: 3

\end{frame}


\section{Notation}
\label{sec:notation-shelley}

The transition system is explained in \cite{small_step_semantics}.

\begin{description}
  \item[Powerset] Given a set $\type{X}$, $\powerset{\type{X}}$ is the set of all
    the subsets of $\type{X}$.
  \item[Sequences] Given a set $\type{X}$, $\seqof{\type{X}}$ is the set of
    sequences having elements taken from $\type{X}$. The empty sequence is
    denoted by $\epsilon$. Given a sequence $\Lambda$, $\Lambda; \type{x}$ is
    the sequence that results from appending $\type{x} \in \type{X}$ to
    $\Lambda$.
  \item[Functions] $A \to B$ denotes a \textbf{total function} from $A$ to $B$.
    Given a function $f$ we write $f~a$ for the application of $f$ to argument
    $a$.
  \item[Inverse Image] Given a function $f: A \to B$ and $b\in B$, we write
    $f^{-1}~b$ for the \textbf{inverse image} of $f$ at $b$, which is defined by
    $\{a \mid\ f a =  b\}$.
  \item[Maps and partial functions] $A \mapsto B$ denotes a \textbf{partial
    function} from $A$ to $B$, which can be seen as a map (dictionary) with
    keys in $A$ and values in $B$. Given a map $m \in A \mapsto B$, notation
    $a \mapsto b \in m$ is equivalent to $m~ a = b$.
    The $\emptyset$ symbol is also used to
    represent the empty map as well.
  \item[Map Operations] Figure~\ref{fig:notation:nonstandard}
    describes some non-standard map operations.
  \item[Relations] A relation on $A\times B$ is a subset of $A\times B$.
    Both maps and functions can be thought of as relations.
    A function $f:A\to B$ is a relation consisting of pairs $(a, f(a))$ such that $a\in A$.
    A map $m: A\mapsto B$ is a relation consisting of pairs $(a, b)$ such that
    $a\mapsto b \in m$.
    Given a relation $R$ on $A\times B$, we define the inverse relation $R^{-1}$ to be
    all pairs $(b, a)$ such that $(a, b)\in R$. Similarly, given a function $f:A\to B$ we
    define the inverse relation $f^{-1}$ to consist of all pairs $(f(a), a)$.
    Finally, given two relations $R\subseteq A\times B$ and $S\subseteq B\times C$,
    we define the compostion $R\circ S$ to be all pairs $(a, c)$ such that
    $(a, b)\in R$ and $(b, c)\in S$ for some $b\in B$.
  \item[Option type] An option type in type $A$ is denoted as $A^? = A + \Nothing$. The
    $A$ case corresponds to the case when there is a value of type $A$ and the $\Nothing$
    case corresponds to the case when there is no value.
  \item[:=] We abuse the \textbf{:=} symbol here to mean propositional equality. In the
  style of semantics we use in this formal spec, definitional equality is not needed.
  It is meant to make the spec easier to read in the sense that each time we use it,
  we use a fresh variable as shorthand notation for an expression, e.g. we write

  \[s := slot + \StabilityWindow\]

  Then, in subsequent expressions, it is more convenient to write simply $s$.
  It is not meant to shadow variables, and if it does, there is likely a problem with the
  rules that must be addressed.
\end{description}

In Figure~\ref{fig:notation:nonstandard}, we specify the notation that we use in
the rest of the document.

\begin{figure}[htb]
  \begin{align*}
    \var{set} \restrictdom \var{map}
    & = \{ k \mapsto v \mid k \mapsto v \in \var{map}, ~ k \in \var{set} \}
    & \text{domain restriction}
    \\
    \var{set} \subtractdom \var{map}
    & = \{ k \mapsto v \mid k \mapsto v \in \var{map}, ~ k \notin \var{set} \}
    & \text{domain exclusion}
    \\
    \var{map} \restrictrange \var{set}
    & = \{ k \mapsto v \mid k \mapsto v \in \var{map}, ~ v \in \var{set} \}
    & \text{range restriction}
    \\
    \var{map} \subtractrange \var{set}
    & = \{ k \mapsto v \mid k \mapsto v \in \var{map}, ~ v \notin \var{set} \}
    & \text{range exclusion}
    \\
    A \triangle B
    & = (A \setminus B) \cup (B \setminus A)
    & \text{symmetric difference}
    \\
    M \unionoverrideRight N
    & = (\dom N \subtractdom M)\cup N
    & \text{union override right}
    \\
    M \unionoverrideLeft N
    & = M \cup (\dom M \subtractdom N)
    & \text{union override left}
    \\
    M \unionoverridePlus N
    & = (M \triangle N)
    \cup \{k\mapsto v_1+v_2\mid {k\mapsto v_1}\in M \land {k\mapsto v_2}\in N \}
    & \text{union override plus} \\
    & & \text{(for monoidal values)}\\
  \end{align*}
  \caption{Non-standard map operators}
  \label{fig:notation:nonstandard}
\end{figure}

\clearpage


\documentclass{llncs}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{comment}
\usepackage{hyperref}
\usepackage{longtable}
\usepackage{stmaryrd}
\newcommand{\interp}[1]{\llbracket #1 \rrbracket}
\newcommand{\maps}{\colon}
\newcommand{\FinSet}{\mathrm{FinSet}}
\newcommand{\Set}{\mathrm{Set}}
\newcommand{\Cat}{\mathrm{Cat}}
\newcommand{\Calc}{\mathrm{Calc}}
\newcommand{\Mon}{\mathrm{Mon}}
\newcommand{\BoolAlg}{\mathrm{BoolAlg}}
\renewcommand{\Form}{\mathrm{Form}}
\newcommand{\leftu}{\mathrm{left}}
\newcommand{\rightu}{\mathrm{right}}
\newcommand{\send}{\mathrm{send}}
\newcommand{\recv}{\mathrm{recv}}
\newcommand{\comm}{\mathrm{comm}}
\renewcommand{\quote}[1]{``#1"}
\newcommand{\deref}[1]{\mathrm{eval}(#1)}
\newcommand{\op}{\mathrm{op}}
\newcommand{\NN}{\mathbb{N}}
\makeatletter
\gdef\tshortstack{\@ifnextchar[\@tshortstack{\@tshortstack[c]}}
\gdef\@tshortstack[#1]{%
  \leavevmode
  \vtop\bgroup
    \baselineskip-\p@\lineskip 3\p@
    \let\mb@l\hss\let\mb@r\hss
    \expandafter\let\csname mb@#1\endcsname\relax
    \let\\\@stackcr
    \@ishortstack}
\makeatother

\title{Logic as a distributive law}
\author{
Michael Stay\inst{1}\\
\and
L.G. Meredith\inst{2}\\
}
\institute{
  {Pyrofex Corp.}\\
  \email{\fontsize{8}{8}\selectfont stay@pyrofex.net}\\
  \and
  {Synereo, Ltd}\\
  \email{\fontsize{8}{8}\selectfont greg@synereo.com}
}
\begin{document}
\maketitle
\begin{abstract}
\noindent
  We present an algorithm for deriving a spatial-behavioral type
  system from a formal presentation of a computational calculus.
  Given a 2-monad $\Calc\maps \Cat \to \Cat$ for the free calculus on
  a category of terms and rewrites and a 2-monad BoolAlg for the free
  Boolean algebra on a category, we get a 2-monad Form = BoolAlg +
  Calc for the free category of formulae and proofs.  We also get the
  2-monad $\BoolAlg \circ \Calc$ for subsets of terms.

  The interpretation of formulae is a natural transformation
  $\interp{-} \maps \Form \Rightarrow \BoolAlg \circ \Calc$ defined by
  the units and multiplications of the monads and a distributive law
  transformation $\delta\maps \Calc \circ \BoolAlg \Rightarrow
  \BoolAlg \circ \Calc.$  This interpretation is consistent both with
  the Curry-Howard isomorphism and with realizability.

  We give an implementation of the ``possibly'' modal operator
  parametrized by a two-hole term context and show that, surprisingly,
  the arrow type constructor in the $\lambda$-calculus is a specific
  case.  We also exhibit nontrivial formulae encoding confinement and
  liveness properties for a reflective higher-order variant of the
  $\pi$-calculus.

\end{abstract}
\section{Introduction}

  Sylvester coined the term ``universal algebra'' to describe the idea of expressing a mathematical structure as set equipped with functions satisfying equations; the idea itself was first developed by Hamilton and de Morgan \cite{Graetzer}.  Most modern programming languages have a notion of a ``module'' or an ``interface'' with this same information; for example, consider this Agda definition of a monoid.
  \begin{verbatim}
    data Monoid (M : Set)(Eq : Equivalence M) : Set where
      monoid :
        (e   : M)
        (_*_ : M -> M -> M)
        (leftId : LeftIdentity Eq z _+_)
        (rightId : RightIdentity Eq z _+_)
        (assoc : Associative Eq _+_) ->
           Monoid M Eq
  \end{verbatim}
  The code defines a sort $M$, a nullary term constructor $e$, and a
  binary term constructor $*,$ subject to three equations. In
  universal algebra, such a structure is called an ``equational
  theory''.

  In 1963, Lawvere \cite{Lawvere} showed that an equational theory was
  a presentation of a category with finite products where all the
  objects are powers of a single generating object; such a category is
  now called a Lawvere theory.  The Agda code above is a presentation
  of a category Th(Mon) whose objects are $1, M, M^2, M^3, \ldots,$
  and whose morphisms are generated by projections, $e,$ and $*,$
  modulo the equations.  This category is purely syntactic.

  To interpret the equational theory as denoting sets and functions,
  we use a product-preserving functor from the Lawvere theory to Set:
  such a functor maps $M$ to a set and $e$ and $*$ to functions
  satisfying the equations.  The category Set encodes what we mean by
  semantics; if instead of Set we used the category Vect of vector
  spaces and linear maps and mapped the product in Th(Mon) to the
  tensor product in Vect, a model of the theory would be an
  associative algebra instead.  If we used a category of endofunctors
  and natural transformations and mapped the product in Th(Mon) to
  composition, a model of the theory would be a monad.

  The category of product-preserving functors from Th(Mon) to Set and
  natural transformations between them is equivalent to the category
  Mon of monoids and monoid homomorphisms.  There is a forgetful
  functor $U\maps \Mon \to \Set$ that forgets all the structure with a
  left adjoint $L\maps \Set \to \Mon$ that picks out the free monoid
  on a set.  Abusing notation somewhat, we use Mon also to mean the
  monad $UL$ that picks out the underlying set of the free monoid on a
  set.  This pattern is a general one: every Lawvere theory
  corresponds to a finitary monad on Set and vice-versa
  \cite{DBLP:journals/entcs/HylandP07}.

  Often modules or interfaces are used to model data structures.  In
  computing, however, we consider not only the structure of data but
  the behavior of processes that change the data.  The
  $\lambda$-calculus is the paradigmatic model of functional
  programming.  It has a single data type, called lambda term,
  described by a one-line grammar.  In this model, computation
  consists of repeatedly applying a single rewrite rule called
  $\beta$-reduction; the rewrite rule matches the current term to a
  pattern, then rearranges the parts of the term.  When the term no
  longer matches the pattern, the rewriting stops and the resulting
  term is taken as the ``answer''.  Similarly, the $\pi$-calculus,
  arguably the paradigmatic model of concurrent programming, has a
  structured data type called a process that is also described by a
  simple grammar.  Computations in the $\pi$-calculus are carried out
  by repeatedly applying rewrite rules, primarily the
  $\mathsf{comm}$-rule.

  This pattern has been recognized and formalized many times.  In his
  seminal paper, ``Functions as Processes''
  \cite{DBLP:journals/mscs/Milner92}, Milner developed what is now the
  standard presentation of a computational calculus.  The presentation
  consists of a grammar describing the primary data type over which
  computations are carried out, a structural equivalence, used to
  erase syntactic differences that are irrelevant to computation, and
  a set of rewrite rules describing how to realize computation through
  operations on the data structures.  On one hand, this generalizes
  universal algebra's generators and relations presentation, with the
  grammar replacing the generators as the free construction, the
  structural equivalence replacing the relations, and the rewrites
  providing the computational semantics.  On the other hand, this
  presentation maps quite well onto Plotkin's SOS format for
  operational semantics \cite{Plotkin04theorigins}, but the scope is
  much larger.  Even the presentation of the Java VM can be seen as
  essentially a rewrite system with the state of the virtual machine
  as instances of a data type over which computation is carried out,
  and the transitions of the virtual machine as the rewrite rules
  \cite{DBLP:conf/oopsla/IgarashiPW99}.  Capturing this higher-order
  computational phenomena, expressed as generalized rewrites, is what
  motivates our movement to higher categorical semantics.

  Lawvere theories can be generalized from  1-categories to 2-categories.  As one might expect, the corresponding notion of equational theory involves one more level of structure; we have sorts, term constructors, {\em rewrites} instead of equations between term constructors, and equations between rewrites.  We typically interpret such a theory in Cat, the  2-category of categories, functors and natural transformations.

  We can use Lawvere 2-theories to describe terms in a computational calculus and the permissible rewrites.  We have a sort for terms;  our examples are necessarily simple calculi due to space constraints, but our approach should work with any formalization of a computational model such as the K framework \cite{DBLP:journals/jlp/RosuS10}, the  ``kiama'' Scala package \cite{DBLP:conf/gttse/Sloane09}, even packages largely focused on documentation and specification, such as Ott  \cite{DBLP:journals/jfp/SewellNOPRSS10}.  We also have term constructors for building up specific terms, rewrites between term constructors for running programs, and equations between rewrites to preserve invariants.  The Lawvere 2-theory captures the syntax of a computational calculus; a 2-functor from the theory into Cat assigns operational semantics.  Note that this is fully consistent with Curry-Howard style interpretations.

  A spatial-behavioral type system is a language in which one can describe both the structure and future behavior of terms.  In keeping with realizability style semantics  \cite{Krivine-TheCurryHowardCorre}, the interpretation of a formula should be a set of terms satisfying the formula.  The interpretation of a proof should be a function that maps a set of assumptions to a set of consequents.

  The obvious language for describing the structure of a term is the 2-theory Th(Calc) of the calculus itself, equipped with extra term constructors for true, false, disjunction, conjunction, and negation.  This suggests adding the 2-monad Calc to the 2-monad BoolAlg for Boolean algebras to get Form, the 2-monad for formulae.

  The obvious language for interpreting formulae is the 2-category of subsets of terms with pointwise rewrites between them.  This suggests composing the monad BoolAlg with the monad Calc.

  The obvious way to interpret formulae is then a natural transformation $\interp{-} \maps \Form \Rightarrow \BoolAlg \circ \Calc$ defined by the units and multiplications of the monads and a distributive law transformation $\delta\maps \Calc \circ \BoolAlg \Rightarrow \BoolAlg \circ \Calc.$

  We can extend this naive type system with fixed points and with modal operators that denote sets of terms that all exhibit a particular behavior; for an example of the latter, the arrow type $A \Rightarrow B$ in the $\lambda$-calculus is the type of terms that when applied to a term of type $A$ eventually rewrite to a term of type $B$.  Other formulae can capture eventual properties like
\begin{itemize}
  \item Authority: it is the case that if my banking service eventually evolves to a state in which Alice has the ability to withdraw money from my account, then the banking service both sent an email to my address requesting confirmation and received the confirmation email in response.
  \item Confinement: this process will only ever send a messages on channels in this set.
  \item Liveness: this process will always eventually handle another message.
  \item Termination: this computation will always halt.
  \item Structure: at the end of sorting, the data will be ordered.
\end{itemize}

The slogan ``logic via distributive law'' is more apt than might first
be supposed, however. At heart, the algorithm says that logical
formula can be thought of as describing collections of terms in terms
of terms built over collections. Since a very broad class of
collections are captured via the notion of monad, the distributive
law, together with the monad laws are iteratively applied to instances
of the latter to arrive at instances of the former. In this sense
$\BoolAlg$ is just one notion of ``collection'', namely that of
$\Set$. There are other notions of collection, such as list, bag,
tree, all of which are captured as monads and for which we could use a
distributive law to provide a semantics for a notion of formula that
has utility and considerable expressive power. Thus, for example, if
the notion of collection were \emph{sets of sequences}, then the
semantics generated by our algorithm would interpret formulae as sets
of sequences of terms, instead of sets of terms, and the term language
extracted for the collection monad would be the familiar operators of
classical linear logic and this would correspond to the well-known
fact that quantales provide full and faithfull models of the
\emph{formulae} of linear logic. The higher categorical machinery
provides the right setting in which to express this idea in it's full
generality. In this paper, however, we focus the exegesis of these
results on $\BoolAlg$ to conserve space, and highlight the core ideas.

\section{Related work}

There is a very large body of work on Lawvere theories and their
generalizations, and we cannot give a proper review here; instead we
list a few key papers with many references.  Hyland and Power
\cite{DBLP:journals/entcs/HylandP07} reviewed the history of both
Lawvere theories and monads as an approach to universal algebra, how
monads came to the fore, Plotkin's work, and the renewed interest in
Lawvere theories in a computer science setting.  Trimble
\cite{Trimble} constructed multisorted Lawvere theories.  Lack and
Rosick\'y \cite{DBLP:journals/acs/LackR11} unified the work of several
earlier authors by considering models in categories other than Set,
extending the notion of Lawvere theory to enriched categories, and
using all limits rather than just products all at once.

Our results can be seen as a natural consequence of Abramsky's
programme, laid out in ``Domain theory in Logical Form''
\cite{DBLP:journals/apal/Abramsky91}, and expanded in ``Proofs as
Processes'' \cite{Abramsky:1992:PP:194588.194591}.  Moreover, they can
be seen as bringing the seminal work of Caires \cite{Caires} under the
rubric of that programme.

\section{A 1-categorical example}

Here we return in slightly greater detail to the example of the logic for the language of monoids.  Because we are working with categories instead of 2-categories, the ``virtual machine'' is particularly bland; it only has terms (the elements of the monoid), not rewrites.

Let FinSet be a skeleton of the category of finite sets.  The Lawvere
theory of a computational calculus is a category Th(Calc) with finite
products equipped with an identity-on-objects functor $\theta\maps
\FinSet^\op \to \mathrm{Th(Calc)}.$  Because the objects of FinSet are
coproducts of the one-element set, the objects of category Th(Calc)
are therefore products of a generating object we will write as $S,$
for ``sort''.  The morphisms of Th(Calc) are generated from a set of
morphisms from finite powers of $S$ to $S$ by products and
composition, so Th(Calc) may be presented by

\begin{itemize}
  \item a sort $S$,
  \item a set of term constructors $f_i\maps S^{n_i} \to S,$ where $i$ ranges over some index set $I$ and $n_i \in \NN,$
  \item and a set of equations between term constructors.
\end{itemize}
We say the arity of $f_i$ is $n_i.$

A product-preserving functor from Th(Calc) to Set picks out a set and equips it with structure maps satisfying the equations.  The category Prod(Th(Calc), Set) of product-preserving functors and natural transformations between them is equivalent to the category Calc of calculi and calculus homomorphisms.  There is a forgetful functor ${U\maps \Calc \to \Set}$ that forgets the extra structure.  The functor $U$ has a left adjoint ${L\maps \Set \to \Calc}$ that picks out the free calculus on a set of base terms.  The monad $\Calc = UL\maps \Set \to \Set$ picks out the underlying set $ULX$ of the free calculus $LX$ on a set $X$.  Lawvere theories are in bijection with finitary monads; the qualifier ``finitary'' means that each term constructor has a finite arity.

Here is a presentation of the ``Lawvere theory of a monoid'' Th(Mon):
\begin{center}
  \begin{longtable}{|p{0.3\linewidth}|p{0.7\linewidth}|}
    \hline
      Sorts:
      \begin{itemize}
        \item $S$
      \end{itemize}\bigskip
      Term constructors:
      \begin{itemize}
        \item $\cdot\maps S^2 \to S$
        \item $e\maps 1 \to S$
      \end{itemize}
    &
      Equations:
      \begin{itemize}
        \item $\cdot \circ (S \times \cdot) = \cdot \circ (\cdot \times S)$ (associativity)
        \item $\cdot \circ (e \times S) \circ \leftu^{-1} = S$ (left unit)
        \item $\cdot \circ (S \times e) \circ \rightu^{-1} = S$ (right unit)
      \end{itemize}\\
    \hline
  \end{longtable}
\end{center}
where ${\leftu\maps 1 \times S \stackrel{\sim}{\to} S}$ and ${\rightu\maps S \times 1 \stackrel{\sim}{\to} S}$ are the canonical isomorphisms.

An implementation of this specification is a product-preserving functor from Th(Mon) to Set; such a functor will assign a set of values to the sort and functions to the term constructors such that the equations are satisfied, {\em i.e.} it will pick out a monoid.  The category of product-preserving functors from Th(Mon) to Set and natural transformations between them is equivalent to the category of monoids and monoid homomorphisms.  There is a forgetful functor $U\maps \Mon \to \Set$ that forgets the multiplication and unit, and outputs the underlying set of elements.  The functor $U$ has a left adjoint $L\maps \Set \to \Mon$ that outputs the free monoid on a set.  The composite functor $\Mon = UL\maps \Set \to \Set$ is the corresponding monad.

All our formulae about monoids will denote subsets of the elements of the monoid; we use Th(BoolAlg) to describe them.  \begin{center}
  \begin{longtable}{|p{0.3\linewidth}|p{0.7\linewidth}|}
    \hline
    Sorts:
    \begin{itemize}
      \item $S$
    \end{itemize}
    Term constructors:
    \begin{itemize}
      \item $\land, \lor\maps S^2 \to S$
      \item $\top, \bot\maps 1 \to S$
      \item $\neg\maps S \to S$
    \end{itemize}
    &
    Equations:
    \begin{itemize}
      \item \raggedright associativity, commutativity and unit laws for $\land$ and $\lor$
      \item distributivity of $\land$ over $\lor$
      \item involution for $\neg$
      \item de Morgan's laws
    \end{itemize}\\
    \hline
  \end{longtable}
\end{center}
From this theory, we can derive the monad BoolAlg for the free Boolean algebra on a set.

The sum of the two theories above is a new theory Th(Form) = Th(Mon) + Th(BoolAlg) whose terms are our formulae.  Th(Form) is presented by identifying the sorts and taking the union of the term constructors and the union of the equations.
\begin{comment}
  \begin{longtable}{|p{0.3\linewidth}|p{0.7\linewidth}|}
    \hline
    Sorts:
    \begin{itemize}
      \item $S$
    \end{itemize}
    Term constructors:
    \begin{itemize}
      \item $\cdot\maps S^2 \to S$
      \item $e\maps 1 \to S$
      \item $\land, \lor\maps S^2 \to S$
      \item $\top, \bot\maps 1 \to S$
      \item $\neg\maps S \to S$
    \end{itemize}
    &
    Equations:
    \begin{itemize}
      \item associativity and unit laws for $\cdot$
      \item \raggedright associativity, commutativity, and unit laws for $\land$ and $\lor$
      \item involution for $\neg$
      \item de Morgan's laws
    \end{itemize}\\
    \hline
  \end{longtable}
\end{comment}

The process of deriving a monad from a theory preserves sums.  Since the sum of two monads is the free product of the two, a general formula will be a term in an alternating composition of the two monads.  For example, suppose that $a, b, c, d \in X;$ then one formula is
\[ (({a}\lor{(b \cdot d)}) \cdot ({c}\lor{d})), \]
which is a term in Mon(BoolAlg(Mon($X$))).  The interpretation of this formula should be the set of monoid elements
\[ \{ ac, ad, bdc, bdd \}, \]
or in other words, the term
\[ (a \cdot c) \lor (a \cdot d) \lor ((b \cdot d) \cdot c) \lor ((b \cdot d) \cdot d) \]
in BoolAlg(Mon(X)).

In order to move all the uses of Mon to the right of the uses of BoolAlg in the alternating composition, we need a distributive law natural transformation
\[ \delta\maps \Mon \circ \BoolAlg \Rightarrow \BoolAlg \circ \Mon. \]
Given $\delta$ and the monad units and multiplications, we can define an interpretation natural transformation
\[ \interp{-}\maps \Form\Rightarrow \BoolAlg \circ \Mon \]
in the obvious way.  Below, we write subsets of Mon($X$) using set notation:
\begin{center}
  \begin{longtable}{|rl|rl|}
    \hline
    $\interp{\top}_X$ &= $\Mon(X)$ &
    $\interp{\bot}_X$ &= $\emptyset$ \\
    $\interp{{A}\lor{B}}_X$ &= $\interp{A}_X \cup \interp{B}_X$ &
    $\interp{{A}\land{B}}_X$ &= $\interp{A}_X \cap \interp{B}_X$\\
    $\interp{\neg A}_X$ &= $\Mon(X) - \interp{A}_X$ &
    $\interp{{A} \cdot {B}}_X$ &= $\Mon(\cdot)(\interp{A}_X \times \interp{B}_X)$\\
    $\interp{e}_X$ &= $\{ e\}$ &
    $\interp{x \in X}_X$ &= $\{ x\}$\\
    \hline
  \end{longtable}
\end{center}

Even in this simple example, we have nontrivial formulae; for example,
\[ prime = \neg e \land \neg(\neg e \cdot \neg e) \]
is a 1-line formula for primality.  For the monoid of natural numbers under multiplication, this formula says a number is prime if it is neither 1 nor has a nontrivial factorization.  It is easy to verify that $\interp{prime}_X = X.$

\section{Moving to 2-categories}

In a computational context, when a data structure has some symmetry we do not care about, we often test two instances of the structure for equality by computing a normal form for each instance and then comparing the normal forms.  Monoids are associative, but we can imagine storing words of a monoid as binary trees internally and then comparing them by computing a normal form.  For the case of a normal form for the trees, we can eliminate 1 in a product by rewriting $(1 \cdot x)$ and $(x \cdot 1)$ to $x$, and we can shift all the parentheses to the right by rewriting $((x \cdot y) \cdot z)$ to $(x \cdot (y \cdot z)).$

The Lawvere 2-theory Th(Mon) is much the same as the 1-theory, except for the weakening of some equations to rewrites and the addition of new equations between the rewrites.

\begin{center}
  \begin{longtable}{|p{0.3\linewidth}|p{0.7\linewidth}|}
    \hline
    Sorts:
    \begin{itemize}
      \item $S$
    \end{itemize}
    Term constructors:
    \begin{itemize}
      \item $\cdot\maps S^2 \to S$
      \item $e\maps 1 \to S$
    \end{itemize}
    &
    Rewrites:
    \begin{itemize}
      \item $a\maps \cdot \circ (S \times \cdot) \Rightarrow \cdot \circ (\cdot \times S)$
      \item $l\maps \cdot \circ (e \times S) \circ \leftu^{-1} \Rightarrow S$
      \item $r\maps \cdot \circ (S \times e) \circ \rightu^{-1} \Rightarrow S$
    \end{itemize}
    Equations:
    \begin{itemize}
      \item $a \circ a = (S \times a) \circ a \circ (a \times S)$ (pentagon equation)
      \item $r \times S = (S \times l) \circ a$ (triangle equation)
    \end{itemize}\\
    \hline
  \end{longtable}
\end{center}
From Th(Mon) we can derive a 2-monad Mon that essentially produces the free monoid on a set, but keeps track of the internal representation of the monoid---a binary tree---and accounts for the work needed to convert the internal representation to its normal form.  From this perspective, we can think of the trees as programs for a simple computational calculus and the process of normalization as the execution of the program.  Later in the paper, we will examine the case of the SKI combinator calculus, a Turing-complete programming language that was a predecessor to the $\lambda$-calculus; it, too, uses binary trees as programs, and execution of the program is a normalization process.

When we add the 2-monad BoolAlg to Mon, we get formulae like $(1 \cdot
\top)$ denoting the set of trees whose leftmost child is the identity;
because Mon is a 2-monad, we also get formula rewrites like
\[ ((r \circ l) \lor y)\maps ((e \cdot (x \cdot e)) \lor y) \to (x \lor y) \]
whose interpretations are homomorphisms of Boolean algebras. Note that
given a term rewrite like $f \maps x \to y$ we get a formula rewrite
$\neg f \maps \neg x \to \neg y$. While $\top, \bot, \lor, \land$ have
pointwise interpretations, $\neg$ is purely formal, i.e. the rewrite
substitutes $\neg y$ for $\neg x$ in a formula.

The formulae in these examples have been propositions about the structure of terms.  Later in the paper, we will also show how to add modal operators that are propositions about the behavior of terms; the arrow type constructor from the $\lambda$-calculus is a prominent example.

\section{Multisorted Lawvere theories}
In the motivation section, each theory had only one sort; practical theories for virtual machines are usually multisorted.  Given a finite set of sorts $\Sigma,$ the category $\FinSet/\Sigma$ is the category whose objects are pairs $(S, s\maps S\to \Sigma),$ where $S$ is a finite set, and whose morphisms are functions $f\maps S \to S'$ such that the relevant triangle commutes.

A multisorted Lawvere theory is a category Th(Calc) with finite products equipped with an identity-on-objects functor ${\theta\maps (\FinSet/\Sigma)^\op \to \mathrm{Th(Calc)}.}$  The category Prod(Th(Calc), $\Set^\Sigma$) is equivalent to the category Calc of calculi and calculus homomorphisms.  There is a forgetful functor ${U\maps \Calc \to \Set^\Sigma}$ with a left adjoint ${L\maps \Set^\Sigma \to \Calc}$ that picks out the free calculus on a $|\Sigma|$-tuple of sets.  The monad ${UL\maps \Set^\Sigma \to \Set^\Sigma}$ picks out the underlying $|\Sigma|$-tuple of sets $ULX$ of the free calculus $LX$ on a $|\Sigma|$-tuple of sets $X$.

An example of a multisorted Lawvere theory is that of a group action on a set, which involves a choice of both a group $G$ and a set $V$ to act on.  The presentation of Th(GrpAct) has a pair of sorts $(G, V)$, all the term constructors and equations as the theory of a group (where we replace $S$ by $G$), together with a new term constructor
\begin{itemize}
  \item $a\maps G \times V \to V$
\end{itemize}
and equations
\begin{itemize}
  \item $a \circ (e \times V) \circ \leftu^{-1}_V = V$ (identity action)
  \item $a \circ (m \times V) = a \circ (G \times a)$ (compatibility).
\end{itemize}

Another example is the theory of a directed graph, with one sort for vertices and another for edges, with source and target maps for term constructors.

\section {Lawvere 2-theories}
In this paper, the multisorted Lawvere 2-theory of a calculus is a 2-category Th(Calc) with strict finite products (that is, its underlying category has products) equipped with an identity-on-objects functor $\theta\maps (\FinSet/\Sigma)^\op \to \mathrm{Th(Calc)},$ where we promote $(\FinSet/\Sigma)$ to a 2-category by adding identity 2-morphisms to every 1-morphism.  As noted in the related work section, other authors have considered much more powerful notions of 2-theory, but we will, for the most part, not need the extra features; however, see the notion of ``lambda theory'' below.  Our notion of a multisorted Lawvere 2-theory may be presented by a finite set of sorts, a set of term constructors with finite arity, a set of rewrites, and a set of equations between rewrites.

Our models of multisorted Lawvere 2-theories are functors into $\Cat^\Sigma$ that preserve products up to isomorphism, not merely up to equivalence; that is, the 2-functor has an underlying functor that preserves products.  As with 1-theories, the 2-category of product-preserving functors from Th(Calc) to $\Cat^\Sigma$, natural transformations, and modifications is equivalent to the 2-category of calculi, calculus homomorphisms, and calculus transformations.

As mentioned above, the SKI combinator calculus is a Turing-complete language; it was invented by Sch\"onfinkel \cite{Schonfinkel} and Curry \cite{Curry} in the 1920s as a way to clarify the role of quantified variables in logic, essentially by eliminating them.  The single-sorted Lawvere 2-theory Th(SKI) has a presentation
\begin{center}
  \begin{longtable}{|p{0.3\linewidth}|p{0.7\linewidth}|}
    \hline
    Sorts:
    \begin{itemize}
      \item $T$
    \end{itemize}
    Term constructors:
    \begin{itemize}
      \item $S, K, I\maps 1 \to T$
      \item $(-\;-)\maps T^2 \to T,$
    \end{itemize}
    &
    Rewrites:
    \begin{itemize}
      \item $\forall x,y,z \in T, \quad \sigma \maps (((S\; x)\; y)\; z) \Rightarrow ((x\; z)\; (y\; z))$
      \item $\forall y,z \in T, \quad \kappa \maps ((K\; y)\; z) \Rightarrow y$
      \item $\forall z \in T, \quad \iota \maps (I\; z) \Rightarrow z$
    \end{itemize}
    No equations.\\
    \hline
  \end{longtable}
\end{center}
In this context, the Church-Rosser theorem for the SKI calculus says that any two terminating rewrites out of an SKI term have the same codomain.  We do not usually want to impose equality on the rewrites, since they can differ greatly in computational complexity.  For example, suppose that we have the term $((K\; I)\; x),$ where $x$ is some term that takes a long time to reduce to its normal form; a rewrite that reduces $x$ first and then uses $\kappa$ takes much longer than just doing $\kappa$ first, though both rewrites begin and end at the same term.

The free model of Th(SKI) on a category takes its objects as terms and its morphisms as rewrites, then freely adjoins $S,K,$ and $I$ and all applications of one object to another, as well as new morphisms generated by $\sigma,\kappa,$ and $\iota.$  The free model on the empty category will contain only terms and rewrites from the SKI calculus.

Just as Lawvere theories capture the notion of a set equipped with functions satisfying equations, 2-Lawvere theories capture the notion of a category equipped with functors and natural transformations satisfying equations.  The former describes what might be called data structures, while the latter captures both the state of a system and the processes that update that state.  In the next section, we introduce a language for talking about sets of states and updates that satisfy some criterion, terms and rewrites that share some property.

\section{Categories of formulae and proofs}

Any 1-theory can be promoted to a 2-theory by turning each equation into a rewrite, then adding an equation asserting that the rewrite is equal to the identity rewrite.  Therefore as before, given a single-sorted Lawvere 2-theory Th(Calc), we get a 2-theory of formulae Th(Form) by adding the 2-theory of Boolean algebras Th(BoolAlg).  The models of Th(Form) are categories of formulae and proofs.

For multisorted Lawvere 1- and 2-theories, there is no canonical choice of sorts to identify between the theory of the calculus and the theory of Boolean algebras; one may choose to add one or more different copies of Th(BoolAlg) and identify each single sort with different sorts in the theory of the calculus.  For example, given the theory of a group action, one could add a copy of Th(BoolAlg) for both $G$ and $V$ and write formulae involving subsets of the group and of the set it acts on.  Later in the paper, we will see how we can use formulae involving names to create namespaces that enforce confinement on processes in the $\pi$-calculus.

\section{Interpretation}

When describing the semantics of a logic, questions of soundness and
completeness are natural.  The whole point of our approach is that it
is correct by construction.  Soundness and completeness for the
modal-free fragment follow directly from the definitions; soundness,
in particular, is a direct consequence of the way realizability is
incorporated into the approach.  For the formulae, the intuitions
underlying the completeness argument amount to the fact that the term
language for the logical operators associated with the collections are
extractions of a term language for the collection monad.  Thus, they
are in perfect correspondence with the model.  In the case of $\Set$,
this is equivalent to the well-known fact that boolean algebras are
effectively realized by the powerset lattice.

Likewise, in the case of $\Set$, the semantics that maps formulae to
the composition of the collection monad with the monad for the term
language supporting computation is nothing more than a pointwise
lifting of the powerset semantics.  The categorical presentation
provides an appropriately abstracted form of this simple
observation. In particular, interpretation is a natural transformation
between 2-monads on Cat; it assigns to each category $X$ a functor
\[\interp{-}_X\maps (\BoolAlg + \Calc)(X) \to (\BoolAlg \circ \Calc)(X).\]
Functoriality means that the interpretation is sound; if the functor is full, the interpretation is complete. Since we define interpretation in terms of the monad laws and BoolAlg + Calc is the free product on the two monads, $\interp{-}_X$ is the identity when restricted to $(\BoolAlg \circ \Calc)(X)$ and thus full.

Notice we have said nothing about the faithfulness of this functor. In
particular, if we consider rewrites as equalities then proofs (and
therefore homsets) collapse, recovering the usual situation of posets as models
of classical logic. On the other hand, when they are not treated as
equalities, we are able to distinguish proofs, even in the classical
setting. There is much, much more to say about this and other
phenomena associated with Joyal's lemma \cite{0910.2401}, but we defer the discussion
to another paper.

\section{Modal operators}

So far, all the examples of formulae have dealt with the structure of the term.  Far more interesting are sets of terms that all share some behavior.  For example, in the SKI calculus, we want to add the idea of an arrow type to our formulae, and have the interpretation of $A \Rightarrow B$ be the subset of terms $t$ such that given a term $u\in\interp{A}$, the term $(t\; u)$ eventually evolves to a term $v\in\interp{B}.$

This notion of eventuality is an example of a modal operator.  Possibility is another, where possibility is to eventuality as ``there exists'' is to ``for all''.  In the SKI calculus, they coincide due to the fact that the calculus is confluent, but they differ in the general case.  Many interesting properties can be stated in terms of eventual and possible states as noted earlier.

To add a modal operator to a formula language, we first add it formally as a term constructor to the term theory, then add the collection theory as before.

To interpret the modal operator, we first interpret the formulae as above, then interpret modal terms as collections of terms, then use the join from the collection monad.

\subsection{Example: Arrow types in the SKI calculus}

The Lawvere 2-theory of the arrow type Th(Arrow) has one term constructor, no rewrites, and no equations.  The lack of rewrites and equations are because the arrow is a purely formal type, and it is only in our choice of semantics that it acquires its customary interpretation.

In order to avoid notational confusion between the arrow type constructor and 2-morphisms, we will use a triple-arrow in the theory.
\begin{center}
  \begin{longtable}{|p{0.3\linewidth}|p{0.7\linewidth}|}
    \hline
    Sorts:
    \begin{itemize}
      \item $T$
    \end{itemize}
    Term constructors:
    \begin{itemize}
      \item $\Rrightarrow \maps T^2 \to T$
    \end{itemize}
    &
    No rewrites; no equations.\\
    \hline
  \end{longtable}
\end{center}

We get the theory of SKI with arrow Th(SKIArr) by adding Th(SKI) to Th(Arrow), and we get the theory of formulae Th(Form) by adding Th(SKIArr) to Th(BoolAlg).

To interpret terms from Th(Form), we compose the interpretation natural transformation above with another natural transformation $\alpha\maps \mathrm{SKIArr} \Rightarrow \mathrm{BoolAlg} \circ \mathrm{SKI},$ followed by the join from the collection monad.  In particular,
\[ \alpha(u \Rrightarrow v) = \{ t \;|\; \exists \rho\maps (t\; u) \Rightarrow v \}. \]

In Lambek's 1980 paper \cite{Lambek} on the denotational semantics of the $\lambda$-calculus, he defined a category whose objects were types and whose morphisms were equivalence classes of lambda terms with one free variable.  In a future paper, we will show how Mellies and Zeilberger's approach to type refinement lets us recapitulate Lambek's construction and extend the arrow to a profunctor.

\subsection{Modal operators parametric in a term constructor}

A term context is a term with a ``hole'' that can be filled by some other term.  Given a Lawvere theory with a sort $S$ for terms, one can derive a new theory of term contexts by replacing each term constructor $f\maps S^n \to S$ with $n$ term constructors $f_i\maps S^{n-1} \to S$ where $1 \le i \le n$.  We think of $f_i$ as being $f$ with the $i$th input being a hole.  Equivalently, if we have a theory with coproducts, we can replace each occurrence of $S$ on the left-hand side of a term constructor with $S+1,$ where the new point represents the hole.

The arrow type is a special case of a more general modal operator parametrized by a two-hole term context $C.$  We denote the operator itself with angle brackets as a reminder of the diamond ``possibly'' modality: $u \langle C \rangle v.$  The interpretation is similar to that of the arrow:
\[ \interp{u \langle C\rangle v} = \{ t \;|\; \exists \rho\maps C[t, u] \Rightarrow v \}; \]
that is, $u \langle C\rangle v$ denotes those terms $t$ that when put
in the context $u$ may possibly evolve to $v.$

This definition is inspired by the work of Sewell, Leifer, and Milner, in which they formalize the notion of a context-labelled transition \cite{DBLP:conf/concur/LeiferM00}.  Given the now standard Hennessy-Milner interpretation of modal logics which interpret possibility in terms of actions labelling transitions, when Sewell {\em et al.} gave a notion of context-labelled transitions, it was only natural
to consider modal operators based on contexts.

\section{Recursion}

Suppose that we have a single-sorted Lawvere 2-theory of a calculus with sort $S$.  We add recursion to our formulae by introducing a new sort $V$ for type variables and new term constructors
\begin{itemize}
  \item $- \maps V \to S$ to let us use type variables in formulae and
  \item $\mu\maps V\times S \to S$ to express fixed points.
\end{itemize}

We interpret terms of the form $\mu X.P[X]$ as the greatest fixed point of $P[X].$

\subsection{The reflective higher-order $\pi$-calculus}

Lawvere theories are limited in that they only talk about products of sorts.  A lambda theory is a generalization of a Lawvere theory that has the ability to talk about function sorts like $A \Rightarrow B$ and sums like $A+B$ or $1 + A + A^2 + \cdots,$ more commonly denoted with the Kleene star $A^*.$  One can think of lambda theories as having access to a ``library'' that takes care of the details of bound variables and substitution for us so we do not have to implement all that machinery.  Much of the work on nominal logics has been about exactly this factorization \cite{GabbayMJ:picfm}.  A lambda theory Th(Calc) is a bicartesian closed 2-category equipped with an identity-on-objects functor from $(\FinSet/\Sigma)^{\op}$ to Th(Calc).  Models of Th(Calc) are functors to Cat that preserve all the structure.

The $\pi$-calculus was invented in the early 1990s by Robin Milner as a model of networks of processes with a dynamically changing topology; two processes initially unaware of each other can be introduced by a third process.  The reflective higher order $\pi$-calculus uses quoted processes as names; the term constructors for quote and eval replace the more traditional nu and replicate constructors.  We also add a ``comm'' term to restrict the contexts in which reduction can occur \cite{DBLP:journals/corr/StayM15}.

Here is a presentation of the multisorted lambda theory Th(RHOpi) for the reflective higher-order $\pi$-calculus:
\begin{center}
  \begin{longtable}{|p{0.3\linewidth}|p{0.7\linewidth}|}
    \hline
    Sorts:
    \begin{itemize}
      \item $N$ for names
      \item $P$ for processes
    \end{itemize}\bigskip
    Term constructors:
    \begin{itemize}
      \item $\send\maps N \times P^* \to P$
      \item \raggedright $\recv\maps N \times (N^* \Rightarrow P) \to P$
      \item $|\maps P^2 \to P$
      \item $0\maps 1 \to P$
      \item $\comm\maps 1 \to P$
      \item $\quote{-}\maps P \to N$
      \item $\deref{-}\maps N \to P$
    \end{itemize}
    &
    Rewrites:
    \begin{itemize}
      \item $\alpha\maps (p_1 | p_2) | p_3 \Rightarrow p_1 | (p_2 | p_3)$
      \item $\beta\maps p_1 | p_2 \Rightarrow p_2 | p_1$
      \item $\iota\maps 0 | p \Rightarrow p$
      \item \raggedright $\chi\maps \send(x, p_1, \ldots, p_n) \;|\; \recv(x, q) \;|\; \comm \Rightarrow q(\quote{p_1}, \ldots, \quote{p_n}) \;|\; \comm$
      \item $\epsilon\maps \deref{\quote{p}} \Rightarrow p$
    \end{itemize}
    Equations:
    \begin{itemize}
      \item \tshortstack[l]{$\alpha = P^3, \beta = P^2, \iota = P$ ($|$ and 0 form a \\ commutative monoid)}
      \item \tshortstack[l]{$\epsilon = P$ (evaluating a quoted process is the \\ same as the process itself)}
    \end{itemize}\\
    \hline
  \end{longtable}
\end{center}
The simplest RHOpi processes are 0, the ``do nothing'' process; and comm, a ``catalyst'' process that enables communication on a channel.  The only rewrite that is not an identity is $\chi,$ the communication event.  The $\chi$ rewrite is neither confluent nor deterministic.  For example, we can model contention for resources with the term
\[ \recv(x, P)\;|\;\recv(x, Q)\;|\;\send(x,R)\;|\;\comm \]
which has two rewrites out of it, one where the continuation $P$ is invoked on the name $\quote{R}$ and the other where the continuation $Q$ is invoked on it.  We can model message arrival order nondeterminism with the term
\[ \send(x, P)\;|\;\send(x, Q)\;|\;\recv(x,R)\;|\;\comm \]
which has two similar rewrites out of it, one where the continuation $P$ is invoked on the name $\quote{R}$ and one where $P$ is invoked on the name $\quote{Q}$.

In the theory above, comm is preserved by the rewrites; one can think of each comm instance as representing a processor.  An alternative would be to consume comm in the $\chi$ rewrite; then comm would track clock ticks; an application of a consumable comm is the formal verification of billing code for tracking compute resources.

Replication of processes, and therefore general recursion, can be encoded \cite{DBLP:journals/entcs/MeredithR05} via
\[D(x) = \recv(x, y\mapsto \send(x, \deref{y}) | \deref{y})\]
\[!P = \send(x, D(x) | P) | D(x).\]

Caires' \cite{Caires} operator $\triangleright$ for rely-guarantee properties, the adjunct to $|,$ is an instance of our generic modal operator, where $u \triangleright v = u \langle - | - \rangle v:$
\[ \interp{u \triangleright v} = \{ t \;|\; \exists \rho\maps (t\;|\;u) \Rightarrow v\} \]

The logic is generated as above, adding modal operators and recursion to the term language, then adding the Boolean algebra; it is equivalent to the logic we generated by hand in \cite{DBLP:conf/tgc/MeredithR05}.  As an example of a formula in this logic, here is a one-line formula for a process that will always be able to handle another message:
\[ \mu X. \recv(\top, x \mapsto X)\;|\;\top. \]
It says that the process must factor into a piece that is waiting for a message on some channel, and when it receives that message, the continuation will have the same form.

\subsubsection{Namespaces}

When we add a copy of BoolAlg to RHOpi for both $N$ and $P,$ we can write down formulae that talk about sets of names.  The formula $\top$ denotes the set of all names; the formula $\quote{\top}$ denotes the set of names that are quoted processes.  These two sets differ when we have a nonempty set of generating names.

An application of namespace logic is a formula for a ``firewall'': any process satisfying this formula receives no messages except on a name in $\quote{\phi}.$
\[ \mu X. \recv(\quote{\phi}, x \mapsto (X \lor 0)\;|\;\neg\recv(\quote{\neg \phi}, \top))\;|\;\neg\recv(\quote{\neg \phi}, \top) \]
It is, perhaps, surprising that this can be accomplished with a compile-time check, since we usually think of firewalls as a dynamic check.  An application of namespaces to statically proving security properties of code in an object capabilities language can be found in \cite{DBLP:journals/corr/MeredithSD13}.

\section{Conclusion and future work}
We have presented an algorithm for generating a logic from three data:
a 2-Lawvere theory describing a notion of computation, a monad for
describing a notion of collection, and a distributive law. As
mentioned in the introduction, the picture emerging from this view of
logic is that logically useful collections of individuals look like
nothing so much as the individuals that represent the formulae that
collect them.  The range of applications of this algorithm is quite
broad.  Any of the languages given semantics in the K Framework,
including C/C++, Java, Javascript, and many others can automatically
be equipped with logics that serve as a basis for of program
specification and automatically checked verification, and
simultaneously as a basis equipping these languages with new and
powerful type systems.  This considerably lowers the barrier to
embuing popular untyped languages with type systems.  Likewise, the
algorithmic nature of the approach also serves as an aid in future
language design.  For example, Synereo is designing a language for the
smart contracts on the blockchain based on the
$\mathsf{RHO}$-calculus \cite{rholang}.  Equipping this language with a type system
amounts to nothing more than deciding on the type of collection one
wants to collect type inhabitants in.

\bibliographystyle{amsplain}
\bibliography{ladl}
\end{document}


\documentclass[11pt, oneside]{article}   	% use "amsart" instead of "article" for AMSLaTeX format
\usepackage{geometry}                		% See geometry.pdf to learn the layout options. There are lots.
\geometry{letterpaper}                   		% ... or a4paper or a5paper or ...
%\geometry{landscape}                		% Activate for for rotated page geometry
%\usepackage[parfill]{parskip}    		% Activate to begin paragraphs with an empty line rather than an indent
\usepackage{graphicx}				% Use pdf, png, jpg, or eps§ with pdflatex; use eps in DVI mode
								% TeX will automatically convert eps --> pdf in pdflatex
\usepackage{amssymb}

\title{Ecological model for GEOCLIM: the ECOWEB module}
\author{Yves Godd\'eris}
\date{March 2016}							% Activate to display a given date or no date

\begin{document}
\maketitle

\section{Generality}
%*****************************************************
The model includes three trophic levels: primary producers, predators, and super predators. The ecological model uses the P input in each oceanic reservoir to calculate the uptake of P and C by the primary producers (entrance gate of those elements in the trophic chains). ECOWEB then sends this uptake back to the GEOCLIM model which stores it into the POC reservoir. Within ECOWEB, the exchange fluxes are in molC/yr, and the stocks in molC. Each species is thus represented by a carbon mass.

The number of species of the three trophic levels is fixed prior to any simulation.

\section{Mass balance for the primary producers}
%*****************************************************
At each time step, the mass of species $i$ is calculated by solving the following mass balance equation:
\begin{eqnarray}
%species budget
\frac{dy_{i}}{dt}=\alpha_{i}-\omega_{i}
\label{spbalance}
\end{eqnarray}

where $y_i$ is the biomass of species $i$ (mol C). $\alpha_i$ is the birth rate of species $i$, and $\omega_i$ is the sum of the death rate and grazing rate when applicable.
\subsection{Reproduction rate}
$\alpha_i$ (mol/yr) is dependent on the temperature of the reservoir $T$, on pH, and on the input of dissolved PO$_{4}^{2-}$ inside the oceanic reservoir.
\\
\begin{eqnarray}
%alpha calculation
\alpha_{i}=br_{i} \cdot a_{1}^{i} \cdot \exp{\left[   \frac {-\left(T-b_{1}^{i}\right)^{2}}   {2\left(c_{1}^{i}\right)^{2}}  \right]} \cdot
         a_{2}^{i} \cdot \exp{\left[   \frac {-\left(pH-b_{2}^{i}\right)^{2}}   {2\left(c_{2}^{i}\right)^{2}}  \right]} \cdot
        I_{i} \cdot F_{in}^{P}
\label{birth}
\end{eqnarray}

The two exponential factors are gaussian functions. $b_{1}^{i}$ and $b_{2}^{i}$ are respectively the optimum living temperature and pH for the species $i$. $c_{1}^{i}$ and $c_{2}^{i}$ define the temperature and pH tolerance of species $i$. $b_{1}^{i}$ is set to the averaged long term temperature, with a random noise between 0 and $\pm$5 degrees C around the optimum living temperature. The temperature tolerance for each species is randomly set a value between 0 and $\pm$4 degrees C around each respective optimum temperature $b_{1}^{i}$. Optimal living pH $b_{2}^{i}$ and tolerance $c_{2}^{i}$ are similarly randomly set. The interval goes from -0.1 to +0.1 for the noise added to the averaged long term pH of the surficial reservoir. Tolerance runs from $\pm$0.3 pH unit.

Finally, $I_{i}$ represents the potential competition for resources amongst the primary producers. For each species $i$, the existence of a competitive relationship is randomly fixed (yes/no). If the answer is no, $I_{i,j}$ is set to 1. Conversely, If the answer is yes, $I_{i,j}$ is calculated as follows for species $i$:
\\
\begin{eqnarray}
%competition
I_{i,j}=\frac{\left(\frac{y_i}{y_j}\right)}  {1+\left(\frac{y_i}{y_j}\right)}
\label{competition}
\end{eqnarray}

For species $j$ with respect to species $i$, it is calculated as:
\\
\begin{eqnarray}
%competition
I_{j,i}=1-I_{i,j}
\label{competition2}
\end{eqnarray}

where $y_i$ and $y_j$ are respectively the biomasses of species $i$ and $j$. If the ratio between these two biomasses tends towards 0, then the efficiency of species $j$ in gathering resources leads to the extinction of $i$. If the biomass ratio turns at the advantage of species $i$, and the reproduction of {j) tends towards 0. The factor $I_i$ is calculated as the average of all the $I_i,j$, for $j$ covering the whole range of species in competition with $i$. This formalism implies that if a species is in competition with one or several others, its reproduction rate is somewhat reduced ($I_i$ is little than 1) compared to non-competing species.

\subsection{Death rate}
The death rate of each primary producer is equal to the sum of the natural death rate $D_i$ and the predation rate $G_{i,k}$  of species by the predator $k$ (both fuxes in moleC/year). The death rate is set proportional to the total mass of the primary producer himself:
\\
\begin{eqnarray}
%primary producer death rate
D_i=k_{death} \cdot y_i
\label{death_rate}
\end{eqnarray}

Several predators can feed on the same primary producer species $i$. The is fixed by drawing lots. Then the total loss of biomass of species $i$ due to predation is:
\\
\begin{eqnarray}
%primary producer death rate
G_i=\sum_k {G_(i,k)}=\sum_k {k_{pred}^{k,i} \cdot \frac{\frac{y_i}{y_k}}{10+\frac{y_i}{y_k}} \cdot y_k}
\label{predation_rate}
\end{eqnarray}

where the sum extends over all the predators ($k$) feeding on species $i$. $k_{pred}^{k,i}$ is a fix constant, but can also be randomly fixed. The relative predator biomass within the whole ecosystem depends on this (those) constant(s). The predation rate is also set proportional to the predator biomass (the more they are, the more they eat. But the proportionality is modulated by a Michaelis-Menten function of the prey/predator ratio. Overall, the second term of equation \ref{spbalance} can be written:
\\
\begin{eqnarray}
%species loss
\omega_{i}=D_i + G_i
\label{destruction}
\end{eqnarray}

\section{Mass balance for the predators}
%*****************************************************
The reproduction rate of predators is set to their grazing rate, following the principle that they are tight to the disponibility of ressources. The number of grazed species for each predator is fixed randomly. Some of them are thus specialist (feeding on few species), and other generalist (feeding on many species). Predators are allowed to feed on other predators. This is once again randomly fixed. The death rate of predator is calculated in the same way as the death rate of primary producers.

\section{Mass balance for the super predators}
%***************************************************
Predators of the trophic level 3 behave as predator of the trophic level 2, with the notable exception that they cannot feed on each other. The $D_i$ terms is thus the only term appearing in the calculation of $\omega_i$.

\section{Pelagic carbonate producers}
%***************************************************
A random number of primary producers are assumed to be carbonate shell producers. No more than 20\% of the primary producer species can be carbonate producers (this is an adjustable parameter). For each surficial oceanic reservoir, the ECOWEB module calculates the ratio between the biomass of primary producers building carbonate shells, and the total biomass of primary producers. This ratio is sent to GEOCLIM which translate it into a carbonate flux as a function of the saturation state of the seawater with respect to calcite. Note that if the surface water becomes undersaturated, the carbonate primary producers undergo extinction ($\alpha_i$ is set to zero).

\section{Extinction}
%----------------------
If, for any given reason, the biomass of a species falls below 1\% of its original steady-state value, the species undergoes extinction. $\alpha_i$ is set to zero, translating the fact that the critical threshold below which reproduction is not more possible has been reached. The threshold of 1\% is an adjustable parameter. $\alpha_i$ is maintained to zero even if favorable conditions are restores over the course of the simulation.

%ecoweb scheme
\begin{center}
\includegraphics[width=10cm]{/Users/yves/fortran/GEOCLIM2/description/biodiv_scheme.jpg}\\
\small{Schematic view of the ECOWEB module}\\
\end{center}

\end{document}


%\subsection{Experiments}

%\subsection{Evaluation methodology}

%\textcolor{red}{ In the off-line study we assume that  we have a third party application-specific tool that predicts the probability score $S(i)$ that each tweet $i$}  is relevant, i.e., the tweet is related to a natural disaster. This probability scoring function is assumed to be given.

Before we proceed to our online user study, we first wish to benchmark the performance of our greedy algorithms and K-means w.r.t.\ clusters obtained via the optimal solution of EF1 defined by our MILP through an offline empirical evaluation.

Because it is critical to explicitly control noise levels of relevance predictions in these experiments in order to evaluate a range of scenarios, we use a noisy corruption of ground truth.
% in terms of predicted relevance noise level.
We also explicitly vary the number of relevant information elements in our ground truth to assess performance variation as a function of class imbalance. Hence, to setup a scenario of 100 tweets with only 20\% of relevant tweets, we randomly select 20 tweets from the set of relevant tweets and 80 tweets from the set of irrelevant tweets. Then, for each tweet $j$, we assign the probability $S(j)$ to that tweet to indicate its relevance probability by introducing a random noise signal as follows:
\begin{equation}
S(j) = \lambda\times rel+(1-\lambda)\times rand()  ,
\end{equation}
%we propose to simply randomly select an element $j$,
%To simulate the
%Finally, we propose to compute a global element score value $S(i)$ by introducing a noise signal ratio as follows:
where $rel$ is a boolean value indicating the real relevance, $rand()$ is a random noise value chosen with uniform distribution in the range $[0,1]$, and $\lambda$ is a weighting parameter  that satisfies $0.5 \leq \lambda \leq 1$, which controls the signal-to-noise ratio in the final probability value. Note that for $\lambda=1$, $S(j)$ gives the ground truth probability value (perfect classifier), whereas for $\lambda=0.5$, $S(j)$ is extremely noisy.

Note that the evaluation was carried out by averaging over 10 independent runs that each select random relevant documents according to the designated proportion.  We report the average ground truth F1-Score (i.e., ground truth is known in the experimental setting) for the EF1-maximizing cluster produced by each method, with 95\% of confidence intervals.
We report and discuss the main results of the experimental evaluation, considering both the accuracy and the effectiveness of the described algorithms. The configuration options that we have evaluated are the following: F1-Score vs. $\#data \in \{10,\dots, 150,\dots, 10^3\}$ $\times$ $ \lambda \in \{  0.6,0.7,0.8,0.9,1.0 \}$ $\times$ Rate of positive date $\in \{0.5\%, 1\%, 2\%, 3\%,$ $ 4\%, 5\%, 10\%, 20\%, 30\%, 50\%\}$. %All algorithm have been implemented with Java while using
We used the Gurobi Optimizer solver through its Java interface for the comparisons to the optimal MILP method~\cite{gurobi2017gurobi}.

In our experimental comparison, we only  %only the most significant and interesting results
evaluate the optimization method up to \#data=150 as the MILP could not scale to a larger data set. In the results shown below, {\bf Initial} refers to the performance of the initial global set of retrieved elements matching the query before applying any clustering (which effectively constraints this global set).  We note that by definition, Initial is a recall-maximizing baseline.

%How does F1 compare to Ef1 as Size of the data?
%How does F1 compare to Ef1 as amount of positive data changes?
%Under what conditions is EF1 is a good surrogate for F1?

%Result set size?  Discuss  F-beta later?  If user thinks result sets are too big, just adjust beta.

%\subsection{Performance analysis}
%We first report the experimental results in terms of F1-Score.

\subfour{Varying \#data:} Here we aim to understand how the different F1-Score optimization algorithms perform as the amount of data varies.  This analysis is provided in Figure  \ref{fig:F1_vs_Data_Twitter} while fixing the rate of positive data to 2\% and $\lambda \in \{0.6,0.9,1.0\}$, and  varying the size of the data.

%(Show for lambda=0.6, 0.9 for 2\% malicious Fig 55(a,d,e) X 3 domains.)  Add lambda=1 if fits.

\begin{figure}[H]
\begin{centering}
\includegraphics[width=8.5cm]{imgs/legend1}
\par\end{centering}
\begin{centering}
{\includegraphics[width=8.75cm]{imgs/Twitter_results/f1_performance_posrate_2\lyxdot 0}}
\par\end{centering}
\caption{Performance with 2\% of positive data.}
\label{fig:F1_vs_Data_Twitter}
\end{figure}

%At first glance, all curves of the same algorithm with the same parameters have the same shape. This indicates that the algorithms are consistent across different datasets while varying the size of the data.
% Omit peak discussion for now... reasons are not completely clear from explanation.  -Scott
%We explain the apparent peak in all figures as a statistical anomaly as small datasets often don't have enough positive data (for low positive data rate), so this is just a statistical artefact  and can be ignored.  It does disappear as the rate of positive data increases. %\textcolor{red}{[Figures not shown]}
%Also,
We observe that beyond a certain \#data threshold, it generally becomes harder to optimize F1 as the size of the data grows as it is harder for any clustering method to separate signal from noise.
%with existing filters.
We also notice that in general, the BPS and greedy algorithms are a good approximation of the optimal solution, with the BPS method doing better for some cases ($\lambda=0.6$ and \#data$\in [10,100]$) since the coarse splits of BPS serve as a guard against ``overfitting'' to noise seen in Optimal.  K-means does not perform as well in the low data regime.
%, but performs comparable to BPS with more data.
%since X-means produces a larger $K$ and more (but smaller) clusters to choose from for optimizing EF1.
%, e.g.,  we observe up to 42\% improvement of Greedy w.r.t Optimal for a classifier with an accuracy of 60\%.

\subfour{Varying relevance noise ($\lambda$):}  Here we aim to understand how the different F1-Score optimization algorithms perform as the amount of noise $\lambda$ (defined previously) in the relevance prediction varies.
The results of this analysis are shown in Figure \ref{fig:F1_vs_Lambda} while having the rate of positive data in \{1\%,10\%,50\%\}, a data size of 150, and varying the amount of noise $\lambda \in [0.6,1.0]$.

%Again, all curves of the same algorithm have roughly the same shape across the different datasets, which indicates that the algorithms are consistent.
We observe that for low rate of positive data the problem becomes easier (higher F1-Score value) as $\lambda$ increases because EF1 becomes closer to the ground truth F1-Score.
%We also note that the BPS algorithm does better for high noise since it does not overfit to singletons as easily as Greedy; in contrast Greedy does overfit which is better for low noise since it can optimize small details that are actually valid.
We also note that for a high rate of positive data, the quality of the classifier has no impact on the accuracy of the algorithms as they tend to return the entire collection -- the performance is almost similar to the Initial performance.  We conclude by observing that K-Means clearly does a poor job of optimizing the F1-Score of its clusters since relevance scores do not play a part in its optimization objective.
%is worse than the Initial set of tweets as it does not consider the relevance signal during clustering.

%We conclude by observing that for low noise, the BPS algorithm is a good approximation of the optimal method.
%(\#data=150,10\% malicious = Fig 69f X 3 domains.)}

\begin{figure}[H]
\begin{centering}
\includegraphics[width=8.5cm]{imgs/legend1}
\par\end{centering}
\begin{centering}
{\includegraphics[width=8.75cm]{imgs/Twitter_results/f1_performance_posrate_Data_150}}\par\end{centering}
\caption{Performance: \#data=150.}
\label{fig:F1_vs_Lambda}
\end{figure}

\subfour{Varying rate of positive data:} Here we aim to understand how the different F1-Score optimization algorithms perform as we vary the proportion of positively labeled ground truth data.
Figure \ref{fig:F1_vs_Pos_Twitter} shows the results of this analysis while fixing the size of the data to 150 and $\lambda\in \{0.6,0.9\}$, and varying the rate of positive data in $[0.5\%,50\%]$.

\begin{figure}[H]
\begin{centering}
\includegraphics[width=8.5cm]{imgs/legend1}
\par\end{centering}
\begin{centering}
{\includegraphics[width=7.75cm]{imgs/Twitter_results/f1_performance_Data_150}}
\par\end{centering}
\caption{Performance with \#data=150.}
\label{fig:F1_vs_Pos_Twitter}
\end{figure}

%Briefly, we note again that all curves of the same algorithm with the same parameters have the same shape across the different datasets.

Briefly, we observe that the problem becomes easier as the number of positive examples increases as well as the value of $\lambda$ increases (i.e., noise decreases).
This is explained by the fact that a high rate of positive examples allows the algorithms to be able to select a subset of elements with a large number of positive examples thus maximizing F1-Score. However, a low value of $\lambda$ tends to lead the algorithms to select all content (close to Initial set) due to the level of noise, while a high value of $\lambda$ tends to lead the algorithms to select the actual positive examples and thus maximize F1-Score. This is shown through the increasing separation of all curves from the Initial curve as $\lambda$ increases.  K-means does not outperform Initial.

%(Fig 78a,d = \#data=150, lambda=0.6/0.9 X 3 domains).  Add lambda=1 if fits.

\subfour{F1-Score vs. EF1-Score:} This analysis aims to answer the following question: \textquotedblleft \textit{Under what conditions is EF1 is a good surrogate for F1?}\textquotedblright{}. The results of this analysis are shown in Figure \ref{fig:F1_vs_EF1_Enron} using a set of four scatter plots.
We show a comparison of the results obtained when optimizing the real F-Score vs. those obtained when optimizing expected F1-Score with different values for $\lambda$.
To provide more clarity, the line $y = x$ is included to convey whether the optimization of a particular set gave the same F1-Score using the different optimization strategies or not. A point above the line corresponds to a set for which the optimization with EF1-Score gave a higher F1-Score, while a point below the line refers to a set for which the optimization with EF1-Score  hurts the F1-Score. We also include the plot of the function that fits the data points to show how far is the expected F1-Score with respect to the real F1-Score. In addition, we provide a global Root Mean Square Error (RMSE) value to get an overview of the approximation.

Briefly, it is clear that for high values of $\lambda$ (e.g., 0.9), the optimization with EF1 gives a good approximation of the real F1-Score, with RMSE=0.01 for $\lambda=0.9$. For a lower $\lambda=0.8$ and $\lambda=0.6$ we do observe significantly more noise (as expected), but we also note from the assymetry that EF1 effectively lower bounds the true F1 score indicating the desirable optimization property that high EF1 implies high F1-score.
%, the RMSE is respectively multiplied by 3 and 8, indicating that for high noise, the approximation may completely be reduced to a random value. Hence, we conclude that for a classifier with a high accuracy (say 80-90\%), the expected F1-Score is a good approximation for the real F1-Score.

\begin{figure}[H]
\begin{centering}
\par\end{centering}
\begin{centering}
{\includegraphics[width=8cm]{imgs/scatter_plot2_EF1\lyxdot vs\lyxdot F1}}
\par\end{centering}
\caption{Scatter plot showing EF1-Score vs. F1-Score.}
\label{fig:F1_vs_EF1_Enron}
\end{figure}

%\begin{figure}[H]
%\begin{centering}
%\par\end{centering}
%\begin{centering}
%\includegraphics[width=9cm]{imgs/boxplot2_EF1\lyxdot vs\lyxdot F1}
%\par\end{centering}
%\caption{EF1-Score vs. F1-Score.}
%\label{fig:Time_vs_Pos_Reddit}
%\end{figure}

%\subfour{Analysis of the multiple filter selection wrapper strategy:} Figure~\ref{fig:FilterWrapperStrategy} shows how many filters it takes (x-axis) to reduce the number of non-filtered emails above the score threshold of $S(j)=0.6$ to zero (y-axis).  A faster reduction to zero indicates better coverage with fewer filters.  We observe the following: (1) for higher relevance noise, fewer filters are needed since all methods tend to return most elements in the first filter, (2) BPS tends to return the highest number of filters, followed by the Optimal MILP method, then the Greedy method, and (3) all methods tend to quickly return filters (at most 5 filters) that cover the relevant elements with a probability score higher than 0.6.

%\begin{figure}[H]
%\begin{centering}
%\includegraphics[width=8.5cm]{imgs/legend2}
%\par\end{centering}
%\begin{centering}
%\subfigure[$\lambda=0.8$.]{\includegraphics[height=2.24cm]{imgs/wrapper/wrapper_posrate_30\lyxdot 0_lambda_0\lyxdot 8_rate_data_150}}
%\subfigure[$\lambda=0.9$.]{\includegraphics[height=2.24cm]{imgs/wrapper/wrapper_posrate_30\lyxdot 0_lambda_0\lyxdot 9_rate_data_150}}
%\subfigure[$\lambda=1.0$.]{\includegraphics[height=2.24cm]{imgs/wrapper/wrapper_posrate_30\lyxdot 0_lambda_1\lyxdot 0_rate_data_150}}
%\par\end{centering}
%\caption{Effect of the filter wrapper strategy on the Twitter dataset with 30\% of positive data and \#data=150.}
%\label{fig:FilterWrapperStrategy}
%\end{figure}

%\subsection{Computational time complexity analysis}
%We now report experimental results under the previous settings in terms of time complexity for  scalability analysis.

%\subfour{Time  vs. \#data:} A critical question for scalability is how time complexity of cluster optimization scales vs. the amount of data.  Figure \ref{fig:Time_vs_Data} reports the results of this analysis on the three datasets while fixing the rate of positive data to 2\% and $\lambda=0.9$, and varying the size of the data.
%Naturally, the problem becomes harder as the size of the data increases.
%We also observe that Enron has highest time complexity followed by the Reddit dataset, and then the Twitter dataset. This is certainly due to the fact that the size of the text associated with each entity in each dataset is different, ranging from dozens of thousands of characters for emails in Enron to 144 characters for tweets in Twitter. Hence, more words increase the time complexity.
%Overall, we critically notice that the BPS algorithm tends to have a lower time complexity than the greedy algorithm making it the most scalable of those compared.

%More complex case for \%malicious.  High noise makes for a hard problem, but increasing \%malicious just causes it to select everything (easy).  Low noise, not much to select for low \%mal, but have to work hard to select the right 50\% for high \%mal.  For moderate noise see both effects!

%\subsection{[Last] Computational time performance (Fig 107d, 117f, 130a,c,e.)  For all 3 domains if space, but not necessary.}

%\begin{figure}
%\begin{centering}
%\includegraphics[width=8.5cm]{imgs/legend2}
%\par\end{centering}
%\begin{centering}
%{\includegraphics[width=6.0cm]{imgs/Twitter_results/time_posrate_2\lyxdot 0_0\lyxdot 9}}
%\par\end{centering}
%\caption{Time complexity: 2\% positive data and  $\lambda=0.9$.}
%\label{fig:Time_vs_Data}
%\end{figure}

%\subfour{Time vs. $\lambda$:}  Another question is how noise levels in relevance prediction affect the time complexity of cluster optimization.  The results of this analysis are shown in Figure \ref{fig:Time_vs_Lambda} while fixing the rate of positive data in  \{3\%,20\%,30\%\} and the size of the data to 150, and varying the amount of noise of the classifier $\lambda \in [0.6,1.0]$.

%In brief, we first notice that clearly, for low rate of positive data (3\%) and with $\lambda=0.6$, the optimization algorithm has a high time complexity (up to 30 second to find the optimal solution). Hence, the optimal solution is clearly not scalable. Moreover, we observe that this time complexity reduces as the value of $\lambda$ increases. We explain that by the fact that the algorithms need to make a large number of iterations to find the best solution with high noise (low $\lambda$). \textcolor{red}{However, we observe an inverse effect for a high rate of positive data. Indeed, with 50\% of positive data the highest is the value of $\lambda$ the highest is the time complexity due to the fact that the optimal solution is hard to compute. For moderate rate of positive data we observe a stable time complexity as we vary  $\lambda$.}

%\begin{figure}
%\begin{centering}
%\includegraphics[width=8.5cm]{imgs/legend2}
%\par\end{centering}
%\begin{centering}
%{\includegraphics[width=8.75cm]{imgs/Twitter_results/time_posrate_Data_150}}
%\par\end{centering}
%\caption{Time complexity:\#data = 150.}
%\label{fig:Time_vs_Lambda}
%\end{figure}

%\subfour{Time vs. rate of positive data:} Finally we ask how changes in the proportion of positive data affect the time complexity of filter optimization.  Figure \ref{fig:Time_vs_Pos_Twitter} shows the results of this analysis while fixing the  the size of the data to 150 and $\lambda\in \{0.6,0.8,1.0\}$, and varying the rate of positive data in $[0.5,50]$.

%\begin{figure}[H]
%\begin{centering}
%\includegraphics[width=8.5cm]{imgs/legend2}
%\par\end{centering}
%\begin{centering}
%{\includegraphics[width=8.75cm]{imgs/Twitter_results/time_Data_150}}
%\par\end{centering}
%\caption{Time complexity on Twitter with \#data=150.}
%\label{fig:Time_vs_Pos_Twitter}
%\end{figure}

%Here again we observe that the results are consistent across the different datasets.
%At first glance, we observe that high noise (low $\lambda$) makes a hard problem for the optimization algorithm, but increasing the rate of positive examples causes it to select everything (making the problem easier).  For low noise (high $\lambda$), only a few elements are selected for a low rate of positive data, but the optimization algorithm has to work hard to select the right 50\% for high rate of positive data.  For moderate noise we observe both effects.

%At first glance, we observe that for small values of $\lambda$ the time complexity for the optimization algorithm tends to increase while decreasing rate of positive data. However, for high values of $\lambda$ the time complexity for the optimization algorithm tends to increase while increasing  the rate of positive data.

%In both cases, we explain that by the fact that This is probably because the optimization algorithm fails in discrimination positive from negative examples.

%\subsection{Summary of findings}
%In conclusion, the evaluations have shown that the BPS and Greedy algorithms are a good approximation of the Optimal MILP formulation and may outperform the MILP in high noise settings (especially the BPS approach which tends to overfit  less to noise). We have also shown that the BPS performs well in terms of objective optimization and is most efficient for scaling to large datasets. Finally, we have demonstrated that the expected F1-Score metric is a good surrogate of the F1-Score metric.
%specifically when using an accurate classifier (roughly 80-90\% accuracy).