Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jerome Vouillon <Jerome.Vouillon@inria.fr>
To: Anton Moscal <msk@post.tepkom.ru>, Didier.Remy@inria.fr
Cc: caml-list@inria.fr
Subject: Re: Objects as sums
Date: Mon, 30 Nov 1998 13:35:53 +0100	[thread overview]
Message-ID: <19981130133553.45757@pauillac.inria.fr> (raw)
In-Reply-To: <Pine.LNX.4.03.9811281337550.22023-100000@post.tepkom.ru>; from Anton Moscal on Sat, Nov 28, 1998 at 01:46:12PM +0300

On Sat, Nov 28, 1998 at 01:46:12PM +0300, Anton Moscal wrote:

[...]
> This is not solution of my task. I want to simulate by ocaml classes the
> following C++ program:
> 
> # include <stdlib.h>
> 
> enum {NODE, LEAF};
> class Node;
> class Leaf;
> class Tree { public:
>   virtual int tag () = 0;
>   virtual Node * node () { abort (); return 0; }
>   virtual Leaf * leaf () { abort (); return 0; }
> };
> 
> class Leaf: public Tree { public:
>   int val;
>   int tag () { return LEAF; }
>   Leaf * leaf () { return this; }
> };
> 
> class Node: public Tree { public:
>   Tree * l, * r;
>   int tag () { return NODE; }
>   Node * node () { return this; }
> };
> 
> int sum (Tree * tree)
> {
>   switch (tree -> tag ()) 
>     {
>     case LEAF: return tree -> leaf () -> val;
>     case NODE: return sum (tree -> node () -> l) + sum (tree -> node () -> r);
>     }
> }
>
[...]
> 
> PS: this trick implements type-safe conversion down to objects hierarchy.
>     Question of my interest is the following: whether such
>     conversion is possible or not?

Yes, it is possible to translate the program into Ocaml.

The difficulty you have probably encounter is that in class "leaf",
for instance, the type of self is not "leaf", but a more general type.
The implementation of method "full_object" below must therefore
contain a coercion. As it is not possible to coerce to a type that is
not yet completely defined (this is the case for type "leaf" at this
point), I used an extra class "leaf_intf" to provide a type for the
coercion.

I have used a single method "full_object" rather than two methods
"node" and "leaf". This way, there is no need to raise any
exception. But this change was not necessary.

Regards,

-- Jérôme

type ('a, 'b) alt = First of 'a | Second of 'b;;

class virtual tree = object
  method virtual full_object : (leaf_intf, node_intf) alt
end and virtual leaf_intf = object
  inherit tree
  method virtual v : int
end and virtual node_intf = object
  inherit tree
  method virtual l : tree
  method virtual r : tree
end;;

class leaf v0 = object (self)
  inherit leaf_intf
  method full_object = First (self :> leaf_intf)
  method v = v0
end;;

class node l0 r0 = object (self)
  inherit node_intf
  method full_object = Second (self :> node_intf)
  method l :> tree = l0
  method r :> tree = r0
end;;

let rec sum t =
  match t#full_object with
    First t' ->
      t'#v
  | Second t' ->
      sum (t'#l) + sum (t'#r);;

# sum (new node (new leaf 3) (new node (new leaf 1) (new leaf 2)) :> tree);;
- : int = 6




      reply	other threads:[~1998-11-30 18:39 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-10-27 20:35 Local opening of modules John Prevost
1998-11-10 16:04 ` Anton Moscal
1998-11-10 21:22   ` John Prevost
1998-11-25 15:17     ` Objects as sums Anton Moscal
1998-11-26 15:58       ` Didier Remy
1998-11-28 10:46         ` Anton Moscal
1998-11-30 12:35           ` Jerome Vouillon [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=19981130133553.45757@pauillac.inria.fr \
    --to=jerome.vouillon@inria.fr \
    --cc=Didier.Remy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=msk@post.tepkom.ru \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox