【问题标题】:OCaml: describe modules in .mli fileOCaml:在 .mli 文件中描述模块
【发布时间】:2016-06-01 19:32:17
【问题描述】:

我正在使用 Core.Std 在 .ml 文件中生成 Set 和 Map:

type temp = int with sexp, compare

type label = Symbol.symbol with sexp, compare

module Temp = struct
  type t = temp with sexp, compare
end
module TempComp = Comparable.Make(Temp)
module TempSet = TempComp.Set 
module TempMap = TempComp.Map

module Label = struct
  type t = label with sexp, compare
end
module LabelComp = Comparable.Make(Label)
module LabelMap = LabelComp.Map

我应该如何在我的.mli 文件中描述TempSet, TempMap, LabelMap

我说: module TempMap : Map.S with type t = temp

但我得到了一个错误:

在这个 `with' 约束中, t 的新定义 与约束签名中的原始定义不匹配: 类型声明不匹配: 类型 t = t 不包括在 type 'a t = (Key.t, 'a, Key.comparator_witness) Map.t

我该如何解决这个错误?

【问题讨论】:

    标签: ocaml


    【解决方案1】:

    'a t 类型是从temp(键)到'a(任意数据)的映射类型。你想说的是,'a t 是来自具体类型 temp 的键的映射,正确的做法是:

    module TempMap : Map.S with type Key.t = temp
    

    但是,虽然这是一种正确的做事方式,但它并不简单,因为它需要您深入研究地图的签名。常见的方式就是说:

    type temp
    module Temp : Comparable with type t = temp
    

    并允许您界面的用户使用Temp.MapTemp.Set 等。此外,请考虑使用更丰富的界面Identifiable,其中还包括哈希表、哈希集和许多其他有用和预期的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多