From: "David Allsopp" <dra-news@metastack.com>
To: "OCaml List" <caml-list@yquem.inria.fr>
Subject: let rec and polymorphic functions
Date: Wed, 27 Jun 2007 09:40:31 +0100 [thread overview]
Message-ID: <000e01c7b896$d3372440$6a7ba8c0@treble> (raw)
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 ();;
next reply other threads:[~2007-06-27 8:40 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-06-27 8:40 David Allsopp [this message]
2007-06-27 9:05 ` [Caml-list] " Jon Harrop
2007-06-27 10:14 ` Arnaud Spiwack
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='000e01c7b896$d3372440$6a7ba8c0@treble' \
--to=dra-news@metastack.com \
--cc=caml-list@yquem.inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox