File size: 12,183 Bytes
8df23f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import Mathlib.Data.Fintype.Sigma
import Mathlib.Tactic.DeriveFintype
import OAI.Computability.BinPacking.Arithmetic.BinaryToTallyMachine
import OAI.Computability.BinPacking.Arithmetic.ResetExpressionCompiler
import OAI.Computability.BinPacking.Inventory.InventoryScores

namespace OAI

noncomputable section

namespace BinPackingGap.PackingSetupExpression

section

open NatExpressionCompiler

structure Parameters where
  P : Nat
  K : Nat
  d : Nat
  tPlus : Nat
  tMinus : Nat

inductive Input
  | n | m | k
  deriving DecidableEq

protected abbrev Input.enumList : List Input := [.n, .m, .k]

protected theorem Input.enumList_getElem?_ctorIdx_eq (x : Input) :
    Input.enumList[x.ctorIdx]? = some x := by
  cases x <;> rfl

protected theorem Input.enumList_nodup : Input.enumList.Nodup := by decide

instance : Fintype Input where
  elems := ⟨Input.enumList, Input.enumList_nodup⟩
  complete x := by cases x <;> decide

inductive Output
  | repetitions | base | edgeRepetitions | bins
  | upOne | upZero | umOne | umZero | edgeStock
  | treeFlags | jobFlags | one | three
  deriving DecidableEq

protected abbrev Output.enumList : List Output := [.repetitions, .base, .edgeRepetitions, .bins,
  .upOne, .upZero, .umOne, .umZero, .edgeStock, .treeFlags, .jobFlags, .one, .three]

protected theorem Output.enumList_getElem?_ctorIdx_eq (x : Output) :
    Output.enumList[x.ctorIdx]? = some x := by
  cases x <;> rfl

protected theorem Output.enumList_nodup : Output.enumList.Nodup := by decide

instance : Fintype Output where
  elems := ⟨Output.enumList, Output.enumList_nodup⟩
  complete x := by cases x <;> decide

def inputs (n m k : Nat) : Input → Nat
  | .n => n
  | .m => m
  | .k => k

def repetitions (p : Parameters) : Expr Input :=
  .mul (.constant 100) (.add (.add (.constant p.P)
    (.mul (.input .n) (.constant p.K))) (.constant 1))

def edgeRepetitions (p : Parameters) : Expr Input :=
  .mul (.input .m) (repetitions p)

def jobFlags (p : Parameters) : Expr Input :=
  .mul (.constant (2 * p.d)) (edgeRepetitions p)

def expression (p : Parameters) : Output → Expr Input
  | .repetitions => repetitions p
  | .base => .add (repetitions p) (.constant 1)
  | .edgeRepetitions => edgeRepetitions p
  | .bins => .add (.mul (.input .n) (.constant (p.tPlus + p.tMinus)))
      (.mul (.constant (4 * p.d)) (edgeRepetitions p))
  | .upOne => .mul (.constant p.d) (.input .k)
  | .upZero => .monus (.add (.mul (.input .n) (.constant p.tPlus)) (jobFlags p))
      (.mul (.constant p.d) (.input .k))
  | .umOne => .mul (.constant p.d) (.monus (.input .n) (.input .k))
  | .umZero => .monus (.mul (.input .n) (.constant p.tMinus))
      (.mul (.constant p.d) (.monus (.input .n) (.input .k)))
  | .edgeStock => .mul (.constant p.d) (repetitions p)
  | .treeFlags => .mul (.input .n) (.constant (p.tPlus + p.tMinus))
  | .jobFlags => jobFlags p
  | .one => .constant 1
  | .three => .constant 3

def outputValues (p : Parameters) (n m k : Nat) : Output → Nat
  | .repetitions => 100 * (p.P + n * p.K + 1)
  | .base => 100 * (p.P + n * p.K + 1) + 1
  | .edgeRepetitions => m * (100 * (p.P + n * p.K + 1))
  | .bins => n * (p.tPlus + p.tMinus) +
      (4 * p.d) * (m * (100 * (p.P + n * p.K + 1)))
  | .upOne => p.d * k
  | .upZero => n * p.tPlus + (2 * p.d) * (m * (100 * (p.P + n * p.K + 1))) - p.d * k
  | .umOne => p.d * (n - k)
  | .umZero => n * p.tMinus - p.d * (n - k)
  | .edgeStock => p.d * (100 * (p.P + n * p.K + 1))
  | .treeFlags => n * (p.tPlus + p.tMinus)
  | .jobFlags => (2 * p.d) * (m * (100 * (p.P + n * p.K + 1)))
  | .one => 1
  | .three => 3

theorem eval_expression (p : Parameters) (n m k : Nat) (o : Output) :
    eval (inputs n m k) (expression p o) = outputValues p n m k o := by
  cases o <;> rfl

end

