Base won't load in utop

462 views Asked by At

So basically I am trying to follow these instructions https://dev.realworldocaml.org/install.html

I followed them literally. I have a working utop. I have a working VS Code environment. I can run code and everything.

We know the Base package is more efficient than the stdlib one for different reasons. For example:

In stdlib, to check if a member of a list exists, we do

List.mem "a" ["a"];;

in Base (for docs, look here https://ocaml.janestreet.com/ocaml-core/v0.12/doc/base/Base/List/index.html), we do something like this:

open Base
List.mem ["a"] "a" ~equal:String.equal;;

When I do the last in utop, I get the following error: `Error: Unbound module Base'

Now, surely Base exists, when I try to install it via opam install Base it gives Package base is already installed (current version is v0.15.1). but it won't load, what is the problem?

When I do #require "Base";; in utop, it says No such package: Base and when I do #require "base";; it loads, now I have no idea what s going on really.

2

There are 2 answers

5
Chris On

Modules start with capitals, but the library is referenced with a lowercase letter. We can see the same with ocamlfind.

$ ocamlfind query base
/home/wtd/.opam/5.0.0/lib/base
$ ocamlfind query Base
ocamlfind: Package `Base' not found

Or in dune.

(executable 
  (name main) 
  (libraries base ...))

One way of ensuring the Base module is available in utop is to launch utop with: utop -require base.


Now for opinion territory: The Base library is different from Stdlib, but more efficient is a judgment you may be too new to the language to make. The example you presented for List.mem is more flexible in Basr than in Stdlib, because the function for determing equality can be provided as an argument, but for this simple case it makes the call to List.mem longer for no benefit.

Now, maybe the Base user wants to check for equality without case coming into play. They might write:

List.mem ["a"] "A" ~equal:String.Caseless.equal

But using Stdlib I might write:

List.exists (fun s -> String.uppercase_ascii s = "A") ["a"]

That's not awful.

0
v_head On

Easily done: add #use "topfind";; before #require "Base";; then open Base