使用Ocaml StringMap.find_opt时遇到未绑定的值错误

问题描述 投票:0回答:1

我尝试编写一个小的ocaml程序,当我使用StringMap.find_opt时遇到了未绑定的值错误。

我对此错误感到困惑,因为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 option的值。

该错误如下所示:

$ 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
1个回答
1
投票

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

您可以像这样创建自己的StringMap模块:

module StringMap = Map.Make(String)

find_opt函数在OCaml 4.05中引入。

© www.soinside.com 2019 - 2024. All rights reserved.