\documentclass[parskip=half]{scrartcl}

\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage[textsize=tiny]{todonotes}
\graphicspath {{images/}}
\hypersetup{
    colorlinks=true,
    citecolor=blue
}

\begin{document}

\title{CS698 (Winter 2017) Notes}
\author{Vineet John (v2john@uwaterloo.ca)}
\date{\today}
\maketitle

\section{Supervised Learning}\label{supervised-learning}

    Classification: domain of output is categorical (discrete)\\
    Regression: domain of output is numerical (continuous)

    Input domain can be either discrete or continuous for both.

    Examples:\\
    Appln - Domain - Range - Type of Problem\\
    Spam Identification - Words/Characters - spam/ham - classification\\
    Stock Prices - Prev price/news - predicted value - regression\\
    Speech recognition - Audio signal - words - classification\\
    Digit recognition - Image - Digits - classification\\
    Housing value - Area/bedrooms/baths - market value - regression\\
    Weather prediction - Local weather/Prev weather - Temp/Rain - Regression/Classificn

    Consider space of probably hypotheses\\
    Approximation of a best solution is done within this space\\
    A good hypothesis generalizes well

    When is it not possible to find a consistent hypothesis

    \begin{itemize}
        \item
        Insufficient Hypothesis space
        \item
        Noise in the training data
    \end{itemize}

    Tradeoff: Expressiveness vs complexity in finding a hypothesis - in the
    hypothesis space

\section{Statistical Learning}\label{statistical-learning}

    \begin{itemize}
        \item
        Joint probabilities
        \item
        Marginalization - sum up a join probability over a variable to express
        it in terms of another variable
        \item
        Conditional probabilities i.e A, given B
        \item
        Conditional probabilies need not sum up to 1, since the idea of a
        context is introduced.
    \end{itemize}

    \subsection{Bayes Rule}\label{bayes-rule}

    \begin{itemize}
        \item
        Posterior probabiliy = prediction, Prior = hypothesis, evidence =
        input variables, likelihood = probability of evidence given the prior
        hypothesis
        \item
        Weigh each hypothesis and use all hypothesis to get an avg prediction
        \item
        No overfitting, all hypotheses considered and weighed according to
        their probabilities
    \end{itemize}

\section{Logistic Regression}
\label{logistic-regression}

    \subsection{Overview - Logistic Regression} % (fold)
    \label{sub:overview_logistic_regression}
    \begin{itemize}
        \item
        In Mixture of Gaussians, we learn the prior and the likelihood and used Bayesian learning to compute the posterior.
        \item
        Unlike Mixture of Gaussians, we learn the posterior directly by maximum likelihood
    \end{itemize}

    % subsection overview_logistic_regression (end)

    \subsection{Problem - Singular Hessian}\label{problem---singular-hessian}

    \begin{itemize}
        \item
        Attempt is to make the ridge of the sigmoid function as shape as
        possible
        \item
        When weights tend to negative infinity, the sigmoid function tends to
        zero
        \item
        As the sigmoid is a component of the hessian, the hessian diagonal
        then is comprised of zeoes.
        \item
        This could result in the Hessian becoming a singular matrix
    \end{itemize}

    \subsection{Solutions}\label{solutions}

    \begin{itemize}
        \item
        Adding a penalty term to the loss function
        \item
        Boils down to adding a magnified identity matrix to the Hessian
    \end{itemize}

\section{Generalized Linear models}\label{generalized-linear-models}

    \begin{itemize}
        \item
        Mapping the input space into a lower dimensional space
        \item
        Perform linear regression of the dataset
        \item
        Polynomial class of functions used as basis functions.
        \item
        Basis functions are used for the mapping
        \item
        The input space x is mapped as $\Phi(x)$
    \end{itemize}

\newpage

\section{Perceptron \& Neural Nets}\label{perceptron-neural-nets}

    \begin{itemize}
        \item
        Output in the form of a non-linear function
        \item
        A neuron produces a single output, which may, in turn, be fed to
        multiple other neurons.
        \item
        Types of activation function:

        \begin{itemize}
            \item
            Threshold: Step function, non-linear
            \item
            Sigmoid: Smoother version of the step function
        \end{itemize}
        \item
        Nets can be trained by tuning the weights.
    \end{itemize}

    \section{Threshold Perceptron Learning}\label{threshold-perceptron-learning}

    \begin{itemize}
        \item
        Add/subtract the input vector to/from the weights if the predicted
        value doesn't match the target.
        \item
        Eta determines the magnitude of the step-size
        \item
        Gradient descent: Assuming threshold perceptron, will converge if data
        is linearly separable.
        \item
        If the data isn't linearly separable

        \begin{itemize}
            \item
            The algorithm will never converge
            \item
            The step vector keeps being reset back \& forth
        \end{itemize}
    \end{itemize}

    \section{Sigmoid Perceptron}\label{sigmoid-perceptron}

    \begin{itemize}
        \item
        Similar to logistic regression in that it computes a sigmoid
        separator.
        \item
        Multiple sigmoid hyperplanes could be used to simulate a ridge-like
        hyperplane. Further compounding can lead to simulating a bump-like
        hyperplane.
    \end{itemize}

    \section{Multi-layer neural networks}\label{multi-layer-neural-networks}

    \begin{itemize}
        \item
        Mapping inputs into a linear feature space, and then applying linear
        learning algorithms like linear regression, threshold functions,
        mixtures of gaussians and logistic regression.
        \item
        A neural net could be used to approximately just about any function
        using a combination of neurons.
    \end{itemize}

    \subsection{Feedforward neural
    network}\label{feedforward-neural-network}

    \begin{itemize}
        \item
        Adaptive non-linear basis function = function chaining
    \end{itemize}

    \section{Multi-Layer Neural Nets}\label{multi-layer-neural-nets}

    \begin{itemize}
        \item
        Sequential gradient descent used, similar to a gradient descent
        approach for linear regression.
        \item
        Backpropagation permits simultaneous calculation of weights for the
        perceptrons in a multi-layered neural network.
        \item
        Involves a forward and a backward phase.
        \item
        The forward phase calculates the output $z_j$ for each unit $j$
        \item
        The backward phase propagates errors to the input units, error denoted
        by $\delta_j$ at each unit $j$
    \end{itemize}

    Neural net = composition of functions
    \begin{itemize}
        \item
        Decompose the derivative of the error function, using the chain rule
        \item
        $w_{ij}$ is the weight metric from unit i to unit j
        \item
        $\delta_{ij}$ is the error metric from unit j to unit i
        \item
        Example using tanh as a hypothesis function to calculate the
        activation weights
    \end{itemize}

\newpage

\section{Kernel methods}\label{kernel-methods}

    \begin{itemize}
        \item
        Avoids the complexity of convex optimization
        \item
        Kernel Trick - Related to duality
        \item
        Derived from the dot product of the input and it's embedding into a
        new feature space.
    \end{itemize}

    \subsection{Generalized linear
    functions}\label{generalized-linear-functions}

    \begin{itemize}
        \item
        Fixed non-linear basis functions
        \item
        Limited hypothesis space
        \item
        Easier to optimize
    \end{itemize}

    \subsection{Neural networks}\label{neural-networks}

    \begin{itemize}
        \item
        Adaptive non-linear basis functions
        \item
        Rich hypothesis space
        \item
        Difficult to opitimize (by virtue of being non-convex)
    \end{itemize}

    \section{Kernel methods (contd.)}\label{kernel-methods-contd.}

    \begin{itemize}
        \item
        Rather than trying to compute the function $\Phi$, the
        objective is to be able to start with an arbitrary Gram matrix.
        \item
        Slide 10 - dimensionality of each vector is 2. The square function is
        the new space than the gram matrix can emulate by computing the dot
        product of the x vector and the z vector.
        \item
        Gaussian kernel is unrelated to the PDF of a gaussian distribution.
        \item
        Primal solution - scales with the number of basis functions
        \item
        Dual solution - scales with data
        \item
        A valid kernel must has non-zero eigenvalues, if it's being guessed rather than computed
    \end{itemize}

\newpage

\section{Gaussian Processes} % (fold)
\label{sec:gaussian_processes}
\begin{itemize}
    \item
    Uncertainty in the weights for a linear regressor can be translated into a corresponding uncertainty for the output space. Both are modeled as Gaussian distributions.
    \item
    Continuous functions implies that there are infinite Gaussian distributions that describe the output space.
    \item
    Kernel function is used as a covariance matrix to indicate that the Gaussian distribution of the output space is very similar for points that are closely-correlated.
    \item
    Describes a distribution over each point in the target function.
    \item
    Bayesian Learning used to learn a posterior distribution of what the Gausian processes look like.
    \item
    Function space view: Scales with the amount of data rather than the number of dimensions. Useful for problems where the dimensions might be infinitely many. The weight space view applies to the opposite scenario.
    \item
    Example of Gait Optimization shown in Figure \ref{fig:gait_optimization}.
    \begin{itemize}
        \item
        Experimentation done using new feature vectors (denoted by $X_{new}$).
        \item
        Minimizes the magnitude of the Gaussian deviation.
        \item
        Iteratively run new experiments with different values of $X_{new}$ to converge to a result.
    \end{itemize}
    \begin{figure}[th]
    \centering
    \includegraphics[width=0.6\textwidth]{gait_optimization}
    \caption{Gait Optimization}
    \label{fig:gait_optimization}
    \end{figure}
\end{itemize}

% section gaussian_processes (end)

\newpage

