<?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/c5862f388f18bdf2460db3f01d99f5fa"
  from="Guillaume Marceau &lt;gmarceau@c...&gt;"
  author="Guillaume Marceau"
  date="2003-12-22T20:26:27"
  subject="Re: [Caml-list] Polymorphic records typing question"
  prev="2003/12/b60a5b139b2013e8b7f1ee93a77ffe68"
  next="2003/12/b7717c6417bff34e7f3d535b9a41f26b"
  prev-in-thread="2003/12/95fe6760008ea6cd96ee4a24c07ce390"
  prev-thread="2003/12/e522814874d2e94650503cf689c10feb"
  next-thread="2003/12/4c6ae63cd6b1e8a8f33149859cfd695c"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] Polymorphic records typing question">
<msg 
  url="2003/12/95fe6760008ea6cd96ee4a24c07ce390"
  from="Alex Baretta &lt;alex@b...&gt;"
  author="Alex Baretta"
  date="2003-12-22T13:33:34"
  subject="[Caml-list] Polymorphic records typing question">
<msg 
  url="2003/12/c5862f388f18bdf2460db3f01d99f5fa"
  from="Guillaume Marceau &lt;gmarceau@c...&gt;"
  author="Guillaume Marceau"
  date="2003-12-22T20:26:27"
  subject="Re: [Caml-list] Polymorphic records typing question">
</msg>
</msg>
</thread>

<contents>

On Mon, 22 Dec 2003, Alex Baretta wrote:

&gt; Here's a puzzle for the Caml breeders:
&gt;
&gt; # type poly = { f : 'a . 'a -&gt; 'a };;
&gt; type poly = { f : 'a. 'a -&gt; 'a; }
&gt; # let id x = x;;
&gt; val id : 'a -&gt; 'a = &lt;fun&gt;
&gt; # let foo = { f = id };;
&gt; val foo : poly = {f = &lt;fun&gt;}
&gt; # let rec id x = x and foo = { f = id } ;;
&gt; This field value has type 'a -&gt; 'a which is less general than 'b. 'b -&gt; 'b
&gt;
&gt; Why is this last recursive definition unacceptable?
&gt;
&gt; Alex
&gt;

Types are not generalized within the scope of their recursive declaration.

For instance, take this contrived definition of the identity function :

   let rec id x =
     if true then (ignore(id 1); x)
     else ((ignore (id [])); x)
                       ^^
   This expression has type 'a list but is here used with type int

Although id could be safely be assigned the type 'a . 'a -&gt; 'a, the type
inference algorithm cannot figure it out.

It is a limitation that raises from the way the type inference is
computed. Then again, it isn't much of a limitation: both my example and
yours can trivially be rewritten such that they type.


Also, this is one good reason to not use the 'and' keyword unless you
actually need a mutual recursion. Instead of writing this:

      let a = ... body_a ...
      and b = ... body_b ... in
      ...

write this:

      let a = ... body_a ... in
      let b = ... body_b ... in
      ...

It will save you the above error in the case 'body_b' uses 'a' polymorphically.



-- 
"The thing I remember most about America is that it's silly.
 That can be quite a relief at times."  -- Thom Yorke, Radiohead

- Guillaume

-------------------
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>

