Open adithyaov opened 10 months ago
local
cannot be implemented for ParserK because there is no way to override all continuations. Earlier also local was implemented for ParserD only and ParserK was converted to ParserD for local.
Ideally, we should remove the usage of local
when ParserK is being used, or use ParserD if it is suitable for the use case. Conversion from ParserD to ParserK and then back to ParserD may not be good for performance.
Anyway, I have provided a way to convert ParserK to ParserD in this PR: https://github.com/composewell/streamly/pull/2705 . We can also port this to 0.10.
With this we can use the following instances:
instance (Show r, MonadReader r m) => MonadReader r (Parser a m) where
{-# INLINE ask #-}
ask = Parser.fromEffect ask
{-# INLINE local #-}
local f (Parser step initial extract) =
Parser
((local f .) . step)
(local f initial)
(local f . extract)
instance (Show r, MonadReader r m) => MonadReader r (ParserK a m) where
{-# INLINE ask #-}
ask = ParserK.fromEffect ask
{-# INLINE local #-}
local f parser = ParserK.adapt $ local f $ ParserK.toParser parser
Should the usage of ParserK.toParser
be encouraged?
The implementation is not straightforward. The following implementation does not work as expected for specific cases. Following is the minimal example to play with,