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 concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 56311BC68 for ; Sun, 15 Oct 2006 13:25:27 +0200 (CEST) Received: from pilet.ens-lyon.fr (pilet.ens-lyon.fr [140.77.167.16]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id k9FBPMnw006771 for ; Sun, 15 Oct 2006 13:25:27 +0200 Received: from localhost (localhost [127.0.0.1]) by pilet.ens-lyon.fr (Postfix) with ESMTP id B522915B9C8; Sun, 15 Oct 2006 13:25:18 +0200 (CEST) Received: from pilet.ens-lyon.fr ([127.0.0.1]) by localhost (pilet [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 04997-27; Sun, 15 Oct 2006 13:25:18 +0200 (CEST) Received: from [192.168.1.2] (ALyon-151-1-109-154.w86-209.abo.wanadoo.fr [86.209.224.154]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (Client did not present a certificate) by pilet.ens-lyon.fr (Postfix) with ESMTP id E61E215B9C5; Sun, 15 Oct 2006 13:25:17 +0200 (CEST) In-Reply-To: <6dbd4d000610142029m92d7005v65e95f031e7eae9b@mail.gmail.com> References: <6dbd4d000610142029m92d7005v65e95f031e7eae9b@mail.gmail.com> Mime-Version: 1.0 (Apple Message framework v752.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <1B6343A2-A779-4BB7-8A5E-64DE51562035@ens-lyon.fr> Cc: caml-list@inria.fr Content-Transfer-Encoding: 7bit From: Etienne Miret Subject: Re: [Caml-list] Type error Date: Sun, 15 Oct 2006 13:26:21 +0200 To: Denis Bueno X-Mailer: Apple Mail (2.752.2) X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at ens-lyon.fr X-Miltered: at concorde with ID 45321AA3.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; ens-lyon:01 val:01 val:01 2006:98 wrote:01 char:01 char:01 compile:01 caml-list:01 int:01 int:01 argument:02 argument:02 poly:02 poly:02 On oct 15 2006 at 05:29, Denis Bueno wrote: > ,---- > | val of_array: ?getter : ('b -> 'a) -> 'b array -> 'a t > | (** [of_array xs] returns a set containing the elements in > [xs]. *) > `---- > > 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. Your very nice of_array signature... I'm afraid you're signature is awful. Basically you're trying to get a 'a -> 'b type, which is impossible without using Obj.magic, and awful anyway. See, if you do # let my_array = [| (1,'a') ; (2,'b') |];; val my_array : (int * char) array = [|(1, 'a'); (2, 'b')|] # let set = of_array ~getter:snd my_array;; val set : char list = ['a'; 'b'] This is okay, but if you decide not to give the optional argument: # let set2 = of_array my_array Then what type should be inferred for set2? of course the real type is (int * char) t, but with the signature you gave for of_array you cannot infer it, since they're is no way to know that the default value for the optional argument has the type 'a -> 'a. By implementing of_array with Obj.magic, you get: # let set2 = of_array my_array;; val set2 : 'a list = [; ] and then you can do funny (and ugly) things: # add 4 set2;; - : int list = [4; 354636; 354630] # add 'c' set2;; - : char list = ['c'; '\ 36'; '\ 30'] # add (3,'c') set2;; - : (int * char) list = [(1, 'a'); (2, 'b'); (3, 'c')] I implemented of_array this way: # let of_array ?(getter = fun x -> Obj.magic x) xs = Array.fold_right (fun x set -> add (getter x) set) xs empty;; val of_array : ?getter:('a -> 'b) -> 'a array -> 'b list = Regards, Etienne