[Haskell-cafe] Parsec and type level numerals

Nathan Bloomfield nbloomf at gmail.com
Sat Dec 13 14:13:01 EST 2008

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
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.)


Nathan Bloomfield
University of Arkansas, Fayetteville
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20081213/58e3b9e8/attachment.htm

More information about the Haskell-Cafe mailing list