@@ -162,23 +162,17 @@ def Mon.length : Mon → Nat
162162def hugeFuel := 1000000
163163
164164def 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
183177def 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
844833def 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
971960theorem 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
13641350theorem 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