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

OCaml submodules with anonymous signatures in module types

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

有没有办法引用Map.Make的return模块类型?或者,是否可以在 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

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

后者可以被视为 hack,因为更简洁的方法是定义您自己的签名。