From: "David Allsopp" <dra-news@metastack.com>
To: "OCaml List" <caml-list@yquem.inria.fr>
Subject: Polymorphism question...
Date: Thu, 8 Mar 2007 10:08:52 -0000 [thread overview]
Message-ID: <00c501c76169$c625e0e0$6a7ba8c0@treble> (raw)
If I write:
let n = ref 0
in
fun x -> incr n; x
then I get a function with type '_a -> '_a --- i.e. with a non-instantiated
type variable rather than the slightly curious polymorphic identity function
that I was expecting.
If I write:
let n = ref 0
fun x -> incr n; x
then I get a polymorphic function with type 'a -> 'a which was what I was
after.
My question: why does declaring the int ref n locally prevent the type of
the function being generalised when if I just put n at "global" scope it
would be? I'm sure there's a reason relating to the standard problem of refs
and inferred polymorphic type variables, but I'm curious as to what is
different in the type system between the two ways of expressing this.
Furthermore, in SML, the code:
local
val n = ref 0
in
fun f x = (n := !n + 1; x)
end;
correctly produces f : 'a -> 'a
So what's different in O'Caml?
David
PS I hit this by mistake when sharing state variables between two related
functions in a module. Rather than declaring:
let some_global = ref whatever
let function1 = (* something that uses some_global *)
let function2 = (* something else that uses some_global *)
let function3 = (* something different that doesn't use some_global *)
I prefer to say:
let (function1, function2) =
let some_global = ref whatever
in
let function1 = ...
and function2 = ...
in
(function1, function2)
let function3 = ...
and accept the unnecessary boxing/unboxing at program launch for a bit of
program neatness (i.e. the "global" is only available to the functions that
need it). SML has a neater construct for this where you can say
local
val some_global = ref whatever
in
fun function1 = ...
and function2 = ...
end;
which is something I rather miss in O'Caml!
reply other threads:[~2007-03-08 10:08 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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='00c501c76169$c625e0e0$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