I'm running into problems around recursive/mutually referential module definitions trying to use Caml's Map/Set stuff. I really want ones that just work on types, not modules. I feel like it should be possible to do this with first-class modules, but I'm failing to make the syntax work.
The signature I want is:
module type NonFunctorSet = sig
type 'a t
val create : ('a -> 'a -> int) -> 'a t
val add : 'a t -> 'a -> 'a t
val remove : 'a t -> 'a -> 'a t
val elements : 'a t -> 'a list
end
Possibly with other Caml.Set
functions included. My idea for how this would work is something like:
type 'a t = {
m : (module Caml.Set.S with type elt = 'a);
set : m.t
}
let create (compare : 'a -> 'a -> t) =
module m = Caml.Set.Make(struct type t = 'a let compare = compare end) in
let set = m.empty in
{m = m; set = set;}
end
But that doesn't work for a number of reasons; 'a isn't exposed in the right places, I can't reference m.t in the same record where m was defined, etc.
Is there a version of this that works?
Adding more context about my use case:
I have two modules, Region
and Tribe
. Tribe
needs access to a lot of the interface of Region
, so I am currently creating Tribe
as a functor, MakeTribe(Region : RegionT)
. Region
mostly doesn't need to know about Tribe
, but it does need to be able to store a mutable collection of Tribe.t
that represent the tribes living in that region.
So, somehow or other, I need a RegionT
like
module type RegionT = sig
type <region>
val get_local_tribes : <region> -> <tribes>
val add_tribe : <region> -> <tribe> -> unit
...
end
I don't really care about the specific syntax of <tribe>
, <tribes>
and <region>
in this, so long as the fully built Tribe
module can know that Region.get_local_tribes
, etc, will yield an actual Tribe.t
The circular dependency problem is that the type <tribe>
does not exist until the module Tribe
is created. My idea so far has been to have RegionT.t
actually be 'a RegionT.t
, and then Tribe
could simply refer to Tribe.t Region.t
. This is all fine if I'm satisfied with keeping a <tribe> list
inside Region
, but I want it to be a set.
I feel this should be possible based on the following example code :
module Example : sig
type t
val compare : t -> t -> int
end = struct
type t = int
let compare = Int.compare
end
module ExampleSet = Caml.Set.Make(struct type t = Example.t let compare = Example.compare end)
All that Example
exposes in its interface is a type and a function from two instances of that type to an int; why is that more than having a 'a -> 'a -> int
, which has the same things?
Using Polymoprhic Sets and Maps from the Base Library
In Base and Core libraries, from Jane Street, ordered data structures, such as maps, sets, hash tables, and hash sets, are all implemented as polymorphic data structures, instead of functorized versions as in the vanilla OCaml standard library.
You can read about them more in the Real World OCaml Maps and Hashtbales chapter. But here are quick recipes. When you see a comparator in the function interface, e.g., in
Map.empty
what it actually wants you is to give you a module that implements the comparator interface. The good news is that most of the modules in Base/Core are implementing it, so you don't have to worry or know anything about this to use it, e.g.,So the simple rule, if you want a set,map,hashtable,hashset where the key element has type
foo
, just pass(module Foo)
as a comparator.Now, what if you want to make a mapping from your custom type? E.g., a pair of ints that you would like to compare in lexicographical order.
First of all, we need to define sexp_of and compare functions. For our type. We will use ppx derivers for it, but it is easy to make it manually if you need.
Now, to create a comparator, we just need to use the
Base.Comparator.Make
functor, e.g.,So now we can do,
Despite that Base's data structures are polymorphic they strictly require that the module that provides the comparator is instantiated and known. You can just use the compare function to create a polymorphic data structure because Base will instantiate a witness type for each defined compare function and capture it in the data structure type to enable binary methods. Anyway, it is a complex issue, read on for easier (and harder) solutions.
Instantiating Sets on mutually dependent modules
In fact, OCaml supports mutually recursive funtors and although I would suggest you to break the recursion by introducing a common abstraction on which both Region and Tribe depend, you can still encode your problem in OCaml, e.g.,
Breaking the Dependency Loop
A much better solution would be to redesign your modules and break the dependency loop. The simplest approach would be just to choose some identifier that will be used to compare tribes, e.g., by their unique names,
There are also tons of other options and in general, when you have mutual dependencies it is actually an indicator of a problem in your design. So, I would still revisit the design stage and eschew the circular dependencies.
Creating Polymorphic Sets using the Vanilla OCaml Standard Library
It is not an easy task, especially if you need to handle operations that involve several sets, e.g.,
Set.union
. The problem is thatSet.Make
is generating a new type for the set per eachcompare
function so when we need to union two sets it is hard for us to prove to the OCaml compiler that they were created from the same type. It is possible but really painful, I am showing how to do this only to discourage you from doing this (and to showcase OCaml's dynamic typing capabilities).First of all we need a witness type that will reify an OCaml type for the set into a concrete value.
Now we can define our polymorphic set as an existential that holds the set itself and the module with operations. It also holds the
tid
(for type identifier) that we will later use to recover the type's
of the set.Now we can write the
create
function that will take the compare function and turn it into a set,The caveat here is that each call to
create
will generate a new instance of the set type's
so we can compare/union/etc two sets that were created with the samecreate
function. In other words, all sets in our implementation shall share the same ancestor. But before that lets take a pain and implement at least two operations,add
andunion
,Now, we can play with it a bit,