Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer is that this is the (somewhat surprising) semantics of <em>imprecise exceptions</em></p> <p>When pure code can be shown to evaluate to a <em>set</em> of exceptional values (i.e. the value of <code>error</code> or <code>undefined</code>, and explicitly <em>not</em> the kind of exceptions <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Exception.html">generated in IO</a>), then the language permits any value of that set to be returned. Exceptional values in Haskell are more like <code>NaN</code> in floating point code, rather than control-flow based exceptions in imperative languages.</p> <p>An occasional gotcha for even advanced Haskellers is a case such as:</p> <pre><code> case x of 1 -&gt; error "One" _ -&gt; error "Not one" </code></pre> <p>Since the code evaluates to a set of exceptions, GHC is free to pick one. With optimizations on, you may well find this always evaluates to "Not one".</p> <p>Why do we do this? Because otherwise we'd overly constrain the evaluation order of the language, e.g. we would have to fix a deterministic result for:</p> <pre><code> f (error "a") (error "b") </code></pre> <p>by for example, requiring that it be evaluated left-to-right if error values are present. Very un-Haskelly!</p> <p>Since we don't want to cripple the optimizations that can be done on our code just to support <code>error</code>, the solution is to specify that the result is a non-deterministic choice from the set of exceptional values: imprecise exceptions! In a way, all exceptions are returned, and one is chosen.</p> <p>Normally, you don't care - an exception is an exception - unless you care about the string inside the exception, in which case using <code>error</code> to debug is highly confusing.</p> <hr> <p>References: <a href="http://research.microsoft.com/en-us/um/people/simonpj/papers/imprecise-exn.htm">A semantics for imprecise exceptions</a>, Simon Peyton Jones, Alastair Reid, Tony Hoare, Simon Marlow, Fergus Henderson. Proc Programming Languages Design and Implementation (PLDI'99), Atlanta. (<a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.50.1525&amp;rep=rep1&amp;type=pdf">PDF</a>)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload