Ocaml utop library paths, Core module - ocaml

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.

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: How to Run a Script with Libraries Included

I'm following the Real World OCaml book to learn OCaml and many of the programs require using the Jane Street Core library. When I use functions from this core library in the toplevel, it works great. There, I just use the following commands to open the Core library.
$ #use "topfind";;
#thread;;
#camlp4o;;
#require "core.top";;
#require "core.syntax";;
open Core.Std;;
Then I can run this program line by line in the toplevel and functions like String.split work fine.
# let languages = "OCaml,Perl,C++,C";;
val languages : string = "OCaml,Perl,C++,C"
# let dashed_languages =
let language_list = String.split languages ~on:',' in
String.concat ~sep:"-" language_list
;;
val dashed_languages : string = "OCaml-Perl-C++-C"
But if I put it all in a script, how do I make the compiler recognize the core libraries? So if I'm running the same program in a script:
open Core.Std;;
open Str;;
let languages = "OCaml,Perl,C++,C";;
let dashed_languages =
let language_list = String.split languages ~on:',' in
String.concat ~sep:"-" language_list
;;
Then I compile it:
ocamlopt -o test test.ml
I get an error:
Error: Unbound module Core
So clearly Core is not being recognized. I can't put the #use and #require commands in my script because OCaml doesn't recognize those. So how can I use Core?
Since you're following Real World OCaml, you may want to use corebuild to compile executables since this is what the book recommends. In this case the command corebuild test.native should correctly compile your script. If you use libraries other than Core, you'll need to specify them with the -pkg option.
Also, note you can omit the double semicolons within a .ml file.
Make sure you have ocamlfind. Also, make sure you have the right environment for it to find packages. If you installed with opam, run "eval opam config env". Then:
ocamlfind ocamlopt -linkpkg -thread -package core test.ml -o test