diff --git a/src/Init/Data/Int/Linear.lean b/src/Init/Data/Int/Linear.lean index e6a7566313..05513b8753 100644 --- a/src/Init/Data/Int/Linear.lean +++ b/src/Init/Data/Int/Linear.lean @@ -250,14 +250,24 @@ def Poly.divCoeffs (k : Int) : Poly → Bool /-- `p.mul k` multiplies all coefficients and constant of the polynomial `p` by `k`. -/ -def Poly.mul (p : Poly) (k : Int) : Poly := +def Poly.mul' (p : Poly) (k : Int) : Poly := match p with | .num k' => .num (k*k') - | .add k' v p => .add (k*k') v (mul p k) + | .add k' v p => .add (k*k') v (mul' p k) + +def Poly.mul (p : Poly) (k : Int) : Poly := + if k == 0 then + .num 0 + else + p.mul' k @[simp] theorem Poly.denote_mul (ctx : Context) (p : Poly) (k : Int) : (p.mul k).denote ctx = k * p.denote ctx := by - induction p <;> simp [mul, denote, *] - rw [Int.mul_assoc, Int.mul_add] + simp [mul] + split + next => simp [*, denote] + next => + induction p <;> simp [mul', denote, *] + rw [Int.mul_assoc, Int.mul_add] attribute [local simp] Int.add_comm Int.add_assoc Int.add_left_comm Int.add_mul Int.mul_add attribute [local simp] Poly.insert Poly.denote Poly.norm Poly.addConst diff --git a/tests/lean/run/grind_Poly_mul_0_bug.lean b/tests/lean/run/grind_Poly_mul_0_bug.lean new file mode 100644 index 0000000000..f05b84e85d --- /dev/null +++ b/tests/lean/run/grind_Poly_mul_0_bug.lean @@ -0,0 +1,7 @@ +open Int.Linear + +def p : Poly := .add 1 1 <| .add 2 0 <| .num 3 + +/-- info: Int.Linear.Poly.num 0 -/ +#guard_msgs (info) in +#eval p |>.mul 0