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

Jón Fairbairn jon.fairbairn at cl.cam.ac.uk
Wed Nov 15 09:48:27 EST 2006


Simon Peyton-Jones <simonpj at microsoft.com> writes:

> | 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.
> 
> I don't agree.  My programs have invariants that I can't
> always express in a way that the type system can
> understand. E.g. I know that a variable is in scope, so
> searching for it in an environment can't fail:
>         head [ v | (n,v) <- env, n==target ] (Maybe if I had
> an Oleg implant I could express all this in the type system
> -- but I don't.)

But instead of “blah (head [ v | (n,v) <- env, n==target ])
blah”, you could write

blah the_v_in_scope blah
where (the_v_in_scope:_) =  [ v | (n,v) <- env, n==target ]

and get a source-code located error message, couldn't you?
It's not very high tech, but it's what you would write if
head didn't exist, and it doesn't seem /that/ great an
imposition. 

-- 
Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk




More information about the Haskell-Cafe mailing list