Using Parsec, I’m able to write a function of type String -> Maybe MyType with relative ease. I would now like to create a Read instance for my type based on that; however, I don’t understand how readsPrec works or what it is supposed to do.
My best guess right now is that readsPrec is used to build a recursive parser from scratch to traverse a string, building up the desired datatype in Haskell. However, I already have a very robust parser who does that very thing for me. So how do I tell readsPrec to use my parser? What is the “operator precedence” parameter it takes, and what is it good for in my context?
If it helps, I’ve created a minimal example on Github. It contains a type, a parser, and a blank Read instance, and reflects quite well where I’m stuck.
(Background: The real parser is for Scheme.)
It’s actually not that robust, your parser has problems with superfluous parentheses, it won’t parse
for example, and it will throw an exception on some malformed inputs, because
may use
read "" :: Int.That out of the way, the precedence argument is used to determine whether parentheses are necessary in some place, e.g. if you have
you don’t need parentheses around a
C 12as an argument of(:+:), since the precedence of application is higher than that of(:+:), but you’d need parentheses aroundC 12as an argument ofD.So you’d usually have something like
where
someParserparses a value from the input without enclosing parentheses, andneedsParens True thingparses athingbetween parentheses, whileneedsParens False thingparses athingoptionally enclosed in parentheses [you should always accept more parentheses than necessary,((((((1))))))should parse fine as anInt].Since the
readsPrec pparsers are used to parse parts of the input as parts of the value when reading lists, tuples etc., they must return not only the parsed value, but also the remaining part of the input.With that, a simple way to transform a
parsecparser to areadsPrecparser would beIf you’re using GHC, it may however be preferable to use a
ReadPrec / ReadPparser (built usingText.ParserCombinators.ReadP[rec]) instead of aparsecparser and definereadPrecinstead ofreadsPrec.