maxitg / SetReplace

C++/Wolfram Language package for exploring set and graph rewriting systems
MIT License
219 stars 46 forks source link

Automatic Wolfram Language syntax checking #484

Open maxitg opened 4 years ago

maxitg commented 4 years ago

The problem

It seems like there is a lot that can be automated about Wolfram Language syntax checking, exception handling, etc. For example, take a look at HypergraphUnificationsPlot.

A lot of that file does not seem necessary. Specifically, https://github.com/maxitg/SetReplace/blob/26047a96206b32b8acbf2075328471fee9073513/Kernel/HypergraphUnificationsPlot.m#L11 can be inferred from the function definition.

https://github.com/maxitg/SetReplace/blob/26047a96206b32b8acbf2075328471fee9073513/Kernel/HypergraphUnificationsPlot.m#L17-L19 is essentially the same for all functions.

https://github.com/maxitg/SetReplace/blob/26047a96206b32b8acbf2075328471fee9073513/Kernel/HypergraphUnificationsPlot.m#L21-L22 can be inferred from the function definition as well.

In fact, ideally, all we would need to do in that file is this:

hypergraphUnificationsPlot::usage = usageString[...];

Options[hypergraphUnificationsPlot] = Options[WolframModelPlot];

$color1 = Red;
$color2 = Blue;

hypergraphUnificationsPlot::emptyEdge = "Empty edges are not supported.";

hypergraphUnificationsPlot[e1_ ? hypergraphQ, e2_ ? hypergraphQ, opts : OptionsPattern[]] := ModuleScope[
  implementation...
  If[somethingBadHappenedQ, abortWithErrorMessage[hypergraphUnificationsPlot::emptyEdge]];
  implementation...
]

definePublicSymbol[hypergraphUnificationsPlot][HypergraphUnificationsPlot]

If the pattern in the definition above did not match, it should know that an error message should be printed. At this point, it can go through the arguments, see which one did not match, and print a message about it based on its position, the original function call, and the test function (we can have a map from test functions to messages).

The tests for most cases of invalid arguments would not be necessary as well, because we can just test definePublicSymbol once with many different argument patterns.

taliesinb commented 4 years ago

For usage messages, I should mention SetUsage in GeneralUtilities, which introduces a rich language of usage messages that actually exceeds what is possible in ordinary Wolfram Language. To see how it "demo itself", try ?SetUsage':

PixelSnap 2020-10-17 at 00 39 26@2x

It uses a superset of the conventioned used by Mathematica's own, strange "docutools" system.

maxitg commented 4 years ago

Yes, that's a lot better than the current system: https://github.com/maxitg/SetReplace/blob/26047a96206b32b8acbf2075328471fee9073513/Kernel/GeneralizedGridGraph.m#L7-L10