File size: 6,179 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
146
147
148
149
150
import OAI.Combinatorics.LowSpaceSubsetSum.Word.WordSeedExecution
import OAI.Combinatorics.LowSpaceSubsetSum.Word.WordSeedConstructors

namespace OAI

/-! # Materializing the actual words used by bounded rejection

The modulus is an already computed scalar argument. Each tail step reads
one word, reduces it modulo that scalar, and pushes the result into the
candidate array. The array is retained as part of the successful seed.
The counter charges five scalar operations per step and the final return;
the cell trace permits both the old and new array during a push.
-/

namespace LowSpaceSubsetSum.WordCandidateExecution

variable {w k : ℕ}

def low (modulus : ℕ) (hm : modulus = 2^k) (x : Fin (2^w)) : Fin (2^k) :=
  ⟨x.val % modulus, by rw [hm]; exact Nat.mod_lt _ (by positivity)⟩

theorem low_eq (modulus : ℕ) (hm : modulus = 2^k) (x : Fin (2^w)) :
    low modulus hm x = ExactWordDraw.low w k x := by
  subst modulus
  rfl

structure Raw (k : ℕ) where
  data : Array (Fin (2^k))
  cursor : ℕ
  work : ℕ
  cells : ℕ

def loop (stream : ℕ → Fin (2^w)) (modulus : ℕ) (hm : modulus = 2^k) :
    ℕ → ℕ → Array (Fin (2^k)) → ℕ → ℕ → Raw k
  | 0, cursor, acc, work, peak =>
      ⟨acc, cursor, work+1, max peak (2*acc.size+12)⟩
  | fuel+1, cursor, acc, work, peak =>
      let x := low modulus hm (stream cursor)
      loop stream modulus hm fuel (cursor+1) (acc.push x) (work+5)
        (max peak (2*(acc.size+1)+12))

theorem loop_data (stream : ℕ → Fin (2^w)) (modulus : ℕ) (hm : modulus = 2^k)
    (fuel cursor : ℕ) (acc : Array (Fin (2^k))) (work peak : ℕ) :
    (loop stream modulus hm fuel cursor acc work peak).data.toList =
      acc.toList ++ List.ofFn (fun i : Fin fuel => low modulus hm (stream (cursor+i.val))) := by
  induction fuel generalizing cursor acc work peak with
  | zero =>
    change acc.toList = acc.toList ++ List.ofFn (fun i : Fin 0 => _)
    simp only [List.ofFn_zero,List.append_nil]
  | succ fuel ih =>
    change (loop stream modulus hm fuel (cursor+1)
      (acc.push (low modulus hm (stream cursor))) (work+5)
        (max peak (2*(acc.size+1)+12))).data.toList = _
    rw [ih]
    simp only [Array.toList_push,List.ofFn_succ,Fin.val_zero,Nat.add_zero,
      Fin.val_succ,List.append_assoc,List.singleton_append]
    congr 2
    congr 1
    funext i
    congr 2
    omega

theorem loop_size (stream : ℕ → Fin (2^w)) (modulus : ℕ) (hm : modulus = 2^k)
    (fuel cursor : ℕ) (acc : Array (Fin (2^k))) (work peak : ℕ) :
    (loop stream modulus hm fuel cursor acc work peak).data.size = acc.size+fuel := by
  have h := congrArg List.length (loop_data stream modulus hm fuel cursor acc work peak)
  simpa only [Array.length_toList,List.length_append,List.length_ofFn] using h

theorem loop_cursor (stream : ℕ → Fin (2^w)) (modulus : ℕ) (hm : modulus = 2^k)
    (fuel cursor : ℕ) (acc : Array (Fin (2^k))) (work peak : ℕ) :
    (loop stream modulus hm fuel cursor acc work peak).cursor = cursor+fuel := by
  induction fuel generalizing cursor acc work peak with
  | zero => rfl
  | succ fuel ih =>
    change (loop stream modulus hm fuel (cursor+1)
      (acc.push (low modulus hm (stream cursor))) (work+5)
        (max peak (2*(acc.size+1)+12))).cursor = _
    rw [ih]
    omega

