Version française
Home     About     Download     Resources     Contact us    
Browse thread
Question on polymorphic typing for curried functions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] Question on polymorphic typing for curried functions
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