Ocaml 具有Janestreet Core_内核的映射

Ocaml 具有Janestreet Core_内核的映射,ocaml,Ocaml,我正在用OCAML4.06、emacs/merlin制作真实世界的Ocaml。 请参阅下面的代码片段。我有两个问题: open Core_kernel let () = let digit_alist = [ 0, "zero"; 1, "one"; 2, "two" ; 3, "three"; 4, "four"; 5, "five"; 6, "six"; 7, "seven"; 8, "eight"; 9, "nine" ] in le

我正在用OCAML4.06、emacs/merlin制作真实世界的Ocaml。 请参阅下面的代码片段。我有两个问题:

open Core_kernel

let () =
  let digit_alist = [ 0, "zero"; 1, "one"; 2, "two"  ; 3, "three"; 4, "four";
                      5, "five"; 6, "six"; 7, "seven"; 8, "eight"; 9, "nine" ] in
  let _ = Map.of_alist_exn digit_alist ~comparator:Int.comparator in
  ()
从merlin进行计算时,它显示两个错误。看起来像是
Map.of\u alist\u exn
不接受带标签的参数
~comparator

This expression has type (int * string) list
       but an expression was expected of type
         ('a, 'b) Core_kernel.Map.comparator =
           (module Core_kernel__.Comparator.S with type comparator_witness = 'b and type t = 'a)

The function applied to this argument has type
         ('a * 'b) Core_kernel__.Import.list -> ('a, 'b, 'c) Base__Map.t
This argument cannot be applied with label ~comparator
Q1)列表exn的映射功能类型是否已更改

我认为函数类型已经改变了。所以我修改了源代码如下:

open Core_kernel

let () =
  let digit_alist = [ 0, "zero"; 1, "one"; 2, "two"  ; 3, "three"; 4, "four";
                      5, "five"; 6, "six"; 7, "seven"; 8, "eight"; 9, "nine" ] in
  let _ = Map.of_alist_exn Int.comparator digit_alist in
  ()
This expression has type
         (Core_kernel.Int.t, Core_kernel.Int.comparator_witness)
         Core_kernel__.Comparator.comparator =
           (Core_kernel.Int.t, Core_kernel.Int.comparator_witness)
           Base__Comparator.t
       but an expression was expected of type
         ('a, 'b) Core_kernel.Map.comparator =
           (module Core_kernel__.Comparator.S with type comparator_witness = 'b and type t = 'a)
此时,梅林抱怨如下:

open Core_kernel

let () =
  let digit_alist = [ 0, "zero"; 1, "one"; 2, "two"  ; 3, "three"; 4, "four";
                      5, "five"; 6, "six"; 7, "seven"; 8, "eight"; 9, "nine" ] in
  let _ = Map.of_alist_exn Int.comparator digit_alist in
  ()
This expression has type
         (Core_kernel.Int.t, Core_kernel.Int.comparator_witness)
         Core_kernel__.Comparator.comparator =
           (Core_kernel.Int.t, Core_kernel.Int.comparator_witness)
           Base__Comparator.t
       but an expression was expected of type
         ('a, 'b) Core_kernel.Map.comparator =
           (module Core_kernel__.Comparator.S with type comparator_witness = 'b and type t = 'a)
我希望
Int.comparator
将扮演有效比较器的角色,但ocaml认为它无效

Q2)我应该给比较器什么


@一个挣扎的noob查看比较器的定义,我们可以看到它的定义如下:

type ('k, 'cmp) comparator = (module Comparator.S with type comparator_witness = 'cmp and type t = 'k)
在ocaml中,模块可以用作第一类值。该特性的一个有趣结果是,模块现在可以定义为类型,并作为函数参数/参数传递/使用。Janestreet
core\u kernel
和/或
base
大量使用一流模块,正如您所发现的那样就是这样一个用例

Comparator.S
的定义如下:

module type Core_kernel.Comparator.S
type t
type comparator_witness
val comparator : (t, comparator_witness) comparator
如果查看
utop
module Int
-
show Core\u kernel.Int
的模块签名,我们可以看到它定义了
Core\u kernel.Comparator.S
模块类型中规定的类型和函数

因此,
Map.of\u alist\u exn
接受
(模块Int)
作为有效参数
(module Int)
是ocaml语法,表示值作为一级模块传递

参考文献:


  • 对于Q2)
    Map.of_-alist_-exn(模块Int)数字_-alist
    ?真实世界Ocaml的开发版本不是用来介绍Janestreet核心库的吗?我不明白为什么
    (module Int)
    可以是
    Map.of\u alist\u exn
    的有效参数,它的类型是
    ('a,'cmp)core\u kernel.Map.comparator->('a*'b)core\u kernel\u.Import.list->('a,'b,'cmp)core\u kernel.Map.t