math / data /lean /OAI /Combinatorics /LowSpaceSubsetSum /Search /BudgetedResidueLoop.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
7.38 kB
import OAI.Combinatorics.LowSpaceSubsetSum.Separators.BudgetedSeparatorFlow
namespace OAI
/-! # Tail-recursive residue enumeration with one shared budget
The loop keeps a residue counter and releases each completed bucket before
starting its successor. The work accumulator avoids return frames.
-/
namespace LowSpaceSubsetSum.BudgetedResidueLoop
open BudgetedSeparatorFlow
def reference (bucket : β„• β†’ Bool) : β„• β†’ β„• β†’ Bool
| _, 0 => false
| z, remaining + 1 => if bucket z then true else reference bucket (z + 1) remaining
def loop (bucket : β„• β†’ β„• β†’ Option (Bool Γ— β„•) Γ— β„•) :
β„• β†’ β„• β†’ β„• β†’ β„• β†’ Option (Bool Γ— β„•) Γ— β„•
| z, remaining, budget, work =>
if 4 ≀ budget then
match remaining with
| 0 => (some (false, budget - 4), work + 1)
| remaining + 1 =>
let out := bucket z (budget - 4)
match out.1 with
| none => (none, work + out.2 + 2)
| some (true, left) => (some (true, left), work + out.2 + 3)
| some (false, left) => loop bucket (z + 1) remaining left (work + out.2 + 3)
else (none, work + 1)
def run (bucket : β„• β†’ β„• β†’ Option (Bool Γ— β„•) Γ— β„•) (z remaining budget : β„•) :=
loop bucket z remaining budget 0
def allowance (bill : β„• β†’ β„•) : β„• β†’ β„• β†’ β„•
| _, 0 => 4
| z, remaining + 1 => 4 + bill z + allowance bill (z + 1) remaining
theorem allowance_eq (bill : β„• β†’ β„•) (z count : β„•) :
allowance bill z count = 4 * (count + 1) +
((List.range count).map (fun i => bill (z + i))).sum := by
induction count generalizing z with
| zero => simp [allowance]
| succ count ih =>
rw [allowance, ih, List.sum_range_succ']
simp only [Nat.add_zero, Nat.succ_eq_add_one]
have he : (fun i => bill (z + (i + 1))) = (fun i => bill (z + 1 + i)) := by
funext i
congr 1
omega
rw [he]
omega
theorem loop_account (bucket : β„• β†’ β„• β†’ Option (Bool Γ— β„•) Γ— β„•)
(hb : βˆ€ z budget, Account budget (bucket z budget))
(z remaining budget work : β„•) :
(loop bucket z remaining budget work).2 ≀ work + budget + 1 ∧
βˆ€ v left, (loop bucket z remaining budget work).1 = some (v, left) β†’
left ≀ budget ∧ (loop bucket z remaining budget work).2 ≀ work + (budget - left) := by
induction remaining generalizing z budget work with
| zero =>
rw [loop]
split
next hbudget =>
constructor
Β· dsimp only; omega
Β· intro v left h
cases h
dsimp only
omega
next hbudget =>
constructor
Β· dsimp only; omega
Β· intro v left h
contradiction
| succ remaining ih =>
rw [loop]
split
next hbudget =>
dsimp only
generalize he : bucket z (budget - 4) = out
have ho := hb z (budget - 4)
rw [he] at ho
have hw := ho.1
cases hv : out.1 with
| none =>
dsimp only
constructor
Β· omega
Β· intro v left h
contradiction
| some p =>
rcases p with ⟨v, left⟩
have hs := ho.2 v left hv
cases v
Β· dsimp only
have hi := ih (z + 1) left (work + out.2 + 3)
constructor
Β· omega
Β· intro v rest h
have hh := hi.2 v rest h
omega
Β· dsimp only
constructor
Β· omega
Β· intro v rest h
cases h
omega
next hbudget =>
constructor
Β· dsimp only; omega
Β· intro v left h
contradiction
theorem account (bucket : β„• β†’ β„• β†’ Option (Bool Γ— β„•) Γ— β„•)
(hb : βˆ€ z budget, Account budget (bucket z budget)) (z remaining budget : β„•) :
Account budget (run bucket z remaining budget) := by
simpa only [run, Account, Nat.zero_add] using loop_account bucket hb z remaining budget 0
theorem value (bucket : β„• β†’ β„• β†’ Option (Bool Γ— β„•) Γ— β„•) (pureBucket : β„• β†’ Bool)
(hv : βˆ€ z budget v left, (bucket z budget).1 = some (v, left) β†’ v = pureBucket z)
(z remaining budget work : β„•) (v : Bool) (left : β„•)
(h : (loop bucket z remaining budget work).1 = some (v, left)) :
v = reference pureBucket z remaining := by
induction remaining generalizing z budget work v left with
| zero =>
rw [loop] at h
split at h
Β· cases h; rfl
Β· contradiction
| succ remaining ih =>
rw [loop] at h
split at h
next hbudget =>
cases he : (bucket z (budget - 4)).1 with
| none => simp only [he] at h; contradiction
| some p =>
rcases p with ⟨v', rest⟩
have heq := hv z (budget - 4) v' rest he
cases v'
Β· simp only [he] at h
have hi := ih (z + 1) rest (work + (bucket z (budget - 4)).2 + 3) v left h
simpa only [reference, ← heq, Bool.false_eq_true, ↓reduceIte] using hi
Β· simp only [he] at h
cases h
simp only [reference, ← heq, ↓reduceIte]
next hbudget => contradiction
theorem remaining (bucket : β„• β†’ β„• β†’ Option (Bool Γ— β„•) Γ— β„•) (bill : β„• β†’ β„•)
(hr : βˆ€ z budget v left, (bucket z budget).1 = some (v, left) β†’ budget ≀ left + bill z)
(z count budget work : β„•) (v : Bool) (left : β„•)
(h : (loop bucket z count budget work).1 = some (v, left)) :
budget ≀ left + allowance bill z count := by
induction count generalizing z budget work v left with
| zero =>
rw [loop] at h
split at h
Β· cases h; simp only [allowance]; omega
Β· contradiction
| succ count ih =>
rw [loop] at h
split at h
next hbudget =>
cases he : (bucket z (budget - 4)).1 with
| none => simp only [he] at h; contradiction
| some p =>
rcases p with ⟨v', rest⟩
have hb := hr z (budget - 4) v' rest he
cases v'
Β· simp only [he] at h
have hi := ih (z + 1) rest (work + (bucket z (budget - 4)).2 + 3) v left h
simp only [allowance]
omega
Β· simp only [he] at h
cases h
simp only [allowance]
omega
next hbudget => contradiction
theorem complete (bucket : β„• β†’ β„• β†’ Option (Bool Γ— β„•) Γ— β„•) (bill : β„• β†’ β„•)
(hc : βˆ€ z budget, bill z ≀ budget β†’ βˆƒ v left, (bucket z budget).1 = some (v, left))
(hr : βˆ€ z budget v left, (bucket z budget).1 = some (v, left) β†’ budget ≀ left + bill z)
(z count budget work : β„•) (hb : allowance bill z count ≀ budget) :
βˆƒ v left, (loop bucket z count budget work).1 = some (v, left) := by
induction count generalizing z budget work with
| zero =>
have hh : 4 ≀ budget := hb
exact ⟨false, budget - 4, by rw [loop, ite_eq_left hh]⟩
| succ count ih =>
have hh : 4 ≀ budget := by simp only [allowance] at hb; omega
have hbill : bill z ≀ budget - 4 := by simp only [allowance] at hb; omega
obtain ⟨v, left, he⟩ := hc z (budget - 4) hbill
have hrest := hr z (budget - 4) v left he
cases v
· obtain ⟨v', rest, hi⟩ := ih (z + 1) left (work + (bucket z (budget - 4)).2 + 3)
(by simp only [allowance] at hb; omega)
exact ⟨v', rest, by rw [loop, ite_eq_left hh]; simpa only [he] using hi⟩
· exact ⟨true, left, by rw [loop, ite_eq_left hh]; simp only [he]⟩
end LowSpaceSubsetSum.BudgetedResidueLoop
end OAI