The areas / V34. The tactics of Mathlib
Mathlib.Tactic.Algebra.Basic
39 declarations. Mathlib docs · Source
The algebra tactic
A suite of three tactics for solving equations in commutative algebras over commutative (semi)rings, where the exponents can also contain variables.
Based largely on the implementation of ring. The algebra normal form mirrors that of ring
except that the constants are expressions in the base ring that are kept in ring normal form.
Organization
This tactic is implemented using the machinery of Ring.Common
- Normalized expressions are stored as an
Common.ExSum, with a custom type for representing coefficients inR. - While
ringstores coefficients as rational numbers normalized bynorm_num,algebrastores coefficients as expressions in the base ringR, normalized byring. - These coefficients are sums, not products. The normal form of
a • x + b • xis(a + b) • x.
This tactic is used internally to implement the polynomial tactic.
Limitations
The main limitation of the current implementation is that it does not handle rational constants
when the algebra A is a field but the base ring R is not. This is never an issue when working
with polynomials, but would be an issue when working with a number field over its ring of integers.
When inferring the base ring, we assume that any two rings R and S that appear are comparable,
in the sense that either R is an S-algebra or S is an R-algebra.
def Mathlib.Tactic.Algebra.Cache.ctorIdx E1 #
{u : Lean.Level} → {A : Q(Type u)} → {sA : Q(CommSemiring «$A»)} → Mathlib.Tactic.Algebra.Cache sA → ℕ
theorem Mathlib.Tactic.Algebra.Cache.mk.inj E1 #
∀ {u : Lean.Level} {A : Q(Type u)} {sA : Q(CommSemiring «$A»)} {toCache : Mathlib.Tactic.Ring.Common.Cache sA}
{field : Option Q(Field «$A»)} {toCache_1 : Mathlib.Tactic.Ring.Common.Cache sA} {field_1 : Option Q(Field «$A»)},
{ toCache := toCache, field := field } = { toCache := toCache_1, field := field_1 } →
toCache = toCache_1 ∧ field = field_1
def Mathlib.Tactic.Algebra.BaseType.ctorIdx E1 #
{u v : Lean.Level} →
{R : Q(Type u)} →
{A : Q(Type v)} →
{sR : Q(CommSemiring «$R»)} →
{sA : Q(CommSemiring «$A»)} →
{sAlg : Q(Algebra «$R» «$A»)} → {a : Q(«$A»)} → Mathlib.Tactic.Algebra.BaseType sAlg a → ℕ
theorem Mathlib.Tactic.Algebra.BaseType.mk.inj E1 #
∀ {u v : Lean.Level} {R : Q(Type u)} {A : Q(Type v)} {sR : Q(CommSemiring «$R»)} {sA : Q(CommSemiring «$A»)}
{sAlg : Q(Algebra «$R» «$A»)} {r : Q(«$R»)} {x x_1 : Mathlib.Tactic.Ring.ExSum q(«$sR») r},
Mathlib.Tactic.Algebra.BaseType.mk r x = Mathlib.Tactic.Algebra.BaseType.mk r x_1 → x = x_1
theorem Mathlib.Tactic.Algebra.BaseType.mk.injEq E0 #
∀ {u v : Lean.Level} {R : Q(Type u)} {A : Q(Type v)} {sR : Q(CommSemiring «$R»)} {sA : Q(CommSemiring «$A»)}
{sAlg : Q(Algebra «$R» «$A»)} (r : Q(«$R»)) (x x_1 : Mathlib.Tactic.Ring.ExSum q(«$sR») r),
(Mathlib.Tactic.Algebra.BaseType.mk r x = Mathlib.Tactic.Algebra.BaseType.mk r x_1) = (x = x_1)
theorem Mathlib.Tactic.Algebra.Cache.mk.injEq E0 #
∀ {u : Lean.Level} {A : Q(Type u)} {sA : Q(CommSemiring «$A»)} (toCache : Mathlib.Tactic.Ring.Common.Cache sA)
(field : Option Q(Field «$A»)) (toCache_1 : Mathlib.Tactic.Ring.Common.Cache sA) (field_1 : Option Q(Field «$A»)),
({ toCache := toCache, field := field } = { toCache := toCache_1, field := field_1 }) =
(toCache = toCache_1 ∧ field = field_1)
structure Mathlib.Tactic.Algebra.Cache E1 #
{u : Lean.Level} → {A : Q(Type u)} → Q(CommSemiring «$A») → Type
This cache contains typeclasses required during algebra's execution. These assumptions
are stronger than ring because algebra occasionally requires commutativity to move between
the base ring and the algebra.
abbrev Mathlib.Tactic.Algebra.Cache.toCache E1 #
{u : Lean.Level} →
{A : Q(Type u)} → {sA : Q(CommSemiring «$A»)} → Mathlib.Tactic.Algebra.Cache sA → Mathlib.Tactic.Ring.Common.Cache sA
ctor Mathlib.Tactic.Algebra.Cache.mk E0 #
{u : Lean.Level} →
{A : Q(Type u)} →
{sA : Q(CommSemiring «$A»)} →
Mathlib.Tactic.Ring.Common.Cache sA → Option Q(Field «$A») → Mathlib.Tactic.Algebra.Cache sA
abbrev Mathlib.Tactic.Algebra.Cache.field E1 #
{u : Lean.Level} →
{A : Q(Type u)} → {sA : Q(CommSemiring «$A»)} → Mathlib.Tactic.Algebra.Cache sA → Option Q(Field «$A»)
A Field instance on A, if available.
def Mathlib.Tactic.Algebra.mkCache E1 #
{u : Lean.Level} → {A : Q(Type u)} → (sA : Q(CommSemiring «$A»)) → Lean.MetaM (Mathlib.Tactic.Algebra.Cache sA)
Create a new cache for A by doing the necessary instance searches.
inductive Mathlib.Tactic.Algebra.BaseType E1 #
{u v : Lean.Level} →
{R : Q(Type u)} →
{A : Q(Type v)} → {sR : Q(CommSemiring «$R»)} → {sA : Q(CommSemiring «$A»)} → Q(Algebra «$R» «$A») → Q(«$A») → Type
The type used to store the coefficients of the algebra tactic, which are expressions in R
kept in ring normal form and mapped into A by the algebraMap.
Note that these are sums, not products!
ctor Mathlib.Tactic.Algebra.BaseType.mk E0 #
{u v : Lean.Level} →
{R : Q(Type u)} →
{A : Q(Type v)} →
{sR : Q(CommSemiring «$R»)} →
{sA : Q(CommSemiring «$A»)} →
{sAlg : Q(Algebra «$R» «$A»)} →
(r : Q(«$R»)) →
Mathlib.Tactic.Ring.ExSum q(«$sR») r → Mathlib.Tactic.Algebra.BaseType sAlg q((algebraMap «$R» «$A») «$r»)
def Mathlib.Tactic.Algebra.ExBase E1 #
{u v : Lean.Level} →
{R : Q(Type u)} →
{A : Q(Type v)} → {sR : Q(CommSemiring «$R»)} → {sA : Q(CommSemiring «$A»)} → Q(Algebra «$R» «$A») → Q(«$A») → Type
ExBase BaseType sα e stores the structure of a normalized expression e, which appears
as the base of an exponent expression e^n. The sum constructor is only used when the exponent
n is not a constant.
def Mathlib.Tactic.Algebra.ExProd E1 #
{u v : Lean.Level} →
{R : Q(Type u)} →
{A : Q(Type v)} → {sR : Q(CommSemiring «$R»)} → {sA : Q(CommSemiring «$A»)} → Q(Algebra «$R» «$A») → Q(«$A») → Type
ExProd BaseType sα e stores the structure of a normalized monomial expression e.
A monomial here is a product of powers of ExBase expressions, terminated by a (nonzero) constant
coefficient. The data of the constant coefficient is stored in the BaseType.
def Mathlib.Tactic.Algebra.ExSum E1 #
{u v : Lean.Level} →
{R : Q(Type u)} →
{A : Q(Type v)} → {sR : Q(CommSemiring «$R»)} → {sA : Q(CommSemiring «$A»)} → Q(Algebra «$R» «$A») → Q(«$A») → Type
ExSum BaseType sα e stores the structure of a normalized polynomial expression e, which is
a sum of monomials.
def Mathlib.Tactic.Algebra.evalCast E1 #
{u v : Lean.Level} →
{R : Q(Type u)} →
{A : Q(Type v)} →
{sR : Q(CommSemiring «$R»)} →
{sA : Q(CommSemiring «$A»)} →
(sAlg : Q(Algebra «$R» «$A»)) →
{a : Q(«$A»)} →
Mathlib.Tactic.Algebra.Cache q(«$sR») →
Mathlib.Tactic.Algebra.Cache q(«$sA») →
Mathlib.Meta.NormNum.Result a →
Option (Mathlib.Tactic.Ring.Common.Result (Mathlib.Tactic.Algebra.ExSum sAlg) q(«$a»))
Evaluates a numeric literal in the algebra A by lifting it through the base ring R.
def Mathlib.Tactic.Algebra.pushCast E1 #
Lean.Expr → Lean.MetaM Lean.Meta.Simp.Result
Push algebraMaps into sums and products and convert algebraMaps from ℕ, ℤ and ℚ
into casts.
def Mathlib.Tactic.Algebra.evalSMulCast E1 #
{u u' v : Lean.Level} →
{R : Q(Type u)} →
{R' : Q(Type u')} →
{A : Q(Type v)} →
{sR : Q(CommSemiring «$R»)} →
{sA : Q(CommSemiring «$A»)} →
(sAlg : Q(Algebra «$R» «$A»)) →
(smul : Q(SMul «$R'» «$A»)) →
(r' : Q(«$R'»)) → Lean.MetaM ((r : Q(«$R»)) × Q(∀ (a : «$A»), «$r» • a = «$r'» • a))
Handle scalar multiplication when the scalar ring R' doesn't match the base ring R.
Assumes R is an R'-algebra (i.e., R' is smaller), and casts the scalar using algebraMap.
def Mathlib.Tactic.Algebra.RingCompute.add E1 #
{u v : Lean.Level} →
{R : Q(Type u)} →
{A : Q(Type v)} →
{sR : Q(CommSemiring «$R»)} →
{sA : Q(CommSemiring «$A»)} →
(sAlg : Q(Algebra «$R» «$A»)) →
Mathlib.Tactic.Ring.Common.Cache sR →
{a b : Q(«$A»)} →
Mathlib.Tactic.Algebra.BaseType sAlg a →
Mathlib.Tactic.Algebra.BaseType sAlg b →
Lean.MetaM
(Mathlib.Tactic.Ring.Common.Result (Mathlib.Tactic.Algebra.BaseType sAlg) q(«$a» + «$b») ×
Option Q(Mathlib.Meta.NormNum.IsNat («$a» + «$b») 0))
Evaluate the sum of two normalized expressions in R using ring.
def Mathlib.Tactic.Algebra.RingCompute.mul E1 #
{u v : Lean.Level} →
{R : Q(Type u)} →
{A : Q(Type v)} →
{sR : Q(CommSemiring «$R»)} →
{sA : Q(CommSemiring «$A»)} →
(sAlg : Q(Algebra «$R» «$A»)) →
Mathlib.Tactic.Ring.Common.Cache sR →
{a b : Q(«$A»)} →
Mathlib.Tactic.Algebra.BaseType sAlg a →
Mathlib.Tactic.Algebra.BaseType sAlg b →
Lean.MetaM (Mathlib.Tactic.Ring.Common.Result (Mathlib.Tactic.Algebra.BaseType sAlg) q(«$a» * «$b»))
Evaluate the product of two normalized expressions in R using ring.
def Mathlib.Tactic.Algebra.RingCompute.cast E1 #
{u v : Lean.Level} →
{R : Q(Type u)} →
{A : Q(Type v)} →
{sR : Q(CommSemiring «$R»)} →
{sA : Q(CommSemiring «$A»)} →
(sAlg : Q(Algebra «$R» «$A»)) →
Mathlib.Tactic.Algebra.Cache sR →
(u' : Lean.Level) →
(R' : Q(Type u')) →
Q(CommSemiring «$R'») →
(_smul : Q(SMul «$R'» «$A»)) →
(r' : Q(«$R'»)) →
Mathlib.Tactic.AtomM
((y : Q(«$A»)) ×
Mathlib.Tactic.Ring.Common.ExSum (Mathlib.Tactic.Algebra.BaseType sAlg) sA q(«$y») ×
Q(∀ (a : «$A»), «$r'» • a = «$y» * a))
Take an expression r' in a ring R' such that R is an R'-algebra and cast r' to R
using algebraMap R' R, so that the scalar multiplication action on A is preserved.
def Mathlib.Tactic.Algebra.RingCompute.neg E1 #
{u v : Lean.Level} →
{R : Q(Type u)} →
{A : Q(Type v)} →
{sR : Q(CommSemiring «$R»)} →
{sA : Q(CommSemiring «$A»)} →
(sAlg : Q(Algebra «$R» «$A»)) →
Mathlib.Tactic.Algebra.Cache sR →
{a : Q(«$A»)} →
(_rA : Q(CommRing «$A»)) →
Mathlib.Tactic.Algebra.BaseType sAlg a →
Lean.MetaM (Mathlib.Tactic.Ring.Common.Result (Mathlib.Tactic.Algebra.BaseType sAlg) q(-«$a»))
Evaluate the product of two normalized expressions in R using ring.
def Mathlib.Tactic.Algebra.RingCompute.pow E1 #
{u v : Lean.Level} →
{R : Q(Type u)} →
{A : Q(Type v)} →
{sR : Q(CommSemiring «$R»)} →
{sA : Q(CommSemiring «$A»)} →
(sAlg : Q(Algebra «$R» «$A»)) →
Mathlib.Tactic.Ring.Common.Cache sR →
{a : Q(«$A»)} →
{b : Q(ℕ)} →
Mathlib.Tactic.Algebra.BaseType sAlg a →
Mathlib.Tactic.Ring.Common.ExProdNat q(«$b») →
OptionT Lean.MetaM
(Mathlib.Tactic.Ring.Common.Result (Mathlib.Tactic.Algebra.BaseType sAlg) q(«$a» ^ «$b»))
Raise a normalized expression in R to the power of a normalized natural number expression
using ring.
def Mathlib.Tactic.Algebra.RingCompute.inv E1 #
{u v : Lean.Level} →
{R : Q(Type u)} →
{A : Q(Type v)} →
{sR : Q(CommSemiring «$R»)} →
{sA : Q(CommSemiring «$A»)} →
(sAlg : Q(Algebra «$R» «$A»)) →
Mathlib.Tactic.Algebra.Cache sR →
{a : Q(«$A»)} →
Option Q(CharZero «$A») →
(fA : Q(Semifield «$A»)) →
Mathlib.Tactic.Algebra.BaseType sAlg a →
Mathlib.Tactic.AtomM
(Option (Mathlib.Tactic.Ring.Common.Result (Mathlib.Tactic.Algebra.BaseType sAlg) q(«$a»⁻¹)))
Evaluate the inverse of two normalized expressions in R using ring.
def Mathlib.Tactic.Algebra.RingCompute.derive E1 #
{u v : Lean.Level} →
{R : Q(Type u)} →
{A : Q(Type v)} →
{sR : Q(CommSemiring «$R»)} →
{sA : Q(CommSemiring «$A»)} →
(sAlg : Q(Algebra «$R» «$A»)) →
Mathlib.Tactic.Algebra.Cache sR →
Mathlib.Tactic.Algebra.Cache sA →
(x : Q(«$A»)) →
Lean.MetaM
(Mathlib.Tactic.Ring.Common.Result
(Mathlib.Tactic.Ring.Common.ExSum (Mathlib.Tactic.Algebra.BaseType sAlg) sA) q(«$x»))
Evaluate constants in A using norm_num.
def Mathlib.Tactic.Algebra.RingCompute.isOne E1 #
{u v : Lean.Level} →
{R : Q(Type u)} →
{A : Q(Type v)} →
{sR : Q(CommSemiring «$R»)} →
{sA : Q(CommSemiring «$A»)} →
(sAlg : Q(Algebra «$R» «$A»)) →
Mathlib.Tactic.Ring.Common.Cache sR →
{x : Q(«$A»)} → Mathlib.Tactic.Algebra.BaseType sAlg x → Option Q(Mathlib.Meta.NormNum.IsNat «$x» 1)
Decide if a coefficient is 1.
def Mathlib.Tactic.Algebra.ringCompare E1 #
{u v : Lean.Level} →
{R : Q(Type u)} →
{A : Q(Type v)} →
{sR : Q(CommSemiring «$R»)} →
{sA : Q(CommSemiring «$A»)} →
(sAlg : Q(Algebra «$R» «$A»)) → Mathlib.Tactic.Ring.Common.RingCompare (Mathlib.Tactic.Algebra.BaseType sAlg)
The data used by the algebra tactic to normalize the constant coefficients, which are
expressions in R normalized by ring.
def Mathlib.Tactic.Algebra.ringCompute E1 #
{u v : Lean.Level} →
{R : Q(Type u)} →
{A : Q(Type v)} →
{sR : Q(CommSemiring «$R»)} →
{sA : Q(CommSemiring «$A»)} →
(sAlg : Q(Algebra «$R» «$A»)) →
Mathlib.Tactic.Algebra.Cache sR →
Mathlib.Tactic.Algebra.Cache sA →
Mathlib.Tactic.Ring.Common.RingCompute (Mathlib.Tactic.Algebra.BaseType sAlg) sA
The data used by the algebra tactic to normalize the constant coefficients, which are
expressions in R normalized by ring.
theorem Mathlib.Tactic.Algebra.Nat.cast_eq_algebraMap E1 #
∀ (A : Type u_1) [inst : CommSemiring A] (n : ℕ), ↑n = (algebraMap ℕ A) n
theorem Mathlib.Tactic.Algebra.Int.cast_eq_algebraMap E1 #
∀ (A : Type u_1) [inst : CommRing A] (n : ℤ), ↑n = (algebraMap ℤ A) n
def Mathlib.Tactic.Algebra.preprocess E1 #
Lean.Expr → Lean.MetaM Lean.Meta.Simp.Result
Remove some nonstandard spellings of algebraMap such as Nat.cast
opaque Mathlib.Tactic.Algebra.collectScalarRingsAux E0 #
Lean.Expr → StateT (List Lean.Expr) Lean.MetaM Unit
Collect all scalar rings from scalar multiplications using a state monad for performance.
Note: The match in this definition should be kept up to date with the Common.eval function.
def Mathlib.Tactic.Algebra.collectScalarRings E1 #
Lean.Expr → Lean.MetaM (List Lean.Expr)
Collect all scalar rings from scalar multiplications and algebraMap applications in the
expression.
def Mathlib.Tactic.Algebra.pickLargerRing E1 #
(u : Lean.Level) × Q(Type u) → (u : Lean.Level) × Q(Type u) → Lean.MetaM ((u : Lean.Level) × Q(Type u))
Given two rings, determine which is 'larger' in the sense that the larger is an algebra over the smaller. Returns the first ring if they're the same or incompatible.
def Mathlib.Tactic.Algebra.inferBase E1 #
{v : Lean.Level} →
{A : Q(Type v)} →
{sA : Q(CommSemiring «$A»)} →
Mathlib.Tactic.Algebra.Cache q(«$sA») → Lean.Expr → Lean.MetaM ((u : Lean.Level) × Q(Type u))
Infer from the expression what base ring the normalization should use. Finds all scalar rings in the expression and picks the 'larger' one in the sense that it is an algebra over the smaller rings.
def Mathlib.Tactic.Algebra.proveEq E1 #
Option ((u : Lean.Level) × Q(Type u)) → Lean.MVarId → Mathlib.Tactic.AtomM Unit
Frontend of algebra: attempt to close a goal g, assuming it is an equation of semirings.
def Mathlib.Tactic.Algebra.algebra E1 #
Lean.ParserDescr
algebra solves equalities in the language of algebras: ring operations and scalar
multiplications.
Given a goal which is an equality in a commutative R-algebra A, algebra parses the LHS and
RHS of the goal as polynomial expressions of A-atoms with coefficients in some semiring R, and
closes the goal if the two expressions are the same. The R-coefficients are put into ring normal
form.
By default, the scalar ring R is inferred automatically by looking for scalar multiplications and
algebraMaps present in the expressions. The inference procedure assumes that any two rings R
and S that appear are comparable, in the sense that either R is an S-algebra or S is an
R-algebra.
algebra with Ruses the termRas the scalar ring, instead of attempting to infer it automatically.
def Mathlib.Tactic.Algebra.algebraWith E1 #
Lean.ParserDescr
algebra solves equalities in the language of algebras: ring operations and scalar
multiplications.
Given a goal which is an equality in a commutative R-algebra A, algebra parses the LHS and
RHS of the goal as polynomial expressions of A-atoms with coefficients in some semiring R, and
closes the goal if the two expressions are the same. The R-coefficients are put into ring normal
form.
By default, the scalar ring R is inferred automatically by looking for scalar multiplications and
algebraMaps present in the expressions. The inference procedure assumes that any two rings R
and S that appear are comparable, in the sense that either R is an S-algebra or S is an
R-algebra.
algebra with Ruses the termRas the scalar ring, instead of attempting to infer it automatically.