Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* Re: subtyping and inheritance
@ 1999-04-15 12:18 Giuseppe Castagna
  1999-04-15 16:02 ` Markus Mottl
  1999-04-20 12:38 ` Didier Remy
  0 siblings, 2 replies; 16+ messages in thread
From: Giuseppe Castagna @ 1999-04-15 12:18 UTC (permalink / raw)
  To: caml-list; +Cc: mottl

Sorry to reopen a thread that is more than 2 months old, but I'm not on the
Caml-List and just today Hugo Herbelin hinted me that some of my work has been
discussed on it. So let me add my two cents worth contribution. 


> [...]
> > So far it seems that things would be unsafe with covariance. But now,
> > Castagna answers my (former) question, whether making "reappear" methods
> > from ancestors would be safe: it is...
> > 
> > The paper looked difficult at first, but turned out to be surprisingly
> > easy to read: Castagna makes the theorie very intuitively clear with his
> > examples of classes "2DPoint" and "3DPoint" and how methods are chosen
> > in the different models.
> > 
> > The record based method (as found in OCAML - the object (record)
> > determines, which method is selected, arguments are not considered)
> > can be obviously extended to support covariance.
> 
> However, it is not possible to apply this extension to Ocaml. Indeed,
> it requires that methods are chosen depending on the dynamic type of
> their arguments. But this information is not available in Ocaml.
> There are also difficulties for type inference.

Jerome is right when he says that dynamic type information is not available 
in Ocaml. Nevertheless I would not be so sure that this means that it is not 
possible to apply this extension to Ocaml. Surely, it would require some 
modifications to the compiler, but all it would have to do is to mark some
particular methods and all the objects that could call these methods by a 
natural number (for those who know, some sort of De Brujin notation, that a
purist may consider as dynamic type information). How to do it in practice is 
described for the language O2 in [1] (but without the natural numbers since
in O2 the dynamic type information is available). This is obtained by simply
modifying the compiler without affecting the language. 

