From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 8CDC7BBAF for ; Wed, 19 May 2010 14:24:31 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AlUCALd180vBMHhLmWdsb2JhbACSAowNFQEBAQEBCAsKBxEivQOFEAQ X-IronPort-AV: E=Sophos;i="4.53,263,1272837600"; d="scan'208";a="50875811" Received: from net.univ-savoie.fr ([193.48.120.75]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/EDH-RSA-DES-CBC3-SHA; 19 May 2010 14:24:31 +0200 Received: from post.bourget.univ-savoie.fr (post.bourget.univ-savoie.fr [193.48.120.73]) by net.univ-savoie.fr (8.12.3/jtpda-5.4) with ESMTP id o4JCOSFt023385 for ; Wed, 19 May 2010 14:24:28 +0200 Received: from [193.48.123.45] (d45.lama.univ-savoie.fr [193.48.123.45]) by post.bourget.univ-savoie.fr (8.12.3/jtpda-5.4) with ESMTP id o4JCOOPs005683 ; Wed, 19 May 2010 14:24:24 +0200 Message-ID: <4BF3D87D.6010805@univ-savoie.fr> Date: Wed, 19 May 2010 14:24:29 +0200 From: Christophe Raffalli User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.9) Gecko/20100423 Thunderbird/3.0.4 MIME-Version: 1.0 To: caml-list@inria.fr Subject: recursive module and types Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.33 (www . roaringpenguin . com / mimedefang) X-Spam: no; 0.00; christophe:01 raffalli:01 christophe:01 raffalli:01 univ-savoie:01 recursive:01 recursive:01 parametric:01 cheers:01 branching:01 sig:01 node:01 val:01 struct:01 node:01 Hello, I needed to define a (large) recursive type using sets basically as in the example below. What I do not like is that I need to repeat the definition of the type twice (in my real code the type is 100 lines long) ... Does anyone see a way to avoid it, apart from using parametric types, which is also done below Cheers, Christophe (* data type of finitely branching trees with sets for leafs ... *) module rec T : sig type t = Node of S.t | Leaf val compare : t -> t -> int end = struct type t = Node of S.t | Leaf let compare u v = match u, v with Leaf, Leaf -> 0 | Node u', Node v' -> S.compare u' v' | Leaf, Node _ -> -1 | Node _, Leaf -> 1 end and S : Set.S with type elt = T.t = Set.Make(T) (* a solution with parametric types pb: the parametric type is not very readable by itself especially when it will be longer *) type 'a pre_t = Node of 'a | Leaf module rec T' : sig type t = S'.t pre_t val compare : t -> t -> int end = struct type t = S'.t pre_t let compare u v = match u, v with Leaf, Leaf -> 0 | Node u', Node v' -> S'.compare u' v' | Leaf, Node _ -> -1 | Node _, Leaf -> 1 end and S' : Set.S with type elt = T'.t = Set.Make(T') (* The same as a functor to allow storing data in the tree *) module DT (D:Set.OrderedType) = struct module rec T : sig type t = Node of D.t * S.t | Leaf val compare : t -> t -> int end = struct type t = Node of D.t * S.t | Leaf let compare u v = match u, v with Leaf, Leaf -> 0 | Node (du,u'), Node (dv,v') -> (match D.compare du dv with 0 -> S.compare u' v' | c -> c) | Leaf, Node _ -> -1 | Node _, Leaf -> 1 end and S : Set.S with type elt = T.t = Set.Make(T) end -- Christophe Raffalli Universite de Savoie Batiment Le Chablais, bureau 21 73376 Le Bourget-du-Lac Cedex tel: (33) 4 79 75 81 03 fax: (33) 4 79 75 87 42 mail: Christophe.Raffalli@univ-savoie.fr www: http://www.lama.univ-savoie.fr/~RAFFALLI --------------------------------------------- IMPORTANT: this mail is signed using PGP/MIME At least Enigmail/Mozilla, mutt or evolution can check this signature. The public key is stored on www.keyserver.net ---------------------------------------------