[MLton] Show type at point / OCaml -dtypes

Vesa Karvonen vesa.karvonen@cs.helsinki.fi
Wed, 27 Jul 2005 03:16:10 +0300


While I was trying to figure out how to implement warnExnMatch, I noticed
that the types of expressions in the MLton code were not always immediately
clear to me.

Type inference is really great when you are working on code that you are
familiar with. I usually have no trouble understanding the types of
expressions in my own programs. However, type declarations / annotations
can be most helpful when trying to make sense out of unfamiliar code.

Since version 3.07 OCaml has provided an option to save type information
inferred by the compiler. Here is a snippet from the OCaml release notes:

 ``- Added a new "-dtypes" option to ocamlc/ocamlopt, and an emacs extension
     "emacs/caml-types.el".  The compiler option saves inferred type information
     to file *.annot, and the emacs extension allows the user to look at the
     type of any subexpression in the source file.  Works even in the case
     of a type error (all the types computed up to the error are available).
     This new feature is also supported by ocamlbrowser.''

Both the Caml and the Tuareg modes for Emacs provide the function "Show
type at point", which looks up and shows the type of the expression under
the point using the annot-file.

If you are unfamiliar with the feature, I recommed that you try it. Here is
a silly OCaml program for testing:

<--- begin silly.sml --->
let () =
  let rec loop msg n =
    if n = 0 then
      (print_string msg ; print_string "\n")
    else
      loop (msg ^ " " ^ msg) (n-1)
  in
    loop "Hello!" 4
<--- end silly.sml --->

Just compile it with

   ocamlc -dtypes silly.ml

and try the "Show type at point" function (menu / C-c C-t) in Emacs under
either the Caml or the Tuareg mode.

I was wondering if there exists a similar tool for SML. If not, I think it
would be a very useful addition to MLton to generate type annotation files
and (for SML mode) to implement "Show type at point" using the generated
files. Note that OCaml's -dtypes is very different from MLton's -show-basis.