Thursday, February 18, 2010

A Haskell Journey

Over the last couple of years, I've been using Haskell (oddly enough) as a scripting/shell language, somewhat akin to bash, to tie together various bits and pieces of code written in other languages. (See for some shell like utilities).

One of my goals for this year is to really master Haskell so I can use it as a primary language. As I dig in, I find there are two levels of Haskell.

Level 1 consists of algebraic data types, pure (and lazy) functions, typeclasses and modules. Someone fluent in another language can (relatively) easily wrap their heads around this portion of the language and start programming. There are plenty of tutorials and books (including "Real World Haskell") that teach this style of Haskell usage.

But, sooner or later one comes across code that is written in "level 2" Haskell - making heavy use of Monads, Monoids, Arrows and all the other Category Theory goodness. Monads get an abnormal mindshare among would-be Haskell developers but there is more to Haskell than Monads. No one has really written a comprehensive guide to this bit of Haskell. The nearest we have is the TypeClassopedia (warning, PDF) but that is more a collection of links for further reading than a detailed exposition. The Wikibook is uneven. RWH does a hop-skip-and jump over these bits - a correct decision given the focus and size of the book.

What we really need is an "Advanced Haskell" book which assumes a knowledge of Level 1 Haskell and then lays out the CT bits in an orderly fashion. Monads for example, are best understood from a Category Theory perspective than through some tortured analogy to Space Stations or Elephants or whatever. Some exposition of Type Theory would help too - a knowledge of kinds, for example is very useful to decode some of the advanced bits ( TAPL's last chapters have a good exposition iirc).

In the absence of an "Advanced Haskell" book, the best option is to read the various papers, trawl the mailing lists and so on for answers to specific questions. I've learned some Category Theory before (and am fairly comfortable with Type Theory) so I haven't found this to be particularly hard but I can see how it could be (very) hard for someone without this background. Mastery of this level would be when I can code *fluently* with Comonads and such. I am not there yet.

That said, Haskell is the most elegant language I've ever used and I plan to write a lot of code in it. I plan to open source some Haskell code over the next couple of months, so we'll see how much I've really understood!


Dress Guard said...

I couldn't agree more!!!

Anonymous said...

I agree completely. I finally think I have command over monads and monoids. I have the research papers on arrows, applicative functors and comonads lying around, and I poke at them. I'm not particularly frightened by them but at the same time I am not a researcher in this material (yet) so I find it hard to make headway in my spare time. The Real World Haskell book did a lot to get me to a point where I could actually write useful programs in Haskell, but I sense the same frontier and that, though I am getting to a point where I'm pretty comfortable with layer 1, layer 2 is still extremely new territory for me. It would be great if there were a category theory catalog for Haskell with actually pertinent useful examples. Comonads, for example, seem very interesting to me, but there are only two useful examples of them I've found online.

Ravi said...

Great Comment. (And interesting blog! I've added your blog to my reader)

Ramkumar Ramachandra said...

Incidentally, what's wrong with Real World Haskell? I think it finishes off level 1 pretty quickly, and dives straight into level 2. I've myself been on level 1 for the past few months, and am on the verge of jumping to level 2.

Ravi said...

"Incidentally, what's wrong with Real World Haskell?"

Who said there is anything wrong? It does a fine job of introducing Haskell. A guide to "deep" Haskell it certainly is not.

" I think it finishes off level 1 pretty quickly, and dives straight into level 2."

No it doesn't "Level 2" as identified *in this blog entry* is hardly dealt with in RWH. Compare the treatments of Monads (to pick something from CT at random) in RWH vs in Wadler's Or Peyton Jones's papers. Or how it is dealt with in a Category Theory textbook. Know whereof ye speak.

" I've myself been on level 1 for the past few months, and am on the verge of jumping to level 2."

IOW, you don't have any experience of what level 2 code looks like yet ;-).

Keep coding, and you will find out RWH doesn't cover it. ;-)

Ramkumar Ramachandra said...

Yeah, I suppose so.

Anonymous said...

From a comment on reddit.

"The third level is advanced usage of the type system, like, say, Control.Monad.Exception or session types, as well as mind bending use of level 1+2 features like say uu-parsinglib, and, in general, continuations.

Level four would then be apprenticeship to the title of "Oleg".

:-P :-P

Sanjoy Das said...

RWH isn't a good book, IMO; phrases like "we cannot *assign* a value to x twice" (my emphasis) are likely to confuse beginners. I'll be impressed when I see a book that begins by defining sum and product types.