Skip to content

Commit 4cb1b3c

Browse files
committed
refactor(Tactic/NormNum): expose the derive API for known expressions directly (#28621)
We already exposed these through a janky `core` method, this now makes them more obviously part of the API, via dot notation. Most of the norm_num changes are just moves out to an auxiliary function. This should make it possible to write `← (← ra.mul rb).add (← rc.mul rd)` to derive a term of the form `a * b + c * d`. This deletes the `core` methods, but we could instead deprecate them. `:= by exact q(delta% inferInstance)` is from [#metaprogramming / tactics > A Qq gotcha @ 💬](https://leanprover.zulipchat.com/#narrow/channel/239415-metaprogramming-.2F-tactics/topic/A.20Qq.20gotcha/near/535030050).
1 parent 4764a22 commit 4cb1b3c

4 files changed

Lines changed: 71 additions & 70 deletions

File tree

Mathlib/Tactic/NormNum/Basic.lean

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -223,20 +223,19 @@ def _root_.Mathlib.Meta.monadLiftOptionMetaM : MonadLift Option MetaM where
223223
| some e => pure e
224224

225225
attribute [local instance] monadLiftOptionMetaM in
226-
/-- Main part of `evalAdd`. -/
227-
def evalAdd.core {u : Level} {α : Q(Type u)} (e : Q(«»)) (f : Q(«» → «» → «»))
228-
(a b : Q(«»)) (ra : Result a) (rb : Result b) : MetaM (Result e) := do
226+
/-- The result of adding two norm_num results. -/
227+
def Result.add {u : Level} {α : Q(Type u)} {a b : Q($α)} (ra : Result q($a)) (rb : Result q($b))
228+
(inst : Q(Add $α) := by exact q(delta% inferInstance)) :
229+
MetaM (Result q($a + $b)) := do
229230
let rec intArm (rα : Q(Ring $α)) := do
230-
haveI' : $e =Q $a + $b := ⟨⟩
231+
assumeInstancesCommute
231232
let ⟨za, na, pa⟩ ← ra.toInt _; let ⟨zb, nb, pb⟩ ← rb.toInt _
232-
haveI' : $f =Q HAdd.hAdd := ⟨⟩
233233
let zc := za + zb
234234
have c := mkRawIntLit zc
235235
haveI' : Int.add $na $nb =Q $c := ⟨⟩
236-
return .isInt rα c zc q(isInt_add (f := $f) (.refl $f) $pa $pb (.refl $c))
237-
let rec nnratArm (dsα : Q(DivisionSemiring $α)) : Option (Result _) := do
238-
haveI' : $e =Q $a + $b := ⟨⟩
239-
haveI' : $f =Q HAdd.hAdd := ⟨⟩
236+
return .isInt rα c zc q(isInt_add (.refl _) $pa $pb (.refl $c))
237+
let rec nnratArm (dsα : Q(DivisionSemiring $α)) : MetaM (Result _) := do
238+
assumeInstancesCommute
240239
let ⟨qa, na, da, pa⟩ ← ra.toNNRat' dsα; let ⟨qb, nb, db, pb⟩ ← rb.toNNRat' dsα
241240
let qc := qa + qb
242241
let dd := qa.den * qb.den
@@ -249,10 +248,9 @@ def evalAdd.core {u : Level} {α : Q(Type u)} (e : Q(«$α»)) (f : Q(«$α»
249248
let r1 : Q(Nat.add (Nat.mul $na $db) (Nat.mul $nb $da) = Nat.mul $k $nc) :=
250249
(q(Eq.refl $t1) : Expr)
251250
let r2 : Q(Nat.mul $da $db = Nat.mul $k $dc) := (q(Eq.refl $t2) : Expr)
252-
return .isNNRat' dsα qc nc dc q(isNNRat_add (f := $f) (.refl $f) $pa $pb $r1 $r2)
253-
let rec ratArm (dα : Q(DivisionRing $α)) : Option (Result _) := do
254-
haveI' : $e =Q $a + $b := ⟨⟩
255-
haveI' : $f =Q HAdd.hAdd := ⟨⟩
251+
return .isNNRat' dsα qc nc dc q(isNNRat_add (.refl _) $pa $pb $r1 $r2)
252+
let rec ratArm (dα : Q(DivisionRing $α)) : MetaM (Result _) := do
253+
assumeInstancesCommute
256254
let ⟨qa, na, da, pa⟩ ← ra.toRat' dα; let ⟨qb, nb, db, pb⟩ ← rb.toRat' dα
257255
let qc := qa + qb
258256
let dd := qa.den * qb.den
@@ -265,25 +263,26 @@ def evalAdd.core {u : Level} {α : Q(Type u)} (e : Q(«$α»)) (f : Q(«$α»
265263
let r1 : Q(Int.add (Int.mul $na $db) (Int.mul $nb $da) = Int.mul $k $nc) :=
266264
(q(Eq.refl $t1) : Expr)
267265
let r2 : Q(Nat.mul $da $db = Nat.mul $k $dc) := (q(Eq.refl $t2) : Expr)
268-
return .isRat dα qc nc dc q(isRat_add (f := $f) (.refl $f) $pa $pb $r1 $r2)
266+
return .isRat dα qc nc dc q(isRat_add (.refl _) $pa $pb $r1 $r2)
269267
match ra, rb with
270268
| .isBool .., _ | _, .isBool .. => failure
271269
| .isNegNNRat dα .., _ | _, .isNegNNRat dα .. => ratArm dα
272270
-- mixing positive rationals and negative naturals means we need to use the full rat handler
273271
| .isNNRat _dsα .., .isNegNat _rα .. | .isNegNat _rα .., .isNNRat _dsα .. =>
274272
-- could alternatively try to combine `rα` and `dsα` here, but we'd have to do a defeq check
275273
-- so would still need to be in `MetaM`.
276-
ratArm (←synthInstanceQ q(DivisionRing $α))
274+
let dα ← synthInstanceQ q(DivisionRing $α)
275+
assumeInstancesCommute
276+
ratArm q($dα)
277277
| .isNNRat dsα .., _ | _, .isNNRat dsα .. => nnratArm dsα
278278
| .isNegNat rα .., _ | _, .isNegNat rα .. => intArm rα
279279
| .isNat _ na pa, .isNat sα nb pb =>
280-
haveI' : $e =Q $a + $b := ⟨⟩
281-
haveI' : $f =Q HAdd.hAdd := ⟨⟩
282280
assumeInstancesCommute
283281
have c : Q(ℕ) := mkRawNatLit (na.natLit! + nb.natLit!)
284282
haveI' : Nat.add $na $nb =Q $c := ⟨⟩
285-
return .isNat sα c q(isNat_add (f := $f) (.refl $f) $pa $pb (.refl $c))
283+
return .isNat sα c q(isNat_add (.refl _) $pa $pb (.refl $c))
286284

285+
attribute [local instance] monadLiftOptionMetaM in
287286
/-- The `norm_num` extension which identifies expressions of the form `a + b`,
288287
such that `norm_num` successfully recognises both `a` and `b`. -/
289288
@[norm_num _ + _] def evalAdd : NormNumExt where eval {u α} e := do
@@ -300,7 +299,7 @@ such that `norm_num` successfully recognises both `a` and `b`. -/
300299
| .isNegNNRat _ .., .isNat _ .. | .isNegNNRat _ .., .isNegNat _ ..
301300
| .isNegNNRat _ .., .isNNRat _ .. | .isNegNNRat _ .., .isNegNNRat _ .. =>
302301
guard <|← withNewMCtxDepth <| isDefEq f q(HAdd.hAdd (α := $α))
303-
evalAdd.core q($e) q($f) q($a) q($b) ra rb
302+
ra.add rb
304303

305304
-- see note [norm_num lemma function equality]
306305
theorem isInt_neg {α} [Ring α] : ∀ {f : α → α} {a : α} {a' b : ℤ},
@@ -313,40 +312,41 @@ theorem isRat_neg {α} [Ring α] : ∀ {f : α → α} {a : α} {n n' : ℤ} {d
313312
| _, _, _, _, _, rfl, ⟨h, rfl⟩, rfl => ⟨h, by rw [← neg_mul, ← Int.cast_neg]; rfl⟩
314313

315314
attribute [local instance] monadLiftOptionMetaM in
316-
/-- Main part of `evalNeg`. -/
317-
def evalNeg.core {u : Level} {α : Q(Type u)} (e : Q(«»)) (f : Q(«» → «»)) (a : Q(«»))
318-
(ra : Result a) (rα : Q(Ring «»)) : MetaM (Result e) := do
319-
have : $f =Q Neg.neg := ⟨⟩
320-
haveI' _e_eq : $e =Q -$a := ⟨⟩
315+
/-- The result of subtracting two norm_num results. -/
316+
def Result.neg {u : Level} {α : Q(Type u)} {a : Q($α)} (ra : Result q($a))
317+
(rα : Q(Ring $α) := by exact q(delta% inferInstance)) :
318+
MetaM (Result q(-$a)) := do
321319
let intArm (rα : Q(Ring $α)) := do
322320
assumeInstancesCommute
323321
let ⟨za, na, pa⟩ ← ra.toInt rα
324322
let zb := -za
325323
have b := mkRawIntLit zb
326324
haveI' : Int.neg $na =Q $b := ⟨⟩
327-
return .isInt rα b zb q(isInt_neg (f := $f) (.refl $f) $pa (.refl $b))
325+
return .isInt rα b zb q(isInt_neg (.refl _) $pa (.refl $b))
328326
let ratArm (dα : Q(DivisionRing $α)) : Option (Result _) := do
329327
assumeInstancesCommute
330328
let ⟨qa, na, da, pa⟩ ← ra.toRat' dα
331329
let qb := -qa
332330
have nb := mkRawIntLit qb.num
333331
haveI' : Int.neg $na =Q $nb := ⟨⟩
334-
return .isRat dα qb nb da q(isRat_neg (f := $f) (.refl $f) $pa (.refl $nb))
332+
return .isRat dα qb nb da q(isRat_neg (.refl _) $pa (.refl $nb))
335333
match ra with
336334
| .isBool _ .. => failure
337335
| .isNat _ .. => intArm rα
338336
| .isNegNat rα .. => intArm rα
339337
| .isNNRat _dsα .. => ratArm (← synthInstanceQ q(DivisionRing $α))
340338
| .isNegNNRat dα .. => ratArm dα
341339

340+
attribute [local instance] monadLiftOptionMetaM in
342341
/-- The `norm_num` extension which identifies expressions of the form `-a`,
343342
such that `norm_num` successfully recognises `a`. -/
344343
@[norm_num -_] def evalNeg : NormNumExt where eval {u α} e := do
345344
let .app (f : Q($α → $α)) (a : Q($α)) ← whnfR e | failure
346345
let ra ← derive a
347346
let rα ← inferRing α
348347
let ⟨(_f_eq : $f =Q Neg.neg)⟩ ← withNewMCtxDepth <| assertDefEqQ _ _
349-
evalNeg.core q($e) q($f) q($a) ra rα
348+
haveI' _e_eq : $e =Q -$a := ⟨⟩
349+
ra.neg
350350

351351
-- see note [norm_num lemma function equality]
352352
theorem isInt_sub {α} [Ring α] : ∀ {f : α → α → α} {a b : α} {a' b' c : ℤ},
@@ -364,19 +364,18 @@ theorem isRat_sub {α} [Ring α] {f : α → α → α} {a b : α} {na nb nc :
364364
rw [show Int.mul (-nb) _ = _ from neg_mul ..]; exact h₁
365365

366366
attribute [local instance] monadLiftOptionMetaM in
367-
/-- Main part of `evalSub`. -/
368-
def evalSub.core {u : Level} {α : Q(Type u)} (e : Q(«»)) (f : Q(«» → «» → «»))
369-
(a b : Q(«»)) (rα : Q(Ring «»)) (ra : Result a) (rb : Result b) : MetaM (Result e) := do
370-
have : $f =Q HSub.hSub := ⟨⟩
371-
haveI' _e_eq : $e =Q $a - $b := ⟨⟩
367+
/-- The result of subtracting two norm_num results. -/
368+
def Result.sub {u : Level} {α : Q(Type u)} {a b : Q($α)} (ra : Result q($a)) (rb : Result q($b))
369+
(inst : Q(Ring $α) := by exact q(delta% inferInstance)) :
370+
MetaM (Result q($a - $b)) := do
372371
let intArm (rα : Q(Ring $α)) := do
373372
assumeInstancesCommute
374373
let ⟨za, na, pa⟩ ← ra.toInt rα; let ⟨zb, nb, pb⟩ ← rb.toInt rα
375374
let zc := za - zb
376375
have c := mkRawIntLit zc
377376
haveI' : Int.sub $na $nb =Q $c := ⟨⟩
378-
return Result.isInt rα c zc q(isInt_sub (f := $f) (.refl $f) $pa $pb (.refl $c))
379-
let ratArm (dα : Q(DivisionRing $α)) : Option (Result _) := do
377+
return Result.isInt rα c zc q(isInt_sub (.refl _) $pa $pb (.refl $c))
378+
let ratArm (dα : Q(DivisionRing $α)) : MetaM (Result _) := do
380379
assumeInstancesCommute
381380
let ⟨qa, na, da, pa⟩ ← ra.toRat' dα; let ⟨qb, nb, db, pb⟩ ← rb.toRat' dα
382381
let qc := qa - qb
@@ -390,16 +389,18 @@ def evalSub.core {u : Level} {α : Q(Type u)} (e : Q(«$α»)) (f : Q(«$α»
390389
let r1 : Q(Int.sub (Int.mul $na $db) (Int.mul $nb $da) = Int.mul $k $nc) :=
391390
(q(Eq.refl $t1) : Expr)
392391
let r2 : Q(Nat.mul $da $db = Nat.mul $k $dc) := (q(Eq.refl $t2) : Expr)
393-
return .isRat dα qc nc dc q(isRat_sub (f := $f) (.refl $f) $pa $pb $r1 $r2)
392+
return .isRat dα qc nc dc q(isRat_sub (.refl _) $pa $pb $r1 $r2)
394393
match ra, rb with
395394
| .isBool .., _ | _, .isBool .. => failure
396395
| .isNegNNRat dα .., _ | _, .isNegNNRat dα .. =>
397396
ratArm dα
398397
| _, .isNNRat _dsα .. | .isNNRat _dsα .., _ =>
399398
ratArm (← synthInstanceQ q(DivisionRing $α))
400-
| .isNegNat rα .., _ | _, .isNegNat rα ..
401-
| .isNat _ .., .isNat _ .. => intArm rα
399+
| .isNegNat _rα .., _ | _, .isNegNat _rα ..
400+
| .isNat _ .., .isNat _ .. =>
401+
intArm inst
402402

403+
attribute [local instance] monadLiftOptionMetaM in
403404
/-- The `norm_num` extension which identifies expressions of the form `a - b` in a ring,
404405
such that `norm_num` successfully recognises both `a` and `b`. -/
405406
@[norm_num _ - _] def evalSub : NormNumExt where eval {u α} e := do
@@ -408,7 +409,7 @@ such that `norm_num` successfully recognises both `a` and `b`. -/
408409
let ⟨(_f_eq : $f =Q HSub.hSub)⟩ ← withNewMCtxDepth <| assertDefEqQ _ _
409410
let ra ← derive a; let rb ← derive b
410411
haveI' _e_eq : $e =Q $a - $b := ⟨⟩
411-
evalSub.core q($e) q($f) q($a) q($b) q($rα) ra rb
412+
ra.sub rb
412413

413414
-- see note [norm_num lemma function equality]
414415
theorem isNat_mul {α} [Semiring α] : ∀ {f : α → α → α} {a b : α} {a' b' c : ℕ},
@@ -461,19 +462,18 @@ theorem isRat_mul {α} [Ring α] {f : α → α → α} {a b : α} {na nb nc :
461462
(Nat.cast_commute (α := α) db dc).invOf_left.invOf_right.right_comm]
462463

463464
attribute [local instance] monadLiftOptionMetaM in
464-
/-- Main part of `evalMul`. -/
465-
def evalMul.core {u : Level} {α : Q(Type u)} (e : Q(«»)) (f : Q(«» → «» → «»))
466-
(a b : Q(«»)) (sα : Q(Semiring «»)) (ra : Result a) (rb : Result b) : MetaM (Result e) := do
467-
haveI' : $f =Q HMul.hMul := ⟨⟩
468-
haveI' : $e =Q $a * $b := ⟨⟩
469-
let rec intArm (rα : Q(Ring $α)) := do
465+
/-- The result of multiplying two norm_num results. -/
466+
def Result.mul {u : Level} {α : Q(Type u)} {a b : Q($α)} (ra : Result q($a)) (rb : Result q($b))
467+
(inst : Q(Semiring $α) := by exact q(delta% inferInstance)) :
468+
MetaM (Result q($a * $b)) := do
469+
let intArm (rα : Q(Ring $α)) := do
470470
assumeInstancesCommute
471471
let ⟨za, na, pa⟩ ← ra.toInt rα; let ⟨zb, nb, pb⟩ ← rb.toInt rα
472472
let zc := za * zb
473473
have c := mkRawIntLit zc
474474
haveI' : Int.mul $na $nb =Q $c := ⟨⟩
475-
return .isInt rα c zc q(isInt_mul (f := $f) (.refl $f) $pa $pb (.refl $c))
476-
let rec nnratArm (dsα : Q(DivisionSemiring $α)) : Option (Result _) := do
475+
return .isInt rα c zc q(isInt_mul (.refl _) $pa $pb (.refl $c))
476+
let nnratArm (dsα : Q(DivisionSemiring $α)) : Option (Result _) := do
477477
assumeInstancesCommute
478478
let ⟨qa, na, da, pa⟩ ← ra.toNNRat' dsα; let ⟨qb, nb, db, pb⟩ ← rb.toNNRat' dsα
479479
let qc := qa * qb
@@ -486,7 +486,7 @@ def evalMul.core {u : Level} {α : Q(Type u)} (e : Q(«$α»)) (f : Q(«$α»
486486
(q(Eq.refl (Nat.mul $na $nb)) : Expr)
487487
have t2 : Q(ℕ) := mkRawNatLit dd
488488
let r2 : Q(Nat.mul $da $db = Nat.mul $k $dc) := (q(Eq.refl $t2) : Expr)
489-
return .isNNRat' dsα qc nc dc q(isNNRat_mul (f := $f) (.refl $f) $pa $pb $r1 $r2)
489+
return .isNNRat' dsα qc nc dc q(isNNRat_mul (.refl _) $pa $pb $r1 $r2)
490490
let rec ratArm (dα : Q(DivisionRing $α)) : Option (Result _) := do
491491
assumeInstancesCommute
492492
let ⟨qa, na, da, pa⟩ ← ra.toRat' dα; let ⟨qb, nb, db, pb⟩ ← rb.toRat' dα
@@ -500,7 +500,7 @@ def evalMul.core {u : Level} {α : Q(Type u)} (e : Q(«$α»)) (f : Q(«$α»
500500
(q(Eq.refl (Int.mul $na $nb)) : Expr)
501501
have t2 : Q(ℕ) := mkRawNatLit dd
502502
let r2 : Q(Nat.mul $da $db = Nat.mul $k $dc) := (q(Eq.refl $t2) : Expr)
503-
return .isRat dα qc nc dc q(isRat_mul (f := $f) (.refl $f) $pa $pb $r1 $r2)
503+
return .isRat dα qc nc dc q(isRat_mul (.refl _) $pa $pb $r1 $r2)
504504
match ra, rb with
505505
| .isBool .., _ | _, .isBool .. => failure
506506
| .isNegNNRat dα .., _ | _, .isNegNNRat dα .. =>
@@ -513,12 +513,12 @@ def evalMul.core {u : Level} {α : Q(Type u)} (e : Q(«$α»)) (f : Q(«$α»
513513
| .isNNRat dsα .., _ | _, .isNNRat dsα .. =>
514514
nnratArm dsα
515515
| .isNegNat rα .., _ | _, .isNegNat rα .. => intArm rα
516-
| .isNat mα' na pa, .isNat mα nb pb =>
516+
| .isNat mα' na pa, .isNat mα nb pb => do
517517
haveI' : $mα =Q by clear! $mα $mα'; apply AddCommMonoidWithOne.toAddMonoidWithOne := ⟨⟩
518518
assumeInstancesCommute
519519
have c : Q(ℕ) := mkRawNatLit (na.natLit! * nb.natLit!)
520520
haveI' : Nat.mul $na $nb =Q $c := ⟨⟩
521-
return .isNat mα c q(isNat_mul (f := $f) (.refl $f) $pa $pb (.refl $c))
521+
return .isNat mα c q(isNat_mul (.refl _) $pa $pb (.refl $c))
522522

523523
/-- The `norm_num` extension which identifies expressions of the form `a * b`,
524524
such that `norm_num` successfully recognises both `a` and `b`. -/
@@ -529,7 +529,7 @@ such that `norm_num` successfully recognises both `a` and `b`. -/
529529
guard <|← withNewMCtxDepth <| isDefEq f q(HMul.hMul (α := $α))
530530
haveI' : $f =Q HMul.hMul := ⟨⟩
531531
haveI' : $e =Q $a * $b := ⟨⟩
532-
evalMul.core q($e) q($f) q($a) q($b) q($sα) ra rb
532+
ra.mul rb
533533

534534
theorem isNNRat_div {α : Type u} [DivisionSemiring α] : {a b : α} → {cn : ℕ} → {cd : ℕ} →
535535
IsNNRat (a * b⁻¹) cn cd → IsNNRat (a / b) cn cd
@@ -718,3 +718,5 @@ end NormNum
718718
end Meta
719719

720720
end Mathlib
721+
722+
open Mathlib.Meta.NormNum

Mathlib/Tactic/NormNum/BigOperators.lean

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,7 @@ partial def evalFinsetProd : NormNumExt where eval {u β} e := do
370370

371371
evalFinsetBigop q(Finset.prod) f res_empty (fun {a s' h} res_fa res_prod_s' ↦ do
372372
let fa : Q($β) := Expr.app f a
373-
let res ← evalMul.core q($fa * Finset.prod $s' $f) q(HMul.hMul) _ _ instS res_fa
374-
res_prod_s'
373+
let res ← res_fa.mul res_prod_s'
375374
let eq : Q(Finset.prod (Finset.cons $a $s' $h) $f = $fa * Finset.prod $s' $f) :=
376375
q(Finset.prod_cons $h)
377376
pure <| res.eq_trans eq)
@@ -397,7 +396,7 @@ partial def evalFinsetSum : NormNumExt where eval {u β} e := do
397396

398397
evalFinsetBigop q(Finset.sum) f res_empty (fun {a s' h} res_fa res_sum_s' ↦ do
399398
let fa : Q($β) := Expr.app f a
400-
let res ← evalAdd.core q($fa + Finset.sum $s' $f) q(HAdd.hAdd) _ _ res_fa res_sum_s'
399+
let res ← res_fa.add res_sum_s'
401400
let eq : Q(Finset.sum (Finset.cons $a $s' $h) $f = $fa + Finset.sum $s' $f) :=
402401
q(Finset.sum_cons $h)
403402
pure <| res.eq_trans eq)

Mathlib/Tactic/Ring/Basic.lean

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,6 @@ partial def ExSum.cast
227227

228228
end
229229

230-
variable {u : Lean.Level}
231-
232230
/--
233231
The result of evaluating an (unnormalized) expression `e` into the type family `E`
234232
(one of `ExSum`, `ExProd`, `ExBase`) is a (normalized) element `e'`
@@ -246,7 +244,8 @@ instance {α : Q(Type u)} {E : Q($α) → Type} {e : Q($α)} [Inhabited (Σ e, E
246244
Inhabited (Result E e) :=
247245
let ⟨e', v⟩ : Σ e, E e := default; ⟨e', v, default⟩
248246

249-
variable {α : Q(Type u)} (sα : Q(CommSemiring $α)) {R : Type*} [CommSemiring R]
247+
variable (sα)
248+
variable {R : Type*} [CommSemiring R]
250249

251250
/--
252251
Constructs the expression corresponding to `.const n`.
@@ -337,7 +336,7 @@ def evalAddOverlap {a b : Q($α)} (va : ExProd sα a) (vb : ExProd sα b) :
337336
match va, vb with
338337
| .const za ha, .const zb hb => do
339338
let ra := Result.ofRawRat za a ha; let rb := Result.ofRawRat zb b hb
340-
let res ← NormNum.evalAdd.core q($a + $b) q(HAdd.hAdd) a b ra rb
339+
let res ← ra.add rb
341340
match res with
342341
| .isNat _ (.lit (.natVal 0)) p => pure <| .zero p
343342
| rc =>
@@ -436,7 +435,7 @@ partial def evalMulProd {a b : Q($α)} (va : ExProd sα a) (vb : ExProd sα b) :
436435
return ⟨a, .const za ha, (q(mul_one $a) : Expr)⟩
437436
else
438437
let ra := Result.ofRawRat za a ha; let rb := Result.ofRawRat zb b hb
439-
let rc ← NormNum.evalMul.core q($a * $b) q(HMul.hMul) _ _ q(CommSemiring.toSemiring) ra rb
438+
let rc ← ra.mul rb
440439
let ⟨zc, hc⟩ := rc.toRatNZ.get!
441440
let ⟨c, pc⟩ := rc.toRawEq
442441
return ⟨c, .const zc hc, pc⟩
@@ -597,13 +596,14 @@ def evalNegProd {a : Q($α)} (rα : Q(Ring $α)) (va : ExProd sα a) :
597596
match va with
598597
| .const za ha =>
599598
let ⟨m1, _⟩ := ExProd.mkNegNat sα rα 1
600-
let rm := Result.isNegNat rα q(nat_lit 1) (q(IsInt.of_raw $α (.negOfNat (nat_lit 1))) : Expr)
599+
let rm := Result.isNegNat rα q(nat_lit 1) q(IsInt.of_raw $α (.negOfNat (nat_lit 1)))
601600
let ra := Result.ofRawRat za a ha
602-
let rb ← NormNum.evalMul.core q($m1 * $a) q(HMul.hMul) _ _
603-
q(CommSemiring.toSemiring) rm ra
601+
let rb ← rm.mul ra
604602
let ⟨zb, hb⟩ := rb.toRatNZ.get!
605-
let ⟨b, (pb : Q((Int.negOfNat (nat_lit 1)).rawCast * $a = $b))⟩ := rb.toRawEq
606-
return ⟨b, .const zb hb, (q(neg_one_mul (R := $α) $pb) : Expr)⟩
603+
let ⟨b, pb⟩ := rb.toRawEq
604+
-- Qq is probably unhappy about `Ring` and `CommSemiring` at the same time.
605+
have pb : Q(((Int.negOfNat (nat_lit 1)).rawCast : $α) * $a = $b) := pb
606+
return ⟨b, .const zb hb, q(neg_one_mul (R := $α) $pb)⟩
607607
| .mul (x := a₁) (e := a₂) va₁ va₂ va₃ =>
608608
let ⟨_, vb, pb⟩ ← evalNegProd rα va₃
609609
return ⟨_, .mul va₁ va₂ vb, (q(neg_mul $a₁ $a₂ $pb) : Expr)⟩

scripts/nolints.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,14 @@
314314
["docBlame", "Mathlib.Tactic.GCongr.ForwardExt.eval"],
315315
["docBlame", "Mathlib.Tactic.Monotonicity.mono.side"],
316316
["docBlame", "Mathlib.Tactic.Sat.buildReify.mkPS"],
317-
["docBlame", "Mathlib.Meta.NormNum.evalAdd.core.intArm"],
318-
["docBlame", "Mathlib.Meta.NormNum.evalAdd.core.nnratArm"],
319-
["docBlame", "Mathlib.Meta.NormNum.evalAdd.core.ratArm"],
317+
["docBlame", "Mathlib.Meta.NormNum.Result.add.intArm"],
318+
["docBlame", "Mathlib.Meta.NormNum.Result.add.nnratArm"],
319+
["docBlame", "Mathlib.Meta.NormNum.Result.add.ratArm"],
320320
["docBlame", "Mathlib.Meta.NormNum.evalLE.core.intArm"],
321321
["docBlame", "Mathlib.Meta.NormNum.evalLE.core.ratArm"],
322322
["docBlame", "Mathlib.Meta.NormNum.evalLT.core.intArm"],
323323
["docBlame", "Mathlib.Meta.NormNum.evalLT.core.nnratArm"],
324324
["docBlame", "Mathlib.Meta.NormNum.evalLT.core.ratArm"],
325-
["docBlame", "Mathlib.Meta.NormNum.evalMul.core.intArm"],
326-
["docBlame", "Mathlib.Meta.NormNum.evalMul.core.nnratArm"],
327-
["docBlame", "Mathlib.Meta.NormNum.evalMul.core.ratArm"]]
325+
["docBlame", "Mathlib.Meta.NormNum.Result.mul.intArm"],
326+
["docBlame", "Mathlib.Meta.NormNum.Result.mul.nnratArm"],
327+
["docBlame", "Mathlib.Meta.NormNum.Result.mul.ratArm"]]

0 commit comments

Comments
 (0)