\section{Support Vector Machines} % (fold)
\label{sec:support_vector_machines}

    \subsection{Overview - SVM} % (fold)
    \label{sub:overview_svm}

    \begin{itemize}
        \item
        Also uses kernel trick, but the kernel uses a subset of the data.
        \item
        SVM attempt to maximize the margin between the support vectors and the hyperplane. Ref Figure \ref{fig:svm_general}
        \begin{figure}[th]
        \centering
        \includegraphics[width=0.4\textwidth]{svm_general}
        \caption{SVM Diagram}
        \label{fig:svm_general}
        \end{figure}
        \item
        SVMs are also called max-margin classifiers. $a > b$ in Figure \ref{fig:max_margin}
        \begin{figure}[th]
        \centering
        \includegraphics[width=0.4\textwidth]{max_margin}
        \caption{Max margin}
        \label{fig:max_margin}
        \end{figure}
        \item
        $w^T \Phi(x) = 0$ represents the linear separator
        \item
        Dividing by $\Vert w\Vert$ ensures that the distance to the linear separator is normalized in terms of unit length.
        \item
        The problem is made tractable in terms of the data by relying only on the support vectors while performing the quadratic optimization.
        \item
        The quadrative objective is reformulated as a dual representation in $\Phi(x)$
        \item
        The result is an optimization objective in the auxilliary variable $a$, which is the weight for the penalty term. $a_n = 0$ for any data-points apart from the support vectors.
    \end{itemize}
    % subsection overview_svm (end)

    \subsection{Overlapping classes and slack variables} % (fold)
    \label{sub:overlapping_classes_and_slack_variables}

    \begin{itemize}
        \item $\epsilon$ parameterized the soft-margin which is an allowance for some data point to lie beyond the margin.
        \item If misclassfied, $\epsilon > 1$.\\ If within the soft margin, $1 > \epsilon > 0$.\\ If on the margin, $\epsilon = 0$\\
        Refer to figure \ref{fig:soft_margins}
        \begin{figure}[ht]
            \centering
            \includegraphics[width=0.4\textwidth]{soft_margins.png}
            \caption{Soft Margins - SVM}
            \label{fig:soft_margins}
        \end{figure}
        \item $C$ controls how much to weigh vanilla SVMs over the new optimization problem for soft margins. Hard margin classifier implies that we're assuming the data is linearly separable.
        \item For the new optimization problem, earlier only support vectors were considered, but now, we consider points on the margin, points within the margin as well as misclassified points.
        \item Computation complexity increases as the number of vectors increase.
    \end{itemize}

    % subsection overlapping_classes_and_slack_variables (end)

    \subsection{Muticlass SVMs} % (fold)
    \label{sub:muticlass_svms}
    \begin{itemize}
        \item One vs Rest, for all classes, then combine.
            \begin{figure}[ht]
                \centering
                \includegraphics[width=0.4\textwidth]{one_vs_all_and_pairwise.png}
                \caption{One vs. All / Pairwise}
                \label{fig:one_vs_all_and_pairwise}
            \end{figure}
            Shaded regions are uncertain
        \item Pairwise comparison - exhaustive
            Similar to the prev. SVM, there are areas of uncertainty between classes.
        \item Continuous ranking - Best ranked
            \begin{figure}[ht]
                \centering
                \includegraphics[width=0.4\textwidth]{continuous_rank_svm.png}
                \caption{Continuous Ranking}
                \label{fig:continuous_rank_svm}
            \end{figure}
            No uncertainty as we pick the highest ranked class.
            This needs a vector $w$ to define the hyperplane for each class. where $w$ represents the distance of a point to the separating hyperplane.
    \end{itemize}
    % subsection muticlass_svms (end)

    \subsection{Multi-class Classification} % (fold)
    \label{sub:multi_class_classification}
    \begin{itemize}
        \item Multi-class expression is a generalization of the binary SVM expression.
        \item The concept of soft-margins can also be applied here.
    \end{itemize}
    % subsection multi_class_classification (end)

    \subsection{Comparison to Perceptrons} % (fold)
    \label{sub:comparison_to_perceptrons}

    Perceptrons:
    \begin{itemize}
        \item
        Linear separator - depends on starting values
        \item
        Simple update rule
        \item
        Prone to over-fitting
    \end{itemize}

    Support vector machines
    \begin{itemize}
        \item
        Unique max-margin linear separator
        \item
        Quadratic optimization
        \item
        Robust to over-fitting
    \end{itemize}

    % subsection comparison_to_perceptrons (end)

% section support_vector_machines (end)

\newpage

\section{Hidden Markov Models} % (fold)
\label{sec:hidden_markov_models}

    \subsection{Overview - Hidden Markov Models} % (fold)
    \label{sub:overview_hidden_markov_models}

        \begin{itemize}
            \item Data-points are not always IID. Context taken into account while predicting current value.
            \item Correlation with the prior data-points are leveraged.
            \item Stationary assumption: Assumes that the correlation between 2 output variables remains constant over the output space.
            \item Emission - $P(x|y)$, Transition - $P(y_t, y_{t-1})$
            \item Both the emission and transition are defined in terms of multinomial (discrete) distributions. e.g. values of a dice
            \item Joint Distribution - $P(y_{1..t}, x_{1..t}) = P(y_i) \prod_i P(x_i|y_i) \prod_i P(y_i|y_{i-1}) $
            \item Example - cascading uncertainty for a robot's movements over time.
            \item HMM Problem: $P(y_t|x_t .. x_1) $
        \end{itemize}

    % subsection overview_hidden_markov_models (end)

    \subsection{Applications of HMMs} % (fold)
    \label{sub:applications_of_hmms}

        \textbf{Monitoring}
        \begin{itemize}
            \item Predicting the current output state given a historical set of inputs.
            \item Can be expressed in terms of a recursive algorithm, that will required the output classes of the previous epochs.
            \item Forward algorithm - computes from the 0th epoch. Linear complexity in the number of epochs.
        \end{itemize}

        \textbf{Prediction/Forecasting}
        \begin{itemize}
            \item Similar approach to monitoring. Can be expressed recursively.
        \end{itemize}

        \textbf{Hindsight}
        \begin{itemize}
            \item Computing the output class at a given epoch, given input observations in the past and future. $1 < k < t$
            \item Forward-backward algorithm - uses epochs $1..k$ in the forward phase and $k..t$ in the backward phase.
        \end{itemize}

        \textbf{Most likely explanation}
        \begin{itemize}
            \item Predict entire sequence of output class $y_1..y_t$. Can be used in the context of speech recognition.
        \end{itemize}

        Viterbi algorithm - dynamic programming formulation. Discrete distribution of the output class y depends on the Initial State ($\pi$), Transition ($\theta$) and Emission ($\phi$)

        \textbf{Example}
        \begin{itemize}
            \item 2 classes - walking($c_1$) and sitting ($c_2$)
            \item input measurements (accelerometer) - high ($v_1$) and low ($v_2$)
            \item 100 training experiments
            \item
                Statistics
                \begin{itemize}
                    \item $\#c_1^{start} = 90$, $\#c_2^{start} = 10$
                    \item $\#(c_1, c_1) = 80000$, $\#(c_2, c_1) = 20000$
                    \item $\#(c_1, c_2) = 10000$, $\#(c_2, c_2) = 90000$
                    \item $\#(v_1, c_1) = 75000$, $\#(v_2, c_1) = 25000$
                    \item $\#(v_1, c_2) = 30000$, $\#(v_2, c_2) = 70000$
                    \item $P(y_{start} = c_1) = 0.9$, $P(y_{start} = c_2) = 0.1$
                    \item Frequency of starting with a particular class
                        \begin{itemize}
                            \item Transition
                            \begin{table}[ht]
                                \centering
                                \begin{tabular}{| l | c c |}
                                    \hline
                                    \textbf{} & \textbf{$c_1$} & \textbf{$c_2$} \\
                                    \hline\hline
                                        $c_1$ & 0.8 & 0.2 \\
                                    \hline
                                        $c_2$ & 0.1 & 0.9 \\
                                    \hline
                                \end{tabular}
                                \caption{Transition Probabilities}
                                \label{tab:transition_probabilities}
                            \end{table}
                            \item Emission
                            \begin{table}[ht]
                                \centering
                                \begin{tabular}{| l | c c |}
                                    \hline
                                    \textbf{} & \textbf{$v_1$} & \textbf{$v_2$} \\
                                    \hline\hline
                                        $c_1$ & 0.75 & 0.25 \\
                                    \hline
                                        $c_2$ & 0.3 & 0.7 \\
                                    \hline
                                \end{tabular}
                                \caption{Emission Probabilities}
                                \label{tab:emission_probabilities}
                            \end{table}
                        \end{itemize}
                \end{itemize}
        \end{itemize}

        \textbf{Forward algorithm - Example of Monitoring}
        \begin{itemize}
            \item Input set is $[v_1, v_1, v_2, v_2]$. Could be accelometer readings.
            \item $P(y_1|v_1) \propto P(v_1|y_1)P(y_1) = [0.675, 0.03] \propto [0.9574,  0.0426] (normalized) $
            \item $P(y_2|v_1, v_1) \propto P(v_1|y_2) \sum_{y_1} P(y_2|y_1) P(y_1|v_1) = [0.5809, 0.0402] \propto [0.9553, 0.0647] (c_1, c_2) $
            \item $P(y_3|v_1, v_1, v_2) \propto P(v_3|y_3) \sum_{y_2} P(y_3|y_2) P(y_2|v_1, v_1) = [0.1903, 0.1063] \propto [0.6417, 0.3583] $
            \item $P(y_4|v_1, v_1, v_2, v_2) \propto P(v_4|y_4) \sum_{y_3} P(y_4|y_3) P(y_3|v_1, v_1, v_2) = [0.1463, 0.2707] \propto [0.3308, 0.6492] $
        \end{itemize}

        \textbf{Most likely explanation - Example}
        \begin{itemize}
            \item
            $$f(y_1, y_2) = P(y_2|y_1) P(v_1|y_1) P(y_1)$$
            $$[y_1 = c_1, y_1 = c_2] [y_2 = c_1, y_2 = c_2] = [[0.54, 0.135], [0.003, 0.027]] $$
            $$\max_{y_1} P(y_1y_2|v_1) = \max_{y_1} f(y_1, y_2) = [0.54, 0.135] $$
            \item
            $$f(y_2, y_3) = P(y_3|y_2) P(v_1|y_2) \max_{y_1} P(y_1y_2|v_1)$$
            $$[y_2 = c_1, y_1 = c_2] [y_3 = c_1, y_2 = c_2] = [[0.324, 0.081], [0.0041, 0.0345]] $$
            $$\max_{y_1y_2} P(y_1y_2y_3|v_1v_1) = \max_{y_2} f(y_2, y_3) = [0.324, 0.081] $$
            \item
            $$f(y_3, y_4) = P(y_4|y_3) P(v_2|y_3) \max_{y_1y_2} P(y_1y_2y_3|v_1v_1)$$
            $$[y_3 = c_1, y_1 = c_2] [y_4 = c_1, y_2 = c_2] = [[0.0648, 0.0162], [0.0057, 0.0310]] $$
            $$\max_{y_1y_2y_3} P(y_1y_2y_3y_4|v_1v_1v_2) = \max_{y_3} f(y_3, y_4) = [0.0648, 0.051] $$
            \item
            $$f(y_4) = P(v_2|y_4) \max_{y_1y_2y_3} P(y_1y_2y_3y_4|v_1v_1v_2)$$
            $$[y_4 = c_1, y_4 = c_2] = [0.0162, 0.357] $$
            $$\max_{y_1y_2y_3y_4} P(y_1y_2y_3y_4|v_1v_1v_2v_2) \propto \max_{y_4} f(y_4) = 0.0357 $$
        \end{itemize}

    % subsection applications_of_hmms (end)

% section hidden_markov_models (end)

\newpage

