File size: 3,548 Bytes
e5a97c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Combinatorics.LowSpaceSubsetSum.Streams.PrefixMachine
import OAI.Combinatorics.LowSpaceSubsetSum.Streams.PrefixTaskCount
import OAI.Combinatorics.LowSpaceSubsetSum.Search.WatchdogRanking

namespace OAI

/-! # Termination of the explicit prefix stack

The rank charges all possible task starts, including the sibling skipped
after an early YES. The stack machine terminates in at most three times
this task count plus two control actions. A collector action is a macro,
whose internal primitive operations still need their own watchdog.
-/

namespace LowSpaceSubsetSum.PrefixMachine

variable {n : ℕ}

def pendingWeight (D : RepresentationParentData n) (B : ℕ) (pending : List (Job n)) : ℕ :=
  (pending.map (fun job => 3 * D.taskStarts B job.order job.restriction + 2)).sum

def rank (D : RepresentationParentData n) (B : ℕ) : Control n → ℕ
  | .eval job pending =>
    3 * D.taskStarts B job.order job.restriction + pendingWeight D B pending + 2
  | .ret _ pending => pendingWeight D B pending + 1

theorem rank_pos (D : RepresentationParentData n) (B : ℕ) (s : Control n) :
    1 ≤ rank D B s := by cases s <;> simp only [rank] <;> omega

theorem rank_next (D : RepresentationParentData n) (B : ℕ)
    (check : Array (Entry (Fin n)) → Bool) (s t : Control n)
    (ht : step D B check s = .next t) : rank D B t < rank D B s := by
  cases s with
  | eval job pending =>
    cases job with
    | mk order P =>
      by_cases ho : ({ D with maskRestriction := P }.collect B).state.user.keys.overflow = true
      · cases order with
        | nil =>
          simp only [step, ho, ↓reduceIte] at ht
          cases ht
          simp only [rank, RepresentationParentData.taskStarts]
          omega
        | cons i rest =>
          simp only [step, ho, ↓reduceIte] at ht
          cases ht
          simp only [rank, pendingWeight, List.map_cons, List.sum_cons,
            RepresentationParentData.taskStarts, ho, ↓reduceIte]
          omega
      · have hf : ({ D with maskRestriction := P }.collect B).state.user.keys.overflow = false :=
          Bool.eq_false_iff.mpr ho
        simp only [step, hf, Bool.false_eq_true, ↓reduceIte] at ht
        cases ht
        cases order <;>
          simp only [rank, RepresentationParentData.taskStarts, hf, Bool.false_eq_true, ↓reduceIte] <;>
          omega
  | ret b pending =>
    cases b with
    | true => simp [step] at ht
    | false =>
      cases pending with
      | nil => simp [step] at ht
      | cons job pending =>
        simp only [step] at ht
        cases ht
        simp only [rank, pendingWeight, List.map_cons, List.sum_cons]
        omega

/-- The explicit stack performs exactly the original prefix search. -/
theorem output_eq_searchRight (D : RepresentationParentData n) (B : ℕ)
    (check : Array (Entry (Fin n)) → Bool) (job : Job n) :
    (Watchdog.run (step D B check) Control.cells
      (3 * D.taskStarts B job.order job.restriction + 2) (.eval job [])).output =
        some (D.searchRight B check job.order job.restriction) := by
  have h := Watchdog.ranked_complete (step D B check) Control.cells (rank D B)
    (rank_pos D B) (rank_next D B check) (meaning D B check) (fun s => by
      have hs := step_meaning D B check s
      cases ht : step D B check s <;> simp only [ht] at hs ⊢ <;> exact hs)
    (.eval job []) (3 * D.taskStarts B job.order job.restriction + 2)
    (by simp [rank, pendingWeight])
  simpa only [meaning, List.any_nil, Bool.or_false, jobValue] using h

end LowSpaceSubsetSum.PrefixMachine

end OAI