Version française
Home     About     Download     Resources     Contact us    
Browse thread
let rec and polymorphic functions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Arnaud Spiwack <aspiwack@l...>
Subject: Re: [Caml-list] let rec and polymorphic functions
Jon Harrop a écrit :
> There are many problems with this. Google for ad-hoc polymorphism, polymorphic 
> recursion and generic printing.
>
> On Wednesday 27 June 2007 09:40:31 David Allsopp wrote:
>   
>>   out "TEST";
>>     
>
> val out : string -> unit
>   
Actually it seems to infer properly "out : (unit, out_channel, unit) 
format -> unit". So the magic is pulled here (which surprises me a lot, 
but well). The problem seems more related to the fact that mutual 
recursive function are monomorphic.
>   
>>   out "%d" 0;
>>     
>
> val out : format -> int -> unit
>
> As printf is ad-hoc polymorphic, you must supply the format specifier 
> immediately and OCaml will generate a custom printer for you. OCaml does not 
> use run-time types so you cannot have a generic print function: you must 
> specific print functions for each of your (possibly higher-order) types.
>
> Also, recursive calls ossify the function to a monomorphic type, so you cannot 
> do polymorphic recursion in OCaml. There are workaround using recursive 
> modules or objects but I don't think this is what you want here.
>
>