Ocaml isn't recognizing List.init in Utop - ocaml

Anytime I use List.init I get the error Error: Unbound value List.init
I think this is because I have Ocaml 4.03.0 and List.init wasn't introduced until 4.06.0. This would lead me to believe that I need to update my ocaml version, but I haven't been able to figure out how to do this. I've tried many opam options, but nothing has worked so far.

With opam you don't upgrade the compiler, but rather install or switch to another compiler installation. That way you can juggle several different installations with separate sets of compatible packages.
You can print a list of available compiler versions by invoking
opam switch list-available
or to reduce it to only the official releases:
opam switch list-available base-compiler
Install a specific version, say 4.06.0, by invoking
opam switch create 4.06.0
eval $(opam env)

List.init is available from the janestreet Core module, it is not available from the standard library. You have to install core ; it is independent of the ocaml compiler.
opam install core
In utop (Welcome to utop version 2.3.0 (using OCaml version 4.03.0)) :
utop # #require "core";;
utop # Core.List.init;;
- : int -> f:(int -> 'a) -> 'a list = <fun>

Related

coq 8.11.0 incompatible with ocaml 4.10?

I am installing coq with opam as per these instructions and have gotten the error message
`No solution for coq: The following dependencies couldn't be met:
- coq → ocaml < 4.10
base of this switch (use '--unlock-base' to force)
I proceeded to switch to ocaml 4.05.0 with the following command
opam switch create with-coq 4.05.0
and could install Coq successfully, but I'd much rather use the updated version of ocaml. Is this an actual incompatibility between Coq and oCaml, or am I doing something wrong?
For added context, I am now using opam 2.0.6, ocaml version 4.05.0, and Coq version 8.11.0. My operating system is macOS. Prior, the only difference was that I tried to use ocaml 4.10.0.
Thank you!
You need the tip of the v8.11 branch or wait for 8.11.1 which should be out shortly.
You don't need to go back to 4.05.0 tho, 4.09 will work fine, tho 4.07.1+flambda is the one I recommend, see https://coq.discourse.group/t/install-notes-on-coq-and-ocaml-versions-configuration/713

OCaml corebuild cannot link to Core.Std

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.

OCaml unable to load "extLib.cma" from toplevel

When I attempt to load the file extLib.cma in utop, OCaml's top level, I get the following error:
Cannot find file extLib.cma
However, if I try to install it using opam I get the following note:
Package extlib is already installed.
What am I doing wrong?
If your purpose is to interactively use some of extLib's functions then
$ utop
μ> #require "extlib";;
μ> ExtString.String.explode "ExtLib";;
- : char list = [E; x; t; L; i; b]
If it's something else, then you may need to specify the exact path to extLib.cma, and something similar to the following should work:
$ utop
μ> #load "/Users/xxx/.opam/4.02.3/lib/extlib/extLib.cma";;
where /Users/xxx is your home directory/folder; 4.02.3 is my current compiler version, set via opam switch (IIRC, it's system by default).

Basic Oasis or Opam file for a simple OCaml project

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

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?