-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Monadic parsers and error-correction #2
Comments
One pretty nice solution is to use a more granular function: pUpTo :: Integer -> Parser Integer
pUpTo n = go 0
where
go :: Integer -> Parser Integer
go s
| n - 10 * s < 0 = pFail
| otherwise = addLength 1 $ do
s' <- (+ 10 * s) . fromIntegral . digitToInt
<$> pRange ('0', intToDigit (fromIntegral (min 9 (n - 10 * s))))
go s' `opt` s' |
Getting error correction working together perfectly in the context of monads in such examples will not work. The problem is that there is no way for the parsing machinery to know about the usage of the values. The expression I believe there are however ways of influencing the error correction. Indeed you could define primitives in a different way, but instead of doing |
Oh yeah, it is even a finite language, so you could just list all options. And that could be optimized similarly to my
This is more what I intended with the question. I was looking for a way to instruct the library to perform certain corrections. In the original example you can see that the library tries to insert 0, which is not a good idea, so I was wondering if I could instead tell the library in some way to remove or change existing digits. |
Yes, by showing what format would be valid, e.g. by listing all options or doing something like |
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
: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:
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: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)
The text was updated successfully, but these errors were encountered: