From: Jeremy Yallop <jeremy.yallop@ed.ac.uk>
To: David Allsopp <dra-news@metastack.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] let rec and polymorphic functions
Date: Wed, 27 Jun 2007 12:12:34 +0100 [thread overview]
Message-ID: <46824622.2080508@ed.ac.uk> (raw)
In-Reply-To: <001801c7b8a5$672a2b80$6a7ba8c0@treble>
David Allsopp wrote:
> That does explain it - which I didn't know. Consider this which is also
> broken (and simpler because it has nothing to do with the ad-hoc
> polymorphism of printf)
>
> let rec id x = x
> and _ = id 0
> in
> id (); (* *** *)
> id 1;;
>
> Gives a type error for *** because id is already inferred as int -> int
> because of the monomorphic nature of the recursive definition.
Right. In particular, it gives a type error because `id' is used at the
type int -> int within its own definition (the rhs of every binding in
the group), so that's the type that it's given for the rest of the
program (the part following "in").
> This is
> over-guarded but I never got an answer on a previous post as to why. The
> equivalent SML does type polymorphically:
>
> let fun id x = x
> val _ = id 0
> in
> id ();
> id 1
> end;
This isn't really "the equivalent SML", since the definition of `id x'
and the application `id 0' aren't in the same recursive group. The
equivalent in SML would be something like
let val rec id = fn x => x
and _ = id 0
in
id ();
id 1
end
although this actual program isn't valid; SML only allows syntactic
function expressions on the rhs of `let rec'. If you throw in a "fn" to
avoid the restriction you'll find that SML behaves essentially the same
as OCaml:
let val rec id = fn x => x
and x = fn _ => id 0
in
id ();
id 1
end
OCaml seems a little inconsistent here, actually. The application `id
0' is only valid as the rhs of let rec because the compiler can
determine that there's no actual recursion involved. There doesn't seem
to be a reason not to apply a similar analysis to type checking,
allowing more polymorphism for functions in the same recursive group
that aren't actually part of a cycle.
Jeremy.
next prev parent reply other threads:[~2007-06-27 11:09 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20070627100004.9E0DABC73@yquem.inria.fr>
2007-06-27 10:24 ` David Allsopp
2007-06-27 11:12 ` Jeremy Yallop [this message]
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
2007-06-27 8:40 David Allsopp
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=46824622.2080508@ed.ac.uk \
--to=jeremy.yallop@ed.ac.uk \
--cc=caml-list@yquem.inria.fr \
--cc=dra-news@metastack.com \
/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