
On Wed, Sep 16, 2009 at 01:19:53AM +0300, Michael Snoyman wrote:
On Tue, Sep 15, 2009 at 11:49 PM, Brandon S. Allbery KF8NH < allbery@ece.cmu.edu> wrote:
I'm looking at the Control.Monad documentation ( http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Mon...), and it doesn't seem that IO is an instance of MonadPlus. I get the same results with a simple code check. Can you clarify? Calling lookup from IO is a common use case for me.
That's a bit of stupidity I wish would be fixed; for some bizarre reason, the instance is in Control.Monad.Error.
And according to the documentation, that instance has a broken version of mzero. So it seems the argument of use MonadPlus because fail is not always defined properly doesn't exactly hold much water here. Can you tell me why you would still recommend representing lookup failure with the mzero function instead of the seemingly more aptly named and more available fail function?
The IO instance of mzero is only broken in the sense that it does not satisfy the law m >> mzero === mzero since the effects of m will happen on the left but not on the right. But this is to be expected with the IO monad: if you have to perform some I/O effects in order to find out whether the computation fails or not, you can't expect to roll back the effects once you do fail. In practice I don't think this would be much of an issue. But anyway, the reason I recommend not using fail is because it is an inelegant, unprincipled hack. If you have no problem with inelegant, unprincipled hacks then I guess I won't be able to convince you. =) Also, if you haven't already, you should read the email from Conor McBride in this same thread: after reading his email I'm now of the opinion that you shouldn't even use MonadPlus, but simply use Maybe. -Brent