Use several modules in OCaml utop
Asked Answered
F

1

11

I'm reading an OCaml project recently and I want to put the source files in the utop so I can do some experiments.
Suppose I have two files amodule.ml, bmodule.ml.
bmodule.ml will use functions defined in amodule.ml, for example, bmodule use Amodule.anyfunction() where anyfunction() is defined in amodule.ml.
I want to put both of them in utop:

#directory "/directory contain amodule.ml and bmodule.ml"
#use "amodule.ml"
#use "bmodule.ml"

And this doesn't work because Amodule is a module name base on the amodule.ml file and the utop don't know these things, I think.
So how I can put these files in the utop without changing the file content?

Foulard answered 14/12, 2013 at 3:5 Comment(3)
Have you tried to cd to the folder where amodule.ml and bmodule.ml are and then type utop there?Valera
Yes.But it still doesn't work.Foulard
what do you mean it doesn't work? can you post a screenshot or full utop texts?Valera
G
14

#use a.ml executes every statement in a.ml just as if you had typed those statements in the toplevel directly. Thus, you do not get a module A defined, so your other file cannot have things like A.foo. If you want module A, you must first byte compile a.ml, and then #load a.cmo.

Guarneri answered 14/12, 2013 at 18:19 Comment(5)
Newer versions of OCaml support #use_mod which should allow this without prior compilation.Avalokitesvara
The directive mentioned by @Avalokitesvara is actually #mod_use, and it is very handy indeed. :)Alonzoaloof
#mod_use is exactly the piece of the puzzle I was missing. Thank you @ShonFeder! I can't believe Real World OCaml does not mention it even once.Volvox
Anyone want some quick karma by turning #mod_use into a proper StackOverflow answer?Rowden
Thanks @Avalokitesvara ! #mod_use is exactly what I've been looking for for hours, and it answers a very basic need. Unbelievable that it's not mentioned in popular introductions to OCaml.Brion

© 2022 - 2024 — McMap. All rights reserved.