I am currently trying to study and understand the source code of the attoparsec
library, but there are some details I can't figure out myself. For example, the definition of the Parser
type:
newtype Parser i a = Parser {
runParser :: forall r.
State i -> Pos -> More
-> Failure i (State i) r
-> Success i (State i) a r
-> IResult i r
}
newtype Pos = Pos { fromPos :: Int }
deriving (Eq, Ord, Show, Num)
data IResult i r =
Fail i [String] String
| Partial (i -> IResult i r)
| Done i r
type Failure i t r = t -> Pos -> More -> [String] -> String
-> IResult i r
type Success i t a r = t -> Pos -> More -> a -> IResult i r
What I don't fully understand yet is the usage of the type-parameter r
. What would be different if I defined the type signature of runParser
like this:
State i -> Pos -> More -> Failure i (State i) a -> Success i (State i) a a -> IResult i a
?
Can you please help me to understand what forall r.
exactly means in this case and why it is necessary to use it in the runParser
's type signature?
Many thx in advance!
UPDATE: To clarify my question further: What I currently don't understand is why it is necessary to bring in the type-parameter r
in the first place. One could imagine, that the Parser
type could have been also defined like this:
newtype Parser i a = Parser {
runParser ::
State i -> Pos -> More
-> Failure i (State i) a
-> Success i (State i) a
-> IResult i a
}
data IResult i a =
Fail i [String] String
| Partial (i -> IResult i a)
| Done i a
type Failure i t a = t -> Pos -> More -> [String] -> String
-> IResult i a
type Success i t a = t -> Pos -> More -> a -> IResult i a
where the type-parameter r
is not used at all. And my question is why this definition would be "wrong" and what problems it would entail...