VCGen `reduceHead?` loops on a projection function applied to a variable
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 55/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- compilers
Research direction
Start with src/Lean/Elab/Tactic/VCGen/Reduce.lean, focusing on the .const branch that unfolds projection functions and calls Sym.shareCommonInc. Read the reproduction and compare behavior with sharing checks enabled and disabled. Done means reduceHead? terminates on S.b x and returns none when no head reduction applies.
Written by the indexing model from the issue text.
Description
Prerequisites
- Check that your issue is not already filed:
https://github.com/leanprover/lean4/issues - Reduce the issue to a minimal, self-contained, reproducible test case.
Avoid dependencies to Mathlib or Batteries. - Test your test case against the latest nightly release
(tested on v4.35.0 and v4.36.0-rc2; the relevant code onmasteris unchanged, see below) - When reporting a perceived soundness bug: N/A
Description
Lean.Elab.Tactic.VCGen.reduceHead? does not terminate on a projection function applied to a variable, e.g. S.b x with x : S a free variable.
In the .const branch, a projection function is unfolded with Meta.unfoldDefinition?, which gives the kernel projection x.2. The result is then passed through Sym.shareCommonInc, which (with the default enforceFoldProjs := true) folds x.2 back into S.b x. go is then called on the original term again, forever:
if ← isProjectionFn name then
let some e' ← Meta.unfoldDefinition? (mkAppRev f rargs) | return lastReduction
let e' ← Sym.shareCommonInc e'
go lastReduction e'.getAppFn e'.getAppRevArgs
Context
Reached through vcgen: emitVC → cleanupVC calls reduceHead on both sides of an Eq VC, so a residual VC such as x.b = y.b (or any VC whose head is a projection function of a variable) hangs with (deterministic) timeout at whnf instead of being emitted.
Steps to Reproduce
import Lean
open Lean Meta Elab Tactic VCGen
structure S where
a : Nat
b : Nat
set_option maxHeartbeats 2000 in
#eval show MetaM Unit from do
withLocalDeclD `x (mkConst ``S) fun x => do
let e := mkApp (mkConst ``S.b) x -- `x.b`
let r ← Sym.SymM.run do
reduceHead? (← Sym.shareCommon e)
IO.println s!"{r}"
Expected behavior: none (no head reduction applies, since the projection delta step only counts as progress when followed by a proj-reduction).
Actual behavior:
error: (deterministic) timeout at `transform`, maximum number of heartbeats (2000) has been reached
It keeps running with much larger heartbeat limits as well.
The two steps individually:
set_option maxHeartbeats 2000000 in
#eval show MetaM Unit from do
withLocalDeclD `x (mkConst ``S) fun x => do
let e := mkApp (mkConst ``S.b) x
let u ← unfoldDefinition? e
IO.println s!"unfoldDefinition?: {u}"
let back ← Sym.SymM.run do Sym.shareCommonInc u.get!
IO.println s!"shareCommonInc of that: {back} (same as start: {back == e})"
let r ← Sym.SymM.run <| Sym.withoutShareCommonChecks do reduceHead? e
IO.println s!"reduceHead? without checks: {r}"
unfoldDefinition?: (some _uniq.1074.2)
shareCommonInc of that: S.b _uniq.1074 (same as start: true)
reduceHead? without checks: none
So with the sharing checks disabled the function terminates with none; with the default config the unfold and the fold undo each other.
Versions
Lean (version 4.35.0, arm64-apple-darwin, Release)Lean (version 4.36.0-rc2, arm64-apple-darwin, commit b35cb10cf2ced51a2fd1880cfdbf75c71ab7a35a, Release)- macOS
The .const/isProjectionFn branch of reduceHead? in src/Lean/Elab/Tactic/VCGen/Reduce.lean on current master is the same.
Additional Information
A possible fix: in the projection branch, only continue if the unfolded term actually makes progress (e.g. if proj-reduction of the resulting .proj succeeds), or call reduceProjAndUnfold? on the unfolded .proj before shareCommonInc, returning lastReduction otherwise.
Impact
Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.
🤖 Generated with Claude Code
- Dominant language
- Lean
- Stars
- 10.3k
- Forks
- 1.1k
- Avg merge
- 19h 8m
- Merged PRs (30d)
- 421
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from leanprover/lean4
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
leanprover/lean4#16681 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
leanprover/lean4#16305 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
leanprover/lean4#16299 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
leanprover/lean4#16257 ·
Maintainers usually reply within 1 day
-
`IO.FS.realPath` reports every failure as `noFileOrDirectory` instead of classifying by `errno`Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
leanprover/lean4#15683 ·
Maintainers usually reply within 1 day
All issues in leanprover/lean4
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
swc-project/swc-next-issues#817 ·
-
Magma mode: div:=, mod:= and the other keyword assignments (join:=, meet:=, diff:=, sdiff:=, and:=, or:=, xor:=) are refusedPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
sagemathinc/sagebrush#1683 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
microts: a model local that every branch assigns raises unused_assignments in the generated RustOpenbug
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
pocket-stack/pocketjs#756 ·
-
bot-triaged module: aotinductor module: compile-time module: inductor module: performance module: rocm oncall: export oncall: pt2 triaged
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day