ocaml command line cannot find “topfind” - ocaml

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).

Related

error when trying to run a file as a script

According to this "Error: unbound module" in OCaml
I should be able to run this
#use "topfind";;
#require "lwt";;
#require "cohttp-lwt-unix";;
as ocaml my_test1.ml
But after I've installed all the libraries and running it as such, I have an error:
$ ocaml my_test.ml
Cannot find file topfind.
Unknown directive `require'.
update
$ ocaml my_test.ml
File "./my_test.ml", line 1:
Error: Reference to undefined global `Mutex'
update2
#use "topfind";;
#require "lwt";;
#require "cohttp-lwt-unix";;
#thread;;
open Cohttp
open Lwt
open Cohttp
open Cohttp_lwt_unix
let () =
Printf.printf ("test1")
;;
and
eval `opam config env`
ocaml test1.ml
same error:
File "./test1.ml", line 1:
Error: Reference to undefined global `Mutex'
For some reason, the interpreter is picking the single-threaded runtime. Probably, there is some problem with your installation or it is a bug in ocamlfind. If you're using system OCaml installation, then my suggestion would be to switch to OPAM's compiler, e.g.,
opam switch 4.05.0
then install the necessary packages, e.g.,
opam install cohttp-lwt-unix
and do not forget to activate your switch with
eval `opam config env`
If the problem still persists, then try to install another version of OCamlFind. If you still have a problem, then submit a bug report. The code you're showing should work (and works on mine machine).
ivg is right in that the interpreter is picking the single-threaded runtime, but that's something you have to fix in your application by adding #thread yourself:
#use "topfind";;
#thread;;
#require "lwt";;
#require "cohttp-lwt-unix";;
This is related to some recent changes in lwt and ocamlfind. You can find some pointers in this bug report I opened recently.

OCaml: Core package not installing

Following the instructions here for Ubuntu: https://github.com/realworldocaml/book/wiki/Installation-Instructions
I've downloaded OCaml and utop which work. However, the Core module cannot be opened.
I've downloaded core, async etc as per the instructions.
My edited .ocamlinit file looks like this:
(* Added by OPAM. *)
let () =
try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
with Not_found -> ()
;;
#use "topfind";;
#thread;;
#camlp4o;;
#require "core.top";;
#require "core.syntax";;
When I open utop I get the message that:
No such package: core.top
No such package: core.syntax
If I try to open core by entering "open Core.Std;;" utop returns: "Error: Unbound module Core". I'm assuming that the installation instruction are out of date but I haven't been able to find any alternate instructions that fix the issue. Is there something wrong with my .ocamlinit file?
I've downloaded core, async etc as per the instructions.
You need to install them, not only download, make sure, that you did:
opam install core
If it still doesn't work, then make sure, that you activated your opam environment, with
eval `opam config env`
Notice, the backticks (they are not single quotes). The command should not print anything.

Telling ocamlbuild to use Core

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?

Error: Unbound module Core when attempting to open Core module with utop from command line

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.

OCaml and Opam: unbound module Core

I'm trying to get an OCaml environment set up, and I've followed the instructions from appendix A of the Real World OCaml beta. I set up opam, and installed a version of OCaml with the command
$ opam switch 4.01.0dev+trunk
which passed fine. I then did an
$ eval `opam config env`
to pull in the changes. I'm running the correct top level, as
$ which ocaml
outputs
/home/bryan/.opam/4.01.0dev+trunk/bin/ocaml
I installed the Core package from Jane street, with the command
$ opam install core
Both ocamlfind and opam search show that the package was installed correctly. However when I try to open it either from the repl or in a file, I get the error 'unbound module Core'. e.g.
$ ocaml
# open Core;;
Error: Unbound module Core
Is there something I'm missing here? Why can't OCaml find my installed module?
So I jumped the gun a bit. I forgot to add some items to my ~/.ocamlinit file. Specifically I forgot to add
#use "topfind"
#camlp4o
#thread
#require "core.top"
#require "core.syntax"
as mentioned in Chapter 1. D'oh!
Please follow the steps in the Real World OCaml Wiki - Installation Instructions.
Under Setting up and using utop, the instructions state that you should add:
#use "topfind";;
#thread;;
#camlp4o;;
#require "core.top";;
#require "core.syntax";;
to your ~/.ocamlinit file.