OCaml Unbound value List.assoc

3.4k views Asked by At

I'm compiling a module with a function that calls List.assoc, it fails to build giving me "Unbound value List.assoc" ... i've tried other List functions and they work fine, it also works fine in utop.

I can reproduce this by compiling an ml file using corebuild and this code

open Core.Std

let p = [(1,2);(2,3);(3,4)]
in List.assoc 2 p

Here is my build command:

$ corebuild blah.byte + ocamlfind ocamlc -c -w A-4-33-40-41-42-43-34-44 -strict-sequence -g -bin-annot -short-paths -thread -syntax camlp4o -package bin_prot.syntax -package sexplib.syntax,comparelib.syntax,fieldslib.syntax,variantslib.syntax -package core -o blah.cmo blah.ml
File "blah.ml", line 5, characters 3-13: Error: Unbound value List.assoc Command exited with code 2.
2

There are 2 answers

0
hcarty On BEST ANSWER

The Core library replaces the standard OCaml List module. When you open Core.Std you mask OCaml's standard List with Core's Core.Std.List module. The Core.Std.List.assoc function does not exist. If you aren't opening Core.Std in utop then you're most likely still working with OCaml's standard library List module.

Core does provide a replacement for this functionality with List.Assoc. You can see some documentation here: https://ocaml.janestreet.com/ocaml-core/latest/doc/core_kernel.112.17.00/_build/lib/core_list/#/module:Assoc

List.Assoc.find_exn looks like it is a replacement for the stdlib's List.assoc.

0
xingfe123 On

The API has changed to:

List.assoc 1 p 

see this: http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html