Rtop and .ocamlinit Syntax Errors and Confusion


#1

Trying to follow along properly with the Real World OCaml utop setup, but using rtop:


Opam automatically starts out my .ocamlinit with this, which I’ve converted to Reason syntax.

let () =
  try (Topdirs.dir_directory(Sys.getenv("OCAML_TOPLEVEL_PATH"))) {
  | Not_found => ()
  };

RWO doesn’t mention this auto-added snippet explicitly, but it does mention checking if OCAML_TOPLEVEL_PATH has been set correctly. I have this in my shell configuration, so I’m good there:

if [ -r ~/.opam/opam-init/init.sh ]; then
  source ~/.opam/opam-init/init.sh > /dev/null 2> /dev/null
fi

Next, RWO reccomends adding these lines:

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

open Base;

Both

#use "topfind";

and

#require "core.top";

cause syntax errors when starting rtop.

I don’t know why; my guess would be that they’re somehow made redundant/obsolete by rtop's environment, but I don’t know why they would cause syntax errors as opposed to something else (Compiler errors? I’m new to ML, I don’t know what I’m talking about).

The only mention I’ve found online is this GitHub issue, which has only been labeled as a bug.


I whittled down my .ocamlinit to the following (I’m using Core instead of Base):

let () =
  try (Topdirs.dir_directory(Sys.getenv("OCAML_TOPLEVEL_PATH"))) {
  | Not_found => ()
  };

#thread;
#require "core";

open Core;

The necessity to require and open Core (for my purposes) is obvious. However, I still don’t know whether the initial function is necessary or what the #thread call does.

If I remove these, leaving only the require and open statements, I’m still able to copy/paste examples from RWO into rtop and they run successfully.

I found the source for Topdirs.dir_directory , and I understand the concept of a PATH, so I get what the function is doing, but that directory only contains a file topfind, which I assume is what is being referenced by the aforementioned “syntax error” #use "topfind".

Googling for ‘“ocaml” “topfind”’, I found some documentation that’s above my head, which mentions camlp4 a couple times, which I hear is incompatible with Reason. So that makes sense I guess.

Clicking further, though, I find this comment in a tutorial on the OCaml website:

#!/usr/bin/env ocaml
(* Need topfind to make require work, need require to use podge package *)
#use "topfind"
#require "podge"

However, #require "core"; works just fine without #use "topfind". So I guess #require is defined somewhere else and I don’t need Topfind? Or it’s already included somewhere?


Opening the file itself, I learned that the “topfind” file is an OCaml script which references the Topfind module, and is not the module itself, so I tried converting it to Reason syntax, to see if that was the cause for the syntax error; I got a different syntax error, so maybe? It runs without error now, but seems to only provide redundant/unnecessary functionality?


I also noticed while in rtop that if I typed #use "top, I got auto-completion suggestions for the extension-less files starting with “top” in the OCAML_TOPLEVEL_PATH (I added some extras to test this out). I tried removing the function at the top of .ocamlinit which adds that directory to the load path, hoping to find that the auto-completion suggestions (at least for my extra files) would also disappear. I still got the same suggestions, though, so it seems the path-updating is unnecessary, leaving only:

#thread;
#require "core";

open Core;

I still haven’t found anything about #thread, except that it is “undocumented”, and that some people who seem to know what they’re doing do leave it in their .ocamlinit, so I guess I will too. :man_shrugging:

Also still don’t know why #require "core.top"; would cause a syntax error… :man_shrugging:

Would absolutely love to be pointed to brief reading/viewing to set me straight; I’m sure I don’t need to slog through any scientific literature just to get my ML feet wet, but I also have no idea what the hell I’m doing.

Rtop is cool though. :+1: