on master help sign in
Add Functor and Applicative instances for IParser
2b11c56Bryan O'Sullivan
Data/Text/Internal/Read.hs+12−2
@@ -18,6 +18,9 @@ module Data.Text.Internal.Read
18 , perhaps
18 , perhaps
19 ) where
19 ) where
20
20
21+import Control.Applicative (Applicative(..))
22+import Control.Arrow (first)
23+import Control.Monad (ap)
21import Data.Char (ord)
24import Data.Char (ord)
22
25
23type IReader t a = t -> Either String (a,t)
26type IReader t a = t -> Either String (a,t)
@@ -26,9 +29,16 @@ newtype IParser t a = P {
26 runP :: IReader t a
29 runP :: IReader t a
27 }
30 }
28
31
32+instance Functor (IParser t) where
33+ fmap f m = P $ fmap (first f) . runP m
34+
35+instance Applicative (IParser t) where
36+ pure a = P $ \t -> Right (a,t)
37+ {-# INLINE pure #-}
38+ (<*>) = ap
39+
29instance Monad (IParser t) where
40instance Monad (IParser t) where
30− return a = P $ \t -> Right (a,t)
41+ return = pure
31− {-# INLINE return #-}
32 m >>= k = P $ \t -> case runP m t of
42 m >>= k = P $ \t -> case runP m t of
33 Left err -> Left err
43 Left err -> Left err
34 Right (a,t') -> runP (k a) t'
44 Right (a,t') -> runP (k a) t'