Skip to content
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

fix: hovers on binders with metavariables #4137

Closed
wants to merge 2 commits into from
Closed

Conversation

nomeata
Copy link
Contributor

@nomeata nomeata commented May 11, 2024

this fixes #4078

@nomeata nomeata added the awaiting-review Waiting for someone to review the PR label May 11, 2024
@nomeata
Copy link
Contributor Author

nomeata commented May 11, 2024

Mostly as a learning experience, I wanted to be see if I can fix a bug this part of the code based. I wonder if this is really the right fix, because I don’t see much withSaveInfoContext in this module, so happy to be educated! @kmill maybe?

@nomeata nomeata requested a review from kmill May 11, 2024 15:37
@nomeata nomeata mentioned this pull request May 11, 2024
1 task
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc May 11, 2024 15:51 Inactive
@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label May 11, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request May 11, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request May 11, 2024
@@ -211,8 +211,9 @@ private partial def elabBinderViews (binderViews : Array BinderView) (fvars : Ar
let id := binderView.id.getId
let kind := kindOfBinderName id
withLocalDecl id binderView.bi type (kind := kind) fun fvar => do
addLocalVarInfo binderView.ref fvar
loop (i+1) (fvars.push (binderView.id, fvar))
withSaveInfoContext do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know this part of the code base very well (in particular, when withSaveInfoContext is needed), but it seems to me that the error actually has something to do with autoimplicits. Over in Lean.Elab.Term.withAutoBoundImplicit when the new autoimplicit is declared, the state is restored, but there's nothing recording the new state in the info trees. Maybe withSaveInfoContext should go there instead?

One piece of evidence for this is that there's no error when hovering over α when autoImplicits isn't activated:

def oops {β} (i : ∀ α, List α) : List β := i β

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, let’s try.

(Unrelatedly, but given that backtracking implementation of auto bound implicit, I wonder how easily that can increase elaboration time significantly.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope,

diff --git a/src/Lean/Elab/Term.lean b/src/Lean/Elab/Term.lean
index ee1014f944..476d807dda 100644
--- a/src/Lean/Elab/Term.lean
+++ b/src/Lean/Elab/Term.lean
@@ -1533,7 +1533,8 @@ partial def withAutoBoundImplicit (k : TermElabM α) : TermElabM α := do
               s.restore
               withLocalDecl n .implicit (← mkFreshTypeMVar) fun x =>
                 withReader (fun ctx => { ctx with autoBoundImplicits := ctx.autoBoundImplicits.push x } ) do
-                  loop (← saveState)
+                  withSaveInfoContext do
+                    loop (← saveState)
             | none   => throw ex
       loop (← saveState)
   else

does not help.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to be that there's stale info because s.restore doesn't restore infotrees. Assuming we want full backtracking, then maybe this is the solution?

partial def withAutoBoundImplicit (k : TermElabM α) : TermElabM α := do
  let flag := autoImplicit.get (← getOptions)
  if flag then
    withReader (fun ctx => { ctx with autoBoundImplicit := flag, autoBoundImplicits := {} }) do
      let rec loop (s : SavedState) : TermElabM α := withIncRecDepth do
        checkSystem "auto-implicit"
        try
          withSaveInfoContext k
        catch
          | ex => match isAutoBoundImplicitLocalException? ex with
            | some n =>
              -- Restore state, declare `n`, and try again
              s.restore (restoreInfo := true)
              withLocalDecl n .implicit (← mkFreshTypeMVar) fun x =>
                withReader (fun ctx => { ctx with autoBoundImplicits := ctx.autoBoundImplicits.push x } ) do
                  loop (← saveState)
            | none   => throw ex
      loop (← saveState)
  else
    k

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In further testing, doing s.restore (restoreInfo := true) is enough and withSaveInfoContext isn't necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to push here and take over, or open a new PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying that in #4192

@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc May 11, 2024 16:32 Inactive
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request May 11, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request May 11, 2024
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the builds-mathlib CI has verified that Mathlib builds against this PR label May 11, 2024
@leanprover-community-mathlib4-bot
Copy link
Collaborator

Mathlib CI status (docs):

@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc May 11, 2024 17:50 Inactive
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request May 11, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request May 11, 2024
nomeata added a commit that referenced this pull request May 16, 2024
this fixes #4078

it is an alternative fix to the one in #4137, suggested by @kmill.
@nomeata nomeata marked this pull request as draft May 16, 2024 15:14
@nomeata nomeata closed this May 16, 2024
github-merge-queue bot pushed a commit that referenced this pull request May 21, 2024
this fixes #4078. It is an alternative fix to the one in #4137,
suggested
by @kmill.

Incidentially, it makes the unused variable linter better. My theory is
that
if we don’t reset the info when backtracking, the binder shows up more
than
once in the info tree, and then it is considered “used”, although there
are
just multiple binders.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-review Waiting for someone to review the PR builds-mathlib CI has verified that Mathlib builds against this PR toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rpc Error when hovering variable
3 participants