Merlin: Possible to view inferred interface/mli?

I write a .ml file, without a .mli file. Is it possible to see what merlin thinks the interface for the ml file is?

3 Likes

Good question, that would indeed be nice. I don’t know the answer, but I use this:

1 Like

Hi, I made a little shell script to programmatically send the right commands to Merlin (2.5.4) to get its inferred interface for a file, you may find it useful: https://gist.github.com/yawaramin/b86557ae81cbd019fcb9e071abe594de

Thanks for the suggestions. I guess I really wanted something that I can use quickly from emacs, as an alternative to ocamlc -i and shell scripts etc.

From another file that uses the module defined in the file of interest, you can ask merlin for the type of the module (maybe twice), and look in the merlin-types buffer.

Here’s a quick Elisp hack that will get you most of the way there. The downside is that it does leave some cruft in the starting buffer, though this is banished with a single call to undo.

(defun infer-file-mli ()
  (interactive)
  (end-of-buffer)
  (open-line 1)
  (insert "end")
  (goto-char 0)
  (open-line 1)
  (insert "module Infer_mli = struct")
  (goto-char 9)
  (merlin-type-enclosing))

One could pop up a temporary buffer or something. (And I wonder if support for something like this should be added to the Merlin emacs mode.)

Can’t you just use merlin’s “merlin-type-enclosing” and “merlin-type-enclosing-go-up” to get the type of the current module? (I think those are the emacs names for the commands, I don’t know what keys they’re bound to, or what they’re called in Vim)

@lpw25 That didn’t work for me when I tried it. I was looking at building a real solution for this and submitting a PR to merlin, but I need to wrap up a few other things first.

I tried myself, and it seems like type-enclosing-go-up refuses to print the type of the whole module. I think this should be fixed though, rather than adding a whole new feature.

2 Likes

@lpw25 I’d like to have features to update/add to an mli file definition automatically. When refactoring it’s sort of a pain to keep everything in sync. I don’t think Merlin supports this workflow currently, though it is possible to copy things from the types buffer into an mli. This functionality could then be used to generate an entire mli.

1 Like

This would be terrific.

Another feature I want is to get the type of a toplevel value from the mli rather than deriving it. Often the mli type is more specific than the derived type of the expression, and that’s what I care about. This is a common problem in OCaml: because we don’t have a clean way to place type definitions in the same file, we use the mli. But the mli contains the external types, not the internal types of the toplevel values, and merlin only shows us the internal types.

1 Like