add Functor and Monad instances for Prelude types
authorRoss Paterson <ross@soi.city.ac.uk>
Mon, 10 Apr 2006 11:14:43 +0000 (11:14 +0000)
committerRoss Paterson <ross@soi.city.ac.uk>
Mon, 10 Apr 2006 11:14:43 +0000 (11:14 +0000)
Control/Monad/Fix.hs
Control/Monad/Instances.hs [new file with mode: 0644]
base.cabal
package.conf.in

index a24d9af..e04a7f6 100644 (file)
@@ -24,6 +24,7 @@ module Control.Monad.Fix (
 
 import Prelude
 import System.IO
+import Control.Monad.Instances ()
 
 -- | @'fix' f@ is the least fixed point of the function @f@,
 -- i.e. the least defined @x@ such that @f x = x@.
@@ -71,3 +72,6 @@ instance MonadFix [] where
 -- IO:
 instance MonadFix IO where
     mfix = fixIO 
+
+instance MonadFix ((->) r) where
+    mfix f = \ r -> let a = f a r in a
diff --git a/Control/Monad/Instances.hs b/Control/Monad/Instances.hs
new file mode 100644 (file)
index 0000000..1641e03
--- /dev/null
@@ -0,0 +1,30 @@
+-----------------------------------------------------------------------------
+-- |
+-- Module      :  Control.Monad.Instances
+-- Copyright   :  (c) The University of Glasgow 2001
+-- License     :  BSD-style (see the file libraries/base/LICENSE)
+--
+-- Maintainer  :  libraries@haskell.org
+-- Stability   :  provisional
+-- Portability :  portable
+--
+-- 'Functor' and 'Monad' instances for @(->) r@ and
+-- 'Functor' instances for @(,) a@ and @'Either' a@.
+
+module Control.Monad.Instances () where
+
+import Prelude
+
+instance Functor ((->) r) where
+       fmap = (.)
+
+instance Monad ((->) r) where
+       return = const
+       f >>= k = \ r -> k (f r) r
+
+instance Functor ((,) a) where
+       fmap f (x,y) = (x, f y)
+
+instance Functor (Either a) where
+       fmap _ (Left x) = Left x
+       fmap f (Right y) = Right (f y)
index c45ccd4..4d5a6e8 100644 (file)
@@ -20,6 +20,7 @@ exposed-modules:
        Control.Exception,
        Control.Monad,
        Control.Monad.Fix,
+       Control.Monad.Instances,
        Control.Monad.ST,
        Control.Monad.ST.Lazy,
        Control.Monad.ST.Strict,
index 2c8067e..947d191 100644 (file)
@@ -18,6 +18,7 @@ exposed-modules:
        Control.Exception,
        Control.Monad,
        Control.Monad.Fix,
+       Control.Monad.Instances,
        Control.Monad.ST,
        Control.Monad.ST.Lazy,
        Control.Monad.ST.Strict,