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
type troubles with functors
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Scott Duckworth <scott.duckworth@g...>
Subject: type troubles with functors
I'm having trouble figuring out a way to represent functor modules as
types in a module type definition.  For lack of a better way of
explaining it, here's the problem I'm having in code:
(* OrderedSet.mli *)
(* Ordered sets over ordered types *)

module type OrderedType = (* same as in .ml file *)
module type S = (* same as in .ml file *)
module Make (Ord : OrderedType) : S with type elt = Ord.t
(* *)
(* Ordered sets over ordered types *)

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

module type S = sig
	type elt
	type t
	val to_list : t -> elt list
	val to_set : t -> EltSet.t     (* PROBLEM *)
	(* ... *)

module Make (Ord : OrderedType) = struct
	module EltSet = Set.Make(Ord)      (* THIS IS WHAT I WANT TO RETURN *)
	type elt = Ord.t
	type t = { l : elt list; s : EltSet.t }
	let to_list s = s.l
	let to_set s = s.s    (* THIS IS WHERE I WANT TO RETURN A SET *)
	(* ... *)
I realize that EltSet.t is not defined at the two points I have noted
a problem, I'm simply stating my intention.  The problem is that
OrderdSet.S does not know the OrderedSet.OrderedType that will be used
in OrderedSet.Make, but it does know that it will be of type Set.S.t. 
(This is one instance where I wish Set was implemented as a
parameterized type, not a functor.)

My question is, how can I correctly type to_set?  I have tried many
different configurations of Set.Make and Set.S, but none of them have
worked.  If anyone knows how this can be done in OCaml's type system,
please let me know.  Thanks!
Scott Duckworth