[Haskell-beginners] Errors and the compiler

Michael Orlitzky michael at orlitzky.com
Fri Oct 12 01:48:09 CEST 2012


On 10/11/2012 04:48 PM, Christopher Howard wrote:
> Point of curiosity: Does an error in a Haskell program have a negative
> impact on the compilers reasoning about or optimization of the code,
> even if said error is never reached in the control flow? For example,
> both of the following would have the same result:
> 
> code:
> --------
> case mExp of
>   Nothing -> expr
>   Just x -> f x
> 
> -- or...
> 
> if isNothing mExp
>   then expr
>   else f (fromJust mExp)
> --------
> 
> In my naive reasoning, I would think that in the latter case the use of
> fromJust introduces, from the compilers perspective, an additional
> possible outcome (an error being thrown). This presumably would
> complicate the compilers reasoning about the code, unless of course the
> compiler is smart enough to figure out that the error will never be reached.

Once upon a time, Ben Lippmeier showed me that,

  foo | something_always_true = bar
      | otherwise = error "this is impossible"

is slower than,

  foo | something_always_true = bar

so I am pessimistic about your example. I would presume GHC does better
on the first one.



More information about the Beginners mailing list