Constructing a datatype given just its constructor as a string?

Trying to create a datatype/constructor given just its constructor as a string, something like: mkConstr :: String -> Constr parseData :: (Data a) => String -> a ***without knowing in advance anything about the datatype apart from the string contents*** So, not something like: parseData "Just 3" :: [Maybe] but simply: parseData "Just 3" (This is linked to the other post: the goal is to be able to deserialize xml without needing to know in advance the data type we are deserializing)

Hi
mkConstr :: String -> Constr parseData :: (Data a) => String -> a
fromConstr, plus a bit of work to create the constructor. http://www.haskell.org/hoogle/?q=fromConstr Thanks Neil

Just noticed that all my responses have been going only to Neil, not to the group. Anyway, the jist of our conversation was that it's not possible to create arbitrary datatypes/constructors from strings in Haskell. Can anyone deny/confirm?

hughperkins:
Just noticed that all my responses have been going only to Neil, not to the group. Anyway, the jist of our conversation was that it's not possible to create arbitrary datatypes/constructors from strings in Haskell. Can anyone deny/confirm?
Entirely possible. Though it would be interesting to see the code that is to process the arbitrary types as they arrive, after deserialising. Anyway there was a thread on this last week. One solution is to have a symbol table mapping type reps to values of a given type, then you use that to know which type to read from the constructor tag, resolving the problem of not knowing what type you're reading. -- Don

Hi Hugh,
2007/6/25, Donald Bruce Stewart
hughperkins:
Just noticed that all my responses have been going only to Neil, not to the group. Anyway, the jist of our conversation was that it's not possible to create arbitrary datatypes/constructors from strings in Haskell. Can anyone deny/confirm?
Anyway there was a thread on this last week.
That thread starting here: http://www.haskell.org/pipermail/haskell-cafe/2007-June/026777.html The takeaway was Stefan O'Rear's suggestion that if you don't want to create a table of datatypes manually, you can use hs-plugins -- something along the lines of data Foo = forall a. MyClass a => Foo a read' typeName s = eval ("Foo (gread \"" ++ s ++ "\" :: " ++ typeName ++ ")") :: Foo - Benja

On Sun, Jun 24, 2007 at 10:55:40PM +0200, Hugh Perkins wrote:
Anyway, the jist of our conversation was that it's not possible to create arbitrary datatypes/constructors from strings in Haskell. Can anyone deny/confirm?
If you want a function like fromConstr, where the 'a' depends on the input string, you can write it in CPS or wrap the result in an existential datatype. But I guess the problem is with "arbitrary", right? Best regards Tomek
participants (5)
-
Benja Fallenstein
-
dons@cse.unsw.edu.au
-
Hugh Perkins
-
Neil Mitchell
-
Tomasz Zielonka