[ new,pragma ] add %ensure_identity#3785
Conversation
This came up during in-person discussion with Ohad Kammar, during the 2026 May IDM: sometimes, it is useful to be able to annotate a function which *should* reduce to the identity function, but where one wants a warning if it did not. Mostly this involves adding a `Warning` to `src/Core/Core.idr`, a `DefFlag` to `src/Core/Context/Context.idr`, and a pragma and its as-written-in-source version to `src/Idris/Syntax/Pragmas.idr` and `src/Idris/Parser.idr` respectively. The rest is then tracking that annotation, updating TTImp and TTC to track it, and the myriad of interfaces which this touches. At the time of writing this commit, the message prints 3 times for some reason, but otherwise appears to work!
|
Can we add this to |
|
The identity optimisation pass is run multiple times, since replacing a function with id can allow later functions to be detected as id. See Idris2/src/Compiler/Inline.idr Line 564 in 6a54860 |
|
Very nice! Will this result in an error with |
Will do! Didn't know that was a thing, so thanks for pointing it out! : )
Oh duh, it's the
Just checked. Yes it will! : D |
I had some confusion as to why the message was being printed 3 times. It was pointed out by Z-snails that we call `transform 3`, since some transformations may enable further optimisations. This means we also try to optimise the function to the identity 3 times, hence the repeated warning message! To make this clear to the user, I have updated the warning itself to track which pass it was on, as well as updated the `setIdentity` function to track which pass it is on (this is not entirely neat, as transform-pass-numbering is done "in reverse" by recursion on a Nat). The result is that the warning now includes the pass number, making it clear that there are multiple transformation steps going on. Hopefully this is helpful, and if not, the commit can be reverted.
|
I added some code to track the number of transformation passes in the warning message itself. I think this is helpful, but if it clutters things too much, I'm happy to revert that commit : ) |
So, with a warning on each pass, we might get two failures followed by a success? Is there a way to check and warn at the end. E.g. we do this manually by doing |
Possibly, but I'm not sure. I don't know whether the name gets transformed/mangled as part of the other optimisations, but if it doesn't we could track the name as part of the Don't know how I would write a test to trigger that, however... |
Description
This came up during in-person discussion with Ohad Kammar, during the 2026 May IDM: sometimes, it is useful to be able to annotate a function which should reduce to the identity function, but where one wants a warning if it did not.
Currently, the message prints 3 times for some reason, but otherwise appears to work!
TODO
Self-check
implementation, I have updated
CHANGELOG_NEXT.md