You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using split on if-then-else simplifies unrelated nested if-then-else #3245
This is rather minor, but I stumbled over this while debugging #3229, and I’d like to record it as an instance of a non-finishing tactic doing possibly unexpected things on the side.
Consider this example:
example (b1 b2 : Bool) :
(match b1 with
| true => 0
| false => (if b2 then 0 else if b2 then 42 else 0)) = 0 := by
split
rfl
Now the goal is, as expected
b2 b1✝: Bool
⊢ (if b2 = true then 0 else if b2 = true then 42 else 0) = 0
The split tactic split the match, and leaves the branch in place.
Using if-then-else however:
example (b1 b2 : Bool) :
(if b1
then 0
else (if b2 then 0 else if b2 then 42 else 0)) = 0 := by
split
rfl
Note that suddenly, the totally unrelated nested if b2 was reduced.
The problem lies is with Lean.Meta.simpIfTarget, which
uses the if-then-else congruence theorems and
a discharger that looks into the hypotheses.
I wonder if it really needs to enable ite_congr – after all, for the if-then-else that was split the condition is already in the context, so no need for the “better” congruence rule and the standard rule should suffice?
I wonder if it really needs to enable ite_congr – after all, for the if-then-else that was split the condition is already in the context, so no need for the “better” congruence rule and the standard rule should suffice?
This is barking up the wrong tree, and seems to have incidentially be fixed by #3258.
But there is still a variant of the issue: After
example (b1 b2 : Bool) (h : b2 = true) :
(if b1 then 0 else (if b2 then 0 else if b2 then 42 else 0)) = 0 := by
split
maybe the discharger should be told exactly which assumption was introduced by split and use only that.
I was curious and had a close look
Details
commit fd4a0601a23082358628d1be80b40982195743a4
Author: Joachim Breitner <[email protected]>
Date: Wed Jul 3 16:16:29 2024 +0200
Try to fix #3245
but the `simpIf` machinery is used elsewhere, with less easy access to
the newly introduced hy
diff --git a/src/Lean/Meta/Tactic/SplitIf.lean b/src/Lean/Meta/Tactic/SplitIf.lean
index 32d954df6f..f7d1433bcb 100644
--- a/src/Lean/Meta/Tactic/SplitIf.lean+++ b/src/Lean/Meta/Tactic/SplitIf.lean@@ -32,17 +32,15 @@ def getSimpContext : MetaM Simp.Context :=
/--
Default `discharge?` function for `simpIf` methods.
- It only uses hypotheses from the local context that have `index < numIndices`.- It is effective after a case-split.- Remark: when `simp` goes inside binders it adds new local declarations to the- local context. We don't want to use these local declarations since the cached result- would depend on them (see issue #3229). `numIndices` is the size of the local- context `decls` field before we start the simplifying the expression.+ Uses the given hypothesis to clean up `ite` applications. It is effective after a case-split.++ It used to search the local context for any applicable hypothesis, but this led to+ issues with the simp cache after going under binder (#3229) and can cause unrelated+ `ite` applications to be simplified (#3245) or simplified the wrong way (#4491).- Remark: this function is now private, and we should use `mkDischarge?`.
-/
-private def discharge? (numIndices : Nat) (useDecide : Bool) : Simp.Discharge := fun prop => do+def mkDischarge? (hyp : Expr) (useDecide : Bool := false) : Simp.Discharge := fun prop => do
let prop ← instantiateMVars prop
trace[Meta.Tactic.splitIf] "discharge? {prop}, {prop.notNot?}"
if useDecide then
@@ -52,21 +50,16 @@ private def discharge? (numIndices : Nat) (useDecide : Bool) : Simp.Discharge :=
let r ← withDefault <| whnf d
if r.isConstOf ``true then
return some <| mkApp3 (mkConst ``of_decide_eq_true) prop d.appArg! (← mkEqRefl (mkConst ``true))
- (← getLCtx).findDeclRevM? fun localDecl => do- if localDecl.index ≥ numIndices || localDecl.isAuxDecl then- return none- else if (← isDefEq prop localDecl.type) then- return some localDecl.toExpr- else match prop.notNot? with- | none => return none- | some arg =>- if (← isDefEq arg localDecl.type) then- return some (mkApp2 (mkConst ``not_not_intro) arg localDecl.toExpr)- else- return none--def mkDischarge? (useDecide := false) : MetaM Simp.Discharge :=- return discharge? (← getLCtx).numIndices useDecide+ let hypType ← inferType hyp+ if (← isDefEq prop hypType) then+ return some hyp+ else match prop.notNot? with+ | none => return none+ | some arg =>+ if (← isDefEq arg hypType) then+ return some (mkApp2 (mkConst ``not_not_intro) arg hyp)+ else+ return none
/-- Return the condition and decidable instance of an `if` expression to case split. -/
private partial def findIfToSplit? (e : Expr) : Option (Expr × Expr) :=
@@ -93,24 +86,24 @@ end SplitIf
open SplitIf
-def simpIfTarget (mvarId : MVarId) (useDecide := false) : MetaM MVarId := do+def simpIfTarget (mvarId : MVarId) (hyp : FVarId) (useDecide := false) : MetaM MVarId := do
let mut ctx ← getSimpContext
- if let (some mvarId', _) ← simpTarget mvarId ctx {} (← mvarId.withContext <| mkDischarge? useDecide) (mayCloseGoal := false) then+ if let (some mvarId', _) ← simpTarget mvarId ctx {} (mkDischarge? (.fvar hyp) useDecide) (mayCloseGoal := false) then
return mvarId'
else
unreachable!
-def simpIfLocalDecl (mvarId : MVarId) (fvarId : FVarId) : MetaM MVarId := do+def simpIfLocalDecl (mvarId : MVarId) (hyp : FVarId) : MetaM MVarId := do
let mut ctx ← getSimpContext
- if let (some (_, mvarId'), _) ← simpLocalDecl mvarId fvarId ctx {} (← mvarId.withContext <| mkDischarge?) (mayCloseGoal := false) then+ if let (some (_, mvarId'), _) ← simpLocalDecl mvarId hyp ctx {} (mkDischarge? (.fvar hyp)) (mayCloseGoal := false) then
return mvarId'
else
unreachable!
def splitIfTarget? (mvarId : MVarId) (hName? : Option Name := none) : MetaM (Option (ByCasesSubgoal × ByCasesSubgoal)) := commitWhenSome? do
if let some (s₁, s₂) ← splitIfAt? mvarId (← mvarId.getType) hName? then
- let mvarId₁ ← simpIfTarget s₁.mvarId- let mvarId₂ ← simpIfTarget s₂.mvarId+ let mvarId₁ ← simpIfTarget s₁.mvarId s₁.fvarId+ let mvarId₂ ← simpIfTarget s₂.mvarId s₂.fvarId
if s₁.mvarId == mvarId₁ && s₂.mvarId == mvarId₂ then
return none
else
@@ -121,8 +114,8 @@ def splitIfTarget? (mvarId : MVarId) (hName? : Option Name := none) : MetaM (Opt
def splitIfLocalDecl? (mvarId : MVarId) (fvarId : FVarId) (hName? : Option Name := none) : MetaM (Option (MVarId × MVarId)) := commitWhenSome? do
mvarId.withContext do
if let some (s₁, s₂) ← splitIfAt? mvarId (← inferType (mkFVar fvarId)) hName? then
- let mvarId₁ ← simpIfLocalDecl s₁.mvarId fvarId- let mvarId₂ ← simpIfLocalDecl s₂.mvarId fvarId+ let mvarId₁ ← simpIfLocalDecl s₁.mvarId s₁.fvarId+ let mvarId₂ ← simpIfLocalDecl s₂.mvarId s₂.fvarId
if s₁.mvarId == mvarId₁ && s₂.mvarId == mvarId₂ then
return none
else
But it turns out that the simpIf and simpIfTarget are used outside the splitIf machinery, and there it’s not as straight-forward to identify and pass down the hyps to consider. Shelving this again until some other time; the issue isn’t severe.
Activity
kim-em commentedon Feb 4, 2024
I realise that I've encountered this several time.
experiment: try out proposed fix for #3245
experiment: try out proposed fix for #3245
nomeata commentedon Apr 8, 2024
This is barking up the wrong tree, and seems to have incidentially be fixed by #3258.
But there is still a variant of the issue: After
we get
and arguably the user did not expect any changes to
if b2⋅So instead of
maybe the discharger should be told exactly which assumption was introduced by
splitand use only that.(Just noting down these thoughs, I am not sure if the issue here has any real impact.)
nomeata commentedon Jul 3, 2024
I was curious and had a close look
Details
But it turns out that the
simpIfandsimpIfTargetare used outside thesplitIfmachinery, and there it’s not as straight-forward to identify and pass down the hyps to consider. Shelving this again until some other time; the issue isn’t severe.keithadler commentedon Sep 4, 2026
No longer reproduces on
4.35.0-nightly-2026-09-04or stable4.33.1. The unrelated nestedif b2is now preserved:The reported behaviour was the
42branch being reduced away. Suggest closing.Triage assisted by Claude; repros run and checked manually.