[MLton] ML question

Matthew Fluet fluet@cs.cornell.edu
Sun, 23 Apr 2006 17:04:21 -0400 (EDT)


> I would like to be able to get accurate identifier cross-reference
> for an ML program.  That is, for every occurrence of every identifier,
> I would like to know its source-code location, whether it's a binding
> instance, and if not, the source-code location of the binding instances.
> Do you have any idea what ML front end might be most readily adapted
> to provide this information?

MLton already provides all of this information, albeit you may need to 
massage the output for your purposes (which is noweb, I'm guessing).  From 
the mlton man page:

-show-def-use file

       Output def-use information to file. Each identifier that is defined
       appears on a line, followed on subsequent lines by the position of
       each use.

For example, if I have the file:

fac.sml:
=====
structure Fac =
    struct
       val rec fac =
          fn 0 => 1
           | n => n * fac (n - 1)
    end

structure Main =
    struct
       fun doit n =
          if n = 0
             then ()
          else let
                  val _ = if 479001600 <> Fac.fac 12
                             then raise Fail "bug"
                             else ()
               in
                  doit (n - 1)
               end
    end

val () = Main.doit 100000000
=====

Then compiling with  mlton -show-def-use fac.du fac.sml  yields a file

fac.du:
=====
signature ARRAY_SLICE_GLOBAL <basis>/arrays-and-vectors/array-slice.sig 1.11
     <basis>/arrays-and-vectors/array-slice.sig 7.15
signature ARRAY_SLICE <basis>/arrays-and-vectors/array-slice.sig 5.11
     <basis>/arrays-and-vectors/array-slice.sig 42.15
     <basis>/libs/basis-extra/top-level/basis-sigs.sml 10.25
     <basis>/libs/basis-extra/top-level/basis.sig 84.30
...
exception Fail <basis>/general/general.sml 19.17
     <basis>/arrays-and-vectors/sequence.fun 232.59
     ...
     <basis>/text/string.sml 61.8
     fac.sml 15.40
...
signature UNSAFE <basis>/unsafe.mlb 17.17
structure Unsafe <basis>/unsafe.mlb 18.17
structure Fac fac.sml 
1.11
     fac.sml 14.42
variable fac fac.sml 3.15
     fac.sml 5.22
     fac.sml 14.42
variable n fac.sml 5.13
     fac.sml 5.18
     fac.sml 5.27
structure Main fac.sml 8.11
     fac.sml 22.10
variable doit fac.sml 10.11
     fac.sml 18.18
     fac.sml 22.10
variable n fac.sml 10.16
     fac.sml 11.13
     fac.sml 18.24
=====

A downside, as you can see, is that it will give you _every_ identifier, 
but you can easily extract the information relevant to one file.

It may be more efficient to modify MLton to output the def-use info in a 
format more suitable for your purposes than it is massage the current 
output; but, all of the info is there.

Vesa Karvonen has been using the def-use info to provide SML navigation in 
Emacs:
   http://mlton.org/pipermail/mlton/2006-April/028599.html