Skip to content

Commit 12b8498

Browse files
committed
initial commit
1 parent c28b052 commit 12b8498

File tree

5 files changed

+164
-19
lines changed

5 files changed

+164
-19
lines changed

src/Init/Grind/CommRing.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ import Init.Grind.CommRing.Fin
1414
import Init.Grind.CommRing.BitVec
1515
import Init.Grind.CommRing.Poly
1616
import Init.Grind.CommRing.Field
17+
import Init.Grind.CommRing.Ordered

src/Init/Grind/CommRing/Basic.lean

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,6 @@ instance : IntModule α where
274274
hmul_zero := by simp [mul_zero]
275275
hmul_add := by simp [left_distrib]
276276
mul_hmul := by simp [intCast_mul, mul_assoc]
277-
neg_hmul := by simp [intCast_neg, neg_mul]
278277
neg_add_cancel := by simp [neg_add_cancel]
279278
sub_eq_add_neg := by simp [sub_eq_add_neg]
280279

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/-
2+
Copyright (c) 2025 Lean FRO, LLC. or its affiliates. All Rights Reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Kim Morrison
5+
-/
6+
module
7+
8+
prelude
9+
import Init.Grind.CommRing.Basic
10+
11+
namespace Lean.Grind
12+
13+
class Ring.IsOrdered (R : Type u) [Ring R] [Preorder R] extends IntModule.IsOrdered R where
14+
/-- In a strict ordered semiring, we have `0 < 1`. -/
15+
zero_lt_one : 0 < 1
16+
/-- In a strict ordered semiring, we can multiply an inequality `a < b` on the left
17+
by a positive element `0 < c` to obtain `c * a < c * b`. -/
18+
protected mul_lt_mul_of_pos_left : ∀ a b c : R, a < b → 0 < c → c * a < c * b
19+
/-- In a strict ordered semiring, we can multiply an inequality `a < b` on the right
20+
by a positive element `0 < c` to obtain `a * c < b * c`. -/
21+
protected mul_lt_mul_of_pos_right : ∀ a b c : R, a < b → 0 < c → a * c < b * c
22+
23+
namespace Ring.IsOrdered
24+
25+
end Ring.IsOrdered
26+
27+
end Lean.Grind

src/Init/Grind/Module/Basic.lean

Lines changed: 133 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,161 @@ class IntModule (M : Type u) extends Zero M, Add M, Neg M, Sub M, HMul Int M M w
3232
zero_hmul : ∀ a : M, (0 : Int) * a = 0
3333
one_hmul : ∀ a : M, (1 : Int) * a = a
3434
add_hmul : ∀ n m : Int, ∀ a : M, (n + m) * a = n * a + m * a
35-
neg_hmul : ∀ n : Int, ∀ a : M, (-n) * a = - (n * a)
3635
hmul_zero : ∀ n : Int, n * (0 : M) = 0
3736
hmul_add : ∀ n : Int, ∀ a b : M, n * (a + b) = n * a + n * b
3837
mul_hmul : ∀ n m : Int, ∀ a : M, (n * m) * a = n * (m * a)
3938
neg_add_cancel : ∀ a : M, -a + a = 0
4039
sub_eq_add_neg : ∀ a b : M, a - b = a + -b
4140

41+
namespace IntModule
42+
4243
attribute [instance 100] IntModule.toZero IntModule.toAdd IntModule.toNeg IntModule.toSub IntModule.toHMul
4344

44-
instance IntModule.toNatModule (M : Type u) [i : IntModule M] : NatModule M :=
45+
instance toNatModule (M : Type u) [i : IntModule M] : NatModule M :=
4546
{ i with
4647
hMul a x := (a : Int) * x
4748
hmul_zero := by simp [IntModule.hmul_zero]
4849
add_hmul := by simp [IntModule.add_hmul]
4950
hmul_add := by simp [IntModule.hmul_add]
5051
mul_hmul := by simp [IntModule.mul_hmul] }
5152

