caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Defeating the value restriction
@ 2011-10-26 20:07 John Carr
  2011-10-26 20:22 ` Jeremy Yallop
  2011-10-28  0:05 ` Jacques Garrigue
  0 siblings, 2 replies; 3+ messages in thread
From: John Carr @ 2011-10-26 20:07 UTC (permalink / raw)
  To: caml-list


Can I rewrite this function to have the type I requested
instead of failing type checking?

let f : ('a * 'a -> 'a) -> int * bool = fun x -> x (0,1), x (true,false)

I assume the type error is caused by the value restriction.
The parameter (x) becomes monomorphic when applied, so it
can not be applied to both types (int*int) and (bool*bool).

A top level syntactic function

let x : ('a * 'a -> 'a) = fun (a,b) -> a

retains its original, generalized type under similar circumstances.

My real example that fails type checking is more complicated.
I believe the root cause is the same.

    --John Carr (jfc@mit.edu)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Defeating the value restriction
  2011-10-26 20:07 [Caml-list] Defeating the value restriction John Carr
@ 2011-10-26 20:22 ` Jeremy Yallop
  2011-10-28  0:05 ` Jacques Garrigue
  1 sibling, 0 replies; 3+ messages in thread
From: Jeremy Yallop @ 2011-10-26 20:22 UTC (permalink / raw)
  To: John Carr; +Cc: caml-list

On 26 October 2011 21:07, John Carr <jfc@mit.edu> wrote:
> Can I rewrite this function to have the type I requested
> instead of failing type checking?

Yes: see "How to write a function with polymorphic arguments?" in the Caml FAQ:

http://caml.inria.fr/resources/doc/faq/core.en.html#polymorphic-arguments

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Defeating the value restriction
  2011-10-26 20:07 [Caml-list] Defeating the value restriction John Carr
  2011-10-26 20:22 ` Jeremy Yallop
@ 2011-10-28  0:05 ` Jacques Garrigue
  1 sibling, 0 replies; 3+ messages in thread
From: Jacques Garrigue @ 2011-10-28  0:05 UTC (permalink / raw)
  To: John Carr; +Cc: caml-list

On 2011/10/27, at 5:07, John Carr wrote:

> Can I rewrite this function to have the type I requested
> instead of failing type checking?
> 
> let f : ('a * 'a -> 'a) -> int * bool = fun x -> x (0,1), x (true,false)
> 
> I assume the type error is caused by the value restriction.
> The parameter (x) becomes monomorphic when applied, so it
> can not be applied to both types (int*int) and (bool*bool).

Just a comment that this completely unrelated to the value restriction.
ML type inference simply doesn't support polymorphism in function
arguments.

As Jeremy Yallop already answered, OCaml supports this by wrapping
the argument in various things. The most recent way to do that
(and intuitively related with the toplevel behaviour) is to use a first-class
module:

module type S = sig val c : 'a * 'a -> 'a end
let f x = let module X = (val x : S) in X.x (0,1) , X.x (true, false)

Jacques Garrigue

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-10-28  0:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-26 20:07 [Caml-list] Defeating the value restriction John Carr
2011-10-26 20:22 ` Jeremy Yallop
2011-10-28  0:05 ` Jacques Garrigue

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).