Skip to content

Commit 41d2ced

Browse files
plp127Bjorn Wehlin
authored andcommitted
feat: have ring_nf fail if no progress (leanprover-community#24529)
Makes `ring_nf` fail if no progress is made, and adds a new option `failIfUnchanged` (default value := `true`) to the config. Motivated by stanford-centaur/PyPantograph#97. The unusedTactic linter is not sufficient here because it fails in the presence of certain errors (for example, `unsolved goals`).
1 parent 06fd3c3 commit 41d2ced

4 files changed

Lines changed: 20 additions & 8 deletions

File tree

Mathlib/NumberTheory/Padics/PadicNumbers.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ theorem exi_rat_seq_conv_cauchy : IsCauSeq (padicNorm p) (limSeq f) := fun ε h
647647
intro j hj
648648
suffices
649649
padicNormE (limSeq f j - f (max N N2) + (f (max N N2) - limSeq f (max N N2)) : ℚ_[p]) < ε by
650-
ring_nf at this
650+
ring_nf at this
651651
rw [← padicNormE.eq_padic_norm']
652652
exact mod_cast this
653653
apply lt_of_le_of_lt

Mathlib/Tactic/Group.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ syntax (name := aux_group₂) "aux_group₂" (location)? : tactic
6464

6565
macro_rules
6666
| `(tactic| aux_group₂ $[at $location]?) =>
67-
`(tactic| ring_nf $[at $location]?)
67+
`(tactic| ring_nf -failIfUnchanged $[at $location]?)
6868

6969
/-- Tactic for normalizing expressions in multiplicative groups, without assuming
7070
commutativity, using only the group axioms without any information about which group

Mathlib/Tactic/Ring/RingNF.lean

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ structure Config where
6464
zetaDelta := false
6565
/-- if true, atoms inside ring expressions will be reduced recursively -/
6666
recursive := true
67+
/-- if true, then fail if no progess is made -/
68+
failIfUnchanged := true
6769
/-- The normalization style. -/
6870
mode := RingMode.SOP
6971
deriving Inhabited, BEq, Repr
@@ -177,7 +179,10 @@ def ringNFTarget (s : IO.Ref AtomM.State) (cfg : Config) : TacticM Unit := withM
177179
goal.assign (← mkOfEqTrue (← r.getProof))
178180
replaceMainGoal []
179181
else
180-
replaceMainGoal [← applySimpResultToTarget goal tgt r]
182+
let newGoal ← applySimpResultToTarget goal tgt r
183+
if cfg.failIfUnchanged && goal == newGoal then
184+
throwError "ring_nf made no progress"
185+
replaceMainGoal [newGoal]
181186

182187
/-- Use `ring_nf` to rewrite hypothesis `h`. -/
183188
def ringNFLocalDecl (s : IO.Ref AtomM.State) (cfg : Config) (fvarId : FVarId) :
@@ -187,7 +192,10 @@ def ringNFLocalDecl (s : IO.Ref AtomM.State) (cfg : Config) (fvarId : FVarId) :
187192
let myres ← M.run s cfg <| rewrite tgt
188193
match ← applySimpResultToLocalDecl goal fvarId myres false with
189194
| none => replaceMainGoal []
190-
| some (_, newGoal) => replaceMainGoal [newGoal]
195+
| some (_, newGoal) =>
196+
if cfg.failIfUnchanged && goal == newGoal then
197+
throwError "ring_nf made no progress"
198+
replaceMainGoal [newGoal]
191199

192200
/--
193201
Simplification tactic for expressions in the language of commutative (semi)rings,

MathlibTest/ring.lean

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,17 +210,21 @@ end
210210
set_option linter.unusedTactic false in
211211
example (x : ℝ) (f : ℝ → ℝ) : True := by
212212
let y := x
213+
/-
214+
Two of these fail, and two of these succeed in rewriting the instance, so it's not a good idea
215+
to use `fail_if_success` since the instances could change without warning.
216+
-/
213217
have : x = y := by
214-
ring_nf
218+
ring_nf -failIfUnchanged
215219
ring_nf!
216220
have : x - y = 0 := by
217-
ring_nf
221+
ring_nf -failIfUnchanged
218222
ring_nf!
219223
have : f x = f y := by
220-
ring_nf
224+
ring_nf -failIfUnchanged
221225
ring_nf!
222226
have : f x - f y = 0 := by
223-
ring_nf
227+
ring_nf -failIfUnchanged
224228
ring_nf!
225229
trivial
226230

0 commit comments

Comments
 (0)