# First attempt to compile source using Zarith

**URL:** <https://discuss.ocaml.org/t/first-attempt-to-compile-source-using-zarith/16774>\
**Category:** Learning\
**Created:** [June 7, 2025, 10:39am UTC](https://discuss.ocaml.org/t/first-attempt-to-compile-source-using-zarith/16774 "2025-06-07T10:39:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![laurentcheno](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/laurentcheno/32/5978_2.png) [@laurentcheno](https://discuss.ocaml.org/u/laurentcheno)\
**Post date:** [June 7, 2025, 10:39am UTC](https://discuss.ocaml.org/t/first-attempt-to-compile-source-using-zarith/16774/1 "2025-06-07T10:39:19Z")

</div>

Please excuse my poor English and my very naive question.

I’m trying to use Zarith for calculation with big numbers.

I wrote this very little code :

```auto
open Zarith

let rec puissance a b = Z.pow (Z.of_int a) b

```

in a file test.ml.

I call the compiler like this :

```auto
ocamlopt -I `ocamlfind query zarith` zarith.cmxa test.ml -o test

```

(I just copied the `ocamlopt -I ‘ocamlfind query graphics‘ graphics.cmxa cardioid.ml -o cardioid` command from book Learning Programming with OCaml.)

But I have this error

```auto
File "test.ml", line 1, characters 6-12:
1 | open Zarith
          ^^^^^^
Error: Unbound module "Zarith"

```

In interactive mode (using ocaml in a terminal) the `#require "zarith";;` works well.

I guess it’s a beginner’s mistake and I appeal to your indulgence.

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [June 7, 2025, 11:55am UTC](https://discuss.ocaml.org/t/first-attempt-to-compile-source-using-zarith/16774/2 "2025-06-07T11:55:13Z")

</div>

> [@laurentcheno](#):
>
> But I have this error

I don’t think there is any “wrapper” module `Zarith` in the `zarith` library, ie `Z` is already a top-level module. If you remove `open Zarith` then your code should build

> [@laurentcheno](#):
>
> I appeal to your indulgence.

No need to do so!

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![laurentcheno](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/laurentcheno/32/5978_2.png) [@laurentcheno](https://discuss.ocaml.org/u/laurentcheno)\
**Post date:** [June 7, 2025, 12:15pm UTC](https://discuss.ocaml.org/t/first-attempt-to-compile-source-using-zarith/16774/3 "2025-06-07T12:15:06Z")

</div>

How I know if I should or not insert a `open Module` ?  
Where is documented the difference between Graphics and Zarith of this issue ?

Can you recommend some useful reading for compiling programs using modules?

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [June 7, 2025, 12:42pm UTC](https://discuss.ocaml.org/t/first-attempt-to-compile-source-using-zarith/16774/4 "2025-06-07T12:42:33Z")

</div>

> [@laurentcheno](#):
>
> How I know if I should or not insert a `open Module` ?

It depends on each library: some libraries are organized with a “wrapping” module named the same way as the library which contains every other item of the library (including any modules that the library may export). This is more typically used nowadays. But as far as OCaml is concerned a library is just a “bag of modules”, and so certain libraries (like `zarith`) simply consist of a few toplevel modules and there is no wrapper module called `Zarith`.

> [@laurentcheno](#):
>
> Where is documented the difference between Graphics and Zarith of this issue ?

The only way to know is look at the documentation of each library or, absent that, look directly in the source (one can also guess it by looking at the `.cmi` artifacts on the file system or by using Merlin/LSP in your editor).

> [@laurentcheno](#):
>
> Can you recommend some useful reading for compiling programs using modules?

Unfortunately, nothing current comes to mind. Nowadays most people use a build system (eg Dune) to build their OCaml code, which largely insulates you from having to understand how things work in detail…

The classic French book “Le Langage Caml” [https://caml.inria.fr/distrib/books/llc.pdf](https://caml.inria.fr/distrib/books/llc.pdf) has a discussion of separate compilation in Chapter 10, but keep in mind that this book was written for Caml Light, so many details need to be adapted to apply to OCaml (however, the substance of the matter remains the same).

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![xavierleroy](https://avatars.discourse-cdn.com/v4/letter/x/a9adbd/32.png) [@xavierleroy](https://discuss.ocaml.org/u/xavierleroy)\
**Post date:** [June 7, 2025, 1:12pm UTC](https://discuss.ocaml.org/t/first-attempt-to-compile-source-using-zarith/16774/5 "2025-06-07T13:12:56Z")

</div>

> [@laurentcheno](#):
>
> I call the compiler like this :
> 
> ```auto
> ocamlopt -I `ocamlfind query zarith` zarith.cmxa test.ml -o test
> 
> ```

I’d suggest you write

```auto
ocamlfind ocamlopt -package zarith -linkpkg test.ml -o test

```

it’s more idiomatic and it can handle more complex dependencies than the `ocamlfind query zarith` trick you used. And if you need to use the graphics library as well, replace `zarith` by `zarith,graphics`.

> [@laurentcheno](#):
>
> How I know if I should or not insert a `open Module` ?  
> Where is documented the difference between Graphics and Zarith of this issue ?

In the fine documentation at [OCaml Packages · Browse Community Packages](https://ocaml.org/packages) . After a bit of navigation, you’ll find [zarith 1.14 (latest) · OCaml Package](https://ocaml.org/p/zarith/latest/doc/index.html), which shows:

> # zarith 1.14
> 
> ## Libraries
> 
> This package provides the following libraries (via ocamlobjinfo):
> 
> ### zarith
> 
> Documentation:
> 
> - `Zarith_version`
> - [`Z`](https://ocaml.org/p/zarith/latest/doc/Z/index.html) Integers.
> - [`Q`](https://ocaml.org/p/zarith/latest/doc/Q/index.html) Rationals.
> - [`Big_int_Z`](https://ocaml.org/p/zarith/latest/doc/Big_int_Z/index.html) `Big_int` interface for Z module.

So, there’s an OPAM package called “zarith”, at version 1.14. It contains a library called “zarith” as well. This library name is what you give to `ocamlfind -package`. This library provides top-level modules called `Z`, `Q`, `Zarith_version`, and `Big_int_Z`. To use a function from the `Z` module, say the `pow` function, you can write either `Z.pow` or do `open Z` and then just write `pow`.

Now, compare with [https://ocaml.org/p/graphics/latest/doc/index.html:](https://ocaml.org/p/graphics/latest/doc/index.html:)

> # graphics 5.1.2
> 
> ## Libraries
> 
> This package provides the following libraries (via ocamlobjinfo):
> 
> ### graphics
> 
> Documentation:
> 
> - [`Graphics`](https://ocaml.org/p/graphics/latest/doc/Graphics/index.html) Machine-independent graphics primitives.
> - [`GraphicsX11`](https://ocaml.org/p/graphics/latest/doc/GraphicsX11/index.html) Additional graphics primitives for the X Windows system.

Here, the library is “graphics”, provides two top-level modules `Graphics` and `GraphicsX11`. To use the `open_graph` function from module `Graphics`, you write either `Graphics.open_graph` or do `open Graphics` and then just write `open_graph`.

So, it all boils down to using either fully-qualified names like `Z.pow` or use short names like `open_graph` after “opening” the module.

I’m sure you get it now. More puissance to you.
