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
#
Related
I've successfully installed Batteries and can get it work...somewhat.
Any ideas why I'm getting the syntax error since Opam list this:
depends: "ocaml" {>= "4.00.0" & < "4.10.0"}
And, I'm at: The OCaml toplevel, version 4.07.1
This code relies on camlp4 preprocessor which is deprecated and is no longer supported. Moreover, list comprehension is no longer a part of the Batteries library and is a separate package. So you need to install it using opam install pa_comprehension. You can still make your code work for OCaml 4.07.1, by issuing the following directives right after you start OCaml toplevel (or utop)
#use "topfind";;
#camlp4o;;
#require "pa_comprehension";;
The first directive (note you have to type # it is a part of the directive), enables ocamlfind in the toplevel (I think it is not needed in utop, but won't hirt). The next directive enables camlp4o syntax, so that all inputs are preprocessed. You're no longer coding in OCaml after that, but in camlp4o dialect. Finally, the last directive loads the preprocessor that supports list comprehensions.
To compile the code that uses list comprehension you need to specify the same options to the compiler (i.e., enable syntax, load the preprocessor), e.g.,
ocamlfind ocamlopt -syntax camlp4o -package pa_comprehension -linkpkg example.ml -o example
The camlp4 package also provides another list comprehension syntax, which is a little bit different from the one that is provided by Batteries. It is called camlp4.listcomprehension, and you can use it with roughly the same invocations modulo the package name, e.g., in toplevel,
#use "topfind";;
#camlp4o;;
#require "camlp4.listcomprehension";;
and to compile
ocamlfind ocamlopt -syntax camlp4o -package camlp4.listcomprehension -linkpkg example.ml -o example
With all that said, I highly discourage you from using camlp4 list comprehension in modern days. This is an obsolete technology.
Besides, your example is ill-formed, you're missing the ? character in the closing bracket, e.g., this is an example interaction with the toplevel,
# #use "topfind";;
# #camlp4o;;
# #require "pa_comprehension";;
# open Batteries;;
# [? x | x <- 1--10; x mod 2 = 0 ?];;
- : int Batteries.Enum.t = <abstr>
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.
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
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"
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.