\subsection{Selecting channels}

Many CA1 LFP recordings are not made with multichannel probes, but rather with one or more tetrodes. It is therefore relevant to ask how GEVec-based detectors perform on these types of recordings. We can approximate this setting with our current multichannel probe recording, by only using one or a few channels.

Each such selection of input channels, in combination with a certain number of delays, yields a different GEVec-based SWR detector. We evaluate and compare these detectors using the test data set, analyzing both accuracy (\cref{fig:PR-searchgrid}) and latency (\cref{fig:Latency-searchgrid}).

\begin{figure}
\img[1.16]{PR-searchgrid}
\captionn[]{Accuracy of GEVec-based SWR detectors, }{for different combinations of active input channels and number of delays. Active input channels are marked in solid black on a schematic of the probe tip. (Height of schematic: 687 \si{\micro\metre}). The percentage and background color of each panel indicate the maximum $F_1$ value obtained for that GEVec-based SWR detector. Each panel includes the same baseline from the state-of-the-art SWR detector, in gray.}
\label{fig:PR-searchgrid}
\end{figure}

\begin{figure}
\img[1.16]{Latency-searchgrid}
\captionn{Latency of GEVec-based SWR detectors}{See \cref{fig:PR-searchgrid} for legend.}
\label{fig:Latency-searchgrid}
\end{figure}

We note some general trends. Using no extra delays results in low accuracy detectors, no matter which channels are included (top row of \cref{fig:PR-searchgrid}). For most input channel combinations, the tradeoff in number of delays observed in the previous section is preserved: using more delays increases accuracy (up to a certain point), while also increasing detection latency.

The weight visualizations of \cref{fig:LSM-comp} show a relatively high weight for the stratum radiatum / sharp wave channels. Using only one such channel as input results in low accuracy detectors however, no matter how many delays are used (second column of \cref{fig:PR-searchgrid,fig:Latency-searchgrid}). Using four such channels (second to last column) results in detectors with an accuracy approaching, but not reaching, that of the state-of-the art ripple-based detector when a sufficient number of delays is used. Interestingly, this decent accuracy is reached despite none of these four channels (numbers 2 to 5) displaying any noticeable ripple activity (\panelref{fig:LSM-comp}{A}).

Conversely, using one channel from the pyramidal cell layer (where ripples are observed) yields GEVec-based detectors on par with those that use all channels as input, both regarding accuracy and latency (third and first column of \cref{fig:PR-searchgrid,fig:Latency-searchgrid}).

This one channel GEVec-based detector thus also outperforms the ``state-of-the-art'' detector based on a band-pass filter. The state-of-the-art detector also uses only one channel as input, and is also a linear filter. This suggests that either 1) finding linear filter weights through `machine learning' (the GEVec technique) yields better results than manual filter design; or 2) that the band-pass filter used as ``state-of-the-art'' detector was not designed optimally in the first place.

Adding a stratum radiatum / sharp wave channel to the single pyramidal cell layer channel (fourth column of \cref{fig:PR-searchgrid,fig:Latency-searchgrid}), or using a cluster of channels in the pyramidal cell layer (last column) does not improve performance over the one channel case.


\chapter*{Abbreviations}
\label{abbrs}
\addToTOC{abbrs}

\begin{deflist}
\abbr{BPTT}{Backpropagation through time}{See \cref{sec:RNN-optim}.}
\abbr{BPF}{Band-pass filter}{See \cref{ch:BPF}.}
\abbr{BCI}{Brain-computer interface}{See \cref{sec:BCI}.}
\abbr{CA1}{``Cornu Ammonis'', subregion 1}{Area in the hippocampus where voltages are recorded from (see \cref{fig:brain-anatomy}).}
\abbr{CA3}{``Cornu Ammonis'', subregion 3}{Area in the hippocampus (see \cref{fig:brain-anatomy}). CA3 sends many axons (called ``Schafer collaterals'') to CA1.}
\abbr{ch.}{Channel}{}
\abbr{CSD}{Current source density}{The rate at which the positive charge surplus of a small volume decreases, $-\pdv*{\rho}{t}$. By charge conservation, equal to the net rate $\div{\J}$ at which positive charges flow out of the small volume. See \cref{sec:CSD}.}
\abbr{FIR}{Finite impulse response}{A linear filter whose output is a convolution of the input signal with some kernel.}
\abbr{GEVal}{Generalized eigenvalue}{See \cref{sec:generalized-eigenproblem}.}
\abbr{GEVec}{Generalized eigenvector}{See \cref{sec:generalized-eigenproblem}.}
\abbr{GRU}{Gated recurrent unit}{See \cref{sec:GRU_eqs}.}
\abbr{IIR}{Infinite impulse response}{A filter whose output at each timestep is a linear combination of both input samples and previous output samples.}
\abbr{IQR}{Interquartile range}{A measure of the spread of a set of one-dimensional values, that is robust to outliers. Difference between the 75th and the 25th data percentile.}
\abbr{KDE}{Kernel density estimate}{}
\abbr{LFP}{Local field potential}{The extracellular electric potential (see \cref{ch:LFP}).}
\abbr{LTP}{Long-term potentiation}{See \cref{sec:synaptic-plasticity}.}
\abbr{RMS}{Root-mean-square}{$\sqrt{\ev{x_t^2}}$ for a signal $x_t$.}
\abbr{RNN}{Recurrent neural network}{See \cref{ch:RNN}.}
\abbr{SNR}{Signal-to-noise ratio}{See \cref{sec:LSM}.}
\abbr{SOTA}{State of the art}{The algorithm currently used for SWR detection, namely an online single channel band-pass filter.}
\abbr{STDP}{Spike-timing-dependent plasticity}{See \cref{sec:synaptic-plasticity}.}
\abbr{SWR}{Sharp wave-ripple}{The pattern in the LFP that we want to detect in real-time. See \cref{ch:SWR}.}
\abbr{VHC}{Ventral hippocampal commisure}{See \cref{sec:disruption}.}
% todo: sort alphabetically :)
\end{deflist}


 \section{Mappings} \label{sec:mappings}
Fran\c{c}ois Faure

\newcommand{\mass}{\ensuremath{M}}             % matrice de masse
\newcommand{\vol}{\ensuremath{\mathcal V}} % volume d'intersection
\newcommand{\press}{\ensuremath{\rho}}

% In section~\ref{sec:pixelcontact}, we have shown how to efficiently compute repulsion forces between arbitrary polyhedra.
% In this section, we present a general framework to map the contact forces to the degrees of freedom of arbitrary physical models.
% We then demonstrate it on a variety of models.

\subsection{Motivation} \label{sec:geometryLayers}
Different geometrical models can be used to model objects in contact.
We organize them in a hierarchy of layers. An example is shown in figure~\ref{fig:hierarchy}, where a rigid object hits a shape embedded in deformable cells.

\begin{figure}
 \centering
 \includegraphics[width=\linewidth]{mappings.png}
 \caption{Mappings from the DOFs to a contact point. Top: two simulated objects in contact (red point). Bottom: hierarchy of geometrical layers. Positions and velocities are propagated top-down. The contact force $f_c$ is accumulated in the contact layers. Forces are then propagated bottom-up.
%Each object has its own state vectors and mappings.
}
 \label{fig:hierarchy}
\end{figure}

% bases de la dynamique
The state of a simulated system can be described by the values and time derivatives of its independent degrees of freedom (DOF) gathered in two vectors $x_0$ and $v_0$.
The dynamics equation (Newton's law) relates the second time derivative $a_0$ of the DOF to the forces $f_0$ acting on them: $f_0 = \mass a_0$, where \mass~ is a matrix modeling the mass of the system.
%We call $f_0$ the \textit{net force}.

% attacher de la g�om�trie
A geometrical model can be attached to the DOF for visualization or contact computation.
Its DOF positions, velocities and associated forces are stored in vectors $x_1$, $v_1$ and $f_1$, respectively.
They are not independent variables, since the positions and velocities are bound to the independent DOF.
We say that the child geometrical model $1$ is mapped from the parent model $0$,
 using a kinematic operator which we call \textit{mapping}:
\begin{eqnarray*} %\label{eq:mapV}
x_1 &=&\JNL_1(x_0)\\
v_1 &=& J_1 v_0
\end{eqnarray*}
Typical mappings include polygonal shapes attached to rigid bodies using local coordinates, or embedded in deformable cells using barycentric coordinates, as well as skin surrounding articulated bodies using vertex blending techniques.
Matrix $J_1 = \frac{\partial x_1}{\partial x_0}$ encodes the linear relation between the DOF velocities and the shape velocities. Due to linearity, the same relation holds on elementary displacements $dx$.
It also holds on accelerations, with an additional offset due to velocities when the position mapping \JNL is nonlinear.
In most cases, operators \JNL~ and \J~ are the same, but in the case of rigid bodies, \JNL~ is nonlinear with respect to $x_0$ and it can not be written as a matrix.
For surfaces embedded in deformable cells, matrix \J~contains the barycentric coordinates.
For surfaces attached to rigid bodies, each row of the matrix encodes the usual relation $v = \dot o + \omega \times (x-o)$ for each vertex.
Similarly, skins around articulated bodies involve, at each vertex, the weighted  contributions of the rigid bodies.

% g�om�trie suppl�mentaire d�e aux contacts
When shapes collide, additional geometry can be necessary to model the contact.
For instance, when an edge intersects another one, a contact force is applied to the intersection points.
These points are defined by their barycentric coordinates with respect to their edge vertices.
Other relations can be used, depending on the kind of geometrical primitives in contact.
This additional geometry requires another geometrical layer connected to the shape by a mapping, as illustrated in figure~\ref{fig:hierarchy}.
% This layer is also connected to the shape using mappings:
% \begin{eqnarray*} %\label{eq:mapV}
% x_2 &=&\JNL_2(x_1)\\
% v_2 &=&J_2 v_1
% \end{eqnarray*}
We extend this approach to tree-like hierarchies of geometries, with the independent DOFs at the root.
For instance, the independent DOF may have two children, one for collision using a coarse mesh, and the other for rendering using a finer mesh. The synchronization between these siblings is automatically guaranteed by their attachment to their common ancestor, the DOF layer.

Positions and velocities are propagated top-down in the hierarchy. Conversely,
in order to take all the forces into account in the dynamics equation, the forces are propagated bottom-up, up to the independent DOFs, where Newton's law $f=ma$ is applied.
Given forces $f_c$ applied to a child model, the mapping computes and accumulates the equivalent forces $f_p$ applied to its parent.
Since equivalent forces must have the same power, the following relation holds:
$$
v_{p}^T f_p = v_c^T f_c
$$
The kinematic relation $v_{c} = J v_{p}$ allows us to rewrite the previous equation as
$$
v_{p}^T f_{p} = v_{p}^T J^T f_c
$$
Since this relation holds for any velocity $v_p$, the principle of virtual work allows us to simplify the previous equation to obtain:
\begin{equation} \label{eq:mapF}
f_{p} = J^T f_c
\end{equation}

\subsection{Mapping functions}
As seen in section~\ref{sec:geometryLayers}, the mappings propagate positions, velocities, displacements and accelerations top-down, and they propagate forces bottom-up.
The top-down propagation methods are:
\begin{itemize}
 \item \texttt{apply (const MechanicalParams*, MultiVecCoordId outPos, ConstMultiVecCoordId inPos )} for positions,
 \item \texttt{applyJ(const MechanicalParams*, MultiVecDerivId outVel, ConstMultiVecDerivId inVel )} for velocities and small displacements,
\item \texttt{computeAccFromMapping(const MechanicalParams*, MultiVecDerivId outAcc, ConstMultiVecDerivId inVel, ConstMultiVecDerivId inAcc )} for accelerations, taking into account velocity-dependent accelerations in nonlinear mappings.
\end{itemize}
The bottom-up propagation methods are:
\begin{itemize}
 \item \texttt{applyJT(const MechanicalParams*, MultiVecDerivId inForce, ConstMultiVecDerivId outForce )} for child forces or changes of child forces,
\item \texttt{applyDJT(const MechanicalParams*, MultiVecDerivId parentForce, ConstMultiVecDerivId  childForce )} for changes of parent force due to a change of mapping with constant child force,
\item \texttt{applyJT(const ConstraintParams*, MultiMatrixDerivId inConst, ConstMultiMatrixDerivId outConst )} for constraint Jacobians,
\end{itemize}

The name of the methods used to propagate velocities or small displacements top-down contain $J$, which denotes the kinematic matrix, while the names of the methods used to propagate forces or constraint Jacobians bottom-up contain $JT$, which denotes the transpose of the same.
Method \texttt{applyJT(const MechanicalParams*, MultiVecDerivId inForce, ConstMultiVecDerivId outForce )} is used to accumulate forces from a child model to its parent. It performs a cumulative write (+=) since a model may have several children:
\begin{equation}
 \label{eq.applyjt}
 f_{p} += J^T f_c
\end{equation}

Some differential equation solvers need compute the change of force df, given a change of position dx.
The displacement dx, considered small, is propagated top-down using the linear operator \texttt{applyJ(const MechanicalParams*, MultiVecDerivId outVel, ConstMultiVecDerivId inVel )}, then the force changes are accumulated bottom-up.
Differentiating eq.\ref{eq.applyjt}, we get:
\begin{equation}
 \label{eq.applydjt}
 \delta f_{p} += J^T \delta f_c + \delta J^T f_c
\end{equation}
Once the change of child force $\delta f_c$ is computed (see the section on force fields), method \texttt{applyJT(const MechanicalParams*, MultiVecDerivId inForce, ConstMultiVecDerivId outForce )} is used to accumulate it in the parent, corresponding to the first term in the right of eq.\ref{eq.applydjt}.
Method \texttt{applyDJT(const MechanicalParams*, MultiVecDerivId parentForce, ConstMultiVecDerivId  childForce )} is used to accumulate the second term, which is due to the change of matrix J due to a displacement.
It is null in linear mappings, such as BarycentricMapping.
This method queries the last displacement propagated and the child force using the MechanicalParams.

Constraints enforced using Lagrange multipliers are represented using linear equations.
If a linear constraint on the child DOFS in expressed as $L_c v_c = a$, where $L_c$ is the Jacobian of the constraint, then the equivalent constraint at the parent level is: $L_p v_p = a$, where $L_p = J^T L_c$.
Method applyJT(const ConstraintParams*, MultiMatrixDerivId inConst, ConstMultiMatrixDerivId outConst ) is used to compute $L_p$.
Since the Jacobians are generally sparse, they are encoded in sparse matrices instead of the dense vectors used for forces.

%This simple relation allows us to map forces bottom-up through the hierarchy.
%The corresponding acceleration can then be computed in the DOF layer.

% We emphasize the simplicity and the generality of equation~\ref{eq:mapF}.
% It makes no assumption about the constitutive laws of the simulated objects.
% It allows us to connect arbitrary bodies using any geometry, provided that a kinematic operator linking the velocity of the geometry to the body DOFs is available.
% %It is not limited to polygonal geometries.

% It encompasses and generalizes \cite{Sifakis07}'s hard bindings which were introduced for Finite Elements and empirically extended to rigid bodies.
% It enables us to efficiently and straightforwardly connect or make collide a wide variety of bodies.

% Let $x_n$, $y_n$, $f_n$ be, respectively, the positions, velocities and net forces in the geometrical model $n$, and $x_{n+1}$, $y_{n+1}$, $f_{n+1}$ the positions, velocities and net forces in model $n+1$ mapped from model $n$.
% The mappings have the following propagation methods:
% \begin{itemize}
%  \item apply: propa
% \end{itemize}


\chapter{Introduction}
% Tony: "1, 2, 3 pages"
% What is the problem?
% Why is it interesting?
% How did you solve it?
% How far did you get?

% Includes motivation, objectives, and contributions (what you achieved)
\section{Motivation}

%Understanding the human brain
The human brain is considered to be one of the most complex objects which have puzzled scientists and philosophers alike throughout the centuries. To gain an understanding of how this mysterious black box works, through experimentation and modelling, would be of great aid to the furthering of science and humanity alike. A part of this includes developing cures for a multitude of mental and brain-related disorders.

%Artificial neural networks
Several medical discoveries from the 20th century have enlightened our knowledge of the human brain. One consequence of this was the development of different mathematical models of artificial neural networks, inspired by the fields of biology and medicine.

% modelling the Attachment Theory. Watch out between the distinction between Self-Attachment and Attachment Theory
Our motivation is to use some of these neural networks to explore the mathematics of a developing theory called Self-Attachment. Part of Attachment theory, this strategy aims to help cure various mental problems that people currently facing. Recent research has shown that a mathematical methodology of analysing these disorders is starting to become feasible. \cite{net_model_neuroses}

The subsequent chapters will introduce the Attachment Theory and the mathematical model. We have mainly focused on the technical side, and have described psychological analogies that place our technical results in context of the theory.

\section{Objectives}

%Main Objectives
Our main objective is to confirm the results of previous work done by Federico Mancinelli, who has been analysing the attachment theory using neural networks. He has performed several experiments regarding clusters of attractors, basin sizes, and Gaussian-distributed patterns. Furthermore, we have been aiming at extending his results by exploring the following concepts:
\begin{itemize}
\item Using the network for performing image recognition
\item Restricted Boltzmann machines
\item Super-attractors
\end{itemize}

%Improvement of Federico's results
In addition to that, we were aiming to improve some of the methods that were used in his experiments. This includes the technique for sampling Gaussian-distributed patterns or for calculating basin sizes. Our final aims consisted of explaining some of the subsequent inconsistencies that have been found in his results.

\section{Our achievements}

Since our project was related to exploring attractor-based neural networks, we have obtained interesting results about their attractors. These attractors are analogous to learned memories or experiences that an individual has learned.

%This list is by no means exhaustive. Feel free to add any other aspects. This is what I(Raz) could recall a few days ago.
Our main contributions are outlined below:
\begin{itemize}
\item Confirming Mancinelli's results by reimplementing them in a completely different environment (Haskell)
\item Proving that the Hopfield network is capable of performing image recognition, by learning image patterns and then recalling the closest image, when queried for an input. Furthermore, we have proven it's associative memory properties, by successfully recalling some of the learned images.
\item We found out that training patterns are not guaranteed to become fixed points in the Hopfield network. This is an aspect that was not mentioned in similar research papers, and we first thought the the patterns are always fixed points.
\item Extending the research to encompass the Boltzmann Machine, which provides a nice way of overcoming some limitations of the Hopfield Network. Amongst other features, it can prevent convergence to spurious patterns by using a stochastic update rule.
\item A thorough analysis of Super-Attractors, which represent patterns that have been used multiple times in the training process. They generally have greater basin sizes, and therefore patterns will have a greater chance of converging to them compared to normal attractors.
\end{itemize}


\subsection{SNOW with C2C Communication}
\label{app:algorithm-a}
In this section,  we show that SNOW is possible in the   \emph{multiple-writers single-reader}
(MWSR) setting %, and prove that any fair and well-formed execution  of $A$ satisfies  the SNOW properties.
%In practice, a system with a single reader may not be very useful but this algorithm serves a counter example
%algorithm
when client-to-client communication is allowed. In particular, we present an algorithm $A$, which has all SNOW properties in such setting.
%Algorithm $A$
%shows that if client-to-client communication is allowed, it is possible to have algorithms  that satisfies all of the SNOW properties with two clients.
We consider a system that has $\ell \geq 1$ writers with ids $w_1,
w_2 \cdots w_{\ell} \in \mathcal{W}$
%(we denote this set by $\mathcal{W}$)
, one reader $r$, and  $k \geq 1$ servers with ids $s_1, s_2\cdots s_k \in \mathcal{S}$.
%(denote as $\mathcal{S}$) that maintains the  objects $o_1, \cdots, o_k$, respectively.
Client-to-client communication is allowed.
%Note that for a two-client system, when both  clients are of the same type, i.e., two writers or two readers, the SNOW properties are trivially satisfied.
%
% For any two tags $t_1, t_2 \in \mathcal{T}$ we say  $t_2 > t_1$ if $(i)$ $t_2.z > t_1.z$ or $(ii)$ $t_2.z = t_1.z$ and $t_2.w > t_1.w$. Therefore, the set $\mathcal{T}$ is totally ordered set. We also assume that every client and servers has a unique id and the ids can be compared w.r.t. some lexicographic order.
%
The pseudocode for algorithm $A$ is presented in Pseudocode~\ref{fig:algo_a}.
%We assume that each of the processes is run in a single-threaded manner.
%, and therefore, each of the servers or the clients executes the algorithmic steps sequentially.
%
We use keys to uniquely identify a \wot{}.  A key $\kappa \in \mathcal{K}$ is defined as a pair $(z, w)$,
where $z \in \mathbb{N}$, and $w \in \mathcal{W}$ is the id of a writer. $\mathcal{K}$ denotes the set of all possible keys.
Also, with each transaction we associate a tag $t \in \mathbb{N}$. % which will help us define an order among the transactions.

%For version control of the  object values  we use tags.  A tag $t$ is defined as a pair $(z, w)$, where $z \in \mathbb{N}$ and $w \in \mathcal{W}$  is the id of a writer. We use $\mathcal{T}$ to denote the set of all possible tags.

\textit{\textbf{State variables:}} %The state variables in  writer, reader and  server processes are as follows.
$(i)$ Each  \emph{writer $w$} stores a counter $z$ corresponding to the
number of \wots{}  it  has  invoked so far, initially $0$.
$(ii)$ The  \emph{reader} $r$ has an
ordered list of elements, $List$, as $(\kappa, (b_1, \cdots, b_k))$,  where
$\kappa  \in \mathcal{K}$  and
$(b_1, \cdots b_k) \in  \{0, 1\}^k$. Initially,
$List= [ ({\kappa}^0, (1, \cdots 1) ]$, where ${\kappa}^0  \equiv (0, w_0)$,
and $w_0$ is any
place holder identifier for writer id.
%$List$ can be thought of as an array, with $0$ as the starting index.
$(iii)$ Each   \emph{server} $s_i \in \mathcal{S}$  stores a set variable $Vals$
with elements
of key-value pairs $({\kappa}, v_i) \in \mathcal{K} \times \mathcal{V}_i$. Initially,
$Vals= \{ ({\kappa}^0, v_i^0)\}$.
%This ensures that each \wot{} generates a unique key.
%labeled with phase names, viz., {\readGetTag}, {\readValueTag}, {\readCompleteTag} and {\writeGetTag}. The server to server messages are labeled as {\readDisperseTag}. Also, in some phases of {\SODA},  the message-disperse primitives {\mdmetaprim} and {\mdvalueprim} are used  as services.

\textit{\textbf{Writer steps:}} Any writer client, $w \in \mathcal{W}$, may invoke a \wot{} $\Writetr{ (o_{i_1}, v_{i_1}), (o_{i_2}, v_{i_2}), \cdots, (o_{i_p}, v_{i_p}) }$, comprising a set of write operations,
%for a \wot{},
%can be  invoked at any writer $w$,
where  $I = \{i_1, i_2, \cdots, i_p\}$ is some subset of $p$ indices of $[k]$. We define the set  $S_I\triangleq \{s_{i_1}, s_{i_2}, \cdots, s_{i_p}\}$.
This procedure consists of two consecutive phases: {\writeValue} and {\informReader}.  In the {\writeValue} phase,  $w$ creates a key ${\kappa}$ as  $ {\kappa}  \equiv (z + 1, w)$; and also increments the local counter $z$ by one.   Then it sends $(${\writeValueTag}$, ({\kappa}, v_{i}))$ to each server $s_i$ in $S_I$, and awaits {\ackTag}s
from each server  in $S_I$.  After receiving all {\ackTag}s,    $w$ initiates the {\informReader} phase during which  it sends
(\informReaderTag, $({\kappa}, (b_{1}, \cdots b_{k})$) to $r$, where for any $i \in [k]$, $b_i$ is a boolean variable, such that $b_i=1$ if $s_i \in S_I$, otherwise $b_i=0$.
Essentially, such a $(k+1)$-tuple
identifies the set of objects that are updated during that \wot{}, i.e., if $b_i=1$ then object
$o_i$ was updated
during the execution of the  \wot{}, otherwise $b_i=0$.
After $w$ receives    {\ackTag}
%tag $t_w$
from $r$ it completes the \wot{}.

\textit{\textbf{Reader steps:}}
We use the same notations for $I$ and $S_I$ as above for the set of indices and corresponding servers, possibly
different across transactions.
The procedure  \Readtr{$ o_{i_1},  o_{i_2}, \cdots, o_{i_p}$},
for any  \rot{},
is  initiated at  reader  $r$, where   $o_{i_1},  o_{i_2}, \cdots, o_{i_p}$  denotes the  subset  of
objects $r$
intends to read. This procedure
consists of only one phase,  {\readValue},  of communication
between the reader and the servers in $S_I$.   Here $r$ sends  the message
(\readValueTag, ${\kappa}_i$) to each server $s_i \in S_I$, where
the ${\kappa}_i$ is the key in  the tuple $({\kappa}_{i}, (b_{1}, \cdots, b_{k}))$  in  $List$ located at  index $j^*$ such that $b_i =1$ such that
$i \in I$. % where $I \triangleq  \{{i_1},  {i_2}, \cdots, {i_p}\}$.
After
receiving the values $v_{i_1}$, $v_{i_2}, \cdots v_{i_p}$ from all  servers in $\mathcal{S_I}$,  where $S_I \triangleq \{s_{i_1},  s_{i_2}, \cdots, s_{i_p}\}$, the transaction completes by
returning $(v_{i_1}, \cdots v_{i_p})$.

On receiving a message
(\informReaderTag, $({\kappa}, (b_{1}, \cdots, b_{k})$) from any writer $w$,  $r$ appends
$({\kappa}, (b_{1}, \cdots, b_{k})$ to its  $List$,  and responds to $w$ with
{\ackTag} and $t_w = |List|$, i.e., number of elements in $List$.
The order of the  elements in  $List$ corresponds to  the order
the \wots{}, the order of the incoming  {\informReaderTag} updates,  as seen by the reader.

\textit{\textbf{Server steps:}} The server responds to messages containing the tags
{\writeValueTag} and \readValueTag.  The first procedure is used if a server $s_i$ receives a
message  $(${\writeValueTag}$, ({\kappa}, v_{i}))$  from a writer $w$,  it  adds $({\kappa}, v_i)$ to its set variable   $Vals$ and sends {\ackTag} back to $w$.
The second procedure is used  if  $s_i$ receives a message, i.e., $(${\readValueTag}$, {\kappa}_{i})$, from $r$, then it responds with $v_i$ such that $({\kappa}_{i}, v_i)$ is in its $Vals$.

\begin{algorithm}[!h]
	\begin{algorithmic}[2]
		%\vspace{-2em}
		\begin{multicols}{2}{\footnotesize
				%\Statex
				\Statex {\bf At writer $w$}
				\Part{{\it State Variables at $w$}}{
					\Statex $z \in \mathbb N$, initially   $0$
				}\EndPart
				\Statex\Statex
				{\bf  \Writetr{$(o_{i_1}, v_{i_1}), \cdots, (o_{o_p},  v_{i_p})$}}
				\Part{ \underline{\writeValue}} {
					\State ${\kappa} \leftarrow (z +1,  w)$
					\State $z \leftarrow z +1 $
					\State $I\triangleq \{i_1, i_2, \cdots, i_p \}$
					%\State $S_{I}\triangleq \{s_{i_1}, s_{i_2}, \cdots, s_{i_p} \}$
					\For{$i \in I$}
					\State Send (\writeValueTag, $({\kappa}, v_{s_i})$) to  $s_i$
					\EndFor
					\State  Await {\ackTag}  from  $s_i$ $\forall$ $i \in I$.
				}\EndPart
				\Statex
				\Part{ \underline{\informReader}} {
					\For{$i \in [k]$}
					\If{$i \in I$}
					\State $b_i \leftarrow 1$
					\Else
					\State $b_i \leftarrow 0$
					\EndIf
					\EndFor
					%      \State $b_{i}\triangleq$   $v_i$ if  $i \in I$, otherwise $\bot$
					\State  Send  (\informReaderTag,
					 \\~~~~~~$({\kappa}, (b_{1}, \cdots, b_{k}))$) to   $r$
					\State  Receive ({\ackTag}, $t_w$) from  $r$
				}\EndPart
		}\end{multicols}
		\vspace{-1.5em}
		\\\hrulefill
		\vspace{-1.5em}
		\begin{multicols}{2}{\footnotesize
				\Statex {\bf At reader $r$}
				\Part{{\it State Variables at $r$}}{
					\Statex $List$, a list  of elements in  $\mathcal{K} \times \{ 0, 1 \}^k $,\\ ~~~~~initially  $[({\kappa}^0, 1, \cdots 1)]$
				}\EndPart

				\Statex
				\Statex  {\bf \Readtr{$ o_{i_1},  o_{i_2}, \cdots, o_{i_p}$}}
				\Part{{\underline{{\readValue}}}}{
					% \State $\forall i, 1 \leq i \leq k$: $t_i^{max}  \leftarrow \max\{ t: (i, t) \in  Tags\}$
					\State $I\triangleq \{i_1, i_2, \cdots, i_p \}$
					%\State $S_{I}\triangleq \{s_{i_1}, s_{i_2}, \cdots, s_{i_p} \}$
					\For{$i \in I$}
					\State $j^* \leftarrow \max_{1 \leq j \leq |List|} \{j:$
					\\~~~~~~~~~~~$List[j].b_i = 1\}$
					\State ${\kappa}_i \leftarrow List[j^*].{\kappa}$
					\State  Send (\readValueTag, ${\kappa}_i$) to $s_i$
					\EndFor
					\Statex
					\State  Await responses  $v_{i}$ from  $s_i$ $\forall$ $i\in I$
					%\Statex /* $t_r \triangleq \max_{1 \leq j \leq |List|} \{ j : List[j].b_i = 1 \wedge i \in I\}$ */
					\State Return  $(v_{i_1}, v_{i_2}, \cdots, v_{i_p})$
				}\EndPart
				%	\Statex
				\\\hrulefill
				\Statex
				\Statex {\bf Response routines}
				\Part{{\underline{On recv  (\informReaderTag,}
						\\\underline{$({\kappa}, (b_{1}, \cdots b_{k}))$) from  $w$}}}{
					\Statex %// $\bigoplus$ denotes append to list
					\State $List  \leftarrow List \bigoplus~ ({\kappa}, (b_{1}, \cdots b_{k}))$
					\\~~~/* $\bigoplus$ for append */
					\State $tag \leftarrow |List|$ /* $| \cdot |$ list size */
					\State Send  ({\ackTag}, $tag$) to  $w$
				}\EndPart
				%\Statex
		}\end{multicols}

		\vspace{-1.5em}
		\\\hrulefill %
		\vspace{-1.5em}
		\begin{multicols}{2}{\footnotesize
				\Statex {\bf At server $s_i$ for any $i \in [k]$}
				\Part{{\it State Variables}}{

				\Statex $Vals\subset \mathcal{K} \times \mathcal{V}_i$, initially   $\{(t^0_{key}, v_i^0)\}$
					%							pair with a tag and a coded element, initially $(t_0, c_0)$.
					%\Statex $t_c$,  the committed tag, initially $t_0$
				}\EndPart
				\Statex

				\Part {\underline{On recv (\writeValueTag, $({\kappa}, v)$)
						from $w$}} {
					\State $Vals \gets   Vals \cup \{({\kappa}, v)\}$
					\State Send {\ackTag} to $w$.
				}\EndPart
				\Statex
				\Part{ \underline{On recv (\readValueTag, ${\kappa}$) from  $r$ }} {
					\State   Send $v$ s.t. $({\kappa}, v) \in Vals$  to $r$
				}\EndPart
		}\end{multicols}
	\end{algorithmic}
	\caption{Steps at writer $w$, reader $r$ and server $s_i$ in $A$.}\label{fig:algo_a}
\end{algorithm}
%	Below we show that algorithm $C$ satisfies the SNOW properties. For the property ``S'' we will lean on an equivalence condition result from atomicity (strong consistency). We reproduce the lemma below. \blue{Note that this lemma for atomicity on read/write atomic register but we will have to adapt it to atomic transactions carefully. For now,  we will assume it and later we will prove it for atomic transactions. }
$A$ respects the SNOW properties as stated below.
% Due to space constraints, the proof of the theorem can be found in Appendix~\ref{app:algorithm-a}. %, where \wots{} are live. % and the proof is omitted, for now, since it is very straightforward.

%Consider any failure-free execution of algorithm $A$. In the steps for the reader assume the quantity
%$t_r \triangleq \max_{1 \leq j \leq |List|} \{ j : List[j].b_i = 1 \wedge i \in I\}$, which is presented as a comment in the pseudo-code for $A$.
%We associate with any transaction $\phi$ a tag
%$tag(\phi)$ such that if  $\phi$ is a \wot{}  $tag(\phi)=t_w$, i.e., the value of $t_w$ before the completion of the operation, and $tag(\phi)=t_r$ when $\phi$ is a \rot{}.

\begin{theorem} Any well-formed  and fair execution of $A$
	%transaction processing in the MWSR setting, %
%	with for objects of type $\mathcal{O}_T$,
	%consisting of  objects $o_1, o_2, \cdots o_k$ maintained by the  servers $s_1, s_y, \cdots, s_k$, respectively; and it
		 guarantees all of the SNOW properties.
	\end{theorem}
\remove{

	\begin{proof} Below we show that $A$ satisfies the  SNOW properties.

	\noindent{\emph{\underline{S property:}}}
	Let $\beta$ be any fair execution  of  $A$ and
 suppose all clients in $\beta$ behave in a well-formed
manner. Suppose $\beta$ contains no incomplete transactions and let  $\Pi$ be the set of transactions in $\beta$.  We define an irreflexive partial ordering ($\prec$) among the transactions in $\Pi$ as follows:  if $\phi$ and $\pi$ are any two distinct transactions in $\Pi$ then we say
	$\phi \prec \pi$ if either $(i)$ $tag(\phi) < tag(\pi)$ or $(ii)$ $tag(\phi) = tag(\pi)$ and $\phi$ is a {\sc write} and $\pi$ is a {\sc read}. We will prove the $S$  (strict-serializability) property of $A$ by proving that the properties $P1$, $P2$, $P3$ and $P4$ of Lemma~\ref{lem:equivalence} hold for $\beta$.

	\emph{P1:}   If $\pi$ is a {\sc read} then since all {\sc read}s are invoked by a single reader $r$ and in a well-formed manner,
	therefore, there cannot be an infinite number of {\sc read}s such that they all
	precede $\pi$ (w.r.t $\prec$).
	 Now, suppose $\pi$ is a {\sc write}. Clearly, from an inspection of the algorithm,
	 $tag(\pi) \in \mathbb{N}$. From inspection of the algorithm, each {\sc write} increases the size of
	 $List$, and the value of the tags are  defined by the size of $List$. Therefore, there can be at
	 most a finite number of {\sc write}s such that can precede $\pi$ (w.r.t. $\prec$) in $\beta$.

	\emph{P2:}  Suppose $\phi$ and $ \pi$ are any two transactions in $\Pi$, such that, $\pi$ begins after $\phi$ completes.
	Then we show that we cannot have $\pi \prec \phi$. Now, we consider four cases, depending on whether $\phi$ and $\pi$ are {\sc read}s or {\sc write}s.
	\begin{enumerate}
	    \item [$(a)$] $\phi$ and $\pi$ are {\sc write}s invoked by writers $w_{\phi}$ and $w_{\pi}$, respectively. Since the size of $List$, in $r$,  grows monotonically with each {\sc write}  hence  $w_{\pi}$ receives the  tag at least as high as $tag(\phi)$, so $\pi\not \prec \phi$.
	      %
	       \item [$(b)$] $\phi$ is a {\sc write}, $\pi$ is a {\sc read} transactions invoked by writer $w_{\phi}$ and $r$, respectively.
	        Since the size of $List$, in $r$,  grows monotonically, and because  $w_{\pi}$ invokes $\pi$ after $\phi$ completes hence  $tag(\pi)$ is at least as high as $tag(\phi)$, so $\pi\not \prec \phi$.
	      %
	        \item[$(c)$] $\phi$ and $\pi$ are {\sc read}s  invoked by reader $r$.
	           Since the size of $List$, in $r$,  grows monotonically,  hence  $w_{\pi}$ invoked $\pi$ after $\phi$ completes hence $tag(\pi)$ is at least as high as $tag(\phi)$, so $\pi\not \prec \phi$.
	        %
	         \item [$(d)$] $\phi$ is a {\sc read}, $\pi$ is a {\sc write}  invoked by reader $r$ and $w_{\pi}$, respectively.
	         This case is simple because new values are added to $List$  only  by writers, and $tag(\pi)$
	         is larger than the tag of $\phi$ and hence   $\pi\not \prec \phi$.
	\end{enumerate}

	\emph{P3:} This is clear by the fact that any {\sc write} transaction always creates a unique tag and all tags are totally ordered since they all belong to $\mathbb{N}$

	\emph{P4:} Consider a {\sc read} $\rho$ as $READ(o_{i_1}, o_{i_2}, \cdots, o_{i_q})$, in $\beta$.
Let the returned value from $\rho$ be $\mathbf{v} \equiv $$(v_{i_1}, v_{i_2}, \cdots, v_{i_q})$ such that
$1 \leq {i_1} <  {i_2} <  \cdots <  {i_q} \leq k$, where value  $v_{i_j}$ corresponds to $o_{i_j}$.
	Suppose $tag(\rho) \in \mathbb{N}$ was created during some {\sc write} transaction, say $\phi$, i.e., $\phi$ is the {\sc write} that
	added the elements in index $(tag(\rho)-1)$ of $List$. Note that element in index $0$ contains the initial value.
	%because {\sc read} transactions do not generate new tags as they do not add any new item to the $Vals$ of any server.
	 Now we consider two cases:

	\emph{Case $tag(\rho) = 1$.} We  know that it corresponds the initial default value $v_i^0$ at each sub-object $o_i$, and this equates to $\rho$ returning the default initial value for each sub-object.
	 %
	% Therefore, $tag(\rho) = tag(\phi)$.

	 \emph{Case $tag(\rho) > 1$.} Then we argue that there exists no {\sc write} transaction, say $\pi$, that updated object $o_{i_j}$,   in $\beta$, such that,  $\pi \neq \phi$ and $\rho$ returns values written by $\pi$ and $\phi \prec \pi \prec \rho$. Suppose we assume the 	contrary, which means $tag(\phi) < tag(\pi) < tag(\rho)$. The latter implies $tag(\phi)  = tag(\pi)$ which is not possible because
	this contradicts the fact that for any two distinct {\sc write}s $tag(\phi) \neq tag(\pi)$  in any execution of   $A$.

	\noindent{\emph{\underline{N property:}}}  By inspection of algorithm $A$ for the  response steps  of the servers to the reader.

	\noindent{\emph{\underline{O property:}}} By inspection of the  {\readValue} phase: it consists of one round of communication between the reader and the servers, where the servers send only one version of the value of the object it maintains.

	\noindent{\emph{\underline{W property:}}}  By inspection of the {\sc write} transaction steps, and  and  that writers always get to complete the transactions they invoke.
	%Finally, the liveness property of {\sc write} transactions can be realized by inspecting the steps of  algorithm $A$.
	\end{proof}
}


\section{Generating functions}


\documentclass[12pt]{article}
\textwidth 150mm
\textheight 230mm
\topmargin -24pt
\oddsidemargin 0.4cm
\evensidemargin 0.4cm

\usepackage{amsmath}
\usepackage{amssymb}

\newcommand{\Gr}{Gr\"obner }
\newcommand{\B}{\mathbb{B\,}}
\newcommand{\F}{\mathbb{F}}

\title{BIBASIS: A Package for Calculating Boolean Involutive Bases}
\date{}
\author{
    Yuri A. Blinkov\\[0.2cm]
    Saratov State University\\
    Saratov, Russia\\
    Email: BlinkovUA@info.sgu.ru\\[0.2cm]
    and \\[0.2cm]
    Mikhail V. Zinin\\[0.2cm]
    Joint Institute for Nuclear Research\\
    Dubna, Russia\\[0.2cm]
    Email: mzinin@gmail.com}

\begin{document}
\maketitle

\section{Introduction}

Involutive polynomial bases are redundant \Gr bases of special structure with some additional useful features in comparison
with reduced \Gr bases~\cite{GB'98} . Apart from numerous applications of involutive bases~\cite{Seiler'10} the
involutive algorithms~\cite{Gerdt'05} provide an efficient method for computing reduced \Gr bases. A reduced \Gr basis
is a well-determined subset of an involutive basis and can be easily extracted from the latter without any extra reductions.
All this takes place not only in rings of commutative polynomials but also in Boolean rings.

Boolean \Gr basis already have already revealed their value and usability in  practice. The first impressive demonstration
of practicability of Boolean \Gr bases was breaking the first HFE (Hidden Fields Equations) challenge in the public
key cryptography done in~\cite{Faugere'03} by computing a Boolean \Gr basis for the system of quadratic
polynomials in 80 variables. Since that time the Boolean \Gr bases application area has widen drastically and nowadays there
is also a number of quite successful examples of using \Gr bases for solving SAT problems.

During our research we had developed~\cite{ISSAC'08, PaCS'08, PCA'09} Boolean involutive algorithms based on Janet and Pommaret
divisions and applied them to computation of Boolean \Gr bases. Our implementation of both divisions has experimentally
demonstrated computational superiority of the Pommaret division implementation. This package BIBASIS is the result
of our thorough research in the field of Boolean \Gr bases. BIBASIS implements the involutive algorithm based on Pommaret
division in a multivariate Boolean ring.

In section 2 the Boolean ring and its peculiarities are shortly introduced. In section 3 we briefly argue
why the involutive algorithm and Pommaret division are good for Boolean ring while the Buhberger's algorithm is not.
And finally in section 4 we give the full description of BIBASIS package capabilities and illustrate it by examples.

\section{Boolean Ring}

{\em Boolean ring} perfectly goes with its name, it is a ring of {\em Boolean functions} of $n$ variables, i.e
mappings from $\{0,1\}^n$ to $\{0,1\}^n$. Considering these variables are $\mathbf{X}:=\{x_1,\ldots,x_n\}$ and
$\F_2$ is the finite field of two elements $\{0,1\}$, Boolean ring can be regarded as the quotient ring
$$
\B[\mathbf{X}]:=\F_2[\mathbf{X}]\,/<x_1^2+x_1,\ldots,x_n^2+x_n>.
$$
Multiplication in $\B[\mathbf{X}]$ is {\em idempotent} and addition is {\em nilpotent}
$$
\forall\, b\in \B[\mathbf{X}]\ :\ \,b^2=b\,,\ b+b=0.
$$
Elements in $\B[\mathbf{X}]$ are {\em Boolean polynomials} and can be represented as finite sums
$$
\sum_j \prod_{x\in\, \Omega_j \subseteq\, \mathbf{X}} x
$$
of {\em Boolean monomials}. Each monomial is a conjunction. If set $\Omega$ is empty, then the corresponding
monomial is the unity Boolean function 1. The sum of zero monomials corresponds to zero polynomial, i.e. is
zero Boolean function 0.

\section{Pommaret Involutive Algorithm}

Detailed description of involutive algorithm can found in~\cite{Gerdt'05}. Here we note that result of both
involutive and Buhberger's algorithms depend on chosen monomial ordering. At that the ordering must be
admissible, i.e.
$$
\ m \neq 1 \Longleftrightarrow m \succ 1, \quad \ \ m_1 \succ m_2 \Longleftrightarrow m_1 m \succ m_2 m
\quad \ \ \forall \ m, m_1, m_2.
$$
But as one can easily check the second condition of admissibility does not hold for any monomial ordering
in Boolean ring:
$$
x_1\succ x_2\quad \xrightarrow{\ \ *x_1\ \ }\quad x_1*x_1\succ x_2*x_2\quad \xrightarrow{\ \ \ \ \ }\quad x_1 \prec x_1x_2
$$
Though $\B[\mathbf{X}]$ is a principal ideal ring, boolean singleton $\{p\}$ is not necessarily a \Gr basis of
ideal $<p>$, for example:
$$
x_1,x_2\in \,<x_1x_2 + x_1 + x_2> \subset \B[x_1, x_2].
$$
That the reason why one cannot apply the Buhberger's algorithm directly in a Boolean ring, using instead a ring
$\F_2[\mathbf{X}]$ and {\em the field binomials} $x_1^2+x_1,\ldots,x_n^2+x_n$.

The involutive algorithm based on Janet division has the same disadvantage unlike the Pommaret division algorithm as shown
in~\cite{ISSAC'08}. The Pommaret division  algorithm can be applied directly in a Boolean ring and admits effective data
structures for monomial representation.

\section{BIBASIS Package}

The package BIBASIS implements the Pommaret division algorithm in a Boolean ring. The first step to using the package
is to load it:
\begin{verbatim}
    1: load_package bibasis;
\end{verbatim}
The current version of the BIBASIS user interface consists only of 2 functions:     \texttt{bibasis} and
\texttt{bibasis\_print\_statistics}.

\vskip 1.0cm

\noindent The \texttt{bibasis} is the function that performs all the computation and has the following syntax:
\begin{center}
    \texttt{bibasis(initial\_polynomial\_list, variables\_list, monomial\_ordering, reduce\_to\_groebner);}
\end{center}
Input:
\begin{itemize}
    \item \texttt{initial\_polynomial\_list} is the list of polynomials containing the known basis of initial
Boolean ideal. All given polynomials are treated modulo 2. See Example 1.

    \item \texttt{variables\_list} is the list of independent variables in decreasing order.

    \item \texttt{monomial\_ordering} is a chosen monomial ordering and the supported ones are:
        \begin{itemize}
            \item[] \texttt{lex} -- pure lexicographical ordering;
            \item[] \texttt{deglex} -- degree lexicographic ordering;
            \item[] \texttt{degrevlex} -- degree reverse lexicographic.
        \end{itemize}
        See Examples 2---4 to check that \Gr (as well as involutive) basis depends on monomial ordering.

    \item \texttt{reduce\_to\_groebner} is a Boolean value, if it is \texttt{t} the output is the reduced
        Boolean \Gr basis, if \texttt{nil}, then the reduced Boolean Pommaret basis. Examples 5,6 show distinctions between these two outputs.
\end{itemize}
Output:
\begin{itemize}
 \item The list of polynomials which constitute the reduced Boolean \Gr or Pommaret basis.
\end{itemize}

\vskip 1.0cm

\noindent The syntax of \texttt{bibasis\_print\_statistics} is simple:
\begin{center}
    \texttt{bibasis\_print\_statistics();}
\end{center}
This function prints out a brief statistics for the last invocation of \texttt{bibasis} function. See Example 7.

\section{Examples}

Example 1:
\begin{verbatim}
    1: load_package bibasis;
    2: bibasis({x+2*y}, {x,y}, lex, t);
    {x}

\end{verbatim}

\noindent Example 2:
\begin{verbatim}
1: load_package bibasis;
2: variables :={x0,x1,x2,x3,x4}$
3: polynomials := {x0*x3+x1*x2,x2*x4+x0}$
4: bibasis(polynomials, variables, lex, t);
{x0 + x2*x4,x2*(x1 + x3*x4)}

\end{verbatim}

\noindent Example 3:
\begin{verbatim}
1: load_package bibasis;
2: variables :={x0,x1,x2,x3,x4}$
3: polynomials := {x0*x3+x1*x2,x2*x4+x0}$
4: bibasis(polynomials, variables, deglex, t);
{x1*x2*(x3 + 1),
 x1*(x0 + x2),
 x0*(x2 + 1),
 x0*x3 + x1*x2,
 x0*(x4 + 1),
 x2*x4 + x0}

\end{verbatim}

\noindent Example 4:
\begin{verbatim}
1: load_package bibasis;
2: variables :={x0,x1,x2,x3,x4}$
3: polynomials := {x0*x3+x1*x2,x2*x4+x0}$
4: bibasis(polynomials, variables, degrevlex, t);
{x0*(x1 + x3),
 x0*(x2 + 1),
 x1*x2 + x0*x3,
 x0*(x4 + 1),
 x2*x4 + x0}

\end{verbatim}

\newpage

\noindent Example 5:
\begin{verbatim}
1: load_package bibasis;
2: variables :={x,y,z}$
3: polinomials := {x, z}$
4: bibasis(polinomials, variables, degrevlex, t);
{x,z}

\end{verbatim}

\noindent Example 6:
\begin{verbatim}
1: load_package bibasis;
2: variables :={x,y,z}$
3: polinomials := {x, z}$
4: bibasis(polinomials, variables, degrevlex, nil);
{x,z,y*z}

\end{verbatim}

\noindent Example 7:
\begin{verbatim}
1: load_package bibasis;
2: variables :={u0,u1,u2,u3,u4,u5,u6,u7,u8,u9}$
3: polinomials := {u0*u1+u1*u2+u1+u2*u3+u3*u4+u4*u5+u5*u6+u6*u7+u7*u8+u8*u9,
3:                 u0*u2+u1+u1*u3+u2*u4+u2+u3*u5+u4*u6+u5*u7+u6*u8+u7*u9,
3:                 u0*u3+u1*u2+u1*u4+u2*u5+u3*u6+u3+u4*u7+u5*u8+u6*u9,
3:                 u0*u4+u1*u3+u1*u5+u2+u2*u6+u3*u7+u4*u8+u4+u5*u9,
3:                 u0*u5+u1*u4+u1*u6+u2*u3+u2*u7+u3*u8+u4*u9+u5,
3:                 u0*u6+u1*u5+u1*u7+u2*u4+u2*u8+u3+u3*u9+u6,
3:                 u0*u7+u1*u6+u1*u8+u2*u5+u2*u9+u3*u4+u7,
3:                 u0*u8+u1*u7+u1*u9+u2*u6+u3*u5+u4+u8,
3:                 u0+u1+u2+u3+u4+u5+u6+u7+u8+u9+1}$
4: bibasis(polinomials, variables, degrevlex, t);
{u3*u6,
 u3*u7,
 u7*(u6 + 1),
 u3*u8,
 u6*u8 + u6 + u7,
 u7*u8,
 u3*(u9 + 1),
 u6*u9 + u7,
 u7*(u9 + 1),
 u8*u9 + u6 + u7 + u8,
 u0 + u3 + u6 + u9 + 1,
 u1 + u7,
 u2 + u7 + u8,
 u4 + u6 + u8,
 u5 + u6 + u7 + u8}
5: bibasis_print_statistics();
        Variables order = u0 > u1 > u2 > u3 > u4 > u5 > u6 > u7 > u8 > u9
Normal forms calculated = 216
  Non-zero normal forms = 85
        Reductions made = 4488
Time: 270 ms
GC time: 0 ms

\end{verbatim}

\begin{thebibliography}{99}

\bibitem{GB'98} V.P.Gerdt and Yu.A.Blinkov. {\em Involutive Bases of Polynomial Ideals}.
Mathematics and Computers in Simulation, 45, 519--542, 1998; {\em Minimal Involutive Bases}, ibid. 543--560.

\bibitem{Seiler'10} W.M.Seiler. {\em Involution: The Formal Theory of Differential Equations and its Applications
in Computer Algebra}. Algorithms and Computation in Mathematics, 24, Springer, 2010.  arXiv:math.AC/0501111

\bibitem{Gerdt'05} Vladimir P. Gerdt. {\em Involutive Algorithms for Computing \Gr Bases}.
Computational Commutative and Non-Commutative Algebraic Geometry. IOS Press, Amsterdam, 2005, pp.199--225.

\bibitem{Faugere'03}
J.-C.Faug\`{e}re and A.Joux. Algebraic Cryptanalysis of Hidden Field Equations
(HFE) Using \Gr Bases. {\em LNCS} 2729, Springer-Verlag, 2003, pp.44--60.

\bibitem{ISSAC'08}
V.P.Gerdt and M.V.Zinin. A Pommaret Division Algorithm for Computing \Gr Bases in Boolean Rings.
{\em Proceedings of ISSAC 2008}, ACM Press, 2008, pp.95--102.

\bibitem{PaCS'08}
V.P.Gerdt and M.V.Zinin. Involutive Method for Computing \Gr Bases over $F_2$.
{\em Programming and Computer Software}, Vol.34, No. 4, 2008, 191--203.

\bibitem{PCA'09}
Vladimir Gerdt, Mikhail Zinin and Yuri Blinkov. On computation of Boolean involutive bases,
Proceedings of International Conference Polynomial Computer Algebra 2009, pp. 17-24
(International Euler Institute, April 7-12, 2009, St. Peterburg, Russia)

\end{thebibliography}

\end{document}


\section{Metatheory}

In this section we present several properties that are formally verified.
For the declarative system, the typing subsumption and subtyping
transitivity lemmas are discussed in detail.
The algorithmic system is proven to be sound with respect
to the declarative system via a transfer relation.
A partial completeness theorem is shown under the rank-1 restriction.
We then briefly describe the challenges we face when proving termination.
Lastly, proof statistics of Abella are discussed.

\subsection{Declarative Properties}\label{sec:meta:decl}

\paragraph{The Typing Subsumption Lemma.}
An important desired property for a type system is \emph{checking subsumption},
which says that any expression $e$ can
check against $B$ if $e$ checks against $A$ and $A \le B$.
Since our bidirectional type system defines the checking mode, inference mode and
application inference mode mutually,
we formalize the generalized \emph{typing subsumption}.

First of all, we give the definition of worklist subtyping,
which is used to further generalize the typing subsumption lemma.
This is necessary because rules like $\mathtt{Decl{\to}I}$
will push the argument type $A$ into the context,
thus when checking against a super type of $A \to B$, say $C \to D$,
will cause the bind of $x$ in the context to a subtype of $A$ (since $C \le A$).

\begin{definition}[Worklist Subtyping]
    Worklist subtyping compares the type of variables bound in the worklist.
    $\Psi <: \Psi'$ iff each binding in $\Psi$ is converted to one with a super type.
    \begin{gather*}
        \inferrule*[right=$\mathtt{<:nil}$]
            {~}{\cdot <: \cdot}
        \qquad
        \inferrule*[right=$\mathtt{<:ty}$]
            {\Psi <: \Psi'}
            {\Psi, a <: \Psi', a}
        \\
        \inferrule*[right=$\mathtt{<:of}$]
            {\Psi' \vdash A \le B \\ \Psi <: \Psi'}
            {\Psi, x:A <: \Psi', x:B}
        \qquad
        \inferrule*[right=$\mathtt{<:\omega}$]
            {\Psi <: \Psi'}
            {\Psi \Vdash \jg <: \Psi' \Vdash \jg}
    \end{gather*}
\end{definition}

A basic property of worklist subtyping is that they acts similarly when
dealing with subtyping between well-formed types.
\begin{lemma}[Worklist Subtyping Equivalence]~\\
    Given $\Psi <: \Psi'$, $\Psi \vdash A \le B \Longleftrightarrow \Psi' \vdash A \le B$.
\end{lemma}

Finally, we give the statement of typing subsumption lemma,
which is generalized by the worklist subtyping relation.

\begin{lemma}[Typing Subsumption]
    Given $\Psi <: \Psi'$,
    \begin{enumerate}[1)]
        \item If $\Psi' \vdash e \Lto A$ and $\Psi' \vdash A \le B$, then $\Psi \vdash e \Lto B$;
        \item If $\Psi' \vdash e \To A$, then $\exists B$ s.t. $\Psi' \vdash B \le A$ and $\Psi \vdash e \To B$.
        \item If $\Psi' \vdash \appInf{C}{e}{A}$ and $\Psi' \vdash D \le C$, then
            $\exists B$ s.t. $\Psi' \vdash B \le A$ and $\Psi \vdash \appInf{D}{e}{B}$.
    \end{enumerate}
\end{lemma}

\begin{proof}
    By induction on the following size measure (lexicographical order on a 3-tuple):
    \begin{itemize}
        \item Checking ($e \Lto A$): $\langle |e|, 1, |A|_\forall + |B|_\forall \rangle$
        \item Inference ($e \To A$): $\langle |e|, 0, 0 \rangle$
        \item Application inference ($\appInf{A}{e}{C}$): $\langle |e|, 2, |C|_\forall + |D|_\forall \rangle$
    \end{itemize}
    Most of the cases are straightforward.
    When rule $\mathtt{{\le}\forall L}$ is applied for the subtyping predicate
    like $\Psi' \vdash A \le B$,
    a mono-type substitution is performed on $\all A$,
    resulting in $[\tau/a]A$.
    Since $\tau$ is a mono-type, the result type reduces the number of $\forall$'s,
    and thus reduces the size measure.
\end{proof}

Interestingly, the two new declarative rules
$\mathtt{Decl\top}$ and $\mathtt{Decl{\bot}App}$ are
discovered when we were trying to prove the property instead of
before exploring the meta-theory.
Given the typing and subtyping judgments $\Psi \vdash e \Lto A$ and $\Psi \vdash A \le \top$,
we should derive $\Psi \vdash e \Lto \top$ from the lemma,
therefore Rule $\mathtt{Decl\top}$ is required,
saying that any expression can be checked against the top type.
Note that here we do not include the condition $e \Lto A$,
since we may elaborate the term to the top term
which no longer refers to the original term.

Similarly, the most general type $\bot$,
being able to convert to any type due to Rule $\mathtt{{\le}Bot}$,
can be converted to any function type,
or simply the most general one $\top \to \bot$,
which accepts any input and returns the $\bot$ type,
resulting in the derivation $\Psi \vdash \appInf{\bot}{e}{\bot}$.
From the lemma we can also derive that by
$\Psi \vdash \appInf{C}{e}{A}$, $\Psi \vdash \bot \le C$ and $\Psi \vdash \bot \le A$.

With the addition of Rules $\mathtt{Decl\top}$ and $\mathtt{Decl{\bot}App}$,
we can prove the typing subsumption lemma.
To the best of the author's knowledge,
they are the minimal set of rules that make the lemma hold.

\paragraph{Transitivity of Subtyping}

The transitivity lemma for declarative subtyping is a commonly expected property.
The proof depends on the following subtyping derivation size relation and an auxiliary lemma.

\begin{definition}[Subtyping Derivation Size]
    \begin{gather*}
        \begin{aligned}
            |1 \le 1| &= 0\\
            |a \le a| &= 0\\
            |A \le \top| &= 0\\
            |\bot \le B| &= 0\\
            |A_1 \to A_2 \le B_1 \to B_2| &= |B_1 \le A_1| + |A_2 \le B_2| + 1\\
            |\all A \le B| &= |[\tau/a]A \le B| + 1\\
            |A \le \all B| &= |A \le B| + 1
        \end{aligned}
    \end{gather*}
\end{definition}

\begin{lemma}[Monotype Subtyping Substitution]
    If $\Psi \vdash \tau$ and $\Psi, a, \Psi_R \vdash A \le B$, then
    $\Psi, [\tau/a]\Psi_R \vdash [\tau/a]A \le [\tau/a]B$.
\end{lemma}

\begin{proof}
    A routine induction on the subtyping relation $\Psi, a, \Psi_R \vdash A \le B$
    finishes the proof.
\end{proof}

\begin{corollary}[Monotype Subtyping Substitution for Type Variables]
    \label{cor:subtyping_subst_mono}
    If $\Psi \vdash \tau$ and $\Psi, a \vdash A \le B$, then
    $\Psi \vdash [\tau/a]A \le [\tau/a]B$.
\end{corollary}

The above lemma and corollary reveal the fact
that a type variable occurred in the subtyping relation
represents an \emph{arbitrary} well-formed monotype.
And it also explains the difference in treatment of polymorphic types between
Rules $\mathtt{{\le}{\forall}L}$ and $\mathtt{{\le}{\forall}R}$:
Rule $\mathtt{{\le}{\forall}R}$ is in fact equivalent to:
$$
\inferrule*[right=$\mathtt{{\le}{\forall}R'}$]
    {\forall \tau \text{ s.t. } \Psi \vdash \tau \Longrightarrow \Psi \vdash A \le [\tau/b]B}
    {\Psi \vdash A \le \all[b] B}
$$

Finally, with the size measure defined and required lemma proven,
we can obtain the transitivity lemma for declarative subtyping.

\begin{lemma}[Subtyping Transitivity]
    If $\Psi \vdash A \le B$ and $\Psi \vdash B \le C$ then
    $\Psi \vdash A \le C$.
\end{lemma}

\begin{proof}
    Induction on the lexicographical order defined by
    $\langle |B|_\forall, |A \le B| + |B \le C| \rangle$.
    Most cases preserve the first element of the size measures $|B|_\forall$,
    and are relatively easy to prove.
    The difficult case is when $B$ is a polymorphic type,
    when the conditions are $\Psi \vdash A \le \all B$ and $\Psi \vdash \all B \le C$.
    They are derived through rules $\mathtt{{\le}\forall L}$ and
    $\mathtt{{\le}\forall R}$, respectively.
    Therefore, we have $\Psi, a \vdash A \le B$ and $\Psi \vdash [\tau/a] B \le C$.
    To exploit the induction hypothesis, the contexts should be unified.
    By Corollary~\ref{cor:subtyping_subst_mono}, $\Psi \vdash A \le [\tau/a]B$.
    Notice that the freshness condition is implicit for rule $\mathtt{{\le}\forall L}$.
    Clearly, $|[\tau/a]B|_\forall < |\all B|_\forall$, i.e. the first size measure decreases.
    By induction hypothesis we get $\Psi \vdash A \le C$ and finish this case.
\end{proof}

\subsection{Transfer}

\begin{figure}[t]
    \begin{gather*}
    \begin{aligned}
    \text{Declarative worklist}\qquad&\Om &::=&\quad \nil \mid \Om, a \mid \Om, x: A \mid \Om \Vdash \jg
    \end{aligned}
    \end{gather*}
    \hfill \framebox{$\Gm \sto \Om$} \hfill $\Gm$ instantiates to $\Om$.
    \begin{gather*}
    \inferrule*[right=$\mathtt{{\sto}}\Om$]
    {~}
    {\Om \sto \Om}
    \quad
    \inferrule*[right=$\mathtt{{\sto}\al}$]
    {\Om\vdash\tau \\ \Om,[\tau/\al]\Gm \sto \Om}
    {\Om,\al,\Gm \sto \Om}
    \end{gather*}
    \Description{Declarative Worklists and Instantiation}
    \caption{Declarative Worklists and Instantiation}
    \label{fig:top:trans}
\end{figure}

Follow the approach of Section~\ref{sec:metatheory},
the transfer relation and the declarative instantiation relation are defined
in Figure~\ref{fig:top:trans}.

Similarly, Lemmas~\ref{lem:top:insert} and \ref{lem:top:extract}
generalizing Rule $\mathtt{{\sto}\al}$ hold as well.

\begin{lemma}[Insert]\label{lem:top:insert}
If $\Gm_L, [\tau/\al]\Gm_R \sto \Om$ and $\Gm_L\vdash \tau$
, then $\Gm_L, \al, \Gm_R \sto \Om$.
\end{lemma}
\begin{lemma}[Extract]\label{lem:top:extract}
If $\Gm_L, \al, \Gm_R \sto \Om$
, then there exists $\tau$ s.t. $\Gm_L\vdash\tau$ and $\Gm_L, [\tau/\al]\Gm_R \sto \Om$.
\end{lemma}

\begin{figure}[ht]
\hfill \framebox{$\|\Om\|$} \hfill Judgment erasure.
\begin{gather*}
\begin{aligned}
\|\nil\| &= \nil\\
\|\Om,a\| &= \|\Om\|, a\\
\|\Om,x:A\| &= \|\Om\|, x:A\\
\|\Om\Vdash\jg\| &= \|\Om\|
\end{aligned}
\end{gather*}

\hfill \framebox{$\Om \rto \Om'$} \hfill Declarative transfer.
\begin{gather*}
\begin{aligned}
\Om,a &\rto \Om \\  \Om,x:A & \rto \Om\\
\Om\Vdash A\le B &\rto \Om &\text{ when } \|\Om\| \vdash A\le B\\
\Om\Vdash e\Lto A &\rto \Om & \text{ when } \|\Om\| \vdash e\Lto A\\
\Om\Vdash e\To_a \jg &\rto \Om\Vdash[A/a]\jg & \text{ when } \|\Om\| \vdash e\To A\\
\Om\Vdash \appInfAlg{A}{e} &\rto \Om\Vdash[C/a]\jg & \text{ when } \|\Om\| \vdash \appInf{A}{e}{C}\\
\end{aligned}
\end{gather*}
\Description{Declarative Transfer}
\caption{Declarative Transfer}
\label{fig:top:decl:worklist}
\end{figure}

Figure~\ref{fig:top:decl:worklist} defines a relation $\Om \rto \Om'$,
checking that every judgment entry in the worklist
holds using a corresponding declarative judgment.

\subsection{Soundness}

Our algorithm is sound with respect to the declarative system.
For any worklist $\Gm$ that reduces successfully,
there is a valid instantiation $\Om$ that transfers all judgments
to the declarative system.
\begin{theorem}[Soundness]
If \emph{wf }$\Gm$ and $\Gm \redto \nil$,
then there exists $\Om$ s.t. $\Gm \sto \Om$ and $\Om \redto \nil$.
\end{theorem}

Soundness is a basic desired property of a type inference algorithm,
which ensures that the algorithm is always producing
valid declarative derivations when the judgments are accepted.

\subsection{Partial Completeness of Subtyping: Rank-1 Restriction}

The algorithm is incomplete due to the subtyping rules 14, 15, 20 and 21.
However, subtyping is complete with respect to the declarative system in a rank-1 setting.

\paragraph{Declarative Rank-1 Restriction}

Rank-1 types are also named type schemes in the Hindley-Milner type system.
$$\begin{aligned}
    \text{Declarative Type Schemes}\qquad&\sigma &::=&\quad \all \sigma \mid \tau\\
\end{aligned}$$
In other words, the universal quantifiers only appear in the top level
of all polymorphic types.

For declarative subtyping, a judgment must be of form $\sigma_1 \le \sigma_2$.

\subsection{Algorithmic Rank-1 Restriction (Partial Completeness)}

The algorithmic mono-types and type schemes are defined as following:
$$\begin{aligned}
    \text{Algorithmic Mono-types}\qquad&\tau_A &::=&\quad
        1 \mid \top \mid \bot \mid a \mid A\to B \mid \al\\
    \text{Algorithmic Type Schemes}\qquad&\sigma_A &::=&\quad \all \sigma_A \mid \tau_A\\
\end{aligned}$$

Starting from the declarative judgment $\sigma_1 \le \sigma_2$,
the algorithmic derivation might involve different other kinds of judgments.
The following derivation, as an example, shows how a rank-1 judgment derives.

$$\begin{aligned}
           & \cdot \Vdash \all a \to a \le \all[b] (b \to b) \to (b \to b)\\
    \rrule{8} & b \Vdash \all a \to a \le (b \to b) \to (b \to b)\\
    \rrule{7} & b, \al \Vdash \al \to \al \le (b \to b) \to (b \to b)\\
    \rrule{6} & b, \al \Vdash \al \le b \to b \Vdash b \to b \le \al\\
    \rrule{} & \cdots
\end{aligned}$$

In this derivation, we begin from a judgment of the form $\sigma \le \sigma$.
After Rule 8 is applied, the judgment becomes $\sigma \le \tau$,
since the right-hand-side polymorphic type is reduced to a declarative mono-type.
Then, Rule 7 introduces existential variables to the left-hand-side,
resulting in a judgment like $\tau_A \le \tau$,
or $\sigma_A \le \tau$ in a more general case.
Finally, Rule 6 breaks a judgment between functions into two sub-judgments,
which swaps the positions of the argument types
and creates a judgment like $\tau \le \tau_A$.
Notice that $\sigma_A$ is not possible to occur to the right
because the function type may not contain any polymorphic types as its argument type.

After a detailed analysis on the judgments derivations,
we found that the only possible judgments that a rank-1 declarative subtyping judgment
might step to belong to the following two categories:
$$\sigma_A \le \sigma \quad\text{or}\quad \tau \le \sigma_A$$
All the possible judgment types shown above fall into these categories.
For example, $\tau_A \le \tau$ is a special form of $\sigma_A \le \sigma$,
and $\tau \le \tau_A$ belongs to $\tau \le \sigma_A$.

An interesting observation is that $\al \le \bt$ does not belong to either category,
neither does $\al \le A \to B$ when $\al \in \text{FV}(A \to B)$.
Therefore, in the rank-1 setting, both cases of incompleteness never occur,
and our algorithm is complete.

\begin{theorem}[Completeness of Rank-1 Subtyping]
    Given $\Psi \vdash \sigma_1 \le \sigma_2$,
    \begin{itemize}
        \item If $\Gm \Vdash \sigma_A \le \sigma \sto \Psi \Vdash \sigma_1 \le \sigma_2$
            \\then $\Gm \Vdash \sigma_A \le \sigma \redto \nil$;
        \item If $\Gm \Vdash \tau \le \sigma_A \sto \Psi \Vdash \sigma_1 \le \sigma_2$
            \\then $\Gm \Vdash \tau \le \sigma_A \redto \nil$.
    \end{itemize}
\end{theorem}

\subsection{Termination}

The measure used in Chapter~\ref{chap:ICFP} no longer works because subtyping judgments like
$$\al \le \bot \to \top$$
cause $\al$ to split into $\al[1] \to \al[2]$, without solving any part of it,
resulting in an increased number of existential variables
and possibly increased complexity of the worklist through the size-increasing substitution
$\{\al := \al[1] \to \al[2]\}$.

We have performed a large set of tests on generated subtyping judgments
that are consist of algorithmic monotypes,
and all judgments terminated within a reasonable number of derivation depth.
Unfortunately, we have not yet find any formal proof for the termination statement.

In some theoretical point of view, an algorithm should terminate on any valid input.
However, there are programs that do not terminate,
which we also consider them as an ``algorithm''.
Therefore, we would still call our reduction procedure an algorithm,
although the termination argument has not been formally proven.

\subsection{Formalization in the Abella Proof Assistant}

We have chosen the Abella (v2.0.7-dev
\footnote{We use a forked version \url{https://github.com/JimmyZJX/abella} by only enhancing the Abella prover with a handy
``applys'' tactic.}
) proof assistant~\citep{AbellaDesc} to develop our formalization.
Equipped with HOAS, Abella eases the formalization and proof tasks a lot
compared with various libraries in Coq.
Additionally, our algorithm heavily uses eager substitutions,
and Abella greatly simplifies relevant proofs thanks to its built-in
substitution representation and higher-order unification algorithms.

The reader may find the source code of the proof at \url{https://github.com/JimmyZJX/Dissertation/tree/main/src/Subtyping}.

\paragraph{Statistics of the Proof}
The proof script consists of 7,301 lines of Abella code with a total of
48 definitions and 592 theorems.
Figure~\ref{table:top:proof_statistics} briefly summarizes the contents of each file.
The files are linearly dependent due to the limitations of Abella.

\begin{table}[t]
    \renewcommand{\arraystretch}{1.2}
    \caption{Statistics for the proof scripts}
    \centering\begin{tabular}{@{}lrrl@{}}
    \toprule
        File(s) & LOC & \#Thm & Description\\
    \midrule
        olist.thm, nat.thm  &   311 & 57  & Basic data structures\\
        typing.thm          &   273 & 7   & Declarative \& algorithmic system, debug examples\\
        decl.thm            &   241 & 33  & Basic declarative properties\\
        order.thm           &   274 & 27  & The $|\cdot|_\forall$ measure; decl. subtyping strengthening\\
        alg.thm             &   699 & 82  & Basic algorithmic properties\\
        trans.thm           &   635 & 53  & \makecell[l]{Worklist instantiation and declarative transfer;\\
                                Lemmas~\ref{lem:insert}, \ref{lem:extract}}\\
        declTyping.thm      & 1,087 & 76  & \makecell[l]{Non-overlapping declarative system; \\
                                Lemmas~\ref{lem:inv_allR}, \ref{lem:inv_chkAll},
                                    \ref{lem:inv_chkLam}, \ref{lem:subsumption}}\\
        soundness.thm       & 1,206 & 81  & Soundness theorem; aux. lemmas on transfer\\
        dcl.thm             &   417 & 12  & Non-overlapping declarative worklist \\
        scheme.thm          & 1,113 & 98  &
                                Type scheme (rank-1 restriction)\\
        completeness.thm    & 1,045 & 63  &
                                Completeness theorem; aux. lemmas and relations\\
    \midrule
        \emph{Total}        & 7,301 & 592 & (48 definitions in total)\\
    \bottomrule
    \end{tabular}
    \label{table:top:proof_statistics}
\end{table}