Skip to content

Commit 9f0aee2

Browse files
committed
fix(RingNF): use assoc as a pre lemma (leanprover-community#36947)
This PR was originally meant as a performance improvement, but now serves as a bug fix. The `cleanup` function used by `ring_nf` used to be non-confluent due to using both `add_assoc_rev` and `add_neg`, and it would apply them in the wrong order. This PR fixes that problem by putting `add_assoc_rev` in the pre-simp-set, and keeping `add_neg` in the post-simp-set. This affects a few proofs that needed to be fixed. When applied to big expressions, associativity lemmas are more efficient as pre lemmas than as post lemmas: as post lemmas, the number of needed rewrites is quadratic instead of linear. This PR applies this idea to the `ring_nf` cleanup function.
1 parent 0e301e8 commit 9f0aee2

5 files changed

Lines changed: 19 additions & 11 deletions

File tree

Mathlib/Analysis/SpecialFunctions/BinaryEntropy.lean

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,6 @@ lemma qaryEntropy_strictAntiOn (qLe2 : 2 ≤ q) :
411411
linarith
412412
· have qpos : 0 < (q : ℝ) := by positivity
413413
ring_nf
414-
simp only [add_lt_iff_neg_right, neg_add_lt_iff_lt_add, add_zero, gt_iff_lt]
415414
have : (q : ℝ) - 1 < p * q := by
416415
have h1 := mul_lt_mul_of_pos_right hp.1 qpos
417416
have h2 : (1 - (q : ℝ)⁻¹) * ↑q = q - 1 := by calc (1 - (q : ℝ)⁻¹) * ↑q

Mathlib/GroupTheory/SpecificGroups/Quaternion.lean

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,9 @@ instance : Group (QuaternionGroup n) where
9393
mul := mul
9494
mul_assoc := by
9595
rintro (i | i) (j | j) (k | k) <;> simp only [(· * ·), mul] <;> ring_nf
96-
congr
97-
calc
98-
-(n : ZMod (2 * n)) = 0 - n := by rw [zero_sub]
99-
_ = 2 * n - n := by norm_cast; simp
100-
_ = n := by ring
96+
have : (2 * n : ZMod (2 * n)) = 0 := by norm_cast; simp
97+
simp only [Mul.mul_eq_hMul]
98+
grind
10199
one := one
102100
one_mul := by
103101
rintro (i | i)

Mathlib/NumberTheory/LSeries/HurwitzZetaEven.lean

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ lemma hasSum_int_evenKernel (a : ℝ) {t : ℝ} (ht : 0 < t) :
166166
have (n : ℤ) : cexp (-(π * (n + a) ^ 2 * t)) = cexp (-(π * a ^ 2 * t)) *
167167
jacobiTheta₂_term n (a * I * t) (I * t) := by
168168
rw [jacobiTheta₂_term, ← Complex.exp_add]
169-
ring_nf
170-
simp
169+
grind [I_sq]
171170
simpa [this] using (hasSum_jacobiTheta₂_term _ (by simpa)).mul_left _
172171

173172
lemma hasSum_int_cosKernel (a : ℝ) {t : ℝ} (ht : 0 < t) :

Mathlib/Tactic/Ring/RingNF.lean

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ def cleanup (cfg : RingNF.Config) (r : Simp.Result) : MetaM Simp.Result := do
114114
| .raw => pure r
115115
| .SOP => do
116116
let thms : SimpTheorems := {}
117-
let thms ← [``add_zero, ``add_assoc_rev, ``_root_.mul_one, ``mul_assoc_rev,
118-
``_root_.pow_one, ``mul_neg, ``add_neg].foldlM (·.addConst ·) thms
117+
let thms ← [``add_zero, ``_root_.mul_one, ``_root_.pow_one, ``mul_neg, ``add_neg
118+
].foldlM (·.addConst ·) thms
119119
let thms ← [``nat_rawCast_0, ``nat_rawCast_1, ``nat_rawCast_2, ``int_rawCast_neg,
120-
``nnrat_rawCast, ``rat_rawCast_neg].foldlM (·.addConst · (post := false)) thms
120+
``nnrat_rawCast, ``rat_rawCast_neg, ``add_assoc_rev, ``mul_assoc_rev
121+
].foldlM (·.addConst · (post := false)) thms
121122
let ctx ← Simp.mkContext { zetaDelta := cfg.zetaDelta }
122123
(simpTheorems := #[thms])
123124
(congrTheorems := ← getSimpCongrTheorems)

MathlibTest/ring.lean

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,14 @@ info: 'test_axioms' depends on axioms: [propext]
275275
-/
276276
#guard_msgs in
277277
#print axioms test_axioms
278+
279+
-- Test that the normalization of `ring_nf` does the right thing
280+
/--
281+
trace: a b c d : ℝ
282+
⊢ a - b - c - d = 0
283+
-/
284+
#guard_msgs in
285+
example (a b c d : ℝ) : a - b - c - d = 0 := by
286+
ring_nf
287+
trace_state
288+
exact test_sorry

0 commit comments

Comments
 (0)