fact stringlengths 6 3.84k | type stringclasses 11 values | library stringclasses 32 values | imports listlengths 1 14 | filename stringlengths 20 95 | symbolic_name stringlengths 1 90 | docstring stringlengths 7 20k ⌀ |
|---|---|---|---|---|---|---|
private int_div_nonneg_of_nonneg_of_pos {a b : ℤ} (ha : 0 ≤ a) (hb : 0 < b) : 0 ≤ a / b :=
Int.ediv_nonneg ha hb.le | lemma | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | int_div_nonneg_of_nonneg_of_pos | null |
private int_div_nonneg_of_pos_of_pos {a b : ℤ} (ha : 0 < a) (hb : 0 < b) : 0 ≤ a / b :=
Int.ediv_nonneg ha.le hb.le | lemma | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | int_div_nonneg_of_pos_of_pos | null |
@[positivity (_ : ℤ) / (_ : ℤ)] evalIntDiv : PositivityExt where eval {u α} _ _ e := do
match u, α, e with
| 0, ~q(ℤ), ~q($a / $b) =>
let ra ← core q(inferInstance) q(inferInstance) a
let rb ← core q(inferInstance) q(inferInstance) b
assertInstancesCommute
match ra, rb with
| .positive (pa : Q(0 < $a)), .positive (pb : Q(0 < $b)) =>
match ← isDefEqQ a b with
| .defEq _ => pure (.positive q(int_div_self_pos $pa))
| .notDefEq => pure (.nonnegative q(int_div_nonneg_of_pos_of_pos $pa $pb))
| .positive (pa : Q(0 < $a)), .nonnegative (pb : Q(0 ≤ $b)) =>
pure (.nonnegative q(int_div_nonneg_of_pos_of_nonneg $pa $pb))
| .nonnegative (pa : Q(0 ≤ $a)), .positive (pb : Q(0 < $b)) =>
pure (.nonnegative q(int_div_nonneg_of_nonneg_of_pos $pa $pb))
| .nonnegative (pa : Q(0 ≤ $a)), .nonnegative (pb : Q(0 ≤ $b)) =>
pure (.nonnegative q(Int.ediv_nonneg $pa $pb))
| _, _ => pure .none
| _, _, _ => throwError "not /" | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalIntDiv | The `positivity` extension which identifies expressions of the form `a / b`,
where `a` and `b` are integers. |
private pow_zero_pos [Semiring α] [PartialOrder α] [IsOrderedRing α] [Nontrivial α]
(a : α) : 0 < a ^ 0 :=
zero_lt_one.trans_le (pow_zero a).ge | theorem | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | pow_zero_pos | null |
@[positivity _ ^ (0 : ℕ)]
evalPowZeroNat : PositivityExt where eval {u α} _zα _pα e := do
let .app (.app _ (a : Q($α))) _ ← withReducible (whnf e) | throwError "not ^"
let _a ← synthInstanceQ q(Semiring $α)
let _a ← synthInstanceQ q(PartialOrder $α)
let _a ← synthInstanceQ q(IsOrderedRing $α)
_ ← synthInstanceQ q(Nontrivial $α)
pure (.positive (q(pow_zero_pos $a) : Expr)) | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalPowZeroNat | The `positivity` extension which identifies expressions of the form `a ^ (0 : ℕ)`.
This extension is run in addition to the general `a ^ b` extension (they are overlapping). |
@[positivity _ ^ (_ : ℕ)]
evalPow : PositivityExt where eval {u α} zα pα e := do
let .app (.app _ (a : Q($α))) (b : Q(ℕ)) ← withReducible (whnf e) | throwError "not ^"
let result ← catchNone do
let .true := b.isAppOfArity ``OfNat.ofNat 3 | throwError "not a ^ n where n is a literal"
let some n := (b.getRevArg! 1).rawNatLit? | throwError "not a ^ n where n is a literal"
guard (n % 2 = 0)
have m : Q(ℕ) := mkRawNatLit (n / 2)
haveI' : $b =Q 2 * $m := ⟨⟩
let _a ← synthInstanceQ q(Ring $α)
let _a ← synthInstanceQ q(LinearOrder $α)
let _a ← synthInstanceQ q(IsStrictOrderedRing $α)
assumeInstancesCommute
haveI' : $e =Q $a ^ $b := ⟨⟩
pure (.nonnegative q((even_two_mul $m).pow_nonneg $a))
orElse result do
let ra ← core zα pα a
let ofNonneg (pa : Q(0 ≤ $a)) (_rα : Q(Semiring $α)) (_oα : Q(IsOrderedRing $α)) :
MetaM (Strictness zα pα e) := do
haveI' : $e =Q $a ^ $b := ⟨⟩
assumeInstancesCommute
pure (.nonnegative q(pow_nonneg $pa $b))
let ofNonzero (pa : Q($a ≠ 0)) (_rα : Q(Semiring $α)) (_oα : Q(IsOrderedRing $α)) :
MetaM (Strictness zα pα e) := do
haveI' : $e =Q $a ^ $b := ⟨⟩
assumeInstancesCommute
let _a ← synthInstanceQ q(NoZeroDivisors $α)
pure (.nonzero q(pow_ne_zero $b $pa))
match ra with
| .positive pa =>
try
let _a ← synthInstanceQ q(Semiring $α)
let _a ← synthInstanceQ q(IsStrictOrderedRing $α)
assumeInstancesCommute
haveI' : $e =Q $a ^ $b := ⟨⟩
pure (.positive q(pow_pos $pa $b))
catch e : Exception =>
trace[Tactic.positivity.failure] "{e.toMessageData}"
let rα ← synthInstanceQ q(Semiring $α)
let oα ← synthInstanceQ q(IsOrderedRing $α)
orElse (← catchNone (ofNonneg q(le_of_lt $pa) rα oα)) (ofNonzero q(ne_of_gt $pa) rα oα)
| .nonnegative pa =>
let sα ← synthInstanceQ q(Semiring $α)
let oα ← synthInstanceQ q(IsOrderedRing $α)
ofNonneg q($pa) q($sα) q($oα)
| .nonzero pa =>
let sα ← synthInstanceQ q(Semiring $α)
let oα ← synthInstanceQ q(IsOrderedRing $α)
ofNonzero q($pa) q($sα) q($oα)
| .none => pure .none | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalPow | The `positivity` extension which identifies expressions of the form `a ^ (b : ℕ)`,
such that `positivity` successfully recognises both `a` and `b`. |
private abs_pos_of_ne_zero {α : Type*} [AddGroup α] [LinearOrder α]
[AddLeftMono α] {a : α} : a ≠ 0 → 0 < |a| := abs_pos.mpr | theorem | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | abs_pos_of_ne_zero | null |
@[positivity |_|]
evalAbs : PositivityExt where eval {_u} (α zα pα) (e : Q($α)) := do
let ~q(@abs _ (_) (_) $a) := e | throwError "not |·|"
try
match ← core zα pα a with
| .positive pa =>
let pa' ← mkAppM ``abs_pos_of_pos #[pa]
pure (.positive pa')
| .nonzero pa =>
let pa' ← mkAppM ``abs_pos_of_ne_zero #[pa]
pure (.positive pa')
| _ => pure .none
catch _ => do
let pa' ← mkAppM ``abs_nonneg #[a]
pure (.nonnegative pa') | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalAbs | The `positivity` extension which identifies expressions of the form `|a|`. |
private int_natAbs_pos {n : ℤ} (hn : 0 < n) : 0 < n.natAbs :=
Int.natAbs_pos.mpr hn.ne' | theorem | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | int_natAbs_pos | null |
@[positivity Int.natAbs _]
evalNatAbs : PositivityExt where eval {u α} _zα _pα e := do
match u, α, e with
| 0, ~q(ℕ), ~q(Int.natAbs $a) =>
let zα' : Q(Zero Int) := q(inferInstance)
let pα' : Q(PartialOrder Int) := q(inferInstance)
let ra ← core zα' pα' a
match ra with
| .positive pa =>
assertInstancesCommute
pure (.positive q(int_natAbs_pos $pa))
| .nonzero pa =>
assertInstancesCommute
pure (.positive q(Int.natAbs_pos.mpr $pa))
| .nonnegative _pa =>
pure .none
| .none =>
pure .none
| _, _, _ => throwError "not Int.natAbs" | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalNatAbs | Extension for the `positivity` tactic: `Int.natAbs` is positive when its input is.
Since the output type of `Int.natAbs` is `ℕ`, the nonnegative case is handled by the default
`positivity` tactic. |
@[positivity Nat.cast _]
evalNatCast : PositivityExt where eval {u α} _zα _pα e := do
let ~q(@Nat.cast _ (_) ($a : ℕ)) := e | throwError "not Nat.cast"
let zα' : Q(Zero Nat) := q(inferInstance)
let pα' : Q(PartialOrder Nat) := q(inferInstance)
let (_i1 : Q(AddMonoidWithOne $α)) ← synthInstanceQ q(AddMonoidWithOne $α)
let (_i2 : Q(AddLeftMono $α)) ← synthInstanceQ q(AddLeftMono $α)
let (_i3 : Q(ZeroLEOneClass $α)) ← synthInstanceQ q(ZeroLEOneClass $α)
assumeInstancesCommute
match ← core zα' pα' a with
| .positive pa =>
let _nz ← synthInstanceQ q(NeZero (1 : $α))
pure (.positive q(Nat.cast_pos'.2 $pa))
| _ =>
pure (.nonnegative q(Nat.cast_nonneg' _)) | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalNatCast | Extension for the `positivity` tactic: `Nat.cast` is always non-negative,
and positive when its input is. |
@[positivity Int.cast _]
evalIntCast : PositivityExt where eval {u α} _zα _pα e := do
let ~q(@Int.cast _ (_) ($a : ℤ)) := e | throwError "not Int.cast"
let zα' : Q(Zero Int) := q(inferInstance)
let pα' : Q(PartialOrder Int) := q(inferInstance)
let ra ← core zα' pα' a
match ra with
| .positive pa =>
let _rα ← synthInstanceQ q(Ring $α)
let _oα ← synthInstanceQ q(IsOrderedRing $α)
let _nt ← synthInstanceQ q(Nontrivial $α)
assumeInstancesCommute
pure (.positive q(Int.cast_pos.mpr $pa))
| .nonnegative pa =>
let _rα ← synthInstanceQ q(Ring $α)
let _oα ← synthInstanceQ q(IsOrderedRing $α)
let _nt ← synthInstanceQ q(Nontrivial $α)
assumeInstancesCommute
pure (.nonnegative q(Int.cast_nonneg.mpr $pa))
| .nonzero pa =>
let _oα ← synthInstanceQ q(AddGroupWithOne $α)
let _nt ← synthInstanceQ q(CharZero $α)
assumeInstancesCommute
pure (.nonzero q(Int.cast_ne_zero.mpr $pa))
| .none =>
pure .none | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalIntCast | Extension for the `positivity` tactic: `Int.cast` is positive (resp. non-negative)
if its input is. |
@[positivity Nat.succ _]
evalNatSucc : PositivityExt where eval {u α} _zα _pα e := do
match u, α, e with
| 0, ~q(ℕ), ~q(Nat.succ $a) =>
assertInstancesCommute
pure (.positive q(Nat.succ_pos $a))
| _, _, _ => throwError "not Nat.succ" | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalNatSucc | Extension for `Nat.succ`. |
@[positivity PNat.val _]
evalPNatVal : PositivityExt where eval {u α} _zα _pα e := do
match u, α, e with
| 0, ~q(ℕ), ~q(PNat.val $a) =>
assertInstancesCommute
pure (.positive q(PNat.pos $a))
| _, _, _ => throwError "not PNat.val" | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalPNatVal | Extension for `PNat.val`. |
@[positivity Nat.factorial _]
evalFactorial : PositivityExt where eval {u α} _ _ e := do
match u, α, e with
| 0, ~q(ℕ), ~q(Nat.factorial $a) =>
assertInstancesCommute
pure (.positive q(Nat.factorial_pos $a))
| _, _, _ => throwError "failed to match Nat.factorial" | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalFactorial | Extension for `Nat.factorial`. |
@[positivity Nat.ascFactorial _ _]
evalAscFactorial : PositivityExt where eval {u α} _ _ e := do
match u, α, e with
| 0, ~q(ℕ), ~q(Nat.ascFactorial ($n + 1) $k) =>
assertInstancesCommute
pure (.positive q(Nat.ascFactorial_pos $n $k))
| _, _, _ => throwError "failed to match Nat.ascFactorial" | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalAscFactorial | Extension for `Nat.ascFactorial`. |
@[positivity Nat.gcd _ _]
evalNatGCD : PositivityExt where eval {u α} z p e := do
match u, α, e with
| 0, ~q(ℕ), ~q(Nat.gcd $a $b) =>
assertInstancesCommute
match ← core z p a with
| .positive pa => return .positive q(Nat.gcd_pos_of_pos_left $b $pa)
| _ =>
match ← core z p b with
| .positive pb => return .positive q(Nat.gcd_pos_of_pos_right $a $pb)
| _ => failure
| _, _, _ => throwError "not Nat.gcd" | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalNatGCD | Extension for `Nat.gcd`.
Uses positivity of the left term, if available, then tries the right term.
The implementation relies on the fact that `Positivity.core` on `ℕ` never returns `nonzero`. |
@[positivity Nat.lcm _ _]
evalNatLCM : PositivityExt where eval {u α} z p e := do
match u, α, e with
| 0, ~q(ℕ), ~q(Nat.lcm $a $b) =>
assertInstancesCommute
match ← core z p a with
| .positive pa =>
match ← core z p b with
| .positive pb =>
return .positive q(Nat.lcm_pos $pa $pb)
| _ => failure
| _ => failure
| _, _, _ => throwError "not Nat.lcm" | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalNatLCM | Extension for `Nat.lcm`. |
@[positivity Nat.sqrt _]
evalNatSqrt : PositivityExt where eval {u α} z p e := do
match u, α, e with
| 0, ~q(ℕ), ~q(Nat.sqrt $n) =>
assumeInstancesCommute
match ← core z p n with
| .positive pa => return .positive q(Nat.sqrt_pos.mpr $pa)
| _ => failure
| _, _, _ => throwError "not Nat.sqrt" | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalNatSqrt | Extension for `Nat.sqrt`. |
@[positivity Int.gcd _ _]
evalIntGCD : PositivityExt where eval {u α} _ _ e := do
match u, α, e with
| 0, ~q(ℕ), ~q(Int.gcd $a $b) =>
let z ← synthInstanceQ (q(Zero ℤ) : Q(Type))
let p ← synthInstanceQ (q(PartialOrder ℤ) : Q(Type))
assertInstancesCommute
match (← catchNone (core z p a)).toNonzero with
| some na => return .positive q(Int.gcd_pos_of_ne_zero_left $b $na)
| none =>
match (← core z p b).toNonzero with
| some nb => return .positive q(Int.gcd_pos_of_ne_zero_right $a $nb)
| none => failure
| _, _, _ => throwError "not Int.gcd" | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalIntGCD | Extension for `Int.gcd`.
Uses positivity of the left term, if available, then tries the right term. |
@[positivity Int.lcm _ _]
evalIntLCM : PositivityExt where eval {u α} _ _ e := do
match u, α, e with
| 0, ~q(ℕ), ~q(Int.lcm $a $b) =>
let z ← synthInstanceQ (q(Zero ℤ) : Q(Type))
let p ← synthInstanceQ (q(PartialOrder ℤ) : Q(Type))
assertInstancesCommute
match (← core z p a).toNonzero with
| some na =>
match (← core z p b).toNonzero with
| some nb => return .positive q(Int.lcm_pos $na $nb)
| _ => failure
| _ => failure
| _, _, _ => throwError "not Int.lcm" | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalIntLCM | Extension for `Int.lcm`. |
@[positivity NNRat.num _]
evalNNRatNum : PositivityExt where eval {u α} _ _ e := do
match u, α, e with
| 0, ~q(ℕ), ~q(NNRat.num $a) =>
let zα : Q(Zero ℚ≥0) := q(inferInstance)
let pα : Q(PartialOrder ℚ≥0) := q(inferInstance)
assumeInstancesCommute
match ← core zα pα a with
| .positive pa => return .positive q(NNRat.num_pos_of_pos $pa)
| .nonzero pa => return .nonzero q(NNRat.num_ne_zero_of_ne_zero $pa)
| _ => return .none
| _, _, _ => throwError "not NNRat.num" | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalNNRatNum | The `positivity` extension which identifies expressions of the form `NNRat.num q`,
such that `positivity` successfully recognises `q`. |
@[positivity NNRat.den _]
evalNNRatDen : PositivityExt where eval {u α} _ _ e := do
match u, α, e with
| 0, ~q(ℕ), ~q(NNRat.den $a) =>
assumeInstancesCommute
return .positive q(den_pos $a)
| _, _, _ => throwError "not NNRat.den"
variable {q : ℚ≥0} | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalNNRatDen | The `positivity` extension which identifies expressions of the form `Rat.den a`. |
@[positivity Rat.num _]
evalRatNum : PositivityExt where eval {u α} _ _ e := do
match u, α, e with
| 0, ~q(ℤ), ~q(Rat.num $a) =>
let zα : Q(Zero ℚ) := q(inferInstance)
let pα : Q(PartialOrder ℚ) := q(inferInstance)
assumeInstancesCommute
match ← core zα pα a with
| .positive pa => pure <| .positive q(num_pos_of_pos $pa)
| .nonnegative pa => pure <| .nonnegative q(num_nonneg_of_nonneg $pa)
| .nonzero pa => pure <| .nonzero q(num_ne_zero_of_ne_zero $pa)
| .none => pure .none
| _, _ => throwError "not Rat.num" | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalRatNum | The `positivity` extension which identifies expressions of the form `Rat.num a`,
such that `positivity` successfully recognises `a`. |
@[positivity Rat.den _]
evalRatDen : PositivityExt where eval {u α} _ _ e := do
match u, α, e with
| 0, ~q(ℕ), ~q(Rat.den $a) =>
assumeInstancesCommute
pure <| .positive q(den_pos $a)
| _, _ => throwError "not Rat.num" | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalRatDen | The `positivity` extension which identifies expressions of the form `Rat.den a`. |
@[positivity _⁺]
evalPosPart : PositivityExt where eval {u α} zα pα e := do
match e with
| ~q(@posPart _ $instαpospart $a) =>
let _instαlat ← synthInstanceQ q(Lattice $α)
let _instαgrp ← synthInstanceQ q(AddGroup $α)
assertInstancesCommute
match ← catchNone (core zα pα a) with
| .positive pf => return .positive q(posPart_pos $pf)
| _ => return .nonnegative q(posPart_nonneg $a)
| _ => throwError "not `posPart`" | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalPosPart | Extension for `posPart`. `a⁺` is always nonnegative, and positive if `a` is. |
@[positivity _⁻]
evalNegPart : PositivityExt where eval {u α} _ _ e := do
match e with
| ~q(@negPart _ $instαnegpart $a) =>
let _instαlat ← synthInstanceQ q(Lattice $α)
let _instαgrp ← synthInstanceQ q(AddGroup $α)
assertInstancesCommute
return .nonnegative q(negPart_nonneg $a)
| _ => throwError "not `negPart`" | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalNegPart | Extension for `negPart`. `a⁻` is always nonnegative. |
@[positivity DFunLike.coe _ _]
evalMap : PositivityExt where eval {_ β} _ _ e := do
let .app (.app _ f) a ← whnfR e
| throwError "not ↑f · where f is of NonnegHomClass"
let pa ← mkAppOptM ``apply_nonneg #[none, none, β, none, none, none, none, f, a]
pure (.nonnegative pa) | def | Tactic | [
"Mathlib.Algebra.Order.Group.PosPart",
"Mathlib.Algebra.Order.Ring.Basic",
"Mathlib.Algebra.Order.Hom.Basic",
"Mathlib.Data.Int.CharZero",
"Mathlib.Data.Nat.Factorial.Basic",
"Mathlib.Data.NNRat.Defs",
"Mathlib.Data.PNat.Defs",
"Mathlib.Tactic.Positivity.Core",
"Qq"
] | Mathlib/Tactic/Positivity/Basic.lean | evalMap | Extension for the `positivity` tactic: nonnegative maps take nonnegative values. |
Strictness (e : Q($α)) where
| positive (pf : Q(0 < $e))
| nonnegative (pf : Q(0 ≤ $e))
| nonzero (pf : Q($e ≠ 0))
| none
deriving Repr | inductive | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | Strictness | Attribute for identifying `positivity` extensions. -/
syntax (name := positivity) "positivity " term,+ : attr
lemma ne_of_ne_of_eq' {α : Sort*} {a c b : α} (hab : (a : α) ≠ c) (hbc : a = b) : b ≠ c := hbc ▸ hab
namespace Mathlib.Meta.Positivity
variable {u : Level} {α : Q(Type u)} (zα : Q(Zero $α)) (pα : Q(PartialOrder $α))
/-- The result of `positivity` running on an expression `e` of type `α`. |
Strictness.toString {e : Q($α)} : Strictness zα pα e → String
| positive _ => "positive"
| nonnegative _ => "nonnegative"
| nonzero _ => "nonzero"
| none => "none" | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | Strictness.toString | Gives a generic description of the `positivity` result. |
Strictness.toPositive {e} : Strictness zα pα e → Option Q(0 < $e)
| .positive pf => some pf
| _ => .none | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | Strictness.toPositive | Extract a proof that `e` is positive, if possible, from `Strictness` information about `e`. |
Strictness.toNonneg {e} : Strictness zα pα e → Option Q(0 ≤ $e)
| .positive pf => some q(le_of_lt $pf)
| .nonnegative pf => some pf
| _ => .none | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | Strictness.toNonneg | Extract a proof that `e` is nonnegative, if possible, from `Strictness` information about `e`. |
Strictness.toNonzero {e} : Strictness zα pα e → Option Q($e ≠ 0)
| .positive pf => some q(ne_of_gt $pf)
| .nonzero pf => some pf
| _ => .none | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | Strictness.toNonzero | Extract a proof that `e` is nonzero, if possible, from `Strictness` information about `e`. |
PositivityExt where
/-- Attempts to prove an expression `e : α` is `>0`, `≥0`, or `≠0`. -/
eval {u : Level} {α : Q(Type u)} (zα : Q(Zero $α)) (pα : Q(PartialOrder $α)) (e : Q($α)) :
MetaM (Strictness zα pα e) | structure | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | PositivityExt | An extension for `positivity`. |
mkPositivityExt (n : Name) : ImportM PositivityExt := do
let { env, opts, .. } ← read
IO.ofExcept <| unsafe env.evalConstCheck PositivityExt opts ``PositivityExt n | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | mkPositivityExt | Read a `positivity` extension from a declaration of the right type. |
Entry := Array (Array DiscrTree.Key) × Name | abbrev | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | Entry | Each `positivity` extension is labelled with a collection of patterns
which determine the expressions to which it should be applied. |
catchNone {e : Q($α)} (t : MetaM (Strictness zα pα e)) : MetaM (Strictness zα pα e) :=
try t catch e =>
trace[Tactic.positivity.failure] "{e.toMessageData}"
pure .none
variable {zα pα} in | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | catchNone | Environment extensions for `positivity` declarations -/
initialize positivityExt : PersistentEnvExtension Entry (Entry × PositivityExt)
(List Entry × DiscrTree PositivityExt) ←
-- we only need this to deduplicate entries in the DiscrTree
have : BEq PositivityExt := ⟨fun _ _ => false⟩
let insert kss v dt := kss.foldl (fun dt ks => dt.insertCore ks v) dt
registerPersistentEnvExtension {
mkInitial := pure ([], {})
addImportedFn := fun s => do
let dt ← s.foldlM (init := {}) fun dt s => s.foldlM (init := dt) fun dt (kss, n) => do
pure (insert kss (← mkPositivityExt n) dt)
pure ([], dt)
addEntryFn := fun (entries, s) ((kss, n), ext) => ((kss, n) :: entries, insert kss ext s)
exportEntriesFn := fun s => s.1.reverse.toArray
}
initialize registerBuiltinAttribute {
name := `positivity
descr := "adds a positivity extension"
applicationTime := .afterCompilation
add := fun declName stx kind => match stx with
| `(attr| positivity $es,*) => do
unless kind == AttributeKind.global do
throwError "invalid attribute 'positivity', must be global"
let env ← getEnv
unless (env.getModuleIdxFor? declName).isNone do
throwError "invalid attribute 'positivity', declaration is in an imported module"
if (IR.getSorryDep env declName).isSome then return -- ignore in progress definitions
let ext ← mkPositivityExt declName
let keys ← MetaM.run' <| es.getElems.mapM fun stx => do
let e ← TermElabM.run' <| withSaveInfoContext <| withAutoBoundImplicit <|
withReader ({ · with ignoreTCFailures := true }) do
let e ← elabTerm stx none
let (_, _, e) ← lambdaMetaTelescope (← mkLambdaFVars (← getLCtx).getFVars e)
return e
DiscrTree.mkPath e
setEnv <| positivityExt.addEntry env ((keys, declName), ext)
| _ => throwUnsupportedSyntax
}
variable {A : Type*} {e : A}
lemma pos_of_isNat {n : ℕ} [Semiring A] [PartialOrder A] [IsOrderedRing A] [Nontrivial A]
(h : NormNum.IsNat e n) (w : Nat.ble 1 n = true) : 0 < (e : A) := by
rw [NormNum.IsNat.to_eq h rfl]
apply Nat.cast_pos.2
simpa using w
lemma pos_of_isNat' {n : ℕ}
[AddMonoidWithOne A] [PartialOrder A] [AddLeftMono A] [ZeroLEOneClass A] [h'' : NeZero (1 : A)]
(h : NormNum.IsNat e n) (w : Nat.ble 1 n = true) : 0 < (e : A) := by
rw [NormNum.IsNat.to_eq h rfl]
apply Nat.cast_pos'.2
simpa using w
lemma nonneg_of_isNat {n : ℕ} [Semiring A] [PartialOrder A] [IsOrderedRing A]
(h : NormNum.IsNat e n) : 0 ≤ (e : A) := by
rw [NormNum.IsNat.to_eq h rfl]
exact Nat.cast_nonneg n
lemma nonneg_of_isNat' {n : ℕ}
[AddMonoidWithOne A] [PartialOrder A] [AddLeftMono A] [ZeroLEOneClass A]
(h : NormNum.IsNat e n) : 0 ≤ (e : A) := by
rw [NormNum.IsNat.to_eq h rfl]
exact Nat.cast_nonneg' n
lemma nz_of_isNegNat {n : ℕ} [Ring A] [PartialOrder A] [IsStrictOrderedRing A]
(h : NormNum.IsInt e (.negOfNat n)) (w : Nat.ble 1 n = true) : (e : A) ≠ 0 := by
rw [NormNum.IsInt.neg_to_eq h rfl]
simp only [ne_eq, neg_eq_zero]
apply ne_of_gt
simpa using w
lemma pos_of_isNNRat {n d : ℕ} [Semiring A] [LinearOrder A] [IsStrictOrderedRing A] :
(NormNum.IsNNRat e n d) → (decide (0 < n)) → ((0 : A) < (e : A))
| ⟨inv, eq⟩, h => by
have pos_invOf_d : (0 < ⅟ (d : A)) := pos_invOf_of_invertible_cast d
have pos_n : (0 < (n : A)) := Nat.cast_pos (n := n) |>.2 (of_decide_eq_true h)
rw [eq]
exact mul_pos pos_n pos_invOf_d
lemma pos_of_isRat {n : ℤ} {d : ℕ} [Ring A] [LinearOrder A] [IsStrictOrderedRing A] :
(NormNum.IsRat e n d) → (decide (0 < n)) → ((0 : A) < (e : A))
| ⟨inv, eq⟩, h => by
have pos_invOf_d : (0 < ⅟(d : A)) := pos_invOf_of_invertible_cast d
have pos_n : (0 < (n : A)) := Int.cast_pos (n := n) |>.2 (of_decide_eq_true h)
rw [eq]
exact mul_pos pos_n pos_invOf_d
lemma nonneg_of_isNNRat {n d : ℕ} [Semiring A] [LinearOrder A] :
(NormNum.IsNNRat e n d) → (decide (n = 0)) → (0 ≤ (e : A))
| ⟨inv, eq⟩, h => by rw [eq, of_decide_eq_true h]; simp
lemma nonneg_of_isRat {n : ℤ} {d : ℕ} [Ring A] [LinearOrder A] :
(NormNum.IsRat e n d) → (decide (n = 0)) → (0 ≤ (e : A))
| ⟨inv, eq⟩, h => by rw [eq, of_decide_eq_true h]; simp
lemma nz_of_isRat {n : ℤ} {d : ℕ} [Ring A] [LinearOrder A] [IsStrictOrderedRing A] :
(NormNum.IsRat e n d) → (decide (n < 0)) → ((e : A) ≠ 0)
| ⟨inv, eq⟩, h => by
have pos_invOf_d : (0 < ⅟(d : A)) := pos_invOf_of_invertible_cast d
have neg_n : ((n : A) < 0) := Int.cast_lt_zero (n := n) |>.2 (of_decide_eq_true h)
have neg := mul_neg_of_neg_of_pos neg_n pos_invOf_d
rw [eq]
exact ne_iff_lt_or_gt.2 (Or.inl neg)
variable {zα pα} in
/-- Converts a `MetaM Strictness` which can fail
into one that never fails and returns `.none` instead. |
throwNone {m : Type → Type*} {e : Q($α)} [Monad m] [Alternative m]
(t : m (Strictness zα pα e)) : m (Strictness zα pα e) := do
match ← t with
| .none => failure
| r => pure r | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | throwNone | Converts a `MetaM Strictness` which can return `.none`
into one which never returns `.none` but fails instead. |
normNumPositivity (e : Q($α)) : MetaM (Strictness zα pα e) := catchNone do
match ← NormNum.derive e with
| .isBool .. => failure
| .isNat _ lit p =>
if 0 < lit.natLit! then
try
let _a ← synthInstanceQ q(Semiring $α)
let _a ← synthInstanceQ q(PartialOrder $α)
let _a ← synthInstanceQ q(IsOrderedRing $α)
let _a ← synthInstanceQ q(Nontrivial $α)
assumeInstancesCommute
have p : Q(NormNum.IsNat $e $lit) := p
haveI' p' : Nat.ble 1 $lit =Q true := ⟨⟩
pure (.positive q(pos_of_isNat (A := $α) $p $p'))
catch e : Exception =>
trace[Tactic.positivity.failure] "{e.toMessageData}"
let _a ← synthInstanceQ q(AddMonoidWithOne $α)
let _a ← synthInstanceQ q(PartialOrder $α)
let _a ← synthInstanceQ q(AddLeftMono $α)
let _a ← synthInstanceQ q(ZeroLEOneClass $α)
let _a ← synthInstanceQ q(NeZero (1 : $α))
assumeInstancesCommute
have p : Q(NormNum.IsNat $e $lit) := p
haveI' p' : Nat.ble 1 $lit =Q true := ⟨⟩
pure (.positive q(pos_of_isNat' (A := $α) $p $p'))
else
try
let _a ← synthInstanceQ q(Semiring $α)
let _a ← synthInstanceQ q(PartialOrder $α)
let _a ← synthInstanceQ q(IsOrderedRing $α)
assumeInstancesCommute
have p : Q(NormNum.IsNat $e $lit) := p
pure (.nonnegative q(nonneg_of_isNat $p))
catch e : Exception =>
trace[Tactic.positivity.failure] "{e.toMessageData}"
let _a ← synthInstanceQ q(AddMonoidWithOne $α)
let _a ← synthInstanceQ q(PartialOrder $α)
let _a ← synthInstanceQ q(AddLeftMono $α)
let _a ← synthInstanceQ q(ZeroLEOneClass $α)
assumeInstancesCommute
have p : Q(NormNum.IsNat $e $lit) := p
pure (.nonnegative q(nonneg_of_isNat' $p))
| .isNegNat _ lit p =>
let _a ← synthInstanceQ q(Ring $α)
let _a ← synthInstanceQ q(PartialOrder $α)
let _a ← synthInstanceQ q(IsStrictOrderedRing $α)
assumeInstancesCommute
have p : Q(NormNum.IsInt $e (Int.negOfNat $lit)) := p
haveI' p' : Nat.ble 1 $lit =Q true := ⟨⟩
pure (.nonzero q(nz_of_isNegNat $p $p'))
| .isNNRat _i q n d p =>
... | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | normNumPositivity | Attempts to prove a `Strictness` result when `e` evaluates to a literal number. |
positivityCanon (e : Q($α)) : MetaM (Strictness zα pα e) := do
let _add ← synthInstanceQ q(AddMonoid $α)
let _le ← synthInstanceQ q(PartialOrder $α)
let _i ← synthInstanceQ q(CanonicallyOrderedAdd $α)
assumeInstancesCommute
pure (.nonnegative q(zero_le $e)) | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | positivityCanon | Attempts to prove that `e ≥ 0` using `zero_le` in a `CanonicallyOrderedAdd` monoid. |
compareHypLE (lo e : Q($α)) (p₂ : Q($lo ≤ $e)) : MetaM (Strictness zα pα e) := do
match ← normNumPositivity zα pα lo with
| .positive p₁ => pure (.positive q(lt_of_lt_of_le $p₁ $p₂))
| .nonnegative p₁ => pure (.nonnegative q(le_trans $p₁ $p₂))
| _ => pure .none | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | compareHypLE | A variation on `assumption` when the hypothesis is `lo ≤ e` where `lo` is a numeral. |
compareHypLT (lo e : Q($α)) (p₂ : Q($lo < $e)) : MetaM (Strictness zα pα e) := do
match ← normNumPositivity zα pα lo with
| .positive p₁ => pure (.positive q(lt_trans $p₁ $p₂))
| .nonnegative p₁ => pure (.positive q(lt_of_le_of_lt $p₁ $p₂))
| _ => pure .none | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | compareHypLT | A variation on `assumption` when the hypothesis is `lo < e` where `lo` is a numeral. |
compareHypEq (e x : Q($α)) (p₂ : Q($x = $e)) : MetaM (Strictness zα pα e) := do
match ← normNumPositivity zα pα x with
| .positive p₁ => pure (.positive q(lt_of_lt_of_eq $p₁ $p₂))
| .nonnegative p₁ => pure (.nonnegative q(le_of_le_of_eq $p₁ $p₂))
| .nonzero p₁ => pure (.nonzero q(ne_of_ne_of_eq' $p₁ $p₂))
| .none => pure .none
initialize registerTraceClass `Tactic.positivity
initialize registerTraceClass `Tactic.positivity.failure | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | compareHypEq | A variation on `assumption` when the hypothesis is `x = e` where `x` is a numeral. |
compareHyp (e : Q($α)) (ldecl : LocalDecl) : MetaM (Strictness zα pα e) := do
have e' : Q(Prop) := ldecl.type
let p : Q($e') := .fvar ldecl.fvarId
match e' with
| ~q(@LE.le.{u} $β $_le $lo $hi) =>
let .defEq (_ : $α =Q $β) ← isDefEqQ α β | return .none
let .defEq _ ← isDefEqQ e hi | return .none
match lo with
| ~q(0) =>
assertInstancesCommute
return .nonnegative q($p)
| _ => compareHypLE zα pα lo e p
| ~q(@LT.lt.{u} $β $_lt $lo $hi) =>
let .defEq (_ : $α =Q $β) ← isDefEqQ α β | return .none
let .defEq _ ← isDefEqQ e hi | return .none
match lo with
| ~q(0) =>
assertInstancesCommute
return .positive q($p)
| _ => compareHypLT zα pα lo e p
| ~q(@Eq.{u+1} $α' $lhs $rhs) =>
let .defEq (_ : $α =Q $α') ← isDefEqQ α α' | pure .none
match ← isDefEqQ e rhs with
| .defEq _ =>
match lhs with
| ~q(0) => pure <| .nonnegative q(le_of_eq $p)
| _ => compareHypEq zα pα e lhs q($p)
| .notDefEq =>
let .defEq _ ← isDefEqQ e lhs | pure .none
match rhs with
| ~q(0) => pure <| .nonnegative q(ge_of_eq $p)
| _ => compareHypEq zα pα e rhs q(Eq.symm $p)
| ~q(@Ne.{u + 1} $α' $lhs $rhs) =>
let .defEq (_ : $α =Q $α') ← isDefEqQ α α' | pure .none
match lhs, rhs with
| ~q(0), _ =>
let .defEq _ ← isDefEqQ e rhs | pure .none
pure <| .nonzero q(Ne.symm $p)
| _, ~q(0) =>
let .defEq _ ← isDefEqQ e lhs | pure .none
pure <| .nonzero q($p)
| _, _ => pure .none
| _ => pure .none
variable {zα pα} in | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | compareHyp | A variation on `assumption` which checks if the hypothesis `ldecl` is `a [</≤/=] e`
where `a` is a numeral. |
orElse {e : Q($α)} (t₁ : Strictness zα pα e) (t₂ : MetaM (Strictness zα pα e)) :
MetaM (Strictness zα pα e) := do
match t₁ with
| .none => catchNone t₂
| p@(.positive _) => pure p
| .nonnegative p₁ =>
match ← catchNone t₂ with
| p@(.positive _) => pure p
| .nonzero p₂ => pure (.positive q(lt_of_le_of_ne' $p₁ $p₂))
| _ => pure (.nonnegative p₁)
| .nonzero p₁ =>
match ← catchNone t₂ with
| p@(.positive _) => pure p
| .nonnegative p₂ => pure (.positive q(lt_of_le_of_ne' $p₂ $p₁))
| _ => pure (.nonzero p₁) | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | orElse | The main combinator which combines multiple `positivity` results.
It assumes `t₁` has already been run for a result, and runs `t₂` and takes the best result.
It will skip `t₂` if `t₁` is already a proof of `.positive`, and can also combine
`.nonnegative` and `.nonzero` to produce a `.positive` result. |
core (e : Q($α)) : MetaM (Strictness zα pα e) := do
let mut result := .none
trace[Tactic.positivity] "trying to prove positivity of {e}"
for ext in ← (positivityExt.getState (← getEnv)).2.getMatch e do
try
result ← orElse result <| ext.eval zα pα e
catch err =>
trace[Tactic.positivity] "{e} failed: {err.toMessageData}"
result ← orElse result <| normNumPositivity zα pα e
result ← orElse result <| positivityCanon zα pα e
if let .positive _ := result then
trace[Tactic.positivity] "{e} => {result.toString}"
return result
for ldecl in ← getLCtx do
if !ldecl.isImplementationDetail then
result ← orElse result <| compareHyp zα pα e ldecl
trace[Tactic.positivity] "{e} => {result.toString}"
throwNone (pure result) | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | core | Run each registered `positivity` extension on an expression, returning a `NormNum.Result`. |
private OrderRel : Type
| le : OrderRel -- `0 ≤ a`
| lt : OrderRel -- `0 < a`
| ne : OrderRel -- `a ≠ 0`
| ne' : OrderRel -- `0 ≠ a` | inductive | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | OrderRel | null |
bestResult (e : Expr) : MetaM (Bool × Expr) := do
let ⟨u, α, _⟩ ← inferTypeQ' e
let zα ← synthInstanceQ q(Zero $α)
let pα ← synthInstanceQ q(PartialOrder $α)
match ← try? (Meta.Positivity.core zα pα e) with
| some (.positive pf) => pure (true, pf)
| some (.nonnegative pf) => pure (false, pf)
| _ => throwError "could not establish the nonnegativity of {e}" | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | bestResult | Given an expression `e`, use the core method of the `positivity` tactic to prove it positive,
or, failing that, nonnegative; return a Boolean (signalling whether the strict or non-strict
inequality was established) together with the proof as an expression. |
proveNonneg (e : Expr) : MetaM Expr := do
let (strict, pf) ← bestResult e
if strict then mkAppM ``le_of_lt #[pf] else pure pf | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | proveNonneg | Given an expression `e`, use the core method of the `positivity` tactic to prove it nonnegative. |
solve (t : Q(Prop)) : MetaM Expr := do
let rest {u : Level} (α : Q(Type u)) z e (relDesired : OrderRel) : MetaM Expr := do
let zα ← synthInstanceQ q(Zero $α)
assumeInstancesCommute
let .true ← isDefEq z q(0 : $α) | throwError "not a positivity goal"
let pα ← synthInstanceQ q(PartialOrder $α)
assumeInstancesCommute
let r ← catchNone <| Meta.Positivity.core zα pα e
let throw (a b : String) : MetaM Expr := throwError
"failed to prove {a}, but it would be possible to prove {b} if desired"
let p ← show MetaM Expr from match relDesired, r with
| .lt, .positive p
| .le, .nonnegative p
| .ne, .nonzero p => pure p
| .le, .positive p => pure q(le_of_lt $p)
| .ne, .positive p => pure q(ne_of_gt $p)
| .ne', .positive p => pure q(ne_of_lt $p)
| .ne', .nonzero p => pure q(Ne.symm $p)
| .lt, .nonnegative _ => throw "strict positivity" "nonnegativity"
| .lt, .nonzero _ => throw "strict positivity" "nonzeroness"
| .le, .nonzero _ => throw "nonnegativity" "nonzeroness"
| .ne, .nonnegative _
| .ne', .nonnegative _ => throw "nonzeroness" "nonnegativity"
| _, .none => throwError "failed to prove positivity/nonnegativity/nonzeroness"
pure p
match t with
| ~q(@LE.le $α $_a $z $e) => rest α z e .le
| ~q(@LT.lt $α $_a $z $e) => rest α z e .lt
| ~q($a ≠ ($b : ($α : Type _))) =>
let _zα ← synthInstanceQ q(Zero $α)
if ← isDefEq b q((0 : $α)) then
rest α b a .ne
else
let .true ← isDefEq a q((0 : $α)) | throwError "not a positivity goal"
rest α a b .ne'
| _ => throwError "not a positivity goal" | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | solve | An auxiliary entry point to the `positivity` tactic. Given a proposition `t` of the form
`0 [≤/</≠] e`, attempts to recurse on the structure of `t` to prove it. It returns a proof
or fails. |
positivity (goal : MVarId) : MetaM Unit := do
let t : Q(Prop) ← withReducible goal.getType'
let p ← solve t
goal.assign p | def | Tactic | [
"Mathlib.Tactic.NormNum.Core",
"Mathlib.Tactic.HaveI",
"Mathlib.Algebra.Order.Invertible",
"Mathlib.Algebra.Order.Ring.Cast",
"Mathlib.Control.Basic",
"Mathlib.Data.Nat.Cast.Basic",
"Qq"
] | Mathlib/Tactic/Positivity/Core.lean | positivity | The main entry point to the `positivity` tactic. Given a goal `goal` of the form `0 [≤/</≠] e`,
attempts to recurse on the structure of `e` to prove the goal.
It will either close `goal` or fail. |
@[positivity Finset.card _]
evalFinsetCard : PositivityExt where eval {u α} _ _ e := do
match u, α, e with
| 0, ~q(ℕ), ~q(Finset.card $s) =>
let some ps ← proveFinsetNonempty s | return .none
assertInstancesCommute
return .positive q(Finset.Nonempty.card_pos $ps)
| _ => throwError "not Finset.card" | def | Tactic | [
"Mathlib.Algebra.Order.BigOperators.Group.Finset",
"Mathlib.Data.Finset.Density",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Tactic.Positivity.Core"
] | Mathlib/Tactic/Positivity/Finset.lean | evalFinsetCard | Extension for `Finset.card`. `#s` is positive if `s` is nonempty.
It calls `Mathlib.Meta.proveFinsetNonempty` to attempt proving that the finset is nonempty. |
@[positivity Fintype.card _]
evalFintypeCard : PositivityExt where eval {u α} _ _ e := do
match u, α, e with
| 0, ~q(ℕ), ~q(@Fintype.card $β $instβ) =>
let instβno ← synthInstanceQ q(Nonempty $β)
assumeInstancesCommute
return .positive q(@Fintype.card_pos $β $instβ $instβno)
| _ => throwError "not Fintype.card" | def | Tactic | [
"Mathlib.Algebra.Order.BigOperators.Group.Finset",
"Mathlib.Data.Finset.Density",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Tactic.Positivity.Core"
] | Mathlib/Tactic/Positivity/Finset.lean | evalFintypeCard | Extension for `Fintype.card`. `Fintype.card α` is positive if `α` is nonempty. |
@[positivity Finset.dens _]
evalFinsetDens : PositivityExt where eval {u 𝕜} _ _ e := do
match u, 𝕜, e with
| 0, ~q(ℚ≥0), ~q(@Finset.dens $α $instα $s) =>
let some ps ← proveFinsetNonempty s | return .none
assumeInstancesCommute
return .positive q(@Nonempty.dens_pos $α $instα $s $ps)
| _, _, _ => throwError "not Finset.dens"
attribute [local instance] monadLiftOptionMetaM in | def | Tactic | [
"Mathlib.Algebra.Order.BigOperators.Group.Finset",
"Mathlib.Data.Finset.Density",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Tactic.Positivity.Core"
] | Mathlib/Tactic/Positivity/Finset.lean | evalFinsetDens | Extension for `Finset.dens`. `s.dens` is positive if `s` is nonempty.
It calls `Mathlib.Meta.proveFinsetNonempty` to attempt proving that the finset is nonempty. |
@[positivity Finset.sum _ _]
evalFinsetSum : PositivityExt where eval {u α} zα pα e := do
match e with
| ~q(@Finset.sum $ι _ $instα $s $f) =>
let i : Q($ι) ← mkFreshExprMVarQ q($ι) .syntheticOpaque
have body : Q($α) := .betaRev f #[i]
let rbody ← core zα pα body
let p_pos : Option Q(0 < $e) := ← (do
let .positive pbody := rbody | pure none -- Fail if the body is not provably positive
let some ps ← proveFinsetNonempty s | pure none
let .some pα' ← trySynthInstanceQ q(IsOrderedCancelAddMonoid $α) | pure none
assertInstancesCommute
let pr : Q(∀ i, 0 < $f i) ← mkLambdaFVars #[i] pbody
return some q(@sum_pos $ι $α $instα $pα $pα' $f $s (fun i _ ↦ $pr i) $ps))
if let some p_pos := p_pos then
return .positive p_pos
else
let pbody ← rbody.toNonneg
let pr : Q(∀ i, 0 ≤ $f i) ← mkLambdaFVars #[i] pbody
let pα' ← synthInstanceQ q(IsOrderedAddMonoid $α)
assertInstancesCommute
return .nonnegative q(@sum_nonneg $ι $α $instα $pα $pα' $f $s fun i _ ↦ $pr i)
| _ => throwError "not Finset.sum"
variable {α : Type*} {s : Finset α} | def | Tactic | [
"Mathlib.Algebra.Order.BigOperators.Group.Finset",
"Mathlib.Data.Finset.Density",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Tactic.Positivity.Core"
] | Mathlib/Tactic/Positivity/Finset.lean | evalFinsetSum | The `positivity` extension which proves that `∑ i ∈ s, f i` is nonnegative if `f` is, and
positive if each `f i` is and `s` is nonempty.
TODO: The following example does not work
```
example (s : Finset ℕ) (f : ℕ → ℤ) (hf : ∀ n, 0 ≤ f n) : 0 ≤ s.sum f := by positivity
```
because `compareHyp` can't look for assumptions behind binders. |
Head where
| const (c : Name)
| lambda
| forall
deriving Inhabited, BEq | inductive | Tactic | [
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Push/Attr.lean | Head | The type for a constant to be pushed by `push`. |
Head.toString : Head → String
| .const c => c.toString
| .lambda => "fun"
| .forall => "Forall" | def | Tactic | [
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Push/Attr.lean | Head.toString | Converts a `Head` to a string. |
Head.ofExpr? : Expr → Option Head
| .app f _ => f.getAppFn.constName?.map .const
| .lam .. => some .lambda
| .forallE .. => some .forall
| _ => none | def | Tactic | [
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Push/Attr.lean | Head.ofExpr | Returns the head of an expression. |
isPullThm (declName : Name) (inv : Bool) : MetaM (Option Head) := do
let cinfo ← getConstInfo declName
forallTelescope cinfo.type fun _ type => do
let some (lhs, rhs) := type.eqOrIff? | return none
let (lhs, rhs) := if inv then (rhs, lhs) else (lhs, rhs)
let some head := Head.ofExpr? rhs | return none
if Head.ofExpr? lhs != some head && containsHead lhs head then
return head
return none
where
/-- Checks if the expression has the head in any subexpression.
We don't need to check this for `.lambda`, because the term being a function
is sufficient for `pull fun _ ↦ _` to be applicable. -/
containsHead (e : Expr) : Head → Bool
| .const c => e.containsConst (· == c)
| .lambda => true
| .forall => (e.find? (· matches .forallE ..)).isSome | def | Tactic | [
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Push/Attr.lean | isPullThm | The `push` environment extension -/
initialize pushExt : SimpleScopedEnvExtension SimpTheorem (DiscrTree SimpTheorem) ←
registerSimpleScopedEnvExtension {
initial := {}
addEntry := fun d e => d.insertCore e.keys e
}
/--
Checks if the theorem is suitable for the `pull` tactic. That is,
check if it is of the form `x = f ...` where `x` contains the head `f`,
but `f` is not the head of `x`. |
PullTheorem := SimpTheorem × Head | abbrev | Tactic | [
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Push/Attr.lean | PullTheorem | A theorem for the `pull` tactic |
@[push] log_mul (hx : x ≠ 0) (hy : y ≠ 0) : log (x * y) = log x + log y
@[push] theorem log_abs : log |x| = log x
@[push] theorem not_imp (p q : Prop) : ¬(p → q) ↔ p ∧ ¬q
@[push] theorem not_iff (p q : Prop) : ¬(p ↔ q) ↔ (p ∧ ¬q) ∨ (¬p ∧ q)
@[push] theorem not_not (p : Prop) : ¬ ¬p ↔ p
@[push] theorem not_le : ¬a ≤ b ↔ b < a
```
Note that some `push` lemmas don't push the constant away from the head (`log_abs`) and
some `push` lemmas cancel the constant out (`not_not` and `not_le`).
For the other lemmas that are "genuine" `push` lemmas, a `pull` attribute is automatically added
in the reverse direction. To not add a `pull` tag, use `@[push only]`.
To tag the reverse direction of the lemma, use `@[push ←]`.
-/
syntax (name := pushAttr) "push" (" ←" <|> " <-")? (&" only")? (ppSpace prio)? : attr
@[inherit_doc pushAttr]
initialize registerBuiltinAttribute {
name := `pushAttr
descr := "attribute for push"
add := fun declName stx kind => MetaM.run' do
let inv := !stx[1].isNone
let isOnly := !stx[2].isNone
let prio ← getAttrParamOptPrio stx[3]
let #[thm] ← mkSimpTheoremFromConst declName (inv := inv) (prio := prio) |
throwError "couldn't generate a simp theorem for `push`"
pushExt.add thm
unless isOnly do
let inv := !inv -- the `pull` lemma is added in the reverse direction
if let some head ← isPullThm declName inv then
let #[thm] ← mkSimpTheoremFromConst declName (inv := inv) (prio := prio) |
throwError "couldn't generate a simp theorem for `pull`"
pullExt.add (thm, head)
} | theorem | Tactic | [
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Push/Attr.lean | log_mul | null |
rflTac : TacticM Unit :=
withMainContext do liftMetaFinishingTactic (·.applyRfl) | def | Tactic | [
"Mathlib.Init",
"Lean.Meta.Tactic.Rfl"
] | Mathlib/Tactic/Relation/Rfl.lean | rflTac | This tactic applies to a goal whose target has the form `x ~ x`, where `~` is a reflexive
relation, that is, a relation which has a reflexive lemma tagged with the attribute [refl]. |
_root_.Lean.Expr.relSidesIfRefl? (e : Expr) : MetaM (Option (Name × Expr × Expr)) := do
if let some (_, lhs, rhs) := e.eq? then
return (``Eq, lhs, rhs)
if let some (lhs, rhs) := e.iff? then
return (``Iff, lhs, rhs)
if let some (_, lhs, _, rhs) := e.heq? then
return (``HEq, lhs, rhs)
if let .app (.app rel lhs) rhs := e then
unless (← (reflExt.getState (← getEnv)).getMatch rel).isEmpty do
match rel.getAppFn.constName? with
| some n => return some (n, lhs, rhs)
| none => return none
return none | def | Tactic | [
"Mathlib.Init",
"Lean.Meta.Tactic.Rfl"
] | Mathlib/Tactic/Relation/Rfl.lean | _root_.Lean.Expr.relSidesIfRefl | If `e` is the form `@R .. x y`, where `R` is a reflexive
relation, return `some (R, x, y)`.
As a special case, if `e` is `@HEq α a β b`, return ``some (`HEq, a, b)``. |
_root_.Lean.Expr.relSidesIfSymm? (e : Expr) : MetaM (Option (Name × Expr × Expr)) := do
if let some (_, lhs, rhs) := e.eq? then
return (``Eq, lhs, rhs)
if let some (lhs, rhs) := e.iff? then
return (``Iff, lhs, rhs)
if let some (_, lhs, _, rhs) := e.heq? then
return (``HEq, lhs, rhs)
if let .app (.app rel lhs) rhs := e then
unless (← (symmExt.getState (← getEnv)).getMatch rel).isEmpty do
match rel.getAppFn.constName? with
| some n => return some (n, lhs, rhs)
| none => return none
return none | def | Tactic | [
"Mathlib.Init",
"Lean.Meta.Tactic.Symm"
] | Mathlib/Tactic/Relation/Symm.lean | _root_.Lean.Expr.relSidesIfSymm | If `e` is the form `@R .. x y`, where `R` is a symmetric
relation, return `some (R, x, y)`.
As a special case, if `e` is `@HEq α a β b`, return ``some (`HEq, a, b)``. |
instCommSemiringNat : CommSemiring ℕ := inferInstance | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | instCommSemiringNat | A shortcut instance for `CommSemiring ℕ` used by ring. |
instCommSemiringInt : CommSemiring ℤ := inferInstance | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | instCommSemiringInt | A shortcut instance for `CommSemiring ℤ` used by ring. |
sℕ : Q(CommSemiring ℕ) := q(instCommSemiringNat) | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | sℕ | A typed expression of type `CommSemiring ℕ` used when we are working on
ring subexpressions of type `ℕ`. |
sℤ : Q(CommSemiring ℤ) := q(instCommSemiringInt)
mutual | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | sℤ | A typed expression of type `CommSemiring ℤ` used when we are working on
ring subexpressions of type `ℤ`. |
ExBase : ∀ {u : Lean.Level} {α : Q(Type u)}, Q(CommSemiring $α) → (e : Q($α)) → Type
/--
An atomic expression `e` with id `id`.
Atomic expressions are those which `ring` cannot parse any further.
For instance, `a + (a % b)` has `a` and `(a % b)` as atoms.
The `ring1` tactic does not normalize the subexpressions in atoms, but `ring_nf` does.
Atoms in fact represent equivalence classes of expressions, modulo definitional equality.
The field `index : ℕ` should be a unique number for each class,
while `value : expr` contains a representative of this class.
The function `resolve_atom` determines the appropriate atom for a given expression.
-/
| atom {sα} {e} (id : ℕ) : ExBase sα e
/-- A sum of monomials. -/
| sum {sα} {e} (_ : ExSum sα e) : ExBase sα e | inductive | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExBase | The base `e` of a normalized exponent expression. |
ExProd : ∀ {u : Lean.Level} {α : Q(Type u)}, Q(CommSemiring $α) → (e : Q($α)) → Type
/-- A coefficient `value`, which must not be `0`. `e` is a raw rat cast.
If `value` is not an integer, then `hyp` should be a proof of `(value.den : α) ≠ 0`. -/
| const {sα} {e} (value : ℚ) (hyp : Option Expr := none) : ExProd sα e
/-- A product `x ^ e * b` is a monomial if `b` is a monomial. Here `x` is an `ExBase`
and `e` is an `ExProd` representing a monomial expression in `ℕ` (it is a monomial instead of
a polynomial because we eagerly normalize `x ^ (a + b) = x ^ a * x ^ b`.) -/
| mul {u : Lean.Level} {α : Q(Type u)} {sα} {x : Q($α)} {e : Q(ℕ)} {b : Q($α)} :
ExBase sα x → ExProd sℕ e → ExProd sα b → ExProd sα q($x ^ $e * $b) | inductive | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExProd | A monomial, which is a product of powers of `ExBase` expressions,
terminated by a (nonzero) constant coefficient. |
ExSum : ∀ {u : Lean.Level} {α : Q(Type u)}, Q(CommSemiring $α) → (e : Q($α)) → Type
/-- Zero is a polynomial. `e` is the expression `0`. -/
| zero {u : Lean.Level} {α : Q(Type u)} {sα : Q(CommSemiring $α)} : ExSum sα q(0 : $α)
/-- A sum `a + b` is a polynomial if `a` is a monomial and `b` is another polynomial. -/
| add {u : Lean.Level} {α : Q(Type u)} {sα : Q(CommSemiring $α)} {a b : Q($α)} :
ExProd sα a → ExSum sα b → ExSum sα q($a + $b) | inductive | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExSum | A polynomial expression, which is a sum of monomials. |
partial ExBase.eq
{u : Lean.Level} {α : Q(Type u)} {sα : Q(CommSemiring $α)} {a b : Q($α)} :
ExBase sα a → ExBase sα b → Bool
| .atom i, .atom j => i == j
| .sum a, .sum b => a.eq b
| _, _ => false
@[inherit_doc ExBase.eq] | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExBase.eq | Equality test for expressions. This is not a `BEq` instance because it is heterogeneous. |
partial ExProd.eq
{u : Lean.Level} {α : Q(Type u)} {sα : Q(CommSemiring $α)} {a b : Q($α)} :
ExProd sα a → ExProd sα b → Bool
| .const i _, .const j _ => i == j
| .mul a₁ a₂ a₃, .mul b₁ b₂ b₃ => a₁.eq b₁ && a₂.eq b₂ && a₃.eq b₃
| _, _ => false
@[inherit_doc ExBase.eq] | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExProd.eq | null |
partial ExSum.eq
{u : Lean.Level} {α : Q(Type u)} {sα : Q(CommSemiring $α)} {a b : Q($α)} :
ExSum sα a → ExSum sα b → Bool
| .zero, .zero => true
| .add a₁ a₂, .add b₁ b₂ => a₁.eq b₁ && a₂.eq b₂
| _, _ => false | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExSum.eq | null |
partial ExBase.cmp
{u : Lean.Level} {α : Q(Type u)} {sα : Q(CommSemiring $α)} {a b : Q($α)} :
ExBase sα a → ExBase sα b → Ordering
| .atom i, .atom j => compare i j
| .sum a, .sum b => a.cmp b
| .atom .., .sum .. => .lt
| .sum .., .atom .. => .gt
@[inherit_doc ExBase.cmp] | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExBase.cmp | A total order on normalized expressions.
This is not an `Ord` instance because it is heterogeneous. |
partial ExProd.cmp
{u : Lean.Level} {α : Q(Type u)} {sα : Q(CommSemiring $α)} {a b : Q($α)} :
ExProd sα a → ExProd sα b → Ordering
| .const i _, .const j _ => compare i j
| .mul a₁ a₂ a₃, .mul b₁ b₂ b₃ => (a₁.cmp b₁).then (a₂.cmp b₂) |>.then (a₃.cmp b₃)
| .const _ _, .mul .. => .lt
| .mul .., .const _ _ => .gt
@[inherit_doc ExBase.cmp] | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExProd.cmp | null |
partial ExSum.cmp
{u : Lean.Level} {α : Q(Type u)} {sα : Q(CommSemiring $α)} {a b : Q($α)} :
ExSum sα a → ExSum sα b → Ordering
| .zero, .zero => .eq
| .add a₁ a₂, .add b₁ b₂ => (a₁.cmp b₁).then (a₂.cmp b₂)
| .zero, .add .. => .lt
| .add .., .zero => .gt | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExSum.cmp | null |
partial ExBase.cast
{v : Lean.Level} {β : Q(Type v)} {sβ : Q(CommSemiring $β)} {a : Q($α)} :
ExBase sα a → Σ a, ExBase sβ a
| .atom i => ⟨a, .atom i⟩
| .sum a => let ⟨_, vb⟩ := a.cast; ⟨_, .sum vb⟩ | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExBase.cast | Converts `ExBase sα` to `ExBase sβ`, assuming `sα` and `sβ` are defeq. |
partial ExProd.cast
{v : Lean.Level} {β : Q(Type v)} {sβ : Q(CommSemiring $β)} {a : Q($α)} :
ExProd sα a → Σ a, ExProd sβ a
| .const i h => ⟨a, .const i h⟩
| .mul a₁ a₂ a₃ => ⟨_, .mul a₁.cast.2 a₂ a₃.cast.2⟩ | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExProd.cast | Converts `ExProd sα` to `ExProd sβ`, assuming `sα` and `sβ` are defeq. |
partial ExSum.cast
{v : Lean.Level} {β : Q(Type v)} {sβ : Q(CommSemiring $β)} {a : Q($α)} :
ExSum sα a → Σ a, ExSum sβ a
| .zero => ⟨_, .zero⟩
| .add a₁ a₂ => ⟨_, .add a₁.cast.2 a₂.cast.2⟩ | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExSum.cast | Converts `ExSum sα` to `ExSum sβ`, assuming `sα` and `sβ` are defeq. |
Result {α : Q(Type u)} (E : Q($α) → Type) (e : Q($α)) where
/-- The normalized result. -/
expr : Q($α)
/-- The data associated to the normalization. -/
val : E expr
/-- A proof that the original expression is equal to the normalized result. -/
proof : Q($e = $expr) | structure | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | Result | The result of evaluating an (unnormalized) expression `e` into the type family `E`
(one of `ExSum`, `ExProd`, `ExBase`) is a (normalized) element `e'`
and a representation `E e'` for it, and a proof of `e = e'`. |
ExProd.mkNat (n : ℕ) : (e : Q($α)) × ExProd sα e :=
let lit : Q(ℕ) := mkRawNatLit n
⟨q(($lit).rawCast : $α), .const n none⟩ | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExProd.mkNat | Constructs the expression corresponding to `.const n`.
(The `.const` constructor does not check that the expression is correct.) |
ExProd.mkNegNat (_ : Q(Ring $α)) (n : ℕ) : (e : Q($α)) × ExProd sα e :=
let lit : Q(ℕ) := mkRawNatLit n
⟨q((Int.negOfNat $lit).rawCast : $α), .const (-n) none⟩ | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExProd.mkNegNat | Constructs the expression corresponding to `.const (-n)`.
(The `.const` constructor does not check that the expression is correct.) |
ExProd.mkNNRat (_ : Q(DivisionSemiring $α)) (q : ℚ) (n : Q(ℕ)) (d : Q(ℕ)) (h : Expr) :
(e : Q($α)) × ExProd sα e :=
⟨q(NNRat.rawCast $n $d : $α), .const q h⟩ | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExProd.mkNNRat | Constructs the expression corresponding to `.const q h` for `q = n / d`
and `h` a proof that `(d : α) ≠ 0`.
(The `.const` constructor does not check that the expression is correct.) |
ExProd.mkNegNNRat (_ : Q(DivisionRing $α)) (q : ℚ) (n : Q(ℕ)) (d : Q(ℕ)) (h : Expr) :
(e : Q($α)) × ExProd sα e :=
⟨q(Rat.rawCast (.negOfNat $n) $d : $α), .const q h⟩ | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExProd.mkNegNNRat | Constructs the expression corresponding to `.const q h` for `q = -(n / d)`
and `h` a proof that `(d : α) ≠ 0`.
(The `.const` constructor does not check that the expression is correct.) |
ExBase.toProd {α : Q(Type u)} {sα : Q(CommSemiring $α)} {a : Q($α)} {b : Q(ℕ)}
(va : ExBase sα a) (vb : ExProd sℕ b) :
ExProd sα q($a ^ $b * (nat_lit 1).rawCast) := .mul va vb (.const 1 none) | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExBase.toProd | Embed an exponent (an `ExBase, ExProd` pair) as an `ExProd` by multiplying by 1. |
ExProd.toSum {sα : Q(CommSemiring $α)} {e : Q($α)} (v : ExProd sα e) : ExSum sα q($e + 0) :=
.add v .zero | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExProd.toSum | Embed `ExProd` in `ExSum` by adding 0. |
ExProd.coeff {sα : Q(CommSemiring $α)} {e : Q($α)} : ExProd sα e → ℚ
| .const q _ => q
| .mul _ _ v => v.coeff | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | ExProd.coeff | Get the leading coefficient of an `ExProd`. |
Overlap (e : Q($α)) where
/-- The expression `e` (the sum of monomials) is equal to `0`. -/
| zero (_ : Q(IsNat $e (nat_lit 0)))
/-- The expression `e` (the sum of monomials) is equal to another monomial
(with nonzero leading coefficient). -/
| nonzero (_ : Result (ExProd sα) e)
variable {a a' a₁ a₂ a₃ b b' b₁ b₂ b₃ c c₁ c₂ : R}
/-! ### Addition -/ | inductive | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | Overlap | Two monomials are said to "overlap" if they differ by a constant factor, in which case the
constants just add. When this happens, the constant may be either zero (if the monomials cancel)
or nonzero (if they add up); the zero case is handled specially. |
add_overlap_pf (x : R) (e) (pq_pf : a + b = c) :
x ^ e * a + x ^ e * b = x ^ e * c := by subst_vars; simp [mul_add] | theorem | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | add_overlap_pf | null |
add_overlap_pf_zero (x : R) (e) :
IsNat (a + b) (nat_lit 0) → IsNat (x ^ e * a + x ^ e * b) (nat_lit 0)
| ⟨h⟩ => ⟨by simp [h, ← mul_add]⟩
private local instance {m} [Pure m] : MonadLift Option (OptionT m) where
monadLift f := .mk <| pure f | theorem | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | add_overlap_pf_zero | null |
evalAddOverlap {a b : Q($α)} (va : ExProd sα a) (vb : ExProd sα b) :
OptionT MetaM (Overlap sα q($a + $b)) := do
Lean.Core.checkSystem decl_name%.toString
match va, vb with
| .const za ha, .const zb hb => do
let ra := Result.ofRawRat za a ha; let rb := Result.ofRawRat zb b hb
let res ← ra.add rb
match res with
| .isNat _ (.lit (.natVal 0)) p => pure <| .zero p
| rc =>
let ⟨zc, hc⟩ ← rc.toRatNZ
let ⟨c, pc⟩ := rc.toRawEq
pure <| .nonzero ⟨c, .const zc hc, pc⟩
| .mul (x := a₁) (e := a₂) va₁ va₂ va₃, .mul vb₁ vb₂ vb₃ => do
guard (va₁.eq vb₁ && va₂.eq vb₂)
match ← evalAddOverlap va₃ vb₃ with
| .zero p => pure <| .zero (q(add_overlap_pf_zero $a₁ $a₂ $p) : Expr)
| .nonzero ⟨_, vc, p⟩ =>
pure <| .nonzero ⟨_, .mul va₁ va₂ vc, (q(add_overlap_pf $a₁ $a₂ $p) : Expr)⟩
| _, _ => OptionT.fail | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | evalAddOverlap | Given monomials `va, vb`, attempts to add them together to get another monomial.
If the monomials are not compatible, returns `none`.
For example, `xy + 2xy = 3xy` is a `.nonzero` overlap, while `xy + xz` returns `none`
and `xy + -xy = 0` is a `.zero` overlap. |
add_pf_zero_add (b : R) : 0 + b = b := by simp | theorem | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | add_pf_zero_add | null |
add_pf_add_zero (a : R) : a + 0 = a := by simp | theorem | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | add_pf_add_zero | null |
add_pf_add_overlap
(_ : a₁ + b₁ = c₁) (_ : a₂ + b₂ = c₂) : (a₁ + a₂ : R) + (b₁ + b₂) = c₁ + c₂ := by
subst_vars; simp [add_assoc, add_left_comm] | theorem | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | add_pf_add_overlap | null |
add_pf_add_overlap_zero
(h : IsNat (a₁ + b₁) (nat_lit 0)) (h₄ : a₂ + b₂ = c) : (a₁ + a₂ : R) + (b₁ + b₂) = c := by
subst_vars; rw [add_add_add_comm, h.1, Nat.cast_zero, add_pf_zero_add] | theorem | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | add_pf_add_overlap_zero | null |
add_pf_add_lt (a₁ : R) (_ : a₂ + b = c) : (a₁ + a₂) + b = a₁ + c := by simp [*, add_assoc] | theorem | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | add_pf_add_lt | null |
add_pf_add_gt (b₁ : R) (_ : a + b₂ = c) : a + (b₁ + b₂) = b₁ + c := by
subst_vars; simp [add_left_comm] | theorem | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | add_pf_add_gt | null |
partial evalAdd {a b : Q($α)} (va : ExSum sα a) (vb : ExSum sα b) :
MetaM <| Result (ExSum sα) q($a + $b) := do
Lean.Core.checkSystem decl_name%.toString
match va, vb with
| .zero, vb => return ⟨b, vb, q(add_pf_zero_add $b)⟩
| va, .zero => return ⟨a, va, q(add_pf_add_zero $a)⟩
| .add (a := a₁) (b := _a₂) va₁ va₂, .add (a := b₁) (b := _b₂) vb₁ vb₂ =>
match ← (evalAddOverlap sα va₁ vb₁).run with
| some (.nonzero ⟨_, vc₁, pc₁⟩) =>
let ⟨_, vc₂, pc₂⟩ ← evalAdd va₂ vb₂
return ⟨_, .add vc₁ vc₂, q(add_pf_add_overlap $pc₁ $pc₂)⟩
| some (.zero pc₁) =>
let ⟨c₂, vc₂, pc₂⟩ ← evalAdd va₂ vb₂
return ⟨c₂, vc₂, q(add_pf_add_overlap_zero $pc₁ $pc₂)⟩
| none =>
if let .lt := va₁.cmp vb₁ then
let ⟨_c, vc, (pc : Q($_a₂ + ($b₁ + $_b₂) = $_c))⟩ ← evalAdd va₂ vb
return ⟨_, .add va₁ vc, q(add_pf_add_lt $a₁ $pc)⟩
else
let ⟨_c, vc, (pc : Q($a₁ + $_a₂ + $_b₂ = $_c))⟩ ← evalAdd va vb₂
return ⟨_, .add vb₁ vc, q(add_pf_add_gt $b₁ $pc)⟩
/-! ### Multiplication -/ | def | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | evalAdd | Adds two polynomials `va, vb` together to get a normalized result polynomial.
* `0 + b = b`
* `a + 0 = a`
* `a * x + a * y = a * (x + y)` (for `x`, `y` coefficients; uses `evalAddOverlap`)
* `(a₁ + a₂) + (b₁ + b₂) = a₁ + (a₂ + (b₁ + b₂))` (if `a₁.lt b₁`)
* `(a₁ + a₂) + (b₁ + b₂) = b₁ + ((a₁ + a₂) + b₂)` (if not `a₁.lt b₁`) |
one_mul (a : R) : (nat_lit 1).rawCast * a = a := by simp [Nat.rawCast] | theorem | Tactic | [
"Mathlib.Tactic.NormNum.Inv",
"Mathlib.Tactic.NormNum.Pow",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Ring/Basic.lean | one_mul | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.