Serialising types with existential data constructors

hello all, I've been using existentially quantified data constructors like
data Box = forall a. Cxt a => Box a
quite successfully for a while. But now I am trying to implement the Load/Save mechanism and getting stuck with that. It's not hard to write a Box into a file, but how do I get it back? Has anyone solved the same problem before? I would be very thankful for any suggestions! If Template Haskell would support finding out all instances of a given class, I could generate a function that would map Names of types to appropriate Box readers. In the file I would write entries like <NameOfTypeInTheBox> : <BoxContents> But sadly, TH doesn't allow that yet. Cheers, Misha

Hello Misha, Tuesday, September 12, 2006, 4:28:28 PM, you wrote:
quite successfully for a while. But now I am trying to implement the Load/Save mechanism and getting stuck with that. It's not hard to write a Box into a file, but how do I get it back?
gshow/gread provided by module Data.Generics.Text someone once asked me to write binary-format gput/gget, but i don't done it -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Bulat Ziganshin wrote:
data Box = forall a. Cxt a => Box a quite successfully for a while. But now I am trying to implement the Load/Save mechanism and getting stuck with that. It's not hard to write a Box into a file, but how do I get it back?
gshow/gread provided by module Data.Generics.Text
I am afraid this won't do it. For gread to work I still need to know the result type before I call it, but the problem with the Box is exactly that you don't know the type it will contain in advance. Maybe there is a way to dump the binary representation of the whole box and then read it back (using unsafeCoerce or similar stuff on the way)? Cheers, Misha

On 12.09 15:28, Misha Aizatulin wrote:
I've been using existentially quantified data constructors like
data Box = forall a. Cxt a => Box a
If you can include Typeable into the mix then serializing works. Serialize the value as "<name of type> <value>". When deserializing use a Map <name of type> <decoder-function> and get the appropriate decoder from there for the type in question. - Einar Karttunen

Einar Karttunen wrote:
I've been using existentially quantified data constructors like
data Box = forall a. Cxt a => Box a
If you can include Typeable into the mix then serializing works.
Serialize the value as "<name of type> <value>".
When deserializing use a Map <name of type> <decoder-function> and get the appropriate decoder from there for the type in question.
This is indeed the only solution I see so far. It has a serious problem though: as soon as I write the mapping, I limit once and for all the set of all types that can be used with my box. And I do so in a non-extensible way - if someone later would like to use my box with some other type in it, they wouldn't be able to. In fact, I start wondering, how OO languages solve the same problem. I'll take a look at Java now. Cheers, Misha

I start wondering, how OO languages solve the same problem.
Conceptually, what is needed is a mapping of the "head" of the input to a type. This is indeed a recurring problem in OO languages; think of object serialization or XML/Object mapping. One common way of accomplishing the mapping is to associate custom attributes (aka annotations) with classes that exactly define "when you see this element tag, instantiate this class". It is then the task of a compile-time or run-time reflection to gather these attributes and generate code from it -- code that actually constructs instances according to the mapping and the input. Klaus Ostermann and I allude to this non-trivial extensibility problem in our GPCE 2006 paper and we started to look into ways (not in that paper) to resolve the problem in a principled way. Best, Ralf
-----Original Message----- From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe- bounces@haskell.org] On Behalf Of Misha Aizatulin Sent: Wednesday, September 13, 2006 8:13 AM To: haskell-cafe@haskell.org Subject: RE: Re: [Haskell-cafe] Serialising types with existential data constructors
Einar Karttunen wrote:
I've been using existentially quantified data constructors like
data Box = forall a. Cxt a => Box a
If you can include Typeable into the mix then serializing works.
Serialize the value as "<name of type> <value>".
When deserializing use a Map <name of type> <decoder-function> and get the appropriate decoder from there for the type in question.
This is indeed the only solution I see so far. It has a serious problem though: as soon as I write the mapping, I limit once and for all the set of all types that can be used with my box. And I do so in a non-extensible way - if someone later would like to use my box with some other type in it, they wouldn't be able to.
In fact, I start wondering, how OO languages solve the same problem. I'll take a look at Java now.
Cheers, Misha _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Klaus Ostermann and I allude to this non-trivial extensibility problem in our GPCE 2006 paper and we started to look into ways (not in that paper) to resolve the problem in a principled way.
I'm really looking forward to that! For now I'll probably use Template Haskell to register all instances that have to be serialized and output the mapping head->type in the end. Cheers, Misha

Misha Aizatulin wrote:
This is indeed the only solution I see so far. It has a serious problem though: as soon as I write the mapping, I limit once and for all the set of all types that can be used with my box. And I do so in a non-extensible way - if someone later would like to use my box with some other type in it, they wouldn't be able to.
This doesn't really help you now, but extending Haskell with open types and functions would solve this... http://www.informatik.uni-bonn.de/~loeh/OpenDatatypes.pdf http://haskell.org/haskellwiki/Extensible_datatypes -- Ashley Yakeley
participants (5)
-
Ashley Yakeley
-
Bulat Ziganshin
-
Einar Karttunen
-
Misha Aizatulin
-
Ralf Lammel