File size: 6,422 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import OAI.Combinatorics.LowSpaceSubsetSum.Representation.RepresentationParentComplete
import OAI.Combinatorics.LowSpaceSubsetSum.Streams.DictionaryCompleted
import OAI.Combinatorics.LowSpaceSubsetSum.Separators.SeparatorEncodedCheck

namespace OAI

/-! # Actual depth-first right-parent prefix recomputation -/

namespace LowSpaceSubsetSum

namespace MaskRestriction

variable {n : ℕ}

def force (P : MaskRestriction n) (i : Fin n) (side : Bool) : MaskRestriction n :=
  if side then { P with ones := Insert.insert i P.ones }
  else { P with zeros := Insert.insert i P.zeros }

def follow (P : MaskRestriction n) (order : List (Fin n)) (S : Finset (Fin n)) : MaskRestriction n :=
  order.foldl (fun P i => P.force i (decide (i ∈ S))) P

theorem force_compatible (P : MaskRestriction n) (domain S : Finset (Fin n))
    (h : P.Compatible domain S) (i : Fin n) :
    (P.force i (decide (i ∈ S))).Compatible domain S := by
  by_cases hi : i ∈ S
  · simp only [hi, decide_true, force, ↓reduceIte, Compatible]
    refine ⟨?_, h.2⟩
    intro j hj
    obtain ⟨hj, hjD⟩ := Finset.mem_inter.mp hj
    rcases Finset.mem_insert.mp hj with rfl | hj
    · exact hi
    · exact h.1 (Finset.mem_inter.mpr ⟨hj, hjD⟩)
  · simp only [hi, decide_false, force, Bool.false_eq_true, ↓reduceIte, Compatible]
    refine ⟨h.1, Finset.disjoint_left.mpr ?_⟩
    intro j hj hjS
    rcases Finset.mem_insert.mp hj with rfl | hj
    · exact hi hjS
    · exact Finset.disjoint_left.mp h.2 hj hjS

end MaskRestriction

namespace RepresentationParentData

variable {n : ℕ} (D : RepresentationParentData n)

/-- Each task regenerates its restricted leaves and streams. An overflowing
full prefix is dropped; a completed array is passed to the actual checker.
The first YES returns immediately. -/
def searchRight (B : ℕ) (check : Array (Entry (Fin n)) → Bool) :
    List (Fin n) → MaskRestriction n → Bool
  | order, P =>
    let task := { D with maskRestriction := P }
    let out := (task.collect B).state.user
    if out.keys.overflow then
      match order with
      | [] => false
      | i :: rest =>
        if searchRight B check rest (P.force i false) then true
        else searchRight B check rest (P.force i true)
    else check out.records

theorem searchRight_sound (B : ℕ) (check : Array (Entry (Fin n)) → Bool)
    (Q : Prop) (hcheck : ∀ P, check ({ D with maskRestriction := P }.entries B) = true → Q)
    (order : List (Fin n)) (P : MaskRestriction n) (hyes : D.searchRight B check order P = true) : Q := by
  induction order generalizing P with
  | nil =>
    simp only [searchRight] at hyes
    split at hyes
    · contradiction
    · exact hcheck P hyes
  | cons i order ih =>
    simp only [searchRight] at hyes
    split at hyes
    · split at hyes
      · rename_i hl
        exact ih _ hl
      · exact ih _ hyes
    · exact hcheck P hyes

/-- A fitting task at the end of a designated mask path suffices. Earlier
completion reaches the checker; earlier YES also returns immediately. -/
theorem searchRight_complete (B : ℕ) (check : Array (Entry (Fin n)) → Bool)
    (domain S : Finset (Fin n))
    (hfind : ∀ P, P.Compatible domain S →
      ({ D with maskRestriction := P }.collect B).state.user.keys.overflow = false →
        check ({ D with maskRestriction := P }.entries B) = true)
    (order : List (Fin n)) (P : MaskRestriction n) (hP : P.Compatible domain S)
    (hfull : ({ D with maskRestriction := P.follow order S }.collect B).state.user.keys.overflow = false) :
    D.searchRight B check order P = true := by
  induction order generalizing P with
  | nil =>
    have hc : ({ D with maskRestriction := P }.collect B).state.user.keys.overflow = false := hfull
    simpa only [searchRight, hc, Bool.false_eq_true, ↓reduceIte, entries] using hfind P hP hc
  | cons i order ih =>
    by_cases ho : ({ D with maskRestriction := P }.collect B).state.user.keys.overflow = true
    · have hnext := ih (P.force i (decide (i ∈ S))) (P.force_compatible domain S hP i) hfull
      cases hb : decide (i ∈ S) with
      | false =>
        simp only [hb] at hnext
        simp only [searchRight, ho, ↓reduceIte, hnext]
      | true =>
        simp only [hb] at hnext
        simp only [searchRight, ho, ↓reduceIte]
        split
        · rfl
        · exact hnext
    · have hc : ({ D with maskRestriction := P }.collect B).state.user.keys.overflow = false :=
        Bool.eq_false_iff.mpr ho
      simpa only [searchRight, hc, Bool.false_eq_true, ↓reduceIte, entries] using hfind P hP hc
/-- No prefix task can introduce a false YES. This applies to every family
outcome and every overflow pattern, rather than just the surviving bucket. -/
theorem searchRight_subset_sum_sound {g : ℕ} (B : ℕ) (C : TopCoordinates n g)
    (families : Fin 4 → Array (Finset (Fin g))) (left : Array (Entry (Fin n)))
    (order : List (Fin n)) (P : MaskRestriction n) (leftDomain : Finset (Fin n))
    (htop : RepresentationUniverse.topDomain D.leaves = C.domain)
    (htl : Disjoint (RepresentationUniverse.topDomain D.leaves)
      (RepresentationUniverse.mixerDomain D.leaves))
    (htb : Disjoint (RepresentationUniverse.topDomain D.leaves)
      (RepresentationUniverse.backgroundDomain D.leaves))
    (hlb : Disjoint (RepresentationUniverse.mixerDomain D.leaves)
      (RepresentationUniverse.backgroundDomain D.leaves))
    (hshared : leftDomain ∩ RepresentationUniverse.parentDomain D.leaves ⊆ C.domain)
    (hleft : ∀ e ∈ left.toList, Represents D.weights leftDomain C.domain e)
    (hsize : left.size + (D.capacity + 1) ≤ 2 ^ n)
    (hyes : D.searchRight B (fun right => (C.check families
      ((D.input.target : ℤ) - weight (fun i => (D.input.values i : ℤ)) D.signs)
      left.toList right.toList).accepted) order P = true) : Solvable D.input := by
  apply D.searchRight_sound B _ (Solvable D.input) ?_ order P hyes
  intro P hp
  let task := { D with maskRestriction := P }
  apply C.subset_sum_sound D.input D.signs families left.toList (task.entries B).toList
    (leftDomain := leftDomain) (rightDomain := RepresentationUniverse.parentDomain D.leaves)
    (by
      simp only [Array.length_toList]
      exact (Nat.add_le_add_left (task.storage B).1 _).trans hsize)
    hshared hleft ?_ hp
  intro e he
  rw [← htop]
  exact task.entry_represents B htl htb hlb e he

end RepresentationParentData
end LowSpaceSubsetSum

end OAI