From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p2JHomTG024319 for ; Sat, 19 Mar 2011 18:50:48 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: At8GAOKJhE1V2gB4Vmdsb2JhbAClfwEWCw8XJcIgDYVWBJBS X-IronPort-AV: E=Sophos;i="4.63,211,1299452400"; d="scan'208";a="94458504" Received: from emailfrontal1.citycable.ch ([85.218.0.120]) by mail2-smtp-roc.national.inria.fr with SMTP; 19 Mar 2011 18:50:42 +0100 X-Alinto-smtpauth-localdomain: Yes Received: from seldon (unknown [85.218.93.111]) (Authenticated sender: guillaume.yziquel@citycable.ch) by emailfrontal1.citycable.ch (Postfix) with ESMTPA id 8079012C79E; Sat, 19 Mar 2011 18:50:41 +0100 (CET) Received: from yziquel by seldon with local (Exim 4.72) (envelope-from ) id 1Q10Fv-0000qq-7A; Sat, 19 Mar 2011 18:48:03 +0100 Date: Sat, 19 Mar 2011 18:48:03 +0100 From: Guillaume Yziquel To: Raphael Proust Cc: caml-list@yquem.inria.fr Message-ID: <20110319174803.GP20405@localhost> References: <201103191648.40345.raphlalou@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: <201103191648.40345.raphlalou@gmail.com> User-Agent: Mutt/1.5.20 (2009-06-14) Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id p2JHomTG024319 Subject: Re: [Caml-list] Recursive Parametric class type Typing Le Saturday 19 Mar 2011 à 16:48:40 (+0100), Raphael Proust a écrit : > Hi list, > > Trying to bind a javascript library for js_of_ocaml, I encountered the following > pattern (here drastically simplified): > > class type ['t] c = > object > method plus: 't -> unit > method minus: unit -> 't > method container: unit -> container > end > and container = > object > method int: unit -> int c > method string: unit -> string c > end > > The following error is raised at compile time: > Error: This type string should be an instance of type int > > In the use case: > - instead of [int] and [string] there are six different [class type]s; > - [classe type]s have more methods; and > - there are additional recursive [class type]s > > Why is there such a limitation on the types? Is there a work around that > wouldn't induce (too much) code duplication? The reason why there is this behaviour is unification within the 'and' declaration. method int says that 't in ['t] c is of type int, therefore it should also be an int c in method string instead of a string c. Workaround: Use recursive modules. They are a solution for keeping recursion while breaking type unification. Something in the taste of: yziquel@seldon:~$ ocaml Objective Caml version 3.12.0 # module rec Q : sig class type ['w] e = object method r : 'w end end = struct class type t = object method z : int Q.e method u : string Q.e end class type ['w] e = object method r : 'w end end;; module rec Q : sig class type ['a] e = object method r : 'a end end Not sure if this fits your bill when it comes to avoiding code duplication. -- Guillaume Yziquel