[Haskell-cafe] haskell programming guidelines

Cale Gibbard cgibbard at gmail.com
Fri Feb 24 12:39:27 EST 2006

On 20/02/06, John Meacham <john at repetae.net> wrote:
> There is a more straightforward way to get localized error messages
> rather than using 'maybe' and hand-writing an appropriate error, and
> that is to rely on irrefutable bindings.
> f x = ... y ... where
>         Just y = Map.lookup x theMap
> now if the lookup fails you automatically get an error message pointing
> to the exact line number of the failure. or if the failure message of
> the routine is more important than the source location you can do
> f x = ... y ... where
>         Identity y = Map.lookup x theMap
> it is anoying you have to make a choice between these two possibilities,
> but this can be mitigated with CPP magic or the SRCLOC_ANNOTATE pragma.
>         John

I look at the above as generating a proof obligation for me as the
programmer that the lookup will never fail, or at least the ability to
convince myself. :) If you want to handle errors, you should actually
handle them, not let your users get "Irrefutable pattern failed"
messages. Also, if someone else later comes along and wants to catch
that error, they have to either do it in IO, which can be fiddly if
the error occurs deep in the evaluation of some structure, or they
refactor your code so that it returns the error explicitly. Sure,
irrefutable pattern matches are useful, but they shouldn't be used if
you expect they'll ever fail.

 - Cale

More information about the Haskell-Cafe mailing list