【发布时间】:2019-04-21 16:56:38
【问题描述】:
我尝试写一个小的ocaml程序,在使用StringMap.find_opt时遇到了unbound value错误。
我对这个错误感到困惑,因为 find_opt in 是在 https://caml.inria.fr/pub/docs/manual-ocaml/libref/Misc.StringMap.html 中定义的
我找到See if key exists in a String Map 并尝试使用 StringMap.find 代替,但显然在我的程序中 StringMap.find 被定义为 val find : key -> 'a t -> 'a 所以它不能返回类型为 'a 选项的值根据需要。
错误如下所示:
$ ocamlbuild test.native
+ /Users/KKK/.opam/default/bin/ocamlc.opt -c -o semant.cmo semant.ml
File "semant.ml", line 253, characters 19-37:
Error: Unbound value StringMap.find_opt
Command exited with code 2.
Compilation unsuccessful after building 13 targets (11 cached) in 00:00:00.
相关代码如下:
let f2 = function
Some _ -> raise (Failure ("trying to redeclare variable"))
| None ->
let f3 = function
Array(t1, t2) ->
if (check_array_type (t1, t2)) then let lvs' = StringMap.add id t envs.lvs in let envs2 = {stmts = SVdecl(t, id, (Void, SNoexpr)) :: envs.stmts; lvs = lvs'} in envs2
else raise(Failure("array key must be int or string"))
| _ -> let lvs' = StringMap.add id t envs.lvs in let envs2 = {stmts = SVdecl(t, id, (Void, SNoexpr)) :: envs.stmts; lvs = lvs'} in envs2
in f3 t
in f2 (StringMap.find_opt id envs.lvs)
编辑: 我的 Ocaml 版本是 4.07.1。我已经包括了
module StringMap = Map.Make(String)
在我的文件的开头。 编辑2: 事实证明,我的顶级 ocaml 的版本为 4.02.3,这导致了问题。感谢您的帮助!
【问题讨论】:
-
您能说明您正在使用哪个版本的 OCaml 编译器吗?为此,只需使用
ocaml --version的输出编辑您的问题。
标签: ocaml