Posts Tagged ‘haskell’

CamHac: Haskell Hackathon in Cambridge, UK

Friday, August 19th, 2011

From August 12-14, 2011, Simon Marlow organised a Haskell Hackathon at Homerton College in Cambridge. Needless to say, several GhentFPG members were interested to attend. In the end, a party of four (Jeroen Janssen, Jasper Van Der Jeugt, Bart Coppens and myself) made the crossing to the UK. It was a smooth ride, even though the train in front of us stopped in the middle of the Channel Tunnel and we had to wait for about 50 minutes before our train was allowed to proceed.

We stayed at the Cambridge YHA, in a room we shared with Simon Meijer and some unnamed Belgian guy from Moeskroen, whose Dutch resembled West-Flemish quite closely. The latter was there to study during the day and party during the night. And he was not happy with the fact that we got up around 8:00 AM :-D I only discovered a shower with warm water on Sunday, having taken cold shower — I mean really cold — the previous two days. But the breakfast more than made up for that.

When signing up to CamHac, I had the plan to work on HaBench, or rather, see how Fibon could be made into what we envisioned during BelHac as a Haskell benchmark suite. However, research requirements dictated otherwise. Given that we are collaborating with researchers from the KULeuven on a project using COLE and active learning to build models for the effect of optimisation sequences, and that the COLE framework requires access to the supercomputer backend at Ghent University, I decided to write a Haskell web application using Snap that would allow the submission of optimisation sequences and get the results for the objective functions (speedup, compilation time, code size, …) back for the used benchmark suite. Having no previous experience with Snap, I found the framework to be easy to use. The core of the application was finished by Sunday afternoon; on the way home I added a watchdog thread to update the database with finished experiments.

I had a ton of fun those three days, learned a lot, and — fingers crossed — started to grok monads a bit better. The event was full, as 72 people registered. I am not sure everybody turned up, but the room was crowded at all times. And a lot of work was done, see the post-hackathon report.

The first Belgian Haskell Hackathon

Sunday, November 7th, 2010

During the weekend of November 5-7, 2010, The Ghent Functional Programming Group and the ZeusWPI organised a Haskell hackathon, the first of these events to take place on Belgian soil.

The organisers

Thanks to Ghent University, we had obtained a nice venue: a refurbished coal power plant, of which we could use the upper floor, where the huge silos for storing the coals were located.

The Therminal

The event entailed three days. On Friday we had the registration, the hey-cool-to-see-you-are-here-too and general get-to-know each other, followed by a very nice talk by Miran Lipovača, the author of the ‘Learn you a Haskell for great good’ book (soon to be available from your favorite bookshop(s)). From 5 p.m. onwards, we had the Functional Programming in Industry symposium, where Duncan Coutts, Romain and Donald Steward entertained the masses with great stories about using FP in their daily work. Since we be engineers, and the tradition of our department states that talks in the Jozef Plateau-room are followed by a reception (at least, when people defend their PhD. this is the case, but let’s ignore that little trivia for the moment), we also fed the masses with a bunch of excellent (220) sandwiches. Sadly, some were left over, even though the local folks ate more than (twice) their share once it became clear that there would be leftovers. No worries, we’re well trained, given the number of PhD’s defended every year.

The weekend itself was pretty much devoted to the actual hacking. Kenneth (boegel) and I found some support for our HaBench effort in the person of Yuriy Kashnikov. After a round of project introductions, the work began in earnest only to be briefly interrupted by the announcement that food was available, kindly provided by Het Stokbroodje (we paid, of course, but still, the food was yummy and they delivered on both Saturday and Sunday). The evening saw a march with a few hackers to Julien, Ghent’s moat famous french fries place (I’m told).

On Sunday, hacking continued and in the afternoon, a few 5-minute lightning talks were held by people who considered having made significant progress. I briefly talked about HaBench (slides (PDF)). The general reception was pretty good, I believe. Clearly, a new benchmark suite is highly desirable. Finally, we wrapped up and cleaned shop before the housekeeper came round to chase us out again.

The Geeks

Moving hMollom to a monadic base

Monday, April 12th, 2010

In a move to finally grok monads and monad stack a bit better, I decided to move the hMollom code to a monad based implementation. The exported functions now execute in the MollomMonad, which at this point is defined as follows:


type MollomState = ReaderT MollomConfiguration (StateT MollomServerList IO)
type MollomMonad = ErrorT MollomError (StateT (Maybe SessionID) MollomState)

The main concept of the code remains the same, each exported function calls the service function. The latter returns a MollomState a, to capture the fact that during the Mollom request, we potentially need to update the list of servers we can query with API calls. This result is then lifted into the MollomMonad stack by using the following function.


returnStateT a = StateT $ \s -> liftM (flip (,) s) a

