Bourne shell script to print Merlin's inferred module sig of a file

I managed to whip up a script to get the module signature of a file using Merlin’s (2.5.4 only) inference: https://gist.github.com/yawaramin/b86557ae81cbd019fcb9e071abe594de

You can use it like: sig src/foo.ml

And it will print the signature to stdout. This is useful for reading into an editor. E.g., with vim I can open a new signature file :e foo.mli and then do :r !sig % to immediately read in the signature.

7 Likes

I think this could be simpler if you stick to merlin 3.0 and use $ ocamlmerlin single. Haven’t tried it though.

Why don’t you use the -i option of the OCaml compiler ?

$ cat a.ml
type t = A
let v = A
let f = function A -> A

$ ocamlc -i a.ml
type t = A
val v : t
val f : t -> t

Because there’s no convenient way to pass all the necessary options in a big project. Though it’s not even that hard, you can simply peek at jbuilder’s command log and repurpose one of the commands there.

I frequently use cmitomli on files in _build/:
https://opam.ocaml.org/packages/cmitomli/

6 Likes

Kudos for the tip about cmitomli, it still works great with 4.06 :+1: