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
isFibAux_two_mul_done {n a b n' a' : ℕ} (H : IsFibAux n a b) (hn : 2 * n = n') (h : a * (2 * b - a) = a') : fib n' = a' := (isFibAux_two_mul H hn h rfl).1
theorem
Tactic
[ "Mathlib.Data.Nat.Fib.Basic", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatFib.lean
isFibAux_two_mul_done
null
isFibAux_two_mul_add_one_done {n a b n' a' : ℕ} (H : IsFibAux n a b) (hn : 2 * n + 1 = n') (h : a * a + b * b = a') : fib n' = a' := (isFibAux_two_mul_add_one H hn h rfl).1
theorem
Tactic
[ "Mathlib.Data.Nat.Fib.Basic", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatFib.lean
isFibAux_two_mul_add_one_done
null
proveNatFib (en' : Q(ℕ)) : (em : Q(ℕ)) × Q(Nat.fib $en' = $em) := match en'.natLit! with | 0 => have : $en' =Q nat_lit 0 := ⟨⟩; ⟨q(nat_lit 0), q(Nat.fib_zero)⟩ | 1 => have : $en' =Q nat_lit 1 := ⟨⟩; ⟨q(nat_lit 1), q(Nat.fib_one)⟩ | 2 => have : $en' =Q nat_lit 2 := ⟨⟩; ⟨q(nat_lit 1), q(Nat.fib_two)⟩ | n' => have en : Q(ℕ) := mkRawNatLit <| n' / 2 let ⟨ea, eb, H⟩ := proveNatFibAux en let a := ea.natLit! let b := eb.natLit! if n' % 2 == 0 then have hn : Q(2 * $en = $en') := (q(Eq.refl $en') : Expr) have ea' : Q(ℕ) := mkRawNatLit <| a * (2 * b - a) have h1 : Q($ea * (2 * $eb - $ea) = $ea') := (q(Eq.refl $ea') : Expr) ⟨ea', q(isFibAux_two_mul_done $H $hn $h1)⟩ else have hn : Q(2 * $en + 1 = $en') := (q(Eq.refl $en') : Expr) have ea' : Q(ℕ) := mkRawNatLit <| a * a + b * b have h1 : Q($ea * $ea + $eb * $eb = $ea') := (q(Eq.refl $ea') : Expr) ⟨ea', q(isFibAux_two_mul_add_one_done $H $hn $h1)⟩
def
Tactic
[ "Mathlib.Data.Nat.Fib.Basic", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatFib.lean
proveNatFib
Given the natural number literal `ex`, returns `Nat.fib ex` as a natural number literal and an equality proof. Panics if `ex` isn't a natural number literal.
isNat_fib : {x nx z : ℕ} → IsNat x nx → Nat.fib nx = z → IsNat (Nat.fib x) z | _, _, _, ⟨rfl⟩, rfl => ⟨rfl⟩
theorem
Tactic
[ "Mathlib.Data.Nat.Fib.Basic", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatFib.lean
isNat_fib
null
@[norm_num Nat.fib _] evalNatFib : NormNumExt where eval {_ _} e := do let .app _ (x : Q(ℕ)) ← Meta.whnfR e | failure let sℕ : Q(AddMonoidWithOne ℕ) := q(instAddMonoidWithOneNat) let ⟨ex, p⟩ ← deriveNat x sℕ let ⟨ey, pf⟩ := proveNatFib ex let pf' : Q(IsNat (Nat.fib $x) $ey) := q(isNat_fib $p $pf) return .isNat sℕ ey pf'
def
Tactic
[ "Mathlib.Data.Nat.Fib.Basic", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatFib.lean
evalNatFib
Evaluates the `Nat.fib` function.
private nat_log_zero (n : Nat) : Nat.log 0 n = 0 := Nat.log_zero_left n
lemma
Tactic
[ "Mathlib.Data.Nat.Log", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatLog.lean
nat_log_zero
null
private nat_log_one (n : Nat) : Nat.log 1 n = 0 := Nat.log_one_left n
lemma
Tactic
[ "Mathlib.Data.Nat.Log", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatLog.lean
nat_log_one
null
private nat_log_helper0 (b n : Nat) (hl : Nat.blt n b = true) : Nat.log b n = 0 := by rw [Nat.blt_eq] at hl simp [hl]
lemma
Tactic
[ "Mathlib.Data.Nat.Log", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatLog.lean
nat_log_helper0
null
private nat_log_helper (b n k : Nat) (hl : Nat.ble (b ^ k) n = true) (hh : Nat.blt n (b ^ (k + 1)) = true) : Nat.log b n = k := Nat.log_eq_of_pow_le_of_lt_pow (Nat.le_of_ble_eq_true hl) (Nat.le_of_ble_eq_true hh)
lemma
Tactic
[ "Mathlib.Data.Nat.Log", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatLog.lean
nat_log_helper
null
private isNat_log : {b nb n nn k : ℕ} → IsNat b nb → IsNat n nn → Nat.log nb nn = k → IsNat (Nat.log b n) k | _, _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨rfl⟩
theorem
Tactic
[ "Mathlib.Data.Nat.Log", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatLog.lean
isNat_log
null
proveNatLog (eb en : Q(ℕ)) : (ek : Q(ℕ)) × Q(Nat.log $eb $en = $ek) := match eb.natLit!, en.natLit! with | 0, _ => have : $eb =Q nat_lit 0 := ⟨⟩; ⟨q(nat_lit 0), q(nat_log_zero $en)⟩ | 1, _ => have : $eb =Q nat_lit 1 := ⟨⟩; ⟨q(nat_lit 0), q(nat_log_one $en)⟩ | b, n => if n < b then have hh : Q(Nat.blt $en $eb = true) := (q(Eq.refl true) : Expr) ⟨q(nat_lit 0), q(nat_log_helper0 $eb $en $hh)⟩ else let k := Nat.log b n have ek : Q(ℕ) := mkRawNatLit k have hl : Q(Nat.ble ($eb ^ $ek) $en = true) := (q(Eq.refl true) : Expr) have hh : Q(Nat.blt $en ($eb ^ ($ek + 1)) = true) := (q(Eq.refl true) : Expr) ⟨ek, q(nat_log_helper $eb $en $ek $hl $hh)⟩
def
Tactic
[ "Mathlib.Data.Nat.Log", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatLog.lean
proveNatLog
Given the natural number literals `eb` and `en`, returns `Nat.log eb en` as a natural number literal and an equality proof. Panics if `ex` or `en` aren't natural number literals.
@[norm_num Nat.log _ _] evalNatLog : NormNumExt where eval {u α} e := do let mkApp2 _ (b : Q(ℕ)) (n : Q(ℕ)) ← Meta.whnfR e | failure let sℕ : Q(AddMonoidWithOne ℕ) := q(instAddMonoidWithOneNat) let ⟨eb, pb⟩ ← deriveNat b sℕ let ⟨en, pn⟩ ← deriveNat n sℕ let ⟨ek, pf⟩ := proveNatLog eb en let pf' : Q(IsNat (Nat.log $b $n) $ek) := q(isNat_log $pb $pn $pf) return .isNat sℕ ek pf'
def
Tactic
[ "Mathlib.Data.Nat.Log", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatLog.lean
evalNatLog
Evaluates the `Nat.log` function.
private nat_clog_zero_left (b n : Nat) (hb : Nat.ble b 1 = true) : Nat.clog b n = 0 := Nat.clog_of_left_le_one (Nat.le_of_ble_eq_true hb) n
lemma
Tactic
[ "Mathlib.Data.Nat.Log", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatLog.lean
nat_clog_zero_left
null
private nat_clog_zero_right (b n : Nat) (hn : Nat.ble n 1 = true) : Nat.clog b n = 0 := Nat.clog_of_right_le_one (Nat.le_of_ble_eq_true hn) b
lemma
Tactic
[ "Mathlib.Data.Nat.Log", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatLog.lean
nat_clog_zero_right
null
nat_clog_helper {b m n : ℕ} (hb : Nat.blt 1 b = true) (h₁ : Nat.blt (b ^ m) n = true) (h₂ : Nat.ble n (b ^ (m + 1)) = true) : Nat.clog b n = m + 1 := by rw [Nat.blt_eq] at hb rw [Nat.blt_eq, Nat.pow_lt_iff_lt_clog hb] at h₁ rw [Nat.ble_eq, Nat.le_pow_iff_clog_le hb] at h₂ cutsat
theorem
Tactic
[ "Mathlib.Data.Nat.Log", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatLog.lean
nat_clog_helper
null
private isNat_clog : {b nb n nn k : ℕ} → IsNat b nb → IsNat n nn → Nat.clog nb nn = k → IsNat (Nat.clog b n) k | _, _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨rfl⟩
theorem
Tactic
[ "Mathlib.Data.Nat.Log", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatLog.lean
isNat_clog
null
proveNatClog (eb en : Q(ℕ)) : (ek : Q(ℕ)) × Q(Nat.clog $eb $en = $ek) := let b := eb.natLit! let n := en.natLit! if _ : b ≤ 1 then have h : Q(Nat.ble $eb 1 = true) := reflBoolTrue ⟨q(nat_lit 0), q(nat_clog_zero_left $eb $en $h)⟩ else if _ : n ≤ 1 then have h : Q(Nat.ble $en 1 = true) := reflBoolTrue ⟨q(nat_lit 0), q(nat_clog_zero_right $eb $en $h)⟩ else match h : Nat.clog b n with | 0 => False.elim <| Nat.ne_of_gt (Nat.clog_pos (by cutsat) (by cutsat)) h | k + 1 => have ek : Q(ℕ) := mkRawNatLit k have ek1 : Q(ℕ) := mkRawNatLit (k + 1) have _ : $ek1 =Q $ek + 1 := ⟨⟩ have hb : Q(Nat.blt 1 $eb = true) := reflBoolTrue have hl : Q(Nat.blt ($eb ^ $ek) $en = true) := reflBoolTrue have hh : Q(Nat.ble $en ($eb ^ ($ek + 1)) = true) := reflBoolTrue ⟨ek1, q(nat_clog_helper $hb $hl $hh)⟩
def
Tactic
[ "Mathlib.Data.Nat.Log", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatLog.lean
proveNatClog
Given the natural number literals `eb` and `en`, returns `Nat.clog eb en` as a natural number literal and an equality proof. Panics if `ex` or `en` aren't natural number literals.
@[norm_num Nat.clog _ _] evalNatClog : NormNumExt where eval {u α} e := do let mkApp2 _ (b : Q(ℕ)) (n : Q(ℕ)) ← Meta.whnfR e | failure let sℕ : Q(AddMonoidWithOne ℕ) := q(instAddMonoidWithOneNat) let ⟨eb, pb⟩ ← deriveNat b sℕ let ⟨en, pn⟩ ← deriveNat n sℕ let ⟨ek, pf⟩ := proveNatClog eb en let pf' : Q(IsNat (Nat.clog $b $n) $ek) := q(isNat_clog $pb $pn $pf) return .isNat sℕ ek pf'
def
Tactic
[ "Mathlib.Data.Nat.Log", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatLog.lean
evalNatClog
Evaluates the `Nat.clog` function.
nat_sqrt_helper {x y r : ℕ} (hr : y * y + r = x) (hle : Nat.ble r (2 * y)) : Nat.sqrt x = y := by rw [← hr, ← pow_two] rw [two_mul] at hle exact Nat.sqrt_add_eq' _ (Nat.le_of_ble_eq_true hle)
lemma
Tactic
[ "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatSqrt.lean
nat_sqrt_helper
null
isNat_sqrt : {x nx z : ℕ} → IsNat x nx → Nat.sqrt nx = z → IsNat (Nat.sqrt x) z | _, _, _, ⟨rfl⟩, rfl => ⟨rfl⟩
theorem
Tactic
[ "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatSqrt.lean
isNat_sqrt
null
proveNatSqrt (ex : Q(ℕ)) : (ey : Q(ℕ)) × Q(Nat.sqrt $ex = $ey) := match ex.natLit! with | 0 => have : $ex =Q nat_lit 0 := ⟨⟩; ⟨q(nat_lit 0), q(Nat.sqrt_zero)⟩ | 1 => have : $ex =Q nat_lit 1 := ⟨⟩; ⟨q(nat_lit 1), q(Nat.sqrt_one)⟩ | x => let y := Nat.sqrt x have ey : Q(ℕ) := mkRawNatLit y have er : Q(ℕ) := mkRawNatLit (x - y * y) have hr : Q($ey * $ey + $er = $ex) := (q(Eq.refl $ex) : Expr) have hle : Q(Nat.ble $er (2 * $ey)) := (q(Eq.refl true) : Expr) ⟨ey, q(nat_sqrt_helper $hr $hle)⟩
def
Tactic
[ "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatSqrt.lean
proveNatSqrt
Given the natural number literal `ex`, returns its square root as a natural number literal and an equality proof. Panics if `ex` isn't a natural number literal.
@[norm_num Nat.sqrt _] evalNatSqrt : NormNumExt where eval {_ _} e := do let .app _ (x : Q(ℕ)) ← Meta.whnfR e | failure let sℕ : Q(AddMonoidWithOne ℕ) := q(instAddMonoidWithOneNat) let ⟨ex, p⟩ ← deriveNat x sℕ let ⟨ey, pf⟩ := proveNatSqrt ex let pf' : Q(IsNat (Nat.sqrt $x) $ey) := q(isNat_sqrt $p $pf) return .isNat sℕ ey pf'
def
Tactic
[ "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/NatSqrt.lean
evalNatSqrt
Evaluates the `Nat.sqrt` function.
isNNRat_ofScientific_of_true [DivisionRing α] : {m e : ℕ} → {n : ℕ} → {d : ℕ} → IsNNRat (mkRat m (10 ^ e) : α) n d → IsNNRat (OfScientific.ofScientific m true e : α) n d | _, _, _, _, ⟨_, eq⟩ => ⟨‹_›, by rwa [← Rat.cast_ofScientific, ← Rat.ofScientific_eq_ofScientific, Rat.ofScientific_true_def]⟩
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Rat.Cast.Lemmas" ]
Mathlib/Tactic/NormNum/OfScientific.lean
isNNRat_ofScientific_of_true
null
isNat_ofScientific_of_false [DivisionRing α] : {m e nm ne n : ℕ} → IsNat m nm → IsNat e ne → n = Nat.mul nm ((10 : ℕ) ^ ne) → IsNat (OfScientific.ofScientific m false e : α) n | _, _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, h => ⟨by rw [← Rat.cast_ofScientific, ← Rat.ofScientific_eq_ofScientific] simp only [Nat.cast_id, Rat.ofScientific_false_def, Nat.cast_mul, Nat.cast_pow, Nat.cast_ofNat, h, Nat.mul_eq] norm_cast⟩
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Rat.Cast.Lemmas" ]
Mathlib/Tactic/NormNum/OfScientific.lean
isNat_ofScientific_of_false
null
@[norm_num OfScientific.ofScientific _ _ _] evalOfScientific : NormNumExt where eval {u α} e := do let .app (.app (.app f (m : Q(ℕ))) (b : Q(Bool))) (exp : Q(ℕ)) ← whnfR e | failure let dα ← inferDivisionRing α guard <|← withNewMCtxDepth <| isDefEq f q(OfScientific.ofScientific (α := $α)) assumeInstancesCommute haveI' : $e =Q OfScientific.ofScientific $m $b $exp := ⟨⟩ match b with | ~q(true) => let rme ← derive (q(mkRat $m (10 ^ $exp)) : Q($α)) let some ⟨q, n, d, p⟩ := rme.toNNRat' q(DivisionRing.toDivisionSemiring) | failure return .isNNRat q(DivisionRing.toDivisionSemiring) q n d q(isNNRat_ofScientific_of_true $p) | ~q(false) => let ⟨nm, pm⟩ ← deriveNat m q(AddCommMonoidWithOne.toAddMonoidWithOne) let ⟨ne, pe⟩ ← deriveNat exp q(AddCommMonoidWithOne.toAddMonoidWithOne) have pm : Q(IsNat $m $nm) := pm have pe : Q(IsNat $exp $ne) := pe let m' := nm.natLit! let exp' := ne.natLit! let n' := Nat.mul m' (Nat.pow (10 : ℕ) exp') have n : Q(ℕ) := mkRawNatLit n' haveI : $n =Q Nat.mul $nm ((10 : ℕ) ^ $ne) := ⟨⟩ return .isNat _ n q(isNat_ofScientific_of_false $pm $pe (.refl $n))
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Rat.Cast.Lemmas" ]
Mathlib/Tactic/NormNum/OfScientific.lean
evalOfScientific
The `norm_num` extension which identifies expressions in scientific notation, normalizing them to rat casts if the scientific notation is inherited from the one for rationals.
@[norm_num (_ : Ordinal) * (_ : Ordinal)] evalOrdinalMul : NormNumExt where eval {u α} e := do let some u' := u.dec | throwError "level is not succ" haveI' : u =QL u' + 1 := ⟨⟩ match α, e with | ~q(Ordinal.{u'}), ~q(($a : Ordinal) * ($b : Ordinal)) => let i : Q(AddMonoidWithOne Ordinal.{u'}) := q(inferInstance) let ⟨an, pa⟩ ← deriveNat a i let ⟨bn, pb⟩ ← deriveNat b i have rn : Q(ℕ) := mkRawNatLit (an.natLit! * bn.natLit!) have : ($an * $bn) =Q $rn := ⟨⟩ pure (.isNat i rn q(isNat_ordinalMul $pa $pb (.refl $rn))) | _, _ => throwError "not multiplication on ordinals"
def
Tactic
[ "Mathlib.SetTheory.Ordinal.Exponential", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Ordinal.lean
evalOrdinalMul
info: 12 * 5 -/ #guard_msgs in #norm_num (12 : Ordinal.{0}) * (5 : Ordinal.{0}) lemma isNat_ordinalMul.{u} : ∀ {a b : Ordinal.{u}} {an bn rn : ℕ}, IsNat a an → IsNat b bn → an * bn = rn → IsNat (a * b) rn | _, _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨Eq.symm <| natCast_mul ..⟩ /-- The `norm_num` extension for multiplication on ordinals.
@[norm_num (_ : Ordinal) ≤ (_ : Ordinal)] evalOrdinalLE : NormNumExt where eval {u α} e := do let ⟨_⟩ ← assertLevelDefEqQ u ql(0) match α, e with | ~q(Prop), ~q(($a : Ordinal) ≤ ($b : Ordinal)) => let i : Q(AddMonoidWithOne Ordinal.{u_1}) := q(inferInstance) let ⟨an, pa⟩ ← deriveNat a i let ⟨bn, pb⟩ ← deriveNat b i if an.natLit! ≤ bn.natLit! then have this : decide ($an ≤ $bn) =Q true := ⟨⟩ pure (.isTrue q(isNat_ordinalLE_true $pa $pb $this)) else have this : decide ($an ≤ $bn) =Q false := ⟨⟩ pure (.isFalse q(isNat_ordinalLE_false $pa $pb $this)) | _, _ => throwError "not inequality on ordinals"
def
Tactic
[ "Mathlib.SetTheory.Ordinal.Exponential", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Ordinal.lean
evalOrdinalLE
info: 5 ≤ 12 -/ #guard_msgs in #norm_num (5 : Ordinal.{0}) ≤ 12 /-- info: 5 < 12 -/ #guard_msgs in #norm_num (5 : Ordinal.{0}) < 12 lemma isNat_ordinalLE_true.{u} : ∀ {a b : Ordinal.{u}} {an bn : ℕ}, IsNat a an → IsNat b bn → decide (an ≤ bn) = true → a ≤ b | _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, h => Nat.cast_le.mpr <| of_decide_eq_true h lemma isNat_ordinalLE_false.{u} : ∀ {a b : Ordinal.{u}} {an bn : ℕ}, IsNat a an → IsNat b bn → decide (an ≤ bn) = false → ¬a ≤ b | _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, h => not_iff_not.mpr Nat.cast_le |>.mpr <| of_decide_eq_false h lemma isNat_ordinalLT_true.{u} : ∀ {a b : Ordinal.{u}} {an bn : ℕ}, IsNat a an → IsNat b bn → decide (an < bn) = true → a < b | _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, h => Nat.cast_lt.mpr <| of_decide_eq_true h lemma isNat_ordinalLT_false.{u} : ∀ {a b : Ordinal.{u}} {an bn : ℕ}, IsNat a an → IsNat b bn → decide (an < bn) = false → ¬a < b | _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, h => not_iff_not.mpr Nat.cast_lt |>.mpr <| of_decide_eq_false h /-- The `norm_num` extension for inequality on ordinals.
@[norm_num (_ : Ordinal) < (_ : Ordinal)] evalOrdinalLT : NormNumExt where eval {u α} e := do let ⟨_⟩ ← assertLevelDefEqQ u ql(0) match α, e with | ~q(Prop), ~q(($a : Ordinal) < ($b : Ordinal)) => let i : Q(AddMonoidWithOne Ordinal.{u_1}) := q(inferInstance) let ⟨an, pa⟩ ← deriveNat a i let ⟨bn, pb⟩ ← deriveNat b i if an.natLit! < bn.natLit! then have this : decide ($an < $bn) =Q true := ⟨⟩ pure (.isTrue q(isNat_ordinalLT_true $pa $pb $this)) else have this : decide ($an < $bn) =Q false := ⟨⟩ pure (.isFalse q(isNat_ordinalLT_false $pa $pb $this)) | _, _ => throwError "not strict inequality on ordinals"
def
Tactic
[ "Mathlib.SetTheory.Ordinal.Exponential", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Ordinal.lean
evalOrdinalLT
The `norm_num` extension for strict inequality on ordinals.
@[norm_num (_ : Ordinal) - (_ : Ordinal)] evalOrdinalSub : NormNumExt where eval {u α} e := do let some u' := u.dec | throwError "level is not succ" haveI' : u =QL u' + 1 := ⟨⟩ match α, e with | ~q(Ordinal.{u'}), ~q(($a : Ordinal) - ($b : Ordinal)) => let i : Q(AddMonoidWithOne Ordinal.{u'}) := q(inferInstance) let ⟨an, pa⟩ ← deriveNat a i let ⟨bn, pb⟩ ← deriveNat b i have rn : Q(ℕ) := mkRawNatLit (an.natLit! - bn.natLit!) have : ($an - $bn) =Q $rn := ⟨⟩ pure (.isNat i rn q(isNat_ordinalSub $pa $pb (.refl $rn))) | _, _ => throwError "not subtration on ordinals"
def
Tactic
[ "Mathlib.SetTheory.Ordinal.Exponential", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Ordinal.lean
evalOrdinalSub
info: 12 - 5 -/ #guard_msgs in #norm_num (12 : Ordinal.{0}) - 5 lemma isNat_ordinalSub.{u} : ∀ {a b : Ordinal.{u}} {an bn rn : ℕ}, IsNat a an → IsNat b bn → an - bn = rn → IsNat (a - b) rn | _, _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨Eq.symm <| natCast_sub ..⟩ /-- The `norm_num` extension for subtraction on ordinals.
@[norm_num (_ : Ordinal) / (_ : Ordinal)] evalOrdinalDiv : NormNumExt where eval {u α} e := do let some u' := u.dec | throwError "level is not succ" haveI' : u =QL u' + 1 := ⟨⟩ match α, e with | ~q(Ordinal.{u'}), ~q(($a : Ordinal) / ($b : Ordinal)) => let i : Q(AddMonoidWithOne Ordinal.{u'}) := q(inferInstance) let ⟨an, pa⟩ ← deriveNat a i let ⟨bn, pb⟩ ← deriveNat b i have rn : Q(ℕ) := mkRawNatLit (an.natLit! / bn.natLit!) have : ($an / $bn) =Q $rn := ⟨⟩ pure (.isNat i rn q(isNat_ordinalDiv $pa $pb (.refl $rn))) | _, _ => throwError "not division on ordinals"
def
Tactic
[ "Mathlib.SetTheory.Ordinal.Exponential", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Ordinal.lean
evalOrdinalDiv
info: 12 / 5 -/ #guard_msgs in #norm_num (12 : Ordinal.{0}) / 5 lemma isNat_ordinalDiv.{u} : ∀ {a b : Ordinal.{u}} {an bn rn : ℕ}, IsNat a an → IsNat b bn → an / bn = rn → IsNat (a / b) rn | _, _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨Eq.symm <| natCast_div ..⟩ /-- The `norm_num` extension for division on ordinals.
@[norm_num (_ : Ordinal) % (_ : Ordinal)] evalOrdinalMod : NormNumExt where eval {u α} e := do let some u' := u.dec | throwError "level is not succ" haveI' : u =QL u' + 1 := ⟨⟩ match α, e with | ~q(Ordinal.{u'}), ~q(($a : Ordinal) % ($b : Ordinal)) => let i : Q(AddMonoidWithOne Ordinal.{u'}) := q(inferInstance) let ⟨an, pa⟩ ← deriveNat a i let ⟨bn, pb⟩ ← deriveNat b i have rn : Q(ℕ) := mkRawNatLit (an.natLit! % bn.natLit!) have : ($an % $bn) =Q $rn := ⟨⟩ pure (.isNat i rn q(isNat_ordinalMod $pa $pb (.refl $rn))) | _, _ => throwError "not modulo on ordinals"
def
Tactic
[ "Mathlib.SetTheory.Ordinal.Exponential", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Ordinal.lean
evalOrdinalMod
info: 12 % 5 -/ #guard_msgs in #norm_num (12 : Ordinal.{0}) % 5 lemma isNat_ordinalMod.{u} : ∀ {a b : Ordinal.{u}} {an bn rn : ℕ}, IsNat a an → IsNat b bn → an % bn = rn → IsNat (a % b) rn | _, _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨Eq.symm <| natCast_mod ..⟩ /-- The `norm_num` extension for modulo on ordinals.
isNat_ordinalOPow.{u} : ∀ {a b : Ordinal.{u}} {an bn rn : ℕ}, IsNat a an → IsNat b bn → an ^ bn = rn → IsNat (a ^ b) rn | _, _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨Eq.symm <| natCast_opow ..⟩
lemma
Tactic
[ "Mathlib.SetTheory.Ordinal.Exponential", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Ordinal.lean
isNat_ordinalOPow.
null
@[norm_num (_ : Ordinal) ^ (_ : Ordinal)] evalOrdinalOPow : NormNumExt where eval {u α} e := do let some u' := u.dec | throwError "level is not succ" haveI' : u =QL u' + 1 := ⟨⟩ match α, e with | ~q(Ordinal.{u'}), ~q(($a : Ordinal) ^ ($b : Ordinal)) => let i : Q(AddMonoidWithOne Ordinal.{u'}) := q(inferInstance) let ⟨an, pa⟩ ← deriveNat a i let ⟨bn, pb⟩ ← deriveNat b i have rn : Q(ℕ) := mkRawNatLit (an.natLit! ^ bn.natLit!) have : ($an ^ $bn) =Q $rn := ⟨⟩ pure (.isNat i rn q(isNat_ordinalOPow $pa $pb (.refl $rn))) | _, _ => throwError "not homogeneous power on ordinals"
def
Tactic
[ "Mathlib.SetTheory.Ordinal.Exponential", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Ordinal.lean
evalOrdinalOPow
The `norm_num` extension for homogeneous power on ordinals.
@[norm_num (_ : Ordinal) ^ (_ : ℕ)] evalOrdinalNPow : NormNumExt where eval {u α} e := do let some u' := u.dec | throwError "level is not succ" haveI' : u =QL u' + 1 := ⟨⟩ match α, e with | ~q(Ordinal.{u'}), ~q(($a : Ordinal) ^ ($b : ℕ)) => let i : Q(AddMonoidWithOne Ordinal.{u'}) := q(inferInstance) let ⟨an, pa⟩ ← deriveNat a i let ⟨bn, pb⟩ ← deriveNat b q(inferInstance) have rn : Q(ℕ) := mkRawNatLit (an.natLit! ^ bn.natLit!) have : ($an ^ $bn) =Q $rn := ⟨⟩ pure (.isNat i rn q(isNat_ordinalNPow $pa $pb (.refl $rn))) | _, _ => throwError "not natural power on ordinals"
def
Tactic
[ "Mathlib.SetTheory.Ordinal.Exponential", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Ordinal.lean
evalOrdinalNPow
info: 12 ^ 2 -/ #guard_msgs in #norm_num (12 : Ordinal.{0}) ^ (2 : ℕ) lemma isNat_ordinalNPow.{u} : ∀ {a : Ordinal.{u}} {b an bn rn : ℕ}, IsNat a an → IsNat b bn → an ^ bn = rn → IsNat (a ^ b) rn | _, _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨Eq.symm <| natCast_opow .. |>.trans <| opow_natCast ..⟩ /-- The `norm_num` extension for natural power on ordinals.
@[norm_num Even _] evalEven : NormNumExt where eval {u αP} e := do match u, αP, e with | 0, ~q(Prop), ~q(@Even ℕ _ $a) => assertInstancesCommute let ⟨b, r⟩ ← deriveBoolOfIff q($a % 2 = 0) q(Even $a) q((@Nat.even_iff $a).symm) return .ofBoolResult r | 0, ~q(Prop), ~q(@Even ℤ _ $a) => assertInstancesCommute let ⟨b, r⟩ ← deriveBoolOfIff q($a % 2 = 0) q(Even $a) q((@Int.even_iff $a).symm) return .ofBoolResult r | _, _, _ => failure
def
Tactic
[ "Mathlib.Algebra.Ring.Int.Parity", "Mathlib.Tactic.NormNum.Core" ]
Mathlib/Tactic/NormNum/Parity.lean
evalEven
`norm_num` extension for `Even`. Works for `ℕ` and `ℤ`.
@[norm_num Odd _] evalOdd : NormNumExt where eval {u αP} e := do match u, αP, e with | 0, ~q(Prop), ~q(@Odd ℕ $inst $a) => assertInstancesCommute let ⟨b, r⟩ ← deriveBoolOfIff q($a % 2 = 1) q(Odd $a) q((@Nat.odd_iff $a).symm) return .ofBoolResult r | 0, ~q(Prop), ~q(@Odd ℤ $inst $a) => assertInstancesCommute let ⟨b, r⟩ ← deriveBoolOfIff q($a % 2 = 1) q(Odd $a) q((@Int.odd_iff $a).symm) return .ofBoolResult r | _ => failure
def
Tactic
[ "Mathlib.Algebra.Ring.Int.Parity", "Mathlib.Tactic.NormNum.Core" ]
Mathlib/Tactic/NormNum/Parity.lean
evalOdd
`norm_num` extension for `Odd`. Works for `ℕ` and `ℤ`.
natPow_zero : Nat.pow a (nat_lit 0) = nat_lit 1 := rfl
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
natPow_zero
null
natPow_one : Nat.pow a (nat_lit 1) = a := Nat.pow_one _
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
natPow_one
null
zero_natPow : Nat.pow (nat_lit 0) (Nat.succ b) = nat_lit 0 := rfl
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
zero_natPow
null
one_natPow : Nat.pow (nat_lit 1) b = nat_lit 1 := Nat.one_pow _
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
one_natPow
null
IsNatPowT (p : Prop) (a b c : Nat) : Prop where /-- Unfolds the assertion. -/ run' : p → Nat.pow a b = c
structure
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
IsNatPowT
This is an opaque wrapper around `Nat.pow` to prevent lean from unfolding the definition of `Nat.pow` on numerals. The arbitrary precondition `p` is actually a formula of the form `Nat.pow a' b' = c'` but we usually don't care to unfold this proposition so we just carry a reference to it.
IsNatPowT.run (p : IsNatPowT (Nat.pow a (nat_lit 1) = a) a b c) : Nat.pow a b = c := p.run' (Nat.pow_one _)
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
IsNatPowT.run
null
IsNatPowT.trans {p : Prop} {b' c' : ℕ} (h1 : IsNatPowT p a b c) (h2 : IsNatPowT (Nat.pow a b = c) a b' c') : IsNatPowT p a b' c' := ⟨h2.run' ∘ h1.run'⟩
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
IsNatPowT.trans
This is the key to making the proof proceed as a balanced tree of applications instead of a linear sequence. It is just modus ponens after unwrapping the definitions.
IsNatPowT.bit0 : IsNatPowT (Nat.pow a b = c) a (nat_lit 2 * b) (Nat.mul c c) := ⟨fun h1 => by simp [two_mul, pow_add, ← h1]⟩
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
IsNatPowT.bit0
null
IsNatPowT.bit1 : IsNatPowT (Nat.pow a b = c) a (nat_lit 2 * b + nat_lit 1) (Nat.mul c (Nat.mul c a)) := ⟨fun h1 => by simp [two_mul, pow_add, mul_assoc, ← h1]⟩
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
IsNatPowT.bit1
null
partial evalNatPow (a b : Q(ℕ)) : (c : Q(ℕ)) × Q(Nat.pow $a $b = $c) := if b.natLit! = 0 then haveI : $b =Q 0 := ⟨⟩ ⟨q(nat_lit 1), q(natPow_zero)⟩ else if a.natLit! = 0 then haveI : $a =Q 0 := ⟨⟩ have b' : Q(ℕ) := mkRawNatLit (b.natLit! - 1) haveI : $b =Q Nat.succ $b' := ⟨⟩ ⟨q(nat_lit 0), q(zero_natPow)⟩ else if a.natLit! = 1 then haveI : $a =Q 1 := ⟨⟩ ⟨q(nat_lit 1), q(one_natPow)⟩ else if b.natLit! = 1 then haveI : $b =Q 1 := ⟨⟩ ⟨a, q(natPow_one)⟩ else let ⟨c, p⟩ := go b.natLit!.log2 a q(nat_lit 1) a b _ .rfl ⟨c, q(($p).run)⟩ where /-- Invariants: `a ^ b₀ = c₀`, `depth > 0`, `b >>> depth = b₀`, `p := Nat.pow $a $b₀ = $c₀` -/ go (depth : Nat) (a b₀ c₀ b : Q(ℕ)) (p : Q(Prop)) (hp : $p =Q (Nat.pow $a $b₀ = $c₀)) : (c : Q(ℕ)) × Q(IsNatPowT $p $a $b $c) := let b' := b.natLit! if depth ≤ 1 then let a' := a.natLit! let c₀' := c₀.natLit! if b' &&& 1 == 0 then have c : Q(ℕ) := mkRawNatLit (c₀' * c₀') haveI : $c =Q Nat.mul $c₀ $c₀ := ⟨⟩ haveI : $b =Q 2 * $b₀ := ⟨⟩ ⟨c, q(IsNatPowT.bit0)⟩ else have c : Q(ℕ) := mkRawNatLit (c₀' * (c₀' * a')) haveI : $c =Q Nat.mul $c₀ (Nat.mul $c₀ $a) := ⟨⟩ haveI : $b =Q 2 * $b₀ + 1 := ⟨⟩ ⟨c, q(IsNatPowT.bit1)⟩ else let d := depth >>> 1 have hi : Q(ℕ) := mkRawNatLit (b' >>> d) let ⟨c1, p1⟩ := go (depth - d) a b₀ c₀ hi p (by exact hp) let ⟨c2, p2⟩ := go d a hi c1 b q(Nat.pow $a $hi = $c1) ⟨⟩ ⟨c2, q(($p1).trans $p2)⟩
def
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
evalNatPow
Proves `Nat.pow a b = c` where `a` and `b` are raw nat literals. This could be done by just `rfl` but the kernel does not have a special case implementation for `Nat.pow` so this would proceed by unary recursion on `b`, which is too slow and also leads to deep recursion. We instead do the proof by binary recursion, but this can still lead to deep recursion, so we use an additional trick to do binary subdivision on `log2 b`. As a result this produces a proof of depth `log (log b)` which will essentially never overflow before the numbers involved themselves exceed memory limits.
intPow_ofNat (h1 : Nat.pow a b = c) : Int.pow (Int.ofNat a) b = Int.ofNat c := by simp [← h1]
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
intPow_ofNat
null
intPow_negOfNat_bit0 {b' c' : ℕ} (h1 : Nat.pow a b' = c') (hb : nat_lit 2 * b' = b) (hc : c' * c' = c) : Int.pow (Int.negOfNat a) b = Int.ofNat c := by rw [← hb, Int.negOfNat_eq, Int.pow_eq, pow_mul, neg_pow_two, ← pow_mul, two_mul, pow_add, ← hc, ← h1] simp
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
intPow_negOfNat_bit0
null
intPow_negOfNat_bit1 {b' c' : ℕ} (h1 : Nat.pow a b' = c') (hb : nat_lit 2 * b' + nat_lit 1 = b) (hc : c' * (c' * a) = c) : Int.pow (Int.negOfNat a) b = Int.negOfNat c := by rw [← hb, Int.negOfNat_eq, Int.negOfNat_eq, Int.pow_eq, pow_succ, pow_mul, neg_pow_two, ← pow_mul, two_mul, pow_add, ← hc, ← h1] simp [mul_comm, mul_left_comm]
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
intPow_negOfNat_bit1
null
partial evalIntPow (za : ℤ) (a : Q(ℤ)) (b : Q(ℕ)) : ℤ × (c : Q(ℤ)) × Q(Int.pow $a $b = $c) := have a' : Q(ℕ) := a.appArg! if 0 ≤ za then haveI : $a =Q .ofNat $a' := ⟨⟩ let ⟨c, p⟩ := evalNatPow a' b ⟨c.natLit!, q(.ofNat $c), q(intPow_ofNat $p)⟩ else haveI : $a =Q .negOfNat $a' := ⟨⟩ let b' := b.natLit! have b₀ : Q(ℕ) := mkRawNatLit (b' >>> 1) let ⟨c₀, p⟩ := evalNatPow a' b₀ let c' := c₀.natLit! if b' &&& 1 == 0 then have c : Q(ℕ) := mkRawNatLit (c' * c') have pc : Q($c₀ * $c₀ = $c) := (q(Eq.refl $c) : Expr) have pb : Q(2 * $b₀ = $b) := (q(Eq.refl $b) : Expr) ⟨c.natLit!, q(.ofNat $c), q(intPow_negOfNat_bit0 $p $pb $pc)⟩ else have c : Q(ℕ) := mkRawNatLit (c' * (c' * a'.natLit!)) have pc : Q($c₀ * ($c₀ * $a') = $c) := (q(Eq.refl $c) : Expr) have pb : Q(2 * $b₀ + 1 = $b) := (q(Eq.refl $b) : Expr) ⟨-c.natLit!, q(.negOfNat $c), q(intPow_negOfNat_bit1 $p $pb $pc)⟩
def
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
evalIntPow
Evaluates `Int.pow a b = c` where `a` and `b` are raw integer literals.
isNat_pow {α} [Semiring α] : ∀ {f : α → ℕ → α} {a : α} {b a' b' c : ℕ}, f = HPow.hPow → IsNat a a' → IsNat b b' → Nat.pow a' b' = c → IsNat (f a b) c | _, _, _, _, _, _, rfl, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨by simp⟩
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
isNat_pow
null
isInt_pow {α} [Ring α] : ∀ {f : α → ℕ → α} {a : α} {b : ℕ} {a' : ℤ} {b' : ℕ} {c : ℤ}, f = HPow.hPow → IsInt a a' → IsNat b b' → Int.pow a' b' = c → IsInt (f a b) c | _, _, _, _, _, _, rfl, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨by simp⟩
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
isInt_pow
null
isRat_pow {α} [Ring α] {f : α → ℕ → α} {a : α} {an cn : ℤ} {ad b b' cd : ℕ} : f = HPow.hPow → IsRat a an ad → IsNat b b' → Int.pow an b' = cn → Nat.pow ad b' = cd → IsRat (f a b) cn cd := by rintro rfl ⟨_, rfl⟩ ⟨rfl⟩ (rfl : an ^ b = _) (rfl : ad ^ b = _) have := invertiblePow (ad:α) b rw [← Nat.cast_pow] at this use this; simp [invOf_pow, Commute.mul_pow]
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
isRat_pow
null
isNNRat_pow {α} [Semiring α] {f : α → ℕ → α} {a : α} {an cn : ℕ} {ad b b' cd : ℕ} : f = HPow.hPow → IsNNRat a an ad → IsNat b b' → Nat.pow an b' = cn → Nat.pow ad b' = cd → IsNNRat (f a b) cn cd := by rintro rfl ⟨_, rfl⟩ ⟨rfl⟩ (rfl : an ^ b = _) (rfl : ad ^ b = _) have := invertiblePow (ad:α) b rw [← Nat.cast_pow] at this use this; simp [invOf_pow, Commute.mul_pow, Nat.cast_commute]
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
isNNRat_pow
null
evalPow.core {u : Level} {α : Q(Type u)} (e : Q(«$α»)) (f : Q(«$α» → ℕ → «$α»)) (a : Q(«$α»)) (b nb : Q(ℕ)) (pb : Q(IsNat «$b» «$nb»)) (sα : Q(Semiring «$α»)) (ra : Result a) : Option (Result e) := do haveI' : $e =Q $a ^ $b := ⟨⟩ haveI' : $f =Q HPow.hPow := ⟨⟩ match ra with | .isBool .. => failure | .isNat sα na pa => assumeInstancesCommute have ⟨c, r⟩ := evalNatPow na nb return .isNat sα c q(isNat_pow (f := $f) (.refl $f) $pa $pb $r) | .isNegNat rα .. => assumeInstancesCommute let ⟨za, na, pa⟩ ← ra.toInt rα have ⟨zc, c, r⟩ := evalIntPow za na nb return .isInt rα c zc q(isInt_pow (f := $f) (.refl $f) $pa $pb $r) | .isNNRat dα _qa na da pa => assumeInstancesCommute have ⟨nc, r1⟩ := evalNatPow na nb have ⟨dc, r2⟩ := evalNatPow da nb let qc := mkRat nc.natLit! dc.natLit! return .isNNRat dα qc nc dc q(isNNRat_pow (f := $f) (.refl $f) $pa $pb $r1 $r2) | .isNegNNRat dα qa na da pa => assumeInstancesCommute have ⟨zc, nc, r1⟩ := evalIntPow qa.num q(Int.negOfNat $na) nb have ⟨dc, r2⟩ := evalNatPow da nb let qc := mkRat zc dc.natLit! return .isRat dα qc nc dc q(isRat_pow (f := $f) (.refl $f) $pa $pb $r1 $r2) attribute [local instance] monadLiftOptionMetaM in
def
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
evalPow.core
Main part of `evalPow`.
@[norm_num _ ^ (_ : ℕ)] evalPow : NormNumExt where eval {u α} e := do let .app (.app (f : Q($α → ℕ → $α)) (a : Q($α))) (b : Q(ℕ)) ← whnfR e | failure let ⟨nb, pb⟩ ← deriveNat b q(instAddMonoidWithOneNat) let sα ← inferSemiring α let ra ← derive a guard <|← withDefault <| withNewMCtxDepth <| isDefEq f q(HPow.hPow (α := $α)) haveI' : $e =Q $a ^ $b := ⟨⟩ haveI' : $f =Q HPow.hPow := ⟨⟩ evalPow.core q($e) q($f) q($a) q($b) q($nb) q($pb) q($sα) ra
def
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
evalPow
The `norm_num` extension which identifies expressions of the form `a ^ b`, such that `norm_num` successfully recognises both `a` and `b`, with `b : ℕ`.
isNat_zpow_pos {α : Type*} [DivisionSemiring α] {a : α} {b : ℤ} {nb ne : ℕ} (pb : IsNat b nb) (pe' : IsNat (a ^ nb) ne) : IsNat (a ^ b) ne := by rwa [pb.out, zpow_natCast]
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
isNat_zpow_pos
null
isNat_zpow_neg {α : Type*} [DivisionSemiring α] {a : α} {b : ℤ} {nb ne : ℕ} (pb : IsInt b (Int.negOfNat nb)) (pe' : IsNat (a ^ nb)⁻¹ ne) : IsNat (a ^ b) ne := by rwa [pb.out, Int.cast_negOfNat, zpow_neg, zpow_natCast]
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
isNat_zpow_neg
null
isInt_zpow_pos {α : Type*} [DivisionRing α] {a : α} {b : ℤ} {nb ne : ℕ} (pb : IsNat b nb) (pe' : IsInt (a ^ nb) (Int.negOfNat ne)) : IsInt (a ^ b) (Int.negOfNat ne) := by rwa [pb.out, zpow_natCast]
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
isInt_zpow_pos
null
isInt_zpow_neg {α : Type*} [DivisionRing α] {a : α} {b : ℤ} {nb ne : ℕ} (pb : IsInt b (Int.negOfNat nb)) (pe' : IsInt (a ^ nb)⁻¹ (Int.negOfNat ne)) : IsInt (a ^ b) (Int.negOfNat ne) := by rwa [pb.out, Int.cast_negOfNat, zpow_neg, zpow_natCast]
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
isInt_zpow_neg
null
isNNRat_zpow_pos {α : Type*} [DivisionSemiring α] {a : α} {b : ℤ} {nb : ℕ} {num : ℕ} {den : ℕ} (pb : IsNat b nb) (pe' : IsNNRat (a ^ nb) num den) : IsNNRat (a^b) num den := by rwa [pb.out, zpow_natCast]
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
isNNRat_zpow_pos
null
isNNRat_zpow_neg {α : Type*} [DivisionSemiring α] {a : α} {b : ℤ} {nb : ℕ} {num : ℕ} {den : ℕ} (pb : IsInt b (Int.negOfNat nb)) (pe' : IsNNRat ((a ^ nb)⁻¹) num den) : IsNNRat (a^b) num den := by rwa [pb.out, Int.cast_negOfNat, zpow_neg, zpow_natCast]
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
isNNRat_zpow_neg
null
isRat_zpow_pos {α : Type*} [DivisionRing α] {a : α} {b : ℤ} {nb : ℕ} {num : ℤ} {den : ℕ} (pb : IsNat b nb) (pe' : IsRat (a ^ nb) num den) : IsRat (a ^ b) num den := by rwa [pb.out, zpow_natCast]
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
isRat_zpow_pos
null
isRat_zpow_neg {α : Type*} [DivisionRing α] {a : α} {b : ℤ} {nb : ℕ} {num : ℤ} {den : ℕ} (pb : IsInt b (Int.negOfNat nb)) (pe' : IsRat ((a ^ nb)⁻¹) num den) : IsRat (a ^ b) num den := by rwa [pb.out, Int.cast_negOfNat, zpow_neg, zpow_natCast]
theorem
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
isRat_zpow_neg
null
@[norm_num _ ^ (_ : ℤ)] evalZPow : NormNumExt where eval {u α} e := do let .app (.app (f : Q($α → ℤ → $α)) (a : Q($α))) (b : Q(ℤ)) ← whnfR e | failure let _c ← synthInstanceQ q(DivisionSemiring $α) let rb ← derive (α := q(ℤ)) b match rb with | .isBool .. | .isNNRat _ .. | .isNegNNRat _ .. => failure | .isNat sβ nb pb => have h : $e =Q (HPow.hPow (γ := $α) $a $b) := ⟨⟩ h.check match ← derive q($a ^ $nb) with | .isBool .. => failure | .isNat sα' ne' pe' => assumeInstancesCommute return .isNat sα' ne' q(isNat_zpow_pos $pb $pe') | .isNegNat sα' ne' pe' => let _c ← synthInstanceQ q(DivisionRing $α) assumeInstancesCommute return .isNegNat sα' ne' q(isInt_zpow_pos $pb $pe') | .isNNRat dsα' qe' nume' dene' pe' => assumeInstancesCommute return .isNNRat dsα' qe' nume' dene' q(isNNRat_zpow_pos $pb $pe') | .isNegNNRat dα' qe' nume' dene' pe' => assumeInstancesCommute let proof := q(isRat_zpow_pos $pb $pe') return .isRat dα' qe' nume' dene' proof | .isNegNat sβ nb pb => have h : $e =Q (HPow.hPow (γ := $α) $a $b) := ⟨⟩ h.check match ← derive q(($a ^ $nb)⁻¹) with | .isBool .. => failure | .isNat sα' ne' pe' => assumeInstancesCommute return .isNat sα' ne' q(isNat_zpow_neg $pb $pe') | .isNegNat sα' ne' pe' => let _c ← synthInstanceQ q(DivisionRing $α) assumeInstancesCommute return .isNegNat sα' ne' q(isInt_zpow_neg $pb $pe') | .isNNRat dsα' qe' nume' dene' pe' => assumeInstancesCommute return .isNNRat dsα' qe' nume' dene' q(isNNRat_zpow_neg $pb $pe') | .isNegNNRat dα' qe' nume' dene' pe' => assumeInstancesCommute return .isRat dα' qe' q(.negOfNat $nume') dene' q(isRat_zpow_neg $pb $pe')
def
Tactic
[ "Mathlib.Data.Int.Cast.Lemmas", "Mathlib.Tactic.NormNum.Basic" ]
Mathlib/Tactic/NormNum/Pow.lean
evalZPow
The `norm_num` extension which identifies expressions of the form `a ^ b`, such that `norm_num` successfully recognises both `a` and `b`, with `b : ℤ`.
IsNatPowModT (p : Prop) (a b m c : Nat) : Prop where run' : p → Nat.mod (Nat.pow a b) m = c
structure
Tactic
[ "Mathlib.Tactic.NormNum.Pow" ]
Mathlib/Tactic/NormNum/PowMod.lean
IsNatPowModT
Represents and proves equalities of the form `a^b % m = c` for natural numbers.
IsNatPowModT.run (p : IsNatPowModT (Nat.mod (Nat.pow a (nat_lit 1)) m = Nat.mod a m) a b m c) : Nat.mod (Nat.pow a b) m = c := p.run' (congr_arg (fun x => x % m) (Nat.pow_one a))
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Pow" ]
Mathlib/Tactic/NormNum/PowMod.lean
IsNatPowModT.run
null
IsNatPowModT.trans (h1 : IsNatPowModT p a b m c) (h2 : IsNatPowModT (Nat.mod (Nat.pow a b) m = c) a b' m c') : IsNatPowModT p a b' m c' := ⟨h2.run' ∘ h1.run'⟩
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Pow" ]
Mathlib/Tactic/NormNum/PowMod.lean
IsNatPowModT.trans
null
IsNatPowModT.bit0 : IsNatPowModT (Nat.mod (Nat.pow a b) m = c) a (nat_lit 2 * b) m (Nat.mod (Nat.mul c c) m) := ⟨fun h1 => by simp only [two_mul, Nat.pow_eq, pow_add, ← h1, Nat.mul_eq]; exact Nat.mul_mod ..⟩
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Pow" ]
Mathlib/Tactic/NormNum/PowMod.lean
IsNatPowModT.bit0
null
natPow_zero_natMod_zero : Nat.mod (Nat.pow a (nat_lit 0)) (nat_lit 0) = nat_lit 1 := by simp [Nat.mod, Nat.modCore]
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Pow" ]
Mathlib/Tactic/NormNum/PowMod.lean
natPow_zero_natMod_zero
null
natPow_zero_natMod_one : Nat.mod (Nat.pow a (nat_lit 0)) (nat_lit 1) = nat_lit 0 := by simp [Nat.mod, Nat.modCore_eq]
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Pow" ]
Mathlib/Tactic/NormNum/PowMod.lean
natPow_zero_natMod_one
null
natPow_zero_natMod_succ_succ : Nat.mod (Nat.pow a (nat_lit 0)) (Nat.succ (Nat.succ m)) = nat_lit 1 := by rfl
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Pow" ]
Mathlib/Tactic/NormNum/PowMod.lean
natPow_zero_natMod_succ_succ
null
natPow_one_natMod : Nat.mod (Nat.pow a (nat_lit 1)) m = Nat.mod a m := by rw [natPow_one]
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Pow" ]
Mathlib/Tactic/NormNum/PowMod.lean
natPow_one_natMod
null
IsNatPowModT.bit1 : IsNatPowModT (Nat.mod (Nat.pow a b) m = c) a (nat_lit 2 * b + 1) m (Nat.mod (Nat.mul c (Nat.mod (Nat.mul c a) m)) m) := ⟨by rintro rfl change a ^ (2 * b + 1) % m = (a ^ b % m) * ((a ^ b % m * a) % m) % m rw [pow_add, two_mul, pow_add, pow_one, Nat.mul_mod (a ^ b % m) a, Nat.mod_mod, ← Nat.mul_mod (a ^ b) a, ← Nat.mul_mod, mul_assoc]⟩
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Pow" ]
Mathlib/Tactic/NormNum/PowMod.lean
IsNatPowModT.bit1
null
partial evalNatPowMod (a b m : Q(ℕ)) : (c : Q(ℕ)) × Q(Nat.mod (Nat.pow $a $b) $m = $c) := if b.natLit! = 0 then haveI : $b =Q 0 := ⟨⟩ if m.natLit! = 0 then -- a ^ 0 % 0 = 1 haveI : $m =Q 0 := ⟨⟩ ⟨q(nat_lit 1), q(natPow_zero_natMod_zero)⟩ else have m' : Q(ℕ) := mkRawNatLit (m.natLit! - 1) if m'.natLit! = 0 then -- a ^ 0 % 1 = 0 haveI : $m =Q 1 := ⟨⟩ ⟨q(nat_lit 0), q(natPow_zero_natMod_one)⟩ else -- a ^ 0 % m = 1 have m'' : Q(ℕ) := mkRawNatLit (m'.natLit! - 1) haveI : $m =Q Nat.succ (Nat.succ $m'') := ⟨⟩ ⟨q(nat_lit 1), q(natPow_zero_natMod_succ_succ)⟩ else if b.natLit! = 1 then -- a ^ 1 % m = a % m have c : Q(ℕ) := mkRawNatLit (a.natLit! % m.natLit!) haveI : $b =Q 1 := ⟨⟩ haveI : $c =Q Nat.mod $a $m := ⟨⟩ ⟨c, q(natPow_one_natMod)⟩ else have c₀ : Q(ℕ) := mkRawNatLit (a.natLit! % m.natLit!) haveI : $c₀ =Q Nat.mod $a $m := ⟨⟩ let ⟨c, p⟩ := go b.natLit!.log2 a m q(nat_lit 1) c₀ b _ .rfl ⟨c, q(($p).run)⟩ where /-- Invariants: `a ^ b₀ % m = c₀`, `depth > 0`, `b >>> depth = b₀` -/ go (depth : Nat) (a m b₀ c₀ b : Q(ℕ)) (p : Q(Prop)) (hp : $p =Q (Nat.mod (Nat.pow $a $b₀) $m = $c₀)) : (c : Q(ℕ)) × Q(IsNatPowModT $p $a $b $m $c) := let b' := b.natLit! let m' := m.natLit! if depth ≤ 1 then let a' := a.natLit! let c₀' := c₀.natLit! if b' &&& 1 == 0 then have c : Q(ℕ) := mkRawNatLit ((c₀' * c₀') % m') haveI : $c =Q Nat.mod (Nat.mul $c₀ $c₀) $m := ⟨⟩ haveI : $b =Q 2 * $b₀ := ⟨⟩ ⟨c, q(IsNatPowModT.bit0)⟩ else have c : Q(ℕ) := mkRawNatLit ((c₀' * ((c₀' * a') % m')) % m') haveI : $c =Q Nat.mod (Nat.mul $c₀ (Nat.mod (Nat.mul $c₀ $a) $m)) $m := ⟨⟩ haveI : $b =Q 2 * $b₀ + 1 := ⟨⟩ ⟨c, q(IsNatPowModT.bit1)⟩ else let d := depth >>> 1 have hi : Q(ℕ) := mkRawNatLit (b' >>> d) let ⟨c1, p1⟩ := go (depth - d) a m b₀ c₀ hi p (by exact hp) let ⟨c2, p2⟩ := go d a m hi c1 b q(Nat.mod (Nat.pow $a $hi) $m = $c1) ⟨⟩ ⟨c2, q(($p1).trans $p2)⟩ ...
def
Tactic
[ "Mathlib.Tactic.NormNum.Pow" ]
Mathlib/Tactic/NormNum/PowMod.lean
evalNatPowMod
Evaluates and proves `a^b % m` for natural numbers using fast modular exponentiation.
not_prime_mul_of_ble (a b n : ℕ) (h : a * b = n) (h₁ : a.ble 1 = false) (h₂ : b.ble 1 = false) : ¬ n.Prime := not_prime_of_mul_eq h (ble_eq_false.mp h₁).ne' (ble_eq_false.mp h₂).ne'
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Prime.Basic" ]
Mathlib/Tactic/NormNum/Prime.lean
not_prime_mul_of_ble
null
deriveNotPrime (n d : ℕ) (en : Q(ℕ)) : Q(¬ Nat.Prime $en) := Id.run <| do let d' : ℕ := n / d let prf : Q($d * $d' = $en) := (q(Eq.refl $en) : Expr) let r : Q(Nat.ble $d 1 = false) := (q(Eq.refl false) : Expr) let r' : Q(Nat.ble $d' 1 = false) := (q(Eq.refl false) : Expr) return q(not_prime_mul_of_ble _ _ _ $prf $r $r')
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Prime.Basic" ]
Mathlib/Tactic/NormNum/Prime.lean
deriveNotPrime
Produce a proof that `n` is not prime from a factor `1 < d < n`. `en` should be the expression that is the natural number literal `n`.
MinFacHelper (n k : ℕ) : Prop := 2 < k ∧ k % 2 = 1 ∧ k ≤ minFac n
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Prime.Basic" ]
Mathlib/Tactic/NormNum/Prime.lean
MinFacHelper
A predicate representing partial progress in a proof of `minFac`.
MinFacHelper.one_lt {n k : ℕ} (h : MinFacHelper n k) : 1 < n := by have : 2 < minFac n := h.1.trans_le h.2.2 obtain rfl | h := n.eq_zero_or_pos · contradiction rcases (succ_le_of_lt h).eq_or_lt with rfl | h · simp_all exact h
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Prime.Basic" ]
Mathlib/Tactic/NormNum/Prime.lean
MinFacHelper.one_lt
null
minFacHelper_0 (n : ℕ) (h1 : Nat.ble (nat_lit 2) n = true) (h2 : nat_lit 1 = n % (nat_lit 2)) : MinFacHelper n (nat_lit 3) := by refine ⟨by simp, by simp, ?_⟩ refine (le_minFac'.mpr fun p hp hpn ↦ ?_).resolve_left (Nat.ne_of_gt (Nat.le_of_ble_eq_true h1)) rcases hp.eq_or_lt with rfl | h · simp [(Nat.dvd_iff_mod_eq_zero ..).1 hpn] at h2 · exact h
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Prime.Basic" ]
Mathlib/Tactic/NormNum/Prime.lean
minFacHelper_0
null
minFacHelper_1 {n k k' : ℕ} (e : k + 2 = k') (h : MinFacHelper n k) (np : minFac n ≠ k) : MinFacHelper n k' := by rw [← e] refine ⟨Nat.lt_add_right _ h.1, ?_, ?_⟩ · rw [add_mod, mod_self, add_zero, mod_mod] exact h.2.1 rcases h.2.2.eq_or_lt with rfl | h2 · exact (np rfl).elim rcases (succ_le_of_lt h2).eq_or_lt with h2|h2 · refine ((h.1.trans_le h.2.2).ne ?_).elim have h3 : 2 ∣ minFac n := by rw [Nat.dvd_iff_mod_eq_zero, ← h2, succ_eq_add_one, add_mod, h.2.1] rw [dvd_prime <| minFac_prime h.one_lt.ne'] at h3 norm_num at h3 exact h3 exact h2
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Prime.Basic" ]
Mathlib/Tactic/NormNum/Prime.lean
minFacHelper_1
null
minFacHelper_2 {n k k' : ℕ} (e : k + 2 = k') (nk : ¬ Nat.Prime k) (h : MinFacHelper n k) : MinFacHelper n k' := by refine minFacHelper_1 e h fun h2 ↦ ?_ rw [← h2] at nk exact nk <| minFac_prime h.one_lt.ne'
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Prime.Basic" ]
Mathlib/Tactic/NormNum/Prime.lean
minFacHelper_2
null
minFacHelper_3 {n k k' : ℕ} (e : k + 2 = k') (nk : (n % k).beq 0 = false) (h : MinFacHelper n k) : MinFacHelper n k' := by refine minFacHelper_1 e h fun h2 ↦ ?_ have nk := Nat.ne_of_beq_eq_false nk rw [← Nat.dvd_iff_mod_eq_zero, ← h2] at nk exact nk <| minFac_dvd n
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Prime.Basic" ]
Mathlib/Tactic/NormNum/Prime.lean
minFacHelper_3
null
isNat_minFac_1 : {a : ℕ} → IsNat a (nat_lit 1) → IsNat a.minFac 1 | _, ⟨rfl⟩ => ⟨minFac_one⟩
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Prime.Basic" ]
Mathlib/Tactic/NormNum/Prime.lean
isNat_minFac_1
null
isNat_minFac_2 : {a a' : ℕ} → IsNat a a' → a' % 2 = 0 → IsNat a.minFac 2 | a, _, ⟨rfl⟩, h => ⟨by rw [cast_ofNat, minFac_eq_two_iff, Nat.dvd_iff_mod_eq_zero, h]⟩
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Prime.Basic" ]
Mathlib/Tactic/NormNum/Prime.lean
isNat_minFac_2
null
isNat_minFac_3 : {n n' : ℕ} → (k : ℕ) → IsNat n n' → MinFacHelper n' k → nat_lit 0 = n' % k → IsNat (minFac n) k | n, _, k, ⟨rfl⟩, h1, h2 => by rw [eq_comm, ← Nat.dvd_iff_mod_eq_zero] at h2 exact ⟨le_antisymm (minFac_le_of_dvd h1.1.le h2) h1.2.2⟩
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Prime.Basic" ]
Mathlib/Tactic/NormNum/Prime.lean
isNat_minFac_3
null
isNat_minFac_4 : {n n' k : ℕ} → IsNat n n' → MinFacHelper n' k → (k * k).ble n' = false → IsNat (minFac n) n' | n, _, k, ⟨rfl⟩, h1, h2 => by refine ⟨(Nat.prime_def_minFac.mp ?_).2⟩ rw [Nat.prime_def_le_sqrt] refine ⟨h1.one_lt, fun m hm hmn h2mn ↦ ?_⟩ exact lt_irrefl m <| calc m ≤ sqrt n := hmn _ < k := sqrt_lt.mpr (ble_eq_false.mp h2) _ ≤ n.minFac := h1.2.2 _ ≤ m := Nat.minFac_le_of_dvd hm h2mn
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Prime.Basic" ]
Mathlib/Tactic/NormNum/Prime.lean
isNat_minFac_4
null
@[norm_num Nat.minFac _] partial evalMinFac : NormNumExt where eval {_ _} e := do let .app (.const ``Nat.minFac _) (n : Q(ℕ)) ← whnfR e | failure let sℕ : Q(AddMonoidWithOne ℕ) := q(instAddMonoidWithOneNat) let ⟨nn, pn⟩ ← deriveNat n sℕ let n' := nn.natLit! let rec aux (ek : Q(ℕ)) (prf : Q(MinFacHelper $nn $ek)) : (c : Q(ℕ)) × Q(IsNat (Nat.minFac $n) $c) := let k := ek.natLit! if n' < k * k then let r : Q(Nat.ble ($ek * $ek) $nn = false) := (q(Eq.refl false) : Expr) ⟨nn, q(isNat_minFac_4 $pn $prf $r)⟩ else let d : ℕ := k.minFac if d < k then have ek' : Q(ℕ) := mkRawNatLit <| k + 2 let pk' : Q($ek + 2 = $ek') := (q(Eq.refl $ek') : Expr) let pd := deriveNotPrime k d ek aux ek' q(minFacHelper_2 $pk' $pd $prf) else if n' % k = 0 then let r : Q(nat_lit 0 = $nn % $ek) := (q(Eq.refl 0) : Expr) let r' : Q(IsNat (minFac $n) $ek) := q(isNat_minFac_3 _ $pn $prf $r) ⟨ek, r'⟩ else let r : Q(Nat.beq ($nn % $ek) 0 = false) := (q(Eq.refl false) : Expr) have ek' : Q(ℕ) := mkRawNatLit <| k + 2 let pk' : Q($ek + 2 = $ek') := (q(Eq.refl $ek') : Expr) aux ek' q(minFacHelper_3 $pk' $r $prf) let rec core : MetaM <| Result q(Nat.minFac $n) := do if n' = 1 then let pn : Q(IsNat $n (nat_lit 1)) := pn return .isNat sℕ q(nat_lit 1) q(isNat_minFac_1 $pn) if n' % 2 = 0 then let pq : Q($nn % 2 = 0) := (q(Eq.refl 0) : Expr) return .isNat sℕ q(nat_lit 2) q(isNat_minFac_2 $pn $pq) let pp : Q(Nat.ble 2 $nn = true) := (q(Eq.refl true) : Expr) let pq : Q(1 = $nn % 2) := (q(Eq.refl (nat_lit 1)) : Expr) let ⟨c, pc⟩ := aux q(nat_lit 3) q(minFacHelper_0 $nn $pp $pq) return .isNat sℕ c pc core
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Prime.Basic" ]
Mathlib/Tactic/NormNum/Prime.lean
evalMinFac
The `norm_num` extension which identifies expressions of the form `minFac n`.
isNat_prime_0 : {n : ℕ} → IsNat n (nat_lit 0) → ¬ n.Prime | _, ⟨rfl⟩ => not_prime_zero
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Prime.Basic" ]
Mathlib/Tactic/NormNum/Prime.lean
isNat_prime_0
null
isNat_prime_1 : {n : ℕ} → IsNat n (nat_lit 1) → ¬ n.Prime | _, ⟨rfl⟩ => not_prime_one
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Prime.Basic" ]
Mathlib/Tactic/NormNum/Prime.lean
isNat_prime_1
null
isNat_prime_2 : {n n' : ℕ} → IsNat n n' → Nat.ble 2 n' = true → IsNat (minFac n') n' → n.Prime | _, _, ⟨rfl⟩, h1, ⟨h2⟩ => prime_def_minFac.mpr ⟨ble_eq.mp h1, h2⟩
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Prime.Basic" ]
Mathlib/Tactic/NormNum/Prime.lean
isNat_prime_2
null
isNat_not_prime {n n' : ℕ} (h : IsNat n n') : ¬n'.Prime → ¬n.Prime := isNat.natElim h
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Prime.Basic" ]
Mathlib/Tactic/NormNum/Prime.lean
isNat_not_prime
null
@[norm_num Nat.Prime _] evalNatPrime : NormNumExt where eval {_ _} e := do let .app (.const `Nat.Prime _) (n : Q(ℕ)) ← whnfR e | failure let ⟨nn, pn⟩ ← deriveNat n _ let n' := nn.natLit! let rec core : MetaM (Result q(Nat.Prime $n)) := do match n' with | 0 => haveI' : $nn =Q 0 := ⟨⟩; return .isFalse q(isNat_prime_0 $pn) | 1 => haveI' : $nn =Q 1 := ⟨⟩; return .isFalse q(isNat_prime_1 $pn) | _ => let d := n'.minFac if d < n' then let prf : Q(¬ Nat.Prime $nn) := deriveNotPrime n' d nn return .isFalse q(isNat_not_prime $pn $prf) let r : Q(Nat.ble 2 $nn = true) := (q(Eq.refl true) : Expr) let .isNat _ _lit (p2n : Q(IsNat (minFac $nn) $nn)) ← evalMinFac.core nn _ nn q(.raw_refl _) nn.natLit! | failure return .isTrue q(isNat_prime_2 $pn $r $p2n) core
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Prime.Basic" ]
Mathlib/Tactic/NormNum/Prime.lean
evalNatPrime
The `norm_num` extension which identifies expressions of the form `Nat.Prime n`.
isNat_realSqrt {x : ℝ} {nx ny : ℕ} (h : IsNat x nx) (hy : ny * ny = nx) : IsNat √x ny := ⟨by simp [h.out, ← hy]⟩
lemma
Tactic
[ "Mathlib.Data.Real.Sqrt" ]
Mathlib/Tactic/NormNum/RealSqrt.lean
isNat_realSqrt
null
isNat_nnrealSqrt {x : ℝ≥0} {nx ny : ℕ} (h : IsNat x nx) (hy : ny * ny = nx) : IsNat (NNReal.sqrt x) ny := ⟨by simp [h.out, ← hy]⟩
lemma
Tactic
[ "Mathlib.Data.Real.Sqrt" ]
Mathlib/Tactic/NormNum/RealSqrt.lean
isNat_nnrealSqrt
null
isNNRat_nnrealSqrt_of_isNNRat {x : ℝ≥0} {n sn : ℕ} {d sd : ℕ} (hn : sn * sn = n) (hd : sd * sd = d) (h : IsNNRat x n d) : IsNNRat (NNReal.sqrt x) sn sd := by obtain ⟨_, rfl⟩ := h refine ⟨?_, ?out⟩ · apply invertibleOfNonzero rw [← mul_self_ne_zero, ← Nat.cast_mul, hd] exact Invertible.ne_zero _ · simp [← hn, ← hd, NNReal.sqrt_mul]
lemma
Tactic
[ "Mathlib.Data.Real.Sqrt" ]
Mathlib/Tactic/NormNum/RealSqrt.lean
isNNRat_nnrealSqrt_of_isNNRat
null
isNat_realSqrt_neg {x : ℝ} {nx : ℕ} (h : IsInt x (Int.negOfNat nx)) : IsNat √x (nat_lit 0) := ⟨by simp [Real.sqrt_eq_zero', h.out]⟩
lemma
Tactic
[ "Mathlib.Data.Real.Sqrt" ]
Mathlib/Tactic/NormNum/RealSqrt.lean
isNat_realSqrt_neg
null
isNat_realSqrt_of_isRat_negOfNat {x : ℝ} {num : ℕ} {denom : ℕ} (h : IsRat x (.negOfNat num) denom) : IsNat √x (nat_lit 0) := by refine ⟨?_⟩ obtain ⟨inv, rfl⟩ := h have h₁ : 0 ≤ (num : ℚ) * ⅟(denom : ℝ) := mul_nonneg (Nat.cast_nonneg' _) (invOf_nonneg.2 <| Nat.cast_nonneg' _) simpa [Nat.cast_zero, Real.sqrt_eq_zero', Int.cast_negOfNat, neg_mul, neg_nonpos] using h₁
lemma
Tactic
[ "Mathlib.Data.Real.Sqrt" ]
Mathlib/Tactic/NormNum/RealSqrt.lean
isNat_realSqrt_of_isRat_negOfNat
null
isNNRat_realSqrt_of_isNNRat {x : ℝ} {n sn : ℕ} {d sd : ℕ} (hn : sn * sn = n) (hd : sd * sd = d) (h : IsNNRat x n d) : IsNNRat √x sn sd := by obtain ⟨_, rfl⟩ := h refine ⟨?_, ?out⟩ · apply invertibleOfNonzero rw [← mul_self_ne_zero, ← Nat.cast_mul, hd] exact Invertible.ne_zero _ · simp [← hn, ← hd, Real.sqrt_mul (mul_self_nonneg ↑sn)]
lemma
Tactic
[ "Mathlib.Data.Real.Sqrt" ]
Mathlib/Tactic/NormNum/RealSqrt.lean
isNNRat_realSqrt_of_isNNRat
null
@[norm_num √_] evalRealSqrt : NormNumExt where eval {u α} e := do match u, α, e with | 0, ~q(ℝ), ~q(√$x) => match ← derive x with | .isBool _ _ => failure | .isNat sℝ ex pf => let x := ex.natLit! let y := Nat.sqrt x unless y * y = x do failure have ey : Q(ℕ) := mkRawNatLit y have pf₁ : Q($ey * $ey = $ex) := (q(Eq.refl $ex) : Expr) assumeInstancesCommute return .isNat q($sℝ) q($ey) q(isNat_realSqrt $pf $pf₁) | .isNegNat _ ex pf => assumeInstancesCommute return .isNat q(inferInstance) q(nat_lit 0) q(isNat_realSqrt_neg $pf) | .isNegNNRat sℝ eq n ed pf => assumeInstancesCommute return .isNat q(inferInstance) q(nat_lit 0) q(isNat_realSqrt_of_isRat_negOfNat $pf) | .isNNRat sℝ eq n' ed pf => let n : ℕ := n'.natLit! let d : ℕ := ed.natLit! let sn := Nat.sqrt n let sd := Nat.sqrt d unless sn * sn = n ∧ sd * sd = d do failure have esn : Q(ℕ) := mkRawNatLit sn have esd : Q(ℕ) := mkRawNatLit sd have hn : Q($esn * $esn = $n') := (q(Eq.refl $n') : Expr) have hd : Q($esd * $esd = $ed) := (q(Eq.refl $ed) : Expr) assumeInstancesCommute return .isNNRat q($sℝ) (sn / sd) _ q($esd) q(isNNRat_realSqrt_of_isNNRat $hn $hd $pf) | _ => failure
def
Tactic
[ "Mathlib.Data.Real.Sqrt" ]
Mathlib/Tactic/NormNum/RealSqrt.lean
evalRealSqrt
`norm_num` extension that evaluates the function `Real.sqrt`.