Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The short answer is that the Monad class in Haskell adds the <code>fail</code> operation to the original mathematical idea of monads, which makes it somewhat controversial how to make the Either type into a (Haskell) <code>Monad</code>, because there are many ways to do it.</p> <p>There are several implementations floating around that do different things. The 3 basic approaches that I'm aware of are:</p> <ul> <li><code>fail = Left</code>. This seems to be what most people expect, but it actually can't be done in strict Haskell 98. The instance would have to be declared as <code>instance Monad (Either String)</code>, which is not legal under H98 because it mentions a particular type for one of <code>Either</code>s parameters (in GHC, the FlexibleInstances extension would cause the compiler to accept it).</li> <li>Ignore <code>fail</code>, using the default implementation which just calls <code>error</code>. This is what's happening in your example. This version has the advantage of being H98 compliant, but the disadvantage of being rather surprising to the user (with the surprise coming at runtime).</li> <li>The <code>fail</code> implementation calls some other class to convert a String into whatever type. This is done in MTL's <code>Control.Monad.Error</code> module, which declares <code>instance Error e =&gt; Monad (Either e)</code>. In this implementation, <code>fail msg = Left (strMsg msg)</code>. This one is again legal H98, and again occasionally surprising to users because it introduces another type class. In contrast to the last example though, the surprise comes at compile time.</li> </ul>
 

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