From: Ross Paterson Date: Mon, 10 Apr 2006 11:14:43 +0000 (+0000) Subject: add Functor and Monad instances for Prelude types X-Git-Tag: 2007-09-13~394 X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=4d6fe46a37ba0e82c4182cd26a9e89a258b0c6df;p=ghc-base.git add Functor and Monad instances for Prelude types --- diff --git a/Control/Monad/Fix.hs b/Control/Monad/Fix.hs index a24d9af..e04a7f6 100644 --- a/Control/Monad/Fix.hs +++ b/Control/Monad/Fix.hs @@ -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 index 0000000..1641e03 --- /dev/null +++ b/Control/Monad/Instances.hs @@ -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) diff --git a/base.cabal b/base.cabal index c45ccd4..4d5a6e8 100644 --- a/base.cabal +++ b/base.cabal @@ -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, diff --git a/package.conf.in b/package.conf.in index 2c8067e..947d191 100644 --- a/package.conf.in +++ b/package.conf.in @@ -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,