<br><br><div class="gmail_quote">On Thu, Jul 29, 2010 at 11:48 PM, Lyndon Maydwell <span dir="ltr">&lt;<a href="mailto:maydwell@gmail.com">maydwell@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
You cannot break out of a monad if all you have available to use are<br>
the monad typeclass functions, however there is nothing preventing an<br>
instance from being created that allows escape. Many of these escape<br>
methods come in the form of runX functions, but you can use<br>
constructors to break out with pattern matching if they are exposed.<br></blockquote><div><br></div><div>There is one case where you can break out of a monad without knowing which monad it is.  Well, kind of.  It&#39;s cheating in a way because it does force the use of the Identity monad.  Even if it&#39;s cheating, it&#39;s still very clever and interesting.</div>
<div><a href="http://okmij.org/ftp/Computation/lem.html">http://okmij.org/ftp/Computation/lem.html</a></div><div><br></div><div><a href="http://okmij.org/ftp/Computation/lem.html"></a>The specific function is:</div><span class="Apple-style-span" style="font-family: &#39;Times New Roman&#39;; font-size: medium; "><pre>
     &gt; purify :: (forall m. Monad m =&gt; ((a -&gt; m b) -&gt; m b)) -&gt; ((a-&gt;b)-&gt;b)
     &gt; purify f = \k -&gt; runIdentity (f (return . k))
</pre></span><div>We take some arbitrary monad &#39;m&#39; and escape from it.  Actually, the trick is that f must work for ALL monads.  So we pick just one that allows escape and apply f to it.  Here we picked Identity.  You could have picked Maybe, lists, and any of the others that allow escaping.</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
As far as I can tell, IO is more of an outlier in this regard.<br></blockquote><div><br></div><div>Yes I agree there.  And even with IO we have unsafePerformIO that lets you escape.</div><div><br></div><div>Jason</div></div>