caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* let rec and polymorphic functions
@ 2007-06-27  8:40 David Allsopp
  2007-06-27  9:05 ` [Caml-list] " Jon Harrop
  0 siblings, 1 reply; 9+ messages in thread
From: David Allsopp @ 2007-06-27  8:40 UTC (permalink / raw)
  To: OCaml List

Why is let rec apparently unable to infer polymorphic function types? In
both the expressions below, I'd expect [out] to have type [('a, unit,
string, unit) format4 -> 'a]. Why when used in a [let rec] construct is it
clearly inferred as [('_a, unit, string, unit) format4 -> '_a] and then
instantiated as [(unit, unit, string, unit) format4 -> unit] by the first
call to [out] in [f]? It seems to contradict the end of Section 6.7.1 of the
manual.

I know that [out] and [f] are not mutually recursive so there's no need to
use [let rec] but I tend to use [let rec] in situations where I'm defining
two functions where one uses the other at the [let ... in] level as it saves
writing the extra [in]! This appears potentially to be a mistake, though...

As ever, a technical explanation of why the type system behaves this way
much appreciated! I won't make judgement on the hours of time wasted by the
cryptic type errors in this case ;o)

Just in case it matters, I'm using O'Caml 3.09.3...


David

(*
 * This first example works.
 *)
let out line =
  Printf.printf line
in
  let f () =
    (*
     * [out] is clearly polymorphic
     *)
    out "TEST";
    out "%d" 0;
    out "%b" false;
  in
    f ();;

(*
 * This second example does not. Why?
 *)
let rec out line =
  Printf.printf line
and f () =
  (*
   * [out] gets inferred as string -> unit here...
   *)
  out "TEST";
  (*
   * ... and so we get a "too many parameters" error here.
   *)
  out "%d" 0;
in
  f ();;


^ permalink raw reply	[flat|nested] 9+ messages in thread
[parent not found: <20070627100004.9E0DABC73@yquem.inria.fr>]

end of thread, other threads:[~2007-06-27 13:18 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-27  8:40 let rec and polymorphic functions David Allsopp
2007-06-27  9:05 ` [Caml-list] " Jon Harrop
2007-06-27 10:14   ` Arnaud Spiwack
     [not found] <20070627100004.9E0DABC73@yquem.inria.fr>
2007-06-27 10:24 ` David Allsopp
2007-06-27 11:12   ` Jeremy Yallop
2007-06-27 11:29     ` David Allsopp
2007-06-27 12:00     ` Virgile Prevosto
2007-06-27 13:00       ` Jeremy Yallop
2007-06-27 13:12   ` Jon Harrop

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).