Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a module type from an interface?

Tags:

module

ocaml

I would like to have my own implementation of an existing module but to keep a compatible interface with the existing module. I don't have a module type for the existing module, only an interface. So I can't use include Original_module in my interface. Is there a way to get a module type from an interface?

An example could be with the List module from the stdlib. I create a My_list module with exactly the same signature than List. I could copy list.mli to my_list.mli, but it does not seem very nice.

like image 684
Louis Roché Avatar asked May 18 '16 13:05

Louis Roché


2 Answers

In some cases, you should use

include module type of struct include M end   (* I call it OCaml keyword mantra *)

rather than

include module type of M

since the latter drops the equalities of data types with their originals defined in M.

The difference can be observed by ocamlc -i xxx.mli:

include module type of struct include Complex end

has the following type definition:

type t = Complex.t = { re : float; im : float; }

which means t is an alias of the original Complex.t.

On the other hand,

include module type of Complex

has

type t = { re : float; im : float; }

Without the relation with Complex.t, it becomes a different type from Complex.t: you cannot mix code using the original module and your extended version without the include hack. This is not what you want usually.

like image 136
camlspotter Avatar answered Oct 20 '22 10:10

camlspotter


You can look at RWO : if you want to include the type of a module (like List.mli) in another mli file :

include (module type of List)
like image 29
Pierre G. Avatar answered Oct 20 '22 08:10

Pierre G.