I am surprised that I could not find information about this. I must be the only person who has problems with him.
So let's say I have a dash counter. I want it to count the number of dashes in a string and return the string. Imagine that I gave an example that will not work with parsec state processing. Therefore, this should work:
dashCounter = do str <- many1 dash count <- get return (count,str) dash = do char '-' modify (+1)
And indeed, it compiles. Ok, so I'm trying to use it:
:t parse dashCounter "" "----" parse dashCounter "" "----" :: (Control.Monad.State.Class.MonadState t Data.Functor.Identity.Identity, Num t) => Either ParseError (t, [Char])
Ok, that makes sense. It should return the state and 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 "" "----"
Unfortunately. But then, how could it ever hope to work in the first place? Unable to enter 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) "" "----"
It seems to me that I need to run RunState, or there should be a function that already does this inside, but I can not figure out where to go from here.
Edit: I had to specify more clearly, I did not want to use parsec state processing. The reason is that I have a feeling that I do not want his rollback to affect what he collects, with a problem that I am ready to solve.
However, Mr. McCann 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
Many thanks.