<div>Hrmm. This should probably be made consistent with the MonadPlus instance for IO, so</div>
<div> </div>
<div>> empty = ioError (userError "mzero")<br></div>
<div>Otherwse, I'm surprised this isn't already in the standard library. </div>
<div> </div>
<div>I'd suggest submitting it to libraries@.</div>
<div> </div>
<div>-Edward Kmett<br></div>
<div class="gmail_quote">On Thu, Jul 9, 2009 at 9:27 AM, Cristiano Paris <span dir="ltr"><<a href="mailto:cristiano.paris@gmail.com">cristiano.paris@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">As a joke, I wrote an instance of Alternative for IO actions:
<div><br></div>
<div>
<div>{-# LANGUAGE ScopedTypeVariables #-}</div>
<div>module Main where</div>
<div><br></div>
<div>import Control.Applicative</div>
<div>import Control.Exception</div>
<div><br></div>
<div>instance Alternative IO where</div>
<div> empty = undefined</div>
<div> x <|> y = handle (\ (_ :: SomeException) -> y) x</div>
<div><br></div>
<div>This would allow to write IO code which failsafes to a value if the previous computation failed, i.e.:</div>
<div><br></div>
<div>
<div>*Main Control.Applicative> undefined <|> print "Hello"</div>
<div>"Hello"</div>
<div>*Main Control.Applicative> print "Hello" <|> undefined</div>
<div>"Hello"</div>
<div><br></div>
<div>It seems a neat way to catch exception in some scenarios. What do you think? Why is not Alternative IO defined in Control.Applicative?</div></div>
<div><br></div>
<div>Thanks,</div>
<div><br></div><font color="#888888">
<div>Cristiano</div></font></div><br>_______________________________________________<br>Haskell-Cafe mailing list<br><a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br><a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br>