From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p87LiYkH024343 for ; Wed, 7 Sep 2011 23:44:34 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Am8EAELlZ05KfVMzimdsb2JhbABDnRKKVAgUAQEBCgkNBxIGIoFGAQEBAQIBEgITEwYBOAEDAQsBBQUOODQBBQEcBi4Hh1OaQwqOEoU2iSgCAwaGBWAEh2qLSIUSgSiGNDyBR4Iz X-IronPort-AV: E=Sophos;i="4.68,347,1312149600"; d="scan'208";a="108070737" Received: from mail-gw0-f51.google.com ([74.125.83.51]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 07 Sep 2011 23:44:28 +0200 Received: by gwj17 with SMTP id 17so176739gwj.10 for ; Wed, 07 Sep 2011 14:44:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=sender:subject:mime-version:content-type:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; bh=jJ60U/IJg05LQhT45jTvmDUlb4kbMclu2p7IJ509q4Q=; b=A8RbDSdpAhAtg3t++wHFb9I3uw736Q5TRw07rpZ48pUsryh0w0GhqoCQ2MDX+zw1GF b4+cTHVoaUyu+UoipqDMF4TuSU/Knia3x1dDV75JU4OFWAoD/T+Me1Js3Gs2CVEaLUx0 GRohmvf4L/Vv8DN0yBovg+cAXBwBvHTN+54SU= Received: by 10.150.218.19 with SMTP id q19mr81504ybg.398.1315431867589; Wed, 07 Sep 2011 14:44:27 -0700 (PDT) Received: from [10.2.23.10] ([118.103.30.26]) by mx.google.com with ESMTPS id 11sm2332032ani.11.2011.09.07.14.44.18 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 07 Sep 2011 14:44:26 -0700 (PDT) Sender: Jacques Garrigue Mime-Version: 1.0 (Apple Message framework v1084) Content-Type: text/plain; charset=us-ascii From: Jacques Garrigue In-Reply-To: Date: Thu, 8 Sep 2011 06:44:02 +0900 Cc: caml-list@inria.fr Content-Transfer-Encoding: 7bit Message-Id: <14203FCD-811F-41CC-A0D7-5A360D4815CA@math.nagoya-u.ac.jp> References: To: Dmitry Grebeniuk X-Mailer: Apple Mail (2.1084) Subject: Re: [Caml-list] recursive module, object types, tying knot On 2011/09/08, at 6:29, Dmitry Grebeniuk wrote: > Hello. > > I have an usual question. I want to make some object > types (container-like) that can map the stored contents > with its method. For example, let the container will be > the simple list: > class ['a] (lst : list 'a) = object ... > method map : ('a -> 'b) -> 'b lst > > The compiler yields an error: > # class ['a] lst x = object > method map : 'b . ('a -> 'b) -> 'b lst > = fun f -> List.map f x end;; > Error: This type scheme cannot quantify 'b : > it escapes this scope. The answer is unfortunately short: there is no solution. Namely recursive types in ocaml must be regular. I.e., they must expand to a finite graph. In particular this means that, inside the class lst, all occurences of lst must have exactly the parameter 'a. As you have already found, you can avoid this problem by defining a record which "hides" the use of lst. Namely, you must break the cycle with a datatype, either record or sum type. Unfortunately this also means that this type has to belong to some module, and you also lose subtyping. Jacques Garrigue