on master help sign in
Add MonadReader, MonadState and MonadIO to ParserD/ParserK
9119e44Julian Ospald
src/Streamly/Internal/Data/Parser/ParserD/Type.hs+26
@@ -1,3 +1,4 @@
1+{-# LANGUAGE UndecidableInstances #-}
1#include "inline.hs"
2#include "inline.hs"
2
3
3-- |
4-- |
@@ -139,6 +140,9 @@ where
139import Control.Applicative (Alternative(..), liftA2)
140import Control.Applicative (Alternative(..), liftA2)
140import Control.Exception (assert, Exception(..))
141import Control.Exception (assert, Exception(..))
141import Control.Monad (MonadPlus(..), (>=>))
142import Control.Monad (MonadPlus(..), (>=>))
143+import Control.Monad.IO.Class (MonadIO, liftIO)
144+import Control.Monad.Reader.Class (MonadReader, ask, local)
145+import Control.Monad.State.Class (MonadState, get, put)
142import Control.Monad.Catch (MonadCatch, try, throwM, MonadThrow)
146import Control.Monad.Catch (MonadCatch, try, throwM, MonadThrow)
143import Data.Bifunctor (Bifunctor(..))
147import Data.Bifunctor (Bifunctor(..))
144import Fusion.Plugin.Types (Fuse(..))
148import Fusion.Plugin.Types (Fuse(..))
@@ -1121,3 +1125,25 @@ instance MonadCatch m => MonadPlus (Parser m a) where
1121
1125
1122 {-# INLINE mplus #-}
1126 {-# INLINE mplus #-}
1123 mplus = alt
1127 mplus = alt
1128+
1129+instance (MonadThrow m, MonadReader r m, MonadCatch m) => MonadReader r (Parser m a) where
1130+ {-# INLINE ask #-}
1131+ ask = fromEffect ask
1132+ {-# INLINE local #-}
1133+ local f (Parser step init' extract) =
1134+ Parser ((local f .) . step)
1135+ (local f init')
1136+ (local f . extract)
1137+
1138+
1139+instance (MonadThrow m, MonadState s m) => MonadState s (Parser m a) where
1140+ {-# INLINE get #-}
1141+ get = fromEffect get
1142+ {-# INLINE put #-}
1143+ put = fromEffect . put
1144+
1145+
1146+instance (MonadThrow m, MonadIO m) => MonadIO (Parser m a) where
1147+ {-# INLINE liftIO #-}
1148+ liftIO = fromEffect . liftIO
1149+
src/Streamly/Internal/Data/Parser/ParserK/Type.hs+23
@@ -1,3 +1,4 @@
1+{-# LANGUAGE UndecidableInstances #-}
1#include "inline.hs"
2#include "inline.hs"
2
3
3-- |
4-- |
@@ -35,6 +36,9 @@ import Control.Applicative (Alternative(..), liftA2)
35import Control.Exception (assert, Exception(..))
36import Control.Exception (assert, Exception(..))
36import Control.Monad (MonadPlus(..), ap)
37import Control.Monad (MonadPlus(..), ap)
37import Control.Monad.Catch (MonadCatch, MonadThrow(..), try)
38import Control.Monad.Catch (MonadCatch, MonadThrow(..), try)
39+import Control.Monad.IO.Class (MonadIO, liftIO)
40+import Control.Monad.Reader.Class (MonadReader, ask, local)
41+import Control.Monad.State.Class (MonadState, get, put)
38#if MIN_VERSION_base(4,9,0)
42#if MIN_VERSION_base(4,9,0)
39import qualified Control.Monad.Fail as Fail
43import qualified Control.Monad.Fail as Fail
40#endif
44#endif
@@ -411,6 +415,25 @@ instance Monad m => Fail.MonadFail (Parser m a) where
411 fail = die
415 fail = die
412#endif
416#endif
413
417
418+instance (MonadThrow m, MonadReader r m, MonadCatch m) => MonadReader r (Parser m a) where
419+ {-# INLINE ask #-}
420+ ask = fromEffect ask
421+ {-# INLINE local #-}
422+ local f (fromParserK -> dp) =
423+ toParserK $ local f dp
424+
425+
426+instance (MonadThrow m, MonadState s m) => MonadState s (Parser m a) where
427+ {-# INLINE get #-}
428+ get = fromEffect get
429+ {-# INLINE put #-}
430+ put = fromEffect . put
431+
432+
433+instance (MonadThrow m, MonadIO m) => MonadIO (Parser m a) where
434+ {-# INLINE liftIO #-}
435+ liftIO = fromEffect . liftIO
436+
414-------------------------------------------------------------------------------
437-------------------------------------------------------------------------------
415-- Alternative
438-- Alternative
416-------------------------------------------------------------------------------
439-------------------------------------------------------------------------------