From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id VAA26827; Sun, 14 Dec 2003 21:06:35 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id VAA26870 for ; Sun, 14 Dec 2003 21:06:34 +0100 (MET) Received: from mailhost.ens-lyon.fr (pluvier.ens-lyon.fr [140.77.167.5]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id hBEK6Xr25064 for ; Sun, 14 Dec 2003 21:06:33 +0100 (MET) Received: by mailhost.ens-lyon.fr with scanned-ok (Exim 3.35 #1 (Debian)) id 1AVcVc-0001Md-00 for ; Sun, 14 Dec 2003 21:06:32 +0100 Received: from mailhost.ens-lyon.fr ([127.0.0.1]) by localhost (pluvier [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 05074-07 for ; Sun, 14 Dec 2003 21:06:32 +0100 (CET) Received: from [140.77.128.119] (helo=ens-lyon.org) by mailhost.ens-lyon.fr with esmtp (Exim 3.35 #1 (Debian)) id 1AVcVc-0001MP-00 for ; Sun, 14 Dec 2003 21:06:32 +0100 Message-ID: <3FDCC2C9.8030609@ens-lyon.org> Date: Sun, 14 Dec 2003 21:06:33 +0100 From: Jean-Baptiste Rouquier User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20031024 Debian/1.5-2 X-Accept-Language: en MIME-Version: 1.0 To: "caml-list@inria.fr" Subject: Re: [Caml-list] polymorphic function not recognised as such References: <3FDCB684.5000203@socialtools.net> In-Reply-To: <3FDCB684.5000203@socialtools.net> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at ens-lyon.fr X-Loop: caml-list@inria.fr X-Spam: no; 0.00; ens-lyon:01 caml-list:01 checker:01 char:01 char:01 checker:01 segfault:01 segfaults:01 bug:01 faq:01 faq:01 beginner's:01 beginners:01 bin:01 compiler:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk The following type compare_things : thing -> thing -> ('a -> 'a -> bool) -> bool doesn't mean that comparison_fun has to be polymorphic, but it means that any function with type ('a -> 'a -> bool), where 'a is any type, is acceptable. For instance, the type checker would accept a function of the type (char -> char -> bool). If you force the type checker to accept your function, for instance this way (warning, awful code !): let compare_things t1 t2 (comparison_fun: 'a -> 'a -> bool) = match (t1, t2) with (Int i, _) -> comparison_fun (Obj.magic i) (Obj.magic (int_of_thing t2)) | (String s, _) -> comparison_fun (Obj.magic s) (Obj.magic (string_of_thing t2)) ;; then you can get a segfault if you don't give a polymorphic function to compare_things : let string_rev s = let l = String.length s in let result = String.create l in for i=0 to l-1 do result.[i] <- s.[l-i-1] done; result let my_comparison s s' = (string_rev s) > (string_rev s') let ok = compare_things (Int 0) (String "42") (<) let segfaults = compare_things (Int 0) (String "42") my_comparison work around: pass 2 args to compare_things, namely compare_int and compare_strings. Jean-Baptiste. Benjamin Geer wrote: > Can anyone explain to me why the following doesn't compile: > > ---------- > > type thing = Int of int | String of string ;; > > let int_of_thing t = > match t with > Int i -> i > | String s -> int_of_string s ;; > > let string_of_thing t = > match t with > Int i -> string_of_int i > | String s -> s ;; > > let compare_things t1 t2 comparison_fun = > match (t1, t2) with > (Int i, _) -> comparison_fun i (int_of_thing t2) > | (String s, _) -> comparison_fun s (string_of_thing t2) ;; > > let is_less_than t1 t2 = > compare_things t1 t2 (<) ;; > > let is_greater_than t1 t2 = > compare_things t1 t2 (>) ;; > > ---------- > > The compiler says: > > File "test.ml", line 16, characters 38-39: > This expression has type string but is here used with type int > > It seems to have decided that in the function compare_things, > comparison_fun is int -> int -> 'a because that's how it's used in the > first pattern, not realising that I mean to pass a polymorphic > function, 'a -> 'a -> bool. > > Is there a way to make this work? (Specifying the type of > comparison_fun as 'a -> 'a -> bool makes no difference.) > > Ben > > ------------------- > To unsubscribe, mail caml-list-request@inria.fr Archives: > http://caml.inria.fr > Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: > http://caml.inria.fr/FAQ/ > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners