This PR allows `simp` to recognize and warn about simp lemmas that are likely looping in the current simp set. It does so automatically whenever simplification fails with the dreaded “max recursion depth” error fails, but it can be made to do it always with `set_option linter.loopingSimpArgs true`. This check is not on by default because it is somewhat costly, and can warn about simp calls that still happen to work. This closes #5111. In the end, this implemented much simpler logic than described there (and tried in the abandoned #8688; see that PR description for more background information), but it didn’t work as well as I thought. The current logic is: “Simplify the RHS of the simp theorem, complain if that fails”. It is a reasonable policy for a Lean project to say that all simp invocation should be so that this linter does not complain. Often it is just a matter of explicitly disabling some simp theorems from the default simp set, to make it clear and robust that in this call, we do not want them to trigger. But given that often such simp call happen to work, it’s too pedantic to impose it on everyone.
127 lines
3.1 KiB
Text
127 lines
3.1 KiB
Text
axiom testSorry : α
|
||
|
||
theorem le_of_not_lt {a b : Nat} (_: ¬ a < b): b ≤ a := testSorry
|
||
theorem lt_of_succ_lt (_: a + 1 < b): a < b := testSorry
|
||
theorem succ_pred_eq_of_pos (_: 0 < v): v - 1 + 1 = v := testSorry
|
||
|
||
set_option trace.Meta.Tactic.simp true
|
||
set_option linter.unusedSimpArgs false
|
||
--set_option trace.Debug.Meta.Tactic.simp true
|
||
|
||
set_option Elab.async false -- for stable message ordering in #guard_msgs
|
||
|
||
/--
|
||
warning: declaration uses 'sorry'
|
||
---
|
||
trace: [Meta.Tactic.simp.rewrite] h₁:1000:
|
||
k ≤ v - 1
|
||
==>
|
||
True
|
||
[Meta.Tactic.simp.discharge] succ_pred_eq_of_pos discharge ✅️
|
||
0 < v
|
||
[Meta.Tactic.simp.rewrite] h₂:1000:
|
||
0 < v
|
||
==>
|
||
True
|
||
[Meta.Tactic.simp.rewrite] succ_pred_eq_of_pos:1000:
|
||
v - 1 + 1
|
||
==>
|
||
v
|
||
[Meta.Tactic.simp.rewrite] ite_true:1000:
|
||
if True then ⟨v, ⋯⟩ else ⟨v - 1, ⋯⟩
|
||
==>
|
||
⟨v, ⋯⟩
|
||
[Meta.Tactic.simp.rewrite] eq_self:1000:
|
||
⟨v, ⋯⟩ = ⟨v, ⋯⟩
|
||
==>
|
||
True
|
||
-/
|
||
#guard_msgs in
|
||
example (h₁: k ≤ v - 1) (h₂: 0 < v):
|
||
(if k ≤ v - 1 then Fin.mk (v-1+1) sorry else Fin.mk (v-1) sorry) = Fin.mk v sorry (n:=n) := by
|
||
simp only [
|
||
h₁, h₂,
|
||
ite_true,
|
||
succ_pred_eq_of_pos
|
||
----------------
|
||
, le_of_not_lt
|
||
, lt_of_succ_lt
|
||
]
|
||
|
||
-- it works
|
||
|
||
/--
|
||
warning: declaration uses 'sorry'
|
||
---
|
||
trace: [Meta.Tactic.simp.rewrite] h₁:1000:
|
||
k ≤ v - 1
|
||
==>
|
||
True
|
||
[Meta.Tactic.simp.discharge] succ_pred_eq_of_pos discharge ✅️
|
||
0 < v
|
||
[Meta.Tactic.simp.rewrite] h₂:1000:
|
||
0 < v
|
||
==>
|
||
True
|
||
[Meta.Tactic.simp.rewrite] succ_pred_eq_of_pos:1000:
|
||
v - 1 + 1
|
||
==>
|
||
v
|
||
[Meta.Tactic.simp.rewrite] ite_true:1000:
|
||
if True then ⟨v, ⋯⟩ else ⟨v - 1, ⋯⟩
|
||
==>
|
||
⟨v, ⋯⟩
|
||
[Meta.Tactic.simp.rewrite] eq_self:1000:
|
||
⟨v, ⋯⟩ = ⟨v, ⋯⟩
|
||
==>
|
||
True
|
||
-/
|
||
#guard_msgs in
|
||
example (h₁: k ≤ v - 1) (h₂: 0 < v):
|
||
(if k ≤ v - 1 then Fin.mk (v-1+1) sorry else Fin.mk (v-1) sorry) = Fin.mk v sorry (n:=n) := by
|
||
simp (config := { memoize := false}) only [
|
||
h₁, h₂,
|
||
ite_true,
|
||
succ_pred_eq_of_pos
|
||
----------------
|
||
, le_of_not_lt
|
||
, lt_of_succ_lt
|
||
]
|
||
|
||
/--
|
||
warning: declaration uses 'sorry'
|
||
---
|
||
trace: [Meta.Tactic.simp.rewrite] h₁:1000:
|
||
k ≤ v - 1
|
||
==>
|
||
True
|
||
[Meta.Tactic.simp.discharge] succ_pred_eq_of_pos discharge ✅️
|
||
0 < v
|
||
[Meta.Tactic.simp.rewrite] h₂:1000:
|
||
0 < v
|
||
==>
|
||
True
|
||
[Meta.Tactic.simp.rewrite] succ_pred_eq_of_pos:1000:
|
||
v - 1 + 1
|
||
==>
|
||
v
|
||
[Meta.Tactic.simp.rewrite] ite_true:1000:
|
||
if True then ⟨v, ⋯⟩ else ⟨v - 1, ⋯⟩
|
||
==>
|
||
⟨v, ⋯⟩
|
||
[Meta.Tactic.simp.rewrite] eq_self:1000:
|
||
⟨v, ⋯⟩ = ⟨v, ⋯⟩
|
||
==>
|
||
True
|
||
-/
|
||
#guard_msgs in
|
||
example (h₁: k ≤ v - 1) (h₂: 0 < v):
|
||
(if k ≤ v - 1 then Fin.mk (v-1+1) sorry else Fin.mk (v-1) sorry) = Fin.mk v sorry (n:=n) := by
|
||
simp only [
|
||
h₁, h₂,
|
||
ite_true,
|
||
succ_pred_eq_of_pos
|
||
----------------
|
||
--, le_of_not_lt
|
||
--, lt_of_succ_lt
|
||
]
|