File size: 3,363 Bytes
4a56cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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