[Haskell-cafe] serialize an unknown type

Corentin Dupont corentin.dupont at gmail.com
Sun Oct 21 22:25:53 CEST 2012


On Sun, Oct 21, 2012 at 8:42 PM, MigMit <miguelimo38 at yandex.ru> wrote:

> Oh, now I've got it.
>
> First of all, functions of type "IO () -> Event a" are impossible (unless
> you resort to tricks with unsafePerformIO, which is not what you want to
> deal with right now). You've probably meant something like "IO (Event a)"
> (at least, that is the type of function which would read "Event a" from
> file or input stream or something else external).
>

Yes, sorry, that's what I meant.


>
> Secondly, functions (and values) with parametric types, like "IO (Event
> a)" require you to provide the type "a" in your code. They won't take it
> from somewhere magically. You can take a look at the "read" function in
> Prelude for example.
>
> If you really want to store the type information and read it back, you
> should do it yourself, inventing some representation for your type, writing
> it to disk, reading back and parsing it. And you can't do that for all
> types in existence, so you'll need to do that for some specific types (and
> no, instances of Typeable aren't what you want). And you'll have to deal
> with type system, which won't allow you to just say "hey, let that be
> whatever type it happens to be, I don't care"; you'd have to wrap this into
> one existentially quantified type (or GADT).
>
> Keep in mind that this is not very Haskell-y. First of all, try to
> analyse, what this "a" in "Event a" could be. What are the limits here? And
> don't say there aren't any, because if you don't know anything about the
> type, you can't do anything with it. So, maybe you would end up with a
> finite set of types -- this would simplify matters a lot. Or maybe you'd
> find out that there are inifinitely many types of events -- but they can be
> somehow generated with a finite number of constructors -- that would be
> quite simple as well.
>
> So, what is the bigger picture here?
>


In my application, the user can define the "a". That's what makes it
difficult. For example, the user can define a new enumerate and submit it
to the program (it will be interpreted by hint):
data Choice = You | Me  | Them | Everybody deriving (Enum, Typeable, Show,
Eq, Bounded)
So, the list of types is not known in advance.
I could ask my user to make his new type an instance of a class as
suggested by Alberto...



>
> On Oct 21, 2012, at 9:20 PM, Corentin Dupont <corentin.dupont at gmail.com>
> wrote:
>
> > Hi,
> > Sorry if it was not enough explicit.
> > I want to write functions like this:
> >
> > serialize :: (Show a) => Event a -> IO ()
> > deserialize :: (Read a) => IO () -> Event a
> >
> > The functions would write and read the data in a file,
> storing/retrieving also the type "a" I suppose...
> > BR,
> > C
> >
> >
> >
> > On Sun, Oct 21, 2012 at 7:03 PM, MigMit <miguelimo38 at yandex.ru> wrote:
> > Seems like nobody really understands what is it that you want to
> accomplish or what your problem is.
> >
> > Отправлено с iPhone
> >
> > 21.10.2012, в 20:39, Corentin Dupont <corentin.dupont at gmail.com>
> написал(а):
> >
> >> Nobody on this one?
> >> Here is a simplified version:
> >>
> >> data Event a where
> >>     InputChoice ::  a -> Event a
> >>
> >> How to serialize/deserialize this?
> >>
> >> Cheers,
> >> Corentin
> >>
> >> On Sat, Oct 20, 2012 at 10:49 PM, Corentin Dupont <
> corentin.dupont at gmail.com> wrote:
> >> Hi the list!
> >> I have a simple question, how can I serialize/deserialize a structure
> like this:
> >>
> >> data InputChoice c  deriving Typeable
> >> data Event a where
> >>     InputChoice :: (Eq c, Show c) => [c] -> c -> Event (InputChoice c)
> >>  (...)
> >>
> >> I'd like that the values of type "c" get serialized to a String...
> That's the easy part, but for deserializing, oops!
> >>
> >> Cheers,
> >> Corentin
> >>
> >> _______________________________________________
> >> Haskell-Cafe mailing list
> >> Haskell-Cafe at haskell.org
> >> http://www.haskell.org/mailman/listinfo/haskell-cafe
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20121021/726337ae/attachment.htm>


More information about the Haskell-Cafe mailing list