Compiling program that uses Lwt_term - ocaml

I have the following code in OCaml:
open Lwt
open Lwt_term
let () = Lwt_main.run (
lwt l = Lwt_read_line.read_line ~prompt:[text "foo> "] ())
when I try to compile using
ocamlfind ocamlc -package lwt cli.ml - o cli.byte
it I get the following error:
File "cli.ml", line 2, characters 0-13:
Error: Unbound module Lwt_term
I know it's unrelated to the problem above, but from utop I can open Lwt but I still can't open Lwt_term. What am I doing wrong?

Source file is located there so I bet that you need lwt.text package

Not really a real answer to the original question, but I have ended up using Core to accomplish it. The enlightenment came from finding the readline_test.ml file in a tests folder in the Core library (https://github.com/janestreet/core_extended/blob/master/lib_test/readline_test.ml).
Following that example it was really easy to implement readline-like functionality.

Related

In OCaml 4.03.0 FFI fails to compile with "No implementations provided" Error

I upgraded my ocaml to 4.03.0.
Then, some wrapper libraries failed to build raising "No implemntations provided" Error.
I prepare a small example to explain my situation.
I write a C code in hello_stubs.c
#include<stdio.h>
#include<caml/mlvalues.h>
CAMLprim value caml_print_hello(value unit)
{
printf("Hello\n");
return Val_unit;
}
Next, I prepare the interface file for ocaml, in hello.mli.
external print_hello : unit -> unit = "caml_print_hello"
Then, I code a main program in main.ml
Hello.print_hello();;
To compile these programs, I executed the following commands.
ocamlc -c hello.mli
ocamlc -c hello_stubs.c
ocamlopt -o main main.ml hello_stubs.o
Then, unfortunately, the last command failed with the following error message.
File "_none_", line 1:
Warning 58: no cmx file was found in path for module Hello, and its interface was not compiled with -opaque
File "main.ml", line 1:
Error: No implementations provided for the following modules:
Hello referenced from main.cmx
According to the message,
I've tried ocamlc -opaque hello.mli, but it didn't solve the problem.
I also confirmed that the commands above work fine for ocaml 4.02.3.
Do you know how to compile this example with ocaml 4.03.0?
The fix is easy: create hello.ml of the same contents of hello.mli and compile it and link for main.
I guess this is due to the following change of 4.03.0:
PR#4166, PR#6956: force linking when calling external C primitives
(Jacques Garrigue, reports by Markus Mottl and Christophe Troestler)
The related section of the reference manual should be updated. See http://caml.inria.fr/mantis/view.php?id=7371

ocaml doesn't load .ocamlinit in script mode

I find that when I run ocaml in terminal, ie, in interactive mode, it will load .ocamlinit. However, when I run ocaml test.ml, ie, in script mode, it doesn't load .ocamlinit file. This actually causes some trouble for me, since I have the following setup in my .ocamlinit:
let () =
try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
with Not_found -> ()
;;
#use "topfind";;
#thread;;
#camlp4o;;
#require "core.top";;
#require "core.syntax";;
So when I run in interactive mode, #use "topfind" will be executed and I can open other libraries in my code. But as .ocamlinit is not loaded in script mode, when I run ocaml test.ml in terminal, with test.ml like this:
open Core.Std;;
let () = print_endline "hello world"
It will fail with error: "Error: Unbound module Core".
My question is:
How to fix this so that when running ocaml in script mode, the .ocamlinit file will also be loaded?
Why the ocaml toplevel system doesn't load .ocamlinit file in script mode?
ps. The OCaml version is 4.01.0
I think that's a "bug" (or at least unexpected behaviour) in the toplevel. This could be fixed in future versions of OCaml. I don't have a satisfying workaround to propose, besides ocaml -noprompt < test.ml (which is not equivalent as you will get some noisy output from the toplevel instead of just your file executed), or of course compiling the program and running it (which may be as simple as ocamlbuild test.byte && ./test.byte).

How to use functions in Ocaml Batteries?

I would like to use functions of module List of Ocaml Batteries. I have installed Batteries, and write code as follows:
open Extlib
...
Extlib.ExtList.List.remove ...
While compiling, it gives me an error Error: Unbound module Extlib. Does anyone know what happens?
See the documentation, particularly the getting started section, here:
https://github.com/ocaml-batteries-team/batteries-included/wiki/
While Batteries uses some code from Extlib, they are separate projects.

Error: Unbound value Ppl.manager_alloc_strict

I just installed a package called Ppl, here is the result of find -name "*ppl.*" under /usr/:
./lib/libppl.so.7.1.0
./lib/libppl.so.7
./lib/libcloog-ppl.so.0
./local/share/man/man3/libppl.3
./local/share/aclocal/ppl.m4
./local/include/ppl.hh
./local/lib/libppl.so
./local/lib/libppl.la
./local/lib/libppl.a
./local/lib/libppl.so.9
./local/lib/libppl.so.9.0.0
But when I run the following code by ocamlc -I /usr/local/lib/ocaml/3.11.2/apron -I /usr/local/lib/ocaml/3.11.2/gmp/ -I /usr/local/lib/ -c file.ml, I got an error Unbound value Ppl.manager_alloc_strict.
open Apron;;
open Mpqf;;
open Format;;
let print_array = Abstract0.print_array;;
let lincons1_array_print fmt x =
Lincons1.array_print fmt x
;;
let generator1_array_print fmt x =
Generator1.array_print fmt x
;;
let manpk = Polka.manager_alloc_strict();;
let manbox = Box.manager_alloc ();;
let manoct = Oct.manager_alloc ();;
let manppl = Ppl.manager_alloc_strict();;
...
Does anyone know what happened? Thank you very much!
You've showed us the library, but not anything regarding the OCaml interface to the library -- cmx or cmxa for native compilation. Just as the comment I left prior, do the same thing with where Ppl package for OCaml is. OCaml is not interfacing with the C library directly (.a, .so), but through a compiled interface (cmxa or cmi files).
You might also consider using the ocamlbuild system. You can tag modules with external dependencies, and have the build system find the package (via ocamlfind, or hard-coded).

Regular Expressions in OCaml

I want to use regexps in OCaml and it seems that Str module provides these functionalities.
So I tried with a simple program:
open Str
let regx = regexp "."
but it gives me the following error
File "lol.ml", line 1, characters 0-1:
Error: Error while linking lol.cmo:
Reference to undefined global `Str'
As if module is not present but if I remove open Str it says that regexp is an unbound value.
I don't get what kind of issue it is, Str should be a standard module (according to http://caml.inria.fr/pub/docs/old-311/libref/Str.html) so I'm clueless.. the only think I thought is that signature (mli) is present but implementation (ml) is not.
I'm running Objective Caml version 3.11.0 according to ocaml tool.
Can anyone help me figuring this out?
Thanks in advance
From the manual:
Programs that use the str library must be linked as follows:
ocamlc other options str.cma other files
ocamlopt other options str.cmxa other files
Or you can put
#load "str.cma";;
if you are doing it in the interpreter
As an alternative to the Str module there's also Re2.
Install it using opam install re2
Use the module in your_file.ml like this:
open Re2.Std
open Re2.Infix
let change input_text = Re2.rewrite_exn ~/"change this" "to that" input_text
let () = printf "%s" (change "change this")
Compile with ocamlbuild -use-ocamlfind -package re2 -package core -tag thread your_file.byte