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?
Hi Andrey Dynamic types are a possibility, if you want to handle an 'open set' of types: http://www.haskell.org/ghc/docs/6.10.3/html/libraries/base/Data-Dynamic.html & http://people.cs.uu.nl/arthurb/dynamic.html Although I don't see your use case as needing heterogeneous lists, section 2 of the HList paper enumerates the design options available for 'open types' (the authors conclude against dynamics in this case). http://homepages.cwi.nl/~ralf/HList/ Best wishes Stephen
Hi, Stephen. Thanks a lot — HList paper gave me a good overview of possible soultions. For the first try I will stuck with Data.Dynamic. On Sat, Dec 12, 2009 at 7:18 PM, Stephen Tetley <stephen.tetley@gmail.com> wrote:
Hi Andrey
Dynamic types are a possibility, if you want to handle an 'open set' of types:
http://www.haskell.org/ghc/docs/6.10.3/html/libraries/base/Data-Dynamic.html & http://people.cs.uu.nl/arthurb/dynamic.html
Although I don't see your use case as needing heterogeneous lists, section 2 of the HList paper enumerates the design options available for 'open types' (the authors conclude against dynamics in this case).
http://homepages.cwi.nl/~ralf/HList/
Best wishes
Stephen
-- С уважением, Андрей Попп. +7 911 740 24 91
participants (2)
-
Andrey Popp -
Stephen Tetley