Modules with multiple types

Hi folks,

I am trying to collect as many examples as possible of modules (or module types) with multiple distinct types that both have to be guarded by some kind of abstraction boundary but need to freely talk to each others’ implementations.

In other words an “abstract data type” which is composed of multiple actual types. For example, a data dictionary with both a distinguished type of keys and a dict type:

module type Dict = sig
    type key
    module M : Ordered with key = t
    type 'a dict
    val add : key -> 'a -> 'a dict -> 'a dict
    val get : key -> 'a dict -> 'a option
end

This example is not super interesting to me because I’m not persuaded that the data type of keys really needs encapsulation / information hiding in this scenario.

For a scenario where hiding the implementation of keys may be desirable, the Stdlib documentation has an example of a heterogeneous dictionary which creates keys associated with different types.

1 Like

Not sure it fits what you are asking but PG’OCaml’s abstraction over Lwt, Async, Unix, has 3 types:

@JohnJ That’s a really good example, thank you. I had no idea OCaml could handle heterogenous data in that way.

@smondet It’s a good example. I was originally thinking of simple textbook style examples, something where it’s easy to see how the parts interact. But a real world example is great! My motivation for asking this was to see examples of OCaml code that would be difficult to translate into Java or C# because a module contains multiple data types but a “class” is itself only one data type. So I will need some time to read into this code carefully and think a bit about whether you could write this code using an In_channel class (or interface), an Out_channel class and a Thread class/interface - where each class has its own private data and the classes only talk to each other through public interfaces, but without exposing more to each other than they do to the outside world.

1 Like

ocamlgraph has some examples: G (ocamlgraph.Graph.Sig.G)