ArchiveOrangemail archive

The Haskell Cafe


haskell-cafe.haskell.org
(List home) (Recent threads) (40 other Haskell lists)

Subscription Options

  • RSS or Atom: Read-only subscription using a browser or aggregator. This is the recommended way if you don't need to send messages to the list. You can learn more about feed syndication and clients here.
  • Conventional: All messages are delivered to your mail address, and you can reply. To subscribe, send an email to the list's subscribe address with "subscribe" in the subject line, or visit the list's homepage here.
  • Moderate traffic list: up to 30 messages per day
  • This list contains about 114,613 messages, beginning Oct 2000
  • 6 messages added yesterday
Report the Spam
This button sends a spam report to the moderator. Please use it sparingly. For other removal requests, read this.
Are you sure? yes no

Serialising types with existential data constructors

Ad
Misha Aizatulin 1158048940Tue, 12 Sep 2006 08:15:40 +0000 (UTC)
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
Bulat Ziganshin 1158052226Tue, 12 Sep 2006 09:10:26 +0000 (UTC)
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
Misha Aizatulin 1158055363Tue, 12 Sep 2006 10:02:43 +0000 (UTC)
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.TextI 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
Einar Karttunen 1158113665Wed, 13 Sep 2006 02:14:25 +0000 (UTC)
On 12.09 15:28, Misha Aizatulin wrote:
>   I've been using existentially quantified data constructors like
> 
> > data Box = forall a. Cxt a => Box aIf 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
Misha Aizatulin 1158145193Wed, 13 Sep 2006 10:59:53 +0000 (UTC)
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
Ralf Lammel 1158148124Wed, 13 Sep 2006 11:48:44 +0000 (UTC)
> 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: Ralf Lammel 
> ] On Behalf Of Misha Aizatulin
> Sent: Wednesday, September 13, 2006 8:13 AM
> To: 
> 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
> 
> http://www.haskell.org/mailman/listinfo/haske...
Misha Aizatulin 1158395790Sat, 16 Sep 2006 08:36:30 +0000 (UTC)
> 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
Ashley Yakeley 1158164063Wed, 13 Sep 2006 16:14:23 +0000 (UTC)
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/OpenD...
http://haskell.org/haskellwiki/Extensible_dat...
Home | About | Privacy