If the toplevel is already launched, you can dynamically load the library:
# #load "nums.cma";;
# Num.mult_num;;
- : Num.num -> Num.num -> Num.num = <fun>
Another possibility (which will work for all third party libraries and will manage paths and dependencies for you) is to use ocamlfind
. For this, issue
#use "topfind";;
(or better put it in your ~/.ocamlinit
file). To load a library, just do
# #require "num";;
/usr/lib/ocaml/nums.cma: loaded
/home/user/.opam/system/lib/num-top: added to search path
/home/user/.opam/system/lib/num-top/num_top.cma: loaded
/home/user/.opam/system/lib/num: added to search path
(If ocamlfind
— hence topfind
— is not available, install it using opam.)
Here is an example of multiplication:
# Num.(num_of_int 30 */ num_of_int 1234);;
- : Num.num = Num.Int 37020
The construction Num.(e)
is a shorthand for let open Num in e
and makes possible to use the Num
functions without prefix in e
. Here is a definition of the factorial:
# let rec fac n =
let open Num in
if n =/ Int 0 then Int 1 else n */ fac (n -/ Int 1);;
val fac : Num.num -> Num.num = <fun>
You can try it with
# fac Num.(Int 100);;
- : Num.num = Num.Big_int <abstr>
If you used #require
, it installs a pretty printer for Num
values so the previous interaction looks like:
# fac Num.(Int 100);;
- : Num.num =
<num 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000>
which is much easier to read!