M2 icon indicating copy to clipboard operation
M2 copied to clipboard

Add assertLevel and automatic timer

Open mahrud opened this issue 1 year ago • 4 comments

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.

mahrud avatar May 08 '24 18:05 mahrud

From https://github.com/Macaulay2/M2/pull/3490#issuecomment-2364174273:

I'd like assert to behave like elapsedTime, and in particular I want to be able to sprinkle tons of assertions in my code and run them only depending on the value of debugLevel or assertLevel or something like that. It's possible that, with some work, we could get a interpreter-level assert keyword to also print extra debug information, in the spirit of how you use Equality expressions.

mahrud avatar Sep 20 '24 17:09 mahrud

@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?

mahrud avatar Sep 20 '24 17:09 mahrud

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.

d-torrance avatar Sep 20 '24 22:09 d-torrance

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.

mahrud avatar Sep 21 '24 15:09 mahrud