<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/12/eac46552a7b577d9a89a07fc38092a70"
  from="Jean-Baptiste Rouquier &lt;jean-baptiste.rouquier@e...&gt;"
  author="Jean-Baptiste Rouquier"
  date="2003-12-14T20:06:35"
  subject="Re: [Caml-list] polymorphic function not recognised as such"
  prev="2003/12/4254f003f0a5d186c5e7a8b31e551054"
  next="2003/12/1380e77c5d5f6a5e2966de692bdc968a"
  prev-in-thread="2003/12/4254f003f0a5d186c5e7a8b31e551054"
  next-in-thread="2003/12/1380e77c5d5f6a5e2966de692bdc968a"
  prev-thread="2003/12/c9409b63462d80cc0a4b0355adf1e0b0"
  next-thread="2003/12/2539c2c91c0d6a1135376d2e330ca702"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] polymorphic function not recognised as such">
<msg 
  url="2003/12/4254f003f0a5d186c5e7a8b31e551054"
  from="Benjamin Geer &lt;ben@s...&gt;"
  author="Benjamin Geer"
  date="2003-12-14T19:14:17"
  subject="[Caml-list] polymorphic function not recognised as such">
<msg 
  url="2003/12/eac46552a7b577d9a89a07fc38092a70"
  from="Jean-Baptiste Rouquier &lt;jean-baptiste.rouquier@e...&gt;"
  author="Jean-Baptiste Rouquier"
  date="2003-12-14T20:06:35"
  subject="Re: [Caml-list] polymorphic function not recognised as such">
</msg>
<msg 
  url="2003/12/1380e77c5d5f6a5e2966de692bdc968a"
  from="Benjamin Geer &lt;ben@s...&gt;"
  author="Benjamin Geer"
  date="2003-12-14T21:38:31"
  subject="Re: [Caml-list] polymorphic function not recognised as such">
<msg 
  url="2003/12/22a7ccbbbae6a7e18cdc45f3ea6dabc4"
  from="Olivier Andrieu &lt;andrieu@i...&gt;"
  author="Olivier Andrieu"
  date="2003-12-14T22:06:18"
  subject="Re: [Caml-list] polymorphic function not recognised as such">
</msg>
<msg 
  url="2003/12/af83cb0ff0838dd0dc048fae73cb32e2"
  from="Michal Moskal &lt;malekith@p...&gt;"
  author="Michal Moskal"
  date="2003-12-15T12:11:02"
  subject="Re: [Caml-list] polymorphic function not recognised as such">
</msg>
</msg>
</msg>
</thread>

<contents>
The following type
    compare_things : thing -&gt; thing -&gt; ('a -&gt; 'a -&gt; bool) -&gt; bool
doesn't mean that comparison_fun has to be polymorphic, but it means
that any function with type ('a -&gt; 'a -&gt; bool), where 'a is any type, is
acceptable. For instance, the type checker would accept a function of
the type (char -&gt; char -&gt; 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 -&gt; 'a -&gt; bool) =
  match (t1, t2) with
      (Int i, _) -&gt; comparison_fun (Obj.magic i) (Obj.magic
(int_of_thing t2))
    | (String s, _) -&gt; 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] &lt;- s.[l-i-1] done;
  result

let my_comparison s s' =
  (string_rev s) &gt; (string_rev s')

let ok = compare_things (Int 0) (String "42") (&lt;)
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:

&gt; Can anyone explain to me why the following doesn't compile:
&gt;
&gt; ----------
&gt;
&gt; type thing = Int of int | String of string ;;
&gt;
&gt; let int_of_thing t =
&gt;   match t with
&gt;       Int i -&gt; i
&gt;     | String s -&gt; int_of_string s ;;
&gt;
&gt; let string_of_thing t =
&gt;   match t with
&gt;       Int i -&gt; string_of_int i
&gt;     | String s -&gt; s ;;
&gt;
&gt; let compare_things t1 t2 comparison_fun =
&gt;   match (t1, t2) with
&gt;       (Int i, _) -&gt; comparison_fun i (int_of_thing t2)
&gt;     | (String s, _) -&gt; comparison_fun s (string_of_thing t2) ;;
&gt;
&gt; let is_less_than t1 t2 =
&gt;   compare_things t1 t2 (&lt;) ;;
&gt;
&gt; let is_greater_than t1 t2 =
&gt;   compare_things t1 t2 (&gt;) ;;
&gt;
&gt; ----------
&gt;
&gt; The compiler says:
&gt;
&gt; File "test.ml", line 16, characters 38-39:
&gt; This expression has type string but is here used with type int
&gt;
&gt; It seems to have decided that in the function compare_things, 
&gt; comparison_fun is int -&gt; int -&gt; 'a because that's how it's used in the 
&gt; first pattern, not realising that I mean to pass a polymorphic 
&gt; function, 'a -&gt; 'a -&gt; bool.
&gt;
&gt; Is there a way to make this work?  (Specifying the type of 
&gt; comparison_fun as 'a -&gt; 'a -&gt; bool makes no difference.)
&gt;
&gt; Ben
&gt;
&gt; -------------------
&gt; To unsubscribe, mail caml-list-request@inria.fr Archives: 
&gt; http://caml.inria.fr
&gt; Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: 
&gt; http://caml.inria.fr/FAQ/
&gt; 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

</contents>

</message>

