OCaml and Opam: unbound module Core
Asked Answered
Q

3

62

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?

Quartet answered 15/7, 2013 at 1:48 Comment(0)
Q
73

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!

Quartet answered 15/7, 2013 at 2:22 Comment(10)
Manually adding lines to config files? Isn't that a bit archaic?Selves
Yes, you're right. I was just a little frustrated at the amount of setup required to get started with Opam, utop and the Core library.Selves
@Selves it is a bit of slog. Not the worst I've had so far, but not super straightforward either.Arboriculture
I have the same problem, but I’ve put those lines in the .ocamlinit, and the error keeps raising. :-(Postpaid
Indeed my problem is quite different: running ocaml, open Core.Std;; works fine, but inside a script, it cannot be compiled – raises Unbound module Core.Postpaid
Let it go… finally I realise what’s wrong: I was trying to compile using ocamlc, when the right way is by using corebuild. Sorry for bothering.Postpaid
Now in the wiki, but still a useful answer.Dialectologist
Requiring to edit a textfile as part of an installation procedure to achieve something that apparently everybody wants seems archaic to me as well. Or is the Core library so exquisite and exceptional? I just installed ocaml and I have no clue what the five lines added to ~/.ocamlinit mean. After just being asked by a script "[2/4] Do you want to update your ~/.ocamlinit?", I wonder why these five lines are not added as part of some installation script.Duran
Just comment as supplement to this answer, here's a video showing how to install OPAM on ubuntu 14.04 and run open Core.Std;; in utop. I tested as the video goes on my env also. I'm using Mac OSX 10.10.5.Sixfold
"core.syntax" is deprecated as of 2022 use "ppx_jane" instead. Follow this (dev.realworldocaml.org/install.html) for more detailsSorrento
F
12

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.

Forfend answered 14/3, 2018 at 23:17 Comment(0)
T
1

If you're building your code with dune and you think you have core installed, you still need to declare your app's dependencies in the dune file.

; bin/dune
 (executable
  (public_name app)
  (name main)
  (libraries app core))
Twelfthtide answered 13/2 at 2:38 Comment(1)
In my case (Ocaml 5.1.1, opam 2.1.5, on Ubuntu 22.04) no need to create file ~/.ocamlinit. Just adding core into bin/dune file as above make dune build and dune utop complete successfully.Escalera

© 2022 - 2024 — McMap. All rights reserved.