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=1.0 required=5.0 tests=AWL,SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 1D933BC6B for ; Sat, 4 Aug 2007 18:15:43 +0200 (CEST) Received: from wx-out-0506.google.com (wx-out-0506.google.com [66.249.82.237]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l74GFg8G017385 for ; Sat, 4 Aug 2007 18:15:42 +0200 Received: by wx-out-0506.google.com with SMTP id h28so871165wxd for ; Sat, 04 Aug 2007 09:15:42 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=lLKNMG4ds8sjk2HWnZv8nEX9NKwAMCxCNcsXQZ/sSgOn+Hw7OKTtp7HWpzh/nJzBZZu+b/6+BVj4D1EZcHK3MYv0vvScpbqQTTbUcL0q0hF52t9cp7lyeZ1CA0hBmpr7mxuGOywLhRFyYFFckHrz2zv1CqteeoMomOdgyK5qW5o= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=EF2OzF05L8s/L3XwyYCH9P1vpU3SzEKcfjmkbVE00MwTL/j9Ycvnu57qyeM6M9Jowq+qP5+78m2fSH8KkcpdTJsAiKTf9VNHrnhQYC+Zoh++Cj6DN7ssbbybOYQ6JRoNnDzoAp7jrj3RRn24ODXLU7Fc+52xq/zIni0ZKvqP3fA= Received: by 10.90.99.20 with SMTP id w20mr3845113agb.1186244141986; Sat, 04 Aug 2007 09:15:41 -0700 (PDT) Received: by 10.90.101.16 with HTTP; Sat, 4 Aug 2007 09:15:41 -0700 (PDT) Message-ID: <6dbd4d000708040915l1d8b62cbi265244983bbc5e71@mail.gmail.com> Date: Sat, 4 Aug 2007 10:15:41 -0600 From: "Denis Bueno" To: "Arnaud Spiwack" Subject: Re: [Caml-list] Instanciating functor types with extra parameters Cc: "Caml List" In-Reply-To: <46B4A343.5030900@lix.polytechnique.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <46B4A343.5030900@lix.polytechnique.fr> X-j-chkmail-Score: MSGID : 46B4A62E.000 on discorde : j-chkmail score : X : 0/20 1 0.000 -> 1 X-Miltered: at discorde with ID 46B4A62E.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; functor:01 lix:01 functor:01 orderedtype:01 sig:01 val:01 orderedtype:01 struct:01 genericity:01 occurences:01 rewriting:01 struct:01 pervasives:01 pervasives:01 functors:01 On 8/4/07, Arnaud Spiwack wrote: > When I have a functor type, like for example (not too innocent I'm afraid) : > > module type OrderedType = > sig > type t > val compare : t -> t -> int > end > > > Something that naive intuition would allow you to do is something like : > > module GenOrder : OrderedType = > struct > type t = 'a > let compare = compare > end > [... snip ...] > My point is that I know absolutely no way of doing such a thing. Hence I > can't make a set with total or partial genericity. If I want to add type > parameters I have to rewrite the *whole* set library. Actually... I've > got to copy past it, and replace all occurences of t with an 'a t > for instance. Or ('a,'b) t if I have two arguments. I really had to do > that once, and almost a couple of other time. That's why it infuriates > me as I said earlier. You can do something slightly less general that certainly doesn't require rewriting the whole set library: One comparator definition per set you'd like to create. module StringOrder : OrderedType = struct type t = string (* just to be clear about which compare to capture) let compare = Pervasives.compare end module IntOrder : OrderedType = struct type t = int let compare = Pervasives.compare end ... and so on. You can use these to create functors, of course: module StringSet = Set.Make (StringOrder) module IntSet = Set.Make (IntSet) I think you'll find, though, as time goes on that you'll use special comparators: that is, not just generic string comparison, but some other total order on strings. And you'll want to name MySpecialStringCompare accordnigly. module CaseInsensitiveStrOrder : OrderedType = struct type t = string let compare x y = Pervasives.compare (downcase x) (downcase y) end module CaseInsensitiveSet = Set.Make (CaseInsensitiveStrOrder) Denis -- "Program testing can be used to show the presence of bugs, but never to show their absence." -- Edsger Dijkstra