[Haskell-cafe] Documenting the impossible

Henning Thielemann lemming at henning-thielemann.de
Sat Jun 14 15:52:25 EDT 2008

On Sat, 14 Jun 2008, Andrew Coppin wrote:

> How about a {-# IMPOSSIBLE #-} pragma that documents the fact that a 
> particular point in the program *should* be unreachable?
> ...
> When compiled without optimisations, the pragma just causes an exception to 
> be thrown, rather like "error" does. When compiled with optimisations, the 
> whole case alternative is removed, and no code is generated for it. (And if 
> the impossible somehow happens... behaviour is undefined.) So you test your 
> program with your code compiled unoptimised, and when you're "sure" the 
> impossible can't happen, you tell the compiler to remove the check for it. 
> (Actually, it would possibly be a good idea to have a switch to turn this on 
> and off seperately if needed...)

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.

More information about the Haskell-Cafe mailing list