[Haskell-cafe] Re: Debugging partial functions by the rules

Donald Bruce Stewart dons at cse.unsw.edu.au
Wed Nov 15 03:44:25 EST 2006

> Donald Bruce Stewart wrote:
> > So all this talk of locating head [] and fromJust failures got me
> > thinking:
> >
> >     Couldn't we just use rewrite rules to rewrite *transparently*
> >     all uses of fromJust to safeFromJust, tagging the call site
> >     with a location?
> I'm sorry for shifting the topic: I'm wondering if, rather than trying
> to make an error message more informative, we ought to make sure that
> no error will ever arise? 
> The fromJust and `head of empty list' errors are totally equivalent to
> the dereferencing of zero pointer in C++ or NullPointerException in
> Java. It pains me to see that exactly the same problem arises in
> Haskell -- keeping in mind that already in C++ and Java one may
> exterminate these errors given right encapsulations. Languages like
> Cyclone or Cw use the type system to eliminate such errors. Surely
> Haskell can do something about this?

Yes, these techniques are fairly well known now, and hopefully some of
the more experienced Haskellers are using them (I certainly use the
non-empty list tricks). Any anyone with more than 6 months Haskell knows
to avoid fromJust.

The problem I see is that head/fromJust errors are usually caused by
*beginner* Haskellers, who don't know the techniques for statically
avoiding them.

One solution would be to deprecate fromJust (we recently decided not to
add fromLeft/Right for the same reasons). Having a compiler warning is a
good way to encourage good behaviour :) 

But it seems hardly likely that head will be deprecated any time soon,
and we have no support for checked non-empty lists in the base libraries.

So how do we help out the beginners, other than warning about fromJust,
and providing a useful error message as we can, for when they just go
ahead and use head anyway?

-- Don

More information about the Haskell-Cafe mailing list