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 6232281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:05:25+00:00 2026-05-24T10:05:25+00:00

I’m surprised that I could not find any info on this. I must be

  • 0

I’m surprised that I could not find any info on this. I must be the only person having any trouble with it.

So, let’s say I have a dash counter. I want it to count the number of dashes in the string, and return the string. Pretend I gave an example that won’t work using parsec’s state handling. So this should work:

dashCounter = do
  str <- many1 dash
  count <- get
  return (count,str)


dash = do
  char '-'
  modify (+1)

And indeed, this compiles. Okay, so I try to use it:

:t parse dashCounter "" "----"
parse dashCounter "" "----"
  :: (Control.Monad.State.Class.MonadState
        t Data.Functor.Identity.Identity,
      Num t) =>
     Either ParseError (t, [Char])

Okay, that makes sense. It should return the state and the string. Cool.

>parse dashCounter "" "----"

<interactive>:1:7:
    No instance for (Control.Monad.State.Class.MonadState
                       t0 Data.Functor.Identity.Identity)
      arising from a use of `dashCounter'
    Possible fix:
      add an instance declaration for
      (Control.Monad.State.Class.MonadState
         t0 Data.Functor.Identity.Identity)
    In the first argument of `parse', namely `dashCounter'
    In the expression: parse dashCounter "" "----"
    In an equation for `it': it = parse dashCounter "" "----"

Oops. But then how could it have ever hoped to work in the first place? There’s no way to input the initial state.

There is also a function:

>runPT dashCounter (0::Int) "" "----"

But it gives a similar error.

<interactive>:1:7:
    No instance for (Control.Monad.State.Class.MonadState Int m0)
      arising from a use of `dashCounter'
    Possible fix:
      add an instance declaration for
      (Control.Monad.State.Class.MonadState Int m0)
    In the first argument of `runPT', namely `dashCounter'
    In the expression: runPT dashCounter (0 :: Int) "" "----"
    In an equation for `it':
        it = runPT dashCounter (0 :: Int) "" "----"

I feel like I should have to runState on it, or there should be a function that already does it internally, but I can’t seem to figure out where to go from here.

Edit: I should have specified more clearly, I did not want to use parsec’s state handling. The reason is I have a feeling I don’t want its backtracking to affect what it collects with the problem I’m preparing to solve it with.

However, Mr. McCann has figured out how this should fit together and the final code would look like this:

dashCounter = do
  str <- many1 dash
  count <- get
  return (count,str)

dash = do
  c <- char '-'
  modify (+1)
  return c

test = runState (runPT dashCounter () "" "----------") 0

Thanks a lot.

  • 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-05-24T10:05:25+00:00Added an answer on May 24, 2026 at 10:05 am

    You’ve actually got multiple problems going on here, all of which are relatively non-obvious the first time around.

    Starting with the simplest: dash is returning (), which doesn’t seem to be what you want given that you’re collecting the results. You probably wanted something like dash = char '-' <* modify (+1). (Note that I’m using an operator from Control.Applicative here, because it looks tidier)

    Next, clearing up a point of confusion: When you get the reasonable-looking type signature in GHCi, note the context of (Control.Monad.State.Class.MonadState t Data.Functor.Identity.Identity, Num t). That’s not saying what things are, it’s telling you want they need to be. Nothing guarantees that the instances it’s asking for exist and, in fact, they don’t. Identity is not a state monad!

    On the other hand, you’re absolutely correct in thinking that parse doesn’t make sense; you can’t use it here. Consider its type: Stream s Identity t => Parsec s () a -> SourceName -> s -> Either ParseError a. As is customary with monad transformers, Parsec is an synonym for ParsecT applied to the identity monad. And while ParsecT does provide user state, you apparently don’t want to use it, and ParsecT does not give an instance of MonadState anyhow. Here’s the only relevant instance: MonadState s m => MonadState s (ParsecT s' u m). In other words, to treat a parser as a state monad you have to apply ParsecT to some other state monad.

    This sort of brings us to the next problem: Ambiguity. You’re using a lot of type class methods and no type signatures, so you’re likely to run into situations where GHC can’t know what type you actually want, so you have to tell it.

    Now, as a quick solution, let’s first define a type synonym to give a name to the monad transformer stack we want:

    type StateParse a = ParsecT String () (StateT Int Identity) a
    

    Give dashCounter the relevant type signature:

    dashCounter :: StateParse (Int, String)
    dashCounter = do str <- many1 dash
                     count <- get
                     return (count,str)
    

    And add a special-purpose “run” function:

    runStateParse p sn inp count = runIdentity $ runStateT (runPT p () sn inp) count
    

    Now, in GHCi:

    Main> runStateParse dashCounter "" "---" 0
    (Right (3,"---"),3)
    

    Also, note that it’s pretty common to use a newtype around a transformer stack instead of just a type synonym. This can help with the ambiguity issues in some cases, and obviously avoids ending up with gigantic type signatures.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.