def inventoryOutput (D : InventoryData) : Output → Nat
  | .repetitions => D.R
  | .base => D.R + 1
  | .edgeRepetitions => D.graph.edges.length * D.R
  | .bins => D.B
  | .upOne => D.globalStock (.up true)
  | .upZero => D.globalStock (.up false)
  | .umOne => D.globalStock (.um true)
  | .umZero => D.globalStock (.um false)
  | .edgeStock => D.d * D.R
  | .treeFlags => D.flagStock .tree
  | .jobFlags => D.flagStock .main
  | .one => 1
  | .three => 3

theorem outputValues_eq_inventory (p : Parameters) (D : InventoryData)
    (hd : p.d = D.d) (hplus : p.tPlus = D.tPlus) (hminus : p.tMinus = D.tMinus)
    (hR : D.R = 100 * (p.P + D.graph.n * p.K + 1)) :
    outputValues p D.graph.n D.graph.edges.length D.k = inventoryOutput D := by
  funext o
  cases o <;>
    simp [outputValues, inventoryOutput, ← hR, hd, hplus, hminus,
      InventoryData.globalStock, InventoryData.flagStock, InventoryData.minusSlots,
      InventoryData.treeFlags, InventoryData.jobFlags, D.B_closed, D.plusSlots_eq,
      D.sum_J, InventoryData.t, Nat.mul_assoc]

end BinPackingGap.PackingSetupExpression

namespace BinPackingGap.PackingSetupMachine

section

open NatExpressionCompiler BinaryRegisterProgram FiniteTapeProgram
open PackingSetupExpression

abbrev Reg (p : Parameters) := Input ⊕ (Output ⊕ (Σ o : Output, Node (expression p o)))

def input (p : Parameters) : Input → Reg p := Sum.inl
def output (p : Parameters) (o : Output) : Reg p := .inr (.inl o)

def nodes (p : Parameters) (o : Output) : Node (expression p o) ↪ Reg p where
  toFun n := .inr (.inr ⟨o, n⟩)
  inj' := by
    intro a b h
    have hs := Sum.inr.inj (Sum.inr.inj h)
    simpa only [Sigma.mk.inj_iff, heq_eq_eq, true_and] using hs

def frame (p : Parameters) (ins : Input → Nat) (outs : Output → Nat) : Reg p → Nat
  | .inl a => ins a
  | .inr (.inl o) => outs o
  | .inr (.inr _) => 0

theorem frame_update (p : Parameters) (ins : Input → Nat) (outs : Output → Nat)
    (o : Output) (v : Nat) :
    Function.update (frame p ins outs) (output p o) v =
      frame p ins (Function.update outs o v) := by
  funext r
  rcases r with a | (q | n)
  · simp [frame, output]
  · by_cases h : q = o <;> simp [frame, output, h]
  · simp [frame, output]

def block (p : Parameters) (o : Output) : List (Command (Reg p)) :=
  ResetExpressionCompiler.commands (input p) (expression p o) (nodes p o)
    (by intros; simp [input, nodes]) (output p o) (by
      intro n equal
      change (Sum.inr (Sum.inl o) : Reg p) = Sum.inr (Sum.inr ⟨o, n⟩) at equal
      cases Sum.inr.inj equal)

theorem block_correct (p : Parameters) (o : Output)
    (ins : Input → Nat) (outs : Output → Nat) :
    Ready (block p o) (frame p ins outs) ∧
      resultOf (block p o) (frame p ins outs) =
        frame p ins (Function.update outs o (eval ins (expression p o))) := by
  have h := ResetExpressionCompiler.correct (input p) (expression p o) (nodes p o)
    (by intros; simp [input, nodes]) (output p o) (by
      intro n equal
      change (Sum.inr (Sum.inl o) : Reg p) = Sum.inr (Sum.inr ⟨o, n⟩) at equal
      cases Sum.inr.inj equal)
    (by intros; simp [input, output]) (frame p ins outs) (fun _ => rfl)
  refine ⟨h.1, ?_⟩
  calc
    resultOf (block p o) (frame p ins outs) =
        Function.update (frame p ins outs) (output p o) (eval ins (expression p o)) := by
      simpa only [block, input, frame] using h.2
    _ = _ := frame_update p ins outs o _

def outputOrder : List Output :=
  [.repetitions, .base, .edgeRepetitions, .bins, .upOne, .upZero,
    .umOne, .umZero, .edgeStock, .treeFlags, .jobFlags, .one, .three]

theorem mem_outputOrder (o : Output) : o ∈ outputOrder := by
  cases o <;> simp [outputOrder]

def commandsFor (p : Parameters) (order : List Output) : List (Command (Reg p)) :=
  order.flatMap (block p)

def outputsAfter (p : Parameters) (ins : Input → Nat) :
    List Output → (Output → Nat) → Output → Nat
  | [], outs => outs
  | o :: tail, outs => outputsAfter p ins tail
      (Function.update outs o (eval ins (expression p o)))

