I tried to write a small ocaml program and encountered an unbound value error when I used StringMap.find_opt.

I am confused by this error since find_opt in is defined in https://caml.inria.fr/pub/docs/manual-ocaml/libref/Misc.StringMap.html

I found See if key exists in a String Map and tried to use StringMap.find instead but clearly in my program StringMap.find is defined as val find : key -> 'a t -> 'a so it cannot return a value of type 'a option as desired.

The error looks like the following:

$ 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.

and the relevant code looks like the following:

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)

Edit: My Ocaml version is 4.07.1. and I already included

module StringMap = Map.Make(String)

at the beginning of my file. Edit2: It turns out that my toplevel ocaml has version 4.02.3 and that caused the problem. Thanks for the help!

1 Answers

1
Jeffrey Scofield On Best Solutions

As near as I can tell, there is no actual Misc module. I've never heard of it, and it doesn't seem to appear in the actual text of the manual. The only link (that I can find) is from the index of modules. I'm thinking this might represent unintended leakage of some internal modules. (But I could be wrong.)

You can make your own StringMap module like this:

module StringMap = Map.Make(String)

The find_opt function was introduced in OCaml 4.05.