VCGen `reduceHead?` loops on a projection function applied to a variable

Open
#16,416 0 comments 0 reactions 0 assignees View on GitHub

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 on master is 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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from leanprover/lean4

All issues in leanprover/lean4

Similar issues

More Compilers issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.