Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9227237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:01:20+00:00 2026-06-18T05:01:20+00:00

Using Parsec, I’m able to write a function of type String -> Maybe MyType

  • 0

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.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:01:22+00:00Added an answer on June 18, 2026 at 5:01 am

    However, I already have a very robust parser who does that very thing for me.

    It’s actually not that robust, your parser has problems with superfluous parentheses, it won’t parse

    ((1) (2))
    

    for example, and it will throw an exception on some malformed inputs, because

    singleP = Single . read <$> many digit
    

    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

    infixr 6 :+:
    
    data a :+: b = a :+: b
    
    data C = C Int
    
    data D = D C
    

    you don’t need parentheses around a C 12 as an argument of (:+:), since the precedence of application is higher than that of (:+:), but you’d need parentheses around C 12 as an argument of D.

    So you’d usually have something like

    readsPrec p = needsParens (p >= precedenceLevel) someParser
    

    where someParser parses a value from the input without enclosing parentheses, and needsParens True thing parses a thing between parentheses, while needsParens False thing parses a thing optionally enclosed in parentheses [you should always accept more parentheses than necessary, ((((((1)))))) should parse fine as an Int].

    Since the readsPrec p parsers 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 parsec parser to a readsPrec parser would be

    withRemaining :: Parser a -> Parser (a, String)
    withRemaining p = (,) <$> p <*> getInput
    
    parsecToReadsPrec :: Parser a -> Int -> ReadS a
    parsecToReadsPrec parsecParser prec input
        = case parse (withremaining $ needsParens (prec >= threshold) parsecParser) "" input of
            Left _ -> []
            Right result -> [result]
    

    If you’re using GHC, it may however be preferable to use a ReadPrec / ReadP parser (built using Text.ParserCombinators.ReadP[rec]) instead of a parsec parser and define readPrec instead of readsPrec.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Text.ParserCombinators.Parsec and Text.XHtml to parse an input like this: - First type
how would I parse a file containing keys and values using parsec into [[(String,
I'm using Text.ParserCombinators.Parsec and Text.XHtml to parse an input like this: This is the
I'm using Text.ParserCombinators.Parsec and Text.XHtml to parse an input like this: this is the
I'm trying to write a parser for the propositional calculus using Parsec. The parser
I have this parser for string parsing using Haskell Parsec library. myStringLiteral = lexeme
I'm using Parsec 3.1.2 with GHC 7.4.1 to try to write a parser for
I'm trying to write a parser using Parsec that will parse literate Haskell files,
I'm attempting to write a parser in Haskell using Parsec. Currently I have a
I'm trying to write a parser in Haskell using the Parsec package. One part

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.