16/02/2011
That’s the question.
In designing a couple libraries the question of the proper use of the Monad class’s fail method has come up more than once.
On the one hand, many people consider fail to be a wart on the face of an otherwise pure implementation of an elegant construction borrowed from category theory. It’s as if someone defined scissors as
“two metal blades with handles, attached by a pivot… plus a bandaid in case you cut yourself”.
But on the other hand many instances of Monad have a logical and intuitive way of encapsulating failure, whether it be Nothing in Maybe, or the empty list in []. And for monad’s that have no reasonable way of encapsulating failure, a default method (raising an exception) can be used.
Read the rest of this article »
2/01/2011
If the title hasn't scared you off yet, here's the story: I was hacking on someone else's code on the plane and trying to wrap my head around some Applicative class code; in particular the code in question used the Applicative instance for ((->) a) i.e. functions. This turned my brain to cream-of-wheat and I had to take a break.
If you aren't familiar with Applicative Functors, they are somewhere in between the familiar and simple Functor class and the Monad class; an abstraction for function application with a context. Check out here for more.
Read the rest of this article »
14/12/2010
I’ve just started trying to learn and debug Template Haskell and found it a bit rough trying to explore TH interactively, but a couple of things have helped.
First and most obvious, we can use runQ to see the abstract TH syntax which a quasi-quoted expression expands to:
Prelude> :set -XTemplateHaskell
Prelude> :m + Language.Haskell.TH
Prelude Language.Haskell.TH> runQ [| \a -> a+1 |]
LamE [VarP a_1] (InfixE (Just (VarE a_1)) (VarE GHC.Num.+) (Just (LitE (IntegerL 1))))
Read the rest of this article »