\section{Deep Neural Networks} % (fold)
\label{sec:deep_neural_networks}

    \begin{itemize}
        \item
        Example of scaling w.r.t. depth of the network as opposed to a single hidden layer (Parity function.)
        \item
        Most popular activation functions for units - sigmoid (0 to 1) and tanh (-1 to 1)
        \item
        \textbf{Vanishing gradient}
        \begin{itemize}
            \item
            Vanishing gradient problem: Appearing to converge too soon. Gradient might not result in any change in the initial layers after backpropagation.
            \item
            Sequential squashing of the gradient results in gradually vanishing over time. Chain rule results in multiplying multiple numbers between 0 and 1, which could lead to computing a gradient below the required threshold to take a step in the direction of the minima of the convex function.
            \item
            Compounded product of the output value ranges over multiple layers resultings in the gradient converging to 0. This is because both the output and the gradient for both logistic sigmoid units and hyperbolic tan units are always between 0 and 1 in terms of magnitude.
            \item
            Possible solutions include
            \begin{itemize}
                \item
                Using a larger gradient for the layers close to the output layer.
                \item
                Using pre-trained units.
                \item
                Using recitified linear units.
            \end{itemize}
        \end{itemize}
    \end{itemize}

    \subsection{Rectified Linear Units} % (fold)
    \label{sub:rectified_linear_units}

    \begin{itemize}
        \item
        $h(a) = max(0, a) $
        \item
        Softplus: $h(a) = log(1 + e^a) $, where $a = \sum_i w_i x_i $
        \item
        If gradient is negative, don't back-propagate, if not, then set gradient to 1. This ensures that the gradient doesn't vanish for pathways in the network with $a > 0$.
        \item
        Softplus might also result in a vanishing gradient if the network has several layers.
        \item
        The disadvantage of using the rectified linear function is that it only approximately a linear function at most. Piecewise linear function (Figure \ref{fig:piecewise-linear-function}).
        \begin{figure}[ht]
            \centering
            \includegraphics[width=0.4\textwidth]{piecewise-linear-function}
            \caption{Piecewise Linear Function}
            \label{fig:piecewise-linear-function}
        \end{figure}
    \end{itemize}

    % subsection rectified_linear_units (end)

    \subsection{Maxout Units} % (fold)
    \label{sub:maxout_units}

        \begin{itemize}
            \item Maxout Unit (Figure \ref{fig:maxout-unit})
            \begin{figure}[ht]
                \centering
                \includegraphics[width=0.4\textwidth]{maxout-unit}
                \caption{Maxout Unit}
                \label{fig:maxout-unit}
            \end{figure}
            \item
            Choose $a$ such that $a = max(a_1, a_2, a_3)$, where $a_{ij} = \sum_j w_{ij} z_j $
            \item
            Produces a piecewise linear function, which is also convex.
        \end{itemize}

    % subsection maxout_units (end)

    \subsection{Overfitting} % (fold)
    \label{sub:overfitting}

    \begin{itemize}
        \item
        Due to high expressivity, the data could be very easily overfit.
        \item
        Solutions:
        \begin{itemize}
            \item
            Regularization: Penalizing large weights
            \item
            Data augmentation: Minor variations in the input data to allow the network to train a model that is invariant to small changes in the input data.
            \item
            Dropout: Randomly drop some units from the network during the training phase. Intended to increase network robustness against overfitting to specific datapoints.\\
            Dropout can be viewed as an approximation ensemble learning. Units need to stay in memory for this to work.
        \end{itemize}
    \end{itemize}

    % subsection overfitting (end)

% section deep_neural_networks (end)

\section{Convolutional Neural Networks} % (fold)
\label{sec:convolutional_neural_networks}

    \begin{itemize}
        \item
        Convolution: A mathematical operation on two function that produced a third, which can be viewed as a modified version of one of the initial two functions.
        \item
        Example of convolution: smoothing a function. (Figure \ref{fig:cnn-smoothing})
        \begin{figure}[ht]
            \centering
            \includegraphics[width=0.4\textwidth]{cnn-smoothing}
            \caption{Smoothing function - convolution}
            \label{fig:cnn-smoothing}
        \end{figure}
        \item
        $x$, the original input, is multiplied by a weight factor $w$ to obtain an output (either discrete or continous)
        \item
        Image example: white-to-black produces a black edge and vice-versa. Simple convolution.
        \item
        Could also be used for feature extraction when combined with an activation function.
        \item
        A convolution is a linear subset of units based on a specific weight vector.
        \item
        Activation functions confirm the existence of a patter.
        \item
        Gabor filters: Base weights used for image recognition. Comparisons against them could be use to detect patterns via convolutions.
        \item
        Architecture: Comprised of alternating convolutional and pooling layers. (Figure \ref{fig:cnn-arch})
        \begin{figure}[ht]
            \centering
            \includegraphics[width=0.4\textwidth]{cnn-arch}
            \caption{CNN Architecture}
            \label{fig:cnn-arch}
        \end{figure}
        \item
        Pooling: Aggregating the output of the result of the convolutional layer. It could be using a max/sum/avg function.
        \item
        A max pooling function could simulate an invariant i.e. doesn't matter which section of a given patch a pattern is found in.
        \item
        Pooling function is typically a commutative math operator.
    \end{itemize}

    \subsection{CNN Properties} % (fold)
    \label{sub:cnn_properties}

        \begin{itemize}
            \item
            CNNs won't usea fully connected network. Connections are sparse because only contiguous patches of the image are considered as input features to the convolutional layers.
            \item
            Weights are the same across a single layer, so the fact that they are shared simplifies the network.
        \end{itemize}

    % subsection cnn_properties (end)

    \subsection{MNIST CNN Example} % (fold)
    \label{sub:mnist_cnn_example}

        \begin{itemize}
            \item
            32x32 images, 5x5 patches as features for the convolutional layer, resulting in an abstracted 28x28 input space. Pooling layer of 2x2 dimensions, which means that every 4 units is pooled, reducing the feature space to a 14x14 space.
            \item
            This 14x14 is now treated as the original 32x32 image, and recursively worked on by alternating convolutional and pooling layers.
        \end{itemize}

    % subsection mnist_cnn_example (end)

% section convolutional_neural_networks (end)

\newpage

\section{Recurrent/Recursive Neural Networks} % (fold)
\label{sec:recurrent_recursive_neural_networks}

    \begin{itemize}
        \item
        Typically used when the input data is comprised of sequences, time series, textual data.
        \item
        Weights are shared across time steps.
        \item
        Unit representation: (Figure \ref{fig:rnn-units})
        \begin{figure}[ht]
            \centering
            \includegraphics[width=0.4\textwidth]{rnn-units}
            \caption{RNN Units}
            \label{fig:rnn-units}
        \end{figure}
        \item
        Errors tend to cascade in RNNs, as they are based on sequential data. The errors might also be compounded.
        \item
        HMMs can be a specific implementation of an RNN.
        Belief monitoring can be done as shown below. Figure \ref{fig:rnn}
        $P(Y_1|X_1)$ could be computed as either the sigmoid or softmax (binary/categorical outputs), or a Gaussian distribution (continuous outputs)
        \begin{figure}[ht]
            \centering
            \includegraphics[width=0.7\textwidth]{rnn}
            \caption{RNN - Belief Monitoring}
            \label{fig:rnn}
        \end{figure}
        \item
        Prediction cannot be done since the output $y$ at a given time-step depends on it's input at the same step.
        \item
        HMM: more flexible queries\\
        RNN: more expressive distributions
    \end{itemize}

    \subsection{Bi-directional RNN} % (fold)
    \label{sub:bi_directional_rnn}
        \begin{itemize}
            \item Hindsight queries : Figure \ref{fig:bi-directional_rnn}
            \begin{figure}[ht]
            \centering
            \includegraphics[width=0.7\textwidth]{bi-directional_rnn}
            \caption{Bi-directional RNN - Hindsight queries}
            \label{fig:bi-directional_rnn}
        \end{figure}
            \item
            $P(y_2| x_1, x_2, x_3, x_4)$ is an example of a hindsight prediction that can be made using bi-directional RNNs.
        \end{itemize}
    % subsection bi_directional_rnn (end)

    \subsection{Encoder - Decoder Model} % (fold)
    \label{sub:encoder_decoder_model}

        \begin{itemize}
            \item
            \textbf{Usage in Machine translation:}
            \begin{itemize}
                \item
                Encoding sentences into a vector representation.
                \item
                The encoded segment is the context.
                \item
                While decoding, each step in the model stores what the network has translated thus far, the output for this is the word in the new language.
            \end{itemize}
        \end{itemize}

    % subsection encoder_decoder_model (end)

    \subsection{Recursive Neural Networks} % (fold)
    \label{sub:recursive_neural_networks}

        \begin{itemize}
            \item
            Generalize recurrent neural networks from chains to trees.
            \item
            Useful to identify vector representation of variable length data.
            \item
            Applications for Semantic Parsing. Figure \ref{fig:dependency-parser}
            \begin{figure}[ht]
                \centering
                \includegraphics[width=0.7\textwidth]{dependency-parser}
                \caption{Dependency Parsing}
                \label{fig:dependency-parser}
            \end{figure}
        \end{itemize}

    % subsection recursive_neural_networks (end)

    \subsection{LSTM} % (fold)
    \label{sub:lstm}

        \begin{itemize}
            \item
            Gate units determine if the input is to be kept in memory or discarded
            \item
            Recurrent gate units decide whether or not to preserve the memory contents of the previous time-step
            \item
            Similarly there exists a gated output which decides whether or not the LSTM unit produces an output at the given time-step
        \end{itemize}

    % subsection lstm (end)

% section recurrent_recursive_neural_networks (end)

\section{Autoencoders} % (fold)
\label{sec:autoencoders}

    \begin{itemize}
        \item
        $x \Rightarrow f(x) \Rightarrow g(f(x)) \Rightarrow x$
        \item
        Architecture: Figure \ref{fig:autoencoders}.
        \begin{figure}[ht]
            \centering
            \includegraphics[width=0.7\textwidth]{autoencoders}
            \caption{Autoencoders}
            \label{fig:autoencoders}
        \end{figure}
    \end{itemize}

    \textbf{Linear Autoencoder}
    \begin{itemize}
        \item
        No activation function involved. The hidden layer is infered from the weights.
        \item
        $W_g W_f x = x$
        \item
        $W_g W_f$ would need to be an identity matrix. This derivation is not trivial due to the reduced dimensionality. $W_g$ and $W_f$ are pseudo-inverses of each other.
        \item
        Deriving the weights using the Euclidean norm objective is equivalent to PCA.
    \end{itemize}

    \textbf{Non-linear Autoencoder}
    \begin{itemize}
        \item
        Non-linear manifold
        \item
        $f$ is a non-linear function that can map data into a lower-dimensional representation. $g$ is another non-linear function that does the exact opposite.
    \end{itemize}

    \textbf{Deep Autoencoder:}
    $f$ and $g$ both consist of multiple layers

    \textbf{Sparse Representation:}
    The hidden layer can be just as large as the input layer, but the loss function penalizes non-zero entries, and tries to create the intermediate representation of the data which is sparse.

    \textbf{Denoising Data:}
    Loss function would use the noisy version as input and the clean version as output.

    \textbf{Probabilistic Autoencoder:}
    Illustration: Figure \ref{fig:autoencoders}.
    \begin{figure}[ht]
        \centering
        \includegraphics[width=0.7\textwidth]{probabilistic-autoencoder}
        \caption{Probabilistic Autoencoder}
        \label{fig:probabilistic-autoencoder}
    \end{figure}

    \textbf{Generative Model:}
    Sample the hidden `h' and use it as a generative base by which to generate values of `x'.

% section autoencoders (end)

\section{Generative Adversarial Networks} % (fold)
\label{sec:generative_adversarial_networks}

    \begin{itemize}
        \item
        Similar to Probabilistic Autoencoders, we can use the distribution $P(h)$ to sample new values for $x$.
        \item
        Problem: how to come up with a $P(h)$?
    \end{itemize}

    \textbf{Variational Autoencoder:}
    \begin{itemize}
        \item
        The encoded representation $h$ should be a Gaussian distribution with mean 0 and variance 1.
        \item
        Encoder part of network should approximate the mean and the variance.
        \item
        Illustration: Figure \ref{fig:variation-autoencoder}.
        \begin{figure}[ht]
            \centering
            \includegraphics[width=0.7\textwidth]{variation-autoencoder}
            \caption{Variational autoencoder}
            \label{fig:variation-autoencoder}
        \end{figure}
        \item
        Sampling hinders backpropagation.
    \end{itemize}

    \textbf{Generative Adversarial Network:}
    \begin{itemize}
        \item
        Game theoretic basis
        \item
        Generator vs Discriminator (compete; trained simultaneously)
        \item
        $W_d$ is minimized by a gradient descent algorithm during which $W_g$ remains static.
        Conversely, $W_g$ is maximized by a gradient ascent algorithm during which $W_d$ remains static.
        \item
        Illustration: Figure \ref{fig:gan-architecture}.
        \begin{figure}[ht]
            \centering
            \includegraphics[width=0.5\textwidth]{gan-architecture}
            \caption{GAN Architecture}
            \label{fig:gan-architecture}
        \end{figure}
    \end{itemize}

