I don’t know. The best I’ve ever come up with is this:
type length = { length : 'a. 'a list -> int }
module Len : sig
type t
val v : length -> t
end = struct
type t = {
len : 'a. 'a list -> int;
etc : string;
}
let v {length} = {
len = length;
etc = "abc";
}
end
So the user has to call v {length} instead of v length.
How to write a function with polymorphic arguments?
In ML, an argument of a function cannot be polymorphic inside the body of the function; hence the following typing:
let f (g : 'a -> 'a) x y = g x, g y
val f : ('a -> 'a) -> 'a -> 'a -> 'a * 'a = <fun>
The function is not as polymorphic as we could have hoped.
Nevertheless, in OCaml it is possible to use first-order polymorphism. For this, you can use either records or objects; in the case of records, you need to declare the type before using it in the function.
let f (o : <g : 'a. 'a -> 'a>) x y = o#g x, o#g y
val f : < g : 'a. 'a -> 'a > -> 'b -> 'c -> 'b * 'c = <fun>
type id = { g : 'a. 'a -> 'a; }
type id = { g : 'a. 'a -> 'a; }
let f r x y = r.g x, r.g y
val f : id -> 'a -> 'b -> 'a * 'b = <fun>
I fixed the problem with module on my side. You can have an example in encore.
module Len : sig
type t
module Make : functor (S: sig val length: 'a -> int end) -> sig val v: t end
end = struct
type t = { length : 'a. 'a -> int }
module Make (S: sig val length: 'a -> int end) = struct
let v = { length = S.length }
end
end