%=================================================

% Notes on human-viewable (and robot-viewable) images,
% information content and so on.

% These are thoughts in progress -- hopefully this text
% will be useful at some point, chopped up and rearranged
% into a coherent story!

\documentclass[letterpaper, 11pt]{article}

%=================================================

\usepackage{amsmath,amssymb}

\title{Viewing Informative Images}
\author{Phil Marshall\thanks{\texttt{dr.phil.marshall@gmail.edu}}}
\date{\today}

%%-------------------------------------------------

\begin{document}

\maketitle

\begin{abstract}

In astronomy, and many other areas of science, discoveries can be made simply
by looking at images. While this might be thought of as data exploration, we
argue that it is actually a form of data modeling -- and that  by considering
a robotic viewer who computes and reports the Shannon information content of
images relative to its model for how they were generated, we arrive at some
hypotheses for how one might prepare images for a human image inspection
program that has high capability for discovery.

\end{abstract}

% =================================================

\section{Introduction}

Images contain information. In astronomy, new planets, stars and galaxies can
be discovered simply by looking at images taken of the sky through telescopes
-- a statement which can be generalised to many other fields of science. How
is this possible? For something new to be discovered in an image, that new
feature must, by definition, be something that has not been seen before:
something that is at odds with our expectations of the features that similar
images contain, something that is surprising. Image viewing is a form of data
exploration, an important step before making quantitative measurements. But
both exploration and measurement involve data modeling, or inference: the
viewer necessarily interprets the image in the context of a model for that
image that they have in their heads. The surprisingness of a feature in an
image is relative to this model: discovery is relative to what is already
known. Note that in general discovery is a personal experience: each viewer
has their own model for image features against which new images are compared.
Only if the model is shared among (and agreed upon by) all viewers will a
discovery be global: if making such global discoveries is our goal as
scientists, then we must make this model as complete as possible, and publish
it. This is what astronomers do.

A mathematical description of discovery must therefore relate the data in hand
to the model in mind, and will provide a quantification of that new
information which constitutes the discovery.  In probability theory, the
(Shannon) information is defined as $I = -\log_2 P$, where $P$ is a
probability. $I$ is a measure of surprise: the lower an event's probability,
the more surprising it is, and the higher is its information content $I$. We
can  compute the  information content of a dataset $\boldsymbol{d}$ using this
definition, such that $I(\boldsymbol{d}|H)$ quantifies how surprising that
dataset is to its observer, who (naturally)  has a set of expectations $H$
about it.  Specifically, the information content of the dataset (in bits) is
given by
\begin{equation}
I(\boldsymbol{d}|H) = -\log_2 {\rm Pr}(\boldsymbol{d}|H).
\end{equation}
This particular probability (also known as the evidence, or marginal
likelihood) is an integral over the (typically immense) parameter space of the
model,
${\rm Pr}(\boldsymbol{d}|H) =
\int {\rm Pr}(\boldsymbol{d}|\boldsymbol{\theta},H)
     {\rm Pr}(\boldsymbol{\theta}|H)$. In this expression
we see two familiar quantities, the likelihood of the data, and the prior PDF
over the model parameters.  The negative sign indicates that information
content is highest when the marginal likelihood is {\it minimized}. That is,
datasets  that are fitted terribly by the model, even when considering all
possible  values of the parameters of the model, are highly informative.
Further, if the data quality improves, then the goodness of fit to this
terrible model  will get {\it worse}, in that the significance of the
residuals will increase -- which means a decrease in the likelihood, and an
increase in the information content of the dataset. In this way,  information
content can be high because the data is good, or the model is bad -- or both.

What does this mean for image viewing?  Let us consider a robot whose purpose
is to view images and report new discoveries from them. We could design it
such that it computes and reports the information $I$ associated with each
feature in the image, based on an internal model for astronomical images that
we provide.  Clearly, if $I$ is to be a useful metric for the robot to use
when  evaluating a dataset, the model $H$ has to be as complete as possible:
it has to represent everything that we know about the features present in
astronomical images, including the probability distributions for the
parameters that describe those features. If $H$ is incomplete in some way, the
robot will be surprised by features that we find familiar. Only when $H$ is
complete will the robot be efficient at discovery, and report features that
are new to all of us as well as to itself. Note that in science we seek both
discovery and understanding, which means maximizing information content with
respect to $\boldsymbol{d}$, but minimizing it with respect to $H$: for a
given dataset we want to find the model that agrees with it the most, but for
a given model we want the dataset that agrees with it the least.

Our working supposition is that the robot introduced above is a plausible
model for how humans actually process images when they view them.  By
considering our response to various changes in the data we view, or in our
internal model of it,  we can make some recommendations for the design of
practical image-viewing robots; likewise, guided by the mathematical rules
followed by our hypothetical robot, we hope to be able to make some hypotheses
about how to carry out a human image inspection discovery program.

% ----------------------------------------------------------------------------

\section{Expectations about Images}

What is our internal model for astronomical images? What prior PDF do we
assign to its parameters?  The answers to these questions are rather different
to the models and priors often used in the image processing literature. There,
for example, gross image properties such as sparseness and positivity have
been used extensively. The prior PDFs associated with these expectations are
so uninformative that the information content of an observed image will always
be high, just because images drawn from these prior distributions do not look
like astronomical images. Features in the image will appear to have
information $I$ that increases as they depart further from the featureless
background: big galaxies will appear to be more informative  than small  ones,
no matter how interesting the latter are, and so on.

When viewing astronomical images ourselves we are typically more interested in
the features of images rather than the images themselves, and how interested
we are depends on what we have seen before. A prior PDF for features in
astronomical images is so high dimensional and
complex that it has not yet been encoded such that a robot can view images
in the manner of a human. However, one way to approach this problem
would be to build one in
the same way that we did for ourselves: we note interesting things in images
based on our experience of a large library of previously viewed images. Humans
are extremely good at interpolating between points in image feature space,
recognising a galaxy as being a galaxy based on their experience of many other
images of galaxies.  What is especially impressive is that this set of images
is typically highly heterogeneous, having varying resolution, depth, dynamic
range and spectral coverage. This means that when we are presented with a new
image, we have to infer its resolution, depth, dynamic range and  spectral
coverage from the data itself, with only fairly broad priors to work from. Had
our training images been displayed {\it homogeneously}, these priors could be
made much narrower, to the point where the properties of the imaging system
could be assumed rather than inferred from the data each time. This would
simplify the robot's task significantly.

Note that in considering such a procedure, we are dividing up our image  model
parameters into those we are interested in (that describe astrophysical
objects) and those we are not (that describe the imaging system). We don't
just want informative images -- we want that information to be about
astrophysical objects. Image features of non-astrophysical origin are called
artifacts. They  have potentially high surprise value, but are not
interesting. Artifacts can be introduced by either the hardware or software
parts of an imaging system, but either way they provide high information
content unless they are included in the model.  How should we deal with
artifacts to make our robot's task easier? One way would be to include them in
its model, so that it would recognise artifacts and ignore them. The problem
with this approach is one of confusion: if a particular class of artifacts
looks like a new, interesting thing, images that contain examples of those
new, interesting things will be assigned low information content by the robot.
If we are looking for faint, blue, streaks, we had better not
introduce artifacts that are faint, blue and streaky.
Model degeneracy between artifacts and new features is a particularly
problematic barrier to discovery. Perhaps the best way round it is to  note
that truly informative images are {\it clean}, and assert that the
fewer artifacts introduced, the better.

{\it A model for features in astronomical images can be built by interpolating
a large sample of library images. This task can be simplified by partitioning
off the volume of parameter space associated with the imaging system itself,
(by preparing a homogeneous set of library images) and by avoiding (where
possible) the need to parametrize uninteresting artifacts (by designing an
imaging system that does not introduce them in the first place).}

% ----------------------------------------------------------------------------

\section{Image Quality}

A viewer with a given model in mind will make discoveries in the images  that
are highly {\it informative}. What do we mean by this? Consider the following
examples of high information content images, ones with high ``surprise value''
that were found to contain:  an unusual faint object was spotted;  an
otherwise typical bright object was seen to actually be two objects; a
familiar-looking object was found to have unusual colour.  In each of these
examples, the feature was somewhat familiar, but some property of the image
itself enabled the surprise of the feature: {\it angular resolution, depth and
colour are all clearly related to information content}.

The robot's information content, $I(\boldsymbol{d}|H)$, can be seen to depend
on these image properties too, as follows. Recall that in order to compute
$I(\boldsymbol{d}|H)$, the robot has to predict the image by varying the
parameters of its model, compute the likelihood of each predicted image, and
then integrate over all its model parameters to compute the evidence for the
model. Increasing the signal to noise of an image (by collecting more photons
such that the fractional pixel uncertainty due to Poisson fluctuations is
reduced) has the effect of steepening the likelihood function and so reducing
the volume of parameter space associated with high goodness of fit. Model
features that fitted low signal to noise images badly will give even worse
fits when the signal to noise improves. Increasing the resolution of an image
has a similar effect: where before, a range of model features  differing only
in unresolved detail all gave comparably high likelihoods, with higher
resolution only a fraction of those models will still be good fits, and so the
marginal likelihood will decrease and $I$ will increase. Likewise, adding more
spectral coverage, by including images in more filters, means that a narrower
range of model features will fit the data -- the modeller has to get the
colours right as well as the shapes.

How can we increase the informativeness of a displayed image? Astronomical
image signal to noise, or depth, is limited by the telescope collecting area,
throughput and the exposure time used; angular resolution is similarly limited
by either the telescope aperture size, or the stability of the atmosphere, or
in some cases the motion of the telescope, and so on. The choice of filters
used determines the spectral coverage of the image set. In principle, an image
viewing robot could take all these factors into account when computing its
predicted images, making a high fidelity model image of the sky, and then
degrading it in order to match the observed image and  compute its likelihood.
In practice, however, such calculations may be too computationally expensive
to perform in the available time. Instead, intermediate images with enhanced
resolution and apparent signal to noise can be produced by inferring model
images assuming uninformative priors. Such inferences incorporate two pieces
of information that the robot has, but may not be able to use efficiently: the
point spread function in the image, and a noise model. Such intermediate image
reconstruction is the subject of much of the image restoration literature
referred to above. The output model images are ``deconvolved,'' and have
higher angular resolution, and smaller (although perhaps correlated) pixel
uncertainties. However, they may also contain new artifacts. Whether the
introduction of these artifacts negates the benefits of the deconvolution
analysis will depend on the discoverable  features in the image.

Do we human viewers carry out an approximation to this restoration procedure?
Information about the point spread function of the image is available via the
stars present -- but do we use this information and perform some correction --
deconvolution -- when viewing the image? To what extent are we able to correct
for a particular set of filters having been used, so that we can compare with
our model images in some other set? Are we able to reliably tell the
difference between noise and real, but faint, features in an image? Answering
these questions would require a controlled study in feature detection in
images by human viewers, something that is now possible on a large scale via
the online citizen science platform, the Zooniverse. Images of the same
simulated scene both before and after recontruction could be shown to the
assembled crowd, and their detection rate of various features analyzed. It is
important to do this test with simulated data, to be able to measure absolute
detection sensitivity rather than that relative to a set of ``expert''
viewers.

Deconvolution to obtain a lower noise, higher resolution model image is a
procedure is most straightforwardly carried out on a single filter image. The
point spread function varies, in general, with time and wavelength: every
image's PSF is different. This means that any artifacts introduced by the
deconvolution process are also likely to be different between images of
different filters, leading to complex patterns in a color composite image.
However, we note that combining raw images in three filters into a composite
{\it without} deconvolving them to the same resolution will also cause
artifacts: colored features that are not real, but simply a product
of the three filters' images having different PSFs and depths. Combining images into
color composites would seem to produce artifacts generically: determining
which method of color image production cause the most confusing set of
artifacts will likely depend on the discoverable features present and the
image quality, and is a matter for experiment.

{\it Our ability to improve and extend our model for features in astronomical
images depends on the quality of images available. Beyond the limits imposed
by the image acquisition hardware, we can improve the resolution and signal to
noise of images by inferring intermediate model images, at the price of
introducing new, colored artifacts. However, since colored artifacts are
introduced by forming color composites from input images with different
resolutions and depths anyway, the benefits of deconvolution must be
determined in context by the viewing of test, simulated images.}

% ----------------------------------------------------------------------------

\section{Practical Image Preparation and Viewing}

Suppose we are given three images, labelled red green and blue, with different
depth, and resolution. How should we best present these data to human viewers,
to produce a homogeneous set of  maximally informative images? Based on the
discussion above, we recognise the following useful procedures:

\begin{description}

\item{Resolution matching}. Infer three model images with the same angular
resolution so that,  when they are combined, produce no new color artifacts.
If this can be achieved, the image will appear to have been made with a system
with constant angular resolution. All other images in the set should be
prepared with this resolution as well, to make a homogeneous viewing set.

\item{Deconvolution.} Choose the model image resolution to be as high as
possible, while preventing the introduction of new artifacts. This trade-off
must be understood and quantified.

\item{Standard stretching.} Display the composite images with a common stretch
and scale such that they appear to have been taken with a color camera of
pre-determined sensitivity.

\end{description}

% Since it is, by definition, dependent on the feature prior, information
% content is clearly subject specific.  The informativeness of an image will
% also be subjective because we each have our own sensor system through which to
% pass the composite image. The color balance, contrast, brightness and dynamic
% range of the image may need to be adjustable by the viewer to account for
% this. However, control of these image properties may be able to be simplified
% to a small number of parameters. These parameters would then need to be used
% consistently on all images in a set for homogeneity to be preserved: it may be
% simplest to fix the presentation for all viewers and live with the resulting
% scatter in discovery sensitivity.

% ----------------------------------------------------------------------------

\section{Testing Discovery Sensitivity in Human Viewers}

Methods, experimental set up.

Test images. Some specific features to discover.

% Suugestion: A practical experiment would be to view a training set of N
% images, and then measure how surprised you are by M test images. To test
% information content, a different M images could be prepared, and the mean
% surprise level measured and compared to what it was before.  Surprise could be
% measured by questionnaire - did you see the faint blue streak?

% ----------------------------------------------------------------------------

\end{document}


% !TeX spellcheck = en_GB
\section{Modeling}
\subsection{Introduction}
The system is modeled as \textbf{N Transmitter-Receiver couples} which communicate through \textbf{C Channels}. A \textit{collision} can occur on a \textbf{Channel} if more than one \textbf{Transmitter} want to transmit a packet in that \textbf{Channel}. The \textbf{Transmitter} stores in a queue the packets that it wants to transmit and, then, it sends them; the \textbf{Channel} "knows" if a collision occurs and handles it; the \textbf{Receiver} only receive packets.
\subsection{General Assumptions}
The following general assumptions have been made:
\begin{itemize}
	\item \textbf{Slotted}: packets are attempted to be \textbf{Transmitter} by the Transmitter only at the \textbf{beginning of the time-slot}
	\item \textbf{Constant Packet Size and Transmission Rate}: each packet has a \textbf{constant packet size} and each \textbf{Transmitter} has a constant and equal transmission rate for which to transmit a packet (without collision) from the \textbf{Receiver} to the transmitter will last \textbf{one time-slot}.
	\item \textbf{No Propagation Error in the channel}: the only cause of a failed transmission has to be considered the \textit{packet} collision. Other causes, (i.e. path-loss, shadowing, small-scale fading), are neglected.
	\item \textbf{FIFO Queues of unlimited capacity at the Transmitter}
	\item \textbf{Transmitters and Receivers always synchronized with the time-slot period}: the \textbf{Receiver} knows in which \textbf{Channel} the \textbf{Transmitter} will try to send his packet in each time-slot and the \textbf{Receiver} will be ready to listen in the correct \textbf{Channel}.
	\item \textbf{After an eventual collision the transmitter will change his channel choice}: this is the default configuration, on Chapter 5 this configuration will be compared with the Non-Change Channel one.
\end{itemize}

