OCaml子模块在模块类型中具有匿名签名

时间:2015-12-24 21:59:58

标签: ocaml

我正在尝试使用Map.Make实现对其键和值类型通用的Trie。这个仿函数接受一个Map.OrderedType并生成一个模块,里面定义了一堆Map实用程序。但是,Map.Make的返回模块类型是匿名的。我想在我的Trie模块类型中公开我的TrieType正在使用的特定Map实现。

有没有办法引用Map.Make的返回模块类型?或者,是否可以在SML中使用类似于透明归属的内容来公开模块M,而不必为其保留特定的模块类型或隐藏其任何成员?

module OrderedStringType : Map.OrderedType = struct
    type t = string
    let compare = String.compare
end

module type TrieType = sig

    (* I want to expose the M submodule in the interface, but its
     * module type is anonymous *)
    (* here is some strawman syntax *)
    (* module M : Map.Make(Set.OrderedType) *)
    type 'a t
end

module Trie(X : Map.OrderedType) : TrieType = struct
    module M = Map.Make(X)

    type 'a t = EmptyTrie | Trie of 'a tChild
    and 'a tChild = {value : 'a option ; children : 'a t M.t}
end


module TruncatedTrie(X : TrieType) = struct
    (* Implementation to follow *)
end

1 个答案:

答案 0 :(得分:1)

Map.Make仿函数生成的模块类型不是匿名的,但实际上有一个名称:Map.S。您可以扩展甚至隐藏签名中的字段:

module type Trie = sig
  include Map.S
  val new_method : t -> int
end

要隐藏方法,您可以使用某些无人居住的类型重新定义它:

module type Trie = sig
  include Map.S
  val length : [> `trie_doesn't_have_length_method ]
end

后者可以被视为黑客攻击,因为更清晰的方法是定义自己的签名。