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
List.range_succ_eq_map' {n nn n' : ℕ} (pn : NormNum.IsNat n nn) (pn' : nn = Nat.succ n') : List.range n = 0 :: List.map Nat.succ (List.range n') := by rw [pn.out, Nat.cast_id, pn', List.range_succ_eq_map] set_option linter.unusedVariables false in
lemma
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
List.range_succ_eq_map'
null
partial List.proveNilOrCons {u : Level} {α : Q(Type u)} (s : Q(List $α)) : MetaM (List.ProveNilOrConsResult s) := s.withApp fun e a => match (e, e.constName, a) with | (_, ``EmptyCollection.emptyCollection, _) => haveI : $s =Q {} := ⟨⟩; pure (.nil q(.refl [])) | (_, ``List.nil, _) => haveI : $s =Q [] := ⟨⟩; pure (.nil q(rfl)) | (_, ``List.cons, #[_, (a : Q($α)), (s' : Q(List $α))]) => haveI : $s =Q $a :: $s' := ⟨⟩; pure (.cons a s' q(rfl)) | (_, ``List.range, #[(n : Q(ℕ))]) => have s : Q(List ℕ) := s; .uncheckedCast _ _ <$> show MetaM (ProveNilOrConsResult s) from do let ⟨nn, pn⟩ ← NormNum.deriveNat n _ haveI' : $s =Q .range $n := ⟨⟩ let nnL := nn.natLit! if nnL = 0 then haveI' : $nn =Q 0 := ⟨⟩ return .nil q(List.range_zero' $pn) else have n' : Q(ℕ) := mkRawNatLit (nnL - 1) have : $nn =Q .succ $n' := ⟨⟩ return .cons _ _ q(List.range_succ_eq_map' $pn (.refl $nn)) | (_, ``List.finRange, #[(n : Q(ℕ))]) => have s : Q(List (Fin $n)) := s .uncheckedCast _ _ <$> show MetaM (ProveNilOrConsResult s) from do haveI' : $s =Q .finRange $n := ⟨⟩ return match ← Nat.unifyZeroOrSucc n with -- We want definitional equality on `n`. | .zero _pf => .nil q(List.finRange_zero) | .succ n' _pf => .cons _ _ q(List.finRange_succ_eq_map $n') | (.const ``List.map [v, _], _, #[(β : Q(Type v)), _, (f : Q($β → $α)), (xxs : Q(List $β))]) => do haveI' : $s =Q ($xxs).map $f := ⟨⟩ return match ← List.proveNilOrCons xxs with | .nil pf => .nil q(($pf ▸ List.map_nil : List.map _ _ = _)) | .cons x xs pf => .cons q($f $x) q(($xs).map $f) q(($pf ▸ List.map_cons : List.map _ _ = _)) | (_, fn, args) => throwError "List.proveNilOrCons: unsupported List expression {s} ({fn}, {args})"
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
List.proveNilOrCons
Either show the expression `s : Q(List α)` is Nil, or remove one element from it. Fails if we cannot determine which of the alternatives apply to the expression.
Multiset.ProveZeroOrConsResult {α : Q(Type u)} (s : Q(Multiset $α)) /-- The set is zero. -/ | zero (pf : Q($s = 0)) /-- The set equals `a` inserted into the strict subset `s'`. -/ | cons (a : Q($α)) (s' : Q(Multiset $α)) (pf : Q($s = Multiset.cons $a $s'))
inductive
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Multiset.ProveZeroOrConsResult
This represents the result of trying to determine whether the given expression `s : Q(Multiset $α)` is either empty or consists of an element inserted into a strict subset.
Multiset.ProveZeroOrConsResult.uncheckedCast {α : Q(Type u)} {β : Q(Type v)} (s : Q(Multiset $α)) (t : Q(Multiset $β)) : Multiset.ProveZeroOrConsResult s → Multiset.ProveZeroOrConsResult t | .zero pf => .zero pf | .cons a s' pf => .cons a s' pf
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Multiset.ProveZeroOrConsResult.uncheckedCast
If `s` unifies with `t`, convert a result for `s` to a result for `t`. If `s` does not unify with `t`, this results in a type-incorrect proof.
Multiset.ProveZeroOrConsResult.eq_trans {α : Q(Type u)} {s t : Q(Multiset $α)} (eq : Q($s = $t)) : Multiset.ProveZeroOrConsResult t → Multiset.ProveZeroOrConsResult s | .zero pf => .zero q(Eq.trans $eq $pf) | .cons a s' pf => .cons a s' q(Eq.trans $eq $pf)
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Multiset.ProveZeroOrConsResult.eq_trans
If `s = t` and we can get the result for `t`, then we can get the result for `s`.
Multiset.insert_eq_cons {α : Type*} (a : α) (s : Multiset α) : insert a s = Multiset.cons a s := rfl
lemma
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Multiset.insert_eq_cons
null
Multiset.range_zero' {n : ℕ} (pn : NormNum.IsNat n 0) : Multiset.range n = 0 := by rw [pn.out, Nat.cast_zero, Multiset.range_zero]
lemma
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Multiset.range_zero'
null
Multiset.range_succ' {n nn n' : ℕ} (pn : NormNum.IsNat n nn) (pn' : nn = Nat.succ n') : Multiset.range n = n' ::ₘ Multiset.range n' := by rw [pn.out, Nat.cast_id, pn', Multiset.range_succ]
lemma
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Multiset.range_succ'
null
partial Multiset.proveZeroOrCons {α : Q(Type u)} (s : Q(Multiset $α)) : MetaM (Multiset.ProveZeroOrConsResult s) := match s.getAppFnArgs with | (``EmptyCollection.emptyCollection, _) => haveI : $s =Q {} := ⟨⟩; pure (.zero q(rfl)) | (``Zero.zero, _) => haveI : $s =Q 0 := ⟨⟩; pure (.zero q(rfl)) | (``Multiset.cons, #[_, (a : Q($α)), (s' : Q(Multiset $α))]) => haveI : $s =Q .cons $a $s' := ⟨⟩ pure (.cons a s' q(rfl)) | (``Multiset.ofList, #[_, (val : Q(List $α))]) => do haveI : $s =Q .ofList $val := ⟨⟩ return match ← List.proveNilOrCons val with | .nil pf => .zero q($pf ▸ Multiset.coe_nil : Multiset.ofList _ = _) | .cons a s' pf => .cons a q($s') q($pf ▸ Multiset.cons_coe $a $s' : Multiset.ofList _ = _) | (``Multiset.range, #[(n : Q(ℕ))]) => do have s : Q(Multiset ℕ) := s; .uncheckedCast _ _ <$> show MetaM (ProveZeroOrConsResult s) from do let ⟨nn, pn⟩ ← NormNum.deriveNat n _ haveI' : $s =Q .range $n := ⟨⟩ let nnL := nn.natLit! if nnL = 0 then haveI' : $nn =Q 0 := ⟨⟩ return .zero q(Multiset.range_zero' $pn) else have n' : Q(ℕ) := mkRawNatLit (nnL - 1) haveI' : $nn =Q ($n').succ := ⟨⟩ return .cons _ _ q(Multiset.range_succ' $pn rfl) | (fn, args) => throwError "Multiset.proveZeroOrCons: unsupported multiset expression {s} ({fn}, {args})"
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Multiset.proveZeroOrCons
Either show the expression `s : Q(Multiset α)` is Zero, or remove one element from it. Fails if we cannot determine which of the alternatives apply to the expression.
Finset.ProveEmptyOrConsResult {α : Q(Type u)} (s : Q(Finset $α)) /-- The set is empty. -/ | empty (pf : Q($s = ∅)) /-- The set equals `a` inserted into the strict subset `s'`. -/ | cons (a : Q($α)) (s' : Q(Finset $α)) (h : Q($a ∉ $s')) (pf : Q($s = Finset.cons $a $s' $h))
inductive
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Finset.ProveEmptyOrConsResult
This represents the result of trying to determine whether the given expression `s : Q(Finset $α)` is either empty or consists of an element inserted into a strict subset.
Finset.ProveEmptyOrConsResult.uncheckedCast {α : Q(Type u)} {β : Q(Type v)} (s : Q(Finset $α)) (t : Q(Finset $β)) : Finset.ProveEmptyOrConsResult s → Finset.ProveEmptyOrConsResult t | .empty pf => .empty pf | .cons a s' h pf => .cons a s' h pf
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Finset.ProveEmptyOrConsResult.uncheckedCast
If `s` unifies with `t`, convert a result for `s` to a result for `t`. If `s` does not unify with `t`, this results in a type-incorrect proof.
Finset.ProveEmptyOrConsResult.eq_trans {α : Q(Type u)} {s t : Q(Finset $α)} (eq : Q($s = $t)) : Finset.ProveEmptyOrConsResult t → Finset.ProveEmptyOrConsResult s | .empty pf => .empty q(Eq.trans $eq $pf) | .cons a s' h pf => .cons a s' h q(Eq.trans $eq $pf)
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Finset.ProveEmptyOrConsResult.eq_trans
If `s = t` and we can get the result for `t`, then we can get the result for `s`.
Finset.insert_eq_cons {α : Type*} [DecidableEq α] (a : α) (s : Finset α) (h : a ∉ s) : insert a s = Finset.cons a s h := by simp
lemma
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Finset.insert_eq_cons
null
Finset.range_zero' {n : ℕ} (pn : NormNum.IsNat n 0) : Finset.range n = {} := by rw [pn.out, Nat.cast_zero, Finset.range_zero]
lemma
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Finset.range_zero'
null
Finset.range_succ' {n nn n' : ℕ} (pn : NormNum.IsNat n nn) (pn' : nn = Nat.succ n') : Finset.range n = Finset.cons n' (Finset.range n') Finset.notMem_range_self := by rw [pn.out, Nat.cast_id, pn', Finset.range_add_one, Finset.insert_eq_cons]
lemma
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Finset.range_succ'
null
Finset.univ_eq_elems {α : Type*} [Fintype α] (elems : Finset α) (complete : ∀ x : α, x ∈ elems) : Finset.univ = elems := by ext x; simpa using complete x
lemma
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Finset.univ_eq_elems
null
partial Finset.proveEmptyOrCons {α : Q(Type u)} (s : Q(Finset $α)) : MetaM (ProveEmptyOrConsResult s) := match s.getAppFnArgs with | (``EmptyCollection.emptyCollection, _) => haveI : $s =Q {} := ⟨⟩; pure (.empty q(rfl)) | (``Finset.cons, #[_, (a : Q($α)), (s' : Q(Finset $α)), (h : Q($a ∉ $s'))]) => haveI : $s =Q .cons $a $s' $h := ⟨⟩ pure (.cons a s' h q(.refl $s)) | (``Finset.mk, #[_, (val : Q(Multiset $α)), (nd : Q(Multiset.Nodup $val))]) => do match ← Multiset.proveZeroOrCons val with | .zero pf => pure <| .empty (q($pf ▸ Finset.mk_zero) : Q(Finset.mk $val $nd = ∅)) | .cons a s' pf => do let h : Q(Multiset.Nodup ($a ::ₘ $s')) := q($pf ▸ $nd) let nd' : Q(Multiset.Nodup $s') := q((Multiset.nodup_cons.mp $h).2) let h' : Q($a ∉ $s') := q((Multiset.nodup_cons.mp $h).1) return (.cons a q(Finset.mk $s' $nd') h' (q($pf ▸ Finset.mk_cons $h) : Q(Finset.mk $val $nd = Finset.cons $a ⟨$s', $nd'⟩ $h'))) | (``Finset.range, #[(n : Q(ℕ))]) => have s : Q(Finset ℕ) := s; .uncheckedCast _ _ <$> show MetaM (ProveEmptyOrConsResult s) from do let ⟨nn, pn⟩ ← NormNum.deriveNat n _ haveI' : $s =Q .range $n := ⟨⟩ let nnL := nn.natLit! if nnL = 0 then haveI : $nn =Q 0 := ⟨⟩ return .empty q(Finset.range_zero' $pn) else have n' : Q(ℕ) := mkRawNatLit (nnL - 1) haveI' : $nn =Q ($n').succ := ⟨⟩ return .cons n' _ _ q(Finset.range_succ' $pn (.refl $nn)) | (``Finset.univ, #[_, (instFT : Q(Fintype $α))]) => do haveI' : $s =Q .univ := ⟨⟩ match (← whnfI instFT).getAppFnArgs with | (``Fintype.mk, #[_, (elems : Q(Finset $α)), (complete : Q(∀ x : $α, x ∈ $elems))]) => do let res ← Finset.proveEmptyOrCons elems pure <| res.eq_trans q(Finset.univ_eq_elems $elems $complete) | e => throwError "Finset.proveEmptyOrCons: could not determine elements of Fintype instance {e}" | (fn, args) => throwError "Finset.proveEmptyOrCons: unsupported finset expression {s} ({fn}, {args})"
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Finset.proveEmptyOrCons
Either show the expression `s : Q(Finset α)` is empty, or remove one element from it. Fails if we cannot determine which of the alternatives apply to the expression.
Result.eq_trans {α : Q(Type u)} {a b : Q($α)} (eq : Q($a = $b)) : Result b → Result a | .isBool true proof => have a : Q(Prop) := a have b : Q(Prop) := b have eq : Q($a = $b) := eq have proof : Q($b) := proof Result.isTrue (x := a) q($eq ▸ $proof) | .isBool false proof => have a : Q(Prop) := a have b : Q(Prop) := b have eq : Q($a = $b) := eq have proof : Q(¬ $b) := proof Result.isFalse (x := a) q($eq ▸ $proof) | .isNat inst lit proof => Result.isNat inst lit q($eq ▸ $proof) | .isNegNat inst lit proof => Result.isNegNat inst lit q($eq ▸ $proof) | .isNNRat inst q n d proof => Result.isNNRat inst q n d q($eq ▸ $proof) | .isNegNNRat inst q n d proof => Result.isNegNNRat inst q n d q($eq ▸ $proof)
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Result.eq_trans
If `a = b` and we can evaluate `b`, then we can evaluate `a`.
protected Finset.sum_empty {β α : Type*} [CommSemiring β] (f : α → β) : IsNat (Finset.sum ∅ f) 0 := ⟨by simp⟩
lemma
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Finset.sum_empty
null
protected Finset.prod_empty {β α : Type*} [CommSemiring β] (f : α → β) : IsNat (Finset.prod ∅ f) 1 := ⟨by simp⟩
lemma
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Finset.prod_empty
null
partial evalFinsetBigop {α : Q(Type u)} {β : Q(Type v)} (op : Q(Finset $α → ($α → $β) → $β)) (f : Q($α → $β)) (res_empty : Result q($op Finset.empty $f)) (res_cons : {a : Q($α)} -> {s' : Q(Finset $α)} -> {h : Q($a ∉ $s')} -> Result (α := β) q($f $a) -> Result (α := β) q($op $s' $f) -> MetaM (Result (α := β) q($op (Finset.cons $a $s' $h) $f))) : (s : Q(Finset $α)) → MetaM (Result (α := β) q($op $s $f)) | s => do match ← Finset.proveEmptyOrCons s with | .empty pf => pure <| res_empty.eq_trans q(congr_fun (congr_arg _ $pf) _) | .cons a s' h pf => do let fa : Q($β) := Expr.betaRev f #[a] let res_fa ← derive fa let res_op_s' : Result q($op $s' $f) ← evalFinsetBigop op f res_empty @res_cons s' let res ← res_cons res_fa res_op_s' let eq : Q($op $s $f = $op (Finset.cons $a $s' $h) $f) := q(congr_fun (congr_arg _ $pf) _) pure (res.eq_trans eq) attribute [local instance] monadLiftOptionMetaM in
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
evalFinsetBigop
Evaluate a big operator applied to a finset by repeating `proveEmptyOrCons` until we exhaust all elements of the set.
@[norm_num @Finset.prod _ _ _ _ _] partial evalFinsetProd : NormNumExt where eval {u β} e := do let .app (.app (.app (.app (.app (.const ``Finset.prod [v, _]) α) β') _) s) f ← whnfR e | failure guard <| ← withNewMCtxDepth <| isDefEq β β' have α : Q(Type v) := α have s : Q(Finset $α) := s have f : Q($α → $β) := f let instCS : Q(CommSemiring $β) ← synthInstanceQ q(CommSemiring $β) <|> throwError "not a commutative semiring: {β}" let instS : Q(Semiring $β) := q(CommSemiring.toSemiring) let n : Q(ℕ) := .lit (.natVal 1) let pf : Q(IsNat (Finset.prod ∅ $f) $n) := q(@Finset.prod_empty $β $α $instCS $f) let res_empty := Result.isNat _ n pf evalFinsetBigop q(Finset.prod) f res_empty (fun {a s' h} res_fa res_prod_s' ↦ do let fa : Q($β) := Expr.app f a let res ← res_fa.mul res_prod_s' let eq : Q(Finset.prod (Finset.cons $a $s' $h) $f = $fa * Finset.prod $s' $f) := q(Finset.prod_cons $h) pure <| res.eq_trans eq) s attribute [local instance] monadLiftOptionMetaM in
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
evalFinsetProd
`norm_num` plugin for evaluating products of finsets. If your finset is not supported, you can add it to the match in `Finset.proveEmptyOrCons`.
@[norm_num @Finset.sum _ _ _ _ _] partial evalFinsetSum : NormNumExt where eval {u β} e := do let .app (.app (.app (.app (.app (.const ``Finset.sum [v, _]) α) β') _) s) f ← whnfR e | failure guard <| ← withNewMCtxDepth <| isDefEq β β' have α : Q(Type v) := α have s : Q(Finset $α) := s have f : Q($α → $β) := f let instCS : Q(CommSemiring $β) ← synthInstanceQ q(CommSemiring $β) <|> throwError "not a commutative semiring: {β}" let pf : Q(IsNat (Finset.sum ∅ $f) (nat_lit 0)) := q(@Finset.sum_empty $β $α $instCS $f) let res_empty := Result.isNat _ _ q($pf) evalFinsetBigop q(Finset.sum) f res_empty (fun {a s' h} res_fa res_sum_s' ↦ do let fa : Q($β) := Expr.app f a let res ← res_fa.add res_sum_s' let eq : Q(Finset.sum (Finset.cons $a $s' $h) $f = $fa + Finset.sum $s' $f) := q(Finset.sum_cons $h) pure <| res.eq_trans eq) s
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
evalFinsetSum
`norm_num` plugin for evaluating sums of finsets. If your finset is not supported, you can add it to the match in `Finset.proveEmptyOrCons`.
NormNumExt where /-- The extension should be run in the `pre` phase when used as simp plugin. -/ pre := true /-- The extension should be run in the `post` phase when used as simp plugin. -/ post := true /-- Attempts to prove an expression is equal to some explicit number of the relevant type. -/ eval {u : Level} {α : Q(Type u)} (e : Q($α)) : MetaM (Result e) /-- The name of the `norm_num` extension. -/ name : Name := by exact decl_name% variable {u : Level}
structure
Tactic
[ "Mathlib.Lean.Expr.Rat", "Mathlib.Tactic.Hint", "Mathlib.Tactic.NormNum.Result", "Mathlib.Util.AtLocation", "Mathlib.Util.Qq", "Lean.Elab.Tactic.Location" ]
Mathlib/Tactic/NormNum/Core.lean
NormNumExt
Attribute for identifying `norm_num` extensions. -/ syntax (name := norm_num) "norm_num " term,+ : attr namespace Mathlib namespace Meta.NormNum initialize registerTraceClass `Tactic.norm_num /-- An extension for `norm_num`.
mkNormNumExt (n : Name) : ImportM NormNumExt := do let { env, opts, .. } ← read IO.ofExcept <| unsafe env.evalConstCheck NormNumExt opts ``NormNumExt n
def
Tactic
[ "Mathlib.Lean.Expr.Rat", "Mathlib.Tactic.Hint", "Mathlib.Tactic.NormNum.Result", "Mathlib.Util.AtLocation", "Mathlib.Util.Qq", "Lean.Elab.Tactic.Location" ]
Mathlib/Tactic/NormNum/Core.lean
mkNormNumExt
Read a `norm_num` extension from a declaration of the right type.
Entry := Array (Array DiscrTree.Key) × Name
abbrev
Tactic
[ "Mathlib.Lean.Expr.Rat", "Mathlib.Tactic.Hint", "Mathlib.Tactic.NormNum.Result", "Mathlib.Util.AtLocation", "Mathlib.Util.Qq", "Lean.Elab.Tactic.Location" ]
Mathlib/Tactic/NormNum/Core.lean
Entry
Each `norm_num` extension is labelled with a collection of patterns which determine the expressions to which it should be applied.
NormNums where /-- The tree of `norm_num` extensions. -/ tree : DiscrTree NormNumExt := {} /-- Erased `norm_num`s. -/ erased : PHashSet Name := {} deriving Inhabited
structure
Tactic
[ "Mathlib.Lean.Expr.Rat", "Mathlib.Tactic.Hint", "Mathlib.Tactic.NormNum.Result", "Mathlib.Util.AtLocation", "Mathlib.Util.Qq", "Lean.Elab.Tactic.Location" ]
Mathlib/Tactic/NormNum/Core.lean
NormNums
The state of the `norm_num` extension environment
derive {α : Q(Type u)} (e : Q($α)) (post := false) : MetaM (Result e) := do if e.isRawNatLit then let lit : Q(ℕ) := e return .isNat (q(instAddMonoidWithOneNat) : Q(AddMonoidWithOne ℕ)) lit (q(IsNat.raw_refl $lit) : Expr) profileitM Exception "norm_num" (← getOptions) do let s ← saveState let normNums := normNumExt.getState (← getEnv) let arr ← normNums.tree.getMatch e for ext in arr do if (bif post then ext.post else ext.pre) && ! normNums.erased.contains ext.name then try let new ← withReducibleAndInstances <| ext.eval e trace[Tactic.norm_num] "{ext.name}:\n{e} ==> {new}" return new catch err => trace[Tactic.norm_num] "{ext.name} failed {e}: {err.toMessageData}" s.restore throwError "{e}: no norm_nums apply"
def
Tactic
[ "Mathlib.Lean.Expr.Rat", "Mathlib.Tactic.Hint", "Mathlib.Tactic.NormNum.Result", "Mathlib.Util.AtLocation", "Mathlib.Util.Qq", "Lean.Elab.Tactic.Location" ]
Mathlib/Tactic/NormNum/Core.lean
derive
Environment extensions for `norm_num` declarations -/ initialize normNumExt : ScopedEnvExtension Entry (Entry × NormNumExt) NormNums ← -- we only need this to deduplicate entries in the DiscrTree have : BEq NormNumExt := ⟨fun _ _ ↦ false⟩ /- Insert `v : NormNumExt` into the tree `dt` on all key sequences given in `kss`. -/ let insert kss v dt := kss.foldl (fun dt ks ↦ dt.insertCore ks v) dt registerScopedEnvExtension { mkInitial := pure {} ofOLeanEntry := fun _ e@(_, n) ↦ return (e, ← mkNormNumExt n) toOLeanEntry := (·.1) addEntry := fun { tree, erased } ((kss, n), ext) ↦ { tree := insert kss ext tree, erased := erased.erase n } } /-- Run each registered `norm_num` extension on an expression, returning a `NormNum.Result`.
deriveNat {α : Q(Type u)} (e : Q($α)) (_inst : Q(AddMonoidWithOne $α) := by with_reducible assumption) : MetaM ((lit : Q(ℕ)) × Q(IsNat $e $lit)) := do let .isNat _ lit proof ← derive e | failure pure ⟨lit, proof⟩
def
Tactic
[ "Mathlib.Lean.Expr.Rat", "Mathlib.Tactic.Hint", "Mathlib.Tactic.NormNum.Result", "Mathlib.Util.AtLocation", "Mathlib.Util.Qq", "Lean.Elab.Tactic.Location" ]
Mathlib/Tactic/NormNum/Core.lean
deriveNat
Run each registered `norm_num` extension on a typed expression `e : α`, returning a typed expression `lit : ℕ`, and a proof of `isNat e lit`.
deriveInt {α : Q(Type u)} (e : Q($α)) (_inst : Q(Ring $α) := by with_reducible assumption) : MetaM ((lit : Q(ℤ)) × Q(IsInt $e $lit)) := do let some ⟨_, lit, proof⟩ := (← derive e).toInt | failure pure ⟨lit, proof⟩
def
Tactic
[ "Mathlib.Lean.Expr.Rat", "Mathlib.Tactic.Hint", "Mathlib.Tactic.NormNum.Result", "Mathlib.Util.AtLocation", "Mathlib.Util.Qq", "Lean.Elab.Tactic.Location" ]
Mathlib/Tactic/NormNum/Core.lean
deriveInt
Run each registered `norm_num` extension on a typed expression `e : α`, returning a typed expression `lit : ℤ`, and a proof of `IsInt e lit` in expression form.
deriveRat {α : Q(Type u)} (e : Q($α)) (_inst : Q(DivisionRing $α) := by with_reducible assumption) : MetaM (ℚ × (n : Q(ℤ)) × (d : Q(ℕ)) × Q(IsRat $e $n $d)) := do let some res := (← derive e).toRat' | failure pure res
def
Tactic
[ "Mathlib.Lean.Expr.Rat", "Mathlib.Tactic.Hint", "Mathlib.Tactic.NormNum.Result", "Mathlib.Util.AtLocation", "Mathlib.Util.Qq", "Lean.Elab.Tactic.Location" ]
Mathlib/Tactic/NormNum/Core.lean
deriveRat
Run each registered `norm_num` extension on a typed expression `e : α`, returning a rational number, typed expressions `n : ℤ` and `d : ℕ` for the numerator and denominator, and a proof of `IsRat e n d` in expression form.
deriveBool (p : Q(Prop)) : MetaM ((b : Bool) × BoolResult p b) := do let .isBool b prf ← derive q($p) | failure pure ⟨b, prf⟩
def
Tactic
[ "Mathlib.Lean.Expr.Rat", "Mathlib.Tactic.Hint", "Mathlib.Tactic.NormNum.Result", "Mathlib.Util.AtLocation", "Mathlib.Util.Qq", "Lean.Elab.Tactic.Location" ]
Mathlib/Tactic/NormNum/Core.lean
deriveBool
Run each registered `norm_num` extension on a typed expression `p : Prop`, and returning the truth or falsity of `p' : Prop` from an equivalence `p ↔ p'`.
deriveBoolOfIff (p p' : Q(Prop)) (hp : Q($p ↔ $p')) : MetaM ((b : Bool) × BoolResult p' b) := do let ⟨b, pb⟩ ← deriveBool p match b with | true => return ⟨true, q(Iff.mp $hp $pb)⟩ | false => return ⟨false, q((Iff.not $hp).mp $pb)⟩
def
Tactic
[ "Mathlib.Lean.Expr.Rat", "Mathlib.Tactic.Hint", "Mathlib.Tactic.NormNum.Result", "Mathlib.Util.AtLocation", "Mathlib.Util.Qq", "Lean.Elab.Tactic.Location" ]
Mathlib/Tactic/NormNum/Core.lean
deriveBoolOfIff
Run each registered `norm_num` extension on a typed expression `p : Prop`, and returning the truth or falsity of `p' : Prop` from an equivalence `p ↔ p'`.
eval (e : Expr) (post := false) : MetaM Simp.Result := do if e.isExplicitNumber then return { expr := e } let ⟨_, _, e⟩ ← inferTypeQ' e (← derive e post).toSimpResult
def
Tactic
[ "Mathlib.Lean.Expr.Rat", "Mathlib.Tactic.Hint", "Mathlib.Tactic.NormNum.Result", "Mathlib.Util.AtLocation", "Mathlib.Util.Qq", "Lean.Elab.Tactic.Location" ]
Mathlib/Tactic/NormNum/Core.lean
eval
Run each registered `norm_num` extension on an expression, returning a `Simp.Result`.
NormNums.eraseCore (d : NormNums) (declName : Name) : NormNums := { d with erased := d.erased.insert declName }
def
Tactic
[ "Mathlib.Lean.Expr.Rat", "Mathlib.Tactic.Hint", "Mathlib.Tactic.NormNum.Result", "Mathlib.Util.AtLocation", "Mathlib.Util.Qq", "Lean.Elab.Tactic.Location" ]
Mathlib/Tactic/NormNum/Core.lean
NormNums.eraseCore
Erases a name marked `norm_num` by adding it to the state's `erased` field and removing it from the state's list of `Entry`s.
NormNums.erase {m : Type → Type} [Monad m] [MonadError m] (d : NormNums) (declName : Name) : m NormNums := do unless d.tree.values.any (·.name == declName) && ! d.erased.contains declName do throwError "'{declName}' does not have [norm_num] attribute" return d.eraseCore declName initialize registerBuiltinAttribute { name := `norm_num descr := "adds a norm_num extension" applicationTime := .afterCompilation add := fun declName stx kind ↦ match stx with | `(attr| norm_num $es,*) => do let env ← getEnv unless (env.getModuleIdxFor? declName).isNone do throwError "invalid attribute 'norm_num', declaration is in an imported module" if (IR.getSorryDep env declName).isSome then return -- ignore in progress definitions let ext ← mkNormNumExt 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 normNumExt.add ((keys, declName), ext) kind | _ => throwUnsupportedSyntax erase := fun declName => do let s := normNumExt.getState (← getEnv) let s ← s.erase declName modifyEnv fun env => normNumExt.modifyState env fun _ => s }
def
Tactic
[ "Mathlib.Lean.Expr.Rat", "Mathlib.Tactic.Hint", "Mathlib.Tactic.NormNum.Result", "Mathlib.Util.AtLocation", "Mathlib.Util.Qq", "Lean.Elab.Tactic.Location" ]
Mathlib/Tactic/NormNum/Core.lean
NormNums.erase
Erase a name marked as a `norm_num` attribute. Check that it does in fact have the `norm_num` attribute by making sure it names a `NormNumExt` found somewhere in the state's tree, and is not erased.
tryNormNum (post := false) (e : Expr) : SimpM Simp.Step := do try return .done (← eval e post) catch _ => return .continue variable (ctx : Simp.Context) (useSimp := true) in mutual /-- A discharger which calls `norm_num`. -/ partial def discharge (e : Expr) : SimpM (Option Expr) := do (← deriveSimp e).ofTrue /-- A `Methods` implementation which calls `norm_num`. -/ partial def methods : Simp.Methods := if useSimp then { pre := Simp.preDefault #[] >> tryNormNum post := Simp.postDefault #[] >> tryNormNum (post := true) discharge? := discharge } else { pre := tryNormNum post := tryNormNum (post := true) discharge? := discharge } /-- Traverses the given expression using simp and normalises any numbers it finds. -/ partial def deriveSimp (e : Expr) : MetaM Simp.Result := (·.1) <$> Simp.main e ctx (methods := methods)
def
Tactic
[ "Mathlib.Lean.Expr.Rat", "Mathlib.Tactic.Hint", "Mathlib.Tactic.NormNum.Result", "Mathlib.Util.AtLocation", "Mathlib.Util.Qq", "Lean.Elab.Tactic.Location" ]
Mathlib/Tactic/NormNum/Core.lean
tryNormNum
A simp plugin which calls `NormNum.eval`.
getSimpContext (cfg args : Syntax) (simpOnly := false) : TacticM Simp.Context := do let config ← elabSimpConfigCore cfg let simpTheorems ← if simpOnly then simpOnlyBuiltins.foldlM (·.addConst ·) {} else getSimpTheorems let { ctx, .. } ← elabSimpArgs args[0] (eraseLocal := false) (kind := .simp) (simprocs := {}) (← Simp.mkContext config (simpTheorems := #[simpTheorems]) (congrTheorems := ← getSimpCongrTheorems)) return ctx open Elab Tactic in
def
Tactic
[ "Mathlib.Lean.Expr.Rat", "Mathlib.Tactic.Hint", "Mathlib.Tactic.NormNum.Result", "Mathlib.Util.AtLocation", "Mathlib.Util.Qq", "Lean.Elab.Tactic.Location" ]
Mathlib/Tactic/NormNum/Core.lean
getSimpContext
Constructs a simp context from the simp argument syntax.
elabNormNum (cfg args loc : Syntax) (simpOnly := false) (useSimp := true) : TacticM Unit := withMainContext do let ctx ← getSimpContext cfg args (!useSimp || simpOnly) let loc := expandOptLocation loc transformAtNondepPropLocation (fun e ctx ↦ deriveSimp ctx useSimp e) "norm_num" loc (failIfUnchanged := false) (mayCloseGoalFromHyp := true) ctx
def
Tactic
[ "Mathlib.Lean.Expr.Rat", "Mathlib.Tactic.Hint", "Mathlib.Tactic.NormNum.Result", "Mathlib.Util.AtLocation", "Mathlib.Util.Qq", "Lean.Elab.Tactic.Location" ]
Mathlib/Tactic/NormNum/Core.lean
elabNormNum
Elaborates a call to `norm_num only? [args]` or `norm_num1`. * `args`: the `(simpArgs)?` syntax for simp arguments * `loc`: the `(location)?` syntax for the optional location argument * `simpOnly`: true if `only` was used in `norm_num` * `useSimp`: false if `norm_num1` was used, in which case only the structural parts of `simp` will be used, not any of the post-processing that `simp only` does without lemmas
@[tactic normNum1Conv] elabNormNum1Conv : Tactic := fun _ ↦ withMainContext do let ctx ← getSimpContext mkNullNode mkNullNode true Conv.applySimpResult (← deriveSimp ctx (← instantiateMVars (← Conv.getLhs)) (useSimp := false)) @[inherit_doc normNum] syntax (name := normNumConv) "norm_num" optConfig &" only"? (simpArgs)? : conv
def
Tactic
[ "Mathlib.Lean.Expr.Rat", "Mathlib.Tactic.Hint", "Mathlib.Tactic.NormNum.Result", "Mathlib.Util.AtLocation", "Mathlib.Util.Qq", "Lean.Elab.Tactic.Location" ]
Mathlib/Tactic/NormNum/Core.lean
elabNormNum1Conv
Normalize numerical expressions. Supports the operations `+` `-` `*` `/` `⁻¹` `^` and `%` over numerical types such as `ℕ`, `ℤ`, `ℚ`, `ℝ`, `ℂ` and some general algebraic types, and can prove goals of the form `A = B`, `A ≠ B`, `A < B` and `A ≤ B`, where `A` and `B` are numerical expressions. It also has a relatively simple primality prover. -/ elab (name := normNum) "norm_num" cfg:optConfig only:&" only"? args:(simpArgs ?) loc:(location ?) : tactic => elabNormNum cfg args loc (simpOnly := only.isSome) (useSimp := true) /-- Basic version of `norm_num` that does not call `simp`. -/ elab (name := normNum1) "norm_num1" loc:(location ?) : tactic => elabNormNum mkNullNode mkNullNode loc (simpOnly := true) (useSimp := false) open Lean Elab Tactic @[inherit_doc normNum1] syntax (name := normNum1Conv) "norm_num1" : conv /-- Elaborator for `norm_num1` conv tactic.
@[tactic normNumConv] elabNormNumConv : Tactic := fun stx ↦ withMainContext do let ctx ← getSimpContext stx[1] stx[3] !stx[2].isNone Conv.applySimpResult (← deriveSimp ctx (← instantiateMVars (← Conv.getLhs)) (useSimp := true))
def
Tactic
[ "Mathlib.Lean.Expr.Rat", "Mathlib.Tactic.Hint", "Mathlib.Tactic.NormNum.Result", "Mathlib.Util.AtLocation", "Mathlib.Util.Qq", "Lean.Elab.Tactic.Location" ]
Mathlib/Tactic/NormNum/Core.lean
elabNormNumConv
Elaborator for `norm_num` conv tactic.
isInt_ediv_zero : ∀ {a b r : ℤ}, IsInt a r → IsNat b (nat_lit 0) → IsNat (a / b) (nat_lit 0) | _, _, _, ⟨rfl⟩, ⟨rfl⟩ => ⟨by simp [Int.ediv_zero]⟩
lemma
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/NormNum/DivMod.lean
isInt_ediv_zero
null
isInt_ediv {a b q m a' : ℤ} {b' r : ℕ} (ha : IsInt a a') (hb : IsNat b b') (hm : q * b' = m) (h : r + m = a') (h₂ : Nat.blt r b' = true) : IsInt (a / b) q := ⟨by obtain ⟨⟨rfl⟩, ⟨rfl⟩⟩ := ha, hb simp only [Nat.blt_eq] at h₂; simp only [← h, ← hm, Int.cast_id] rw [Int.add_mul_ediv_right _ _ (Int.ofNat_ne_zero.2 ((Nat.zero_le ..).trans_lt h₂).ne')] rw [Int.ediv_eq_zero_of_lt, zero_add] <;> [simp; simpa using h₂]⟩
lemma
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/NormNum/DivMod.lean
isInt_ediv
null
isInt_ediv_neg {a b q q' : ℤ} (h : IsInt (a / -b) q) (hq : -q = q') : IsInt (a / b) q' := ⟨by rw [Int.cast_id, ← hq, ← @Int.cast_id q, ← h.out, ← Int.ediv_neg, Int.neg_neg]⟩
lemma
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/NormNum/DivMod.lean
isInt_ediv_neg
null
isNat_neg_of_isNegNat {a : ℤ} {b : ℕ} (h : IsInt a (.negOfNat b)) : IsNat (-a) b := ⟨by simp [h.out]⟩ attribute [local instance] monadLiftOptionMetaM in
lemma
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/NormNum/DivMod.lean
isNat_neg_of_isNegNat
null
@[norm_num (_ : ℤ) / _, Int.ediv _ _] partial evalIntDiv : NormNumExt where eval {u α} e := do let .app (.app f (a : Q(ℤ))) (b : Q(ℤ)) ← whnfR e | failure guard <|← withNewMCtxDepth <| isDefEq f q(HDiv.hDiv (α := ℤ)) haveI' : u =QL 0 := ⟨⟩; haveI' : $α =Q ℤ := ⟨⟩ haveI' : $e =Q ($a / $b) := ⟨⟩ let rℤ : Q(Ring ℤ) := q(Int.instRing) let ⟨za, na, pa⟩ ← (← derive a).toInt rℤ match ← derive (u := .zero) b with | .isNat inst nb pb => assumeInstancesCommute if nb.natLit! == 0 then have _ : $nb =Q nat_lit 0 := ⟨⟩ return .isNat q(instAddMonoidWithOne) q(nat_lit 0) q(isInt_ediv_zero $pa $pb) else let ⟨zq, q, p⟩ := core a na za pa b nb pb return .isInt rℤ q zq p | .isNegNat _ nb pb => assumeInstancesCommute let ⟨zq, q, p⟩ := core a na za pa q(-$b) nb q(isNat_neg_of_isNegNat $pb) have q' := mkRawIntLit (-zq) have : Q(-$q = $q') := (q(Eq.refl $q') :) return .isInt rℤ q' (-zq) q(isInt_ediv_neg $p $this) | _ => failure where /-- Given a result for evaluating `a b` in `ℤ` where `b > 0`, evaluate `a / b`. -/ core (a na : Q(ℤ)) (za : ℤ) (pa : Q(IsInt $a $na)) (b : Q(ℤ)) (nb : Q(ℕ)) (pb : Q(IsNat $b $nb)) : ℤ × (q : Q(ℤ)) × Q(IsInt ($a / $b) $q) := let b := nb.natLit! let q := za / b have nq := mkRawIntLit q let r := za.natMod b have nr : Q(ℕ) := mkRawNatLit r let m := q * b have nm := mkRawIntLit m have pf₁ : Q($nq * $nb = $nm) := (q(Eq.refl $nm) :) have pf₂ : Q($nr + $nm = $na) := (q(Eq.refl $na) :) have pf₃ : Q(Nat.blt $nr $nb = true) := (q(Eq.refl true) :) ⟨q, nq, q(isInt_ediv $pa $pb $pf₁ $pf₂ $pf₃)⟩
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/NormNum/DivMod.lean
evalIntDiv
The `norm_num` extension which identifies expressions of the form `Int.ediv a b`, such that `norm_num` successfully recognises both `a` and `b`.
isInt_emod_zero : ∀ {a b r : ℤ}, IsInt a r → IsNat b (nat_lit 0) → IsInt (a % b) r | _, _, _, e, ⟨rfl⟩ => by simp [e]
lemma
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/NormNum/DivMod.lean
isInt_emod_zero
null
isInt_emod {a b q m a' : ℤ} {b' r : ℕ} (ha : IsInt a a') (hb : IsNat b b') (hm : q * b' = m) (h : r + m = a') (h₂ : Nat.blt r b' = true) : IsNat (a % b) r := ⟨by obtain ⟨⟨rfl⟩, ⟨rfl⟩⟩ := ha, hb simp only [← h, ← hm, Int.add_mul_emod_self_right] rw [Int.emod_eq_of_lt] <;> [simp; simpa using h₂]⟩
lemma
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/NormNum/DivMod.lean
isInt_emod
null
isInt_emod_neg {a b : ℤ} {r : ℕ} (h : IsNat (a % -b) r) : IsNat (a % b) r := ⟨by rw [← Int.emod_neg, h.out]⟩ attribute [local instance] monadLiftOptionMetaM in
lemma
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/NormNum/DivMod.lean
isInt_emod_neg
null
@[norm_num (_ : ℤ) % _, Int.emod _ _] partial evalIntMod : NormNumExt where eval {u α} e := do let .app (.app f (a : Q(ℤ))) (b : Q(ℤ)) ← whnfR e | failure guard <|← withNewMCtxDepth <| isDefEq f q(HMod.hMod (α := ℤ)) haveI' : u =QL 0 := ⟨⟩; haveI' : $α =Q ℤ := ⟨⟩ haveI' : $e =Q ($a % $b) := ⟨⟩ let rℤ : Q(Ring ℤ) := q(Int.instRing) let some ⟨za, na, pa⟩ := (← derive a).toInt rℤ | failure go a na za pa b (← derive (u := .zero) b) where /-- Given a result for evaluating `a b` in `ℤ`, evaluate `a % b`. -/ go (a na : Q(ℤ)) (za : ℤ) (pa : Q(IsInt $a $na)) (b : Q(ℤ)) : Result b → Option (Result q($a % $b)) | .isNat inst nb pb => do assumeInstancesCommute if nb.natLit! == 0 then have _ : $nb =Q nat_lit 0 := ⟨⟩ return .isInt q(Int.instRing) na za q(isInt_emod_zero $pa $pb) else let ⟨r, p⟩ := core a na za pa b nb pb return .isNat q(instAddMonoidWithOne) r p | .isNegNat _ nb pb => do assumeInstancesCommute let ⟨r, p⟩ := core a na za pa q(-$b) nb q(isNat_neg_of_isNegNat $pb) return .isNat q(instAddMonoidWithOne) r q(isInt_emod_neg $p) | _ => none /-- Given a result for evaluating `a b` in `ℤ` where `b > 0`, evaluate `a % b`. -/ core (a na : Q(ℤ)) (za : ℤ) (pa : Q(IsInt $a $na)) (b : Q(ℤ)) (nb : Q(ℕ)) (pb : Q(IsNat $b $nb)) : (r : Q(ℕ)) × Q(IsNat ($a % $b) $r) := let b := nb.natLit! let q := za / b have nq := mkRawIntLit q let r := za.natMod b have nr : Q(ℕ) := mkRawNatLit r let m := q * b have nm := mkRawIntLit m have pf₁ : Q($nq * $nb = $nm) := (q(Eq.refl $nm) :) have pf₂ : Q($nr + $nm = $na) := (q(Eq.refl $na) :) have pf₃ : Q(Nat.blt $nr $nb = true) := (q(Eq.refl true) :) ⟨nr, q(isInt_emod $pa $pb $pf₁ $pf₂ $pf₃)⟩
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/NormNum/DivMod.lean
evalIntMod
The `norm_num` extension which identifies expressions of the form `Int.emod a b`, such that `norm_num` successfully recognises both `a` and `b`.
isInt_dvd_true : {a b : ℤ} → {a' b' c : ℤ} → IsInt a a' → IsInt b b' → Int.mul a' c = b' → a ∣ b | _, _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨_, rfl⟩
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/NormNum/DivMod.lean
isInt_dvd_true
null
isInt_dvd_false : {a b : ℤ} → {a' b' : ℤ} → IsInt a a' → IsInt b b' → Int.emod b' a' != 0 → ¬a ∣ b | _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, e => mt Int.emod_eq_zero_of_dvd (by simpa using e) attribute [local instance] monadLiftOptionMetaM in
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/NormNum/DivMod.lean
isInt_dvd_false
null
@[norm_num (_ : ℤ) ∣ _] evalIntDvd : NormNumExt where eval {u α} e := do let .app (.app f (a : Q(ℤ))) (b : Q(ℤ)) ← whnfR e | failure haveI' : u =QL 0 := ⟨⟩; haveI' : $α =Q Prop := ⟨⟩ haveI' : $e =Q ($a ∣ $b) := ⟨⟩ guard <|← withNewMCtxDepth <| isDefEq f q(Dvd.dvd (α := ℤ)) let rℤ : Q(Ring ℤ) := q(Int.instRing) let ⟨za, na, pa⟩ ← (← derive a).toInt rℤ let ⟨zb, nb, pb⟩ ← (← derive b).toInt rℤ if zb % za == 0 then let zc := zb / za have c := mkRawIntLit zc haveI' : Int.mul $na $c =Q $nb := ⟨⟩ return .isTrue q(isInt_dvd_true $pa $pb (.refl $nb)) else have : Q(Int.emod $nb $na != 0) := (q(Eq.refl true) : Expr) return .isFalse q(isInt_dvd_false $pa $pb $this)
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/NormNum/DivMod.lean
evalIntDvd
The `norm_num` extension which identifies expressions of the form `(a : ℤ) ∣ b`, such that `norm_num` successfully recognises both `a` and `b`.
isNat_eq_false [AddMonoidWithOne α] [CharZero α] : {a b : α} → {a' b' : ℕ} → IsNat a a' → IsNat b b' → Nat.beq a' b' = false → ¬a = b | _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, h => by simpa using Nat.ne_of_beq_eq_false h
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Inv" ]
Mathlib/Tactic/NormNum/Eq.lean
isNat_eq_false
null
isInt_eq_false [Ring α] [CharZero α] : {a b : α} → {a' b' : ℤ} → IsInt a a' → IsInt b b' → decide (a' = b') = false → ¬a = b | _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, h => by simpa using of_decide_eq_false h
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Inv" ]
Mathlib/Tactic/NormNum/Eq.lean
isInt_eq_false
null
NNRat.invOf_denom_swap [Semiring α] (n₁ n₂ : ℕ) (a₁ a₂ : α) [Invertible a₁] [Invertible a₂] : n₁ * ⅟a₁ = n₂ * ⅟a₂ ↔ n₁ * a₂ = n₂ * a₁ := by rw [mul_invOf_eq_iff_eq_mul_right, ← Nat.commute_cast, mul_assoc, ← mul_left_eq_iff_eq_invOf_mul, Nat.commute_cast]
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Inv" ]
Mathlib/Tactic/NormNum/Eq.lean
NNRat.invOf_denom_swap
null
isNNRat_eq_false [Semiring α] [CharZero α] : {a b : α} → {na nb : ℕ} → {da db : ℕ} → IsNNRat a na da → IsNNRat b nb db → decide (Nat.mul na db = Nat.mul nb da) = false → ¬a = b | _, _, _, _, _, _, ⟨_, rfl⟩, ⟨_, rfl⟩, h => by rw [NNRat.invOf_denom_swap]; exact mod_cast of_decide_eq_false h
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Inv" ]
Mathlib/Tactic/NormNum/Eq.lean
isNNRat_eq_false
null
Rat.invOf_denom_swap [Ring α] (n₁ n₂ : ℤ) (a₁ a₂ : α) [Invertible a₁] [Invertible a₂] : n₁ * ⅟a₁ = n₂ * ⅟a₂ ↔ n₁ * a₂ = n₂ * a₁ := by rw [mul_invOf_eq_iff_eq_mul_right, ← Int.commute_cast, mul_assoc, ← mul_left_eq_iff_eq_invOf_mul, Int.commute_cast]
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Inv" ]
Mathlib/Tactic/NormNum/Eq.lean
Rat.invOf_denom_swap
null
isRat_eq_false [Ring α] [CharZero α] : {a b : α} → {na nb : ℤ} → {da db : ℕ} → IsRat a na da → IsRat b nb db → decide (Int.mul na (.ofNat db) = Int.mul nb (.ofNat da)) = false → ¬a = b | _, _, _, _, _, _, ⟨_, rfl⟩, ⟨_, rfl⟩, h => by rw [Rat.invOf_denom_swap]; exact mod_cast of_decide_eq_false h attribute [local instance] monadLiftOptionMetaM in
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Inv" ]
Mathlib/Tactic/NormNum/Eq.lean
isRat_eq_false
null
@[norm_num _ = _] evalEq : NormNumExt where eval {v β} e := do haveI' : v =QL 0 := ⟨⟩; haveI' : $β =Q Prop := ⟨⟩ let .app (.app f a) b ← whnfR e | failure let ⟨u, α, a⟩ ← inferTypeQ' a have b : Q($α) := b haveI' : $e =Q ($a = $b) := ⟨⟩ guard <|← withNewMCtxDepth <| isDefEq f q(Eq (α := $α)) let ra ← derive a; let rb ← derive b let rec intArm (rα : Q(Ring $α)) := do let ⟨za, na, pa⟩ ← ra.toInt rα; let ⟨zb, nb, pb⟩ ← rb.toInt rα if za = zb then haveI' : $na =Q $nb := ⟨⟩ return .isTrue q(isInt_eq_true $pa $pb) else if let some _i ← inferCharZeroOfRing? rα then let r : Q(decide ($na = $nb) = false) := (q(Eq.refl false) : Expr) return .isFalse q(isInt_eq_false $pa $pb $r) else failure --TODO: nonzero characteristic ≠ let rec nnratArm (dsα : Q(DivisionSemiring $α)) := do let ⟨qa, na, da, pa⟩ ← ra.toNNRat' dsα; let ⟨qb, nb, db, pb⟩ ← rb.toNNRat' dsα if qa = qb then haveI' : $na =Q $nb := ⟨⟩ haveI' : $da =Q $db := ⟨⟩ return .isTrue q(isNNRat_eq_true $pa $pb) else if let some _i ← inferCharZeroOfDivisionSemiring? dsα then let r : Q(decide (Nat.mul $na $db = Nat.mul $nb $da) = false) := (q(Eq.refl false) : Expr) return .isFalse q(isNNRat_eq_false $pa $pb $r) else failure --TODO: nonzero characteristic ≠ let rec ratArm (dα : Q(DivisionRing $α)) := do let ⟨qa, na, da, pa⟩ ← ra.toRat' dα; let ⟨qb, nb, db, pb⟩ ← rb.toRat' dα if qa = qb then haveI' : $na =Q $nb := ⟨⟩ haveI' : $da =Q $db := ⟨⟩ return .isTrue q(isRat_eq_true $pa $pb) else if let some _i ← inferCharZeroOfDivisionRing? dα then let r : Q(decide (Int.mul $na (.ofNat $db) = Int.mul $nb (.ofNat $da)) = false) := (q(Eq.refl false) : Expr) return .isFalse q(isRat_eq_false $pa $pb $r) else failure --TODO: nonzero characteristic ≠ match ra, rb with | .isBool b₁ p₁, .isBool b₂ p₂ => have a : Q(Prop) := a; have b : Q(Prop) := b match b₁, p₁, b₂, p₂ with | true, (p₁ : Q($a)), true, (p₂ : Q($b)) => return .isTrue q(eq_of_true $p₁ $p₂) | false, (p₁ : Q(¬$a)), false, (p₂ : Q(¬$b)) => return .isTrue q(eq_of_false $p₁ $p₂) | false, (p₁ : Q(¬$a)), true, (p₂ : Q($b)) => ...
def
Tactic
[ "Mathlib.Tactic.NormNum.Inv" ]
Mathlib/Tactic/NormNum/Eq.lean
evalEq
The `norm_num` extension which identifies expressions of the form `a = b`, such that `norm_num` successfully recognises both `a` and `b`.
int_gcd_helper' {d : ℕ} {x y : ℤ} (a b : ℤ) (h₁ : (d : ℤ) ∣ x) (h₂ : (d : ℤ) ∣ y) (h₃ : x * a + y * b = d) : Int.gcd x y = d := by refine Nat.dvd_antisymm ?_ (Int.natCast_dvd_natCast.1 (Int.dvd_coe_gcd h₁ h₂)) rw [← Int.natCast_dvd_natCast, ← h₃] apply dvd_add · exact (Int.gcd_dvd_left ..).mul_right _ · exact (Int.gcd_dvd_right ..).mul_right _
theorem
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
int_gcd_helper'
null
nat_gcd_helper_dvd_left (x y : ℕ) (h : y % x = 0) : Nat.gcd x y = x := Nat.gcd_eq_left (Nat.dvd_of_mod_eq_zero h)
theorem
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
nat_gcd_helper_dvd_left
null
nat_gcd_helper_dvd_right (x y : ℕ) (h : x % y = 0) : Nat.gcd x y = y := Nat.gcd_eq_right (Nat.dvd_of_mod_eq_zero h)
theorem
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
nat_gcd_helper_dvd_right
null
nat_gcd_helper_2 (d x y a b : ℕ) (hu : x % d = 0) (hv : y % d = 0) (h : x * a = y * b + d) : Nat.gcd x y = d := by rw [← Int.gcd_natCast_natCast] apply int_gcd_helper' a (-b) (Int.natCast_dvd_natCast.mpr (Nat.dvd_of_mod_eq_zero hu)) (Int.natCast_dvd_natCast.mpr (Nat.dvd_of_mod_eq_zero hv)) rw [mul_neg, ← sub_eq_add_neg, sub_eq_iff_eq_add'] exact mod_cast h
theorem
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
nat_gcd_helper_2
null
nat_gcd_helper_1 (d x y a b : ℕ) (hu : x % d = 0) (hv : y % d = 0) (h : y * b = x * a + d) : Nat.gcd x y = d := (Nat.gcd_comm _ _).trans <| nat_gcd_helper_2 _ _ _ _ _ hv hu h
theorem
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
nat_gcd_helper_1
null
nat_gcd_helper_1' (x y a b : ℕ) (h : y * b = x * a + 1) : Nat.gcd x y = 1 := nat_gcd_helper_1 1 _ _ _ _ (Nat.mod_one _) (Nat.mod_one _) h
theorem
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
nat_gcd_helper_1'
null
nat_gcd_helper_2' (x y a b : ℕ) (h : x * a = y * b + 1) : Nat.gcd x y = 1 := nat_gcd_helper_2 1 _ _ _ _ (Nat.mod_one _) (Nat.mod_one _) h
theorem
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
nat_gcd_helper_2'
null
nat_lcm_helper (x y d m : ℕ) (hd : Nat.gcd x y = d) (d0 : Nat.beq d 0 = false) (dm : x * y = d * m) : Nat.lcm x y = m := mul_right_injective₀ (Nat.ne_of_beq_eq_false d0) <| by dsimp only rw [← dm, ← hd, Nat.gcd_mul_lcm]
theorem
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
nat_lcm_helper
null
int_gcd_helper {x y : ℤ} {x' y' d : ℕ} (hx : x.natAbs = x') (hy : y.natAbs = y') (h : Nat.gcd x' y' = d) : Int.gcd x y = d := by subst_vars; rw [Int.gcd_def]
theorem
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
int_gcd_helper
null
int_lcm_helper {x y : ℤ} {x' y' d : ℕ} (hx : x.natAbs = x') (hy : y.natAbs = y') (h : Nat.lcm x' y' = d) : Int.lcm x y = d := by subst_vars; rw [Int.lcm_def] open Qq Lean Elab.Tactic Mathlib.Meta.NormNum
theorem
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
int_lcm_helper
null
isNat_gcd : {x y nx ny z : ℕ} → IsNat x nx → IsNat y ny → Nat.gcd nx ny = z → IsNat (Nat.gcd x y) z | _, _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨rfl⟩
theorem
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
isNat_gcd
null
isNat_lcm : {x y nx ny z : ℕ} → IsNat x nx → IsNat y ny → Nat.lcm nx ny = z → IsNat (Nat.lcm x y) z | _, _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨rfl⟩
theorem
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
isNat_lcm
null
isInt_gcd : {x y nx ny : ℤ} → {z : ℕ} → IsInt x nx → IsInt y ny → Int.gcd nx ny = z → IsNat (Int.gcd x y) z | _, _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨rfl⟩
theorem
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
isInt_gcd
null
isInt_lcm : {x y nx ny : ℤ} → {z : ℕ} → IsInt x nx → IsInt y ny → Int.lcm nx ny = z → IsNat (Int.lcm x y) z | _, _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨rfl⟩
theorem
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
isInt_lcm
null
proveNatGCD (ex ey : Q(ℕ)) : (ed : Q(ℕ)) × Q(Nat.gcd $ex $ey = $ed) := match ex.natLit!, ey.natLit! with | 0, _ => have : $ex =Q nat_lit 0 := ⟨⟩; ⟨ey, q(Nat.gcd_zero_left $ey)⟩ | _, 0 => have : $ey =Q nat_lit 0 := ⟨⟩; ⟨ex, q(Nat.gcd_zero_right $ex)⟩ | 1, _ => have : $ex =Q nat_lit 1 := ⟨⟩; ⟨q(nat_lit 1), q(Nat.gcd_one_left $ey)⟩ | _, 1 => have : $ey =Q nat_lit 1 := ⟨⟩; ⟨q(nat_lit 1), q(Nat.gcd_one_right $ex)⟩ | x, y => let (d, a, b) := Nat.xgcdAux x 1 0 y 0 1 if d = x then have pq : Q(Nat.mod $ey $ex = 0) := (q(Eq.refl (nat_lit 0)) : Expr) ⟨ex, q(nat_gcd_helper_dvd_left $ex $ey $pq)⟩ else if d = y then have pq : Q(Nat.mod $ex $ey = 0) := (q(Eq.refl (nat_lit 0)) : Expr) ⟨ey, q(nat_gcd_helper_dvd_right $ex $ey $pq)⟩ else have ea' : Q(ℕ) := mkRawNatLit a.natAbs have eb' : Q(ℕ) := mkRawNatLit b.natAbs if d = 1 then if a ≥ 0 then have pt : Q($ex * $ea' = $ey * $eb' + 1) := (q(Eq.refl ($ex * $ea')) : Expr) ⟨q(nat_lit 1), q(nat_gcd_helper_2' $ex $ey $ea' $eb' $pt)⟩ else have pt : Q($ey * $eb' = $ex * $ea' + 1) := (q(Eq.refl ($ey * $eb')) : Expr) ⟨q(nat_lit 1), q(nat_gcd_helper_1' $ex $ey $ea' $eb' $pt)⟩ else have ed : Q(ℕ) := mkRawNatLit d have pu : Q(Nat.mod $ex $ed = 0) := (q(Eq.refl (nat_lit 0)) : Expr) have pv : Q(Nat.mod $ey $ed = 0) := (q(Eq.refl (nat_lit 0)) : Expr) if a ≥ 0 then have pt : Q($ex * $ea' = $ey * $eb' + $ed) := (q(Eq.refl ($ex * $ea')) : Expr) ⟨ed, q(nat_gcd_helper_2 $ed $ex $ey $ea' $eb' $pu $pv $pt)⟩ else have pt : Q($ey * $eb' = $ex * $ea' + $ed) := (q(Eq.refl ($ey * $eb')) : Expr) ⟨ed, q(nat_gcd_helper_1 $ed $ex $ey $ea' $eb' $pu $pv $pt)⟩
def
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
proveNatGCD
Given natural number literals `ex` and `ey`, return their GCD as a natural number literal and an equality proof. Panics if `ex` or `ey` aren't natural number literals.
@[norm_num Nat.gcd _ _] evalNatGCD : NormNumExt where eval {u α} e := do let .app (.app _ (x : Q(ℕ))) (y : Q(ℕ)) ← Meta.whnfR e | failure haveI' : u =QL 0 := ⟨⟩; haveI' : $α =Q ℕ := ⟨⟩ haveI' : $e =Q Nat.gcd $x $y := ⟨⟩ let sℕ : Q(AddMonoidWithOne ℕ) := q(instAddMonoidWithOneNat) let ⟨ex, p⟩ ← deriveNat x sℕ let ⟨ey, q⟩ ← deriveNat y sℕ let ⟨ed, pf⟩ := proveNatGCD ex ey return .isNat sℕ ed q(isNat_gcd $p $q $pf)
def
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
evalNatGCD
Evaluate the `Nat.gcd` function.
proveNatLCM (ex ey : Q(ℕ)) : (ed : Q(ℕ)) × Q(Nat.lcm $ex $ey = $ed) := match ex.natLit!, ey.natLit! with | 0, _ => show (ed : Q(ℕ)) × Q(Nat.lcm 0 $ey = $ed) from ⟨q(nat_lit 0), q(Nat.lcm_zero_left $ey)⟩ | _, 0 => show (ed : Q(ℕ)) × Q(Nat.lcm $ex 0 = $ed) from ⟨q(nat_lit 0), q(Nat.lcm_zero_right $ex)⟩ | 1, _ => show (ed : Q(ℕ)) × Q(Nat.lcm 1 $ey = $ed) from ⟨ey, q(Nat.lcm_one_left $ey)⟩ | _, 1 => show (ed : Q(ℕ)) × Q(Nat.lcm $ex 1 = $ed) from ⟨ex, q(Nat.lcm_one_right $ex)⟩ | x, y => let ⟨ed, pd⟩ := proveNatGCD ex ey have p0 : Q(Nat.beq $ed 0 = false) := (q(Eq.refl false) : Expr) have em : Q(ℕ) := mkRawNatLit (x * y / ed.natLit!) have pm : Q($ex * $ey = $ed * $em) := (q(Eq.refl ($ex * $ey)) : Expr) ⟨em, q(nat_lcm_helper $ex $ey $ed $em $pd $p0 $pm)⟩
def
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
proveNatLCM
Given natural number literals `ex` and `ey`, return their LCM as a natural number literal and an equality proof. Panics if `ex` or `ey` aren't natural number literals.
@[norm_num Nat.lcm _ _] evalNatLCM : NormNumExt where eval {u α} e := do let .app (.app _ (x : Q(ℕ))) (y : Q(ℕ)) ← Meta.whnfR e | failure haveI' : u =QL 0 := ⟨⟩; haveI' : $α =Q ℕ := ⟨⟩ haveI' : $e =Q Nat.lcm $x $y := ⟨⟩ let sℕ : Q(AddMonoidWithOne ℕ) := q(instAddMonoidWithOneNat) let ⟨ex, p⟩ ← deriveNat x sℕ let ⟨ey, q⟩ ← deriveNat y sℕ let ⟨ed, pf⟩ := proveNatLCM ex ey return .isNat sℕ ed q(isNat_lcm $p $q $pf)
def
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
evalNatLCM
Evaluates the `Nat.lcm` function.
proveIntGCD (ex ey : Q(ℤ)) : (ed : Q(ℕ)) × Q(Int.gcd $ex $ey = $ed) := let ⟨ex', hx⟩ := rawIntLitNatAbs ex let ⟨ey', hy⟩ := rawIntLitNatAbs ey let ⟨ed, pf⟩ := proveNatGCD ex' ey' ⟨ed, q(int_gcd_helper $hx $hy $pf)⟩
def
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
proveIntGCD
Given two integers, return their GCD and an equality proof. Panics if `ex` or `ey` aren't integer literals.
@[norm_num Int.gcd _ _] evalIntGCD : NormNumExt where eval {u α} e := do let .app (.app _ (x : Q(ℤ))) (y : Q(ℤ)) ← Meta.whnfR e | failure let ⟨ex, p⟩ ← deriveInt x _ let ⟨ey, q⟩ ← deriveInt y _ haveI' : u =QL 0 := ⟨⟩; haveI' : $α =Q ℕ := ⟨⟩ haveI' : $e =Q Int.gcd $x $y := ⟨⟩ let ⟨ed, pf⟩ := proveIntGCD ex ey return .isNat _ ed q(isInt_gcd $p $q $pf)
def
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
evalIntGCD
Evaluates the `Int.gcd` function.
proveIntLCM (ex ey : Q(ℤ)) : (ed : Q(ℕ)) × Q(Int.lcm $ex $ey = $ed) := let ⟨ex', hx⟩ := rawIntLitNatAbs ex let ⟨ey', hy⟩ := rawIntLitNatAbs ey let ⟨ed, pf⟩ := proveNatLCM ex' ey' ⟨ed, q(int_lcm_helper $hx $hy $pf)⟩
def
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
proveIntLCM
Given two integers, return their LCM and an equality proof. Panics if `ex` or `ey` aren't integer literals.
@[norm_num Int.lcm _ _] evalIntLCM : NormNumExt where eval {u α} e := do let .app (.app _ (x : Q(ℤ))) (y : Q(ℤ)) ← Meta.whnfR e | failure let ⟨ex, p⟩ ← deriveInt x _ let ⟨ey, q⟩ ← deriveInt y _ haveI' : u =QL 0 := ⟨⟩; haveI' : $α =Q ℕ := ⟨⟩ haveI' : $e =Q Int.lcm $x $y := ⟨⟩ let ⟨ed, pf⟩ := proveIntLCM ex ey return .isNat _ ed q(isInt_lcm $p $q $pf)
def
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
evalIntLCM
Evaluates the `Int.lcm` function.
isInt_ratNum : ∀ {q : ℚ} {n : ℤ} {n' : ℕ} {d : ℕ}, IsRat q n d → n.natAbs = n' → n'.gcd d = 1 → IsInt q.num n | _, n, _, d, ⟨hi, rfl⟩, rfl, h => by constructor have : 0 < d := Nat.pos_iff_ne_zero.mpr <| by simpa using hi.ne_zero simp_rw [Rat.mul_num, Rat.den_intCast, invOf_eq_inv, Rat.inv_natCast_den_of_pos this, Rat.inv_natCast_num_of_pos this, Rat.num_intCast, one_mul, mul_one, h, Nat.cast_one, Int.ediv_one, Int.cast_id]
theorem
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
isInt_ratNum
null
isNat_ratDen : ∀ {q : ℚ} {n : ℤ} {n' : ℕ} {d : ℕ}, IsRat q n d → n.natAbs = n' → n'.gcd d = 1 → IsNat q.den d | _, n, _, d, ⟨hi, rfl⟩, rfl, h => by constructor have : 0 < d := Nat.pos_iff_ne_zero.mpr <| by simpa using hi.ne_zero simp_rw [Rat.mul_den, Rat.den_intCast, invOf_eq_inv, Rat.inv_natCast_den_of_pos this, Rat.inv_natCast_num_of_pos this, Rat.num_intCast, one_mul, mul_one, Nat.cast_id, h, Nat.div_one]
theorem
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
isNat_ratDen
null
@[nolint unusedHavesSuffices, norm_num Rat.num _] evalRatNum : NormNumExt where eval {u α} e := do let .proj _ _ (q : Q(ℚ)) ← Meta.whnfR e | failure have : u =QL 0 := ⟨⟩; have : $α =Q ℤ := ⟨⟩; have : $e =Q Rat.num $q := ⟨⟩ let ⟨q', n, d, eq⟩ ← deriveRat q (_inst := q(inferInstance)) let ⟨n', hn⟩ := rawIntLitNatAbs n let ⟨gcd, pf⟩ := proveNatGCD q($n') q($d) have : $gcd =Q nat_lit 1 := ⟨⟩ return .isInt _ n q'.num q(isInt_ratNum $eq $hn $pf)
def
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
evalRatNum
Evaluates the `Rat.num` function.
@[nolint unusedHavesSuffices, norm_num Rat.den _] evalRatDen : NormNumExt where eval {u α} e := do let .proj _ _ (q : Q(ℚ)) ← Meta.whnfR e | failure have : u =QL 0 := ⟨⟩; have : $α =Q ℕ := ⟨⟩; have : $e =Q Rat.den $q := ⟨⟩ let ⟨q', n, d, eq⟩ ← deriveRat q (_inst := q(inferInstance)) let ⟨n', hn⟩ := rawIntLitNatAbs n let ⟨gcd, pf⟩ := proveNatGCD q($n') q($d) have : $gcd =Q nat_lit 1 := ⟨⟩ return .isNat _ d q(isNat_ratDen $eq $hn $pf)
def
Tactic
[ "Mathlib.Algebra.Ring.Divisibility.Basic", "Mathlib.Data.Int.GCD", "Mathlib.Tactic.NormNum" ]
Mathlib/Tactic/NormNum/GCD.lean
evalRatDen
Evaluates the `Rat.den` function.
inferOrderedSemiring (α : Q(Type u)) : MetaM <| (_ : Q(Semiring $α)) × (_ : Q(PartialOrder $α)) × Q(IsOrderedRing $α) := let go := do let semiring ← synthInstanceQ q(Semiring $α) let partialOrder ← synthInstanceQ q(PartialOrder $α) let isOrderedRing ← synthInstanceQ q(IsOrderedRing $α) return ⟨semiring, partialOrder, isOrderedRing⟩ go <|> throwError "not an ordered semiring"
def
Tactic
[ "Mathlib.Tactic.NormNum.Eq", "Mathlib.Algebra.Order.Field.Defs", "Mathlib.Algebra.Order.Invertible", "Mathlib.Algebra.Order.Monoid.WithTop", "Mathlib.Algebra.Order.Ring.Cast" ]
Mathlib/Tactic/NormNum/Ineq.lean
inferOrderedSemiring
Helper function to synthesize typed `Semiring α` `PartialOrder α` `IsOrderedSemiring α` expressions.
inferOrderedRing (α : Q(Type u)) : MetaM <| (_ : Q(Ring $α)) × (_ : Q(PartialOrder $α)) × Q(IsOrderedRing $α) := let go := do let ring ← synthInstanceQ q(Ring $α) let partialOrder ← synthInstanceQ q(PartialOrder $α) let isOrderedRing ← synthInstanceQ q(IsOrderedRing $α) return ⟨ring, partialOrder, isOrderedRing⟩ go <|> throwError "not an ordered ring"
def
Tactic
[ "Mathlib.Tactic.NormNum.Eq", "Mathlib.Algebra.Order.Field.Defs", "Mathlib.Algebra.Order.Invertible", "Mathlib.Algebra.Order.Monoid.WithTop", "Mathlib.Algebra.Order.Ring.Cast" ]
Mathlib/Tactic/NormNum/Ineq.lean
inferOrderedRing
Helper function to synthesize typed `Ring α` `PartialOrder α` `IsOrderedSemiring α` expressions.
inferLinearOrderedSemifield (α : Q(Type u)) : MetaM <| (_ : Q(Semifield $α)) × (_ : Q(LinearOrder $α)) × Q(IsStrictOrderedRing $α) := let go := do let semifield ← synthInstanceQ q(Semifield $α) let linearOrder ← synthInstanceQ q(LinearOrder $α) let isStrictOrderedRing ← synthInstanceQ q(IsStrictOrderedRing $α) return ⟨semifield, linearOrder, isStrictOrderedRing⟩ go <|> throwError "not a linear ordered semifield"
def
Tactic
[ "Mathlib.Tactic.NormNum.Eq", "Mathlib.Algebra.Order.Field.Defs", "Mathlib.Algebra.Order.Invertible", "Mathlib.Algebra.Order.Monoid.WithTop", "Mathlib.Algebra.Order.Ring.Cast" ]
Mathlib/Tactic/NormNum/Ineq.lean
inferLinearOrderedSemifield
Helper function to synthesize typed `Semifield α` `LinearOrder α` `IsStrictOrderedRing α` expressions.
inferLinearOrderedField (α : Q(Type u)) : MetaM <| (_ : Q(Field $α)) × (_ : Q(LinearOrder $α)) × Q(IsStrictOrderedRing $α) := let go := do let field ← synthInstanceQ q(Field $α) let linearOrder ← synthInstanceQ q(LinearOrder $α) let isStrictOrderedRing ← synthInstanceQ q(IsStrictOrderedRing $α) return ⟨field, linearOrder, isStrictOrderedRing⟩ go <|> throwError "not a linear ordered field" variable {α : Type*}
def
Tactic
[ "Mathlib.Tactic.NormNum.Eq", "Mathlib.Algebra.Order.Field.Defs", "Mathlib.Algebra.Order.Invertible", "Mathlib.Algebra.Order.Monoid.WithTop", "Mathlib.Algebra.Order.Ring.Cast" ]
Mathlib/Tactic/NormNum/Ineq.lean
inferLinearOrderedField
Helper function to synthesize typed `Field α` `LinearOrder α` `IsStrictOrderedRing α` expressions.
isNat_le_true [Semiring α] [PartialOrder α] [IsOrderedRing α] : {a b : α} → {a' b' : ℕ} → IsNat a a' → IsNat b b' → Nat.ble a' b' = true → a ≤ b | _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, h => Nat.mono_cast (Nat.le_of_ble_eq_true h)
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Eq", "Mathlib.Algebra.Order.Field.Defs", "Mathlib.Algebra.Order.Invertible", "Mathlib.Algebra.Order.Monoid.WithTop", "Mathlib.Algebra.Order.Ring.Cast" ]
Mathlib/Tactic/NormNum/Ineq.lean
isNat_le_true
null
isNat_lt_false [Semiring α] [PartialOrder α] [IsOrderedRing α] {a b : α} {a' b' : ℕ} (ha : IsNat a a') (hb : IsNat b b') (h : Nat.ble b' a' = true) : ¬a < b := not_lt_of_ge (isNat_le_true hb ha h)
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Eq", "Mathlib.Algebra.Order.Field.Defs", "Mathlib.Algebra.Order.Invertible", "Mathlib.Algebra.Order.Monoid.WithTop", "Mathlib.Algebra.Order.Ring.Cast" ]
Mathlib/Tactic/NormNum/Ineq.lean
isNat_lt_false
null
isNNRat_le_true [Semiring α] [LinearOrder α] [IsStrictOrderedRing α] : {a b : α} → {na nb : ℕ} → {da db : ℕ} → IsNNRat a na da → IsNNRat b nb db → decide (Nat.mul na (db) ≤ Nat.mul nb (da)) → a ≤ b | _, _, _, _, da, db, ⟨_, rfl⟩, ⟨_, rfl⟩, h => by have h := (Nat.cast_le (α := α)).mpr <| of_decide_eq_true h have ha : 0 ≤ ⅟(da : α) := invOf_nonneg.mpr <| Nat.cast_nonneg da have hb : 0 ≤ ⅟(db : α) := invOf_nonneg.mpr <| Nat.cast_nonneg db have h := (mul_le_mul_of_nonneg_left · hb) <| mul_le_mul_of_nonneg_right h ha rw [← mul_assoc, Nat.commute_cast] at h simp only [Nat.mul_eq, Nat.cast_mul, mul_invOf_cancel_right'] at h rwa [Nat.commute_cast] at h
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Eq", "Mathlib.Algebra.Order.Field.Defs", "Mathlib.Algebra.Order.Invertible", "Mathlib.Algebra.Order.Monoid.WithTop", "Mathlib.Algebra.Order.Ring.Cast" ]
Mathlib/Tactic/NormNum/Ineq.lean
isNNRat_le_true
null
isNNRat_lt_true [Semiring α] [LinearOrder α] [IsStrictOrderedRing α] [Nontrivial α] : {a b : α} → {na nb : ℕ} → {da db : ℕ} → IsNNRat a na da → IsNNRat b nb db → decide (na * db < nb * da) → a < b | _, _, _, _, da, db, ⟨_, rfl⟩, ⟨_, rfl⟩, h => by have h := (Nat.cast_lt (α := α)).mpr <| of_decide_eq_true h have ha : 0 < ⅟(da : α) := pos_invOf_of_invertible_cast da have hb : 0 < ⅟(db : α) := pos_invOf_of_invertible_cast db have h := (mul_lt_mul_of_pos_left · hb) <| mul_lt_mul_of_pos_right h ha rw [← mul_assoc, Nat.commute_cast] at h simp? at h says simp only [Nat.cast_mul, mul_invOf_cancel_right'] at h rwa [Nat.commute_cast] at h
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Eq", "Mathlib.Algebra.Order.Field.Defs", "Mathlib.Algebra.Order.Invertible", "Mathlib.Algebra.Order.Monoid.WithTop", "Mathlib.Algebra.Order.Ring.Cast" ]
Mathlib/Tactic/NormNum/Ineq.lean
isNNRat_lt_true
null
isNNRat_le_false [Semiring α] [LinearOrder α] [IsStrictOrderedRing α] [Nontrivial α] {a b : α} {na nb : ℕ} {da db : ℕ} (ha : IsNNRat a na da) (hb : IsNNRat b nb db) (h : decide (nb * da < na * db)) : ¬a ≤ b := not_le_of_gt (isNNRat_lt_true hb ha h)
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Eq", "Mathlib.Algebra.Order.Field.Defs", "Mathlib.Algebra.Order.Invertible", "Mathlib.Algebra.Order.Monoid.WithTop", "Mathlib.Algebra.Order.Ring.Cast" ]
Mathlib/Tactic/NormNum/Ineq.lean
isNNRat_le_false
null
isNNRat_lt_false [Semiring α] [LinearOrder α] [IsStrictOrderedRing α] {a b : α} {na nb : ℕ} {da db : ℕ} (ha : IsNNRat a na da) (hb : IsNNRat b nb db) (h : decide (nb * da ≤ na * db)) : ¬a < b := not_lt_of_ge (isNNRat_le_true hb ha h)
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Eq", "Mathlib.Algebra.Order.Field.Defs", "Mathlib.Algebra.Order.Invertible", "Mathlib.Algebra.Order.Monoid.WithTop", "Mathlib.Algebra.Order.Ring.Cast" ]
Mathlib/Tactic/NormNum/Ineq.lean
isNNRat_lt_false
null
isRat_le_true [Ring α] [LinearOrder α] [IsStrictOrderedRing α] : {a b : α} → {na nb : ℤ} → {da db : ℕ} → IsRat a na da → IsRat b nb db → decide (Int.mul na (.ofNat db) ≤ Int.mul nb (.ofNat da)) → a ≤ b | _, _, _, _, da, db, ⟨_, rfl⟩, ⟨_, rfl⟩, h => by have h := Int.cast_mono (R := α) <| of_decide_eq_true h have ha : 0 ≤ ⅟(da : α) := invOf_nonneg.mpr <| Nat.cast_nonneg da have hb : 0 ≤ ⅟(db : α) := invOf_nonneg.mpr <| Nat.cast_nonneg db have h := (mul_le_mul_of_nonneg_left · hb) <| mul_le_mul_of_nonneg_right h ha rw [← mul_assoc, Int.commute_cast] at h simp only [Int.ofNat_eq_coe, Int.mul_def, Int.cast_mul, Int.cast_natCast, mul_invOf_cancel_right'] at h rwa [Int.commute_cast] at h
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Eq", "Mathlib.Algebra.Order.Field.Defs", "Mathlib.Algebra.Order.Invertible", "Mathlib.Algebra.Order.Monoid.WithTop", "Mathlib.Algebra.Order.Ring.Cast" ]
Mathlib/Tactic/NormNum/Ineq.lean
isRat_le_true
null
isRat_lt_true [Ring α] [LinearOrder α] [IsStrictOrderedRing α] [Nontrivial α] : {a b : α} → {na nb : ℤ} → {da db : ℕ} → IsRat a na da → IsRat b nb db → decide (na * db < nb * da) → a < b | _, _, _, _, da, db, ⟨_, rfl⟩, ⟨_, rfl⟩, h => by have h := Int.cast_strictMono (R := α) <| of_decide_eq_true h have ha : 0 < ⅟(da : α) := pos_invOf_of_invertible_cast da have hb : 0 < ⅟(db : α) := pos_invOf_of_invertible_cast db have h := (mul_lt_mul_of_pos_left · hb) <| mul_lt_mul_of_pos_right h ha rw [← mul_assoc, Int.commute_cast] at h simp? at h says simp only [Int.cast_mul, Int.cast_natCast, mul_invOf_cancel_right'] at h rwa [Int.commute_cast] at h
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Eq", "Mathlib.Algebra.Order.Field.Defs", "Mathlib.Algebra.Order.Invertible", "Mathlib.Algebra.Order.Monoid.WithTop", "Mathlib.Algebra.Order.Ring.Cast" ]
Mathlib/Tactic/NormNum/Ineq.lean
isRat_lt_true
null
isRat_le_false [Ring α] [LinearOrder α] [IsStrictOrderedRing α] [Nontrivial α] {a b : α} {na nb : ℤ} {da db : ℕ} (ha : IsRat a na da) (hb : IsRat b nb db) (h : decide (nb * da < na * db)) : ¬a ≤ b := not_le_of_gt (isRat_lt_true hb ha h)
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Eq", "Mathlib.Algebra.Order.Field.Defs", "Mathlib.Algebra.Order.Invertible", "Mathlib.Algebra.Order.Monoid.WithTop", "Mathlib.Algebra.Order.Ring.Cast" ]
Mathlib/Tactic/NormNum/Ineq.lean
isRat_le_false
null
isRat_lt_false [Ring α] [LinearOrder α] [IsStrictOrderedRing α] {a b : α} {na nb : ℤ} {da db : ℕ} (ha : IsRat a na da) (hb : IsRat b nb db) (h : decide (nb * da ≤ na * db)) : ¬a < b := not_lt_of_ge (isRat_le_true hb ha h) /-! # (In)equalities -/
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Eq", "Mathlib.Algebra.Order.Field.Defs", "Mathlib.Algebra.Order.Invertible", "Mathlib.Algebra.Order.Monoid.WithTop", "Mathlib.Algebra.Order.Ring.Cast" ]
Mathlib/Tactic/NormNum/Ineq.lean
isRat_lt_false
null