<br><br><div class="gmail_quote">On Thu, Jul 29, 2010 at 11:48 PM, Lyndon Maydwell <span dir="ltr"><<a href="mailto:maydwell@gmail.com">maydwell@gmail.com</a>></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's cheating in a way because it does force the use of the Identity monad. Even if it's cheating, it'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: 'Times New Roman'; font-size: medium; "><pre>
> purify :: (forall m. Monad m => ((a -> m b) -> m b)) -> ((a->b)->b)
> purify f = \k -> runIdentity (f (return . k))
</pre></span><div>We take some arbitrary monad 'm' 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>