order
tactic #
This module defines the order
tactic, a decision procedure for the theories of Preorder
,
PartialOrder
, and LinearOrder
.
Implementation Details #
Below, we describe the algorithm for each type of order. All algorithms begin with two steps:
- Negate the goal so that our goal now is to derive
False
. - Collect the set of facts, i.e., atomic expressions in one of six forms:
x = y
,x ≠ y
,x ≤ y
,¬(x ≤ y)
,x < y
, and¬(x < y)
. We then attempt to derive a contradiction from this set of facts.
Preorder #
- Preprocessing. We replace some facts as follows:
- Replace
x < y
with two equivalent facts:x ≤ y
and¬(y ≤ x)
. - Replace
x = y
withx ≤ y
andy ≤ x
. - Remove
x ≠ y
. Note that the last two operations weaken the set of facts.
- Building the
≤
-graph. We construct a graph where vertices correspond to atoms, and an edge(x, y)
exists if the factx ≤ y
is present in our set of facts. We call this graph a≤
-graph. - Growing the
≤
-graph with≮
-facts. In preorders,¬(x < y)
is equivalent to(x ≤ y) → (y ≤ x)
. Thus, ify
is reachable fromx
in the≤
-graph, we can derive the new facty ≤ x
. At this step, we add such edges to the graph while possible. - Finding contradictions using
≰
-facts. For each fact¬(x ≤ y)
, we check ify
is reachable fromx
in the≤
-graph. If so, we derive the desired contradiction.
Why is this a decision procedure? #
Technically, it is not, because it cannot prove (x = y) → (y ≠ z) → (x ≠ z)
. Goals involving
only =
and ≠
can be handled by the cc
tactic. Assume, then, that a set T
of facts is
contradictory, but there is no chain x₁ = x₂ = ... = xₖ
in T
along with the fact x₁ ≠ xₖ
. Then
we claim that the described algorithm is able to deduce a contradiction from T
. Let T'
be the
set of facts after preprocessing. Then T'
remains contradictory.
Indeed, suppose that T'
is satisfiable, i.e., there exists a model M
that satisfies T'
.
Consider a quotient M'
of M
by the equivalence relation ~
, where a ~ b
holds for a ≠ b
iff
both a
and b
are values of some variables x
and y
from T
, and there is
a chain x = ... = y
in T
. Define the relation R'
on M'
as α R' β
if and only if a R b
in M
for some a ∈ α
and b ∈ β
. Then M'
is a model satisfying T
:
- For any fact
x = y
inT
, we haveM'(x) = M'(y)
inM'
. - For any fact
x ≠ y
inT
, we haveM'(x) ≠ M'(y)
, since otherwise, there would exist a chainx = ... = y
inT
. - For any fact
x ≤ y
inT
, and thus inT'
, we haveM(x) R M(y)
, soM'(x) R' M'(y)
. - For any fact
¬(x ≤ y)
inT
, and thus inT'
, we have¬M(x) R M(y)
. Then, for anyx' ~ x
andy' ~ y
, we can deducex ≤ x'
andy' ≤ y
fromT'
. IfM(x') R M(y')
, thenM(x) R M(x') R M(y') R M(y)
, which contradicts the assumption thatM
is a model ofT'
. This contradiction implies that¬M'(x) R' M'(y)
, as required.
If, at step 6, no contradictory ≰
-facts were found, we must show that a model satisfies T'
.
A suitable model can be constructed using the connected components of the =
-graph (defined
similarly to the ≤
-graph),
with the relation R
defined as C₁ R C₂
iff C₂
is reachable from C₁
in the ≤
-graph. Each
variable x
is interpreted as its component [x]
. This forms a preorder, and we verify that each
fact in T'
is satisfied:
x = y
is satisfied becausex
andy
must be in the same component in the=
-graph.x ≤ y
is satisfied by the construction of the≤
-graph.x ≠ y
is satisfied because otherwise,x
andy
would belong to the same component in the=
-graph, contradicting our initial assumption.¬(x < y)
is satisfied because otherwise¬[y] R [x]
, meaning there is a path fromx
toy
, which would have caused an edge(y, x)
to be added at step 5, leading to a contradiction.
Partial Order #
- Preprocessing. We replace some facts as follows:
- Replace
x < y
withx ≤ y
andx ≠ y
. - Replace
x = y
withx ≤ y
andy ≤ x
. - Replace
¬(x ≤ y)
withx ≠ y
and¬(x < y)
.
- Building the
≤
-graph: Same as for preorders. - Growing the
≤
-graph with≮
-facts: Same as for preorders. - Finding contradictions using
≠
-facts. We identify strongly connected components in the≤
-graph using a standard algorithm. For each factx ≠ y
, we check whetherx
andy
belong to the same component. If they do, thenx = y
is provable, contradictingx ≠ y
.
Why is this a decision procedure? #
Assume that a set T
of facts is contradictory. We must show that the described algorithm can
derive a contradiction. Let T'
be the set of facts after preprocessing. By construction, T'
is
also contradictory (they are equisatisfiable). If, at step 6, no contradictory ≠
-facts were found,
we must show that a model satisfies T'
. A suitable model consists of the strongly connected
components of the ≤
-graph, with the relation R
defined as C₁ R C₂
iff C₂
is reachable
from C₁
. Each variable x
is interpreted as its component [x]
. This forms a partial order, and
we verify that each fact in T'
is satisfied:
x ≤ y
is satisfied because it directly implies[x] R [y]
.x ≠ y
is satisfied because otherwise,x
andy
would belong to the same component, leading to a contradiction at step 6.¬(x < y)
is satisfied because otherwise[x] ≠ [y]
and there is a path fromx
toy
, which would have merged them into the same component at step 5.
Linear Order #
- Preprocessing. We replace some facts as follows:
- Replace
x < y
withx ≤ y
andx ≠ y
. - Replace
x = y
withx ≤ y
andy ≤ x
. - Replace
¬(x ≤ y)
withx ≠ y
andy ≤ x
. - Replace
¬(x < y)
withy ≤ x
.
- Building the
≤
-graph: Same as for preorders. - Finding contradictions using
≠
-facts: Same as for partial orders.
Note that the algorithm for linear orders is simply the algorithm for partial orders with an
additional preprocessing step. It also skips the growing step because there is no ≮
-facts.
Why is this a decision procedure? #
We need to slightly modify the proof for partial orders. In this case, T
and T'
are again
equisatisfiable. Suppose the algorithm cannot find a contradiction, and construct the model of T'
.
The carrier of the model is once again the set of strongly connected components in the ≤
-graph,
with variables interpreted as their respective components. Note that the reachability relation
(used before) on components is acyclic. Therefore, it can be
topologically ordered, meaning it forms a
linear order where C₁ R C₂
whenever C₂
is reachable from C₁
. It is easy to see that all facts
in T'
are satisfied by the model.
Finds a contradictory ≠
-fact whose .lhs
and .rhs
belong to the same strongly connected
component in the ≤
-graph, implying they must be equal, and then uses it to derive False
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Using the ≤
-graph g
, find a contradiction with some ≰
-fact.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Each fact ¬ (x < y)
allows to add the edge (x, y)
when y
is reachable from x
in the
graph. We repeat adding edges using this until no more edges can be added.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
Find the "best" instance of an order on a given type. A linear order is preferred over a partial order, and a partial order is preferred over a preorder.
Equations
- One or more equations did not get rendered due to their size.
Instances For
A finishing tactic for solving goals in arbitrary Preorder
, PartialOrder
,
or LinearOrder
.
Equations
- Mathlib.Tactic.Order.tacticOrder = Lean.ParserDescr.node `Mathlib.Tactic.Order.tacticOrder 1024 (Lean.ParserDescr.nonReservedSymbol "order" false)