The areas / V34. The tactics of Mathlib
Mathlib.Tactic.Linarith.Oracle.FourierMotzkin
59 declarations. Mathlib docs · Source
The Fourier-Motzkin elimination procedure
The Fourier-Motzkin procedure is a variable elimination method for linear inequalities. https://en.wikipedia.org/wiki/Fourier%E2%80%93Motzkin_elimination
Given a set of linear inequalities comps = {tᵢ Rᵢ 0},
we aim to eliminate a single variable a from the set.
We partition comps into comps_pos, comps_neg, and comps_zero,
where comps_pos contains the comparisons tᵢ Rᵢ 0 in which
the coefficient of a in tᵢ is positive, and similar.
For each pair of comparisons tᵢ Rᵢ 0 ∈ comps_pos, tⱼ Rⱼ 0 ∈ comps_neg,
we compute coefficients vᵢ, vⱼ ∈ ℕ such that vᵢ*tᵢ + vⱼ*tⱼ cancels out a.
We collect these sums vᵢ*tᵢ + vⱼ*tⱼ R' 0 in a set S and set comps' = S ∪ comps_zero,
a new set of comparisons in which a has been eliminated.
Theorem: comps and comps' are equisatisfiable.
We recursively eliminate all variables from the system. If we derive an empty clause 0 < 0,
we conclude that the original system was unsatisfiable.
Datatypes
The CompSource and PComp datatypes are specific to the FM elimination routine;
they are not shared with other components of linarith.
Elimination procedure
theorem Mathlib.Tactic.Linarith.CompSource.scale.injEq E0 #
∀ (a : ℕ) (a_1 : Mathlib.Tactic.Linarith.CompSource) (a_2 : ℕ) (a_3 : Mathlib.Tactic.Linarith.CompSource),
(Mathlib.Tactic.Linarith.CompSource.scale a a_1 = Mathlib.Tactic.Linarith.CompSource.scale a_2 a_3) =
(a = a_2 ∧ a_1 = a_3)
abbrev Mathlib.Tactic.Linarith.CompSource.ctorElimType E1 #
{motive : Mathlib.Tactic.Linarith.CompSource → Sort u} → ℕ → Sort (max 1 u)
def Mathlib.Tactic.Linarith.CompSource.ctorIdx E1 #
Mathlib.Tactic.Linarith.CompSource → ℕ
def Mathlib.Tactic.Linarith.PComp.ctorIdx E1 #
Mathlib.Tactic.Linarith.PComp → ℕ
theorem Mathlib.Tactic.Linarith.PComp.mk.inj E1 #
∀ {c : Mathlib.Tactic.Linarith.Comp} {src : Mathlib.Tactic.Linarith.CompSource}
{history effective implicit vars : Std.TreeSet ℕ compare} {c_1 : Mathlib.Tactic.Linarith.Comp}
{src_1 : Mathlib.Tactic.Linarith.CompSource} {history_1 effective_1 implicit_1 vars_1 : Std.TreeSet ℕ compare},
{ c := c, src := src, history := history, effective := effective, implicit := implicit, vars := vars } =
{ c := c_1, src := src_1, history := history_1, effective := effective_1, implicit := implicit_1,
vars := vars_1 } →
c = c_1 ∧ src = src_1 ∧ history = history_1 ∧ effective = effective_1 ∧ implicit = implicit_1 ∧ vars = vars_1
def Mathlib.Tactic.Linarith.instInhabitedCompSource.default E1 #
Mathlib.Tactic.Linarith.CompSource
theorem Mathlib.Tactic.Linarith.CompSource.add.injEq E0 #
∀ (a a_1 a_2 a_3 : Mathlib.Tactic.Linarith.CompSource), (a.add a_1 = a_2.add a_3) = (a = a_2 ∧ a_1 = a_3)
theorem Mathlib.Tactic.Linarith.CompSource.assump.inj E1 #
∀ {a a_1 : ℕ}, Mathlib.Tactic.Linarith.CompSource.assump a = Mathlib.Tactic.Linarith.CompSource.assump a_1 → a = a_1
abbrev Mathlib.Tactic.Linarith.CompSource.brecOn.go E1 #
{motive : Mathlib.Tactic.Linarith.CompSource → Sort u} →
(t : Mathlib.Tactic.Linarith.CompSource) →
((t : Mathlib.Tactic.Linarith.CompSource) → Mathlib.Tactic.Linarith.CompSource.below t → motive t) →
motive t ×' Mathlib.Tactic.Linarith.CompSource.below t
theorem Mathlib.Tactic.Linarith.LinarithData.mk.injEq E0 #
∀ (maxVar : ℕ) (comps : Mathlib.Tactic.Linarith.PCompSet) (maxVar_1 : ℕ) (comps_1 : Mathlib.Tactic.Linarith.PCompSet),
({ maxVar := maxVar, comps := comps } = { maxVar := maxVar_1, comps := comps_1 }) =
(maxVar = maxVar_1 ∧ comps = comps_1)
theorem Mathlib.Tactic.Linarith.LinarithData.mk.inj E1 #
∀ {maxVar : ℕ} {comps : Mathlib.Tactic.Linarith.PCompSet} {maxVar_1 : ℕ} {comps_1 : Mathlib.Tactic.Linarith.PCompSet},
{ maxVar := maxVar, comps := comps } = { maxVar := maxVar_1, comps := comps_1 } → maxVar = maxVar_1 ∧ comps = comps_1
theorem Mathlib.Tactic.Linarith.PComp.mk.injEq E0 #
∀ (c : Mathlib.Tactic.Linarith.Comp) (src : Mathlib.Tactic.Linarith.CompSource)
(history effective implicit vars : Std.TreeSet ℕ compare) (c_1 : Mathlib.Tactic.Linarith.Comp)
(src_1 : Mathlib.Tactic.Linarith.CompSource) (history_1 effective_1 implicit_1 vars_1 : Std.TreeSet ℕ compare),
({ c := c, src := src, history := history, effective := effective, implicit := implicit, vars := vars } =
{ c := c_1, src := src_1, history := history_1, effective := effective_1, implicit := implicit_1,
vars := vars_1 }) =
(c = c_1 ∧ src = src_1 ∧ history = history_1 ∧ effective = effective_1 ∧ implicit = implicit_1 ∧ vars = vars_1)
def Mathlib.Tactic.Linarith.LinarithData.ctorIdx E1 #
Mathlib.Tactic.Linarith.LinarithData → ℕ
theorem Mathlib.Tactic.Linarith.CompSource.assump.injEq E0 #
∀ (a a_1 : ℕ), (Mathlib.Tactic.Linarith.CompSource.assump a = Mathlib.Tactic.Linarith.CompSource.assump a_1) = (a = a_1)
theorem Mathlib.Tactic.Linarith.CompSource.add.inj E1 #
∀ {a a_1 a_2 a_3 : Mathlib.Tactic.Linarith.CompSource}, a.add a_1 = a_2.add a_3 → a = a_2 ∧ a_1 = a_3
theorem Mathlib.Tactic.Linarith.CompSource.scale.inj E1 #
∀ {a : ℕ} {a_1 : Mathlib.Tactic.Linarith.CompSource} {a_2 : ℕ} {a_3 : Mathlib.Tactic.Linarith.CompSource},
Mathlib.Tactic.Linarith.CompSource.scale a a_1 = Mathlib.Tactic.Linarith.CompSource.scale a_2 a_3 →
a = a_2 ∧ a_1 = a_3
theorem Mathlib.Tactic.Linarith.CompSource.brecOn.eq E1 #
∀ {motive : Mathlib.Tactic.Linarith.CompSource → Sort u} (t : Mathlib.Tactic.Linarith.CompSource)
(F_1 : (t : Mathlib.Tactic.Linarith.CompSource) → Mathlib.Tactic.Linarith.CompSource.below t → motive t),
Mathlib.Tactic.Linarith.CompSource.brecOn t F_1 = F_1 t (Mathlib.Tactic.Linarith.CompSource.brecOn.go t F_1).2
inductive Mathlib.Tactic.Linarith.CompSource E1 #
Type
CompSource tracks the source of a comparison.
The atomic source of a comparison is an assumption, indexed by a natural number.
Two comparisons can be added to produce a new comparison,
and one comparison can be scaled by a natural number to produce a new comparison.
ctor Mathlib.Tactic.Linarith.CompSource.assump E0 #
ℕ → Mathlib.Tactic.Linarith.CompSource
ctor Mathlib.Tactic.Linarith.CompSource.add E0 #
Mathlib.Tactic.Linarith.CompSource → Mathlib.Tactic.Linarith.CompSource → Mathlib.Tactic.Linarith.CompSource
ctor Mathlib.Tactic.Linarith.CompSource.scale E0 #
ℕ → Mathlib.Tactic.Linarith.CompSource → Mathlib.Tactic.Linarith.CompSource
instance Mathlib.Tactic.Linarith.instInhabitedCompSource E0 #
Inhabited Mathlib.Tactic.Linarith.CompSource
def Mathlib.Tactic.Linarith.CompSource.flatten E1 #
Mathlib.Tactic.Linarith.CompSource → Std.HashMap ℕ ℕ
Given a CompSource cs, cs.flatten maps an assumption index
to the number of copies of that assumption that appear in the history of cs.
For example, suppose cs is produced by scaling assumption 2 by 5,
and adding to that the sum of assumptions 1 and 2.
cs.flatten maps 1 ↦ 1, 2 ↦ 6.
def Mathlib.Tactic.Linarith.CompSource.toString E1 #
Mathlib.Tactic.Linarith.CompSource → String
Formats a CompSource for printing.
instance Mathlib.Tactic.Linarith.instToFormatCompSource E0 #
Std.ToFormat Mathlib.Tactic.Linarith.CompSource
structure Mathlib.Tactic.Linarith.PComp E1 #
Type
A PComp stores a linear comparison Σ cᵢ*xᵢ R 0,
along with information about how this comparison was derived.
The original expressions fed into linarith are each assigned a unique natural number label.
The historical set PComp.history stores the labels of expressions
that were used in deriving the current PComp.
Variables are also indexed by natural numbers. The sets PComp.effective, PComp.implicit,
and PComp.vars contain variable indices.
PComp.varscontains the variables that appear in any inequality in the historical set.PComp.effectivecontains the variables that have been effectively eliminated fromPComp. A variablenis said to be effectively eliminated inp : PCompif the elimination ofnproduced at least one of the ancestors ofp(orpitself).PComp.implicitcontains the variables that have been implicitly eliminated fromPComp. A variablenis said to be implicitly eliminated inpif it satisfies the following properties:nappears in some inequality in the historical set (i.e. inp.vars).ndoes not appear inp.c.vars(i.e. it has been eliminated).nwas not effectively eliminated.
We track these sets in order to compute whether the history of a PComp is minimal.
Checking this directly is expensive, but effective approximations can be defined in terms of these
sets. During the variable elimination process, a PComp with non-minimal history can be discarded.
ctor Mathlib.Tactic.Linarith.PComp.mk E0 #
Mathlib.Tactic.Linarith.Comp →
Mathlib.Tactic.Linarith.CompSource →
Std.TreeSet ℕ compare →
Std.TreeSet ℕ compare → Std.TreeSet ℕ compare → Std.TreeSet ℕ compare → Mathlib.Tactic.Linarith.PComp
abbrev Mathlib.Tactic.Linarith.PComp.c E1 #
Mathlib.Tactic.Linarith.PComp → Mathlib.Tactic.Linarith.Comp
The comparison Σ cᵢ*xᵢ R 0.
abbrev Mathlib.Tactic.Linarith.PComp.src E1 #
Mathlib.Tactic.Linarith.PComp → Mathlib.Tactic.Linarith.CompSource
We track how the comparison was constructed by adding and scaling previous comparisons, back to the original assumptions.
abbrev Mathlib.Tactic.Linarith.PComp.history E1 #
Mathlib.Tactic.Linarith.PComp → Std.TreeSet ℕ compare
The set of original assumptions which have been used in constructing this comparison.
abbrev Mathlib.Tactic.Linarith.PComp.effective E1 #
Mathlib.Tactic.Linarith.PComp → Std.TreeSet ℕ compare
The variables which have been effectively eliminated, i.e. by running the elimination algorithm on that variable.
abbrev Mathlib.Tactic.Linarith.PComp.implicit E1 #
Mathlib.Tactic.Linarith.PComp → Std.TreeSet ℕ compare
The variables which have been implicitly eliminated.
These are variables that appear in the historical set,
do not appear in c itself, and are not in effective.
abbrev Mathlib.Tactic.Linarith.PComp.vars E1 #
Mathlib.Tactic.Linarith.PComp → Std.TreeSet ℕ compare
The union of all variables appearing in those original assumptions
which appear in the history set.
def Mathlib.Tactic.Linarith.PComp.maybeMinimal E1 #
Mathlib.Tactic.Linarith.PComp → ℕ → Bool
Any comparison whose history is not minimal is redundant,
and need not be included in the new set of comparisons.
elimedGE : ℕ is a natural number such that all variables with index ≥ elimedGE have been
removed from the system.
This test is an overapproximation to minimality. It gives necessary but not sufficient conditions.
If the history of c is minimal, then c.maybeMinimal is true,
but c.maybeMinimal may also be true for some c with non-minimal history.
Thus, if c.maybeMinimal is false, c is known not to be minimal and must be redundant.
See https://doi.org/10.1016/B978-0-444-88771-9.50019-2 (Theorem 13).
The condition described there considers only implicitly eliminated variables that have been
officially eliminated from the system. This is not the case for every implicitly eliminated
variable. Consider eliminating z from {x + y + z < 0, x - y - z < 0}. The result is the set
{2*x < 0}; y is implicitly but not officially eliminated.
This implementation of Fourier-Motzkin elimination processes variables in decreasing order of
indices. Immediately after a step that eliminates variable k, variable k' has been eliminated
iff k' ≥ k. Thus we can compute the intersection of officially and implicitly eliminated variables
by taking the set of implicitly eliminated variables with indices ≥ elimedGE.
def Mathlib.Tactic.Linarith.PComp.cmp E1 #
Mathlib.Tactic.Linarith.PComp → Mathlib.Tactic.Linarith.PComp → Ordering
The src : CompSource field is ignored when comparing PComps. Two PComps proving the same
comparison, with different sources, are considered equivalent.
def Mathlib.Tactic.Linarith.PComp.scale E1 #
Mathlib.Tactic.Linarith.PComp → ℕ → Mathlib.Tactic.Linarith.PComp
PComp.scale c n scales the coefficients of c by n and notes this in the CompSource.
def Mathlib.Tactic.Linarith.PComp.add E1 #
Mathlib.Tactic.Linarith.PComp → Mathlib.Tactic.Linarith.PComp → ℕ → Mathlib.Tactic.Linarith.PComp
PComp.add c1 c2 elimVar creates the result of summing the linear comparisons c1 and c2,
during the process of eliminating the variable elimVar.
The computation assumes, but does not enforce, that elimVar appears in both c1 and c2
and does not appear in the sum.
Computing the sum of the two comparisons is easy; the complicated details lie in tracking the
additional fields of PComp.
- The historical set
pcomp.historyofc1 + c2is the union of the two historical sets. varsis the union ofc1.varsandc2.vars.- The effectively eliminated variables of
c1 + c2are the union of the two effective sets, withelim_varinserted. - The implicitly eliminated variables of
c1 + c2are those that appear invarsbut notc.varsoreffective.
(Note that the description of the implicitly eliminated variables of c1 + c2 in the algorithm
described in Section 6 of https://doi.org/10.1016/B978-0-444-88771-9.50019-2 seems to be wrong:
that says it should be (c1.implicit.union' c2.implicit).sdiff explicit.
Since the implicitly eliminated sets start off empty for the assumption,
this formula would leave them always empty.)
def Mathlib.Tactic.Linarith.PComp.assump E1 #
Mathlib.Tactic.Linarith.Comp → ℕ → Mathlib.Tactic.Linarith.PComp
PComp.assump c n creates a PComp whose comparison is c and whose source is
CompSource.assump n, that is, c is derived from the nth hypothesis.
The history is the singleton set {n}.
No variables have been eliminated (effectively or implicitly).
instance Mathlib.Tactic.Linarith.instToFormatPComp E0 #
Std.ToFormat Mathlib.Tactic.Linarith.PComp
instance Mathlib.Tactic.Linarith.instToStringPComp E0 #
ToString Mathlib.Tactic.Linarith.PComp
abbrev Mathlib.Tactic.Linarith.PCompSet E1 #
Type
A collection of comparisons.
def Mathlib.Tactic.Linarith.elimVar E1 #
Mathlib.Tactic.Linarith.Comp → Mathlib.Tactic.Linarith.Comp → ℕ → Option (ℕ × ℕ)
If c1 and c2 both contain variable a with opposite coefficients,
produces v1 and v2 such that a has been cancelled in v1*c1 + v2*c2.
def Mathlib.Tactic.Linarith.pelimVar E1 #
Mathlib.Tactic.Linarith.PComp → Mathlib.Tactic.Linarith.PComp → ℕ → Option Mathlib.Tactic.Linarith.PComp
pelimVar p1 p2 calls elimVar on the Comp components of p1 and p2.
If this returns v1 and v2, it creates a new PComp equal to v1*p1 + v2*p2,
and tracks this in the CompSource.
def Mathlib.Tactic.Linarith.PComp.isContr E1 #
Mathlib.Tactic.Linarith.PComp → Bool
A PComp represents a contradiction if its Comp field represents a contradiction.
def Mathlib.Tactic.Linarith.elimWithSet E1 #
ℕ → Mathlib.Tactic.Linarith.PComp → Mathlib.Tactic.Linarith.PCompSet → Mathlib.Tactic.Linarith.PCompSet
elimWithSet a p comps collects the result of calling pelimVar p p' a
for every p' ∈ comps.
structure Mathlib.Tactic.Linarith.LinarithData E1 #
Type
The state for the elimination monad.
maxVar: the largest variable index that has not been eliminated.comps: a set of comparisons
The elimination procedure proceeds by eliminating variable v from comps progressively
in decreasing order.
ctor Mathlib.Tactic.Linarith.LinarithData.mk E0 #
ℕ → Mathlib.Tactic.Linarith.PCompSet → Mathlib.Tactic.Linarith.LinarithData
abbrev Mathlib.Tactic.Linarith.LinarithData.maxVar E1 #
Mathlib.Tactic.Linarith.LinarithData → ℕ
The largest variable index that has not been (officially) eliminated.
abbrev Mathlib.Tactic.Linarith.LinarithData.comps E1 #
Mathlib.Tactic.Linarith.LinarithData → Mathlib.Tactic.Linarith.PCompSet
The set of comparisons.
abbrev Mathlib.Tactic.Linarith.LinarithM E1 #
Type → Type
The linarith monad extends an exceptional monad with a LinarithData state.
An exception produces a contradictory PComp.
def Mathlib.Tactic.Linarith.getMaxVar E1 #
Mathlib.Tactic.Linarith.LinarithM ℕ
Returns the current max variable.
def Mathlib.Tactic.Linarith.getPCompSet E1 #
Mathlib.Tactic.Linarith.LinarithM Mathlib.Tactic.Linarith.PCompSet
Return the current comparison set.
def Mathlib.Tactic.Linarith.validate E1 #
Mathlib.Tactic.Linarith.LinarithM Unit
Throws an exception if a contradictory PComp is contained in the current state.
def Mathlib.Tactic.Linarith.update E1 #
ℕ → Mathlib.Tactic.Linarith.PCompSet → Mathlib.Tactic.Linarith.LinarithM Unit
Updates the current state with a new max variable and comparisons,
and calls validate to check for a contradiction.
def Mathlib.Tactic.Linarith.splitSetByVarSign E1 #
ℕ →
Mathlib.Tactic.Linarith.PCompSet →
Mathlib.Tactic.Linarith.PCompSet × Mathlib.Tactic.Linarith.PCompSet × Mathlib.Tactic.Linarith.PCompSet
splitSetByVarSign a comps partitions the set comps into three parts.
poscontains the elements ofcompsin whichahas a positive coefficient.negcontains the elements ofcompsin whichahas a negative coefficient.notPresentcontains the elements ofcompsin whichahas coefficient 0.
Returns (pos, neg, notPresent).
def Mathlib.Tactic.Linarith.elimVarM E1 #
ℕ → Mathlib.Tactic.Linarith.LinarithM Unit
elimVarM a performs one round of Fourier-Motzkin elimination, eliminating the variable a
from the linarith state.
def Mathlib.Tactic.Linarith.elimAllVarsM E1 #
Mathlib.Tactic.Linarith.LinarithM Unit
elimAllVarsM eliminates all variables from the linarith state, leaving it with a set of
ground comparisons. If this succeeds without exception, the original linarith state is consistent.
def Mathlib.Tactic.Linarith.mkLinarithData E1 #
List Mathlib.Tactic.Linarith.Comp → ℕ → Mathlib.Tactic.Linarith.LinarithData
mkLinarithData hyps vars takes a list of hypotheses and the largest variable present in
those hypotheses. It produces an initial state for the elimination monad.
def Mathlib.Tactic.Linarith.CertificateOracle.fourierMotzkin E1 #
Mathlib.Tactic.Linarith.CertificateOracle
An oracle that uses Fourier-Motzkin elimination.