File size: 5,293 Bytes
5d5048e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | import OAI.Combinatorics.LowSpaceSubsetSum.Representation.RepresentationJoinLedger
import OAI.Combinatorics.LowSpaceSubsetSum.Streams.PrefixTaskCount
namespace OAI
/-! # Counted depth-first prefix execution
Only the current collector result survives until its overflow test or checker.
On overflow the result is discarded before entering either child. The full
cost tree is an analysis object; execution skips the second child after YES.
Collector and checker implementations carry their own internal costs.
-/
namespace LowSpaceSubsetSum.CountedPrefixSearch
variable {n : ℕ}
def run (collect : MaskRestriction n → RecordedDictionary.State (Entry (Fin n)) × ℕ)
(check : Array (Entry (Fin n)) → Bool × ℕ) (H : ℕ) :
List (Fin n) → MaskRestriction n → Bool × ℕ
| order, P =>
let task := collect P
if task.1.keys.overflow then
match order with
| [] => (false, task.2 + H)
| i :: rest =>
let left := run collect check H rest (P.force i false)
if left.1 then (true, task.2 + left.2 + H) else
let right := run collect check H rest (P.force i true)
(right.1, task.2 + left.2 + right.2 + H)
else
let checked := check task.1.records
(checked.1, task.2 + checked.2 + H)
/-- The hypothetical full tree includes a sibling skipped after early YES. -/
def fullCost (collect : MaskRestriction n → RecordedDictionary.State (Entry (Fin n)) × ℕ)
(check : Array (Entry (Fin n)) → Bool × ℕ) (H : ℕ) :
List (Fin n) → MaskRestriction n → ℕ
| order, P =>
let task := collect P
if task.1.keys.overflow then
match order with
| [] => task.2 + H
| i :: rest => task.2 + fullCost collect check H rest (P.force i false) +
fullCost collect check H rest (P.force i true) + H
else task.2 + (check task.1.records).2 + H
theorem run_value (D : RepresentationParentData n) (B : ℕ)
(collect : MaskRestriction n → RecordedDictionary.State (Entry (Fin n)) × ℕ)
(hcollect : ∀ P, (collect P).1 = ({ D with maskRestriction := P }.collect B).state.user)
(check : Array (Entry (Fin n)) → Bool × ℕ) (H : ℕ)
(order : List (Fin n)) (P : MaskRestriction n) :
(run collect check H order P).1 = D.searchRight B (fun a => (check a).1) order P := by
induction order generalizing P with
| nil =>
simp only [run, RepresentationParentData.searchRight, hcollect]
split <;> rfl
| cons i order ih =>
simp only [run, RepresentationParentData.searchRight, hcollect]
split
· simp only [ih]
split <;> rfl
· rfl
theorem run_work
(collect : MaskRestriction n → RecordedDictionary.State (Entry (Fin n)) × ℕ)
(check : Array (Entry (Fin n)) → Bool × ℕ) (H : ℕ)
(order : List (Fin n)) (P : MaskRestriction n) :
(run collect check H order P).2 ≤ fullCost collect check H order P := by
induction order generalizing P with
| nil => simp only [run, fullCost]; split <;> rfl
| cons i order ih =>
have hl := ih (P.force i false)
have hr := ih (P.force i true)
simp only [run, fullCost]
split
· split <;> dsimp only <;> omega
· rfl
/-- Separate task setup, join counters and completed checker calls. This
composes concrete task cost proofs without revisiting the exponential tree. -/
theorem fullCost_le_ledger (D : RepresentationParentData n) (B : ℕ)
(collect : MaskRestriction n → RecordedDictionary.State (Entry (Fin n)) × ℕ)
(hcollect : ∀ P, (collect P).1 = ({ D with maskRestriction := P }.collect B).state.user)
(check : Array (Entry (Fin n)) → Bool × ℕ) (H A M C K : ℕ)
(hwork : ∀ P, (collect P).2 ≤ A * (D.capacity + 1) +
M * (({ D with maskRestriction := P }).localLedger B).queries +
C * (({ D with maskRestriction := P }).localLedger B).reports +
K * (({ D with maskRestriction := P }).localLedger B).copies)
(order : List (Fin n)) (P : MaskRestriction n) :
fullCost collect check H order P ≤
(A * (D.capacity + 1) + H) * D.taskStarts B order P +
M * (D.prefixLedger B order P).queries +
C * (D.prefixLedger B order P).reports +
K * (D.prefixLedger B order P).copies +
((D.completedArrays B order P).map (fun a => (check a).2)).sum := by
induction order generalizing P with
| nil =>
have hw := hwork P
simp only [fullCost, hcollect, RepresentationParentData.taskStarts,
RepresentationParentData.prefixLedger, RepresentationParentData.completedArrays]
split <;> simp only [List.map_nil, List.sum_nil, List.map_cons, List.sum_cons,
Nat.add_zero, Nat.mul_one]
· omega
· omega
| cons i order ih =>
have hw := hwork P
have h₀ := ih (P.force i false)
have h₁ := ih (P.force i true)
simp only [fullCost, hcollect, RepresentationParentData.taskStarts,
RepresentationParentData.prefixLedger, RepresentationParentData.completedArrays]
split
· simp only [CompressionLedger.add, Nat.mul_add, Nat.mul_one, List.map_append, List.sum_append]
simp only [Nat.mul_add, Nat.mul_one] at hw h₀ h₁
omega
· simp only [Nat.mul_one, List.map_cons, List.map_nil, List.sum_cons, List.sum_nil, Nat.add_zero]
omega
end LowSpaceSubsetSum.CountedPrefixSearch
end OAI
|