Skip to content

Monadic parsers and error-correction #2

@noughtmare

Description

@noughtmare

I am running into an issue with monadic parsers and error correction.

Take for example this parser that parses natural numbers, but only if their value is below 42:

pUpTo42 :: Parser Integer
pUpTo42 = do
  n <- pNatural
  if n <= 42 then return n else pFail

If we now try to run this parser on a string that contains a number that is too large we get into an infinite loop:

λ parse ((,) <$> pUpTo42 <*> pEnd) (createStr (LineColPos 0 0 0) "43")
(^CInterrupted.

I think this is caused by the error correction trying to add more digits until it parses correctly, but no amount of digit-adding will correct this issue. If I change the type to Parser Int you can clearly see this:

λ (\(x, y) -> print x *> mapM_ print y) $ parse ((,) <$> pUpTo42Int <*> pEnd) (createStr (LineColPos 0 0 0) "43")
-6071859544651202560
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]
--    Inserted  '0' at position LineColPos 0 2 2 expecting one of ['0'..'9', Whitespace]

Ideally, I would like the error correction to replace any larger number by the number 42. Is there a way to accomplish that?

Also note that this is not my actual problem, I've simplified it to this problem. My original motivation was this blog post: https://juliu.is/permutate-parsers/, which reminded me of this library.

P.S. Is anybody still watching this repo? (you can add the eyes emoji as reaction to let me know without filling up the thread)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions