
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

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 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

On Sun, Oct 21, 2012 at 12:39 PM, Corentin Dupont wrote: Nobody on this one?
Here is a simplified version: data Event a where
InputChoice :: a -> Event a How to serialize/deserialize this? How were you expecting to serialize/deserialize a function?
--
brandon s allbery kf8nh sine nomine associates
allbery.b@gmail.com ballbery@sinenomine.net
unix/linux, openafs, kerberos, infrastructure http://sinenomine.net

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
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
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

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
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
написал(а): 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@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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Sun, Oct 21, 2012 at 07:20:10PM +0200, Corentin Dupont 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...
Can't you simply, when defining the type event, add a "deriving (Show, Read)"? Then the standard (but slow) read/show serialisation would work for you. If you're asking to de-serialise an unknown type (i.e. you don't know what type it should restore a-priori, but you want to do that based on the contents of the file), things become a little more complex. Unless you can further restrict the type 'a', really complex. Maybe stating your actual problem, rather than the implementation question, would be better? regards, iustin

Hi Iustin,
yes I want to deserialize an unknown type based on the content of the file
(in this example).
Let's say I can reduce the spectum of types to: Strings, all the types in
Enum, Ints. Is it possible?
My real problem is that on my web interface I want to use web routes to
allow a user to pass an Event back to the engine.
The problem is that it seems that web-routes only accepts Strings (or some
known types) to be passed on the web route, whereas I need to pass random
types.
On Sun, Oct 21, 2012 at 8:00 PM, Iustin Pop
On Sun, Oct 21, 2012 at 07:20:10PM +0200, Corentin Dupont 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...
Can't you simply, when defining the type event, add a "deriving (Show, Read)"? Then the standard (but slow) read/show serialisation would work for you.
If you're asking to de-serialise an unknown type (i.e. you don't know what type it should restore a-priori, but you want to do that based on the contents of the file), things become a little more complex. Unless you can further restrict the type 'a', really complex.
Maybe stating your actual problem, rather than the implementation question, would be better?
regards, iustin

On Sun, Oct 21, 2012 at 08:11:50PM +0200, Corentin Dupont wrote:
Hi Iustin, yes I want to deserialize an unknown type based on the content of the file (in this example). Let's say I can reduce the spectum of types to: Strings, all the types in Enum, Ints. Is it possible?
Maybe :) I don't know how to do "all the types in enum". For Strings, Ints and Float, you could have something like: serialise a pair instead of just the type, and decode: data Event = EventString String | EventInt Int | EventFloat Float … (kind, val) <- read in_data return $ case kind of "string" -> EventString val "int -> EventInt (read val) "float" -> EventFloat (read val) But this is only for a very few specific types.
My real problem is that on my web interface I want to use web routes to allow a user to pass an Event back to the engine. The problem is that it seems that web-routes only accepts Strings (or some known types) to be passed on the web route, whereas I need to pass random types.
Are you sure you need to pass random types? Can't you define a (large) set of known types, for example? regards, iustin
On Sun, Oct 21, 2012 at 8:00 PM, Iustin Pop
wrote: On Sun, Oct 21, 2012 at 07:20:10PM +0200, Corentin Dupont 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...
Can't you simply, when defining the type event, add a "deriving (Show, Read)"? Then the standard (but slow) read/show serialisation would work for you.
If you're asking to de-serialise an unknown type (i.e. you don't know what type it should restore a-priori, but you want to do that based on the contents of the file), things become a little more complex. Unless you can further restrict the type 'a', really complex.
Maybe stating your actual problem, rather than the implementation question, would be better?
regards, iustin

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).
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?
On Oct 21, 2012, at 9:20 PM, Corentin Dupont
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
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
написал(а): 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
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Sun, Oct 21, 2012 at 8:42 PM, MigMit
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
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
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
написал(а): 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@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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

You can include the type in the serialized string. When recovering you can
read the type and use to look for the appropriate deserializer in a lookup
table where you have registered the deserializer.
I use this trick in the IDynamic package,. that serializes-deserializes
dynamic types:
http://hackage.haskell.org/package/IDynamic-0.1
2012/10/20 Corentin Dupont
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Alberto.
participants (5)
-
Alberto G. Corona
-
Brandon Allbery
-
Corentin Dupont
-
Iustin Pop
-
MigMit