[Haskell-cafe] Documenting the impossible

Andrew Coppin andrewcoppin at btinternet.com
Sun Jun 15 04:15:49 EDT 2008


Henning Thielemann wrote:
> I think it is another instance of mixing up errors and exceptions (you 
> know the haskellwiki pages ...)
>
> Since an 'error' marks a programming error (which should never occur) 
> it would not hurt the program if all 'error's are replaced by 
> 'undefined', an illegal memory access or any other misbehaviour. So 
> 'error' is exactly what you propose as IMPOSSIBLE pragma. A compiler 
> option for replacing all 'error's by nops would do want you want.

OK, so suppose I write a module that contains a function that accepts a 
number parameter, and that parameter must be greater than 2. I have no 
control over what value users of the library pass to my function. 
Suppose some client calls my function with 1 as an argument - is that an 
error, or an exception?

On the other hand, suppose I write the same function, but now it's not 
exported. So the only code that can possibly call this function is my 
own code. So I can [theoretically] guarantee it will never be called 
with the "wrong" argument [assuming the code I write isn't defective].

As far as I can tell, there's no way of making this distinction in 
Haskell code. You'd use "error" in both cases - or if you're feeling 
brave, remove it in the second case and hope you're right. I'm just 
saying it would be nice to be able to keep it in the code for 
maintainability's sake, but not have the runtime penalty for it once 
you're "sure" your code is safe.

It looks like Don's "assert" thing might be able to do this. [I had no 
idea this existed by the way...]

Hmm, this gives me a new idea for a feature request - how about a 
function that evaluates to the source code line number it was called 
from? ;-)



More information about the Haskell-Cafe mailing list