<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Bluish Coder: haskell</title>
 <link href="http://bluishcoder.co.nz/tag/haskell/atom.xml" rel="self"/>
 <link href="http://bluishcoder.co.nz/"/>
 <updated>2020-07-10T16:25:05+12:00</updated>
 <id>http://bluishcoder.co.nz/</id>
 <author>
   <name>Bluishcoder</name>
   <email>admin@bluishcoder.co.nz</email>
 </author>

 
 <entry>
   <title>Random and Binary IO using Iteratees</title>
   <link href="http://bluishcoder.co.nz/2008/12/08/random-and-binary-io-using-iteratees.html"/>
   <updated>2008-12-08T02:31:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2008/12/08/random-and-binary-io-using-iteratees</id>
   <content type="html">&lt;p&gt;I wrote previously about interesting developments related to &lt;a href=&quot;http://bluishcoder.co.nz/2008/10/left-fold-enumerator-for-io.html&quot;&gt;left folds and i/o&lt;/a&gt;. Oleg Kiselyov has come up with more &lt;a href=&quot;http://okmij.org/ftp/Streams.html#iteratee&quot;&gt;Iteratee&lt;/a&gt; based goodness involving &lt;a href=&quot;http://okmij.org/ftp/Streams.html#random-bin-IO&quot;&gt;binary i/o and random access&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Iteratees presuppose sequential processing. A general-purpose input method
must also support random IO: processing a seek-able input stream from an
arbitrary position, jumping back and forth through the stream. We demonstrate
random IO with iteratees, as well as reading non-textual files and converting
raw bytes into multi-byte quantities such as integers, rationals, and TIFF
dictionaries. Positioning of the input stream is evocative of delimited
continuations.
...
We show a representative application of the library: reading a sample TIFF
file, printing selected values from the TIFF dictionary, verifying the values
of selected pixels and computing the histogram of pixel values. The pixel
verification procedure stops reading the pixel matrix as soon as all
specified pixel values are verified. The histogram accumulation does read the
entire matrix, but incrementally. Neither pixel matrix processing procedure
loads the whole matrix in memory. In fact, we never read and retain more than
the IO-buffer-full of raw data.&lt;/p&gt;&lt;/blockquote&gt;
</content>
 </entry>
 
 <entry>
   <title>Dynamic Compilation and Loading of Modules in Haskell</title>
   <link href="http://bluishcoder.co.nz/2008/11/25/dynamic-compilation-and-loading-of.html"/>
   <updated>2008-11-25T02:09:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2008/11/25/dynamic-compilation-and-loading-of</id>
   <content type="html">&lt;p&gt;The Haskell system &lt;a href=&quot;http://haskell.org/ghc&quot;&gt;GHC&lt;/a&gt; has libraries that provide the ability to compile Haskell code and dynamically load it into a running Haskell program. A library that provides this functionality is &lt;a href=&quot;http://www.cse.unsw.edu.au/~dons/hs-plugins/&quot;&gt;hs-plugins&lt;/a&gt;. Unfortunately hs-plugins doesn&#39;t work with the latest GHC release, &lt;a href=&quot;http://haskell.org/ghc/download_ghc_6_10_1.html&quot;&gt;6.10.1&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There is an API to the internals of GHC that allows implementing runtime compilation and loading. The api is &lt;a href=&quot;http://haskell.org/ghc/docs/6.10.1/html/libraries/ghc/GHC.html&quot;&gt;documented&lt;/a&gt; but there aren&#39;t very many &lt;a href=&quot;http://www.haskell.org/haskellwiki/GHC/As_a_library&quot;&gt;usage examples&lt;/a&gt; that work with the latest GHC release. I stumbled across a blog post which no longer exists describing an older version of the API, but managed to retrieve it from &lt;a href=&quot;http://web.archive.org/web/*/http://austin.youareinferior.net/?q=node/29&quot;&gt;archive.org&lt;/a&gt; which provided a lot of help since the API&#39;s are similar.&lt;/p&gt;

&lt;p&gt;As a means to learn how to use the API I decided on a very simple use case of compiling and loading a very simple Haskell module containing a single function &#39;print&#39; that outputs a string. The module looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module Test (Test.print) where

