Skip to content

fix: kernel exception from fvars left from ?m a b instantiation - #4410

Merged
leodemoura merged 1 commit into
leanprover:masterfrom
llllvvuu:fix/recursive_let_deps
Jun 10, 2024
Merged

leodemoura merged 1 commit into
leanprover:masterfrom
llllvvuu:fix/recursive_let_deps

Conversation

@llllvvuu

@llllvvuu llllvvuu commented Jun 9, 2024 •

Copy link
Copy Markdown
Contributor

Closes #4375

The following example raises error: (kernel) declaration has free variables '_example':

example: Nat → Nat :=
  let a : Nat := Nat.zero
  fun (_ : Nat) =>
    let b : Nat := Nat.zero
    (fun (_ : a = b) => 0) (Eq.refl a)

During elaboration of 0, elabNumLit creates a synthetic mvar ?_uniq.16 which gets abstracted by elabFun to ?_uniq.16 := ?_uniq.50 _uniq.6 _uniq.12. The isDefEq to instOfNatNat 0 results in:

?_uniq.50 :=
  fun ([email protected]._hyg.13 : Nat) =>
    let b : Nat := Nat.zero
    fun ([email protected]._hyg.23 : Eq.{1} Nat _uniq.4 b) =>
      instOfNatNat 0

This has a free variable _uniq.4 which was a.

When the application of ?_uniq.50 to #[#2, #0] is instantiated, the let b : Nat := Nat.zero blocks the beta-reduction and _uniq.4 remains in the expression.

fix: add (useZeta := true) here:

instantiateExprMVars (f.betaRev args.reverse)

@llllvvuu
llllvvuu force-pushed the fix/recursive_let_deps branch 2 times, most recently from aaaee8c to dcfd93b Compare June 9, 2024 08:40
@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 Jun 9, 2024
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Jun 9, 2024
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Jun 9, 2024
@llllvvuu llllvvuu changed the title fix: mkLambdaFVarsWithLetDeps handles lets before lambdas fix: in mkLambdaFVarsWithLetDeps, handle lets before lambdas Jun 9, 2024
@nomeata

nomeata commented Jun 9, 2024

Copy link
Copy Markdown
Collaborator

Hello @llllvvuu and thanks for your first contribution to lean!

The src/Lean/Meta/ExprDefEq.lean file is very central to lean, and typically not the first module to touch when contributing to lean, and for the long-term maintainability of the code it is preferrable if such central modules are written by not too many people. Therefore it is possible and likely that the maintainers of this component will chose to implement their own fix, instead of using this one. Nevertheless your contribution is useful as it helps to understand the issue and points to one possible way to address it.

Good issue reports, including good minimized test cases, and contributions to less central components (error messages, leaf tactics, etc.) are a good way to build expertise, reputation and trust, before high-risk areas of the code.

@llllvvuu
llllvvuu force-pushed the fix/recursive_let_deps branch from 3f03a5b to f07661f Compare June 9, 2024 09:43
@llllvvuu

llllvvuu commented Jun 9, 2024

Copy link
Copy Markdown
Contributor Author

No worries, it's not a big deal to me whether this particular patch + test case is what gets merged. I'm glad if it's of some use.

ghost pushed a commit to leanprover-community/batteries that referenced this pull request Jun 9, 2024
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Jun 9, 2024
@ghost ghost added the builds-mathlib CI has verified that Mathlib builds against this PR label Jun 9, 2024
@ghost

ghost commented Jun 9, 2024 •

Copy link
Copy Markdown

Mathlib CI status (docs):