% section generative_adversarial_networks (end)

\newpage

\section{Ensemble Learning} % (fold)
\label{sec:ensemble_learning}

    \begin{itemize}
        \item
        Method to combine several hypotheses into a single stronger hypotheses.
        \item
        Bagging: Majority voting for a hypothesis, in classification problems
        \item
        Boosting: Computes a weighted majority
    \end{itemize}

    \textbf{Bagging vs. Boosting}
    \begin{itemize}
        \item
        Bagging
        \begin{itemize}
            \item
            Majority vote of a set of hypotheses
            \item
            Assumptions: Hypotheses are independent (error probability independent and roughly the same)
        \end{itemize}
        \item
        Boosting
        \begin{itemize}
            \item
            Weighted majority vote
            \item
            Weighted based on respective error rates
            \item
            Different hypotheses can be obtained by varying the weight of each datapoint in the training set.
        \end{itemize}
    \end{itemize}

    \begin{figure}[ht]
        \centering
        \includegraphics[width=.4\textwidth]{bagging_independent_classifiers}
        \caption{Bagging - Independent Classifiers}
        \label{fig:bagging_independent_classifiers}
    \end{figure}

    \textbf{Random forests:} Bagging, with decision trees (only classification)

    \textbf{Combining classifier parameters: } Might not always provide the benefits of ensemble learning. Combining the predictions usually proves to be more useful. Refer to Figure \ref{fig:parameter_averaging}. The 2 neural networks are independently accurate at computing the XOR, but averaging the weights yields an incorrect computation.
    \begin{figure}[ht]
        \centering
        \includegraphics[width=.4\textwidth]{parameter_averaging}
        \caption{Parameter Averaging}
        \label{fig:parameter_averaging}
    \end{figure}

% section ensemble_learning (end)

\newpage

\section{Stream Learning} % (fold)
\label{sec:stream_learning}

    \begin{itemize}
        \item
        Model continuously refreshed as new training data arrives.
        \item
        Bayesian learning being inherently suited to stream/online learning.
        \item
        Posterior hypothesis at each point is used as the prior belief for the next point.
        \item
        To avoid iterating multiple times over the training data, stochastic gradient descent is used.
    \end{itemize}

    \textbf{Robbins-Monro sufficient conditions}
    \begin{itemize}
        \item
        Choose learning rate $a_n$ such that $\sum_{n=1}^{\infty} a_n = \infty$, but $\sum_{n=1}^{\infty} (a_n)^2 < \infty$
        \item
        Example of $a_n$ is $\frac{1}{n} $
    \end{itemize}

% section stream_learning (end)

\newpage

