caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Question on polymorphic typing for curried functions
@ 2007-08-24 19:43 Christopher Kauffman
  2007-08-24 20:25 ` [Caml-list] " Jon Harrop
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Christopher Kauffman @ 2007-08-24 19:43 UTC (permalink / raw)
  To: OCaml

I am looking for a bit of information on the behavior of curried functions wrt 
polymorphic arguments. For instance, in the following example, using a curried 
function seems to lose the nice polymorphism that I desire.

# let genf f a b = f a b;;
val genf : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c = <fun>
# let specf = genf (<);;
val specf : '_a -> '_a -> bool = <fun>
# specf 1 2;;
- : bool = true
# specf;;
- : int -> int -> bool = <fun>

An alternative definition for the specific 'specf' maintains polymorphism of its 
arguments.

# let specf a b = genf (<) a b;;
val specf : 'a -> 'a -> bool = <fun>
# specf 1 2;;
- : bool = true
# specf 1.0 2.0;;
- : bool = true
# specf;;
- : 'a -> 'a -> bool = <fun>

Is there a set of rules or guidelines that determine when argument types are 
specialized versus staying polymorphic?

Cheers,
Chris


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

* Re: [Caml-list] Question on polymorphic typing for curried functions
  2007-08-24 19:43 Question on polymorphic typing for curried functions Christopher Kauffman
@ 2007-08-24 20:25 ` Jon Harrop
  2007-08-24 21:04 ` Julien Moutinho
  2007-08-24 23:32 ` Harrison, John R
  2 siblings, 0 replies; 4+ messages in thread
From: Jon Harrop @ 2007-08-24 20:25 UTC (permalink / raw)
  To: caml-list

On Friday 24 August 2007 20:43:26 Christopher Kauffman wrote:
> I am looking for a bit of information on the behavior of curried functions
> wrt polymorphic arguments. For instance, in the following example, using a
> curried function seems to lose the nice polymorphism that I desire.
>
> # let genf f a b = f a b;;
> val genf : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c = <fun>
> # let specf = genf (<);;
> val specf : '_a -> '_a -> bool = <fun>
> # specf 1 2;;
> - : bool = true
> # specf;;
> - : int -> int -> bool = <fun>
>
> An alternative definition for the specific 'specf' maintains polymorphism
> of its arguments.
>
> # let specf a b = genf (<) a b;;
> val specf : 'a -> 'a -> bool = <fun>
> # specf 1 2;;
> - : bool = true
> # specf 1.0 2.0;;
> - : bool = true
> # specf;;
> - : 'a -> 'a -> bool = <fun>
>
> Is there a set of rules or guidelines that determine when argument types
> are specialized versus staying polymorphic?

Yes. If you partially apply then you get a monomorphic result:

# genf (<);;
- : '_a -> '_a -> bool = <fun>

If you then wrap that in any kind of closure then it becomes polymorphic 
again:

# (fun a -> genf (<));;
- : 'a -> 'b -> 'b -> bool = <fun>

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists/?e


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

* Re: [Caml-list] Question on polymorphic typing for curried functions
  2007-08-24 19:43 Question on polymorphic typing for curried functions Christopher Kauffman
  2007-08-24 20:25 ` [Caml-list] " Jon Harrop
@ 2007-08-24 21:04 ` Julien Moutinho
  2007-08-24 23:32 ` Harrison, John R
  2 siblings, 0 replies; 4+ messages in thread
From: Julien Moutinho @ 2007-08-24 21:04 UTC (permalink / raw)
  To: Christopher Kauffman; +Cc: caml-list

On Fri, Aug 24, 2007 at 02:43:26PM -0500, Christopher Kauffman wrote:
> I am looking for a bit of information on the behavior of curried functions 
> wrt polymorphic arguments. For instance, in the following example, using a 
> curried function seems to lose the nice polymorphism that I desire.
> [...]
> Is there a set of rules or guidelines that determine when argument types 
> are specialized versus staying polymorphic?

This may be of interest to you:
http://caml.inria.fr/pub/old_caml_site/FAQ/FAQ_EXPERT-eng.html#eta_expansion


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

* RE: [Caml-list] Question on polymorphic typing for curried functions
  2007-08-24 19:43 Question on polymorphic typing for curried functions Christopher Kauffman
  2007-08-24 20:25 ` [Caml-list] " Jon Harrop
  2007-08-24 21:04 ` Julien Moutinho
@ 2007-08-24 23:32 ` Harrison, John R
  2 siblings, 0 replies; 4+ messages in thread
From: Harrison, John R @ 2007-08-24 23:32 UTC (permalink / raw)
  To: Christopher Kauffman; +Cc: OCaml

Hi Chris,

| I am looking for a bit of information on the behavior of
| curried functions wrt polymorphic arguments. For instance, in
| the following example, using a curried function seems to lose
| the nice polymorphism that I desire.

OCaml uses a variant of the so-called "value restriction" to ensure
soundness in the way polymorphism and imperative features interact.
You can find more information in this paper by Jacques Garrigue:

  http://www.math.nagoya-u.ac.jp/~garrigue/papers/morepoly-long.pdf

It can sometimes be annoying and unintuitive (for me anyway) to lose
polymorphism by eta contraction. But this approach is at least
relatively simple and well-understood.

John.


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

end of thread, other threads:[~2007-08-24 23:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-24 19:43 Question on polymorphic typing for curried functions Christopher Kauffman
2007-08-24 20:25 ` [Caml-list] " Jon Harrop
2007-08-24 21:04 ` Julien Moutinho
2007-08-24 23:32 ` Harrison, John R

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