Furthermore if you do not mind to extend the language (notice I said "extend" 
and not "modify") then you can do very nice things. This has been done AND 
implemented for Java. The extension of Java by "encapsulated multi-methods" 
(that's how we christened them) is described in [2]. This is obtained by simply
adding a modifier to Java's syntax. We also have a compiler under Solaris that 
compiles this Java extension. It is interesting to note that it is a
conservative 
extension of Java, which simply means that if you feed the extended compiler by
a 
standard Java program you will obtain the same code as with the standard
compiler
(well, not exactly, object structures will have an extra useless slot).


Let me end with some advertising. Both references should be much easier to read 
than the paper of mine cited in the previous messages. They can be retrieved at
http://www.dmi.ens.fr/~castagna/pub.html
If somebody would like to try to implement it in OCaml I'm ready to help him on
the 
design part (e.g. a student looking for a subject for master thesis ;-)

Cheers

---Beppe---

[1] J.Boyland and G. Castagna. Type-Safe Compilation of Covariant
Specialization: 
A Practical Case. In Proc. of ECOOP'96. Lecture Notes in Computer Science, 
1098:3-25, 1996.

[2] J. Boyland and G. Castagna. Parasitic methods: an implementation of 
multi-methods for Java. In Proc. of OOPSLA'97. SIGPLAN Notices 32(10):66-76. 
1997

P.S. We did not made the compiler public available. So there is no doc, etc.,
etc. ...




^ permalink raw reply	[flat|nested] 16+ messages in thread
* subtyping and inheritance
@ 1999-01-11 18:52 Markus Mottl
  1999-01-15 15:02 ` Jerome Vouillon
  0 siblings, 1 reply; 16+ messages in thread
From: Markus Mottl @ 1999-01-11 18:52 UTC (permalink / raw)
  To: OCAML

Hello again,

this time a question about inheritance and subtyping...
Let's consider the following code snippet:

---------------------------------------------------------------------------
class foo =
object (self:'self)
  method x = "x"
  method eq (other:'self) = if self#x = other#x then true else false
end

class bar =
object
  inherit foo
  method y = "y"
end

let f = new foo
and b = new bar
;;

print_string (string_of_bool (f #eq f)); print_newline ();
print_string (string_of_bool (b #eq b)); print_newline ();
---------------------------------------------------------------------------

So far no problem. Class "bar" inherits from "foo" and adds a method.
The program will print out two times "true", because objects of type
"foo" can be tested for equivalence within their class and objects of type
"bar" as well.

But now, I would really like to compare objects of type "foo" with
objects of type "bar" as in:

---------------------------------------------------------------------------
print_string (string_of_bool (f #eq (b :> foo))); print_newline ();
---------------------------------------------------------------------------

This is, of course, not possible, because "bar" is not a subtype of
"foo" and thus, cannot be coerced to "foo".

But "bar" inherits everything from "foo" so I think it would be safe
to call methods ** as they are defined in "foo" **. I think the problem
occurs because of the following rule taken from the OCAML manual:

  Only the last definition of a method is kept: the redefinition in a
  subclass of a method that was visible in the parent class overrides
  the definition in the parent class.

This rule prohibits that definitions as they appear in the parent
class may be reused in case that an object of a subclass (in terms of
inheritance, not subtyping!) shall be seen from the parent's "point
of view". In the upper case, method "eq" is automatically redefined,
because it has the type "'self" in its signature.  Actually, *this*
is the problem, not the additional method "y" as is stated at the end
of the error message generated on the last "print_string...".

My specific problem is: I have a program with classes for "terminal"
and "nonterminal" symbols. Both inherit from a base class "symbol". The
symbol class inherits from a class "ord", which defines a virtual method
"compare". This allows me to compare terminals with terminals and
nonterminals with nonterminals, but it also allows comparison between
symbols (all three classes redefine "compare").

Look at this graph:

         ord                ->  defines virtual method "compare"
          |
        symbol              ->  redefines "compare"
       /      \
   terminal  nonterminal    ->  both redefine "compare"

But the problem is: I will never be able to compare terminals and
nonterminals with each other, because the appropriate comparison method
as defined in "symbol" has been "forgotten" - it is redefined in all
child classes. Thus, I am not able to coerce them to "symbol".

I have worked around this with the following inheritance scheme:

    ---- ord -----          ->  defines virtual method "compare"
    |            |
    |   symbol   |          ->  has a method "compare_symbol"
    |  /      \  |
   terminal  nonterminal    ->  both redefine "compare"

Although it is now possible to coerce terminals and nonterminals
to symbols and have them thus compared via "compare_symbol", I am not
content with this scheme: I would really like to have all methods from
"ord" in "symbol", which all make use of the virtual "compare" method.

I am neither sure whether I am overlooking some clever trick to get the
result I want, nor whether a change in the handling of coercions ("memory"
for methods of parent classes) is really safe. But at the moment I cannot
think of any way to circumvent type safety. Comments on this welcome!

Best regards,
Markus

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl




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

end of thread, other threads:[~1999-04-21 16:58 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-04-15 12:18 subtyping and inheritance Giuseppe Castagna
1999-04-15 16:02 ` Markus Mottl
1999-04-20 12:38 ` Didier Remy
1999-04-20 15:06   ` Giuseppe Castagna
1999-04-21 12:18     ` Didier Remy
  -- strict thread matches above, loose matches on Subject: below --
1999-01-11 18:52 Markus Mottl
1999-01-15 15:02 ` Jerome Vouillon
1999-01-15 17:37   ` Markus Mottl
1999-01-18 19:55     ` Jerome Vouillon
1999-01-18 21:18       ` Markus Mottl
1999-01-20 11:50         ` Hendrik Tews
1999-01-25  0:08           ` Markus Mottl
1999-01-27 14:18             ` Jerome Vouillon
1999-01-27 14:45               ` Markus Mottl
1999-01-28 19:40               ` Hendrik Tews
1999-01-27 14:28           ` Jerome Vouillon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox