Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用核心库在ocaml中定义映射类型_Ocaml_Ocaml Core - Fatal编程技术网

使用核心库在ocaml中定义映射类型

使用核心库在ocaml中定义映射类型,ocaml,ocaml-core,Ocaml,Ocaml Core,OCaml语言的核心库附带了非常有用的映射和表模块。 如果我想使用某个内置类型的映射,我知道如何定义自己的类型: type mytype = int String.Map.t (* A mapping from strings to integers *) 我还知道如何使用多态比较定义自定义映射: type mytype = (string, string) Map.Poly.t (* A map from strings to strings *) 我不知道的是如何使用非多态性比较从我自己

OCaml语言的核心库附带了非常有用的映射和表模块。 如果我想使用某个内置类型的映射,我知道如何定义自己的类型:

type mytype = int String.Map.t (* A mapping from strings to integers *)
我还知道如何使用多态比较定义自定义映射:

type mytype = (string, string) Map.Poly.t (* A map from strings to strings *)
我不知道的是如何使用非多态性比较从我自己的类型到我自己的类型来定义自定义映射。 假设我有

type row_t = Row of int
type column_t = Column of int
(* I want a map from rows to columns *)
type mymap_t = (row_t, column_t, ???) Map.t

我知道第三个参数应该是comparator,但我不知道应该放在里面什么:
Int.comparator
Int.comparator\u-witness
都不能给出期望的结果。

你可以参考Ashish提到的博客文章

然而,在使用Core时,我通常更喜欢用“自动”的方式为自定义结构生成映射和集(这要感谢Core语法扩展)

下面是一个小例子:

module T = struct
  type t = Row of int
  with sexp, compare
end
include T
include Comparable.Make(T)
因此,这将生成您通常需要的所有比较函数(以及其他有用函数)和基本数据结构:

type t = T.t = Row of int
...
val (>) : T.t -> T.t -> bool = <fun>    (* compare functions *)
val (<) : T.t -> T.t -> bool = <fun>
val equal : T.t -> T.t -> bool = <fun>
val compare : T.t -> T.t -> int = <fun>
val min : T.t -> T.t -> T.t = <fun>
val max : T.t -> T.t -> T.t = <fun>
...
module Map :  (* non-polymorphic Map module *)
...
end
module Set :  (* non-polymorphic Set module *)
...
end

真有帮助。哇,谢谢!看到
compariable.Make(T)
实际上创建了
Set
Map
和一系列其他元素,而不仅仅是比较器,这着实令人惊讶。
type column_t = Column of int
let map = Map.singleton (Row 1) (Column 2)