print :: String -&amp;gt; IO ()
print x = 
  putStrLn x
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To compile and load this code dynamically we need a &lt;a href=&quot;http://haskell.org/ghc/docs/6.10.1/html/libraries/ghc/GHC.html#4&quot;&gt;compilation target&lt;/a&gt;. This is created using the function &lt;a href=&quot;http://haskell.org/ghc/docs/6.10.1/html/libraries/ghc/GHC.html#v%3aguessTarget&quot;&gt;guessTarget&lt;/a&gt;. It&#39;s passed a string that refers to the name of a module, or the name of a source file. By using a source file we can tell the Haskell system to compile and load it. The phase argument I set to Nothing as I have no idea what that is.&lt;/p&gt;

&lt;p&gt;Once we have the target it needs to be added to the current compilation session. This is done via the &lt;a href=&quot;http://haskell.org/ghc/docs/6.10.1/html/libraries/ghc/GHC.html#v%3AaddTarget&quot;&gt;addTarget&lt;/a&gt; function. When all the targets are added, a call to &lt;a href=&quot;http://haskell.org/ghc/docs/6.10.1/html/libraries/ghc/GHC.html#v%3Aload&quot;&gt;load&lt;/a&gt; will do the equivalent of a &lt;code&gt;ghc --make&lt;/code&gt; to build the module if needed. So all the relevant target code looks like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;do
  target &amp;lt;- guessTarget &quot;Test.hs&quot; Nothing
  addTarget target
  load LoadAllTargets
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice this is run within the &#39;do&#39; syntax. This is because all these compiler functions must be called with a special Monad, called &lt;a href=&quot;http://haskell.org/ghc/docs/6.10.1/html/libraries/ghc/GHC.html#t%3AGhcMonad&quot;&gt;GhcMonad&lt;/a&gt;. The way this works is described later but just imagine that the GhcMonad is implicitly passed to all the Ghc functions we are calling automatically.&lt;/p&gt;

&lt;p&gt;&#39;load&#39; returns a &lt;a href=&quot;http://haskell.org/ghc/docs/6.10.1/html/libraries/ghc/GHC.html#t%3ASuccessFlag&quot;&gt;success flag&lt;/a&gt; that can be pattern matched on to determine if it succeeds or failed. If it succeeds we can start using the new module. To be able to access exported functions from the module we need to get a reference to the module using &lt;a href=&quot;http://haskell.org/ghc/docs/6.10.1/html/libraries/ghc/GHC.html#v%3AfindModule&quot;&gt;findModule&lt;/a&gt; and use &lt;a href=&quot;http://haskell.org/ghc/docs/6.10.1/html/libraries/ghc/GHC.html#v%3AsetContext&quot;&gt;setContext&lt;/a&gt; on it. &#39;setContext&#39; takes two arrays containing modules. All modules in the first array will have their top level scope available. All modules listed in the second array will have only their exports available. In this example we want to access the &#39;print&#39; function of &#39;Test&#39;, which is exported.&lt;/p&gt;

&lt;p&gt;The function &lt;a href=&quot;http://haskell.org/ghc/docs/6.10.1/html/libraries/ghc/GHC.html#v%3AcompileExpr&quot;&gt;compileExpr&lt;/a&gt; is used to compile a string containing a Haskell expression. The result of this is an &lt;a href=&quot;http://haskell.org/ghc/docs/6.10.1/html/libraries/ghc/GHC.html#t%3AHValue&quot;&gt;HValue&lt;/a&gt; which we can do things with. The expression that is compiled can access any of the bindings available in the context we set up via &#39;setContext&#39;. So passing &quot;Test.print&quot; to &#39;compileExpr&#39; will return the function &#39;print&#39; from the &#39;Test&#39; module that we dynamically built and loaded. Code similar to this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;do
  r &amp;lt;- load LoadAllTargets
  case r of
    Failed -&amp;gt; error &quot;Compilation Failed&quot;
    Succeeded -&amp;gt; do
      m &amp;lt;- findModule (mkModuleName &quot;Test&quot;) Nothing
      setContext [] [m]
      value &amp;lt;- compileExpr &quot;Test.print&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Unfortunately the result of &#39;compileExpr&#39; is of no use to us as an HValue if we want the actual function type so we can call it. Haskell being strongly typed we have to &#39;cheat&#39; by telling Haskell &#39;yes, I know you think this is an HValue, but I can assure you, it&#39;s really a function&#39;. This is Haskell&#39;s super dangerous &lt;a href=&quot;http://www.haskell.org/ghc/docs/latest/html/libraries/base/Unsafe-Coerce.html&quot;&gt;Unsafe.Coerce&lt;/a&gt;. It allows us to coerce from one type to another and is, as the name implies, completely unsafe. If the type you are coercing isn&#39;t really of the type you are coercing too then you&#39;ll very likely crash your program and/or corrupt data. It&#39;s the reinterpret_cast&amp;lt;&gt; of the Haskell world. But for handling dynamic loading of code it seems exactly what we want. I&#39;m not sure of any other way to convert the HValue type.&lt;/p&gt;

