* let rec and polymorphic functions
@ 2007-06-27 8:40 David Allsopp
2007-06-27 9:05 ` [Caml-list] " Jon Harrop
0 siblings, 1 reply; 3+ messages in thread
From: David Allsopp @ 2007-06-27 8:40 UTC (permalink / raw)
To: OCaml List
Why is let rec apparently unable to infer polymorphic function types? In
both the expressions below, I'd expect [out] to have type [('a, unit,
string, unit) format4 -> 'a]. Why when used in a [let rec] construct is it
clearly inferred as [('_a, unit, string, unit) format4 -> '_a] and then
instantiated as [(unit, unit, string, unit) format4 -> unit] by the first
call to [out] in [f]? It seems to contradict the end of Section 6.7.1 of the
manual.
I know that [out] and [f] are not mutually recursive so there's no need to
use [let rec] but I tend to use [let rec] in situations where I'm defining
two functions where one uses the other at the [let ... in] level as it saves
writing the extra [in]! This appears potentially to be a mistake, though...
As ever, a technical explanation of why the type system behaves this way
much appreciated! I won't make judgement on the hours of time wasted by the
cryptic type errors in this case ;o)
Just in case it matters, I'm using O'Caml 3.09.3...
David
(*
* This first example works.
*)
let out line =
Printf.printf line
in
let f () =
(*
* [out] is clearly polymorphic
*)
out "TEST";
out "%d" 0;
out "%b" false;
in
f ();;
(*
* This second example does not. Why?
*)
let rec out line =
Printf.printf line
and f () =
(*
* [out] gets inferred as string -> unit here...
*)
out "TEST";
(*
* ... and so we get a "too many parameters" error here.
*)
out "%d" 0;
in
f ();;
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] let rec and polymorphic functions
2007-06-27 8:40 let rec and polymorphic functions David Allsopp
@ 2007-06-27 9:05 ` Jon Harrop
2007-06-27 10:14 ` Arnaud Spiwack
0 siblings, 1 reply; 3+ messages in thread
From: Jon Harrop @ 2007-06-27 9:05 UTC (permalink / raw)
To: caml-list
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
> 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.
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The OCaml Journal
http://www.ffconsultancy.com/products/ocaml_journal/?e
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] let rec and polymorphic functions
2007-06-27 9:05 ` [Caml-list] " Jon Harrop
@ 2007-06-27 10:14 ` Arnaud Spiwack
0 siblings, 0 replies; 3+ messages in thread
From: Arnaud Spiwack @ 2007-06-27 10:14 UTC (permalink / raw)
To: caml-list
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.
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2007-06-27 10:14 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-27 8:40 let rec and polymorphic functions David Allsopp
2007-06-27 9:05 ` [Caml-list] " Jon Harrop
2007-06-27 10:14 ` Arnaud Spiwack
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox