leanprover / leanprover/lean4

`subst` and `simp` reorder hypotheses when rewriting

Open
#14,985 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug P-low
Dominant language
Lean
Stars
9.2k
Forks
990
Avg merge
1d 17h
Merged PRs (30d)
175

Description

Prerequisites
Description

When using subst or simp to rewrite at hypotheses, the order of the hypotheses changes.
The ones who had their type changed move to the bottom.
This can cause strange behavior, including reviving a hypothesis with a shadowed name which makes another hypothesis inaccessible.

Context

#lean4 > `subst` revives old hypotheses

Steps to Reproduce
example {a b : Nat} (ha : a = 37) (hb : b = 37) : b = a := by
  have h : a = 37 := ha
  have h : b = 37 := hb
  subst ha
  exact h

example {a b : Nat} (ha : a = 37) (hb : b = 37) : b = a := by
  have h : a = 37 := ha
  have h : b = 37 := hb
  simp [ha] at *
  exact h

Expected behavior: After subst or simp the order of the hypotheses should stay the same, and the above proofs should succeed.

Actual behavior: Both subst and simp revive the old h, causing the exact to fail. If you comment out the first have, the proofs succeed.

Versions
Lean 4.35.0-nightly-2026-08-31
Target: x86_64-unknown-linux-gnu Linux
Additional Information

rw performs as expected, so perhaps its rewriting logic could be used.

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Contributor guide

Open the contributing guide

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.

Research direction

Start by running the two minimal examples in the issue and compare the hypothesis ordering after subst and simp with the behavior of rw. Trace the rewriting paths for these tactics; done means hypothesis order is preserved and both examples succeed without reviving the shadowed h.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.