caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ruchira Datta <datta@math.berkeley.edu>
To: caml-list@inria.fr
Subject: Re: [Caml-list] lablgtk polymorphic variants question
Date: Mon, 19 Mar 2001 21:23:26 -0800	[thread overview]
Message-ID: <20010319212326.A1126@blue1.berkeley.edu> (raw)

I had written:
>> I guess I don't fully understand polymorphic variants.  It would have seemed
>> to me that if (new GBin.frame) expects an argument of type Gtk.frame, then
>> something of type [ 'frame] should satisfy it.  Why should it be necessary
>> that all four variants [`widget|`container|`bin|`frame] be able to occur?
>> I thought that the inferred type of a function would always allow less 
>> variants in its argument.

Jacques Garrigue wrote:
>LablGTK uses an encoding of widget types, in variant types, were
>variant types are used as capacities. This can seem a bit unusual, but
>you can see it in the fact the Gtk.obj abstract type is contravariant.

The contravariance annotation became possible in 3.01, so the code failed
to compile for me while presumably the author succeeded in compiling it.

I see that the polymorphic variants chosen in LablGTK reflect the GTK
class hierarchy.  For example, in gtk.ml is the declaration

type radio_menu_item =
	[`widget|`container|`bin|`item|`menuitem|`checkmenuitem|`radiomenuitem]

and the class hierarchy says:

 GtkObject
  +GtkWidget
  | +GtkContainer
  | | +GtkBin
  | | | +GtkItem
  | | | | +GtkMenuItem
  | | | | | +GtkCheckMenuItem
  | | | | | | `GtkRadioMenuItem

I understand that the function being contravariant means the opposite of
>> I thought that the inferred type of a function would always allow less 
>> variants in its argument.
my last quoted sentence holds.  I can see that, for example, any function
which expects a bin object ([`widget|`container|`bin]) should also accept
a frame object ([`widget|`container|`bin|`frame]).  So, the functions
have to be contravariant, i.e., have to allow more variants in the 
argument.  Looking at mlgtk, I see that there the GTK class hierarchy
doesn't show up at all.  Everything is directly a subclass of
Gtk.Unsafe.gtkobject.  I guess you couldn't use the regular OCaml objects,
with a normal class hierarchy reflecting the GTK class hierarchy,
because the physical storage for the object actually resides elsewhere 
(i.e., under the control of the GTK C library)?  So, using polymorphic
variants instead of Ocaml objects lets you still keep the class hierarchy,
and your functions reflect subtypes properly given the proper variance
annotation.  You don't need to worry about virtual methods or anything, 
because the OCaml function just calls the C method anyway, which will take 
care of that.  Is my understanding correct?

Perhaps someone who understands the issues could give a small example
showing exactly what would be wrong with an OCaml class hierarchy
which just mirrors the GTK class hierarchy?

Thanks,

Ruchira Datta
datta@math.berkeley.edu
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


             reply	other threads:[~2001-03-20  5:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-03-20  5:23 Ruchira Datta [this message]
2001-03-20 14:03 ` Jacques Garrigue
  -- strict thread matches above, loose matches on Subject: below --
2001-03-19 17:33 Ruchira Datta
2001-03-19  6:13 Ruchira Datta
2001-03-19  9:43 ` Sven LUTHER
2001-03-19 13:58   ` Stefano Zacchiroli
2001-03-19 10:27 ` Jacques Garrigue

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=20010319212326.A1126@blue1.berkeley.edu \
    --to=datta@math.berkeley.edu \
    --cc=caml-list@inria.fr \
    /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;
as well as URLs for NNTP newsgroup(s).