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 mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 2BADFBB84 for ; Fri, 26 Sep 2008 08:41:39 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApoEAFMh3EiD1+WR/2dsb2JhbAC0H4ZVPIFk X-IronPort-AV: E=Sophos;i="4.33,310,1220220000"; d="scan'208";a="17771663" Received: from alumnus.caltech.edu ([131.215.229.145]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 26 Sep 2008 08:41:38 +0200 Received: from alumnus.caltech.edu (localhost [127.0.0.1]) by alumnus.caltech.edu (8.13.6/8.13.6) with ESMTP id m8Q6f0cI019834 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Thu, 25 Sep 2008 23:41:00 -0700 (PDT) Received: (from txr@localhost) by alumnus.caltech.edu (8.13.6/8.12.3/Submit) id m8Q6enK4019826; Thu, 25 Sep 2008 23:40:49 -0700 (PDT) Date: Thu, 25 Sep 2008 23:40:49 -0700 (PDT) Message-Id: <200809260640.m8Q6enK4019826@alumnus.caltech.edu> From: Tim Rentsch To: Sebastien.Hinderer@ens-lyon.org Cc: caml-list@yquem.inria.fr In-reply-to: <20080925222439.GA7968@galois> (message from =?iso-8859-1?Q?S=E9bastien?= Hinderer on Fri, 26 Sep 2008 00:24:39 +0200) Subject: Re: [Caml-list] mutually dependent class and type Reply-To: txr@alumni.caltech.edu References: <20080925222439.GA7968@galois> X-alumnus.caltech.edu-MailScanner-Information: Please contact the ISP for more information X-alumnus.caltech.edu-MailScanner-ID: m8Q6f0cI019834 X-alumnus.caltech.edu-MailScanner: Found to be clean X-MailScanner-From: txr@alumnus.caltech.edu X-Spam: no; 0.00; ens-lyon:01 recursive:01 ocaml:01 functors:01 abstract:01 caml-list:01 data:02 data:02 define:02 define:02 modules:02 defined:02 defined:02 parameter:02 string:02 > Date: Fri, 26 Sep 2008 00:24:39 +0200 > From: =?iso-8859-1?Q?S=E9bastien?= Hinderer > > Is there a (clean) way to define simultaneously a class and a type that > are mutually recursive ? > Something like this : > class element (c : content) = > object > ... > end and type content = > | Data of string > | Elements of element list;; > > This is of course not a valid OCaml definition, but is there a way to > express it ? Frequently a good way around such problems is to abstract the type definition with respect to the mutually dependent defined item. Then, supply the appropriate parameter when defining the class: type 'a generic_content = | Data of string | Elements of 'a list class element (c : element generic_content) = object method c_value = c end Voila! The class is defined in terms of a type that depends on the class. For classes, this technique produces a nicer result if applied to a class type rather than a class directly. Then we can define the desired type 'content' appropriately and use it in the class definition: type 'a generic_content = | Data of string | Elements of 'a list class type element_t = object method c_value : element_t generic_content end type content = element_t generic_content class element (c : content) = object method c_value = c end This technique of parameterizing a type definition with respect to a dependent type is a good one to know; it also is sometimes useful with modules/functors.