The areas / V34. The tactics of Mathlib
Mathlib.Tactic.Module
53 declarations. Mathlib docs · Source
A tactic for normalization over modules
This file provides the two tactics match_scalars and module. Given a goal which is an equality
in a type M (with M an AddCommMonoid), the match_scalars tactic parses the LHS and RHS of
the goal as linear combinations of M-atoms over some semiring R, and reduces the goal to
the respective equalities of the R-coefficients of each atom. The module tactic does this and
then runs the ring tactic on each of these coefficient-wise equalities, failing if this does not
resolve them.
This file also contains the scalar-normalization engine (normalizeScalar, qNF.rebuild and
eval) which backs the module_nf tactic defined in Mathlib.Tactic.ModuleNF.
The scalar type R is not pre-determined: instead it starts as ℕ (when each atom is initially
given a scalar (1:ℕ)) and gets bumped up into bigger semirings when such semirings are
encountered. However, to permit this, it is assumed that there is a "linear order" on all the
semirings which appear in the expression: for any two semirings R and S which occur, we have
either Algebra R S or Algebra S R.
Theory of lists of pairs (scalar, vector)
This section contains the lemmas which are orchestrated by the match_scalars and module tactics
to prove goals in modules. The basic object which these lemmas concern is NF R M, a type synonym
for a list of ordered pairs in R × M, where typically M is an R-module.
Lists of expressions representing scalars and vectors, and operations on such lists
Core of the module tactic
theorem Mathlib.Tactic.Module.NF.eval.match_1.congr_eq_1 E1 #
∀ {R : Type u_1} {M : Type u_2} (motive : R × M → Sort u_3) (x : R × M) (h_1 : (r : R) → (x : M) → motive (r, x))
(r : R) (x_1 : M),
x = (r, x_1) →
(match x with
| (r, x) => h_1 r x) ≍
h_1 r x_1
def Mathlib.Tactic.Module.NF E1 #
Type u_1 → Type u_2 → Type (max u_2 u_1)
Basic theoretical "normal form" object of the match_scalars and module tactics: a type
synonym for a list of ordered pairs in R × M, where typically M is an R-module. This is the
form to which the tactics reduce module expressions.
(It is not a full "normal form" because the scalars, i.e. R components, are not themselves
ring-normalized. But this partial normal form is more convenient for our purposes.)
def Mathlib.Tactic.Module.NF.cons E1 #
{R : Type u_2} → {M : Type u_3} → R × M → Mathlib.Tactic.Module.NF R M → Mathlib.Tactic.Module.NF R M
Augment a Module.NF R M object l, i.e. a list of pairs in R × M, by prepending another
pair p : R × M.
def Mathlib.Tactic.Module.NF.«term_::ᵣ_» E1 #
Lean.TrailingParserDescr
Augment a Module.NF R M object l, i.e. a list of pairs in R × M, by prepending another
pair p : R × M.
theorem Mathlib.Tactic.Module.NF.inductionOn E1 #
∀ {R : Type u_2} {M : Type u_3} {motive : Mathlib.Tactic.Module.NF R M → Prop} (l : Mathlib.Tactic.Module.NF R M),
motive [] → (∀ (p : R × M) (l : Mathlib.Tactic.Module.NF R M), motive l → motive (p ::ᵣ l)) → motive l
An induction principle for NF which mirrors induction on lists, with cases
for [] and NF.cons
def Mathlib.Tactic.Module.NF.eval E1 #
{R : Type u_2} → {M : Type u_3} → [Add M] → [Zero M] → [SMul R M] → Mathlib.Tactic.Module.NF R M → M
Evaluate a Module.NF R M object l, i.e. a list of pairs in R × M, to an element of M, by
forming the "linear combination" it specifies: scalar-multiply each R term to the corresponding
M term, then add them all up.
theorem Mathlib.Tactic.Module.NF.eval_cons E0 #
∀ {R : Type u_2} {M : Type u_3} [inst : AddMonoid M] [inst_1 : SMul R M] (p : R × M) (l : Mathlib.Tactic.Module.NF R M),
(p ::ᵣ l).eval = p.1 • p.2 + l.eval
theorem Mathlib.Tactic.Module.NF.eval_nil E1 #
∀ {R : Type u_2} {M : Type u_3} [inst : Add M] [inst_1 : Zero M] [inst_2 : SMul R M],
Mathlib.Tactic.Module.NF.eval [] = 0
theorem Mathlib.Tactic.Module.NF.eval_cons_eq E0 #
∀ {R : Type u_2} {M : Type u_3} [inst : AddCommMonoid M] [inst_1 : Semiring R] [inst_2 : Module R M] {r r' : R} (x : M)
{l : Mathlib.Tactic.Module.NF R M} {e : M}, r = r' → l.eval = e → ((r, x) ::ᵣ l).eval = r' • x + e
theorem Mathlib.Tactic.Module.NF.atom_eq_eval E1 #
∀ {M : Type u_3} [inst : AddMonoid M] (x : M), x = Mathlib.Tactic.Module.NF.eval [(1, x)]
theorem Mathlib.Tactic.Module.NF.zero_eq_eval E1 #
∀ (M : Type u_3) [inst : AddMonoid M], 0 = Mathlib.Tactic.Module.NF.eval []
theorem Mathlib.Tactic.Module.NF.add_eq_eval₁ E1 #
∀ {R : Type u_2} {M : Type u_3} [inst : AddMonoid M] [inst_1 : SMul R M] (a₁ : R × M) {a₂ : R × M}
{l₁ l₂ l : Mathlib.Tactic.Module.NF R M},
l₁.eval + (a₂ ::ᵣ l₂).eval = l.eval → (a₁ ::ᵣ l₁).eval + (a₂ ::ᵣ l₂).eval = (a₁ ::ᵣ l).eval
theorem Mathlib.Tactic.Module.NF.add_eq_eval₂ E1 #
∀ {R : Type u_2} {M : Type u_3} [inst : Semiring R] [inst_1 : AddCommMonoid M] [inst_2 : Module R M] (r₁ r₂ : R) (x : M)
{l₁ l₂ l : Mathlib.Tactic.Module.NF R M},
l₁.eval + l₂.eval = l.eval → ((r₁, x) ::ᵣ l₁).eval + ((r₂, x) ::ᵣ l₂).eval = ((r₁ + r₂, x) ::ᵣ l).eval
theorem Mathlib.Tactic.Module.NF.add_eq_eval₃ E1 #
∀ {R : Type u_2} {M : Type u_3} [inst : Semiring R] [inst_1 : AddCommMonoid M] [inst_2 : Module R M] {a₁ : R × M}
(a₂ : R × M) {l₁ l₂ l : Mathlib.Tactic.Module.NF R M},
(a₁ ::ᵣ l₁).eval + l₂.eval = l.eval → (a₁ ::ᵣ l₁).eval + (a₂ ::ᵣ l₂).eval = (a₂ ::ᵣ l).eval
theorem Mathlib.Tactic.Module.NF.add_eq_eval E1 #
∀ {R : Type u_2} {M : Type u_3} {R₁ : Type u_4} {R₂ : Type u_5} [inst : AddCommMonoid M] [inst_1 : Semiring R]
[inst_2 : Module R M] [inst_3 : Semiring R₁] [inst_4 : Module R₁ M] [inst_5 : Semiring R₂] [inst_6 : Module R₂ M]
{l₁ l₂ l : Mathlib.Tactic.Module.NF R M} {l₁' : Mathlib.Tactic.Module.NF R₁ M} {l₂' : Mathlib.Tactic.Module.NF R₂ M}
{x₁ x₂ : M},
x₁ = l₁'.eval →
x₂ = l₂'.eval → l₁.eval = l₁'.eval → l₂.eval = l₂'.eval → l₁.eval + l₂.eval = l.eval → x₁ + x₂ = l.eval
theorem Mathlib.Tactic.Module.NF.sub_eq_eval₁ E1 #
∀ {R : Type u_2} {M : Type u_3} [inst : SMul R M] [inst_1 : AddGroup M] (a₁ : R × M) {a₂ : R × M}
{l₁ l₂ l : Mathlib.Tactic.Module.NF R M},
l₁.eval - (a₂ ::ᵣ l₂).eval = l.eval → (a₁ ::ᵣ l₁).eval - (a₂ ::ᵣ l₂).eval = (a₁ ::ᵣ l).eval
theorem Mathlib.Tactic.Module.NF.sub_eq_eval₂ E1 #
∀ {R : Type u_2} {M : Type u_3} [inst : Ring R] [inst_1 : AddCommGroup M] [inst_2 : Module R M] (r₁ r₂ : R) (x : M)
{l₁ l₂ l : Mathlib.Tactic.Module.NF R M},
l₁.eval - l₂.eval = l.eval → ((r₁, x) ::ᵣ l₁).eval - ((r₂, x) ::ᵣ l₂).eval = ((r₁ - r₂, x) ::ᵣ l).eval
theorem Mathlib.Tactic.Module.NF.sub_eq_eval₃ E1 #
∀ {R : Type u_2} {M : Type u_3} [inst : Ring R] [inst_1 : AddCommGroup M] [inst_2 : Module R M] {a₁ : R × M}
(a₂ : R × M) {l₁ l₂ l : Mathlib.Tactic.Module.NF R M},
(a₁ ::ᵣ l₁).eval - l₂.eval = l.eval → (a₁ ::ᵣ l₁).eval - (a₂ ::ᵣ l₂).eval = ((-a₂.1, a₂.2) ::ᵣ l).eval
theorem Mathlib.Tactic.Module.NF.sub_eq_eval E1 #
∀ {R : Type u_2} {M : Type u_3} {R₁ : Type u_4} {R₂ : Type u_5} {S₁ : Type u_6} {S₂ : Type u_7} [inst : AddCommGroup M]
[inst_1 : Ring R] [inst_2 : Module R M] [inst_3 : Semiring R₁] [inst_4 : Module R₁ M] [inst_5 : Semiring R₂]
[inst_6 : Module R₂ M] [inst_7 : Semiring S₁] [inst_8 : Module S₁ M] [inst_9 : Semiring S₂] [inst_10 : Module S₂ M]
{l₁ l₂ l : Mathlib.Tactic.Module.NF R M} {l₁' : Mathlib.Tactic.Module.NF R₁ M} {l₂' : Mathlib.Tactic.Module.NF R₂ M}
{l₁'' : Mathlib.Tactic.Module.NF S₁ M} {l₂'' : Mathlib.Tactic.Module.NF S₂ M} {x₁ x₂ : M},
x₁ = l₁''.eval →
x₂ = l₂''.eval →
l₁'.eval = l₁''.eval →
l₂'.eval = l₂''.eval → l₁.eval = l₁'.eval → l₂.eval = l₂'.eval → l₁.eval - l₂.eval = l.eval → x₁ - x₂ = l.eval
instance Mathlib.Tactic.Module.NF.instNeg E0 #
{R : Type u_2} → {M : Type u_3} → [Neg R] → Neg (Mathlib.Tactic.Module.NF R M)
theorem Mathlib.Tactic.Module.NF.neg_cons E1 #
∀ {R : Type u_2} {M : Type u_3} [inst : Neg R] (p : R × M) (l : Mathlib.Tactic.Module.NF R M),
-p ::ᵣ l = (-p.1, p.2) ::ᵣ (-l)
theorem Mathlib.Tactic.Module.NF.eval_neg E1 #
∀ {R : Type u_2} {M : Type u_3} [inst : AddCommGroup M] [inst_1 : Ring R] [inst_2 : Module R M]
(l : Mathlib.Tactic.Module.NF R M), (-l).eval = -l.eval
theorem Mathlib.Tactic.Module.NF.zero_sub_eq_eval E1 #
∀ {R : Type u_2} {M : Type u_3} [inst : AddCommGroup M] [inst_1 : Ring R] [inst_2 : Module R M]
(l : Mathlib.Tactic.Module.NF R M), 0 - l.eval = (-l).eval
theorem Mathlib.Tactic.Module.NF.neg_eq_eval E1 #
∀ {S : Type u_1} {R : Type u_2} {M : Type u_3} [inst : AddCommGroup M] [inst_1 : Semiring S] [inst_2 : Module S M]
[inst_3 : Ring R] [inst_4 : Module R M] {l : Mathlib.Tactic.Module.NF R M} {l₀ : Mathlib.Tactic.Module.NF S M},
l.eval = l₀.eval → ∀ {x : M}, x = l₀.eval → -x = (-l).eval
instance Mathlib.Tactic.Module.NF.instSMulOfMul E0 #
{R : Type u_2} → {M : Type u_3} → [Mul R] → SMul R (Mathlib.Tactic.Module.NF R M)
theorem Mathlib.Tactic.Module.NF.smul_apply E0 #
∀ {R : Type u_2} {M : Type u_3} [inst : Mul R] (r : R) (l : Mathlib.Tactic.Module.NF R M),
r • l =
List.map
(fun x ↦
match x with
| (a, x) => (r * a, x))
l
theorem Mathlib.Tactic.Module.NF.smul_cons E1 #
∀ {R : Type u_2} {M : Type u_3} [inst : Mul R] (r : R) (p : R × M) (l : Mathlib.Tactic.Module.NF R M),
r • p ::ᵣ l = (r * p.1, p.2) ::ᵣ (r • l)
theorem Mathlib.Tactic.Module.NF.eval_smul E1 #
∀ {R : Type u_2} {M : Type u_3} [inst : AddCommMonoid M] [inst_1 : Semiring R] [inst_2 : Module R M]
{l : Mathlib.Tactic.Module.NF R M} {x : M}, x = l.eval → ∀ (r : R), (r • l).eval = r • x
theorem Mathlib.Tactic.Module.NF.smul_eq_eval E1 #
∀ {S : Type u_1} {R : Type u_2} {M : Type u_3} {R₀ : Type u_4} [inst : AddCommMonoid M] [inst_1 : Semiring R]
[inst_2 : Module R M] [inst_3 : Semiring R₀] [inst_4 : Module R₀ M] [inst_5 : Semiring S] [inst_6 : Module S M]
{l : Mathlib.Tactic.Module.NF R M} {l₀ : Mathlib.Tactic.Module.NF R₀ M} {s : S} {r : R} {x : M},
x = l₀.eval → l.eval = l₀.eval → r • x = s • x → s • x = (r • l).eval
def Mathlib.Tactic.Module.NF.algebraMap E1 #
{S : Type u_1} →
(R : Type u_2) →
{M : Type u_3} →
[inst : CommSemiring S] →
[inst_1 : Semiring R] → [Algebra S R] → Mathlib.Tactic.Module.NF S M → Mathlib.Tactic.Module.NF R M
Operate on a Module.NF S M object l, i.e. a list of pairs in S × M, where S is some
commutative semiring, by applying to each S-component the algebra-map from S into a specified
S-algebra R.
theorem Mathlib.Tactic.Module.NF.algebraMap_cons E1 #
∀ {S : Type u_1} (R : Type u_2) {M : Type u_3} [inst : CommSemiring S] [inst_1 : Semiring R] [inst_2 : Algebra S R]
(p : S × M) (l : Mathlib.Tactic.Module.NF S M),
Mathlib.Tactic.Module.NF.algebraMap R (p ::ᵣ l) =
((algebraMap S R) p.1, p.2) ::ᵣ Mathlib.Tactic.Module.NF.algebraMap R l
theorem Mathlib.Tactic.Module.NF.eval_algebraMap E1 #
∀ {S : Type u_1} (R : Type u_2) {M : Type u_3} [inst : CommSemiring S] [inst_1 : Semiring R] [inst_2 : Algebra S R]
[inst_3 : AddMonoid M] [inst_4 : SMul S M] [inst_5 : MulAction R M] [IsScalarTower S R M]
(l : Mathlib.Tactic.Module.NF S M), (Mathlib.Tactic.Module.NF.algebraMap R l).eval = l.eval
abbrev Mathlib.Tactic.Module.qNF E1 #
{u v : Lean.Level} → Q(Type u) → Q(Type v) → Type
Basic meta-code "normal form" object of the match_scalars and module tactics: a type synonym
for a list of ordered triples comprising expressions representing terms of two types R and M
(where typically M is an R-module), together with a natural number "index".
The natural number represents the index of the M term in the AtomM monad: this is not enforced,
but is sometimes assumed in operations. Thus when items ((a₁, x₁), k) and ((a₂, x₂), k)
appear in two different Module.qNF objects (i.e. with the same ℕ-index k), it is expected that
the expressions x₁ and x₂ are the same. It is also expected that the items in a Module.qNF
list are in strictly increasing order by natural-number index.
By forgetting the natural number indices, an expression representing a Mathlib.Tactic.Module.NF
object can be built from a Module.qNF object; this construction is provided as
Mathlib.Tactic.Module.qNF.toNF.
def Mathlib.Tactic.Module.qNF.toNF E1 #
{u v : Lean.Level} →
{M : Q(Type v)} → {R : Q(Type u)} → Mathlib.Tactic.Module.qNF R M → Q(Mathlib.Tactic.Module.NF «$R» «$M»)
Given l of type qNF R M, i.e. a list of (Q($R) × Q($M)) × ℕs (two Exprs and a natural
number), build an Expr representing an object of type NF R M (i.e. List (R × M)) in the
in the obvious way: by forgetting the natural numbers and gluing together the Exprs.
def Mathlib.Tactic.Module.qNF.onScalar E1 #
{v : Lean.Level} →
{M : Q(Type v)} →
{u₁ u₂ : Lean.Level} →
{R₁ : Q(Type u₁)} →
{R₂ : Q(Type u₂)} → Mathlib.Tactic.Module.qNF R₁ M → Q(«$R₁» → «$R₂») → Mathlib.Tactic.Module.qNF R₂ M
Given l of type qNF R₁ M, i.e. a list of (Q($R₁) × Q($M)) × ℕs (two Exprs and a natural
number), apply an expression representing a function with domain R₁ to each of the Q($R₁)
components.
def Mathlib.Tactic.Module.qNF.add E1 #
{u v : Lean.Level} →
{M : Q(Type v)} →
{R : Q(Type u)} →
Q(Semiring «$R») → Mathlib.Tactic.Module.qNF R M → Mathlib.Tactic.Module.qNF R M → Mathlib.Tactic.Module.qNF R M
Given two terms l₁, l₂ of type qNF R M, i.e. lists of (Q($R) × Q($M)) × ℕs (two Exprs
and a natural number), construct another such term l, which will have the property that in the
$R-module $M, the sum of the "linear combinations" represented by l₁ and l₂ is the linear
combination represented by l.
The construction assumes, to be valid, that the lists l₁ and l₂ are in strictly increasing order
by ℕ-component, and that if pairs (a₁, x₁) and (a₂, x₂) appear in l₁, l₂ respectively with
the same ℕ-component k, then the expressions x₁ and x₂ are equal.
The construction is as follows: merge the two lists, except that if pairs (a₁, x₁) and (a₂, x₂)
appear in l₁, l₂ respectively with the same ℕ-component k, then contribute a term
(a₁ + a₂, x₁) to the output list with ℕ-component k.
def Mathlib.Tactic.Module.qNF.mkAddProof E1 #
{u v : Lean.Level} →
{M : Q(Type v)} →
{R : Q(Type u)} →
{iR : Q(Semiring «$R»)} →
{iM : Q(AddCommMonoid «$M»)} →
(iRM : Q(Module «$R» «$M»)) →
(l₁ l₂ : Mathlib.Tactic.Module.qNF R M) →
have a := (Mathlib.Tactic.Module.qNF.add iR l₁ l₂).toNF;
have a_1 := l₂.toNF;
have a_2 := l₁.toNF;
Q(«$a_2».eval + «$a_1».eval = «$a».eval)
Given two terms l₁, l₂ of type qNF R M, i.e. lists of (Q($R) × Q($M)) × ℕs (two Exprs
and a natural number), recursively construct a proof that in the $R-module $M, the sum of the
"linear combinations" represented by l₁ and l₂ is the linear combination represented by
Module.qNF.add iR l₁ l₁.
def Mathlib.Tactic.Module.qNF.sub E1 #
{u v : Lean.Level} →
{M : Q(Type v)} →
{R : Q(Type u)} →
Q(Ring «$R») → Mathlib.Tactic.Module.qNF R M → Mathlib.Tactic.Module.qNF R M → Mathlib.Tactic.Module.qNF R M
Given two terms l₁, l₂ of type qNF R M, i.e. lists of (Q($R) × Q($M)) × ℕs (two Exprs
and a natural number), construct another such term l, which will have the property that in the
$R-module $M, the difference of the "linear combinations" represented by l₁ and l₂ is the
linear combination represented by l.
The construction assumes, to be valid, that the lists l₁ and l₂ are in strictly increasing order
by ℕ-component, and that if pairs (a₁, x₁) and (a₂, x₂) appear in l₁, l₂ respectively with
the same ℕ-component k, then the expressions x₁ and x₂ are equal.
The construction is as follows: merge the first list and the negation of the second list, except
that if pairs (a₁, x₁) and (a₂, x₂) appear in l₁, l₂ respectively with the same
ℕ-component k, then contribute a term (a₁ - a₂, x₁) to the output list with ℕ-component k.
def Mathlib.Tactic.Module.qNF.mkSubProof E1 #
{u v : Lean.Level} →
{M : Q(Type v)} →
{R : Q(Type u)} →
(iR : Q(Ring «$R»)) →
(iM : Q(AddCommGroup «$M»)) →
(iRM : Q(Module «$R» «$M»)) →
(l₁ l₂ : Mathlib.Tactic.Module.qNF R M) →
have a := (Mathlib.Tactic.Module.qNF.sub iR l₁ l₂).toNF;
have a_1 := l₂.toNF;
have a_2 := l₁.toNF;
Q(«$a_2».eval - «$a_1».eval = «$a».eval)
Given two terms l₁, l₂ of type qNF R M, i.e. lists of (Q($R) × Q($M)) × ℕs (two Exprs
and a natural number), recursively construct a proof that in the $R-module $M, the difference
of the "linear combinations" represented by l₁ and l₂ is the linear combination represented by
Module.qNF.sub iR l₁ l₁.
def Mathlib.Tactic.Module.qNF.matchRings E1 #
{v : Lean.Level} →
{M : Q(Type v)} →
{iM : Q(AddCommMonoid «$M»)} →
{u₁ : Lean.Level} →
{R₁ : Q(Type u₁)} →
{iR₁ : Q(Semiring «$R₁»)} →
(iRM₁ : Q(Module «$R₁» «$M»)) →
{u₂ : Lean.Level} →
{R₂ : Q(Type u₂)} →
(iR₂ : Q(Semiring «$R₂»)) →
(iRM₂ : Q(Module «$R₂» «$M»)) →
(l₁ : Mathlib.Tactic.Module.qNF R₁ M) →
(l₂ : Mathlib.Tactic.Module.qNF R₂ M) →
(r : Q(«$R₂»)) →
(x : Q(«$M»)) →
Lean.MetaM
((u : Lean.Level) ×
(R : Q(Type u)) ×
(iR : Q(Semiring «$R»)) ×
(x_1 : Q(Module «$R» «$M»)) ×
((l₁' : Mathlib.Tactic.Module.qNF R M) ×
have a := l₁.toNF;
have a_1 := l₁'.toNF;
Q(«$a_1».eval = «$a».eval)) ×
((l₂' : Mathlib.Tactic.Module.qNF R M) ×
have a := l₂.toNF;
have a_1 := l₂'.toNF;
Q(«$a_1».eval = «$a».eval)) ×
(r' : Q(«$R»)) × Q(«$r'» • «$x» = «$r» • «$x»))
Given an expression M representing a type which is an AddCommMonoid and a module over two
semirings R₁ and R₂, find the "bigger" of the two semirings. That is, we assume that it will
turn out to be the case that either (1) R₁ is an R₂-algebra and the R₂ scalar action on M is
induced from R₁'s scalar action on M, or (2) vice versa; we return the semiring R₁ in the
first case and R₂ in the second case.
Moreover, given expressions representing particular scalar multiplications of R₁ and/or R₂ on
M (a List (R₁ × M), a List (R₂ × M), a pair (r, x) : R₂ × M), bump these up to the "big"
ring by applying the algebra-map where needed.
opaque Mathlib.Tactic.Module.parse E1 #
{v : Lean.Level} →
{M : Q(Type v)} →
(iM : Q(AddCommMonoid «$M»)) →
(x : Q(«$M»)) →
Mathlib.Tactic.AtomM
((u : Lean.Level) ×
(R : Q(Type u)) ×
(iR : Q(Semiring «$R»)) ×
(x_1 : Q(Module «$R» «$M»)) ×
(l : Mathlib.Tactic.Module.qNF R M) ×
have a := l.toNF;
Q(«$x» = «$a».eval))
The main algorithm behind the match_scalars and module tactics: partially-normalizing an
expression in an additive commutative monoid M into the form c1 • x1 + c2 • x2 + ... c_k • x_k,
where x1, x2, ... are distinct atoms in M, and c1, c2, ... are scalars. The scalar type of the
expression is not pre-determined: instead it starts as ℕ (when each atom is initially given a
scalar (1:ℕ)) and gets bumped up into bigger semirings when such semirings are encountered.
It is assumed that there is a "linear order" on all the semirings which appear in the expression:
for any two semirings R and S which occur, we have either Algebra R S or Algebra S R.
TODO: implement a variant in which a semiring R is provided by the user, and the assumption is
instead that for any semiring S which occurs, we have Algebra S R. The PR https://github.com/leanprover-community/mathlib4/pull/16984 provides a
proof-of-concept implementation of this variant, but it would need some polishing before joining
Mathlib.
Possible TODO, if poor performance on large problems is witnessed: switch the implementation from
AtomM to CanonM, per the discussion
https://github.com/leanprover-community/mathlib4/pull/16593/files#r1749623191
opaque Mathlib.Tactic.Module.reduceCoefficientwise E1 #
{u v : Lean.Level} →
{M : Q(Type v)} →
{R : Q(Type u)} →
{x : Q(AddCommMonoid «$M»)} →
{x_1 : Q(Semiring «$R»)} →
(iRM : Q(Module «$R» «$M»)) →
(l₁ l₂ : Mathlib.Tactic.Module.qNF R M) →
Lean.MetaM
(List Lean.MVarId ×
have a := l₂.toNF;
have a_1 := l₁.toNF;
Q(«$a_1».eval = «$a».eval))
Given expressions R and M representing types such that M's is a module over R's, and
given two terms l₁, l₂ of type qNF R M, i.e. lists of (Q($R) × Q($M)) × ℕs (two Exprs
and a natural number), construct a list of new goals: that the R-coefficient of an M-atom which
appears in only one list is zero, and that the R-coefficients of an M-atom which appears in both
lists are equal. Also construct (dependent on these new goals) a proof that the "linear
combinations" represented by l₁ and l₂ are equal in M.
def Mathlib.Tactic.Module.matchScalarsAux E0 #
Lean.MVarId → Mathlib.Tactic.AtomM (List Lean.MVarId)
Given a goal which is an equality in a type M (with M an AddCommMonoid), parse the LHS and
RHS of the goal as linear combinations of M-atoms over some semiring R, and reduce the goal to
the respective equalities of the R-coefficients of each atom.
This is an auxiliary function which produces slightly awkward goals in R; they are later cleaned
up by the function Mathlib.Tactic.Module.postprocess.
def Mathlib.Tactic.Module.algebraMapThms E1 #
Array Lean.Name
Lemmas used to post-process the result of the match_scalars and module tactics by converting
the algebraMap operations which (which proliferate in the constructed scalar goals) to more
familiar forms: ℕ, ℤ and ℚ casts.
def Mathlib.Tactic.Module.postprocessCtx E1 #
Lean.MetaM Lean.Meta.Simp.Context
A Simp.Context containing push_cast and algebraMapThms lemmas.
def Mathlib.Tactic.Module.postprocess E1 #
Lean.MVarId → Lean.MetaM Lean.MVarId
Postprocessing for the scalar goals constructed in the match_scalars and module tactics.
These goals feature a proliferation of algebraMap operations (because the scalars start in ℕ and
get successively bumped up by algebraMaps as new semirings are encountered), so we reinterpret the
most commonly occurring algebraMaps (those out of ℕ, ℤ and ℚ) into their standard forms
(ℕ, ℤ and ℚ casts) and then try to disperse the casts using the various push_cast lemmas.
The * 1 tags which the parser marks atoms are also cleared.
def Mathlib.Tactic.Module.matchScalars E1 #
Lean.MVarId → Lean.MetaM (List Lean.MVarId)
Given a goal which is an equality in a type M (with M an AddCommMonoid), parse the LHS and
RHS of the goal as linear combinations of M-atoms over some semiring R, and reduce the goal to
the respective equalities of the R-coefficients of each atom.
def Mathlib.Tactic.Module.normalizeScalar E1 #
Lean.Meta.Simp.Context → Lean.Expr → Mathlib.Tactic.AtomM Lean.Meta.Simp.Result
Normalize a scalar produced by Mathlib.Tactic.Module.parse. This performs
the same processing as Mathlib.Tactic.Module.postprocess and then normalizes
with ring_nf if it's applicable.
def Mathlib.Tactic.Module.qNF.rebuild E1 #
{u v : Lean.Level} →
{M : Q(Type v)} →
{R : Q(Type u)} →
(iM : Q(AddCommMonoid «$M»)) →
(iR : Q(Semiring «$R»)) →
(iRM : Q(Module «$R» «$M»)) →
Lean.Meta.Simp.Context →
(l : Mathlib.Tactic.Module.qNF R M) →
Mathlib.Tactic.AtomM
((e : Q(«$M»)) ×
have a := l.toNF;
Q(«$a».eval = «$e»))
Rebuild the reified list l as an expression c₁' • x₁ + (c₂' • x₂ + ... + 0)
with normalized scalars and a proof that it equals NF.eval l.
def Mathlib.Tactic.Module.qNF.liftToBase? E1 #
{u v : Lean.Level} →
{M : Q(Type v)} →
{R : Q(Type u)} →
(iM : Q(AddCommMonoid «$M»)) →
(iR : Q(Semiring «$R»)) →
(iRM : Q(Module «$R» «$M»)) →
(w : Lean.Level) × Q(Type w) →
(l : Mathlib.Tactic.Module.qNF R M) →
Lean.MetaM
(Option
((w : Lean.Level) ×
(B : Q(Type w)) ×
(iB : Q(Semiring «$B»)) ×
(x : Q(Module «$B» «$M»)) ×
(l' : Mathlib.Tactic.Module.qNF B M) ×
have a := l.toNF;
have a_1 := l'.toNF;
Q(«$a_1».eval = «$a».eval)))
Attempt to lift the scalars of l into the ring base.
base must be a semiring acting on M, and qNF.matchRings must be able to relate the parsed ring
R to it (it throws when the two rings are not comparable; this is caught here and returned as
none). On success, the lifted list is returned together with a proof that its evaluation agrees
with that of l.
def Mathlib.Tactic.Module.eval E1 #
{v : Lean.Level} →
{M : Q(Type v)} →
Q(AddCommMonoid «$M») →
Option ((u : Lean.Level) × Q(Type u)) →
Lean.Meta.Simp.Context → Q(«$M») → Mathlib.Tactic.AtomM Lean.Meta.Simp.Result
Normalize an expression in an AddCommMonoid into the form c₁ • x₁ + (c₂ • x₂ + ... + 0)
with normalized scalars by chaining parse and qNF.rebuild.
When base? is provided, eval will also attempt to lift the coefficients into base?. If base?
is not a semiring acting on M or when qNF.matchRings cannot relate the parsed ring to base?
then the result falls back to the parsed ring.
def Mathlib.Tactic.Module.tacticMatch_scalars E1 #
Lean.ParserDescr
Given a goal parseable as a linear combination ⊢ a • x + ... + b • y = c • x + ... + d • y,
match_scalars splits up the goal into equalities of the scalars for each respective atom. This
means the example goal above is replaced by goals ⊢ a = c (from x), ..., ⊢ b = d (from y).
The module tactic is equivalent to match_scalars <;> ring1.
match_scalars can parse into expressions made of the operators +, -, • and the numeral 0.
Any other subexpressions (including variables) are treated as atoms.
If the goal is an equality in the type M, then match_scalars requires an AddCommMonoid M
instance. If the goal contains a scalar multiplication (a : R) • (x : M), this requires a
Semiring R and Module R M instance. If any of the instances are missing, match_scalars fails.
The scalar type for the goals produced by the match_scalars tactic is the largest scalar type
encountered; for example, if ℕ, ℚ and a characteristic-zero field K all occur as scalars, then
the goals produced are equalities in K with the appropriate casts (from ℕ, ℤ, ℚ) and
algebraMaps (otherwise) inserted. Inserted casts are simplified by lemmas tagged @[push_cast].
If the set of scalar types encountered is not totally ordered (in the sense that for all rings R,
S encountered, it holds that either Algebra R S or Algebra S R), then match_scalars fails.
Examples:
example [AddCommMonoid M] [Semiring R] [Module R M] (a b : R) (x : M) :
a • x + b • x = (b + a) • x := by
match_scalars
-- one goal: `⊢ a + b = b + a`
example [AddCommGroup M] [Ring R] [Module R M] (a b : R) (x : M) :
a • (a • x - b • y) + (b • a • y + b • b • x) = x := by
match_scalars
-- two goals:
-- `⊢ a * a + b * b = 1` (from the `x` atom)
-- `⊢ a * -b + b * a = 0` (from the `y` atom)
example [AddCommGroup M] [Ring R] [Module R M] (a : R) (x : M) :
-(2:R) • a • x = a • (-2:ℤ) • x := by
match_scalars
-- one goal: `⊢ -2 * a = a * -2`
def Mathlib.Tactic.Module.tacticModule E1 #
Lean.ParserDescr
Given a goal parseable as a linear combination ⊢ a • x + ... + b • y = c • x + ... + d • y,
module proves the equalities of the scalars for each respective atom, by ring normalization.
This means the example goal above is solved if ring can prove ⊢ a = c (from x), ..., ⊢ b = d
(from y).
module is equivalent to match_scalars <;> ring1. If ring1 fails to prove one of the
equalities, you can instead use match_scalars followed by specialized proofs for each scalar.
Examples:
example [AddCommMonoid M] [CommSemiring R] [Module R M] (a b : R) (x : M) :
a • x + b • x = (b + a) • x := by
module
example [AddCommMonoid M] [Field K] [CharZero K] [Module K M] (x : M) :
(2:K)⁻¹ • x + (3:K)⁻¹ • x + (6:K)⁻¹ • x = x := by
module
example [AddCommGroup M] [CommRing R] [Module R M] (a : R) (v w : M) :
(1 + a ^ 2) • (v + w) - a • (a • v - w) = v + (1 + a + a ^ 2) • w := by
module
example [AddCommGroup M] [CommRing R] [Module R M] (a b μ ν : R) (x y : M) :
(μ - ν) • a • x = (a • μ • x + b • ν • y) - ν • (a • x + b • y) := by
module