\chapter{Code Samples and Comments}
\label{app:code}

\section{Fibonacci Implementations}
\label{sec:fib-imp}
The Fibonacci numbers are an integer sequence given by
\begin{equation}
  F_n = F_{n-1} + F_{n-2}
\end{equation}
and
\begin{equation}
  F_0 = 0, F_1 = 1\;.
\end{equation}
Because this is a very simple, recursive definition, it was chosen for the demonstration of certain concepts in this thesis. However, it is very important to note that the straight forward implementation
\imlcode{~~\=fu\=n fib 0 = 0\\
\>\>| fib 1 = 1\\
\>\>| fib x = fib (x - 1) + fib (x - 2)\textrm{,}}
\noindent
which we presented as an example for pattern matching in \textit{Standard ML} (page \pageref{alg:patmatch}, section \ref{sec:patmatch}), is very inefficient because it has a runtime behavior of $O(F_n)$. An efficient version of the function,
\imlcode{~~\=fu\=n \=fi\=b' 0 = (0, 1)\\
\>\>| fib' n = let\\
\>\>\>\>val (x1, x2) = fib' (n - 1)\\
\>\>\>in\\
\>\>\>\>(x2, x1 + x2)\\
\>\>\>end;\\
\>(*gets 2nd element of touple*)\\
\>fun fib n = fib' (n - 1) |> snd \textrm{,}}
\noindent
shows linear runtime behavior. Annotations (section \ref{sec:annot}) were demonstrated using the Fibonacci function (page \pageref{alg:parannot}):
\imlcode{~~\=fu\=n fib 0 = 0\\
\>\>| fib 1 = 1\\
\>\>| fib x = fib (x - 1) + par (fib (x - 2))}
\noindent
In practice, the gain from parallelism in this example would not just be marginal, the overhead for managing parallel execution would most likely result in a performance worse than the sequential version. Note that annotations of this kind can only work in a programming language following a lazy evaluation strategy because in an eager language like \textit{Standard ML} the annotated expression would be evaluated before being passed to the hypothetical {\tt par} evaluation.
Futures have been discussed in detail throughout this thesis (sections \ref{sec:futurespro}, \ref{sec:actors_scala}, \ref{sec:csp}, \ref{sec:isafut}). The example on page \pageref{alg:futures} redefined {\tt fib} with a future:
\imlcode{\label{alg:fib-futures}
~~\=fu\=n \=fi\=b \=0 = 0\\
\>\>| fib 1 = 1\\
\>\>| fib x = let\\
\>\>\>\>\>fun fib' () = fib (x - 2)\\
\>\>\>\>\>val fibf = Future.fork fib'\\
\>\>\>\>in fib (x - 1) + (Future.join fibf) end}
This code does work in {\em Isabelle/ML} (section \ref{sec:isabelleml}). The performance concerns we saw with the previous example also apply here: in practice, parallelizing the evaluation of the Fibonacci function in this way makes no sense.


\section{{\tt merge\_lists} implementation}
\label{app:merge-lists}
In the \texttt{Theory\_Data} implementation on page \pageref{alg:thydata-functor} (section \ref{ssec:parall-thy}) we omitted the definitions of the functions {\tt merge\_lists} and {\tt merge\_lists'} for simplicity reasons. They have been added here (program \ref{alg:merge-lists}). Please note that they require that their input lists of type {\tt int list} are already ordered. The difference is that {\tt merge\_lists} accepts the two lists in a touple and {\tt merge\_lists'} as two separate arguments.
\begin{program}
\caption{{\tt merge\_lists} implementation.}
\label{alg:merge-lists}
\begin{MLCode}
fun merge out [] xs = (rev out) @ xs
  | merge out xs [] = (rev out) @ xs
  | merge out (xs' as x::xs) (ys' as y::ys) =
      if x = y then
        merge (x::out) xs ys
      else if x < y then
        merge (x::out) xs ys'
      else
        merge (y::out) xs' ys;
val merge_lists' = merge [];
fun merge_lists (xs, ys) = merge_lists' xs ys;
\end{MLCode}
\end{program}


\section{Irrationality of \texorpdfstring{$\sqrt{2}$}{the square root of 2} in \textit{Isar}}
\label{app:sqrt2-isar}

Program \ref{alg:isar-sqrt2} is a simple proof in \textit{Isar}, showing that the square root of 2 is an irrational number\footnote{\tt \textasciitilde\textasciitilde/src/HOL/ex/Sqrt.thy}.


\begin{program}
\caption{\textit{Isabelle/Isar} proof of the irrationality of $\sqrt{2}$.}
\label{alg:isar-sqrt2}
\vspace{\medskipamount}
\noindent
\colorbox{color5}{\texttt{\small
\begin{minipage}{.98\textwidth}
\setlength{\parindent}{1pc}
\noindent
{\tiny\ttfamily\color{color6} 1~}~{\bfseries\color{color1} lemma} {\color{color3} "$\exists$a b::real. a $\notin$ $\mathbb{Q}$ $\land$ b $\notin$ $\mathbb{Q}$ $\land$ a powr b $\in$ $\mathbb{Q}$"}\\
{\tiny\ttfamily\color{color6} 2~}~{\indent({\bfseries\color{color1} is} {\color{color3} "EX a b. ?P a b"})}\\
{\tiny\ttfamily\color{color6} 3~}~{\bfseries\color{color1} proof} cases\\
{\tiny\ttfamily\color{color6} 4~}~{\indent\bfseries\color{color1} assume} {\color{color3} "sqrt 2 powr sqrt 2 $\in$ $\mathbb{Q}$"}\\
{\tiny\ttfamily\color{color6} 5~}~{\indent\bfseries\color{color1} then have} {\color{color3} "?P (sqrt 2) (sqrt 2)"}\\
{\tiny\ttfamily\color{color6} 6~}~{\indent\indent\bfseries\color{color1} by} (metis sqrt\_2\_not\_rat)\\
{\tiny\ttfamily\color{color6} 7~}~{\indent\bfseries\color{color1} then show} {\color{color2} ?thesis} {\bfseries\color{color1} by} blast\\
{\tiny\ttfamily\color{color6} 8~}~{\bfseries\color{color1} next}\\
{\tiny\ttfamily\color{color6} 9~}~{\indent\bfseries\color{color1} assume} 1: {\color{color3} "sqrt 2 powr sqrt 2 $\notin$ $\mathbb{Q}$"}\\
{\tiny\ttfamily\color{color6} 10}~{\indent\bfseries\color{color1} have} {\color{color3} "(sqrt 2 powr sqrt 2) powr sqrt 2 = 2"}\\
{\tiny\ttfamily\color{color6} 11}~{\indent\indent\bfseries\color{color1} using} powr\_realpow [of \_ 2]\\
{\tiny\ttfamily\color{color6} 12}~{\indent\indent\bfseries\color{color1} by} (simp add: powr\_powr power2\_eq\_square [symmetric])\\
{\tiny\ttfamily\color{color6} 13}~{\indent\bfseries\color{color1} then have} {\color{color3} "?P (sqrt 2 powr sqrt 2) (sqrt 2)"}\\
{\tiny\ttfamily\color{color6} 14}~{\indent\indent{\bfseries\color{color1} by} (metis 1 Rats\_number\_of sqrt\_2\_not\_rat)}\\
{\tiny\ttfamily\color{color6} 15}~{\indent\bfseries\color{color1} then show} {\color{color2} ?thesis} {\bfseries\color{color1} by} blast\\
{\tiny\ttfamily\color{color6} 16}~{\bfseries\color{color1} qed}
\end{minipage}}}
\end{program}

