Core and Base Unbound Modules

I recently got the book Real World Ocaml and I cannot even get past the installation. I followed the instructions on ocaml.org as they laid out. I ran each command in sequence, I probably have reinstalled it this way 4 times by now. I can compile ocaml but I absolutely cannot get core or base to work. I am running windows 11, and my opam list shows core, base, and utop.
I try to use # require β€œbase”;; does not work.
I try to create a .ocamlinit file, does not work.
I use dune build with core and base both in the dune file
No matter what I do, no matter how many reinstall these modules do not work. I am so lost.

Sorry you are having a rough onboarding experience! We will help, and if there’s anything we can take away to improve the onboarding after helping , we’ll do so!

Could you please post the textual output you get after these commands, and gives details (e.g., about where you have created the .ocamlinit file)?

Also, could you confirm you followed these installation instructions for windows ? Install OCaml

Hi, thank you!

  • I did install ocaml using that webpage’s instructions and have been coding and able to run ocaml without packages just fine. However, now that I’m trying to get more advanced I am trying to get these packages and that has been my roadblock

  • The .ocamlinit file is in my C:\Users\User_Name directory

  • dune build will output :
    File β€œbin/main.ml”, line 1, characters 0-9:
    1 | open Base;;
    ^^^^^^^^^
    Error (warning 33 [unused-open]): unused open Base.
    File β€œtest/test_test.ml”, line 1, characters 0-9:
    1 | open Base;;
    ^^^^^^^^^
    Error (warning 33 [unused-open]): unused open Base.
    PS C:\Users\Dominic\Desktop\Summer Learning\OCaml\test> dune runtest
    File β€œtest/test_test.ml”, line 1, characters 0-9:
    1 | open Base;;
    ^^^^^^^^^
    Error (warning 33 [unused-open]): unused open Base.

  • trying to run my code with utop after doing # #require β€œbase”; using shift enter exits here:
    utop # open Base;;
    Error: Unbound module Base
    ─( 13:14:52 )─< command 12 >────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
    utop # open Core;;
    Error: Unbound module Core
    ─( 14:27:25 )─< command 13 >────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─utop #

1 Like

Note: please use triple-backticks to surround code samples to format them correctly. Eg

utop # open Base;;
  • The .ocamlinit file is in my C:\Users\User_Name directory

I am fairly sure this is not correct. It should be something like %USERPROFILE%\AppData. But I’m unable to find any documentation on this. I recommend experimenting a bit to see what works.

Error (warning 33 [unused-open]): unused open Base.

This is correct. You opened the module but didn’t use it in the file so far, hence the warning. The warning goes away once you start using the stuff defined in Base.

utop # open Base;;
Error: Unbound module Base

This is also correct. You didn’t β€˜require’ the library before trying to open its module. Try:

$ utop -require base
utop # open Base;;;

EDIT: related issue? utop/init.ml does not respect XDG on Windows Β· Issue #451 Β· ocaml-community/utop Β· GitHub

1 Like
PS C:\Users\User_Name\Desktop\test> (& opam env) -split '\r?\n' | ForEach-Object { Invoke-Expression $_ }
PS C:\Users\User_Name\Desktop\test> utop -require base
─( 15:33:57 )─< command 0 >────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # open Base;;
─( 15:34:11 )─< command 1 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─utop #         
( 15:34:23 )─< command 0 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # open Base;;
Error: Unbound module Base
─( 15:34:23 )─< command 1 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # open Core;;
Error: Unbound module Core
─( 15:34:24 )─< command 2 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop #
3+4 ;;
- : int = 7
─( 15:34:24 )─< command 3 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # 
8 / 3 ;; 
- : int = 2
─( 15:34:24 )─< command 4 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop #
3.5 +. 6. ;;
- : float = 9.5
─( 15:34:24 )─< command 5 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop #
3*5>14 ;;
- : bool = true
─( 15:34:24 )─< command 6 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop #
let square x = x*x ;;
val square : int -> int = <fun>
─( 15:34:24 )─< command 7 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop #
square 2 ;;
- : int = 4
─( 15:34:24 )─< command 8 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop #
square (square 2) ;;
- : int = 16
─( 15:34:24 )─< command 9 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop #
let ratio x y =
  Float.of_int x /. Float.of_int y ;;
