move Monad and MonadFix instances for Either from mtl (proposal #4159)
authorRoss Paterson <ross@soi.city.ac.uk>
Thu, 29 Jul 2010 12:24:49 +0000 (12:24 +0000)
committerRoss Paterson <ross@soi.city.ac.uk>
Thu, 29 Jul 2010 12:24:49 +0000 (12:24 +0000)
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.

Control/Monad/Fix.hs
Control/Monad/Instances.hs

index 57308bf..e8ba03d 100644 (file)
@@ -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
index 6caf060..d41be4f 100644 (file)
@@ -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