theorem loop_work (stream : ℕ → Fin (2^w)) (modulus : ℕ) (hm : modulus = 2^k)
    (fuel cursor : ℕ) (acc : Array (Fin (2^k))) (work peak : ℕ) :
    (loop stream modulus hm fuel cursor acc work peak).work = work+5*fuel+1 := by
  induction fuel generalizing cursor acc work peak with
  | zero =>
    change work+1 = work+5*0+1
    omega
  | succ fuel ih =>
    change (loop stream modulus hm fuel (cursor+1)
      (acc.push (low modulus hm (stream cursor))) (work+5)
        (max peak (2*(acc.size+1)+12))).work = _
    rw [ih]
    omega

theorem loop_cells (stream : ℕ → Fin (2^w)) (modulus : ℕ) (hm : modulus = 2^k)
    (fuel cursor : ℕ) (acc : Array (Fin (2^k))) (work peak : ℕ) :
    (loop stream modulus hm fuel cursor acc work peak).cells ≤
      max peak (2*(acc.size+fuel)+12) := by
  induction fuel generalizing cursor acc work peak with
  | zero =>
    change max peak (2*acc.size+12) ≤ max peak (2*(acc.size+0)+12)
    omega
  | succ fuel ih =>
    change (loop stream modulus hm fuel (cursor+1)
      (acc.push (low modulus hm (stream cursor))) (work+5)
        (max peak (2*(acc.size+1)+12))).cells ≤ _
    refine (ih _ _ _ _).trans ?_
    simp only [Array.size_push]
    omega

structure Buffer (k N : ℕ) where
  data : Vector (Fin (2^k)) N
  cursor : ℕ
  work : ℕ
  cells : ℕ

def run (stream : ℕ → Fin (2^w)) (modulus : ℕ) (hm : modulus = 2^k)
    (N cursor : ℕ) : Buffer k N :=
  let r := loop stream modulus hm N cursor #[] 0 0
  ⟨⟨r.data, by simpa only [Array.size_empty,Nat.zero_add] using
      loop_size stream modulus hm N cursor #[] 0 0⟩,
    r.cursor,r.work,r.cells⟩

theorem run_data (stream : ℕ → Fin (2^w)) (modulus : ℕ) (hm : modulus = 2^k)
    (N cursor : ℕ) :
    (run stream modulus hm N cursor).data = Vector.ofFn
      (fun i : Fin N => ExactWordDraw.low w k (stream (cursor+i.val))) := by
  apply Vector.toArray_inj.mp
  apply Array.toList_inj.mp
  simpa only [run,Array.toList_empty,List.nil_append,low_eq,
    Vector.toArray_ofFn,Array.toList_ofFn] using
      loop_data stream modulus hm N cursor #[] 0 0

theorem run_cursor (stream : ℕ → Fin (2^w)) (modulus : ℕ) (hm : modulus = 2^k)
    (N cursor : ℕ) : (run stream modulus hm N cursor).cursor = cursor+N :=
  loop_cursor stream modulus hm N cursor #[] 0 0

theorem run_work (stream : ℕ → Fin (2^w)) (modulus : ℕ) (hm : modulus = 2^k)
    (N cursor : ℕ) : (run stream modulus hm N cursor).work = 5*N+1 := by
  simpa only [run,Nat.zero_add] using loop_work stream modulus hm N cursor #[] 0 0

theorem run_cells (stream : ℕ → Fin (2^w)) (modulus : ℕ) (hm : modulus = 2^k)
    (N cursor : ℕ) : (run stream modulus hm N cursor).cells ≤ 2*N+12 := by
  simpa only [run,Array.size_empty,Nat.zero_add,Nat.zero_max] using
    loop_cells stream modulus hm N cursor #[] 0 0

end LowSpaceSubsetSum.WordCandidateExecution

end OAI