Search code examples
ocamlutop

Loading a module with dependencies in utop


I have two modules A.ml and B.ml like so:

A.ml:

type t = int
let from_int (i : int) : t = i

B.ml:

open A
let my_t : t = from_int 0

I can compile them just fine by invoking ocamlc A.ml B.ml however I have no idea how to load them both in utop in order to use my_t interactively. Using:

  • utop -init B.ml yields Error: Reference to undefined global 'A'
  • utop followed by #use "A.ml";; and #use "B.ml";; leads to the same error
  • removing open A from B.ml makes this double #use work but ocamlc A.ml B.ml now fails on B with Error: Unbound type constructor t.

Solution

  • You have to compile first a.ml :

      ocamlc -c a.ml  // yields a.cmo
    

    in utop :

      #load "a.cmo";;
      #use "b.ml";;