Type checking ocaml scripts

Is there any good way to batch type check OCaml scripts ?

For now I’m using

ocaml -init myscript < /dev/null

with a suitable:

let () = if !Sys.interactive then () else main ()

in the script source but it feels a bit like a hack (and the exit code is not right in case of error).

1 Like

If your scripts do not contain toplevel directives you could use ocamlc -stop-after typing foo.ml. Don’t know of a solution in the general case.

Cheers,
Nicolas

2 Likes

Thanks but a script starts with a shebang. And then if you are doing anything serious it is followed by #use "topfind".

So invoking the compiler can’t be done here.

Alternatively, you can use ocamlscript:

opam info ocamlscript
[...]
synopsis     Tool which compiles OCaml scripts into native code
2 Likes

At that point I rather use my own :–) The idea here is just to rely on a base install of ocaml+ocamlfind.

Since typechecking the scripts requires to provide an interpretation of the #require directives (and to ignore calls to Toplevel functions), I fear that the easiest option would be to call ocamlc/opt -stop-after typing from b0caml itself.

1 Like

Sure these things can be solved with interpreters that parse the script. In b0caml (and I guess ocamlscript) since I actually compile the script to an executable, I don’t even need to stop-after.

I was really looking to solve the problem for #!/usr/bin/env ocaml.

(Note that except for the exit code what I mentioned above almost works, the painful and brittle workaround is for the build system to parse the output to look for Error: messages).

How about keeping the original source code without any shebang or other directives, and using the build system to generate the final script with the shebang etc. on top? That way you can use ocaml or whatever to typecheck the source code.

Sure, there’s thousands of horrible workarounds, I was just enquiring whether I had missed something. It seems I didn’t.

Not really surprised. The whole toplevel/ocaml interpreter user experience is utterly broken without anyone caring more than that (and I won’t bother open an issue upstream, these usability issues just end up being closed by the “friendly bot” :joy:).

Yeah imho the OCaml compilers should ignore any #...;; directives.

You don’t like utop?
I find it decent.

We could consider having the ocaml toplevel support -stop-after X options – without running the actual code of each phrase. If X is typing or after, the phrase extends the typing environment.

Edit; I guess the next step is a feature request on ocaml/ocaml.

1 Like