Search code examples
smlpolyml

Check if function is defined in SML


Given an SML source file, is it possible to check (using Poly/ML) whether or not a list of function/value names are defined? If so, how?

Alternatively, I've noticed that you can do the following. Suppose we have a source file to be inspected named somefile.sml. Suppose we create the file test.sml, with the following contents:

use "somefile"
f; (* defined in somefile.sml *)
g; (* undefined *)

And then we run:

use "test" handle e as SyntaxError => (print (exnMessage e));

Unfortunately, this only prints out "Static Errors". Is there any way, similar to that described above, to determine (in code) which functions in "test.sml" are not defined?


Solution

  • There's probably no way to do this portably but in Poly/ML you can find out whether a value, or anything else, is defined using PolyML.globalNameSpace.

    To test for a value (e.g. a function) use

    #lookupVal PolyML.globalNameSpace
    

    This takes a name and returns a option type which is SOME if the value has been defined and NONE if it has not. So

    #lookupVal PolyML.globalNameSpace "f";
    

    will return

    SOME ?
    

    while

    #lookupVal PolyML.globalNameSpace "g";
    

    will return NONE.