SetReplace icon indicating copy to clipboard operation
SetReplace copied to clipboard

Automatic Wolfram Language syntax checking

Open maxitg opened this issue 5 years ago • 2 comments

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.

maxitg avatar Oct 16 '20 22:10 maxitg

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.

taliesinb avatar Oct 16 '20 22:10 taliesinb

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

maxitg avatar Oct 16 '20 22:10 maxitg