This is illustrated in the following control flow figure (for a full sized image, follow the link to flickr):

Control flow in hMollom

The error handling is done by usage of the ErrorT monad transformer, which add the possibility to return a Left MollomError as function value, indicating a fault during the handling of the request. At this point, I have not added a decent runMollomMonad function, as I am still pondering which functions may be most useful for the users of this library. However, you can easily employ the following function, where defaultMollomConfiguration :: MollomConfiguration and we start without any servers that are available.


(\m -> (runStateT $ (runReaderT $ (runStateT . runErrorT $ m) (Nothing) ) defaultMollomConfig) UninitialisedServerList)

As usual, the code is available from both the GitHub repository, as well as from Hackage. All feedback is welcome. Thanks to #haskell and #ghentfpg for bearing with my questions.

hMollom

Friday, February 26th, 2010

You may or may not be aware of the excellent anti-spam service provided by Mollom, founded by two friends and colleagues, Dries Buytaert and Benjamin Schrauwen. Mollom provides an API to program against, allowing support libraries to be written in a variety of languages and for numerous platforms. Top-notch examples are the Drupal plugin (obviously :-) and the WordPress plugin. Next to that there are a number of libraries for Java, PHP5, Ruby, Python, etc. Sadly, a Haskell library was lacking.

Given that I enjoy programming in Haskell and need to sharpen my Haskell-fu, I set out to write a library for interfacing with the Mollom API. The library is far from finished, but it is functional at this point, so if you have Mollom keys, you can call the services and fight the spam war on your Haskell driven website.

I maintain two repositories where you can get the library:

At this point the library has been given version 0.1. So it is still lacking quite a lot of features. Stuff that is on the immediate TODO list:

  • Add fault handling
  • Add server list refreshing
  • Encapsulate the state in a monad

Any feedback is appreciated.

Haskell wraper for Mollom

Sunday, October 5th, 2008

I have been a Haskell fan since I first get introduced to the language, in the now deprecated course taught by Koen De Bosschere when I took my final year before graduating as a licentiate in computer science (which would now be a MsC. in CS). Due to a lack of time, mostly, I never proceeded very far beyond the basics, not even when I was a teaching assistant for this course. Still, with Mollom having been released, the Haskell application database Hackage finally coming into adulthood, or some form of it, I deemed the time ripe to attempt writing a wrapper for Mollom.

That said, first thing to do is check Hackage and find stuff we can reuse. haxr immediately comes to mind for the XMLRPC stuff. Other packages I am using are base64-string, crypto, configfile,
Installing the above will download and install a number of other libraries on which these depend. Still, Hackage seems to do the trick just fine, installing everything I needed.

Aligning data from separate files using Haskell

Friday, May 30th, 2008

If you need to align the data from multiple files fast, Haskell is the way to go. For example, given a number of files, with an equal number of lines, each containing a number. How can we easily create a single file where each line contains items from each of the original files, separated by a separator of your choice?

One way to do it is like this.

  1. module Main() where
  2. import Control.Monad(mapM)
  3. import Data.List(transpose, intersperse)
  4. import System.Environment(getArgs)
  5. import System.IO
  6. main = do
  7. header:lookups:files <- getArgs
  8. handles <- mapM (\x -> openFile x ReadMode) files
  9. contents <- mapM hGetContents handles
  10. let output = map (concat . (\x -> intersperse “:” (lookups:x))) $ transpose $ map lines contents
  11. putStrLn header
  12. mapM_ putStrLn output

First of all, I think it is good practice to only import the things you need. Hence the e.g., import Control.Monad(mapM). Yet, I can’t seem to import IOMode from System.IO so there I imported the lot.
Second, it is paramount that each line inside a do statement has the same type, namely IO () in this case. This means we use a mapM_ in the last statement (mapM_ :: (Monad m) => (a -> m b) -> [a] -> m (), where m becomes IO).

Essentially, we handle all files simultaneously, by mapping functions manipulating them into the IO monad using mapM (the type is mapM :: (Monad m) => (a -> m b) -> [a] -> m [b], where once again m becomes IO as we’re executing actions in main :: IO ()). For example, we read the contents of each file in a lazy manner using hGetContents, which results in something that has the type IO [String]. Since we assigned this to the contents name, using <-, contents :: [String]. So, we have a list of Strings, representing the content of each file. Using pure functions, we get the data in the shape we like: we split the contents of each file with lines, and transpose the result. Then we can inject the separator using intersperse and we concatenate the result. Finally, we need to show the result. We put everything to stdout, using putStrLn :: String -> IO (). Because we like a header in our new file, we print it out first. Then we print each line of the new file.

Admittedly, I used to have a hard time grokking this, but since I looked at the Write yourself a Scheme in 48 hours things have picked up.