caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] marshalling polymorphic variants
@ 2004-06-28 21:02 henri dubois-ferriere
  2004-06-29  7:55 ` Basile Starynkevitch [local]
  2004-06-29  8:45 ` Richard Jones
  0 siblings, 2 replies; 6+ messages in thread
From: henri dubois-ferriere @ 2004-06-28 21:02 UTC (permalink / raw)
  To: caml-list

hi, 
in the simple case below, it is possible to marshal out a ([`A | `B]
list), and then read it back as a [> `A | `B | `C] list.

can this behavior be counted on , or is it not something an
application can assume to always work?

thanks
henri



# Marshal.to_channel oc ([`A; `B] : [`A | `B] list) [];;
- : unit = ()

# let a = (Marshal.from_channel ic : [> `A| `B | `C] list);;
val a : [> `A | `B | `C ] list = [`A; `B]

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] marshalling polymorphic variants
  2004-06-28 21:02 [Caml-list] marshalling polymorphic variants henri dubois-ferriere
@ 2004-06-29  7:55 ` Basile Starynkevitch [local]
  2004-06-29  8:54   ` Jacques GARRIGUE
  2004-06-29  8:45 ` Richard Jones
  1 sibling, 1 reply; 6+ messages in thread
From: Basile Starynkevitch [local] @ 2004-06-29  7:55 UTC (permalink / raw)
  To: caml-list

On Mon, Jun 28, 2004 at 11:02:25PM +0200, henri dubois-ferriere wrote:

> in the simple case below, it is possible to marshal out a ([`A | `B]
> list), and then read it back as a [> `A | `B | `C] list.


I think that yes, because:

  1. marshalling don't care about type casts (hence you can shot in
     your leg if you abuse it) and is not type safe.

  2. polymorphic variants are (IIRC) represented by using the hashcode
     of their name, so `A is always `A in any polymorphic variant
     context.

Regards.

-- 
Basile STARYNKEVITCH -- basile dot starynkevitch at inria dot fr
Project cristal.inria.fr - (temporarily)
http://cristal.inria.fr/~starynke --- all opinions are only mine 

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] marshalling polymorphic variants
  2004-06-28 21:02 [Caml-list] marshalling polymorphic variants henri dubois-ferriere
  2004-06-29  7:55 ` Basile Starynkevitch [local]
@ 2004-06-29  8:45 ` Richard Jones
  2004-06-29 17:50   ` Alex Baretta
  1 sibling, 1 reply; 6+ messages in thread
From: Richard Jones @ 2004-06-29  8:45 UTC (permalink / raw)
  To: henri dubois-ferriere; +Cc: caml-list

On Mon, Jun 28, 2004 at 11:02:25PM +0200, henri dubois-ferriere wrote:
> hi, 
> in the simple case below, it is possible to marshal out a ([`A | `B]
> list), and then read it back as a [> `A | `B | `C] list.
> 
> can this behavior be counted on , or is it not something an
> application can assume to always work?

Better to use Obj.magic if you're explicitly trying to subvert the
type system?

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://www.j-london.com/
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
'There is a joke about American engineers and French engineers. The
American team brings a prototype to the French team. The French team's
response is: "Well, it works fine in practice; but how will it hold up
in theory?"'

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] marshalling polymorphic variants
  2004-06-29  7:55 ` Basile Starynkevitch [local]
@ 2004-06-29  8:54   ` Jacques GARRIGUE
  0 siblings, 0 replies; 6+ messages in thread
From: Jacques GARRIGUE @ 2004-06-29  8:54 UTC (permalink / raw)
  To: basile.starynkevitch; +Cc: caml-list

From: "Basile Starynkevitch [local]" <basile.starynkevitch@inria.fr>
> On Mon, Jun 28, 2004 at 11:02:25PM +0200, henri dubois-ferriere wrote:
> 
> > in the simple case below, it is possible to marshal out a ([`A | `B]
> > list), and then read it back as a [> `A | `B | `C] list.
> 
> 
> I think that yes, because:
> 
>   1. marshalling don't care about type casts (hence you can shot in
>      your leg if you abuse it) and is not type safe.
> 
>   2. polymorphic variants are (IIRC) represented by using the hashcode
>      of their name, so `A is always `A in any polymorphic variant
>      context.

Actually in this case there are no casts involved, as you can already
convert an [`A | `B] list into an [> `A | `B | `C] list.

# fun x -> (x : [`A | `B] list :> [> `A | `B | `C] list);;
- : [ `A | `B ] list -> [> `A | `B | `C ] list = <fun>

So, yes, this is safe, and intended to stay so, even between different
versions of the compiler (as long as we don't change the hash
function, and there is very little reason to do so.)

Jacques Garrigue

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] marshalling polymorphic variants
  2004-06-29  8:45 ` Richard Jones
@ 2004-06-29 17:50   ` Alex Baretta
  2004-06-29 21:54     ` henri dubois-ferriere
  0 siblings, 1 reply; 6+ messages in thread
From: Alex Baretta @ 2004-06-29 17:50 UTC (permalink / raw)
  To: Richard Jones, Ocaml

Richard Jones wrote:
> On Mon, Jun 28, 2004 at 11:02:25PM +0200, henri dubois-ferriere wrote:
> 
>>hi, 
>>in the simple case below, it is possible to marshal out a ([`A | `B]
>>list), and then read it back as a [> `A | `B | `C] list.
>>
>>can this behavior be counted on , or is it not something an
>>application can assume to always work?
> 
> 
> Better to use Obj.magic if you're explicitly trying to subvert the
> type system?
> 
> Rich.
> 

It's better to use C if you are explicitly trying to subvert the type 
system...

Alex

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] marshalling polymorphic variants
  2004-06-29 17:50   ` Alex Baretta
@ 2004-06-29 21:54     ` henri dubois-ferriere
  0 siblings, 0 replies; 6+ messages in thread
From: henri dubois-ferriere @ 2004-06-29 21:54 UTC (permalink / raw)
  To: Alex Baretta; +Cc: Richard Jones, Ocaml

actually i'm not the kind who tries to subvert the type system ;)

the scenario is as follows: an application saves state using some
polymorphic variants.
later the application has grown and adds more polymorphic variants to
the 'vocabulary' of state it might read in.
so, i wanted to be sure that the later application could still read
the earlier variants in a safe manner.

henri

On Tue, 29 Jun 2004 19:50:28 +0200, Alex Baretta <alex@baretta.com> wrote:
> 
> Richard Jones wrote:
> > On Mon, Jun 28, 2004 at 11:02:25PM +0200, henri dubois-ferriere wrote:
> >
> >>hi,
> >>in the simple case below, it is possible to marshal out a ([`A | `B]
> >>list), and then read it back as a [> `A | `B | `C] list.
> >>
> >>can this behavior be counted on , or is it not something an
> >>application can assume to always work?
> >
> >
> > Better to use Obj.magic if you're explicitly trying to subvert the
> > type system?
> >
> > Rich.
> >
> 
> It's better to use C if you are explicitly trying to subvert the type
> system...
> 
> Alex
> 
> 
> 
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2004-06-29 21:54 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-28 21:02 [Caml-list] marshalling polymorphic variants henri dubois-ferriere
2004-06-29  7:55 ` Basile Starynkevitch [local]
2004-06-29  8:54   ` Jacques GARRIGUE
2004-06-29  8:45 ` Richard Jones
2004-06-29 17:50   ` Alex Baretta
2004-06-29 21:54     ` henri dubois-ferriere

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).