From: Mart Lubbers Date: Tue, 5 Apr 2016 18:36:14 +0000 (+0200) Subject: long way with the long review X-Git-Tag: long_review1~3 X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=2b240dc391ecafb6c56423232745af1893fa17ef;p=rsss1516.git long way with the long review --- diff --git a/long/.chktexrc b/long/.chktexrc new file mode 100644 index 0000000..28a4938 --- /dev/null +++ b/long/.chktexrc @@ -0,0 +1 @@ +VerbEnvir { CleanCode CI CleanInline } diff --git a/long/long.tex b/long/long.tex index 1c35411..a9d5f44 100644 --- a/long/long.tex +++ b/long/long.tex @@ -1,281 +1,198 @@ %&long \begin{document} -\frame{\titlepage} + +\maketitle + +\tableofcontents \section{Introduction} -\begin{frame} - \frametitle{Scientific Impact} - \begin{block}{Metrics} - \begin{itemize}[<+->] - \item Cited $676$ times in $23$ years - \item $\pm 30$ cites per year - \item Cited as de-facto monad introducing paper - \item Monads in computer science: Moggi et al.\ in $1989$ - \end{itemize} - \end{block} -\end{frame} - -\begin{frame} - \frametitle{Objective \& Current state} - \begin{block}{Objective} - \begin{itemize} - \item First introduction to monads - \item Layman's terms - \end{itemize} - \end{block} - - \pause% - \begin{block}{Current state of the art} - \begin{itemize}[<+->] - \item $2016$: $2$ papers citing Wadler - \item $2015$: $24$ papers citing Wadler - \item Ranging from - \begin{itemize}[<+->] - \item Introducing new monad types - \item Generic programming - \item Designing modular DSL's - \item Concurrency frameworks in functional languages - \item\ldots - \end{itemize} - \end{itemize} - \end{block} -\end{frame} - -\section{Contents} -\subsection{Introduction} -\begin{frame} - \frametitle{Introduction} - \begin{itemize} - \item Monads (Moggi et al.) - \item Integrate the impure in the pure - \item Wadler shows \textsc{Haskell} examples - \item Following samples are in \textsc{Clean} - \end{itemize} -\end{frame} - -\subsection{Naive approach} -\begin{frame}[fragile] - \frametitle{Case study (1)} - \framesubtitle{Evaluator} - \begin{CleanCode} +\subsection{Setting and objective} +\emph{Monads for functional programming} was initially published in +\emph{Program Design Calculi} which was a collection of lecture notes +accompanying the \emph{Marktoberdorf} summer school in 1992. Later it was +reissued in \emph{Springer}s \emph{Advanced Functional Programming} book which +presents the tutorials from a spring school in Sweden in 1995. + +The notes were written to introduce the reader to the idea of a Monad usable in +a functional programming context. Where monads are already defined and studied +in category theory in the early fifties. Moggi et al.\ were the first in 1988 +to see the usefulness in computer science and in particular functional +programming. At the time of writing this paper was introducing state of the art +research which later became the preferred way of dealing with side-effects in a +pure language. The paper familiarized readers with the concepts while only +requiring basic knowledge in functional programming. + +\subsection{Impact} +While Wadler had already published a lot about monads this turned out to be one +of the most accessible and readable papers concerning the topic. A lot of the +references in the paper cite his own work, but this is logical since he was +part of the small group of researchers that kick-started the use of monads in +functional programming. + +The paper has been cited over $676$ times over the past $23$ years resulting in +a $\pm 30$ cites per year. It is cited as the de-facto monad introducing paper. +In 2015 and 2016 the paper has already been cited numerous times in topics +including but not limited to: Introducing new monad types, generic programming, +designing modular design specific languages, concurrency frameworks in +functional languages and probabilistic computing. + +\section{Proposal and evidence} +\subsection{Proposal} +Wadler proposes to capture side-effects in monads. A monad is a structure +containing a monad type: +\CI{:: M a}\\ +Secondly a monad contains a function to lift a value to the monadic domain: +\CI{unit :: a -> M a}\\ +Lastly a monad contains the function that transforms \CI{a} into \CI{b} while +capturing the possible side-effects in \CI{M}: +\CI{(>>=) infixl 1 :: (M a) (a -> M b) -> M b} + +When this structure adheres certain laws they are very useful in capturing +side-effects that would otherwise be difficult to handle in an impure language. +Wadler shows this by tackling several classical problems arising in the pure +functional programming world. + +\subsection{\emph{Expression problem}} +In this review we will show one of the examples given to illustrate the meaning +and usage the monadic pattern. The code snippets in the original paper +supposedly are \emph{haskell} however they are not all valid. Thus the +following snippets have been translated to valid +\emph{Clean}~\footnote{\url{http://clean.cs.ru.nl}} + +\paragraph{Simple evaluator} +Say we have a simple evaluator where an expression is either an integer or a +division between two expressions. Writing a simple evaluator is very straight +forward. +\begin{CleanCode} :: Term = Con Int | Div Term Term eval :: Term -> Int eval (Con a) = a eval (Div t u) = eval t / eval u -//Examples answer :: Term -answer = (Div (Div (Con 1972) (Con 2)) (Con 23)) +answer = Div (Div (Con 1972) (Con 2)) (Con 23) error :: Term error = (Div (Con 1) (Con 0)) - \end{CleanCode} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Case study (2)} - \framesubtitle{Exceptions} - \begin{CleanCode} +\end{CleanCode} + +\paragraph{Exceptions} +When we evaluate \CI{eval error} we get a runtime exception since dividing by +zero is illegal. If we want to add exceptions things get a little more frisky. +We extend the type with an exception type that either contains an exception +or a return value. The following code will catch exceptions nicely. +\begin{CleanCode} :: M a = Raise Exception | Return a :: Exception :== String eval :: Term -> M Int -eval (Con a) = Return a +eval (Con a) = return a eval (Div t u) = case eval t of Raise e = Raise e Return a = case eval u of Raise e = Raise e - Return b = if (b == 0) - (Raise "Divide by zero") - (Return (a / b)) - \end{CleanCode} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Case study (3)} - \framesubtitle{State, count divisions} - \begin{CleanCode} + Return b = if (b == 0) (Raise "Divide by zero") (Return (a/b)) +\end{CleanCode} + +\paragraph{State} +Say we want to add state to the evaluator. Now we have to redo the entire +evaluation part again. Say we have single integer as a state which counts the +number of divisions taken. Then we get the following code which counts the +number of divisions. +\begin{CleanCode} :: M a :== State -> (a, State) :: State :== Int eval :: Term -> M Int -eval (Con a) x = (a, x) -eval (Div t u) -# (a, y) = eval t x -# (b, z) = eval u y -= (a/b, z+1) - \end{CleanCode} -\end{frame} - -\subsection{Monadic approach} -\begin{frame}[fragile] - \frametitle{Monadic (1)} - Monad is a triple: $(M, unit, \star)$ - \pause% - \begin{block}{Signatures} - \begin{CleanCode} -:: M a = ... -unit :: a -> M a -((#$\star$#)) infixl 1 :: (M a) (a -> M b) -> M b - \end{CleanCode} - \end{block} - \pause% - \begin{block}{Evaluator (Identity monad)} - \begin{CleanCode} -:: M a :== a -unit :: a -> a -unit a = a -((#$\star$#)) infixl 1 :: (M a) (a -> M b) -> M b -((#$\star$#)) a k = k a - \end{CleanCode} - \end{block} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Monadic (2)} - \framesubtitle{Exceptions} - \pause% - \begin{CleanCode} +eval (Con a) = \x.(a, x) +eval (Div t u) = + \x.let (a, y) = eval t x in + let (b, z) = eval u y in + (a/b, z+1) +\end{CleanCode} + +\subsection{A monadic approach to the \emph{Expression problem}} +\paragraph{Exceptions} +The example for exceptions can be implemented using monads as follows: +\begin{CleanCode} :: M a = Raise Exception | Return a :: Exception :== String - \end{CleanCode} - \pause% - \begin{CleanCode} + unit :: a -> M a unit a = Return a -((#$\star$#)) infixl 1 :: (M a) (a -> M b) -> M b -((#$\star$#)) m k = case m of +(>>=) infixl 1 :: (M a) (a -> M b) -> M b +(>>=) m k = case m of Raise e = Raise e Return a = k a - \end{CleanCode} - \pause% - \begin{CleanCode} + raise :: Exception -> M a raise e = Raise e - \end{CleanCode} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Monadic (3)} - \framesubtitle{State} - \pause% - \begin{CleanCode} -:: M a = State -> (a, State) + +eval :: Term -> M Int +eval (Con a) = unit a +eval (Div t u) = eval t >>= \a->eval u >>= \b->case b of + 0 = Raise "Divide by zero" + b = unit (a/b) +\end{CleanCode} + +\paragraph{State} +The example for states can be implemented using monads as follows: +\begin{CleanCode} +:: Void = Void +:: M a :== State -> (a, State) :: State :== Int - \end{CleanCode} - \pause% - \begin{CleanCode} + unit :: a -> M a -unit a = \x.(a, x) +unit a = \x->(a, x) -((#$\star$#)) infixl 1 :: (M a) (a -> M b) -> M b -((#$\star$#)) m k = \x.let (a, y) = m x in +(>>=) infixl 1 :: (M a) (a -> M b) -> M b +(>>=) m k = \x->let (a, y) = m x in let (b, z) = k a y in (b, z) - \end{CleanCode} - \pause% - \begin{CleanCode} + tick :: M Void -tick = \x.(Void, x + 1) - \end{CleanCode} -\end{frame} - -\subsection{Laws} -\begin{frame}[fragile] - \frametitle{Laws} - \begin{block}{Monoid} - \begin{itemize} - \item Left unit: - \begin{CleanCode} -unit a (#$\star$#) \b.n = n[a/b] - \end{CleanCode} - \item Right unit - \begin{CleanCode} -m (#$\star$#) \a.unit a = m - \end{CleanCode} - \item Associativity - \begin{CleanCode} -m (#$\star$#) (\a.n (#$\star$#) \b.o) = (m (#$\star$#) \a.n) (#$\star$#) \b.o - \end{CleanCode} - \end{itemize} - \end{block} -\end{frame} - -\subsection{Further evidence} -\begin{frame}[fragile] - \frametitle{Different approach} - \framesubtitle{Parsers (1)} - \begin{CleanCode} -:: M a = State -> [(a, State)] -:: State :== [Char] +tick = \x->(Void, x+1) -unit :: a -> M a -unit a = \x.[(a, x)] - -((#$\star$#)) infixl 1 :: (M a) (a -> M b) -> M b -((#$\star$#)) m k = \x.[(b, z) \\ (a, y) <- m x, (b, z) <- k a y] - \end{CleanCode} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Different approach} - \framesubtitle{Parsers (2)} - \begin{CleanCode} -zero :: M a -zero = \x.[] - -((#$\oplus$#)) infixl 4 :: (M a) (M a) -> M a -((#$\oplus$#)) m n = \x.m x ++ n x - -((#$\triangleright$#)) infixl 4 :: (M a) (a -> Bool) -> M a -((#$\triangleright$#)) m p = m (#$\star$#) \a.if (p a) (unit a) zero - -item :: M Char -item = \x.case x of - [] = [] - [a:x] = [(a, x)] - \end{CleanCode} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Different approach} - \framesubtitle{Parsers (3)} - \begin{CleanCode} -iterate :: M a -> M [a] -iterate m = (m (#$\star$#) \a.iterate m (#$\star$#) \x.unit [a:x]) (#$\oplus$#) unit [] - -iterate (item (#$\triangleright$#) isDigit) "23 and more" -[([2,3], " and more"), ([2], "3 and more"), ([], "23 and more")] - \end{CleanCode} - \pause - \begin{CleanCode} -((#$\oslash$#)) infixl 4 :: (M a) (M a) -> M a -((#$\oslash$#)) m n = \x.if (m x <> []) (m x) (n x) - \end{CleanCode} -\end{frame} - -\section{Discussion} -\begin{frame} - \begin{block}{Writing style} - \begin{itemize}[<+->] - \item Pleasure to read - \item Classical problems - \item Fancy words - \end{itemize} - \end{block} - \pause - \begin{block}{Discussion queries} - \begin{itemize}[<+->] - \item Toy implementations, bigger? - \item Would the last section about parsers be a paper on its own? - \end{itemize} - \end{block} -\end{frame} - -\begin{frame} - \frametitle{Questions or discussion points?} -\end{frame} +eval :: Term -> M Int +eval (Con a) = unit a +eval (Div t u) = eval t >>= \a->eval u >>= \b->tick >>= \_->unit (a/b) +\end{CleanCode} + +\subsection{Evidence} +Besides the example shown above Wadler shows also shows the usefulness of +monads in a setting where you use lists, arrays and recursive descent parsers. +These examples show the flexibility and ease that monads bring. Especially for +input/output the monadic pattern is useful. If one wants to capture +side-effects, instead of reimplementing every function for the particular side +effects you can just write the monadic operations and let the monads handle the +rest. + +Even after more than 20 years the monadic pattern is used throughout the +functional programming world and is incorporated in all the functional +programming languages used by the academic and by everyday programmers such as +\emph{Haskell} and \emph{Clean}. + +\section{Analysis} +\subsection{Writing style} +The paper has been written for (under)-graduate students and thus it is +extremely readable. Even with no particular domain knowledge the paper is very +clear. The author tackles the expression problem, arrays as a state and parsers +which are classical problems in the functional programming field. Even when the +reader has little knowledge of functional programming those problems are +familiar and are effective in illustrating the power of monads. + +\subsection{Discussion} +Within the time frame of publishing these are near perfect publication, however +in hindsight there are some things that could be improved upon. + +The language use is sometimes a bit informal and possibly even fluffy. However, +the fact that these were originally lecture notes for a summer school clear +this issue up. + +Secondly the section about parsers is quite big and not might not be necessary +for illustrating the power of monads. The contents of this section could very +well be a paper on its own. \end{document} diff --git a/long/pre.tex b/long/pre.tex index 99a38e7..98d66fc 100644 --- a/long/pre.tex +++ b/long/pre.tex @@ -1,20 +1,19 @@ -\documentclass{beamer} +\documentclass{article} +\usepackage[a4paper]{geometry} +\usepackage{hyperref} \usepackage{clean} -\usecolortheme{dove} -\beamertemplatenavigationsymbolsempty% - -\title[RSSS]{Monads for functional programming\\P.\ Wadler} -\subtitle{Originally published in Springer's \emph{Program Design Calculi} in - $1993$} -\author[Lubbers]{M.~Lubbers~\inst{1}} -\institute[Radboud University]{% - \inst{1}% - Computing Science: Software Science\\ - Radboud University Nijmegen +\title{% + Monads for functional programming\\ + P.\ Wadler\\ + {\large% + Originally published in Springer's \emph{Program Design Calculi} in + 1993 + } } -\date[RSSS-w2]{2016{--}03{--}22} +\author{M.~Lubbers} +\date{\today} \hypersetup{% pdftitle={Monads for functional programming}, @@ -23,10 +22,3 @@ pdfproducer={Mart Lubbers}, hidelinks } - -\AtBeginSection[]{% - \begin{frame} - \frametitle{Table of Contents} - \tableofcontents[currentsection] - \end{frame} -}