&lt;p&gt;The type of &#39;print&#39; in the &#39;Test&#39; module is &#39;String -&gt; IO ()&#39;. &#39;unsafeCoerce&#39; can be called to convert the &#39;HValue&#39; to this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;value &amp;lt;- compileExpr &quot;Test.print&quot;
  do let value&#39; =  (unsafeCoerce value) :: String -&amp;gt; IO ()
     return value&#39;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The receiver of the result, value&#39;, can now call the function as it knows its type. The complete code for this example is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import GHC
import GHC.Paths
import DynFlags
import Unsafe.Coerce

main :: IO ()
main =
    defaultErrorHandler defaultDynFlags $ do
      func &amp;lt;- runGhc (Just libdir) $ do
        dflags &amp;lt;- getSessionDynFlags
        setSessionDynFlags dflags
        target &amp;lt;- guessTarget &quot;Test.hs&quot; Nothing
        addTarget target
        r &amp;lt;- load LoadAllTargets
        case r of
          Failed -&amp;gt; error &quot;Compilation failed&quot;
          Succeeded -&amp;gt; do
            m &amp;lt;- findModule (mkModuleName &quot;Test&quot;) Nothing
            setContext [] [m]
            value &amp;lt;- compileExpr (&quot;Test.print&quot;)
            do let value&#39; = (unsafeCoerce value) :: String -&amp;gt; IO ()
               return value&#39;
      func &quot;Hello&quot;
      return ()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Build using:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ghc -package ghc --make Api.hs
[1 of 1] Compiling Main             ( Api.hs, Api.o )
Linking Api ...
$ ./Api
Hello
$
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can test that it is dynamically compiling and loading by modifying &#39;Test.hs&#39; so the output is different. Change the &#39;putStrLn&#39; to &lt;code&gt;putStrLn $ x ++ &quot; World!&quot;&lt;/code&gt; and rerun &#39;Api&#39; without recompiling it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ./Api
Hello World!
$
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I mentioned previously the implicit &#39;GhcMonad&#39; that is passed to each GHC API function. This is provided by the &lt;a href=&quot;http://haskell.org/ghc/docs/6.10.1/html/libraries/ghc/GHC.html#v%3ArunGhc&quot;&gt;runGhc&lt;/a&gt; word. It takes a pointer to the directory where GHC&#39;s library files reside. In this example I use the GHC.Paths (available from &lt;a href=&quot;http://hackage.haskell.org/packages/hackage.html&quot;&gt;Hackage&lt;/a&gt; package to deal with this using &#39;libdir&#39;. The second argument is the action to perform, the GhcMonad, and is built using the &#39;do&#39; syntax. Haskell&#39;s &#39;do&#39; syntax is syntactic sugar that does the magic of threading the GhcMonad state holding the targets, modules, contexts, etc through the various function calls. Find a favourite &lt;a href=&quot;http://forums.somethingawful.com/showthread.php?threadid=2841145&amp;amp;pagenumber=6#post346173552&quot;&gt;Monad tutorial/explanation&lt;/a&gt; if you want to know more.&lt;/p&gt;

&lt;p&gt;&#39;defaultErrorHandler&#39; sets up the environment to handle compilation errors in a standard way. The &#39;&lt;a href=&quot;http://haskell.org/ghc/docs/6.10.1/html/libraries/ghc/GHC.html#3&quot;&gt;DynFlags&lt;/a&gt;&#39; related calls get and set standard flags that can be used to configure the compilation. You can set the equivalent of GHC arguments to control extra features that GHC can use, or use the interpreter (as used by GHCi) rather than the compiler.&lt;/p&gt;