53+
variable {M : Type u} [IntModule M]
54+
55+
theorem add_neg_cancel (a : M) : a + -a = 0 := by
56+
rw [add_comm, neg_add_cancel]
57+
58+
theorem add_left_inj {a b : M} (c : M) : a + c = b + c ↔ a = b :=
59+
fun h => by simpa [add_assoc, add_neg_cancel, add_zero] using (congrArg (· + -c) h),
60+
fun g => congrArg (· + c) g⟩
61+
62+
theorem add_right_inj (a b c : M) : a + b = a + c ↔ b = c := by
63+
rw [add_comm a b, add_comm a c, add_left_inj]
64+
65+
theorem neg_zero : (-0 : M) = 0 := by
66+
rw [← add_left_inj 0, neg_add_cancel, add_zero]
67+
68+
theorem neg_neg (a : M) : -(-a) = a := by
69+
rw [← add_left_inj (-a), neg_add_cancel, add_neg_cancel]
70+
71+
theorem neg_eq_zero (a : M) : -a = 0 ↔ a = 0 :=
72+
fun h => by
73+
replace h := congrArg (-·) h
74+
simpa [neg_neg, neg_zero] using h,
75+
fun h => by rw [h, neg_zero]⟩
76+
77+
theorem neg_add (a b : M) : -(a + b) = -a + -b := by
78+
rw [← add_left_inj (a + b), neg_add_cancel, add_assoc (-a), add_comm a b, ← add_assoc (-b),
79+
neg_add_cancel, zero_add, neg_add_cancel]
80+
81+
theorem neg_sub (a b : M) : -(a - b) = b - a := by
82+
rw [sub_eq_add_neg, neg_add, neg_neg, sub_eq_add_neg, add_comm]
83+
84+
theorem sub_self (a : M) : a - a = 0 := by
85+
rw [sub_eq_add_neg, add_neg_cancel]
86+
87+
theorem sub_eq_iff {a b c : M} : a - b = c ↔ a = c + b := by
88+
rw [sub_eq_add_neg]
89+
constructor
90+
next => intro; subst c; rw [add_assoc, neg_add_cancel, add_zero]
91+
next => intro; subst a; rw [add_assoc, add_comm b, neg_add_cancel, add_zero]
92+
93+
theorem sub_eq_zero_iff {a b : M} : a - b = 0 ↔ a = b := by
94+
simp [sub_eq_iff, zero_add]
95+
96+
theorem neg_hmul (n : Int) (a : M) : (-n) * a = - (n * a) := by
97+
apply (add_left_inj (n * a)).mp
98+
rw [← add_hmul, Int.add_left_neg, zero_hmul, neg_add_cancel]
99+
100+
theorem hmul_neg (n : Int) (a : M) : n * (-a) = - (n * a) := by
101+
apply (add_left_inj (n * a)).mp
102+
rw [← hmul_add, neg_add_cancel, neg_add_cancel, hmul_zero]
103+
104+
end IntModule
105+
52106
/-- A preorder is a reflexive, transitive relation `≤` with `a < b` defined in the obvious way. -/
53107
class Preorder (α : Type u) extends LE α, LT α where
54108
le_refl : ∀ a : α, a ≤ a
55-
le_trans : ∀ a b c : α, a ≤ b → b ≤ c → a ≤ c
109+
le_trans : ∀ {a b c : α}, a ≤ b → b ≤ c → a ≤ c
56110
lt := fun a b => a ≤ b ∧ ¬b ≤ a
57-
lt_iff_le_not_le : ∀ a b : α, a < b ↔ a ≤ b ∧ ¬b ≤ a := by intros; rfl
111+
lt_iff_le_not_le : ∀ {a b : α}, a < b ↔ a ≤ b ∧ ¬b ≤ a := by intros; rfl
112+
113+
namespace Preorder
114+
115+
variable {α : Type u} [Preorder α]
116+
117+
theorem le_of_lt {a b : α} (h : a < b) : a ≤ b := (lt_iff_le_not_le.mp h).1
118+
119+
theorem lt_of_lt_of_le {a b c : α} (h₁ : a < b) (h₂ : b ≤ c) : a < c := by
120+
simp [lt_iff_le_not_le] at h₁ ⊢
121+
exact ⟨le_trans h₁.1 h₂, fun h => h₁.2 (le_trans h₂ h)⟩
122+
123+
theorem lt_of_le_of_lt {a b c : α} (h₁ : a ≤ b) (h₂ : b < c) : a < c := by
124+
simp [lt_iff_le_not_le] at h₂ ⊢
125+
exact ⟨le_trans h₁ h₂.1, fun h => h₂.2 (le_trans h h₁)⟩
126+
127+
theorem lt_trans {a b c : α} (h₁ : a < b) (h₂ : b < c) : a < c :=
128+
lt_of_lt_of_le h₁ (le_of_lt h₂)
129+
130+
theorem lt_irrefl {a : α} (h : a < a) : False := by
131+
simp [lt_iff_le_not_le] at h
132+
133+
end Preorder
58134

