When I attempt to load the file extLib.cma in utop, OCaml's top level, I get the following error:
Cannot find file extLib.cma
However, if I try to install it using opam I get the following note:
Package extlib is already installed.
What am I doing wrong?
If your purpose is to interactively use some of extLib's functions then
$ utop
μ> #require "extlib";;
μ> ExtString.String.explode "ExtLib";;
- : char list = [E; x; t; L; i; b]
If it's something else, then you may need to specify the exact path to extLib.cma, and something similar to the following should work:
$ utop
μ> #load "/Users/xxx/.opam/4.02.3/lib/extlib/extLib.cma";;
where /Users/xxx is your home directory/folder; 4.02.3 is my current compiler version, set via opam switch (IIRC, it's system by default).
Related
When I enter ocaml --version it returns
/usr/bin/ocaml: unknown option '--version'.
I installed it using OPAM and ran the instructions
opam init
eval $(opam env)
eval opam env
opam switch create 4.07.0
and everything seemed to go fine. Entering which ocaml returns a correct-looking path. And when entering a terminal session it seems to work fine. I don't see this error mentioned anywhere when I search for it.
OCaml tools are weird and usually do not follow the POSIX guidelines of using double dashes for command line options. At this point probably for historic reasons and because the standard Arg module parses command line arguments this way. In any case, this will do the trick:
ocaml -version
I've installed opam, run opam init, run opam switch 4.06.0 which created a 4.06.0 directory inside ~/.opam, run "eval opam confing env" which exports $OCAML_TOPLEVEL_PATH as ~/.opam/4.06.0/lib/toplevel amongst other things, when launching ocaml I get the dreaded:
$ ocaml
OCaml version 4.06.0
Cannot find file topfind.
Unknown directive `camlp4o'.
#
I've looked at this and this neither of which address my issue and I'm at my wits' end (first time setting up OCaml). This is my ~/.ocamlinit:
(* Added by OPAM. *)
let () =
try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
with Not_found -> ()
;;
#use "topfind"
#camlp4o
#thread
#require "core.top"
#require "core.syntax"
EDIT: Looks like I hadn't installed core, installing core resolved that but now amongst the slew of import diagnostics I get:
Exception:
Invalid_argument
"The ocamltoplevel.cma library from compiler-libs cannot be loaded inside the OCaml toplevel".
And then a bit further down:
Raised at file "pervasives.ml", line 33, characters 25-45
Called from file "toplevel/toploop.ml", line 468, characters 4-128
Called from file "toplevel/topdirs.ml", line 144, characters 10-51
Camlp4 Parsing version 4.06.0
You should run
eval `opam config env`
Note the backticks. They are usually located to the left of the key 1 on most modern keyboards. The command should not output anything, if you see any output it means that you're running it incorrectly. You have to run this command to activate the opam installation every time you start a new shell (unless you've put this command in your shell initialization scripts, like .bashrc)
If the problem persists, then make sure, that you have installed the ocamlfind package,
opam install ocamlfind
What seemed to work for me:
make sure core is installed (opam install core)
make sure camlp4 is installed (opam install camlp4)
Insert Topfind.don't_load ["compiler-libs.toplevel"];; in-between #use "topfind";; and #require "core.top";;, as per this. It is an issue that doesn't appear to be fixed in the latest version of core (0.9.2).
From this SO question: List Comprehension in Ocaml?, I could install the comprehension package with opam install pa_comprehension, and use the package in toplevel REPL.
# #require "pa_comprehension";;
# open Batteries;;
# [? 2 * x | x <- 0 -- max_int ; x * x > 3 ?];;
- : int Batteries.Enum.t = <abstr>
Then, how can I compile the code?
Unfortunately, since pa_comprehension package name is not ended with .syntax extension, this is a little bit harder than it should be. (The fact that this issue is still not fixed, shows that using pa_comprehension is not very popular in modern OCaml). So, without the proper extension, we need to do everything manually. If your file is named pr.ml, then the correct invocation is:
$ ocamlbuild -use-ocamlfind -syntax camlp4o -pkg camlp4,pa_comprehension pr.native
If you don't wan't to type it every time, then you can create _tags file, with the following contents:
$ cat _tags
<**/*> : package(camlp4),syntax(camlp4o),package(pa_comprehension)
In that case, the command line spell is a little bit easier:
$ ocamlbuild -use-ocamlfind pr.native
In my project I have a file that uses Core.Std stuff, so I have run
opam install core
and added
open Core.Std
in my file.
When I run
ocamlbuild myprogram.native
it says:
Error: Unbound module Core
pointing to line with the open statement above.
So, I try this:
ocamlbuild -use-ocamlfind -pkgs core.std myprogram.native
and get the following message:
ocamlfind: Package `core.std' not found
So I thought that maybe I needed to run opam install core.std as well, but apparently there is no such thing according to opam. I also tried "open Core.Std;;" in the ocaml repl, but that did not work either. Any ideas?
You can either use corebuild which is usually shipped with this library or, you can try this:
ocamlbuild -use-ocamlfind -pkg core
P.S. use ocamlfind list command to view the list of available packages.
P.P.S. In addition to corebuild they usually ship coretop, a script that allows you to run core-enabled top-level. It uses utop underneath the hood, so make sure that you have installed it with opam install utop (if you're using opam), before your experiments.
Remove .std from your ocamlbuild cmd?
I am trying to follow along with Real World OCaml by setting up opam, utop, and the Core modules as well. I've followed these directions with some success. In particular, when I run utop without specifying the file to load, I can load Core with no issues:
[dswain#Dupree scratch]$ utop
Welcome to utop version 1.12 (using OCaml version 4.01.0)!
Findlib has been successfully loaded. Additional directives:
...
utop # open Core;;
utop #
So far so good. Now if I attempt to load a script from the command line with the same code:
[dswain#Dupree scratch]$ cat test.ml
open Core.Std;;
[dswain#Dupree scratch]$ utop test.ml
File "test.ml", line 1, characters 0-13:
Error: Unbound module Core
[dswain#Dupree scratch]$
I assume this is a configuration mistake I've made somewhere, but I'm not quite sure where.
Setup details
I've attempted to reinstall ocaml, opam, utop, and core to no avail. I've got the following config changes that I'm aware that opam init made during setup:
~/.ocamlinit:
#use "topfind";;
#thread;;
#camlp4o;;
#require "core.top";;
#require "core.syntax";;
~/.bash_profile
# OPAM configuration
. /home/dswain/.opam/opam-init/init.sh > /dev/null 2> /dev/null || true
eval `opam config env`
I'm running Arch Linux, and followed the steps to install opam via the AUR with no issues I'm aware of. I was also able to build & install all of the packages from the installation instructions without any errors I know of.
This is because utop executes a script-file before it runs .ocamlinit file. I'm not sure whether it is a bug or a feature, but that is how the code is written. Indeed, most users runs utop from emacs and send pieces of code to utop with C-c C-s.
If you're not comfortable with emacs, I can suggest to use the following workflow:
start utop
do some coding in your favorite editor
load code in utop with #use "your_file.ml";;
play with it, if not satisfied goto 2.