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.0 required=5.0 tests=none 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 AEE97BBC4 for ; Wed, 1 Apr 2009 05:25:17 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Am0BAPR60knZSMDqi2dsb2JhbACBUpRBAQEBCgsKBw8Ft1yDegY X-IronPort-AV: E=Sophos;i="4.39,305,1235948400"; d="scan'208";a="37614783" Received: from fmmailgate03.web.de ([217.72.192.234]) by mail4-smtp-sop.national.inria.fr with ESMTP; 01 Apr 2009 05:25:17 +0200 Received: from smtp05.web.de (fmsmtp05.dlan.cinetic.de [172.20.4.166]) by fmmailgate03.web.de (Postfix) with ESMTP id D114DF9C8FA0; Wed, 1 Apr 2009 05:25:16 +0200 (CEST) Received: from [78.43.226.218] (helo=frosties.localdomain) by smtp05.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.110 #277) id 1Lor4m-0004La-00; Wed, 01 Apr 2009 05:25:16 +0200 Received: from mrvn by frosties.localdomain with local (Exim 4.69) (envelope-from ) id 1Lor4l-0000zT-Ql; Wed, 01 Apr 2009 05:25:15 +0200 From: Goswin von Brederlow To: peng.zang@gmail.com Cc: caml-list@yquem.inria.fr, Goswin von Brederlow Subject: Re: [Caml-list] Bug? Constraints get ignored in methods References: <87ab71ic1f.fsf@frosties.localdomain> <200903312124.38637.peng.zang@gmail.com> Date: Wed, 01 Apr 2009 05:25:15 +0200 In-Reply-To: <200903312124.38637.peng.zang@gmail.com> (Peng Zang's message of "Tue, 31 Mar 2009 21:24:36 -0400") Message-ID: <87vdppdpic.fsf@frosties.localdomain> User-Agent: Gnus/5.110006 (No Gnus v0.6) XEmacs/21.4.21 (linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: goswin-v-b@web.de X-Sender: goswin-v-b@web.de X-Provags-ID: V01U2FsdGVkX19htwEXwNZfS/RCIKKDWwHKgLXwVOPqFgUdghEe gHamvdEDyKQMjzU4QcqkbUMEqnBcgQDd8DBBixSuRZn80YbdL1 6fh3RPGpQ= X-Spam: no; 0.00; bug:01 mylist:01 escapes:01 ocaml:01 recursive:01 coercions:01 recursive:01 cheers:01 val:01 mutable:01 peng:98 peng:98 mfg:98 polymorphic:01 polymorphic:01 Peng Zang writes: > Hi, > > Here's an example of how constraints are specified for polymorphic methods. > In this example I define a list type which can compare to anything that is > foldable. > > class type ['a] foldable = object > method foldl : 'z. ('z -> 'a -> 'z) -> 'z -> 'z > end > > class type ['a] mylist = object > inherit ['a] foldable > method compare : 'z. ('a #foldable as 'z) -> int > end > > Direct application to your example would not work: > > # class virtual base = object > method virtual setnext : 'a. (#base as 'a) option -> unit > end > Error: This type scheme cannot quantify 'a : it escapes this scope. > # > > OCaml does not allow the recursive reference when the method is polymorphic. > One option is to just deal with coercions or a function that does it for you: > > class virtual base = object > method virtual setnext : base option -> unit > end > > let callsetnext (obj:#base) (n:#base option) = > obj#setnext (n :> base option) That is the part I wanted to clean up / simplify. Doesn't look nice if some methods are called with # and others need the wrapper function. > Another option is to factor out the basic operations you need like the in list > example. I didn't make the list compare method work with other lists, I made > it more general to work with anything that is foldable. This avoids the > recursive reference because foldable is defined ahead of time. That was what I did except with virtual methods where the implementation is type specific. > Cheers, > > Peng So something like this: class type linked = object val mutable next : #linked option end class type base_type = object inherit linked method set_next : 'a. (#linked as 'a) option -> unit end That could actually work. I hope. Thanks. MfG Goswin