caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
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 10:00:28 +0900	[thread overview]
Message-ID: <F31806A7-4DDD-4DCB-9D7E-299A04EABD72@gmail.com> (raw)
In-Reply-To: <C7AAD554-4DA5-4811-8AAA-84E14E46C31D@philou.ch>

On 2011/01/15, at 2:46, Philippe Strauss wrote:

> Hello ocaml users,
> 
> I'm in a fight with hindley-milner when writing a .mli for a set of classes which implement a pipeline pattern, for processing either float array or Complex.t array, caching the result in each pipeline element.
> 
> each element type (class) inherit from node_virt_t, which use parametrized type for circumventing around the lack of "c++" like method overloading, for having the ability to use either float array or Complex.t array as input/output data type.
> 
> the whole things works perfectly, but the .mli seems not easy to write.
> uncomment class node_spectrum_t or class node_mag_t and you'll get:
> 
> File "nodes.mli", line 68, characters 30-45:
> Error: The type parameter Complex.t array
>       does not meet its constraint: it should be float array
> 
> which I don't understand.

[...]
The problem can be tracked down to this single line:

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

Object types are allowed to be recursive, but they are restricted to _regular_ types,
where recursive occurences have identical type parameters.
In particular, this means that in the above line, ('a,'b) node_virt_t and ('b,'c) node_virt_t
must have the same parameters, i.e. that 'a = 'b and 'b = 'c, i.e. all your types
end up being identical.

But in your particular case, the recursion seems unnecessary.
You could write:

class type ['b, 'c] node_t :
   object
       method get_id : int -> int
       method private inc_id : int -> unit
       method virtual ptyp : unit -> node_parameters_t
       method virtual process : 'b -> 'c
       method private pstore : int -> int * int * 'c
       method get : int -> int * int * 'c
end

class virtual ['b, 'c] node_virt_t : ('a, 'b) node_t -> parameters_t -> int -> int ->
   object
       val previous : ('a, 'b) node_t
       val parms : parameters_t
       val hres : (int * int * string, 'c) Hashtbl.t
       val mutable id : int array
       inherit ['b,'c] node_t
end

Hopefully, this should solve your typing problem.

Of course, you might also consider whether you really need to use objects for that.
Closures are usually enough for cacheing, and they do not involve the advanced
aspects of the type system.

Jacques Garrigue



      parent reply	other threads:[~2011-01-15  1:00 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
2011-01-15  1:00 ` Jacques Garrigue [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=F31806A7-4DDD-4DCB-9D7E-299A04EABD72@gmail.com \
    --to=garrigue@math.nagoya-u.ac.jp \
    --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).