How to use Control.Monad.State with Parsec?

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 '-' 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' you 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 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.

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 '-' 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' you 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 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.

If you want to use the user state component Parsec offers as a built-in feature, then you can use the getState and modifyState monadic functions. I tried to stay true to your example program, though using the return of dash doesn't seem useful. Import Text.

Parsec dashCounter :: Parsec String Int (Int, ()) dashCounter = do str.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions