Skip to content

loose fvar in split #3229

Closed
leanprover-community/mathlib4
#10328

Description

@digama0
example (b : Bool) :
    (if b then 1 else if b then 1 else 0) = (if b then 1 else 0) := by
  split <;> sorry
(kernel) declaration has free variables '_example'

Observed on leanprover/lean4:v4.5.0-rc1

Activity

  1. added
    bug
    on Jan 30, 2024
  2. self-assigned this
    on Feb 2, 2024
  3. nomeata commented on Feb 2, 2024

    @nomeata
    Collaborator

    Looking into this; partial debugging update:

    This looks like a simp cache error. This is the term:

    _example : ∀ (b : Bool), (if b = true then 1 else if b = true then 1 else 0) = if b = true then 1 else 0 :=
        fun b =>
          Or.casesOn (Classical.em (b = true))
            (fun h =>
              Eq.mpr
                (id
                  (congr
                    (congrArg Eq
                      (Eq.trans (ite_congr (Eq.refl (b = true)) (fun a => Eq.refl 1) fun a => if_pos h) (if_pos h)))
                    (if_pos h)))
                (Eq.refl 1))
            fun h =>
            Eq.mpr
              (id
                (congr
                  (congrArg Eq
                    (Eq.trans (ite_congr (Eq.refl (b = true)) (fun a => Eq.refl 1) fun a => if_neg a) (if_neg h)))
                  (if_neg _uniq.495)))
              (Eq.refl 0) 
    

    Note the if_neg _uniq.495 conspicuously following the if_neg a above.

    Changing the if b then 1 else 0 terms a bit (e.g. changing the numbers) unbreaks it.

    Also writing

    example (b : Bool) :
        (if b then 1 else 0) = (if b then 1 else if b then 1 else 0) := by
    

    where the outer (if b then 1 else 0) is visited first unbreaks it.

    I did not manage to reproduce it with just a call to simp, but I can by exposing simpIfTarget:

    import Lean
    
    open Lean Meta Elab Tactic in
    elab "simp_if" : tactic => do
      withMainContext do
        replaceMainGoal [ ← simpIfTarget (← getMainGoal) ]
    
    set_option trace.Elab.definition true
    
    example (b : Bool) (h : ¬ (b = true)) :
         (if b then 1 else if b then 1 else 0) = (if b then 1 else 0) := by
      simp_if 
      rfl
    
  4. nomeata commented on Feb 2, 2024

    @nomeata
    Collaborator

    Could this code be blamed:

    /--
      Default `discharge?` function for `simpIf` methods.
      It only uses hypotheses from the local context. It is effective
      after a case-split. -/
    def discharge? (useDecide := false) : Simp.Discharge := fun prop => do
      let prop ← instantiateMVars prop
      trace[Meta.Tactic.splitIf] "discharge? {prop}, {prop.notNot?}"
      if useDecide then …
      (← getLCtx).findDeclRevM? fun localDecl => do
         if 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
    

    It seems that caching based on expressions is inherently incompatible with this kind of “peeking at the context”.

    A blunt fix would be to set

        let ctx := { ctx with config := { ctx.config with memoize := false }}
    

    in SplitIf.getSimpContext.

    A more careful fix would be to let a the discharger method to set cache := false, and have that propagate to the Simp.Result.

    Although I probably don’t have the full picture yet, given that dischargeUsingAssumption? exists as well…

    So maybe some dischargeDepth bump is missing?

    (BTW, this is not a regression; this bug existed for at least a year.)

  5. removed their assignment
    on Feb 2, 2024
  6. added a commit that references this issue on Feb 5, 2024
    73a0a3e
  7. added a commit that references this issue on Feb 6, 2024
    17520fa
  8. added a commit that references this issue on Feb 7, 2024
    99a46af
  9. added a commit that references this issue on Feb 9, 2024
    3911495
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions