More Haskell

Posted on January 5, 2011 with tags . See the previous or next posts.

It’s no secret to the people I around me that over the past two years I’ve become a Haskell fan, to put it mildly. I mean, what’s not to like? Just in the past month:

  • a conceptual one-line change (map ⇒ parMap, but the actual commit is a big longer as it splits a foldl in two) and re-linking with the threaded runtime resulted in the balancing routines used in the ganeti-htools code to achieve between 60% and 80% increase with the number of cores; without any locking primitives, thread synchronisation, etc.; and yet the algorithm is not at all slower when run single-threaded, which is something very hard to do in other, mainstream languages
  • a recent test project of mine introduced me to the web framework Yesod, which gives statically-type checked URL construction and referencing, without requiring me to do any value escaping, URL component construction, etc.; comparing that to what I’m used from other languages (though admitedly I don’t have too much experience with web development), it’s unbelievable how well the type system plays even with the untyped world of the Web applications

The downside is that whenever I feel “Ok, now I kind of know my way around Haskell”, I learn of another thing that completely resets how I feel about my Haskell understanding. The most recent example is Template Haskell.

I mean, there are people who already blame Haskell for its arcane syntax, which I like. But Template Haskell goes a step forward and allows you to use a Haskell library to write Haskell code, with an even more contorted syntax—or at least that how it looks for now to my eyes. Gladly this is not easily doable in a recursive way (meta-TH code writing TH code that will write Haskell code), otherwise heads would explode (at least mine).

But even though it seems very complicated, I feel that the power that it enables (a “macro” system that generates code that will be statically checked) is removing one of the last big advantages that (for me) Python had over Haskell: it’s easy to do class modification in Python and introspection (e.g. via metaclasses) to automate boiler plate away, but it’s of course not type safe (so unittests galore). With TH, you can do the same customization of data types (and not only) and compile-time introspection (called reification) to alter (some) of the properties of the respective type, for example automatic instance derivation, so one less reason to say “this particular problem is harder to do in Haskell than in other languages”.

I haven’t yet used TH for my own code and until I actually use it extensively I won’t be sure it’s actually a good replacement for “throw a __metaclass__ in there and automate the boring parts away”. But in any case, it looks to be yet another awesome tool in the Haskell programmer’s toolbox, which I can’t wait to use.