File size: 3,363 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
import OAI.Combinatorics.LowSpaceSubsetSum.Search.BudgetedOuterReplay
import OAI.Combinatorics.LowSpaceSubsetSum.Search.BudgetedReplayLoops

namespace OAI

namespace LowSpaceSubsetSum.BudgetedOuterLoop

universe u v w
variable {α : Type u} {σ : Type v} {τ : Type w}

def loop (charge : ℕ) (read : σ → Option (α × σ) × ℕ) (key : α → ℤ × ℕ) (K : ℤ)
    (report : α → τ → ℕ → Option ((τ × Bool) × ℕ) × ℕ) :
    ℕ → σ → τ → ℕ → ℕ → ℕ → ℕ → Option (TieReplay.Result σ τ × ℕ) × ℕ
  | fuel, s, a, q, r, budget, work =>
    if charge ≤ budget then
      let left := budget - charge
      match fuel with
      | 0 => (some (⟨s, a, q, r, false, false⟩, left), work + 1)
      | fuel + 1 =>
        let fetched := read s
        match fetched.1 with
        | none => (some (⟨s, a, q + 1, r, true, false⟩, left), work + (fetched.2 + 5))
        | some (x, s') =>
          let found := key x
          if found.1 = K then
            let reported := report x a left
            match reported.1 with
            | none => (none, work + (fetched.2 + found.2 + reported.2 + 8))
            | some ((next, halt), remaining) =>
              if halt then
                (some (⟨s', next, q + 1, r + 1, false, true⟩, remaining),
                  work + (fetched.2 + found.2 + reported.2 + 8))
              else loop charge read key K report fuel s' next (q + 1) (r + 1) remaining
                (work + (fetched.2 + found.2 + reported.2 + 8))
          else (some (⟨s, a, q + 1, r, true, false⟩, left), work + (fetched.2 + found.2 + 6))
    else (none, work + 1)

theorem loop_eq (charge : ℕ) (read : σ → Option (α × σ) × ℕ) (key : α → ℤ × ℕ) (K : ℤ)
    (report : α → τ → ℕ → Option ((τ × Bool) × ℕ) × ℕ)
    (fuel : ℕ) (s : σ) (a : τ) (q r budget work : ℕ) :
    loop charge read key K report fuel s a q r budget work =
      ((BudgetedOuterReplay.execute charge read key K report fuel s a q r budget).1,
        work + (BudgetedOuterReplay.execute charge read key K report fuel s a q r budget).2) := by
  induction fuel generalizing s a q r budget work with
  | zero => rw [loop, BudgetedOuterReplay.execute]; split <;> rfl
  | succ fuel ih =>
    rw [loop, BudgetedOuterReplay.execute]
    split
    · dsimp only
      cases hr : (read s).1 with
      | none => dsimp only
      | some p =>
        rcases p with ⟨x, s'⟩
        dsimp only
        split
        · cases hc : (report x a (budget - charge)).1 with
          | none => dsimp only
          | some p =>
            rcases p with ⟨⟨next, halt⟩, remaining⟩
            dsimp only
            split
            · rfl
            · rw [ih]
              simp only [Nat.add_assoc]
        · rfl
    · rfl

def execute (charge : ℕ) (read : σ → Option (α × σ) × ℕ) (key : α → ℤ × ℕ) (K : ℤ)
    (report : α → τ → ℕ → Option ((τ × Bool) × ℕ) × ℕ)
    (fuel : ℕ) (s : σ) (a : τ) (q r budget : ℕ) :=
  loop charge read key K report fuel s a q r budget 0

@[csimp] theorem execute_eq : @BudgetedOuterReplay.execute.{u, v, w} = @execute.{u, v, w} := by
  funext α σ τ charge read key K report fuel s a q r budget
  rw [execute, loop_eq]
  simp only [Nat.zero_add]

end LowSpaceSubsetSum.BudgetedOuterLoop

end OAI