OCaml's utop: importing external modules - ocaml

I'm interested in introducing Angstrom, a parsing combinator library, to a project of mine.
I installed Angstrom to my system with opam as follows:
$> opam install angstrom
I also successfully compiled the project with reference to the module in question using ocamlfind:
$> ocamlbuild -use-ocamlfind -pkgs 'angstrom' project.byte
Surprisingly, I'm having trouble importing Angstrom to the utop repl. Commands like #open Angstrom indicate a value bound to the module name. I have searched for documentation of module imports from the repl, but I haven't found the result that I'm looking for.
What's the best way to reference an external module from utop?

I found an answer to my question. From utop:
utop # #require "angstrom";;
utop # open Angstrom;;
My apologies for perhaps posting prematurely, but hopefully someone will find this answer helpful in the future.

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.

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 batteries installation

There seems to be conflicting information about batteries installation. I have tried several suggestions, but none have worked for me.
I first tried
ocamlfind batteries/ocaml
but that gave this error:
ocamlfind: Cannot find command: /username/godi/lib/ocaml/pkg-lib/batteries/ocaml
I then tried copying the ocamlinit file from the batteries directory to .ocamlinit in my home directory. This gave this error:
Cannot find file topfind.
File ".ocamlinit", line 38, characters 0-20:
Error: Unbound module Toploop
I am using ocaml 4.00.1.
Note: I apologize if this question is redundant with this one ocaml batteries compiling : Unbound module Toploop but the answer given was not explicit enough for me to actually try.
Like #rgrinberg said, try to install batteries with opam. For that, first download the quick installer:
$ wget http://www.ocamlpro.com/pub/opam_installer.sh
Then execute this script:
$ sh ./opam_installer.sh /usr/local/bin
It will install the latest "stable" opam (you can of course change the path /usr/local/bin) and the latest version of the OCaml compiler.
After that, you just need to run:
$ opam install batteries
and it should be ok.
You can also check ocaml.org install section (by package manager) or opam website.

Trying to go through Lwt tutorial, but there seems to be a library problem

I'm running Ocaml 3.12 on Ubuntu installed via Godi.
I'm going through the Lwt tutorial. I've started up the toplevel and done (as instructed):
# #use "topfind";;
# #require "lwt";;
The require of "lwt" seems to be successful (no complaints about not being able to find it).
Then a bit later I try:
# Lwt_io.read_char;;
And the toplevel complains:
# Error: Reference to undefined global `Lwt_io'
When I look in ~/godi-3.12/lib/ocaml/pkg-lib/lwt I see that lwt_io.cmi and lwt_io.mli files are present. godi says I have version 2.2.1 of lwt installed.
I also tried running lwt-toplevel, but could not type anything into it...
Lwt_io module belongs to lwt.unix subpackage. Use it:
#require "lwt.unix";;