Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Instanciating functor types with extra parameters
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-08-04 (16:03)
From: Arnaud Spiwack <aspiwack@l...>
Subject: Instanciating functor types with extra parameters
Hi caml list !

The recent, or rather current, thread on priority queues, raised back an 
issue that've I've been really infuriated with a couple of time the 
past, like, 2 years.

When I have a functor type, like for example (not too innocent I'm afraid) :

module type OrderedType =
    type t
    val compare : t -> t -> int

Something that naive intuition would allow you to do is something like :

module GenOrder : OrderedType =
    type t = 'a
    let compare = compare

Though this is more or less nonsense. And is not currently possible 
under OCaml type system.

My point is that I know absolutely no way of doing such a thing. Hence I 
can't make a set with total or partial genericity. If I want to add type 
parameters I have to rewrite the *whole* set library. Actually... I've 
got to copy past it, and replace all occurences of    t   with an 'a t 
for instance. Or ('a,'b) t if I have two arguments. I really had to do 
that once, and almost a couple of other time. That's why it infuriates 
me as I said earlier.

The whole issue is that it totaly breaks the purpose of functors to have 
to recast it for a very small type variation.

Thus I'm raising the question : is there a good reason (I mean a good 
reason from the user point of view here, I kinda understand it may make 
things much more complicated). An other question would be : is there a 
way to work around this issue ? Yet another question would be : should 
there be a separated syntax to mean that the type can have arbitrary 
arguments encapsulated ?

PS : I'm aware that there is (several) available implementation of 
unfunctorized Set and Map, the questions are about the more general case 
though. I very often give up the idea of writing something in a 
functorized style, because it's too fragile, which is a bit paradoxal to me.

Arnaud Spiwack