val ratio : int -> int -> float = <fun>
─( 15:34:25 )─< command 10 >────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # 
ratio 4 7 ;;
- : float = 0.5714285714285714
─( 15:34:25 )─< command 11 >────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─utop #        

I get an output but the modules are still unbound.

Also in reference to

This is correct. You opened the module but didn’t use it in the file so far, hence the warning. The warning goes away once you start using the stuff defined in

I heard this as well but $ dune runtest or $ dune exec test does not output the code like if I were to run it without core and base – is this expected?

─( 15:33:57 )─< command 0 >────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # open Base;;
─( 15:34:11 )─< command 1 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─utop #         
( 15:34:23 )─< command 0 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # open Base;;
Error: Unbound module Base

I don’t understand this output. You opened Base twice, the first time it worked and the second time it didn’t? Did you edit the utop output somehow?

I heard this as well but $ dune runtest or $ dune exec test does not output the code like if I were to run it without core and base – is this expected?

I don’t know what’s in the test so I don’t know what the output should be. Can you try this:

$ utop -require base
utop # open Base;;
utop # Fn.non;;

It should output the signature of base v0.15.0 Β· OCaml Package

1 Like
(*Test Program*)
open Base;;
open Core;;

3+4 ;;

8 / 3 ;; 

3.5 +. 6. ;; 

3*5>14 ;;

let square x = x*x ;;

square 2 ;;

square (square 2) ;;

let ratio x y =
  Float.of_int x /. Float.of_int y ;;

ratio 4 7 ;; 

I was able to get outputs for this program before I added core and base but now it just gives the unused error. Only when I try to run the dune build

I don’t understand this output. You opened Base twice, the first time it worked and the second time it didn’t? Did you edit the utop output somehow?

The output is first when I open base with utop and then secondly when I try to run my program by pressing shift + enter

This is the output of what you asked:

───────────────────────────────────────────────────────────────────────────────────┬─────────────────────────────────────────────────────────────┬────────────────────────────────────────────────────────────────────────────────────                                                                                   β”‚ Welcome to utop version 2.15.0 (using OCaml version 5.3.0)! β”‚                                                                                                                                                                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Type #utop_help for help about using utop.

─( 17:17:31 )─< command 0 >────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # open Base;;
─( 17:17:31 )─< command 1 >────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # Fn.non;;
- : ('a -> bool/2) -> 'a -> bool/2 = <fun>

So…everything is working then. The outputs are exactly as expected. Base is loaded. You’re getting the β€˜unused’ error in your test program because you’re not actually using anything from Base. All the operations and definitions in the test program are using basic arithmetic with ints and floats. If you do something like let is = Fn.non not;; then you will be using something from Base and the β€˜unused’ warning will go away.

Btw, Base is Jane Street’s portable, minimal version of their standard library. Core has a bunch of extra stuff in it that’s not very portable so they recommend that most people use Base unless they actually need something from Core.

We can use open! instead of open.

This suppress the unused warning.

3 Likes

Ah I see, I am not sure how I was getting outputs of the mathematical operations before… I may have gotten it mixed up in my head. I have added some Base functions and now I can execute and build the program. As well as some print statements.

Thanks for your patience!

Btw, Base is Jane Street’s portable, minimal version of their standard library. Core has a bunch of extra stuff in it that’s not very portable so they recommend that most people use Base unless they actually need something from Core.

Will note this, I have heard a lot of complaints about this book because they start you off my installing core when for the most part you use base in the beginning.

2 Likes

I think the book was originally written when there was only Core. Base came later as a more minimal and more portable, stand-alone foundation for Core.