在 Hashtbl 上编译 OCaml Book 示例

Compiling an OCaml Book example on Hashtbl

我试过编译下面的例子

open Core.Std
module Foo_and_bar : sig
    type t = S of string | I of int
    include Hashable.S with type t := t
  end = struct
    module T = struct
      type t = S of string | I of int with sexp, compare
      let hash t1 t2 = (hashData t1)
    end
    include T
    include Comparable.Make(T)
  end;;

with camlopt -o exec core.cmxa algebra.ml 可以在此 book 上找到,但结果,我在 sexp 声明中遇到错误。我哪里错了?

核心库附带一个可以简化您的生活的工具。它被命名为 corebuild。有了它你可以编译:

corebuild algebra.native

它会生成名为algrebra.native

的可执行文件

关于你的尝试,那么你指的是core.cmxaocamlopt不知道在哪里可以找到它。此外,您需要使用 camlp4 预处理器来启用语法扩展。使用 ocamlfind 包装 ocamlopt 这可以很容易地实现:

ocamlfind ocamlopt -thread -syntax camlp4o -package core.syntax algebra.ml -o exec

这里,-syntax camlp4o启用语法扩展,-package core.syntax启用核心,还启用核心的语法扩展。

但我仍然建议使用 corebuild,至少一开始是这样。