Skip to content

Using split on if-then-else simplifies unrelated nested if-then-else #3245

Open

Description

@nomeata

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

the goal becomes

b1b2: Bool
h✝: ¬b1 = true
⊢ (if b2 = true then 0 else 0) = 0

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?

Versions

v4.5.0

Impact

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

Activity

  1. added
    bug
    on Feb 2, 2024
  2. kim-em commented on Feb 4, 2024

    @kim-em
    Collaborator

    I realise that I've encountered this several time.

  3. nomeata commented on Apr 8, 2024

    @nomeata
    CollaboratorAuthor

    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
    

    we get

    2 goals
    case inl
    b1 b2 : Bool
    h : b2 = true
    h✝ : b1 = true
    ⊢ 0 = 0
    
    case inr
    b1 b2 : Bool
    h : b2 = true
    h✝ : ¬b1 = true
    ⊢ 0 = 0
    

    and arguably the user did not expect any changes to if b2⋅

    So instead of

    private def discharge? (numIndices : Nat) (useDecide : Bool) : Simp.Discharge := fun prop => do
    …
         if localDecl.index ≥ numIndices || localDecl.isAuxDecl then
    

    maybe the discharger should be told exactly which assumption was introduced by split and use only that.

    (Just noting down these thoughs, I am not sure if the issue here has any real impact.)

  4. nomeata commented on Jul 3, 2024

    @nomeata
    CollaboratorAuthor

    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.

  5. added
    P-low
    on Aug 9, 2024
  6. keithadler commented on Sep 4, 2026

    @keithadler

    No longer reproduces on 4.35.0-nightly-2026-09-04 or stable 4.33.1. The unrelated nested if b2 is now preserved:

    case isFalse
    b1 b2 : Bool
    h✝ : ¬b1 = true
    ⊢ (if b2 = true then 0 else if b2 = true then 42 else 0) = 0
    

    The reported behaviour was the 42 branch being reduced away. Suggest closing.

    Triage assisted by Claude; repros run and checked manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P-lowbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions