Skip to content

Commit d006606

Browse files
committed
rewrite parser monad instances
Functor and Applicative used the Monad instance. This is bad practice and recent compilers emit a warning.
1 parent 7b67b51 commit d006606

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/Language/Fortran/Parser/Monad.hs

+15-9
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,22 @@ class Tok a where
7878

7979
newtype Parse b c a = Parse { unParse :: ParseState b -> ParseResult b c a }
8080

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''
8395

96+
instance (Loc b, LastToken b c, Show c) => Monad (Parse b c) where
8497
(Parse m) >>= f = Parse $ \s ->
8598
case m s of
8699
ParseOk a s' -> unParse (f a) s'
@@ -98,13 +111,6 @@ instance (Loc b, LastToken b c, Show c) => MonadFail (Parse b c) where
98111
, errFilename = psFilename s
99112
, errMsg = msg }
100113

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-
108114
instance (Loc b, LastToken b c, Show c) => MonadState (ParseState b) (Parse b c) where
109115
get = Parse $ \s -> ParseOk s s
110116
put s = Parse $ \_ -> ParseOk () s

0 commit comments

Comments
 (0)