M2
M2 copied to clipboard
Add assertLevel and automatic timer
We should add a global function like assertLevel and promote a practice of adding things like this whenever possible:
if assertLevel > 0 then assert isWellDefined X;
if assertLevel > 1 then assert verifyResult X;
if assertLevel > 2 then ...
In particular, deeper methods in Core should have a higher threshold than methods in packages. This should also replace Verify option of inducedMap,lift,extend.
Tangentially related: is there something I could add in my init.m2 file so that every command I enter in the interpreter automatically gets called with elapsedTime? e.g. sometimes I want to see elapsedTime for everything without typing it every time. Perhaps there could be a global boolean flag like notify, say benchmarkMode or timingMode.
From https://github.com/Macaulay2/M2/pull/3490#issuecomment-2364174273:
I'd like
assertto behave likeelapsedTime, and in particular I want to be able to sprinkle tons of assertions in my code and run them only depending on the value ofdebugLevelorassertLevelor something like that. It's possible that, with some work, we could get a interpreter-levelassertkeyword to also print extra debug information, in the spirit of how you useEqualityexpressions.
@d-torrance I think changing assert to be a keyword (and hence being able to just do assert 1 + 1 == 2 without parentheses) should be relatively painless, but I think it would be nice to also add a threshold do it. For instance assert blah and assert_0 blah should always run, while assert_n foo should run if assertLevel (or just debugLevel, idk) is at least n (and maybe the code inside TEST should run all assertions up to a high threshold).
However, I don't know how to make assert understand _0 or _n in general. Any ideas?
If assert is a keyword, then I think assert_n foo will raise a syntax error. Maybe we could make assert_ a keyword too? I'm not sure if this will parse, though, since _ isn't usually allowed in words.
I think you're right. Maybe just assert, assert1, etc? Or maybe there can be only two levels: ignore assertions or run them, based on whether debugLevel is positive or not. I think without a keyword it's not possible to prevent the input to assert from being evaluated.