59135
class IntModule.IsOrdered (M : Type u) [Preorder M] [IntModule M] where
60136
neg_le_iff : ∀ a b : M, -a ≤ b ↔ -b ≤ a
61-
neg_lt_iff : ∀ a b : M, -a < b ↔ -b < a
62-
add_lt_left : ∀ a b c : M, a < b → a + c < b + c
63-
add_lt_right : ∀ a b c : M, a < b → c + a < c + b
64-
hmul_pos : ∀ (k : Int) (a : M), 0 < a → (0 < k ↔ 0 < k * a)
65-
hmul_neg : ∀ (k : Int) (a : M), a < 0 → (0 < k ↔ k * a < 0)
66-
hmul_nonneg : ∀ (k : Int) (a : M), 0 ≤ a → 0 ≤ k → 0 ≤ k * a
67-
hmul_nonpos : ∀ (k : Int) (a : M), a ≤ 00 ≤ k → k * a ≤ 0
137+
add_le_left : ∀ {a b : M}, a ≤ b → (c : M) → a + c ≤ b + c
138+
hmul_pos : ∀ (k : Int) {a : M}, 0 < a → (0 < k ↔ 0 < k * a)
139+
hmul_nonneg : ∀ {k : Int} {a : M}, 0 ≤ k → 0 ≤ a → 0 ≤ k * a
140+
141+
namespace IntModule.IsOrdered
142+
143+
variable {M : Type u} [Preorder M] [IntModule M] [IntModule.IsOrdered M]
144+
145+
theorem le_neg_iff {a b : M} : a ≤ -b ↔ b ≤ -a := by
146+
conv => lhs; rw [← neg_neg a]
147+
rw [neg_le_iff, neg_neg]
148+
149+
theorem neg_lt_iff {a b : M} : -a < b ↔ -b < a := by
150+
simp [Preorder.lt_iff_le_not_le]
151+
rw [neg_le_iff, le_neg_iff]
152+
153+
theorem lt_neg_iff {a b : M} : a < -b ↔ b < -a := by
154+
conv => lhs; rw [← neg_neg a]
155+
rw [neg_lt_iff, neg_neg]
156+
157+
theorem neg_nonneg_iff {a : M} : 0 ≤ -a ↔ a ≤ 0 := by
158+
rw [le_neg_iff, neg_zero]
159+
160+
theorem neg_pos_iff {a : M} : 0 < -a ↔ a < 0 := by
161+
rw [lt_neg_iff, neg_zero]
162+
163+
theorem add_lt_left {a b : M} (h : a < b) (c : M) : a + c < b + c := by
164+
simp [Preorder.lt_iff_le_not_le] at h ⊢
165+
constructor
166+
· exact add_le_left h.1 _
167+
· intro w
168+
apply h.2
169+
replace w := add_le_left w (-c)
170+
rw [add_assoc, add_assoc, add_neg_cancel, add_zero, add_zero] at w
171+
exact w
172+
173+
theorem add_le_right (a : M) {b c : M} (h : b ≤ c) : a + b ≤ a + c := by
174+
rw [add_comm a b, add_comm a c]
175+
exact add_le_left h a
176+
177+
theorem add_lt_right (a : M) {b c : M} (h : b < c) : a + b < a + c := by
178+
rw [add_comm a b, add_comm a c]
179+
exact add_lt_left h a
180+
181+
theorem hmul_neg (k : Int) {a : M} (h : a < 0) : 0 < k ↔ k * a < 0 := by
182+
simpa [IntModule.hmul_neg, neg_pos_iff] using hmul_pos k (neg_pos_iff.mpr h)
183+
184+
theorem hmul_nonpos {k : Int} {a : M} (hk : 0 ≤ k) (ha : a ≤ 0) : k * a ≤ 0 := by
185+
simpa [IntModule.hmul_neg, neg_nonneg_iff] using hmul_nonneg hk (neg_nonneg_iff.mpr ha)
186+
187+
188+
189+
end IntModule.IsOrdered
68190

69191
/--
70192
Special case of Mathlib's `NoZeroSMulDivisors Nat α`.

src/Init/Grind/Module/Int.lean

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,13 @@ namespace Lean.Grind
1818

1919
instance : Preorder Int where
2020
le_refl := Int.le_refl
21-
le_trans _ _ _ := Int.le_trans
21+
le_trans := Int.le_trans
2222
lt_iff_le_not_le := by omega
2323

2424
instance : IntModule.IsOrdered Int where
2525
neg_le_iff := by omega
26-
neg_lt_iff := by omega
27-
add_lt_left := by omega
28-
add_lt_right := by omega
26+
add_le_left := by omega
2927
hmul_pos k a ha := ⟨fun hk => Int.mul_pos hk ha, fun h => Int.pos_of_mul_pos_left h ha⟩
30-
hmul_neg k a ha := ⟨fun hk => Int.mul_neg_of_pos_of_neg hk ha, fun h => Int.pos_of_mul_neg_left h ha⟩
31-
hmul_nonpos k a ha hk := Int.mul_nonpos_of_nonneg_of_nonpos hk ha
32-
hmul_nonneg k a ha hk := Int.mul_nonneg hk ha
28+
hmul_nonneg hk ha := Int.mul_nonneg hk ha
3329

3430
end Lean.Grind

0 commit comments

Comments
 (0)