Bob Fang
Bob Fang

Reputation: 7381

Currying function cancels polymorphism in OCaml?

I have a function:

 let rec loop size elem =
    if size <= 0 then []
     else elem::( loop (size - 1) elem);;

And if I type it in the utop I got the type int -> 'a -> 'a list = <fun> which is expected, however if I make a function let loop_3 = loop 3, the type of loop_3 becomes '_a -> '_a list = <fun>. The major difference concerning me is that it changes the function from a polymorphic function (whose input is 'a) to a weak polymorphism function (input type of '_a). Why is this happening? Is there any way to work around this?

Upvotes: 0

Views: 131

Answers (1)

ivg
ivg

Reputation: 35210

To workaround you need to eta-expand, i.e. to provide all parameters.

You see the results of OCaml's value restriction. There're lots of good answers on the topic in SO and in OCaml's FAQ. One of my favorite answer is this.

Upvotes: 1

Related Questions