How to initialize a Base.Hashtbl on an abstract type?

An answer specific to your question is:

module MyModule : sig 
    type t [@@deriving compare, hash, sexp_of]
end = struct 
    type t = int list [@@deriving compare, hash, sexp_of]
end

module Table = Hashtbl.Make(MyModule)

However, the idiomatic approach is to automatically derive all other data types, including maps, sets, hash_sets, etc, instead of implementing each one manually. You can do this with the Identifiable.Make function, that derive a module that implements the Identifiable.S interface, from the provided minimal representation, e.g.,

module MyModule : sig 
    type t 
    include Idenfiable.S with type t := t
end = struct 
    module Base = struct 
       type t = int list [@@deriving bin_io, compare, hash, sexp]
       let of_string x = failwith "implement-me"
       let to_string x = failwith "implement-me"
       let module_name = "My_unit.MyModule"
    end
    include Base
    include Identifiable.Make(Base)
end

The Identifiable.S interface is very rich, so you may choose something smaller, like Identifiable.S_plain, Comparable.S, Comparable.S_plain, etc.

Also, when a deriver is not recognized, then you need to install it via opam and specify in the build system.

2 Likes