Jackson Tale
Jackson Tale

Reputation: 25812

Try to further understanding the interface/module of OCaml

I understand in OCaml there are concepts of interfaces and module.

And I understand how to use them now.

However, what I don't understand is how to fully utilise them.


For example, in Java, let's say we have a interface Map and we also have Hashtable and HashMap that implement Map.

In code, I can do like:

Map m = new Hashtable();
m.put("key", value);

Someday, if I change my mind, I can change to Hashmap very quickly by changing Map m = new Hashtable(); to Map m = new HashMap();, right?


But how can I easily do that in Ocaml?

For example, I have MapSig and 'HashMap:MapSigand "Hashtable:MapSig in OCaml.

How can I change the implementation easily?

I don't think I can because in OCaml I have to do like:

let m = Hashtable.create ();;

Hashtable.put m key value;;

if I want to use HashMap instead, I have to replace every Hashtable with HashMap in the code, right?


Edit:

I am not only seeking a way to make a alias to modules. I also consider the validity of implementations, i.e., whether the implementation follow the desired interface.

For example, in above Java example, only if HashMap has implemented Map interface, I can replace Hashtable with HashMap. otherwise, Java compiler will complain.

but if I do module M = Hashtable in OCaml, and if HashMap does not follow MapSig and I replace Hashtable with HashMap, what will happen? I think compiler won't complain, right?

Upvotes: 0

Views: 956

Answers (3)

Yawar
Yawar

Reputation: 11607

The most direct correspondence between your Java example and OCaml is using a functor (what OCaml calls a static function from a module to a module). So suppose you have the following implemented in OCaml:

module type Map = sig
  (* For simplicity assume any key and value type is allowed *)
  type ('k, 'v) t

  val make : unit -> ('k, 'v) t
  val put : ('k, 'v) t -> ~key:'k -> ~value:'v -> unit
end

module Hashtable : Map = struct ... end
module HashMap : Map = struct ... end

Then you would write a functor like this:

module MyFunctor(Map : Map) = struct
  let my_map =
    let map = Map.make () in
    Map.put map ~key ~value;
    map
end

Then you would instantiate a module using the functor:

module MyModule = MyFunctor(Hashtable)

And voila, changing the implementation is a one-line diff because both the module implementations conform to the Map signature:

module MyModule = MyFunctor(HashMap)

Upvotes: 0

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66818

Here's an example that shows what I think you're asking for:

# module type HASH = sig type t val hash : t -> int end ;;
module type HASH = sig type t val hash : t -> int end
# module I = struct type t = int let hash i = i end ;;
module I : sig type t = int val hash : 'a -> 'a end
# module J = struct type t = int end ;;
module J : sig type t = int end
# module M : HASH = I ;;
module M : HASH
# module N : HASH = J ;;
Error: Signature mismatch:
       Modules do not match: sig type t = int end is not included in HASH
       The field `hash' is required but not provided

The extra ": HASH" specifies that the module must match the HASH signature (and it also restricts it to that signature).

Just as a side comment, I believe the OCaml module system is world famous for its expressivity (at least in module system circles). I'm still a beginner at it, but it is worth studying.

Upvotes: 1

Kakadu
Kakadu

Reputation: 2839

Since 3.12.1 OCaml allows this syntax for opening and aliasing modules:

let foo .... =
  let module HashTable = HashMap in (* magic is here *)
  let h = HashTable.create () in
  ....

So u just need to rename module what you are using where you are using it.

Upvotes: 1

Related Questions