\section{Formulae} % (fold)
\label{sec:formulae}

    \begin{itemize}

        \item
        KNN:
        $$y_x = mode({y_{x'}| x' \in knn(x)})$$

        \item
        Weighted KNN (used for regression):
        $$y_x = \sum_{x' \in knn(x)} w(x, x') y_{x'}$$

        \item
        Euclidean Loss:
        $$L(w) = \frac{1}{2} \sum_{n=1}^{N} (y(x_n, w) - t_n)^2 $$

        \item
        Linear regression Convex Optimization Objective:
        $$w = A^{-1}b$$
        where
        $$A = \sum_{n=1}^{N} \bar{x_n} \bar{x_n}^T$$ and $$b = \sum_{n=1}^{N} t_n \bar{x_n}$$

        \item
        Regularized Ridge Regression:
        $$w = argmin_w \frac{1}{2} \sum_{n=1}^{N} (y(x_n, w) - t_n)^2 + \lambda \lVert w \rVert $$
        equivalent to
        $$w = (\lambda I + A)^{-1}b$$

        \item
        Joint distribution marginalization
        $$Pr(A = a) = \sum_b Pr(A = a \wedge B = b) $$

        \item
        Conditional Probability
        $$Pr(A|B) = \frac{Pr(A \wedge B)}{Pr(B)} $$
        implying the chain rule,
        $$Pr(A \wedge B) = Pr(A|B) Pr(B)$$

        \item
        Bayes Rule:
        $$Pr(B|A) = \frac{Pr(A|B) Pr(B))}{Pr(A)} $$

        \item
        Bayesian Learning: \\
        Prior: $Pr(H)$ \\
        Likelihood: $Pr(e|H)$ \\
        Evidence: $e = <e_1, e_2, ... e_n>$
        $$Pr(H|e) = k Pr(e|H) Pr(H)$$
        where $k$ is a normalizing constant equivalent to $Pr(e)$ in Bayes Rule

        For a given hypothesis,
        $$Pr(e|h) = \prod_n P(e_n|h)$$

        \item
        Maximum a posteriori (MAP):
        $$h_{map} = argmax_{h_i} P(h_i|e)$$
        $$h_{map} = argmax_{h_i} P(h) P(e|h_i)$$

        \item
        Maximum likelihood estimation:
        Same as MAP, except that all the hypotheses have an equal prior probability.
        $$h_{mle} = argmax_{h_i} P(e|h_i)$$
        $$h_{mle} = argmax_{h_i} \sum_n \log P(e_n|h)$$

        \item
        Gaussian PDF:
        $$\frac{1}{\sqrt{2\pi\sigma}} e^{- \frac{(x - \mu)^2}{2 \sigma^2}}$$

        \item
        Bias - Variance tradeoff
        $$E[loss] = bias^2 + variance + noise$$

        \item
        Bayesian Linear Regression:
        $$\bar{w} = \sigma^{-2} A^{-1} \bar{X}^T y $$
        where
        $$A = \sigma^{-2} \bar{X}^T \bar{X} + \Sigma^{-1}$$

        \item
        Mixture of Gaussians Posterior Distribution:
        $$Pr(C_k|x) = \frac{1}{1 + e^{-(w^Tx + w_0)}}$$
        where
        $$w = \Sigma^{-1} (\mu_k - \mu_j)$$
        $$w_0 = -\frac{1}{2} \mu_k^T \Sigma^{-1} \mu_k + \frac{1}{2} \mu_j^T \Sigma^{-1} \mu_j + \ln\frac{\pi_k}{\pi_j}$$

        \item
        Mixture of Gaussians MLE Solution:
        $$\pi = \frac{\sum_{n \in c_1} 1}{N}$$
        $$\mu_1 = \frac{\sum_{n \in c_1} x_n}{N}; \mu_2 = \frac{\sum_{n \in c_2} x_n}{N}$$
        $$\Sigma = \frac{S_1 + S_2}{N}$$
        where
        $$S_1 = \sum_{n \in c_1} (x_n - \mu_1) (x_n - \mu_1)^T; S_2 = \sum_{n \in c_2} (x_n - \mu_2) (x_n - \mu_2)^T$$

        \item
        Logistic Regression Loss function:
        $$L(w) = - \sum_n y_n \ln \sigma(w^T \bar{x}) + (1 - y_n) \ln (1 - \sigma(w^T \bar{x})) $$

        \item
        Logistic Regression Gradient descent:
        $$w = w - H^{-1} \nabla L(w)$$
        where H is the Hessian matrix
        $$H = \bar{X} R \bar{X}^T$$
        R is a diagonal matrix with $R_{ii} = \sigma_i(1 - \sigma_i)$ and $\sigma_i = \sigma(w^T \bar{x}_n)$

        \item
        Perceptron Learning Gradient Descent:
        $$w = w - \eta \nabla E$$
        where $$E = - \sum_{(x_n, y_n) \in M} y_n w^T x_n$$
        and M is a set of misclassified examples and $y \in (1, -1) \forall y$

        \item
        Sigmoid perceptron learning min squared error:
        $$E(w) = \frac{1}{2} \sum_n (y_n - \sigma(w^T x_n))^2 $$
        $Also, \nabla(\sigma) = \sigma(1 - \sigma)$

        \item
        Multi-layer neural network error function:
        $$E(w) = \frac{1}{2} \lVert f(x_n, W) - y_n \rVert_2^2 $$

        \item
        Multi-layer neural network Gradient descent
        $$w_{ji} = w_{ji} - \eta \frac{\partial E_n}{\partial w_{ji}} $$

        \item
        Backpropagation algorithm
        \begin{equation*}
        \delta_j = \left\{
        \begin{array}{@{}ll@{}}
        h'(a_j) (z_j - y_j), & \text{base case, if j is an output unit} \\
        h'(a_j) \sum_k w_{kj}\delta_k, & \text{recursion, if j is a hidden unit}
        \end{array}\right.
        \end{equation*}

        \item
        Kernel function:
        $$k(x, x') = \phi(x)^T \phi(x') $$

        \item
        Weights:
        $$w = - \frac{1}{\lambda} \sum_n (w^T \phi(x_n) - y_n) \phi(x_n) $$
        $$\Phi = [\phi(x_1), \phi(x_2) ... \phi(x_N)] $$
        $$w = \Phi a $$
        $$a_n = - \frac{1}{\lambda} (w^T \phi(x_n) - y_n) $$

        \item
        Gram matrix
        $$K = \Phi^T \Phi$$
        Optimal $a$
        $$a = (K + \lambda I)^{-1} y $$

        \item
        Kernel prediction:
        $$y_* = \phi(x_*)^T w $$
        $$y_* = \phi(x_*)^T \Phi a $$
        $$y_* = k(x_*, X) (K + \lambda I)^{-1} y $$
        where $(X, y)$ is the training set and $(x_*, y_*)$ is the test instance

        \item
        Given that $k_1(x, x')$ and $k_2(x, x')$ are valid kernels, the below kernels are valid too
        \begin{itemize}
            \item
            $ck_1(x, x') \forall c>0 $
            \item
            $f(x)k_1(x, x')f(x') $
            \item
            $q(k_1(x, x')) $ where q is a polnomial with co-efficients $\geq 0$
            \item
            $exp(k_1(x, x')) $
            \item
            $k_1(x, x') + k_2(x, x')$
            \item
            $k_1(x, x')k_2(x, x')$
            \item
            $k_3(\phi(x), \phi(x'))$
            \item
            $x^T A x'$ where A is symmetric positive semi-definite
            \item
            $k_a(x_a, x'_a) + k_b(x_b, x'_b)$
            \item
            $k_a(x_a, x'_a)k_b(x_b, x'_b)$ where $x = \left(\begin{array}{c} x_a\\ x_b\\ \end{array} \right)$
        \end{itemize}

    \end{itemize}

    % section formulae (end)

\end{document}


\documentclass[a4paper,UKenglish]{lipics-v2016}
\usepackage{microtype}
\usepackage{bussproofs}
\usepackage{stmaryrd}

\newcommand{\new}{\mathsf{new}}
\newcommand{\for}{\mathrm{for }}
\newcommand{\Ncal}{\mathcal{N}}
\newcommand{\interp}[1]{\llbracket #1 \rrbracket}
\newcommand{\interpp}[1]{\{\!| #1 |\!\}}
\newcommand{\from}{\leftarrow}
\newcommand{\maps}{\colon}
\newcommand{\Th}{\mathrm{Th}}
\newcommand{\Gph}{\mathrm{Gph}}
\newcommand{\FinSet}{\mathrm{FinSet}}
\newcommand{\FPGphCat}{\mathrm{FPGphCat}}
\newcommand{\Set}{\mathrm{Set}}
\newcommand{\Cat}{\mathrm{Cat}}
\newcommand{\Calc}{\mathrm{Calc}}
\newcommand{\Mon}{\mathrm{Mon}}
\newcommand{\op}{\mathrm{op}}
\newcommand{\NN}{\mathbb{N}}
\newcommand{\pic}{$\pi$-calculus}

\title{Representing operational semantics with enriched Lawvere theories}
\author[1]{
Michael Stay
}
\author[2]{
L.\ G.\ Meredith
}
\affil[1]{
  Pyrofex Corp., Kirkland, WA, USA\\
  {\tt stay@pyrofex.net}
}
\affil[2]{
{RChain Cooperative, Seattle, WA, USA}\\
{\tt greg@rchain.coop}
}
\Copyright{Michael Stay, Lucius Gregory Meredith}
\subjclass{F.1.2 Modes of computation, F.3.2 Semantics of Programming Languages, F.4 Mathematical Logic and Formal Languages, D.1.3 Concurrent Programming, D.3.1 Formal Definitions and Theory, D.3.3 Language Constructs and Features}
\keywords{Concurrent combinator, nominal, pi calculus}

%Editor-only macros:: begin (do not touch as author)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\EventEditors{John Q. Open and Joan R. Acces}
\EventNoEds{2}
\EventLongTitle{42nd Conference on Very Important Topics (CVIT 2016)}
\EventShortTitle{CVIT 2016}
\EventAcronym{CVIT}
\EventYear{2016}
\EventDate{December 24--27, 2016}
\EventLocation{Little Whinging, United Kingdom}
\EventLogo{}
\SeriesVolume{42}
\ArticleNo{23}
% Editor-only macros::end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\maketitle
\begin{abstract}
  \noindent
  Many term calculi, like $\lambda$-calculus or {\pic}, involve
  binders for names, and the mathematics of bound variable names is
  subtle.  Sch\"onfinkel introduced the SKI combinator calculus in
  1924 to clarify the role of quantified variables in intuitionistic
  logic by eliminating them. Yoshida demonstrated how to
  eliminate the bound names coming from the input prefix in the
  asynchronous {\pic}, but her combinators still depend on the $\new$
  operator to bind names.  Recently, Meredith and Stay
  showed how to modify Yoshida's combinators by replacing $\new$ and
  replication with reflective operators to provide the first
  combinator calculus with no bound names into which the asynchronous
  {\pic} has a faithful embedding. Here we provide an alternative set
  of combinators built from $\mathsf{SKI}$ plus reflection that also
  eliminates all nominal phenomena, yet provides a faithful
  embedding  of a reflective higher-order pi calculus.
  We show that with the nominal features effectively
  eliminated as syntactic sugar, multisorted Lawvere theories enriched
  over graphs suffice to capture the operational semantics of the
  calculus.
\end{abstract}
\EnableBpAbbreviations

\section{Introduction}
Many term calculi, like $\lambda$-calculus or {\pic}, involve binders
for names, and the mathematics of bound variable names is subtle.
Sch\"onfinkel introduced the SKI combinator calculus in 1924 to
clarify the role of quantified variables in intuitionistic logic by
eliminating them \cite{finkel}. Yoshida demonstrated how to eliminate
the bound names coming from the input prefix in the asynchronous
{\pic}, but her combinators still depend on the $\new$ operator to
bind names. Curry developed Sch\"onfinkel's ideas much
further. Recently, Meredith and Stay \cite{Rhocomb} showed how to
modify Yoshida's combinators by replacing $\new$ and replication with
reflective operators to provide the first combinator calculus with no
bound names into which the asynchronous {\pic} has a faithful
embedding of a reflective higher-order pi calculus.
Here we provide an alternative set of combinators built
from $\mathsf{SKI}$ plus reflection that also eliminates all nominal
phenomena, yet provides a faithful embedding.

The recent work by Jamie Gabbay and Andrew Pitts
\cite{DBLP:journals/fac/GabbayP02} and others
\cite{DBLP:journals/jcss/Clouston14} on nominal set theory has put the
study of bound names and substitution on a much nicer foundation, at
the cost of an increase in the complexity of the semantic
framework. It interprets nominal phenomena in terms of atoms in
Fraenkl-Mostowski set theory. Clouston's work in particular makes
evident the additional machinery needed to interpret nominal phenomena
as Lawvere theories. On the other hand, with the nominal features
effectively eliminated as syntactic sugar, we show that multisorted
Lawvere theories enriched over graphs suffice to capture the
operational semantics of the calculus.

\section{Previous work}

There is a long history and an enormous body of work on modeling term rewriting and operational semantics with various notions of category enriched over category-like structures; we only have room here for a sampling.  L\"uth and Ghani \cite{DBLP:conf/ctcs/LuethG97} use poset-enriched categories to study the modularity of strong normalization.  One approach to nominality is the one we mentioned in the introduction; a different approach deals with nominal issues by allowing ``funtion types'' in the signature:  Seely \cite{DBLP:conf/lics/Seely87}
% R. A. G. Seely, Modeling computations: a 2-categorical framework, in Proc. Symp. Logic Comp. Sci. 1987, Computer Society of the IEEE, pp. 65–71. Also available at http://www.math.mcgill.ca/rags/WkAdj/LICS.pdf.
suggested using 2-categories for modeling the denotational semantics of lambda calculus in Scott domains to capture the adjunction between $\beta$ reduction and $\eta$ conversion;  Hilken \cite{DBLP:journals/tcs/Hilken96}
% B. Hilken, Towards a proof theory of rewriting: the simply-typed 2λ- calculus, Theor. Comp. Sci. 170 (1996), 407–444. Also available at http://math.ucr.edu/home/baez/qg-winter2007/hilken_2lambda_calculus.ps
expands Seely's work by exploring the proof theory using categorical logic; and Hirschowitz \cite{DBLP:journals/corr/Hirschowitz13}
% Tom Hirschowitz, Cartesian closed 2-categories and permutation equivalence in higher-order rewriting, Logical Methods in Computer Science, IfCoLog (Interna- tional Federation of Computational Logic), 9 (3) (2013), 10–28. Also available at https://hal.archives-ouvertes.fr/hal-00540205/file/macy.pdf
generalizes algebraic signatures to cartesian closed 2-signatures.  A third approach is to model substitution explicitly: Stell \cite{Stell}
% http://www.comp.leeds.ac.uk/jgs/caen.ps
considered sesquicategories for term rewriting; in his system, objects are finite sets of variables, morphisms are substitutions, and 2-morphisms are roughly rewrite rules.

\section{Gph-enriched categories}
Here we review some standard definitions and results in enriched category theory; see \cite{CIS-335497}, \cite{Power99EnrichedLawvereTheories}, \cite{DBLP:journals/acs/LackR11}, and \cite{Trimble} for more details.

A {\bf directed multigraph with self loops}, hereafter {\bf graph}, consists of a set $E$ of edges, a set $V$ of vertices, two functions $s,t\maps E \to V$ picking out the source and target of each edge, and a function $a\maps V \to E$ such that $s\circ a$ and $t \circ a$ are both the identity on $V$---that is, $a$ equips each vertex in $V$ with a chosen self loop.  There are no constraints on $E, V, s,$ or $t$, so a graph may have infinitely many vertices and infinitely many edges between any pair of vertices.  A {\bf graph homomorphism} from $(E, V, s, t, a)$ to $(E', V', s', t', a')$ is a pair of functions $(\epsilon\maps E \to E', \upsilon\maps V \to V')$ such that $\upsilon\circ s = s' \circ \epsilon$ and $\upsilon\circ t = t' \circ \epsilon$.  {\bf Gph} is the category of graphs and graph homomorphisms.  Gph has finite products: the terminal graph is the graph with one vertex and one loop, while the product of two graphs $(E, V, s, t, a) \times (E', V', s', t', a')$ is $(E \times E', V \times V', s \times s', t\times t', a \times a').$

A {\bf Gph-enriched category} consists of
\begin{itemize}
  \item a set of objects;
  \item for each pair of objects $x, y,$ a graph $\hom(x,y);$
  \item for each triple of objects $x, y, z,$ a composition graph homomorphism $\circ\maps \hom(y, z) \times \hom(x, y) \to \hom(x, z);$ and
  \item for each object $x,$ a vertex of $\hom(x, x),$ the identity on $x,$
\end{itemize}
such that composition is associative, and composition and the identity obey the unit laws.  A Gph-enriched category has finite products if the underlying category does.

Any category is trivially Gph-enrichable by treating the elements of the hom sets as vertices and adjoining a self loop to each vertex.  The category Gph is nontrivially Gph-enriched: Gph is a topos, and therefore cartesian closed, and therefore enriched over itself.  Given two graph homomorphisms $F, F'\maps (E, V, s, t, a) \to (E', V', s', t', a'),$ a {\bf graph transformation} assigns to each vertex $v$ in $V$ an edge $e'$ in $E'$ such that $s'(e') = F(v)$ and $t'(e') = F'(v).$  Given any two graphs $G$ and $G',$ there is an exponential graph $G'^G$ whose vertices are graph homomorphisms between them and whose edges are graph transformations.

A {\bf Gph-enriched functor} between two Gph-enriched categories $C, D$ is a functor between the underlying categories such that the graph structure on each hom set is preserved, {\em i.e.} the functions between hom sets are graph homomorphisms between the hom graphs.

Let $S$ be a finite set, $\FinSet$ be a skeleton of the category of finite sets and functions between them, and $\FinSet/S$ be the category of functions into $S$ and commuting triangles.  A {\bf multisorted Gph-enriched Lawvere theory}, hereafter {\bf Gph-theory} is a Gph-enriched category with finite products Th equipped with a finite set $S$ of {\bf sorts} and a Gph-enriched functor $\theta\maps \FinSet^{\op}/S \to \Th$ that preserves products strictly.  Any Gph-theory has an underlying multisorted Lawvere theory given by forgetting the edges of each hom graph.

A {\bf model} of a Gph-theory Th is a Gph-enriched functor from Th to Gph that preserves products up to natural isomorphism.  A {\bf homomorphism of models} is a braided Gph-enriched natural transformation between the functors.  Let FPGphCat be the 2-category of small Gph-enriched categories with finite products, product-preserving Gph-functors, and braided Gph-natural transformations.  The forgetful functor $U\maps \FPGphCat[\Th, \Gph] \to \Gph$ that picks out the underlying graph of a model has a left adjoint that picks out the free model on a graph.

Gph-enriched categories are part of a spectrum of 2-category-like structures.  A strict 2-category is a category enriched over Cat with its usual product.  Sesquicategories are categories enriched over Cat with the ``funny'' tensor product \cite{Lack2010}; a sesquicategory can be thought of as a 2-category where the interchange law does not hold.  A Gph-enriched category can be thought of as a sesquicategory where 2-morphisms (now edges) cannot be composed.  Any strict 2-category has an underlying sesquicategory, and any sesquicategory has an underlying Gph-enriched category; these forgetful functors have left adjoints.

\section{Gph-theories as models of computation}

Lawvere theories and their generalizations are categories with infinitely many objects and morphisms, but most theories of interest are finitely generated.  A presentation of the underlying multisorted Lawvere theory of a finitely-generated Gph-theory is a signature for a term calculus, consisting of a set of sorts, a set of term constructors, and a set of equations, while the edges in the hom graphs of the theory encode the reduction relation.

Here is a presentation of the SKI combinator calculus as a Gph-theory:
\begin{itemize}
  \item one sort $T$, for terms
  \item term constructors
  \[\begin{array}{rl}
    S&:1 \to T\\
    K&:1 \to T\\
    I&:1 \to T\\
    (-\; -)&: T^2 \to T\\
  \end{array}\]
  \item structural congruence (no equations)
  \item rewrites
  \[\begin{array}{rl}
    \sigma&:(((S\; x)\; y)\; z) \Rightarrow ((x\; z)\; (y\; z))\\
    \kappa&:((K\; y)\; z) \Rightarrow y\\
    \iota&:(I\; z) \Rightarrow z\\
  \end{array}\]
\end{itemize}
where in the rewrites we have used expressions like $((K\; y)\; z)$ as shorthand for
\[ T\times T \xrightarrow{\mbox{\tiny left}^{-1}} 1\times T \times T \xrightarrow{K \times T \times T} T\times T \times T \xrightarrow{(-\;-)\times T} T\times T \xrightarrow{(-\;-)} T. \]

A model $M$ of this Gph-theory in Gph picks out a graph $M(T)$ of terms and rewrites.  It picks out three special vertices $S,K,$ and $I$ of $M(T)$; it equips $M(T)$ with a graph homomorphism from $M(T)^2$ to $M(T)$ that says for every pair of vertices $(u,v),$ there is a vertex $(u\;v)$, and similarly for edges; and it equips $M(T)$ with graph transformations asserting the existence of an edge out of a reducible expression to the term it reduces to.

That this Gph-theory captures the operational semantics of the SKI calculus is almost definitional: there is an edge between distinct vertices in the free model on the empty graph if and only if the source vertex is reducible to the target vertex in a single step.

It is straightforward to verify that Gph-theories suffice to capture the operational semantics of any calculus where every context is a reduction context.  This restriction on reduction contexts is a consequence of the fact that models map term constructors to graph homomorphisms: given a model $M$, a graph homomorphism $F\maps M(T) \to M(T)$, and an edge $e\maps t_1 \to t_2,$ there is necessarily an edge $F(e)\maps F(t_1) \to F(t_2).$

\section{Gph-theory for SKI with the weak head normal form evaluation strategy}
\label{whnf}
In modern programming languages, many contexts are {\em not} reduction contexts.  In Haskell, for instance, there are no reductions under a lambda abstraction: even if $t_1$ reduces to $t_2$ as a program, the term $\backslash x \to t_1$ does not reduce to $\backslash x \to t_2.$

Gph-theories can still capture the operational semantics of calculi with restrictions on reduction contexts by introducing term constructors that explicitly mark the reduction contexts.  For example, suppose that we want an evaluation strategy for the SKI calculus that only reduces the leftmost combinator when it has been applied to sufficiently many arguments, {\em i.e.} we want the {\em weak head normal form}; we can accomplish this by introducing a term constructor $R\maps T \to T$ that explicitly marks the reduction contexts.  We then add a structural congruence rule for propagating the context and modify the existing reduction rules to apply only to marked contexts.

\begin{itemize}
  \item one sort $T$, for terms
  \item term constructors
  \[\begin{array}{rl}
    S&:1 \to T\\
    K&:1 \to T\\
    I&:1 \to T\\
    (-\; -)&: T^2 \to T\\
    R&:T \to T\\
  \end{array}\]
  \item structural congruence
  \[\begin{array}{rl}
    R(x\; y) &= (Rx\; y)\\
  \end{array}\]
  \item rewrites
  \[\begin{array}{rl}
    \sigma&:(((RS\; x)\; y)\; z) \Rightarrow ((Rx\; z)\; (y\; z))\\
    \kappa&:((RK\; y)\; z) \Rightarrow Ry\\
    \iota&:(RI\; z) \Rightarrow Rz\\
  \end{array}\]
\end{itemize}

\begin{theorem}
  Let $t$ be a term in which $R$ does not appear.  Then $Rt$ reduces to $Rt',$ where $t'$ is the weak head normal form of $t.$
\end{theorem}

\begin{proof}
If we form the term $Rt$ where $t$ contains no uses of $R$, no reductions will ever take place in the right-hand argument of an application: the structural congruence and rewrite rules enforce that the $R$ context can only move to the left term in an application, never the right.  The result follows by induction on the number of steps to reach $t'.$
\end{proof}

\section{Explicit reduction contexts as gas}
The Ethereum \cite{wood2014ethereum} and RChain \cite{RChain} projects are building virtual machines on the blockchain.  Both use the concept of a linear resource called ``gas'' (as in gasoline) that is consumed as the virtual machine executes.  Gph-theories can capture the operational semantics of a calculus where reduction contexts are consumable, and thus play a role similar to that of gas \cite{DBLP:journals/corr/StayM15}.

\begin{itemize}
  \item one sort $T$, for terms
  \item term constructors
  \[\begin{array}{rl}
    S&:1 \to T\\
    K&:1 \to T\\
    I&:1 \to T\\
    (-\; -)&: T^2 \to T\\
    R&:T \to T\\
  \end{array}\]
  \item structural congruence
  \[\begin{array}{rl}
    R(x\; y) &= (Rx\; y)\\
  \end{array}\]
  \item rewrites
  \[\begin{array}{rl}
    \sigma&:(((RS\; x)\; y)\; z) \Rightarrow ((x\; z)\; (y\; z))\\
    \kappa&:((RK\; y)\; z) \Rightarrow y\\
    \iota&:(RI\; z) \Rightarrow z\\
  \end{array}\]
\end{itemize}

\begin{theorem}
  Let $t$ be a term in which $R$ does not appear; let $t'$ be the weak head normal form of $t$; let $m$ be the number of steps by which $Rt$ reduces to $Rt'$ in the calculus of section \ref{whnf}; and let $n\ge m$.  Then in this calculus, $R^n t$ reduces to $R^{n-m}t'$ in $m$ steps.
\end{theorem}

\begin{proof}
As before, if we form the term $Rt$ where $t$ contains no uses of $R$, no reductions will ever take place in the right-hand argument of an application.  Each application of the reduction rules reduces the number of $R$s by one, and structural equivalence preserves the number of $R$s.  The result follows by induction on the number of steps to reach $t'.$
\end{proof}

\section{Gph-theory for a pi calculus variant}
\label{rhocomb}
Gph-theories can capture the operational semantics of concurrent calculi as well as serial calculi like SKI above.

  Meredith and Radestock \cite{DBLP:journals/entcs/MeredithR05} describe a reflective higher-order variant of pi calculus we call the RHO calculus.  Rather than the usual replication and $\new$ operators, they have quoting and unquoting operators.  Quoting turns a process into a name and unquoting does the opposite; freshness of names is obtained using a type discipline.  They prove that there is a faithful embedding of the monadic asynchronous pi calculus into the RHO calculus.

\subsection{The RHO calculus}
\subsubsection{Syntax}
\[\begin{array}{rlr}
  P, Q &::= 0 & \mbox{the stopped process}\\
  &| \quad \for(y \from x)P & \mbox{input guarded process} \\
  &| \quad x!P & \mbox{output process}\\
  &| \quad P \;|\; Q & \mbox{parallel composition}\\
  &| \quad *x & \mbox{deference}\\
  &\\
  x, y &::= \&P & \mbox{quotation}\\
\end{array}\]

Note that in the original rho-calculus papers the notation was
somewhat different. The quotation and dereference constructions were
originally written, $\ulcorner P \urcorner$ and $\urcorner x \ulcorner$,
respectively. Here we have adopted a more programmer
friendly style employing the $\&$ and $*$ of the $\mathsf{C}$ programming
language for reference (quotation) and dereference, respectively. Input
guards which were written with a whimper $?$ in more traditional process calculi style  are now written in for-comprehension style as adopted
in languages like $\mathsf{Scala}$; e.g. $x?(y)P$ is written
here $\mathsf{for}( y \from x )P$.

\subsubsection{Free and bound names}
\[\begin{array}{rl}
FN(0) &= \emptyset \\
FN(\for(y \from x)P) &= \{x\}\cup (FN(P)\backslash \{y\}) \\
FN(x!P) &= \{x\}\cup FN(P) \\
\end{array}\quad\quad
\begin{array}{rl}
FN(P|Q) &= FN(P)\cup FN(Q) \\
FN(*x) &= \{x\}
\end{array}\]
\subsubsection{Structural congruence}
Structural (process) congruence is the smallest congruence $\equiv$ containing $\alpha$-equivalence and making $(|, 0)$ into a commutative monoid.
\subsubsection{Name equivalence}
Name equivalence is the smallest equivalence relation $\equiv_N$ on names such that
\begin{center}
  \AXC{} \UIC{$\&*x \equiv_N x$} \DP and \AXC{$P \equiv Q$} \UIC{$\&P \equiv_N \&Q$} \DP.
\end{center}
\subsubsection{Substitution}
Syntactic substitution:
\[\begin{array}{rl}
  (0)\{\&Q/\&P\} &= 0\\
  (\for (y \from x) R)\{\&Q/\&P\} &= \for (z \from (x\{\&Q/\&P\})) (R\{z/y\}\{\&Q/\&P\})\\
  (x!R)\{\&Q/\&P\} &= (x\{\&Q/\&P\})!(R\{\&Q/\&P\})\\
  (R|S)\{\&Q/\&P\} &= (R\{\&Q/\&P\}) \;|\; (S\{\&Q/\&P\})\\
  (*x)\{\&Q/\&P\} &= \left\{\begin{array}{rl}
    *\&Q & \mbox{when } x \equiv_N \&Q\\
    *x & \mbox{otherwise,}
  \end{array}\right.
\end{array}\]
where
\[ x\{\&Q/\&P\} = \left\{\begin{array}{rl}
                  \&Q & \mbox{if } x\equiv_N \&P \\
                  x & \mbox{ otherwise}
                \end{array}\right.\]
and, in the rule for input, $z$ is chosen to be distinct from $\&P, \&Q,$ the free names in $Q,$ and all the names in $R.$

Semantic substitution, for use in $\alpha$-equivalence:
\[ (*x)\{\&Q/\&P\} = \left\{\begin{array}{rl}
  Q & \mbox{when } x \equiv_N \&Q\\
  *x & \mbox{otherwise}
\end{array}\right. \]

\subsubsection{Reduction rules}
We use $\to$ to denote single-step reduction.
\begin{center}
\AXC{$x_0 \equiv_N x_1$}
\UIC{$\for(y \from x_1)P \;|\; x_0!Q \quad \to\quad P\{\&Q / y\}$} \DP \quad \quad
\end{center}

\begin{center}
\AXC{$P\to P'$}
\UIC{$P\;|\; Q \quad \to \quad P' \;|\; Q$} \DP
\end{center}

\begin{center}
\AXC{$P\equiv P'$} \AXC{$P' \to Q'$} \AXC{$Q' \equiv Q$}
\TIC{$P\to Q$} \DP
\end{center}
\subsection{RHO combinators}
We can define an embedding $\interp{-}$ of closed RHO calculus terms into a set of RHO combinators.  We follow Milner \cite{milner91polyadicpi} in thinking of an input-prefixed process $\for(x \from y)P$ as consisting of two parts: the first names the channel $y$ on which the process is listening, while the second describes the continuation $\lambda x.P$ in terms of an abstracted name.  The right hand side of the communication rule, in effect, applies the continuation to the name to be substituted.  Since the only bound names in the RHO calculus come from input prefixing, we can completely eliminate bound names by using abstraction elimination on the continuation.  Like the weak head normal form SKI calculus above, this combinator calculus uses a linear resource $C$ to reify reduction contexts.

A Gph-theory for the operational semantics of these combinators has:
\begin{itemize}
  \item one sort $T$, for terms
  \item term constructors
    \[\begin{array}{rl}
      C &: 1 \to T \\
      0 &: 1 \to T \\
      | &: 1 \to T \\
      \for &: 1 \to T \\
      ! &: 1 \to T \\
      \& &: 1 \to T \\
    \end{array}\quad\quad
    \begin{array}{rl}
      * &: 1 \to T \\
      S &: 1 \to T \\
      K &: 1 \to T \\
      I &: 1 \to T \\
      () &: T \times T \to T \\
    \end{array}\]
  \item structural congruence rules
    \[\begin{array}{rll}
      ((|\; 0)\; P) &= P & \mbox{unit law}\\
      ((|\; ((|\; P)\; Q))\; R) &= ((|\; P)\; ((|\; Q)\; R) & \mbox{associativity}\\
      ((|\; P)\; Q) &= ((|\; Q)\; P) &\mbox{commutativity}\\
    \end{array}\]
  \item reduction rules
    \[\begin{array}{ll}
      \sigma\maps (((S\; P)\; Q)\; R) \Rightarrow ((P\; R)\; (Q\; R)) & \mbox{action of }S \\
      \kappa\maps ((K\; P)\; Q) \Rightarrow P & \mbox{action of }K\\
      \iota\maps (I\; P) \Rightarrow P & \mbox{action of }I\\
      \xi\maps ((|\; C)\; ((|\; ((\for\; (\&\; P))\; Q))\; ((!\; (\&\; P))\; R))) \Rightarrow ((|\; C)\; (Q\; (\&\; R))) & \mbox{communication}\\
      \epsilon\maps ((|\; C)\;(*\; (\&\; P))) \Rightarrow ((|\; C)\; P) & \mbox{evaluation} \\
    \end{array}\]
\end{itemize}

\subsection{Embeddings}
We define an interpretation function $\interp{-}$ from RHO calculus terms into RHO combinators by

\[\begin{array}{rl}
  \interp{0} &= 0 \\
  \interp{\for(x \from \&P)Q} &= ((for\; (\&\; \interp{P}))\; \interp{Q}_x)\\
  \interp{\&P!Q} &= ((!\; (\&\; \interp{P}))\; \interp{Q}) \\
  \interp{P|Q} &= ((|\; \interp{P})\; \interp{Q})\\
  \interp{*\&P} &= (*\; (\&\; \interp{P}))
\end{array}\]
where $\interp{-}_x$ eliminates the free name $x:$
\[\begin{array}{rl}
  \interp{P}_x &= (K\; \interp{P}) \mbox{ where $x$ is not free in } P \\
  \interp{\for(y \from \&P)Q}_x &= ((S\; ((S\; (K for))\; ((S\; (K\; \&))\; \interp{P}_x)))\; \interp{\interp{Q}_y}_x) \\
  \interp{\&P!Q}_x &= ((S\; ((S\; (K\; !))\; ((S\; (K\; \&))\; \interp{P}_x)))\; \interp{Q}_x) \\
  \interp{P|Q}_x &= ((S\; ((S\; (K\; |))\; \interp{P}_x))\; \interp{Q}_x) \\
  \interp{*\&P} &= \left\{\begin{array}{ll}
    ((S\; (K\; *))\; I) & \mbox{when } \&P \equiv_N x\\
    ((S\; (K\; *))\; ((S (K\; \&))\; \interp{P}_x) & \mbox{otherwise.}
  \end{array}\right.
\end{array}\]

Consider the following sorting on RHO combinators:
\[\begin{array}{rl}
  C &: W\\
  0 &: W\\
  | &: W \Rightarrow W \Rightarrow W\\
  \for &: N \Rightarrow (N \Rightarrow W) \Rightarrow W\\
  ! &: N \Rightarrow W \Rightarrow W\\
\end{array}\quad\quad
\begin{array}{rl}
  \& &: W \Rightarrow N\\
  * &: N \Rightarrow W\\
  S &: \forall X,Y,Z.(Z \Rightarrow Y \Rightarrow X) \Rightarrow (Z \Rightarrow Y) \Rightarrow Z \Rightarrow X\\
  K &: \forall X,Y.X \Rightarrow Y \Rightarrow X\\
  I &: \forall X.X \Rightarrow X\\
\end{array}\]

The left- and right-hand sides of each of the structural congruence and rewrite rules have the sort $W,$ the interpretation of any RHO calculus term has the sort $W,$ and the result of eliminating an abstraction has the sort $N \Rightarrow W.$

We define an interpretation function $\interpp{-}$ from $W$-sorted RHO combinators not containing $C$ into the RHO calculus by
\[\begin{array}{rl}
  \interpp{0} &= 0\\
  \interpp{P\;|\;Q} &= \interpp{P} \;|\; \interpp{Q}\\
  \interpp{((\for\; (\&\; P))\; Q)} &= for (\&\interpp{R} \leftarrow \&\interpp{P})\interpp{(Q\; (\&\; R))}\\
  \interpp{((!\; (\&\; P))\; Q)} &= \&\interpp{P}!\interpp{Q}\\
  \interpp{(*(\&\; P))} &= *\&\interpp{P}\\
  \interpp{(((S\; P)\; Q)\; R)} &= \interpp{((P\; R)\; (Q\; R))}\\
  \interpp{((K\; P)\; Q)} &= \interpp{P}\\
  \interpp{(I\; P)} &= \interpp{P}
\end{array}\]
where $R$ is any $W$-sorted RHO combinator.

Some simple calculation shows that
\begin{theorem}
\label{roundtrip}
  $\interpp{\interp{P}}$ is $\alpha$-equivalent to $P$, $Q$ is reducible to $\interp{\interpp{Q}}$ without using the rewrite $\xi,$ and $\interp{\interpp{-}}$ is idempotent.
\end{theorem}
See the appendix for more details.

\subsection{Barbed bisimilarity}
An {\bf observation relation} $\downarrow_\Ncal$ over a set of names $\Ncal$ is the smallest relation satisfying
\begin{center}
  \AXC{$y \in \Ncal$} \AXC{$x \equiv_N y$} \BIC{$x!P \downarrow_\Ncal x$} \DP $\quad$ and $\quad$ \AXC{$P  \downarrow_\Ncal x \mbox{ or } Q \downarrow_\Ncal x$} \UIC{$P\;|\;Q  \downarrow_\Ncal x$} \DP
\end{center}
for the RHO calculus or
\begin{center}
  \AXC{$y \in \Ncal$} \AXC{$x \equiv_N y$} \BIC{$((!\; x)\; P) \downarrow_\Ncal x$} \DP $\quad$ and $\quad$ \AXC{$P  \downarrow_\Ncal x \mbox{ or } Q \downarrow_\Ncal x$} \UIC{$((|\; P)\; Q)  \downarrow_\Ncal x$} \DP.
\end{center}
for the RHO combinators.

We denote eventual reduction by $\to^*$ and write $P \downarrow^*_\Ncal x$ if there exists a process $Q$ such that $P \to^* Q$ and $Q \downarrow_\Ncal x.$

An {\bf $\Ncal$-barbed bisimulation} over a set of names $\Ncal$ is a symmetric binary relation $S_\Ncal$ between agents such that $P \mathop{S_\Ncal} Q$ implies
\begin{enumerate}
  \item if $P \to P'$ then $Q \to^* Q'$ and $P' \mathop{S_\Ncal} Q',$ and
  \item if $P \downarrow_\Ncal x,$ then $Q \downarrow^*_\Ncal x.$
\end{enumerate}
$P$ is $\Ncal$-barbed bisimilar to $Q,$ written $P \approx Q,$ if $P \mathop{S_\Ncal} Q$ for some $\Ncal$-barbed bisimulation $S_\Ncal.$

\subsection{Faithfulness}

\begin{theorem}
  $P \approx_{\mbox{\tiny calc}} Q \iff  ((|\; C)\; \interp{P}) \approx_{\mbox{\tiny comb}} ((|\; C)\; \interp{Q})$.
\end{theorem}

\begin{proof}[Proof sketch]
The only occurrence of $C$ on the right is at the topmost context and the rewrite rules preserve the location of $C$, so the only reduction context is the topmost one.  The rest follows from the two interpretation functions and theorem \ref{roundtrip}.  In particular, while the only reduction rule in the RHO calculus is synchronizing on a name, there are extra reduction rules for the RHO combinators; however, these extra reduction rules never send or receive on a name and never prevent sending or receiving on a name.  Therefore, each synchronization in the evaluation of a RHO calculus term corresponds to a synchronization in the corresponding RHO combinator term and some number of reductions of $S,K,I,$ or evaluating a quoted process.
\end{proof}

In fact, we believe a much stronger property than bisimilarity should hold: since $S,K,$ and $I$ are only used for eliminating dummy variables and the $\epsilon$ reduction plays the role of semantic substitution, $\interp{\interpp{-}}$ should pick out a normal form for a RHO combinator.  We should get a set of normal-form equivalence classes of $W$-sorted RHO combinators that is isomorphic to the set of $\alpha$-equivalence classes of RHO calculus terms.  Then we should get
\[ P \xrightarrow{\mbox{\tiny comm}} P' \quad \iff \quad \interp{P} \xrightarrow{\xi} \interp{P'}\]
and
\[ Q \xrightarrow{\xi} Q' \quad \iff \quad \interpp{Q} \xrightarrow{\mbox{\tiny comm}} \interpp{Q'}, \]
where we now regard the left and right sides as being equivalence classes.

\section{Conclusion and future work}
This paper is part of a pair of papers demonstrating that reflection
provides a powerful technique for treating nominal phenomena as
syntactic sugar, thus paving the way for simpler semantic treatments
of richly featured calculi, such as the {\pic} and other calculi of
concurrency. We illustrated the point by providing faithful semantics
of both the $\lambda$-calculus and the {\pic} in terms of graph-enriched
Lawvere theories. This work may be considered preparatory for a more
elaborate study of logics for concurrency in which the nominal
phenomena have logical status, but may be treated in a technically
simpler fashion.

\section{Appendix: abstraction elimination calculations}
\[\begin{array}{rl}
  & ((K\; \interp{P})\; x) \\
  = & \interp{P}\\
  \\

  & (((S\; ((S\; (K\; for))\; ((S\; (K\; \&))\; \; \interp{P}_x)))\; \interp{\interp{Q}_y}_x)\; x)\\
  =&  ((((S\; (K\; for))\; ((S\; (K\; \&))\; \; \interp{P}_x))\; x)\; (\interp{\interp{Q}_y}_x\; x))\\
  =&  ((((K\; for)\; x)\; (((S\; (K\; \&))\; \; \interp{P}_x)\; x))\; \interp{Q}_y)\\
  =&  ((for\; (((K\; \&)\; x)\; (\interp{P}_x\; x)))\; \interp{Q}_y)\\
  =&  ((for\; (\&\; \interp{P}))\; \interp{Q}_y)\\
  \\
  & (((S\; ((S\; (K\; !))\; ((S\; (K\; \&))\; \interp{P}_x)))\; \interp{Q}_x)\; x)\\
  =&  (((((S\; (K\; !))\; ((S\; (K\; \&))\; \interp{P}_x)))\; x)\; (\interp{Q}_x\; x))\\
  =&  ((((K\; !)\; x)\; (((S\; (K\; \&))\; \interp{P}_x)\; x))\; \interp{Q})\\
  =&  ((!\; (((K\; \&)\; x)\; (\interp{P}_x\; x)))\; \interp{Q})\\
  =&  ((!\; (\&\; \interp{P}))\; \interp{Q})\\
  \\
  & (((S\; ((S\; (K\; |))\; \interp{P}_x))\; \interp{Q}_x)\; x)\\
  =& ((((S\; (K\; |))\; \interp{P}_x)\; x)\; (\interp{Q}_x\; x))\\
  =& ((((K\; |)\; x)\; (\interp{P}_x\; x))\; \interp{Q})\\
  =& (|\; \interp{P})\; \interp{Q})\\
  \\
  & (((S\; (K\; *))\; I)\; x)\\
  =&  (((K\; *)\; x)\; (I\; x))\\
  =&  (*\; x)\\
  \\
  & (((S\; (K\; *))\; ((S\; (K\; \&))\; \interp{P}_x))\; x)\\
  =&  (((K\; *)\; x)\; (((S\; (K\; \&))\; \interp{P}_x)\; x))\\
  =&  (*\; (((K\; \&)\; x)\; (\interp{P}_x\; x)))\\
  =&  (*\; (\&\; \interp{P}))\\
\end{array}\]

\bibliographystyle{plainurl}
\bibliography{calco}
\end{document}

Representing operational semantics with enriched Lawvere theories calco2017/calco_orig.tex


\chapter{Self-organization}
\label{sec:selfOrganization}

This chapter describes the algorithms emplyed by \emph{HyCube} for joining/leaving the network, as well as maintenance and recovery processes, maintaining good routing properties under dynamically changing conditions. The algorithms are based on nodes exchanging information about references stored in routing tables and neighborhood sets. When a node receives a notification about existence of some other peer or receives references maintained by some other node (requested for example in the recovery or joining process, or generated by the notifying node itself), it should analyze the node and check if the reference should be stored in the routing tables or the neighborhood set. Every node is analyzed as a candidate to the neighborhood set and appropriate routing table slots.
For the primary routing table, this is the slot at the level calculated based on the common prefix length with the node ($l - 1 - commonPrefixLength$, $l$ - the number of hierarchy levels) at the position equal to the first different digit (group of $d$ bits, $d$ - the number of dimensions).
For the secondary routing table, the slot corresponds to the lowest-level hypercube containing the analyzed node, adjacent to the hypercube of the analyzing node. A simple check may be performed to verify this condition: nodes $X$ and $Y$ are in adjacent hypercubes at levels $l - 1 - j$ to $l - 1 - i$ if and only if $i$ first digits ($d$-bit groups) of $X$ and $Y$ are equal, and $i$-th to $j$-th digits (numbered starting from 0) differ on one (the same for all these digits) bit - this bit corresponds to the dimension in which the two hypercubes are adjacent, and only the slot at the lowest level should be considered (to prevent placing the same nodes in overlapping routing table slots at different levels). Furthermore, depending on settings, additional rules described in Section \ref{sec:rtOverlapping} may apply to prevent storing nodes in the primary routing table slots overlapping with the lower-level secondary routing table slots.
For selecting nodes within individual routing table slots and within the neighborhood set, the algorithms described in Sections \ref{sec:HyCubeNodeSelection} and \ref{sec:uniformNSDistribution} apply.

\section{Maintenance and recovery}

Maintenance is a process of ensuring good system properties that takes place continuously (ensuring the relevance of routing tables, updating information about neighbors). The maintenance mechanism used in \emph{HyCube} is directly connected with the routing table neighbor selection (described in detail in Section \ref{sec:HyCubeNodeSelectionLNS}). Every node periodically sends keepalive messages to all the nodes in its routing tables, and, depending on whether the keepalive response is received or not, the node's liveness value is updated. Based on the liveness value, the routing table reference may be skipped in the next hop selection (to avoid failed paths), may be replaced, or removed from the routing tables.

Recovery is a process of repairing the network topology and propagating necessary information over the network on account of a topology change (new nodes joining/leaving the system), topology breakdown (node failures) or attacks. The recovery technique, employed by \emph{HyCube}, is a periodic procedure, run every specified time interval (parameter value). The value of the interval should be adjusted depending on the DHT nature. The higher is the level of churn, the recovery should be run more often, while if the system has a very stable nature, this interval might be larger. The recovery algorithm in \emph{HyCube} proceeds in two phases:

\begin{enumerate}

\item In the first phase, the node sends a recovery request to all nodes in its routing tables, which it turn return their routing tables to the requesting node. Upon receiving the responses, the requesting node processes the references returned and updates its routing tables and neighborhood set.

\item In the second recovery step, the node performing the recovery sends a notification (NOTIFY message) about its existence to all nodes in its neighborhood set and routing tables (immediately after sending the recovery requests). The notification is also sent to the routing tables nodes to spread the information about the node to more distant parts of the hypercube. However, to limit the overhead, the maximum number of the routing table nodes to which notification is sent should be limited - the message would be sent to certain maximum number of nodes (random selection from all routing table nodes).

\end{enumerate}

Because such an approach may lead to exchanging a large number of messages and also increase processing overhead at the node level, neighborhood set recovery - a variant of the recovery was introduced. Nodes performing the neighborhood set recovery send the recovery requests only to nodes in their neighborhood sets, which dramatically decreases the network traffic and processing overhead. In addition to the recovery interval, \emph{HyCube} allows defining the recovery plan - determining a sequence of recovery variants (for individual recovery runs, successive recovery types from the recovery plan are performed - following a cyclical pattern). Full recovery has better properties in terms of keeping the routing tables up-to-date. However, especially for larger networks, the overhead is significantly larger. Although there might be situations in which the neighborhood set is completely corrupt, and sending recovery requests to the routing table nodes would return much more nodes, usually it is sufficient to repeat the neighborhood set recovery instead. Therefore, the neighborhood set recovery is the default recovery method used in \emph{HyCube}.

To spread the information about the node in an even greater degree, it is possible to send a notification also to every node to which a reference is returned in recovery responses. However, this approach proved to generate enormous network traffic, as well as very high nodes' processing overhead. Performed simulations proved that notifying all neighborhood set nodes and 16 random routing table nodes during every recovery does not significantly increase the overhead, and, if the recovery procedure is run frequently enough, it is sufficient to properly propagate the information about the node's existence. Furthermore, depending on configuration, nodes may process the recovery request messages as notifications (analyze the sender as a routing table candidate), in which case, a notification message does not have to be later sent to the nodes to which the recovery requests were sent.

\section{Joining the system (connecting to the DHT)}

When a new node joins the existing DHT, it should have knowledge about any node already connected to the system. \emph{HyCube} implements two different approaches for joining the system. One of them is based on routing a JOIN message through the system to the node closest to the new node's ID, and the routing tables are formed by the references returned by intermediate nodes along the path (including also themselves), as well as the closest node. The second method is based on searching for the nodes closest to the new node's ID, which form initial routing tables for a new node. The routing approach requires much less overhead - a smaller number of messages are exchanged. However the search method is not vulnerable to any single node possibly causing the join message to be dropped. This section presents both join techniques.

\subsection{``Route-join'' procedure}

The route-join procedure is based on the joining mechanism presented in \cite{pastry}. To initiate the route-join procedure, the joining node should send a ``join'' request (JOIN message) to any node already participating in the system. The message is routed to the closest possible node to the joining node's ID (however, omitting the exact match, as the message would be possibly routed back to the joining node). All intermediate nodes send back (directly to the joining node) JOIN\_REPLY messages that contain all references stored by them in their routing tables. Every node returned is analyzed by the joining node and its routing tables are updated based on the criteria described in Sections \ref{sec:HyCubeNodeSelection} and \ref{sec:uniformNSDistribution}. As the message gets closer to the new node ID, references returned are more likely to be good candidates for the neighborhood set. Furthermore, as the common prefix length with the joining node ID increases, more and more routing table slots correspond to the same hypercubes as the one of the joining node, so more and more references returned by the intermediate nodes might be also used for building the routing tables. For that reason, it is a good idea to disable the prefix mismatch heuristic for JOIN messages (the behavior is configured by a system parameter).

\subsection{``Search-join'' procedure}

In the search-join procedure, the joining node initially retrieves all references from routing tables from a known node connected to the system. The references returned are used to perform a search (Section \ref{sec:search}) for the closest nodes to the joining node ID with certain values of parameters $\alpha$, $\beta$, $\gamma$: $\alpha_{join}$, $\beta_{join}$ and $\gamma_{join}$ (system parameters), and the $ITN$ (ignore target node) parameter set to \emph{true}. The value of $k$ is not important, because all nodes returned by intermediate nodes are processed (updating the routing tables). Initially, the set $\Gamma$ is filled with $\gamma$ closest (Euclidean) nodes retrieved in the initial phase - from the bootstrap node. Afterwards, the search procedure proceeds as described in Section \ref{sec:search}.

Because the node performing the search is the node whose identifier is being looked for, to allow the use of the Steinhaus transform, the initial values of Steinhaus points should not be set to the joining node ID (Euclidean metric would be then used for local next hop selections), but should be given the values of IDs of the initial nodes themselves (only for the initial nodes returned by the bootstrap nodes - for any nodes added to $\Gamma$ later, the Steinhaus point is updated based on the information contained in the response message that contained the node reference).

\section{Leaving the system (disconnecting from the DHT)}

The maintenance and recovery mechanisms should be able to maintain connection graph consistency and keep routing tables of nodes up-to-date, ensuring good routing and searching properties. However, these algorithms have certain ``inertia'' and react with a certain delay. A simple solution to overcome this problem is sending the leave information (LEAVE message) to all neighbors in the leaving node's neighborhood set. Such LEAVE messages should contain the list of neighborhood set nodes of the leaving node, and the nodes receiving it should remove references to the LEAVE sender from the routing tables and the neighborhood set, and process all the nodes included in the message to immediately fill the lost routing table or/and neighborhood references.

% ex: set tabstop=4 shiftwidth=4 softtabstop=4 noexpandtab fileformat=unix filetype=tex encoding=utf-8 fileencodings= fenc= spelllang=pl,en spell: