Built with doc-gen4, running Lean4. Bubbles () indicate interactive fragments: hover for details, tap to reveal contents. Use Ctrl+↑Ctrl+↓to navigate, Ctrl+🖱️to focus. On Mac, use Cmdinstead of Ctrl.
/-
Copyright (c) 2020 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis
Ported by: Scott Morrison
-/
import Mathlib.Tactic.Linarith.Datatypes

/-!
# The Fourier-Motzkin elimination procedure

The Fourier-Motzkin procedure is a variable elimination method for linear inequalities.


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.
-/

open Std

namespace Linarith

/-!
### Datatypes

The `CompSource` and `PComp` datatypes are specific to the FM elimination routine;
they are not shared with other components of `linarith`.
-/

/--
`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.
 -/
inductive 
CompSource: Type
CompSource
:
Type: Type 1
Type
|
assump: CompSource
assump
:
Nat: Type
Nat
CompSource: Type
CompSource
| add :
CompSource: Type
CompSource
CompSource: Type
CompSource
CompSource: Type
CompSource
|
scale: CompSourceCompSource
scale
:
Nat: Type
Nat
CompSource: Type
CompSource
CompSource: Type
CompSource
deriving
Inhabited: Sort u → Sort (max1u)
Inhabited
/-- 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
CompSource.flatten: CompSourceHashMap
CompSource.flatten
:
CompSource: Type
CompSource
HashMap: (α : Type ?u.939) → Type ?u.938 → [inst : BEq α] → [inst : Hashable α] → Type (max0?u.939?u.938)
HashMap
Nat: Type
Nat
Nat: Type
Nat
| (
CompSource.assump: CompSource
CompSource.assump
n:
n
) =>
HashMap.empty: {α : Type ?u.963} → {β : Type ?u.962} → [inst : BEq α] → [inst_1 : Hashable α] → HashMap α β
HashMap.empty
.
insert: {α : Type ?u.1002} → {x : BEq α} → {x_1 : Hashable α} → {β : Type ?u.1001} → HashMap α βαβHashMap α β
insert
n:
n
1: ?m.1025
1
| (
CompSource.add: CompSourceCompSourceCompSource
CompSource.add
c1 c2) => (
CompSource.flatten: CompSourceHashMap
CompSource.flatten
c1).
mergeWith: {α : Type ?u.1052} → {x : BEq α} → {x_1 : Hashable α} → {β : Type ?u.1051} → (αβββ) → HashMap α βHashMap α βHashMap α β
mergeWith
(fun
_: ?m.1065
_
b: ?m.1068
b
b': ?m.1071
b'
=>
b: ?m.1068
b
+
b': ?m.1071
b'
) (
CompSource.flatten: CompSourceHashMap
CompSource.flatten
c2) | (
CompSource.scale: CompSourceCompSource
CompSource.scale
n:
n
c) => (
CompSource.flatten: CompSourceHashMap
CompSource.flatten
c).
mapVal: {α : Type ?u.1137} → {x : BEq α} → {x_1 : Hashable α} → {β : Type ?u.1136} → {γ : Type ?u.1135} → (αβγ) → HashMap α βHashMap α γ
mapVal
(fun
_: ?m.1151
_
v: ?m.1154
v
=>
v: ?m.1154
v
*
n:
n
) /-- Formats a `CompSource` for printing. -/ def
CompSource.toString: CompSourceString
CompSource.toString
:
CompSource: Type
CompSource
String: Type
String
| (
CompSource.assump: CompSource
CompSource.assump
e:
e
) =>
ToString.toString: {α : Type ?u.3434} → [self : ToString α] → αString
ToString.toString
e:
e
| (
CompSource.add: CompSourceCompSourceCompSource
CompSource.add
c1 c2) =>
CompSource.toString: CompSourceString
CompSource.toString
c1 ++
" + ": String
" + "
++
CompSource.toString: CompSourceString
CompSource.toString
c2 | (
CompSource.scale: CompSourceCompSource
CompSource.scale
n:
n
c) =>
ToString.toString: {α : Type ?u.3557} → [self : ToString α] → αString
ToString.toString
n:
n
++
" * ": String
" * "
++
CompSource.toString: CompSourceString
CompSource.toString
c instance :
ToFormat: Type ?u.4009 → Type ?u.4009
ToFormat
CompSource: Type
CompSource
:= ⟨fun
a: ?m.4017
a
=>
CompSource.toString: CompSourceString
CompSource.toString
a: ?m.4017
a
⟩ /-- 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.vars` contains the variables that appear in any inequality in the historical set. * `PComp.effective` contains the variables that have been effectively eliminated from `PComp`. A variable `n` is said to be *effectively eliminated* in `p : PComp` if the elimination of `n` produced at least one of the ancestors of `p` (or `p` itself). * `PComp.implicit` contains the variables that have been implicitly eliminated from `PComp`. A variable `n` is said to be *implicitly eliminated* in `p` if it satisfies the following properties: - `n` appears in some inequality in the historical set (i.e. in `p.vars`). - `n` does not appear in `p.c.vars` (i.e. it has been eliminated). - `n` was 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. -/ structure
PComp: Type
PComp
:
Type: Type 1
Type
where /-- The comparison `Σ cᵢ*xᵢ R 0`. -/
c: PCompComp
c
:
Comp: Type
Comp
/-- We track how the comparison was constructed by adding and scaling previous comparisons, back to the original assumptions. -/
src: PCompCompSource
src
:
CompSource: Type
CompSource
/-- The set of original assumptions which have been used in constructing this comparison. -/
history: PCompRBSet compare
history
:
RBSet: (α : Type ?u.4133) → (ααOrdering) → Type ?u.4133
RBSet
: Type
Ord.compare: {α : Type ?u.4134} → [self : Ord α] → ααOrdering
Ord.compare
/-- The variables which have been *effectively eliminated*, i.e. the by running the elimination algorithm on that variable. -/
effective: PCompRBSet compare
effective
:
RBSet: (α : Type ?u.4169) → (ααOrdering) → Type ?u.4169
RBSet
: Type
Ord.compare: {α : Type ?u.4170} → [self : Ord α] → ααOrdering
Ord.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.-/
implicit: PCompRBSet compare
implicit
:
RBSet: (α : Type ?u.4193) → (ααOrdering) → Type ?u.4193
RBSet
: Type
Ord.compare: {α : Type ?u.4194} → [self : Ord α] → ααOrdering
Ord.compare
/-- The union of all variables appearing in those original assumptions which appear in the `history` set. -/
vars: PCompRBSet compare
vars
:
RBSet: (α : Type ?u.4217) → (ααOrdering) → Type ?u.4217
RBSet
: Type
Ord.compare: {α : Type ?u.4218} → [self : Ord α] → ααOrdering
Ord.compare
/-- 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
PComp.maybeMinimal: PCompBool
PComp.maybeMinimal
(c :
PComp: Type
PComp
) (
elimedGE:
elimedGE
:
: Type
) :
Bool: Type
Bool
:= c.
history: PCompRBSet compare
history
.
size: {α : Type ?u.5062} → {cmp : ααOrdering} → RBSet α cmp
size
1: ?m.5078
1
+ ((c.
implicit: PCompRBSet compare
implicit
.
filter: {α : Type ?u.5088} → {cmp : ααOrdering} → RBSet α cmp(αBool) → RBSet α cmp
filter
(· ≥
elimedGE:
elimedGE
)).
union: {α : Type ?u.5142} → {cmp : ααOrdering} → RBSet α cmpRBSet α cmpRBSet α cmp
union
c.
effective: PCompRBSet compare
effective
).
size: {α : Type ?u.5151} → {cmp : ααOrdering} → RBSet α cmp
size
/-- The `src : CompSource` field is ignored when comparing `PComp`s. Two `PComp`s proving the same comparison, with different sources, are considered equivalent. -/ def
PComp.cmp: PCompPCompOrdering
PComp.cmp
(
p1: PComp
p1
p2: PComp
p2
:
PComp: Type
PComp
) :
Ordering: Type
Ordering
:=
p1: PComp
p1
.
c: PCompComp
c
.
cmp: CompCompOrdering
cmp
p2: PComp
p2
.
c: PCompComp
c
/-- `PComp.scale c n` scales the coefficients of `c` by `n` and notes this in the `CompSource`. -/ def
PComp.scale: PCompPComp
PComp.scale
(c :
PComp: Type
PComp
) (
n:
n
:
: Type
) :
PComp: Type
PComp
:= { c with c := c.
c: PCompComp
c
.
scale: CompComp
scale
n:
n
, src := c.
src: PCompCompSource
src
.
scale: CompSourceCompSource
scale
n:
n
} /-- `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.history` of `c1 + c2` is the union of the two historical sets. * `vars` is the union of `c1.vars` and `c2.vars`. * The effectively eliminated variables of `c1 + c2` are the union of the two effective sets, with `elim_var` inserted. * The implicitly eliminated variables of `c1 + c2` are those that appear in `vars` but not `c.vars` or `effective`. (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
PComp.add: PCompPCompPComp
PComp.add
(
c1: PComp
c1
c2: PComp
c2
:
PComp: Type
PComp
) (
elimVar:
elimVar
:
: Type
) :
PComp: Type
PComp
:= let
c: ?m.5617
c
:=
c1: PComp
c1
.
c: PCompComp
c
.
add: CompCompComp
add
c2: PComp
c2
.
c: PCompComp
c
let
src: ?m.5622
src
:=
c1: PComp
c1
.
src: PCompCompSource
src
.add
c2: PComp
c2
.
src: PCompCompSource
src
let
history: ?m.5627
history
:=
c1: PComp
c1
.
history: PCompRBSet compare
history
.
union: {α : Type ?u.5628} → {cmp : ααOrdering} → RBSet α cmpRBSet α cmpRBSet α cmp
union
c2: PComp
c2
.
history: PCompRBSet compare
history
let
vars: ?m.5643
vars
:=
c1: PComp
c1
.
vars: PCompRBSet compare
vars
.
union: {α : Type ?u.5644} → {cmp : ααOrdering} → RBSet α cmpRBSet α cmpRBSet α cmp
union
c2: PComp
c2
.
vars: PCompRBSet compare
vars
let
effective: ?m.5655
effective
:= (
c1: PComp
c1
.
effective: PCompRBSet compare
effective
.
union: {α : Type ?u.5656} → {cmp : ααOrdering} → RBSet α cmpRBSet α cmpRBSet α cmp
union
c2: PComp
c2
.
effective: PCompRBSet compare
effective
).
insert: {α : Type ?u.5665} → {cmp : ααOrdering} → RBSet α cmpαRBSet α cmp
insert
elimVar:
elimVar
let
implicit: ?m.5676
implicit
:= (
vars: ?m.5643
vars
.
sdiff: {α : Type ?u.5677} → {cmp : ααOrdering} → RBSet α cmpRBSet α cmpRBSet α cmp
sdiff
(
.ofList: {α : Type ?u.5686} → List α(cmp : ααOrdering) → RBSet α cmp
.ofList
c: ?m.5617
c
.
vars: CompList
vars
_: ?m.5687?m.5687Ordering
_
)).
sdiff: {α : Type ?u.5694} → {cmp : ααOrdering} → RBSet α cmpRBSet α cmpRBSet α cmp
sdiff
effective: ?m.5655
effective
c: ?m.5617
c
,
src: ?m.5622
src
,
history: ?m.5627
history
,
effective: ?m.5655
effective
,
implicit: ?m.5676
implicit
,
vars: ?m.5643
vars
⟩ /-- `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 `n`th hypothesis. The history is the singleton set `{n}`. No variables have been eliminated (effectively or implicitly). -/ def
PComp.assump: CompPComp
PComp.assump
(
c: Comp
c
:
Comp: Type
Comp
) (
n:
n
:
: Type
) :
PComp: Type
PComp
where c :=
c: Comp
c
src :=
CompSource.assump: CompSource
CompSource.assump
n:
n
history :=
RBSet.empty: {α : Type ?u.6357} → {cmp : ααOrdering} → RBSet α cmp
RBSet.empty
.
insert: {α : Type ?u.6360} → {cmp : ααOrdering} → RBSet α cmpαRBSet α cmp
insert
n:
n
effective :=
.empty: {α : Type ?u.6379} → {cmp : ααOrdering} → RBSet α cmp
.empty
implicit :=
.empty: {α : Type ?u.6384} → {cmp : ααOrdering} → RBSet α cmp
.empty
vars :=
.ofList: {α : Type ?u.6389} → List α(cmp : ααOrdering) → RBSet α cmp
.ofList
c: Comp
c
.
vars: CompList
vars
_: ?m.6390?m.6390Ordering
_
instance: ToFormat PComp
instance
:
ToFormat: Type ?u.6499 → Type ?u.6499
ToFormat
PComp: Type
PComp
:= ⟨fun
p: ?m.6507
p
=>
format: {α : Type ?u.6515} → [self : ToFormat α] → αFormat
format
p: ?m.6507
p
.
c: PCompComp
c
.
coeffs: CompLinexp
coeffs
++
toString: {α : Type ?u.6542} → [self : ToString α] → αString
toString
p: ?m.6507
p
.
c: PCompComp
c
.
str: CompIneq
str
++
"0": String
"0"
instance: ToString PComp
instance
:
ToString: Type ?u.7044 → Type ?u.7044
ToString
PComp: Type
PComp
:= ⟨fun
p: ?m.7052
p
=>
toString: {α : Type ?u.7060} → [self : ToString α] → αString
toString
p: ?m.7052
p
.
c: PCompComp
c
.
coeffs: CompLinexp
coeffs
++
toString: {α : Type ?u.7077} → [self : ToString α] → αString
toString
p: ?m.7052
p
.
c: PCompComp
c
.
str: CompIneq
str
++
"0": String
"0"
⟩ /-- A collection of comparisons. -/ abbrev
PCompSet: Type
PCompSet
:=
RBSet: (α : Type ?u.7298) → (ααOrdering) → Type ?u.7298
RBSet
PComp: Type
PComp
PComp.cmp: PCompPCompOrdering
PComp.cmp
/-! ### Elimination procedure -/ /-- 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
elimVar: CompCompOption ( × )
elimVar
(
c1: Comp
c1
c2: Comp
c2
:
Comp: Type
Comp
) (
a:
a
:
: Type
) :
Option: Type ?u.7308 → Type ?u.7308
Option
(
: Type
×
: Type
) := let
v1: ?m.7316
v1
:=
c1: Comp
c1
.
coeffOf: Comp
coeffOf
a:
a
let
v2: ?m.7321
v2
:=
c2: Comp
c2
.
coeffOf: Comp
coeffOf
a:
a
if
v1: ?m.7316
v1
*
v2: ?m.7321
v2
<
0: ?m.7330
0
then let
vlcm: ?m.7419
vlcm
:=
Nat.lcm:
Nat.lcm
v1: ?m.7316
v1
.
natAbs:
natAbs
v2: ?m.7321
v2
.
natAbs:
natAbs
some: {α : Type ?u.7423} → αOption α
some
vlcm: ?m.7419
vlcm
/
v1: ?m.7316
v1
.
natAbs:
natAbs
,
vlcm: ?m.7419
vlcm
/
v2: ?m.7321
v2
.
natAbs:
natAbs
else
none: {α : Type ?u.7524} → Option α
none
/-- `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
pelimVar: PCompPCompOption PComp
pelimVar
(
p1: PComp
p1
p2: PComp
p2
:
PComp: Type
PComp
) (
a:
a
:
: Type
) :
Option: Type ?u.7704 → Type ?u.7704
Option
PComp: Type
PComp
:= do let (
n1:
n1
,
n2:
n2
) ←
elimVar: CompCompOption ( × )
elimVar
p1: PComp
p1
.
c: PCompComp
c
p2: PComp
p2
.
c: PCompComp
c
a:
a
return (
p1: PComp
p1
.
scale: PCompPComp
scale
n1:
n1
).
add: PCompPCompPComp
add
(
p2: PComp
p2
.
scale: PCompPComp
scale
n2:
n2
)
a:
a
/-- A `PComp` represents a contradiction if its `Comp` field represents a contradiction. -/ def
PComp.isContr: PCompBool
PComp.isContr
(p :
PComp: Type
PComp
) :
Bool: Type
Bool
:= p.
c: PCompComp
c
.
isContr: CompBool
isContr
/-- `elimWithSet a p comps` collects the result of calling `pelimVar p p' a` for every `p' ∈ comps`. -/ def
elimWithSet: PCompPCompSetPCompSet
elimWithSet
(
a:
a
:
: Type
) (p :
PComp: Type
PComp
) (
comps: PCompSet
comps
:
PCompSet: Type
PCompSet
) :
PCompSet: Type
PCompSet
:=
comps: PCompSet
comps
.
foldl: {σ : Sort ?u.8258} → {α : Type ?u.8257} → {cmp : ααOrdering} → (σασ) → σRBSet α cmpσ
foldl
(fun
s: ?m.8269
s
pc: ?m.8272
pc
=> match
pelimVar: PCompPCompOption PComp
pelimVar
p
pc: ?m.8272
pc
a:
a
with |
some: {α : Type ?u.8275} → αOption α
some
pc: PComp
pc
=> if
pc: PComp
pc
.
maybeMinimal: PCompBool
maybeMinimal
a:
a
then
s: ?m.8269
s
.
insert: {α : Type ?u.8384} → {cmp : ααOrdering} → RBSet α cmpαRBSet α cmp
insert
pc: PComp
pc
else
s: ?m.8269
s
|
none: {α : Type ?u.8398} → Option α
none
=>
s: ?m.8269
s
)
RBSet.empty: {α : Type ?u.8471} → {cmp : ααOrdering} → RBSet α cmp
RBSet.empty
/-- 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. -/ structure
LinarithData: Type
LinarithData
:
Type: Type 1
Type
where /-- The largest variable index that has not been (officially) eliminated. -/
maxVar: LinarithData
maxVar
:
: Type
/-- The set of comparisions. -/ comps :
PCompSet: Type
PCompSet
/-- The linarith monad extends an exceptional monad with a `LinarithData` state. An exception produces a contradictory `PComp`. -/ @[reducible] def
LinarithM: TypeType
LinarithM
:
Type: Type 1
Type
Type: Type 1
Type
:=
StateT: Type ?u.10445 → (Type ?u.10445 → Type ?u.10444) → Type ?u.10445 → Type (max?u.10445?u.10444)
StateT
LinarithData: Type
LinarithData
(
ExceptT: Type ?u.10449 → (Type ?u.10449 → Type ?u.10448) → Type ?u.10449 → Type ?u.10448
ExceptT
PComp: Type
PComp
Id: Type ?u.10452 → Type ?u.10452
Id
) /-- Returns the current max variable. -/ def
getMaxVar: LinarithM
getMaxVar
:
LinarithM: TypeType
LinarithM
: Type
:=
LinarithData.maxVar: LinarithData
LinarithData.maxVar
<$>
get: {σ : outParam (Type ?u.10485)} → {m : Type ?u.10485 → Type ?u.10484} → [self : MonadState σ m] → m σ
get
/-- Return the current comparison set. -/ def
getPCompSet: LinarithM PCompSet
getPCompSet
:
LinarithM: TypeType
LinarithM
PCompSet: Type
PCompSet
:=
LinarithData.comps: LinarithDataPCompSet
LinarithData.comps
<$>
get: {σ : outParam (Type ?u.11133)} → {m : Type ?u.11133 → Type ?u.11132} → [self : MonadState σ m] → m σ
get
/-- Throws an exception if a contradictory `PComp` is contained in the current state. -/ def
validate: LinarithM Unit
validate
:
LinarithM: TypeType
LinarithM
Unit: Type
Unit
:= do match
(← getPCompSet): ?m.11868
(←
getPCompSet: LinarithM PCompSet
getPCompSet
(← getPCompSet): ?m.11868
)
.
toList: {α : Type ?u.11870} → {cmp : ααOrdering} → RBSet α cmpList α
toList
.
find?: {α : Type ?u.11879} → (αBool) → List αOption α
find?
(fun p :
PComp: Type
PComp
=> p.
isContr: PCompBool
isContr
) with |
none: {α : Type ?u.11892} → Option α
none
=> return
(): Unit
()
|
some: {α : Type ?u.11820} → αOption α
some
c =>
throw: {ε : outParam (Type ?u.11991)} → {m : Type ?u.11990 → Type ?u.11989} → [self : MonadExcept ε m] → {α : Type ?u.11990} → εm α
throw
c /-- Updates the current state with a new max variable and comparisons, and calls `validate` to check for a contradiction. -/ def
update: PCompSetLinarithM Unit
update
(
maxVar:
maxVar
:
: Type
) (
comps: PCompSet
comps
:
PCompSet: Type
PCompSet
) :
LinarithM: TypeType
LinarithM
Unit: Type
Unit
:= do
StateT.set: {σ : Type ?u.13123} → {m : Type ?u.13123 → Type ?u.13122} → [inst : Monad m] → σStateT σ m PUnit
StateT.set
maxVar:
maxVar
,
comps: PCompSet
comps
validate: LinarithM Unit
validate
/-- `splitSetByVarSign a comps` partitions the set `comps` into three parts. * `pos` contains the elements of `comps` in which `a` has a positive coefficient. * `neg` contains the elements of `comps` in which `a` has a negative coefficient. * `notPresent` contains the elements of `comps` in which `a` has coefficient 0. Returns `(pos, neg, notPresent)`. -/ def
splitSetByVarSign: PCompSetPCompSet × PCompSet × PCompSet
splitSetByVarSign
(
a:
a
:
: Type
) (
comps: PCompSet
comps
:
PCompSet: Type
PCompSet
) :
PCompSet: Type
PCompSet
×
PCompSet: Type
PCompSet
×
PCompSet: Type
PCompSet
:=
comps: PCompSet
comps
.
foldl: {σ : Sort ?u.13483} → {α : Type ?u.13482} → {cmp : ααOrdering} → (σασ) → σRBSet α cmpσ
foldl
(funpos, neg,
notPresent: PCompSet
notPresent
pc: ?m.13497
pc
=> let
n: ?m.13537
n
:=
pc: ?m.13497
pc
.
c: PCompComp
c
.
coeffOf: Comp
coeffOf
a:
a
if
n: ?m.13537
n
>
0: ?m.13542
0
thenpos.
insert: {α : Type ?u.13621} → {cmp : ααOrdering} → RBSet α cmpαRBSet α cmp
insert
pc: ?m.13497
pc
, neg,
notPresent: PCompSet
notPresent
else if
n: ?m.13537
n
<
0: ?m.13644
0
thenpos, neg.
insert: {α : Type ?u.13730} → {cmp : ααOrdering} → RBSet α cmpαRBSet α cmp
insert
pc: ?m.13497
pc
,
notPresent: PCompSet
notPresent
elsepos, neg,
notPresent: PCompSet
notPresent
.
insert: {α : Type ?u.13755} → {cmp : ααOrdering} → RBSet α cmpαRBSet α cmp
insert
pc: ?m.13497
pc
⟩) ⟨
RBSet.empty: {α : Type ?u.13999} → {cmp : ααOrdering} → RBSet α cmp
RBSet.empty
,
RBSet.empty: {α : Type ?u.14016} → {cmp : ααOrdering} → RBSet α cmp
RBSet.empty
,
RBSet.empty: {α : Type ?u.14021} → {cmp : ααOrdering} → RBSet α cmp
RBSet.empty
⟩ /-- `elimVarM a` performs one round of Fourier-Motzkin elimination, eliminating the variable `a` from the `linarith` state. -/ def
elimVarM: LinarithM Unit
elimVarM
(
a:
a
:
: Type
) :
LinarithM: TypeType
LinarithM
Unit: Type
Unit
:= do let
vs: ?m.15604
vs
getMaxVar: LinarithM
getMaxVar
if (
a:
a
vs: ?m.15604
vs
) then (do letpos, neg,
notPresent: PCompSet
notPresent
⟩ :=
splitSetByVarSign: PCompSetPCompSet × PCompSet × PCompSet
splitSetByVarSign
a:
a
(← getPCompSet): ?m.15676
(←
getPCompSet: LinarithM PCompSet
getPCompSet
(← getPCompSet): ?m.15676
)
update: PCompSetLinarithM Unit
update
(
vs: ?m.15604
vs
-
1: ?m.15721
1
) (pos.
foldl: {σ : Sort ?u.15776} → {α : Type ?u.15775} → {cmp : ααOrdering} → (σασ) → σRBSet α cmpσ
foldl
(fun
s: ?m.15787
s
p: ?m.15790
p
=>
s: ?m.15787
s
.
union: {α : Type ?u.15792} → {cmp : ααOrdering} → RBSet α cmpRBSet α cmpRBSet α cmp
union
(
elimWithSet: PCompPCompSetPCompSet
elimWithSet
a:
a
p: ?m.15790
p
neg))
notPresent: PCompSet
notPresent
)) else
pure: {f : Type ?u.15910 → Type ?u.15909} → [self : Pure f] → {α : Type ?u.15910} → αf α
pure
(): 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
elimAllVarsM: LinarithM Unit
elimAllVarsM
:
LinarithM: TypeType
LinarithM
Unit: Type
Unit
:= do for
i: ?m.17475
i
in (
List.range: List
List.range
((←
getMaxVar: LinarithM
getMaxVar
) +
1: ?m.17353
1
)).
reverse: {α : Type ?u.17405} → List αList α
reverse
do
elimVarM: LinarithM Unit
elimVarM
i: ?m.17475
i
/-- `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
mkLinarithData: List CompLinarithData
mkLinarithData
(
hyps: List Comp
hyps
:
List: Type ?u.19152 → Type ?u.19152
List
Comp: Type
Comp
) (
maxVar:
maxVar
:
: Type
) :
LinarithData: Type
LinarithData
:= ⟨
maxVar:
maxVar
,
.ofList: {α : Type ?u.19162} → List α(cmp : ααOrdering) → RBSet α cmp
.ofList
(
hyps: List Comp
hyps
.
enum: {α : Type ?u.19164} → List αList ( × α)
enum
.
map: {α : Type ?u.19168} → {β : Type ?u.19167} → (αβ) → List αList β
map
$ fun
n:
n
,
cmp: Comp
cmp
⟩ =>
PComp.assump: CompPComp
PComp.assump
cmp: Comp
cmp
n:
n
)
_: ?m.19163?m.19163Ordering
_
⟩ /-- `produceCertificate hyps vars` tries to derive a contradiction from the comparisons in `hyps` by eliminating all variables ≤ `maxVar`. If successful, it returns a map `coeff : ℕ → ℕ` as a certificate. This map represents that we can find a contradiction by taking the sum `∑ (coeff i) * hyps[i]`. -/ def
FourierMotzkin.produceCertificate: CertificateOracle
FourierMotzkin.produceCertificate
:
CertificateOracle: Type
CertificateOracle
:= fun
hyps: ?m.19652
hyps
maxVar: ?m.19655
maxVar
=> match
ExceptT.run: {ε : Type ?u.19658} → {m : Type ?u.19658 → Type ?u.19657} → {α : Type ?u.19658} → ExceptT ε m αm (Except ε α)
ExceptT.run
(
StateT.run: {σ : Type ?u.19663} → {m : Type ?u.19663 → Type ?u.19662} → {α : Type ?u.19663} → StateT σ m ασm (α × σ)
StateT.run
(do
validate: LinarithM Unit
validate
;
elimAllVarsM: LinarithM Unit
elimAllVarsM
:
LinarithM: TypeType
LinarithM
Unit: Type
Unit
) (
mkLinarithData: List CompLinarithData
mkLinarithData
hyps: ?m.19652
hyps
maxVar: ?m.19655
maxVar
)) with | (
Except.ok: {ε : Type ?u.19774} → {α : Type ?u.19773} → αExcept ε α
Except.ok
_) =>
failure: {f : Type ?u.19795 → Type ?u.19794} → [self : Alternative f] → {α : Type ?u.19795} → f α
failure
| (
Except.error: {ε : Type ?u.19817} → {α : Type ?u.19816} → εExcept ε α
Except.error
contr: PComp
contr
) => return
contr: PComp
contr
.
src: PCompCompSource
src
.
flatten: CompSourceHashMap
flatten
end Linarith