2015-05-10 46 views
0

我試圖編譯下面的例子的OCaml的書例如在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;; 

camlopt -o exec core.cmxa algebra.ml,可以在此book中查到,但是其結果是,我得到一個錯誤與SEXP聲明。我哪裏錯了?

回答

4

核心庫附帶一個工具,可以簡化您的生活。它被命名爲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,至少在開始時。

+0

那麼這個庫完全替代標準的OCaml嗎?在同一個源代碼中,我有一個聲明模塊IntSet = Set.Make( struct let compare = Pervasives.compare type t = int end)'。所以我必須用來自其他基礎庫的集合來替換它? – jackb

+1

是的。而且你必須小心不要意外擊中庫存。好消息是,它們已經爲所有類型實例化了地圖,集合,哈希表等。例如,'IntSet'在'Int.Set'下可用。還有'Int.Map','Int.Table','Int.Hash_set'等等 – ivg