<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">% flag to use fancy fonts / graphics
\newif\ifjnf\jnftrue

\documentclass[11pt]{article}

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

\usepackage{csnotes}

% local definitions for this lecture
\newcommand{\lam}[2]{\ensuremath{\lambda #1.\,#2}}
\newcommand{\definedas}{\ensuremath{\triangleq}}
\newcommand{\subst}[3]{\ensuremath{#1[#2/#3]}}

\begin{document}
\header{25 October 2011}{Stephen Longfield}

\section{Monad Review}

In category theory a monad is a triplet, $(C, \eta, \mu)$, containing
an endofunctor over categories along with two natural transformations.
The endofunctor over categories, C, takes an input of a value in some
category (for example, integers in the category of sets), and gives an
output that is also in that category, therefore, it has type $C : T
\rightarrow T$.  The two transformations it must define are a unit
function that maps a value into the monad (known as $\eta$, with type
$I_C \rightarrow T$), and a multiplication function, known as $\mu$,
which goes from a repeated application of the endofunctor to a single,
represented as having type $T^2 \rightarrow T$ or $T \circ T
\rightarrow T$.  These transformations must be defined in such a way
that they follow certain coherence properties.

This concept has been adopted by functional programming language
designers as a way of decorating certain data types. These decorations
may be things like the \texttt{Maybe} monad, which extends any type to
be a category with the option of being \texttt{Nothing}. In some
functional languages, most notably Haskell, they are also used to
augment series execution of functions, to indicating if these
functions are allowed to have ``impure'' side effects, such as I/O.
This allows Haskell programmers to separate functions whose output
will always be the same as for a given input (referentially
transparent) from ones whose output may vary (such as reading from an
input buffer). Eugenio Moggi is the Italian researcher who put down
the framework for the use of Monads in a functional language, as was
discussed at the Cornell Programming Languages Discussion Group last
year.

\subsection{Monads in Haskell}

The papers that we are going to be discussing today are both based on
the idea of augmenting Haskell with concurrency monads. Therefore, it
will be helpful to review how monads are implemented and how they are
used in Haskell.

In Haskell a monad is a kind of typeclass, a structure not entirely
different from a Java Interface, where a type specification is given
for several functions that anyone that would like to be members of
this typeclass must satisfy.  Additionally, a Haskell typeclass may
define default implementations of the functions, such as in the case
of the \texttt{Eq} typeclass, where not equals is simply defined as
the boolean negation of equals..

The type specification for a Haskell monad is:

\begin{verbatim}
class Monad m where
  (&gt;&gt;=) :: m a -&gt; ( a -&gt; m b) -&gt; m b
  return :: a -&gt; m a
\end{verbatim}

The first operator, \texttt{(&gt;&gt;=)} is known as the "bind" operator.
It takes an argument of type \texttt{a} wrapped in the monad
\texttt{m}, a function which maps from values of type \texttt{a} to
values of type \texttt{b} wrapped in monad \texttt{m}, and returns a
value of type \texttt{b} wrapped in the monad \texttt{m}.  For
example, in the set monad, this would let you take a set (like, $\{1,
5\}$) take a function such as \texttt{f x = \{x, 2x\}}, and would
generate an output of $\{1,2,5,10\}$. This is the operator that is
used for composing multiple monadic actions in series.  It is
analogous to the $\mu$ mapping from category theory monads.

The second operator, \texttt{return}, takes a value of type \texttt{a}
and wraps it in monad \texttt{m}. For sets of integers, it would take
a single integer and return a set (e.g. \texttt{return 1} would result
in $\{1\}$) It is analogous to the $\eta$ operator.

\subsection{Examples}

An example used in the first paper discussed below is the
\texttt{writer} monad.  It decorates the result of some computation
with a string, which could be used for logging actions.  In the paper
it is first defined as a monadic typeclass that defines a function
\texttt{write}, which moves a \texttt{String} into the monad.

\begin{verbatim}
class Monad m =&gt; Writer m where
  write :: String -&gt; m ()
\end{verbatim}

To use the \texttt{writer} monad, we need to create a type which is an
instance of this typeclass. This instance must also be an instance of
the \texttt{monad} typeclass.  In the paper, this instance was called
\texttt{W}.

\begin{verbatim}
type W a = (a, String)

instance Monad W where
  (a, s) &gt;&gt;= k = let (b, s') = k a in (b, s++s')
  return x     = (x, "")
  
instance Writer W where
  write s = ((), s)
\end{verbatim}

What the above code does is to declare a new type named \texttt{W}
that takes an argument of some arbitrary type \texttt{a}, and creates
a type \texttt{(a, String)}.  It then makes this type an instance of
the \texttt{monad} typeclass, where the bind operator when given a
pair of some value \texttt{(a, s)} and a function \texttt{k} applies
\texttt{k} to the value \texttt{a}, extracts the result as \texttt{(b,
  s')}, and then returns \texttt{(b, s++s')}, where \texttt{++} is the
Haskell concatenation operator.

The return operator is much simpler, simply taking in some value
\texttt{x}, and returning a pair with that value and the empty
string. This type \texttt{W} is also made into an instance of the
\texttt{Writer} typeclass, where the \texttt{write} function returns a
pair with the unit type, \texttt{()}, and the input string.

To use this monad we need one additional construct: a \textit{run}
function.  For the writer monad, this consists of extracting the
string and tossing away the value that was computed.  In Haskell, this
is not atypical, as we're often more interested in a monad's side
effects than the actual value that it contains.  Here, that function
is called \texttt{output} and is defined as:

\begin{verbatim}
output :: W a -&gt; String
output (a, s) = s
\end{verbatim}

This monad can then be used to compose several functions together, and
make notes about their intermediate state.  This can be done in two
separate ways.  Most directly, the bind operator can be used to string
several functions together. However, more readably, you can use
Haskell $do$ syntax, where statements can contain bindings to
variables which will be passed to subsequent statements.

For example, the following two code segments represent equivalent
functionality, but the one written in $do$ notation looks more
natural, and does not require every line to be made into an anonymous
function (done here with the backslash operator, a Haskell operator
chosen since it looks somewhat similar to $\lambda$, but can be typed
by a standard keyboard).

\begin{verbatim}

expr1 &gt;&gt;= \x -&gt;         do x &lt;- expr1 
expr2 &gt;&gt;= \_ -&gt;            ; expr2
expr3 &gt;&gt;= \y -&gt;            ; y &lt;- expr3
return expr4               ; return expr4

\end{verbatim}


\section{Functional Pearls: A Poor Man's Concurrency Monad}

This paper describes the implementation of a concurrency monad
entirely within the language of Haskell without the addition of any
language features.  In the paper, the bind operator, \texttt{&gt;&gt;=} is
represented by $\star$, however in these notes, I will be representing
it as \texttt{&gt;&gt;=} for the sake of consistency with the way Haskell is
typically input.

The "Functional Pearls" in the title of this paper refers to a class
of paper.  These papers are designed to be accessible and well-written
documents discussing an interesting technique or methodology. They are
not required to be ground-breaking when they are published, but due to
the cleverness of the ideas that they discuss, and the level of
accessibility they are designed for, they are almost always worth
reading.

To build the concurrency monad, this paper creates a Monad
transformer. A monad transformer takes in a monad of one type, and
then creates a monad of another type.  For instance, if you wanted to
take a value you got from an I/O operation, and perform an analysis on
it which might fail, you could do this by creating a monad that has
the properties of both the \texttt{IO} monad and the \texttt{Maybe}
monad.  In this paper, we'll be discussing a monad transformer called
\texttt{C}, which allows any monadic action lifted into it to be
considered as atomic in concurrent operations.

As the author of this paper decided he would not add any primitives to
the language, the model of concurrency created does not actually
exhibit actual concurrency, but instead interleaves the execution of
several monadic operators. To do this, it needs to have some way to
suspend the operation. This is being done with continuations, where
each computation is given a place to go once its computation is done,
and can postpone going to that location.  We can think of the
continuation as the "future" of the computation.  To think of that in
the typesystem, if we have some computation with the type of
\texttt{Action m} (where \texttt{m} is some monad), a function that
uses a continuation with result type \texttt{a} has the type:

\begin{verbatim}
type C m a = ( a -&gt; Action m ) -&gt; Action m
\end{verbatim}

Given that, the monad transformer \texttt{C} is defined as:

\begin{verbatim}
instance Monad m =&gt; Monad (C m) where
  f &gt;&gt;= k    = \c -&gt; f ( \a -&gt; k a c)
  return x   = \c -&gt; c x
\end{verbatim}

To understand this, we can look at the types of the different
operators and think about the kinds of types we would like to get out.

% Return
The simplest one is first, the return operator.  We would like to give
it a value of some type \texttt{a}, and have it return a type of
\texttt{C m a}. To do this, all we have to do is to take a
continuation and call it.

%Bind
The bind operator is much more complicated.  We'd like to take a value
of \texttt{C m a} (or \texttt{ (a -&gt; Action m) -&gt; Action m)}), a
function of type \texttt{a -&gt; C m B} (or \texttt{a -&gt; ( B -&gt; Action m)
  -&gt; Action M}), and return a value of \texttt{C m B}, (or \texttt{ (B
  -&gt; Action m) -&gt; Action m)}).  To do this, we'll be using some
anonymous functions to take in continuations and apply them to actions
in order to expose their values.  Below is the bind operator with type
annontations:

\begin{verbatim}
f:((a -&gt; Action m)-&gt;Action m) &gt;&gt;= k:(a-&gt;(B-&gt;Action m)-&gt;Action m)) = 
    \c : ( B -&gt; Action m ) -&gt; f ( \a : (a) . k a c )
\end{verbatim}

% Action
The paper then defines the specifics of the \texttt{Action} operator
type, which is one of an Atomic action inside the monad
(\texttt{Atom}), a fork operation, which forks off two different
actions (\texttt{Fork}) and a stop operation, which ends the current
thread of execution (\texttt{Stop}). These three different kinds of
actions provide all of the primitives needed for a basic
implementation of concurrency.

% Round Robin Scheduler
Because the concurrency design in this paper isn't actual parallel
concurrency made with separate threads, it requires a scheduler in the
language to take \texttt{Actions} and execute them one at a time.  The
example given in the paper is a round-robin scheduler, where a stack
of executable processes is maintained, with a single element being
popped off of it at a time.  If an Atomic action is popped off, it is
allowed to execute one step, and then its continuation is put back on
the bottom of the stack.  If a fork action is popped off, it is split
into two actions, which are placed on the bottom.  When a stop action
is popped off, it is simply allowed to die.

% Use for Concurrent Output
In the paper, the round robin scheduler is used in conjunction with
the writer monad, to provide for interleaved output.  Two examples are
shown, one where doing the entire printing action is lifted as an
atomic action (and hence, the interleaving happens at the string
level), and another where each character print is lifted as an atomic
action, causing the interleaving to happen at that level.

% Merging of Infinite Lists


\section{Combining Events and Threads for Scalable Network Services}

% Mention that this paper continues where the last one left off
This paper is an extension of the work from the previous paper,
however, its implementation of a concurrency monad uses language
features to allow for actual parallel execution. It continues to use a
cooperative execution model, and use lazy execution of continuations
to allow for interruption.

% The difference between Event and Threaded based Concurrency
This paper claims that the model of concurrency it uses elegantly
bridges the gap between thread-based and event-based parallelism. This
is the same goal that the Actor abstraction discussed on the 20th of
October had. Thread-based execution is typically considered to
interface with shared memory, and have no control over when preemption
may occur. Event-based parallelism must set up waits for each action
that it would like to respond to, and wait for that event to occur
before they run, which may make reasoning about control flow
difficult.  This paper proposes a hybrid model that allows programmers
to write in the somewhat more natural threaded style, but with the
thread scheduler allowing for event handlers.

% The similarities
The reason the paper cites for choosing the hybrid model is actually
the similarity of the two models.  In 1978 Lauer and Needham described
a mapping between Threads and Events, which argued that fundamentally,
they are the same, though they may be interfaced with and implemented
differently. During class discussion, there were some disagreements
with the exact duality, but some of the points still stood. In the
paper, they chose to hybridize by using thread abstractions to
represent control flow, and event abstractions to provide scheduling
control. In particular, they used continuation-passing-style (CPS)
event handlers to blend control and scheduling flow. This model was
implemented in Haskell using monadic operators in a similar way to the
Claessen paper above.  The main difference is the scheduler allows for
true parallelism, instead of just interleaving of operations.
Exceptions are handled through the continuations.

\end{document}

</pre></body></html>