[Haskell-cafe] Preserving data type information

Andrey Popp 8mayday at gmail.com
Sat Dec 12 10:58:12 EST 2009


I am writing URL parsing library which works the following way:
- User have a spec of URL, like this:
  > f :: String
  > f = "/archive/<int:number>
- There is data type and parser for specs:
  > data Value = IntegerValue Integer | CharValue Char
  > parseSpec :: Parser (Parser (Map String Value))
- So after parsing spec I have parser for URLs and after parsing them
— I have mapping from strings to Value values extracted from URL and
then I can process them.

Now, I want to others to extend my specs by providing additional
types, for example:
> spec :: String
> spec = "/archive/<year:archiveyear>
And store year in:
> data Year = Year Integer

But for that I need to extend Value data type too:
> data Value = Integer Value | CharValue Char | YearValue Year
Which is undesirable...

So, I decide to use type class instead of Value type:
> class Value a
> instance Value Integer
> instance Value Char
> instance Value Year
> parseSpec :: (Value a) => Parser (Parser (Map String a))

But after parsing URL I have mapping from string to some values of
type (Value a) => a which I can't process, but only by applying
functions from Value type class, which is limited.

So, is there any way to extract type information from containers like
(Value a) => Map String a? Something like this:
> getInteger :: (Value a) => Map String a -> Maybe Integer


Or maybe, am I doing something wrong from the beginning?


More information about the Haskell-Cafe mailing list