entries friends calendar user info My Website Previous Previous Next Next
Anthony Bailey's blog - Mo' monads
anthonybailey
[info]anthonybailey
Add to Memories
Tell a Friend
Mo' monads
Andrew Birkett tries to sneak a monad passed us over on his software engineering blog.

(This is of no interest to most of my usual tiny audience, and the main reason I'm responding by making an entry here rather than a comment over there is that said comments are stripping tags and making it hard to read my example code. Plus, I get to ping his trackback, and that always makes me feel like a proper blogger.)

OK, so I approve of where he's coming from with the whole stealth-abstraction thing, and overall this is yet another bit of Birkett writing that added value to my world.

Yet there's something about the example as presented that I don't like much. I'll try to illustrate what it is that unsettles me by being unduly mean to the precise phrasing of this particular statement:
Instead, the only thing you can do with a Maybe is pass it a couple of function pointers called "do this when you have data" and "do this when you're empty".
But what can the Maybe object usefully do when it's empty? It has no information other than the context that you pass it. "Do" is somehow too active: really it's more like "allow this to happen". Similarly, when the Maybe is valid I think what's really going on is not "do this" but rather "tell your data to do this". Making the Maybe an active participant is good, but its participation should only be considered to go so far as expressing the thing that it knows, i.e. "I have data - here it is" or "I have no data".

This affects my preferred definition of the method the Maybe would use to participate. Andrew suggests you give it an arbitrary pair of methods. I guess I think that's too general.

In a functional language, I believe I prefer
case : Maybe α → (α → β) → β → β
where
case (Yes x) f a = f x
and
case No f a = a
(I note the similarity to standard fold methods. I think I recall seeing that analogy made explicit the very first time anybody ever showed me their monads. [Oh, let me have that one, I've been very restrained. It was enough I could do not to promote a similar pun to the subject, and Andrew did manage to tell the reader they hadn't noticed they were playing with their monads over on his page...] The University surprised us with the notion of a monad in an exam question at the end of the first year of my theory-happy undergrad CompSci degree.)

In a language such as C++, where functions and closures and the like are merely second-class citizens, I think I want the method Lift on the templated class Maybe<A> to take a (pointer to a) function on A, and simply return a bool indicating whether it got to apply it or not. Using this in client code then becomes
Maybe<A> foo;
void methodToLift(A);

foo.Lift(&methodToLift));
in the typical case where there's no alternative action to perform when data is absent, and
if (!foo.Lift(&methodToLift)) { alternative_action }
when there is. (In turn, a language with nicer idiomatic phrasing (such as Perl) would allow the latter case to become
foo.Lift(&methodToLift) or alternative_action;
which reads even more naturally.)

Tags:
Current Mood: geeky

about this journal
License:Public Domain Dedication
Feed:RSS feed
Contact details
Blog - permalink
Tumblelog - Anthony uncut
My views, not Amazon's
tags
page summary