# thesis / pyhaskell.tex

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 %------------------ \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{sec: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} `