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.6 required=5.0 tests=AWL,DNS_FROM_RFC_ABUSE, 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 BAAB0BC69 for ; Sun, 15 Oct 2006 05:40:53 +0200 (CEST) Received: from wx-out-0506.google.com (wx-out-0506.google.com [66.249.82.226]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id k9F3erO1031638 for ; Sun, 15 Oct 2006 05:40:53 +0200 Received: by wx-out-0506.google.com with SMTP id s7so1223077wxc for ; Sat, 14 Oct 2006 20:40:52 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=RR/O+mbZ3Wt45yqkAJIuBgq9geWWvsyVHdge9yV/vB/UZmrhkAgVMYcVp5UvUnp3Z64Qwzk29ExmPv+JVGvz71mAh9kNQ3cTcQJUZdEXitocFGMPh+kvE0hi5CUgsZ9xMST2hth9ZrEj2a2d5D2gGa4VZxw4XrPDSgJeJ8LK3dg= Received: by 10.70.118.4 with SMTP id q4mr8546405wxc; Sat, 14 Oct 2006 20:40:52 -0700 (PDT) Received: by 10.70.124.17 with HTTP; Sat, 14 Oct 2006 20:40:52 -0700 (PDT) Message-ID: Date: Sun, 15 Oct 2006 16:40:52 +1300 From: "Jonathan Roewen" To: "Denis Bueno" Subject: Re: [Caml-list] Type error Cc: "OCaml Mailing List" In-Reply-To: <6dbd4d000610142029m92d7005v65e95f031e7eae9b@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <6dbd4d000610142029m92d7005v65e95f031e7eae9b@mail.gmail.com> X-j-chkmail-Score: MSGID : 4531ADC5.000 on discorde : j-chkmail score : X : 0/20 1 X-Miltered: at discorde with ID 4531ADC5.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; val:01 val:01 bool:01 cmi:01 compilable:01 beginner's:01 ocaml:01 bug:01 beginners:01 wrote:01 compile:01 caml-list:01 caml-list:01 bin:01 caml:02 How about not defining a default, and matching on getter as an option? let of_array ?getter xs = match getter with | None -> Array.fold_right (fun x set -> add x set) xs empty | Some f -> Array.fold_right (fun x set -> add (f x) set) xs empty Jonathan On 10/15/06, Denis Bueno wrote: > I'm writing a simple (stupid) de-functorised Set library. Its skeleton is: > > ,---- > | type 'a t > | (** The type of sets. *) > | > | val empty: 'a t > | (** The empty set. *) > | > | val mem: 'a -> 'a t -> bool > | (** [mem x s] tests whether [x] belongs to the set [s]. *) > | > | val add: 'a -> 'a t -> 'a t > | (** [add x s] returns a set containing all elements of [s], > | plus [x]. If [x] was already in [s], [s] is returned unchanged. *) > `---- > > In my implementation, sets are lists. > > One of the things I'd like to do is write an of_array to create an > array from a set. However, it is often the case that I have an array > of objects from which I'd like to pull out one field & make a set of > those fields. So, my of_array looks like this: > > Interface: > ,---- > | val of_array: ?getter : ('b -> 'a) -> 'b array -> 'a t > | (** [of_array xs] returns a set containing the elements in [xs]. *) > `---- > > Implementation: > ,---- > | let of_array ?(getter = fun x -> x) xs = > | Array.fold_right (fun x set -> add (getter x) set) xs empty;; > `---- > > I get a type error when trying to compile this. I don't know how to > fix it, or even if it's possible without wrecking my nice of_array > signature. The type error: > > ,---- > | The implementation pSet.ml does not match the interface pSet.cmi: > | Values do not match: > | val of_array : ?getter:('a -> 'a) -> 'a array -> 'a list > | is not included in > | val of_array : ?getter:('a -> 'b) -> 'a array -> 'b t > `---- > > I understand why this shouldn't be compilable, but, is it possible to > do something similarly elegant (without creating a separate function)? > > -Denis > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs >