【问题标题】:Encounter unbound value error when using Ocaml StringMap.find_opt使用 Ocaml StringMap.find_opt 时遇到未绑定值错误
【发布时间】: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


【解决方案1】:

据我所知,没有实际的Misc 模块。我从未听说过它,而且它似乎没有出现在手册的实际文本中。唯一的链接(我能找到)来自模块索引。我认为这可能代表某些内部模块的意外泄漏。 (但我可能是错的。)

您可以像这样制作自己的 StringMap 模块:

module StringMap = Map.Make(String)

find_opt 函数是在 OCaml 4.05 中引入的。

【讨论】:

  • 我可以使用omodcompiler-libs包中找到Misc模块:utop # Omod.load "Misc";; [INC] /Users/sf/.opam/4.07.0/lib/ocaml [INC] /Users/sf/.opam/4.07.0/lib/ocaml/compiler-libs - : bool = true但我无法将它加载到顶层...
  • 我确实在文件开头使用模块 StringMap = Map.Make(String) 制作了自己的 StringMap 模块。
  • @PLNewbie 那么,我的猜测(根据我对您的问题的评论)是在文档介绍此功能之前,您使用的是旧版本的 OCaml。
  • @ShonFeder 这可能不是原因,因为我的 ocaml 版本为 4.07.1。
  • 没问题!看起来ocamlbuild -which ocaml 将显示它正在构建的版本。你能确认这显示的是旧版本吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
  • 1970-01-01
相关资源
最近更新 更多