&lt;p&gt;Hopefully that helps explain some of what is going on and how to use the current GHC api. I&#39;d appreciate comments on better ways of doing things, and other examples of the API.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>The Left Fold Enumerator for i/o</title>
   <link href="http://bluishcoder.co.nz/2008/10/11/left-fold-enumerator-for-io.html"/>
   <updated>2008-10-11T00:47:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2008/10/11/left-fold-enumerator-for-io</id>
   <content type="html">&lt;p&gt;There have been some interesting papers and talks about approaches to handling i/o using left folds recently.&lt;/p&gt;

&lt;p&gt;First was the &lt;a href=&quot;http://www.galois.com/blog/2008/09/12/left-fold-enumerators-a-safe-expressive-and-efficient-io-interface-for-haskell/&quot;&gt;galois tech talk&lt;/a&gt; about a safe and efficient i/o interface in Haskell using left fold enumerators. &lt;a href=&quot;http://www.galois.com/~dons/slides/08-09-tibell.pdf&quot;&gt;PDF slides are here&lt;/a&gt;. The example web server, Hyena, is &lt;a href=&quot;http://github.com/tibbe/&quot;&gt;available on github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Oleg Kiselyov then &lt;a href=&quot;http://www.deinprogramm.de/defun-2008/abstracts/oleg-abstract.txt&quot;&gt;gave a talk&lt;/a&gt; at &lt;a href=&quot;http://www.deinprogramm.de/defun-2008/&quot;&gt;DEFUN&lt;/a&gt; about using left folds for web servers. The slides and source for the talk are available at Oleg&#39;s site in the &lt;a href=&quot;http://okmij.org/ftp/Streams.html#iteratee&quot;&gt;Haskell Iteratee I/O section&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;We explain input processing with left-fold enumerator, using as an example
HTTP request processing in Haskell. The approach is general and applies to
processing data from various collections, from in-memory data structures to
databases, files, sockets, etc.&lt;/p&gt;

&lt;p&gt;Our approach differs in: permitting incremental processing; i/o interleaving
comes by default; we shall see an example of i/o multiplexing with no need
for threads and related locking and synchronization.&lt;/p&gt;

&lt;p&gt;Unlike lazy IO, our approach is correct. There is not even hint of
UnsafePerformIO. Unlike Handle-based IO, accessing a disposed resource like a
closed handle is just impossible in our approach. Our approach has some other
nice properties, permitting composing streams and stream processors.&lt;/p&gt;

&lt;p&gt;One can use the same processor to handle several streams one after another.
Or use two processors to process parts of the same source. One can combine
processors vertically, which is very useful when one stream is embedded
(chunk-encoded, escaped, UTF8-encoded) into another. Enumerators and
iteratees, which generalize fold, have nice algebraic properties. But we
won&#39;t talk about them.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;Interesting weekend reading!&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Minix and Haskell</title>
   <link href="http://bluishcoder.co.nz/2006/06/07/minix-and-haskell.html"/>
   <updated>2006-06-07T20:22:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2006/06/07/minix-and-haskell</id>
   <content type="html">&lt;p&gt;There&#39;s an &lt;a href=&quot;http://groups.google.com/group/fa.haskell/browse_frm/thread/43f22847305b00ba&quot;&gt;interesting post&lt;/a&gt; on the Haskell group about &lt;a href=&quot;http://www.minix3.org/&quot;&gt;Minix&lt;/a&gt; and the possibility of writing OS core components in Haskell for it.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.minix3.org/&quot;&gt;Minix&lt;/a&gt; is an open source operating system that&#39;s been around for quite some time. I first tried it out sometime in the early 90&#39;s after downloading over some very slow dialup connection connected to a BBS.&lt;/p&gt;

