Try to further understanding the interface/module of OCaml

喜夏-厌秋 提交于 2019-12-02 16:43:08

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/15004147/try-to-further-understanding-the-interface-module-of-ocaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!