I am a new OCaml user. I have asked a question to learn how to set up a basic OCaml project, but I still have issues. Jump to the end for a TL;DR
For instance, I am trying to learn Irmin. On the home page of Irmin I see I have to do
opam install irmin git cohttp
Doing so, I end up with Irmin version 0.8.3. Unfortunately, I cannot follow their examples, because apparently Irmin is at version 0.9.4, and it looks like the API has changed. So, I would like to start a clean project having as dependency just Irmin 0.9.4
First, I have set up an opam switch with
opam switch install playground -A 4.02.1
to be sure to work on a clean slate. Then, I have created a basic _oasis file that looks like this
OASISFormat: 0.4
Name: Playground
Version: 0.1.0
Synopsis: OCaml playground
Authors: Andrea Ferretti
License: Apache-2.0
BuildTools: ocamlbuild
BuildDepends:
irmin,
irmin.unix,
lwt,
lwt.unix
and then I have copied the example from the Irmin homepage in example.ml.
open Lwt
open Irmin_unix
let store = Irmin.basic (module Irmin_git.FS) (module Irmin.Contents.String)
let config = Irmin_git.config ~root:"/tmp/irmin/test" ~bare:true ()
let prog =
Irmin.create store config task >>= fun t ->
Irmin.update (t "Updating foo/bar") ["foo"; "bar"] "hi!" >>= fun () ->
Irmin.read_exn (t "Reading foo/bar") ["foo"; "bar"] >>= fun x ->
Printf.printf "Read: %s\n%!" x;
return_unit
let () = Lwt_main.run prog
If I do oasis setup and then ocamlbuild example.ml I get a bunch of errors
W: Cannot get variable ext_obj
W: Cannot get variable ext_lib
W: Cannot get variable ext_dll
W: Cannot get variable ocamlfind
So in short my question is:
What is the simplest way to set up a clean project that depends on Irmin 0.9.4, being sure that it is self-contained (it will not rely on preinstalled libraries other than those installed by the build process)?
So, first of all your don't have any active targets in your _oasis file. What I mean, is that OASIS is about building exectuables and libraries. You haven't described either. That means, that your BuildDepends doesn't have any effect, since it has only sense for Library and Executable entries. So, a first approximation would be the following:
OASISFormat: 0.4
Name: Playground
Version: 0.1.0
Synopsis: OCaml playground
Authors: Andrea Ferretti
License: Apache-2.0
BuildTools: ocamlbuild
BuildDepends: irmin.unix, lwt.unix
Executable "example"
Path: .
MainIs: example.ml
CompiledObject: best
Doing so, I end up with Irmin version 0.8.3. Unfortunately, I cannot
follow their examples, because apparently Irmin is at version 0.9.4,
and it looks like the API has changed. So, I would like to start a
clean project having as dependency just Irmin 0.9.4
If you got a version that is not the newest, then you can try to persuade opam constraint solver, that you really need that specific version, like:
opam install irmin.0.9.4 git cohttp
Also, opam internal constraint solver isn't very sophisticated, so make sure that you have aspcud installed on your system. The following will install the latest opam with aspcud on a fresh ubuntu installation:
sudo add-apt-repository --yes ppa:avsm/ppa
sudo apt-get update
sudo apt-get --yes install opam
What is the simplest way to set up a clean project that depends on Irmin 0.9.4, being sure that it is self-contained (it will not rely on
preinstalled libraries other than those installed by the build
process)?
Well, simplicity is a matter of personal opinion. For example, you can do the same without any oasis at all, just using opam file, where you set your build field, just to ["ocamlbuild"; "-use-ocamlfind"; "-pkgs irmin.unix,lwt.unix"; "example.native"], but how well it will scale...
Related
I'm having trouble linking a very simple OCaml program:
open Core
Format.printf "hello world %s\n" "foobar";;
Format.printf "argv= %s\n" (Sys.get_argv()).(0) ;;
which I compile with
ocamlfind ocamlc -thread -package core visitor.ml
The compile step always generates the error:
Error: Required module `Core__Core_sys' is unavailable
I've pinned version 4.0.9, and I can see the file:
$ ocamlfind query core
/home/ubuntu/.opam/4.09.0/lib/core
and $ ls -la /home/ubuntu/.opam/4.09.0/lib/core shows
-rw-r--r-- 1 ubuntu ubuntu 17891 Dec 3 20:14 core__Core_sys.cmi
-rw-r--r-- 1 ubuntu ubuntu 93777 Dec 3 20:14 core__Core_sys.cmt
-rw-r--r-- 1 ubuntu ubuntu 75659 Dec 3 20:14 core__Core_sys.cmti
-rw-r--r-- 1 ubuntu ubuntu 16958 Dec 3 20:14 core__Core_sys.cmx
I've tried everything I can think of, with no luck. BTW, I notice that the documentation https://ocaml.org/api/Sys.html makes no mention at all of get_argv but if I try just plain Sys.argv I get a warning:
# Sys.argv ;;
Alert deprecated: Core.Sys.argv
[since 2019-08] Use [Sys.get_argv] instead, which has the correct behavior when [caml_sys_modify_argv] is called.
So I conclude that the core OCaml documentation published at ocaml.org is more than two years out of date! How can one obtain up-to-date documentation, ideally documentation that describes these kinds of newbie errors?
A few points. First, it looks like you are on a fairly old version of OCaml. Unless you need to stay on 4.09 for some reason, I highly recommend upgrading to the latest version, 4.13.1. Instructions for installing are here: https://ocaml.org/learn/tutorials/up_and_running.html
In case you have any trouble with that, try upgrading to the latest version of opam (the OCaml package manager) and doing opam update to download the most up-to-date package index.
Second, it looks like you are trying to use Jane Street's Core library, which is a third-party package which is intended as a standard library replacement. As such, it has its own version of the OCaml standard library's Sys module, i.e. Core.Sys. Regarding the alert that you are getting, the Core.Sys.argv value is actually deprecated by Jane Street Core: https://ocaml.janestreet.com/ocaml-core/latest/doc/core/Core__/Core_sys/index.html#val-argv
A single result from get_argv (). This value is indefinitely deprecated. It is kept for compatibility...
This leads us to the final issue, when you try to compile it is unable to find the core package. There are a couple of choices here. First, one option is that the core package and standard library replacement are actually optional; you may not actually need them. If you're an OCaml beginner you could try sticking with the standard library only (so no open Core, no trying to compile with the core package).
Another option, if you decide to keep using the core package, is to the dune build system instead of ocamlfind. Dune is a powerful, modern OCaml build system that handles almost all aspects of package linking during the build, so you don't need to worry about issuing individual compile commands.
Here's what a dune file would look like:
(executable
(name visitor)
(libraries core))
And the visitor.ml file would be in the same directory:
let () =
Printf.printf "hello world %s\n" "foobar";
Printf.printf "argv= %s\n" Sys.argv.(0)
Then you would run:
dune exec ./visitor.exe
The .exe is a dune convention, executables across operating systems are given this extension.
Finally, in source code you never actually need ;;. More about that here: https://discuss.ocaml.org/t/terminate-a-line-with-or-or-in-or-nothing/8941/21?u=yawaramin
Note on documentation being out of date: it would help if you could point us to where you got the instructions that led you to this point of confusion. There is a lot of effort to clean up install instructions and modernize documentation, but unfortunately there are a lot of outdated getting started guides out there. The 'Up and Running' link I provided at the top of this answer is the best resource.
You need to link the package by adding the -linkpkg flag:
ocamlfind ocamlc -thread -package core -linkpkg visitor.ml
My problem is similar to this, however, in my case .ocamlinit is set.
Here is my ocaml version.
mymac:Desktop myusr$ ocaml --version
The OCaml toplevel, version 4.08.1
Here is my opam version.
mymac:Desktop myusr$ opam --version
2.0.5
Here is my opam switch.
mymac:Desktop myusr$ opam switch
# switch compiler description
→ 4.08.1 ocaml-base-compiler.4.08.1 4.08.1
default ocaml-base-compiler.4.08.1 default
Here's my .ocamlinit:
mymac:Desktop myusr$ cat ~/.ocamlinit
(* ## added by OPAM user-setup for ocamltop / base ## 3ec62baf6f9c219ae06d9814069da862 ## you can edit, but keep this line *)
#use "topfind";;
(* ## end of OPAM user-setup addition for ocamltop / base ## keep this line *)
#thread;;
#require "core.top";;
#require "core.syntax";;
Here is the evidence that I already have core installed.
mymac:Desktop myusr$ opam install core utop
[NOTE] Package utop is already installed (current version is 2.4.1).
[NOTE] Package core is already installed (current version is v0.12.3).
Here is the sum.ml file from Real World OCaml:
open Core.Std
let rev read_and_accumulate accum =
let line = In_channel.input_line In_channel.stdin in
match line with
| None -> accum
| Some x -> read_and_accumulate (accum +. Float.of_string x)
let () =
printf "Total: %F\n" (read_and_accumulate 0.)
Here's what happens when I try to build it with corebuild:
mymac:Desktop myusr$ corebuild sum.native
+ ocamlfind ocamlc -c -w A-4-33-40-41-42-43-34-44 -strict-sequence -g -bin-annot -short-paths -thread -package core -ppx 'ppx-jane -as-ppx' -o sum.cmo sum.ml
File "sum.ml", line 1, characters 5-13:
1 | open Core.Std
^^^^^^^^
Error: Unbound module Core.Std
Command exited with code 2.
Hint: Recursive traversal of subdirectories was not enabled for this build,
as the working directory does not look like an ocamlbuild project (no
'_tags' or 'myocamlbuild.ml' file). If you have modules in subdirectories,
you should add the option "-r" or create an empty '_tags' file.
To enable recursive traversal for some subdirectories only, you can use the
following '_tags' file:
true: -traverse
<dir1> or <dir2>: traverse
Compilation unsuccessful after building 2 targets (1 cached) in 00:00:00.
Why isn't corebuild linking to the core library? How can I fix this?
The build script loads everything correctly. The module that you're trying to load no longer exists. You're trying to use an old version of the Real World OCaml book together with a very new version of OCaml and Core. The Janestreet Core library has changed a lot since those times. You should either switch to a newer book or downgrade to an older version of OCaml and Core library.
Using modern Core
Since the admission of Dune and module aliases, it is no longer needed to have an extra Std submodule, therefore Janestreet removed it (after a two-year-long deprecation). Therefore, now we're writing
open Core
instead of
open Core.Std (* no longer works *)
The same is true with Core_kernel et alas.
Since OCaml and Janesteet have moved since that time a lot, the newer version of RWO was created with updated examples. It is still a work in progress but looks quite close to be ready. So you can switch to it.
Sticking to older versions of OCaml
If you would like to use the first version of Real World OCaml, then you have to choose a version of OCaml and Janesteet's Core library which are known to be compatible. I failed to find any authoritative recommendations on which version it is better to use with the old book. So I would suggest using OCaml 4.02.3. Then you can install core as usual with opam install core (it should install version 113.33.03), and as far as I remember, it should work with the old book. If you or anyone else is having problems with this version please tell me in the comments section, and I will update this recommendation.
How can I install a specific version of ocaml compiler (and compatible packages) using opam (or another package manger)?
I took a quick look through the opam documentation, but I don't find a relevant information.
I need ocaml compiler (preferably the native code compiler) to build unison, a software for file synchronization. I need to build unison on two machines using the same version of ocaml, or otherwise unison emits an error and aborts its duty (yiiii!).
I tried building ocaml version 4.04.0 from a tar ball and then using it for building unison, but on one of the machine the build of unison failed with the error message,
make[1]: Entering directory '/home/norio/Downloads/unison/unison-2.48.4_expand/src'
ocamlc -o mkProjectInfo unix.cma str.cma mkProjectInfo.ml
File "mkProjectInfo.ml", line 1:
Error: Error while linking /home/norio/Downloads/unison/ocaml_for_unison/lib/ocaml/unix.cma(Unix):
The external function `unix_has_symlink' is not available
if [ -f `which etags` ]; then \
etags *.mli */*.mli *.ml */*.ml */*.m *.c */*.c *.txt \
; fi
make[1]: Leaving directory '/home/norio/Downloads/unison/unison-2.48.4_expand/src'
I don't want to set off for the quest of unix_has_symlink function and devote myself for the exploration of the swamp of library dependencies where many developers had fallen before the civilization came and package managers were invented.
Is there anything like,
opam install ocamlc-4.04 and opam install all-packages?
Addendum
The error message about unix_has_symlink was observed on a machine running Linux Mint 18 Cinnamon 64 bit. Is this function a part of some unix/linux library, rather than ocaml package?
To create a switch with a particular version of the compiler do
opam switch create <compiler-version>
(Note: for the old opam 1.x it was opam switch <compiler-version>)
E.g.,
opam switch create 4.07.0
Or, if you want to create a fresh new switch that uses the same compiler as some other switch, then the syntax is
opam switch create <name> <compiler-version>
E.g.,
opam switch create myproj 4.07.0
Note, that if <name> is a folder, then a local switch will be created, e.g., opam switch ./myproj 4.07.0 will create a switch directly in the myproj folder.
To start with a specific version, i.e., when you first install opam, just do
opam init --compiler=<version>
E.g.,
opam init --compiler=4.07.0
To list available versions do
opam switch
To see even more, do
opam switch list-available
To install a variant of a compiler, e.g., a compiler with flambda or spacetime, use the following general syntax,
opam switch create <switch-name> ocaml-variants.<version>+options <options>...
E.g.,
opam switch create myswitch ocaml-variants.4.13.0+options ocaml-option-flambda
use opam search ocaml-options for the full list of available options. It is possible to specify several options, e.g.,
opam switch create myswitch ocaml-variants.4.13.0+options ocaml-option-flambda ocaml-option-spacetime ocaml-option-static
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?
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.