This is really a side question to OCaml programming, just to get a deeper understanding of the Toplevel directives.
When using OCaml’s Toplevel, one may sometimes use some directives such as #load "foo.cmo", #use "foo.ml" or #mod_use "foo.ml" .
The normal way to go is to give a direct string argument to the #use directive:
#use "/home/test/ocaml/lists.ml";; (* OK *)
But giving an OCaml string argument to the #use directive doesn’t work:
Sys.chdir "/home/test/ocaml";;
let s = ( Sys.getcwd () ) ^ "/" ^ "lists.ml";;
(* or *)
let s = "/home/test/ocaml" ^ "/" ^ "lists.ml";;
val s : string = "/home/test/ocaml/lists.ml"
#use s;; (* NO *)
(* or *)
#use ( Sys.getcwd () ) ^ "/" ^ "lists.ml";; (* NO *)
Wrong type of argument for directive `use'.
#use ( ( Sys.getcwd () ) ^ "/" ^ "lists.ml" );; (* NO *)
#use (Sys.getcwd ())^"/"^"lists.ml";;
^^^
Error: Syntax error: operator expected.
How can we tweak that to feed a Toplevel directive such as #use with a prebuilt OCaml string?
++++++++
Side-side question:
is there a way/ a command to get ALL the defined types and bound values of the current environment (within some Toplevel session)?
You can call the directive directly from the Topdirs module:
Topdirs.dir_use Format.std_formatter s;;
You can access Toploop.toplevel_env. I’m not sure what the best way to iterate on it is, but maybe calling Env.summary then traversing the summary manually would work for you.
Ah, sorry, it’s one of these little quirks of the toplevel I forgot about. The toplevel has access to the Env module, but doesn’t re-export it, so you need to load it again yourself. In practice, #load "ocamlcommon.cma";; should do that.
It seems that for some reason, on 4.12.0 you can load ocamlcommon.cma directly, so I forgot to add the extra #directory "+compiler-libs";;. With that extra directory, you should be able not only to load ocamlcommon.cma without the path, but also to use its components (without the directive the toplevel can’t find the corresponding cmi files).
In addition to the very basic information about Toplevel directives in refman and to the ultra basic information from #help;; (topfind), I was wondering if there a place where this is fully documented.
It looks like camlcity.org/Findlib is the expected place.
Is that right? Or are there other places “to know all about Findlib and related tools” ?
Maybe @gstolpmann (author of Findlib) or @samoht (maintainer) (see opam info ocamlfind) can enlighten us about that?
I agree, this needs more visibility. While most functions of Toploop and Topdirs, etc. seem very advanced, the functions for adding directories, loading .cmo files and using .ml files allow to completely eschew using toplevel directives #bla, which also eschews the need for ;; before/after them.
That explains why some functions look a bit too general for basic usage (having a formatter as first parameter). Would having a simplified, select subset of these function as a public module, so that #command;; could be deprecated, be an interesting OCaml RFE ?
Not only would this bring the syntax in line, they are also more powerful (taking real OCaml string, variables, etc.).