How do I load compiler-libs in utop?

  1. Library referred to.

  2. init/m_gen.ml:

#use "topfind";;
#require "core.top";;
#require "core_unix";;
#require "compiler-libs";;
#require "compiler-libs.common";;
#require "httpaf";;
#require "httpaf-lwt-unix";;
#require "re";;
#require "str";;
#require "uri";;
#require "ppx_jane";;

#directory "_build/default/slib/.m_gen.objs/byte";;
#load "_build/default/m_gen/.m_gen.objs/byte/m_gen.cmo";;

Stdio.printf "Loaded";;

  1. m_gen/m_gen.ml:
open Base


let parse_file (fname: string) : Parsetree.structure =
  let ast = Pparse.parse_implementation ~tool_name:"test_past" fname in
    ast
;;


let read_dir_rec (dirs: string list) : string list = 
  let rec read_dir__helper (dirs: string list) (cur_list: string list) : string list =
    match dirs with
    [] -> cur_list
    | path :: dirs -> 
        let local_entrys : Core_unix.Readdir_detailed.t list = Core_unix.ls_dir_detailed path in
        let is_dir (kind: Core_unix.file_kind option) : bool = match kind with
          Some(S_DIR) -> true
          | _ -> false in
        let local_dirs : string list = List.filter_map local_entrys 
            ~f: (fun x -> if is_dir x.kind then Some(x.name) else None) in
        let local_files : string list = List.filter_map local_entrys 
            ~f: (fun x -> if not (is_dir x.kind) then Some(String.concat [path; x.name]) else None) in
        let cur_list = List.append local_files cur_list in
        let dirs = List.rev_append local_dirs dirs in
          read_dir__helper dirs cur_list
  in
  read_dir__helper dirs [] ;;


let rewrite_data (dirs: string list) : Parsetree.structure list =
  begin
    Stdio.printf "Parsing Directories: %s\n%!" (String.concat ~sep:":" dirs);
    let all_files: string list = read_dir_rec dirs in
    let ml_files: string list = List.filter all_files ~f:(fun x -> String.is_suffix x ~suffix:".ml") in
    let parsed_data = List.map ml_files ~f:parse_file in
      parsed_data
  end
;;

let _xx = rewrite_data ["m_data"];;


if not !Sys.interactive then
    let args: string list = Array.to_list @@ Sys.get_argv () in
    match args with
      [] -> Stdio.printf("Impossible.")
      | [_] -> Stdio.printf("Need to specify atleast one directory.")
      | _ :: _tl -> Stdio.printf("a") (* rewrite_data tl *)


Problem I run into:

 make u__m_gen
utop -init init/m_gen.ml
─────────────────────────┬─────────────────────────────────────────────────────────────┬─────────────────────────
                         β”‚ Welcome to utop version 2.11.0 (using OCaml version 5.0.0)! β”‚                         
                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                         
Findlib has been successfully loaded. Additional directives:
  #require "package";;      to load a package
  #list;;                   to list the available packages
  #camlp4o;;                to load camlp4 (standard syntax)
  #camlp4r;;                to load camlp4 (revised syntax)
  #predicates "p,q,...";;   to set these predicates
  Topfind.reset();;         to force that packages will be reloaded
  #thread;;                 to enable threads

File "init/m_gen.ml", line 1:
Error: Reference to undefined global `Pparse'

Type #utop_help for help about using utop.

─( 23:50:30 )─< command 0 >───────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # 

For some reason, it can’t find Pparse.

What am I doing wrong ?

I’m using Ocaml 5.0.0, but could try with older versions. I tested in two environments:

  1. the standard toplevel β€œocaml”
  2. β€œutop” (freshly installed; I never use it)

I did:

#use "topfind";;
#require "compiler-libs.common";;
#show Pparse ;;

and I got the signature printed, no problem.

So far, so good.

  1. Then I did (in utop)
utop # Pparse.preprocess "1";;
Line 1:
Error: Reference to undefined global `Pparse'

In the OCaml toplevel, it worked fine.

So whatever’s going on, it’s utop-specific, and it isn’t just you.

Hope this helps.

2 Likes

Yeah, I just needed to use the top level to β€œprint” some values that did not have @@deriving show defined on them.

For whatever reason, I could not #load the cmo , but I could #reqquire the ml , which is enough to get the value and print.

Thanks!

You should use utop-full for a version of utop where the compiler-library are available. The root issue is that utop is using the compiler library findlib package itself but removing them from the REPL scope.

2 Likes