Thanks for excellent explanation!&nbsp; Examples really help.<br><br>So, in general&nbsp; &#39;fail&#39; behavior will differ from monad to monad. <br>In this example:<br><br>divBy :: Monad m =&gt; Int -&gt; Int -&gt; m Int<br>divBy a 0 = fail &quot;div by zero&quot;
<br>divBy a b = return (a `div` b)<br><br>Default &#39;fail&#39; implementation in Monad class will be:<br>*DivBy&gt; divBy 5 0<br>Loading package haskell98-1.0 ... linking ... done.<br>*** Exception: user error (div by zero)
<br><br>And when explicitly defining monad as Maybe it will be different:<br>*DivBy&gt; divBy 5 0::Maybe Int<br>Nothing<br><br>I am curious if it is possible to&nbsp; &#39;cast&#39; divBy to List, Identity, other monads? How?<br>
<br><div><span class="gmail_quote"><br>On 6/6/07, <b class="gmail_sendername">Tillmann Rendel</b> &lt;<a href="mailto:rendel@rbg.informatik.tu-darmstadt.de">rendel@rbg.informatik.tu-darmstadt.de</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Dmitri O.Kondratiev wrote:<br>&gt; Monad class contains declaration<br>&gt;<br>&gt; *fail* :: String -&gt; m a<br>&gt;<br>&gt; and provides default implementation for &#39;fail&#39; as:<br>&gt;<br>&gt; fail s = error s<br>
&gt;<br>&gt; On the other hand Prelude defines:<br>&gt; *<br>&gt; error* :: String -&gt; a<br>&gt;<br>&gt; which stops execution and displays an error message.<br>&gt;<br>&gt; Questions:<br>&gt; 1) What value and type &#39;error&#39; actually returns in:
<br>&gt; error &quot;some message&quot; ?<br><br>For practical purposes:<br><br>&nbsp;&nbsp; typechecking: every type the context asks for.<br>&nbsp;&nbsp; execution: no value, because execution stops.<br><br>For theoretical purposes, error could be implemented by
<br><br>&nbsp;&nbsp; error :: String -&gt; a<br>&nbsp;&nbsp; error msg = error msg<br><br>with the extra-semantical magical side effect of printing msg and<br>aborting execution.<br><br>&gt; 2) How declaration<br>&gt; String -&gt; m a<br>&gt; matches with
<br>&gt; String -&gt; a ?<br><br>Alpha renaming to fresh variables yields<br><br>&nbsp;&nbsp; String -&gt; b c<br>&nbsp;&nbsp; String -&gt; d<br><br>wich unifies by taking d := b c.<br><br>&gt; 3) In Maybe monad:<br>&gt; fail = Nothing<br>&gt;
<br>&gt; When and how &#39;fail&#39; is used in Maybe monad?<br><br>The default fail implementation is not very clever. If something fails,<br>execution is aborted and the user is confronted with some error message.<br>Some monads support richer error handling schemes. The maybe monad
<br>encodes a succeeding computation with Just it&#39;s result, and a failed<br>computation with Nothing.<br><br>An example:<br><br>-- divBy is a possible failing computation in some monad<br>divBy :: Monad m =&gt; Int -&gt; Int -&gt; m Int
<br>divBy a 0 = fail &quot;div by zero&quot;<br>divBy a b = return (a `div` b)<br><br>-- div by three succeeds<br>15 `divBy` 3 :: Maybe Int&nbsp;&nbsp; ~~&gt;&nbsp;&nbsp; Just 5<br><br>-- div by zero fails<br>15 `divBy` 0 :: Maybe Int&nbsp;&nbsp; ~~&gt;&nbsp;&nbsp; Nothing
<br><br>-- divByAll is a shortcut for a list of divBy&#39;s<br>divByAll :: Monad m =&gt; Int -&gt; [Int] -&gt; [m Int]<br>divByAll a bs = map (divBy a) bs<br><br>-- div by all returns a list of computations<br>15 `divByAll` [3, 0] :: [Maybe Int]&nbsp;&nbsp; ~~&gt;&nbsp;&nbsp; [Just 5, Nothing]
<br><br>-- sequence succeeds if all computations in a list succeeds<br>sequence (15 `divByAll` [3, 0]) :: Maybe [Int]&nbsp;&nbsp; ~~&gt;&nbsp;&nbsp; Nothing<br>sequence (15 `divByAll` [3, 5]) :: Maybe [Int]&nbsp;&nbsp; ~~&gt;&nbsp;&nbsp; Just [5, 3]<br><br>divBy, divByAll, sequence do not know anything about Maybe, they work
<br>for all monads, because they only use &gt;&gt;=, fail and return.<br><br>The idea is that Monad defines some general interface for computations,<br>and the various Monad instances define the exact behaviour. Maybe&#39;s
<br>behaviour is: if a subcomputation fails, the whole computation fails.<br><br>There are other monads. consider the list monad, and it&#39;s behaviour: if<br>a subcomputation fails, backtrack and try some other alternative.
<br><br>Tillmann<br></blockquote></div><br><br clear="all"><br><br>