File size: 11,050 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import Mathlib.Data.Fin.VecNotation
import Mathlib.Data.Fintype.Fin
import Mathlib.Data.Nat.Bits
import Mathlib.Logic.Embedding.Basic
import Mathlib.Tactic.FinCases

namespace OAI

namespace BinPackingGap.BinaryRegisterProgram

structure Operands (Reg : Type*) where
  destination : Reg
  left : Reg
  right : Reg
  left_ne_right : left β‰  right
  left_ne_destination : left β‰  destination
  right_ne_destination : right β‰  destination

variable {Reg K : Type*}

def addSlots (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) : Fin 7 β†ͺ K where
  toFun i := slot (![Sum.inl op.left, Sum.inl op.right, Sum.inr 0, Sum.inr 1,
    Sum.inr 2, Sum.inl op.destination, Sum.inr 3] i)
  inj' := by
    intro i j hij
    have heq := slot.injective hij
    fin_cases i <;> fin_cases j <;>
      simp_all [op.left_ne_right, op.left_ne_destination, op.right_ne_destination,
        Ne.symm op.left_ne_right, Ne.symm op.left_ne_destination,
        Ne.symm op.right_ne_destination]

@[simp] theorem addSlots_zero (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
    addSlots slot op 0 = slot (Sum.inl op.left) := rfl

@[simp] theorem addSlots_one (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
    addSlots slot op 1 = slot (Sum.inl op.right) := rfl

@[simp] theorem addSlots_two (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
    addSlots slot op 2 = slot (Sum.inr 0) := rfl

@[simp] theorem addSlots_three (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
    addSlots slot op 3 = slot (Sum.inr 1) := rfl

@[simp] theorem addSlots_four (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
    addSlots slot op 4 = slot (Sum.inr 2) := rfl

@[simp] theorem addSlots_five (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
    addSlots slot op 5 = slot (Sum.inl op.destination) := rfl

@[simp] theorem addSlots_six (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
    addSlots slot op 6 = slot (Sum.inr 3) := rfl

def mulSlots (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) : Fin 9 β†ͺ K where
  toFun i := slot (![Sum.inl op.left, Sum.inl op.right, Sum.inl op.destination,
    Sum.inr 0, Sum.inr 1, Sum.inr 2, Sum.inr 3, Sum.inr 4, Sum.inr 5] i)
  inj' := by
    intro i j hij
    have heq := slot.injective hij
    fin_cases i <;> fin_cases j <;>
      simp_all [op.left_ne_right, op.left_ne_destination, op.right_ne_destination,
        Ne.symm op.left_ne_right, Ne.symm op.left_ne_destination,
        Ne.symm op.right_ne_destination]

@[simp] theorem mulSlots_zero (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
    mulSlots slot op 0 = slot (Sum.inl op.left) := rfl

@[simp] theorem mulSlots_one (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
    mulSlots slot op 1 = slot (Sum.inl op.right) := rfl

@[simp] theorem mulSlots_two (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
    mulSlots slot op 2 = slot (Sum.inl op.destination) := rfl

@[simp] theorem mulSlots_three (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
    mulSlots slot op 3 = slot (Sum.inr 0) := rfl

@[simp] theorem mulSlots_four (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
    mulSlots slot op 4 = slot (Sum.inr 1) := rfl

@[simp] theorem mulSlots_five (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
    mulSlots slot op 5 = slot (Sum.inr 2) := rfl

@[simp] theorem mulSlots_six (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
    mulSlots slot op 6 = slot (Sum.inr 3) := rfl

@[simp] theorem mulSlots_seven (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
    mulSlots slot op 7 = slot (Sum.inr 4) := rfl

@[simp] theorem mulSlots_eight (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
    mulSlots slot op 8 = slot (Sum.inr 5) := rfl

def registerWords (values : Reg β†’ β„•) : Reg βŠ• Fin 6 β†’ List Bool
  | Sum.inl r => (values r).bits
  | Sum.inr _ => []

@[simp] theorem registerWords_reg (values : Reg β†’ β„•) (r : Reg) :
    registerWords values (Sum.inl r) = (values r).bits := rfl

@[simp] theorem registerWords_scratch (values : Reg β†’ β„•) (i : Fin 6) :
    registerWords values (Sum.inr i) = [] := rfl

noncomputable def registerTapes (slot : (Reg βŠ• Fin 6) β†ͺ K)
    (base : K β†’ List Bool) (values : Reg β†’ β„•) : K β†’ List Bool := by
  classical
  exact fun k =>
    if h : βˆƒ i, slot i = k then registerWords values (Classical.choose h) else base k

@[simp] theorem registerTapes_slot (slot : (Reg βŠ• Fin 6) β†ͺ K)
    (base : K β†’ List Bool) (values : Reg β†’ β„•) (i : Reg βŠ• Fin 6) :
    registerTapes slot base values (slot i) = registerWords values i := by
  unfold registerTapes
  rw [dite_eq_left ⟨i, rfl⟩]
  congr 1
  exact slot.injective (Classical.choose_spec (show βˆƒ j, slot j = slot i from ⟨i, rfl⟩))

@[simp] theorem registerTapes_reg (slot : (Reg βŠ• Fin 6) β†ͺ K)
    (base : K β†’ List Bool) (values : Reg β†’ β„•) (r : Reg) :
    registerTapes slot base values (slot (Sum.inl r)) = (values r).bits :=
  registerTapes_slot slot base values (Sum.inl r)

@[simp] theorem registerTapes_scratch (slot : (Reg βŠ• Fin 6) β†ͺ K)
    (base : K β†’ List Bool) (values : Reg β†’ β„•) (i : Fin 6) :
    registerTapes slot base values (slot (Sum.inr i)) = [] :=
  registerTapes_slot slot base values (Sum.inr i)

theorem registerTapes_other (slot : (Reg βŠ• Fin 6) β†ͺ K)
    (base : K β†’ List Bool) (values : Reg β†’ β„•) (k : K)
    (hk : Β¬ βˆƒ i, slot i = k) : registerTapes slot base values k = base k := by
  simp [registerTapes, hk]

theorem registerTapes_update [DecidableEq Reg] [DecidableEq K]
    (slot : (Reg βŠ• Fin 6) β†ͺ K) (base : K β†’ List Bool) (values : Reg β†’ β„•)
    (d : Reg) (v : β„•) :
    Function.update (registerTapes slot base values) (slot (Sum.inl d)) v.bits =
      registerTapes slot base (Function.update values d v) := by
  funext k
  by_cases hk : βˆƒ j, slot j = k
  · obtain ⟨j, rfl⟩ := hk
    cases j with
    | inl r =>
      by_cases hrd : r = d
      Β· subst r; simp
      Β· have hslot : slot (Sum.inl r) β‰  slot (Sum.inl d) :=
          slot.injective.ne (by simpa only [ne_eq, Sum.inl.injEq] using hrd)
        simp [Function.update_of_ne, hrd, hslot]
    | inr i =>
      have hslot : slot (Sum.inr i) β‰  slot (Sum.inl d) := slot.injective.ne (by simp)
      simp [Function.update_of_ne, hslot]
  Β· have hslot : k β‰  slot (Sum.inl d) := fun h => hk ⟨Sum.inl d, h.symm⟩
    simp [Function.update_of_ne hslot, registerTapes_other slot base _ k hk]

theorem registerTapes_mul_workspace (slot : (Reg βŠ• Fin 6) β†ͺ K)
    (op : Operands Reg) (base : K β†’ List Bool) (values : Reg β†’ β„•)
    (i : Fin 9) (hi : 3 ≀ i.val) :
    registerTapes slot base values (mulSlots slot op i) = [] := by
  fin_cases i <;> simp_all

theorem registerTapes_add_workspace (slot : (Reg βŠ• Fin 6) β†ͺ K)
    (op : Operands Reg) (base : K β†’ List Bool) (values : Reg β†’ β„•)
    (i : Fin 7) (hi : 2 ≀ i.val) (hout : i β‰  5) :
    registerTapes slot base values (addSlots slot op i) = [] := by
  fin_cases i <;> simp_all

end BinPackingGap.BinaryRegisterProgram

noncomputable section

namespace BinPackingGap.PackingRegisterFrame

open BinaryRegisterProgram

variable {Local Global K : Type}

def withScratch (register : Local β†ͺ Global) : (Local βŠ• Fin 6) β†ͺ (Global βŠ• Fin 6) where
  toFun
    | .inl r => .inl (register r)
    | .inr i => .inr i
  inj' := by
    intro a b h
    cases a <;> cases b <;> simp_all only [Sum.inl.injEq, Sum.inr.injEq,
      Sum.inl_ne_inr, Sum.inr_ne_inl]
    exact register.injective h

def localSlots (slot : (Global βŠ• Fin 6) β†ͺ K) (register : Local β†ͺ Global) :
    (Local βŠ• Fin 6) β†ͺ K := (withScratch register).trans slot

@[simp] theorem localSlots_reg (slot : (Global βŠ• Fin 6) β†ͺ K)
    (register : Local β†ͺ Global) (r : Local) :
    localSlots slot register (.inl r) = slot (.inl (register r)) := rfl

@[simp] theorem localSlots_scratch (slot : (Global βŠ• Fin 6) β†ͺ K)
    (register : Local β†ͺ Global) (i : Fin 6) :
    localSlots slot register (.inr i) = slot (.inr i) := rfl

theorem registerTapes_eq_self (slot : (Local βŠ• Fin 6) β†ͺ K)
    (base : K β†’ List Bool) (values : Local β†’ Nat)
    (shape : βˆ€ i, base (slot i) = registerWords values i) :
    registerTapes slot base values = base := by
  funext k
  by_cases hk : βˆƒ i, slot i = k
  · obtain ⟨i, rfl⟩ := hk
    rw [registerTapes_slot, shape]
  Β· exact registerTapes_other slot base values k hk

theorem restrict (slot : (Global βŠ• Fin 6) β†ͺ K) (register : Local β†ͺ Global)
    (base : K β†’ List Bool) (values : Global β†’ Nat) :
    registerTapes (localSlots slot register) (registerTapes slot base values)
      (values ∘ register) = registerTapes slot base values := by
  apply registerTapes_eq_self
  intro i
  cases i <;> simp [Function.comp_def]

theorem replace (slot : (Global βŠ• Fin 6) β†ͺ K) (register : Local β†ͺ Global)
    (base : K β†’ List Bool) (before after : Global β†’ Nat) (localAfter : Local β†’ Nat)
    (inside : βˆ€ r, after (register r) = localAfter r)
    (outside : βˆ€ r, (Β¬ βˆƒ l, register l = r) β†’ after r = before r) :
    registerTapes (localSlots slot register) (registerTapes slot base before) localAfter =
      registerTapes slot base after := by
  funext k
  by_cases hk : βˆƒ i, slot i = k
  · obtain ⟨i, rfl⟩ := hk
    cases i with
    | inl r =>
        by_cases hr : βˆƒ l, register l = r
        · obtain ⟨l, rfl⟩ := hr
          change registerTapes (localSlots slot register) _ localAfter
            (localSlots slot register (.inl l)) = _
          simp only [registerTapes_reg, inside]
        Β· have hlocal : Β¬ βˆƒ i, localSlots slot register i = slot (.inl r) := by
            rintro ⟨i, hi⟩
            have h := slot.injective hi
            cases i with
            | inl l => exact hr ⟨l, Sum.inl.inj h⟩
            | inr j => cases h
          rw [registerTapes_other _ _ _ _ hlocal, registerTapes_reg,
            registerTapes_reg, outside r hr]
    | inr j =>
        change registerTapes (localSlots slot register) _ localAfter
          (localSlots slot register (.inr j)) = _
        simp only [registerTapes_scratch]
  Β· have hlocal : Β¬ βˆƒ i, localSlots slot register i = k := by
      rintro ⟨i, hi⟩
      exact hk ⟨withScratch register i, hi⟩
    rw [registerTapes_other _ _ _ _ hlocal, registerTapes_other _ _ _ _ hk,
      registerTapes_other _ _ _ _ hk]

theorem external_update [DecidableEq K] (slot : (Global βŠ• Fin 6) β†ͺ K) (base : K β†’ List Bool)
    (values : Global β†’ Nat) (output : K) (outside : βˆ€ i, slot i β‰  output)
    (word : List Bool) :
    registerTapes slot (Function.update base output word) values =
      Function.update (registerTapes slot base values) output word := by
  funext k
  by_cases hk : k = output
  Β· subst k
    have hn : Β¬ βˆƒ i, slot i = output := by rintro ⟨i, hi⟩; exact outside i hi
    simp [registerTapes_other _ _ _ _ hn]
  Β· by_cases hs : βˆƒ i, slot i = k
    · obtain ⟨i, rfl⟩ := hs
      simp [outside i]
    Β· simp [registerTapes_other _ _ _ _ hs, hk]

end BinPackingGap.PackingRegisterFrame

end

end OAI