Skip to content

Commit 5ee79ed

Browse files
committed
De-fuel some grind definitions
(but only those that do not have a `Nat.rec`-based compagnon)
1 parent e09c73e commit 5ee79ed

File tree

3 files changed

+45
-75
lines changed

3 files changed

+45
-75
lines changed

src/Init/Grind/Module/NatModuleNorm.lean

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ theorem Poly.denoteN_append {α} [NatModule α] (ctx : Context α) (p₁ p₂ :
7979

8080
attribute [local simp] Poly.denoteN_append
8181

82-
theorem Poly.denoteN_combine' {α} [NatModule α] (ctx : Context α) (fuel : Nat) (p₁ p₂ : Poly)
83-
: p₁.NonnegCoeffs → p₂.NonnegCoeffs → (p₁.combine' fuel p₂).denoteN ctx = p₁.denoteN ctx + p₂.denoteN ctx := by
84-
fun_induction p₁.combine' fuel p₂ <;> intro h₁ h₂ <;> try simp [*, zero_add, add_zero]
82+
theorem Poly.denoteN_combine {α} [NatModule α] (ctx : Context α) (p₁ p₂ : Poly)
83+
: p₁.NonnegCoeffs → p₂.NonnegCoeffs → (p₁.combine p₂).denoteN ctx = p₁.denoteN ctx + p₂.denoteN ctx := by
84+
fun_induction p₁.combine p₂ <;> intro h₁ h₂ <;> try simp [*, zero_add, add_zero]
8585
next hx _ h ih =>
8686
simp at hx
8787
simp +zetaDelta at h
@@ -103,10 +103,6 @@ theorem Poly.denoteN_combine' {α} [NatModule α] (ctx : Context α) (fuel : Nat
103103
cases h₂; next h₂ =>
104104
simp [ih h₁ h₂, *]; ac_rfl
105105

106-
theorem Poly.denoteN_combine {α} [NatModule α] (ctx : Context α) (p₁ p₂ : Poly)
107-
: p₁.NonnegCoeffs → p₂.NonnegCoeffs → (p₁.combine p₂).denoteN ctx = p₁.denoteN ctx + p₂.denoteN ctx := by
108-
intros; simp [combine, denoteN_combine', *]
109-
110106
theorem Poly.denoteN_mul' {α} [NatModule α] (ctx : Context α) (p : Poly) (k : Nat) : p.NonnegCoeffs → (p.mul' k).denoteN ctx = k • p.denoteN ctx := by
111107
induction p <;> simp [mul', *, nsmul_zero]
112108
next ih =>
@@ -151,9 +147,8 @@ theorem Poly.append_Nonneg (p₁ p₂ : Poly) : p₁.NonnegCoeffs → p₂.Nonne
151147
fun_induction append <;> intro h₁ h₂; simp [*]
152148
next ih => cases h₁; constructor; assumption; apply ih <;> assumption
153149

154-
theorem Poly.combine'_Nonneg (fuel : Nat) (p₁ p₂ : Poly) : p₁.NonnegCoeffs → p₂.NonnegCoeffs → (p₁.combine' fuel p₂).NonnegCoeffs := by
155-
fun_induction Poly.combine'
156-
next => apply Poly.append_Nonneg
150+
theorem Poly.combine_Nonneg (p₁ p₂ : Poly) : p₁.NonnegCoeffs → p₂.NonnegCoeffs → (p₁.combine p₂).NonnegCoeffs := by
151+
fun_induction Poly.combine
157152
next => intros; assumption
158153
next => intros; assumption
159154
next ih =>
@@ -172,9 +167,6 @@ theorem Poly.combine'_Nonneg (fuel : Nat) (p₁ p₂ : Poly) : p₁.NonnegCoeffs
172167
constructor; assumption
173168
apply ih; constructor; assumption; assumption; assumption
174169

175-
theorem Poly.combine_Nonneg (p₁ p₂ : Poly) : p₁.NonnegCoeffs → p₂.NonnegCoeffs → (p₁.combine p₂).NonnegCoeffs := by
176-
simp [combine]; apply Poly.combine'_Nonneg
177-
178170
theorem Expr.toPolyN_Nonneg (e : Expr) : e.toPolyN.NonnegCoeffs := by
179171
fun_induction toPolyN <;> try constructor <;> simp
180172
next => constructor; simp; constructor

src/Init/Grind/Ordered/Linarith.lean

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,22 @@ def Poly.append (p₁ p₂ : Poly) : Poly :=
123123
| .nil => p₂
124124
| .add k x p₁ => .add k x (append p₁ p₂)
125125

126-
def Poly.combine' (fuel : Nat) (p₁ p₂ : Poly) : Poly :=
127-
match fuel with
128-
| 0 => p₁.append p₂
129-
| fuel + 1 => match p₁, p₂ with
126+
def Poly.combine (p₁ p₂ : Poly) : Poly :=
127+
match p₁, p₂ with
130128
| .nil, p₂ => p₂
131129
| p₁, .nil => p₁
132130
| .add a₁ x₁ p₁, .add a₂ x₂ p₂ =>
133131
bif Nat.beq x₁ x₂ then
134132
let a := a₁ + a₂
135133
bif a == 0 then
136-
combine' fuel p₁ p₂
134+
combine p₁ p₂
137135
else
138-
.add a x₁ (combine' fuel p₁ p₂)
136+
.add a x₁ (combine p₁ p₂)
139137
else bif Nat.blt x₂ x₁ then
140-
.add a₁ x₁ (combine' fuel p₁ (.add a₂ x₂ p₂))
138+
.add a₁ x₁ (combine p₁ (.add a₂ x₂ p₂))
141139
else
142-
.add a₂ x₂ (combine' fuel (.add a₁ x₁ p₁) p₂)
143-
144-
def Poly.combine (p₁ p₂ : Poly) : Poly :=
145-
combine' 100000000 p₁ p₂
140+
.add a₂ x₂ (combine (.add a₁ x₁ p₁) p₂)
141+
termination_by sizeOf p₁ + sizeOf p₂
146142

147143
/-- Converts the given expression into a polynomial. -/
148144
def Expr.toPoly' (e : Expr) : Poly :=
@@ -205,18 +201,15 @@ theorem Poly.denote_append {α} [IntModule α] (ctx : Context α) (p₁ p₂ : P
205201

206202
attribute [local simp] Poly.denote_append
207203

208-
theorem Poly.denote_combine' {α} [IntModule α] (ctx : Context α) (fuel : Nat) (p₁ p₂ : Poly) : (p₁.combine' fuel p₂).denote ctx = p₁.denote ctx + p₂.denote ctx := by
209-
fun_induction p₁.combine' fuel p₂ <;>
204+
theorem Poly.denote_combine {α} [IntModule α] (ctx : Context α) (p₁ p₂ : Poly) : (p₁.combine p₂).denote ctx = p₁.denote ctx + p₂.denote ctx := by
205+
fun_induction p₁.combine p₂ <;>
210206
simp_all +zetaDelta [denote]
211207
next h _ =>
212208
rw [Int.add_comm] at h
213209
rw [add_left_comm, add_assoc, ← add_assoc, ← add_zsmul, h, zero_zsmul, zero_add]
214210
next => rw [add_zsmul]; ac_rfl
215211
all_goals ac_rfl
216212

217-
theorem Poly.denote_combine {α} [IntModule α] (ctx : Context α) (p₁ p₂ : Poly) : (p₁.combine p₂).denote ctx = p₁.denote ctx + p₂.denote ctx := by
218-
simp [combine, denote_combine']
219-
220213
attribute [local simp] Poly.denote_combine
221214

222215
private theorem Expr.denote_toPoly'_go {α} [IntModule α] {k p} (ctx : Context α) (e : Expr)

src/Init/Grind/Ring/CommSolver.lean

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -162,23 +162,17 @@ def Mon.length : Mon → Nat
162162
def hugeFuel := 1000000
163163

164164
def Mon.mul (m₁ m₂ : Mon) : Mon :=
165-
-- We could use `m₁.length + m₂.length` to avoid hugeFuel
166-
go hugeFuel m₁ m₂
167-
where
168-
go (fuel : Nat) (m₁ m₂ : Mon) : Mon :=
169-
match fuel with
170-
| 0 => concat m₁ m₂
171-
| fuel + 1 =>
172-
match m₁, m₂ with
173-
| m₁, .unit => m₁
174-
| .unit, m₂ => m₂
175-
| .mult pw₁ m₁, .mult pw₂ m₂ =>
176-
bif pw₁.varLt pw₂ then
177-
.mult pw₁ (go fuel m₁ (.mult pw₂ m₂))
178-
else bif pw₂.varLt pw₁ then
179-
.mult pw₂ (go fuel (.mult pw₁ m₁) m₂)
180-
else
181-
.mult { x := pw₁.x, k := pw₁.k + pw₂.k } (go fuel m₁ m₂)
165+
match m₁, m₂ with
166+
| m₁, .unit => m₁
167+
| .unit, m₂ => m₂
168+
| .mult pw₁ m₁, .mult pw₂ m₂ =>
169+
bif pw₁.varLt pw₂ then
170+
.mult pw₁ (mul m₁ (.mult pw₂ m₂))
171+
else bif pw₂.varLt pw₁ then
172+
.mult pw₂ (mul (.mult pw₁ m₁) m₂)
173+
else
174+
.mult { x := pw₁.x, k := pw₁.k + pw₂.k } (mul m₁ m₂)
175+
termination_by sizeOf m₁ + sizeOf m₂
182176

183177
def Mon.mul_nc (m₁ m₂ : Mon) : Mon :=
184178
match m₁ with
@@ -820,26 +814,21 @@ where
820814
| .num k' => acc.insert (k*k' % c) m
821815
| .add k' m' p => go p (acc.insert (k*k' % c) (m.mul_nc m'))
822816

823-
def Poly.combineC (p₁ p₂ : Poly) (c : Nat) : Poly :=
824-
go hugeFuel p₁ p₂
825-
where
826-
go (fuel : Nat) (p₁ p₂ : Poly) : Poly :=
827-
match fuel with
828-
| 0 => p₁.concat p₂
829-
| fuel + 1 => match p₁, p₂ with
830-
| .num k₁, .num k₂ => .num ((k₁ + k₂) % c)
831-
| .num k₁, .add k₂ m₂ p₂ => addConstC (.add k₂ m₂ p₂) k₁ c
832-
| .add k₁ m₁ p₁, .num k₂ => addConstC (.add k₁ m₁ p₁) k₂ c
833-
| .add k₁ m₁ p₁, .add k₂ m₂ p₂ =>
834-
match m₁.grevlex m₂ with
835-
| .eq =>
836-
let k := (k₁ + k₂) % c
837-
bif k == 0 then
838-
go fuel p₁ p₂
839-
else
840-
.add k m₁ (go fuel p₁ p₂)
841-
| .gt => .add k₁ m₁ (go fuel p₁ (.add k₂ m₂ p₂))
842-
| .lt => .add k₂ m₂ (go fuel (.add k₁ m₁ p₁) p₂)
817+
def Poly.combineC (p₁ p₂ : Poly) (c : Nat) : Poly := match p₁, p₂ with
818+
| .num k₁, .num k₂ => .num ((k₁ + k₂) % c)
819+
| .num k₁, .add k₂ m₂ p₂ => addConstC (.add k₂ m₂ p₂) k₁ c
820+
| .add k₁ m₁ p₁, .num k₂ => addConstC (.add k₁ m₁ p₁) k₂ c
821+
| .add k₁ m₁ p₁, .add k₂ m₂ p₂ =>
822+
match m₁.grevlex m₂ with
823+
| .eq =>
824+
let k := (k₁ + k₂) % c
825+
bif k == 0 then
826+
combineC p₁ p₂ c
827+
else
828+
.add k m₁ (combineC p₁ p₂ c)
829+
| .gt => .add k₁ m₁ (combineC p₁ (.add k₂ m₂ p₂) c)
830+
| .lt => .add k₂ m₂ (combineC (.add k₁ m₁ p₁) p₂ c)
831+
termination_by sizeOf p₁ + sizeOf p₂
843832

844833
def Poly.mulC (p₁ : Poly) (p₂ : Poly) (c : Nat) : Poly :=
845834
go p₁ (.num 0)
@@ -970,11 +959,8 @@ theorem Mon.denote_mulPow_nc {α} [Semiring α] (ctx : Context α) (p : Power) (
970959

971960
theorem Mon.denote_mul {α} [CommSemiring α] (ctx : Context α) (m₁ m₂ : Mon)
972961
: denote ctx (mul m₁ m₂) = m₁.denote ctx * m₂.denote ctx := by
973-
unfold mul
974-
generalize hugeFuel = fuel
975-
fun_induction mul.go
976-
<;> simp [denote, denote_concat, one_mul,
977-
mul_assoc, mul_left_comm, CommSemiring.mul_comm, *]
962+
fun_induction mul
963+
<;> simp [denote, one_mul, mul_assoc, mul_left_comm, CommSemiring.mul_comm, *]
978964
next h₁ h₂ _ =>
979965
have := eq_of_blt_false h₁ h₂
980966
simp [Power.denote_eq, pow_add, this]
@@ -1363,10 +1349,9 @@ theorem Poly.denote_mulMonC_nc {α c} [Ring α] [IsCharP α c] (ctx : Context α
13631349

13641350
theorem Poly.denote_combineC {α c} [Ring α] [IsCharP α c] (ctx : Context α) (p₁ p₂ : Poly)
13651351
: (combineC p₁ p₂ c).denote ctx = p₁.denote ctx + p₂.denote ctx := by
1366-
unfold combineC; generalize hugeFuel = fuel
1367-
fun_induction combineC.go
1368-
<;> simp [*, denote_concat, denote_addConstC, denote, intCast_add,
1369-
add_comm, add_left_comm, add_assoc, IsCharP.intCast_emod, zsmul_eq_intCast_mul]
1352+
fun_induction combineC
1353+
<;> simp [*, denote_addConstC, denote, intCast_add, add_comm, add_left_comm, add_assoc,
1354+
IsCharP.intCast_emod, zsmul_eq_intCast_mul]
13701355
next hg _ h _ =>
13711356
simp +zetaDelta at h
13721357
rw [← add_assoc, Mon.eq_of_grevlex hg, ← right_distrib, ← intCast_add,

0 commit comments

Comments
 (0)