theorem outputsAfter_eq (p : Parameters) (ins : Input → Nat) (order : List Output)
    (outs : Output → Nat) (o : Output) :
    outputsAfter p ins order outs o =
      if o ∈ order then eval ins (expression p o) else outs o := by
  induction order generalizing outs with
  | nil => simp [outputsAfter]
  | cons q tail ih =>
      rw [outputsAfter, ih]
      by_cases ht : o ∈ tail
      · simp [ht]
      · by_cases he : o = q
        · subst o; simp [ht]
        · simp [ht, he]

theorem commandsFor_correct (p : Parameters) (order : List Output)
    (ins : Input → Nat) (outs : Output → Nat) :
    Ready (commandsFor p order) (frame p ins outs) ∧
      resultOf (commandsFor p order) (frame p ins outs) =
        frame p ins (outputsAfter p ins order outs) := by
  induction order generalizing outs with
  | nil => exact ⟨True.intro, rfl⟩
  | cons o tail ih =>
      have head := block_correct p o ins outs
      have rest := ih (Function.update outs o (eval ins (expression p o)))
      constructor
      · change Ready (block p o ++ commandsFor p tail) (frame p ins outs)
        rw [Ready_append, head.2]
        exact ⟨head.1, rest.1⟩
      · change resultOf (block p o ++ commandsFor p tail) (frame p ins outs) = _
        rw [resultOf_append, head.2]
        exact rest.2

def commands (p : Parameters) := commandsFor p outputOrder

theorem commands_correct (p : Parameters) (n m k : Nat) :
    Ready (commands p) (frame p (inputs n m k) (fun _ => 0)) ∧
      resultOf (commands p) (frame p (inputs n m k) (fun _ => 0)) =
        frame p (inputs n m k) (outputValues p n m k) := by
  have h := commandsFor_correct p outputOrder (inputs n m k) (fun _ => 0)
  refine ⟨h.1, h.2.trans ?_⟩
  congr 1
  funext o
  rw [outputsAfter_eq, ite_eq_left (mem_outputOrder o), eval_expression]

variable {K A : Type} [DecidableEq K]

def code (p : Parameters) (slot : (Reg p ⊕ Fin 6) ↪ K) :
    Code (K := K) (S := BinaryAddMachine.State A) :=
  BinaryRegisterStructured.code slot (commands p)

def timePolynomial (p : Parameters) : Polynomial Nat :=
  runtimePolynomial (commands p) + Polynomial.C 1

theorem exec (p : Parameters) (slot : (Reg p ⊕ Fin 6) ↪ K)
    (base : K → List Bool) (n m k width : Nat)
    (hn : n.size ≤ width) (hm : m.size ≤ width) (hk : k.size ≤ width)
    (ambient : A) :
    ∃ steps ≤ (timePolynomial p).eval width,
      Exec (code p slot)
        ⟨BinaryAddMachine.clean ambient,
          registerTapes slot base (frame p (inputs n m k) (fun _ => 0))⟩ steps
        ⟨BinaryAddMachine.clean ambient,
          registerTapes slot base (frame p (inputs n m k) (outputValues p n m k))⟩ := by
  have hwidth : ∀ r, (frame p (inputs n m k) (fun _ => 0) r).size ≤ width := by
    intro r
    rcases r with a | (o | temp)
    · cases a
      · exact hn
      · exact hm
      · exact hk
    · simp [frame]
    · simp [frame]
  have correct := commands_correct p n m k
  obtain ⟨steps, bound, run⟩ := BinaryRegisterStructured.exec_polynomial slot (commands p)
    base (frame p (inputs n m k) (fun _ => 0)) correct.1 ambient width hwidth
  refine ⟨steps, ?_, ?_⟩
  · simpa only [timePolynomial, Polynomial.eval_add, Polynomial.eval_C] using bound
  · rw [correct.2] at run
    exact run

end

open BinaryRegisterProgram FiniteTapeProgram PackingSetupExpression

theorem exec_graph {K A : Type} [DecidableEq K]
    (p : Parameters) (slot : (Reg p ⊕ Fin 6) ↪ K) (base : K → List Bool)
    (x : GraphReductionInput) (ambient : A) :
    ∃ steps ≤ (timePolynomial p).eval (graphBits x).length,
      Exec (code p slot)
        ⟨BinaryAddMachine.clean ambient,
          registerTapes slot base
            (frame p (inputs x.graph.n x.graph.edges.length x.k) (fun _ => 0))⟩ steps
        ⟨BinaryAddMachine.clean ambient,
          registerTapes slot base
            (frame p (inputs x.graph.n x.graph.edges.length x.k)
              (outputValues p x.graph.n x.graph.edges.length x.k))⟩ :=
  exec p slot base x.graph.n x.graph.edges.length x.k (graphBits x).length
    ((nat_size_le_self x.graph.n).trans (graph_vertexCount_le_bits x))
    ((nat_size_le_self x.graph.edges.length).trans (graph_edgeCount_le_bits x))
    ((nat_size_le_self x.k).trans (x.k_le.trans (graph_vertexCount_le_bits x))) ambient

end BinPackingGap.PackingSetupMachine

end

end OAI