Issue when opening a module

Hello!

OCaml noob here! I’m trying to use the “Batteries” module, but… it doesnt work!

Here is an example:

open Batteries;;

print_endline (dump [5;4;2]);;

I’m compiling with: opam exec ocamlc main.ml the error message is:

File "main.ml", line 1, characters 5-14:
1 | open Batteries;;
         ^^^^^^^^^
Error: Unbound module Batteries

and the Batteries module seems installed:

$ opam install Batteries
[NOTE] Package batteries is already installed (current version is 3.0.0).

What am I missing?

Btw, do we need the ;; at the end of the open statement? I’ve found both syntax, what is the recommended one?

Hi, and welcome!

ocamlc is strictly a compiler. If you’re familiar with C, it’s equivalent to something like gcc.

What that means is that if I write a C program which requires me to look up header files or link against libraries in non-standard locations, I need to inform gcc of this (with -I /my/header_dir or -L /my/lib_dir -l my_lib, respectively).

If you’re using a build system such as Dune, it will take of these details for you and invoke tools such as ocamlc with the correct arguments automatically.

It’s still possible to use ocamlc directly in a somewhat automatic fashion using a tool called Findlib.

In your example, the solution is to invoke ocamlc through the ocamlfind program (part of Findlib):

$ ocamlfind ocamlc -package batteries -linkpkg main.ml -o main

The above will use Findlib to query the location of the files installed by the batteries package and pass them to ocamlc.

In answer to your second question, ;; is never (to my best knowledge) required in OCaml source code. It’s only necessary while using the toplevel (ie, the REPL) in order to terminate expressions.

Your example can be written like this:

open Batteries

let () = print_endline (dump [5; 4; 3])
2 Likes

Thank you @jhaberku for your detailled answer! The doc is huge and it is not easy to find the info we need :slight_smile:

About ;;, isn’t it mandatory for multiple lines declarations? Like in:

 let rec getVars (formula : tformula) : VarsSet.t =
    match formula with
    | Value _ -> VarsSet.empty
    | Var var ->  VarsSet.singleton var
    | Not f -> getVars f
    | And (f1, f2) -> VarsSet.union (getVars f1) (getVars f2)
    | Or (f1, f2) -> VarsSet.union (getVars f1) (getVars f2)
    | Implies (f1, f2) -> VarsSet.union (getVars f1) (getVars f2)
    | Equivalent (f1, f2) -> VarsSet.union (getVars f1) (getVars f2);;

? Without ;; the compiler complains on the next line.

Can you give us the precise compiler error ? At first glance this code seems fine and should work without the ;; (unless you’re working in toplevel)

I’m trying to compile this:

let rec getVars (formula : tformula) : VarsSet.t =
    match formula with
    | Value _ -> VarsSet.empty
    | Var var ->  VarsSet.singleton var
    | Not f -> getVars f
    | And (f1, f2) -> VarsSet.union (getVars f1) (getVars f2)
    | Or (f1, f2) -> VarsSet.union (getVars f1) (getVars f2)
    | Implies (f1, f2) -> VarsSet.union (getVars f1) (getVars f2)
    | Equivalent (f1, f2) -> VarsSet.union (getVars f1) (getVars f2)


assert (VarsSet.equal 
            (getVars ex1) 
            (VarsSet.of_list ["P2"; "P1"; "Q1"; "Q2"])
);;

And I have the following error:

39 | assert (VarsSet.equal 
     ^^^^^^
Error: Syntax error

So yes I’m working at top level (this assert is here to perform some quick & dirty test). Hence I understand that working at top level is not an idiomatic ocaml usage, am I right?

You need to add a ;; to separate both items. But it is true that this style is not recommended nowadays. It would be better to use:

let () =
  assert ...

If you use this form, no ;; is necessary. The syntax of the accepted terms can be found at https://caml.inria.fr/pub/docs/manual-ocaml/modules.html#module-items

Cheers,
Nicolás

3 Likes

By the way, when OCaml people say ‘toplevel’ we mean the REPL (the interactive OCaml console, typically utop). So ;; is required by the REPL because that’s how it distinguishes the end of an input; but it’s not required in source code files.

1 Like