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.4 required=5.0 tests=AWL,DNS_FROM_RFC_ABUSE autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 10A99BC69 for ; Mon, 6 Aug 2007 15:47:43 +0200 (CEST) Received: from dispatch.cs.umd.edu (dispatch.cs.umd.edu [128.8.128.60]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l76DlfoV025149 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Mon, 6 Aug 2007 15:47:42 +0200 Received: from [192.168.0.2] (c-69-243-63-39.hsd1.md.comcast.net [69.243.63.39]) (authenticated bits=0) by dispatch.cs.umd.edu (8.13.1/8.12.5) with ESMTP id l76DlTDA012219 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Mon, 6 Aug 2007 09:47:34 -0400 Message-ID: <46B72671.3040301@cs.umd.edu> Date: Mon, 06 Aug 2007 09:47:29 -0400 From: Mike Furr User-Agent: Mozilla-Thunderbird 2.0.0.4 (X11/20070622) MIME-Version: 1.0 To: Caml List Subject: Re: [Caml-list] Instanciating functor types with extra parameters References: <46B4A343.5030900@lix.polytechnique.fr> In-Reply-To: <46B4A343.5030900@lix.polytechnique.fr> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-CSD-MailScanner-Information: Please email staff@cs.umd.edu for more information X-CSD-MailScanner: Found to be clean X-CSD-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-4.389, required 5, autolearn=not spam, ALL_TRUSTED -1.80, AWL 0.01, BAYES_00 -2.60) X-CSD-MailScanner-From: furr@cs.umd.edu X-Miltered: at concorde with ID 46B7267D.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; functor:01 orderedtype:01 struct:01 struct:01 elt:01 node:01 elt:01 val:01 val:01 pervasives:01 orderedtype:01 cheers:01 -mike:01 polymorphic:01 wrote:01 Arnaud Spiwack wrote: > Something that naive intuition would allow you to do is something like : > > module GenOrder : OrderedType = > struct > type t = 'a > let compare = compare > end I had a similar problem while developing my OCaml-Reins data structure library (first release will be "real-soon-now"). Stephen Weeks offered the following solution: the main idea is that you build a generic core module that is parameterized by the maximum number of type variables desired. So for a set, you might define module BaseSet = struct type 'a elt_ type 'a set_ = Empty | Node of 'a set_ * 'a elt_ * 'a set_ val empty : 'a set_ val add : 'a elt_ -> 'a set_ -> 'a set_ ... val compare_ : ('a elt_ -> 'a elt_ -> int) -> 'a set_ -> 'a set_ -> int ... end Then, you can create a polymorphic version module PolySet = struct include BaseSet type 'a t = 'a set_ type 'a elt = 'a let compare = compare_ Pervasives.compare ... end or a monomorphic version: module MonoSet(C : OrderedType) = struct type elt = C.t type 'a elt_ = elt type t = C.t set_ let compare = compare_ C.compare end Thus allowing you to share all of the underlying code with only a little boilerplate. Cheers, -mike