File size: 4,388 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
import OAI.Computability.BinPacking.Arithmetic.NatExpressionBounds
import OAI.Computability.BinPacking.Machines.PackingCountedProgram

namespace OAI

namespace BinPackingGap.PackingRegisterUpdate

open Turing
open BinPackingGames.Foundations.Complexity
open BinaryRegisterProgram
open FiniteTapeProgram PackingMachineBlocks

inductive Kind
  | add | multiply
  deriving DecidableEq

instance : Fintype Kind where
  elems := {.add, .multiply}
  complete k := by cases k <;> simp

def operation (kind : Kind) : Nat → Nat → Nat :=
  match kind with
  | .add => Nat.add
  | .multiply => Nat.mul

def operands : Operands (Fin 3) where
  destination := 2
  left := 0
  right := 1
  left_ne_right := by decide
  left_ne_destination := by decide
  right_ne_destination := by decide

def operator : Kind → Command (Fin 3)
  | .add => .add operands
  | .multiply => .mul operands

def commands (kind : Kind) : List (Command (Fin 3)) :=
  [operator kind, .clear 1, .copy 1 2 (by decide), .clear 2]

def values (a b : Nat) : Fin 3 → Nat := ![a, b, 0]

theorem commands_ready (kind : Kind) (a b : Nat) : Ready (commands kind) (values a b) := by
  cases kind <;>
    simp [commands, operator, Ready, commandReady, result, destination, value, operands, values]

theorem commands_result (kind : Kind) (a b : Nat) :
    resultOf (commands kind) (values a b) = values a (operation kind a b) := by
  funext r
  cases kind <;> fin_cases r <;>
    simp [commands, operator, resultOf_cons, result, destination, value, operands, values,
      operation]

theorem operation_size_le (kind : Kind) (a b : Nat) :
    (operation kind a b).size ≤ a.size + b.size + 1 := by
  cases kind with
  | add =>
      have h := BinaryArithmetic.size_add_le a b
      change (a + b).size ≤ a.size + b.size + 1
      omega
  | multiply =>
      exact (BinaryArithmetic.size_mul_le a b).trans (Nat.le_succ _)

theorem commands_budget (kind : Kind) (a b : Nat) :
    budget (commands kind) (values a b) =
      cost (operator kind) (values a b) + b.size + 3 * (operation kind a b).size + 4 := by
  cases kind <;>
    simp [commands, operator, budget, MachineFiniteSequence.steps, result, destination,
      value, operands, values, cost, operation] <;> omega

theorem commands_budget_le (kind : Kind) (a b : Nat) :
    budget (commands kind) (values a b) + 1 ≤ 64 * (a.size + b.size + 1) ^ 2 := by
  have hsize := operation_size_le kind a b
  have hw : 1 ≤ a.size + b.size + 1 := by omega
  have hsquare : a.size + b.size + 1 ≤ (a.size + b.size + 1) ^ 2 := by nlinarith
  rw [commands_budget]
  cases kind <;> simp [operator, cost, operands, values, operation] at * <;> nlinarith

def main (kind : Kind) : BinaryRegisterProgram.Label (commands kind) :=
  .inl (BinaryRegisterProgram.main (operator kind))

variable {K : Type} [DecidableEq K]

def code (slots : (Fin 3 ⊕ Fin 6) ↪ K) (kind : Kind) : Code (K := K) (S := State) :=
  invoke arithmeticControl (BinaryAddMachine.clean ()) (main kind)
    (BinaryRegisterProgram.program slots (commands kind))

theorem exec (slots : (Fin 3 ⊕ Fin 6) ↪ K) (kind : Kind)
    (base : K → List Bool) (a b : Nat) (state : State) :
    ∃ steps ≤ 64 * (a.size + b.size + 1) ^ 2,
      Exec (code slots kind) ⟨state, registerTapes slots base (values a b)⟩ steps
        ⟨.arithmetic (BinaryAddMachine.clean ()),
          registerTapes slots base (values a (operation kind a b))⟩ := by
  have run := BinaryRegisterProgram.programInTime slots (commands kind) base
    (values a b) (commands_ready kind a b) ()
  rw [commands_result] at run
  have run' : StateTransition.EvalsToInTime
      (TM2.step (BinaryRegisterProgram.program (A := Unit) slots (commands kind)))
      ⟨some (main kind), BinaryAddMachine.clean (), registerTapes slots base (values a b)⟩
      (some ⟨none, BinaryAddMachine.clean (),
        registerTapes slots base (values a (operation kind a b))⟩)
      (budget (commands kind) (values a b)) := run
  obtain ⟨n, hn, hrun⟩ := invokeExecInTime arithmeticControl (BinaryAddMachine.clean ())
    (main kind) (BinaryRegisterProgram.program slots (commands kind))
    ⟨state, registerTapes slots base (values a b)⟩ (BinaryAddMachine.clean ())
    (registerTapes slots base (values a (operation kind a b))) _ run'
  exact ⟨n, hn.trans (commands_budget_le kind a b), hrun⟩

end BinPackingGap.PackingRegisterUpdate

end OAI