4

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?

Alex Coplan
  • 13,211
  • 19
  • 77
  • 138
  • Can you give an example for a scenario where such a test would be useful? – waldrumpus Apr 29 '15 at 11:41
  • 1
    I'm building an automated testing application for user-submitted code. I have written a simple unit test library and will be processing submissions in a containerised instance of Poly/ML. The plan is to first load the submitted code, checking to see if it compiles, and then load the unit test library, followed by the test suite for the specific submission. The problem arises when users submit code that compiles correctly, but they fail to implement one or more methods that are tested in the test suite. Then the test suite will fail to compile and this isn't desirable behaviour. – Alex Coplan Apr 29 '15 at 12:53
  • @AlexCoplan can't you force the user submitted code to implement a certain signature? – Ionuț G. Stan Apr 29 '15 at 14:52
  • I'm not sure this can be done with the interpreter alone. Maybe with an external parser library, to find out which definitions exist? – waldrumpus Apr 30 '15 at 09:38
  • I assume you want to grade the existing functions and only penalize for the non-existing ones? Elaborating on @AlexCoplan's suggestion, you could ask of the students to provide a stub implementation even for functions that they didn't know how to implement. – waldrumpus Apr 30 '15 at 09:44

1 Answers1

4

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.

David Matthews
  • 516
  • 2
  • 1