Ocaml topfind lwt - ocaml

I am trying to use lwt module with Ocaml
%cat .ocamlinit
#use "topfind";;
#require "lwt.simple-top";;
#require "lwt.syntax";;
#require "findlib";;
#require "num.core";;
#load "nums.cma";;
#load "unix.cma";;
#camlp4o;;
open Lwt;;
open Lwt_io;;
When I try to use any Lwt API using this command to build
"ocamlbuild -use-ocamlfind myFile.native"
I receive this error:
"Error: Unbound module Lwt"
When I type in terminal %ocaml it loads successfully and i can use Lwt.API's
What is going on? any suggestion?

ocamlbuild doesn't read .ocamlinit, it is purely for toplevel (i.e. ocaml).
In order to compile lwt-dependent project you will need to specify dependencies, e.g. :
ocamlbuild -use-ocamlfind -package lwt myFile.native
See also How to compile ocaml to native code

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.

Standard .ocamlinit configuration

I thought I set this up correctly, like explained on realworldocaml, but when I try to do
open Core;;
I get
Unbound module Core
I think this is related to .ocamlinit, but I don't know what else should I add / remove from it.
#use "topfind";;
#thread;;
#camlp4o;;
#require "core.top";;
#require "core.syntax";;
#require "ppx_jane";;
(* Added by OPAM. *)
let () =
try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
with Not_found -> ()
;;
I don't really understand if that try should be the first thing in the file (but I tried both version and I have the same result).
What am I missing here?
I looked over this question, but my situation isn't like that (I don't get all those errors, only the Unbound module one).
You ocamlinit is fine, so you either didn't install the core library at all, or didn't activate opam, if you're using it. Make sure, that you did the following:
eval $(opam config env)
opam install core
coretop
and then in the REPL, you can do
open Core.Std;;

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.