caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeremy Yallop <yallop@gmail.com>
To: Philippe Strauss <philou@philou.ch>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] writing a not too simple parametrized class interface
Date: Sat, 15 Jan 2011 00:53:04 +0000	[thread overview]
Message-ID: <AANLkTikgyY90U0v=83ss+ZO2gCiO15jahRXffTBMf_Vs@mail.gmail.com> (raw)
In-Reply-To: <C7AAD554-4DA5-4811-8AAA-84E14E46C31D@philou.ch>

On 14 January 2011 17:46, Philippe Strauss <philou@philou.ch> wrote:
> the whole things works perfectly, but the .mli seems not easy to write.

It can be useful in such circumstances to ask ocamlc to print out an
interface (.mli) for you.  Once you've written your implementation
(nodes.ml), you can run

    ocamlc -i nodes.ml

to dump the interface inferred by OCaml.  The result is not always a
good starting point for your actual interface file, since any
structural types such as objects (or polymorphic variants) will be
expanded in full, and will not reflect the inheritance (or variant
extension) that you used in your code.  Still, it can be helpful for
debugging.

You can also use ocamlc in this way with interface files as input, and
this turns out to be helpful in diagnosing your problem here.  Running

    ocamlc -i nodes.mli

on your code gives the following output for node_virt_t (in amongst
the other class types):

  class virtual ['a, 'b] node_virt_t :
    ('a, 'a) node_virt_t ->
    parameters_t ->
    int ->
    int ->
    object
      constraint 'b = 'a
      val hres : (int * int * string, 'a) Hashtbl.t
      val mutable id : int array
      val parms : parameters_t
      val previous : ('a, 'a) node_virt_t
      method get : int -> int * int * 'a
      method get_id : int -> int
      method private inc_id : int -> unit
      method virtual process : 'a -> 'a
      method private pstore : int -> int * int * 'a
      method virtual ptyp : unit -> node_parameters_t
    end

If you compare this with your original type, you'll notice that all
your type variables have been unified: you wrote

   class virtual ['b, 'c] node_virt_t : ('a, 'b) node_virt_t -> ...

but ocamlc has changed this to

  class virtual ['a, 'b] node_virt_t : ('a, 'a) node_virt_t -> ...

and added a further constraint on the class parameters:

      constraint 'b = 'a

This has happened because you've used type variables inconsistently in
the type of node_virt_t: you've given it parameters 'b and 'c, but
used 'a, 'b and 'c to the right of the colon.

The following type for node_virt_t uses 'a and 'b more consistently,
and may more closely reflect your intentions:

 class virtual ['a, 'b] node_virt_t :
    ('a, 'b) node_virt_t ->
    parameters_t ->
    int ->
    int ->
    object
        val previous : ('a, 'b) node_virt_t
        val parms : parameters_t
        val hres : (int * int * string, 'b) Hashtbl.t
        val mutable id : int array
        method get_id : int -> int
        method private inc_id : int -> unit
        method virtual ptyp : unit -> node_parameters_t
        method virtual process : 'a -> 'b
        method private pstore : int -> int * int * 'b
        method get : int -> int * int * 'b
 end

  reply	other threads:[~2011-01-15  0:53 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-14 17:46 Philippe Strauss
2011-01-15  0:53 ` Jeremy Yallop [this message]
2011-01-15  1:00 ` 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='AANLkTikgyY90U0v=83ss+ZO2gCiO15jahRXffTBMf_Vs@mail.gmail.com' \
    --to=yallop@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=philou@philou.ch \
    /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).