@@ -78,9 +78,22 @@ class Tok a where
78
78
79
79
newtype Parse b c a = Parse { unParse :: ParseState b -> ParseResult b c a }
80
80
81
- instance (Loc b , LastToken b c , Show c ) => Monad (Parse b c ) where
82
- return a = Parse $ \ s -> ParseOk a s
81
+ instance (Loc b , LastToken b c , Show c ) => Functor (Parse b c ) where
82
+ fmap f (Parse p) = Parse $ \ s -> case p s of
83
+ ParseOk a s' -> ParseOk (f a) s'
84
+ ParseFailed e -> ParseFailed e
85
+
86
+ instance (Loc b , LastToken b c , Show c ) => Applicative (Parse b c ) where
87
+ pure a = Parse $ \ s -> ParseOk a s
88
+ (Parse pl) <*> (Parse pr) = Parse $ \ s ->
89
+ case pl s of
90
+ ParseFailed e -> ParseFailed e
91
+ ParseOk ab s' ->
92
+ case pr s' of
93
+ ParseFailed e -> ParseFailed e
94
+ ParseOk a s'' -> ParseOk (ab a) s''
83
95
96
+ instance (Loc b , LastToken b c , Show c ) => Monad (Parse b c ) where
84
97
(Parse m) >>= f = Parse $ \ s ->
85
98
case m s of
86
99
ParseOk a s' -> unParse (f a) s'
@@ -98,13 +111,6 @@ instance (Loc b, LastToken b c, Show c) => MonadFail (Parse b c) where
98
111
, errFilename = psFilename s
99
112
, errMsg = msg }
100
113
101
- instance (Loc b , LastToken b c , Show c ) => Functor (Parse b c ) where
102
- fmap = liftM
103
-
104
- instance (Loc b , LastToken b c , Show c ) => Applicative (Parse b c ) where
105
- pure = return
106
- (<*>) = ap
107
-
108
114
instance (Loc b , LastToken b c , Show c ) => MonadState (ParseState b ) (Parse b c ) where
109
115
get = Parse $ \ s -> ParseOk s s
110
116
put s = Parse $ \ _ -> ParseOk () s
0 commit comments