How to dump .cmt/.cmti files to text formal?

Hello,
Is this possible to kind of recreate automatically a .mli file from a .cmt or .cmti file?
Thanks,
F.

When I have the time, I plan to hack a CLI-based search engine for type signatures installed
in an opam switch using this information.

@AltGr I guess you know that :wink:

I had forgotten this one is the right tool (for .cmi files):

You might want to look at ocp-index. Among other things, it provides shell completion for values and types such that one can search for them by providing a prefix.

:$ oct print_endline 
val Pervasives.print_endline: string -> unit
Print a string, followed by a newline character, on
                                             standard output and flush standard output.

Here oct is a little snipped for Bash:

oct() {
  ocp-index complete --format "%k %p: %t
%d" "$@"
}

_oct() {
    COMPREPLY=( $(ocp-index complete --color=never --format "%q" "${COMP_WORDS[C
}

complete -F _oct oct

1 Like

I use ocp-browser for this use case.

I know of https://github.com/mjambon/cmitomli, which gives you .mlis from .cmis.

If the focus is on type-based search, this can be quite tricky if you want to take into account some equivalence between signatures. The idea was explored by Roberto di Cosmo 20 years ago: Type isomorphisms for module signatures [PDF].

1 Like

I know of this work.
But I want to do a search engine.
Looking for type isomorphisms is an advanced concept.
Maybe we don’t need that from day 0.

Have you looked at ocamloscope[1] by Jun Furuse?
I believe this is the same thing - i.e. parsing .cmi files to deduct signatures and then providing a search engine ala hoogle.

/Anders

[1] https://github.com/camlspotter/ocamloscope.2

I tried it in the past.
I had great hopes.
I was not impressed.

Yup! What you want is the Untypeast module. It will convert a Typedtree (which you get by reading a cmt file with Cmt_format) to a Parsetree, which you can then print as ocaml source code with Pprintast.
All these libraries are available in the compiler-libs package, which is part of the ocaml standard library.

1 Like

thierry’s tool (hooglebackend) already does the job; using compiler-libs I guess