Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Okay, ReadP should be a MonadFix. It can, if you have the internal representation, be converted to `ReadNT []`, where:
- newtype ReadNT m a = ReadNT { runReadNT :: String -> m (a, Either Int String) }
- deriving instance Functor m => Functor (ReadNT m)
- instance Monad m => Applicative (ReadNT m) where { pure a = ReadNT $ const $ pure (a, Left 0); (<*>) = ap }
- instance Monad m => Monad (ReadNT m) where
- ReadNT m >>= f = ReadNT $ \s -> do
- (a, r) <- m s
- case r of
- Left i -> do
- (b, t) -> runReadNT (f a) $ drop i s
- case t of
- Left j -> (b, Left (i + j))
- Right sn -> (b, Right sn)
- Right sr -> do
- (b, t) -> runReadNT (f a) sr
- case t of
- Left j -> (b, Right (drop j sr))
- Right sn -> (b, Right sn)
- -- It has the obvious MonadFix instance.
- instance MonadFix m => MonadFix (ReadNT m) where
- mfix f = ReadNT $ \s -> mfix $ \ ~(a, _) -> runReadNT (f a) s
Advertisement
Add Comment
Please, Sign In to add comment