I'm writing a parser in Haskell, and one of the parsing functions (prefixParser
) should return a modified version of itself on every call. The code here is simplified, but I hope every necessary bit of information is included.
type MyParsec r = Parsec [Char] () Identity r
newtype SomeResult = String
newtype RecursiveParser = MyParsec (SomeResult, RecursiveParser)
items :: RecursiveParser -> MyParsec [SomeResult]
items prefixParser = do
(someResult, newPrefixParser) <- prefixParser
rst <- items newPrefixParser
return (someResult : rst)
Now the trouble is that prefixParser
's type is RecursiveParser
but I call it inside a do
block that expects a MyParsec
. Ergo I get a
Couldn't match expected type
error. Can I even do something like this or am I (as usual) not quite getting Haskell's type system?
(The prefixParser
is meant to parse an incrementing number [1. 2. 3. ...]
, and as of yet not implemented.)
type
instead ofnewtype
– Li-yao Xianewtype
correctly. It's worth reading aboutnewtype
(and its big brotherdata
) – luquitype
recursively. He needs to use anewtype
, but in the correct way. – chi