Current state of ocamlnat

I’ve been trying to use ocamlnat, the experimental native toplevel distributed with the compiler, but not installed by default. This is an attempt of a follow-up to Interim report on experience with ocamlnat.

Following advice on discord, I created a fresh 4.10 switch and installed ocamlnat by doing opam pin add ocaml-variants 4.10.0+flambda+no-flat-float-array --edit and editing the opam file to include make ocamlnat, as explained in the manual. However, executing ocamlnat fails because no cmx file is found for Topdirs.

Does anyone have working installation instructions for ocamlnat? Is loading of libraries into ocamlnat still as cumbersome as in the thread mentioned above? Is it possible to build a version of utop that uses the native toplevel? Seemingly, a native REPL is not far away, but so far it does not seem to be usable?

1 Like

I think you’ll need to provide more precise details, it may have been broken upstream since it’s not being tested in the CI (and will not be). I have an opam repo over there with a working ocamlnat (but for 4.07.1).

As far as library loading is concerned many (but not all) of them can be loaded via omod and for line edition simply use rlwrap or Down.

Ok, thanks. Here is what I did. In a fresh switch of 4.10+flambda+no-flat-float-array, I opam pinned ocaml-variants and edited as described above. The additional make line in the opam file was
[make "-j%{jobs}%" {os != "cygwin"} "ocamlnat"].
Installation seems to have worked since ocamlnat is available on the path after reinstalling. It also seems to be basically the same change to the opam file you were using?

Here is the output after typing ocamlnat at the command line:

OCaml version 4.10.0 - native toplevel

File "_none_", line 1:
Warning 58: no cmx file was found in path for module Topdirs, and its interface was not compiled with -opaque
>> Fatal error: Opttoploop.dll_run dlopen(/var/folders/bh/0mc1q9cn5ps48h60dygx_lhm00011n/T/camlTOP386c084.so, 10): Symbol not found: _camlTopdirs
  Referenced from: /var/folders/bh/0mc1q9cn5ps48h60dygx_lhm00011n/T/camlTOP386c084.so
  Expected in: flat namespace
 in /var/folders/bh/0mc1q9cn5ps48h60dygx_lhm00011n/T/camlTOP386c084.so
Fatal error: exception Misc.Fatal_error

I’ll have a look later but did you try to add -I options so that the missing topdirs.cmx gets into the search path ?

Mmmh actually that looks weird it should look for Opttopdir. I suspect you have stuff in your .ocamlinit that ocamlnat doesn’t like. Try to invoke ocamlnat -noinit

Ah yes! I have this in the .ocamlinit :

(* Added by OPAM. *)
let () =
  try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
  with Not_found -> ()
;;

When I invoke ocamlnat -noinit I get a prompt!

2 Likes

Note that this bit should no longer be needed you can remove it from your .ocamlinit. In general at the moment though it’sdifficult to share your usual .ocamlinit with ocamlnat.

Ok cool. I tried as a next step to install ocamlfind and invoke #use "topfind";; this fails with

File "_none_", line 1:
Warning 58: no cmx file was found in path for module Topdirs, and its interface was not compiled with -opaque
File "_none_", line 1:
Warning 58: no cmx file was found in path for module Toploop, and its interface was not compiled with -opaque
>> Fatal error: Opttoploop.dll_run dlopen(/var/folders/bh/0mc1q9cn5ps48h60dygx_lhm00011n/T/camlTOP5546fbf.so, 10): Symbol not found: _camlTopdirs
  Referenced from: /var/folders/bh/0mc1q9cn5ps48h60dygx_lhm00011n/T/camlTOP5546fbf.so
  Expected in: flat namespace
 in /var/folders/bh/0mc1q9cn5ps48h60dygx_lhm00011n/T/camlTOP5546fbf.so
Fatal error: exception Misc.Fatal_error

Is this just a problem with ocamlfind not installing a cmx or is it expected that findlib will not work? (in the latter case, it seems ocamlnat will be cumbersome to use)

ocamlfind will not work because it uses the bytecode toplevel API and a bit stupidly the ocamlnat API is separate despite providing the same functionality (see point 4. here).

As I said above use omod if you want to get to something (follow the tutorial I linked above). But not to anything unfortunately, report any loading issues you hit (some of which I’m already aware) on omod's bug tracker.

I just saw you posted omod as a solution for library loading. I tried it, and #use "omod.nattop" succeeds, however with the warning
Warning 58: no cmx file was found in path for module Opttopdirs, and its interface was not compiled with -opaque. I tried to load cmdliner which succeeds.

I’ll report any issues here or on the omod tracker.

A general question: How much work would it be if anyone decided to port utop to ocamlnat, disregarding the fact that the latter is experimental?

Note that there is an ongoing project that aims to modernize/fix the native toplevel (incidentally it is spearheaded by the original author of utop), see https://github.com/ocaml/RFCs/pull/15.

Best wishes,
Nicolás

2 Likes

I just came across this project https://github.com/bmeurer/ocamlnat Is this the one ocaml currently has in distribution?

Answering myself, not it isn’t.