[commit: packages/Cabal] ghc-head: Functor/Applicative instance for types in ReadP.hs (f6b172d)

git at git.haskell.org git at git.haskell.org
Mon Aug 26 23:28:10 CEST 2013


Repository : ssh://git@git.haskell.org/Cabal

On branch  : ghc-head
Link       : http://git.haskell.org/?p=packages/Cabal.git;a=commit;h=f6b172ddf4e9374cdeb15c148dc89bce602b1818

>---------------------------------------------------------------

commit f6b172ddf4e9374cdeb15c148dc89bce602b1818
Author: Benno Fünfstück <benno.fuenfstueck at gmail.com>
Date:   Wed Jul 10 19:48:14 2013 +0200

    Functor/Applicative instance for types in ReadP.hs
    
    This defines the following instances for the types in ReadP.hs:
      * Functor/Applicative for P
      * Appplicative for Parser


>---------------------------------------------------------------

f6b172ddf4e9374cdeb15c148dc89bce602b1818
 Cabal/Distribution/Compat/ReadP.hs |   14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/Cabal/Distribution/Compat/ReadP.hs b/Cabal/Distribution/Compat/ReadP.hs
index e087ed2..62f875f 100644
--- a/Cabal/Distribution/Compat/ReadP.hs
+++ b/Cabal/Distribution/Compat/ReadP.hs
@@ -71,6 +71,8 @@ module Distribution.Compat.ReadP
 
 import Control.Monad( MonadPlus(..), liftM2 )
 import Data.Char (isSpace)
+import Control.Applicative (Applicative(..))
+import Control.Monad (join)
 
 infixr 5 +++, <++
 
@@ -87,6 +89,14 @@ data P s a
 
 -- Monad, MonadPlus
 
+instance Functor (P s) where
+  fmap f x = x >>= return . f
+
+instance Applicative (P s) where
+  pure = return
+
+  f <*> a = f >>= \f' -> fmap f' a
+
 instance Monad (P s) where
   return x = Result x Fail
 
@@ -138,6 +148,10 @@ type ReadP r a = Parser r Char a
 instance Functor (Parser r s) where
   fmap h (R f) = R (\k -> f (k . h))
 
+instance Applicative (Parser r s) where
+  pure = return
+  f <*> a = f >>= \f' -> fmap f' a
+
 instance Monad (Parser r s) where
   return x  = R (\k -> k x)
   fail _    = R (\_ -> Fail)





More information about the ghc-commits mailing list