Ocaml: unknown directive `require' - ocaml

When loading OCaml, I receive this message when loading ocaml at the terminal:
ocaml
OCaml version 4.07.1
Unknown directive `require'.
What is the problem exactly?
I had previously modified my ocamlinit file, because I had some problems. It now contains:
(* ## added by OPAM user-setup for ocamltop / base ## 3ec62baf6f9c219ae06d9814069da862 ## you can edit, but keep this line *)
(* Added by OPAM. *)
let () = try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
with Not_found -> ()
;;
#require "yojson";;
#use "topfind";;
#camlp4o
#thread;;
Topfind.don't_load ["compiler-libs.toplevel"];;
#require "core.top";;
#require "core.syntax";;
(* ## end of OPAM user-setup addition for ocamltop / base ## keep this line *)
Edit:
I looked at this question before ocaml command line cannot find “topfind”, however I did not find it helpful, since nowhere in the answers was it specified that you need to run eval $(opam config env) every time before opening ocaml, as someone below has informed me. So I think this person's clarification is useful to have on this site for other people.

You shall put #use "topfind" before any #require directive. So put #require "yojson";; to the end of the .ocamlinit file (also it is a good idea to add it after the comment).
The #require directive is provided by the ocamlfind tool via the topfind script, which is loaded into the toplevel via the #use directive, which is a standard builtin directive for loading files. The topfind file initializes the ocamlfind system in the toplevel, so that the toplevel now can access to the ocamlfind infrastructure and load libraries installed in the system. If you're using opam to install packages, then do not forget to do eval $(opam config env) (or a shorter version, available in opam 2.x eval $(opam env)) in your terminal, before starting the toplevel. E.g.,
eval $(opam config env)
ocaml
and here is the correct contents of the .ocamlinit file:
(* ## added by OPAM user-setup for ocamltop / base ## 3ec62baf6f9c219ae06d9814069da862 ## you can edit, but keep this line *)
(* Added by OPAM. *)
let () = try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
with Not_found -> ()
;;
#use "topfind";;
#camlp4o
#thread;;
Topfind.don't_load ["compiler-libs.toplevel"];;
#require "core.top";;
#require "core.syntax";;
(* ## end of OPAM user-setup addition for ocamltop / base ## keep this line *)
#require "yojson";;

Related

use packages installed with opam inside utop

I installed yojson with opam:
opam install yojson
and I want to use it inside utop, but I haven't been able to make it work. I've tried these commands inside utop and none of them worked (it complains that it can't find the file / package):
#use "Yojson";;
#use "yojson";;
#require "Yojson";;
#require "yojson";;
Is there any additional configuration I should be aware of to use opam packages in utop?
The correct invocation is (include the # character):
#use "topfind";;
#require "yojson";;
The first command enables the #require directive (it is not the standard one, but comes from the ocamlfind tool), it is a good idea to add it to your ~/.ocamlinit file, if it is not there already. The second directive, actually loads the yojson library. You can also use the #list directive to list all available packages, as well as the ocamlfind list shell command for the same puprose.

Use ocamlfind with ocaml interpreter, without compiling

I have a file main.ml that uses some package that makes the command ocaml main.ml fail and report this error:
Error: Unbound module X
And the only solution I've found so far is to run:
ocamlbuild main.native -use-ocamlfind -package X
./main.native
But this compiles the file. Is there a way to just run the regular ocaml interpreter (ocaml main.ml) with some additional flags to make it find the package?
The easiest way is probably to use topfind and add #require "core" at the top of your file. If you want to avoid adding toplevel directives in your file, you can use ocamlfind query -i-format -r to compute the required include directories:
ocaml $(ocamlfind query -i-format -r core) main.ml
You can put at the beginning of your file some top-level directives which find and load needed modules. For example, when I want to use Core library in my script I insert:
#use "topfind";;
#thread;;
#require "core";;
open Core.Std
(* Code of my script *)
To use topfind package ocamlfind has to be installed (opam install ocamlfind)
You can also query the path of your package to ocamlfind :
ocamlfind query package_X
That will return the path of your package : /path_to/packageX
And then :
ocaml main.ml -I /path_to/packageX

replace camlp4 with camlp5 in utop

I'm working with OCaml code that needs camlp5; however, the toplevel (I'm using utop) automatically loads the newer, incompatible camlp4. These are the startup messages:
Findlib has been successfully loaded. Additional directives:
#require "package";; to load a package
#list;; to list the available packages
#camlp4o;; to load camlp4 (standard syntax)
#camlp4r;; to load camlp4 (revised syntax)
#predicates "p,q,...";; to set these predicates
Topfind.reset();; to force that packages will be reloaded
#thread;; to enable threads
The .ocamlinit im using looks like this:
#use "topfind";;
#thread;;
I installed camlp5 with opam and it shows up when I issue ocamlfind list; also the file camlp5.cma is located in /home/<username>/.opam/4.02.1/lib/camlp5/camlp5.cma.
But trying to load camlp5o and camlp5r fails with Unknown directive camlp5o.
How can i make utop aware of camlp5 and how can I replace camlp4 with camlp5 as the default preprocessor?
Many thanks in advance!
$ ocaml
OCaml version 4.02.1
Findlib has been successfully loaded. Additional directives:
#require "package";; to load a package
#list;; to list the available packages
#camlp4o;; to load camlp4 (standard syntax)
#camlp4r;; to load camlp4 (revised syntax)
#predicates "p,q,...";; to set these predicates
Topfind.reset();; to force that packages will be reloaded
#thread;; to enable threads
# #require "camlp5";;
/home/kakadu/.opam/4.02.1/lib/camlp5: added to search path
# #load "camlp5o.cma";;
Camlp5 parsing version 6.12
#

Can ocaml-top load external libraries?

I'm learning OCaml following the tutorial http://caml.inria.fr/pub/docs/oreilly-book/html/index.html. I'd like to try some examples using list functions of 'core' library.
I start ocaml-top (http://www.typerex.org/ocaml-top.html) and get the following error when open 'Core.Std':
# open Core.Std
Characters 0-13:
open Core.Std;;
^^^^^^^^^^^^^
Error: Unbound module Core
The file ~/.ocamlinit contains the following lines (added from Ocaml and Opam: unbound module Core):
(* Added by OPAM. *)
let () =
try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
with Not_found -> ()
;;
#use "topfind"
#camlp4o
#thread
#require "core"
#require "core.top"
#require "core.syntax"
Executing ocaml from command line works fine (it loads a lot of .cma files on init) and there is no problem opening 'Core.Std'.
I can compile the module with:
$ ocamlfind ocamlc -thread -package core calc.ml
I've created my own toplevel linked with 'core', and works fine too:
$ ocamlfind ocamlmktop -o coretop -thread -package core
But ocaml-top can't open 'Core.Std'. Launching ocaml-top with my toplevel doesn't open 'Core.Std'
$ ocaml-top -ocaml /home/thelinuxkitten/coretop
Inserting the contents of ~/.ocamlinit at ocaml-top's prompt
(* Added by OPAM. *)
let () =
try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
with Not_found -> ()
;;
#use "topfind";;
#camlp4o;;
#thread;;
#require "core";;
#require "core.top";;
#require "core.syntax";;
produces the following output in the evaluation panel:
OCaml version 4.01.0
# let () =
try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
with Not_found -> ()
# #use "topfind"
- : unit = ()
Findlib has been successfully loaded. Additional directives:
#require "package";; to load a package
#list;; to list the available packages
#camlp4o;; to load camlp4 (standard syntax)
#camlp4r;; to load camlp4 (revised syntax)
#predicates "p,q,...";; to set these predicates
Topfind.reset();; to force that packages will be reloaded
#thread;; to enable threads
- : unit = ()
# #camlp4o
Camlp4 Parsing version 4.01.0
# #thread
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/lib/ocaml/dynlink.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/lib/ocaml/camlp4: added to search path
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/lib/ocaml/camlp4/camlp4o.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/lib/ocaml/threads: added to search path
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/lib/ocaml/unix.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/lib/ocaml/threads/threads.cma: loaded
# #require "core"
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/lib/ocaml/bigarray.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/bin_prot: added to search path
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/bin_prot/bin_prot.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/variantslib: added to search path
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/variantslib/variantslib.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/sexplib: added to search path
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/sexplib/sexplib.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/fieldslib: added to search path
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/fieldslib/fieldslib.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/pa_bench: added to search path
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/pa_bench/pa_bench_lib.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/oUnit: added to search path
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/oUnit/oUnitAdvanced.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/oUnit/oUnit.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/pa_ounit: added to search path
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/pa_ounit/pa_ounit_lib.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/typerep_kernel: added to search path
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/typerep_kernel/typerep_kernel.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/core_kernel: added to search path
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/core_kernel/raise_without_backtrace.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/core_kernel/core_kernel.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/pa_test: added to search path
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/pa_test/pa_test.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/core: added to search path
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/core/core.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/core/core_top.cma: loaded
# #require "core.top"
# #require "core.syntax"
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/core/core_top.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/type_conv: added to search path
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/type_conv/pa_type_conv.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/sexplib/pa_sexp_conv.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/fieldslib/pa_fields_conv.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/variantslib/pa_variants_conv.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/comparelib: added to search path
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/comparelib/comparelib.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/comparelib/pa_compare.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/bin_prot/pa_bin_prot.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/custom_printf: added to search path
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/custom_printf/pa_custom_printf.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/pa_pipebang: added to search path
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/pa_pipebang/pa_pipebang.cma: loaded
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/herelib: added to search path
/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/herelib/pa_herelib.cma: loaded
#
I've installed OCaml, core and ocaml-top with OPAM
$ opam config env
CAML_LD_LIBRARY_PATH="/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/stublibs:/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/lib/ocaml/stublibs"; export CAML_LD_LIBRARY_PATH;
OPAMROOT="/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam"; export OPAMROOT;
PERL5LIB="/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/perl5:"; export PERL5LIB;
OCAML_TOPLEVEL_PATH="/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/lib/toplevel"; export OCAML_TOPLEVEL_PATH;
MANPATH="/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/man:/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/man:/usr/local/man:/usr/brlcad/share/man"; export MANPATH;
PATH="/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/.opam/system/bin:/home/thelinuxkitten/ocamlbrew/ocaml-4.01.0/bin:/home/thelinuxkitten/debian/script:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/brlcad/bin"; export PATH;
ocaml-top's web have a simplest documentation.
So, my question is: Anyone knowns if ocaml-top can load and open external libs like 'core'?
Thanks
Notice how .ocamlinit has #use "topfind" before it issues other directives. That's because topfind is an ocaml script that adds support for that #require directives.
Following the recommends of ygrek I've typed the contents of .ocamlinit in ocaml-top's prompt, and then the evaluation of 'open Core.Std;;' works. It seems, like said Ashish Agarwal, that ocaml-top doesn't read .ocamlinit.
ocaml-top can load/open external libraries, but the initialization of the integrated ocaml's interpreter must be done manually.
Thanks
First, make sure that your .profile or .bashrc contains the following:
eval `opam config env`
Then open the file ~/.opam/system/share/ocaml-top/toplevel_init.ml. This file is read by ocaml-top when it starts. Insert the following lines:
let () =
try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
with Not_found -> ()
;;
#use "topfind"
#thread
#require "core"
#require "core.top"
#require "core.syntax"

Ocaml utop library paths, Core module

I am attempting to use the Core module in utop, as originated by Jane Street and installed using opam.
Here's the problem
utop # open Core.Std;;
Error: Unbound module Core
utop does not seem to have the path to the Core module.
How do you specify a path that can be found by utop to access the Core module? Is there a utop init file that specifies library paths ?
I have the same error message from the OCaml 4.01.0 interpreter.
The only way I can avoid this error is actually changing directory to /Users/myname/.opam/system/lib/core.
I had the same problem, the directions here got it working for me.
https://github.com/realworldocaml/book/wiki/Installation-Instructions#setting-up-and-using-utop
add the following lines to your ~/.ocamlinit file
#use "topfind";;
#thread;;
#camlp4o;;
#require "core.top";;
#require "core.syntax";;
Assuming that you have core properly installed through opam:
# require "core";;
open Core.Std;;
Should work.