3

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.
gallais
  • 11,823
  • 2
  • 30
  • 63
  • 1
    `#use` is for direct inclusion - to dump a file into the toplevel as a _module_, you want `#mod_use`. There's also ocamlmktop, which constructs a toplevel with a bunch of modules linked into it. And finally there is `#load` as given in Pierre's answer. – gsg Oct 25 '17 at 07:45

1 Answers1

5

You have to compile first a.ml :

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

in utop :

  #load "a.cmo";;
  #use "b.ml";;
Pierre G.
  • 4,346
  • 1
  • 12
  • 25
  • I see. It's even possible to throw these into an `init.ml` and use `utop -init init.ml` in order not to have to invoke these commands each time you start `utop`. Thanks! – gallais Oct 24 '17 at 17:31