【发布时间】:2020-04-25 16:32:40
【问题描述】:
TL;DR - 在 OCaml 中,如何调用类似于 type 'elt set = (module BatSet.S with type elt = 'elt) 的类型以及如何生成该类型的值?
目前,我正在阅读 Tezos 协议[*] 中的代码,我看到了以下代码。
module type Boxed_set = sig
type elt
val elt_ty : elt comparable_ty
module OPS : S.SET with type elt = elt
val boxed : OPS.t
val size : int
end
type 'elt set = (module Boxed_set with type elt = 'elt)
我从未听说过像type a = (module B) 这样的语法。所以我打开一个顶级 OCaml 解释器并使用模块 batteries 重现类似的用法。以下代码是我复制的日志。
$ ocaml
OCaml version 4.07.1
# #use "topfind";;
- : unit = ()
Findlib has been successfully loaded. Additional directives:
#require "package";; to load a package
#list;; to list the available packages
#camlp4o;; to load camlp4 (standard syntax)
#camlp4r;; to load camlp4 (revised syntax)
#predicates "p,q,...";; to set these predicates
Topfind.reset();; to force that packages will be reloaded
#thread;; to enable threads
- : unit = ()
# #require "batteries";;
[...]: loaded
# type 'elt set = (module BatSet.S with type elt = 'elt);;
type 'elt set = (module BatSet.S with type elt = 'elt)
这种奇怪的类型定义确实有效,但我卡在这里。模块本身如何成为类型?如何创建一个类型为'elt set 的值,如let v : int set = (...)?有什么关键字可以调用这样的类型吗?
[*] https://gitlab.com/tezos/tezos/blob/master/src/proto_alpha/lib_protocol/script_typed_ir.ml,提交哈希:86b5227f7efd8aa78fcc427776920480c6c0e780
【问题讨论】: