
Hello all. I've got a puzzling Parsec problem. Perhaps the collective wisdom of haskell-cafe can point me in the right direction. I want to be able to parse a string of digits to a type level numeral as described in the Number parameterized typeshttp://okmij.org/ftp/papers/number-parameterized-types.pdfpaper. After fiddling with the problem for a while, I'm not convinced it's possible- it seems as though one would need to know the type of the result before parsing, but then we wouldn't need to parse in the first place. :) My first (simplified) approximation is as follows:
data Zero = Zero data Succ a = Succ a
class Card t instance Card Zero instance (Card a) => Card (Succ a)
parseP :: (Card a) => Parser a parseP = do { char '1' ; rest <- parseP ; return $ Succ rest } <|> return Zero
I'd like for this to parse, for example, "111" into Succ Succ Succ Zero. Of course this doesn't work because parseP is ill-typed, but I'm not sure how to fix it. It seems that what I'm asking for is a function whose type is forall a. (Card a) => String -> a, which is problematic. Has anyone tried this before? I'm new to using Parsec and to parsing in general, so I apologize if this is a silly question. (Parsec is very impressive, by the way.) Thanks- Nathan Bloomfield University of Arkansas, Fayetteville