-
Notifications
You must be signed in to change notification settings - Fork 437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Upstream derive handler for ToExpr
from Mathlib
#5906
base: master
Are you sure you want to change the base?
Conversation
Thanks for your interest Alex — upstreaming this has been on my radar for awhile, especially since |
Thanks for your input, Kyle. For the record, I did mention, quite a while back, on the Zulip that I'd like to have |
I've opened an issue to track the discussion at #5909 |
Great, the ToExpr RFC was just accepted — looking forward to seeing progress on this! |
Wonderful! I'm currently on vacation, but will push this forward when I'm back on the 25th! By the way, where did this acceptance happen? I didn't see any activity on the issue. |
It looks like there's a gap in FRO communication here — the triage team accepted it on Thursday and assigned to me seeing that it gets implemented. I'd suggest that we do two PRs here. One would be adding ToLevel and fixing all the currently existing ToExpr handlers, and then the second would be this PR, upstreaming the ToExpr handler. Does that sound reasonable? (No need to respond until you get back!) |
We should use the improved ToLevel since we definitely don't want the universe bump. |
I've opened a PR to upstream ToLevel at #6285 |
This PR upstreams the `ToLevel` typeclass from mathlib and uses it to fix the existing `ToExpr` instances so that they are truly universe polymorphic (previously it generated malformed expressions when the universe level was nonzero). We improve on the mathlib definition of `ToLevel` to ensure the class always lives in `Type`, irrespective of the universe parameter. This implements part one of the plan to upstream a derive handler for `ToExpr`, as discussed in #5906 and #5909. --------- Co-authored-by: Kyle Miller <[email protected]> Co-authored-by: Tobias Grosser <[email protected]>
toExpr := $(mkIdent auxFunName).{$levels,*} | ||
toTypeExpr := $toTypeExpr) | ||
instances := instances.push instCmd | ||
return instances |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a sort-of-bug that would be nice to fix. If you use these deriving handlers with set_option autoImplicit false
, then the universe level variables cause errors.
Ideally we would use $(...):ident.{$levels,*}
on this instance
to insert levels, but that would prevent the automatic instance name generator from being used.
Alternatively, we could wrap the instance
in three commands: section
, universe $levels*
, and end
.
Would you mind adding a test that the deriving handler works when autoImplicit
is false as well? (Take a look at lean/run/partialDelta.lean
for an example of how test files are structured.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "bug" is actually slightly trickier than that still: things also break if we explicitly add a universe u
command, even if autoImplicits are enabled.
Alternatively, we could wrap the instance in three commands: section, universe $levels*, and end.
I implemented this, expect using universe $levels* in
as an abbreviation for the section stuff.
Then, to prevent universe clashes, I check which universe names have been explicitly added to the Elaboration state, and remove those from the $levels list.
This fixes the tests that use exclusively explicit or implicit universes, but there is a final test that has both that still fails.
EDIT: turns out that failing test had a typo, and was in fact failing because it had an autoImplicit type parameter. I've fixed that typo, and added yet another testcase that tests the autoImplicit type parameter (and is currently still failing).
This also ensures that `mkLet` is resolved to `Lean.Elab.Deriving.mkLet` (as intended) instead of `Lean.mkLet`
The final test case currently fails, exposing a bug when auto implicits are disabled
Mathlib CI status (docs):
|
…oImplicits are enabled
…resent in the ambient context
Turns out I had a typo in the argument list. The problem was not with implicit universes, rather it was with implicit parameters. Also adds new testcases to expose this bug properly
This takes the derive handler for
ToExpr
from mathlib, and upstreams it into core.The derive handler uses an auxiliary typeclass,
ToLevel
to ensure the derivedToExpr
instances are properly universe polymorphic (when the original type is polymorphic), so we upstream this, too.Credit goes to @kmill for originally developing the derive handler. We'll use this in a light-weight, basic QQ alternative that currently lives in a PR to LNSym, but we plan to upstream also.