<?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/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"
  prev="2003/12/eac46552a7b577d9a89a07fc38092a70"
  next="2003/12/22a7ccbbbae6a7e18cdc45f3ea6dabc4"
  prev-in-thread="2003/12/eac46552a7b577d9a89a07fc38092a70"
  next-in-thread="2003/12/22a7ccbbbae6a7e18cdc45f3ea6dabc4"
  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>
Jean-Baptiste Rouquier wrote:
&gt;    compare_things : thing -&gt; thing -&gt; ('a -&gt; 'a -&gt; bool) -&gt; bool
&gt; doesn't mean that comparison_fun has to be polymorphic, but it means 
&gt; that any function with type ('a -&gt; 'a -&gt; bool), where 'a is any type, is 
&gt; acceptable. For instance, the type checker would accept a function of 
&gt; the type (char -&gt; char -&gt; bool).

OK, but it seems that the type checker has inferred the type of 
comparison_fun just by looking at its first use in compare_things, and 
has decided that it must be monomorphic; this is an incorrect inference. 
  My naive impression is that if the type checker was willing to accept 
both uses of comparison_fun as valid (or perhaps if it also looked at 
the places where compare_things is called), it would conclude that 
comparison_fun does indeed have to be polymorphic.  In that case, I 
would get a compile error if I tried to pass a non-polymorphic function 
to compare_things.  Would this be this asking too much?  (I suspect the 
answer is yes; I'm just curious...)

Ben

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

</contents>

</message>

