Commit f6b172dd authored by Benno Fünfstück's avatar Benno Fünfstück
Browse files

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
parent 2ad0b45a
......@@ -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)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment