From: skaller <skaller@users.sourceforge.net>
To: Kirill <kirillkh@gmail.com>
Cc: Richard Jones <rich@annexia.org>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] weird type behavior
Date: Sun, 29 Oct 2006 16:07:54 +1100 [thread overview]
Message-ID: <1162098474.7104.81.camel@rosella.wigram> (raw)
In-Reply-To: <1162084356.23148.64.camel@nfnl>
On Sun, 2006-10-29 at 03:12 +0200, Kirill wrote:
> Hi,
>
> Unfortunately, I wasn't able to infer, how to solve my problem from the
> FAQ, nor from other sources. I can see that partial application somehow
> interferes with polymorphism, and partial application does look
> necessary for my task. Does it mean it's completely impossible in OCaml?
> Or is there still some way to overcome the problem?
A limitation of Hindley-Milner type inference:
you cannot pass a polymorphic function to a function.
The Ocaml run-time supports this fine, but the
type inference engine is only able to cope with universal
quantification. [To be clear: the Ocaml *type system* allows it
but the inference engine cannot infer it and there's currently
no way to override the inference engine: a coercion would be
unsafe, and an annotation on the argument is a constraint applied
after inference so doesn't help]
So when you DO pass a polymorphic function
to a function, it is specialised (monomorphised) inside
the function, depending on its use. For example:
# let g x = x,x in
let f g x y = g x, g y in
f g 1 1.2;;
This expression has type float but is here used with type int
# let g x = x,x in
g 1, g 1.2;;
- : (int * int) * (float * float) = ((1, 1), (1.2, 1.2))
The function g really is polymorphic here, but not inside f.
The notation
'_a
generally means this monormorphised version of type variable 'a:
it happens to be 'int' in the first example, so the system
prints that instead: '_a means 'some ground type we don't
know' as opposed to 'a which means 'any type'.
Ocaml will allow you to pass a polymorphic function provided
you wrap it in a class or record:
# type poly = { h : 'a . 'a -> 'a * 'a };;
type poly = { h : 'a. 'a -> 'a * 'a; }
You can see here the quantifier 'a . is localised to
the record field. Note the type 'poly' is in fact
quite monomorphic -- there's no type parameter,
one of the fields just happens to be a polymorphic
function.
# let g x = x,x in
let f k x y = k.h x, k.h y in
f {h=g} 1 1.2;;
- : (int * int) * (float * float) = ((1, 1), (1.2, 1.2))
--
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net
next prev parent reply other threads:[~2006-10-29 5:08 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-10-28 15:49 Kirill
2006-10-28 16:12 ` [Caml-list] " Richard Jones
2006-10-28 17:17 ` Kirill
[not found] ` <20061028161506.GA2596@furbychan.cocan.org>
[not found] ` <1162083950.23148.58.camel@nfnl>
2006-10-29 1:12 ` Kirill
2006-10-29 1:34 ` Kirill
2006-10-29 5:07 ` skaller [this message]
2006-10-29 10:03 ` Boris Yakobowski
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=1162098474.7104.81.camel@rosella.wigram \
--to=skaller@users.sourceforge.net \
--cc=caml-list@yquem.inria.fr \
--cc=kirillkh@gmail.com \
--cc=rich@annexia.org \
/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