Who needs polymorphics types? (when we have modules)

You cannot write the following function with monomorphic functor currently:

type 'a t =
  | Elt of 'a
  | List of  'a list t
let rec list: 'a. 'a t -> 'a list = function
  | Elt x -> [x]
  | List l -> List.concat (list l)

since the corresponding recursive functor is unsafe:

module Listify(T: sig type t end) = struct
  type t = T.t list
end
module rec L: functor (T: sig type t end) -> sig
  type t =
    | Elt of T.t
    | List of L(Listify(T)).t
  val list: t -> T.t list
end = functor (T: sig type t end) -> struct
  type t =
    | Elt of T.t
    | List of L(Listify(T)).t
  let rec list = function
    | Elt x -> [x]
    | List l ->
      let module Sub = L(Listify(T)) in
      List.concat (Sub.list l)
end

But even without this limitation, modules are far more complex than polymorphic types. Expressing the simple feature (polymorphic types) using the complex one (modules and functors) for the sake of losing type inference (and we also need explicit type applications with functors) does not seem like a good idea.

3 Likes