From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: Caml-list@yquem.inria.fr Delivered-To: Caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 93AF7BBCA for ; Mon, 17 Mar 2008 13:37:49 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApoEAAYD3keCNhAB/2dsb2JhbACnEQ X-IronPort-AV: E=Sophos;i="4.25,512,1199660400"; d="scan'208";a="23861148" Received: from kurims.kurims.kyoto-u.ac.jp ([130.54.16.1]) by mail4-smtp-sop.national.inria.fr with ESMTP; 17 Mar 2008 13:37:48 +0100 Received: from localhost (orion [130.54.16.5]) by kurims.kurims.kyoto-u.ac.jp (8.13.8/8.13.8) with ESMTP id m2HCbYA7009907; Mon, 17 Mar 2008 21:37:34 +0900 (JST) Date: Mon, 17 Mar 2008 21:37:28 +0900 (JST) Message-Id: <20080317.213728.191427528.garrigue@math.nagoya-u.ac.jp> To: serp@stork.ru Cc: Caml-list@yquem.inria.fr Subject: Re: [Caml-list] Polymorphic method and polymorphic type From: Jacques Garrigue In-Reply-To: <47DE4635.7050101@stork.ru> References: <47DE4635.7050101@stork.ru> X-Mailer: Mew version 4.2 on Emacs 22.1 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; checker:01 polymorphic:01 polymorphic:01 abstract:01 caml-list:01 functions:01 constraint:01 int:01 int:01 explicitly:02 expression:02 match:02 match:02 variables:02 string:02 From: SerP > Simple example. All functions are fake. > > class getter = object method add_some p = p+1 end;; > type 'a get_mode = [ `Read | `Watch of (#getter as 'a)];; > let string_of_get_mode (gm: 'a get_mode) = match gm with `Read -> "Read" > | `Watch g -> let i = g#add_some 1 in ("Watch "^ (string_of_int i));; > class c = object method pgm : 'a. ('a get_mode) -> string = > string_of_get_mode end;; > -------------- > This expression has type 'a. (#getter as 'a) get_mode -> string > but is here used with type 'b. (#getter as 'b) get_mode -> string > Type #getter as 'c = < add_some : int -> int; .. > > is not compatible with type 'c > > ==================== > Please help. Is it my error? how should we do that the right way? Unfortunately, explicitly polymorphic type variables inside constrained type parameters are not allowed. This is due to the way type expansion is done inside the type checker... It should be possible to solve it, but I'm afraid lots of places would have to be modified to provide comprehensive support. Currently, the way to do it is to avoid constrained type parameters. type 'a get_mode = [ `Read | `Watch of 'a];; let string_of_get_mode (gm: #getter get_mode) = match gm with `Read -> "Read" | `Watch g -> let i = g#add_some 1 in ("Watch "^ (string_of_int i));; class c = object method pgm : 'a. (#getter as 'a) get_mode -> string = string_of_get_mode end;; This should not be too difficult usually. However, there is no workaround if the type with the constraint is abstract. Jacques Garrigue