Re: Problème d'oubli dans un module [A problem with modul

From: Xavier Leroy (Xavier.Leroy@inria.fr)
Date: Sun Nov 07 1999 - 15:21:16 MET


Date: Sun, 7 Nov 1999 15:21:16 +0100
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: Valentin Bonnard <bonnard@clipper.ens.fr>, caml-list@inria.fr
Subject: Re: =?iso-8859-1?Q?Problème_d'oubli_dans_un_module_=5BA_problem_with_modul?=
In-Reply-To: <19991030044139.30767@soling.ens.fr>; from Valentin Bonnard on Sat, Oct 30, 1999 at 04:41:39AM +0200

> [ I can still use ``get'': ]
>
> # let z = Obj.magic y;;
> val z : '_a = <poly>
> # print_int (z#get);;
> 3- : unit = ()
>
> [ And it works, I understand that it is guarantied to work. ]

No, it's not. To begin with, any piece of code that contains
"Obj.magic" is not guaranteed to work. (You'd be amazed at how many
type-based compiler optimizations can be broken by innocuous-looking
uses of Obj.magic. I spent one full day once tracking a GC "bug" in
ocamlopt that had been reported to us; the "bug" turned out to be one
such use of Obj.magic in the user's code. Since then, I've been
grepping for "Obj.magic" in every bug report that we get, and throwing
away immediately those where it occurs. You've been warned!)

Second, you're making assumptions on how subtyping coercions are
compiled. (See below.)

> [ Now, I will try to do the same thing with modules. ]
> [ Now, how can I use ``get'' ? ]
> # (Obj.magic Y: sig val get : int end).get;;

You can't. First of all, there is no module-level equivalent of
Obj.magic. (Thanks God.) Second, the run-time representation of Y
really contains no "get" member, it's just the empty tuple.

Subtyping between modules is coercitive, meaning that at the point
where a module is viewed with a supertype, the run-time representation
of the module changes to reflect the change in type. This allows to
compile accesses to structure fields by offsets (computed from the
signature of the structure) rather than by name, which is good for
efficiency.

In contrast, subtyping between objets doesn't change the object
representation in the OCaml compiler. There are several reasons for
this, one being that it makes it much easier to preserve object
identity (for objects having mutable instance variables). But one
could come up with compilation schemes where subsumption on objects
changes the internal representation.

> [ I'd like to access the forgotten get member.
> In case you are wondering, yes, this need arises
> from a real problem. ]

What about looking for a type-safe solution to that real problem?

- Xavier Leroy



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:28 MET