Ocaml 带元组键的Base.Hashtbl的使用

Ocaml 带元组键的Base.Hashtbl的使用,ocaml,Ocaml,我是ocaml的新手。我想用int*int元组作为键。这是正在使用的教程。有元组模块吗?可以使用int*int定义模块吗?我发现的示例似乎都来自struct 以下是我现在拥有此代码的建议之一: module IntPair = struct module T = struct type t = int * int let compare x y = Tuple2.compare ~cmp1:Int.compare ~cmp2:Int.compare x y let s

我是ocaml的新手。我想用int*int元组作为键。这是正在使用的教程。有元组模块吗?可以使用int*int定义模块吗?我发现的示例似乎都来自struct

以下是我现在拥有此代码的建议之一:

module IntPair = struct
  module T = struct
    type t = int * int
    let compare x y = Tuple2.compare ~cmp1:Int.compare ~cmp2:Int.compare x y
    let sexp_of_t = Tuple2.sexp_of_t Int.sexp_of_t Int.sexp_of_t
  end

  include T
  include Comparable.Make(T)
end

let my_table = Hashtbl.create(module IntPair);;
但我得到了以下错误: 错误:签名不匹配: ... 值't_of_sexp'是必需的,但未提供


什么是sexp的t_?

t_of sexp
是t的
sexp_的倒数,并且在
Tuple2
Int
中具有类似的辅助函数,您应该能够使用它们。对于
hash
您可以使用
Hashtbl.hash
。像这样的方法应该会奏效:

module IntPair = struct
  module T = struct
    type t = int * int
    let compare x y = Tuple2.compare ~cmp1:Int.compare ~cmp2:Int.compare x y
    let sexp_of_t = Tuple2.sexp_of_t Int.sexp_of_t Int.sexp_of_t
    let t_of_sexp = Tuple2.t_of_sexp Int.t_of_sexp Int.t_of_sexp
    let hash = Hashtbl.hash
  end

  include T
  include Comparable.Make(T)
end

let my_table = Hashtbl.create(module IntPair);;

这回答了你的问题吗?简短回答:在
Core\u kernel
中有一个
Tuple2
模块,但在
Base
@glennsl Hashtbl中没有。create(module Tuple2)产生错误:“值`hash'是必需的,但没有提供”这似乎有效。但是我不得不移动桌子让我的桌子。。。语句转换为函数,否则会出现编译错误,因为未定义值的类型。所以sexp_of_t和t_of_sexp类似于序列化函数?是否有一种方法可以打印出哈希表的内容以进行调试?实际上,它使用s表达式作为序列化格式。因此,您可以使用
Hashtbl.sexp\u of\u t
序列化哈希表,并使用
sexp.to\u string\u hum