&lt;p&gt;From the post:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Andrew Tanenbaum said that it is important for a language like OCaml or
Haskell to get more visibility among the OS developers; and Minix offers a
very good way to do that. Minix3 is based on micro-kernel; all of the OS
services (memory manager, file system, all the drivers, etc.) run as regular
processes communicating through a well-defined protocol. It is irreleveant
what language these services are written in, so long as they obey the
protocol.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;The poster goes on to suggest a way of slowly migrating the system routines to Haskell:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;With Minix, we can replace one kernel service of the full-fledged, working OS
with the one written in Haskell -- and immediately see how it all works. If
it doesn&#39;t, the rest of OS still works and so we can unload the faulty
service and load another implementation. Minix3 does indeed offer an easy
road to the OS kernel for a language other than C. I said that there may be
quite a bit of interest in Haskell community in such a project. I wonder if
I&#39;m right...&lt;/p&gt;&lt;/blockquote&gt;
</content>
 </entry>
 
 <entry>
   <title>Linspire to use Haskell</title>
   <link href="http://bluishcoder.co.nz/2006/05/22/linspire-to-use-haskell.html"/>
   <updated>2006-05-22T19:48:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2006/05/22/linspire-to-use-haskell</id>
   <content type="html">&lt;p&gt;According to the &lt;a href=&quot;http://sequence.complete.org/hwn/20060522&quot;&gt;Haskell Weekly News&lt;/a&gt;, the OS team at &lt;a href=&quot;http://www.linspire.com/&quot;&gt;Linspire&lt;/a&gt; are standardising on on Haskell as their preferred language for core OS development. Linspire is a Linux distribution.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;http://article.gmane.org/gmane.comp.lang.haskell.cafe/12662&quot;&gt;announcement from the Linspire team is here&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Playing with HAppS</title>
   <link href="http://bluishcoder.co.nz/2006/05/10/playing-with-happs.html"/>
   <updated>2006-05-10T05:55:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2006/05/10/playing-with-happs</id>
   <content type="html">&lt;p&gt;I downloaded &lt;a href=&quot;http://bluishcoder.co.nz/2006/04/haskell-application-server-happs.html&quot;&gt;HAppS&lt;/a&gt;, the Haskell Application Server, &lt;a href=&quot;http://bluishcoder.co.nz/2006/04/haskell-application-server-happs.html&quot;&gt;I mentioned previously&lt;/a&gt; to try it out.&lt;/p&gt;

&lt;p&gt;I already had GHC installed so I could build darcs but I needed a few additional packages to install HAppS. Most of these I obtained via apt-get but I had to manually install the following two:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.cse.unsw.edu.au/~dons/fps.html&quot;&gt;fps&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.cs.york.ac.uk/fp/HaXml/&quot;&gt;HaXml&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Once they were installed compiling and building HAppS is done using the &lt;a href=&quot;http://www.haskell.org/cabal/&quot;&gt;Cabal&lt;/a&gt; based setup file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;runhaskell Setup.hs configure
runhaskell Setup.hs build
sudo runhaskell Setup.hs install
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once that&#39;s out of the way I tried out the &#39;hello&#39; example. This is located in the &#39;examples&#39; subdirectory of &#39;HAppS&#39; and needs to be built and run:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;make
./hello
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This starts up an application server listening on port 8000. Where to go from there in terms of building apps I&#39;m not sure as there doesn&#39;t seem to be much &#39;how to&#39; style documentation. Digging through the source and examples looks to be the next thing to try.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Unifying Events and Threads in Haskell</title>
   <link href="http://bluishcoder.co.nz/2006/05/03/unifying-events-and-threads-in-haskell.html"/>
   <updated>2006-05-03T08:16:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2006/05/03/unifying-events-and-threads-in-haskell</id>
   <content type="html">&lt;p&gt; Update: 3 May 2006: &lt;a href=&quot;http://bluishcoder.co.nz/2006/05/source-for-unify-available.html&quot;&gt;Source is now available&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lambda-the-ultimate.org/node/1435&quot;&gt;Lambda the Ultimate points&lt;/a&gt; to a great paper, A Language-based Approach to Unifying Events and Threads, by Peng Li and Steve Zdancewic.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;This paper presents a language based technique to unify two seemingly
opposite programming models for building massively concurrent network
services: the event driven model and the multithreaded model. The result is a
unified concurrency model providing both thread abstractions and event
abstractions.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;The paper is about the implementation of the unified concurrency model in Haskell. The threads are &#39;lightweight&#39; threads and according to the paper scale to 10 million threads. The scheduler outperforms NPTL in I/O benchmarks.&lt;/p&gt;

&lt;blockquote&gt;&lt;ul&gt;
&lt;li&gt;Monads provide the thread abstraction by defining an imperative
sub-language of Haskell with system calls and thread control
primitives.&lt;/li&gt;
&lt;li&gt;Higher-order functions provide the internal representation of threads in
continuation passing style.&lt;/li&gt;
&lt;li&gt;Lazy data structures provide the event abstraction, which is a lazy tree
that represents the trace of system calls generated by threads.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Some interesting benchmark results too. One benchmark test they did involved launching 10 million threads that just yield within a loop. This was running on a machine with 1GB set aside for the GHC heap. The per thread cost was only 48 bytes!&lt;/p&gt;

