Add lean source and binary files (part 42)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- lean/OAI/Combinatorics/ContingencyTables/Machines/ListSubtractProgram.lean +88 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/ListWordProgram.lean +88 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/LiteralProgram.lean +144 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/LowerCapacityProgram.lean +111 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/MatrixCodeEquality.lean +45 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/MatrixEncoding.lean +63 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/MatrixSubtractProgram.lean +99 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/MaximalListProgram.lean +78 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/MedianFractionProgram.lean +248 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/NatListEquality.lean +74 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/NaturalBlockProgram.lean +95 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/NetworkCapacityProgram.lean +233 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/NetworkFeasibilityProgram.lean +106 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/NetworkTableProgram.lean +174 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/OffsetCountProgram.lean +87 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/OnlineIntegerRetry.lean +183 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/OnlinePartialCall.lean +108 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/OnlineRetryPrefix.lean +94 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/OnlineRetryRuntime.lean +58 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/PackedMatrixInjective.lean +39 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/PairedCatalogProgram.lean +147 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/PairedDemandProgram.lean +213 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/PairedFactorialProgram.lean +131 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/PairedProfileProgram.lean +163 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/PowerApproximationProgram.lean +72 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/PreparedDenseCount.lean +76 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/PreparedOffsetCount.lean +119 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/PreparedTransitionCache.lean +80 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/PublicInputLength.lean +77 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/QuadraticPrefixRuntime.lean +84 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/RationalIntervalProgram.lean +97 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/RepeatedFractionProgram.lean +89 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/ResidualDenseProgram.lean +49 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/ResidualGraphProgram.lean +205 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/ResidualWeightProgram.lean +57 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/RetryNumeratorProgram.lean +121 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/RoundedPowerProgram.lean +133 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/ScalarScheduleBounds.lean +46 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/ScalarScheduleProgram.lean +82 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/ScaledWordProgram.lean +43 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/ScheduledCountInput.lean +233 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/ScheduledCountSemantics.lean +151 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/ScheduledResidualLaw.lean +68 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/ScheduledResidualProgram.lean +112 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/ScheduledScalarBounds.lean +137 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/SignedFloorProgram.lean +148 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/SignedPairProgram.lean +133 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/SparseViewMatrixProgram.lean +73 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/TableIndexProgram.lean +73 -0
- lean/OAI/Combinatorics/ContingencyTables/Machines/TightenedInputProgram.lean +228 -0
lean/OAI/Combinatorics/ContingencyTables/Machines/ListSubtractProgram.lean
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Transport.BoundedCellShift
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Machines.NetworkFeasibilityProgram
|
| 3 |
+
|
| 4 |
+
namespace OAI
|
| 5 |
+
|
| 6 |
+
/-! # Binary subtraction at one list coordinate
|
| 7 |
+
|
| 8 |
+
The scan depends on the stored list length. The subtracted amount remains
|
| 9 |
+
binary throughout and never controls a loop.
|
| 10 |
+
-/
|
| 11 |
+
|
| 12 |
+
namespace ContingencyTables.ListSubtractProgram
|
| 13 |
+
|
| 14 |
+
open MatchingFPRAS.TreeTyped Polynomial
|
| 15 |
+
|
| 16 |
+
abbrev Input := (List ℕ×ℕ)×ℕ
|
| 17 |
+
abbrev Cell := Input×ℕ
|
| 18 |
+
|
| 19 |
+
def entry (z : Cell) : ℕ :=
|
| 20 |
+
z.1.1.1.getD z.2 0-if z.2=z.1.1.2 then z.1.2 else 0
|
| 21 |
+
|
| 22 |
+
def entryRealizer : Realizer entry := by
|
| 23 |
+
let read : Realizer (fun z : Cell=>z.1.1.1.getD z.2 0) :=
|
| 24 |
+
composition (pair (composition (composition first first) first) second) (getDFast 0)
|
| 25 |
+
let amount : Realizer (fun z : Cell=>if z.2=z.1.1.2 then z.1.2 else 0) :=
|
| 26 |
+
(choose (composition (pair second (composition (composition first first) second)) natEqual)
|
| 27 |
+
(composition first second) (constant 0)).congr (by
|
| 28 |
+
intro z
|
| 29 |
+
simp only [Function.comp_apply,decide_eq_true_eq])
|
| 30 |
+
exact composition (pair read amount) subtract
|
| 31 |
+
|
| 32 |
+
lemma polynomial_entry : PolynomialTime entryRealizer := by
|
| 33 |
+
unfold entryRealizer
|
| 34 |
+
apply polynomial_composition
|
| 35 |
+
· apply polynomial_pair
|
| 36 |
+
· exact polynomial_composition (polynomial_pair
|
| 37 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 38 |
+
polynomial_first) polynomial_second) (polynomial_getDFast 0)
|
| 39 |
+
· exact polynomial_congr _ (polynomial_choose
|
| 40 |
+
(polynomial_composition (polynomial_pair polynomial_second
|
| 41 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 42 |
+
polynomial_second)) polynomial_natEqual)
|
| 43 |
+
(polynomial_composition polynomial_first polynomial_second) (polynomial_constant 0))
|
| 44 |
+
· exact polynomial_subtract
|
| 45 |
+
|
| 46 |
+
def indices (z : Input) : List ℕ := List.range z.1.1.length
|
| 47 |
+
|
| 48 |
+
def indicesRealizer : Realizer indices :=
|
| 49 |
+
composition (composition (composition first first) listLength) range
|
| 50 |
+
|
| 51 |
+
lemma polynomial_indices : PolynomialTime indicesRealizer := by
|
| 52 |
+
apply polynomial_boundedRange
|
| 53 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 54 |
+
polynomial_listLength) X
|
| 55 |
+
intro z
|
| 56 |
+
simpa only [Function.comp_apply,eval_X] using
|
| 57 |
+
(list_length_le_weight z.1.1).trans ((weight_fst z.1).trans (weight_fst z))
|
| 58 |
+
|
| 59 |
+
def subtractAt (z : Input) : List ℕ :=
|
| 60 |
+
(indices z).map (fun i=>entry (z,i))
|
| 61 |
+
|
| 62 |
+
def subtractAtRealizer : Realizer subtractAt :=
|
| 63 |
+
composition (pair identity indicesRealizer) (mapWith entryRealizer)
|
| 64 |
+
|
| 65 |
+
lemma polynomial_subtractAt : PolynomialTime subtractAtRealizer :=
|
| 66 |
+
polynomial_composition (polynomial_pair polynomial_identity polynomial_indices)
|
| 67 |
+
(polynomial_mapWith polynomial_entry)
|
| 68 |
+
|
| 69 |
+
lemma subtractAt_length (xs : List ℕ) (i t : ℕ) :
|
| 70 |
+
(subtractAt ((xs,i),t)).length=xs.length := by
|
| 71 |
+
simp [subtractAt,indices]
|
| 72 |
+
|
| 73 |
+
lemma subtractAt_ofFn {m : ℕ} (x : Fin m→ℕ) (i : Fin m) (t : ℕ) :
|
| 74 |
+
subtractAt ((List.ofFn x,i.val),t)=List.ofFn (BoundedCellShift.margin x i t) := by
|
| 75 |
+
simp only [subtractAt,indices,List.length_ofFn,DirectedBFSCode.range_eq_map,←List.ofFn_eq_map,
|
| 76 |
+
List.map_ofFn]
|
| 77 |
+
apply congrArg List.ofFn
|
| 78 |
+
funext j
|
| 79 |
+
simp only [entry,List.getD_eq_getElem?_getD,List.getElem?_ofFn,BoundedCellShift.margin]
|
| 80 |
+
by_cases h : j=i
|
| 81 |
+
· subst j
|
| 82 |
+
simp
|
| 83 |
+
· have hn : j.val≠i.val := fun he=>h (Fin.ext he)
|
| 84 |
+
simp [h,hn]
|
| 85 |
+
|
| 86 |
+
end ContingencyTables.ListSubtractProgram
|
| 87 |
+
|
| 88 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/ListWordProgram.lean
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Transport.ListBitProposal
|
| 2 |
+
import OAI.Combinatorics.MatchingCount.Complexity.TreeChoiceCosts
|
| 3 |
+
import OAI.Combinatorics.MatchingCount.Machines.TreeSum
|
| 4 |
+
|
| 5 |
+
namespace OAI
|
| 6 |
+
|
| 7 |
+
/-! # Binary execution of the finite-list proposal
|
| 8 |
+
|
| 9 |
+
The list is scanned only up to its own length. A binary index beyond the
|
| 10 |
+
end produces `none`; its numerical magnitude never determines a loop bound.
|
| 11 |
+
-/
|
| 12 |
+
|
| 13 |
+
namespace ContingencyTables.ProfilePrograms
|
| 14 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped
|
| 15 |
+
open scoped BigOperators
|
| 16 |
+
|
| 17 |
+
variable {A : Type} [Codec A] [Inhabited A]
|
| 18 |
+
|
| 19 |
+
def getOptionFast : Realizer (fun x : List A×ℕ => x.1[x.2]?) :=
|
| 20 |
+
(choose (composition (pair second (composition first listLength)) less)
|
| 21 |
+
(composition (getDFast default) TreeTyped.some) (constant none)).congr (by
|
| 22 |
+
intro x
|
| 23 |
+
simp only [Function.comp_apply,decide_eq_true_eq]
|
| 24 |
+
by_cases h : x.2<x.1.length
|
| 25 |
+
· rw [ite_eq_left h,List.getD_eq_getElem?_getD,List.getElem?_eq_getElem h]
|
| 26 |
+
rfl
|
| 27 |
+
· rw [ite_eq_right h,List.getElem?_eq_none (by omega)])
|
| 28 |
+
|
| 29 |
+
theorem polynomial_getOptionFast : PolynomialTime (getOptionFast (A := A)) := by
|
| 30 |
+
unfold getOptionFast
|
| 31 |
+
exact polynomial_congr _ (polynomial_choose
|
| 32 |
+
(polynomial_composition (polynomial_pair polynomial_second
|
| 33 |
+
(polynomial_composition polynomial_first polynomial_listLength)) polynomial_less)
|
| 34 |
+
(polynomial_composition (polynomial_getDFast default) polynomial_some)
|
| 35 |
+
(polynomial_constant none))
|
| 36 |
+
|
| 37 |
+
def listWordChoice (x : List A×List Bool) : Option A :=
|
| 38 |
+
x.1[StackAddition.value x.2]?
|
| 39 |
+
|
| 40 |
+
def listWordChoiceRealizer : Realizer (listWordChoice (A := A)) :=
|
| 41 |
+
composition (pair first (composition second bitValue)) getOptionFast
|
| 42 |
+
|
| 43 |
+
theorem polynomial_listWordChoice : PolynomialTime (listWordChoiceRealizer (A := A)) :=
|
| 44 |
+
polynomial_composition
|
| 45 |
+
(polynomial_pair polynomial_first (polynomial_composition polynomial_second polynomial_bitValue))
|
| 46 |
+
polynomial_getOptionFast
|
| 47 |
+
|
| 48 |
+
omit [Codec A] [Inhabited A] in
|
| 49 |
+
lemma bitValue_eq_word (k : ℕ) (bits : Fin k → Bool) :
|
| 50 |
+
StackAddition.value (List.ofFn bits)=(FairBits.binaryWordEquiv k bits).val := by
|
| 51 |
+
have hv (k : ℕ) (bits : Fin k → Bool) :
|
| 52 |
+
StackAddition.value (List.ofFn bits)=∑ i, (bits i).toNat*2^i.val := by
|
| 53 |
+
induction k with
|
| 54 |
+
| zero => simp [StackAddition.value]
|
| 55 |
+
| succ k ih =>
|
| 56 |
+
rw [List.ofFn_succ,StackAddition.value,ih,Fin.sum_univ_succ]
|
| 57 |
+
simp only [Fin.val_zero,pow_zero,Nat.mul_one,Fin.val_succ,pow_succ]
|
| 58 |
+
rw [Finset.mul_sum]
|
| 59 |
+
congr 1
|
| 60 |
+
apply Finset.sum_congr rfl
|
| 61 |
+
intro i _
|
| 62 |
+
ring
|
| 63 |
+
rw [hv]
|
| 64 |
+
change _=∑ i : Fin k, (finTwoEquiv.symm (bits i)).val*2^i.val
|
| 65 |
+
apply Finset.sum_congr rfl
|
| 66 |
+
intro i _
|
| 67 |
+
cases bits i <;> rfl
|
| 68 |
+
|
| 69 |
+
omit [Codec A] [Inhabited A] in
|
| 70 |
+
theorem listWordChoice_ofFn (xs : List A) (k : ℕ) (bits : Fin k → Bool) :
|
| 71 |
+
listWordChoice (xs,List.ofFn bits)=FairBits.listBitDraw xs k bits := by
|
| 72 |
+
simp only [listWordChoice,FairBits.listBitDraw,bitValue_eq_word]
|
| 73 |
+
|
| 74 |
+
omit [Codec A] [Inhabited A] in
|
| 75 |
+
/-- The literal binary program has the previously proved dyadic proposal
|
| 76 |
+
law, including all unused indices that return `none`. -/
|
| 77 |
+
theorem listWordChoice_mass [Fintype A] [DecidableEq A]
|
| 78 |
+
(xs : List A) (hxs : xs.Nodup) (k : ℕ) (hk : xs.length≤2^k) (a : A) :
|
| 79 |
+
(FirstSuccess.mapLaw
|
| 80 |
+
(FirstSuccess.uniformLaw (α := Fin k → Bool))
|
| 81 |
+
(fun bits => listWordChoice (xs,List.ofFn bits))).mass (some a)=
|
| 82 |
+
if a∈xs then 1/(2:ℚ)^k else 0 := by
|
| 83 |
+
simp_rw [listWordChoice_ofFn]
|
| 84 |
+
exact FairBits.listBitDraw_mass xs hxs k hk a
|
| 85 |
+
|
| 86 |
+
end ContingencyTables.ProfilePrograms
|
| 87 |
+
|
| 88 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/LiteralProgram.lean
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.MatchingCount.Tapes.PackedExecution
|
| 2 |
+
import OAI.Combinatorics.MatchingCount.Machines.OnlinePhysicalProgram
|
| 3 |
+
import OAI.Combinatorics.MatchingCount.Machines.PhysicalProgramMap
|
| 4 |
+
import OAI.Combinatorics.MatchingCount.Machines.PhysicalProgramSeal
|
| 5 |
+
|
| 6 |
+
namespace OAI
|
| 7 |
+
|
| 8 |
+
/-! # Public binary input and output for typed finite programs
|
| 9 |
+
|
| 10 |
+
Literal packing is independent of the counting algorithm. Its cost includes every packing, execution, and printing step.
|
| 11 |
+
-/
|
| 12 |
+
|
| 13 |
+
namespace ContingencyTables.LiteralProgram
|
| 14 |
+
open MatchingFPRAS Turing PhysicalPost PhysicalStream LiteralPacked TreeTyped Polynomial
|
| 15 |
+
|
| 16 |
+
noncomputable def pack : Program := Program.ofTM1 packCode .start (fun _ => 0)
|
| 17 |
+
noncomputable def erase : Program := Program.ofTM1 (LiteralTape.eraseCode header.length) () ()
|
| 18 |
+
noncomputable def unpack : Program := Program.ofTM1 unpackCode .start (fun _ => 0)
|
| 19 |
+
noncomputable def post : Program := erase.seq unpack
|
| 20 |
+
noncomputable def packTime (n : ℕ) : ℕ :=
|
| 21 |
+
(1+LiteralBlockMap.budget 1 (18*width) n 0)*MachineCost.programCost packCode
|
| 22 |
+
noncomputable def postTime (n : ℕ) : ℕ :=
|
| 23 |
+
MachineCost.programCost (LiteralTape.eraseCode header.length)+
|
| 24 |
+
(1+LiteralBlockMap.budget (18*width) 1 n 0)*MachineCost.programCost unpackCode
|
| 25 |
+
|
| 26 |
+
lemma packTime_mono {n m : ℕ} (h : n ≤ m) : packTime n ≤ packTime m := by
|
| 27 |
+
unfold packTime LiteralBlockMap.budget
|
| 28 |
+
gcongr
|
| 29 |
+
|
| 30 |
+
lemma postTime_mono {n m : ℕ} (h : n ≤ m) : postTime n ≤ postTime m := by
|
| 31 |
+
unfold postTime LiteralBlockMap.budget
|
| 32 |
+
gcongr
|
| 33 |
+
|
| 34 |
+
lemma pack_realizes (as : List Symbol) (ha : ∀ a∈as,a≠0) :
|
| 35 |
+
pack.Realizes (Tape.mk₁ as) (.done (Tape.mk₁ (TreePost.packed as))) (packTime as.length) :=
|
| 36 |
+
Program.realizes_TM1 _ _ _ _ _ _ _ (pack_exec as ha)
|
| 37 |
+
|
| 38 |
+
lemma post_realizes (as : List Symbol) (ha : ∀ a∈as,a≠6 ∧ a≠7) :
|
| 39 |
+
post.Realizes (Tape.mk₁ (TreePost.packed as)) (.done (Tape.mk₁ as)) (postTime as.length) := by
|
| 40 |
+
have h1 : erase.Realizes (Tape.mk₁ (TreePost.packed as))
|
| 41 |
+
(.done (Tape.mk₁ (as.reverse.flatMap chunk)))
|
| 42 |
+
(MachineCost.programCost (LiteralTape.eraseCode header.length)) := by
|
| 43 |
+
rw [packed_eq]
|
| 44 |
+
exact (Program.realizes_TM1 _ _ _ _ _ _ _
|
| 45 |
+
(LiteralTape.erase_exec header (as.reverse.flatMap chunk))).mono (by simp only [Nat.one_mul]; rfl)
|
| 46 |
+
have h2 : unpack.Realizes (Tape.mk₁ (as.reverse.flatMap chunk)) (.done (Tape.mk₁ as))
|
| 47 |
+
((1+LiteralBlockMap.budget (18*width) 1 as.length 0)*MachineCost.programCost unpackCode) :=
|
| 48 |
+
Program.realizes_TM1 _ _ _ _ _ _ _ (unpack_exec as ha)
|
| 49 |
+
exact Program.seq_realizes erase unpack _ (.done (Tape.mk₁ (as.reverse.flatMap chunk)))
|
| 50 |
+
(fun _ => .done (Tape.mk₁ as)) _ _ h1 h2
|
| 51 |
+
|
| 52 |
+
noncomputable def wrap {f : List Symbol → Experiment (List Symbol)}
|
| 53 |
+
(F : OnlineTree.Realizer f) : Program := (pack.seq (OnlineTree.physicalProgram F)).seq post
|
| 54 |
+
|
| 55 |
+
noncomputable def wrapTime {f : List Symbol → Experiment (List Symbol)}
|
| 56 |
+
(F : OnlineTree.Realizer f) (as : List Symbol) : ℕ :=
|
| 57 |
+
packTime as.length+OnlineTree.physicalCost F as+postTime ((f as).worst List.length)
|
| 58 |
+
|
| 59 |
+
private lemma experiment_all_and {A : Type} {p : Experiment A} {P Q : A → Prop}
|
| 60 |
+
(hp : p.All P) (hq : p.All Q) : p.All (fun a => P a ∧ Q a) := by
|
| 61 |
+
induction p with
|
| 62 |
+
| done a => exact ⟨hp,hq⟩
|
| 63 |
+
| read p ih => exact ⟨ih false hp.1 hq.1,ih true hp.2 hq.2⟩
|
| 64 |
+
| delay n p ih => exact ih hp hq
|
| 65 |
+
|
| 66 |
+
lemma wrap_realizes {f : List Symbol → Experiment (List Symbol)}
|
| 67 |
+
(F : OnlineTree.Realizer f) (as : List Symbol) (ha : ∀ a∈as,a≠0)
|
| 68 |
+
(hout : (f as).All (fun bs => ∀ b∈bs,b≠6 ∧ b≠7)) :
|
| 69 |
+
(wrap F).Realizes (Tape.mk₁ as) ((f as).map Tape.mk₁) (wrapTime F as) := by
|
| 70 |
+
have h1 : (pack.seq (OnlineTree.physicalProgram F)).Realizes (Tape.mk₁ as)
|
| 71 |
+
((f as).map (fun bs => Tape.mk₁ (TreePost.packed bs)))
|
| 72 |
+
(packTime as.length+OnlineTree.physicalCost F as) :=
|
| 73 |
+
Program.seq_realizes pack (OnlineTree.physicalProgram F) _ (.done (Tape.mk₁ (TreePost.packed as)))
|
| 74 |
+
(fun _ => (f as).map (fun bs => Tape.mk₁ (TreePost.packed bs))) _ _
|
| 75 |
+
(pack_realizes as ha) (OnlineTree.physical_realizes F as)
|
| 76 |
+
exact Program.seq_realizes_map _ _ _ (f as) (fun bs => Tape.mk₁ (TreePost.packed bs))
|
| 77 |
+
(fun bs => .done (Tape.mk₁ bs)) _ _ h1
|
| 78 |
+
((experiment_all_and hout (Experiment.all_worst (f as) List.length)).mono
|
| 79 |
+
(fun bs hbs => (post_realizes bs hbs.1).mono (postTime_mono hbs.2)))
|
| 80 |
+
|
| 81 |
+
/-- A polynomial representing the exact elementary block-map cost. -/
|
| 82 |
+
noncomputable def budgetPolynomial (w H : ℕ) (p q : Polynomial ℕ) : Polynomial ℕ :=
|
| 83 |
+
(p+1)*(C (3+w+H)+2*(q+p*C (H+w)))
|
| 84 |
+
|
| 85 |
+
lemma eval_budgetPolynomial (w H : ℕ) (p q : Polynomial ℕ) (n : ℕ) :
|
| 86 |
+
(budgetPolynomial w H p q).eval n=LiteralBlockMap.budget w H (p.eval n) (q.eval n) := by
|
| 87 |
+
simp only [budgetPolynomial,LiteralBlockMap.budget,eval_mul,eval_add,eval_C,eval_one,eval_ofNat]
|
| 88 |
+
|
| 89 |
+
/-- Any polynomial bound on the typed experiment gives a polynomial bound
|
| 90 |
+
on the complete public binary execution. -/
|
| 91 |
+
theorem polynomial_wrap {f : List Symbol → Experiment (List Symbol)}
|
| 92 |
+
(F : OnlineTree.Realizer f) (p : Polynomial ℕ)
|
| 93 |
+
(hp : ∀ as,F.cost as+(f as).worst weight ≤ p.eval (weight as)) :
|
| 94 |
+
∃ q : Polynomial ℕ,∀ as,wrapTime F as ≤ q.eval as.length := by
|
| 95 |
+
let b : Polynomial ℕ := 18*X+1
|
| 96 |
+
let v := p.comp b
|
| 97 |
+
let a := OnlineStack.accesses F.program.code
|
| 98 |
+
let o := OnlineStack.overhead F.program.code
|
| 99 |
+
let pk : Polynomial ℕ := (1+budgetPolynomial 1 (18*width) X 0)*C (MachineCost.programCost packCode)
|
| 100 |
+
let out : Polynomial ℕ := C (MachineCost.programCost (LiteralTape.eraseCode header.length))+
|
| 101 |
+
(1+budgetPolynomial (18*width) 1 v 0)*C (MachineCost.programCost unpackCode)
|
| 102 |
+
let fac : Polynomial ℕ := C o*(1+2*C a*(b+v*C a+1))+1
|
| 103 |
+
refine ⟨pk+v*fac+out,fun as => ?_⟩
|
| 104 |
+
have hv : v.eval as.length=p.eval (weight as) := by
|
| 105 |
+
simp only [v,b,eval_comp,eval_add,eval_mul,eval_ofNat,eval_X,eval_one,LiteralCodec.weight_word]
|
| 106 |
+
have hc : F.cost as ≤ v.eval as.length := by rw [hv]; exact (Nat.le_add_right _ _).trans (hp as)
|
| 107 |
+
have hw : (f as).worst weight ≤ v.eval as.length := by rw [hv]; exact (Nat.le_add_left _ _).trans (hp as)
|
| 108 |
+
have hl : (f as).worst List.length ≤ v.eval as.length :=
|
| 109 |
+
Experiment.worst_le _ _ _ ((Experiment.all_worst _ weight).mono
|
| 110 |
+
(fun bs hbs => (list_length_le_weight bs).trans (hbs.trans hw)))
|
| 111 |
+
have hf : OnlineStack.Executes.factor F.program.code (weight as) (F.cost as) ≤ fac.eval as.length := by
|
| 112 |
+
have he : fac.eval as.length=OnlineStack.Executes.factor F.program.code (weight as) (v.eval as.length) := by
|
| 113 |
+
simp only [fac,b,eval_add,eval_mul,eval_C,eval_ofNat,eval_one,eval_X,
|
| 114 |
+
OnlineStack.Executes.factor,LiteralCodec.weight_word,a,o]
|
| 115 |
+
rw [he]
|
| 116 |
+
exact OnlineStack.Executes.factor_mono _ _ _ _ le_rfl hc
|
| 117 |
+
have hpk : pk.eval as.length=packTime as.length := by
|
| 118 |
+
simp only [pk,eval_mul,eval_add,eval_one,eval_C,eval_budgetPolynomial,eval_X,eval_zero,packTime]
|
| 119 |
+
have hout : out.eval as.length=postTime (v.eval as.length) := by
|
| 120 |
+
simp only [out,eval_add,eval_mul,eval_one,eval_C,eval_budgetPolynomial,eval_zero,postTime]
|
| 121 |
+
simp only [eval_add,eval_mul,hpk,hout]
|
| 122 |
+
unfold wrapTime OnlineTree.physicalCost
|
| 123 |
+
exact Nat.add_le_add (Nat.add_le_add_left (Nat.mul_le_mul hc hf) _) (postTime_mono hl)
|
| 124 |
+
|
| 125 |
+
/-- Deterministic typed programs therefore execute on the public binary
|
| 126 |
+
tape with one fixed finite transition table and a polynomial time bound. -/
|
| 127 |
+
theorem deterministic_compiles {f : List Symbol → List Symbol}
|
| 128 |
+
(F : TreeTyped.Realizer f) (hF : PolynomialTime F)
|
| 129 |
+
(hout : ∀ as,∀ b∈f as,b≠6 ∧ b≠7) :
|
| 130 |
+
∃ (A : RandomMachine) (q : Polynomial ℕ),∀ as,(∀ a∈as,a≠0) →
|
| 131 |
+
∀ t,q.eval as.length ≤ t → ∀ bits : Fin t → Bool,
|
| 132 |
+
(run A as bits).state=none ∧ (run A as bits).tape.right₀=ListBlank.mk (f as) := by
|
| 133 |
+
obtain ⟨p,hp⟩ := hF
|
| 134 |
+
obtain ⟨q,hq⟩ := polynomial_wrap (OnlineTree.deterministic F) p hp
|
| 135 |
+
refine ⟨(wrap (OnlineTree.deterministic F)).machine,q,?_⟩
|
| 136 |
+
intro as ha t ht bits
|
| 137 |
+
have hr := wrap_realizes (OnlineTree.deterministic F) as ha (hout as)
|
| 138 |
+
apply hr.run_all (fun T => T.right₀=ListBlank.mk (f as)) _ t ((hq as).trans ht) bits
|
| 139 |
+
change (Tape.mk₁ (f as)).right₀=ListBlank.mk (f as)
|
| 140 |
+
simp only [Tape.mk₁,Tape.mk₂,Tape.mk'_right₀]
|
| 141 |
+
|
| 142 |
+
end ContingencyTables.LiteralProgram
|
| 143 |
+
|
| 144 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/LowerCapacityProgram.lean
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.ListSubtractProgram
|
| 2 |
+
|
| 3 |
+
namespace OAI
|
| 4 |
+
|
| 5 |
+
/-! # Executable subtraction of a lower bound from a capacity matrix -/
|
| 6 |
+
|
| 7 |
+
namespace ContingencyTables.LowerCapacityProgram
|
| 8 |
+
|
| 9 |
+
open MatchingFPRAS.TreeTyped Polynomial ResidualGraphProgram
|
| 10 |
+
|
| 11 |
+
abbrev Input := (NatMatrix×(ℕ×ℕ))×ℕ
|
| 12 |
+
abbrev RowInput := Input×ℕ
|
| 13 |
+
|
| 14 |
+
def readRow (z : RowInput) : List ℕ := z.1.1.1.getD z.2 []
|
| 15 |
+
|
| 16 |
+
def readRowRealizer : Realizer readRow :=
|
| 17 |
+
composition (pair (composition (composition first first) first) second) (getDFast [])
|
| 18 |
+
|
| 19 |
+
lemma polynomial_readRow : PolynomialTime readRowRealizer :=
|
| 20 |
+
polynomial_composition (polynomial_pair
|
| 21 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 22 |
+
polynomial_first) polynomial_second) (polynomial_getDFast [])
|
| 23 |
+
|
| 24 |
+
def selected (z : RowInput) : Bool := decide (z.2=z.1.1.2.1)
|
| 25 |
+
|
| 26 |
+
def selectedRealizer : Realizer selected :=
|
| 27 |
+
composition (pair second (composition (composition (composition first first) second) first)) natEqual
|
| 28 |
+
|
| 29 |
+
lemma polynomial_selected : PolynomialTime selectedRealizer :=
|
| 30 |
+
polynomial_composition (polynomial_pair polynomial_second
|
| 31 |
+
(polynomial_composition
|
| 32 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 33 |
+
polynomial_second) polynomial_first)) polynomial_natEqual
|
| 34 |
+
|
| 35 |
+
def shiftedInput (z : RowInput) : ListSubtractProgram.Input :=
|
| 36 |
+
((readRow z,z.1.1.2.2),z.1.2)
|
| 37 |
+
|
| 38 |
+
def shiftedInputRealizer : Realizer shiftedInput :=
|
| 39 |
+
pair (pair readRowRealizer
|
| 40 |
+
(composition (composition (composition first first) second) second))
|
| 41 |
+
(composition first second)
|
| 42 |
+
|
| 43 |
+
lemma polynomial_shiftedInput : PolynomialTime shiftedInputRealizer :=
|
| 44 |
+
polynomial_pair (polynomial_pair polynomial_readRow
|
| 45 |
+
(polynomial_composition
|
| 46 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 47 |
+
polynomial_second) polynomial_second))
|
| 48 |
+
(polynomial_composition polynomial_first polynomial_second)
|
| 49 |
+
|
| 50 |
+
def row (z : RowInput) : List ℕ :=
|
| 51 |
+
if z.2=z.1.1.2.1 then ListSubtractProgram.subtractAt (shiftedInput z) else readRow z
|
| 52 |
+
|
| 53 |
+
def rowRealizer : Realizer row :=
|
| 54 |
+
(choose selectedRealizer
|
| 55 |
+
(composition (f := shiftedInput) (g := ListSubtractProgram.subtractAt)
|
| 56 |
+
shiftedInputRealizer ListSubtractProgram.subtractAtRealizer) readRowRealizer).congr (by
|
| 57 |
+
intro z
|
| 58 |
+
simp only [Function.comp_apply,row,selected,decide_eq_true_eq])
|
| 59 |
+
|
| 60 |
+
lemma polynomial_row : PolynomialTime rowRealizer :=
|
| 61 |
+
polynomial_congr _ (polynomial_choose polynomial_selected
|
| 62 |
+
(polynomial_composition polynomial_shiftedInput ListSubtractProgram.polynomial_subtractAt)
|
| 63 |
+
polynomial_readRow)
|
| 64 |
+
|
| 65 |
+
def indices (z : Input) : List ℕ := List.range z.1.1.length
|
| 66 |
+
|
| 67 |
+
def indicesRealizer : Realizer indices :=
|
| 68 |
+
composition (composition (composition first first) listLength) range
|
| 69 |
+
|
| 70 |
+
lemma polynomial_indices : PolynomialTime indicesRealizer := by
|
| 71 |
+
apply polynomial_boundedRange
|
| 72 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 73 |
+
polynomial_listLength) X
|
| 74 |
+
intro z
|
| 75 |
+
simpa only [Function.comp_apply,eval_X] using
|
| 76 |
+
(list_length_le_weight z.1.1).trans ((weight_fst z.1).trans (weight_fst z))
|
| 77 |
+
|
| 78 |
+
def matrix (z : Input) : NatMatrix := (indices z).map (fun i=>row (z,i))
|
| 79 |
+
def matrixRealizer : Realizer matrix :=
|
| 80 |
+
composition (pair identity indicesRealizer) (mapWith rowRealizer)
|
| 81 |
+
lemma polynomial_matrix : PolynomialTime matrixRealizer :=
|
| 82 |
+
polynomial_composition (polynomial_pair polynomial_identity polynomial_indices)
|
| 83 |
+
(polynomial_mapWith polynomial_row)
|
| 84 |
+
|
| 85 |
+
lemma matrix_code {m n : ℕ} (b : Fin m→Fin n→ℕ) (i₀ : Fin m) (j₀ : Fin n) (t : ℕ) :
|
| 86 |
+
matrix ((natCode b,(i₀.val,j₀.val)),t)=natCode (BoundedCellShift.lower b i₀ j₀ t) := by
|
| 87 |
+
simp only [matrix,indices,natCode,List.length_ofFn,DirectedBFSCode.range_eq_map,←List.ofFn_eq_map,
|
| 88 |
+
List.map_ofFn]
|
| 89 |
+
apply congrArg List.ofFn
|
| 90 |
+
funext i
|
| 91 |
+
dsimp only [Function.comp_apply]
|
| 92 |
+
have hr : readRow ((((List.ofFn (fun i=>List.ofFn (b i)),(i₀.val,j₀.val)),t),i.val))=
|
| 93 |
+
List.ofFn (b i) := by
|
| 94 |
+
simp only [readRow,List.getD_eq_getElem?_getD,List.getElem?_ofFn,i.isLt,
|
| 95 |
+
dite_true,Option.getD_some,Fin.eta]
|
| 96 |
+
by_cases hi : i=i₀
|
| 97 |
+
· subst i
|
| 98 |
+
simp only [row,ite_true,shiftedInput,hr]
|
| 99 |
+
rw [ListSubtractProgram.subtractAt_ofFn]
|
| 100 |
+
apply congrArg List.ofFn
|
| 101 |
+
funext j
|
| 102 |
+
simp [BoundedCellShift.margin,BoundedCellShift.lower]
|
| 103 |
+
· have hn : i.val≠i₀.val := fun he=>hi (Fin.ext he)
|
| 104 |
+
simp only [row,hn,ite_false,hr]
|
| 105 |
+
apply congrArg List.ofFn
|
| 106 |
+
funext j
|
| 107 |
+
simp [BoundedCellShift.lower,hi]
|
| 108 |
+
|
| 109 |
+
end ContingencyTables.LowerCapacityProgram
|
| 110 |
+
|
| 111 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/MatrixCodeEquality.lean
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Sampling.ProfileCodeEquality
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Dense.DenseIntegerProgram
|
| 3 |
+
|
| 4 |
+
namespace OAI
|
| 5 |
+
|
| 6 |
+
/-! # Polynomial equality of explicitly stored rectangular arrays -/
|
| 7 |
+
|
| 8 |
+
namespace ContingencyTables.DensePrograms
|
| 9 |
+
open MatchingFPRAS.TreeTyped
|
| 10 |
+
|
| 11 |
+
def matrixSignature (X : MatrixCode) : List ℕ×List ℕ := (X.map List.length,X.flatten)
|
| 12 |
+
|
| 13 |
+
def matrixSignatureRealizer : Realizer matrixSignature := pair (map listLength) flatten
|
| 14 |
+
|
| 15 |
+
lemma polynomial_matrixSignature : PolynomialTime matrixSignatureRealizer :=
|
| 16 |
+
polynomial_pair (polynomial_map polynomial_listLength) polynomial_flatten
|
| 17 |
+
|
| 18 |
+
lemma matrixSignature_injective : Function.Injective matrixSignature := by
|
| 19 |
+
intro X Y h
|
| 20 |
+
exact List.eq_iff_flatten_eq.mpr ⟨congrArg Prod.snd h,congrArg Prod.fst h⟩
|
| 21 |
+
|
| 22 |
+
def matrixEqual : Realizer (fun z : MatrixCode×MatrixCode => decide (z.1=z.2)) :=
|
| 23 |
+
(composition (pair (composition first matrixSignatureRealizer)
|
| 24 |
+
(composition second matrixSignatureRealizer)) ProfilePrograms.profileCodeEqual).congr (by
|
| 25 |
+
intro z
|
| 26 |
+
simp only [Function.comp_apply,matrixSignature_injective.eq_iff])
|
| 27 |
+
|
| 28 |
+
lemma polynomial_matrixEqual : PolynomialTime matrixEqual := by
|
| 29 |
+
unfold matrixEqual
|
| 30 |
+
apply polynomial_congr
|
| 31 |
+
exact polynomial_composition (polynomial_pair
|
| 32 |
+
(polynomial_composition polynomial_first polynomial_matrixSignature)
|
| 33 |
+
(polynomial_composition polynomial_second polynomial_matrixSignature))
|
| 34 |
+
ProfilePrograms.polynomial_profileCodeEqual
|
| 35 |
+
|
| 36 |
+
lemma matrixCode_injective {m n : ℕ} :
|
| 37 |
+
Function.Injective (matrixCode (m := m) (n := n)) := by
|
| 38 |
+
intro X Y h
|
| 39 |
+
have hi := List.ofFn_injective h
|
| 40 |
+
funext i
|
| 41 |
+
exact List.ofFn_injective (congrFun hi i)
|
| 42 |
+
|
| 43 |
+
end ContingencyTables.DensePrograms
|
| 44 |
+
|
| 45 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/MatrixEncoding.lean
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Sampling.GreedyPublicProgram
|
| 2 |
+
|
| 3 |
+
namespace OAI
|
| 4 |
+
|
| 5 |
+
/-! # The public matrix encoding uniquely identifies a fixed-shape table -/
|
| 6 |
+
|
| 7 |
+
namespace ContingencyTables.Algorithms
|
| 8 |
+
open MatchingFPRAS MatchingFPRAS.LiteralParse Turing
|
| 9 |
+
|
| 10 |
+
lemma encodeMatrix_flat {m n : ℕ} (X : Fin m → Fin n → ℕ) :
|
| 11 |
+
encodeMatrix X=((List.ofFn fun i => List.ofFn (X i)).flatten).flatMap encodeNat := by
|
| 12 |
+
rw [←GreedyFeasibleTable.printMatrix_ofFn]
|
| 13 |
+
simp only [GreedyFeasibleTable.printMatrix,List.flatten_eq_flatMap,List.flatMap_assoc,id_eq]
|
| 14 |
+
|
| 15 |
+
lemma matrix_flat_injective (m n : ℕ) (X Y : Fin m → Fin n → ℕ)
|
| 16 |
+
(h : (List.ofFn (fun i => List.ofFn (X i))).flatten=
|
| 17 |
+
(List.ofFn (fun i => List.ofFn (Y i))).flatten) : X=Y := by
|
| 18 |
+
have hr : List.ofFn (fun i => List.ofFn (X i))=List.ofFn (fun i => List.ofFn (Y i)) :=
|
| 19 |
+
List.eq_iff_flatten_eq.mpr ⟨h,by
|
| 20 |
+
simp only [List.map_ofFn]
|
| 21 |
+
congr 1
|
| 22 |
+
funext i
|
| 23 |
+
exact (List.length_ofFn).trans (List.length_ofFn.symm)⟩
|
| 24 |
+
have hf := List.ofFn_injective hr
|
| 25 |
+
funext i
|
| 26 |
+
exact List.ofFn_injective (congrFun hf i)
|
| 27 |
+
|
| 28 |
+
lemma encodeMatrix_injective {m n : ℕ} :
|
| 29 |
+
Function.Injective (encodeMatrix (m := m) (n := n)) := by
|
| 30 |
+
intro X Y h
|
| 31 |
+
have hp := congrArg parse h
|
| 32 |
+
rw [encodeMatrix_flat,encodeMatrix_flat,GreedyFeasibleTable.parse_natWords,
|
| 33 |
+
GreedyFeasibleTable.parse_natWords] at hp
|
| 34 |
+
exact matrix_flat_injective m n X Y hp
|
| 35 |
+
|
| 36 |
+
lemma parse_blank_extension (as : List Symbol) (n : ℕ) :
|
| 37 |
+
parse (as++List.replicate n (0:Symbol))=parse as := by
|
| 38 |
+
have hz : (List.replicate n (0:Symbol)).foldr (fun a ns => numStep (a,ns)) []=[] := by
|
| 39 |
+
induction n with
|
| 40 |
+
| zero => rfl
|
| 41 |
+
| succ n ih =>
|
| 42 |
+
simp only [List.replicate_succ,List.foldr_cons,ih]
|
| 43 |
+
rfl
|
| 44 |
+
unfold parse
|
| 45 |
+
rw [List.foldr_append,hz]
|
| 46 |
+
|
| 47 |
+
/-- Parsing natural-number fields is unchanged by trailing tape blanks. -/
|
| 48 |
+
def blankNumbers (tape : ListBlank Symbol) : List ℕ :=
|
| 49 |
+
tape.liftOn parse (by
|
| 50 |
+
rintro as _ ⟨n,rfl⟩
|
| 51 |
+
exact (parse_blank_extension as n).symm)
|
| 52 |
+
|
| 53 |
+
lemma blankMatrix_injective {m n : ℕ} (X Y : Fin m → Fin n → ℕ)
|
| 54 |
+
(h : ListBlank.mk (encodeMatrix X)=ListBlank.mk (encodeMatrix Y)) : X=Y := by
|
| 55 |
+
have hp := congrArg blankNumbers h
|
| 56 |
+
change parse (encodeMatrix X)=parse (encodeMatrix Y) at hp
|
| 57 |
+
rw [encodeMatrix_flat,encodeMatrix_flat,GreedyFeasibleTable.parse_natWords,
|
| 58 |
+
GreedyFeasibleTable.parse_natWords] at hp
|
| 59 |
+
exact matrix_flat_injective m n X Y hp
|
| 60 |
+
|
| 61 |
+
end ContingencyTables.Algorithms
|
| 62 |
+
|
| 63 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/MatrixSubtractProgram.lean
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Dense.DenseIntegerProgram
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Transport.DirectedBFSCode
|
| 3 |
+
import OAI.Combinatorics.MatchingCount.Complexity.TreePolynomialGrid
|
| 4 |
+
|
| 5 |
+
namespace OAI
|
| 6 |
+
|
| 7 |
+
/-! # Entrywise natural subtraction on a supplied matrix shape -/
|
| 8 |
+
|
| 9 |
+
namespace ContingencyTables.MatrixSubtractProgram
|
| 10 |
+
|
| 11 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped Polynomial DensePrograms
|
| 12 |
+
|
| 13 |
+
abbrev Input := MatrixCode × MatrixCode
|
| 14 |
+
abbrev RowInput := Input × ℕ
|
| 15 |
+
|
| 16 |
+
def entry (z : RowInput × ℕ) : ℕ :=
|
| 17 |
+
matrixEntry (z.1.1.1,(z.1.2,z.2))-matrixEntry (z.1.1.2,(z.1.2,z.2))
|
| 18 |
+
def entryRealizer : Realizer entry := by
|
| 19 |
+
let left : Realizer (fun z : RowInput × ℕ => matrixEntry (z.1.1.1,(z.1.2,z.2))) :=
|
| 20 |
+
composition (f := fun z : RowInput × ℕ => (z.1.1.1,(z.1.2,z.2))) (g := matrixEntry)
|
| 21 |
+
(pair (composition (composition first first) first)
|
| 22 |
+
(pair (composition first second) second)) matrixEntryRealizer
|
| 23 |
+
let right : Realizer (fun z : RowInput × ℕ => matrixEntry (z.1.1.2,(z.1.2,z.2))) :=
|
| 24 |
+
composition (f := fun z : RowInput × ℕ => (z.1.1.2,(z.1.2,z.2))) (g := matrixEntry)
|
| 25 |
+
(pair (composition (composition first first) second)
|
| 26 |
+
(pair (composition first second) second)) matrixEntryRealizer
|
| 27 |
+
exact composition (f := fun z : RowInput × ℕ =>
|
| 28 |
+
(matrixEntry (z.1.1.1,(z.1.2,z.2)),matrixEntry (z.1.1.2,(z.1.2,z.2))))
|
| 29 |
+
(g := fun p : ℕ × ℕ => p.1-p.2) (pair left right) subtract
|
| 30 |
+
lemma polynomial_entry : PolynomialTime entryRealizer := by
|
| 31 |
+
unfold entryRealizer
|
| 32 |
+
exact polynomial_composition (polynomial_pair
|
| 33 |
+
(polynomial_composition (polynomial_pair
|
| 34 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 35 |
+
polynomial_first)
|
| 36 |
+
(polynomial_pair (polynomial_composition polynomial_first polynomial_second)
|
| 37 |
+
polynomial_second)) polynomial_matrixEntry)
|
| 38 |
+
(polynomial_composition (polynomial_pair
|
| 39 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 40 |
+
polynomial_second)
|
| 41 |
+
(polynomial_pair (polynomial_composition polynomial_first polynomial_second)
|
| 42 |
+
polynomial_second)) polynomial_matrixEntry)) polynomial_subtract
|
| 43 |
+
|
| 44 |
+
def sourceRow (z : RowInput) : List ℕ := z.1.1.getD z.2 []
|
| 45 |
+
def sourceRowRealizer : Realizer sourceRow :=
|
| 46 |
+
composition (pair (composition first first) second) (getDFast [])
|
| 47 |
+
lemma polynomial_sourceRow : PolynomialTime sourceRowRealizer :=
|
| 48 |
+
polynomial_composition (polynomial_pair
|
| 49 |
+
(polynomial_composition polynomial_first polynomial_first) polynomial_second)
|
| 50 |
+
(polynomial_getDFast [])
|
| 51 |
+
|
| 52 |
+
def columns (z : RowInput) : List ℕ := List.range (sourceRow z).length
|
| 53 |
+
def columnsRealizer : Realizer columns :=
|
| 54 |
+
composition (f := sourceRow) (g := fun xs : List ℕ => List.range xs.length)
|
| 55 |
+
sourceRowRealizer (composition listLength range)
|
| 56 |
+
lemma polynomial_columns : PolynomialTime columnsRealizer := by
|
| 57 |
+
apply polynomial_composition polynomial_sourceRow
|
| 58 |
+
apply polynomial_boundedRange polynomial_listLength X
|
| 59 |
+
intro xs
|
| 60 |
+
simpa only [eval_X] using list_length_le_weight xs
|
| 61 |
+
|
| 62 |
+
def row (z : RowInput) : List ℕ := (columns z).map (fun j => entry (z,j))
|
| 63 |
+
def rowRealizer : Realizer row :=
|
| 64 |
+
composition (pair identity columnsRealizer) (mapWith entryRealizer)
|
| 65 |
+
lemma polynomial_row : PolynomialTime rowRealizer :=
|
| 66 |
+
polynomial_composition (polynomial_pair polynomial_identity polynomial_columns)
|
| 67 |
+
(polynomial_mapWith polynomial_entry)
|
| 68 |
+
|
| 69 |
+
def indices (z : Input) : List ℕ := List.range z.1.length
|
| 70 |
+
def indicesRealizer : Realizer indices := composition (composition first listLength) range
|
| 71 |
+
lemma polynomial_indices : PolynomialTime indicesRealizer := by
|
| 72 |
+
apply polynomial_boundedRange (polynomial_composition polynomial_first polynomial_listLength) X
|
| 73 |
+
intro z
|
| 74 |
+
simpa only [Function.comp_apply,eval_X] using
|
| 75 |
+
(list_length_le_weight z.1).trans (weight_fst z)
|
| 76 |
+
|
| 77 |
+
def matrix (z : Input) : MatrixCode := (indices z).map (fun i => row (z,i))
|
| 78 |
+
def matrixRealizer : Realizer matrix :=
|
| 79 |
+
composition (pair identity indicesRealizer) (mapWith rowRealizer)
|
| 80 |
+
theorem polynomial_matrix : PolynomialTime matrixRealizer :=
|
| 81 |
+
polynomial_composition (polynomial_pair polynomial_identity polynomial_indices)
|
| 82 |
+
(polynomial_mapWith polynomial_row)
|
| 83 |
+
|
| 84 |
+
theorem matrix_code {m n : ℕ} (B Y : Fin m → Fin n → ℕ) :
|
| 85 |
+
matrix (matrixCode B,matrixCode Y)=matrixCode (fun i j => B i j-Y i j) := by
|
| 86 |
+
simp only [matrix,indices,matrixCode,List.length_ofFn,DirectedBFSCode.range_eq_map,
|
| 87 |
+
←List.ofFn_eq_map,List.map_ofFn,Function.comp_def]
|
| 88 |
+
apply congrArg List.ofFn
|
| 89 |
+
funext i
|
| 90 |
+
simp only [row,columns,sourceRow,List.getD_eq_getElem?_getD,List.getElem?_ofFn,
|
| 91 |
+
i.isLt,dite_true,Option.getD_some,List.length_ofFn,DirectedBFSCode.range_eq_map,
|
| 92 |
+
←List.ofFn_eq_map,List.map_ofFn,Function.comp_def,entry]
|
| 93 |
+
apply congrArg List.ofFn
|
| 94 |
+
funext j
|
| 95 |
+
exact congrArg₂ Nat.sub (matrixEntry_code B i j) (matrixEntry_code Y i j)
|
| 96 |
+
|
| 97 |
+
end ContingencyTables.MatrixSubtractProgram
|
| 98 |
+
|
| 99 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/MaximalListProgram.lean
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Dense.DenseExponentListProgram
|
| 2 |
+
import OAI.Combinatorics.MatchingCount.Machines.TreeIndex
|
| 3 |
+
|
| 4 |
+
namespace OAI
|
| 5 |
+
|
| 6 |
+
/-! # Computing a maximal margin in binary -/
|
| 7 |
+
|
| 8 |
+
namespace ContingencyTables.DensePrograms
|
| 9 |
+
open MatchingFPRAS.TreeTyped
|
| 10 |
+
|
| 11 |
+
def maximumIndex (xs : List ℕ) : ℕ := xs.idxOf (listMaximum (xs,0))
|
| 12 |
+
|
| 13 |
+
def maximumIndexRealizer : Realizer maximumIndex :=
|
| 14 |
+
composition (pair (composition (pair identity (constant 0)) listMaximumRealizer) identity)
|
| 15 |
+
(indexOf natEqual)
|
| 16 |
+
|
| 17 |
+
theorem polynomial_maximumIndex : PolynomialTime maximumIndexRealizer :=
|
| 18 |
+
polynomial_composition (polynomial_pair
|
| 19 |
+
(polynomial_composition (polynomial_pair polynomial_identity (polynomial_constant 0))
|
| 20 |
+
polynomial_listMaximum) polynomial_identity) (polynomial_indexOf polynomial_natEqual)
|
| 21 |
+
|
| 22 |
+
lemma listMaximum_mem_or_default (xs : List ℕ) (a : ℕ) :
|
| 23 |
+
listMaximum (xs,a)=a ∨ listMaximum (xs,a)∈xs := by
|
| 24 |
+
induction xs generalizing a with
|
| 25 |
+
| nil => exact Or.inl rfl
|
| 26 |
+
| cons b bs ih =>
|
| 27 |
+
change listMaximum (bs,max b a)=a ∨ listMaximum (bs,max b a)∈b::bs
|
| 28 |
+
rcases ih (max b a) with he|hm
|
| 29 |
+
· rw [he]
|
| 30 |
+
rcases le_total b a with h|h
|
| 31 |
+
· exact Or.inl (max_eq_right h)
|
| 32 |
+
· exact Or.inr (by simp [max_eq_left h])
|
| 33 |
+
· exact Or.inr (List.mem_cons_of_mem b hm)
|
| 34 |
+
|
| 35 |
+
lemma listMaximum_mem (xs : List ℕ) (hx : xs≠[]) : listMaximum (xs,0)∈xs := by
|
| 36 |
+
cases xs with
|
| 37 |
+
| nil => exact (hx rfl).elim
|
| 38 |
+
| cons a xs =>
|
| 39 |
+
change listMaximum (xs,max a 0)∈a::xs
|
| 40 |
+
rw [Nat.max_zero]
|
| 41 |
+
rcases listMaximum_mem_or_default xs a with he|hm
|
| 42 |
+
· simp [he]
|
| 43 |
+
· exact List.mem_cons_of_mem a hm
|
| 44 |
+
|
| 45 |
+
theorem maximumIndex_lt (xs : List ℕ) (hx : xs≠[]) : maximumIndex xs < xs.length :=
|
| 46 |
+
List.idxOf_lt_length_iff.mpr (listMaximum_mem xs hx)
|
| 47 |
+
|
| 48 |
+
theorem maximumIndex_value (xs : List ℕ) (hx : xs≠[]) :
|
| 49 |
+
xs.getD (maximumIndex xs) 0=listMaximum (xs,0) := by
|
| 50 |
+
rw [List.getD_eq_getElem xs 0 (maximumIndex_lt xs hx)]
|
| 51 |
+
exact List.getElem_idxOf (maximumIndex_lt xs hx)
|
| 52 |
+
|
| 53 |
+
theorem maximumIndex_maximal (xs : List ℕ) (hx : xs≠[]) (a : ℕ) (ha : a∈xs) :
|
| 54 |
+
a≤xs.getD (maximumIndex xs) 0 := by
|
| 55 |
+
rw [maximumIndex_value xs hx]
|
| 56 |
+
exact le_listMaximum_member xs 0 a ha
|
| 57 |
+
|
| 58 |
+
def maximumPosition {n : ℕ} (f : Fin (n+1) → ℕ) : Fin (n+1) :=
|
| 59 |
+
⟨maximumIndex (List.ofFn f),by
|
| 60 |
+
simpa only [List.length_ofFn] using maximumIndex_lt (List.ofFn f)
|
| 61 |
+
(by intro h; have := congrArg List.length h; simp only [List.length_ofFn,List.length_nil] at this; omega)⟩
|
| 62 |
+
|
| 63 |
+
theorem maximumPosition_spec {n : ℕ} (f : Fin (n+1) → ℕ) (i : Fin (n+1)) :
|
| 64 |
+
f i≤f (maximumPosition f) := by
|
| 65 |
+
have hx : List.ofFn f≠[] := by
|
| 66 |
+
intro h
|
| 67 |
+
have := congrArg List.length h
|
| 68 |
+
simp only [List.length_ofFn,List.length_nil] at this
|
| 69 |
+
omega
|
| 70 |
+
have hm := maximumIndex_maximal (List.ofFn f) hx (f i) (List.mem_ofFn.mpr ⟨i,rfl⟩)
|
| 71 |
+
have he : (List.ofFn f).getD (maximumIndex (List.ofFn f)) 0=f (maximumPosition f) := by
|
| 72 |
+
rw [List.getD_eq_getElem (List.ofFn f) 0 (maximumIndex_lt (List.ofFn f) hx),List.getElem_ofFn]
|
| 73 |
+
rfl
|
| 74 |
+
rwa [he] at hm
|
| 75 |
+
|
| 76 |
+
end ContingencyTables.DensePrograms
|
| 77 |
+
|
| 78 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/MedianFractionProgram.lean
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.MatchingCount.Complexity.TreeFractionCosts
|
| 2 |
+
import OAI.Combinatorics.MatchingCount.Machines.TreeFastLookup
|
| 3 |
+
import OAI.Combinatorics.MatroidCounting.CommonBases
|
| 4 |
+
|
| 5 |
+
namespace OAI
|
| 6 |
+
|
| 7 |
+
/-! # Binary fraction median with polynomial running time -/
|
| 8 |
+
|
| 9 |
+
namespace ContingencyTables.MedianFractionProgram
|
| 10 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped MatchingFPRAS.TreeFraction Polynomial
|
| 11 |
+
|
| 12 |
+
abbrev State := Fraction × (List Fraction × List Fraction)
|
| 13 |
+
|
| 14 |
+
def insertRaw (q : Fraction) : List Fraction → List Fraction
|
| 15 |
+
| [] => [q]
|
| 16 |
+
| x::xs => if TreeFraction.less (x,q) then x::insertRaw q xs else q::x::xs
|
| 17 |
+
|
| 18 |
+
def step (z : Fraction × State) : State :=
|
| 19 |
+
(z.2.1,(z.1::z.2.2.1,
|
| 20 |
+
if TreeFraction.less (z.1,z.2.1) then z.1::z.2.2.2 else z.2.1::z.1::z.2.2.1))
|
| 21 |
+
|
| 22 |
+
def stepRealizer : Realizer step := by
|
| 23 |
+
let q : Realizer (fun z : Fraction × State => z.2.1) := composition second first
|
| 24 |
+
let xs : Realizer (fun z : Fraction × State => z.1::z.2.2.1) :=
|
| 25 |
+
composition (pair first (composition (composition second second) first)) listCons
|
| 26 |
+
let yes : Realizer (fun z : Fraction × State => z.1::z.2.2.2) :=
|
| 27 |
+
composition (pair first (composition (composition second second) second)) listCons
|
| 28 |
+
let no : Realizer (fun z : Fraction × State => z.2.1::z.1::z.2.2.1) :=
|
| 29 |
+
composition (pair q xs) listCons
|
| 30 |
+
exact pair q (pair xs (choose (composition (pair first q) TreeFraction.lessRealizer) yes no))
|
| 31 |
+
|
| 32 |
+
lemma polynomial_step : PolynomialTime stepRealizer := by
|
| 33 |
+
unfold stepRealizer
|
| 34 |
+
exact polynomial_pair (polynomial_composition polynomial_second polynomial_first)
|
| 35 |
+
(polynomial_pair
|
| 36 |
+
(polynomial_composition (polynomial_pair polynomial_first
|
| 37 |
+
(polynomial_composition (polynomial_composition polynomial_second polynomial_second)
|
| 38 |
+
polynomial_first)) polynomial_listCons)
|
| 39 |
+
(polynomial_choose
|
| 40 |
+
(polynomial_composition (polynomial_pair polynomial_first
|
| 41 |
+
(polynomial_composition polynomial_second polynomial_first)) TreeFraction.polynomial_less)
|
| 42 |
+
(polynomial_composition (polynomial_pair polynomial_first
|
| 43 |
+
(polynomial_composition (polynomial_composition polynomial_second polynomial_second)
|
| 44 |
+
polynomial_second)) polynomial_listCons)
|
| 45 |
+
(polynomial_composition (polynomial_pair
|
| 46 |
+
(polynomial_composition polynomial_second polynomial_first)
|
| 47 |
+
(polynomial_composition (polynomial_pair polynomial_first
|
| 48 |
+
(polynomial_composition (polynomial_composition polynomial_second polynomial_second)
|
| 49 |
+
polynomial_first)) polynomial_listCons)) polynomial_listCons)))
|
| 50 |
+
|
| 51 |
+
def insert (z : Fraction × List Fraction) : List Fraction :=
|
| 52 |
+
(z.2.reverse.foldl (fun s x => step (x,s)) (z.1,([],[z.1]))).2.2
|
| 53 |
+
|
| 54 |
+
lemma insert_fold (q : Fraction) (xs : List Fraction) :
|
| 55 |
+
xs.reverse.foldl (fun s x => step (x,s)) (q,([],[q])) = (q,(xs,insertRaw q xs)) := by
|
| 56 |
+
induction xs with
|
| 57 |
+
| nil => rfl
|
| 58 |
+
| cons x xs ih =>
|
| 59 |
+
simp only [List.reverse_cons,List.foldl_append,ih,List.foldl_cons,List.foldl_nil]
|
| 60 |
+
rfl
|
| 61 |
+
|
| 62 |
+
lemma insert_eq (q : Fraction) (xs : List Fraction) : insert (q,xs)=insertRaw q xs := by
|
| 63 |
+
unfold insert
|
| 64 |
+
rw [insert_fold]
|
| 65 |
+
|
| 66 |
+
lemma insert_weight (q : Fraction) (xs : List Fraction) :
|
| 67 |
+
weight (insertRaw q xs)=1+weight q+weight xs := by
|
| 68 |
+
induction xs with
|
| 69 |
+
| nil => simp [insertRaw]
|
| 70 |
+
| cons x xs ih =>
|
| 71 |
+
simp only [insertRaw]
|
| 72 |
+
split <;> simp only [weight_cons,ih]
|
| 73 |
+
all_goals omega
|
| 74 |
+
|
| 75 |
+
lemma fold_weight (xs : List Fraction) (q : Fraction) (zs ys : List Fraction) :
|
| 76 |
+
let t := xs.foldl (fun s x => step (x,s)) (q,(zs,ys))
|
| 77 |
+
t.1=q ∧ weight t.2.1+1=weight zs+weight xs ∧
|
| 78 |
+
weight t.2.2 ≤ max (weight ys) (weight q+weight zs+1)+weight xs := by
|
| 79 |
+
induction xs generalizing zs ys with
|
| 80 |
+
| nil =>
|
| 81 |
+
exact ⟨rfl,by simp only [List.foldl_nil,weight_nil],by
|
| 82 |
+
simpa only [List.foldl_nil,weight_nil] using
|
| 83 |
+
(Nat.le_max_left (weight ys) (weight q+weight zs+1)).trans (Nat.le_succ _)⟩
|
| 84 |
+
| cons x xs ih =>
|
| 85 |
+
rw [List.foldl_cons]
|
| 86 |
+
change (let t := xs.foldl (fun s x => step (x,s)) (q,(x::zs,if TreeFraction.less (x,q) then x::ys else q::x::zs));
|
| 87 |
+
t.1=q ∧ weight t.2.1+1=weight zs+weight (x::xs) ∧
|
| 88 |
+
weight t.2.2 ≤ max (weight ys) (weight q+weight zs+1)+weight (x::xs))
|
| 89 |
+
have hh := ih (x::zs) (if TreeFraction.less (x,q) then x::ys else q::x::zs)
|
| 90 |
+
dsimp only at hh ⊢
|
| 91 |
+
refine ⟨hh.1,?_,?_⟩
|
| 92 |
+
· simp only [weight_cons] at hh ⊢; omega
|
| 93 |
+
· by_cases h : TreeFraction.less (x,q)=true
|
| 94 |
+
· simp only [h,ite_true,weight_cons] at hh ⊢; omega
|
| 95 |
+
· simp only [h,Bool.false_eq_true,ite_false,weight_cons] at hh ⊢; omega
|
| 96 |
+
|
| 97 |
+
lemma state_weight (s : State) :
|
| 98 |
+
weight s=1+weight s.1+(1+weight s.2.1+weight s.2.2) := by
|
| 99 |
+
rcases s with ⟨q,xs,ys⟩
|
| 100 |
+
simp only [weight_prod]
|
| 101 |
+
|
| 102 |
+
lemma polynomial_stepFold : PolynomialTime (fold stepRealizer) := by
|
| 103 |
+
apply polynomial_fold polynomial_step (5*X+5)
|
| 104 |
+
intro xs s pre post he
|
| 105 |
+
rcases s with ⟨q,zs,ys⟩
|
| 106 |
+
have hh := fold_weight pre q zs ys
|
| 107 |
+
have hp := weight_prefix he
|
| 108 |
+
simp only [eval_add,eval_mul,eval_ofNat,eval_X,weight_prod]
|
| 109 |
+
rw [state_weight,hh.1]
|
| 110 |
+
omega
|
| 111 |
+
|
| 112 |
+
def insertRealizer : Realizer insert := by
|
| 113 |
+
let prep : Realizer (fun z : Fraction × List Fraction => (z.2.reverse,(z.1,([],[z.1])))) :=
|
| 114 |
+
pair (composition second reverse)
|
| 115 |
+
(pair first (pair (constant ([] : List Fraction)) (composition (pair first (constant ([] : List Fraction))) listCons)))
|
| 116 |
+
let tail : Realizer (fun z : List Fraction × State =>
|
| 117 |
+
(z.1.foldl (fun s x => step (x,s)) z.2).2.2) :=
|
| 118 |
+
composition (f := fun z : List Fraction × State =>
|
| 119 |
+
(z.1.foldl (fun s x => step (x,s)) z.2).2) (g := fun z => z.2)
|
| 120 |
+
(composition (fold stepRealizer) second) second
|
| 121 |
+
exact composition
|
| 122 |
+
(f := fun z : Fraction × List Fraction => (z.2.reverse,(z.1,([],[z.1]))))
|
| 123 |
+
(g := fun z : List Fraction × State =>
|
| 124 |
+
(z.1.foldl (fun s x => step (x,s)) z.2).2.2) prep tail
|
| 125 |
+
|
| 126 |
+
lemma polynomial_insert : PolynomialTime insertRealizer := by
|
| 127 |
+
unfold insertRealizer
|
| 128 |
+
exact polynomial_composition
|
| 129 |
+
(polynomial_pair (polynomial_composition polynomial_second polynomial_reverse)
|
| 130 |
+
(polynomial_pair polynomial_first (polynomial_pair (polynomial_constant ([] : List Fraction))
|
| 131 |
+
(polynomial_composition (polynomial_pair polynomial_first (polynomial_constant ([] : List Fraction)))
|
| 132 |
+
polynomial_listCons))))
|
| 133 |
+
(polynomial_composition (polynomial_composition polynomial_stepFold polynomial_second)
|
| 134 |
+
polynomial_second)
|
| 135 |
+
|
| 136 |
+
def sort (xs : List Fraction) : List Fraction :=
|
| 137 |
+
xs.reverse.foldl (fun s x => insert (x,s)) []
|
| 138 |
+
|
| 139 |
+
def sortRealizer : Realizer sort :=
|
| 140 |
+
composition (f := fun xs : List Fraction => (xs.reverse,([] : List Fraction)))
|
| 141 |
+
(g := fun z : List Fraction × List Fraction => z.1.foldl (fun s x => insert (x,s)) z.2)
|
| 142 |
+
(pair reverse (constant [])) (fold insertRealizer)
|
| 143 |
+
|
| 144 |
+
lemma sortFold_weight (xs zs : List Fraction) :
|
| 145 |
+
weight (xs.foldl (fun s x => insert (x,s)) zs)+1=weight zs+weight xs := by
|
| 146 |
+
induction xs generalizing zs with
|
| 147 |
+
| nil => simp
|
| 148 |
+
| cons x xs ih =>
|
| 149 |
+
have hh := ih (insert (x,zs))
|
| 150 |
+
simp only [List.foldl_cons,insert_eq,insert_weight,weight_cons] at hh ⊢
|
| 151 |
+
omega
|
| 152 |
+
|
| 153 |
+
lemma polynomial_insertFold : PolynomialTime (fold insertRealizer) := by
|
| 154 |
+
apply polynomial_fold polynomial_insert (2*X)
|
| 155 |
+
intro xs zs pre post he
|
| 156 |
+
have hh := sortFold_weight pre zs
|
| 157 |
+
have hp := weight_prefix he
|
| 158 |
+
simp only [eval_mul,eval_ofNat,eval_X,weight_prod]
|
| 159 |
+
omega
|
| 160 |
+
|
| 161 |
+
lemma polynomial_sort : PolynomialTime sortRealizer :=
|
| 162 |
+
polynomial_composition (polynomial_pair polynomial_reverse (polynomial_constant ([] : List Fraction)))
|
| 163 |
+
polynomial_insertFold
|
| 164 |
+
|
| 165 |
+
lemma insert_pos (q : Fraction) (xs : List Fraction) (hq : 0 < q.2)
|
| 166 |
+
(hx : ∀ x∈xs,0 < x.2) : ∀ y∈insertRaw q xs,0 < y.2 := by
|
| 167 |
+
induction xs with
|
| 168 |
+
| nil => simpa only [insertRaw,List.mem_singleton] using fun y hy => hy ▸ hq
|
| 169 |
+
| cons x xs ih =>
|
| 170 |
+
have hxx := hx x (by simp)
|
| 171 |
+
have ht := ih (fun y hy => hx y (by simp [hy]))
|
| 172 |
+
simp only [insertRaw]
|
| 173 |
+
split <;> intro y hy <;> simp only [List.mem_cons] at hy
|
| 174 |
+
· rcases hy with rfl|hy
|
| 175 |
+
· exact hxx
|
| 176 |
+
· exact ht y hy
|
| 177 |
+
· rcases hy with rfl|rfl|hy
|
| 178 |
+
· exact hq
|
| 179 |
+
· exact hxx
|
| 180 |
+
· exact hx y (by simp [hy])
|
| 181 |
+
|
| 182 |
+
lemma insert_map (q : Fraction) (xs : List Fraction) (hq : 0 < q.2)
|
| 183 |
+
(hx : ∀ x∈xs,0 < x.2) :
|
| 184 |
+
(insertRaw q xs).map val=(xs.map val).orderedInsert (·≤·) (val q) := by
|
| 185 |
+
induction xs with
|
| 186 |
+
| nil => rfl
|
| 187 |
+
| cons x xs ih =>
|
| 188 |
+
have hxx := hx x (by simp)
|
| 189 |
+
have ht := ih (fun y hy => hx y (by simp [hy]))
|
| 190 |
+
simp only [insertRaw,List.map_cons,List.orderedInsert_cons,TreeFraction.less_val x q hxx hq]
|
| 191 |
+
by_cases h : val x < val q
|
| 192 |
+
· simp [h,not_le.mpr h,ht]
|
| 193 |
+
· simp [h,le_of_not_gt h]
|
| 194 |
+
|
| 195 |
+
lemma sort_cons (x : Fraction) (xs : List Fraction) : sort (x::xs)=insert (x,sort xs) := by
|
| 196 |
+
simp only [sort,List.reverse_cons,List.foldl_append,List.foldl_cons,List.foldl_nil]
|
| 197 |
+
|
| 198 |
+
lemma sort_pos (xs : List Fraction) (hx : ∀ x∈xs,0 < x.2) : ∀ y∈sort xs,0 < y.2 := by
|
| 199 |
+
induction xs with
|
| 200 |
+
| nil => simp [sort]
|
| 201 |
+
| cons x xs ih =>
|
| 202 |
+
rw [sort_cons,insert_eq]
|
| 203 |
+
exact insert_pos x (sort xs) (hx x (by simp))
|
| 204 |
+
(ih (fun y hy => hx y (by simp [hy])))
|
| 205 |
+
|
| 206 |
+
lemma sort_map (xs : List Fraction) (hx : ∀ x∈xs,0 < x.2) :
|
| 207 |
+
(sort xs).map val=(xs.map val).mergeSort (·≤·) := by
|
| 208 |
+
rw [List.mergeSort_eq_insertionSort]
|
| 209 |
+
induction xs with
|
| 210 |
+
| nil => rfl
|
| 211 |
+
| cons x xs ih =>
|
| 212 |
+
have ht : ∀ y∈xs,0 < y.2 := fun y hy => hx y (by simp [hy])
|
| 213 |
+
rw [sort_cons,insert_eq,insert_map x (sort xs) (hx x (by simp)) (sort_pos xs ht),ih ht]
|
| 214 |
+
rfl
|
| 215 |
+
|
| 216 |
+
def median (xs : List Fraction) : Fraction :=
|
| 217 |
+
(sort (xs.map TreeFraction.normalize)).getD (xs.length/2) (0,1)
|
| 218 |
+
|
| 219 |
+
def medianRealizer : Realizer median :=
|
| 220 |
+
composition (f := fun xs : List Fraction => (sort (xs.map TreeFraction.normalize),xs.length/2))
|
| 221 |
+
(g := fun z : List Fraction × ℕ => z.1.getD z.2 (0,1)) (pair (composition (f := fun xs : List Fraction => xs.map TreeFraction.normalize)
|
| 222 |
+
(g := sort) (TreeTyped.map normalizeRealizer) sortRealizer)
|
| 223 |
+
(composition (pair listLength (constant 2)) quotient)) (getDFast (0,1))
|
| 224 |
+
|
| 225 |
+
lemma polynomial_median : PolynomialTime medianRealizer :=
|
| 226 |
+
polynomial_composition
|
| 227 |
+
(polynomial_pair (polynomial_composition (polynomial_map TreeFraction.polynomial_normalize)
|
| 228 |
+
polynomial_sort)
|
| 229 |
+
(polynomial_composition (polynomial_pair polynomial_listLength (polynomial_constant 2))
|
| 230 |
+
polynomial_quotient)) (polynomial_getDFast (0,1))
|
| 231 |
+
|
| 232 |
+
lemma median_val (xs : List Fraction) :
|
| 233 |
+
val (median xs)=CommonBasesFPRAS.rationalMedian (xs.map val) := by
|
| 234 |
+
have hs := sort_map (xs.map TreeFraction.normalize) (by
|
| 235 |
+
intro y hy
|
| 236 |
+
obtain ⟨x,_,rfl⟩ := List.mem_map.mp hy
|
| 237 |
+
exact normalize_den_pos x)
|
| 238 |
+
have hm : (xs.map TreeFraction.normalize).map val=xs.map val := by
|
| 239 |
+
simp only [List.map_map,Function.comp_def,normalize_val]
|
| 240 |
+
rw [hm] at hs
|
| 241 |
+
unfold median CommonBasesFPRAS.rationalMedian
|
| 242 |
+
rw [←hs,List.length_map]
|
| 243 |
+
simp only [List.getD_eq_getElem?_getD,List.getElem?_map]
|
| 244 |
+
cases (sort (xs.map TreeFraction.normalize))[xs.length/2]? <;> simp only [Option.getD_none,Option.map_none,Option.getD_some,Option.map_some,val, Nat.cast_zero,zero_div]
|
| 245 |
+
|
| 246 |
+
end ContingencyTables.MedianFractionProgram
|
| 247 |
+
|
| 248 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/NatListEquality.lean
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.IntegerMatrixProgram
|
| 2 |
+
|
| 3 |
+
namespace OAI
|
| 4 |
+
|
| 5 |
+
/-! # Binary list equality for matching tabulated states and outputs -/
|
| 6 |
+
|
| 7 |
+
namespace ContingencyTables.NatListEquality
|
| 8 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped
|
| 9 |
+
|
| 10 |
+
def indices (z : List ℕ×List ℕ) : List ℕ := List.range z.1.length
|
| 11 |
+
|
| 12 |
+
def indicesRealizer : Realizer indices := composition (composition first listLength) range
|
| 13 |
+
|
| 14 |
+
lemma polynomial_indices : PolynomialTime indicesRealizer := by
|
| 15 |
+
apply polynomial_boundedRange (polynomial_composition polynomial_first polynomial_listLength)
|
| 16 |
+
Polynomial.X
|
| 17 |
+
intro z
|
| 18 |
+
simpa only [Polynomial.eval_X,Function.comp_apply] using
|
| 19 |
+
(list_length_le_weight z.1).trans (weight_fst z)
|
| 20 |
+
|
| 21 |
+
def atEqual (z : (List ℕ×List ℕ)×ℕ) : Bool := decide (z.1.1.getD z.2 0=z.1.2.getD z.2 0)
|
| 22 |
+
|
| 23 |
+
def atEqualRealizer : Realizer atEqual :=
|
| 24 |
+
composition (pair
|
| 25 |
+
(composition (pair (composition first first) second) (getDFast 0))
|
| 26 |
+
(composition (pair (composition first second) second) (getDFast 0))) natEqual
|
| 27 |
+
|
| 28 |
+
lemma polynomial_atEqual : PolynomialTime atEqualRealizer :=
|
| 29 |
+
polynomial_composition (polynomial_pair
|
| 30 |
+
(polynomial_composition (polynomial_pair
|
| 31 |
+
(polynomial_composition polynomial_first polynomial_first) polynomial_second)
|
| 32 |
+
(polynomial_getDFast 0))
|
| 33 |
+
(polynomial_composition (polynomial_pair
|
| 34 |
+
(polynomial_composition polynomial_first polynomial_second) polynomial_second)
|
| 35 |
+
(polynomial_getDFast 0))) polynomial_natEqual
|
| 36 |
+
|
| 37 |
+
def listEqual (z : List ℕ×List ℕ) : Bool :=
|
| 38 |
+
decide (z.1.length=z.2.length) && (indices z).all (fun i => atEqual (z,i))
|
| 39 |
+
|
| 40 |
+
def listEqualRealizer : Realizer listEqual :=
|
| 41 |
+
(composition (pair
|
| 42 |
+
(composition (pair (composition first listLength) (composition second listLength)) natEqual)
|
| 43 |
+
(composition (composition (pair identity indicesRealizer) (mapWith atEqualRealizer)) all))
|
| 44 |
+
boolAnd).congr (by
|
| 45 |
+
intro z
|
| 46 |
+
simp only [Function.comp_apply,List.all_map,Function.comp_def,id_eq,listEqual])
|
| 47 |
+
|
| 48 |
+
theorem polynomial_listEqual : PolynomialTime listEqualRealizer := by
|
| 49 |
+
unfold listEqualRealizer
|
| 50 |
+
apply polynomial_congr
|
| 51 |
+
exact polynomial_composition (polynomial_pair
|
| 52 |
+
(polynomial_composition (polynomial_pair
|
| 53 |
+
(polynomial_composition polynomial_first polynomial_listLength)
|
| 54 |
+
(polynomial_composition polynomial_second polynomial_listLength)) polynomial_natEqual)
|
| 55 |
+
(polynomial_composition (polynomial_composition
|
| 56 |
+
(polynomial_pair polynomial_identity polynomial_indices)
|
| 57 |
+
(polynomial_mapWith polynomial_atEqual)) polynomial_all)) polynomial_boolAnd
|
| 58 |
+
|
| 59 |
+
theorem listEqual_true (xs ys : List ℕ) : listEqual (xs,ys)=true ↔ xs=ys := by
|
| 60 |
+
simp only [listEqual,Bool.and_eq_true,decide_eq_true_eq,List.all_eq_true]
|
| 61 |
+
constructor
|
| 62 |
+
· rintro ⟨hlen,h⟩
|
| 63 |
+
apply List.ext_getElem hlen
|
| 64 |
+
intro i hi hj
|
| 65 |
+
have hv:=of_decide_eq_true (h i (List.mem_range.mpr hi))
|
| 66 |
+
simpa only [atEqual,List.getD_eq_getElem?_getD,List.getElem?_eq_getElem hi,
|
| 67 |
+
List.getElem?_eq_getElem hj,Option.getD_some] using hv
|
| 68 |
+
· intro h
|
| 69 |
+
subst ys
|
| 70 |
+
exact ⟨rfl,fun _ _ => by simp [atEqual]⟩
|
| 71 |
+
|
| 72 |
+
end ContingencyTables.NatListEquality
|
| 73 |
+
|
| 74 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/NaturalBlockProgram.lean
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.DependentWordProgram
|
| 2 |
+
import Mathlib.Data.List.SplitLengths
|
| 3 |
+
|
| 4 |
+
namespace OAI
|
| 5 |
+
|
| 6 |
+
/-! # Bounded scans of natural-number blocks
|
| 7 |
+
|
| 8 |
+
Numeric widths select bounded slices of the supplied list. They never
|
| 9 |
+
control an allocation, so malformed widths preserve polynomial cost.
|
| 10 |
+
-/
|
| 11 |
+
|
| 12 |
+
namespace ContingencyTables.NaturalBlockProgram
|
| 13 |
+
|
| 14 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped Polynomial
|
| 15 |
+
|
| 16 |
+
abbrev Input := List ℕ × List ℕ
|
| 17 |
+
abbrev Entry := Input × ℕ
|
| 18 |
+
|
| 19 |
+
def start (z : Entry) : ℕ := (z.1.1.take z.2).sum
|
| 20 |
+
def startRealizer : Realizer start :=
|
| 21 |
+
composition (composition (pair (composition first first) second) takeFast) listSum
|
| 22 |
+
lemma polynomial_start : PolynomialTime startRealizer :=
|
| 23 |
+
polynomial_composition (polynomial_composition (polynomial_pair
|
| 24 |
+
(polynomial_composition polynomial_first polynomial_first) polynomial_second)
|
| 25 |
+
polynomial_takeFast) polynomial_listSum
|
| 26 |
+
|
| 27 |
+
def block (z : Entry) : List ℕ :=
|
| 28 |
+
(z.1.2.drop (start z)).take (z.1.1.getD z.2 0)
|
| 29 |
+
def blockRealizer : Realizer block :=
|
| 30 |
+
composition (pair
|
| 31 |
+
(composition (pair (composition first second) startRealizer) dropFast)
|
| 32 |
+
(composition (pair (composition first first) second) (getDFast 0))) takeFast
|
| 33 |
+
lemma polynomial_block : PolynomialTime blockRealizer :=
|
| 34 |
+
polynomial_composition (polynomial_pair
|
| 35 |
+
(polynomial_composition (polynomial_pair
|
| 36 |
+
(polynomial_composition polynomial_first polynomial_second) polynomial_start)
|
| 37 |
+
polynomial_dropFast)
|
| 38 |
+
(polynomial_composition (polynomial_pair
|
| 39 |
+
(polynomial_composition polynomial_first polynomial_first) polynomial_second)
|
| 40 |
+
(polynomial_getDFast 0))) polynomial_takeFast
|
| 41 |
+
|
| 42 |
+
def indices (z : Input) : List ℕ := List.range z.1.length
|
| 43 |
+
def indicesRealizer : Realizer indices := composition (composition first listLength) range
|
| 44 |
+
lemma polynomial_indices : PolynomialTime indicesRealizer := by
|
| 45 |
+
apply polynomial_boundedRange (polynomial_composition polynomial_first polynomial_listLength) X
|
| 46 |
+
intro z
|
| 47 |
+
simpa only [Function.comp_apply,eval_X] using
|
| 48 |
+
(list_length_le_weight z.1).trans (weight_fst z)
|
| 49 |
+
|
| 50 |
+
def blocks (z : Input) : List (List ℕ) := (indices z).map (fun i => block (z,i))
|
| 51 |
+
def blocksRealizer : Realizer blocks :=
|
| 52 |
+
composition (pair identity indicesRealizer) (mapWith blockRealizer)
|
| 53 |
+
theorem polynomial_blocks : PolynomialTime blocksRealizer :=
|
| 54 |
+
polynomial_composition (polynomial_pair polynomial_identity polynomial_indices)
|
| 55 |
+
(polynomial_mapWith polynomial_block)
|
| 56 |
+
|
| 57 |
+
lemma block_splitLengths (widths values : List ℕ) (i : ℕ) :
|
| 58 |
+
block ((widths,values),i)=(widths.splitLengths values).getD i [] := by
|
| 59 |
+
induction widths generalizing values i with
|
| 60 |
+
| nil => simp [block,start]
|
| 61 |
+
| cons w ws ih =>
|
| 62 |
+
cases i with
|
| 63 |
+
| zero => simp [block,start,List.splitLengths_cons]
|
| 64 |
+
| succ i =>
|
| 65 |
+
simpa only [block,start,List.take_succ_cons,List.sum_cons,List.getD_cons_succ,
|
| 66 |
+
List.splitLengths_cons,List.drop_drop,Nat.add_comm] using ih (values.drop w) i
|
| 67 |
+
|
| 68 |
+
theorem blocks_eq_splitLengths (widths values : List ℕ) :
|
| 69 |
+
blocks (widths,values)=widths.splitLengths values := by
|
| 70 |
+
apply List.ext_getElem
|
| 71 |
+
· simp [blocks,indices]
|
| 72 |
+
· intro i hi hj
|
| 73 |
+
have hiw : i < widths.length := by
|
| 74 |
+
simpa only [blocks,indices,List.length_map,List.length_range] using hi
|
| 75 |
+
simp only [blocks,indices,List.getElem_map,List.getElem_range]
|
| 76 |
+
have h := block_splitLengths widths values i
|
| 77 |
+
simpa only [List.getD_eq_getElem?_getD,List.getElem?_eq_getElem hj,Option.getD_some] using h
|
| 78 |
+
|
| 79 |
+
lemma splitLengths_flatten (rows : List (List ℕ)) :
|
| 80 |
+
(rows.map List.length).splitLengths rows.flatten=rows := by
|
| 81 |
+
induction rows with
|
| 82 |
+
| nil => rfl
|
| 83 |
+
| cons row rows ih =>
|
| 84 |
+
simpa only [List.map_cons,List.flatten_cons,List.splitLengths_cons,
|
| 85 |
+
List.take_left,List.drop_left] using congrArg (List.cons row) ih
|
| 86 |
+
|
| 87 |
+
/-- A literal list of rows is recovered exactly from its stored row
|
| 88 |
+
lengths and its flattened code, including rows of length zero. -/
|
| 89 |
+
theorem blocks_flatten (rows : List (List ℕ)) :
|
| 90 |
+
blocks (rows.map List.length,rows.flatten)=rows := by
|
| 91 |
+
rw [blocks_eq_splitLengths,splitLengths_flatten]
|
| 92 |
+
|
| 93 |
+
end ContingencyTables.NaturalBlockProgram
|
| 94 |
+
|
| 95 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/NetworkCapacityProgram.lean
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Transport.NetworkVertexCode
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Machines.FlowLoopProgram
|
| 3 |
+
|
| 4 |
+
namespace OAI
|
| 5 |
+
|
| 6 |
+
/-! # Constructing the bounded-table capacity network in binary -/
|
| 7 |
+
|
| 8 |
+
namespace ContingencyTables.NetworkCapacityProgram
|
| 9 |
+
|
| 10 |
+
open MatchingFPRAS.TreeTyped Polynomial NetworkVertexCode ResidualGraphProgram
|
| 11 |
+
|
| 12 |
+
abbrev EntryInput := Input×(ℕ×ℕ)
|
| 13 |
+
|
| 14 |
+
def kindRealizer : Realizer (fun z : ℕ×ℕ=>kind z.1 z.2) :=
|
| 15 |
+
(choose (composition (pair second (constant 0)) natEqual) (constant 0)
|
| 16 |
+
(choose (composition (pair second (constant 1)) natEqual) (constant 1)
|
| 17 |
+
(choose (composition (pair second (composition (pair (constant 2) first) add)) less)
|
| 18 |
+
(constant 2) (constant 3)))).congr (by
|
| 19 |
+
intro z
|
| 20 |
+
simp only [Function.comp_apply,kind,decide_eq_true_eq])
|
| 21 |
+
|
| 22 |
+
lemma polynomial_kind : PolynomialTime kindRealizer :=
|
| 23 |
+
polynomial_congr _ (polynomial_choose
|
| 24 |
+
(polynomial_composition (polynomial_pair polynomial_second (polynomial_constant 0))
|
| 25 |
+
polynomial_natEqual) (polynomial_constant 0)
|
| 26 |
+
(polynomial_choose
|
| 27 |
+
(polynomial_composition (polynomial_pair polynomial_second (polynomial_constant 1))
|
| 28 |
+
polynomial_natEqual) (polynomial_constant 1)
|
| 29 |
+
(polynomial_choose
|
| 30 |
+
(polynomial_composition (polynomial_pair polynomial_second
|
| 31 |
+
(polynomial_composition (polynomial_pair (polynomial_constant 2) polynomial_first)
|
| 32 |
+
polynomial_add)) polynomial_less)
|
| 33 |
+
(polynomial_constant 2) (polynomial_constant 3))))
|
| 34 |
+
|
| 35 |
+
def rowCount (z : EntryInput) : ℕ := z.1.1.1.length
|
| 36 |
+
|
| 37 |
+
def rowCountRealizer : Realizer rowCount :=
|
| 38 |
+
composition (composition (composition first first) first) listLength
|
| 39 |
+
|
| 40 |
+
lemma polynomial_rowCount : PolynomialTime rowCountRealizer :=
|
| 41 |
+
polynomial_composition (polynomial_composition
|
| 42 |
+
(polynomial_composition polynomial_first polynomial_first) polynomial_first)
|
| 43 |
+
polynomial_listLength
|
| 44 |
+
|
| 45 |
+
def columnBase (z : EntryInput) : ℕ := 2+rowCount z
|
| 46 |
+
|
| 47 |
+
def columnBaseRealizer : Realizer columnBase :=
|
| 48 |
+
composition (f := fun z : EntryInput=>(2,rowCount z))
|
| 49 |
+
(g := fun a : ℕ×ℕ=>a.1+a.2) (pair (constant 2) rowCountRealizer) add
|
| 50 |
+
|
| 51 |
+
lemma polynomial_columnBase : PolynomialTime columnBaseRealizer :=
|
| 52 |
+
polynomial_composition (polynomial_pair (polynomial_constant 2) polynomial_rowCount)
|
| 53 |
+
polynomial_add
|
| 54 |
+
|
| 55 |
+
def kinds (z : EntryInput) : ℕ×ℕ :=
|
| 56 |
+
(kind (rowCount z) z.2.1,kind (rowCount z) z.2.2)
|
| 57 |
+
|
| 58 |
+
def kindsRealizer : Realizer kinds :=
|
| 59 |
+
pair (composition (pair rowCountRealizer (composition second first)) kindRealizer)
|
| 60 |
+
(composition (pair rowCountRealizer (composition second second)) kindRealizer)
|
| 61 |
+
|
| 62 |
+
lemma polynomial_kinds : PolynomialTime kindsRealizer :=
|
| 63 |
+
polynomial_pair (polynomial_composition (polynomial_pair polynomial_rowCount
|
| 64 |
+
(polynomial_composition polynomial_second polynomial_first)) polynomial_kind)
|
| 65 |
+
(polynomial_composition (polynomial_pair polynomial_rowCount
|
| 66 |
+
(polynomial_composition polynomial_second polynomial_second)) polynomial_kind)
|
| 67 |
+
|
| 68 |
+
def pattern (a b : ℕ) (z : EntryInput) : Bool :=
|
| 69 |
+
decide ((kinds z).1=a) && decide ((kinds z).2=b)
|
| 70 |
+
|
| 71 |
+
def patternRealizer (a b : ℕ) : Realizer (pattern a b) :=
|
| 72 |
+
composition kindsRealizer (composition (pair
|
| 73 |
+
(composition (pair first (constant a)) natEqual)
|
| 74 |
+
(composition (pair second (constant b)) natEqual)) boolAnd)
|
| 75 |
+
|
| 76 |
+
lemma polynomial_pattern (a b : ℕ) : PolynomialTime (patternRealizer a b) :=
|
| 77 |
+
polynomial_composition polynomial_kinds (polynomial_composition
|
| 78 |
+
(polynomial_pair
|
| 79 |
+
(polynomial_composition (polynomial_pair polynomial_first (polynomial_constant a))
|
| 80 |
+
polynomial_natEqual)
|
| 81 |
+
(polynomial_composition (polynomial_pair polynomial_second (polynomial_constant b))
|
| 82 |
+
polynomial_natEqual)) polynomial_boolAnd)
|
| 83 |
+
|
| 84 |
+
def rowRead (z : EntryInput) : ℕ := z.1.1.1.getD (z.2.2-2) 0
|
| 85 |
+
|
| 86 |
+
def rowReadRealizer : Realizer rowRead :=
|
| 87 |
+
composition (pair (composition (composition first first) first)
|
| 88 |
+
(composition (pair (composition second second) (constant 2)) subtract)) (getDFast 0)
|
| 89 |
+
|
| 90 |
+
lemma polynomial_rowRead : PolynomialTime rowReadRealizer :=
|
| 91 |
+
polynomial_composition (polynomial_pair
|
| 92 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 93 |
+
polynomial_first)
|
| 94 |
+
(polynomial_composition (polynomial_pair
|
| 95 |
+
(polynomial_composition polynomial_second polynomial_second) (polynomial_constant 2))
|
| 96 |
+
polynomial_subtract)) (polynomial_getDFast 0)
|
| 97 |
+
|
| 98 |
+
def columnRead (z : EntryInput) : ℕ := z.1.1.2.getD (z.2.1-columnBase z) 0
|
| 99 |
+
|
| 100 |
+
def columnReadRealizer : Realizer columnRead :=
|
| 101 |
+
composition (pair (composition (composition first first) second)
|
| 102 |
+
(composition (pair (composition second first) columnBaseRealizer) subtract)) (getDFast 0)
|
| 103 |
+
|
| 104 |
+
lemma polynomial_columnRead : PolynomialTime columnReadRealizer :=
|
| 105 |
+
polynomial_composition (polynomial_pair
|
| 106 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 107 |
+
polynomial_second)
|
| 108 |
+
(polynomial_composition (polynomial_pair
|
| 109 |
+
(polynomial_composition polynomial_second polynomial_first) polynomial_columnBase)
|
| 110 |
+
polynomial_subtract)) (polynomial_getDFast 0)
|
| 111 |
+
|
| 112 |
+
def boundRead (z : EntryInput) : ℕ :=
|
| 113 |
+
capacityEntry (z.1.2,(z.2.1-2,z.2.2-columnBase z))
|
| 114 |
+
|
| 115 |
+
def boundReadRealizer : Realizer boundRead :=
|
| 116 |
+
composition (pair (composition first second)
|
| 117 |
+
(pair (composition (pair (composition second first) (constant 2)) subtract)
|
| 118 |
+
(composition (pair (composition second second) columnBaseRealizer) subtract)))
|
| 119 |
+
capacityEntryRealizer
|
| 120 |
+
|
| 121 |
+
lemma polynomial_boundRead : PolynomialTime boundReadRealizer :=
|
| 122 |
+
polynomial_composition (polynomial_pair
|
| 123 |
+
(polynomial_composition polynomial_first polynomial_second)
|
| 124 |
+
(polynomial_pair
|
| 125 |
+
(polynomial_composition (polynomial_pair
|
| 126 |
+
(polynomial_composition polynomial_second polynomial_first) (polynomial_constant 2))
|
| 127 |
+
polynomial_subtract)
|
| 128 |
+
(polynomial_composition (polynomial_pair
|
| 129 |
+
(polynomial_composition polynomial_second polynomial_second) polynomial_columnBase)
|
| 130 |
+
polynomial_subtract))) polynomial_capacityEntry
|
| 131 |
+
|
| 132 |
+
def entryRealizer : Realizer entry :=
|
| 133 |
+
(choose (patternRealizer 0 2) rowReadRealizer
|
| 134 |
+
(choose (patternRealizer 2 3) boundReadRealizer
|
| 135 |
+
(choose (patternRealizer 3 1) columnReadRealizer (constant 0)))).congr (by
|
| 136 |
+
intro z
|
| 137 |
+
have hp (a b : ℕ) : pattern a b z=true ↔
|
| 138 |
+
kind z.1.1.1.length z.2.1=a ∧ kind z.1.1.1.length z.2.2=b := by
|
| 139 |
+
change ((decide (kind z.1.1.1.length z.2.1=a)) &&
|
| 140 |
+
(decide (kind z.1.1.1.length z.2.2=b)))=true ↔ _
|
| 141 |
+
rw [Bool.and_eq_true]
|
| 142 |
+
constructor
|
| 143 |
+
· rintro ⟨ha,hb⟩
|
| 144 |
+
exact ⟨_root_.of_decide_eq_true ha,_root_.of_decide_eq_true hb⟩
|
| 145 |
+
· rintro ⟨ha,hb⟩
|
| 146 |
+
exact ⟨_root_.decide_eq_true ha,_root_.decide_eq_true hb⟩
|
| 147 |
+
simp only [hp,columnBase,rowRead,columnRead,boundRead,entry,rowCount])
|
| 148 |
+
|
| 149 |
+
lemma polynomial_entry : PolynomialTime entryRealizer :=
|
| 150 |
+
polynomial_congr _ (polynomial_choose (polynomial_pattern 0 2) polynomial_rowRead
|
| 151 |
+
(polynomial_choose (polynomial_pattern 2 3) polynomial_boundRead
|
| 152 |
+
(polynomial_choose (polynomial_pattern 3 1) polynomial_columnRead (polynomial_constant 0))))
|
| 153 |
+
|
| 154 |
+
def vertexCountRealizer : Realizer vertexCount :=
|
| 155 |
+
composition (f := fun z : Input=>(2,z.1.1.length+z.1.2.length))
|
| 156 |
+
(g := fun a : ℕ×ℕ=>a.1+a.2)
|
| 157 |
+
(pair (constant 2) (composition
|
| 158 |
+
(f := fun z : Input=>(z.1.1.length,z.1.2.length))
|
| 159 |
+
(g := fun a : ℕ×ℕ=>a.1+a.2)
|
| 160 |
+
(pair (composition (composition first first) listLength)
|
| 161 |
+
(composition (composition first second) listLength)) add)) add
|
| 162 |
+
|
| 163 |
+
lemma polynomial_vertexCount : PolynomialTime vertexCountRealizer :=
|
| 164 |
+
polynomial_composition (polynomial_pair (polynomial_constant 2)
|
| 165 |
+
(polynomial_composition (polynomial_pair
|
| 166 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 167 |
+
polynomial_listLength)
|
| 168 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_second)
|
| 169 |
+
polynomial_listLength)) polynomial_add)) polynomial_add
|
| 170 |
+
|
| 171 |
+
def vertices (z : Input) : List ℕ := List.range (vertexCount z)
|
| 172 |
+
def verticesRealizer : Realizer vertices := composition vertexCountRealizer range
|
| 173 |
+
|
| 174 |
+
lemma polynomial_vertices : PolynomialTime verticesRealizer := by
|
| 175 |
+
apply polynomial_boundedRange polynomial_vertexCount (2+2*X)
|
| 176 |
+
intro z
|
| 177 |
+
have hr := (list_length_le_weight z.1.1).trans ((weight_fst z.1).trans (weight_fst z))
|
| 178 |
+
have hc := (list_length_le_weight z.1.2).trans ((weight_snd z.1).trans (weight_fst z))
|
| 179 |
+
simp only [vertexCount,eval_add,eval_ofNat,eval_mul,eval_X]
|
| 180 |
+
omega
|
| 181 |
+
|
| 182 |
+
def row (z : Input×ℕ) : List ℕ :=
|
| 183 |
+
(vertices z.1).map (fun v=>entry (z.1,(z.2,v)))
|
| 184 |
+
|
| 185 |
+
def rowRealizer : Realizer row :=
|
| 186 |
+
composition (pair identity (composition first verticesRealizer))
|
| 187 |
+
(mapWith (composition
|
| 188 |
+
(pair (composition first first) (pair (composition first second) second)) entryRealizer))
|
| 189 |
+
|
| 190 |
+
lemma polynomial_row : PolynomialTime rowRealizer :=
|
| 191 |
+
polynomial_composition (polynomial_pair polynomial_identity
|
| 192 |
+
(polynomial_composition polynomial_first polynomial_vertices))
|
| 193 |
+
(polynomial_mapWith (polynomial_composition (polynomial_pair
|
| 194 |
+
(polynomial_composition polynomial_first polynomial_first)
|
| 195 |
+
(polynomial_pair (polynomial_composition polynomial_first polynomial_second)
|
| 196 |
+
polynomial_second)) polynomial_entry))
|
| 197 |
+
|
| 198 |
+
def matrixRealizer : Realizer matrix :=
|
| 199 |
+
composition (pair identity verticesRealizer) (mapWith rowRealizer)
|
| 200 |
+
|
| 201 |
+
lemma polynomial_matrix : PolynomialTime matrixRealizer :=
|
| 202 |
+
polynomial_composition (polynomial_pair polynomial_identity polynomial_vertices)
|
| 203 |
+
(polynomial_mapWith polynomial_row)
|
| 204 |
+
|
| 205 |
+
def solve (z : Input) : FlowMatrix := FlowLoopProgram.run (matrix z,(0,1))
|
| 206 |
+
|
| 207 |
+
def solveRealizer : Realizer solve :=
|
| 208 |
+
composition (pair matrixRealizer (constant (0,1))) FlowLoopProgram.runRealizer
|
| 209 |
+
|
| 210 |
+
lemma polynomial_solve : PolynomialTime solveRealizer :=
|
| 211 |
+
polynomial_composition (polynomial_pair polynomial_matrix (polynomial_constant (0,1)))
|
| 212 |
+
FlowLoopProgram.polynomial_run
|
| 213 |
+
|
| 214 |
+
theorem solve_code {m n : ℕ} (r : Fin m→ℕ) (c : Fin n→ℕ) (b : Fin m→Fin n→ℕ) :
|
| 215 |
+
solve (data r c b)=flowCode (EdmondsKarpAlgorithm.maxFlow
|
| 216 |
+
(BoundedTableFeasibility.networkCapacity r c b)
|
| 217 |
+
(BoundedTableFeasibility.vertices m n LayeredTableNetwork.source)
|
| 218 |
+
(BoundedTableFeasibility.vertices m n LayeredTableNetwork.sink)) := by
|
| 219 |
+
have hs : (BoundedTableFeasibility.vertices m n (LayeredTableNetwork.source :
|
| 220 |
+
LayeredTableNetwork.Vertex m n)).val=0 := index_vertices _
|
| 221 |
+
have ht : (BoundedTableFeasibility.vertices m n (LayeredTableNetwork.sink :
|
| 222 |
+
LayeredTableNetwork.Vertex m n)).val=1 := index_vertices _
|
| 223 |
+
unfold solve
|
| 224 |
+
rw [matrix_code]
|
| 225 |
+
simpa only [hs,ht] using FlowLoopProgram.run_code
|
| 226 |
+
(BoundedTableFeasibility.networkCapacity r c b)
|
| 227 |
+
(BoundedTableFeasibility.vertices m n LayeredTableNetwork.source)
|
| 228 |
+
(BoundedTableFeasibility.vertices m n LayeredTableNetwork.sink)
|
| 229 |
+
BoundedTableFeasibility.source_ne_sink
|
| 230 |
+
|
| 231 |
+
end ContingencyTables.NetworkCapacityProgram
|
| 232 |
+
|
| 233 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/NetworkFeasibilityProgram.lean
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.NetworkCapacityProgram
|
| 2 |
+
|
| 3 |
+
namespace OAI
|
| 4 |
+
|
| 5 |
+
/-! # The actual polynomial binary bounded-table feasibility test -/
|
| 6 |
+
|
| 7 |
+
namespace ContingencyTables.NetworkFeasibilityProgram
|
| 8 |
+
|
| 9 |
+
open MatchingFPRAS.TreeTyped NetworkVertexCode NetworkCapacityProgram ResidualGraphProgram
|
| 10 |
+
open scoped BigOperators
|
| 11 |
+
|
| 12 |
+
def sourcePair (z : Input) : SignedPairProgram.Signed :=
|
| 13 |
+
SignedPairProgram.sum ((solve z).getD 0 [])
|
| 14 |
+
|
| 15 |
+
def sourcePairRealizer : Realizer sourcePair :=
|
| 16 |
+
composition (composition (pair solveRealizer (constant 0)) (getDFast []))
|
| 17 |
+
SignedPairProgram.sumRealizer
|
| 18 |
+
|
| 19 |
+
lemma polynomial_sourcePair : PolynomialTime sourcePairRealizer :=
|
| 20 |
+
polynomial_composition (polynomial_composition
|
| 21 |
+
(polynomial_pair polynomial_solve (polynomial_constant 0)) (polynomial_getDFast []))
|
| 22 |
+
SignedPairProgram.polynomial_sum
|
| 23 |
+
|
| 24 |
+
def inputValues (z : Input) : (ℕ×ℕ)×SignedPairProgram.Signed :=
|
| 25 |
+
((z.1.1.sum,z.1.2.sum),sourcePair z)
|
| 26 |
+
|
| 27 |
+
def inputValuesRealizer : Realizer inputValues :=
|
| 28 |
+
pair (pair (composition (composition first first) listSum)
|
| 29 |
+
(composition (composition first second) listSum)) sourcePairRealizer
|
| 30 |
+
|
| 31 |
+
lemma polynomial_inputValues : PolynomialTime inputValuesRealizer :=
|
| 32 |
+
polynomial_pair (polynomial_pair
|
| 33 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 34 |
+
polynomial_listSum)
|
| 35 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_second)
|
| 36 |
+
polynomial_listSum)) polynomial_sourcePair
|
| 37 |
+
|
| 38 |
+
def testValues (z : (ℕ×ℕ)×SignedPairProgram.Signed) : Bool :=
|
| 39 |
+
decide (z.1.1=z.1.2) && decide (z.2.1=z.1.1+z.2.2)
|
| 40 |
+
|
| 41 |
+
def testValuesRealizer : Realizer testValues :=
|
| 42 |
+
composition (pair (composition first natEqual)
|
| 43 |
+
(composition (pair (composition second first)
|
| 44 |
+
(composition (pair (composition first first) (composition second second)) add))
|
| 45 |
+
natEqual)) boolAnd
|
| 46 |
+
|
| 47 |
+
lemma polynomial_testValues : PolynomialTime testValuesRealizer :=
|
| 48 |
+
polynomial_composition (polynomial_pair
|
| 49 |
+
(polynomial_composition polynomial_first polynomial_natEqual)
|
| 50 |
+
(polynomial_composition (polynomial_pair
|
| 51 |
+
(polynomial_composition polynomial_second polynomial_first)
|
| 52 |
+
(polynomial_composition (polynomial_pair
|
| 53 |
+
(polynomial_composition polynomial_first polynomial_first)
|
| 54 |
+
(polynomial_composition polynomial_second polynomial_second)) polynomial_add))
|
| 55 |
+
polynomial_natEqual)) polynomial_boolAnd
|
| 56 |
+
|
| 57 |
+
def test (z : Input) : Bool := testValues (inputValues z)
|
| 58 |
+
def testRealizer : Realizer test := composition inputValuesRealizer testValuesRealizer
|
| 59 |
+
|
| 60 |
+
lemma polynomial_test : PolynomialTime testRealizer :=
|
| 61 |
+
polynomial_composition polynomial_inputValues polynomial_testValues
|
| 62 |
+
|
| 63 |
+
lemma flowCode_row_value {n : ℕ} (f : PublishedEdmondsKarp.Flow n) (s : Fin n) :
|
| 64 |
+
SignedPairProgram.value (SignedPairProgram.sum ((flowCode f).getD s.val []))=
|
| 65 |
+
PublishedEdmondsKarp.value s f := by
|
| 66 |
+
have hr : (flowCode f).getD s.val []=
|
| 67 |
+
List.ofFn (fun v=>ForestAffineProgram.signedParts (f s v)) := by
|
| 68 |
+
simp only [flowCode,List.getD_eq_getElem?_getD,List.getElem?_ofFn,s.isLt,
|
| 69 |
+
dite_true,Option.getD_some,Fin.eta]
|
| 70 |
+
rw [hr,SignedPairProgram.value_sum]
|
| 71 |
+
simp only [List.map_ofFn,Function.comp_def,SignedPairProgram.value,
|
| 72 |
+
ForestAffineProgram.signedParts_sub,List.sum_ofFn,PublishedEdmondsKarp.value]
|
| 73 |
+
|
| 74 |
+
lemma sourcePair_value {m n : ℕ} (r : Fin m→ℕ) (c : Fin n→ℕ) (b : Fin m→Fin n→ℕ) :
|
| 75 |
+
SignedPairProgram.value (sourcePair (data r c b))=
|
| 76 |
+
LayeredTableNetwork.value (BoundedTableFeasibility.computedFlow r c b) := by
|
| 77 |
+
unfold sourcePair
|
| 78 |
+
rw [solve_code]
|
| 79 |
+
have hs : (BoundedTableFeasibility.vertices m n (LayeredTableNetwork.source :
|
| 80 |
+
LayeredTableNetwork.Vertex m n)).val=0 := index_vertices _
|
| 81 |
+
rw [←hs,flowCode_row_value]
|
| 82 |
+
exact (BoundedTableFeasibility.pull_value _).symm
|
| 83 |
+
|
| 84 |
+
lemma test_eq_feasible {m n : ℕ} (r : Fin m→ℕ) (c : Fin n→ℕ) (b : Fin m→Fin n→ℕ) :
|
| 85 |
+
test (data r c b)=BoundedTableFeasibility.feasibleTest r c b := by
|
| 86 |
+
apply Bool.eq_iff_iff.mpr
|
| 87 |
+
simp only [test,testValues,inputValues,Bool.and_eq_true,decide_eq_true_eq,
|
| 88 |
+
BoundedTableFeasibility.feasibleTest,data,List.sum_ofFn]
|
| 89 |
+
have hv := sourcePair_value r c b
|
| 90 |
+
have he : (sourcePair (data r c b)).1=
|
| 91 |
+
(∑ i,r i)+(sourcePair (data r c b)).2 ↔
|
| 92 |
+
SignedPairProgram.value (sourcePair (data r c b))=∑ i,(r i:ℤ) := by
|
| 93 |
+
unfold SignedPairProgram.value
|
| 94 |
+
rw [←Nat.cast_sum]
|
| 95 |
+
omega
|
| 96 |
+
exact and_congr_right (fun _=>he.trans (by rw [hv]))
|
| 97 |
+
|
| 98 |
+
theorem test_correct {m n : ℕ} (hEK : PublishedEdmondsKarp.EdmondsKarp1972Theorem1)
|
| 99 |
+
(r : Fin m→ℕ) (c : Fin n→ℕ) (b : Fin m→Fin n→ℕ) :
|
| 100 |
+
test (data r c b)=true ↔ ∃ X,HasMargins X r c ∧ ∀ i j,X i j≤b i j := by
|
| 101 |
+
rw [test_eq_feasible]
|
| 102 |
+
exact BoundedTableFeasibility.feasibleTest_correct hEK r c b
|
| 103 |
+
|
| 104 |
+
end ContingencyTables.NetworkFeasibilityProgram
|
| 105 |
+
|
| 106 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/NetworkTableProgram.lean
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.NetworkFeasibilityProgram
|
| 2 |
+
|
| 3 |
+
namespace OAI
|
| 4 |
+
|
| 5 |
+
/-! # Extracting the bounded table from the executed flow
|
| 6 |
+
|
| 7 |
+
The output reads the row-to-column arcs of the computed flow. Both the
|
| 8 |
+
matrix dimensions and the encoded entries are obtained from the input;
|
| 9 |
+
there is no choice of a mathematical witness in the program.
|
| 10 |
+
-/
|
| 11 |
+
|
| 12 |
+
namespace ContingencyTables.NetworkTableProgram
|
| 13 |
+
|
| 14 |
+
open MatchingFPRAS.TreeTyped Polynomial NetworkVertexCode ResidualGraphProgram
|
| 15 |
+
open scoped BigOperators
|
| 16 |
+
|
| 17 |
+
abbrev Prepared := (List ℕ×List ℕ)×FlowMatrix
|
| 18 |
+
abbrev CellInput := Prepared×(ℕ×ℕ)
|
| 19 |
+
|
| 20 |
+
def cell (z : CellInput) : ℕ :=
|
| 21 |
+
let a := flowEntry (z.1.2,(2+z.2.1,2+z.1.1.1.length+z.2.2))
|
| 22 |
+
a.1-a.2
|
| 23 |
+
|
| 24 |
+
def cellRealizer : Realizer cell := by
|
| 25 |
+
let F : Realizer (fun z : CellInput=>z.1.2) := composition first second
|
| 26 |
+
let I : Realizer (fun z : CellInput=>2+z.2.1) :=
|
| 27 |
+
composition (f := fun z : CellInput=>(2,z.2.1))
|
| 28 |
+
(g := fun a : ℕ×ℕ=>a.1+a.2) (pair (constant 2) (composition second first)) add
|
| 29 |
+
let J : Realizer (fun z : CellInput=>2+z.1.1.1.length+z.2.2) :=
|
| 30 |
+
composition (f := fun z : CellInput=>(2+z.1.1.1.length,z.2.2))
|
| 31 |
+
(g := fun a : ℕ×ℕ=>a.1+a.2)
|
| 32 |
+
(pair (composition (f := fun z : CellInput=>(2,z.1.1.1.length))
|
| 33 |
+
(g := fun a : ℕ×ℕ=>a.1+a.2)
|
| 34 |
+
(pair (constant 2) (composition (composition (composition first first) first) listLength)) add)
|
| 35 |
+
(composition second second)) add
|
| 36 |
+
exact composition (f := fun z : CellInput=>
|
| 37 |
+
flowEntry (z.1.2,(2+z.2.1,2+z.1.1.1.length+z.2.2)))
|
| 38 |
+
(g := fun a : SignedPairProgram.Signed=>a.1-a.2)
|
| 39 |
+
(composition (f := fun z : CellInput=>
|
| 40 |
+
(z.1.2,(2+z.2.1,2+z.1.1.1.length+z.2.2))) (g := flowEntry)
|
| 41 |
+
(pair F (pair I J)) flowEntryRealizer) subtract
|
| 42 |
+
|
| 43 |
+
lemma polynomial_cell : PolynomialTime cellRealizer := by
|
| 44 |
+
unfold cellRealizer
|
| 45 |
+
apply polynomial_composition
|
| 46 |
+
· apply polynomial_composition
|
| 47 |
+
· exact polynomial_pair (polynomial_composition polynomial_first polynomial_second)
|
| 48 |
+
(polynomial_pair
|
| 49 |
+
(polynomial_composition (polynomial_pair (polynomial_constant 2)
|
| 50 |
+
(polynomial_composition polynomial_second polynomial_first)) polynomial_add)
|
| 51 |
+
(polynomial_composition (polynomial_pair
|
| 52 |
+
(polynomial_composition (polynomial_pair (polynomial_constant 2)
|
| 53 |
+
(polynomial_composition
|
| 54 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 55 |
+
polynomial_first) polynomial_listLength)) polynomial_add)
|
| 56 |
+
(polynomial_composition polynomial_second polynomial_second)) polynomial_add))
|
| 57 |
+
· exact polynomial_flowEntry
|
| 58 |
+
· exact polynomial_subtract
|
| 59 |
+
|
| 60 |
+
def rowIndices (z : Prepared) : List ℕ := List.range z.1.1.length
|
| 61 |
+
def columnIndices (z : Prepared) : List ℕ := List.range z.1.2.length
|
| 62 |
+
|
| 63 |
+
def rowIndicesRealizer : Realizer rowIndices :=
|
| 64 |
+
composition (composition (composition first first) listLength) range
|
| 65 |
+
|
| 66 |
+
def columnIndicesRealizer : Realizer columnIndices :=
|
| 67 |
+
composition (composition (composition first second) listLength) range
|
| 68 |
+
|
| 69 |
+
lemma polynomial_rowIndices : PolynomialTime rowIndicesRealizer := by
|
| 70 |
+
apply polynomial_boundedRange
|
| 71 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 72 |
+
polynomial_listLength) X
|
| 73 |
+
intro z
|
| 74 |
+
simpa only [Function.comp_apply,eval_X] using
|
| 75 |
+
(list_length_le_weight z.1.1).trans ((weight_fst z.1).trans (weight_fst z))
|
| 76 |
+
|
| 77 |
+
lemma polynomial_columnIndices : PolynomialTime columnIndicesRealizer := by
|
| 78 |
+
apply polynomial_boundedRange
|
| 79 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_second)
|
| 80 |
+
polynomial_listLength) X
|
| 81 |
+
intro z
|
| 82 |
+
simpa only [Function.comp_apply,eval_X] using
|
| 83 |
+
(list_length_le_weight z.1.2).trans ((weight_snd z.1).trans (weight_fst z))
|
| 84 |
+
|
| 85 |
+
def row (z : Prepared×ℕ) : List ℕ :=
|
| 86 |
+
(columnIndices z.1).map (fun j=>cell (z.1,(z.2,j)))
|
| 87 |
+
|
| 88 |
+
def rowRealizer : Realizer row :=
|
| 89 |
+
composition (pair identity (composition first columnIndicesRealizer))
|
| 90 |
+
(mapWith (composition (f := fun z : (Prepared×ℕ)×ℕ=>(z.1.1,(z.1.2,z.2)))
|
| 91 |
+
(g := cell) (pair (composition first first)
|
| 92 |
+
(pair (composition first second) second)) cellRealizer))
|
| 93 |
+
|
| 94 |
+
lemma polynomial_row : PolynomialTime rowRealizer :=
|
| 95 |
+
polynomial_composition (polynomial_pair polynomial_identity
|
| 96 |
+
(polynomial_composition polynomial_first polynomial_columnIndices))
|
| 97 |
+
(polynomial_mapWith (polynomial_composition (polynomial_pair
|
| 98 |
+
(polynomial_composition polynomial_first polynomial_first)
|
| 99 |
+
(polynomial_pair (polynomial_composition polynomial_first polynomial_second)
|
| 100 |
+
polynomial_second)) polynomial_cell))
|
| 101 |
+
|
| 102 |
+
def table (z : Prepared) : NatMatrix :=
|
| 103 |
+
(rowIndices z).map (fun i=>row (z,i))
|
| 104 |
+
|
| 105 |
+
def tableRealizer : Realizer table :=
|
| 106 |
+
composition (pair identity rowIndicesRealizer) (mapWith rowRealizer)
|
| 107 |
+
|
| 108 |
+
lemma polynomial_table : PolynomialTime tableRealizer :=
|
| 109 |
+
polynomial_composition (polynomial_pair polynomial_identity polynomial_rowIndices)
|
| 110 |
+
(polynomial_mapWith polynomial_row)
|
| 111 |
+
|
| 112 |
+
def prepare (z : Input) : Prepared :=
|
| 113 |
+
(z.1,NetworkCapacityProgram.solve z)
|
| 114 |
+
|
| 115 |
+
def prepareRealizer : Realizer prepare :=
|
| 116 |
+
pair first NetworkCapacityProgram.solveRealizer
|
| 117 |
+
|
| 118 |
+
lemma polynomial_prepare : PolynomialTime prepareRealizer :=
|
| 119 |
+
polynomial_pair polynomial_first NetworkCapacityProgram.polynomial_solve
|
| 120 |
+
|
| 121 |
+
def output (z : Input) : NatMatrix := table (prepare z)
|
| 122 |
+
def outputRealizer : Realizer output := composition prepareRealizer tableRealizer
|
| 123 |
+
lemma polynomial_output : PolynomialTime outputRealizer :=
|
| 124 |
+
polynomial_composition polynomial_prepare polynomial_table
|
| 125 |
+
|
| 126 |
+
private lemma signedParts_toNat (z : ℤ) :
|
| 127 |
+
(ForestAffineProgram.signedParts z).1-(ForestAffineProgram.signedParts z).2=z.toNat := by
|
| 128 |
+
cases z <;> simp [ForestAffineProgram.signedParts]
|
| 129 |
+
|
| 130 |
+
lemma cell_code {m n : ℕ} (r : Fin m→ℕ) (c : Fin n→ℕ) (b : Fin m→Fin n→ℕ)
|
| 131 |
+
(i : Fin m) (j : Fin n) :
|
| 132 |
+
cell (prepare (data r c b),(i.val,j.val))=
|
| 133 |
+
LayeredTableNetwork.decode (BoundedTableFeasibility.computedFlow r c b) i j := by
|
| 134 |
+
let f := EdmondsKarpAlgorithm.maxFlow (BoundedTableFeasibility.networkCapacity r c b)
|
| 135 |
+
(BoundedTableFeasibility.vertices m n LayeredTableNetwork.source)
|
| 136 |
+
(BoundedTableFeasibility.vertices m n LayeredTableNetwork.sink)
|
| 137 |
+
have he := flowEntry_code f
|
| 138 |
+
(BoundedTableFeasibility.vertices m n (LayeredTableNetwork.row i))
|
| 139 |
+
(BoundedTableFeasibility.vertices m n (LayeredTableNetwork.column j))
|
| 140 |
+
simp only [index_vertices,LayeredTableNetwork.row,LayeredTableNetwork.column,index] at he
|
| 141 |
+
have hlen : (data r c b).1.1.length=m := by simp only [data,List.length_ofFn]
|
| 142 |
+
dsimp only [cell,prepare]
|
| 143 |
+
rw [hlen,NetworkCapacityProgram.solve_code,he]
|
| 144 |
+
change (ForestAffineProgram.signedParts (f _ _)).1-
|
| 145 |
+
(ForestAffineProgram.signedParts (f _ _)).2=(f _ _).toNat
|
| 146 |
+
exact signedParts_toNat _
|
| 147 |
+
|
| 148 |
+
lemma output_code {m n : ℕ} (r : Fin m→ℕ) (c : Fin n→ℕ) (b : Fin m→Fin n→ℕ) :
|
| 149 |
+
output (data r c b)=natCode
|
| 150 |
+
(LayeredTableNetwork.decode (BoundedTableFeasibility.computedFlow r c b)) := by
|
| 151 |
+
simp only [output,table,row,rowIndices,columnIndices,prepare,data,List.length_ofFn,
|
| 152 |
+
DirectedBFSCode.range_eq_map,←List.ofFn_eq_map,List.map_ofFn,natCode]
|
| 153 |
+
apply congrArg List.ofFn
|
| 154 |
+
funext i
|
| 155 |
+
apply congrArg List.ofFn
|
| 156 |
+
funext j
|
| 157 |
+
exact cell_code r c b i j
|
| 158 |
+
|
| 159 |
+
/-- The computed list output encodes a table with the supplied margins and
|
| 160 |
+
bounds whenever the concrete feasibility test accepts. -/
|
| 161 |
+
theorem output_correct {m n : ℕ} (hEK : PublishedEdmondsKarp.EdmondsKarp1972Theorem1)
|
| 162 |
+
(r : Fin m→ℕ) (c : Fin n→ℕ) (b : Fin m→Fin n→ℕ)
|
| 163 |
+
(ht : NetworkFeasibilityProgram.test (data r c b)=true) :
|
| 164 |
+
∃ X, output (data r c b)=natCode X ∧ HasMargins X r c ∧ ∀ i j,X i j≤b i j := by
|
| 165 |
+
have hp : (∑ i,r i)=∑ j,c j ∧
|
| 166 |
+
LayeredTableNetwork.value (BoundedTableFeasibility.computedFlow r c b)=∑ i,(r i:ℤ) := by
|
| 167 |
+
simpa only [NetworkFeasibilityProgram.test_eq_feasible,
|
| 168 |
+
BoundedTableFeasibility.feasibleTest,decide_eq_true_eq] using ht
|
| 169 |
+
exact ⟨_,output_code r c b,LayeredTableNetwork.decode_boundedTable r c b _
|
| 170 |
+
(BoundedTableFeasibility.computedFlow_feasible hEK r c b) hp.2 hp.1⟩
|
| 171 |
+
|
| 172 |
+
end ContingencyTables.NetworkTableProgram
|
| 173 |
+
|
| 174 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/OffsetCountProgram.lean
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.ScaledWordProgram
|
| 2 |
+
import Mathlib.Algebra.Order.Floor.Div
|
| 3 |
+
|
| 4 |
+
namespace OAI
|
| 5 |
+
|
| 6 |
+
/-! # Exact binary tabulation of the bounded offset law -/
|
| 7 |
+
|
| 8 |
+
namespace ContingencyTables.OffsetCountPrograms
|
| 9 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped
|
| 10 |
+
|
| 11 |
+
def ceilQuotient (z : ℕ×ℕ) : ℕ := (z.1+z.2-1)/z.2
|
| 12 |
+
|
| 13 |
+
def ceilQuotientRealizer : Realizer ceilQuotient :=
|
| 14 |
+
composition (pair
|
| 15 |
+
(composition (pair (composition (pair first second) add) (constant 1)) subtract)
|
| 16 |
+
second) quotient
|
| 17 |
+
|
| 18 |
+
lemma polynomial_ceilQuotient : PolynomialTime ceilQuotientRealizer :=
|
| 19 |
+
polynomial_composition (polynomial_pair
|
| 20 |
+
(polynomial_composition (polynomial_pair
|
| 21 |
+
(polynomial_composition (polynomial_pair polynomial_first polynomial_second) polynomial_add)
|
| 22 |
+
(polynomial_constant 1)) polynomial_subtract)
|
| 23 |
+
polynomial_second) polynomial_quotient
|
| 24 |
+
|
| 25 |
+
lemma ceilQuotient_eq (n d : ℕ) (hd : 0<d) :
|
| 26 |
+
ceilQuotient (n,d)=⌈(n:ℚ)/d⌉₊ := by
|
| 27 |
+
change n ⌈/⌉ d=⌈(n:ℚ)/d⌉₊
|
| 28 |
+
have hdq : (0:ℚ)<d := by exact_mod_cast hd
|
| 29 |
+
apply le_antisymm
|
| 30 |
+
· rw [ceilDiv_le_iff_le_mul hd]
|
| 31 |
+
have hh:=Nat.le_ceil ((n:ℚ)/d)
|
| 32 |
+
have hn : n≤⌈(n:ℚ)/d⌉₊*d := by exact_mod_cast (div_le_iff₀ hdq).mp hh
|
| 33 |
+
simpa only [Nat.mul_comm] using hn
|
| 34 |
+
· rw [Nat.ceil_le,div_le_iff₀ hdq]
|
| 35 |
+
have hn : n≤d*(n ⌈/⌉ d) := by
|
| 36 |
+
simpa only [smul_eq_mul] using (le_smul_ceilDiv (b := n) hd)
|
| 37 |
+
have hn' : n≤(n ⌈/⌉ d)*d := by simpa only [Nat.mul_comm] using hn
|
| 38 |
+
exact_mod_cast hn'
|
| 39 |
+
|
| 40 |
+
abbrev Input := (ℕ×ℕ)×ℕ
|
| 41 |
+
|
| 42 |
+
def endpoint (z : Input) : ℕ := ceilQuotient (z.2*z.1.2,z.1.1)
|
| 43 |
+
|
| 44 |
+
def endpointRealizer : Realizer endpoint :=
|
| 45 |
+
composition (pair
|
| 46 |
+
(composition (pair second (composition first second)) multiply)
|
| 47 |
+
(composition first first)) ceilQuotientRealizer
|
| 48 |
+
|
| 49 |
+
lemma polynomial_endpoint : PolynomialTime endpointRealizer :=
|
| 50 |
+
polynomial_composition (polynomial_pair
|
| 51 |
+
(polynomial_composition (polynomial_pair polynomial_second
|
| 52 |
+
(polynomial_composition polynomial_first polynomial_second)) polynomial_multiply)
|
| 53 |
+
(polynomial_composition polynomial_first polynomial_first)) polynomial_ceilQuotient
|
| 54 |
+
|
| 55 |
+
def offsetCount (z : Input) : ℕ := endpoint (z.1,z.2+1)-endpoint z
|
| 56 |
+
|
| 57 |
+
def offsetCountRealizer : Realizer offsetCount :=
|
| 58 |
+
composition (pair
|
| 59 |
+
(composition (pair first (composition second successor)) endpointRealizer)
|
| 60 |
+
endpointRealizer) subtract
|
| 61 |
+
|
| 62 |
+
lemma polynomial_offsetCount : PolynomialTime offsetCountRealizer :=
|
| 63 |
+
polynomial_composition (polynomial_pair
|
| 64 |
+
(polynomial_composition (polynomial_pair polynomial_first
|
| 65 |
+
(polynomial_composition polynomial_second polynomial_successor)) polynomial_endpoint)
|
| 66 |
+
polynomial_endpoint) polynomial_subtract
|
| 67 |
+
|
| 68 |
+
lemma endpoint_eq (M Q β : ℕ) (hM : 0<M) :
|
| 69 |
+
endpoint ((M,Q),β)=FairBits.endpoint M Q β := by
|
| 70 |
+
rw [endpoint,ceilQuotient_eq _ _ hM]
|
| 71 |
+
simp only [FairBits.endpoint,Nat.cast_mul]
|
| 72 |
+
|
| 73 |
+
/-- Only two ceiling divisions are required, regardless of the number of
|
| 74 |
+
random words whose probability is being counted. -/
|
| 75 |
+
theorem offsetCount_eq (M Q β : ℕ) (hM : 0<M) (hQ : 0<Q) (hβ : β<M) :
|
| 76 |
+
offsetCount ((M,Q),β)=(FairBits.fiber M Q β).card := by
|
| 77 |
+
rw [FairBits.fiber_card hM hQ hβ,offsetCount,endpoint_eq _ _ _ hM,endpoint_eq _ _ _ hM]
|
| 78 |
+
|
| 79 |
+
theorem scaledLaw_mass (M k : ℕ) (hM : 0<M) (β : Fin M) :
|
| 80 |
+
(FairBits.scaledLaw M k hM).mass β=
|
| 81 |
+
(offsetCount ((M,2^k),β.val):ℚ)/(2:ℚ)^k := by
|
| 82 |
+
rw [offsetCount_eq _ _ _ hM (by positivity) β.isLt]
|
| 83 |
+
simp only [FairBits.scaledLaw,FairBits.outputMass,Nat.cast_pow,Nat.cast_ofNat]
|
| 84 |
+
|
| 85 |
+
end ContingencyTables.OffsetCountPrograms
|
| 86 |
+
|
| 87 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/OnlineIntegerRetry.lean
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.OnlineRetryPrefix
|
| 2 |
+
import OAI.Combinatorics.MatchingCount.Machines.OnlineTree
|
| 3 |
+
|
| 4 |
+
namespace OAI
|
| 5 |
+
|
| 6 |
+
/-! # A fixed online machine for repeat-until-success -/
|
| 7 |
+
|
| 8 |
+
namespace ContingencyTables.OnlineIntegerRetry
|
| 9 |
+
open MatchingFPRAS MatchingFPRAS.TreeData MatchingFPRAS.TreeMachine
|
| 10 |
+
open MatchingFPRAS.TreeFunction
|
| 11 |
+
open MatchingFPRAS.TreeTyped (Codec)
|
| 12 |
+
open MatchingFPRAS.RegisterProc MatchingFPRAS.OnlineStack
|
| 13 |
+
open MatchingFPRAS.PhysicalStream
|
| 14 |
+
|
| 15 |
+
inductive Label where
|
| 16 |
+
| prepare | trial | decode | test | retry | finish | halt
|
| 17 |
+
deriving DecidableEq, Inhabited
|
| 18 |
+
|
| 19 |
+
instance : Fintype Label := ⟨{.prepare,.trial,.decode,.test,.retry,.finish,.halt}, by
|
| 20 |
+
intro x; cases x <;> simp⟩
|
| 21 |
+
|
| 22 |
+
def prepareProc : RegisterProc.Proc (K Reg) :=
|
| 23 |
+
RegisterProc.seq (copy 0 1 (by decide)) (cons 1 2 (by decide))
|
| 24 |
+
def retryProc : RegisterProc.Proc (K Reg) :=
|
| 25 |
+
RegisterProc.seq (split 1 2 (by decide))
|
| 26 |
+
(RegisterProc.seq (copy 1 0 (by decide)) (clear 1))
|
| 27 |
+
def finishProc : RegisterProc.Proc (K Reg) :=
|
| 28 |
+
RegisterProc.seq (clear 1)
|
| 29 |
+
(RegisterProc.seq (split 1 2 (by decide)) (clear 1))
|
| 30 |
+
|
| 31 |
+
lemma prepare_exec (x s : Data) :
|
| 32 |
+
RegisterProc.Exec prepareProc (encodeEnv (env x .nil s))
|
| 33 |
+
(encodeEnv (env x .nil (.cons x s))) (5*TreeFunction.size x+8) := by
|
| 34 |
+
exact ((copy01 x .nil s).seq (cons12 x x s)).mono (by simp only [size_nil]; omega)
|
| 35 |
+
|
| 36 |
+
lemma retry_exec (x y s : Data) :
|
| 37 |
+
RegisterProc.Exec retryProc (encodeEnv (env y .nil (.cons x s)))
|
| 38 |
+
(encodeEnv (env x .nil s)) (6*TreeFunction.size x+TreeFunction.size y+11) := by
|
| 39 |
+
exact ((split12 y .nil x s).seq ((copy10 y x s).seq (clear1 x x s))).mono
|
| 40 |
+
(by simp only [size_nil]; omega)
|
| 41 |
+
|
| 42 |
+
lemma finish_exec (x y s : Data) :
|
| 43 |
+
RegisterProc.Exec finishProc (encodeEnv (env y (bit true) (.cons x s)))
|
| 44 |
+
(encodeEnv (env y .nil s)) (3*TreeFunction.size x+12) := by
|
| 45 |
+
exact ((clear1 y (bit true) (.cons x s)).seq
|
| 46 |
+
((split12 y .nil x s).seq (clear1 y x s))).mono (by
|
| 47 |
+
simp only [bit,ite_true,size_cons,size_nil]
|
| 48 |
+
omega)
|
| 49 |
+
|
| 50 |
+
variable {A B : Type} [Codec A] [Codec B] [Inhabited B]
|
| 51 |
+
variable {f : A → Experiment (Option B)} (F : OnlineTree.Realizer f)
|
| 52 |
+
|
| 53 |
+
def instructions : Label → OnlineControl.Instr Reg Label
|
| 54 |
+
| .prepare => .call (OnlineProc.deterministic prepareProc) .trial
|
| 55 |
+
| .trial => .call F.program .decode
|
| 56 |
+
| .decode => .call (OnlineProc.deterministic (split 1 0 (by decide))) .test
|
| 57 |
+
| .test => .branch 1 .retry .finish
|
| 58 |
+
| .retry => .call (OnlineProc.deterministic retryProc) .prepare
|
| 59 |
+
| .finish => .call (OnlineProc.deterministic finishProc) .halt
|
| 60 |
+
| .halt => .halt
|
| 61 |
+
|
| 62 |
+
/-- A single finite control graph, independent of the number of retries. -/
|
| 63 |
+
def program : OnlineProc.Proc (K Reg) := OnlineControl.compile (instructions F)
|
| 64 |
+
|
| 65 |
+
def output (a : A) (s : Data) : Option B → Option Label×Env Reg
|
| 66 |
+
| none => (some .prepare,env (TreeTyped.encode a) .nil s)
|
| 67 |
+
| some b => (none,env (TreeTyped.encode b) .nil s)
|
| 68 |
+
|
| 69 |
+
def prefixExperiment (a : A) : ℕ → Experiment (Option B)
|
| 70 |
+
| 0 => .done none
|
| 71 |
+
| N+1 => (f a).bind (fun b => b.elim (prefixExperiment a N) (fun b => .done (some b)))
|
| 72 |
+
|
| 73 |
+
def stepCost (a : A) : ℕ := F.cost a+12*TreeFunction.size (TreeTyped.encode a)+TreeFunction.size (TreeTyped.encode (default : B))+40
|
| 74 |
+
|
| 75 |
+
lemma prefix_step (a : A) (s : Data) (rest : Experiment (Option B))
|
| 76 |
+
(hrest : OnlineRetryPrefix.At (instructions F) (output a s)
|
| 77 |
+
(some .prepare) (env (TreeTyped.encode a) .nil s) rest) :
|
| 78 |
+
∃ q,OnlineRetryPrefix.At (instructions F) (output a s)
|
| 79 |
+
(some .prepare) (env (TreeTyped.encode a) .nil s) q ∧
|
| 80 |
+
(∀ g : Option B → ℚ,q.mean g=
|
| 81 |
+
((f a).bind (fun b => b.elim rest (fun b => .done (some b)))).mean g) ∧
|
| 82 |
+
q.cost ≤ rest.cost+stepCost F a := by
|
| 83 |
+
classical
|
| 84 |
+
let M:=instructions F
|
| 85 |
+
let out : Option B → Option Label×Env Reg := output a s
|
| 86 |
+
obtain ⟨u,hu,hum,huc⟩:=F.correct a (.cons (TreeTyped.encode a) s)
|
| 87 |
+
have next (b : Option B) : ∃ q,
|
| 88 |
+
OnlineRetryPrefix.At M out (some .decode)
|
| 89 |
+
(env (TreeTyped.encode b) .nil (.cons (TreeTyped.encode a) s)) q ∧
|
| 90 |
+
(∀ g : Option B → ℚ,q.mean g=(b.elim rest (fun b => .done (some b))).mean g) ∧
|
| 91 |
+
q.cost ≤ rest.cost+6*TreeFunction.size (TreeTyped.encode a)+TreeFunction.size (TreeTyped.encode (default : B))+27 := by
|
| 92 |
+
cases b with
|
| 93 |
+
| none =>
|
| 94 |
+
obtain ⟨v,hv,hvm,hvc⟩:=OnlineRetryPrefix.deterministicCall M out .retry .prepare
|
| 95 |
+
retryProc rfl _ _ _ (retry_exec (TreeTyped.encode a) (TreeTyped.encode (default : B)) s) rest hrest
|
| 96 |
+
have ht:=OnlineRetryPrefix.nilBranch M out .test .retry .finish 1 rfl
|
| 97 |
+
(env (TreeTyped.encode (default : B)) (bit false) (.cons (TreeTyped.encode a) s)) rfl v hv
|
| 98 |
+
obtain ⟨q,hq,hqm,hqc⟩:=OnlineRetryPrefix.deterministicCall M out .decode .test
|
| 99 |
+
(split 1 0 (by decide)) rfl _ _ _
|
| 100 |
+
(split10 (bit false) (TreeTyped.encode (default : B)) .nil (.cons (TreeTyped.encode a) s))
|
| 101 |
+
(.delay 1 v) ht
|
| 102 |
+
refine ⟨q,hq,?_,?_⟩
|
| 103 |
+
· intro g
|
| 104 |
+
exact (hqm g).trans (hvm g)
|
| 105 |
+
· simp only [Experiment.cost,bit,Bool.false_eq_true,ite_false,size_nil] at hqc
|
| 106 |
+
omega
|
| 107 |
+
| some b =>
|
| 108 |
+
have hh:=OnlineRetryPrefix.halt M out .halt rfl (some b) rfl
|
| 109 |
+
obtain ⟨v,hv,hvm,hvc⟩:=OnlineRetryPrefix.deterministicCall M out .finish .halt
|
| 110 |
+
finishProc rfl _ _ _ (finish_exec (TreeTyped.encode a) (TreeTyped.encode b) s)
|
| 111 |
+
(.delay 1 (.done (some b))) hh
|
| 112 |
+
have ht:=OnlineRetryPrefix.consBranch M out .test .retry .finish 1 rfl
|
| 113 |
+
(env (TreeTyped.encode b) (bit true) (.cons (TreeTyped.encode a) s)) .nil .nil rfl v hv
|
| 114 |
+
obtain ⟨q,hq,hqm,hqc⟩:=OnlineRetryPrefix.deterministicCall M out .decode .test
|
| 115 |
+
(split 1 0 (by decide)) rfl _ _ _
|
| 116 |
+
(split10 (bit true) (TreeTyped.encode b) .nil (.cons (TreeTyped.encode a) s)) (.delay 1 v) ht
|
| 117 |
+
refine ⟨q,hq,?_,?_⟩
|
| 118 |
+
· intro g
|
| 119 |
+
exact (hqm g).trans (hvm g)
|
| 120 |
+
· simp only [Experiment.cost,bit,ite_true,size_cons,size_nil] at hqc hvc
|
| 121 |
+
omega
|
| 122 |
+
choose v hv hvm hvc using next
|
| 123 |
+
have ht:=OnlineRetryPrefix.call M out .trial .decode F.program rfl
|
| 124 |
+
(env (TreeTyped.encode a) .nil (.cons (TreeTyped.encode a) s))
|
| 125 |
+
(fun b => env (TreeTyped.encode b) .nil (.cons (TreeTyped.encode a) s)) u hu v hv
|
| 126 |
+
obtain ⟨q,hq,hqm,hqc⟩:=OnlineRetryPrefix.deterministicCall M out .prepare .trial
|
| 127 |
+
prepareProc rfl _ _ _ (prepare_exec (TreeTyped.encode a) s) (.delay 1 (u.bind v)) ht
|
| 128 |
+
refine ⟨q,hq,?_,?_⟩
|
| 129 |
+
· intro g
|
| 130 |
+
rw [hqm,Experiment.mean,Experiment.mean_bind]
|
| 131 |
+
simp_rw [hvm]
|
| 132 |
+
rw [Experiment.mean_bind]
|
| 133 |
+
exact hum _
|
| 134 |
+
· have hb:=Experiment.cost_bind_le u v
|
| 135 |
+
(rest.cost+6*TreeFunction.size (TreeTyped.encode a)+TreeFunction.size (TreeTyped.encode (default : B))+27) hvc
|
| 136 |
+
simp only [Experiment.cost] at hqc
|
| 137 |
+
unfold stepCost
|
| 138 |
+
omega
|
| 139 |
+
|
| 140 |
+
/-- All finite prefix laws belong to the same fixed looping program.
|
| 141 |
+
Failure leaves the original input at the live retry entry. -/
|
| 142 |
+
theorem prefix_correct (a : A) (s : Data) (N : ℕ) :
|
| 143 |
+
∃ q,OnlineRetryPrefix.At (instructions F) (output a s)
|
| 144 |
+
(some .prepare) (env (TreeTyped.encode a) .nil s) q ∧
|
| 145 |
+
(∀ g : Option B → ℚ,q.mean g=(prefixExperiment (f := f) a N).mean g) ∧
|
| 146 |
+
q.cost ≤ N*stepCost F a := by
|
| 147 |
+
induction N with
|
| 148 |
+
| zero => exact ⟨.done none,.done none,fun _ => rfl,by simp [Experiment.cost]⟩
|
| 149 |
+
| succ N ih =>
|
| 150 |
+
obtain ⟨p,hp,hpm,hpc⟩:=ih
|
| 151 |
+
obtain ⟨q,hq,hqm,hqc⟩:=prefix_step F a s p hp
|
| 152 |
+
refine ⟨q,hq,?_,?_⟩
|
| 153 |
+
· intro g
|
| 154 |
+
rw [hqm,prefixExperiment,Experiment.mean_bind,Experiment.mean_bind]
|
| 155 |
+
congr 1
|
| 156 |
+
funext b
|
| 157 |
+
cases b <;> simp only [Option.elim_none,Option.elim_some,hpm]
|
| 158 |
+
· rw [Nat.succ_mul]
|
| 159 |
+
omega
|
| 160 |
+
|
| 161 |
+
/-- The finite prefixes also execute on the actual compiled tape machine.
|
| 162 |
+
The failed endpoint still has a live control state. -/
|
| 163 |
+
theorem physical_prefix (a : A) (s : Data) (N H : ℕ)
|
| 164 |
+
(hH : StackCost.StackBound (OnlineTree.initial (program F) (TreeTyped.encode a) s).stk H) :
|
| 165 |
+
∃ q,PhysicalPost.Implements (OnlineStack.table (program F).code (program F).coin)
|
| 166 |
+
(fun b => OnlineStack.cfg (OnlineControl.cfg (instructions F)
|
| 167 |
+
(output a s b).1 (output a s b).2))
|
| 168 |
+
(OnlineStack.cfg (OnlineTree.initial (program F) (TreeTyped.encode a) s)) q ∧
|
| 169 |
+
(∀ g : Option B → ℚ,q.mean g=(prefixExperiment (f := f) a N).mean g) ∧
|
| 170 |
+
q.cost ≤ (N*stepCost F a)*
|
| 171 |
+
OnlineStack.Executes.factor (program F).code H (N*stepCost F a) := by
|
| 172 |
+
obtain ⟨p,hp,hpm,hpc⟩:=prefix_correct F a s N
|
| 173 |
+
have hp' : Executes (program F).code (program F).coin
|
| 174 |
+
(fun b => OnlineControl.cfg (instructions F) (output a s b).1 (output a s b).2)
|
| 175 |
+
(OnlineTree.initial (program F) (TreeTyped.encode a) s) p := hp
|
| 176 |
+
obtain ⟨q,hq,hqm,hqc⟩:=hp'.compile H hH
|
| 177 |
+
refine ⟨q,hq,fun g => (hqm g).trans (hpm g),?_⟩
|
| 178 |
+
exact hqc.trans (Nat.mul_le_mul hpc
|
| 179 |
+
(OnlineStack.Executes.factor_mono H H p.cost (N*stepCost F a) le_rfl hpc))
|
| 180 |
+
|
| 181 |
+
end ContingencyTables.OnlineIntegerRetry
|
| 182 |
+
|
| 183 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/OnlinePartialCall.lean
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.OnlineRetryPrefix
|
| 2 |
+
|
| 3 |
+
namespace OAI
|
| 4 |
+
|
| 5 |
+
/-! # Calling the actual retry loop without pretending it has halted -/
|
| 6 |
+
|
| 7 |
+
namespace ContingencyTables.OnlinePartialCall
|
| 8 |
+
open MatchingFPRAS MatchingFPRAS.TreeData
|
| 9 |
+
open MatchingFPRAS.TreeMachine (Env K encodeEnv)
|
| 10 |
+
open MatchingFPRAS.OnlineStack MatchingFPRAS.OnlineControl
|
| 11 |
+
open MatchingFPRAS.PhysicalStream Turing
|
| 12 |
+
|
| 13 |
+
variable {R Λ α : Type} [DecidableEq R]
|
| 14 |
+
|
| 15 |
+
def embed (M : Λ → Instr R Λ) (l : Λ) :
|
| 16 |
+
(procedure (M l)).Label → Label M := fun u => .inr ⟨l,u⟩
|
| 17 |
+
|
| 18 |
+
def callCfg (M : Λ → Instr R Λ) (l : Λ)
|
| 19 |
+
(c : TM2.Cfg (fun _ : K R => Bool) (procedure (M l)).Label RegisterProc.Store) :=
|
| 20 |
+
StackComposition.mapCfg (embed M l) ((successor (M l)).map Sum.inl) c
|
| 21 |
+
|
| 22 |
+
/-- A finite prefix of a called procedure can end at a live internal label.
|
| 23 |
+
Only actual halts return to the continuation label. -/
|
| 24 |
+
theorem partial_call (M : Λ → Instr R Λ) (l : Λ)
|
| 25 |
+
(hcall : ∃ P next, M l = .call P next) (E : Env R)
|
| 26 |
+
(out : α → TM2.Cfg (fun _ : K R => Bool) (procedure (M l)).Label RegisterProc.Store)
|
| 27 |
+
(p : Experiment α)
|
| 28 |
+
(hp : Executes (procedure (M l)).code (procedure (M l)).coin out
|
| 29 |
+
⟨some default,none,encodeEnv E⟩ p) :
|
| 30 |
+
Executes (code M) (coins M) (fun a => callCfg M l (out a))
|
| 31 |
+
(OnlineControl.cfg M (some l) E) (.delay 1 p) := by
|
| 32 |
+
have hh := hp.relabel (code M) (coins M) (embed M l)
|
| 33 |
+
((successor (M l)).map Sum.inl) (fun _ => rfl) (fun _ => rfl)
|
| 34 |
+
obtain ⟨P,next,h⟩ := hcall
|
| 35 |
+
apply Executes.step _ _ _ _ rfl
|
| 36 |
+
simpa only [OnlineControl.cfg,code,h,TM2.stepAux,callCfg,StackComposition.mapCfg_some,embed] using hh
|
| 37 |
+
|
| 38 |
+
abbrev Config (M : Λ → Instr R Λ) :=
|
| 39 |
+
TM2.Cfg (fun _ : K R => Bool) (Label M) RegisterProc.Store
|
| 40 |
+
|
| 41 |
+
def At (M : Λ → Instr R Λ) (out : α → Config M)
|
| 42 |
+
(l : Option Λ) (E : Env R) (p : Experiment α) : Prop :=
|
| 43 |
+
Executes (code M) (coins M) out (OnlineControl.cfg M l E) p
|
| 44 |
+
|
| 45 |
+
lemma halt (M : Λ → Instr R Λ) (out : α → Config M)
|
| 46 |
+
(l : Λ) (h : M l=.halt) (a : α) (E : Env R) (ha : out a=OnlineControl.cfg M none E) :
|
| 47 |
+
At M out (some l) E (.delay 1 (.done a)) := by
|
| 48 |
+
apply Executes.step _ _ _ _ rfl
|
| 49 |
+
simp only [code,h,TM2.stepAux]
|
| 50 |
+
change Executes (code M) (coins M) out (OnlineControl.cfg M none E) (.done a)
|
| 51 |
+
rw [←ha]
|
| 52 |
+
exact .done a
|
| 53 |
+
|
| 54 |
+
lemma nilBranch (M : Λ → Instr R Λ) (out : α → Config M)
|
| 55 |
+
(l a b : Λ) (i : R) (h : M l=.branch i a b)
|
| 56 |
+
(E : Env R) (hi : E i=.nil) (p : Experiment α)
|
| 57 |
+
(hp : At M out (some a) E p) : At M out (some l) E (.delay 1 p) := by
|
| 58 |
+
apply Executes.step _ _ _ _ rfl
|
| 59 |
+
simpa only [At,code,h,TM2.stepAux,encodeEnv,hi,encode,List.head?_cons,
|
| 60 |
+
Option.getD_some,Bool.cond_false,OnlineControl.cfg,Option.map_some] using hp
|
| 61 |
+
|
| 62 |
+
lemma consBranch (M : Λ → Instr R Λ) (out : α → Config M)
|
| 63 |
+
(l a b : Λ) (i : R) (h : M l=.branch i a b)
|
| 64 |
+
(E : Env R) (x y : Data) (hi : E i=.cons x y) (p : Experiment α)
|
| 65 |
+
(hp : At M out (some b) E p) : At M out (some l) E (.delay 1 p) := by
|
| 66 |
+
apply Executes.step _ _ _ _ rfl
|
| 67 |
+
simpa only [At,code,h,TM2.stepAux,encodeEnv,hi,encode,List.head?_cons,
|
| 68 |
+
Option.getD_some,Bool.cond_true,OnlineControl.cfg,Option.map_some] using hp
|
| 69 |
+
|
| 70 |
+
lemma call {β : Type} (M : Λ → Instr R Λ) (out : α → Config M)
|
| 71 |
+
(l next : Λ) (P : OnlineProc.Proc (K R)) (h : M l=.call P next)
|
| 72 |
+
(E : Env R) (F : β → Env R) (q : Experiment β)
|
| 73 |
+
(hq : Executes P.code P.coin (fun b => ⟨none,none,encodeEnv (F b)⟩)
|
| 74 |
+
⟨some default,none,encodeEnv E⟩ q)
|
| 75 |
+
(p : β → Experiment α) (hp : ∀ b,At M out (some next) (F b) (p b)) :
|
| 76 |
+
At M out (some l) E (.delay 1 (q.bind p)) := by
|
| 77 |
+
have hq' : Executes (procedure (M l)).code (procedure (M l)).coin
|
| 78 |
+
(fun b => ⟨none,none,encodeEnv (F b)⟩) ⟨some default,none,encodeEnv E⟩ q := by
|
| 79 |
+
rw [show procedure (M l)=P by rw [h]; rfl]
|
| 80 |
+
exact hq
|
| 81 |
+
have hh:=hq'.relabel (code M) (coins M) (fun u => Sum.inr ⟨l,u⟩)
|
| 82 |
+
((successor (M l)).map Sum.inl) (fun _ => rfl) (fun _ => rfl)
|
| 83 |
+
apply Executes.step _ _ _ _ rfl
|
| 84 |
+
have hb:=hh.bind p out (fun b => by
|
| 85 |
+
simpa only [At,OnlineControl.cfg,StackComposition.mapCfg_none,h,successor,Option.map_some] using hp b)
|
| 86 |
+
simpa only [code,h,TM2.stepAux,StackComposition.mapCfg_some] using hb
|
| 87 |
+
|
| 88 |
+
lemma deterministicCall (M : Λ → Instr R Λ) (out : α → Config M)
|
| 89 |
+
(l next : Λ) (P : RegisterProc.Proc (K R))
|
| 90 |
+
(h : M l=.call (OnlineProc.deterministic P) next)
|
| 91 |
+
(E F : Env R) (n : ℕ) (he : RegisterProc.Exec P (encodeEnv E) (encodeEnv F) n)
|
| 92 |
+
(p : Experiment α) (hp : At M out (some next) F p) :
|
| 93 |
+
∃ q,At M out (some l) E q ∧ (∀ f : α → ℚ,q.mean f=p.mean f) ∧
|
| 94 |
+
q.cost ≤ n+1+p.cost := by
|
| 95 |
+
obtain ⟨k,hk,he⟩:=he
|
| 96 |
+
obtain ⟨r,hr,hm,hc⟩:=Executes.of_run P.code k _ _ _ he
|
| 97 |
+
refine ⟨.delay 1 (r.bind (fun _ => p)),
|
| 98 |
+
call M out l next (OnlineProc.deterministic P) h E (fun _ : Unit => F)
|
| 99 |
+
r hr (fun _ => p) (fun _ => hp),?_,?_⟩
|
| 100 |
+
· intro f
|
| 101 |
+
simpa only [Experiment.mean,Experiment.mean_bind] using hm (fun _ => p.mean f)
|
| 102 |
+
· have hh:=Experiment.cost_bind_le r (fun _ => p) p.cost (fun _ => le_rfl)
|
| 103 |
+
simp only [Experiment.cost]
|
| 104 |
+
omega
|
| 105 |
+
|
| 106 |
+
end ContingencyTables.OnlinePartialCall
|
| 107 |
+
|
| 108 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/OnlineRetryPrefix.lean
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.MatchingCount.Machines.OnlineControl
|
| 2 |
+
|
| 3 |
+
namespace OAI
|
| 4 |
+
|
| 5 |
+
/-! # Finite execution prefixes of the unbounded exact-rejection loop -/
|
| 6 |
+
|
| 7 |
+
namespace ContingencyTables.OnlineRetryPrefix
|
| 8 |
+
open MatchingFPRAS MatchingFPRAS.TreeData MatchingFPRAS.PhysicalStream
|
| 9 |
+
open MatchingFPRAS.TreeMachine (Env K encodeEnv)
|
| 10 |
+
open MatchingFPRAS.RegisterProc MatchingFPRAS.OnlineStack
|
| 11 |
+
open MatchingFPRAS.OnlineControl Turing
|
| 12 |
+
|
| 13 |
+
variable {R Λ α : Type} [DecidableEq R]
|
| 14 |
+
|
| 15 |
+
/-- A failed finite prefix ends at the live retry label, while a successful
|
| 16 |
+
prefix ends in the machine's halted configuration. -/
|
| 17 |
+
def At (M : Λ → Instr R Λ) (out : α → Option Λ×Env R)
|
| 18 |
+
(l : Option Λ) (E : Env R) (p : Experiment α) : Prop :=
|
| 19 |
+
Executes (code M) (coins M) (fun a => OnlineControl.cfg M (out a).1 (out a).2) (OnlineControl.cfg M l E) p
|
| 20 |
+
|
| 21 |
+
lemma done (M : Λ → Instr R Λ) (out : α → Option Λ×Env R) (a : α) :
|
| 22 |
+
At M out (out a).1 (out a).2 (.done a) := .done a
|
| 23 |
+
|
| 24 |
+
lemma halt (M : Λ → Instr R Λ) (out : α → Option Λ×Env R)
|
| 25 |
+
(l : Λ) (h : M l=.halt) (a : α) (ha : (out a).1=none) :
|
| 26 |
+
At M out (some l) (out a).2 (.delay 1 (.done a)) := by
|
| 27 |
+
apply Executes.step _ _ _ _ rfl
|
| 28 |
+
have hd:=done M out a
|
| 29 |
+
simpa only [At,code,h,TM2.stepAux,OnlineControl.cfg,ha,Option.map_none] using hd
|
| 30 |
+
|
| 31 |
+
lemma go (M : Λ → Instr R Λ) (out : α → Option Λ×Env R)
|
| 32 |
+
(l k : Λ) (h : M l=.go k) (E : Env R) (p : Experiment α)
|
| 33 |
+
(hp : At M out (some k) E p) :
|
| 34 |
+
At M out (some l) E (.delay 1 p) := by
|
| 35 |
+
apply Executes.step _ _ _ _ rfl
|
| 36 |
+
simpa only [At,code,h,TM2.stepAux,OnlineControl.cfg,Option.map_some] using hp
|
| 37 |
+
|
| 38 |
+
lemma nilBranch (M : Λ → Instr R Λ) (out : α → Option Λ×Env R)
|
| 39 |
+
(l a b : Λ) (i : R) (h : M l=.branch i a b)
|
| 40 |
+
(E : Env R) (hi : E i=.nil) (p : Experiment α)
|
| 41 |
+
(hp : At M out (some a) E p) :
|
| 42 |
+
At M out (some l) E (.delay 1 p) := by
|
| 43 |
+
apply Executes.step _ _ _ _ rfl
|
| 44 |
+
simpa only [At,code,h,TM2.stepAux,encodeEnv,hi,encode,List.head?_cons,
|
| 45 |
+
Option.getD_some,Bool.cond_false,OnlineControl.cfg,Option.map_some] using hp
|
| 46 |
+
|
| 47 |
+
lemma consBranch (M : Λ → Instr R Λ) (out : α → Option Λ×Env R)
|
| 48 |
+
(l a b : Λ) (i : R) (h : M l=.branch i a b)
|
| 49 |
+
(E : Env R) (x y : Data) (hi : E i=.cons x y) (p : Experiment α)
|
| 50 |
+
(hp : At M out (some b) E p) :
|
| 51 |
+
At M out (some l) E (.delay 1 p) := by
|
| 52 |
+
apply Executes.step _ _ _ _ rfl
|
| 53 |
+
simpa only [At,code,h,TM2.stepAux,encodeEnv,hi,encode,List.head?_cons,
|
| 54 |
+
Option.getD_some,Bool.cond_true,OnlineControl.cfg,Option.map_some] using hp
|
| 55 |
+
|
| 56 |
+
lemma call {β : Type} (M : Λ → Instr R Λ) (out : α → Option Λ×Env R)
|
| 57 |
+
(l next : Λ) (P : OnlineProc.Proc (K R)) (h : M l=.call P next)
|
| 58 |
+
(E : Env R) (F : β → Env R) (q : Experiment β)
|
| 59 |
+
(hq : Executes P.code P.coin (fun b => ⟨none,none,encodeEnv (F b)⟩)
|
| 60 |
+
⟨some default,none,encodeEnv E⟩ q)
|
| 61 |
+
(p : β → Experiment α) (hp : ∀ b,At M out (some next) (F b) (p b)) :
|
| 62 |
+
At M out (some l) E (.delay 1 (q.bind p)) := by
|
| 63 |
+
have hq' : Executes (procedure (M l)).code (procedure (M l)).coin
|
| 64 |
+
(fun b => ⟨none,none,encodeEnv (F b)⟩) ⟨some default,none,encodeEnv E⟩ q := by
|
| 65 |
+
rw [show procedure (M l)=P by rw [h]; rfl]
|
| 66 |
+
exact hq
|
| 67 |
+
have hh:=hq'.relabel (code M) (coins M) (fun u => Sum.inr ⟨l,u⟩)
|
| 68 |
+
((successor (M l)).map Sum.inl) (fun _ => rfl) (fun _ => rfl)
|
| 69 |
+
apply Executes.step _ _ _ _ rfl
|
| 70 |
+
have hb:=hh.bind p (fun a => OnlineControl.cfg M (out a).1 (out a).2) (fun b => by
|
| 71 |
+
simpa only [At,OnlineControl.cfg,StackComposition.mapCfg_none,h,successor,Option.map_some] using hp b)
|
| 72 |
+
simpa only [code,h,TM2.stepAux,StackComposition.mapCfg_some] using hb
|
| 73 |
+
|
| 74 |
+
lemma deterministicCall (M : Λ → Instr R Λ) (out : α → Option Λ×Env R)
|
| 75 |
+
(l next : Λ) (P : RegisterProc.Proc (K R))
|
| 76 |
+
(h : M l=.call (OnlineProc.deterministic P) next)
|
| 77 |
+
(E F : Env R) (n : ℕ) (he : RegisterProc.Exec P (encodeEnv E) (encodeEnv F) n)
|
| 78 |
+
(p : Experiment α) (hp : At M out (some next) F p) :
|
| 79 |
+
∃ q,At M out (some l) E q ∧ (∀ f : α → ℚ,q.mean f=p.mean f) ∧
|
| 80 |
+
q.cost ≤ n+1+p.cost := by
|
| 81 |
+
obtain ⟨k,hk,he⟩:=he
|
| 82 |
+
obtain ⟨r,hr,hm,hc⟩:=Executes.of_run P.code k _ _ _ he
|
| 83 |
+
refine ⟨.delay 1 (r.bind (fun _ => p)),
|
| 84 |
+
call M out l next (OnlineProc.deterministic P) h E (fun _ : Unit => F)
|
| 85 |
+
r hr (fun _ => p) (fun _ => hp),?_,?_⟩
|
| 86 |
+
· intro f
|
| 87 |
+
simpa only [Experiment.mean,Experiment.mean_bind] using hm (fun _ => p.mean f)
|
| 88 |
+
· have hh:=Experiment.cost_bind_le r (fun _ => p) p.cost (fun _ => le_rfl)
|
| 89 |
+
simp only [Experiment.cost]
|
| 90 |
+
omega
|
| 91 |
+
|
| 92 |
+
end ContingencyTables.OnlineRetryPrefix
|
| 93 |
+
|
| 94 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/OnlineRetryRuntime.lean
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Sampling.PhysicalRetryTail
|
| 2 |
+
|
| 3 |
+
namespace OAI
|
| 4 |
+
|
| 5 |
+
/-! # Expected physical time of the implemented online rejection loop -/
|
| 6 |
+
|
| 7 |
+
namespace ContingencyTables.OnlineRetryRuntime
|
| 8 |
+
open MatchingFPRAS MatchingFPRAS.PhysicalStream MatchingFPRAS.PhysicalPost
|
| 9 |
+
|
| 10 |
+
variable {A B : Type} [TreeTyped.Codec A] [TreeTyped.Codec B] [Inhabited B]
|
| 11 |
+
variable {f : A → Experiment (Option B)} (F : OnlineTree.Realizer f)
|
| 12 |
+
|
| 13 |
+
noncomputable def machineTable :=
|
| 14 |
+
OnlineStack.table (OnlineIntegerRetry.program F).code (OnlineIntegerRetry.program F).coin
|
| 15 |
+
|
| 16 |
+
noncomputable def initialCfg (a : A) (s : TreeData.Data) :=
|
| 17 |
+
OnlineStack.cfg (OnlineTree.initial (OnlineIntegerRetry.program F) (TreeTyped.encode a) s)
|
| 18 |
+
|
| 19 |
+
noncomputable def outputCfg (a : A) (s : TreeData.Data) (b : Option B) :=
|
| 20 |
+
OnlineStack.cfg (OnlineControl.cfg (OnlineIntegerRetry.instructions F)
|
| 21 |
+
(OnlineIntegerRetry.output a s b).1 (OnlineIntegerRetry.output a s b).2)
|
| 22 |
+
|
| 23 |
+
noncomputable def attemptBound (a : A) (H : ℕ) : ℕ :=
|
| 24 |
+
OnlineIntegerRetry.stepCost F a *
|
| 25 |
+
OnlineStack.Executes.factor (OnlineIntegerRetry.program F).code H
|
| 26 |
+
(OnlineIntegerRetry.stepCost F a) + 1
|
| 27 |
+
|
| 28 |
+
lemma output_halted (a : A) (s : TreeData.Data) (b : B) :
|
| 29 |
+
(outputCfg F a s (some b)).state=none := rfl
|
| 30 |
+
|
| 31 |
+
/-- This statement concerns the fixed physical transition table of the
|
| 32 |
+
online loop, not only the abstract number of rejection attempts. -/
|
| 33 |
+
theorem expected_time (a : A) (s : TreeData.Data) (H : ℕ)
|
| 34 |
+
(hH : StackCost.StackBound
|
| 35 |
+
(OnlineTree.initial (OnlineIntegerRetry.program F) (TreeTyped.encode a) s).stk H)
|
| 36 |
+
(hf : ∀ N,(OnlineIntegerRetry.prefixExperiment (f := f) a N).mean
|
| 37 |
+
(fun b => if b=none then 1 else 0) ≤ (1/2:ℚ)^N) :
|
| 38 |
+
Summable (PhysicalRetryTail.tail (machineTable F) (initialCfg F a s)) ∧
|
| 39 |
+
(∑' t,PhysicalRetryTail.tail (machineTable F) (initialCfg F a s) t) ≤
|
| 40 |
+
2*(attemptBound F a H:ℝ) := by
|
| 41 |
+
apply GeometricRuntime.expected_time_half _ (attemptBound F a H) (Nat.zero_lt_succ _)
|
| 42 |
+
(PhysicalRetryTail.tail_nonnegative _ _) (PhysicalRetryTail.tail_antitone _ _)
|
| 43 |
+
intro N
|
| 44 |
+
obtain ⟨q,hq,hqm,hqc⟩:=PhysicalRetryIteration.linear_physical_prefix F a s H hH N
|
| 45 |
+
have hc : q.cost ≤ N*attemptBound F a H :=
|
| 46 |
+
hqc.trans (Nat.mul_le_mul_left N (Nat.le_add_right _ _))
|
| 47 |
+
have hb:=PhysicalRetryTail.tail_le_failure (machineTable F) (outputCfg F a s)
|
| 48 |
+
(initialCfg F a s) q hq (output_halted F a s) _ hc
|
| 49 |
+
have hp : (q.mean (fun b => if b=none then 1 else 0):ℝ) ≤ (1/2:ℝ)^N := by
|
| 50 |
+
rw [hqm]
|
| 51 |
+
have h := (Rat.cast_le (K := ℝ)).mpr (hf N)
|
| 52 |
+
norm_num only [Rat.cast_pow,Rat.cast_div,Rat.cast_one,Rat.cast_ofNat] at h
|
| 53 |
+
exact h
|
| 54 |
+
exact hb.trans hp
|
| 55 |
+
|
| 56 |
+
end ContingencyTables.OnlineRetryRuntime
|
| 57 |
+
|
| 58 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/PackedMatrixInjective.lean
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.LiteralProgram
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Machines.MatrixEncoding
|
| 3 |
+
import OAI.Combinatorics.ContingencyTables.Sampling.ProfilePublicMachine
|
| 4 |
+
|
| 5 |
+
namespace OAI
|
| 6 |
+
|
| 7 |
+
/-! # Packed physical output still identifies its contingency table -/
|
| 8 |
+
|
| 9 |
+
namespace ContingencyTables.PackedMatrixInjective
|
| 10 |
+
open MatchingFPRAS MatchingFPRAS.PhysicalStream MatchingFPRAS.PhysicalPost Turing
|
| 11 |
+
|
| 12 |
+
lemma deterministic_unique (P : Program) (input X Y : Tape Symbol) (n m : ℕ)
|
| 13 |
+
(hX : P.Realizes input (.done X) n) (hY : P.Realizes input (.done Y) m) : X=Y := by
|
| 14 |
+
obtain ⟨p,hp,hpm,_⟩ := hX
|
| 15 |
+
obtain ⟨q,hq,hqm,_⟩ := hY
|
| 16 |
+
have ha : p.All (fun T => T=X) := Experiment.all_of_mean_eq hpm _ rfl
|
| 17 |
+
have hb : q.All (fun T => T=Y) := Experiment.all_of_mean_eq hqm _ rfl
|
| 18 |
+
have he := hp.padded_execution (fun _ => rfl) (p.cost+q.cost) (Nat.le_add_right _ _) (fun _ => false)
|
| 19 |
+
have hf := hq.padded_execution (fun _ => rfl) (p.cost+q.cost) (Nat.le_add_left _ _) (fun _ => false)
|
| 20 |
+
rw [Experiment.all_run p _ ha] at he
|
| 21 |
+
rw [Experiment.all_run q _ hb] at hf
|
| 22 |
+
exact congrArg Cfg.tape (he.symm.trans hf)
|
| 23 |
+
|
| 24 |
+
lemma table_injective {m n : ℕ} {r : Fin m → ℕ} {c : Fin n → ℕ} (X Y : Table r c)
|
| 25 |
+
(h : Tape.mk₁ (TreePost.packed (GreedyFeasibleTable.printMatrix (DensePrograms.matrixCode X.val)))=
|
| 26 |
+
Tape.mk₁ (TreePost.packed (GreedyFeasibleTable.printMatrix (DensePrograms.matrixCode Y.val)))) : X=Y := by
|
| 27 |
+
have hx := LiteralProgram.post_realizes (GreedyFeasibleTable.printMatrix (DensePrograms.matrixCode X.val))
|
| 28 |
+
(ProfilePrograms.printMatrix_alphabet _)
|
| 29 |
+
have hy := LiteralProgram.post_realizes (GreedyFeasibleTable.printMatrix (DensePrograms.matrixCode Y.val))
|
| 30 |
+
(ProfilePrograms.printMatrix_alphabet _)
|
| 31 |
+
rw [←h] at hy
|
| 32 |
+
have ht := deterministic_unique LiteralProgram.post _ _ _ _ _ hx hy
|
| 33 |
+
have he := congrArg Tape.right₀ ht
|
| 34 |
+
simp only [Tape.mk₁,Tape.mk₂,Tape.mk'_right₀,DensePrograms.matrixCode,GreedyFeasibleTable.printMatrix_ofFn] at he
|
| 35 |
+
exact Subtype.ext (Algorithms.blankMatrix_injective X.val Y.val he)
|
| 36 |
+
|
| 37 |
+
end ContingencyTables.PackedMatrixInjective
|
| 38 |
+
|
| 39 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/PairedCatalogProgram.lean
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.CanonicalInitialEncoding
|
| 2 |
+
|
| 3 |
+
namespace OAI
|
| 4 |
+
|
| 5 |
+
/-! # Computing paired-coordinate metadata from a binary profile
|
| 6 |
+
|
| 7 |
+
Consecutive coordinates are paired by the bounded list scan
|
| 8 |
+
(0,1),(2,3),.... This is the exact metadata of the canonical paired-label
|
| 9 |
+
order, rather than a catalog supplied as an external oracle.
|
| 10 |
+
-/
|
| 11 |
+
|
| 12 |
+
namespace ContingencyTables.PairedCatalogProgram
|
| 13 |
+
|
| 14 |
+
open MatchingFPRAS.TreeTyped ProfilePrograms
|
| 15 |
+
|
| 16 |
+
def indexPair (i : ℕ) : ℕ×ℕ := (2*i,2*i+1)
|
| 17 |
+
|
| 18 |
+
def twiceRealizer : Realizer (fun i : ℕ => 2*i) :=
|
| 19 |
+
composition (f := fun i : ℕ => (2,i)) (g := fun z : ℕ×ℕ => z.1*z.2)
|
| 20 |
+
(pair (constant 2) identity) multiply
|
| 21 |
+
|
| 22 |
+
def indexPairRealizer : Realizer indexPair :=
|
| 23 |
+
pair twiceRealizer
|
| 24 |
+
(composition (f := fun i : ℕ => (2*i,1)) (g := fun z : ℕ×ℕ => z.1+z.2)
|
| 25 |
+
(pair twiceRealizer (constant 1)) add)
|
| 26 |
+
|
| 27 |
+
lemma polynomial_indexPair : PolynomialTime indexPairRealizer :=
|
| 28 |
+
polynomial_pair
|
| 29 |
+
(polynomial_composition (polynomial_pair (polynomial_constant 2) polynomial_identity)
|
| 30 |
+
polynomial_multiply)
|
| 31 |
+
(polynomial_composition (polynomial_pair
|
| 32 |
+
(polynomial_composition (polynomial_pair (polynomial_constant 2) polynomial_identity)
|
| 33 |
+
polynomial_multiply) (polynomial_constant 1)) polynomial_add)
|
| 34 |
+
|
| 35 |
+
def indicesRealizer : Realizer (fun xs : List ℕ => List.range (xs.length/2)) :=
|
| 36 |
+
composition (composition (pair listLength (constant 2)) quotient) range
|
| 37 |
+
|
| 38 |
+
lemma polynomial_indices : PolynomialTime indicesRealizer := by
|
| 39 |
+
apply polynomial_boundedRange
|
| 40 |
+
(polynomial_composition (polynomial_pair polynomial_listLength (polynomial_constant 2))
|
| 41 |
+
polynomial_quotient) Polynomial.X
|
| 42 |
+
intro xs
|
| 43 |
+
simpa only [Function.comp_apply,Polynomial.eval_X] using
|
| 44 |
+
(Nat.div_le_self xs.length 2).trans (list_length_le_weight xs)
|
| 45 |
+
|
| 46 |
+
def catalog (xs : List ℕ) : List (ℕ×ℕ) := (List.range (xs.length/2)).map indexPair
|
| 47 |
+
|
| 48 |
+
def catalogRealizer : Realizer catalog := composition indicesRealizer (map indexPairRealizer)
|
| 49 |
+
|
| 50 |
+
theorem polynomial_catalog : PolynomialTime catalogRealizer :=
|
| 51 |
+
polynomial_composition polynomial_indices (polynomial_map polynomial_indexPair)
|
| 52 |
+
|
| 53 |
+
def pairList {α : Type*} : List α → List (α×Bool)
|
| 54 |
+
| [] => []
|
| 55 |
+
| a::xs => (a,false)::(a,true)::pairList xs
|
| 56 |
+
|
| 57 |
+
lemma pairList_length {α : Type*} (xs : List α) : (pairList xs).length=2*xs.length := by
|
| 58 |
+
induction xs with
|
| 59 |
+
| nil => rfl
|
| 60 |
+
| cons a xs ih => simp only [pairList,List.length_cons,ih]; omega
|
| 61 |
+
|
| 62 |
+
lemma pairList_product {α : Type*} (xs : List α) : pairList xs=xs.product [false,true] := by
|
| 63 |
+
induction xs with
|
| 64 |
+
| nil => rfl
|
| 65 |
+
| cons a xs ih =>
|
| 66 |
+
change (a,false)::(a,true)::pairList xs=(a,false)::(a,true)::xs.product [false,true]
|
| 67 |
+
rw [ih]
|
| 68 |
+
|
| 69 |
+
lemma pairList_get_even {α : Type*} (xs : List α) (i : ℕ) (hi : i<xs.length) :
|
| 70 |
+
(pairList xs)[2*i]'(by rw [pairList_length]; omega)=(xs[i],false) := by
|
| 71 |
+
induction xs generalizing i with
|
| 72 |
+
| nil => simp at hi
|
| 73 |
+
| cons a xs ih =>
|
| 74 |
+
cases i with
|
| 75 |
+
| zero => rfl
|
| 76 |
+
| succ i =>
|
| 77 |
+
simpa only [pairList,Nat.mul_succ,List.getElem_cons_succ] using
|
| 78 |
+
ih i (Nat.lt_of_succ_lt_succ hi)
|
| 79 |
+
|
| 80 |
+
lemma pairList_get_odd {α : Type*} (xs : List α) (i : ℕ) (hi : i<xs.length) :
|
| 81 |
+
(pairList xs)[2*i+1]'(by rw [pairList_length]; omega)=(xs[i],true) := by
|
| 82 |
+
induction xs generalizing i with
|
| 83 |
+
| nil => simp at hi
|
| 84 |
+
| cons a xs ih =>
|
| 85 |
+
cases i with
|
| 86 |
+
| zero => rfl
|
| 87 |
+
| succ i =>
|
| 88 |
+
simpa only [pairList,Nat.mul_succ,List.getElem_cons_succ,Nat.add_assoc] using
|
| 89 |
+
ih i (Nat.lt_of_succ_lt_succ hi)
|
| 90 |
+
|
| 91 |
+
noncomputable section
|
| 92 |
+
open CanonicalInitialEncoding InitialTransversals CompanionArithmeticProposal
|
| 93 |
+
open scoped Classical
|
| 94 |
+
variable {q : ℕ} (b : Fin q → ℕ)
|
| 95 |
+
|
| 96 |
+
def labelOrder : Fin (copyOrder b).length ≃ Copy b :=
|
| 97 |
+
List.Nodup.getEquivOfForallMemList (copyOrder b) (copyOrder_nodup b) (copyOrder_mem b)
|
| 98 |
+
|
| 99 |
+
lemma coordinate_false (i : Fin (copyOrder b).length) :
|
| 100 |
+
(CanonicalInitialEncoding.coordinateEquiv b (labelOrder b i,false)).val=2*i.val := by
|
| 101 |
+
have hl : (pairOrder b).length=2*(copyOrder b).length := by
|
| 102 |
+
change ((copyOrder b).product [false,true]).length=2*(copyOrder b).length
|
| 103 |
+
rw [←pairList_product]
|
| 104 |
+
exact pairList_length _
|
| 105 |
+
let k : Fin (pairOrder b).length := ⟨2*i.val,by rw [hl]; omega⟩
|
| 106 |
+
have hk : (CanonicalInitialEncoding.coordinateEquiv b).symm k=(labelOrder b i,false) := by
|
| 107 |
+
change (pairOrder b)[k.val]=((copyOrder b)[i.val],false)
|
| 108 |
+
simpa only [pairOrder,←pairList_product,k] using pairList_get_even (copyOrder b) i.val i.isLt
|
| 109 |
+
have he := congrArg (fun a => (CanonicalInitialEncoding.coordinateEquiv b a).val) hk
|
| 110 |
+
simpa only [Equiv.apply_symm_apply,k] using he.symm
|
| 111 |
+
|
| 112 |
+
lemma coordinate_true (i : Fin (copyOrder b).length) :
|
| 113 |
+
(CanonicalInitialEncoding.coordinateEquiv b (labelOrder b i,true)).val=2*i.val+1 := by
|
| 114 |
+
have hl : (pairOrder b).length=2*(copyOrder b).length := by
|
| 115 |
+
change ((copyOrder b).product [false,true]).length=2*(copyOrder b).length
|
| 116 |
+
rw [←pairList_product]
|
| 117 |
+
exact pairList_length _
|
| 118 |
+
let k : Fin (pairOrder b).length := ⟨2*i.val+1,by rw [hl]; omega⟩
|
| 119 |
+
have hk : (CanonicalInitialEncoding.coordinateEquiv b).symm k=(labelOrder b i,true) := by
|
| 120 |
+
change (pairOrder b)[k.val]=((copyOrder b)[i.val],true)
|
| 121 |
+
simpa only [pairOrder,←pairList_product,k] using pairList_get_odd (copyOrder b) i.val i.isLt
|
| 122 |
+
have he := congrArg (fun a => (CanonicalInitialEncoding.coordinateEquiv b a).val) hk
|
| 123 |
+
simpa only [Equiv.apply_symm_apply,k] using he.symm
|
| 124 |
+
|
| 125 |
+
/-- Every state encoded in the canonical order has exactly the pair
|
| 126 |
+
catalog computed by the polynomial consecutive-coordinate scan. -/
|
| 127 |
+
theorem catalog_encode (S : Set (Copy b×Bool)) :
|
| 128 |
+
catalog (encodeSet (CanonicalInitialEncoding.coordinateEquiv b) S)=
|
| 129 |
+
List.ofFn (fun i : Fin (copyOrder b).length =>
|
| 130 |
+
((CanonicalInitialEncoding.coordinateEquiv b (labelOrder b i,false)).val,
|
| 131 |
+
(CanonicalInitialEncoding.coordinateEquiv b (labelOrder b i,true)).val)) := by
|
| 132 |
+
have hl : (pairOrder b).length=2*(copyOrder b).length := by
|
| 133 |
+
change ((copyOrder b).product [false,true]).length=2*(copyOrder b).length
|
| 134 |
+
rw [←pairList_product]
|
| 135 |
+
exact pairList_length _
|
| 136 |
+
simp only [catalog,encodeSet,List.length_ofFn,hl]
|
| 137 |
+
rw [Nat.mul_div_cancel_left _ (by decide : 0<2)]
|
| 138 |
+
apply List.ext_getElem
|
| 139 |
+
· simp
|
| 140 |
+
· intro i hi hj
|
| 141 |
+
simp only [List.getElem_map,List.getElem_range,List.getElem_ofFn,indexPair,
|
| 142 |
+
coordinate_false,coordinate_true]
|
| 143 |
+
|
| 144 |
+
end
|
| 145 |
+
end ContingencyTables.PairedCatalogProgram
|
| 146 |
+
|
| 147 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/PairedDemandProgram.lean
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Transport.PairedProfileSemantics
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Machines.SparseViewMatrixProgram
|
| 3 |
+
import OAI.Combinatorics.ContingencyTables.Machines.MatrixSubtractProgram
|
| 4 |
+
import OAI.Combinatorics.ContingencyTables.Rooted.RootedOracleWalkProgram
|
| 5 |
+
|
| 6 |
+
namespace OAI
|
| 7 |
+
|
| 8 |
+
/-! # Replacing the oracle demand by the current encoded paired state
|
| 9 |
+
|
| 10 |
+
Only the retained-cell catalog and the supplied state are scanned. The
|
| 11 |
+
large cells receive zero in each sparse view and hence retain their full
|
| 12 |
+
capacity in the complemented column demand.
|
| 13 |
+
-/
|
| 14 |
+
|
| 15 |
+
namespace ContingencyTables.PairedDemandProgram
|
| 16 |
+
|
| 17 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped DensePrograms
|
| 18 |
+
|
| 19 |
+
abbrev Input := RootedDemandProgram.Input × List ℕ
|
| 20 |
+
|
| 21 |
+
def cellInput (d : RootedForestCatalog.Data) : CompanionCellCatalog.Input :=
|
| 22 |
+
((d.1.2,d.2),d.1.1.2)
|
| 23 |
+
def cellInputRealizer : Realizer cellInput :=
|
| 24 |
+
pair (pair (composition first second) second) (composition (composition first first) second)
|
| 25 |
+
lemma polynomial_cellInput : PolynomialTime cellInputRealizer :=
|
| 26 |
+
polynomial_pair (polynomial_pair (polynomial_composition polynomial_first polynomial_second)
|
| 27 |
+
polynomial_second) (polynomial_composition
|
| 28 |
+
(polynomial_composition polynomial_first polynomial_first) polynomial_second)
|
| 29 |
+
|
| 30 |
+
def cells (z : Input) : CompanionCellCatalog.Input := cellInput z.1.1
|
| 31 |
+
def cellsRealizer : Realizer cells :=
|
| 32 |
+
composition (f := fun z : Input => z.1.1) (g := cellInput)
|
| 33 |
+
(composition first first) cellInputRealizer
|
| 34 |
+
lemma polynomial_cells : PolynomialTime cellsRealizer :=
|
| 35 |
+
polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 36 |
+
polynomial_cellInput
|
| 37 |
+
|
| 38 |
+
def counts (z : Input) : List (ℕ × ℕ) :=
|
| 39 |
+
PairedProfileProgram.profileRows (CompanionCellCatalog.capacities (cells z),z.2)
|
| 40 |
+
def countsRealizer : Realizer counts :=
|
| 41 |
+
composition (f := fun z : Input => (CompanionCellCatalog.capacities (cells z),z.2))
|
| 42 |
+
(g := PairedProfileProgram.profileRows)
|
| 43 |
+
(pair (composition (f := cells) (g := CompanionCellCatalog.capacities)
|
| 44 |
+
cellsRealizer CompanionCellCatalog.capacitiesRealizer) second)
|
| 45 |
+
PairedProfileProgram.profileRowsRealizer
|
| 46 |
+
lemma polynomial_counts : PolynomialTime countsRealizer :=
|
| 47 |
+
polynomial_composition (polynomial_pair
|
| 48 |
+
(polynomial_composition polynomial_cells CompanionCellCatalog.polynomial_capacities)
|
| 49 |
+
polynomial_second) PairedProfileProgram.polynomial_profileRows
|
| 50 |
+
|
| 51 |
+
def sideEntry (z : Bool × (ℕ × ℕ)) : ℕ := if z.1 then z.2.2 else z.2.1
|
| 52 |
+
def sideEntryRealizer : Realizer sideEntry :=
|
| 53 |
+
choose first (composition second second) (composition second first)
|
| 54 |
+
lemma polynomial_sideEntry : PolynomialTime sideEntryRealizer :=
|
| 55 |
+
polynomial_choose polynomial_first
|
| 56 |
+
(polynomial_composition polynomial_second polynomial_second)
|
| 57 |
+
(polynomial_composition polynomial_second polynomial_first)
|
| 58 |
+
|
| 59 |
+
def values (z : Input × Bool) : List ℕ := (counts z.1).map (fun p => sideEntry (z.2,p))
|
| 60 |
+
def valuesRealizer : Realizer values :=
|
| 61 |
+
composition (f := fun z : Input × Bool => (z.2,counts z.1))
|
| 62 |
+
(g := fun p : Bool × List (ℕ × ℕ) => p.2.map (fun a => sideEntry (p.1,a)))
|
| 63 |
+
(pair second (composition (f := Prod.fst) (g := counts) first countsRealizer))
|
| 64 |
+
(mapWith sideEntryRealizer)
|
| 65 |
+
lemma polynomial_values : PolynomialTime valuesRealizer :=
|
| 66 |
+
polynomial_composition (polynomial_pair polynomial_second
|
| 67 |
+
(polynomial_composition polynomial_first polynomial_counts))
|
| 68 |
+
(polynomial_mapWith polynomial_sideEntry)
|
| 69 |
+
|
| 70 |
+
def viewInput (z : Input × Bool) : SparseViewMatrixProgram.Input :=
|
| 71 |
+
(z.1.1.1.1.2,(CompanionCellCatalog.catalog (cells z.1),values z))
|
| 72 |
+
def viewInputRealizer : Realizer viewInput :=
|
| 73 |
+
pair (composition (composition (composition (composition first first) first) first) second)
|
| 74 |
+
(pair (composition (f := fun z : Input × Bool => cells z.1)
|
| 75 |
+
(g := CompanionCellCatalog.catalog)
|
| 76 |
+
(composition (f := Prod.fst) (g := cells) first cellsRealizer)
|
| 77 |
+
CompanionCellCatalog.catalogRealizer) valuesRealizer)
|
| 78 |
+
lemma polynomial_viewInput : PolynomialTime viewInputRealizer :=
|
| 79 |
+
polynomial_pair
|
| 80 |
+
(polynomial_composition (polynomial_composition (polynomial_composition
|
| 81 |
+
(polynomial_composition polynomial_first polynomial_first) polynomial_first)
|
| 82 |
+
polynomial_first) polynomial_second)
|
| 83 |
+
(polynomial_pair (polynomial_composition
|
| 84 |
+
(polynomial_composition polynomial_first polynomial_cells)
|
| 85 |
+
CompanionCellCatalog.polynomial_catalog) polynomial_values)
|
| 86 |
+
|
| 87 |
+
def viewMatrix (z : Input × Bool) : MatrixCode := SparseViewMatrixProgram.matrix (viewInput z)
|
| 88 |
+
def viewMatrixRealizer : Realizer viewMatrix :=
|
| 89 |
+
composition (f := viewInput) (g := SparseViewMatrixProgram.matrix)
|
| 90 |
+
viewInputRealizer SparseViewMatrixProgram.matrixRealizer
|
| 91 |
+
lemma polynomial_viewMatrix : PolynomialTime viewMatrixRealizer :=
|
| 92 |
+
polynomial_composition polynomial_viewInput SparseViewMatrixProgram.polynomial_matrix
|
| 93 |
+
|
| 94 |
+
def complement (z : Input) : MatrixCode :=
|
| 95 |
+
MatrixSubtractProgram.matrix (z.1.1.2,viewMatrix (z,true))
|
| 96 |
+
def complementRealizer : Realizer complement :=
|
| 97 |
+
composition (f := fun z : Input => (z.1.1.2,viewMatrix (z,true)))
|
| 98 |
+
(g := MatrixSubtractProgram.matrix)
|
| 99 |
+
(pair (composition (composition first first) second)
|
| 100 |
+
(composition (f := fun z : Input => (z,true)) (g := viewMatrix)
|
| 101 |
+
(pair identity (constant true)) viewMatrixRealizer)) MatrixSubtractProgram.matrixRealizer
|
| 102 |
+
lemma polynomial_complement : PolynomialTime complementRealizer :=
|
| 103 |
+
polynomial_composition (polynomial_pair
|
| 104 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 105 |
+
polynomial_second)
|
| 106 |
+
(polynomial_composition (polynomial_pair polynomial_identity (polynomial_constant true))
|
| 107 |
+
polynomial_viewMatrix)) MatrixSubtractProgram.polynomial_matrix
|
| 108 |
+
|
| 109 |
+
def output (z : Input) : RootedDemandProgram.Input :=
|
| 110 |
+
(z.1.1,(z.1.2.1,(viewMatrix (z,false),complement z)))
|
| 111 |
+
def outputRealizer : Realizer output :=
|
| 112 |
+
pair (composition first first)
|
| 113 |
+
(pair (composition (composition first second) first)
|
| 114 |
+
(pair (composition (f := fun z : Input => (z,false)) (g := viewMatrix)
|
| 115 |
+
(pair identity (constant false)) viewMatrixRealizer) complementRealizer))
|
| 116 |
+
theorem polynomial_output : PolynomialTime outputRealizer :=
|
| 117 |
+
polynomial_pair (polynomial_composition polynomial_first polynomial_first)
|
| 118 |
+
(polynomial_pair (polynomial_composition
|
| 119 |
+
(polynomial_composition polynomial_first polynomial_second) polynomial_first)
|
| 120 |
+
(polynomial_pair (polynomial_composition
|
| 121 |
+
(polynomial_pair polynomial_identity (polynomial_constant false)) polynomial_viewMatrix)
|
| 122 |
+
polynomial_complement))
|
| 123 |
+
|
| 124 |
+
noncomputable section
|
| 125 |
+
open CompanionFactorialApplication CompanionArithmeticProposal PairedProfileSemantics
|
| 126 |
+
open SmallContextCoordinates PhysicalCompletionFibres FirstPaperProfiles SmallGraphProfiles
|
| 127 |
+
|
| 128 |
+
variable {m n : ℕ} (A : Instance (Fin m) (Fin n))
|
| 129 |
+
|
| 130 |
+
lemma cells_data (x q : Fin m → Fin n → ℕ) (code : List ℕ) :
|
| 131 |
+
cells (RootedDemandProgram.data A x q,code)=CompanionCellCatalog.input A := rfl
|
| 132 |
+
|
| 133 |
+
lemma catalog_eq :
|
| 134 |
+
CompanionCellCatalog.catalog (CompanionCellCatalog.input A)=
|
| 135 |
+
cellCatalog A.small (CompanionCellCatalog.cellEquiv A) := by
|
| 136 |
+
rw [cellCatalog]
|
| 137 |
+
symm
|
| 138 |
+
calc
|
| 139 |
+
List.ofFn (fun i =>
|
| 140 |
+
((CompanionCellCatalog.cellEquiv A i).val.1.val,
|
| 141 |
+
(CompanionCellCatalog.cellEquiv A i).val.2.val))=
|
| 142 |
+
List.ofFn (fun i : Fin (CompanionCellCatalog.catalog
|
| 143 |
+
(CompanionCellCatalog.input A)).length =>
|
| 144 |
+
(CompanionCellCatalog.catalog (CompanionCellCatalog.input A))[i.val]) := by
|
| 145 |
+
apply congrArg List.ofFn
|
| 146 |
+
funext i
|
| 147 |
+
exact CompanionCellCatalog.catalogCell_coordinates A i
|
| 148 |
+
_=_ := List.ofFn_get _
|
| 149 |
+
|
| 150 |
+
lemma values_data (x q : Fin m → Fin n → ℕ) (S : Set (Labels A × Bool)) (side : Bool) :
|
| 151 |
+
values ((RootedDemandProgram.data A x q,
|
| 152 |
+
encodeSet (CompanionComputedInitial.coordinateOrder A) S),side)=
|
| 153 |
+
viewList A.small (CompanionCellCatalog.cellEquiv A)
|
| 154 |
+
(CompanionWeightOracle.profile A S) side := by
|
| 155 |
+
unfold values counts
|
| 156 |
+
rw [cells_data,CompanionCellCatalog.capacities_eq]
|
| 157 |
+
change (PairedProfileProgram.profileRows
|
| 158 |
+
(List.ofFn (CompanionComputedInitial.capacities A),
|
| 159 |
+
encodeSet (CompanionComputedInitial.coordinateOrder A) S)).map _=_
|
| 160 |
+
rw [profileRows_physical]
|
| 161 |
+
simp only [List.map_ofFn,Function.comp_def,viewList,sideEntry]
|
| 162 |
+
cases side <;> rfl
|
| 163 |
+
|
| 164 |
+
lemma viewMatrix_data (x q : Fin m → Fin n → ℕ) (S : Set (Labels A × Bool)) (side : Bool) :
|
| 165 |
+
viewMatrix ((RootedDemandProgram.data A x q,
|
| 166 |
+
encodeSet (CompanionComputedInitial.coordinateOrder A) S),side)=
|
| 167 |
+
matrixCode (smallView A.small (CompanionWeightOracle.profile A S) side) := by
|
| 168 |
+
let z : Input := (RootedDemandProgram.data A x q,
|
| 169 |
+
encodeSet (CompanionComputedInitial.coordinateOrder A) S)
|
| 170 |
+
have hi : viewInput (z,side)=
|
| 171 |
+
((List.ofFn (fun i => (A.r i).toNat),List.ofFn (fun j => (A.c j).toNat)),
|
| 172 |
+
(cellCatalog A.small (CompanionCellCatalog.cellEquiv A),
|
| 173 |
+
viewList A.small (CompanionCellCatalog.cellEquiv A)
|
| 174 |
+
(CompanionWeightOracle.profile A S) side)) := by
|
| 175 |
+
apply Prod.ext
|
| 176 |
+
· rfl
|
| 177 |
+
· apply Prod.ext
|
| 178 |
+
· exact (congrArg CompanionCellCatalog.catalog (cells_data A x q _)).trans (catalog_eq A)
|
| 179 |
+
· exact values_data A x q S side
|
| 180 |
+
change SparseViewMatrixProgram.matrix (viewInput (z,side))=_
|
| 181 |
+
rw [hi]
|
| 182 |
+
rw [SparseViewMatrixProgram.matrix_ofFn]
|
| 183 |
+
apply congrArg matrixCode
|
| 184 |
+
funext i j
|
| 185 |
+
exact lookupView_eq A.small (CompanionCellCatalog.cellEquiv A)
|
| 186 |
+
(CompanionWeightOracle.profile A S) side i j
|
| 187 |
+
|
| 188 |
+
/-- The current code replaces both demand arrays; the old demand only
|
| 189 |
+
provides the immutable graph, capacities, margins and baseline. -/
|
| 190 |
+
theorem output_data (x q : Fin m → Fin n → ℕ) (S : Set (Labels A × Bool)) :
|
| 191 |
+
output (RootedDemandProgram.data A x q,
|
| 192 |
+
encodeSet (CompanionComputedInitial.coordinateOrder A) S)=
|
| 193 |
+
RootedDemandProgram.data A
|
| 194 |
+
(ForestCountIdentification.smallX A.small (CompanionWeightOracle.profile A S))
|
| 195 |
+
(fun i j => A.b i j-
|
| 196 |
+
ForestCountIdentification.smallY A.small (CompanionWeightOracle.profile A S) i j) := by
|
| 197 |
+
have hq : complement (RootedDemandProgram.data A x q,
|
| 198 |
+
encodeSet (CompanionComputedInitial.coordinateOrder A) S)=
|
| 199 |
+
matrixCode (fun i j => A.b i j-
|
| 200 |
+
smallView A.small (CompanionWeightOracle.profile A S) true i j) := by
|
| 201 |
+
change MatrixSubtractProgram.matrix (matrixCode A.b,viewMatrix
|
| 202 |
+
((RootedDemandProgram.data A x q,
|
| 203 |
+
encodeSet (CompanionComputedInitial.coordinateOrder A) S),true))=_
|
| 204 |
+
rw [viewMatrix_data]
|
| 205 |
+
exact MatrixSubtractProgram.matrix_code _ _
|
| 206 |
+
unfold output
|
| 207 |
+
rw [viewMatrix_data,hq]
|
| 208 |
+
rfl
|
| 209 |
+
|
| 210 |
+
end
|
| 211 |
+
end ContingencyTables.PairedDemandProgram
|
| 212 |
+
|
| 213 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/PairedFactorialProgram.lean
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Transport.PairedProfileSemantics
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Machines.FractionAggregateProgram
|
| 3 |
+
import OAI.Combinatorics.MatchingCount.Complexity.TreeNumericCosts
|
| 4 |
+
|
| 5 |
+
namespace OAI
|
| 6 |
+
|
| 7 |
+
/-! # The exact factorial multiplier from the encoded paired state
|
| 8 |
+
|
| 9 |
+
Every factorial argument is bounded by the supplied code length. In a
|
| 10 |
+
valid code each cell block has length twice its capacity, so the computed
|
| 11 |
+
denominator is exactly the manuscript's capacity factorial.
|
| 12 |
+
-/
|
| 13 |
+
|
| 14 |
+
namespace ContingencyTables.PairedFactorialProgram
|
| 15 |
+
|
| 16 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped Polynomial PairedProfileProgram
|
| 17 |
+
open scoped BigOperators Classical
|
| 18 |
+
|
| 19 |
+
def sideFactor (z : List ℕ × Bool) : ℕ := (sideCount z).factorial
|
| 20 |
+
def sideFactorRealizer : Realizer sideFactor := composition sideCountRealizer factorial
|
| 21 |
+
lemma polynomial_sideFactor : PolynomialTime sideFactorRealizer := by
|
| 22 |
+
apply polynomial_boundedFactorial polynomial_sideCount X
|
| 23 |
+
intro z
|
| 24 |
+
simpa only [eval_X] using (sideCount_le z.1 z.2).trans
|
| 25 |
+
((list_length_le_weight z.1).trans (weight_fst z))
|
| 26 |
+
|
| 27 |
+
def pairCount (xs : List ℕ) : ℕ := xs.length/2
|
| 28 |
+
def pairCountRealizer : Realizer pairCount :=
|
| 29 |
+
composition (pair listLength (constant 2)) quotient
|
| 30 |
+
lemma polynomial_pairCount : PolynomialTime pairCountRealizer :=
|
| 31 |
+
polynomial_composition (polynomial_pair polynomial_listLength (polynomial_constant 2))
|
| 32 |
+
polynomial_quotient
|
| 33 |
+
|
| 34 |
+
def denominator (xs : List ℕ) : ℕ := (pairCount xs).factorial
|
| 35 |
+
def denominatorRealizer : Realizer denominator := composition pairCountRealizer factorial
|
| 36 |
+
lemma polynomial_denominator : PolynomialTime denominatorRealizer := by
|
| 37 |
+
apply polynomial_boundedFactorial polynomial_pairCount X
|
| 38 |
+
intro xs
|
| 39 |
+
simpa only [eval_X,pairCount] using (Nat.div_le_self xs.length 2).trans (list_length_le_weight xs)
|
| 40 |
+
|
| 41 |
+
def numerator (xs : List ℕ) : ℕ := sideFactor (xs,false)*sideFactor (xs,true)
|
| 42 |
+
def numeratorRealizer : Realizer numerator :=
|
| 43 |
+
composition (f := fun xs : List ℕ => (sideFactor (xs,false),sideFactor (xs,true)))
|
| 44 |
+
(g := fun p : ℕ × ℕ => p.1*p.2)
|
| 45 |
+
(pair
|
| 46 |
+
(composition (f := fun xs : List ℕ => (xs,false)) (g := sideFactor)
|
| 47 |
+
(pair identity (constant false)) sideFactorRealizer)
|
| 48 |
+
(composition (f := fun xs : List ℕ => (xs,true)) (g := sideFactor)
|
| 49 |
+
(pair identity (constant true)) sideFactorRealizer)) multiply
|
| 50 |
+
lemma polynomial_numerator : PolynomialTime numeratorRealizer :=
|
| 51 |
+
polynomial_composition (polynomial_pair
|
| 52 |
+
(polynomial_composition (polynomial_pair polynomial_identity (polynomial_constant false))
|
| 53 |
+
polynomial_sideFactor)
|
| 54 |
+
(polynomial_composition (polynomial_pair polynomial_identity (polynomial_constant true))
|
| 55 |
+
polynomial_sideFactor)) polynomial_multiply
|
| 56 |
+
|
| 57 |
+
def rowRatio (xs : List ℕ) : TreeFraction.Fraction :=
|
| 58 |
+
TreeFraction.normalize (numerator xs,denominator xs)
|
| 59 |
+
def rowRatioRealizer : Realizer rowRatio :=
|
| 60 |
+
composition (pair numeratorRealizer denominatorRealizer) TreeFraction.normalizeRealizer
|
| 61 |
+
lemma polynomial_rowRatio : PolynomialTime rowRatioRealizer :=
|
| 62 |
+
polynomial_composition (polynomial_pair polynomial_numerator polynomial_denominator)
|
| 63 |
+
TreeFraction.polynomial_normalize
|
| 64 |
+
|
| 65 |
+
def output (z : PairedProfileProgram.Input) : TreeFraction.Fraction :=
|
| 66 |
+
FractionAggregateProgram.product ((rows z).map rowRatio)
|
| 67 |
+
def outputRealizer : Realizer output :=
|
| 68 |
+
composition (composition rowsRealizer (map rowRatioRealizer))
|
| 69 |
+
FractionAggregateProgram.productRealizer
|
| 70 |
+
theorem polynomial_output : PolynomialTime outputRealizer :=
|
| 71 |
+
polynomial_composition (polynomial_composition polynomial_rows
|
| 72 |
+
(polynomial_map polynomial_rowRatio)) FractionAggregateProgram.polynomial_product
|
| 73 |
+
|
| 74 |
+
lemma rowRatio_value (xs : List ℕ) :
|
| 75 |
+
TreeFraction.val (rowRatio xs)=
|
| 76 |
+
((sideCount (xs,false)).factorial:ℚ)*(sideCount (xs,true)).factorial /
|
| 77 |
+
(xs.length/2).factorial := by
|
| 78 |
+
rw [rowRatio,TreeFraction.normalize_val]
|
| 79 |
+
simp only [TreeFraction.val,numerator,sideFactor,denominator,pairCount,Nat.cast_mul]
|
| 80 |
+
|
| 81 |
+
noncomputable section
|
| 82 |
+
open PairedProfileSemantics InitialTransversals CanonicalInitialEncoding
|
| 83 |
+
open CompanionArithmeticProposal CompanionFactorialApplication
|
| 84 |
+
|
| 85 |
+
variable {q : ℕ} (b : Fin q → ℕ)
|
| 86 |
+
|
| 87 |
+
theorem output_canonical (S : Set (Copy b × Bool)) :
|
| 88 |
+
TreeFraction.val (output (List.ofFn b,encodeSet (coordinateEquiv b) S))=
|
| 89 |
+
∏ i : Fin q,
|
| 90 |
+
((∑ k : Fin (b i), if ((⟨i,k⟩ : Copy b),false) ∈ S then 1 else 0).factorial:ℚ)*
|
| 91 |
+
(∑ k : Fin (b i), if ((⟨i,k⟩ : Copy b),true) ∈ S then 1 else 0).factorial /
|
| 92 |
+
(b i).factorial := by
|
| 93 |
+
rw [output,FractionAggregateProgram.product_value,rows_encode]
|
| 94 |
+
simp only [cellRows,List.map_ofFn,List.prod_ofFn,Function.comp_apply]
|
| 95 |
+
apply Finset.prod_congr rfl
|
| 96 |
+
intro i _
|
| 97 |
+
rw [rowRatio_value]
|
| 98 |
+
have hc := counts_cellRow b S i
|
| 99 |
+
have hfalse := congrArg Prod.fst hc
|
| 100 |
+
have htrue := congrArg Prod.snd hc
|
| 101 |
+
change sideCount (_,false)=_ at hfalse
|
| 102 |
+
change sideCount (_,true)=_ at htrue
|
| 103 |
+
rw [hfalse,htrue,interleave_length,List.length_ofFn,
|
| 104 |
+
Nat.mul_div_cancel_left _ (by decide : 0 < 2)]
|
| 105 |
+
|
| 106 |
+
variable {m n : ℕ} (A : Instance (Fin m) (Fin n))
|
| 107 |
+
|
| 108 |
+
/-- The multiplier used by the executed weight oracle is the literal
|
| 109 |
+
factorial ratio, for every subset, not just transversal states. -/
|
| 110 |
+
theorem output_physical (S : Set (Labels A × Bool)) :
|
| 111 |
+
TreeFraction.val (output (List.ofFn (CompanionComputedInitial.capacities A),
|
| 112 |
+
encodeSet (CompanionComputedInitial.coordinateOrder A) S))=
|
| 113 |
+
CompanionWeightOracle.factorialRatio A S := by
|
| 114 |
+
rw [CompanionComputedInitial.encodeSet_reindex,output_canonical]
|
| 115 |
+
have hp (i) (side : Bool) :=
|
| 116 |
+
(physical_profile_reindex A S ((CompanionComputedInitial.cellOrder A).symm i) side).trans
|
| 117 |
+
(by rw [Equiv.apply_symm_apply,copy_viewCount])
|
| 118 |
+
simp_rw [←hp]
|
| 119 |
+
symm
|
| 120 |
+
unfold CompanionWeightOracle.factorialRatio CompanionWeightOracle.factorialNumerator
|
| 121 |
+
CompanionWeightOracle.factorialDenominator
|
| 122 |
+
simp only [Nat.cast_prod,Fintype.prod_prod_type,Fintype.prod_bool,Nat.cast_mul]
|
| 123 |
+
rw [←Finset.prod_div_distrib]
|
| 124 |
+
exact Fintype.prod_equiv (CompanionComputedInitial.cellOrder A) _ _ (fun s => by
|
| 125 |
+
simp only [Equiv.symm_apply_apply,CompanionComputedInitial.capacities]
|
| 126 |
+
ring)
|
| 127 |
+
|
| 128 |
+
end
|
| 129 |
+
end ContingencyTables.PairedFactorialProgram
|
| 130 |
+
|
| 131 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/PairedProfileProgram.lean
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.NaturalBlockProgram
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Machines.PairedCatalogProgram
|
| 3 |
+
|
| 4 |
+
namespace OAI
|
| 5 |
+
|
| 6 |
+
/-! # Reading the two cell counts from the actual paired code
|
| 7 |
+
|
| 8 |
+
The block widths come from the stored capacity list. Each count scans
|
| 9 |
+
only supplied code entries and clips them to one, including on malformed
|
| 10 |
+
input. No numeric capacity controls an unbounded allocation.
|
| 11 |
+
-/
|
| 12 |
+
|
| 13 |
+
namespace ContingencyTables.PairedProfileProgram
|
| 14 |
+
|
| 15 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped Polynomial
|
| 16 |
+
|
| 17 |
+
abbrev ReadInput := (List ℕ × Bool) × (ℕ × ℕ)
|
| 18 |
+
|
| 19 |
+
def sideValue (z : ReadInput) : ℕ :=
|
| 20 |
+
min (z.1.1.getD (if z.1.2 then z.2.2 else z.2.1) 0) 1
|
| 21 |
+
def sideValueRealizer : Realizer sideValue :=
|
| 22 |
+
composition (f := fun z : ReadInput =>
|
| 23 |
+
(z.1.1.getD (if z.1.2 then z.2.2 else z.2.1) 0,1))
|
| 24 |
+
(g := fun p : ℕ × ℕ => min p.1 p.2)
|
| 25 |
+
(pair (composition (pair (composition first first)
|
| 26 |
+
(choose (composition first second) (composition second second)
|
| 27 |
+
(composition second first))) (getDFast 0)) (constant 1)) minimum
|
| 28 |
+
lemma polynomial_sideValue : PolynomialTime sideValueRealizer :=
|
| 29 |
+
polynomial_composition (polynomial_pair
|
| 30 |
+
(polynomial_composition (polynomial_pair
|
| 31 |
+
(polynomial_composition polynomial_first polynomial_first)
|
| 32 |
+
(polynomial_choose (polynomial_composition polynomial_first polynomial_second)
|
| 33 |
+
(polynomial_composition polynomial_second polynomial_second)
|
| 34 |
+
(polynomial_composition polynomial_second polynomial_first))) (polynomial_getDFast 0))
|
| 35 |
+
(polynomial_constant 1)) polynomial_minimum
|
| 36 |
+
|
| 37 |
+
def sideEntries (z : List ℕ × Bool) : List ℕ :=
|
| 38 |
+
(PairedCatalogProgram.catalog z.1).map (fun p => sideValue (z,p))
|
| 39 |
+
def sideEntriesRealizer : Realizer sideEntries :=
|
| 40 |
+
composition (pair identity (composition first PairedCatalogProgram.catalogRealizer))
|
| 41 |
+
(mapWith sideValueRealizer)
|
| 42 |
+
lemma polynomial_sideEntries : PolynomialTime sideEntriesRealizer :=
|
| 43 |
+
polynomial_composition (polynomial_pair polynomial_identity
|
| 44 |
+
(polynomial_composition polynomial_first PairedCatalogProgram.polynomial_catalog))
|
| 45 |
+
(polynomial_mapWith polynomial_sideValue)
|
| 46 |
+
|
| 47 |
+
def sideCount (z : List ℕ × Bool) : ℕ := (sideEntries z).sum
|
| 48 |
+
def sideCountRealizer : Realizer sideCount := composition sideEntriesRealizer listSum
|
| 49 |
+
lemma polynomial_sideCount : PolynomialTime sideCountRealizer :=
|
| 50 |
+
polynomial_composition polynomial_sideEntries polynomial_listSum
|
| 51 |
+
|
| 52 |
+
lemma sideCount_le (xs : List ℕ) (side : Bool) : sideCount (xs,side) ≤ xs.length := by
|
| 53 |
+
have hs : (sideEntries (xs,side)).sum ≤ (sideEntries (xs,side)).length := by
|
| 54 |
+
simpa using (List.sum_le_length_nsmul (sideEntries (xs,side)) 1 (by
|
| 55 |
+
intro a ha
|
| 56 |
+
obtain ⟨p,_,rfl⟩ := List.mem_map.mp ha
|
| 57 |
+
exact Nat.min_le_right _ _))
|
| 58 |
+
exact hs.trans (by
|
| 59 |
+
simp only [sideEntries,PairedCatalogProgram.catalog,List.length_map,List.length_range]
|
| 60 |
+
exact Nat.div_le_self _ _)
|
| 61 |
+
|
| 62 |
+
abbrev Input := List ℕ × List ℕ
|
| 63 |
+
|
| 64 |
+
def width (n : ℕ) : ℕ := 2*n
|
| 65 |
+
def widthRealizer : Realizer width :=
|
| 66 |
+
composition (f := fun n : ℕ => (2,n)) (g := fun p : ℕ × ℕ => p.1*p.2)
|
| 67 |
+
(pair (constant 2) identity) multiply
|
| 68 |
+
lemma polynomial_width : PolynomialTime widthRealizer :=
|
| 69 |
+
polynomial_composition (polynomial_pair (polynomial_constant 2) polynomial_identity)
|
| 70 |
+
polynomial_multiply
|
| 71 |
+
|
| 72 |
+
def rows (z : Input) : List (List ℕ) :=
|
| 73 |
+
NaturalBlockProgram.blocks (z.1.map width,z.2)
|
| 74 |
+
def rowsRealizer : Realizer rows :=
|
| 75 |
+
composition (pair (composition first (map widthRealizer)) second)
|
| 76 |
+
NaturalBlockProgram.blocksRealizer
|
| 77 |
+
lemma polynomial_rows : PolynomialTime rowsRealizer :=
|
| 78 |
+
polynomial_composition (polynomial_pair
|
| 79 |
+
(polynomial_composition polynomial_first (polynomial_map polynomial_width)) polynomial_second)
|
| 80 |
+
NaturalBlockProgram.polynomial_blocks
|
| 81 |
+
|
| 82 |
+
def counts (xs : List ℕ) : ℕ × ℕ := (sideCount (xs,false),sideCount (xs,true))
|
| 83 |
+
def countsRealizer : Realizer counts :=
|
| 84 |
+
pair (composition (f := fun xs : List ℕ => (xs,false)) (g := sideCount)
|
| 85 |
+
(pair identity (constant false)) sideCountRealizer)
|
| 86 |
+
(composition (f := fun xs : List ℕ => (xs,true)) (g := sideCount)
|
| 87 |
+
(pair identity (constant true)) sideCountRealizer)
|
| 88 |
+
lemma polynomial_counts : PolynomialTime countsRealizer :=
|
| 89 |
+
polynomial_pair
|
| 90 |
+
(polynomial_composition (polynomial_pair polynomial_identity (polynomial_constant false))
|
| 91 |
+
polynomial_sideCount)
|
| 92 |
+
(polynomial_composition (polynomial_pair polynomial_identity (polynomial_constant true))
|
| 93 |
+
polynomial_sideCount)
|
| 94 |
+
|
| 95 |
+
def profileRows (z : Input) : List (ℕ × ℕ) := (rows z).map counts
|
| 96 |
+
def profileRowsRealizer : Realizer profileRows := composition rowsRealizer (map countsRealizer)
|
| 97 |
+
theorem polynomial_profileRows : PolynomialTime profileRowsRealizer :=
|
| 98 |
+
polynomial_composition polynomial_rows (polynomial_map polynomial_counts)
|
| 99 |
+
|
| 100 |
+
def interleave : List (ℕ × ℕ) → List ℕ
|
| 101 |
+
| [] => []
|
| 102 |
+
| p :: ps => p.1 :: p.2 :: interleave ps
|
| 103 |
+
|
| 104 |
+
lemma interleave_length (xs : List (ℕ × ℕ)) : (interleave xs).length=2*xs.length := by
|
| 105 |
+
induction xs with
|
| 106 |
+
| nil => rfl
|
| 107 |
+
| cons x xs ih => simp only [interleave,List.length_cons,ih]; omega
|
| 108 |
+
|
| 109 |
+
lemma interleave_get_even (xs : List (ℕ × ℕ)) (i : ℕ) (hi : i < xs.length) :
|
| 110 |
+
(interleave xs)[2*i]'(by rw [interleave_length]; omega)=xs[i].1 := by
|
| 111 |
+
induction xs generalizing i with
|
| 112 |
+
| nil => simp at hi
|
| 113 |
+
| cons x xs ih =>
|
| 114 |
+
cases i with
|
| 115 |
+
| zero => rfl
|
| 116 |
+
| succ i =>
|
| 117 |
+
simpa only [interleave,Nat.mul_succ,List.getElem_cons_succ] using
|
| 118 |
+
ih i (Nat.lt_of_succ_lt_succ hi)
|
| 119 |
+
|
| 120 |
+
lemma interleave_get_odd (xs : List (ℕ × ℕ)) (i : ℕ) (hi : i < xs.length) :
|
| 121 |
+
(interleave xs)[2*i+1]'(by rw [interleave_length]; omega)=xs[i].2 := by
|
| 122 |
+
induction xs generalizing i with
|
| 123 |
+
| nil => simp at hi
|
| 124 |
+
| cons x xs ih =>
|
| 125 |
+
cases i with
|
| 126 |
+
| zero => rfl
|
| 127 |
+
| succ i =>
|
| 128 |
+
simpa only [interleave,Nat.mul_succ,List.getElem_cons_succ,Nat.add_assoc] using
|
| 129 |
+
ih i (Nat.lt_of_succ_lt_succ hi)
|
| 130 |
+
|
| 131 |
+
lemma sideEntries_interleave (xs : List (ℕ × ℕ)) (side : Bool) :
|
| 132 |
+
sideEntries (interleave xs,side)=
|
| 133 |
+
xs.map (fun p => min (if side then p.2 else p.1) 1) := by
|
| 134 |
+
apply List.ext_getElem
|
| 135 |
+
· simp only [sideEntries,PairedCatalogProgram.catalog,List.length_map,List.length_range,
|
| 136 |
+
interleave_length,Nat.mul_div_cancel_left _ (by decide : 0 < 2)]
|
| 137 |
+
· intro i hi _
|
| 138 |
+
have hii : i < xs.length := by
|
| 139 |
+
simpa only [sideEntries,PairedCatalogProgram.catalog,List.length_map,List.length_range,
|
| 140 |
+
interleave_length,Nat.mul_div_cancel_left _ (by decide : 0 < 2)] using hi
|
| 141 |
+
simp only [sideEntries,PairedCatalogProgram.catalog,List.getElem_map,List.getElem_range,
|
| 142 |
+
sideValue,PairedCatalogProgram.indexPair]
|
| 143 |
+
cases side with
|
| 144 |
+
| false =>
|
| 145 |
+
have hidx : 2*i < (interleave xs).length := by rw [interleave_length]; omega
|
| 146 |
+
simp only [Bool.false_eq_true,ite_false,List.getD_eq_getElem?_getD,
|
| 147 |
+
List.getElem?_eq_getElem hidx,Option.getD_some,interleave_get_even xs i hii]
|
| 148 |
+
| true =>
|
| 149 |
+
have hidx : 2*i+1 < (interleave xs).length := by rw [interleave_length]; omega
|
| 150 |
+
simp only [ite_true,List.getD_eq_getElem?_getD,
|
| 151 |
+
List.getElem?_eq_getElem hidx,Option.getD_some,interleave_get_odd xs i hii]
|
| 152 |
+
|
| 153 |
+
lemma interleave_flatMap (xs : List (ℕ × ℕ)) :
|
| 154 |
+
interleave xs=xs.flatMap (fun p => [p.1,p.2]) := by
|
| 155 |
+
induction xs with
|
| 156 |
+
| nil => rfl
|
| 157 |
+
| cons p xs ih =>
|
| 158 |
+
change p.1 :: p.2 :: interleave xs=p.1 :: p.2 :: xs.flatMap (fun p => [p.1,p.2])
|
| 159 |
+
rw [ih]
|
| 160 |
+
|
| 161 |
+
end ContingencyTables.PairedProfileProgram
|
| 162 |
+
|
| 163 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/PowerApproximationProgram.lean
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.RoundedPowerProgram
|
| 2 |
+
|
| 3 |
+
namespace OAI
|
| 4 |
+
|
| 5 |
+
/-! # The executed power-of-two approximation used by the weight oracle
|
| 6 |
+
|
| 7 |
+
Both stages are concrete binary programs: compute the rounded signed
|
| 8 |
+
power, then bisect using natural cross-products and shared denominators.
|
| 9 |
+
The two allowance lists determine the precision parameters.
|
| 10 |
+
-/
|
| 11 |
+
|
| 12 |
+
namespace ContingencyTables.PowerApproximationProgram
|
| 13 |
+
|
| 14 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped
|
| 15 |
+
|
| 16 |
+
abbrev Input := SignedFloorProgram.Input×(List Bool×List Bool)
|
| 17 |
+
|
| 18 |
+
def prepare (z : Input) : DyadicRootProgram.Input :=
|
| 19 |
+
((RoundedPowerProgram.output (z.1,z.2.1),z.2.1),z.2.2)
|
| 20 |
+
def prepareRealizer : Realizer prepare :=
|
| 21 |
+
pair (pair
|
| 22 |
+
(composition (f := fun z : Input => (z.1,z.2.1)) (g := RoundedPowerProgram.output)
|
| 23 |
+
(pair first (composition second first)) RoundedPowerProgram.outputRealizer)
|
| 24 |
+
(composition second first)) (composition second second)
|
| 25 |
+
lemma polynomial_prepare : PolynomialTime prepareRealizer :=
|
| 26 |
+
polynomial_pair (polynomial_pair
|
| 27 |
+
(polynomial_composition (polynomial_pair polynomial_first
|
| 28 |
+
(polynomial_composition polynomial_second polynomial_first))
|
| 29 |
+
RoundedPowerProgram.polynomial_output)
|
| 30 |
+
(polynomial_composition polynomial_second polynomial_first))
|
| 31 |
+
(polynomial_composition polynomial_second polynomial_second)
|
| 32 |
+
|
| 33 |
+
def output (z : Input) : TreeFraction.Fraction := DyadicRootProgram.output (prepare z)
|
| 34 |
+
def outputRealizer : Realizer output :=
|
| 35 |
+
composition prepareRealizer DyadicRootProgram.outputRealizer
|
| 36 |
+
theorem polynomial_output : PolynomialTime outputRealizer :=
|
| 37 |
+
polynomial_composition polynomial_prepare DyadicRootProgram.polynomial_output
|
| 38 |
+
|
| 39 |
+
theorem output_value (p n d : ℕ) (degree depth : List Bool)
|
| 40 |
+
(hlo: -2≤((p:ℚ)-n)/d) (hhi: ((p:ℚ)-n)/d≤2) :
|
| 41 |
+
TreeFraction.val (output (((p,n),d),(degree,depth)))=
|
| 42 |
+
PowerOfTwoApproximation.approximation (((p:ℚ)-n)/d) degree.length depth.length := by
|
| 43 |
+
unfold output prepare
|
| 44 |
+
rw [DyadicRootProgram.output_value _ (RoundedPowerProgram.output_denominator_pos _),
|
| 45 |
+
RoundedPowerProgram.output_value p n d degree hlo hhi]
|
| 46 |
+
rfl
|
| 47 |
+
|
| 48 |
+
/-- The encoded output agrees exactly with the rational value used by
|
| 49 |
+
the existing finite-word correction laws. -/
|
| 50 |
+
theorem output_approximate (p n d : ℕ) (degree depth : List Bool) (sigma : ℚ)
|
| 51 |
+
(hdegree: degree.length=PowerOfTwoApproximation.rootDegree sigma)
|
| 52 |
+
(hdepth: depth.length=PowerOfTwoApproximation.depth sigma)
|
| 53 |
+
(hlo: -2≤((p:ℚ)-n)/d) (hhi: ((p:ℚ)-n)/d≤2) :
|
| 54 |
+
TreeFraction.val (output (((p,n),d),(degree,depth)))=
|
| 55 |
+
PowerOfTwoApproximation.approximate (((p:ℚ)-n)/d) sigma := by
|
| 56 |
+
rw [output_value p n d degree depth hlo hhi,hdegree,hdepth]
|
| 57 |
+
rfl
|
| 58 |
+
|
| 59 |
+
theorem output_error (p n d : ℕ) (degree depth : List Bool) (sigma : ℚ)
|
| 60 |
+
(hdegree: degree.length=PowerOfTwoApproximation.rootDegree sigma)
|
| 61 |
+
(hdepth: depth.length=PowerOfTwoApproximation.depth sigma)
|
| 62 |
+
(hlo: -2≤((p:ℚ)-n)/d) (hhi: ((p:ℚ)-n)/d≤2) (hsigma: 0<sigma) :
|
| 63 |
+
|(TreeFraction.val (output (((p,n),d),(degree,depth))):ℝ)-
|
| 64 |
+
(2:ℝ)^((((p:ℚ)-n)/d):ℝ)|≤
|
| 65 |
+
(sigma:ℝ)*(2:ℝ)^((((p:ℚ)-n)/d):ℝ) := by
|
| 66 |
+
rw [output_approximate p n d degree depth sigma hdegree hdepth hlo hhi]
|
| 67 |
+
simpa only [Rat.cast_div,Rat.cast_sub,Rat.cast_natCast] using
|
| 68 |
+
PowerOfTwoApproximation.approximate_error (((p:ℚ)-n)/d) sigma hlo hhi hsigma
|
| 69 |
+
|
| 70 |
+
end ContingencyTables.PowerApproximationProgram
|
| 71 |
+
|
| 72 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/PreparedDenseCount.lean
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Dense.DenseCountProgram
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Machines.BoundedPowerProgram
|
| 3 |
+
import OAI.Combinatorics.ContingencyTables.Dense.DenseTransitionCounts
|
| 4 |
+
|
| 5 |
+
namespace OAI
|
| 6 |
+
|
| 7 |
+
/-! # Polynomial transition entries with a shared acceptance allowance -/
|
| 8 |
+
|
| 9 |
+
namespace ContingencyTables.DensePrograms
|
| 10 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped ProfilePrograms GridBoundary
|
| 11 |
+
open scoped BigOperators Classical
|
| 12 |
+
|
| 13 |
+
abbrev PreparedCountInput := List Bool×CountInput
|
| 14 |
+
|
| 15 |
+
def preparedGap (z : PreparedCountInput) : ℕ :=
|
| 16 |
+
flatExponent (replaceFlat z.2)-flatExponent z.2.1
|
| 17 |
+
|
| 18 |
+
def preparedGapRealizer : Realizer preparedGap :=
|
| 19 |
+
composition (pair
|
| 20 |
+
(composition (composition second replaceFlatRealizer) flatExponentRealizer)
|
| 21 |
+
(composition (composition second first) flatExponentRealizer)) subtract
|
| 22 |
+
|
| 23 |
+
lemma polynomial_preparedGap : PolynomialTime preparedGapRealizer :=
|
| 24 |
+
polynomial_composition (polynomial_pair
|
| 25 |
+
(polynomial_composition (polynomial_composition polynomial_second polynomial_replaceFlat)
|
| 26 |
+
polynomial_flatExponent)
|
| 27 |
+
(polynomial_composition (polynomial_composition polynomial_second polynomial_first)
|
| 28 |
+
polynomial_flatExponent)) polynomial_subtract
|
| 29 |
+
|
| 30 |
+
def preparedOffCount (z : PreparedCountInput) : ℕ :=
|
| 31 |
+
if hasProposal (countProposalInput z.2) then
|
| 32 |
+
BoundedPowerPrograms.power (z.1,preparedGap z) else 0
|
| 33 |
+
|
| 34 |
+
def preparedOffCountRealizer : Realizer preparedOffCount := by
|
| 35 |
+
let pin : Realizer (fun z : PreparedCountInput => (z.1,preparedGap z)) :=
|
| 36 |
+
pair first preparedGapRealizer
|
| 37 |
+
let powered := composition pin BoundedPowerPrograms.powerRealizer
|
| 38 |
+
exact choose (composition (composition second countProposalInputRealizer) hasProposalRealizer)
|
| 39 |
+
powered (constant 0)
|
| 40 |
+
|
| 41 |
+
/-- The allowance is supplied once. Enumerating a row or the whole matrix
|
| 42 |
+
then uses only polynomial-time operations on binary entries. -/
|
| 43 |
+
theorem polynomial_preparedOffCount : PolynomialTime preparedOffCountRealizer := by
|
| 44 |
+
unfold preparedOffCountRealizer
|
| 45 |
+
exact polynomial_choose
|
| 46 |
+
(polynomial_composition (polynomial_composition polynomial_second polynomial_countProposalInput)
|
| 47 |
+
polynomial_hasProposal)
|
| 48 |
+
(polynomial_composition (polynomial_pair polynomial_first polynomial_preparedGap)
|
| 49 |
+
BoundedPowerPrograms.polynomial_power) (polynomial_constant 0)
|
| 50 |
+
|
| 51 |
+
lemma preparedOffCount_eq (xs : List Bool) (z : CountInput) (h : xs.length=8*z.1.2^5) :
|
| 52 |
+
preparedOffCount (xs,z)=offCountCode z := by
|
| 53 |
+
unfold preparedOffCount offCountCode
|
| 54 |
+
simp only [BoundedPowerPrograms.power_eq,BinaryPowerPrograms.power_eq,preparedGap,
|
| 55 |
+
countPowerInput,h]
|
| 56 |
+
|
| 57 |
+
lemma offCountCode_actual {m n d : ℕ} (R : Option (Fin m) → ℕ)
|
| 58 |
+
(P : Option (Fin n) → ℕ) (hq : m*n≤d)
|
| 59 |
+
(a b : Grid (I := Fin (m*n)) (4*d^8)) :
|
| 60 |
+
offCountCode ((((marginCode R,marginCode P),gridNatCode a),d),gridNatCode b)=
|
| 61 |
+
FiniteAcceptCounts.offCount a
|
| 62 |
+
(fun z : Fin (2^Nat.clog 2 (8*d)) => axisCodeStep a z)
|
| 63 |
+
(fun b => denseExponent R P d finProdFinEquiv b-
|
| 64 |
+
denseExponent R P d finProdFinEquiv a) (8*d^5) b := by
|
| 65 |
+
rw [offCountCode_eq]
|
| 66 |
+
by_cases hba : b=a
|
| 67 |
+
· subst b
|
| 68 |
+
have hn : ¬Neighbor a a := fun h => neighbor_ne a a h rfl
|
| 69 |
+
simp [FiniteAcceptCounts.offCount,hn]
|
| 70 |
+
· simp only [FiniteAcceptCounts.offCount,ite_eq_right hba]
|
| 71 |
+
rw [axisCodeStep_count (dense_proposal_code_bound d (m*n) hq)]
|
| 72 |
+
split_ifs <;> simp
|
| 73 |
+
|
| 74 |
+
end ContingencyTables.DensePrograms
|
| 75 |
+
|
| 76 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/PreparedOffsetCount.lean
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.BoundedPowerProgram
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Machines.OffsetCountProgram
|
| 3 |
+
import OAI.Combinatorics.ContingencyTables.Dense.DenseSuccessCounts
|
| 4 |
+
|
| 5 |
+
namespace OAI
|
| 6 |
+
|
| 7 |
+
/-! # Binary evaluation of the offset numerator product -/
|
| 8 |
+
|
| 9 |
+
namespace ContingencyTables.DensePrograms
|
| 10 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped GridBoundary FairBits
|
| 11 |
+
open scoped BigOperators
|
| 12 |
+
|
| 13 |
+
/-- A shared bit allowance, widths, actual bit lengths and offsets. -/
|
| 14 |
+
abbrev PreparedOffsetData := ((List Bool×List ℕ)×List ℕ)×List ℕ
|
| 15 |
+
|
| 16 |
+
def preparedOffsetInput (z : PreparedOffsetData×ℕ) : OffsetCountPrograms.Input :=
|
| 17 |
+
((z.1.1.1.2.getD z.2 0,
|
| 18 |
+
BoundedPowerPrograms.power (z.1.1.1.1,
|
| 19 |
+
z.1.1.1.1.length-z.1.1.2.getD z.2 0)),z.1.2.getD z.2 0)
|
| 20 |
+
|
| 21 |
+
def preparedOffsetInputRealizer : Realizer preparedOffsetInput := by
|
| 22 |
+
let cap : Realizer (fun z : PreparedOffsetData×ℕ => z.1.1.1.1) :=
|
| 23 |
+
composition (composition (composition first first) first) first
|
| 24 |
+
let widths : Realizer (fun z : PreparedOffsetData×ℕ => z.1.1.1.2) :=
|
| 25 |
+
composition (composition (composition first first) first) second
|
| 26 |
+
let lengths : Realizer (fun z : PreparedOffsetData×ℕ => z.1.1.2) :=
|
| 27 |
+
composition (composition first first) second
|
| 28 |
+
let offsets : Realizer (fun z : PreparedOffsetData×ℕ => z.1.2) := composition first second
|
| 29 |
+
let M := composition (pair widths second) (getDFast 0)
|
| 30 |
+
let r := composition (pair lengths second) (getDFast 0)
|
| 31 |
+
let beta := composition (pair offsets second) (getDFast 0)
|
| 32 |
+
let gap := composition (pair (composition cap listLength) r) subtract
|
| 33 |
+
let Q := composition (pair cap gap) BoundedPowerPrograms.powerRealizer
|
| 34 |
+
exact pair (pair M Q) beta
|
| 35 |
+
|
| 36 |
+
lemma polynomial_preparedOffsetInput : PolynomialTime preparedOffsetInputRealizer := by
|
| 37 |
+
have hcap : PolynomialTime (composition (composition (composition
|
| 38 |
+
(first (A := PreparedOffsetData) (B := ℕ)) first) first) first) := by tree_poly
|
| 39 |
+
have hw : PolynomialTime (composition (composition (composition
|
| 40 |
+
(first (A := PreparedOffsetData) (B := ℕ)) first) first) second) := by tree_poly
|
| 41 |
+
have hr : PolynomialTime (composition (composition
|
| 42 |
+
(first (A := PreparedOffsetData) (B := ℕ)) first) second) := by tree_poly
|
| 43 |
+
have ho : PolynomialTime (composition
|
| 44 |
+
(first (A := PreparedOffsetData) (B := ℕ)) second) := by tree_poly
|
| 45 |
+
have hM := polynomial_composition (polynomial_pair hw polynomial_second) (polynomial_getDFast 0)
|
| 46 |
+
have hR := polynomial_composition (polynomial_pair hr polynomial_second) (polynomial_getDFast 0)
|
| 47 |
+
have hB := polynomial_composition (polynomial_pair ho polynomial_second) (polynomial_getDFast 0)
|
| 48 |
+
have hg := polynomial_composition (polynomial_pair
|
| 49 |
+
(polynomial_composition hcap polynomial_listLength) hR) polynomial_subtract
|
| 50 |
+
have hQ := polynomial_composition (polynomial_pair hcap hg) BoundedPowerPrograms.polynomial_power
|
| 51 |
+
unfold preparedOffsetInputRealizer
|
| 52 |
+
exact polynomial_pair (polynomial_pair hM hQ) hB
|
| 53 |
+
|
| 54 |
+
def preparedOffsetFactor (z : PreparedOffsetData×ℕ) : ℕ :=
|
| 55 |
+
OffsetCountPrograms.offsetCount (preparedOffsetInput z)
|
| 56 |
+
|
| 57 |
+
def preparedOffsetFactorRealizer : Realizer preparedOffsetFactor :=
|
| 58 |
+
composition preparedOffsetInputRealizer OffsetCountPrograms.offsetCountRealizer
|
| 59 |
+
|
| 60 |
+
lemma polynomial_preparedOffsetFactor : PolynomialTime preparedOffsetFactorRealizer :=
|
| 61 |
+
polynomial_composition polynomial_preparedOffsetInput OffsetCountPrograms.polynomial_offsetCount
|
| 62 |
+
|
| 63 |
+
def preparedOffsetIndices (z : PreparedOffsetData) : List ℕ := List.range z.1.1.2.length
|
| 64 |
+
|
| 65 |
+
def preparedOffsetIndicesRealizer : Realizer preparedOffsetIndices :=
|
| 66 |
+
composition (composition (composition (composition first first) second) listLength) range
|
| 67 |
+
|
| 68 |
+
lemma polynomial_preparedOffsetIndices : PolynomialTime preparedOffsetIndicesRealizer := by
|
| 69 |
+
apply polynomial_boundedRange (polynomial_composition (polynomial_composition
|
| 70 |
+
(polynomial_composition polynomial_first polynomial_first) polynomial_second) polynomial_listLength)
|
| 71 |
+
Polynomial.X
|
| 72 |
+
intro z
|
| 73 |
+
simpa only [Function.comp_apply,Polynomial.eval_X] using
|
| 74 |
+
(list_length_le_weight z.1.1.2).trans
|
| 75 |
+
((weight_snd z.1.1).trans ((weight_fst z.1).trans (weight_fst z)))
|
| 76 |
+
|
| 77 |
+
def preparedOffsetProduct (z : PreparedOffsetData) : ℕ :=
|
| 78 |
+
((preparedOffsetIndices z).map (fun i => preparedOffsetFactor (z,i))).prod
|
| 79 |
+
|
| 80 |
+
def preparedOffsetProductRealizer : Realizer preparedOffsetProduct :=
|
| 81 |
+
composition (composition (pair identity preparedOffsetIndicesRealizer)
|
| 82 |
+
(mapWith preparedOffsetFactorRealizer)) listProd
|
| 83 |
+
|
| 84 |
+
/-- The product uses binary ceiling divisions and shared bounded powers.
|
| 85 |
+
It is polynomial in its supplied bit allowance and integer data. -/
|
| 86 |
+
theorem polynomial_preparedOffsetProduct : PolynomialTime preparedOffsetProductRealizer :=
|
| 87 |
+
polynomial_composition (polynomial_composition
|
| 88 |
+
(polynomial_pair polynomial_identity polynomial_preparedOffsetIndices)
|
| 89 |
+
(polynomial_mapWith polynomial_preparedOffsetFactor)) polynomial_listProd
|
| 90 |
+
|
| 91 |
+
lemma preparedOffsetFactor_ofFn {q : ℕ} (cap : List Bool) (w r o : Fin q ��� ℕ)
|
| 92 |
+
(j : Fin q) (hr : r j≤cap.length) :
|
| 93 |
+
preparedOffsetFactor ((((cap,List.ofFn w),List.ofFn r),List.ofFn o),j.val)=
|
| 94 |
+
OffsetCountPrograms.offsetCount ((w j,2^r j),o j) := by
|
| 95 |
+
unfold preparedOffsetFactor preparedOffsetInput
|
| 96 |
+
simp only [List.getD_eq_getElem?_getD,List.getElem?_ofFn,j.isLt,dite_true,Option.getD_some]
|
| 97 |
+
rw [BoundedPowerPrograms.power_eq,Nat.sub_sub_self hr]
|
| 98 |
+
|
| 99 |
+
private lemma range_prod (f : ℕ → ℕ) (q : ℕ) :
|
| 100 |
+
((List.range q).map f).prod=∏ j : Fin q,f j.val := by
|
| 101 |
+
induction q with
|
| 102 |
+
| zero => simp
|
| 103 |
+
| succ q ih =>
|
| 104 |
+
rw [List.prod_range_succ,Fin.prod_univ_castSucc,ih]
|
| 105 |
+
rfl
|
| 106 |
+
|
| 107 |
+
theorem preparedOffsetProduct_ofFn {q : ℕ} (cap : List Bool) (w r o : Fin q → ℕ)
|
| 108 |
+
(hr : ∀ j,r j≤cap.length) :
|
| 109 |
+
preparedOffsetProduct (((cap,List.ofFn w),List.ofFn r),List.ofFn o)=
|
| 110 |
+
∏ j : Fin q,OffsetCountPrograms.offsetCount ((w j,2^r j),o j) := by
|
| 111 |
+
unfold preparedOffsetProduct preparedOffsetIndices
|
| 112 |
+
rw [List.length_ofFn,range_prod]
|
| 113 |
+
apply Finset.prod_congr rfl
|
| 114 |
+
intro j _
|
| 115 |
+
exact preparedOffsetFactor_ofFn cap w r o j (hr j)
|
| 116 |
+
|
| 117 |
+
end ContingencyTables.DensePrograms
|
| 118 |
+
|
| 119 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/PreparedTransitionCache.lean
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Sampling.ProfileTransitionCount
|
| 2 |
+
|
| 3 |
+
namespace OAI
|
| 4 |
+
|
| 5 |
+
/-! # Computing the dense cache and the unused-bit multiplicity -/
|
| 6 |
+
|
| 7 |
+
namespace ContingencyTables.ProfilePrograms
|
| 8 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped DensePrograms
|
| 9 |
+
|
| 10 |
+
attribute [local irreducible] residualParameters residualParametersRealizer canonicalBitCount
|
| 11 |
+
canonicalBitCountRealizer completionWeights completionWeightsRealizer
|
| 12 |
+
|
| 13 |
+
abbrev TransitionCacheParameters := ResidualParameters×ℕ
|
| 14 |
+
|
| 15 |
+
def completionMultiplicity (z : TransitionCacheParameters) : ℕ :=
|
| 16 |
+
BinaryPowerPrograms.power (2,z.2-canonicalBitCount (residualParameters z.1))
|
| 17 |
+
|
| 18 |
+
def completionMultiplicityRealizer : Realizer completionMultiplicity := by
|
| 19 |
+
let used : Realizer (fun z : TransitionCacheParameters =>
|
| 20 |
+
canonicalBitCount (residualParameters z.1)) :=
|
| 21 |
+
composition (f := fun z : TransitionCacheParameters => residualParameters z.1)
|
| 22 |
+
(g := canonicalBitCount)
|
| 23 |
+
(composition (f := fun z : TransitionCacheParameters => z.1) (g := residualParameters)
|
| 24 |
+
first residualParametersRealizer) canonicalBitCountRealizer
|
| 25 |
+
let remaining : Realizer (fun z : TransitionCacheParameters =>
|
| 26 |
+
z.2-canonicalBitCount (residualParameters z.1)) :=
|
| 27 |
+
composition (f := fun z : TransitionCacheParameters =>
|
| 28 |
+
(z.2,canonicalBitCount (residualParameters z.1)))
|
| 29 |
+
(g := fun z : ℕ×ℕ => z.1-z.2) (pair second used) subtract
|
| 30 |
+
exact composition (f := fun z : TransitionCacheParameters =>
|
| 31 |
+
(2,z.2-canonicalBitCount (residualParameters z.1))) (g := BinaryPowerPrograms.power)
|
| 32 |
+
(pair (constant 2) remaining) BinaryPowerPrograms.powerRealizer
|
| 33 |
+
|
| 34 |
+
def prepareTransitionCache (z : TransitionCacheParameters) : TransitionCache :=
|
| 35 |
+
(z.1,(completionMultiplicity z,completionWeights (residualParameters z.1)))
|
| 36 |
+
|
| 37 |
+
def prepareTransitionCacheRealizer : Realizer prepareTransitionCache :=
|
| 38 |
+
pair first (pair completionMultiplicityRealizer
|
| 39 |
+
(composition (f := fun z : TransitionCacheParameters => residualParameters z.1)
|
| 40 |
+
(g := completionWeights)
|
| 41 |
+
(composition (f := fun z : TransitionCacheParameters => z.1) (g := residualParameters)
|
| 42 |
+
first residualParametersRealizer) completionWeightsRealizer))
|
| 43 |
+
|
| 44 |
+
end ContingencyTables.ProfilePrograms
|
| 45 |
+
|
| 46 |
+
namespace ContingencyTables.SmallGraphProfiles
|
| 47 |
+
open ProfilePrograms DensePrograms PhysicalCompletionFibres FirstPaperPhysicalMarginal
|
| 48 |
+
open SmallContextCoordinates CompletionCounts
|
| 49 |
+
open scoped BigOperators Classical
|
| 50 |
+
noncomputable section
|
| 51 |
+
|
| 52 |
+
variable {m n q : ℕ} (r : Fin m → ℕ) (c : Fin n → ℕ)
|
| 53 |
+
variable (e : Fin q ≃ Cells (paperSmall r c))
|
| 54 |
+
variable (hr : 0<(largeIndices ((dimensionAllowance (I := Fin m) (J := Fin n))^20,List.ofFn r)).length)
|
| 55 |
+
variable (hc : 0<(largeIndices ((dimensionAllowance (I := Fin m) (J := Fin n))^20,List.ofFn c)).length)
|
| 56 |
+
local notation "d" => dimensionAllowance (I := Fin m) (J := Fin n)
|
| 57 |
+
local notation "R" => listedRows r c (d^20) (d^12) (stateCapacity r c) hr
|
| 58 |
+
local notation "P" => listedColumns r c (d^20) (d^12) (stateCapacity r c) hc
|
| 59 |
+
|
| 60 |
+
lemma completionMultiplicity_encoded (b h : ℕ) (x : PaperStates r c) :
|
| 61 |
+
completionMultiplicity ((((stateCodeData r c e x,d^12),(d,h)),25*(d+b+h+1)^62))=
|
| 62 |
+
2^(25*(d+b+h+1)^62-normalizedDrawBits (R x) (P x) d h) := by
|
| 63 |
+
unfold completionMultiplicity
|
| 64 |
+
rw [residualParameters_code r c hr hc e,BinaryPowerPrograms.power_eq,
|
| 65 |
+
canonicalBitCount_code (R x) (P x) (by have := listed_dimension_fourteen r c hr hc; omega)
|
| 66 |
+
(listed_paper_totals r c hr hc x)]
|
| 67 |
+
|
| 68 |
+
lemma prepareTransitionCache_encoded (b h : ℕ) (x : PaperStates r c) :
|
| 69 |
+
prepareTransitionCache ((((stateCodeData r c e x,d^12),(d,h)),25*(d+b+h+1)^62))=
|
| 70 |
+
(((stateCodeData r c e x,d^12),(d,h)),
|
| 71 |
+
(2^(25*(d+b+h+1)^62-normalizedDrawBits (R x) (P x) d h),
|
| 72 |
+
completionWeights ((marginCode (R x),marginCode (P x)),(d,h)))) := by
|
| 73 |
+
unfold prepareTransitionCache
|
| 74 |
+
rw [completionMultiplicity_encoded r c e hr hc,
|
| 75 |
+
residualParameters_code r c hr hc e]
|
| 76 |
+
|
| 77 |
+
end
|
| 78 |
+
end ContingencyTables.SmallGraphProfiles
|
| 79 |
+
|
| 80 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/PublicInputLength.lean
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.AlgorithmStatements
|
| 2 |
+
import Mathlib.Data.Nat.Size
|
| 3 |
+
|
| 4 |
+
namespace OAI
|
| 5 |
+
|
| 6 |
+
/-! # Binary input lengths for the sampling machine -/
|
| 7 |
+
|
| 8 |
+
namespace ContingencyTables.Algorithms
|
| 9 |
+
open MatchingFPRAS
|
| 10 |
+
open scoped BigOperators
|
| 11 |
+
|
| 12 |
+
lemma encodeNat_length (n : ℕ) : (encodeNat n).length=n.size+1 := by
|
| 13 |
+
simp only [encodeNat,List.length_append,List.length_map,List.length_singleton,Nat.size_eq_bits_len]
|
| 14 |
+
|
| 15 |
+
lemma encodeNat_length_le (n N : ℕ) (hn : n ≤ N) :
|
| 16 |
+
(encodeNat n).length ≤ Nat.clog 2 (N+1)+1 := by
|
| 17 |
+
rw [encodeNat_length]
|
| 18 |
+
have hs : n.size ≤ Nat.clog 2 (N+1) := Nat.size_le.mpr
|
| 19 |
+
((Nat.lt_succ_of_le hn).trans_le (Nat.le_pow_clog (by decide) (N+1)))
|
| 20 |
+
omega
|
| 21 |
+
|
| 22 |
+
private lemma natWords_length (ns : List ℕ) (N : ℕ) (h : ∀ n∈ns,n ≤ N) :
|
| 23 |
+
(ns.flatMap encodeNat).length ≤ ns.length*(Nat.clog 2 (N+1)+1) := by
|
| 24 |
+
induction ns with
|
| 25 |
+
| nil => simp
|
| 26 |
+
| cons n ns ih =>
|
| 27 |
+
have hn := encodeNat_length_le n N (h n (by simp))
|
| 28 |
+
have ht := ih (fun a ha => h a (by simp [ha]))
|
| 29 |
+
simp only [List.flatMap_cons,List.length_append,List.length_cons,Nat.add_mul,Nat.one_mul]
|
| 30 |
+
omega
|
| 31 |
+
|
| 32 |
+
lemma encodeMargins_length {m n : ℕ} (r : Fin m → ℕ) (c : Fin n → ℕ)
|
| 33 |
+
(htotal : (∑ i,r i)=∑ j,c j) :
|
| 34 |
+
(encodeMargins r c).length ≤ m+n+2+(m+n)*(Nat.clog 2 ((∑ i,r i)+1)+1) := by
|
| 35 |
+
have hm : (encodeNat m).length ≤ m+1 := by
|
| 36 |
+
rw [encodeNat_length]
|
| 37 |
+
exact Nat.add_le_add_right (Nat.size_le.mpr (Nat.lt_two_pow_self)) 1
|
| 38 |
+
have hn : (encodeNat n).length ≤ n+1 := by
|
| 39 |
+
rw [encodeNat_length]
|
| 40 |
+
exact Nat.add_le_add_right (Nat.size_le.mpr (Nat.lt_two_pow_self)) 1
|
| 41 |
+
have hr := natWords_length (List.ofFn r) (∑ i,r i) (by
|
| 42 |
+
intro a ha
|
| 43 |
+
obtain ⟨i,rfl⟩ := List.mem_ofFn.mp ha
|
| 44 |
+
exact Finset.single_le_sum (fun _ _ => Nat.zero_le _) (Finset.mem_univ i))
|
| 45 |
+
have hc := natWords_length (List.ofFn c) (∑ i,r i) (by
|
| 46 |
+
intro a ha
|
| 47 |
+
obtain ⟨j,rfl⟩ := List.mem_ofFn.mp ha
|
| 48 |
+
rw [htotal]
|
| 49 |
+
exact Finset.single_le_sum (fun _ _ => Nat.zero_le _) (Finset.mem_univ j))
|
| 50 |
+
simp only [List.length_ofFn] at hr hc
|
| 51 |
+
simp only [List.flatMap_def,←List.ofFn_comp'] at hr hc
|
| 52 |
+
unfold encodeMargins
|
| 53 |
+
simp only [List.length_append]
|
| 54 |
+
nlinarith
|
| 55 |
+
|
| 56 |
+
/-- The bit input length is polynomial in the parameters used by the
|
| 57 |
+
paper's public sampling statement. -/
|
| 58 |
+
theorem encodeSamplingInput_length {m n : ℕ} (r : Fin m → ℕ) (c : Fin n → ℕ)
|
| 59 |
+
(htotal : (∑ i,r i)=∑ j,c j) (k : ℕ) :
|
| 60 |
+
(encodeSamplingInput r c k).length ≤ 5*(samplingSize n r k)^2 := by
|
| 61 |
+
have hM := encodeMargins_length r c htotal
|
| 62 |
+
have hk : (encodeNat k).length ≤ k+1 := by
|
| 63 |
+
rw [encodeNat_length]
|
| 64 |
+
exact Nat.add_le_add_right (Nat.size_le.mpr (Nat.lt_two_pow_self)) 1
|
| 65 |
+
rw [encodeSamplingInput,List.length_append]
|
| 66 |
+
let S := samplingSize n r k
|
| 67 |
+
have hS : 1 ≤ S := by dsimp [S,samplingSize]; omega
|
| 68 |
+
have hmn : m+n ≤ S := by dsimp [S,samplingSize]; omega
|
| 69 |
+
have hl : Nat.clog 2 ((∑ i,r i)+1)+1 ≤ S := by dsimp [S,samplingSize]; omega
|
| 70 |
+
have hkS : k+1 ≤ S := by dsimp [S,samplingSize]; omega
|
| 71 |
+
have hp := Nat.mul_le_mul hmn hl
|
| 72 |
+
change _ ≤ 5*S^2
|
| 73 |
+
nlinarith
|
| 74 |
+
|
| 75 |
+
end ContingencyTables.Algorithms
|
| 76 |
+
|
| 77 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/QuadraticPrefixRuntime.lean
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.GeometricRuntime
|
| 2 |
+
import Mathlib.Analysis.PSeries
|
| 3 |
+
import Mathlib.Analysis.SpecificLimits.Normed
|
| 4 |
+
|
| 5 |
+
namespace OAI
|
| 6 |
+
|
| 7 |
+
/-! # Expected time from geometrically accurate quadratic-cost prefixes -/
|
| 8 |
+
|
| 9 |
+
namespace ContingencyTables.QuadraticPrefixRuntime
|
| 10 |
+
open scoped BigOperators
|
| 11 |
+
|
| 12 |
+
lemma weighted_geometric : HasSum (fun k : ℕ => (2*(k:ℝ)+3)*(1/2:ℝ)^k) 10 := by
|
| 13 |
+
have h0 := hasSum_geometric_of_norm_lt_one (by norm_num : ‖(1/2:ℝ)‖<1)
|
| 14 |
+
have h1 := hasSum_coe_mul_geometric_of_norm_lt_one (by norm_num : ‖(1/2:ℝ)‖<1)
|
| 15 |
+
convert (h1.mul_left 2).add (h0.mul_left 3) using 1
|
| 16 |
+
· funext k; ring
|
| 17 |
+
· norm_num
|
| 18 |
+
|
| 19 |
+
/-- Quadratic simulation overhead preserves a finite expected running time.
|
| 20 |
+
The small prefactor `δ` is retained through the entire estimate. -/
|
| 21 |
+
theorem quadratic_bound (f : ℕ → ℝ) (T : ℕ) (hT : 0<T) (δ : ℝ) (hδ : 0≤δ)
|
| 22 |
+
(h0 : ∀ t,0≤f t) (hm : Antitone f) (hstart : f 0≤δ)
|
| 23 |
+
(hstep : ∀ N,f (T*(N+1)^2)≤δ*(1/2:ℝ)^N) :
|
| 24 |
+
Summable f ∧ (∑' t,f t)≤11*δ*T := by
|
| 25 |
+
let u : ℕ → ℕ := fun N => T*(N+1)^2
|
| 26 |
+
have hu : Monotone u := by
|
| 27 |
+
intro a b hab
|
| 28 |
+
dsimp only [u]
|
| 29 |
+
exact Nat.mul_le_mul_left T (Nat.pow_le_pow_left (by omega) 2)
|
| 30 |
+
have hup (N : ℕ) : 0<u N := Nat.mul_pos hT (pow_pos (by omega) _)
|
| 31 |
+
have hbound (R : ℕ) : ∑ t ∈ Finset.range R,f t≤11*δ*T := by
|
| 32 |
+
have hR : R≤u R := by
|
| 33 |
+
have ha : R≤(R+1)^2 := by nlinarith
|
| 34 |
+
have hb := Nat.mul_le_mul_right ((R+1)^2) (show 1≤T from hT)
|
| 35 |
+
exact ha.trans (by simpa only [Nat.one_mul] using hb)
|
| 36 |
+
have hs := Finset.le_sum_schlomilch (f := f) (u := u)
|
| 37 |
+
(by intro a b _ hab; exact hm hab) hup hu R
|
| 38 |
+
have hp : ∑ t ∈ Finset.range (u 0),f t≤T*δ := by
|
| 39 |
+
calc
|
| 40 |
+
_ ≤ ∑ _t ∈ Finset.range (u 0),δ := Finset.sum_le_sum (fun t _ => (hm (Nat.zero_le t)).trans hstart)
|
| 41 |
+
_ = T*δ := by simp [u]
|
| 42 |
+
have hb (k : ℕ) : (u (k+1)-u k) • f (u k)≤
|
| 43 |
+
(T:ℝ)*δ*((2*(k:ℝ)+3)*(1/2:ℝ)^k) := by
|
| 44 |
+
have he : u (k+1)=u k+T*(2*k+3) := by dsimp only [u]; ring
|
| 45 |
+
rw [he,Nat.add_sub_cancel_left,nsmul_eq_mul,Nat.cast_mul,Nat.cast_add,
|
| 46 |
+
Nat.cast_mul,Nat.cast_ofNat]
|
| 47 |
+
have hh := mul_le_mul_of_nonneg_left (hstep k) (by positivity : (0:ℝ)≤T*(2*k+3))
|
| 48 |
+
exact hh.trans_eq (by ring)
|
| 49 |
+
have hr : ∑ k ∈ Finset.range R,(u (k+1)-u k) • f (u k)≤10*T*δ := by
|
| 50 |
+
calc
|
| 51 |
+
_ ≤ ∑ k ∈ Finset.range R,(T:ℝ)*δ*((2*(k:ℝ)+3)*(1/2:ℝ)^k) :=
|
| 52 |
+
Finset.sum_le_sum (fun k _ => hb k)
|
| 53 |
+
_ = (T:ℝ)*δ*(∑ k ∈ Finset.range R,(2*(k:ℝ)+3)*(1/2:ℝ)^k) := by rw [Finset.mul_sum]
|
| 54 |
+
_ ≤ (T:ℝ)*δ*10 := mul_le_mul_of_nonneg_left
|
| 55 |
+
((weighted_geometric.summable.sum_le_tsum (Finset.range R)
|
| 56 |
+
(fun k _ => by positivity)).trans_eq weighted_geometric.tsum_eq) (by positivity)
|
| 57 |
+
_ = 10*T*δ := by ring
|
| 58 |
+
have hi : ∑ t ∈ Finset.range R,f t≤∑ t ∈ Finset.range (u R),f t :=
|
| 59 |
+
Finset.sum_le_sum_of_subset_of_nonneg (Finset.range_mono hR) (fun t _ _ => h0 t)
|
| 60 |
+
exact (hi.trans hs).trans ((add_le_add hp hr).trans_eq (by ring))
|
| 61 |
+
exact ⟨summable_of_sum_range_le h0 hbound,Real.tsum_le_of_sum_range_le h0 hbound⟩
|
| 62 |
+
|
| 63 |
+
/-- A bounded cheap prefix followed by a rare quadratic-cost correction. -/
|
| 64 |
+
theorem expected_time (tail : ℕ → ℝ) (S T : ℕ) (hT : 0<T) (δ : ℝ) (hδ : 0≤δ)
|
| 65 |
+
(h0 : ∀ t,0≤tail t) (h1 : ∀ t,tail t≤1) (hm : Antitone tail)
|
| 66 |
+
(hgate : tail S≤δ)
|
| 67 |
+
(hstep : ∀ N,tail (S+T*(N+1)^2)≤δ*(1/2:ℝ)^N) :
|
| 68 |
+
Summable tail ∧ (∑' t,tail t)≤S+11*δ*T := by
|
| 69 |
+
obtain ⟨hs,hb⟩ := quadratic_bound (fun t => tail (t+S)) T hT δ hδ
|
| 70 |
+
(fun t => h0 _) (fun _ _ h => hm (Nat.add_le_add_right h S))
|
| 71 |
+
(by simpa only [Nat.zero_add] using hgate)
|
| 72 |
+
(by intro N; simpa only [Nat.add_comm] using hstep N)
|
| 73 |
+
have ht : Summable tail := (summable_nat_add_iff S).mp hs
|
| 74 |
+
refine ⟨ht,?_⟩
|
| 75 |
+
rw [←ht.sum_add_tsum_nat_add S]
|
| 76 |
+
have hp : ∑ t ∈ Finset.range S,tail t≤(S:ℝ) := by
|
| 77 |
+
calc
|
| 78 |
+
_ ≤ ∑ _t ∈ Finset.range S,(1:ℝ) := Finset.sum_le_sum (fun t _ => h1 t)
|
| 79 |
+
_ = (S:ℝ) := by simp
|
| 80 |
+
exact add_le_add hp hb
|
| 81 |
+
|
| 82 |
+
end ContingencyTables.QuadraticPrefixRuntime
|
| 83 |
+
|
| 84 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/RationalIntervalProgram.lean
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.FractionAcceptanceProgram
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Rooted.ForestScaledPenalty
|
| 3 |
+
|
| 4 |
+
namespace OAI
|
| 5 |
+
|
| 6 |
+
/-! # Exact binary arithmetic for a forest interval penalty
|
| 7 |
+
|
| 8 |
+
A signed affine numerator is stored as a difference of two naturals.
|
| 9 |
+
Its distance outside the interval [0,1] is a nonnegative fraction with
|
| 10 |
+
the same denominator; no signed rational comparison is required.
|
| 11 |
+
-/
|
| 12 |
+
|
| 13 |
+
namespace ContingencyTables.RationalIntervalProgram
|
| 14 |
+
|
| 15 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped
|
| 16 |
+
|
| 17 |
+
abbrev Input := (ℕ×ℕ)×ℕ
|
| 18 |
+
|
| 19 |
+
def numerator (z : Input) : ℕ := (z.1.2-z.1.1)+(z.1.1-(z.1.2+z.2))
|
| 20 |
+
|
| 21 |
+
def numeratorRealizer : Realizer numerator := by
|
| 22 |
+
let P : Realizer (fun z : Input => z.1.1) := composition first first
|
| 23 |
+
let N : Realizer (fun z : Input => z.1.2) := composition first second
|
| 24 |
+
let D : Realizer (fun z : Input => z.2) := second
|
| 25 |
+
exact composition (pair (composition (pair N P) subtract)
|
| 26 |
+
(composition (pair P (composition (pair N D) add)) subtract)) add
|
| 27 |
+
|
| 28 |
+
lemma polynomial_numerator : PolynomialTime numeratorRealizer := by
|
| 29 |
+
unfold numeratorRealizer
|
| 30 |
+
have hp := polynomial_composition
|
| 31 |
+
(polynomial_first (A := ℕ×ℕ) (B := ℕ)) polynomial_first
|
| 32 |
+
have hn := polynomial_composition
|
| 33 |
+
(polynomial_first (A := ℕ×ℕ) (B := ℕ)) polynomial_second
|
| 34 |
+
exact polynomial_composition (polynomial_pair
|
| 35 |
+
(polynomial_composition (polynomial_pair hn hp) polynomial_subtract)
|
| 36 |
+
(polynomial_composition (polynomial_pair hp
|
| 37 |
+
(polynomial_composition (polynomial_pair hn polynomial_second) polynomial_add))
|
| 38 |
+
polynomial_subtract)) polynomial_add
|
| 39 |
+
|
| 40 |
+
def intervalCode (z : Input) : TreeFraction.Fraction :=
|
| 41 |
+
TreeFraction.normalize (numerator z,z.2)
|
| 42 |
+
|
| 43 |
+
def intervalCodeRealizer : Realizer intervalCode :=
|
| 44 |
+
composition (f := fun z : Input => (numerator z,z.2)) (g := TreeFraction.normalize)
|
| 45 |
+
(pair numeratorRealizer second) TreeFraction.normalizeRealizer
|
| 46 |
+
|
| 47 |
+
theorem polynomial_intervalCode : PolynomialTime intervalCodeRealizer :=
|
| 48 |
+
polynomial_composition (polynomial_pair polynomial_numerator polynomial_second)
|
| 49 |
+
TreeFraction.polynomial_normalize
|
| 50 |
+
|
| 51 |
+
lemma intervalCode_denominator (z : Input) : 0<(intervalCode z).2 :=
|
| 52 |
+
TreeFraction.normalize_den_pos _
|
| 53 |
+
|
| 54 |
+
noncomputable section
|
| 55 |
+
|
| 56 |
+
/-- Exact agreement with the interval cost in the forest density. The
|
| 57 |
+
only side condition is the strictly positive tree capacity denominator. -/
|
| 58 |
+
theorem intervalCode_value (p n d : ℕ) (hd : 0<d) :
|
| 59 |
+
TreeFraction.val (intervalCode ((p,n),d))=
|
| 60 |
+
max 0 (max (-(((p:ℚ)-n)/d)) (((p:ℚ)-n)/d-1)) := by
|
| 61 |
+
rw [intervalCode,TreeFraction.normalize_val]
|
| 62 |
+
change (((n-p)+(p-(n+d)):ℕ):ℚ)/d=_
|
| 63 |
+
have hdq : (0:ℚ)<d := by exact_mod_cast hd
|
| 64 |
+
let x : ℚ := ((p:ℚ)-n)/d
|
| 65 |
+
change (((n-p)+(p-(n+d)):ℕ):ℚ)/d=max 0 (max (-x) (x-1))
|
| 66 |
+
by_cases hpn : p≤n
|
| 67 |
+
· have hx : x≤0 := div_nonpos_of_nonpos_of_nonneg
|
| 68 |
+
(sub_nonpos.mpr (by exact_mod_cast hpn)) hdq.le
|
| 69 |
+
have hsub : p≤n+d := by omega
|
| 70 |
+
simp only [Nat.sub_eq_zero_of_le hsub,Nat.add_zero,Nat.cast_sub hpn,
|
| 71 |
+
max_eq_left (show x-1≤-x by linarith),max_eq_right (neg_nonneg.mpr hx)]
|
| 72 |
+
dsimp only [x]
|
| 73 |
+
ring
|
| 74 |
+
· have hnp : n≤p := by omega
|
| 75 |
+
have hx : 0≤x := div_nonneg (sub_nonneg.mpr (by exact_mod_cast hnp)) hdq.le
|
| 76 |
+
by_cases hpd : p≤n+d
|
| 77 |
+
· have hx1 : x≤1 := (div_le_one hdq).mpr (by
|
| 78 |
+
have hh : (p:ℚ)≤n+d := by exact_mod_cast hpd
|
| 79 |
+
linarith)
|
| 80 |
+
simp only [Nat.sub_eq_zero_of_le hnp,Nat.sub_eq_zero_of_le hpd,Nat.zero_add,
|
| 81 |
+
Nat.cast_zero,zero_div]
|
| 82 |
+
symm
|
| 83 |
+
exact max_eq_left (max_le (neg_nonpos.mpr hx) (by linarith))
|
| 84 |
+
· have hdp : n+d≤p := by omega
|
| 85 |
+
have hx1 : 1≤x := (le_div_iff₀ hdq).mpr (by
|
| 86 |
+
have hh : (n:ℚ)+d≤p := by exact_mod_cast hdp
|
| 87 |
+
linarith)
|
| 88 |
+
simp only [Nat.sub_eq_zero_of_le hnp,Nat.zero_add,Nat.cast_sub hdp,Nat.cast_add,
|
| 89 |
+
max_eq_right (show -x≤x-1 by linarith),max_eq_right (sub_nonneg.mpr hx1)]
|
| 90 |
+
dsimp only [x]
|
| 91 |
+
field_simp [hdq.ne']
|
| 92 |
+
ring
|
| 93 |
+
|
| 94 |
+
end
|
| 95 |
+
end ContingencyTables.RationalIntervalProgram
|
| 96 |
+
|
| 97 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/RepeatedFractionProgram.lean
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.MedianFractionProgram
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Machines.BooleanChunkProgram
|
| 3 |
+
import OAI.Combinatorics.ContingencyTables.Dense.DenseMetropolisSemantics
|
| 4 |
+
|
| 5 |
+
namespace OAI
|
| 6 |
+
|
| 7 |
+
/-! # Independent repetition of a finite-word rational program
|
| 8 |
+
|
| 9 |
+
The public confidence amplification only groups bits already supplied
|
| 10 |
+
on the tape. Its binary count cannot cause an unbounded allocation.
|
| 11 |
+
-/
|
| 12 |
+
|
| 13 |
+
namespace ContingencyTables.RepeatedFractionProgram
|
| 14 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped DensePrograms
|
| 15 |
+
|
| 16 |
+
variable {X : Type} [Codec X]
|
| 17 |
+
abbrev Input (X : Type) := (X×(ℕ×ℕ))×List Bool
|
| 18 |
+
|
| 19 |
+
def words (z : Input X) : List (List Bool) :=
|
| 20 |
+
(wordChunks (z.1.2.2,z.2)).take z.1.2.1
|
| 21 |
+
def wordsRealizer : Realizer (words (X:=X)) :=
|
| 22 |
+
composition (f := fun z : Input X => (wordChunks (z.1.2.2,z.2),z.1.2.1))
|
| 23 |
+
(g := fun p : List (List Bool)×ℕ => p.1.take p.2)
|
| 24 |
+
(pair
|
| 25 |
+
(composition (f := fun z : Input X => (z.1.2.2,z.2)) (g := wordChunks)
|
| 26 |
+
(pair (composition (composition first second) second) second) wordChunksRealizer)
|
| 27 |
+
(composition (composition first second) first)) takeFast
|
| 28 |
+
lemma polynomial_words : PolynomialTime (wordsRealizer (X:=X)) :=
|
| 29 |
+
polynomial_composition (polynomial_pair
|
| 30 |
+
(polynomial_composition (polynomial_pair
|
| 31 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_second) polynomial_second)
|
| 32 |
+
polynomial_second) polynomial_wordChunks)
|
| 33 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_second) polynomial_first))
|
| 34 |
+
polynomial_takeFast
|
| 35 |
+
|
| 36 |
+
variable {f : X×List Bool→TreeFraction.Fraction}
|
| 37 |
+
|
| 38 |
+
def sample (f : X×List Bool→TreeFraction.Fraction) (z : Input X×List Bool) : TreeFraction.Fraction :=
|
| 39 |
+
f (z.1.1.1,z.2)
|
| 40 |
+
def sampleRealizer (F : Realizer f) : Realizer (sample f) :=
|
| 41 |
+
composition (f := fun z : Input X×List Bool => (z.1.1.1,z.2)) (g := f)
|
| 42 |
+
(pair (composition (composition first first) first) second) F
|
| 43 |
+
lemma polynomial_sample (F : Realizer f) (hF : PolynomialTime F) :
|
| 44 |
+
PolynomialTime (sampleRealizer F) :=
|
| 45 |
+
polynomial_composition (polynomial_pair
|
| 46 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first) polynomial_first)
|
| 47 |
+
polynomial_second) hF
|
| 48 |
+
|
| 49 |
+
def samples (f : X×List Bool→TreeFraction.Fraction) (z : Input X) : List TreeFraction.Fraction :=
|
| 50 |
+
(words z).map (fun bits => sample f (z,bits))
|
| 51 |
+
def samplesRealizer (F : Realizer f) : Realizer (samples f) :=
|
| 52 |
+
composition (pair identity wordsRealizer) (mapWith (sampleRealizer F))
|
| 53 |
+
lemma polynomial_samples (F : Realizer f) (hF : PolynomialTime F) :
|
| 54 |
+
PolynomialTime (samplesRealizer F) :=
|
| 55 |
+
polynomial_composition (polynomial_pair polynomial_identity polynomial_words)
|
| 56 |
+
(polynomial_mapWith (polynomial_sample F hF))
|
| 57 |
+
|
| 58 |
+
attribute [local irreducible] samples
|
| 59 |
+
|
| 60 |
+
def output (f : X×List Bool→TreeFraction.Fraction) (z : Input X) : TreeFraction.Fraction :=
|
| 61 |
+
MedianFractionProgram.median (samples f z)
|
| 62 |
+
def outputRealizer (F : Realizer f) : Realizer (output f) :=
|
| 63 |
+
composition (f := samples f) (g := MedianFractionProgram.median)
|
| 64 |
+
(samplesRealizer F) MedianFractionProgram.medianRealizer
|
| 65 |
+
lemma polynomial_output (F : Realizer f) (hF : PolynomialTime F) :
|
| 66 |
+
PolynomialTime (outputRealizer F) :=
|
| 67 |
+
polynomial_composition (polynomial_samples F hF) MedianFractionProgram.polynomial_median
|
| 68 |
+
|
| 69 |
+
open GridBoundary CommonBasesFPRAS
|
| 70 |
+
|
| 71 |
+
omit [Codec X] in
|
| 72 |
+
theorem output_ofFn (x : X) (r w : ℕ) (hw : 0 < w) (bits : Fin (r*w)→Bool) :
|
| 73 |
+
TreeFraction.val (output f ((x,(r,w)),List.ofFn bits))=
|
| 74 |
+
rationalMedian (List.ofFn (fun i : Fin r =>
|
| 75 |
+
TreeFraction.val (f (x,List.ofFn (gridWordEquiv r w bits i))))) := by
|
| 76 |
+
rw [output,MedianFractionProgram.median_val]
|
| 77 |
+
have hwords : words ((x,(r,w)),List.ofFn bits)=
|
| 78 |
+
List.ofFn (fun i : Fin r => List.ofFn (gridWordEquiv r w bits i)) := by
|
| 79 |
+
change (wordChunks (w,List.ofFn bits)).take r=_
|
| 80 |
+
rw [wordChunks_ofFn hw]
|
| 81 |
+
exact List.take_of_length_le (by simp)
|
| 82 |
+
unfold samples
|
| 83 |
+
rw [hwords]
|
| 84 |
+
simp only [List.map_ofFn]
|
| 85 |
+
rfl
|
| 86 |
+
|
| 87 |
+
end ContingencyTables.RepeatedFractionProgram
|
| 88 |
+
|
| 89 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/ResidualDenseProgram.lean
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Rooted.LargeMarginProgram
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Dense.DenseCanonicalProgram
|
| 3 |
+
|
| 4 |
+
namespace OAI
|
| 5 |
+
|
| 6 |
+
/-! # A computed dense completion call from a retained small profile -/
|
| 7 |
+
|
| 8 |
+
namespace ContingencyTables.ProfilePrograms
|
| 9 |
+
open MatchingFPRAS.TreeTyped
|
| 10 |
+
|
| 11 |
+
abbrev ResidualParameters := (ProfileCheckData×ℕ)×(ℕ×ℕ)
|
| 12 |
+
abbrev ResidualDrawData := ResidualParameters×List Bool
|
| 13 |
+
|
| 14 |
+
def residualParameters (z : ResidualParameters) : DensePrograms.CanonicalParameters :=
|
| 15 |
+
((profilePaddedMargins true z.1,profilePaddedMargins false z.1),z.2)
|
| 16 |
+
|
| 17 |
+
def residualParametersRealizer : Realizer residualParameters := by
|
| 18 |
+
let rows : Realizer (fun z : ResidualParameters => profilePaddedMargins true z.1) :=
|
| 19 |
+
composition (f := fun z : ResidualParameters => z.1) (g := profilePaddedMargins true)
|
| 20 |
+
first (profilePaddedMarginsRealizer true)
|
| 21 |
+
let columns : Realizer (fun z : ResidualParameters => profilePaddedMargins false z.1) :=
|
| 22 |
+
composition (f := fun z : ResidualParameters => z.1) (g := profilePaddedMargins false)
|
| 23 |
+
first (profilePaddedMarginsRealizer false)
|
| 24 |
+
exact pair (pair rows columns) second
|
| 25 |
+
|
| 26 |
+
theorem polynomial_residualParameters : PolynomialTime residualParametersRealizer := by
|
| 27 |
+
unfold residualParametersRealizer
|
| 28 |
+
exact polynomial_pair (polynomial_pair
|
| 29 |
+
(polynomial_composition polynomial_first (polynomial_profilePaddedMargins true))
|
| 30 |
+
(polynomial_composition polynomial_first (polynomial_profilePaddedMargins false)))
|
| 31 |
+
polynomial_second
|
| 32 |
+
|
| 33 |
+
def residualDenseDraw (z : ResidualDrawData) : DensePrograms.MatrixCode :=
|
| 34 |
+
DensePrograms.canonicalDenseDraw (residualParameters z.1,z.2)
|
| 35 |
+
|
| 36 |
+
def residualDenseDrawRealizer : Realizer residualDenseDraw :=
|
| 37 |
+
composition (pair (composition first residualParametersRealizer) second)
|
| 38 |
+
DensePrograms.canonicalDenseDrawRealizer
|
| 39 |
+
|
| 40 |
+
/-- The actual completion call computes the residual margins, selects
|
| 41 |
+
their large rectangle, initializes it, and draws a dense matrix. -/
|
| 42 |
+
theorem polynomial_residualDenseDraw : PolynomialTime residualDenseDrawRealizer :=
|
| 43 |
+
polynomial_composition (polynomial_pair
|
| 44 |
+
(polynomial_composition polynomial_first polynomial_residualParameters) polynomial_second)
|
| 45 |
+
DensePrograms.polynomial_canonicalDenseDraw
|
| 46 |
+
|
| 47 |
+
end ContingencyTables.ProfilePrograms
|
| 48 |
+
|
| 49 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/ResidualGraphProgram.lean
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.DirectedBFSRuntime
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Machines.SignedPairProgram
|
| 3 |
+
import OAI.Combinatorics.ContingencyTables.Transport.ResidualSearch
|
| 4 |
+
|
| 5 |
+
namespace OAI
|
| 6 |
+
|
| 7 |
+
/-! # Binary construction and search of the actual residual graph -/
|
| 8 |
+
|
| 9 |
+
namespace ContingencyTables.ResidualGraphProgram
|
| 10 |
+
|
| 11 |
+
open MatchingFPRAS.TreeTyped DirectedBFSProgram SignedPairProgram
|
| 12 |
+
|
| 13 |
+
abbrev NatMatrix := List (List ℕ)
|
| 14 |
+
abbrev FlowMatrix := List (List Signed)
|
| 15 |
+
abbrev Data := NatMatrix×FlowMatrix
|
| 16 |
+
abbrev ArcInput := Data×(ℕ×ℕ)
|
| 17 |
+
|
| 18 |
+
def matrixIndices (C : NatMatrix) : List ℕ := List.range C.length
|
| 19 |
+
|
| 20 |
+
def matrixIndicesRealizer : Realizer matrixIndices := composition listLength range
|
| 21 |
+
|
| 22 |
+
lemma polynomial_matrixIndices : PolynomialTime matrixIndicesRealizer := by
|
| 23 |
+
apply polynomial_boundedRange polynomial_listLength Polynomial.X
|
| 24 |
+
intro C
|
| 25 |
+
simpa only [Function.comp_apply,Polynomial.eval_X] using list_length_le_weight C
|
| 26 |
+
|
| 27 |
+
def capacityEntry (z : NatMatrix×(ℕ×ℕ)) : ℕ :=
|
| 28 |
+
(z.1.getD z.2.1 []).getD z.2.2 0
|
| 29 |
+
|
| 30 |
+
def capacityEntryRealizer : Realizer capacityEntry :=
|
| 31 |
+
composition (pair (composition (pair first (composition second first)) (getDFast []))
|
| 32 |
+
(composition second second)) (getDFast 0)
|
| 33 |
+
|
| 34 |
+
lemma polynomial_capacityEntry : PolynomialTime capacityEntryRealizer :=
|
| 35 |
+
polynomial_composition (polynomial_pair (polynomial_composition
|
| 36 |
+
(polynomial_pair polynomial_first (polynomial_composition polynomial_second polynomial_first))
|
| 37 |
+
(polynomial_getDFast [])) (polynomial_composition polynomial_second polynomial_second))
|
| 38 |
+
(polynomial_getDFast 0)
|
| 39 |
+
|
| 40 |
+
def flowEntry (z : FlowMatrix×(ℕ×ℕ)) : Signed :=
|
| 41 |
+
(z.1.getD z.2.1 []).getD z.2.2 (0,0)
|
| 42 |
+
|
| 43 |
+
def flowEntryRealizer : Realizer flowEntry :=
|
| 44 |
+
composition (pair (composition (pair first (composition second first)) (getDFast []))
|
| 45 |
+
(composition second second)) (getDFast (0,0))
|
| 46 |
+
|
| 47 |
+
lemma polynomial_flowEntry : PolynomialTime flowEntryRealizer :=
|
| 48 |
+
polynomial_composition (polynomial_pair (polynomial_composition
|
| 49 |
+
(polynomial_pair polynomial_first (polynomial_composition polynomial_second polynomial_first))
|
| 50 |
+
(polynomial_getDFast [])) (polynomial_composition polynomial_second polynomial_second))
|
| 51 |
+
(polynomial_getDFast (0,0))
|
| 52 |
+
|
| 53 |
+
def arcData (z : ArcInput) : ℕ×Signed :=
|
| 54 |
+
(capacityEntry (z.1.1,z.2),flowEntry (z.1.2,z.2))
|
| 55 |
+
|
| 56 |
+
def arcDataRealizer : Realizer arcData :=
|
| 57 |
+
pair (composition (pair (composition first first) second) capacityEntryRealizer)
|
| 58 |
+
(composition (pair (composition first second) second) flowEntryRealizer)
|
| 59 |
+
|
| 60 |
+
lemma polynomial_arcData : PolynomialTime arcDataRealizer :=
|
| 61 |
+
polynomial_pair (polynomial_composition (polynomial_pair
|
| 62 |
+
(polynomial_composition polynomial_first polynomial_first) polynomial_second)
|
| 63 |
+
polynomial_capacityEntry)
|
| 64 |
+
(polynomial_composition (polynomial_pair
|
| 65 |
+
(polynomial_composition polynomial_first polynomial_second) polynomial_second)
|
| 66 |
+
polynomial_flowEntry)
|
| 67 |
+
|
| 68 |
+
def residualValue (z : ℕ×Signed) : ℕ := (z.1+z.2.2)-z.2.1
|
| 69 |
+
|
| 70 |
+
def residualValueRealizer : Realizer residualValue :=
|
| 71 |
+
composition (pair (composition (pair first (composition second second)) add)
|
| 72 |
+
(composition second first)) subtract
|
| 73 |
+
|
| 74 |
+
lemma polynomial_residualValue : PolynomialTime residualValueRealizer :=
|
| 75 |
+
polynomial_composition (polynomial_pair (polynomial_composition
|
| 76 |
+
(polynomial_pair polynomial_first (polynomial_composition polynomial_second polynomial_second))
|
| 77 |
+
polynomial_add) (polynomial_composition polynomial_second polynomial_first)) polynomial_subtract
|
| 78 |
+
|
| 79 |
+
def positive (z : ℕ×Signed) : Bool := decide (z.2.1< z.1+z.2.2)
|
| 80 |
+
|
| 81 |
+
def positiveRealizer : Realizer positive :=
|
| 82 |
+
composition (pair (composition second first)
|
| 83 |
+
(composition (pair first (composition second second)) add)) less
|
| 84 |
+
|
| 85 |
+
lemma polynomial_positive : PolynomialTime positiveRealizer :=
|
| 86 |
+
polynomial_composition (polynomial_pair
|
| 87 |
+
(polynomial_composition polynomial_second polynomial_first)
|
| 88 |
+
(polynomial_composition (polynomial_pair polynomial_first
|
| 89 |
+
(polynomial_composition polynomial_second polynomial_second)) polynomial_add)) polynomial_less
|
| 90 |
+
|
| 91 |
+
def arc (z : ArcInput) : Bool := positive (arcData z)
|
| 92 |
+
def arcRealizer : Realizer arc := composition arcDataRealizer positiveRealizer
|
| 93 |
+
lemma polynomial_arc : PolynomialTime arcRealizer :=
|
| 94 |
+
polynomial_composition polynomial_arcData polynomial_positive
|
| 95 |
+
|
| 96 |
+
def arcRow (z : Data×ℕ) : List Bool :=
|
| 97 |
+
(List.range z.1.1.length).map (fun v=>arc (z.1,(z.2,v)))
|
| 98 |
+
|
| 99 |
+
def arcRowRealizer : Realizer arcRow :=
|
| 100 |
+
composition
|
| 101 |
+
(f := fun z : Data×ℕ=>(z,List.range z.1.1.length))
|
| 102 |
+
(g := fun z : (Data×ℕ)×List ℕ=>z.2.map (fun v=>arc (z.1.1,(z.1.2,v))))
|
| 103 |
+
(pair identity (composition
|
| 104 |
+
(f := fun z : Data×ℕ=>z.1.1) (g := matrixIndices)
|
| 105 |
+
(composition first first) matrixIndicesRealizer))
|
| 106 |
+
(mapWith (composition
|
| 107 |
+
(f := fun z : (Data×ℕ)×ℕ=>(z.1.1,(z.1.2,z.2))) (g := arc)
|
| 108 |
+
(pair (composition first first) (pair (composition first second) second)) arcRealizer))
|
| 109 |
+
|
| 110 |
+
lemma polynomial_arcRow : PolynomialTime arcRowRealizer :=
|
| 111 |
+
polynomial_composition (polynomial_pair polynomial_identity
|
| 112 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 113 |
+
polynomial_matrixIndices))
|
| 114 |
+
(polynomial_mapWith (polynomial_composition (polynomial_pair
|
| 115 |
+
(polynomial_composition polynomial_first polynomial_first)
|
| 116 |
+
(polynomial_pair (polynomial_composition polynomial_first polynomial_second)
|
| 117 |
+
polynomial_second)) polynomial_arc))
|
| 118 |
+
|
| 119 |
+
def graph (z : Data) : DirectedBFSCode.Graph :=
|
| 120 |
+
(List.range z.1.length).map (fun u=>arcRow (z,u))
|
| 121 |
+
|
| 122 |
+
def graphRealizer : Realizer graph :=
|
| 123 |
+
composition
|
| 124 |
+
(f := fun z : Data=>(z,List.range z.1.length))
|
| 125 |
+
(g := fun z : Data×List ℕ=>z.2.map (fun u=>arcRow (z.1,u)))
|
| 126 |
+
(pair identity (composition (f := fun z : Data=>z.1) (g := matrixIndices)
|
| 127 |
+
first matrixIndicesRealizer)) (mapWith arcRowRealizer)
|
| 128 |
+
|
| 129 |
+
lemma polynomial_graph : PolynomialTime graphRealizer :=
|
| 130 |
+
polynomial_composition (polynomial_pair polynomial_identity
|
| 131 |
+
(polynomial_composition polynomial_first polynomial_matrixIndices))
|
| 132 |
+
(polynomial_mapWith polynomial_arcRow)
|
| 133 |
+
|
| 134 |
+
def natCode {m n : ℕ} (c : Fin m→Fin n→ℕ) : NatMatrix :=
|
| 135 |
+
List.ofFn (fun u=>List.ofFn (c u))
|
| 136 |
+
|
| 137 |
+
def flowCode {m n : ℕ} (f : Fin m→Fin n→ℤ) : FlowMatrix :=
|
| 138 |
+
List.ofFn (fun u=>List.ofFn (fun v=>ForestAffineProgram.signedParts (f u v)))
|
| 139 |
+
|
| 140 |
+
lemma capacityEntry_code {m n : ℕ} (c : Fin m→Fin n→ℕ) (u : Fin m) (v : Fin n) :
|
| 141 |
+
capacityEntry (natCode c,(u.val,v.val))=c u v := by
|
| 142 |
+
simp only [capacityEntry,natCode,List.getD_eq_getElem?_getD,List.getElem?_ofFn,
|
| 143 |
+
u.isLt,v.isLt,dite_true,Option.getD_some,Fin.eta]
|
| 144 |
+
|
| 145 |
+
lemma flowEntry_code {m n : ℕ} (f : Fin m→Fin n→ℤ) (u : Fin m) (v : Fin n) :
|
| 146 |
+
flowEntry (flowCode f,(u.val,v.val))=ForestAffineProgram.signedParts (f u v) := by
|
| 147 |
+
simp only [flowEntry,flowCode,List.getD_eq_getElem?_getD,List.getElem?_ofFn,
|
| 148 |
+
u.isLt,v.isLt,dite_true,Option.getD_some,Fin.eta]
|
| 149 |
+
|
| 150 |
+
lemma residualValue_correct (c : ℕ) (z : Signed) :
|
| 151 |
+
residualValue (c,z)=((c:ℤ)-SignedPairProgram.value z).toNat := by
|
| 152 |
+
unfold residualValue SignedPairProgram.value
|
| 153 |
+
dsimp only
|
| 154 |
+
omega
|
| 155 |
+
|
| 156 |
+
lemma positive_correct (c : ℕ) (z : Signed) :
|
| 157 |
+
positive (c,z)=decide (0<(c:ℤ)-SignedPairProgram.value z) := by
|
| 158 |
+
apply Bool.eq_iff_iff.mpr
|
| 159 |
+
change (decide (z.1 < c+z.2)=true) ↔
|
| 160 |
+
(decide (0<(c:ℤ)-SignedPairProgram.value z)=true)
|
| 161 |
+
rw [decide_eq_true_eq,decide_eq_true_eq]
|
| 162 |
+
change z.1 < c+z.2 ↔ 0<(c:ℤ)-((z.1:ℤ)-z.2)
|
| 163 |
+
omega
|
| 164 |
+
|
| 165 |
+
lemma arc_code {n : ℕ} (c : PublishedEdmondsKarp.Capacity n)
|
| 166 |
+
(f : PublishedEdmondsKarp.Flow n) (u v : Fin n) :
|
| 167 |
+
arc ((natCode c,flowCode f),(u.val,v.val))=ResidualSearch.edge c f u v := by
|
| 168 |
+
simp only [arc,arcData,capacityEntry_code,flowEntry_code,positive_correct,
|
| 169 |
+
SignedPairProgram.value,ForestAffineProgram.signedParts_sub,ResidualSearch.edge]
|
| 170 |
+
|
| 171 |
+
lemma graph_code {n : ℕ} (c : PublishedEdmondsKarp.Capacity n)
|
| 172 |
+
(f : PublishedEdmondsKarp.Flow n) :
|
| 173 |
+
graph (natCode c,flowCode f)=DirectedBFSCode.graphCode (ResidualSearch.edge c f) := by
|
| 174 |
+
simp only [graph,arcRow,natCode,List.length_ofFn,DirectedBFSCode.range_eq_map,
|
| 175 |
+
←List.ofFn_eq_map,List.map_ofFn,DirectedBFSCode.graphCode]
|
| 176 |
+
apply congrArg List.ofFn
|
| 177 |
+
funext u
|
| 178 |
+
apply congrArg List.ofFn
|
| 179 |
+
funext v
|
| 180 |
+
exact arc_code c f u v
|
| 181 |
+
|
| 182 |
+
def search (z : Data×(ℕ×ℕ)) : Option (List ℕ) :=
|
| 183 |
+
DirectedBFSCode.search (graph z.1) z.2.1 z.2.2
|
| 184 |
+
|
| 185 |
+
def searchRealizer : Realizer search :=
|
| 186 |
+
composition (pair (pair (composition first graphRealizer) (composition second first))
|
| 187 |
+
(composition second second)) DirectedBFSRuntime.searchRealizer
|
| 188 |
+
|
| 189 |
+
lemma polynomial_search : PolynomialTime searchRealizer :=
|
| 190 |
+
polynomial_composition (polynomial_pair (polynomial_pair
|
| 191 |
+
(polynomial_composition polynomial_first polynomial_graph)
|
| 192 |
+
(polynomial_composition polynomial_second polynomial_first))
|
| 193 |
+
(polynomial_composition polynomial_second polynomial_second))
|
| 194 |
+
DirectedBFSRuntime.polynomial_search
|
| 195 |
+
|
| 196 |
+
lemma search_code {n : ℕ} (c : PublishedEdmondsKarp.Capacity n)
|
| 197 |
+
(f : PublishedEdmondsKarp.Flow n) (s t : Fin n) :
|
| 198 |
+
search ((natCode c,flowCode f),(s.val,t.val))=
|
| 199 |
+
(ResidualSearch.search c f s t).map (List.map Fin.val) := by
|
| 200 |
+
rw [search,graph_code,DirectedBFSCode.search_code]
|
| 201 |
+
rfl
|
| 202 |
+
|
| 203 |
+
end ContingencyTables.ResidualGraphProgram
|
| 204 |
+
|
| 205 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/ResidualWeightProgram.lean
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Transport.IntegerResidualCorrection
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Machines.VectorRetryProgram
|
| 3 |
+
|
| 4 |
+
namespace OAI
|
| 5 |
+
|
| 6 |
+
/-! # Binary computation of the rare branch's integer weights -/
|
| 7 |
+
|
| 8 |
+
namespace ContingencyTables.ResidualWeightPrograms
|
| 9 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped
|
| 10 |
+
open scoped BigOperators Classical
|
| 11 |
+
|
| 12 |
+
abbrev Input := (ℕ×ℕ)×(ℕ×ℕ)
|
| 13 |
+
|
| 14 |
+
/-- Common denominator, inverse branch probability, number of tables,
|
| 15 |
+
and the ordinary draw's numerator. -/
|
| 16 |
+
def count (z : Input) : ℕ := z.1.1*z.1.2-z.2.1*(z.1.2-1)*z.2.2
|
| 17 |
+
|
| 18 |
+
def countRealizer : Realizer count := by
|
| 19 |
+
let denominator : Realizer (fun z : Input => z.1.1) := composition first first
|
| 20 |
+
let branch : Realizer (fun z : Input => z.1.2) := composition first second
|
| 21 |
+
let cardinality : Realizer (fun z : Input => z.2.1) := composition second first
|
| 22 |
+
let numerator : Realizer (fun z : Input => z.2.2) := composition second second
|
| 23 |
+
let left := composition (pair denominator branch) multiply
|
| 24 |
+
let right := composition (pair (composition (pair cardinality
|
| 25 |
+
(composition branch predecessor)) multiply) numerator) multiply
|
| 26 |
+
exact composition (pair left right) subtract
|
| 27 |
+
|
| 28 |
+
lemma polynomial_count : PolynomialTime countRealizer := by
|
| 29 |
+
unfold countRealizer
|
| 30 |
+
exact polynomial_composition (polynomial_pair
|
| 31 |
+
(polynomial_composition (polynomial_pair
|
| 32 |
+
(polynomial_composition polynomial_first polynomial_first)
|
| 33 |
+
(polynomial_composition polynomial_first polynomial_second)) polynomial_multiply)
|
| 34 |
+
(polynomial_composition (polynomial_pair
|
| 35 |
+
(polynomial_composition (polynomial_pair
|
| 36 |
+
(polynomial_composition polynomial_second polynomial_first)
|
| 37 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_second)
|
| 38 |
+
polynomial_predecessor)) polynomial_multiply)
|
| 39 |
+
(polynomial_composition polynomial_second polynomial_second)) polynomial_multiply))
|
| 40 |
+
polynomial_subtract
|
| 41 |
+
|
| 42 |
+
lemma count_actual {A : Type*} [Fintype A] {Q : ℕ} (p : IntegerLaw A Q)
|
| 43 |
+
(T : ℕ) (hT : 0<T) (a : A) :
|
| 44 |
+
count ((Q,T),(Fintype.card A,p.count a))=IntegerResidualCorrection.count p T a := by
|
| 45 |
+
have ht : (T:ℤ)-1=((T-1:ℕ):ℤ) := by omega
|
| 46 |
+
have he : ResidualMixture.residualNumerator Q T (fun x => (p.count x:ℤ)) a=
|
| 47 |
+
((Q*T:ℕ):ℤ)-((Fintype.card A*(T-1)*p.count a:ℕ):ℤ) := by
|
| 48 |
+
unfold ResidualMixture.residualNumerator
|
| 49 |
+
rw [ht]
|
| 50 |
+
push_cast
|
| 51 |
+
rfl
|
| 52 |
+
rw [IntegerResidualCorrection.count,he,Int.toNat_sub]
|
| 53 |
+
rfl
|
| 54 |
+
|
| 55 |
+
end ContingencyTables.ResidualWeightPrograms
|
| 56 |
+
|
| 57 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/RetryNumeratorProgram.lean
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.BinaryPowerProgram
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Transport.IntegerLaw
|
| 3 |
+
|
| 4 |
+
namespace OAI
|
| 5 |
+
|
| 6 |
+
/-! # Binary first-success numerators via a two-coordinate recurrence -/
|
| 7 |
+
|
| 8 |
+
namespace ContingencyTables.RetryNumeratorPrograms
|
| 9 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped
|
| 10 |
+
|
| 11 |
+
/-- Unary trial count; denominator, failure count, success count, fallback match. -/
|
| 12 |
+
abbrev Input := List Bool×((ℕ×ℕ)×(ℕ×Bool))
|
| 13 |
+
|
| 14 |
+
def twoEntries {A : Type} [Codec A] : Realizer (fun x : A×A => [x.1,x.2]) :=
|
| 15 |
+
composition (pair first (composition second BinaryPowerPrograms.singleton)) listCons
|
| 16 |
+
|
| 17 |
+
lemma polynomial_twoEntries {A : Type} [Codec A] : PolynomialTime (twoEntries (A := A)) :=
|
| 18 |
+
polynomial_composition (polynomial_pair polynomial_first
|
| 19 |
+
(polynomial_composition polynomial_second BinaryPowerPrograms.polynomial_singleton))
|
| 20 |
+
polynomial_listCons
|
| 21 |
+
|
| 22 |
+
def setup (z : Input) : ℕ×(List (List ℕ)×List ℕ) :=
|
| 23 |
+
(z.1.length,([[z.2.1.2,z.2.2.1],[0,z.2.1.1]],[(if z.2.2.2 then 1 else 0),1]))
|
| 24 |
+
|
| 25 |
+
def setupRealizer : Realizer setup := by
|
| 26 |
+
let Q : Realizer (fun z : Input => z.2.1.1) := composition (composition second first) first
|
| 27 |
+
let phi : Realizer (fun z : Input => z.2.1.2) := composition (composition second first) second
|
| 28 |
+
let alpha : Realizer (fun z : Input => z.2.2.1) := composition (composition second second) first
|
| 29 |
+
let hit : Realizer (fun z : Input => z.2.2.2) := composition (composition second second) second
|
| 30 |
+
let column1 := composition (pair phi alpha) twoEntries
|
| 31 |
+
let column2 := composition (pair (constant 0) Q) twoEntries
|
| 32 |
+
let matrix := composition (pair column1 column2) twoEntries
|
| 33 |
+
let initial := choose hit (constant 1) (constant 0)
|
| 34 |
+
let vector := composition (pair initial (constant 1)) twoEntries
|
| 35 |
+
exact pair (composition first listLength) (pair matrix vector)
|
| 36 |
+
|
| 37 |
+
lemma polynomial_setup : PolynomialTime setupRealizer := by
|
| 38 |
+
have hQ : PolynomialTime (composition (composition
|
| 39 |
+
(second (A := List Bool) (B := (ℕ×ℕ)×(ℕ×Bool))) first) first) := by tree_poly
|
| 40 |
+
have hphi : PolynomialTime (composition (composition
|
| 41 |
+
(second (A := List Bool) (B := (ℕ×ℕ)×(ℕ×Bool))) first) second) := by tree_poly
|
| 42 |
+
have halpha : PolynomialTime (composition (composition
|
| 43 |
+
(second (A := List Bool) (B := (ℕ×ℕ)×(ℕ×Bool))) second) first) := by tree_poly
|
| 44 |
+
have hhit : PolynomialTime (composition (composition
|
| 45 |
+
(second (A := List Bool) (B := (ℕ×ℕ)×(ℕ×Bool))) second) second) := by tree_poly
|
| 46 |
+
unfold setupRealizer
|
| 47 |
+
exact polynomial_pair (polynomial_composition polynomial_first polynomial_listLength)
|
| 48 |
+
(polynomial_pair (polynomial_composition (polynomial_pair
|
| 49 |
+
(polynomial_composition (polynomial_pair hphi halpha) polynomial_twoEntries)
|
| 50 |
+
(polynomial_composition (polynomial_pair (polynomial_constant 0) hQ) polynomial_twoEntries))
|
| 51 |
+
polynomial_twoEntries)
|
| 52 |
+
(polynomial_composition (polynomial_pair
|
| 53 |
+
(polynomial_choose hhit (polynomial_constant 1) (polynomial_constant 0))
|
| 54 |
+
(polynomial_constant 1)) polynomial_twoEntries))
|
| 55 |
+
|
| 56 |
+
def count (z : Input) : ℕ := (IntegerMatrixPrograms.iterate (setup z)).headD 0
|
| 57 |
+
|
| 58 |
+
def countRealizer : Realizer count :=
|
| 59 |
+
composition (composition setupRealizer IntegerMatrixPrograms.iterateRealizer) (head 0)
|
| 60 |
+
|
| 61 |
+
/-- All stored numbers are binary; only the supplied trial marker controls
|
| 62 |
+
iteration, so evaluation is polynomial in that marker and binary data. -/
|
| 63 |
+
theorem polynomial_count : PolynomialTime countRealizer := by
|
| 64 |
+
have hi : PolynomialTime (composition setupRealizer IntegerMatrixPrograms.iterateRealizer) := by
|
| 65 |
+
obtain ⟨p,hp⟩ := polynomial_setup
|
| 66 |
+
apply work_composition ⟨p,hp⟩ IntegerMatrixPrograms.work_iterate (Polynomial.X+p)
|
| 67 |
+
intro z
|
| 68 |
+
have hl := (list_length_le_weight z.1).trans (weight_fst z)
|
| 69 |
+
have hw := weight_snd (setup z)
|
| 70 |
+
have hc := hp z
|
| 71 |
+
simp only [Polynomial.eval_add,Polynomial.eval_X,setup] at *
|
| 72 |
+
omega
|
| 73 |
+
exact polynomial_composition hi (polynomial_head 0)
|
| 74 |
+
|
| 75 |
+
def recurrence (Q phi alpha initial : ℕ) : ℕ → ℕ
|
| 76 |
+
| 0 => initial
|
| 77 |
+
| n+1 => phi*recurrence Q phi alpha initial n+alpha*Q^n
|
| 78 |
+
|
| 79 |
+
private lemma iterate_eq (B : List (List ℕ)) (v : List ℕ) (N : ℕ) :
|
| 80 |
+
IntegerMatrixPrograms.iterate (N,(B,v))=
|
| 81 |
+
((fun v => IntegerMatrixPrograms.propagate (B,v))^[N]) v := by
|
| 82 |
+
induction N generalizing v with
|
| 83 |
+
| zero => rfl
|
| 84 |
+
| succ N ih =>
|
| 85 |
+
rw [IntegerMatrixPrograms.iterate_succ,ih,Function.iterate_succ_apply]
|
| 86 |
+
|
| 87 |
+
private lemma propagate_two (Q phi alpha x y : ℕ) :
|
| 88 |
+
IntegerMatrixPrograms.propagate ([[phi,alpha],[0,Q]],[x,y])=
|
| 89 |
+
[phi*x+alpha*y,Q*y] := by
|
| 90 |
+
simp [IntegerMatrixPrograms.propagate,IntegerMatrixPrograms.dot_cons,
|
| 91 |
+
IntegerMatrixPrograms.dot_nil,Nat.mul_comm]
|
| 92 |
+
|
| 93 |
+
lemma count_eq (xs : List Bool) (Q phi alpha : ℕ) (hit : Bool) :
|
| 94 |
+
count (xs,((Q,phi),(alpha,hit)))=
|
| 95 |
+
recurrence Q phi alpha (if hit then 1 else 0) xs.length := by
|
| 96 |
+
have hiter (N initial : ℕ) :
|
| 97 |
+
((fun v => IntegerMatrixPrograms.propagate ([[phi,alpha],[0,Q]],v))^[N]) [initial,1]=
|
| 98 |
+
[recurrence Q phi alpha initial N,Q^N] := by
|
| 99 |
+
induction N with
|
| 100 |
+
| zero => simp [recurrence]
|
| 101 |
+
| succ N ih =>
|
| 102 |
+
rw [Function.iterate_succ_apply',ih,propagate_two]
|
| 103 |
+
simp only [recurrence,pow_succ]
|
| 104 |
+
rw [Nat.mul_comm Q (Q^N)]
|
| 105 |
+
unfold count setup
|
| 106 |
+
rw [iterate_eq,hiter]
|
| 107 |
+
rfl
|
| 108 |
+
|
| 109 |
+
lemma recurrence_retry {A : Type*} [Fintype A] [DecidableEq A] {Q : ℕ}
|
| 110 |
+
(p : IntegerLaw (Option A) Q) (fallback x : A) (N : ℕ) :
|
| 111 |
+
recurrence Q (p.count none) (p.count (some x)) (if x=fallback then 1 else 0) N=
|
| 112 |
+
IntegerLaw.retryCount p fallback N x := by
|
| 113 |
+
induction N with
|
| 114 |
+
| zero => rfl
|
| 115 |
+
| succ N ih =>
|
| 116 |
+
rw [recurrence,ih,IntegerLaw.retryCount]
|
| 117 |
+
exact Nat.add_comm _ _
|
| 118 |
+
|
| 119 |
+
end ContingencyTables.RetryNumeratorPrograms
|
| 120 |
+
|
| 121 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/RoundedPowerProgram.lean
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.SignedFloorProgram
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Machines.DyadicRootProgram
|
| 3 |
+
import OAI.Combinatorics.ContingencyTables.Transport.PowerOfTwoApproximation
|
| 4 |
+
|
| 5 |
+
namespace OAI
|
| 6 |
+
|
| 7 |
+
/-! # Executing the rounded power used by the correction oracle
|
| 8 |
+
|
| 9 |
+
The exponent lies in [-2,2]. Clipping the computed signed floor
|
| 10 |
+
at twice the supplied degree allowance plus one is therefore inactive
|
| 11 |
+
on valid inputs, and keeps every binary execution polynomial even on
|
| 12 |
+
arbitrary malformed numerator/denominator data.
|
| 13 |
+
-/
|
| 14 |
+
|
| 15 |
+
namespace ContingencyTables.RoundedPowerProgram
|
| 16 |
+
|
| 17 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped Polynomial SignedPairProgram
|
| 18 |
+
|
| 19 |
+
abbrev Input := SignedFloorProgram.ScaledInput
|
| 20 |
+
|
| 21 |
+
def normalized (z : Input) : Signed := SignedPairProgram.normalize (SignedFloorProgram.scaledFloor z)
|
| 22 |
+
def normalizedRealizer : Realizer normalized :=
|
| 23 |
+
composition SignedFloorProgram.scaledFloorRealizer SignedPairProgram.normalizeRealizer
|
| 24 |
+
lemma polynomial_normalized : PolynomialTime normalizedRealizer :=
|
| 25 |
+
polynomial_composition SignedFloorProgram.polynomial_scaledFloor SignedPairProgram.polynomial_normalize
|
| 26 |
+
|
| 27 |
+
def cap (z : Input) : ℕ := 2*z.2.length+1
|
| 28 |
+
def capRealizer : Realizer cap := composition
|
| 29 |
+
(f:=fun z : Input=>2*z.2.length) (g:=Nat.succ)
|
| 30 |
+
(composition (f:=fun z : Input=>(2,z.2.length)) (g:=fun p : ℕ×ℕ=>p.1*p.2)
|
| 31 |
+
(pair (constant 2) (composition second listLength)) multiply) successor
|
| 32 |
+
lemma polynomial_cap : PolynomialTime capRealizer :=
|
| 33 |
+
polynomial_composition (polynomial_composition
|
| 34 |
+
(polynomial_pair (polynomial_constant 2)
|
| 35 |
+
(polynomial_composition polynomial_second polynomial_listLength)) polynomial_multiply)
|
| 36 |
+
polynomial_successor
|
| 37 |
+
|
| 38 |
+
def clipped (z : Input) : Signed :=
|
| 39 |
+
(min (normalized z).1 (cap z),min (normalized z).2 (cap z))
|
| 40 |
+
def clippedRealizer : Realizer clipped := pair
|
| 41 |
+
(composition (pair (composition normalizedRealizer first) capRealizer) minimum)
|
| 42 |
+
(composition (pair (composition normalizedRealizer second) capRealizer) minimum)
|
| 43 |
+
lemma polynomial_clipped : PolynomialTime clippedRealizer := polynomial_pair
|
| 44 |
+
(polynomial_composition (polynomial_pair
|
| 45 |
+
(polynomial_composition polynomial_normalized polynomial_first) polynomial_cap) polynomial_minimum)
|
| 46 |
+
(polynomial_composition (polynomial_pair
|
| 47 |
+
(polynomial_composition polynomial_normalized polynomial_second) polynomial_cap) polynomial_minimum)
|
| 48 |
+
|
| 49 |
+
def positiveInput (z : Input) : ℕ×ℕ := (2,(clipped z).1)
|
| 50 |
+
def negativeInput (z : Input) : ℕ×ℕ := (2,(clipped z).2)
|
| 51 |
+
def positiveInputRealizer : Realizer positiveInput := pair (constant 2) (composition clippedRealizer first)
|
| 52 |
+
def negativeInputRealizer : Realizer negativeInput := pair (constant 2) (composition clippedRealizer second)
|
| 53 |
+
lemma polynomial_positiveInput : PolynomialTime positiveInputRealizer :=
|
| 54 |
+
polynomial_pair (polynomial_constant 2) (polynomial_composition polynomial_clipped polynomial_first)
|
| 55 |
+
lemma polynomial_negativeInput : PolynomialTime negativeInputRealizer :=
|
| 56 |
+
polynomial_pair (polynomial_constant 2) (polynomial_composition polynomial_clipped polynomial_second)
|
| 57 |
+
|
| 58 |
+
def positivePower (z : Input) : ℕ := BinaryPowerPrograms.power (positiveInput z)
|
| 59 |
+
def negativePower (z : Input) : ℕ := BinaryPowerPrograms.power (negativeInput z)
|
| 60 |
+
def positivePowerRealizer : Realizer positivePower :=
|
| 61 |
+
composition positiveInputRealizer BinaryPowerPrograms.powerRealizer
|
| 62 |
+
def negativePowerRealizer : Realizer negativePower :=
|
| 63 |
+
composition negativeInputRealizer BinaryPowerPrograms.powerRealizer
|
| 64 |
+
|
| 65 |
+
lemma polynomial_positivePower : PolynomialTime positivePowerRealizer := by
|
| 66 |
+
apply work_composition polynomial_positiveInput BinaryPowerPrograms.work_power (2*X+10)
|
| 67 |
+
intro z
|
| 68 |
+
have hl: z.2.length ≤ weight z:=(list_length_le_weight z.2).trans (weight_snd z)
|
| 69 |
+
have hm: (clipped z).1 ≤ cap z:=min_le_right _ _
|
| 70 |
+
have ht: weight (2:ℕ) ≤ 9:=by decide
|
| 71 |
+
simp only [positiveInput,cap,eval_add,eval_mul,eval_ofNat,eval_X] at *
|
| 72 |
+
omega
|
| 73 |
+
|
| 74 |
+
lemma polynomial_negativePower : PolynomialTime negativePowerRealizer := by
|
| 75 |
+
apply work_composition polynomial_negativeInput BinaryPowerPrograms.work_power (2*X+10)
|
| 76 |
+
intro z
|
| 77 |
+
have hl: z.2.length ≤ weight z:=(list_length_le_weight z.2).trans (weight_snd z)
|
| 78 |
+
have hm: (clipped z).2 ≤ cap z:=min_le_right _ _
|
| 79 |
+
have ht: weight (2:ℕ) ≤ 9:=by decide
|
| 80 |
+
simp only [negativeInput,cap,eval_add,eval_mul,eval_ofNat,eval_X] at *
|
| 81 |
+
omega
|
| 82 |
+
|
| 83 |
+
def output (z : Input) : TreeFraction.Fraction :=
|
| 84 |
+
TreeFraction.normalize (positivePower z,negativePower z)
|
| 85 |
+
def outputRealizer : Realizer output :=
|
| 86 |
+
composition (pair positivePowerRealizer negativePowerRealizer) TreeFraction.normalizeRealizer
|
| 87 |
+
theorem polynomial_output : PolynomialTime outputRealizer :=
|
| 88 |
+
polynomial_composition (polynomial_pair polynomial_positivePower polynomial_negativePower)
|
| 89 |
+
TreeFraction.polynomial_normalize
|
| 90 |
+
|
| 91 |
+
lemma floor_bounds (beta : ℚ) (hlo : -2 ≤ beta) (hhi : beta ≤ 2) (s : ℕ) :
|
| 92 |
+
-2*(s:ℤ) ≤ ⌊(s:ℚ)*beta⌋ ∧ ⌊(s:ℚ)*beta⌋ ≤ 2*(s:ℤ) := by
|
| 93 |
+
have hL:=mul_le_mul_of_nonneg_left hlo (Nat.cast_nonneg s : (0:ℚ) ≤ s)
|
| 94 |
+
have hU:=mul_le_mul_of_nonneg_left hhi (Nat.cast_nonneg s : (0:ℚ) ≤ s)
|
| 95 |
+
constructor
|
| 96 |
+
· apply Int.le_floor.mpr
|
| 97 |
+
push_cast
|
| 98 |
+
linarith
|
| 99 |
+
· have hf: (⌊(s:ℚ)*beta⌋:ℚ) ≤ 2*s:=(Int.floor_le _).trans (by linarith)
|
| 100 |
+
exact_mod_cast hf
|
| 101 |
+
|
| 102 |
+
lemma clipped_eq (p n d : ℕ) (xs : List Bool)
|
| 103 |
+
(hlo: -2 ≤ ((p:ℚ)-n)/d) (hhi: ((p:ℚ)-n)/d ≤ 2) :
|
| 104 |
+
clipped (((p,n),d),xs)=
|
| 105 |
+
((⌊(xs.length:ℚ)*(((p:ℚ)-n)/d)⌋:ℤ).toNat,
|
| 106 |
+
(-⌊(xs.length:ℚ)*(((p:ℚ)-n)/d)⌋:ℤ).toNat) := by
|
| 107 |
+
have hb:=floor_bounds _ hlo hhi xs.length
|
| 108 |
+
have hp: (⌊(xs.length:ℚ)*(((p:ℚ)-n)/d)⌋:ℤ).toNat ≤ 2*xs.length+1:=by omega
|
| 109 |
+
have hn: (-⌊(xs.length:ℚ)*(((p:ℚ)-n)/d)⌋:ℤ).toNat ≤ 2*xs.length+1:=by omega
|
| 110 |
+
simp only [clipped,normalized,SignedPairProgram.normalize_eq,SignedFloorProgram.scaledFloor_value,
|
| 111 |
+
ForestAffineProgram.signedParts,cap,min_eq_left hp,min_eq_left hn]
|
| 112 |
+
|
| 113 |
+
/-- The returned natural fraction is exactly the rational power used
|
| 114 |
+
by the approximation, including negative rounded exponents. -/
|
| 115 |
+
theorem output_value (p n d : ℕ) (xs : List Bool)
|
| 116 |
+
(hlo: -2 ≤ ((p:ℚ)-n)/d) (hhi: ((p:ℚ)-n)/d ≤ 2) :
|
| 117 |
+
TreeFraction.val (output (((p,n),d),xs))=
|
| 118 |
+
(2:ℚ)^PowerOfTwoApproximation.numerator (((p:ℚ)-n)/d) xs.length := by
|
| 119 |
+
rw [output,TreeFraction.normalize_val]
|
| 120 |
+
simp only [positivePower,negativePower,positiveInput,negativeInput,
|
| 121 |
+
BinaryPowerPrograms.power_eq,clipped_eq p n d xs hlo hhi,TreeFraction.val,Nat.cast_pow,Nat.cast_ofNat]
|
| 122 |
+
let q:ℤ:=⌊(xs.length:ℚ)*(((p:ℚ)-n)/d)⌋
|
| 123 |
+
change (2:ℚ)^q.toNat/(2:ℚ)^(-q).toNat=(2:ℚ)^q
|
| 124 |
+
have he: (q.toNat:ℤ)-(-q).toNat=q:=by omega
|
| 125 |
+
conv_rhs => rw [←he]
|
| 126 |
+
rw [zpow_sub₀ (by norm_num : (2:ℚ)≠0),zpow_natCast,zpow_natCast]
|
| 127 |
+
|
| 128 |
+
theorem output_denominator_pos (z : Input) : 0<(output z).2 :=
|
| 129 |
+
TreeFraction.normalize_den_pos _
|
| 130 |
+
|
| 131 |
+
end ContingencyTables.RoundedPowerProgram
|
| 132 |
+
|
| 133 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/ScalarScheduleBounds.lean
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.ScalarScheduleProgram
|
| 2 |
+
|
| 3 |
+
namespace OAI
|
| 4 |
+
|
| 5 |
+
/-! # Numeric bounds for the fixed arithmetic circuits -/
|
| 6 |
+
|
| 7 |
+
namespace ContingencyTables.ScalarScheduleProgram
|
| 8 |
+
open Polynomial
|
| 9 |
+
noncomputable section
|
| 10 |
+
|
| 11 |
+
def Expr.bound : Expr → Polynomial ℕ
|
| 12 |
+
| .input _ => X
|
| 13 |
+
| .number n => C n
|
| 14 |
+
| .plus a b => a.bound+b.bound
|
| 15 |
+
| .times a b => a.bound*b.bound
|
| 16 |
+
|
| 17 |
+
lemma Expr.eval_le (e : Expr) (xs : List ℕ) (M : ℕ)
|
| 18 |
+
(h : ∀ i,xs.getD i 0 ≤ M) : e.eval xs ≤ e.bound.eval M := by
|
| 19 |
+
induction e with
|
| 20 |
+
| input i => simpa only [Expr.eval,Expr.bound,Polynomial.eval_X] using h i
|
| 21 |
+
| number n => simp only [Expr.eval,Expr.bound,eval_C,le_refl]
|
| 22 |
+
| plus a b ha hb =>
|
| 23 |
+
simp only [Expr.eval,Expr.bound,Polynomial.eval_add]
|
| 24 |
+
exact Nat.add_le_add ha hb
|
| 25 |
+
| times a b ha hb =>
|
| 26 |
+
simp only [Expr.eval,Expr.bound,Polynomial.eval_mul]
|
| 27 |
+
exact Nat.mul_le_mul ha hb
|
| 28 |
+
|
| 29 |
+
lemma getD_le_of_mem (xs : List ℕ) (M : ℕ) (h : ∀ x∈xs,x ≤ M) :
|
| 30 |
+
∀ i,xs.getD i 0 ≤ M := by
|
| 31 |
+
induction xs with
|
| 32 |
+
| nil => intro i; simp
|
| 33 |
+
| cons x xs ih =>
|
| 34 |
+
intro i
|
| 35 |
+
cases i with
|
| 36 |
+
| zero => simpa using h x (by simp)
|
| 37 |
+
| succ i =>
|
| 38 |
+
simpa using ih (fun y hy => h y (by simp only [List.mem_cons]; exact Or.inr hy)) i
|
| 39 |
+
|
| 40 |
+
lemma Expr.eval_le_of_mem (e : Expr) (xs : List ℕ) (M : ℕ) (h : ∀ x∈xs,x ≤ M) :
|
| 41 |
+
e.eval xs ≤ e.bound.eval M := e.eval_le xs M (getD_le_of_mem xs M h)
|
| 42 |
+
|
| 43 |
+
end
|
| 44 |
+
end ContingencyTables.ScalarScheduleProgram
|
| 45 |
+
|
| 46 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/ScalarScheduleProgram.lean
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.MatchingCount.Machines.TreeFastLookup
|
| 2 |
+
import OAI.Combinatorics.MatchingCount.Complexity.TreePolynomialDivision
|
| 3 |
+
|
| 4 |
+
namespace OAI
|
| 5 |
+
|
| 6 |
+
/-! # Fixed arithmetic circuits for the scalar counting schedules
|
| 7 |
+
|
| 8 |
+
These circuits only read a fixed finite list and perform binary addition
|
| 9 |
+
and multiplication. Their syntax describes the fixed program, not a
|
| 10 |
+
numeric loop selected by the input.
|
| 11 |
+
-/
|
| 12 |
+
|
| 13 |
+
namespace ContingencyTables.ScalarScheduleProgram
|
| 14 |
+
open MatchingFPRAS.TreeTyped
|
| 15 |
+
|
| 16 |
+
inductive Expr where
|
| 17 |
+
| input : ℕ → Expr
|
| 18 |
+
| number : ℕ → Expr
|
| 19 |
+
| plus : Expr → Expr → Expr
|
| 20 |
+
| times : Expr → Expr → Expr
|
| 21 |
+
|
| 22 |
+
def Expr.eval : Expr → List ℕ → ℕ
|
| 23 |
+
| .input i, xs => xs.getD i 0
|
| 24 |
+
| .number n, _ => n
|
| 25 |
+
| .plus a b, xs => a.eval xs+b.eval xs
|
| 26 |
+
| .times a b, xs => a.eval xs*b.eval xs
|
| 27 |
+
|
| 28 |
+
def Expr.realizer : (e : Expr) → Realizer e.eval
|
| 29 |
+
| .input i => composition (pair identity (constant i)) (getDFast 0)
|
| 30 |
+
| .number n => constant n
|
| 31 |
+
| .plus a b => composition (pair a.realizer b.realizer) add
|
| 32 |
+
| .times a b => composition (pair a.realizer b.realizer) multiply
|
| 33 |
+
|
| 34 |
+
lemma Expr.polynomial (e : Expr) : PolynomialTime e.realizer := by
|
| 35 |
+
induction e with
|
| 36 |
+
| input i => exact polynomial_composition (polynomial_pair polynomial_identity
|
| 37 |
+
(polynomial_constant i)) (polynomial_getDFast 0)
|
| 38 |
+
| number n => exact polynomial_constant n
|
| 39 |
+
| plus a b ha hb => exact polynomial_composition (polynomial_pair ha hb) polynomial_add
|
| 40 |
+
| times a b ha hb => exact polynomial_composition (polynomial_pair ha hb) polynomial_multiply
|
| 41 |
+
|
| 42 |
+
def Expr.pow (a : Expr) : ℕ → Expr
|
| 43 |
+
| 0 => .number 1
|
| 44 |
+
| n+1 => .times (a.pow n) a
|
| 45 |
+
|
| 46 |
+
lemma Expr.eval_pow (a : Expr) (n : ℕ) (xs : List ℕ) :
|
| 47 |
+
(a.pow n).eval xs=(a.eval xs)^n := by
|
| 48 |
+
induction n with
|
| 49 |
+
| zero => rfl
|
| 50 |
+
| succ n ih => simp only [Expr.pow,Expr.eval,ih,pow_succ]
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
instance (n : ℕ) : OfNat Expr n := ⟨.number n⟩
|
| 54 |
+
instance : Add Expr := ⟨.plus⟩
|
| 55 |
+
instance : Mul Expr := ⟨.times⟩
|
| 56 |
+
instance : Pow Expr ℕ := ⟨Expr.pow⟩
|
| 57 |
+
|
| 58 |
+
@[simp] lemma Expr.eval_power (a : Expr) (n : ℕ) (xs : List ℕ) :
|
| 59 |
+
(a^n).eval xs=(a.eval xs)^n := Expr.eval_pow a n xs
|
| 60 |
+
|
| 61 |
+
@[simp] lemma Expr.eval_add (a b : Expr) (xs : List ℕ) :
|
| 62 |
+
(a+b).eval xs=a.eval xs+b.eval xs := rfl
|
| 63 |
+
@[simp] lemma Expr.eval_mul (a b : Expr) (xs : List ℕ) :
|
| 64 |
+
(a*b).eval xs=a.eval xs*b.eval xs := rfl
|
| 65 |
+
@[simp] lemma Expr.eval_ofNat (n : ℕ) (xs : List ℕ) :
|
| 66 |
+
(OfNat.ofNat n : Expr).eval xs=n := rfl
|
| 67 |
+
|
| 68 |
+
def vector (es : List Expr) (xs : List ℕ) : List ℕ := es.map (fun e => e.eval xs)
|
| 69 |
+
|
| 70 |
+
def vectorRealizer : (es : List Expr) → Realizer (vector es)
|
| 71 |
+
| [] => constant []
|
| 72 |
+
| e::es => composition (pair e.realizer (vectorRealizer es)) listCons
|
| 73 |
+
|
| 74 |
+
lemma polynomial_vector (es : List Expr) : PolynomialTime (vectorRealizer es) := by
|
| 75 |
+
induction es with
|
| 76 |
+
| nil => exact polynomial_constant []
|
| 77 |
+
| cons e es ih => exact polynomial_composition (polynomial_pair e.polynomial ih) polynomial_listCons
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
end ContingencyTables.ScalarScheduleProgram
|
| 81 |
+
|
| 82 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/ScaledWordProgram.lean
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.ListWordProgram
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Transport.FiniteScaledDraw
|
| 3 |
+
|
| 4 |
+
namespace OAI
|
| 5 |
+
|
| 6 |
+
/-! # Polynomial binary execution of the bounded quotient draw -/
|
| 7 |
+
|
| 8 |
+
namespace ContingencyTables.ProfilePrograms
|
| 9 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped
|
| 10 |
+
|
| 11 |
+
def scaledWordValue (x : ℕ×List Bool) : ℕ :=
|
| 12 |
+
x.1*StackAddition.value x.2/2^x.2.length
|
| 13 |
+
|
| 14 |
+
def scaledWordRealizer : Realizer scaledWordValue :=
|
| 15 |
+
(composition (pair
|
| 16 |
+
(composition (pair first (composition second bitValue)) multiply)
|
| 17 |
+
(composition second bitDenominatorRealizer)) quotient).congr (by
|
| 18 |
+
intro x
|
| 19 |
+
simp only [Function.comp_apply,bitDenominator_eq,scaledWordValue])
|
| 20 |
+
|
| 21 |
+
theorem polynomial_scaledWord : PolynomialTime scaledWordRealizer := by
|
| 22 |
+
unfold scaledWordRealizer
|
| 23 |
+
exact polynomial_congr _ (polynomial_composition (polynomial_pair
|
| 24 |
+
(polynomial_composition (polynomial_pair polynomial_first
|
| 25 |
+
(polynomial_composition polynomial_second polynomial_bitValue)) polynomial_multiply)
|
| 26 |
+
(polynomial_composition polynomial_second polynomial_bitDenominator)) polynomial_quotient)
|
| 27 |
+
|
| 28 |
+
theorem scaledWordValue_ofFn (M k : ℕ) (hM : 0<M) (bits : Fin k → Bool) :
|
| 29 |
+
scaledWordValue (M,List.ofFn bits)=(FairBits.scaledBitInteger M k hM bits).val := by
|
| 30 |
+
simp only [scaledWordValue,List.length_ofFn,bitValue_eq_word]
|
| 31 |
+
rfl
|
| 32 |
+
|
| 33 |
+
/-- Every finite input yields the prescribed bounded integer. -/
|
| 34 |
+
theorem scaledWordValue_lt (M : ℕ) (hM : 0<M) (bits : List Bool) :
|
| 35 |
+
scaledWordValue (M,bits)<M := by
|
| 36 |
+
let w : Fin bits.length → Bool := fun i => bits[i]
|
| 37 |
+
have he : List.ofFn w=bits := List.ofFn_getElem
|
| 38 |
+
rw [←he,scaledWordValue_ofFn M bits.length hM w]
|
| 39 |
+
exact (FairBits.scaledBitInteger M bits.length hM w).isLt
|
| 40 |
+
|
| 41 |
+
end ContingencyTables.ProfilePrograms
|
| 42 |
+
|
| 43 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/ScheduledCountInput.lean
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Sampling.OriginalOuterVector
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Sampling.UnitOuterVector
|
| 3 |
+
import OAI.Combinatorics.ContingencyTables.Sampling.AllSamplingProgram
|
| 4 |
+
import OAI.Combinatorics.ContingencyTables.Sampling.ProfileScheduleSemantics
|
| 5 |
+
import OAI.Combinatorics.ContingencyTables.Transport.UnaryMarkers
|
| 6 |
+
|
| 7 |
+
namespace OAI
|
| 8 |
+
|
| 9 |
+
/-! # Supplying the finite count evaluator from the public sampling parameters -/
|
| 10 |
+
|
| 11 |
+
namespace ContingencyTables.ProfilePrograms
|
| 12 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped DensePrograms Polynomial
|
| 13 |
+
|
| 14 |
+
attribute [local irreducible] initialCheck marginPadding parameterDimension parameterAccuracy
|
| 15 |
+
parameterCompletionWidth parameterSteps parameterProposalWidth hasLargeRectangle parameterTrials
|
| 16 |
+
profileBoxIndex outerVector UnitCountPrograms.outerVector
|
| 17 |
+
|
| 18 |
+
/-- The same cache parameters used by the executed approximate sampler. -/
|
| 19 |
+
def scheduledCacheParameters (z : SamplingParameters) : TransitionCacheParameters :=
|
| 20 |
+
(((initialCheck z.1,marginPadding z.1),(parameterDimension z,parameterAccuracy z)),
|
| 21 |
+
parameterCompletionWidth z)
|
| 22 |
+
|
| 23 |
+
def scheduledCacheParametersRealizer : Realizer scheduledCacheParameters :=
|
| 24 |
+
pair (pair (pair (composition (f := fun z : SamplingParameters => z.1) (g := initialCheck) first initialCheckRealizer)
|
| 25 |
+
(composition (f := fun z : SamplingParameters => z.1) (g := marginPadding) first marginPaddingRealizer))
|
| 26 |
+
(pair parameterDimensionRealizer parameterAccuracyRealizer)) parameterCompletionWidthRealizer
|
| 27 |
+
|
| 28 |
+
lemma polynomial_scheduledCacheParameters : PolynomialTime scheduledCacheParametersRealizer :=
|
| 29 |
+
polynomial_pair (polynomial_pair (polynomial_pair
|
| 30 |
+
(polynomial_composition polynomial_first polynomial_initialCheck)
|
| 31 |
+
(polynomial_composition polynomial_first polynomial_marginPadding))
|
| 32 |
+
(polynomial_pair polynomial_parameterDimension polynomial_parameterAccuracy))
|
| 33 |
+
polynomial_parameterCompletionWidth
|
| 34 |
+
|
| 35 |
+
def scheduledDenseWidth (z : SamplingParameters) : ℕ :=
|
| 36 |
+
parameterSteps z*(parameterProposalWidth z+parameterCompletionWidth z)+parameterCompletionWidth z
|
| 37 |
+
|
| 38 |
+
def scheduledDenseWidthRealizer : Realizer scheduledDenseWidth :=
|
| 39 |
+
composition (f := fun z : SamplingParameters =>
|
| 40 |
+
(parameterSteps z*(parameterProposalWidth z+parameterCompletionWidth z),parameterCompletionWidth z))
|
| 41 |
+
(g := fun p : ℕ×ℕ => p.1+p.2)
|
| 42 |
+
(pair (composition (f := fun z : SamplingParameters =>
|
| 43 |
+
(parameterSteps z,parameterProposalWidth z+parameterCompletionWidth z))
|
| 44 |
+
(g := fun p : ℕ×ℕ => p.1*p.2)
|
| 45 |
+
(pair parameterStepsRealizer (composition
|
| 46 |
+
(f := fun z : SamplingParameters => (parameterProposalWidth z,parameterCompletionWidth z))
|
| 47 |
+
(g := fun p : ℕ×ℕ => p.1+p.2)
|
| 48 |
+
(pair parameterProposalWidthRealizer parameterCompletionWidthRealizer) add)) multiply)
|
| 49 |
+
parameterCompletionWidthRealizer) add
|
| 50 |
+
|
| 51 |
+
lemma polynomial_scheduledDenseWidth : PolynomialTime scheduledDenseWidthRealizer :=
|
| 52 |
+
polynomial_composition (polynomial_pair (polynomial_composition
|
| 53 |
+
(polynomial_pair polynomial_parameterSteps (polynomial_composition
|
| 54 |
+
(polynomial_pair polynomial_parameterProposalWidth polynomial_parameterCompletionWidth)
|
| 55 |
+
polynomial_add)) polynomial_multiply) polynomial_parameterCompletionWidth) polynomial_add
|
| 56 |
+
|
| 57 |
+
def scheduledUnitWidth (z : SamplingParameters) : ℕ := parameterSteps z*parameterProposalWidth z
|
| 58 |
+
|
| 59 |
+
def scheduledUnitWidthRealizer : Realizer scheduledUnitWidth :=
|
| 60 |
+
composition (f := fun z : SamplingParameters => (parameterSteps z,parameterProposalWidth z))
|
| 61 |
+
(g := fun p : ℕ×ℕ => p.1*p.2) (pair parameterStepsRealizer parameterProposalWidthRealizer) multiply
|
| 62 |
+
|
| 63 |
+
lemma polynomial_scheduledUnitWidth : PolynomialTime scheduledUnitWidthRealizer :=
|
| 64 |
+
polynomial_composition (polynomial_pair polynomial_parameterSteps polynomial_parameterProposalWidth)
|
| 65 |
+
polynomial_multiply
|
| 66 |
+
|
| 67 |
+
attribute [local irreducible] scheduledDenseWidth scheduledUnitWidth
|
| 68 |
+
|
| 69 |
+
/-- The all-small branch uses only the proposal bits of each walk. -/
|
| 70 |
+
def scheduledTrialWidth (z : SamplingParameters) : ℕ :=
|
| 71 |
+
if hasLargeRectangle z then scheduledDenseWidth z else scheduledUnitWidth z
|
| 72 |
+
|
| 73 |
+
def scheduledTrialWidthRealizer : Realizer scheduledTrialWidth :=
|
| 74 |
+
choose hasLargeRectangleRealizer scheduledDenseWidthRealizer scheduledUnitWidthRealizer
|
| 75 |
+
|
| 76 |
+
lemma polynomial_scheduledTrialWidth : PolynomialTime scheduledTrialWidthRealizer :=
|
| 77 |
+
polynomial_choose polynomial_hasLargeRectangle polynomial_scheduledDenseWidth polynomial_scheduledUnitWidth
|
| 78 |
+
|
| 79 |
+
attribute [local irreducible] scheduledTrialWidth
|
| 80 |
+
|
| 81 |
+
/-- Numeric loop lengths are charged explicitly, in addition to binary input size. -/
|
| 82 |
+
def scheduledCountInputMeasure (z : SamplingParameters) : ℕ :=
|
| 83 |
+
parameterSteps z+parameterTrials z+parameterProposalWidth z+scheduledTrialWidth z+weight z
|
| 84 |
+
|
| 85 |
+
lemma weight_le_scheduledCountInputMeasure (z : SamplingParameters) :
|
| 86 |
+
weight z ≤ X.eval (scheduledCountInputMeasure z) := by
|
| 87 |
+
simp only [eval_X,scheduledCountInputMeasure]
|
| 88 |
+
omega
|
| 89 |
+
|
| 90 |
+
def scheduledMatrixParameters (z : SamplingParameters) : MatrixParameters :=
|
| 91 |
+
(scheduledCacheParameters z,unaryMarkers (parameterProposalWidth z))
|
| 92 |
+
|
| 93 |
+
def scheduledMatrixParametersRealizer : Realizer scheduledMatrixParameters :=
|
| 94 |
+
pair scheduledCacheParametersRealizer
|
| 95 |
+
(composition (f := parameterProposalWidth) (g := unaryMarkers)
|
| 96 |
+
parameterProposalWidthRealizer unaryMarkersRealizer)
|
| 97 |
+
|
| 98 |
+
attribute [local irreducible] scheduledMatrixParameters
|
| 99 |
+
|
| 100 |
+
lemma work_scheduledMatrixParameters :
|
| 101 |
+
WorkBound scheduledMatrixParametersRealizer scheduledCountInputMeasure := by
|
| 102 |
+
unfold scheduledMatrixParametersRealizer scheduledMatrixParameters
|
| 103 |
+
have hm : WorkBound (composition parameterProposalWidthRealizer unaryMarkersRealizer)
|
| 104 |
+
scheduledCountInputMeasure := by
|
| 105 |
+
apply work_composition (work_of_polynomial polynomial_parameterProposalWidth X
|
| 106 |
+
weight_le_scheduledCountInputMeasure) work_unaryMarkers X
|
| 107 |
+
intro z
|
| 108 |
+
simp only [id_eq,eval_X,scheduledCountInputMeasure]
|
| 109 |
+
omega
|
| 110 |
+
exact work_pair (work_of_polynomial polynomial_scheduledCacheParameters X
|
| 111 |
+
weight_le_scheduledCountInputMeasure) hm X weight_le_scheduledCountInputMeasure
|
| 112 |
+
|
| 113 |
+
def scheduledStartIndex (z : SamplingParameters) : ℕ := profileBoxIndex (initialCheck z.1)
|
| 114 |
+
|
| 115 |
+
def scheduledStartIndexRealizer : Realizer scheduledStartIndex :=
|
| 116 |
+
composition (composition (f := fun z : SamplingParameters => z.1) (g := initialCheck) first initialCheckRealizer) profileBoxIndexRealizer
|
| 117 |
+
|
| 118 |
+
lemma polynomial_scheduledStartIndex : PolynomialTime scheduledStartIndexRealizer :=
|
| 119 |
+
polynomial_composition (polynomial_composition polynomial_first polynomial_initialCheck)
|
| 120 |
+
polynomial_profileBoxIndex
|
| 121 |
+
|
| 122 |
+
def scheduledWalkInput (z : SamplingParameters) : WalkCountInput :=
|
| 123 |
+
(scheduledMatrixParameters z,(parameterSteps z,scheduledStartIndex z))
|
| 124 |
+
|
| 125 |
+
def scheduledWalkInputRealizer : Realizer scheduledWalkInput :=
|
| 126 |
+
pair scheduledMatrixParametersRealizer (pair parameterStepsRealizer scheduledStartIndexRealizer)
|
| 127 |
+
|
| 128 |
+
attribute [local irreducible] scheduledWalkInput
|
| 129 |
+
|
| 130 |
+
lemma work_scheduledWalkInput : WorkBound scheduledWalkInputRealizer scheduledCountInputMeasure := by
|
| 131 |
+
unfold scheduledWalkInputRealizer scheduledWalkInput
|
| 132 |
+
exact work_pair work_scheduledMatrixParameters
|
| 133 |
+
(work_of_polynomial (polynomial_pair polynomial_parameterSteps polynomial_scheduledStartIndex)
|
| 134 |
+
X weight_le_scheduledCountInputMeasure) X weight_le_scheduledCountInputMeasure
|
| 135 |
+
|
| 136 |
+
def scheduledTrialInput (z : SamplingParameters) : TrialVectorInput := (scheduledWalkInput z,z.1)
|
| 137 |
+
|
| 138 |
+
def scheduledTrialInputRealizer : Realizer scheduledTrialInput := pair scheduledWalkInputRealizer first
|
| 139 |
+
|
| 140 |
+
attribute [local irreducible] scheduledTrialInput
|
| 141 |
+
|
| 142 |
+
lemma work_scheduledTrialInput : WorkBound scheduledTrialInputRealizer scheduledCountInputMeasure := by
|
| 143 |
+
unfold scheduledTrialInputRealizer scheduledTrialInput
|
| 144 |
+
exact work_pair work_scheduledWalkInput (work_of_polynomial polynomial_first X
|
| 145 |
+
weight_le_scheduledCountInputMeasure) X weight_le_scheduledCountInputMeasure
|
| 146 |
+
|
| 147 |
+
def scheduledTrialDenominator (z : SamplingParameters) : ℕ :=
|
| 148 |
+
BinaryPowerPrograms.power (2,scheduledTrialWidth z)
|
| 149 |
+
|
| 150 |
+
def scheduledTrialDenominatorRealizer : Realizer scheduledTrialDenominator :=
|
| 151 |
+
composition (f := fun z : SamplingParameters => (2,scheduledTrialWidth z))
|
| 152 |
+
(g := BinaryPowerPrograms.power)
|
| 153 |
+
(pair (constant 2) scheduledTrialWidthRealizer) BinaryPowerPrograms.powerRealizer
|
| 154 |
+
|
| 155 |
+
attribute [local irreducible] scheduledTrialDenominator
|
| 156 |
+
|
| 157 |
+
lemma work_scheduledTrialDenominator :
|
| 158 |
+
WorkBound scheduledTrialDenominatorRealizer scheduledCountInputMeasure := by
|
| 159 |
+
unfold scheduledTrialDenominatorRealizer scheduledTrialDenominator
|
| 160 |
+
apply work_composition (work_of_polynomial
|
| 161 |
+
(polynomial_pair (polynomial_constant 2) polynomial_scheduledTrialWidth)
|
| 162 |
+
X weight_le_scheduledCountInputMeasure) BinaryPowerPrograms.work_power (X+C (weight (2:ℕ)))
|
| 163 |
+
intro z
|
| 164 |
+
simp only [eval_add,eval_X,eval_C,scheduledCountInputMeasure]
|
| 165 |
+
omega
|
| 166 |
+
|
| 167 |
+
def scheduledOuterInput (z : SamplingParameters) : OuterVectorInput :=
|
| 168 |
+
(scheduledTrialInput z,((unaryMarkers (parameterTrials z),scheduledTrialDenominator z),
|
| 169 |
+
GreedyFeasibleTable.listMatrix z.1.1 z.1.2))
|
| 170 |
+
|
| 171 |
+
def scheduledOuterInputRealizer : Realizer scheduledOuterInput :=
|
| 172 |
+
pair scheduledTrialInputRealizer
|
| 173 |
+
(pair (pair (composition (f := parameterTrials) (g := unaryMarkers)
|
| 174 |
+
parameterTrialsRealizer unaryMarkersRealizer) scheduledTrialDenominatorRealizer)
|
| 175 |
+
(composition (f := fun z : SamplingParameters => z.1)
|
| 176 |
+
(g := fun p => GreedyFeasibleTable.listMatrix p.1 p.2) first GreedyFeasibleTable.tableRealizer))
|
| 177 |
+
|
| 178 |
+
attribute [local irreducible] scheduledOuterInput
|
| 179 |
+
|
| 180 |
+
lemma work_scheduledOuterInput : WorkBound scheduledOuterInputRealizer scheduledCountInputMeasure := by
|
| 181 |
+
unfold scheduledOuterInputRealizer scheduledOuterInput
|
| 182 |
+
have hm : WorkBound (composition parameterTrialsRealizer unaryMarkersRealizer)
|
| 183 |
+
scheduledCountInputMeasure := by
|
| 184 |
+
apply work_composition (work_of_polynomial polynomial_parameterTrials X
|
| 185 |
+
weight_le_scheduledCountInputMeasure) work_unaryMarkers X
|
| 186 |
+
intro z
|
| 187 |
+
simp only [id_eq,eval_X,scheduledCountInputMeasure]
|
| 188 |
+
omega
|
| 189 |
+
exact work_pair work_scheduledTrialInput
|
| 190 |
+
(work_pair (work_pair hm work_scheduledTrialDenominator X weight_le_scheduledCountInputMeasure)
|
| 191 |
+
(work_of_polynomial (polynomial_composition polynomial_first GreedyFeasibleTable.polynomial_table)
|
| 192 |
+
X weight_le_scheduledCountInputMeasure) X weight_le_scheduledCountInputMeasure)
|
| 193 |
+
X weight_le_scheduledCountInputMeasure
|
| 194 |
+
|
| 195 |
+
/-- All first-success numerators, computed from the public binary margins. -/
|
| 196 |
+
def scheduledCounts (z : SamplingParameters) : List ℕ :=
|
| 197 |
+
if hasLargeRectangle z then outerVector (scheduledOuterInput z)
|
| 198 |
+
else UnitCountPrograms.outerVector (scheduledOuterInput z)
|
| 199 |
+
|
| 200 |
+
def scheduledCountsRealizer : Realizer scheduledCounts :=
|
| 201 |
+
choose hasLargeRectangleRealizer
|
| 202 |
+
(composition (f := scheduledOuterInput) (g := outerVector)
|
| 203 |
+
scheduledOuterInputRealizer outerVectorRealizer)
|
| 204 |
+
(composition (f := scheduledOuterInput) (g := UnitCountPrograms.outerVector)
|
| 205 |
+
scheduledOuterInputRealizer UnitCountPrograms.outerVectorRealizer)
|
| 206 |
+
|
| 207 |
+
attribute [local irreducible] scheduledCounts
|
| 208 |
+
|
| 209 |
+
def scheduledCountsMeasure (z : SamplingParameters) : ℕ := scheduledCountInputMeasure z+
|
| 210 |
+
outerVectorMeasure (scheduledOuterInput z)+UnitCountPrograms.outerVectorMeasure (scheduledOuterInput z)
|
| 211 |
+
|
| 212 |
+
lemma work_scheduledCounts : WorkBound scheduledCountsRealizer scheduledCountsMeasure := by
|
| 213 |
+
have hi (z : SamplingParameters) : scheduledCountInputMeasure z ≤ X.eval (scheduledCountsMeasure z) := by
|
| 214 |
+
simp only [eval_X,scheduledCountsMeasure]
|
| 215 |
+
omega
|
| 216 |
+
have hb := work_remeasure work_scheduledOuterInput X hi
|
| 217 |
+
have hw (z : SamplingParameters) : weight z ≤ X.eval (scheduledCountsMeasure z) :=
|
| 218 |
+
(weight_le_scheduledCountInputMeasure z).trans (by simpa only [eval_X] using hi z)
|
| 219 |
+
unfold scheduledCountsRealizer scheduledCounts
|
| 220 |
+
apply work_choose (work_of_polynomial polynomial_hasLargeRectangle X hw)
|
| 221 |
+
· apply work_composition hb work_outerVector X
|
| 222 |
+
intro z
|
| 223 |
+
simp only [eval_X,scheduledCountsMeasure]
|
| 224 |
+
omega
|
| 225 |
+
· apply work_composition hb UnitCountPrograms.work_outerVector X
|
| 226 |
+
intro z
|
| 227 |
+
simp only [eval_X,scheduledCountsMeasure]
|
| 228 |
+
omega
|
| 229 |
+
· exact hw
|
| 230 |
+
|
| 231 |
+
end ContingencyTables.ProfilePrograms
|
| 232 |
+
|
| 233 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/ScheduledCountSemantics.lean
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.ScheduledCountInput
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Sampling.OriginalBoxPush
|
| 3 |
+
import OAI.Combinatorics.ContingencyTables.Sampling.AllSamplingAccuracy
|
| 4 |
+
|
| 5 |
+
namespace OAI
|
| 6 |
+
|
| 7 |
+
/-! # The scheduled executable numerator vector is the public sampler's law -/
|
| 8 |
+
|
| 9 |
+
namespace ContingencyTables.SmallGraphProfiles
|
| 10 |
+
open ProfilePrograms DensePrograms PhysicalCompletionFibres FirstPaperPhysicalMarginal
|
| 11 |
+
open SmallContextCoordinates CompletionCounts OriginalTableBox ResidualMixture FirstSuccess
|
| 12 |
+
open scoped BigOperators Classical
|
| 13 |
+
noncomputable section
|
| 14 |
+
attribute [local irreducible] scheduledTrialDenominator scheduledTrialWidth
|
| 15 |
+
scheduledCounts scheduledOuterInput listedOuterCounts unitOuterCounts matrixInput
|
| 16 |
+
scheduledTableDraw listedOuterDraw unitCodeOuterDraw
|
| 17 |
+
|
| 18 |
+
private lemma dyadic_trialDenom (N s f : ℕ) :
|
| 19 |
+
(2:ℕ)^(N*(s+f)+f)=(2^(s+f))^N*2^f := by
|
| 20 |
+
rw [pow_add,←pow_mul]
|
| 21 |
+
congr 2
|
| 22 |
+
exact Nat.mul_comm _ _
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
variable {m n : ℕ} (r : Fin m → ℕ) (c : Fin n → ℕ)
|
| 26 |
+
local notation "d" => dimensionAllowance (I := Fin m) (J := Fin n)
|
| 27 |
+
local notation "b" => smallBinaryLength r c
|
| 28 |
+
local notation "s" => smallProposalBits (I := Fin m) (J := Fin n)
|
| 29 |
+
|
| 30 |
+
lemma scheduledTrialDenominator_dense (k : ℕ)
|
| 31 |
+
(hlarge : hasLargeRectangle ((List.ofFn r,List.ofFn c),k)=true) :
|
| 32 |
+
scheduledTrialDenominator ((List.ofFn r,List.ofFn c),k)=
|
| 33 |
+
(2^(s+25*(d+b+outerAccuracy d b k+1)^62))^(outerSteps d b k)*
|
| 34 |
+
2^(25*(d+b+outerAccuracy d b k+1)^62) := by
|
| 35 |
+
rw [scheduledTrialDenominator,BinaryPowerPrograms.power_eq,scheduledTrialWidth,hlarge]
|
| 36 |
+
simp only [ite_true,scheduledDenseWidth,parameterSteps_ofFn,parameterProposalWidth_ofFn,parameterCompletionWidth_ofFn]
|
| 37 |
+
exact dyadic_trialDenom _ _ _
|
| 38 |
+
|
| 39 |
+
lemma scheduledTrialDenominator_unit (k : ℕ)
|
| 40 |
+
(hlarge : hasLargeRectangle ((List.ofFn r,List.ofFn c),k)≠true) :
|
| 41 |
+
scheduledTrialDenominator ((List.ofFn r,List.ofFn c),k)=(2^s)^(outerSteps d b k) := by
|
| 42 |
+
rw [scheduledTrialDenominator,BinaryPowerPrograms.power_eq,scheduledTrialWidth,ite_eq_right hlarge]
|
| 43 |
+
rw [scheduledUnitWidth,parameterSteps_ofFn,parameterProposalWidth_ofFn,←pow_mul,Nat.mul_comm]
|
| 44 |
+
|
| 45 |
+
lemma scheduledTrialDenominator_pos (z : SamplingParameters) : 0<scheduledTrialDenominator z := by
|
| 46 |
+
rw [scheduledTrialDenominator,BinaryPowerPrograms.power_eq]
|
| 47 |
+
positivity
|
| 48 |
+
|
| 49 |
+
lemma scheduledOuterInput_code (htotal : ∑ i,r i=∑ j,c j) (k : ℕ) :
|
| 50 |
+
scheduledOuterInput ((List.ofFn r,List.ofFn c),k)=
|
| 51 |
+
(((matrixInput r c (computedCellEquiv r c) b (outerAccuracy d b k)
|
| 52 |
+
(tableState r c (GreedyFeasibleTable.table r c htotal)) (unaryMarkers s),
|
| 53 |
+
(outerSteps d b k,(profileIndex r c (computedCellEquiv r c)
|
| 54 |
+
(tableState r c (GreedyFeasibleTable.table r c htotal))).val)),
|
| 55 |
+
(List.ofFn r,List.ofFn c)),
|
| 56 |
+
((unaryMarkers (outerTrials d k),scheduledTrialDenominator ((List.ofFn r,List.ofFn c),k)),
|
| 57 |
+
matrixCode (GreedyFeasibleTable.table r c htotal).val)) := by
|
| 58 |
+
unfold scheduledOuterInput scheduledTrialInput scheduledWalkInput scheduledMatrixParameters
|
| 59 |
+
scheduledCacheParameters scheduledStartIndex
|
| 60 |
+
rw [initialCheck_code r c htotal,marginPadding_ofFn,parameterDimension_ofFn,
|
| 61 |
+
parameterAccuracy_ofFn,parameterCompletionWidth_ofFn,parameterProposalWidth_ofFn,
|
| 62 |
+
parameterSteps_ofFn,profileBoxIndex_actual,parameterTrials_ofFn,GreedyFeasibleTable.listMatrix_ofFn]
|
| 63 |
+
unfold matrixInput
|
| 64 |
+
rfl
|
| 65 |
+
|
| 66 |
+
/-- No extra table oracle: this is the integer law of the ordinary public branch. -/
|
| 67 |
+
def scheduledCountsLaw (htotal : ∑ i,r i=∑ j,c j) (k : ℕ) :
|
| 68 |
+
IntegerLaw (Table r c)
|
| 69 |
+
((scheduledTrialDenominator ((List.ofFn r,List.ofFn c),k))^(outerTrials d k)) :=
|
| 70 |
+
if hlarge : hasLargeRectangle ((List.ofFn r,List.ofFn c),k)=true then
|
| 71 |
+
(listedOuterCounts r c (computedCellEquiv r c)
|
| 72 |
+
((hasLargeRectangle_spec r c k).mp hlarge).1 ((hasLargeRectangle_spec r c k).mp hlarge).2
|
| 73 |
+
b (outerAccuracy d b k) (smallBinaryLength_parameters r c).1
|
| 74 |
+
(tableState r c (GreedyFeasibleTable.table r c htotal)) (outerSteps d b k) (outerTrials d k)
|
| 75 |
+
(GreedyFeasibleTable.table r c htotal)).rebase
|
| 76 |
+
(congrArg (fun Q => Q^(outerTrials d k)) (scheduledTrialDenominator_dense r c k hlarge).symm)
|
| 77 |
+
else
|
| 78 |
+
(unitOuterCounts r c (computedCellEquiv r c) (allSmall_of_no_large r c k hlarge)
|
| 79 |
+
(tableState r c (GreedyFeasibleTable.table r c htotal)) (outerSteps d b k) (outerTrials d k)
|
| 80 |
+
(GreedyFeasibleTable.table r c htotal)).rebase
|
| 81 |
+
(congrArg (fun Q => Q^(outerTrials d k)) (scheduledTrialDenominator_unit r c k hlarge).symm)
|
| 82 |
+
|
| 83 |
+
lemma scheduledDraw_dense (htotal : ∑ i,r i=∑ j,c j) (k : ℕ)
|
| 84 |
+
(hlarge : hasLargeRectangle ((List.ofFn r,List.ofFn c),k)=true) :
|
| 85 |
+
scheduledTableDraw r c htotal k=
|
| 86 |
+
listedOuterDraw r c (computedCellEquiv r c)
|
| 87 |
+
((hasLargeRectangle_spec r c k).mp hlarge).1 ((hasLargeRectangle_spec r c k).mp hlarge).2
|
| 88 |
+
b (outerAccuracy d b k) (smallBinaryLength_parameters r c).1
|
| 89 |
+
(tableState r c (GreedyFeasibleTable.table r c htotal)) (outerSteps d b k) (outerTrials d k)
|
| 90 |
+
(GreedyFeasibleTable.table r c htotal) := by
|
| 91 |
+
funext bits
|
| 92 |
+
unfold scheduledTableDraw
|
| 93 |
+
rw [dite_eq_left hlarge]
|
| 94 |
+
|
| 95 |
+
lemma scheduledDraw_unit (htotal : ∑ i,r i=∑ j,c j) (k : ℕ)
|
| 96 |
+
(hlarge : hasLargeRectangle ((List.ofFn r,List.ofFn c),k)≠true) :
|
| 97 |
+
scheduledTableDraw r c htotal k=fun bits =>
|
| 98 |
+
unitCodeOuterDraw r c (computedCellEquiv r c) (allSmallFibre r c (allSmall_of_no_large r c k hlarge))
|
| 99 |
+
(tableState r c (GreedyFeasibleTable.table r c htotal)) (outerSteps d b k) (outerTrials d k)
|
| 100 |
+
(GreedyFeasibleTable.table r c htotal) (prefixWord (unitCodeBits_le r c k) bits) := by
|
| 101 |
+
funext bits
|
| 102 |
+
unfold scheduledTableDraw
|
| 103 |
+
rw [dite_eq_right hlarge]
|
| 104 |
+
|
| 105 |
+
lemma scheduledCountsLaw_law (htotal : ∑ i,r i=∑ j,c j) (k : ℕ) :
|
| 106 |
+
(scheduledCountsLaw r c htotal k).toRational
|
| 107 |
+
(pow_pos (scheduledTrialDenominator_pos _) _)=
|
| 108 |
+
mapLaw (uniformLaw (α := Fin (finiteSamplerBits r c k) → Bool))
|
| 109 |
+
(scheduledTableDraw r c htotal k) := by
|
| 110 |
+
unfold scheduledCountsLaw
|
| 111 |
+
split_ifs with hlarge
|
| 112 |
+
· rw [scheduledDraw_dense r c htotal k hlarge]
|
| 113 |
+
exact (IntegerLaw.toRational_rebase _ _ (by positivity) _).trans
|
| 114 |
+
(listedOuterCounts_law r c _ _ _ _ _ _ htotal _ _ _ _)
|
| 115 |
+
· rw [scheduledDraw_unit r c htotal k hlarge]
|
| 116 |
+
exact (IntegerLaw.toRational_rebase _ _ (by positivity) _).trans
|
| 117 |
+
((unitOuterCounts_law r c _ htotal _ _ _ _ _).trans
|
| 118 |
+
(padded_draw_law (unitCodeBits_le r c k) _).symm)
|
| 119 |
+
|
| 120 |
+
/-- Every literal vector entry equals the corresponding embedded table count,
|
| 121 |
+
including zero at infeasible matrices in the original bounding box. -/
|
| 122 |
+
lemma scheduledCounts_actual (htotal : ∑ i,r i=∑ j,c j) (k : ℕ) :
|
| 123 |
+
scheduledCounts ((List.ofFn r,List.ofFn c),k)=
|
| 124 |
+
List.ofFn (((scheduledCountsLaw r c htotal k).push (index r c le_rfl)).count) := by
|
| 125 |
+
unfold scheduledCounts
|
| 126 |
+
by_cases hlarge : hasLargeRectangle ((List.ofFn r,List.ofFn c),k)=true
|
| 127 |
+
· rw [ite_eq_left hlarge,scheduledOuterInput_code r c htotal]
|
| 128 |
+
conv_lhs => rw [scheduledTrialDenominator_dense r c k hlarge]
|
| 129 |
+
rw [outerVector_actual r c (computedCellEquiv r c)
|
| 130 |
+
((hasLargeRectangle_spec r c k).mp hlarge).1 ((hasLargeRectangle_spec r c k).mp hlarge).2
|
| 131 |
+
b (outerAccuracy d b k) (smallBinaryLength_parameters r c).1 htotal _ _
|
| 132 |
+
(unaryMarkers_length _) _ _ _]
|
| 133 |
+
apply congrArg List.ofFn
|
| 134 |
+
funext j
|
| 135 |
+
rw [OriginalBoxLaw.retry_count_some,OriginalBoxLaw.push_count]
|
| 136 |
+
simp only [scheduledCountsLaw,dite_eq_left hlarge,IntegerLaw.rebase,listedOuterCounts]
|
| 137 |
+
simp only [IntegerLaw.retry,unaryMarkers_length]
|
| 138 |
+
· rw [ite_eq_right hlarge,scheduledOuterInput_code r c htotal]
|
| 139 |
+
conv_lhs => rw [scheduledTrialDenominator_unit r c k hlarge]
|
| 140 |
+
rw [unit_outerVector_actual r c (computedCellEquiv r c) b (outerAccuracy d b k)
|
| 141 |
+
(allSmall_of_no_large r c k hlarge) htotal _ _ (unaryMarkers_length _) _ _ _]
|
| 142 |
+
apply congrArg List.ofFn
|
| 143 |
+
funext j
|
| 144 |
+
rw [OriginalBoxLaw.retry_count_some,OriginalBoxLaw.push_count]
|
| 145 |
+
simp only [scheduledCountsLaw,dite_eq_right hlarge,IntegerLaw.rebase,unitOuterCounts]
|
| 146 |
+
simp only [IntegerLaw.retry,unaryMarkers_length]
|
| 147 |
+
|
| 148 |
+
end
|
| 149 |
+
end ContingencyTables.SmallGraphProfiles
|
| 150 |
+
|
| 151 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/ScheduledResidualLaw.lean
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.ScheduledCountSemantics
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Machines.ScheduledResidualProgram
|
| 3 |
+
import OAI.Combinatorics.ContingencyTables.Sampling.CompiledResidualCorrection
|
| 4 |
+
|
| 5 |
+
namespace OAI
|
| 6 |
+
|
| 7 |
+
/-! # Correctness and positivity of the executed correction vector -/
|
| 8 |
+
|
| 9 |
+
namespace ContingencyTables.SmallGraphProfiles
|
| 10 |
+
open ProfilePrograms DensePrograms OriginalTableBox ResidualMixture FirstSuccess CompletionCounts
|
| 11 |
+
open scoped BigOperators Classical
|
| 12 |
+
noncomputable section
|
| 13 |
+
|
| 14 |
+
variable {m n : ℕ} (r : Fin m → ℕ) (c : Fin n → ℕ)
|
| 15 |
+
local notation "d" => dimensionAllowance (I := Fin m) (J := Fin n)
|
| 16 |
+
local notation "D" => correctionPrecision r c
|
| 17 |
+
local notation "q" => largeTailCount r (d^20)*largeTailCount c (d^20)
|
| 18 |
+
|
| 19 |
+
lemma scheduledCounts_domination
|
| 20 |
+
(hPL : PublishedInputs.FiniteBoxPrekopaLeindler q)
|
| 21 |
+
(hCheeger : PublishedInputs.FiniteCheeger (GridBoundary.Grid (I := Fin q) (4*d^8)))
|
| 22 |
+
(htotal : (∑ i,r i)=∑ j,c j) :
|
| 23 |
+
let _ : Nonempty (Table r c) := ⟨GreedyFeasibleTable.table r c htotal⟩
|
| 24 |
+
∀ X,(1-1/((2^D:ℕ):ℚ))*
|
| 25 |
+
((scheduledCountsLaw r c htotal (2*D)).toRational
|
| 26 |
+
(pow_pos (scheduledTrialDenominator_pos _) _)).mass X≤uniformMass (α := Table r c) := by
|
| 27 |
+
let : Nonempty (Table r c) := ⟨GreedyFeasibleTable.table r c htotal⟩
|
| 28 |
+
dsimp only
|
| 29 |
+
rw [scheduledCountsLaw_law]
|
| 30 |
+
obtain ⟨residual,hr⟩ := scheduled_residual_correction r c hPL hCheeger htotal
|
| 31 |
+
intro X
|
| 32 |
+
have hn : 0≤(1/(2:ℚ)^D)*residual.mass X :=
|
| 33 |
+
mul_nonneg (by positivity) (residual.nonnegative X)
|
| 34 |
+
simp only [Nat.cast_pow,Nat.cast_ofNat]
|
| 35 |
+
linarith [hr X]
|
| 36 |
+
|
| 37 |
+
/-- The explicit correction vector is the embedded integer residual law. -/
|
| 38 |
+
theorem scheduledResidualCounts_actual
|
| 39 |
+
(hPL : PublishedInputs.FiniteBoxPrekopaLeindler q)
|
| 40 |
+
(hCheeger : PublishedInputs.FiniteCheeger (GridBoundary.Grid (I := Fin q) (4*d^8)))
|
| 41 |
+
(htotal : (∑ i,r i)=∑ j,c j) :
|
| 42 |
+
let _ : Nonempty (Table r c) := ⟨GreedyFeasibleTable.table r c htotal⟩
|
| 43 |
+
scheduledResidualCounts (((List.ofFn r,List.ofFn c),2*D),2^D)=
|
| 44 |
+
List.ofFn ((IntegerResidualCorrection.law (scheduledCountsLaw r c htotal (2*D)) (2^D)
|
| 45 |
+
(pow_pos (scheduledTrialDenominator_pos _) _) (by positivity)
|
| 46 |
+
(scheduledCounts_domination r c hPL hCheeger htotal)).push (index r c le_rfl)).count := by
|
| 47 |
+
let : Nonempty (Table r c) := ⟨GreedyFeasibleTable.table r c htotal⟩
|
| 48 |
+
dsimp only
|
| 49 |
+
unfold scheduledResidualCounts cachedResidualCounts cachedResidualInput scheduledResidualCache
|
| 50 |
+
rw [scheduledCounts_actual r c htotal,List.sum_ofFn,(IntegerLaw.push _ _).total]
|
| 51 |
+
exact OriginalBoxLaw.residualVector_law r c _ (GreedyFeasibleTable.table r c htotal) _
|
| 52 |
+
(pow_pos (scheduledTrialDenominator_pos _) _) (by positivity)
|
| 53 |
+
(scheduledCounts_domination r c hPL hCheeger htotal)
|
| 54 |
+
|
| 55 |
+
/-- Positive total weight makes the exact weighted rejection loop applicable. -/
|
| 56 |
+
theorem scheduledResidualCounts_sum
|
| 57 |
+
(hPL : PublishedInputs.FiniteBoxPrekopaLeindler q)
|
| 58 |
+
(hCheeger : PublishedInputs.FiniteCheeger (GridBoundary.Grid (I := Fin q) (4*d^8)))
|
| 59 |
+
(htotal : (∑ i,r i)=∑ j,c j) :
|
| 60 |
+
0<(scheduledResidualCounts (((List.ofFn r,List.ofFn c),2*D),2^D)).sum := by
|
| 61 |
+
let : Nonempty (Table r c) := ⟨GreedyFeasibleTable.table r c htotal⟩
|
| 62 |
+
rw [scheduledResidualCounts_actual r c hPL hCheeger htotal,List.sum_ofFn,(IntegerLaw.push _ _).total]
|
| 63 |
+
exact Nat.mul_pos Fintype.card_pos (pow_pos (scheduledTrialDenominator_pos _) _)
|
| 64 |
+
|
| 65 |
+
end
|
| 66 |
+
end ContingencyTables.SmallGraphProfiles
|
| 67 |
+
|
| 68 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/ScheduledResidualProgram.lean
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.ScheduledCountInput
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Sampling.OriginalResidualVector
|
| 3 |
+
|
| 4 |
+
namespace OAI
|
| 5 |
+
|
| 6 |
+
/-! # Executing residual weights from margins, precision, and branch denominator -/
|
| 7 |
+
|
| 8 |
+
namespace ContingencyTables.ProfilePrograms
|
| 9 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped DensePrograms Polynomial
|
| 10 |
+
|
| 11 |
+
attribute [local irreducible] scheduledCounts residualVector originalCandidates originalCardinality
|
| 12 |
+
|
| 13 |
+
/-- Ordinary count vector, original margins, and reciprocal branch probability. -/
|
| 14 |
+
abbrev ScheduledResidualCache := List ℕ×(CompletionMargins×ℕ)
|
| 15 |
+
|
| 16 |
+
def scheduledResidualCache (z : SamplingParameters×ℕ) : ScheduledResidualCache :=
|
| 17 |
+
(scheduledCounts z.1,(z.1.1,z.2))
|
| 18 |
+
|
| 19 |
+
def scheduledResidualCacheRealizer : Realizer scheduledResidualCache :=
|
| 20 |
+
pair (composition (f := fun z : SamplingParameters×ℕ => z.1) (g := scheduledCounts)
|
| 21 |
+
first scheduledCountsRealizer) (pair (composition first first) second)
|
| 22 |
+
|
| 23 |
+
def scheduledResidualMeasure (z : SamplingParameters×ℕ) : ℕ :=
|
| 24 |
+
scheduledCountsMeasure z.1+originalEnumerationMeasure z.1.1+weight z
|
| 25 |
+
|
| 26 |
+
lemma work_scheduledResidualCache :
|
| 27 |
+
WorkBound scheduledResidualCacheRealizer scheduledResidualMeasure := by
|
| 28 |
+
have hi (z : SamplingParameters×ℕ) : weight z ≤ X.eval (scheduledResidualMeasure z) := by
|
| 29 |
+
simp only [eval_X,scheduledResidualMeasure]
|
| 30 |
+
omega
|
| 31 |
+
have hc : WorkBound (composition (first (B := ℕ)) scheduledCountsRealizer)
|
| 32 |
+
scheduledResidualMeasure := by
|
| 33 |
+
apply work_composition (work_of_polynomial polynomial_first X hi) work_scheduledCounts X
|
| 34 |
+
intro z
|
| 35 |
+
simp only [eval_X,scheduledResidualMeasure]
|
| 36 |
+
omega
|
| 37 |
+
exact work_pair hc (work_of_polynomial (polynomial_pair
|
| 38 |
+
(polynomial_composition polynomial_first polynomial_first) polynomial_second) X hi) X hi
|
| 39 |
+
|
| 40 |
+
def cachedResidualInput (z : ScheduledResidualCache) : ResidualVectorCache :=
|
| 41 |
+
((originalCandidates z.2.1,z.1),((z.1.sum,z.2.2),originalCardinality z.2.1))
|
| 42 |
+
|
| 43 |
+
def cachedResidualInputRealizer : Realizer cachedResidualInput :=
|
| 44 |
+
pair (pair (composition (f := fun z : ScheduledResidualCache => z.2.1) (g := originalCandidates)
|
| 45 |
+
(composition second first) originalCandidatesRealizer) first)
|
| 46 |
+
(pair (pair (composition first listSum) (composition second second))
|
| 47 |
+
(composition (f := fun z : ScheduledResidualCache => z.2.1) (g := originalCardinality)
|
| 48 |
+
(composition second first) originalCardinalityRealizer))
|
| 49 |
+
|
| 50 |
+
def cachedResidualMeasure (z : ScheduledResidualCache) : ℕ :=
|
| 51 |
+
originalEnumerationMeasure z.2.1+weight z
|
| 52 |
+
|
| 53 |
+
lemma work_cachedResidualInput : WorkBound cachedResidualInputRealizer cachedResidualMeasure := by
|
| 54 |
+
have hi (z : ScheduledResidualCache) : weight z ≤ X.eval (cachedResidualMeasure z) := by
|
| 55 |
+
simp only [eval_X,cachedResidualMeasure]
|
| 56 |
+
omega
|
| 57 |
+
have he : WorkBound (composition (composition (second (A := List ℕ)) first)
|
| 58 |
+
originalCandidatesRealizer) cachedResidualMeasure := by
|
| 59 |
+
apply work_composition (work_of_polynomial (polynomial_composition polynomial_second polynomial_first)
|
| 60 |
+
X hi) work_originalCandidates X
|
| 61 |
+
intro z
|
| 62 |
+
simp only [eval_X,cachedResidualMeasure,Function.comp_apply]
|
| 63 |
+
omega
|
| 64 |
+
have hn : WorkBound (composition (composition (second (A := List ℕ)) first)
|
| 65 |
+
originalCardinalityRealizer) cachedResidualMeasure := by
|
| 66 |
+
apply work_composition (work_of_polynomial (polynomial_composition polynomial_second polynomial_first)
|
| 67 |
+
X hi) work_originalCardinality X
|
| 68 |
+
intro z
|
| 69 |
+
simp only [eval_X,cachedResidualMeasure,Function.comp_apply]
|
| 70 |
+
omega
|
| 71 |
+
exact work_pair (work_pair he (work_of_polynomial polynomial_first X hi) X hi)
|
| 72 |
+
(work_pair (work_of_polynomial (polynomial_pair
|
| 73 |
+
(polynomial_composition polynomial_first polynomial_listSum)
|
| 74 |
+
(polynomial_composition polynomial_second polynomial_second)) X hi) hn X hi) X hi
|
| 75 |
+
|
| 76 |
+
def cachedResidualCounts (z : ScheduledResidualCache) : List ℕ :=
|
| 77 |
+
residualVector (cachedResidualInput z)
|
| 78 |
+
|
| 79 |
+
def cachedResidualCountsRealizer : Realizer cachedResidualCounts :=
|
| 80 |
+
composition (f := cachedResidualInput) (g := residualVector)
|
| 81 |
+
cachedResidualInputRealizer residualVectorRealizer
|
| 82 |
+
|
| 83 |
+
lemma work_cachedResidualCounts : WorkBound cachedResidualCountsRealizer cachedResidualMeasure :=
|
| 84 |
+
work_then_polynomial work_cachedResidualInput polynomial_residualVector
|
| 85 |
+
|
| 86 |
+
attribute [local irreducible] scheduledResidualCache cachedResidualCounts
|
| 87 |
+
|
| 88 |
+
/-- This computes the complete correction vector; it asks for no probability oracle. -/
|
| 89 |
+
def scheduledResidualCounts (z : SamplingParameters×ℕ) : List ℕ :=
|
| 90 |
+
cachedResidualCounts (scheduledResidualCache z)
|
| 91 |
+
|
| 92 |
+
def scheduledResidualCountsRealizer : Realizer scheduledResidualCounts :=
|
| 93 |
+
composition (f := scheduledResidualCache) (g := cachedResidualCounts)
|
| 94 |
+
scheduledResidualCacheRealizer cachedResidualCountsRealizer
|
| 95 |
+
|
| 96 |
+
lemma work_scheduledResidualCounts :
|
| 97 |
+
WorkBound scheduledResidualCountsRealizer scheduledResidualMeasure := by
|
| 98 |
+
unfold scheduledResidualCountsRealizer scheduledResidualCounts
|
| 99 |
+
obtain ⟨p,hp⟩ := work_scheduledResidualCache
|
| 100 |
+
apply work_composition ⟨p,hp⟩ work_cachedResidualCounts (X+p)
|
| 101 |
+
intro z
|
| 102 |
+
have hw := hp z
|
| 103 |
+
simp only [scheduledResidualCache] at hw
|
| 104 |
+
simp only [eval_add,eval_X,cachedResidualMeasure,scheduledResidualCache]
|
| 105 |
+
have hm : originalEnumerationMeasure z.1.1 ≤ scheduledResidualMeasure z := by
|
| 106 |
+
unfold scheduledResidualMeasure
|
| 107 |
+
omega
|
| 108 |
+
omega
|
| 109 |
+
|
| 110 |
+
end ContingencyTables.ProfilePrograms
|
| 111 |
+
|
| 112 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/ScheduledScalarBounds.lean
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Sampling.ExactInputBounds
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Machines.ScheduledCountInput
|
| 3 |
+
|
| 4 |
+
namespace OAI
|
| 5 |
+
|
| 6 |
+
/-! # Charging scalar tabulation loops to the ordinary sampler's word -/
|
| 7 |
+
|
| 8 |
+
namespace ContingencyTables.ProfilePrograms
|
| 9 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped DensePrograms Polynomial
|
| 10 |
+
|
| 11 |
+
lemma parameterDimension_pos (z : SamplingParameters) : 0 < parameterDimension z := by
|
| 12 |
+
unfold parameterDimension marginDimension
|
| 13 |
+
omega
|
| 14 |
+
|
| 15 |
+
lemma parameterBinaryLength_pos (z : SamplingParameters) : 0 < parameterBinaryLength z := by
|
| 16 |
+
have h := parameterDimension_pos z
|
| 17 |
+
unfold parameterBinaryLength marginBinaryLength parameterDimension at *
|
| 18 |
+
omega
|
| 19 |
+
|
| 20 |
+
lemma parameterSteps_pos (z : SamplingParameters) : 0 < parameterSteps z := by
|
| 21 |
+
unfold parameterSteps
|
| 22 |
+
exact Nat.mul_pos (pow_pos (parameterDimension_pos z) _)
|
| 23 |
+
(pow_pos (by have := parameterBinaryLength_pos z; omega) _)
|
| 24 |
+
|
| 25 |
+
lemma parameterTrials_pos (z : SamplingParameters) : 0 < parameterTrials z := by
|
| 26 |
+
unfold parameterTrials
|
| 27 |
+
exact Nat.mul_pos (pow_pos (parameterDimension_pos z) _) (by omega)
|
| 28 |
+
|
| 29 |
+
lemma parameterCompletionWidth_pos (z : SamplingParameters) : 0 < parameterCompletionWidth z := by
|
| 30 |
+
unfold parameterCompletionWidth
|
| 31 |
+
exact Nat.mul_pos (by decide) (pow_pos (by omega) _)
|
| 32 |
+
|
| 33 |
+
lemma parameterDimension_le_steps (z : SamplingParameters) :
|
| 34 |
+
parameterDimension z ≤ parameterSteps z := by
|
| 35 |
+
have hd : 1 ≤ parameterDimension z := parameterDimension_pos z
|
| 36 |
+
have hb : 1 ≤ z.2+parameterBinaryLength z := by have := parameterBinaryLength_pos z; omega
|
| 37 |
+
exact (Nat.le_self_pow (by decide : 200 ≠ 0) _).trans (by
|
| 38 |
+
unfold parameterSteps
|
| 39 |
+
simpa only [Nat.mul_one] using Nat.mul_le_mul_left ((parameterDimension z)^200)
|
| 40 |
+
(one_le_pow₀ hb : 1 ≤ (z.2+parameterBinaryLength z)^2))
|
| 41 |
+
|
| 42 |
+
lemma parameterBinaryLength_le_steps (z : SamplingParameters) :
|
| 43 |
+
parameterBinaryLength z ≤ parameterSteps z := by
|
| 44 |
+
have hd : 1 ≤ (parameterDimension z)^200 := one_le_pow₀ (parameterDimension_pos z)
|
| 45 |
+
have hb : parameterBinaryLength z ≤ (z.2+parameterBinaryLength z)^2 :=
|
| 46 |
+
(Nat.le_add_left _ _).trans (Nat.le_self_pow (by decide) _)
|
| 47 |
+
unfold parameterSteps
|
| 48 |
+
exact hb.trans (by simpa only [Nat.one_mul] using
|
| 49 |
+
Nat.mul_le_mul_right ((z.2+parameterBinaryLength z)^2) hd)
|
| 50 |
+
|
| 51 |
+
lemma parameterAccuracy_le_steps (z : SamplingParameters) :
|
| 52 |
+
parameterAccuracy z ≤ parameterSteps z := by
|
| 53 |
+
unfold parameterAccuracy parameterSteps
|
| 54 |
+
exact Nat.mul_le_mul_right _ (Nat.pow_le_pow_right (parameterDimension_pos z) (by decide))
|
| 55 |
+
|
| 56 |
+
theorem schedule_numeric_polynomial : ∃ p : Polynomial ℕ,∀ z,
|
| 57 |
+
parameterDimension z+parameterAccuracy z+parameterCompletionWidth z ≤
|
| 58 |
+
p.eval (scheduledCountInputMeasure z) := by
|
| 59 |
+
refine ⟨2*X+25*(3*X+1)^62,?_⟩
|
| 60 |
+
intro z
|
| 61 |
+
let M := scheduledCountInputMeasure z
|
| 62 |
+
have hs : parameterSteps z ≤ M := by unfold M scheduledCountInputMeasure; omega
|
| 63 |
+
have hd := (parameterDimension_le_steps z).trans hs
|
| 64 |
+
have hb := (parameterBinaryLength_le_steps z).trans hs
|
| 65 |
+
have ha := (parameterAccuracy_le_steps z).trans hs
|
| 66 |
+
have hf : parameterCompletionWidth z ≤ 25*(3*M+1)^62 := by
|
| 67 |
+
unfold parameterCompletionWidth
|
| 68 |
+
exact Nat.mul_le_mul_left _ (Nat.pow_le_pow_left (by omega) _)
|
| 69 |
+
simp only [eval_add,eval_mul,eval_pow,eval_ofNat,eval_X,eval_one]
|
| 70 |
+
change _ ≤ 2*M+25*(3*M+1)^62
|
| 71 |
+
omega
|
| 72 |
+
|
| 73 |
+
lemma scheduledCountInputMeasure_le (z : SamplingParameters) :
|
| 74 |
+
scheduledCountInputMeasure z ≤ 4*parameterWordCount z+weight z := by
|
| 75 |
+
let N := parameterSteps z
|
| 76 |
+
let J := parameterTrials z
|
| 77 |
+
let P := parameterProposalWidth z
|
| 78 |
+
let F := parameterCompletionWidth z
|
| 79 |
+
let A := N*(P+F)+F
|
| 80 |
+
have hN : 1 ≤ N := parameterSteps_pos z
|
| 81 |
+
have hJ : 1 ≤ J := parameterTrials_pos z
|
| 82 |
+
have hF : 1 ≤ F := parameterCompletionWidth_pos z
|
| 83 |
+
have hNF : N ≤ N*(P+F) := by
|
| 84 |
+
simpa only [Nat.mul_one] using Nat.mul_le_mul_left N (show 1 ≤ P+F by omega)
|
| 85 |
+
have hPF : P+F ≤ N*(P+F) := by
|
| 86 |
+
simpa only [Nat.one_mul] using Nat.mul_le_mul_right (P+F) hN
|
| 87 |
+
have hA : 1 ≤ A := by unfold A; omega
|
| 88 |
+
have hAW : A ≤ J*A := by
|
| 89 |
+
simpa only [Nat.one_mul] using Nat.mul_le_mul_right A hJ
|
| 90 |
+
have hJW : J ≤ J*A := by
|
| 91 |
+
simpa only [Nat.mul_one] using Nat.mul_le_mul_left J hA
|
| 92 |
+
have hT : scheduledTrialWidth z ≤ A := by
|
| 93 |
+
unfold scheduledTrialWidth scheduledDenseWidth scheduledUnitWidth
|
| 94 |
+
change (if hasLargeRectangle z then N*(P+F)+F else N*P) ≤ A
|
| 95 |
+
split_ifs
|
| 96 |
+
· exact le_rfl
|
| 97 |
+
· unfold A
|
| 98 |
+
exact (Nat.mul_le_mul_left N (Nat.le_add_right P F)).trans (Nat.le_add_right _ _)
|
| 99 |
+
change N+J+P+scheduledTrialWidth z+weight z ≤ 4*(J*A)+weight z
|
| 100 |
+
have hNA : N ≤ A := by unfold A; omega
|
| 101 |
+
have hPA : P ≤ A := by unfold A; omega
|
| 102 |
+
have hnw := hNA.trans hAW
|
| 103 |
+
have hpw := hPA.trans hAW
|
| 104 |
+
have htw := hT.trans hAW
|
| 105 |
+
omega
|
| 106 |
+
|
| 107 |
+
end ContingencyTables.ProfilePrograms
|
| 108 |
+
|
| 109 |
+
namespace ContingencyTables.SmallGraphProfiles
|
| 110 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped ProfilePrograms DensePrograms Polynomial
|
| 111 |
+
open scoped BigOperators Classical
|
| 112 |
+
noncomputable section
|
| 113 |
+
|
| 114 |
+
theorem exact_count_input_measure_polynomial : ∃ p : Polynomial ℕ,
|
| 115 |
+
∀ (m n : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ),
|
| 116 |
+
(∑ i,r i)=∑ j,c j →
|
| 117 |
+
scheduledCountInputMeasure (exactSamplingParameters (List.ofFn r,List.ofFn c)) ≤
|
| 118 |
+
p.eval (Algorithms.samplingSize n r 0) := by
|
| 119 |
+
obtain ⟨p,hp⟩ := exact_ordinary_bits_polynomial
|
| 120 |
+
obtain ⟨q,hq⟩ := exact_margin_weight_polynomial
|
| 121 |
+
obtain ⟨u,hu⟩ := polynomial_exactSamplingParameters
|
| 122 |
+
refine ⟨4*p+u.comp q,?_⟩
|
| 123 |
+
intro m n r c htotal
|
| 124 |
+
have hw : weight (exactSamplingParameters (List.ofFn r,List.ofFn c)) ≤
|
| 125 |
+
u.eval (q.eval (Algorithms.samplingSize n r 0)) :=
|
| 126 |
+
(Nat.le_add_left _ _).trans ((hu (List.ofFn r,List.ofFn c)).trans
|
| 127 |
+
(polynomial_eval_monotone u (hq m n r c htotal)))
|
| 128 |
+
have hb : parameterWordCount (exactSamplingParameters (List.ofFn r,List.ofFn c)) ≤
|
| 129 |
+
p.eval (Algorithms.samplingSize n r 0) := hp m n r c
|
| 130 |
+
have hs := scheduledCountInputMeasure_le (exactSamplingParameters (List.ofFn r,List.ofFn c))
|
| 131 |
+
simp only [eval_add,eval_mul,eval_ofNat,eval_comp]
|
| 132 |
+
omega
|
| 133 |
+
|
| 134 |
+
end
|
| 135 |
+
end ContingencyTables.SmallGraphProfiles
|
| 136 |
+
|
| 137 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/SignedFloorProgram.lean
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.SignedPairProgram
|
| 2 |
+
import OAI.Combinatorics.MatchingCount.Machines.TreeFiniteOps
|
| 3 |
+
import Mathlib.Data.Rat.Floor
|
| 4 |
+
|
| 5 |
+
namespace OAI
|
| 6 |
+
|
| 7 |
+
/-! # Binary floor of a signed rational
|
| 8 |
+
|
| 9 |
+
Two natural quotients and the comparison of their remainders compute
|
| 10 |
+
the exact floor. No loop depends on the numeric numerator or denominator.
|
| 11 |
+
-/
|
| 12 |
+
|
| 13 |
+
namespace ContingencyTables.SignedFloorProgram
|
| 14 |
+
|
| 15 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped SignedPairProgram
|
| 16 |
+
|
| 17 |
+
abbrev Input := Signed×ℕ
|
| 18 |
+
|
| 19 |
+
def numeratorQuotient (z : Input) : ℕ := z.1.1/z.2
|
| 20 |
+
def numeratorQuotientRealizer : Realizer numeratorQuotient :=
|
| 21 |
+
composition (pair (composition first first) second) quotient
|
| 22 |
+
lemma polynomial_numeratorQuotient : PolynomialTime numeratorQuotientRealizer :=
|
| 23 |
+
polynomial_composition (polynomial_pair
|
| 24 |
+
(polynomial_composition polynomial_first polynomial_first) polynomial_second)
|
| 25 |
+
polynomial_quotient
|
| 26 |
+
|
| 27 |
+
def negativeQuotient (z : Input) : ℕ := z.1.2/z.2
|
| 28 |
+
def negativeQuotientRealizer : Realizer negativeQuotient :=
|
| 29 |
+
composition (pair (composition first second) second) quotient
|
| 30 |
+
lemma polynomial_negativeQuotient : PolynomialTime negativeQuotientRealizer :=
|
| 31 |
+
polynomial_composition (polynomial_pair
|
| 32 |
+
(polynomial_composition polynomial_first polynomial_second) polynomial_second)
|
| 33 |
+
polynomial_quotient
|
| 34 |
+
|
| 35 |
+
def carry (z : Input) : Bool := decide (z.1.1%z.2<z.1.2%z.2)
|
| 36 |
+
def carryRealizer : Realizer carry :=
|
| 37 |
+
composition (pair
|
| 38 |
+
(composition (pair (composition first first) second) remainder)
|
| 39 |
+
(composition (pair (composition first second) second) remainder)) less
|
| 40 |
+
lemma polynomial_carry : PolynomialTime carryRealizer :=
|
| 41 |
+
polynomial_composition (polynomial_pair
|
| 42 |
+
(polynomial_composition (polynomial_pair
|
| 43 |
+
(polynomial_composition polynomial_first polynomial_first) polynomial_second)
|
| 44 |
+
polynomial_remainder)
|
| 45 |
+
(polynomial_composition (polynomial_pair
|
| 46 |
+
(polynomial_composition polynomial_first polynomial_second) polynomial_second)
|
| 47 |
+
polynomial_remainder)) polynomial_less
|
| 48 |
+
|
| 49 |
+
def carryValue (z : Input) : ℕ := if carry z then 1 else 0
|
| 50 |
+
def carryValueRealizer : Realizer carryValue :=
|
| 51 |
+
choose carryRealizer (constant 1) (constant 0)
|
| 52 |
+
lemma polynomial_carryValue : PolynomialTime carryValueRealizer :=
|
| 53 |
+
polynomial_choose polynomial_carry (polynomial_constant 1) (polynomial_constant 0)
|
| 54 |
+
|
| 55 |
+
def raw (z : Input) : Signed := (numeratorQuotient z,negativeQuotient z+carryValue z)
|
| 56 |
+
def rawRealizer : Realizer raw := pair numeratorQuotientRealizer
|
| 57 |
+
(composition (pair negativeQuotientRealizer carryValueRealizer) add)
|
| 58 |
+
lemma polynomial_raw : PolynomialTime rawRealizer :=
|
| 59 |
+
polynomial_pair polynomial_numeratorQuotient
|
| 60 |
+
(polynomial_composition (polynomial_pair polynomial_negativeQuotient polynomial_carryValue)
|
| 61 |
+
polynomial_add)
|
| 62 |
+
|
| 63 |
+
def floorCode (z : Input) : Signed := if z.2=0 then (0,0) else raw z
|
| 64 |
+
def floorCodeRealizer : Realizer floorCode :=
|
| 65 |
+
(choose (composition (pair second (constant 0)) natEqual) (constant (0,0))
|
| 66 |
+
rawRealizer).congr (fun z=>by simp only [floorCode,Function.comp_apply,decide_eq_true_eq])
|
| 67 |
+
theorem polynomial_floorCode : PolynomialTime floorCodeRealizer :=
|
| 68 |
+
polynomial_congr _ (polynomial_choose
|
| 69 |
+
(polynomial_composition (polynomial_pair polynomial_second (polynomial_constant 0))
|
| 70 |
+
polynomial_natEqual) (polynomial_constant (0,0)) polynomial_raw)
|
| 71 |
+
|
| 72 |
+
lemma raw_value (p n d : ℕ) (hd : 0<d) :
|
| 73 |
+
value (raw ((p,n),d))=⌊((p:ℚ)-n)/d⌋ := by
|
| 74 |
+
have hdQ: (0:ℚ)<d:=by exact_mod_cast hd
|
| 75 |
+
have hp: ((p%d:ℕ):ℚ)+(d:ℚ)*((p/d:ℕ):ℚ)=(p:ℚ):=by
|
| 76 |
+
exact_mod_cast Nat.mod_add_div p d
|
| 77 |
+
have hn: ((n%d:ℕ):ℚ)+(d:ℚ)*((n/d:ℕ):ℚ)=(n:ℚ):=by
|
| 78 |
+
exact_mod_cast Nat.mod_add_div n d
|
| 79 |
+
have hpr: ((p%d:ℕ):ℚ)<d:=by exact_mod_cast Nat.mod_lt p hd
|
| 80 |
+
have hnr: ((n%d:ℕ):ℚ)<d:=by exact_mod_cast Nat.mod_lt n hd
|
| 81 |
+
have hp0: (0:ℚ)≤((p%d:ℕ):ℚ):=Nat.cast_nonneg _
|
| 82 |
+
have hn0: (0:ℚ)≤((n%d:ℕ):ℚ):=Nat.cast_nonneg _
|
| 83 |
+
symm
|
| 84 |
+
apply Int.floor_eq_iff.mpr
|
| 85 |
+
by_cases h: p%d<n%d
|
| 86 |
+
· have hr: ((p%d:ℕ):ℚ)<((n%d:ℕ):ℚ):=by exact_mod_cast h
|
| 87 |
+
simp only [raw,numeratorQuotient,negativeQuotient,carryValue,carry,h,decide_true,
|
| 88 |
+
ite_true,value,Nat.cast_add,Nat.cast_one,Int.cast_sub,
|
| 89 |
+
Int.cast_add,Int.cast_one,Int.cast_natCast]
|
| 90 |
+
constructor
|
| 91 |
+
· rw [le_div_iff₀ hdQ]
|
| 92 |
+
nlinarith
|
| 93 |
+
· rw [div_lt_iff₀ hdQ]
|
| 94 |
+
nlinarith
|
| 95 |
+
· have hr: ((n%d:ℕ):ℚ)≤((p%d:ℕ):ℚ):=by exact_mod_cast (Nat.le_of_not_gt h)
|
| 96 |
+
simp only [raw,numeratorQuotient,negativeQuotient,carryValue,carry,h,decide_false,
|
| 97 |
+
Bool.false_eq_true,ite_false,Nat.add_zero,value,Int.cast_sub,Int.cast_natCast]
|
| 98 |
+
constructor
|
| 99 |
+
· rw [le_div_iff₀ hdQ]
|
| 100 |
+
nlinarith
|
| 101 |
+
· rw [div_lt_iff₀ hdQ]
|
| 102 |
+
nlinarith
|
| 103 |
+
|
| 104 |
+
theorem floorCode_value (p n d : ℕ) :
|
| 105 |
+
value (floorCode ((p,n),d))=⌊((p:ℚ)-n)/d⌋ := by
|
| 106 |
+
by_cases hd: d=0
|
| 107 |
+
· simp [floorCode,hd,value]
|
| 108 |
+
· simp only [floorCode,hd,ite_false]
|
| 109 |
+
exact raw_value p n d (Nat.pos_of_ne_zero hd)
|
| 110 |
+
|
| 111 |
+
abbrev ScaledInput := Input×List Bool
|
| 112 |
+
|
| 113 |
+
def scale (z : ScaledInput) : Input :=
|
| 114 |
+
((z.2.length*z.1.1.1,z.2.length*z.1.1.2),z.1.2)
|
| 115 |
+
def scaleRealizer : Realizer scale :=
|
| 116 |
+
pair (pair
|
| 117 |
+
(composition (pair (composition second listLength)
|
| 118 |
+
(composition (composition first first) first)) multiply)
|
| 119 |
+
(composition (pair (composition second listLength)
|
| 120 |
+
(composition (composition first first) second)) multiply)) (composition first second)
|
| 121 |
+
lemma polynomial_scale : PolynomialTime scaleRealizer :=
|
| 122 |
+
polynomial_pair (polynomial_pair
|
| 123 |
+
(polynomial_composition (polynomial_pair
|
| 124 |
+
(polynomial_composition polynomial_second polynomial_listLength)
|
| 125 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 126 |
+
polynomial_first)) polynomial_multiply)
|
| 127 |
+
(polynomial_composition (polynomial_pair
|
| 128 |
+
(polynomial_composition polynomial_second polynomial_listLength)
|
| 129 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 130 |
+
polynomial_second)) polynomial_multiply))
|
| 131 |
+
(polynomial_composition polynomial_first polynomial_second)
|
| 132 |
+
|
| 133 |
+
def scaledFloor (z : ScaledInput) : Signed := floorCode (scale z)
|
| 134 |
+
def scaledFloorRealizer : Realizer scaledFloor := composition scaleRealizer floorCodeRealizer
|
| 135 |
+
theorem polynomial_scaledFloor : PolynomialTime scaledFloorRealizer :=
|
| 136 |
+
polynomial_composition polynomial_scale polynomial_floorCode
|
| 137 |
+
|
| 138 |
+
theorem scaledFloor_value (p n d : ℕ) (xs : List Bool) :
|
| 139 |
+
value (scaledFloor (((p,n),d),xs))=
|
| 140 |
+
⌊(xs.length:ℚ)*(((p:ℚ)-n)/d)⌋ := by
|
| 141 |
+
rw [scaledFloor,scale,floorCode_value]
|
| 142 |
+
congr 1
|
| 143 |
+
push_cast
|
| 144 |
+
ring
|
| 145 |
+
|
| 146 |
+
end ContingencyTables.SignedFloorProgram
|
| 147 |
+
|
| 148 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/SignedPairProgram.lean
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Rooted.ForestAffineProgram
|
| 2 |
+
|
| 3 |
+
namespace OAI
|
| 4 |
+
|
| 5 |
+
/-! # Binary signed arithmetic used to prepare the forest evaluator
|
| 6 |
+
|
| 7 |
+
A pair stores a difference of naturals. Arithmetic never iterates through
|
| 8 |
+
the represented integer value, and normalization returns the exact
|
| 9 |
+
positive/negative parts expected by the affine penalty program.
|
| 10 |
+
-/
|
| 11 |
+
|
| 12 |
+
namespace ContingencyTables.SignedPairProgram
|
| 13 |
+
|
| 14 |
+
open MatchingFPRAS.TreeTyped
|
| 15 |
+
open scoped BigOperators
|
| 16 |
+
|
| 17 |
+
abbrev Signed := ℕ×ℕ
|
| 18 |
+
|
| 19 |
+
def value (a : Signed) : ℤ := (a.1:ℤ)-a.2
|
| 20 |
+
|
| 21 |
+
def plus (z : Signed×Signed) : Signed := (z.1.1+z.2.1,z.1.2+z.2.2)
|
| 22 |
+
def neg (a : Signed) : Signed := (a.2,a.1)
|
| 23 |
+
def minus (z : Signed×Signed) : Signed := plus (z.1,neg z.2)
|
| 24 |
+
def times (z : Signed×Signed) : Signed :=
|
| 25 |
+
(z.1.1*z.2.1+z.1.2*z.2.2,z.1.1*z.2.2+z.1.2*z.2.1)
|
| 26 |
+
def normalize (a : Signed) : Signed := (a.1-a.2,a.2-a.1)
|
| 27 |
+
|
| 28 |
+
lemma value_plus (a b : Signed) : value (plus (a,b))=value a+value b := by
|
| 29 |
+
simp only [value,plus,Nat.cast_add]
|
| 30 |
+
ring
|
| 31 |
+
|
| 32 |
+
lemma value_neg (a : Signed) : value (neg a)= -value a := by
|
| 33 |
+
unfold value neg
|
| 34 |
+
ring
|
| 35 |
+
|
| 36 |
+
lemma value_minus (a b : Signed) : value (minus (a,b))=value a-value b := by
|
| 37 |
+
rw [minus,value_plus,value_neg]
|
| 38 |
+
ring
|
| 39 |
+
|
| 40 |
+
lemma value_times (a b : Signed) : value (times (a,b))=value a*value b := by
|
| 41 |
+
simp only [value,times,Nat.cast_add,Nat.cast_mul]
|
| 42 |
+
ring
|
| 43 |
+
|
| 44 |
+
lemma normalize_eq (a : Signed) : normalize a=ForestAffineProgram.signedParts (value a) := by
|
| 45 |
+
rcases a with ⟨p,q⟩
|
| 46 |
+
apply Prod.ext <;> simp only [normalize,ForestAffineProgram.signedParts,value]
|
| 47 |
+
· omega
|
| 48 |
+
· omega
|
| 49 |
+
|
| 50 |
+
lemma value_normalize (a : Signed) : value (normalize a)=value a := by
|
| 51 |
+
rw [normalize_eq]
|
| 52 |
+
exact ForestAffineProgram.signedParts_sub (value a)
|
| 53 |
+
|
| 54 |
+
def plusRealizer : Realizer plus :=
|
| 55 |
+
pair
|
| 56 |
+
(composition (pair (composition first first) (composition second first)) add)
|
| 57 |
+
(composition (pair (composition first second) (composition second second)) add)
|
| 58 |
+
|
| 59 |
+
def negRealizer : Realizer neg := pair second first
|
| 60 |
+
|
| 61 |
+
def minusRealizer : Realizer minus :=
|
| 62 |
+
composition (pair first (composition second negRealizer)) plusRealizer
|
| 63 |
+
|
| 64 |
+
def timesRealizer : Realizer times :=
|
| 65 |
+
pair
|
| 66 |
+
(composition (pair
|
| 67 |
+
(composition (pair (composition first first) (composition second first)) multiply)
|
| 68 |
+
(composition (pair (composition first second) (composition second second)) multiply)) add)
|
| 69 |
+
(composition (pair
|
| 70 |
+
(composition (pair (composition first first) (composition second second)) multiply)
|
| 71 |
+
(composition (pair (composition first second) (composition second first)) multiply)) add)
|
| 72 |
+
|
| 73 |
+
def normalizeRealizer : Realizer normalize :=
|
| 74 |
+
pair subtract (composition (pair second first) subtract)
|
| 75 |
+
|
| 76 |
+
lemma polynomial_plus : PolynomialTime plusRealizer :=
|
| 77 |
+
polynomial_pair
|
| 78 |
+
(polynomial_composition (polynomial_pair
|
| 79 |
+
(polynomial_composition polynomial_first polynomial_first)
|
| 80 |
+
(polynomial_composition polynomial_second polynomial_first)) polynomial_add)
|
| 81 |
+
(polynomial_composition (polynomial_pair
|
| 82 |
+
(polynomial_composition polynomial_first polynomial_second)
|
| 83 |
+
(polynomial_composition polynomial_second polynomial_second)) polynomial_add)
|
| 84 |
+
|
| 85 |
+
lemma polynomial_neg : PolynomialTime negRealizer :=
|
| 86 |
+
polynomial_pair polynomial_second polynomial_first
|
| 87 |
+
|
| 88 |
+
lemma polynomial_minus : PolynomialTime minusRealizer :=
|
| 89 |
+
polynomial_composition (polynomial_pair polynomial_first
|
| 90 |
+
(polynomial_composition polynomial_second polynomial_neg)) polynomial_plus
|
| 91 |
+
|
| 92 |
+
lemma polynomial_times : PolynomialTime timesRealizer :=
|
| 93 |
+
polynomial_pair
|
| 94 |
+
(polynomial_composition (polynomial_pair
|
| 95 |
+
(polynomial_composition (polynomial_pair
|
| 96 |
+
(polynomial_composition polynomial_first polynomial_first)
|
| 97 |
+
(polynomial_composition polynomial_second polynomial_first)) polynomial_multiply)
|
| 98 |
+
(polynomial_composition (polynomial_pair
|
| 99 |
+
(polynomial_composition polynomial_first polynomial_second)
|
| 100 |
+
(polynomial_composition polynomial_second polynomial_second)) polynomial_multiply)) polynomial_add)
|
| 101 |
+
(polynomial_composition (polynomial_pair
|
| 102 |
+
(polynomial_composition (polynomial_pair
|
| 103 |
+
(polynomial_composition polynomial_first polynomial_first)
|
| 104 |
+
(polynomial_composition polynomial_second polynomial_second)) polynomial_multiply)
|
| 105 |
+
(polynomial_composition (polynomial_pair
|
| 106 |
+
(polynomial_composition polynomial_first polynomial_second)
|
| 107 |
+
(polynomial_composition polynomial_second polynomial_first)) polynomial_multiply)) polynomial_add)
|
| 108 |
+
|
| 109 |
+
lemma polynomial_normalize : PolynomialTime normalizeRealizer :=
|
| 110 |
+
polynomial_pair polynomial_subtract
|
| 111 |
+
(polynomial_composition (polynomial_pair polynomial_second polynomial_first) polynomial_subtract)
|
| 112 |
+
|
| 113 |
+
def sum (xs : List Signed) : Signed := ((xs.map Prod.fst).sum,(xs.map Prod.snd).sum)
|
| 114 |
+
|
| 115 |
+
def sumRealizer : Realizer sum :=
|
| 116 |
+
pair (composition (map first) listSum) (composition (map second) listSum)
|
| 117 |
+
|
| 118 |
+
lemma polynomial_sum : PolynomialTime sumRealizer :=
|
| 119 |
+
polynomial_pair
|
| 120 |
+
(polynomial_composition (polynomial_map polynomial_first) polynomial_listSum)
|
| 121 |
+
(polynomial_composition (polynomial_map polynomial_second) polynomial_listSum)
|
| 122 |
+
|
| 123 |
+
lemma value_sum (xs : List Signed) : value (sum xs)=(xs.map value).sum := by
|
| 124 |
+
induction xs with
|
| 125 |
+
| nil => simp [sum,value]
|
| 126 |
+
| cons a xs ih =>
|
| 127 |
+
simp only [sum,List.map_cons,List.sum_cons,value,Nat.cast_add] at *
|
| 128 |
+
rw [←ih]
|
| 129 |
+
ring
|
| 130 |
+
|
| 131 |
+
end ContingencyTables.SignedPairProgram
|
| 132 |
+
|
| 133 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/SparseViewMatrixProgram.lean
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Sampling.SmallViewLookupProgram
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Dense.DenseIntegerProgram
|
| 3 |
+
import OAI.Combinatorics.ContingencyTables.Transport.DirectedBFSCode
|
| 4 |
+
import OAI.Combinatorics.MatchingCount.Complexity.TreePolynomialGrid
|
| 5 |
+
|
| 6 |
+
namespace OAI
|
| 7 |
+
|
| 8 |
+
/-! # Materializing a sparse retained-cell view in a rectangular matrix -/
|
| 9 |
+
|
| 10 |
+
namespace ContingencyTables.SparseViewMatrixProgram
|
| 11 |
+
|
| 12 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped Polynomial ProfilePrograms DensePrograms
|
| 13 |
+
|
| 14 |
+
abbrev Input := (List ℕ × List ℕ) × (List (ℕ × ℕ) × List ℕ)
|
| 15 |
+
abbrev RowInput := Input × ℕ
|
| 16 |
+
|
| 17 |
+
def entry (z : RowInput × ℕ) : ℕ := lookupView (z.1.1.2,(z.1.2,z.2))
|
| 18 |
+
def entryRealizer : Realizer entry :=
|
| 19 |
+
composition (f := fun z : RowInput × ℕ => (z.1.1.2,(z.1.2,z.2))) (g := lookupView)
|
| 20 |
+
(pair (composition (composition first first) second)
|
| 21 |
+
(pair (composition first second) second)) lookupViewRealizer
|
| 22 |
+
lemma polynomial_entry : PolynomialTime entryRealizer :=
|
| 23 |
+
polynomial_composition (polynomial_pair
|
| 24 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first) polynomial_second)
|
| 25 |
+
(polynomial_pair (polynomial_composition polynomial_first polynomial_second) polynomial_second))
|
| 26 |
+
polynomial_lookupView
|
| 27 |
+
|
| 28 |
+
def columns (z : RowInput) : List ℕ := List.range z.1.1.2.length
|
| 29 |
+
def columnsRealizer : Realizer columns :=
|
| 30 |
+
composition (composition (composition (composition first first) second) listLength) range
|
| 31 |
+
lemma polynomial_columns : PolynomialTime columnsRealizer := by
|
| 32 |
+
apply polynomial_boundedRange (polynomial_composition
|
| 33 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 34 |
+
polynomial_second) polynomial_listLength) X
|
| 35 |
+
intro z
|
| 36 |
+
simpa only [Function.comp_apply,eval_X] using (list_length_le_weight z.1.1.2).trans
|
| 37 |
+
((weight_snd z.1.1).trans ((weight_fst z.1).trans (weight_fst z)))
|
| 38 |
+
|
| 39 |
+
def row (z : RowInput) : List ℕ := (columns z).map (fun j => entry (z,j))
|
| 40 |
+
def rowRealizer : Realizer row :=
|
| 41 |
+
composition (pair identity columnsRealizer) (mapWith entryRealizer)
|
| 42 |
+
lemma polynomial_row : PolynomialTime rowRealizer :=
|
| 43 |
+
polynomial_composition (polynomial_pair polynomial_identity polynomial_columns)
|
| 44 |
+
(polynomial_mapWith polynomial_entry)
|
| 45 |
+
|
| 46 |
+
def rowIndices (z : Input) : List ℕ := List.range z.1.1.length
|
| 47 |
+
def rowIndicesRealizer : Realizer rowIndices :=
|
| 48 |
+
composition (composition (composition first first) listLength) range
|
| 49 |
+
lemma polynomial_rowIndices : PolynomialTime rowIndicesRealizer := by
|
| 50 |
+
apply polynomial_boundedRange (polynomial_composition
|
| 51 |
+
(polynomial_composition polynomial_first polynomial_first) polynomial_listLength) X
|
| 52 |
+
intro z
|
| 53 |
+
simpa only [Function.comp_apply,eval_X] using (list_length_le_weight z.1.1).trans
|
| 54 |
+
((weight_fst z.1).trans (weight_fst z))
|
| 55 |
+
|
| 56 |
+
def matrix (z : Input) : MatrixCode := (rowIndices z).map (fun i => row (z,i))
|
| 57 |
+
def matrixRealizer : Realizer matrix :=
|
| 58 |
+
composition (pair identity rowIndicesRealizer) (mapWith rowRealizer)
|
| 59 |
+
theorem polynomial_matrix : PolynomialTime matrixRealizer :=
|
| 60 |
+
polynomial_composition (polynomial_pair polynomial_identity polynomial_rowIndices)
|
| 61 |
+
(polynomial_mapWith polynomial_row)
|
| 62 |
+
|
| 63 |
+
theorem matrix_ofFn {m n : ℕ} (r : Fin m → ℕ) (c : Fin n → ℕ)
|
| 64 |
+
(catalog : List (ℕ × ℕ)) (values : List ℕ) :
|
| 65 |
+
matrix ((List.ofFn r,List.ofFn c),(catalog,values))=
|
| 66 |
+
matrixCode (fun i : Fin m => fun j : Fin n =>
|
| 67 |
+
lookupView ((catalog,values),(i.val,j.val))) := by
|
| 68 |
+
simp only [matrix,rowIndices,row,columns,List.length_ofFn,DirectedBFSCode.range_eq_map,
|
| 69 |
+
←List.ofFn_eq_map,List.map_ofFn,Function.comp_def,entry,matrixCode]
|
| 70 |
+
|
| 71 |
+
end ContingencyTables.SparseViewMatrixProgram
|
| 72 |
+
|
| 73 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/TableIndexProgram.lean
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Dense.DenseTableCoordinates
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Dense.GridIndexProgram
|
| 3 |
+
import OAI.Combinatorics.ContingencyTables.Transport.IndexedTableLaw
|
| 4 |
+
|
| 5 |
+
namespace OAI
|
| 6 |
+
|
| 7 |
+
/-! # Computing the position of a table in the completion enumeration -/
|
| 8 |
+
|
| 9 |
+
namespace ContingencyTables.DensePrograms
|
| 10 |
+
open MatchingFPRAS MatchingFPRAS.TreeTyped
|
| 11 |
+
open scoped BigOperators Classical
|
| 12 |
+
|
| 13 |
+
def tableFreeCoordinates (z : ExponentData) : List ℕ :=
|
| 14 |
+
(tableFreeCells z).map (fun ij => tableFreeValue (z,ij))
|
| 15 |
+
|
| 16 |
+
def tableFreeCoordinatesRealizer : Realizer tableFreeCoordinates :=
|
| 17 |
+
composition (pair identity tableFreeCellsRealizer) (mapWith tableFreeValueRealizer)
|
| 18 |
+
|
| 19 |
+
lemma polynomial_tableFreeCoordinates : PolynomialTime tableFreeCoordinatesRealizer :=
|
| 20 |
+
polynomial_composition (polynomial_pair polynomial_identity polynomial_tableFreeCells)
|
| 21 |
+
(polynomial_mapWith polynomial_tableFreeValue)
|
| 22 |
+
|
| 23 |
+
lemma tableFreeCoordinates_code {m n : ℕ} (R : Option (Fin m) → ℕ)
|
| 24 |
+
(P : Option (Fin n) → ℕ) (d : ℕ) (X : Table R P) :
|
| 25 |
+
tableFreeCoordinates (tableCoordinatesCode R P d X)=
|
| 26 |
+
List.ofFn (fun k : Fin (m*n) =>
|
| 27 |
+
X.val (some (finProdFinEquiv.symm k).1) (some (finProdFinEquiv.symm k).2)) := by
|
| 28 |
+
rw [tableFreeCoordinates,tableFreeCells_code,List.map_ofFn]
|
| 29 |
+
apply congrArg List.ofFn
|
| 30 |
+
funext k
|
| 31 |
+
exact tableFreeValue_code R P d X _ _
|
| 32 |
+
|
| 33 |
+
def tableIndexInput (z : CompletionData) : GridIndexData :=
|
| 34 |
+
(tableFreeCoordinates (z,0),z.1.1.sum+1)
|
| 35 |
+
|
| 36 |
+
def tableIndexInputRealizer : Realizer tableIndexInput :=
|
| 37 |
+
pair (composition (pair identity (constant 0)) tableFreeCoordinatesRealizer)
|
| 38 |
+
(composition (composition (composition first first) listSum) successor)
|
| 39 |
+
|
| 40 |
+
lemma polynomial_tableIndexInput : PolynomialTime tableIndexInputRealizer :=
|
| 41 |
+
polynomial_pair (polynomial_composition (polynomial_pair polynomial_identity
|
| 42 |
+
(polynomial_constant 0)) polynomial_tableFreeCoordinates)
|
| 43 |
+
(polynomial_composition (polynomial_composition
|
| 44 |
+
(polynomial_composition polynomial_first polynomial_first) polynomial_listSum)
|
| 45 |
+
polynomial_successor)
|
| 46 |
+
|
| 47 |
+
def tableIndex (z : CompletionData) : ℕ := gridIndex (tableIndexInput z)
|
| 48 |
+
|
| 49 |
+
def tableIndexRealizer : Realizer tableIndex :=
|
| 50 |
+
composition tableIndexInputRealizer gridIndexRealizer
|
| 51 |
+
|
| 52 |
+
theorem polynomial_tableIndex : PolynomialTime tableIndexRealizer :=
|
| 53 |
+
polynomial_composition polynomial_tableIndexInput polynomial_gridIndex
|
| 54 |
+
|
| 55 |
+
theorem tableIndex_actual {m n : ℕ} (R : Option (Fin m) → ℕ)
|
| 56 |
+
(P : Option (Fin n) → ℕ) (X : Table R P) :
|
| 57 |
+
tableIndex ((marginCode R,marginCode P),tableOutput X)=
|
| 58 |
+
(IndexedTableLaw.index R P (le_refl (∑ i,R i)) X).val := by
|
| 59 |
+
unfold tableIndex tableIndexInput
|
| 60 |
+
change gridIndex (tableFreeCoordinates (tableCoordinatesCode R P 0 X),
|
| 61 |
+
(marginCode R).sum+1)=_
|
| 62 |
+
rw [tableFreeCoordinates_code]
|
| 63 |
+
have hsum : (marginCode R).sum=∑ i,R i := by
|
| 64 |
+
simp only [marginCode,List.sum_cons,List.sum_ofFn,Fintype.sum_option]
|
| 65 |
+
rw [hsum]
|
| 66 |
+
change gridIndex (List.ofFn (fun k =>
|
| 67 |
+
(BoxCompletion.encode R P (le_refl (∑ i,R i)) X k).val),(∑ i,R i)+1)=_
|
| 68 |
+
rw [gridIndex_ofFn]
|
| 69 |
+
rfl
|
| 70 |
+
|
| 71 |
+
end ContingencyTables.DensePrograms
|
| 72 |
+
|
| 73 |
+
end OAI
|
lean/OAI/Combinatorics/ContingencyTables/Machines/TightenedInputProgram.lean
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import OAI.Combinatorics.ContingencyTables.Machines.EndpointMatrixProgram
|
| 2 |
+
import OAI.Combinatorics.ContingencyTables.Transport.ComputedTableTightening
|
| 3 |
+
import OAI.Combinatorics.ContingencyTables.Machines.NetworkTableProgram
|
| 4 |
+
|
| 5 |
+
namespace OAI
|
| 6 |
+
|
| 7 |
+
/-! # Executed endpoint translation and a feasible tightened baseline
|
| 8 |
+
|
| 9 |
+
The lower and upper matrices are computed by the endpoint scans. Binary
|
| 10 |
+
subtraction updates the capacities and both margins, then the same proved
|
| 11 |
+
flow program produces a baseline table in the translated instance.
|
| 12 |
+
-/
|
| 13 |
+
|
| 14 |
+
namespace ContingencyTables.TightenedInputProgram
|
| 15 |
+
|
| 16 |
+
open MatchingFPRAS.TreeTyped Polynomial ResidualGraphProgram NetworkVertexCode
|
| 17 |
+
open scoped BigOperators Classical
|
| 18 |
+
|
| 19 |
+
abbrev TranslationInput := Input×NatMatrix
|
| 20 |
+
abbrev MarginInput := TranslationInput×ℕ
|
| 21 |
+
abbrev CellInput := MarginInput×ℕ
|
| 22 |
+
|
| 23 |
+
def rowValue (z : MarginInput) : ℕ :=
|
| 24 |
+
z.1.1.1.1.getD z.2 0-(z.1.1.2.getD z.2 []).sum
|
| 25 |
+
def rowValueRealizer : Realizer rowValue :=
|
| 26 |
+
composition (pair
|
| 27 |
+
(composition (pair (composition (composition (composition first first) first) first) second)
|
| 28 |
+
(getDFast 0))
|
| 29 |
+
(composition (composition (pair (composition (composition first first) second) second)
|
| 30 |
+
(getDFast [])) listSum)) subtract
|
| 31 |
+
lemma polynomial_rowValue : PolynomialTime rowValueRealizer :=
|
| 32 |
+
polynomial_composition (polynomial_pair
|
| 33 |
+
(polynomial_composition (polynomial_pair
|
| 34 |
+
(polynomial_composition (polynomial_composition (polynomial_composition
|
| 35 |
+
polynomial_first polynomial_first) polynomial_first) polynomial_first)
|
| 36 |
+
polynomial_second) (polynomial_getDFast 0))
|
| 37 |
+
(polynomial_composition (polynomial_composition (polynomial_pair
|
| 38 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first)
|
| 39 |
+
polynomial_second) polynomial_second) (polynomial_getDFast [])) polynomial_listSum))
|
| 40 |
+
polynomial_subtract
|
| 41 |
+
|
| 42 |
+
def columnValue (z : MarginInput) : ℕ :=
|
| 43 |
+
z.1.1.1.2.getD z.2 0-(BoundedComplementProgram.columnValues (z.1.1,z.2)).sum
|
| 44 |
+
def columnValueRealizer : Realizer columnValue :=
|
| 45 |
+
composition (pair
|
| 46 |
+
(composition (pair (composition (composition (composition first first) first) second) second)
|
| 47 |
+
(getDFast 0))
|
| 48 |
+
(composition (composition
|
| 49 |
+
(pair (composition first first) second) BoundedComplementProgram.columnValuesRealizer)
|
| 50 |
+
listSum)) subtract
|
| 51 |
+
lemma polynomial_columnValue : PolynomialTime columnValueRealizer :=
|
| 52 |
+
polynomial_composition (polynomial_pair
|
| 53 |
+
(polynomial_composition (polynomial_pair
|
| 54 |
+
(polynomial_composition (polynomial_composition (polynomial_composition
|
| 55 |
+
polynomial_first polynomial_first) polynomial_first) polynomial_second)
|
| 56 |
+
polynomial_second) (polynomial_getDFast 0))
|
| 57 |
+
(polynomial_composition (polynomial_composition
|
| 58 |
+
(polynomial_pair (polynomial_composition polynomial_first polynomial_first) polynomial_second)
|
| 59 |
+
BoundedComplementProgram.polynomial_columnValues) polynomial_listSum)) polynomial_subtract
|
| 60 |
+
|
| 61 |
+
def position (z : CellInput) : ℕ×ℕ := (z.1.2,z.2)
|
| 62 |
+
def positionRealizer : Realizer position := pair (composition first second) second
|
| 63 |
+
lemma polynomial_position : PolynomialTime positionRealizer :=
|
| 64 |
+
polynomial_pair (polynomial_composition polynomial_first polynomial_second) polynomial_second
|
| 65 |
+
|
| 66 |
+
def cellValue (z : CellInput) : ℕ :=
|
| 67 |
+
capacityEntry (z.1.1.2,position z)-capacityEntry (z.1.1.1.2,position z)
|
| 68 |
+
def cellValueRealizer : Realizer cellValue :=
|
| 69 |
+
composition (pair
|
| 70 |
+
(composition (pair (composition (composition first first) second) positionRealizer)
|
| 71 |
+
capacityEntryRealizer)
|
| 72 |
+
(composition (pair (composition (composition (composition first first) first) second)
|
| 73 |
+
positionRealizer) capacityEntryRealizer)) subtract
|
| 74 |
+
lemma polynomial_cellValue : PolynomialTime cellValueRealizer :=
|
| 75 |
+
polynomial_composition (polynomial_pair
|
| 76 |
+
(polynomial_composition (polynomial_pair
|
| 77 |
+
(polynomial_composition (polynomial_composition polynomial_first polynomial_first) polynomial_second)
|
| 78 |
+
polynomial_position) polynomial_capacityEntry)
|
| 79 |
+
(polynomial_composition (polynomial_pair
|
| 80 |
+
(polynomial_composition (polynomial_composition (polynomial_composition
|
| 81 |
+
polynomial_first polynomial_first) polynomial_first) polynomial_second)
|
| 82 |
+
polynomial_position) polynomial_capacityEntry)) polynomial_subtract
|
| 83 |
+
|
| 84 |
+
def rowIndices (z : TranslationInput) : List ℕ := BoundedComplementProgram.rowIndices z.1
|
| 85 |
+
def rowIndicesRealizer : Realizer rowIndices :=
|
| 86 |
+
composition first BoundedComplementProgram.rowIndicesRealizer
|
| 87 |
+
lemma polynomial_rowIndices : PolynomialTime rowIndicesRealizer :=
|
| 88 |
+
polynomial_composition polynomial_first BoundedComplementProgram.polynomial_rowIndices
|
| 89 |
+
|
| 90 |
+
def columnIndices (z : TranslationInput) : List ℕ := BoundedComplementProgram.columnIndices z.1
|
| 91 |
+
def columnIndicesRealizer : Realizer columnIndices :=
|
| 92 |
+
composition first BoundedComplementProgram.columnIndicesRealizer
|
| 93 |
+
lemma polynomial_columnIndices : PolynomialTime columnIndicesRealizer :=
|
| 94 |
+
polynomial_composition polynomial_first BoundedComplementProgram.polynomial_columnIndices
|
| 95 |
+
|
| 96 |
+
def capacityRow (z : MarginInput) : List ℕ :=
|
| 97 |
+
(columnIndices z.1).map (fun j=>cellValue (z,j))
|
| 98 |
+
def capacityRowRealizer : Realizer capacityRow :=
|
| 99 |
+
composition (pair identity (composition first columnIndicesRealizer)) (mapWith cellValueRealizer)
|
| 100 |
+
lemma polynomial_capacityRow : PolynomialTime capacityRowRealizer :=
|
| 101 |
+
polynomial_composition (polynomial_pair polynomial_identity
|
| 102 |
+
(polynomial_composition polynomial_first polynomial_columnIndices))
|
| 103 |
+
(polynomial_mapWith polynomial_cellValue)
|
| 104 |
+
|
| 105 |
+
def translate (z : TranslationInput) : Input :=
|
| 106 |
+
(((rowIndices z).map (fun i=>rowValue (z,i)),
|
| 107 |
+
(columnIndices z).map (fun j=>columnValue (z,j))),
|
| 108 |
+
(rowIndices z).map (fun i=>capacityRow (z,i)))
|
| 109 |
+
def translateRealizer : Realizer translate :=
|
| 110 |
+
pair (pair
|
| 111 |
+
(composition (pair identity rowIndicesRealizer) (mapWith rowValueRealizer))
|
| 112 |
+
(composition (pair identity columnIndicesRealizer) (mapWith columnValueRealizer)))
|
| 113 |
+
(composition (pair identity rowIndicesRealizer) (mapWith capacityRowRealizer))
|
| 114 |
+
lemma polynomial_translate : PolynomialTime translateRealizer :=
|
| 115 |
+
polynomial_pair (polynomial_pair
|
| 116 |
+
(polynomial_composition (polynomial_pair polynomial_identity polynomial_rowIndices)
|
| 117 |
+
(polynomial_mapWith polynomial_rowValue))
|
| 118 |
+
(polynomial_composition (polynomial_pair polynomial_identity polynomial_columnIndices)
|
| 119 |
+
(polynomial_mapWith polynomial_columnValue)))
|
| 120 |
+
(polynomial_composition (polynomial_pair polynomial_identity polynomial_rowIndices)
|
| 121 |
+
(polynomial_mapWith polynomial_capacityRow))
|
| 122 |
+
|
| 123 |
+
lemma rowValue_code {m n : ℕ} (r : Fin m→ℕ) (c : Fin n→ℕ) (l u : Fin m→Fin n→ℕ) (i : Fin m) :
|
| 124 |
+
rowValue ((data r c l,natCode u),i.val)=TableTightening.rows r l i := by
|
| 125 |
+
simp only [rowValue,data,natCode,List.getD_eq_getElem?_getD,List.getElem?_ofFn,
|
| 126 |
+
i.isLt,dite_true,Option.getD_some,Fin.eta,List.sum_ofFn,TableTightening.rows]
|
| 127 |
+
|
| 128 |
+
lemma columnValue_code {m n : ℕ} (r : Fin m→ℕ) (c : Fin n→ℕ) (l u : Fin m→Fin n→ℕ) (j : Fin n) :
|
| 129 |
+
columnValue ((data r c l,natCode u),j.val)=TableTightening.columns c l j := by
|
| 130 |
+
simp only [columnValue,BoundedComplementProgram.columnValues,data,natCode,List.map_ofFn,
|
| 131 |
+
Function.comp_def,List.getD_eq_getElem?_getD,List.getElem?_ofFn,
|
| 132 |
+
j.isLt,dite_true,Option.getD_some,Fin.eta,List.sum_ofFn,TableTightening.columns]
|
| 133 |
+
|
| 134 |
+
lemma cellValue_code {m n : ℕ} (r : Fin m→ℕ) (c : Fin n→ℕ) (l u : Fin m→Fin n→ℕ)
|
| 135 |
+
(i : Fin m) (j : Fin n) :
|
| 136 |
+
cellValue (((data r c l,natCode u),i.val),j.val)=TableTightening.bounds l u i j := by
|
| 137 |
+
change capacityEntry (natCode u,(i.val,j.val))-capacityEntry (natCode l,(i.val,j.val))=_
|
| 138 |
+
rw [capacityEntry_code,capacityEntry_code]
|
| 139 |
+
rfl
|
| 140 |
+
|
| 141 |
+
lemma translate_code {m n : ℕ} (r : Fin m→ℕ) (c : Fin n→ℕ) (l u : Fin m→Fin n→ℕ) :
|
| 142 |
+
translate (data r c l,natCode u)=data (TableTightening.rows r l)
|
| 143 |
+
(TableTightening.columns c l) (TableTightening.bounds l u) := by
|
| 144 |
+
unfold translate
|
| 145 |
+
apply Prod.ext
|
| 146 |
+
· apply Prod.ext
|
| 147 |
+
· simp only [rowIndices,BoundedComplementProgram.rowIndices,data,List.length_ofFn,
|
| 148 |
+
DirectedBFSCode.range_eq_map,←List.ofFn_eq_map,List.map_ofFn]
|
| 149 |
+
apply congrArg List.ofFn
|
| 150 |
+
funext i
|
| 151 |
+
exact rowValue_code r c l u i
|
| 152 |
+
· simp only [columnIndices,BoundedComplementProgram.columnIndices,data,List.length_ofFn,
|
| 153 |
+
DirectedBFSCode.range_eq_map,←List.ofFn_eq_map,List.map_ofFn]
|
| 154 |
+
apply congrArg List.ofFn
|
| 155 |
+
funext j
|
| 156 |
+
exact columnValue_code r c l u j
|
| 157 |
+
· simp only [rowIndices,BoundedComplementProgram.rowIndices,data,List.length_ofFn,
|
| 158 |
+
DirectedBFSCode.range_eq_map,←List.ofFn_eq_map,List.map_ofFn,natCode]
|
| 159 |
+
apply congrArg List.ofFn
|
| 160 |
+
funext i
|
| 161 |
+
dsimp only [Function.comp_apply]
|
| 162 |
+
simp only [capacityRow,columnIndices,BoundedComplementProgram.columnIndices,List.length_ofFn,
|
| 163 |
+
DirectedBFSCode.range_eq_map,←List.ofFn_eq_map,List.map_ofFn]
|
| 164 |
+
apply congrArg List.ofFn
|
| 165 |
+
funext j
|
| 166 |
+
exact cellValue_code r c l u i j
|
| 167 |
+
|
| 168 |
+
def prepare (z : Input) : TranslationInput :=
|
| 169 |
+
((z.1,EndpointMatrixProgram.lowerMatrix z),EndpointMatrixProgram.upperMatrix z)
|
| 170 |
+
def prepareRealizer : Realizer prepare :=
|
| 171 |
+
pair (pair first EndpointMatrixProgram.lowerMatrixRealizer) EndpointMatrixProgram.upperMatrixRealizer
|
| 172 |
+
lemma polynomial_prepare : PolynomialTime prepareRealizer :=
|
| 173 |
+
polynomial_pair (polynomial_pair polynomial_first EndpointMatrixProgram.polynomial_lowerMatrix)
|
| 174 |
+
EndpointMatrixProgram.polynomial_upperMatrix
|
| 175 |
+
|
| 176 |
+
def compute (z : Input) : Input := translate (prepare z)
|
| 177 |
+
def computeRealizer : Realizer compute :=
|
| 178 |
+
composition (f:=prepare) (g:=translate) prepareRealizer translateRealizer
|
| 179 |
+
lemma polynomial_compute : PolynomialTime computeRealizer :=
|
| 180 |
+
polynomial_composition polynomial_prepare polynomial_translate
|
| 181 |
+
|
| 182 |
+
lemma compute_code {m n : ℕ} (r : Fin m→ℕ) (c : Fin n→ℕ) (b : Fin m→Fin n→ℕ) :
|
| 183 |
+
compute (data r c b)=data (ComputedTableTightening.newRows r c b)
|
| 184 |
+
(ComputedTableTightening.newColumns r c b) (ComputedTableTightening.newBounds r c b) := by
|
| 185 |
+
unfold compute prepare
|
| 186 |
+
rw [EndpointMatrixProgram.lowerMatrix_code,EndpointMatrixProgram.upperMatrix_code]
|
| 187 |
+
exact translate_code r c (ComputedTableTightening.lower r c b) (ComputedTableTightening.upper r c b)
|
| 188 |
+
|
| 189 |
+
def baseline (z : Input) : NatMatrix := NetworkTableProgram.output (compute z)
|
| 190 |
+
def baselineRealizer : Realizer baseline :=
|
| 191 |
+
composition (f:=compute) (g:=NetworkTableProgram.output) computeRealizer NetworkTableProgram.outputRealizer
|
| 192 |
+
lemma polynomial_baseline : PolynomialTime baselineRealizer :=
|
| 193 |
+
polynomial_composition polynomial_compute NetworkTableProgram.polynomial_output
|
| 194 |
+
|
| 195 |
+
/-- Endpoint translation preserves a concrete feasible witness. -/
|
| 196 |
+
theorem translated_nonempty {m n : ℕ} (hEK : PublishedEdmondsKarp.EdmondsKarp1972Theorem1)
|
| 197 |
+
(r : Fin m→ℕ) (c : Fin n→ℕ) (b : Fin m→Fin n→ℕ)
|
| 198 |
+
(hne : ∃ X,TableTightening.Feasible r c b X) :
|
| 199 |
+
∃ Y,HasMargins Y (ComputedTableTightening.newRows r c b)
|
| 200 |
+
(ComputedTableTightening.newColumns r c b) ∧
|
| 201 |
+
∀ i j,Y i j≤ComputedTableTightening.newBounds r c b i j := by
|
| 202 |
+
obtain ⟨X,hX⟩ := hne
|
| 203 |
+
let x : BoundedTable r c b := ⟨⟨X,hX.1⟩,hX.2⟩
|
| 204 |
+
let y := ComputedTableTightening.tighten hEK r c b ⟨X,hX⟩ x
|
| 205 |
+
exact ⟨y.val.val,y.val.property,y.property⟩
|
| 206 |
+
|
| 207 |
+
/-- The stored baseline comes from the executed flow decoder, with all
|
| 208 |
+
its margin and capacity obligations discharged by endpoint translation. -/
|
| 209 |
+
theorem baseline_correct {m n : ℕ} (hEK : PublishedEdmondsKarp.EdmondsKarp1972Theorem1)
|
| 210 |
+
(r : Fin m→ℕ) (c : Fin n→ℕ) (b : Fin m→Fin n→ℕ)
|
| 211 |
+
(hne : ∃ X,TableTightening.Feasible r c b X) :
|
| 212 |
+
∃ Y,baseline (data r c b)=natCode Y ∧
|
| 213 |
+
HasMargins Y (ComputedTableTightening.newRows r c b)
|
| 214 |
+
(ComputedTableTightening.newColumns r c b) ∧
|
| 215 |
+
∀ i j,Y i j≤ComputedTableTightening.newBounds r c b i j := by
|
| 216 |
+
have ht := (NetworkFeasibilityProgram.test_correct hEK
|
| 217 |
+
(ComputedTableTightening.newRows r c b)
|
| 218 |
+
(ComputedTableTightening.newColumns r c b)
|
| 219 |
+
(ComputedTableTightening.newBounds r c b)).mpr (translated_nonempty hEK r c b hne)
|
| 220 |
+
have h := NetworkTableProgram.output_correct hEK
|
| 221 |
+
(ComputedTableTightening.newRows r c b)
|
| 222 |
+
(ComputedTableTightening.newColumns r c b)
|
| 223 |
+
(ComputedTableTightening.newBounds r c b) ht
|
| 224 |
+
simpa only [baseline,compute_code] using h
|
| 225 |
+
|
| 226 |
+
end ContingencyTables.TightenedInputProgram
|
| 227 |
+
|
| 228 |
+
end OAI
|