-
Notifications
You must be signed in to change notification settings - Fork 819
fix: instantiate and normalize level metavariables in getLevel
#13343
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
Closed
+37
−8
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import Lean | ||
|
|
||
| /-! | ||
| Test that `getLevel` instantiates and normalizes level metavariables. | ||
|
|
||
| Without this, `getDecLevel` fails on sort-polymorphic types with `Prop` components: | ||
| `PProd.{?u, ?v} Nat True` has sort `max ?u ?v`. After `?u := 1`, `?v := 0`, this should | ||
| normalize to `1` (decrementable to `0`), but without instantiation `decLevel` sees | ||
| `max ?u ?v`, tries to decrement each arm independently inside the `max`, follows | ||
| `?v → 0`, and fails because `dec 0 = none`. | ||
| -/ | ||
|
|
||
| -- Direct reproducer: getDecLevel on PProd with assigned-but-uninstantiated level mvars | ||
| open Lean Meta in | ||
| #eval show MetaM _ from do | ||
| let u ← mkFreshLevelMVar | ||
| let v ← mkFreshLevelMVar | ||
| let ty := mkApp2 (mkConst ``PProd [u, v]) (mkConst ``Nat) (mkConst ``True) | ||
| assignLevelMVar u.mvarId! (.succ .zero) | ||
| assignLevelMVar v.mvarId! .zero | ||
| let lvl ← getDecLevel ty | ||
| assert! lvl == .zero | ||
|
|
||
| -- End-to-end: for loop with sort-polymorphic mut var | ||
| def foo : Unit := Id.run do | ||
| let mut x : PProd Nat True := ⟨0, trivial⟩ | ||
| for _ in [true] do | ||
| x := ⟨0, trivial⟩ | ||
| break |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Usually we leave instantiation and other such operations to whoever needs it. Can you comment on why it's better to change
getLevelinstead of fixingdecLevel??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.
Indeed, that's exactly my backup plan!
Uh oh!
There was an error while loading. Please reload this page.
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.
Note that
decLevelitself is able to instantiate metavariables already. The actual issue isn't about instantiation, but thatprocessMaxshould skip arguments where decrementing fails, so long as at least one succeeds, if I'm understanding it correctly. In your example, decrementingmax (u + 1) 0should beu, even though you can't decrement0.For
imaxthough, decrementing the second argument must succeed. The first argument's success is optional.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.
Hmm, but what about
max (?u+1) ?v? If this returns just?uthen we are losing information because?vcould be larger than?u+1, no? Maybe the correct thing is to filter outmaxcomponents that can't be decremented? That works as long asdecLevel? l = noneiffl = 0... Not sure whether this is true.Uh oh!
There was an error while loading. Please reload this page.
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.
At any rate, all I need right now is to instantiate and then normalize the resulting constant Level expression. I think I'm going to do just that in
getDecLevelandgetDecLevel?.I don't want to touch
decLevel?directly, because it has many transitive call sites; it's called from the main def eq solving function, for example.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.
Oh, yeah, my approach would need can/can't/unknown.
How about this: give
decLevel?the ability to simplifymaxexpressions? Here's a possibility:Uh oh!
There was an error while loading. Please reload this page.
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.
I'm worried about the unknown impact of normalizing the expressions. I don't have any specific concerns though, just the fact that it will result in significantly reordered level expressions. Giving
decAux?the ability to more precisely decrementmaxexpressions seems like a smaller change to me, despite the fact it has a bigger impact — and I do worry aboutdecLevel?andgetDecLevel?having subtly different behavior.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.
I take it back — after having looked at how isLevelDefEq works, I think you're perfectly justified in using
normalize. HavingdecAux?do any simplification just makes it more complicated, and it's an imperfect simplification anyway.(I want to see the performance impact of some changes to
decAux?so I made #13352, but I don't intend to merge it. Feel free to take the extra test file for this PR.)