@llllvvuu llllvvuu changed the title fix: in mkLambdaFVarsWithLetDeps, handle lets before lambdas fix: kernel exception due to fvars left after instantiating?m a b c; add (useZeta := true) Jun 10, 2024
@llllvvuu llllvvuu changed the title fix: kernel exception due to fvars left after instantiating?m a b c; add (useZeta := true) fix: kernel exception due to fvars left after instantiating ?m a b c; add (useZeta := true) Jun 10, 2024
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Jun 10, 2024
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Jun 10, 2024
@llllvvuu
llllvvuu force-pushed the fix/recursive_let_deps branch from 8d77259 to f73d07e Compare June 10, 2024 02:38
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Jun 10, 2024
@llllvvuu
llllvvuu force-pushed the fix/recursive_let_deps branch from f73d07e to 80affa4 Compare June 10, 2024 03:00
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Jun 10, 2024
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Jun 10, 2024
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Jun 10, 2024
@llllvvuu
llllvvuu force-pushed the fix/recursive_let_deps branch from 80affa4 to c365a0e Compare June 10, 2024 05:34
@llllvvuu llllvvuu changed the title fix: kernel exception due to fvars left after instantiating ?m a b c; add (useZeta := true) fix: kernel exception from fvars left from ?m a b instantiation Jun 10, 2024
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Jun 10, 2024
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Jun 10, 2024
@llllvvuu

llllvvuu commented Jun 10, 2024 •

Copy link
Copy Markdown
Contributor Author

I just changed the fix/description, new one is just changing a flag. It still doesn't achieve not touching central modules, but I hope it sheds some more light on the issue (test case further minimized as well).

The following example raises `error: (kernel) declaration has free
variables '_example'`:
```lean
example: True → Nat :=
  let a : Nat := Nat.zero
  fun (_ : True) =>
    let b : Nat := Nat.zero
    (fun (_ : a = b) => 0) (Eq.refl a)
```

During elaboration of `0`, `elabNumLit` creates a synthetic mvar
`?_uniq.16` which gets abstracted by `elabFun` to `?_uniq.16 :=
?_uniq.50 _uniq.6 _uniq.12`. The `isDefEq` to `instOfNatNat 0` results
in:
```
?_uniq.50 :=
  fun ([email protected]._hyg.13 : True) =>
    let b : Nat := Nat.zero
    fun ([email protected]._hyg.23 : Eq.{1} Nat _uniq.4 b) =>
      instOfNatNat 0
```

This has a free variable `_uniq.4` which was `a`.

When the application of `?_uniq.50` to `#[leanprover#2, #0]` is instantiated, the
`let b : Nat := Nat.zero` blocks the beta-reduction and `_uniq.4`
remains in the expression.

fix: add `(useZeta := true)` here:
https://github.com/leanprover/lean4/blob/ea46bf2839ad1c98d3a0c3e5caad8a81f812934c/src/Lean/MetavarContext.lean#L567
@llllvvuu
llllvvuu force-pushed the fix/recursive_let_deps branch from c365a0e to 12395b2 Compare June 10, 2024 06:43
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Jun 10, 2024
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Jun 10, 2024
@leodemoura

Copy link
Copy Markdown
Member

I just changed the fix/description, new one is just changing a flag. It still doesn't achieve not touching central modules, but I hope it sheds some more light on the issue (test case further minimized as well).

Looks good. We will merge it. Thanks!

@leodemoura
leodemoura added this pull request to the merge queue Jun 10, 2024
Merged via the queue into leanprover:master with commit 6a7bed9 Jun 10, 2024
mathlib-bors Bot pushed a commit to leanprover-community/mathlib4 that referenced this pull request Jun 12, 2024
This linter was silently not doing anything until leanprover/lean4#4410 was fixed, and now it is working so a backlog of warnings needed to be addressed. Some were addressed here: #13680.

The warnings in this PRs are false positives (leanprover-community/batteries#428?), but a workaround is put in place.



Co-authored-by: L Lllvvuu <[email protected]>
AntoineChambert-Loir pushed a commit to leanprover-community/mathlib4 that referenced this pull request Jun 20, 2024
This linter was silently not doing anything until leanprover/lean4#4410 was fixed, and now it is working so a backlog of warnings needed to be addressed. Some were addressed here: #13680.

The warnings in this PRs are false positives (leanprover-community/batteries#428?), but a workaround is put in place.



Co-authored-by: L Lllvvuu <[email protected]>
@kmill kmill mentioned this pull request Jul 25, 2024
3 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Bug with nested ifs and lets

3 participants