Skip to content

Commit c3d31cf

Browse files
authored
feat: helper theorems for equality detection and coefficent normalization (#8650)
This PR adds helper theorems for coefficient normalization and equality detection. This theorems are for the linear arithmetic procedure in `grind`.
1 parent f7ecf06 commit c3d31cf

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

src/Init/Grind/Ordered/Linarith.lean

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ theorem lt_eq_combine {α} [IntModule α] [Preorder α] [IntModule.IsOrdered α]
311311
replace h₁ := hmul_neg (↑p₂.leadCoeff.natAbs) h₁ |>.mp hp₁
312312
assumption
313313

314-
theorem eq_eq_combine {α} [IntModule α] [Preorder α] [IntModule.IsOrdered α] (ctx : Context α) (p₁ p₂ p₃ : Poly)
314+
theorem eq_eq_combine {α} [IntModule α] (ctx : Context α) (p₁ p₂ p₃ : Poly)
315315
: le_le_combine_cert p₁ p₂ p₃ → p₁.denote' ctx = 0 → p₂.denote' ctx = 0 → p₃.denote' ctx = 0 := by
316316
simp [le_le_combine_cert]; intro _ h₁ h₂; subst p₃; simp [h₁, h₂]
317317

@@ -415,6 +415,21 @@ theorem not_lt_norm {α} [IntModule α] [LinearOrder α] [IntModule.IsOrdered α
415415
simp [← sub_eq_add_neg, sub_self] at h₁
416416
assumption
417417

418+
/-!
419+
Equality detection
420+
-/
421+
def eq_of_le_ge_cert (p₁ p₂ : Poly) : Bool :=
422+
p₂ == p₁.mul (-1)
423+
424+
theorem eq_of_le_ge {α} [IntModule α] [PartialOrder α] [IntModule.IsOrdered α] (ctx : Context α) (p₁ : Poly) (p₂ : Poly)
425+
: eq_of_le_ge_cert p₁ p₂ → p₁.denote' ctx ≤ 0 → p₂.denote' ctx ≤ 0 → p₁.denote' ctx = 0 := by
426+
simp [eq_of_le_ge_cert]
427+
intro; subst p₂; simp
428+
intro h₁ h₂
429+
replace h₂ := add_le_left h₂ (p₁.denote ctx)
430+
rw [add_comm, neg_hmul, one_hmul, ← sub_eq_add_neg, sub_self, zero_add] at h₂
431+
exact PartialOrder.le_antisymm h₁ h₂
432+
418433
/-!
419434
Helper theorems for closing the goal
420435
-/
@@ -427,6 +442,34 @@ theorem lt_unsat {α} [IntModule α] [Preorder α] (ctx : Context α) : (Poly.ni
427442
have := Preorder.lt_iff_le_not_le.mp h
428443
simp at this
429444

430-
-- TODO: support for the special variable representing (1 : α). Example: `3 * (1 : α) ≤ 0`
445+
/-!
446+
Coefficient normalization
447+
-/
448+
449+
def coeff_cert (p₁ p₂ : Poly) (k : Nat) :=
450+
k > 0 && p₁ == p₂.mul k
451+
452+
theorem eq_coeff {α} [IntModule α] [NoNatZeroDivisors α] (ctx : Context α) (p₁ p₂ : Poly) (k : Nat)
453+
: coeff_cert p₁ p₂ k → p₁.denote ctx = 0 → p₂.denote ctx = 0 := by
454+
simp [coeff_cert]; intro h _; subst p₁; simp
455+
exact no_nat_zero_divisors k (p₂.denote ctx) (Nat.ne_zero_of_lt h)
456+
457+
theorem le_coeff {α} [IntModule α] [LinearOrder α] [IntModule.IsOrdered α] (ctx : Context α) (p₁ p₂ : Poly) (k : Nat)
458+
: coeff_cert p₁ p₂ k → p₁.denote ctx ≤ 0 → p₂.denote ctx ≤ 0 := by
459+
simp [coeff_cert]; intro h _; subst p₁; simp
460+
have : ↑k > (0 : Int) := Int.natCast_pos.mpr h
461+
intro h₁; apply Classical.byContradiction
462+
intro h₂; replace h₂ := lt_of_not_le h₂
463+
replace h₂ := IsOrdered.hmul_pos (↑k) h₂ |>.mp this
464+
exact Preorder.lt_irrefl 0 (Preorder.lt_of_lt_of_le h₂ h₁)
465+
466+
theorem lt_coeff {α} [IntModule α] [LinearOrder α] [IntModule.IsOrdered α] (ctx : Context α) (p₁ p₂ : Poly) (k : Nat)
467+
: coeff_cert p₁ p₂ k → p₁.denote ctx < 0 → p₂.denote ctx < 0 := by
468+
simp [coeff_cert]; intro h _; subst p₁; simp
469+
have : ↑k > (0 : Int) := Int.natCast_pos.mpr h
470+
intro h₁; apply Classical.byContradiction
471+
intro h₂; replace h₂ := le_of_not_lt h₂
472+
replace h₂ := IsOrdered.hmul_nonneg (Int.le_of_lt this) h₂
473+
exact Preorder.lt_irrefl 0 (Preorder.lt_of_le_of_lt h₂ h₁)
431474

432475
end Lean.Grind.Linarith

0 commit comments

Comments
 (0)