[Haskell-cafe] Maybe and partial functions

Steve Downey sdowney at gmail.com
Tue Mar 13 20:51:54 EDT 2007

This isn't just a question about Haskell. It applies to any language
with an exception mechanism, including C++ and Java. Even C (segv is
an exception mechanism...)
The question is really how to communicate failure to the caller, in a
way the caller can not ignore, without unduely inconvienencing the
So there is a two pronged test. Could the caller have anticipated the
failure, and should the caller anticipate failure.

head of an empty list is a good example of the first prong. It is easy
to check for. In many cases it can be shown to be impossible. So
burdoning all callers with dealing with failure explicitly is not
good. An exception is good here.

Contrast that with lookup in a table. The caller has no reasonable way
of telling before hand that the call will be succesful.  The caller
should be prepared to handle lookup failure, so a Maybe is good here.
In C++ a null value of some kind is the norm.

One of the questions that C++ and Java answered was whether or not a
caller should be forced to deal with an exception explicitly by the
called function. The answer has turned out to be no, for somewhat
different reasons in both languages. But it is something that Haskell
should consider.

Particularly Java library integrator's discovery that strongly typed
exceptions do not scale. Frameworks like Tomcat seem to have found
that there isn't much middle ground between exceptions that are
handled close to the library, and just barely escape the library
interface, and the generic 'something went wrong somewhere',
'abort,retry,fail?' kind of error.

Of course, for library reuse, this is secondary to the incipient
package nameing disaster. Not tertiary, since it is at the heart of
lib composability....

On 3/12/07, Dougal Stanton <ithika at gmail.com> wrote:
> The Maybe construction is very useful for explicitly handling
> circumstances where the function cannot produce a sensible answer.
> But how far should this notion be taken? When you're writing a function
> which you realise may not produce what you want, in what circumstances
> would you use a Maybe, and when would you just throw an error?
> I'm wondering both in terms of good engineering practise and also for
> correctness. Should elementary partial functions like
> > 5 `div` 0
> or
> > head []
> return Nothing? I guess it's a bit of a silly suggestion, but it helps
> to highlight why we use Maybe in the first place. So --- where's the
> cutoff point in your code?
> Cheers,
> D.
> --
> Dougal Stanton
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list