From: Ross Paterson Date: Thu, 29 Jul 2010 12:24:49 +0000 (+0000) Subject: move Monad and MonadFix instances for Either from mtl (proposal #4159) X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=6ecbc9c6c22c0c5de88d138abe10174bb09ba903;p=ghc-base.git move Monad and MonadFix instances for Either from mtl (proposal #4159) The Monad and MonadFix instances for Either (formerly in the mtl package) are moved to Control.Monad.Instances and Control.Monad.Fix respectively. The Monad instance is still an orphan, to retain Haskell 98 compatibility, but the MonadFix instance is together with its class. The Error constraint is removed from both instances, and the default definition of fail is used. --- diff --git a/Control/Monad/Fix.hs b/Control/Monad/Fix.hs index 57308bf..e8ba03d 100644 --- a/Control/Monad/Fix.hs +++ b/Control/Monad/Fix.hs @@ -78,9 +78,16 @@ instance MonadFix [] where instance MonadFix IO where mfix = fixIO +-- Prelude types with Monad instances in Control.Monad.Instances + instance MonadFix ((->) r) where mfix f = \ r -> let a = f a r in a +instance MonadFix (Either e) where + mfix f = let a = f (unRight a) in a + where unRight (Right x) = x + unRight (Left _) = error "mfix Either: Left" + #if defined(__GLASGOW_HASKELL__) instance MonadFix (ST s) where mfix = fixST diff --git a/Control/Monad/Instances.hs b/Control/Monad/Instances.hs index 6caf060..d41be4f 100644 --- a/Control/Monad/Instances.hs +++ b/Control/Monad/Instances.hs @@ -31,3 +31,8 @@ instance Functor ((,) a) where instance Functor (Either a) where fmap _ (Left x) = Left x fmap f (Right y) = Right (f y) + +instance Monad (Either e) where + return = Right + Left l >>= _ = Left l + Right r >>= k = k r