Ocaml lsp in vs code gives "unbound module Base"

231652998-a5b5dc96-d11b-4a23-bdab-3604e2663f88

Hi, I have Ocaml switch 4.13.1 running on Mac. utop is working fine, can load the Base module when I do:

#use "topfind";;
#require "base";;
open Base
List.mem ["1"] "1" ~equal:String.equal

even though I followed these instructions Real World OCaml which assume I don’t have even to do open Base, but the VS Code is having a problem loading the Base module. I have OCaml -LSP language server installed for VS code for the switch I have. But when I try the above code, it gives me red error squiggly line under the # of #use "topfind";;. When I remove use and require I get the following error in the screenshot attached. Can you please help with this? I have tried everything since yesterday, I tried to build a project with dune and still in the main the above code have squiggly lines.

Can you please:

  1. post your dune file

  2. post the error of dune build

I suspect there is an easy fix, but it is hard to debug without pertinent info.

Thanks!

So this is the dune file:

(executable
 (public_name box)
 (name main)
 (libraries box))

this is the code inside main.ml

#require "base";;
open Base 
List.mem ["1"] "1" ~equal:String.equal;;

and it still shows squiggly red lines.

and this is the dune build error:
Screenshot 2023-04-13 at 19.49.17

and this is my ~\.ocamlinit content in my home directory:

#use "topfind";;
#thread;;
#camlp4o;;
#require "core.top";;
#require "core.syntax";;
#require "ppx_jane";;

the problem remains the same when I remove open Base;; from this ocamlinit file

The dune file:

(executable
 (public_name box)
 (name main)
 (libraries box))

main.ml

#require "base";;
open Base 
List.mem ["1"] "1" ~equal:String.equal;;
(* let x = 9;; *)

The error is:
Screenshot 2023-04-13 at 19.49.17

~\.ocamlinit

#use "topfind";;
#thread;;
#camlp4o;;
#require "core.top";;
#require "core.syntax";;
#require "ppx_jane";;

I replied, but the Akismet hid the answer!

How big are the dune / *.ml files? If it’s less than 50kb, just copypaste.

I think I saw it before it got deleted. I think you just need to make sure base is included in your dune libraries.

I am talking about a basic project of 5 lines of code!
This is my main.ml

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

dune

(executable
 (public_name box)
 (name main)
 (libraries box base))

and this is the error I get:
Capture

I believe the #require "base";; is utop notation. What if you removed that line from the *.ml file ?

I did and updated my reply

open Base

let _ = List.mem ["a"] "a" ~equal: String.equal;;

^-- does this work for you ?

The project builds when I do dune build @all, and then dune exec bin/main.exe, but I can see no output true in the command, why?

Because your program doesn’t call any functions related to printing…

This worked:

open Base
let t = List.mem ["a"] "a" ~equal:String.equal;;
let () = Stdlib.Printf.printf "%B" (t);;

But I don’t know why I have to bind?

Right, sorry, I am new to ocaml, and confusing toplevel with ocaml!

A module implementation basically consists of definitions and expressions. The rule is that any expression at the top level of a module implementation has to be separated from previous items by a ;;. Otherwise, in many cases it would be impossible to tell where the previous item stops and the new expression begins.

Binding with let () = turns the expression into a definition, so it’s no longer necessary to use the ;;.

1 Like

Protip which you will see mentioned a lot in this forum: for newcomers to the language, it’s very helpful to turn on autoformatting so that it can guide you towards writing more idiomatic code and clearly highlight syntax errors.