Source

thesis / pyhaskell.tex

Full commit
%------------------
\chapter{PyHaskell}
%------------------


\section{Haskell-Python}
%-----------------------
TODO: Describe how it was started.


\section{Previous work}
%----------------------
\label{sec:pipeline}
% Changeset d23acda9d37f is last before start of thesis.
TODO: Describe in a high-level way what work has gone into it so far.

\citet{skrede} has extended the project by designing and implementing a
pipeline that uses \gls{ghc} as a frontend for the lambda calculus \gls{vm}.
The structure of the pipeline can be seen in \autoref{fig:pipeline}.
\begin{figure}[tbp]
	\center
	\includegraphics[scale=0.75]{./img/pipeline}
	\caption[Haskell-Python pipeline]{%
        Haskell-Python pipeline by \citet{skrede}\label{fig:pipeline}}
\end{figure}
The pipeline starts with the \gls{ghc} frontend that takes care of parsing,
type checking and desugaring Haskell source code. \gls{ghc} then generates an
intermediate representation of the Core language that is written to a ``hcr''
file. The \texttt{core2js} Haskell program then converts the ``hcr'' file to
JSCore, a \gls{json} representation of Core, with the help of a Haskell
package called extcore\footnote{extcore package: \url{http://hackage.haskell.org/package/extcore}}.
Finally the JSCore file is parsed and interpreted by PyHaskell~\cite{skrede}.

TODO: explain and link to \mypref{ghc-optimizations} how we miss out on some,
and unboxing is only half-done.


\section{Improvements}
%---------------------
\todo{Need better section-title!}
Describe, in detail, the work I have done on PyHaskell during my thesis.

\subsection{runMainIO}
\mycode{runMainIO} is a simple wrapper around \mycode{Main.main} that should
catch otherwise uncaught exceptions, and should also flushe stdout/stderr
before exiting.
\mytodo{TODO, move this notice or fix/implement it.}

\subsection{putStrLn}
\mycode{putStrLn} is a simple function that takes a list of \mycode{Char}'s
(a string), and the prints them (with a newline) to \mycode{stdout}. The type
of \mycode{putStrLn} is defined in Haskell Prelude as:
\mint{haskell}|String -> IO ()|
While we had implemented it with the correct behaviour, in as far as it printed
the correct things to \mycode{stdout}, it returned the \mycode{Char} list
instead of \mycode{IO ()}. It was done this way to avoid having to implement
parts such as IO, but it pushed the list further down the evaluation stack,
which could be problematic.

Consider the simple example of printing the string ``Hey'' to \mycode{stdout}:
\mint{haskell}|main = putStrLn "Hey"|
In the external core output produced by \gls{ghc}
(\mylst{lst:putStrLn-extcore}\footnote{\mylst{lst:putStrLn-extcore} has been
Z-decoded according to \mypref{tab:zencoding}}), one might see that the example
simply calls \mycode{putStrLn} with the unpacked string ``Hey''. PyHaskell
converts this to a set of operations that it evaluates, and in line six of
\mylst{lst:putStrLn-evallog} the string is printed. The argument to
\mycode{putStrLn} can be seen again in line seven, and it is given to
\mycode{runMainIO} in line eight, where evaluation ends.

\mytodo{actually implement IO (), and return it, then explain what I did and show an example}

\begin{listing}[htbp]
\begin{lstlisting}[basicstyle=\footnotesize, numberstyle=\tiny\color{gray}, numbers=right]
%module main:Main
  main:Main.main :: (ghc-prim:GHC.Types.IO
                     ghc-prim:GHC.Tuple.()) =
    base:System.IO.putStrLn
    (ghc-prim:GHC.CString.unpackCString#
     ("Hey"::ghc-prim:GHC.Prim.Addr#));
  main::Main.main :: (ghc-prim:GHC.Types.IO
                       ghc-prim:GHC.Tuple.()) =
    base:GHC.TopHandler.runMainIO @ ghc-prim:GHC.Tuple.()
    main:Main.main;
\end{lstlisting}
\caption{putStrLn example, GHC external core output\label{lst:putStrLn-extcore}}
\end{listing}

\begin{listing}[htbp]
\begin{lstlisting}[basicstyle=\footnotesize, numberstyle=\tiny\color{gray}, numbers=right]
runMainIO putStrLn unpackCString# Hey  => 
putStrLn unpackCString# Hey  => 
unpackCString# Hey  => 
(Constr : H, (Constr : e, (Constr : y, (Constr []))))  => 
putStrLn (Constr : H, (Constr : e, (Constr : y, (Constr []))))  => 
Hey
(Constr : H, (Constr : e, (Constr : y, (Constr []))))  => 
runMainIO (Constr : H, (Constr : e, (Constr : y, (Constr []))))  => 
\end{lstlisting}
\caption{putStrLn example, PyHaskell evaluation log\label{lst:putStrLn-evallog}}
\end{listing}

\subsection{Show}
The class \mycode{Show} and its instances implement the function \mycode{show},
which convert a value to a string. For values of type \mycode{Int}
\mycode{show} will return a string with the value inside it. For example
calling \mycode{show} with the value 10 returns the string ``10'':
\mint{haskell}|show 10 = "10"|

Show was only implemented for \mycode{Int} values.
\mytodo{fix show, describe}

\subsection{Builtin libraries}
To be able to support more advanced benchmarks and tests, I've had to extend
the support of the Haskell Prelude by adding functionality implemented in
RPython.

\subsubsection{List}
The list module have been extended with the functions \mycode{length} and
\mycode{replicate}.


\section{Current status/Summary}
%-------------------------------
\todo{Need better section-title!}
Describe what the status/what we support/dont-support at the end of my thesis.
Some sort of summary, with fancy tables.

\begin{table}[tbp] \footnotesize \center
\caption{Overview over Haskell features PyHaskell support\label{tab:tests}}
\begin{tabular}{l c l}
    \toprule
	Feature tested & Success & Comment \\
	\midrule
	Hello world! & Yes & \\
	Case expression & Yes & \\
	Let expression & Yes & \\
	Data constructor & Yes & \\
	Partial function application & Yes & \\
	Recursive function & Yes & \\
    List concatenation `++' & Yes & \\
	Cons operator `:' & Yes & \\
	Function composition operator `.' & Yes & \\
	Function application operator `\$' & Yes & \\
	Indexing operator `!!' & No & Missing ``GHC.List'' support \\
	Recursive let expression & No & Missing ``GHC.List'' support \\
	Guards & No & Problem with extcore package \\
	Pattern matching & No & \\
	List comprehension & No & \\
	\bottomrule
\end{tabular}
\end{table}