&lt;p&gt;Their multiprocessor benchmark, yielded a 3.65 times speedup over a non-MP version version on a 4CPU machine (The library is able to take advantage of SMP machines).&lt;/p&gt;

&lt;p&gt;Other interesting things in the paper are a web server they built using this framework and an application level TCP stack written entirely in Haskell.&lt;/p&gt;

&lt;p&gt;I hope the source for the things mention in this paper becomes available. In the meantime, &lt;a href=&quot;http://www.cis.upenn.edu/~lipeng/papers/lz06submitted.pdf&quot;&gt;you can read it here&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Source for Unify Available</title>
   <link href="http://bluishcoder.co.nz/2006/05/03/source-for-unify-available.html"/>
   <updated>2006-05-03T08:15:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2006/05/03/source-for-unify-available</id>
   <content type="html">&lt;p&gt;The source for the system presented in the paper &#39;&lt;a href=&quot;http://bluishcoder.co.nz/2006/04/unifying-events-and-threads-in-haskell.html&quot;&gt;A Language Based Approach to Unifying Events and Threads&lt;/a&gt;&#39; is now available.&lt;/p&gt;

&lt;p&gt;Download information is available at the &lt;a href=&quot;http://www.seas.upenn.edu/~lipeng/homepage/unify.html&quot;&gt;Unify home page&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;The source code package can be browsed online here. It contains the
application-level thread library, a simple web server and a HTTP load
generator.  For more information, see the &lt;code&gt;READ_ME&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;To compile and run the source code, you will need the Linux 2.6 kernel, the
most recent development snapshot of GHC 6.5, and the Linux asynchronous I/O
library (libaio).&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;(From &lt;a href=&quot;http://lambda-the-ultimate.org/node/1435#comment-16497&quot;&gt;Lambda the Ultimate&lt;/a&gt;).&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Space Invaders Emulator in Haskell</title>
   <link href="http://bluishcoder.co.nz/2006/05/02/space-invaders-emulator-in-haskell.html"/>
   <updated>2006-05-02T01:03:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2006/05/02/space-invaders-emulator-in-haskell</id>
   <content type="html">&lt;p&gt;I been tinkering with &lt;a href=&quot;http://www.haskell.org/&quot;&gt;Haskell&lt;/a&gt; lately to get familiar with a purely functional programming language. As a project to work on I&#39;m trying to port the &lt;a href=&quot;http://bluishcoder.co.nz/2006/03/factor-space-invaders-updated.html&quot;&gt;Space Invaders emulator I wrote for Factor&lt;/a&gt; to Haskell.&lt;/p&gt;

&lt;p&gt;I&#39;m pretty much a complete newbie to Haskell and have no idea on the best ways to implement this type of thing. I&#39;m starting simple, seeing where the bottlenecks are, and will improve the code from there.&lt;/p&gt;

&lt;p&gt;For representing the memory I&#39;m using a standard Haskell Array type. Being a purely functional programming language this means that any updates to the array result in it being copied and the copy being returned. If this turns out to be too inefficient there are some variations on this array type, including UArray for unboxed storage of values.&lt;/p&gt;

&lt;p&gt;My first cut at an 8080 CPU abstraction was:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;data Cpu = 
 Cpu { a  :: Word8, 
       b  :: Word8, 
       c  :: Word8, 
       d  :: Word8, 
       e  :: Word8, 
       f  :: Word8, 
       h  :: Word8, 
       l  :: Word8, 
       pc :: Word16,
       sp :: Word16,
       doInterrupts :: Bool,
       mem :: Memory }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Functions that set register values in the Cpu type actually return a new Cpu with the old values copied across and the new value set. &#39;Memory&#39; is the array type:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type Memory = Array Word16 Word8
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This represents a mapping from 16 bit numbers (the memory address) to 8 bit values. To make the code easier to read I also created some data types to refer to the 8080 registers:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;data Register16 = BC | DE | HL | SP | PC deriving (Show)
data Register8  = A | B | C | D | E | F | H | L deriving (Show)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To implement the instruction set I created an &#39;Instruction&#39; datatype with constructors for each possible type of 8080 instruction. I got this idea from the &lt;a href=&quot;http://www.mutantlemon.com/omegagb/devlog/&quot;&gt;OmegaGB Gameboy Emulator in Haskell&lt;/a&gt;. Much thanks to the author of that for making their developer log and design ideas available. As an example, part of my &#39;Instruction&#39; type is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;data Instruction =
   MoveRegister   Register8 Register8 -- MOV r1,r2 
 | MoveFromMemory  Register8   -- MOV r,M 
 | MoveToMemory   Register8  -- MOV M,r 
 | MoveImmediate   Register8 Word8  -- MVI r,data 
 | MoveToMemoryImmediate  Word8   -- MVI M,data 
 | LoadRegisterPairImmediate Register16 Word16 -- LXI rp,data16
 | LoadAccumulatorDirect  Word16   -- LDA addr
 | StoreAccumulatorDirect Word16   -- STA addr
 | LoadHAndLDirect  Word16   -- LHLD addr
 | StoreHAndLDirect  Word16   -- SHLD addr
 | ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;My usual approach for decoding the machine code opcodes into these Instruction types would be to create a function that maps each opcode from 0 through to 255 to one of the instructions. Instead of doing this I went to the 8080 instruction set reference and created Haskell overloaded functions that work on the binary of the opcode and decode the registers,etc to operate on from that. So some of these functions look like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;opcodeToInstruction (Binary 0 1 d3 d2 d1 1 1 0) _ _ = 
 let r = register8Lookup (d3,d2,d1) in
   MoveFromMemory r

opcodeToInstruction (Binary 0 1 1 1 0 s3 s2 s1) _ _ = 
 let r = register8Lookup (s3,s2,s1) in
   MoveToMemory r

opcodeToInstruction (Binary 0 1 d3 d2 d1 s3 s2 s1) _ _ = 
 let r2 = register8Lookup (s3,s2,s1)
     r1 = register8Lookup (d3,d2,d1) in
   MoveRegister r1 r2

opcodeToInstruction (Binary 0 0 1 1 0 1 1 0) b2 _ = 
   MoveToMemoryImmediate b2
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &#39;register8Lookup&#39; uses the binary string for the register as it is encoded in the opcode to return the actual register type:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;register8Lookup :: (Integer,Integer,Integer) -&amp;gt; Register8
register8Lookup (1,1,1) = A
register8Lookup (0,0,0) = B
register8Lookup (0,0,1) = C
register8Lookup (0,1,0) = D
register8Lookup (0,1,1) = E
register8Lookup (1,0,0) = H
register8Lookup (1,0,1) = L
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This had the advantage of letting the Haskell compiler work out exactly what instruction to decode the opcode value too. It&#39;s made the code quite readable.&lt;/p&gt;

&lt;p&gt;A &#39;runInstruction&#39; function is used to execute the instruction and modify the Cpu type. Some examples of implementations are:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;runInstruction :: Cpu -&amp;gt; Instruction -&amp;gt; Cpu
-- Data transfer group
runInstruction cpu (MoveRegister r1 r2) =
 setRegister8 cpu r1 (getRegister8 cpu r2)

runInstruction cpu (MoveFromMemory r) =
   let hl = getRegister16 cpu HL
       v  = readMemory8 cpu hl in
     setRegister8 cpu r v

runInstruction cpu (MoveToMemory r) =
   let hl = getRegister16 cpu HL
       v  = getRegister8 cpu r in
         writeMemory8 cpu hl v

runInstruction cpu (MoveImmediate r v) =
   setRegister8 cpu r v

runInstruction cpu (MoveToMemoryImmediate v) =
   let hl = getRegister16 cpu HL in
     writeMemory8 cpu hl v

-- Branch Group
runInstruction cpu (Jump addr) = 
 setRegister16 cpu PC addr

runInstruction cpu (ConditionalJump condition addr) = 
   let c = isConditionTrue cpu condition in
     if c then setRegister16 cpu PC addr else cpu 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The code that looks up the machine code byte from memory converts it to the &#39;Binary&#39; datatype and calls &#39;opcodeToInstruction&#39; to get the instruction out of it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;toBinary :: Word8 -&amp;gt; Binary
opcodeToInstruction :: Binary -&amp;gt; Word8 -&amp;gt; Word8 -&amp;gt; Instruction
readMemory8 :: Cpu -&amp;gt; Word16 -&amp;gt; Word8

step1 :: Cpu -&amp;gt; Cpu
step1 cpu = 
  let addr = pc cpu
      opcode  = readMemory8 cpu addr
      b2 = readMemory8 cpu (addr+1)
      b3 = readMemory8 cpu (addr+2) 
      instruction = opcodeToInstruction (toBinary opcode) b2 b3 in
    runInstruction cpu instruction
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With all the instructions implemented I ran a simple benchmark of 1,000,000 instructions. Using &#39;-O2&#39; with &lt;a href=&quot;http://www.haskell.org/ghc&quot;&gt;GHC&lt;/a&gt; I got approximately 20 seconds on my machine (an Athlon 2500+) which is a tad slow.&lt;/p&gt;

&lt;p&gt;As a comparison, my Factor implementation runs 1,000,000 instructions in about 1.7 seconds.&lt;/p&gt;

&lt;p&gt;An easy change to make was to change the memory type to use a UArray which is an unboxed array. This immediately change the timing to 1.6 seconds. Which is much better than I expected. Remember that this is unoptimised code and purely functional. The only impure code is the I/O printing the CPU registers after each instruction (which I piped to /dev/null for the timings).&lt;/p&gt;

&lt;p&gt;The implementation currently emulates about 90% of the 8080 CPU instruction set. I still need to handle interrupts and then add a simple GUI. Once that&#39;s done I&#39;ll make the code available.&lt;/p&gt;

&lt;p&gt;Overall I found Haskell quite easy to develop with. I used &#39;ghci&#39;, the interpreter that is part of GHC, for most of the testing and iterative development. Using that was a lot like developing with a Lisp REPL. The static typing didn&#39;t get in my way and in fact it pointed to errors I&#39;d made at compile time instead of run time in a lot of cases. I&#39;ll see if that remains the case when I tackle the GUI though which is likely to be a bit more difficult.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Building wxFruit and Yampa with GHC 6.4.x</title>
   <link href="http://bluishcoder.co.nz/2006/04/24/building-wxfruit-and-yampa-with-ghc.html"/>
   <updated>2006-04-24T15:52:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2006/04/24/building-wxfruit-and-yampa-with-ghc</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;http://zoo.cs.yale.edu/classes/cs490/03-04b/bartholomew.robinson/&quot;&gt;wxFruit&lt;/a&gt; is a Haskell GUI library based on Functional Reactive Programming concepts. It uses wxWindows as its underlying GUI framework. For FRP it uses the Haskell FRP framework &lt;a href=&quot;http://haskell.org/yampa/&quot;&gt;Yampa&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Both libraries are relatively old and seem to have suffered bitrot as I couldn&#39;t get them to compile using a recent GHC build (GHC 6.4.x). A bit of searching came across &lt;a href=&quot;http://www.ruby-talk.org/cgi-bin/scat.rb/haskell/haskell-jp/701&quot;&gt;this post to the haskell-jp list&lt;/a&gt; which had some patches to enable these to build. I&#39;ve extracted the relevant patches here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://bluishcoder.co.nz/haskell/afrp.diff&quot;&gt;afrp.diff&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://bluishcoder.co.nz/haskell/wxfruit.diff&quot;&gt;wxfruit.diff&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;These can be applied by using &#39;patch&#39; from within the original wxFruit and Yampa distributions:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd afrp-0.4
patch -p1 &amp;amp;lt;afrp.diff
cd ../wxfruit-0.1
patch -p1 &amp;amp;lt;wxfruit.diff
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Alternatively you can download the already patched versions I&#39;ve made here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://bluishcoder.co.nz/haskell/afrp-0.4-patched.tar.bz2&quot;&gt;afrp-0.4-patched.tar.bz2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://bluishcoder.co.nz/haskell/wxfruit-0.1-patched.tar.bz2&quot;&gt;wxfruit-0.1-patched.tar.bz2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;To build and install the patched Yampa, the following should work:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd afrp-0.4
runhaskell Setup.hs configure
runhaskell Setup.hs build
sudo runhaskell Setup.hs install
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once this is done the wxFruit paddle example builds with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd ../wxfruit-0.1
ghc --make -farrows -o paddle paddle.hs
&lt;/code&gt;&lt;/pre&gt;
</content>
 </entry>
 
 
</feed>