\subsection{Preliminar Validation}
Before the implementation a preliminar validation phase is necessary to ensure that the model is correct. Let analyze if the assumptions made in the previous section are reasonable:
\begin{itemize}
	\item The \textbf{slotted assumption} (transmission only at the beginning of the timeslot) is reasonable due to the fact that exist some network protocols which work under this assumption, see for instance Slotted Aloha. % \colorbox{yellow}{that is the most famous one}.
	\item We consider the \textbf{packet size constant} because if the packet length is so large that more than one slots are needed, we can consider, from the viewpoint of the model, that this unique packet send in two different slots is like two packets of fixed-length send each one in a slot. Taken this into account is reasonable to state that the packet size is constant and that one packet is transmitted within a slot (if no collisions occur).
	\item The critical issue of every slotted network is the one related to the \textbf{collisions}: they have an huge impact on the general performance of the network, so it is reasonable to neglect the other propagation errors that are not network-specific or that depend from the environment (as path-loss).
	\item It's reasonable that the transmitter and the receiver are \textbf{synchronized with the time-slot period}, in fact other slotted-based network as slotted ALOHA requires a synchronization of this type.
	%\item \colorbox{yellow}{When a packet collide it's reasonable} to think that the transmitter will change the transmission channel in order to avoid another collision. Indeed this along with the back-off time are the techniques that should avoid another collision.
\end{itemize}
\subsection{Factors}
The following factors have been defined which may affect the performance of the system:
\begin{itemize}
	\item \textbf{N}: \textbf{Transmitter-Receiver} Couples.
	\item \textbf{C}: numbers of \textbf{Channels}.
	\item \textbf{p}: \textbf{probability of success} for sending a packet in the current time-slot for a Transmitter.
	\item \textbf{$\dfrac{1}{\lambda}$}: exponential distribution \textbf{mean inter-arrival time of packets} at the Transmitter.
	\item $T_{slot}$: \textbf{time-slot duration}.

\end{itemize}


\chapter{Enumerations}
\label{enumerationschapter}

This chapter describes the \defrsixlibrary{enums} library for dealing with enumerated values
\mainindex{enumeration}and sets of enumerated values.  Enumerated
values are represented by ordinary symbols, while finite sets of
enumerated values form a separate type, known as the
\defining{enumeration sets}.
The enumeration sets are further partitioned into sets that
share the same \defining{universe} and \defining{enumeration type}.
These universes and enumeration types are created by the
{\cf make-enumeration} procedure.  Each call to that procedure
creates a new enumeration type.

This library interprets each enumeration set with respect to
its specific universe of symbols and enumeration type.
This facilitates efficient implementation of enumeration sets
and enables the complement operation.

In the descriptions of the following procedures, \var{enum-set}
ranges over the enumeration sets, which are defined as the subsets
of the universes that can be defined using {\cf make-enumeration}.

\begin{entry}{%
\proto{make-enumeration}{ symbol-list}{procedure}}

\domain{\var{Symbol-list} must be a list of symbols.}
The {\cf make-\hp{}enumeration} procedure
creates a new enumeration type whose universe consists of
those symbols (in canonical order of their first appearance
in the list) and returns that universe as an enumeration
set whose universe is itself and whose enumeration type is
the newly created enumeration type.
\end{entry}

\begin{entry}{%
\proto{enum-set-universe}{ enum-set}{procedure}}

Returns the set of all symbols that comprise
the universe of its argument, as an enumeration set.
\end{entry}

\begin{entry}{%
\proto{enum-set-indexer}{ enum-set}{procedure}}

Returns a unary procedure that, given a symbol
that is in the universe of \var{enum-set}, returns its 0-origin index
within the canonical ordering of the symbols in the universe; given a
symbol not in the universe, the unary procedure returns \schfalse.

\begin{scheme}
(let* ((e (make-enumeration '(red green blue)))
       (i (enum-set-indexer e)))
  (list (i 'red) (i 'green) (i 'blue) (i 'yellow))) \lev (0 1 2 \schfalse)%
\end{scheme}

The {\cf enum-set-indexer} procedure could be defined as follows using the
{\cf memq} procedure from the \rsixlibrary{lists} library:

\begin{scheme}
(define (enum-set-indexer set)
  (let* ((symbols (enum-set->list
                    (enum-set-universe set)))
         (cardinality (length symbols)))
    (lambda (x)
      (cond
       ((memq x symbols)
        => (lambda (probe)
             (- cardinality (length probe))))
       (else \schfalse)))))%
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{enum-set-constructor}{ enum-set}{procedure}}

Returns a unary procedure that, given a
list of symbols that belong to the universe of \var{enum-set}, returns
a subset of that universe that contains exactly the symbols in the
list.  The values in the list must all belong to the universe.
\end{entry}

\begin{entry}{%
\proto{enum-set->list}{ enum-set}{procedure}}

Returns a list of the symbols that belong to its
argument, in the canonical order of the universe of \var{enum-set}.

\begin{scheme}
(let* ((e (make-enumeration '(red green blue)))
       (c (enum-set-constructor e)))
  (enum-set->list (c '(blue red)))) \lev (red blue)
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{enum-set-member?}{ symbol enum-set}{procedure}
\proto{enum-set-subset?}{ \vari{enum-set} \varii{enum-set}}{procedure}
\proto{enum-set=?}{ \vari{enum-set} \varii{enum-set}}{procedure}}

The {\cf enum-set-member?} procedure returns \schtrue{} if its first argument is an
element of its second argument, \schfalse{} otherwise.

The {\cf enum-set-subset?} procedure returns \schtrue{} if the universe of
\vari{enum-set} is a subset of the universe of \varii{enum-set}
(considered as sets of symbols) and every element of \vari{enum-set}
is a member of \varii{enum-set}.  It returns \schfalse{} otherwise.

The {\cf enum-set=?} procedure returns \schtrue{} if \vari{enum-set}  is a
subset of \varii{enum-set} and vice versa, as determined by the
{\cf enum-set-subset?} procedure.  This implies that the universes of
the two sets are equal as sets of symbols, but does not imply
that they are equal as enumeration types.  Otherwise, \schfalse{} is
returned.

\begin{schemenoindent}
(let* ((e (make-enumeration '(red green blue)))
       (c (enum-set-constructor e)))
  (list
   (enum-set-member? 'blue (c '(red blue)))
   (enum-set-member? 'green (c '(red blue)))
   (enum-set-subset? (c '(red blue)) e)
   (enum-set-subset? (c '(red blue)) (c '(blue red)))
   (enum-set-subset? (c '(red blue)) (c '(red)))
   (enum-set=? (c '(red blue)) (c '(blue red)))))
\ev (\schtrue{} \schfalse{} \schtrue{} \schtrue{} \schfalse{} \schtrue{})%
\end{schemenoindent}
\end{entry}

\begin{entry}{%
\proto{enum-set-union}{ \vari{enum-set} \varii{enum-set}}{procedure}
\proto{enum-set-intersection}{ \vari{enum-set} \varii{enum-set}}{procedure}
\proto{enum-set-difference}{ \vari{enum-set} \varii{enum-set}}{procedure}}

\domain{\vari{Enum-set} and \varii{enum-set} must be enumeration sets
  that have the same enumeration type.}

The {\cf enum-set-union} procedure returns the union of \vari{enum-set} and \varii{enum-set}.
The {\cf enum-set-intersection} procedure returns the intersection of \vari{enum-set} and \varii{enum-set}.
The {\cf enum-set-difference} procedure returns the difference of \vari{enum-set}
and \varii{enum-set}.

\begin{scheme}
(let* ((e (make-enumeration '(red green blue)))
       (c (enum-set-constructor e)))
  (list (enum-set->list
         (enum-set-union (c '(blue)) (c '(red))))
        (enum-set->list
         (enum-set-intersection (c '(red green))
                                (c '(red blue))))
        (enum-set->list
         (enum-set-difference (c '(red green))
                              (c '(red blue))))))
\lev ((red blue) (red) (green))
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{enum-set-complement}{ enum-set}{procedure}}

Returns \var{enum-set}'s
complement with respect to its universe.

\begin{scheme}
(let* ((e (make-enumeration '(red green blue)))
       (c (enum-set-constructor e)))
  (enum-set->list
    (enum-set-complement (c '(red)))))
\ev (green blue)
\end{scheme}
\end{entry}

\begin{entry}{%
\proto{enum-set-projection}{ \vari{enum-set} \varii{enum-set}}{procedure}}

Projects \vari{enum-set} into the universe
of \varii{enum-set}, dropping any elements of \vari{enum-set} that do
not belong to the universe of \varii{enum-set}.  (If \vari{enum-set}
is a subset of the universe of its second, no elements are
dropped, and the injection is returned.)  The result has the enumeration type of
\varii{enum-set}.

\begin{scheme}
(let ((e1 (make-enumeration
            '(red green blue black)))
      (e2 (make-enumeration
            '(red black white))))
  (enum-set->list
    (enum-set-projection e1 e2)))
\ev (red black)
\end{scheme}
\end{entry}

\begin{entry}{}
\pproto{(define-enumeration \hyper{type-name}}{\exprtype}
\mainschindex{define-enumeration}{\tt\obeyspaces%
  (\hyper{symbol} \dotsfoo)\\
  \hyper{constructor-syntax})}

The {\cf define-enumeration} form defines an enumeration type and
provides two macros for constructing its members and sets of its
members.

A {\cf define-enumeration} form is a definition and can appear
anywhere any other \hyper{definition} can appear.

\hyper{Type-name} is an identifier that is bound as a syntactic keyword;
\hyper{symbol}~\dotsfoo{} are the symbols that comprise the
universe of the enumeration (in order).

{\cf (\hyper{type-name} \hyper{symbol})} checks at macro-expansion
time whether the name of \hyper{symbol} is in the universe associated with
\hyper{type-name}.  If it is, {\cf (\hyper{type-name}
  \hyper{symbol})} is equivalent to {\cf \hyper{symbol}}.
It is a syntax violation if it is not.

\hyper{Constructor-syntax} is an identifier that is bound to a
macro that, given any finite sequence of the symbols in the universe,
possibly with duplicates, expands into an expression that evaluates
to the enumeration set of those symbols.

{\cf (\hyper{constructor-syntax} \hyper{symbol}~\dotsfoo{})} checks at
macro-expansion time whether every \hyper{symbol}~\dotsfoo{} is in the
universe associated with \hyper{type-name}.  It is a syntax violation
if one or more is not.
Otherwise
\begin{scheme}
(\hyper{constructor-syntax} \hyper{symbol}~\dotsfoo{})
\end{scheme}
%
is equivalent to
%
\begin{scheme}
((enum-set-constructor (\hyper{constructor-syntax}))
 '(\hyper{symbol}~\dotsfoo{}))\rm.
\end{scheme}

Example:

\begin{scheme}
(define-enumeration color
  (black white purple maroon)
  color-set)

(color black)                      \ev black
(color purpel)                     \ev \exception{\&syntax}
(enum-set->list (color-set))       \ev ()
(enum-set->list
  (color-set maroon white))        \ev (white maroon)
\end{scheme}

\begin{note}
  In {\cf (\hyper{type-name} \hyper{symbol})} and {\cf
    (\hyper{constructor-syntax} \hyper{symbol}~\dotsfoo{})} forms,
  only the names of the \hyper{symbol}s are significant.
\end{note}
\end{entry}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "r6rs-lib"
%%% End:


\documentclass[12]{scrartcl}
\usepackage{amssymb,amsmath,gensymb,dsfont,calc,multicol,fullpage}
\makeatletter
\newcommand\Aboxed[1]{
   \@Aboxed#1\ENDDNE}
\def\@Aboxed#1&#2\ENDDNE{%
   &
   \settowidth\@tempdima{$\displaystyle#1{}$}
   \setlength\@tempdima{\@tempdima+\fboxsep+\fboxrule}
   \kern-\@tempdima
   \boxed{#1#2}
}
\makeatother

\begin{document}

\title{Homework 1, Section 2.3: 3, 5, 8(f), 10 }
\author{Alex Gordon}
\date{\today}
\maketitle
\section*{Homework}
\subsection*{3. A)}
First we must start with an outline of the proof we are about to perform. The table below includes the verification of the n = 1 through 4. It also shows the last row checked (n = m - 1) and the next row to be checked (n = m). \\ \\
\begin{tabular}{ | c | c | r | r |  }
  \hline
 $n$ & $a_n$ (recursive formula) & closed formula & equal?  \\
  \hline
  1 & 1 & $4 \times 1 - 3 = 1 $ & yes \\     \hline
  2 & $ 1 + 4 = 5$ & $4 \times 2 - 3 = 5$ & yes \\     \hline
  3 & $ 5 + 4 = 9$ & $4 \times 3 - 3 = 9$ & yes \\     \hline
  4 & $ 9 + 4 = 13$ & $4 \times 4 - 3 = 13$ & yes \\      \hline
  ... & ... & ... & ... \\   \hline
 $m - 1$ & $a_{m-2} + 4 = 4m - 7$ & $4(m - 1) - 3 = 4m - 7$ & yes \\   \hline \hline
  $m$ & $a_{m-1} + 4$& $4m - 3$ & ??? \\
  \hline
\end{tabular}
\\
\\All let is left is to simplify the recursive formula for the closed formula. We can do this by some simple substitution.
\begin{align*}
a_m &= a_{m-1} + 4
\\ a_{m-1} &= (4m - 7)
\\ a_m &= (4m - 7) + 4
\\ &= 4m - 3
\end{align*}
This now shows that the recursive formula is equal to the closed formula.
\subsection*{3. B)}
First we must start with an outline of the proof we are about to perform. The table below includes the verification of the n = 1 through 4. It also shows the last row checked (n = m - 1) and the next row to be checked (n = m). \\ \\
\begin{tabular}{ | c | c | r | r |  }
  \hline
 $n$ & $a_n$ (recursive formula) & closed formula & equal?  \\
  \hline
  1 & 5 & $\frac{1(1+9)}{2} = 5 $ & yes \\     \hline
  2 & $5 + 2 + 4 = 11$ & $\frac{2(2+9)}{2} = 11$ & yes \\     \hline
  3 & $11 + 3 + 4 = 18$ & $\frac{3(3+9)}{2} = 18$ & yes \\     \hline
  4 & $18 + 4 + 4 = 26$ & $\frac{4(4+9)}{2} = 26$ & yes \\      \hline
  ... & ... & ... & ... \\   \hline
 $m - 1$ & $ a_{m-2} + a_{m-1} + 4 = \frac{(m-1)((m)+9)}{2}$ & $ \frac{(m-1)(m-1) + 9}{2}  = \frac{m(m+9)}{2} $ & yes \\   \hline \hline
  $m$ & $a_{m-1} + a_{m} + 4$& $\frac{4(m)}{2}$ & ??? \\
  \hline
\end{tabular}
\\
\\All let is left is to simplify the recursive formula for the closed formula. We can do this by some simple substitution.
\begin{align*}
a_m &= \frac{(m-1)((m)+9)}{2}
\\ a_{m-1} &= \frac{m(m+9)}{2}
\\ &= \frac{m^2 + 9m}{2}
\end{align*}
This now shows that the recursive formula is equal to the closed formula.
\subsection*{3. C)}
First we must start with an outline of the proof we are about to perform. The table below includes the verification of the n = 1 through 4. It also shows the last row checked (n = m - 1) and the next row to be checked (n = m). \\ \\
\begin{tabular}{ | c | c | r | r |  }
  \hline
 $n$ & $a_n$ (recursive formula) & closed formula & equal?  \\
  \hline
  1 & 1 & $\frac{1(2)(3)}{6} = 1 $ & yes \\     \hline
  2 & $1 + 2^2 = 5$ & $\frac{2(3)(5)}{6} = 5$ & yes \\     \hline
  3 & $5 + 3^2 = 14$ & $\frac{3(4)(7)}{2} = 14$ & yes \\     \hline
  4 & $14 + 4^2 = 30$ & $\frac{4(5)(9)}{2} = 30$ & yes \\      \hline
  ... & ... & ... & ... \\   \hline
 $m - 1$ & $ a_{m-2} + (m-1)^2 = \frac{(m-1)(m)(2m-1)}{6}$ & $\frac{(m-1)(m)(2m-1)}{6}$ & yes \\   \hline \hline
  $m$ & $a_{m-1} + m^2$& $\frac{(m)(m+1)(2m+1)}{6}$ & ??? \\
  \hline
\end{tabular}
\\
\\All let is left is to simplify the recursive formula for the closed formula. We can do this by some simple substitution.
\begin{align*}
a_m &= a_{m-1}  + m^2
\\  &= \frac{(m-1)(m)(2m-1)}{6} + m^2
\\  &= \frac{(m-1)(m)(2m-1) +6m^2}{6}
\\ &= \frac{m[(m-1)(2m-1) +6m]}{6}
\\ &= \frac{m(m+1)(2m+1)}{6}
\end{align*}
This now shows that the recursive formula is equal to the closed formula.
\subsection*{3. D)}
First we must start with an outline of the proof we are about to perform. The table below includes the verification of the n = 1 through 4. It also shows the last row checked (n = m - 1) and the next row to be checked (n = m). \\ \\
\begin{tabular}{ | c | c | r | r |  }
  \hline
 $n$ & $a_n$ (recursive formula) & closed formula & equal?  \\
  \hline
  1 & 1 & $2^1 = 1 $ & yes \\     \hline
  2 & $1 + 2^2 = 5$ & $2^2 - 1= 3$ & yes \\     \hline
  3 & $5 + 3^2 = 14$ & $2^3 - 1= 7$ & yes \\     \hline
  4 & $14 + 4^2 = 30$ & $2^4 - 1= 15$ & yes \\      \hline
  ... & ... & ... & ... \\   \hline
 $m - 1$ & $ 2 (a_{m-2}) + 1 = 2^{m-1} - 1$ & $2^{m-1} - 1$ & yes \\   \hline \hline
  $m$ & $2 (a_{m-1}) + 1$& $2^m-1$ & ??? \\
  \hline
\end{tabular}
\subsection*{3. E)}

\subsection*{3. F)}

\subsection*{5. A)}

\subsection*{10. A)}

$\sum\limits_{i=1}^n = \frac{i(i+1)}{2}= \frac{n(n+1)(n+2)}{6}$

\end{document}


\section{Output Size}
\label{sec:value-size}

Figure \ref{fig:test} gives the formula for calculating the size of a UTxO entry
in the Alonzo era. In addition to the data found in the UTxO in the ShelleyMA
era, the hash of a datum (or $\Nothing$) is added to the output type, which
is accounted for in the size calculation.

\begin{figure*}[h]
  \emph{Constants}
  \begin{align*}
  & \mathsf{JustDataHashSize} \in \MemoryEstimate \\
  & \text{The size of a datum hash wrapped in $\DataHash^?$} \\~
  \\
  & \mathsf{NothingSize} \in \MemoryEstimate \\
  & \text{The size of $\Nothing$ wrapped in $\DataHash^?$}
  \end{align*}
  %
  \emph{Helper Functions}
  \begin{align*}
    & \fun{dataHashSize} \in \DataHash^? \to \MemoryEstimate \\
    & \fun{dataHashSize}~ \Nothing = \mathsf{NothingSize} \\
    & \fun{dataHashSize}~ \wcard = \mathsf{JustDataHashSize} \\
    & \text{Return the size of $\DataHash^?$} \\~\\
    & \fun{utxoEntrySize} \in \TxOut \to \MemoryEstimate \\
    & \fun{utxoEntrySize}~\var{(a, v, d)} = \mathsf{utxoEntrySizeWithoutVal} + (\fun{size} (\fun{getValue}~(a, v, d))) + \mathsf{dataHashSize}~d \\
    & \text{Calculate the size of a UTxO entry}
  \end{align*}
  \caption{Value Size}
  \label{fig:test}
\end{figure*}

\begin{note}
  Get dataHashSize from heapwords on the code
\end{note}

There is a change of constant value from the ShelleyMA era, specifically:

\begin{itemize}
  \item $\mathsf{adaOnlyUTxOSize} = 29$ instead of $27$
  \item $\mathsf{k_0} = 2$ instead of $0$
\end{itemize}

Additionally, the new constants used in Alonzo have values :

\begin{itemize}
  \item $\mathsf{JustDataHashSize} = 10$ words
  \item $\mathsf{NothingSize} = 0$ words
\end{itemize}


\chapter{Soundness and completeness}
\label{sec:soundness_and_completeness}

We have two ways of checking or verifying that an argument is valid: (1) using truth tables and (2) using the natural deduction system to provide a proof. Consequently, we also have two ways of characterizing the concept of \textit{validity}. (See table \ref{table:truth_tables_or_derivations}.)
You might think that we can take it for granted that, with respect to determining if an argument is valid, both methods will always give us the same result, but that is not exactly the case. (We, right now, can take it for granted, but that's only because the requisite work to show that the two methods will always agree has already been done.) If you think about it for a moment, you'll notice that the two methods don't have anything in common, and so, it is not intuitively obvious that they will always produce the same result. But they do.

%and not just for \textit{valid}, but for \textit{contradiction}, \textit{contingent}, \textit{equivalent}, \textit{inconsistent}, and \textit{consistent}---and any sentence that is a tautology will also be a theorem. (A summary of how we establish each of these concepts using truth tables and the natural deduction system is given in \ref{table:truth_tables_or_derivations}.)

\begin{sidewaystable}\centering\sffamily\footnotesize
\ra{1.4}
\begin{tabularx}{1.0\linewidth}{@{}>{\columncolor{white}[0pt][\tabcolsep]} l  X >{\columncolor{white}[\tabcolsep][0pt]} X @{}}
				&	\textth{Truth table (semantic) definition} 	&	\textth{Proof-theoretic (syntactic) definition} \\

\rowcolor{blue!5}Tautology   &	A sentence whose truth table has a \textit{T} on every line under the main connective & A sentence that can be derived without any premises. I.e., a theorem.\\

Contradiction		&	A sentence whose truth table has an \textit{F} on every line under the main connective  &	A sentence whose negation can be derived without any premises\\

\rowcolor{blue!5}Contingent sentence	&	A sentence whose truth table has both \textit{T} and \textit{F} (in any combination) under the main connective & A sentence that is not a theorem or contradiction \\

Equivalent sentences &	The columns under the main connective for both sentences are identical.& The sentences can be derived from each other	\\

\rowcolor{blue!5}Inconsistent sentences	&	Sentences that do not have a single line in their truth tables where, in the column under the main connective, they all have a \textit{T}.	& Sentences  from which one can derive a contradiction \\

Consistent sentences	&	Sentences that have at least one line in their truth tables where, in the column under  the main connective, they all have a  \textit{T}. & Sentences that are not inconsistent	\\

\rowcolor{blue!5}Valid argument		&	An argument whose truth table has no lines where there is a  \textit{T} under each main connective for the premises and an  \textit{F} under the main connective for the conclusion.  & An argument where one can derive the conclusion from the premises	\\
\end{tabularx}
\caption{The two ways of defining each of these logical concepts in TFL.}\label{table:truth_tables_or_derivations}
\end{sidewaystable}

How do we know that the truth table method and the natural deduction method will always agree? Demonstrating that they will goes beyond the scope of this book. But we will review the two properties that a logic system (like TFL) must have for the two methods to always be in agreement.  To begin, let us define two new terms.
\begin{earg}
\item[] \textit{p-valid}: being valid because a proof can be given using the rules in our natural deduction system. (\textit{p-valid} is short for \textit{proof-valid}. This is also sometimes called \textit{syntactically valid}).
\item[] \textit{tt-valid}: being valid because there is no line in a truth table where the premises are true and the conclusion is false. (This is also sometimes called \textit{semantically valid}).
\end{earg}
First, it must be the case that every argument that is p-valid is tt-valid. This property is called \define{soundness}.

\begin{factboxy}{Soundness}\label{def:Soundness}
\define{Soundness} is a property of a logic system iff, for any argument, if the argument is p-valid, then the argument tt-valid.
\tcblower
Equivalently, \define{soundness} is a property of a logic system iff, for any sentence, if a sentence is a theorem, then it is a tautology.
\end{factboxy}

\noindent Soundness is a property of TFL because every argument for which we can give a proof (and hence show that it is valid that way) will also be valid by the truth table method.

\begin{notebox}
\textit{Soundness}, the property of logical systems that we are discussing here, is different than the \textit{sound}, the property of individual arguments, that is defined on p.~\pageref{def-sound-arg}.
\end{notebox}

Soundness is the property that goes in this direction: p-valid $\Rightarrow$ tt-valid. The other direction, tt-valid $\Rightarrow$ p-valid, is called \define{completeness}.

\begin{notebox}
Like `$\eif$', `$\Rightarrow$' can be read as `if \ldots, then \ldots'. Since `p-valid $\Rightarrow$ tt-valid' is not an expression in TFL, we shouldn't use the `$\eif$' symbol in it. Instead, we are using the \textit{metalogical arrow} to express the relationship between \textit{p-valid} and \textit{tt-valid}.
\end{notebox}

\begin{factboxy}{Completeness}\label{def:completeness}
\define{Completeness} is a property of a logic system iff, for any argument, if the argument is tt-valid, the the argument is p-valid.
\tcblower
Equivalently, \define{completeness} is a property of a logic system iff, for any sentence, if the sentence is a tautology, then it is a theorem.
\end{factboxy}

Proving that a logic system is complete is generally harder than proving soundness. Proving soundness for a logic system amounts to showing that all of the rules of the deduction system work the way they are supposed to work. Showing that a logic system is complete means showing that all of the rules that are needed have been included, and none have been left out.
Again, showing this is beyond the scope of this book. The important point is that, happily, TFL is both sound and complete. This is not the case for all formal languages (or all logical systems). Because it is true of TFL, we can choose to give proofs or give truth tables---whichever is easier for the task at hand.

Some people are naturally drawn to truth tables because they can be produced mechanically, and that seems easier. But, as we mentioned in chapter \ref{s:NDVeryIdea}, when arguments contain more than three letters, their truth table become quite large. Also, providing a proof informs us of the steps that must be taken to get from the premises to the conclusion. It illustrates \textit{why} an argument is valid in a way that a truth table cannot. Comparing proofs also gives us insight into how arguments are similar or different, and that, in turn, informs us about the similarities and differences between various reasoning strategies. Truth tables, meanwhile, tell us nothing but whether an argument is valid or invalid.

It also bears mentioning that TFL is the standard first step into formal logic, but more complex systems of logic cannot employ truth tables and so derivations must be used. It is wise, therefore, to master derivations in TFL before moving onto to other branches of logic.

At the same time, there are some logical properties, the presence (or really the absence) of which, can only only be established with truth tables. In each of these cases, we might surmise from our failure to find a proof that the property is present, but our failure might just be a consequence of not trying hard enough. This is true for showing that (1) an argument is invalid, (2) a sentence is \textit{not} a theorem, (3) a sentence is \textit{not} a contradiction, (4) a sentence is contingent (which is to say that it's \textit{not} a theorem and \textit{not} a contradiction), (4) two sentences are \textit{not} equivalent, and (5) two or more sentences are consistent (which is to say that they are \textit{not} inconsistent). If we wish to show that any of those properties apply, then we have to resort to truth tables.

\begin{table*}\centering\sffamily\footnotesize
\ra{1.4}
\begin{tabular}{@{}l l l@{}}\toprule
\textth{To verify}			&	\textth{that it is} &	\textth{that it is not} \\\midrule
Tautology 		& proof or a truth table 							& truth table \\
Contradiction 	&  proof or a truth table  		 				& truth table\\
Contingent		& truth table 										& proof or a truth table\\
Equivalent 		& proof or a truth table 		 					& truth table\\
Consistent 		& truth table 										& proof or a truth table\\
Valid 				& proof or a truth table 							& truth table \\
\bottomrule
\end{tabular}
\caption{This table summarizes what is required to check each of these logical properties.}\label{table.proof-or-model}
\end{table*}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% exercises

\section{Practice exercises}
\setcounter{ProbPart}{0}

\problempart  For each of the following, if the argument is valid, give a proof. If it is not valid, make a truth table showing that it is not.
\begin{earg}
\item $\enot (P \eor Q) \proves P \eand Q$

\item $\enot(A \eor B) \proves C \eif \enot A$

\item $\enot(A \eor B) \proves B \eif A$

\item $\enot(A \eor (B \eor C)) \proves \enot{C}$
\item $\enot(A \eand (B \eor C)) \proves \enot{C}$

\item $((\enot X \eiff X) \eor X) \proves X$
\item $F \eand (K \eand R) \proves F \eiff (K \eiff R)$

\item $\enot L,  K \eif \enot L \proves \enot K$
\item $L, K \eif \enot L \proves \enot K$

\item $\proves A \eif [((B \eand C) \eor D) \eif A]$
\item $\proves A \eif (A \eif B)$
\end{earg}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Answers}
\setcounter{ProbPart}{0}

\problempart
\begin{earg}
\item $\enot (P \eor Q) \proves P \eand Q$ is invalid.

\begin{center}
\begin{tabular}{d d | f e e e   j 	 j e e}
P& Q&  $\enot$& (P& $\eor$& Q)& \proves& (P& $\eand$& Q)\\
\hline
T& T&   \TTbf{F}&   T& T& T&\cm&      T& \TTbf{T}& T\Tstrut\\
T& F&   \TTbf{F}&   T& T& F&\cm&      T& \TTbf{F}& F\\
F& T&   \TTbf{F}&   F& T& T&\cm&      F& \TTbf{F}& T\\
F& F& 	 \TTbf{T}&   F& F& F&\xm&      F& \TTbf{F}& F\\
\end{tabular}
\end{center}

\item $\enot(A \eor B) \proves C \eif \enot A$ is valid.

\item $\enot(A \eor B) \proves B \eif A$ is valid.

\item $\enot(A \eor (B \eor C)) \proves \enot{C}$ is valid.
\item $\enot(A \eand (B \eor C)) \proves \enot{C}$ is invalid.

\begin{center}
\begin{tabular}{d d d | f e e e e f   j 	 j e}
A& B& C&  \enot& (A& \eand& (B& $\eor$& C))& $\proves$&  $\enot$& C\\
\hline
T& T& T&  \TTbf{F}&   T& T&   T& T& T&\cm &   \TTbf{F}& T\Tstrut\\
T& T& F&  \TTbf{F}&   T& T&   T& T& F&\cm &     \TTbf{T}& F\\
T& F& T&  \TTbf{F}&   T& T&   F& T& T&\cm &     \TTbf{F}& T\\
T& F& F&  \TTbf{T}&   T& F&   F& F& F& \cm &    \TTbf{T}& F\\\arrayrulecolor{light-gray}\hline
F& T& T&  \TTbf{T}&   F& F&   T& T& T&\xm &     \TTbf{F}& T\Tstrut\\
F& T& F&  \TTbf{T}&   F& F&   T& T& F& \cm &    \TTbf{T}& F\\
F& F& T&  \TTbf{T}&   F& F&   F& T& T& \xm &    \TTbf{F}& T\\
F& F& F&  \TTbf{T}&   F& F&   F& F& F& \cm  &   \TTbf{T}& F\\
\end{tabular}
\end{center}

\item $(\enot X \eiff X) \eor X \proves X$ is valid.
\item $F \eand (K \eand R) \proves F \eiff (K \eiff R)$ is valid.

\item $\enot L,  K \eif \enot L \proves \enot K$ is invalid.

\begin{center}
\begin{tabular}{d d | f e    j e e e   j 	 j e}
$K$& $L$&  $\enot$& $L$,& ($K$& $\eif$& $\enot$& $L$)& \proves&  $\enot$& $K$\\
\hline
T& T&  \TTbf{F}& T&    T& \TTbf{F}& F& T&\cm&    \TTbf{F}& T\Tstrut\\
T& F&  \TTbf{T}& F&    T& \TTbf{T}& T& F&\xm&    \TTbf{F}& T\\
F& T&  \TTbf{F}& T&    F& \TTbf{T}& F& T&\cm&    \TTbf{T}& F\\
F& F&  \TTbf{T}& F&    F& \TTbf{T}& T& F&\cm&    \TTbf{T}& F\\
\end{tabular}
\end{center}

\item $L, K \eif \enot L \proves \enot K$ is valid.

\item $\proves A \eif [((B \eand C) \eor D) \eif A]$ is theorem.
\item $\proves A \eif (A \eif B)$ is not a theorem---and so not a tautology.

\begin{center}
\begin{tabular}{d d | f  e e e e e}
$A$& $B$&  \proves& $A$& $\eif$& ($A$& $\eif$& $B$)\\
\hline
T& T&\cm&    T& \TTbf{T}&   T& T& T\Tstrut\\
T& F&\xm&    T& \TTbf{F}&   T& F& F\\
F& T&\cm&    F& \TTbf{T}&   F& T& T\\
F& F&\cm&    F& \TTbf{T}&   F& T& F\\
\end{tabular}
\end{center}

\end{earg}


\documentclass[11pt,letterpaper]{article}
\usepackage{microtype}
\usepackage{authblk}
% \usepackage[utf8]{inputenc}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\let\amssquare\square
\usepackage{mathtools}
% \usepackage{stmaryrd}
% \usepackage{tensor}
\usepackage[mathscr]{eucal}
\usepackage{url}
% \usepackage{marvosym}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
%\usepackage{geometry}
\usepackage{minted}
\usepackage{cite}
\usepackage{multicol}
\usepackage{tocloft}
\usepackage{enumerate}

\usepackage{tikz-cd}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\usetikzlibrary{cd}
\usetikzlibrary{arrows}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.markings}
\tikzset{ed/.style={auto,inner sep=2pt,font=\scriptsize}} %edges
\tikzset{>=stealth'}
\tikzset{vert/.style={draw,circle, minimum size=6mm, inner sep=0pt, fill=white}}
\tikzset{vertbig/.style={draw,circle, minimum size=8mm, inner sep=0pt, fill=white}}
\tikzset{->-/.style={decoration={
      markings,
      mark=at position #1 with {\arrow{>}}},postaction={decorate}}}

\usepackage[hidelinks]{hyperref} % Should be imported LAST

\theoremstyle{plain}
\newtheorem{theorem}{Theorem}[subsection]
% \newtheorem{axiom}[theorem]{Axiom}
\newtheorem*{theoremstar}{Theorem}
% \newtheorem{fact}[theorem]{Fact}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{conjecture}[theorem]{Conjecture}

\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
% \newtheorem{convention}[theorem]{Convention}
% \newtheorem{construction}[theorem]{Construction}
\newtheorem{example}[theorem]{Example}
% \newtheorem{examples}[theorem]{Examples}
% \newtheorem{notation}[theorem]{Notation}
\newtheorem{remark}[theorem]{Remark}
% \newtheorem{idea}[theorem]{Idea}
% \newtheorem{question}[theorem]{Question}

\newcommand{\C}{\mathscr{C}}
\newcommand{\homC}{\underline{\C}}
\newcommand{\D}{\mathscr{D}}
\newcommand{\E}{\mathscr{E}}
\newcommand{\M}{\mathscr{M}}
\newcommand{\N}{\mathscr{N}}
\newcommand{\T}{\mathscr{T}}
\renewcommand{\S}{\mathscr{S}}

\newcommand{\bN}{\mathbb{N}}
\newcommand{\bZ}{\mathbb{Z}}

\newcommand{\lenslib}{\texttt{lens}}

\newcommand{\Pastro}{\Phi}
% \newcommand{\Pastro}{\mathrm{Pastro}}
\newcommand{\Double}{\mathcal{D}}

% Categories
\newcommand{\Set}{\mathbf{Set}}
\newcommand{\Cat}{\mathbf{Cat}}
%\newcommand{\Hask}{\mathbf{Hask}}
\newcommand{\Prof}{\mathbf{Prof}}
\newcommand{\Core}{\mathbf{Core}}
\newcommand{\MonCat}{\mathbf{MonCat}}
\newcommand{\LaxMonCat}{\mathbf{LaxMonCat}}
\newcommand{\SymmMonCat}{\mathbf{SymmMonCat}}
\newcommand{\StrictSymmMonCat}{\mathbf{StrictSymmMonCat}}

\newcommand{\Tele}{\mathbf{Tele}}
\newcommand{\StrictTele}{\mathbf{StrictTele}}
\newcommand{\Tamb}{\mathbf{Tamb}}

\newcommand{\Endo}{\mathbf{Endo}}
\newcommand{\Strong}{\mathbf{Strong}}
\newcommand{\Point}{\mathbf{Point}}
\newcommand{\CoPoint}{\mathbf{CoPoint}}
\newcommand{\App}{\mathbf{App}}
\newcommand{\Traversable}{\mathbf{Traversable}}
\newcommand{\IdxTraversable}{\mathbf{IdxTraversable}}

\newcommand{\Act}{\mathbf{Act}}
\newcommand{\Optic}{\mathbf{Optic}}
\newcommand{\Twoptic}{\mathbf{Optic}^2}
\newcommand{\Lawful}{\mathbf{Lawful}}
%\newcommand{\SemiOptic}{\mathbf{SemiOptic}}
\newcommand{\Lens}{\mathbf{Lens}}
\newcommand{\Prism}{\mathbf{Prism}}
\newcommand{\Setter}{\mathbf{Setter}}
\newcommand{\Traversal}{\mathbf{Traversal}}
\newcommand{\Getter}{\mathbf{Getter}}
\newcommand{\Review}{\mathbf{Review}}
\newcommand{\Fold}{\mathbf{Fold}}
\newcommand{\IdxLens}{\mathbf{IdxLens}}
\newcommand{\IdxTraversal}{\mathbf{IdxTraversal}}

\newcommand{\switched}{\mathbin{\tilde{\otimes}}}

\newcommand{\conc}{\mathbb{C}}
\newcommand{\conctwice}{\mathbb{C}^2}

\newcommand{\id}{\mathrm{id}}
\newcommand{\op}{\mathrm{op}}
\newcommand{\const}{\mathrm{const}}
\DeclareMathOperator{\ob}{ob}
\DeclareMathOperator{\copr}{copr}
\newcommand{\inl}{\mathrm{inl}}
\newcommand{\inr}{\mathrm{inr}}
\DeclareMathOperator{\im}{im}
\newcommand{\act}{\cdot}
\newcommand{\codisc}{\mathsf{codisc}}
\DeclareMathOperator*{\colim}{\mathrm{colim}}
\newcommand{\teletimes}{\mathbin{\boxtimes}}
\newcommand{\defeq}{\mathrel{\vcentcolon=}}

\newcommand*\circled[1]{\tikz[baseline={([yshift=-0.65ex]current bounding box.center)}]{
   \node[shape=circle,draw,inner sep=1pt] (char) {#1};}}
\newcommand{\actL}{{\circled{\tiny$\mathsf{L}$}}}
\newcommand{\actR}{{\circled{\tiny$\mathsf{R}$}}}

\newcommand{\rep}[2]{{\ensuremath \left\langle #1 \mid #2 \right\rangle}}
\newcommand{\repthree}[3]{{\ensuremath \langle #1 \mid #2 \mid #3 \rangle}}
\newcommand{\repfour}[4]{{\ensuremath \langle #1 \mid #2 \mid #3 \mid #4 \rangle}}

\newcommand{\fget}{\textsc{Get}}
\newcommand{\fput}{\textsc{Put}}
\newcommand{\fmodify}{\textsc{Modify}}
\newcommand{\freview}{\textsc{Review}}
\newcommand{\fcreate}{\textsc{Create}}
\newcommand{\fmatching}{\textsc{Matching}}
\newcommand{\funzip}{\textsc{Unzip}}
\newcommand{\fover}{\textsc{Over}}
\newcommand{\findex}{\textsc{Index}}

\newcommand{\mget}{\textsc{MGet}}
\newcommand{\mput}{\textsc{MPut}}
\newcommand{\munzip}{\textsc{Munzip}}

\newcommand{\inside}{\mathsf{inside}}
\newcommand{\outside}{\mathsf{outside}}
\newcommand{\once}{\mathsf{once}}
\newcommand{\twice}{\mathsf{twice}}

% Special arrows
%\newcommand{\isoto}{\xrightarrow{\cong}}
\newcommand{\hto}{\ensuremath{\,\mathaccent\shortmid\rightarrow\,}}

\makeatletter
\providecommand{\leftsquigarrow}{%
  \mathrel{\mathpalette\reflect@squig\relax}%
}
\newcommand{\reflect@squig}[2]{%
  \reflectbox{$\m@th#1\rightsquigarrow$}%
}
\makeatother

% Draft helpers
\newcommand{\todo}[1]{\textcolor{red}{\small #1}}

\title{Mixed Optics}
\author{Mitchell Riley}
\affil{Wesleyan University \\ \texttt{mvriley@wesleyan.edu}}
\begin{document}
\maketitle

\section{Mixed Optics}\label{sec:mixed-optics}

We can generalise the definition of $\Optic$ so that the two halves lie in different categories, allowing us to capture a few more optic variants found in the wild. Suppose $\C_L$ and $\C_R$ are categories that are acted on by a common monoidal category $\M$. Write these actions as $\actL : \M \to [\C_L, \C_L]$ and $\actR : \M \to [\C_R, \C_R]$ respectively.

\begin{definition}
  Given two objects of $\C_L \times \C_R^\op$, say $(S, S')$ and $(A, A')$, an \emph{optic} $p : (S, S') \hto (A, A')$ is an element of the set
  \begin{align*}
    \Optic_{\actL, \actR}((S, S'), (A, A')) := \int^{M \in \M} \C_L(S, M \actL A) \times \C_R(M \actR A', S')
  \end{align*}
\end{definition}

Several results from Section~\ref{sec:optics} have analogues in this setting.
\begin{proposition}\label{prop:mixed-optic-is-cat}
  $\Optic_{\actL, \actR}$ forms a category. \qed
\end{proposition}
\begin{proposition}
  There is a functor $\iota : \C_L \times \C_R^\op \to \Optic_{\actL, \actR}$, which on objects is given by $\iota(S, S') = (S, S')$ and on a morphism $(f, g) : (S, S') \to (A, A')$ by \[\iota(f, g) = \rep{{(\lambda^{\C_L}_A)}^{-1} f}{g \lambda^{\C_R}_{A'}},\] where $\lambda^{\C_L}_A : A \to IA$ in $\C_L$ and $\lambda^{\C_R}_{A'} : A' \to IA'$ in $\C_R$. \qed
\end{proposition}
\begin{proposition}\label{prop:mixed-change-of-action}
  Given two morphisms of actions $F_L : (\M, \C_L) \to (\N, \D_L)$ and $F_R : (\M, \C_R) \to (\N, \D_R)$, there is an induced functor $\Optic(F_L, F_R) : \Optic_{\actL_1, \actR_1} \to \Optic_{\actL_2, \actR_2}$. \qed
\end{proposition}

The notion of lawfulness used earlier breaks down in this setting, as the definition of $\Twoptic(S, A)$ only makes sense when $\C_L = \C_R$. The following definition gives a reasonable approximation.

\begin{definition}
If $F_L : (\M, \C_L) \to (\N, \D)$ and $F_R : (\M, \C_R) \to (\N, \D)$ are morphisms of actions, a mixed optic $p : (S, S) \hto (A, A)$ in $\Optic_{\actL, \actR}$ is \emph{lawful relative to $(F_L, F_R)$} if its image under $\Optic(F_L, F_R)$ in $\Optic_\N$ is lawful.
\end{definition}

\subsection{Degenerate Optics}

The \mintinline{haskell}{Getter}s and \mintinline{haskell}{Review}s of the Haskell lens library corresponds simply to a morphisms $S \to A$ and $A' \to S'$ respectively. These are in fact examples of mixed optics.

Recall that for any category $\C$, there is a category $\C_\codisc$ with the same objects, but where every homset is the singleton set. For any action of $\M$ on $\C$ there is a corresponding action of $\M$ on $\C_\codisc$ that has the same action on objects. There is a morphism of actions $i : (\M, \C) \to (\M, \C_\codisc)$.

For a category with finite products $\C$, define $\Getter$ to be the category of mixed optics where $\C$ acts on $\C$ via $\times$ on the left, and on $\C_\codisc$ also by $\times$. This category is indeed equivalent to $\C$:
\begin{align*}
\Getter((S, S'), (A, A'))
&\defeq \int^{M \in \M} \C(S, M \times A) \times \C_\codisc(M \times A', S')\\
&\cong \int^{M \in \M} \C(S, M \times A) \\
&\cong \C(S, 1 \times A) \\
&\cong \C(S, A)
\end{align*}
The only sensible notion of lawfulness for $\Getter$s is lawfulness relative to $(i, \id_{\C_\codisc})$, but all optics in $\Optic_{\C_\codisc}$ are trivially lawful.

\begin{remark}
If we we are working linearly, we should not expect $\Optic_{\otimes, \otimes_\codisc}$ to be described so neatly; the above isomorphism relies on the unit of the monoidal structure being terminal.
\end{remark}

One defines $\Review := \Optic_{\sqcup, \sqcup_\codisc}$ analogously. There are induced functors $\Optic(\id_\C, i) : \Lens \to \Getter$ and $\Optic(i, \id_\C) : \Prism \to \Review$.

Finally, we can also encode the category of \mintinline{haskell}{Fold}s. In $\Set$, a fold $S \hto A$ corresponds to a function $S \to \mathrm{List}\,A$. Define
\begin{align*}
\Fold := \Optic_{\Traversable, \C, \C_\codisc}
\end{align*}
The key is that the $\mathrm{List}$ functor is the terminal $\Traversable$ functor. We therefore have isomorphisms
\begin{align*}
&\int^{M \in \Traversable} \Set(S, MA) \times \Set_\codisc(MA', S') \\
&\cong \int^{M \in \Traversable} \Set(S, MA) \\
&\cong \Set(S, \mathrm{List}\,A)
\end{align*}
There is an induced functor $\Optic(\id_C, i) : \Traversal \to \Fold$.

\subsection{Indexed Lenses}
As well as giving a principled home to degenerate optics, the notion of mixed optics also allows us to describe \emph{indexed optics} categorically. The most interesting example of an indexed optic variant is the \emph{indexed traversal}, which, for example, allows one to know the index of an entry of a list as one is traversing it. As a warmup, however, let us look at \emph{indexed lenses}, which provide access to an index alongside a target, but do not allow updates to the index.

Implementing indexed optics by `passing through' the indices is based on ideas from~\cite{IndexedOpticsPost}.

Suppose $\C$ is cartesian closed. We define two actions of $\C$, the first on $\C$ and the second on $\C \times \C^\op$:
\begin{align*}
M \actL A  &:= M \times A\\
M \actR (A, I) &:= (M \times A, \homC(M, I))
\end{align*}

\begin{definition}
The category of \emph{indexed lenses} $\IdxLens$ in $\C$ is the category of mixed optics $\Optic_{\actL, \actR}$ with the actions as given above.
\end{definition}
Describing concrete indexed lenses is straightforward:
\begin{align*}
  &\Optic_{\actL, \actR}((S, (S', J')), (A, (A', I'))) \\
  &= \int^{M \in \C} \C(S, M \actL A) \times (\C \times \C^\op)(M \actR (A', I'), (S', J')) \\
  &\cong \int^{M \in \C} \C(S, M \times A) \times (\C \times \C^\op)((M \times A', \homC(M, I')), (S', J')) \\
  &\cong \int^{M \in \C} \C(S, M \times A) \times \C(M \times A', S') \times \C^\op(\homC(M, I'), J') \\
  &\cong \int^{M \in \C} \C(S, M \times A) \times \C(M \times A', S') \times \C(J', \homC(M, I')) \\
  &\cong \int^{M \in \C} \C(S, M) \times \C(S, A) \times \C(M \times A', S') \times \C(M \times J', I') \\
  &\cong \C(S, A) \times \C(S \times A', S') \times \C(S \times J', I')
\end{align*}
Let us call this third, new map $\findex : S \times J' \to I'$.

The presence of $J'$ above allows us to chain together indexed lenses and have the the resulting lens remember both indices that appeared. Specifically, in practice our indexed lenses are of shape
\[
(S, (S, J)) \hto (A, (A, I \times J))
\]
and natural in $J$, so we can choose a specific instantiation of $J$ as necessary to make the indexed lenses composable. \todo{more detail?}

There is a morphism of actions $U : (\actR, \C \times \C^\op) \to (\times, \C)$ that simply projects to the first component. An indexed lens is lawful relative to $(\id_\C, U)$ if the underlying lens is lawful. This \emph{doesn't}, however, guarantee some properties we might expect of a lawful lens. For example, we might prefer that $\fput$ does not change the index. It is unclear whether such a law fits in this framework.

\begin{remark}
There appears to be some flexibility in what actions we use to define indexed lenses. To have the indices behave covariantly rather than contravariantly, we could have $\C$ act on $\C$ and $\C \times \C$ as follows:
\begin{align*}
M \actL A &:= M \times A \\
M \actR (A, I) &:= (M \times A, M \times I)
\end{align*}
which gives concrete optics in the form
\begin{align*}
\C(S, A) \times \C(S \times A', S') \times \C(S \times I', J').
\end{align*}
We chose the previous, slightly more contorted actions so that indexed lenses are more clearly related to indexed traversals, which we define presently.
%We calculate:
%\begin{align*}
%  &\Optic_{\actL, \actR}((S, (S', J')), (A, (A', I'))) \\
%  &\cong \int^{(M, R) \in \C \times \C} \C(S, (M, R) \actL A) \times (\C \times \C)((M, R) \actR (A', I'), (S', J')) \\
%  &\cong \int^{(M, R) \in \C \times \C} \C(S, M \times R \times A) \times \C(M \times A', S') \times \C(R \times I', J') \\
%  &\cong  \C(S, A) \times \C(S \times A', S') \times \C(S \times I', J')
%\end{align*}
\end{remark}

\subsection{Indexed Traversals}

Returning to indexed traversals, we first need to define indexed traversable functors. Unfortunately this has many moving parts! For simplicity we will restrict our attention to $\C = \Set$, although it may be possible to apply the results more generally.

\begin{proposition}
The assignment $F \act X \mapsto \Set(R, FX)$ extends to a lax action of $\App$ on $\Set$, called the \emph{$R$-indexed action of $\App$ on $\Set$}.
\end{proposition}
\begin{proof}
The structure map for the lax action is given by the composite
\begin{align*}
F \act (G \act X) &= \Set(R, F\Set(R, GX)) \\
&\to \Set(R, \Set(FR, FGX)) \\
&\to \Set(R \times FR, FGX) \\
&\to \Set(R \times R, FGX) \\
&\to \Set(R, FGX) = (FG) \act X
\end{align*}
\todo{more}
\end{proof}

\begin{definition}
A \emph{traversable functor indexed by $R$} is a functor $T : \Set \to \Set$ equipped with a distributive law $\delta_F : T(\Set(R, F-)) \to FT$ for $T$ between the $R$-indexed action of $\App$ on $\Set$ and the (unindexed) action of $\App$ on $\Set$ by evaluation.
\end{definition}

\begin{definition}
The category of \emph{indexed traversable functors} $\IdxTraversable$ has as objects pairs $(T, R)$, where $R \in \Set$ and $T : \Set \to \Set$ is a traversable functor indexed by $R$. A morphism $(T, R) \to (T', R')$ is a natural transformation $\alpha : T \Rightarrow T'$ and morphism (note the direction!) $r : R' \to R$, such that $\alpha$ commutes with the distributive laws on both traversable functors, using $r$ to mediate between the index objects. \todo{draw this out}
\end{definition}

\begin{proposition}
$\IdxTraversable$ acts on $\C \times \C$ by $(T, R) \act (A, I) \mapsto (TA, \homC(R, I))$ and on $\C$ simply by $(T, R) \act A \mapsto TA$.
\end{proposition}

\begin{conjecture}
The category of indexed traversals corresponds to the category of mixed optics with the action of $\IdxTraversable$ on $\C \times \C$ on the left and $\C$ on the right.
\end{conjecture}

\subsection{Implementation}
\todo{todo?}
There is a notion of Tambara module structure for profunctors between two different categories. This allows us to re-derive the profunctor encoding for degenerate optics. For example, in the case of \mintinline{haskell}{Getter}s, a profunctor $P : \C^\op \times \C_\mathsf{codisc}$ essentially ignores its covariant argument. This is captured exactly by the `\mintinline{haskell}{RightPhantom}' typeclass or its equivalent in profunctor optic libraries.

\bibliographystyle{alpha}
\bibliography{optics.bib}
\end{document}


% This is a simple sample document.  For more complicated documents take a look in the excersice tab. Note that everything that comes after a % symbol is treated as comment and ignored when the code is compiled.

\documentclass{article} % \documentclass{} is the first command in any LaTeX code.  It is used to define what kind of document you are creating such as an article or a book, and begins the document preamble

\usepackage{amsmath} % \usepackage is a command that allows you to add functionality to your LaTeX code

\title{Simple Sample} % Sets article title
\author{My Name} % Sets authors name
\date{\today} % Sets date for date compiled

% The preamble ends with the command \begin{document}
\begin{document} % All begin commands must be paired with an end command somewhere
\maketitle % creates title using infromation in preamble (title, author, date)

\section{Hello World!} % creates a section

\textbf{Hello World!} Today I am learning \LaTeX. %notice how the command will end at the first non-alphabet charecter such as the . after \LaTeX
 \LaTeX{} is a great program for writing math. I can write in line math such as $a^2+b^2=c^2$ %$ tells LaTexX to compile as math
 . I can also give equations their own space:
\begin{equation} % Creates an equation environment and is compiled as math
\gamma^2+\theta^2=\omega^2
\end{equation}
If I do not leave any blank lines \LaTeX{} will continue  this text without making it into a new paragraph.  Notice how there was no indentation in the text after equation (1).
Also notice how even though I hit enter after that sentence and here $\downarrow$
 \LaTeX{} formats the sentence without any break.  Also   look  how      it   doesn't     matter          how    many  spaces     I put     between       my    words.

For a new paragraph I can leave a blank space in my code.


\section{Binary Address Format}
\label{sec:address-binary}

\newcommand{\binary}[1]{\ensuremath{\mathsf{#1}}}

Excepting bootstrap addresses,
the binary format for every address has a 1-byte header, followed by a payload
(bootstrap addresses use the binary encoding from the Byron era).
The header is composed of two nibbles (two 4-bit segments),
indicating the address type and the network id.

$$
\begin{array}{r@{~=~}l}
    \mathsf{address} & \mathsf{header\_byte}|\mathsf{payload} \\
                     & \mathsf{addr\_type\_nibble}|\mathsf{network\_nibble}|\mathsf{payload}
\end{array}
$$

\subsection{Header, first nibble}
\label{sec:address-binary-header-first-nibble}

The first nibble of the header specifies the type of the address.
Bootstrap addresses can also be identified by the first nibble.

\begin{center}
\begin{tabular}{ |c|c|c|c| }
 \hline
 address type & payment credential & stake credential & header, first nibble \\
 \hline
 \hline
 base address & keyhash      & keyhash    & \binary{0000} \\
 base address & scripthash   & keyhash    & \binary{0001} \\
 base address & keyhash      & scripthash & \binary{0010} \\
 base address & scripthash   & scripthash & \binary{0011} \\
 \hline
 pointer address & keyhash    & ptr & \binary{0100} \\
 pointer address & scripthash & ptr & \binary{0101} \\
 \hline
 enterprise address & keyhash    & - & \binary{0110} \\
 enterprise address & scripthash & - & \binary{0111} \\
 \hline
 bootstrap address & keyhash    & - & \binary{1000} \\
 \hline
 stake address & - & keyhash & \binary{1110} \\
 stake address & - & scripthash & \binary{1111} \\
 \hline
 future formats & ? & ? & \binary{1001}-\binary{1101} \\
 \hline
\end{tabular}
\end{center}

\subsection{Header, second nibble}
\label{sec:address-binary-header-second-nibble}

Excepting bootstrap addresses, the second nibble of the header specifies the network.

\begin{center}
\begin{tabular}{ |c|c| }
 \hline
 network & header, second nibble \\
 \hline
 \hline
 testnets & \binary{0000} \\
 mainnet & \binary{0001} \\
 future networks & \binary{0010}-\binary{1111}\\
 \hline
\end{tabular}
\end{center}

\subsection{Header, examples}
\label{sec:address-binary-header-examples}

On a \textbf{testnet},
the header for a \textbf{pointer} address whose payment credential is a \textbf{keyhash} is:
$$\binary{00000100}$$

On \textbf{mainnet}, the header for a \textbf{pointer} address whose payment credential is a \textbf{keyhash} is:
$$\binary{00010100}$$

On \textbf{mainnet}, the header for a \textbf{pointer} address whose payment credential is a \textbf{scripthash} is:
$$\binary{00010101}$$

\subsection{Payloads}
\label{sec:address-binary-payloads}

The payload for the different address types are as follows:

\begin{center}
\begin{tabular}{ |c|c| }
 \hline
 address type & payload \\
 \hline
 \hline
 base address & two 28-byte bytestrings \\
 \hline
 pointer address & one 28-byte bytestring,
 and three variable-length unsigned integers \\
 \hline
 enterprise address & one 28-byte bytestrings \\
 \hline
 stake address & one 28-byte bytestrings \\
 \hline
\end{tabular}
\end{center}

The variable-length encoding used in pointers addresses is the base-128 representation
of the number, with the the most significant bit of each byte indicating continuation.
If the significant bit is $0$, then another bytes follows.


\section{Implementation}

Here we discuss two main implementations of the Parareal algorithm using OpenMP.

\subsection{Naive OpenMP}

From the basic description of Parareal, we see that the fine propagator is able
to be computed in parallel, so the main idea here is to parallelize the for loop
corresponding to the fine propagator. See algorithm \ref{alg:parareal} for the
pseudocode. A quick description is as follows:
\begin{enumerate}[(1)]
  \item Lines $(1-2)$ describe the initial coarse approximation of the system.
    It's important that these solutions are loaded into both $y_c$ and $y$,
    since they're needed for the next bit to satisfy the first same as last
    property.
  \item Lines $(4-8)$ are the computation of the fine approximation, and the
    construction of the corrector term $\delta y$. There are a few implicit
    assumptions here:
    \begin{itemize}
      \item We assume that $y(n)$ hold the previous $\lambda_n^k$.
      \item We assume that $y_c(n)$ already holds the value $\coarse(t_{n+1},
        t_n, \lambda_n^k)$.
    \end{itemize}
    Both of these assumptions are true atleast for the first iteration due to
    our considerations in the first part. Furthermore, this part is completely
    dependent on previously computed information, and has no previous dependence
    on previous iterates, and therefore is embarrassingly parallel.
  \item Lines $(9-12)$ actually do the parareal iteration, we predict via line
    $10$, and we correct via our previously computed $\delta y(n)$.
\end{enumerate}
\begin{breakablealgorithm}
  \caption{Naive Parallel Parareal Algorithm}
  \label{alg:parareal}
  \begin{algorithmic}[1]
    \Require $y_0$ and coarse and fine solvers $\coarse$, $\fine$.
    \State $y_c \gets \coarse(t_f, t_0, y_0)$.\Comment{Coarsely approximate
      solution}
    \State $y \gets y_c$.
    \While{$\textrm{iter} < \textrm{max\_iter}\ \&\&$ not converged}
      \State \#pragma omp parallel for
      \For{$n = 0 \to P$}
        \State $y_f(n) = \fine(t_{n+1},t_n,y(n))$.
        \State $\delta y(n) = y_f(n) - y_c(n)$.\Comment{corrector term. FSAL}
      \EndFor
      \For{$n = 0 \to P$}
        \State $y_c(n) = \coarse(t_{n+1},t_n,y(n))$.\Comment{Predict.}
        \State $y(n) = y_c(n) + \delta y(n)$.\Comment{Correct.}
      \EndFor
    \EndWhile
  \end{algorithmic}
\end{breakablealgorithm}

\subsection{Pipelined OpenMP}

There's some clear problems with the naive implementation of Parareal.
Primarily, let's examine the coarse computation portions, lines $1$ and $10$ of
algorithm $\ref{alg:parareal}$. Both of these portions iteratively compute the
coarse approximation to the solution, but they lock up all processors while they
do so. In fact, once we've computed the approximation for the $n$th node, we can
immediately continue, instead of waiting for the rest of the serial computation
to finish. This is referred to in literature (Ruprecht \cite{ruprecht}) as
\textit{pipelining}. In fact, this would happen naturally in the MPI
implementation, but since I had decided to use OpenMP, we have to tackle this
problem.

The solution here is actually to mimic an MPI enviornment by wrapping the
entire parareal algorithm in a parallel region. The idea is that thread $p$ has
the job of processing the fine and coarse operators, and later solution at
position $t_{p+1}$. While we don't have the cost of MPI communication across
processes, we still need to appropriately place locks around our reads and
writes to prevent race conditions. A brief description of this new algorithm,
whose pseudocode is provided in algorithm \ref{alg:pparareal}, follows:
\begin{enumerate}[(1)]
  \item Line $1$ encases the whole algorithm in a parallel region, essentially
    mimicing a MPI centric algorithm, except in shared memory.
  \item Lines $(2-8)$ are the initial coarse approximation of the solution.
    Note, that for the $p$th process, we actually integrate from $t_0 \to t_p$,
    in order to compute the coarse solution at $t_{p+1}$. We could instead have
    process $0$ do the whole integration, and then communicate those values to
    all processes, but it was observed then that this would be no better than
    the serial computation in the naive algorithm. Note that the nowait
    statement allows to continue immediately, which is the \textit{pipelining}
    that we desire.
  \item Lines $(12-16)$ describe the parallel computation of $\fine$ and $\delta
    y$. Note, it's important to have locks around the points from which we read
    and write, since across this algorithm thread $p$ reads from position $p$
    and writes into position $p+1$. A point of note is that if we could refine
    this algorithm so that it read and wrote into only position $p$, save for
    one location, it would vastly improve.
  \item Lines $(17-22)$ describe the predictor and corrector step of parareal,
    accompanied with the necessary locks. Note that this is surrounded in a
    ordered directive, which is necessary since to compute the predictor step
    for $p+1$, we need $y(p)$. Therefore, we have no choice but to do this
    iteratively. However, because of the nowait clause on line $10$, we aren't
    forced to wait for the other threads to finish before continuing on.
\end{enumerate}
\begin{breakablealgorithm}
  \caption{Pipelined Parallel Parareal Algorithm (source Ruprecht \cite{ruprecht})}
  \label{alg:pparareal}
  \begin{algorithmic}[1]
    \Require $y_0$ and coarse and fine solvers $\coarse$, $\fine$.
    \State \#pragma omp parallel\Comment{Enclose whole algorithm in parallel region}
    \State \#pragma omp for nowait
    \For{$p = 0 \to P$}
      \If{$p \neq 0$}
        \State $y(p) = \coarse(t_{p},t_0,y_0)$.\Comment{Repeated work across
        threads, but $\coarse$ is cheap.}
      \EndIf
      \State $y_c(p+1) = \coarse(t_{p+1},t_p, y(p))$.
    \EndFor
    \While{$\textrm{iter} < \textrm{max\_iter}\ \&\&$ not converged}
      \State \#pragma omp for ordered nowait\Comment{nowait critical to avoid
      waiting for threads $> p$}
      \For{$p = 0 \to P$}
        \State omp\_set\_lock($p$) \Comment{Read lock.}
        \State $\textrm{temp} = \fine(t_{p+1},t_p,y(p))$.
        \State omp\_unset\_lock($p$) and then omp\_set\_lock($p+1$)\Comment{Write lock.}
        \State $y_f(p+1) = \textrm{temp},\ \delta y(p+1) = y_f(p+1)-y_c(p+1)$.
        \State omp\_unset\_lock($p+1$)
        \State \#pragma omp ordered \Comment{ordered because $y(p)$ depends on
        $y(p-1)$.}
        \Indent
          \State omp\_set\_lock($p$) \Comment{Read lock.}
          \State $\textrm{temp} = \coarse(t_{p+1},t_p,y(p))$.
          \State omp\_unset\_lock($p$) and then omp\_set\_lock($p+1$)\Comment{Write lock.}
          \State $y_c(p+1) = \textrm{temp},\ y(p+1) = y_c(p+1) + \delta y(p+1)$.
          \State omp\_unset\_lock($p+1$)
        \EndIndent
      \EndFor
    \EndWhile
  \end{algorithmic}
\end{breakablealgorithm}