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=1.0 required=5.0 tests=AWL,SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 51759BC0A for ; Wed, 28 Feb 2007 02:18:08 +0100 (CET) Received: from mu-out-0910.google.com (mu-out-0910.google.com [209.85.134.189]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l1S1I7Dt004472 for ; Wed, 28 Feb 2007 02:18:08 +0100 Received: by mu-out-0910.google.com with SMTP id w9so1735702mue for ; Tue, 27 Feb 2007 17:18:07 -0800 (PST) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=C1n1XMHFaYQE6k1p5wSoGdeoSfgr0+pY7kM7qVEnt00aSFluhTkS3fBRDM/nrBoudHHIkzP2bKeek94nYYQN8cDMZlt0rl8Lg5/0VK+Z616A93tFa07Seu1sjgGfuodfaR0jscX1fDZ6BWi9XEMHps3WkJw/m9Pe2GPjmv799XQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=EYc8KvQ+gBwpZiLJ0qKVkUXy++Mk3gYusW4KmDTi99d42oeCRNLx1guvz4sifxQgRaujhlT+G0qykp7rReq3c151jJ9DY8Eza1xN4zIfzPm9QCneAhQZ5bl8rXqZNpnMv94IErzKWf3WK4Ooko4d1y6gRiZpZa9sVbwXAH2VHXs= Received: by 10.82.178.11 with SMTP id a11mr2517494buf.1172625487612; Tue, 27 Feb 2007 17:18:07 -0800 (PST) Received: by 10.82.136.14 with HTTP; Tue, 27 Feb 2007 17:18:07 -0800 (PST) Message-ID: <4a708d20702271718r3fa74211jbf81372bc9fb8b79@mail.gmail.com> Date: Wed, 28 Feb 2007 02:18:07 +0100 From: "Lukasz Stafiniak" To: caml-list@inria.fr Subject: Re: [Caml-list] Fwd: "ocaml_beginners"::[] Trouble combining polymorphic classes and polymorphic methods In-Reply-To: <20070228.092306.92341518.garrigue@math.nagoya-u.ac.jp> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <301730110702251747y72ae9fbdqd33bd8d08293cbe3@mail.gmail.com> <301730110702271322m72d20cffh37bddb14dc91b6f9@mail.gmail.com> <20070228.092306.92341518.garrigue@math.nagoya-u.ac.jp> X-j-chkmail-Score: MSGID : 45E4D84F.001 on discorde : j-chkmail score : X : 0/20 1 0.000 -> 1 X-Miltered: at discorde with ID 45E4D84F.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; lukasz:01 ocaml:01 foo:01 foo:01 escapes:01 recursive:01 subtyping:01 polymorphic:01 polymorphic:01 beginners:01 arbitrary:01 wrote:01 compile:01 typing:01 caml-list:01 On 2/28/07, Jacques Garrigue wrote: > From: "Geoffrey Romer" > > > > I'm trying to create a polymorphic class 'a foo which has a > > polymorphic method that takes as a parameter another foo object, but > > one with arbitrary type. In other words, something like this: > > > > class virtual ['a] foo = > > object (self) > > method virtual bar : 'b. 'b foo -> unit > > end;; > > > > When I try to compile this, though, I get a warning that I "cannot > > quantify 'b because it escapes this scope". When I drop the " 'b. " it > > compiles fine, but the reported type for bar is 'a foo -> unit; i.e. > > it's no longer polymorphic. > > > > Is there a problem with trying to make a method polymorphic with > > respect to the class type in this way? How can I make this work? > > The reason it does not work is that recursive object types have to be > regular. That is, when using foo inside its own definition, the > parameters must be the same. Since 'a <> 'b, this fails. > > This restriction is due to the structural typing of objects, and as > such it does not apply to records, which are nominal. So you can > write: > type 'a foo = { bar : 'b. 'b foo -> unit; } > which has basically the same meaning. > Thank you for the answer. Let's play the dual of this game: # type 'a sty = [`Foo of (int * 'a) sty | `Bar];; Characters 4-45: type 'a sty = [`Foo of (int * 'a) sty | `Bar];; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ In the definition of sty, type (int * 'a) sty should be 'a sty # type 'a nty = Foo of (int * 'a) nty | Bar;; type 'a nty = Foo of (int * 'a) nty | Bar (I wonder if this is the reason when people rant that structural types are a weaker form of type control... ;) Recently I've started using closed polymorphic variant types to build subtyping hierarchies and this gives more type control to me, I think.)