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:

You can use it like: sig src/

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.


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
type t = A
let v = A
let f = function A -> A

$ ocamlc -i
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/:


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