File size: 12,246 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 | import OAI.Computability.BinPacking.Computation.MachineHorner
namespace OAI
namespace BinPackingGap.PackingTokenAffine
open Turing
open BinPackingGames.Foundations.Complexity
open BinPackingGames.Reduction
open MachineSubstitution (pushWord stepAux_pushWord)
def transition (_ : Unit) (_ : Bool) : Unit := ()
def emit (coefficient : Nat) (_ : Unit) : Bool → List Bool
| true => List.replicate coefficient true
| false => []
def scanOutput (coefficient : Nat) (input : List Bool) : List Bool :=
MachineTransducer.output transition (emit coefficient) () input
@[simp] theorem scanOutput_nil (coefficient : Nat) : scanOutput coefficient [] = [] := rfl
theorem scanOutput_cons (coefficient : Nat) (bit : Bool) (rest : List Bool) :
scanOutput coefficient (bit :: rest) =
emit coefficient () bit ++ scanOutput coefficient rest := rfl
theorem scanOutput_replicate_append (coefficient a : Nat) (suffix : List Bool) :
scanOutput coefficient (List.replicate a true ++ suffix) =
List.replicate (coefficient * a) true ++ scanOutput coefficient suffix := by
induction a with
| zero => simp only [List.replicate_zero, List.nil_append, Nat.mul_zero]
| succ a ih =>
calc
scanOutput coefficient (List.replicate (a + 1) true ++ suffix) =
List.replicate coefficient true ++
(List.replicate (coefficient * a) true ++ scanOutput coefficient suffix) := by
rw [List.replicate_succ, List.cons_append, scanOutput_cons]
simp only [emit, ih]
_ = List.replicate (coefficient + coefficient * a) true ++
scanOutput coefficient suffix := by
rw [List.replicate_add, List.append_assoc]
_ = List.replicate (coefficient * (a + 1)) true ++
scanOutput coefficient suffix := by
simp only [Nat.mul_succ, Nat.add_comm]
@[simp] theorem scanOutput_replicate (coefficient a : Nat) :
scanOutput coefficient (List.replicate a true) =
List.replicate (coefficient * a) true := by
simpa only [List.append_nil, scanOutput_nil] using
scanOutput_replicate_append coefficient a []
@[simp] theorem scanOutput_encodeWord (coefficient a : Nat) :
scanOutput coefficient (encodeWord a) = List.replicate (coefficient * a) true := by
rw [encodeWord, scanOutput_replicate_append]
simp only [scanOutput_cons, emit, scanOutput_nil, List.append_nil]
theorem tokens_encodeWord (a b : Nat) (suffix : List Bool) :
List.replicate a true ++ (encodeWord b ++ suffix) =
encodeWord (a + b) ++ suffix := by
simp only [encodeWord, List.replicate_add, List.append_assoc]
theorem emit_length_le (coefficient : Nat) (control : Unit) (bit : Bool) :
(emit coefficient control bit).length ≤ coefficient := by
cases bit <;> simp [emit]
abbrev State (A : Type) := MachineTransducerCopy.State A Unit
def clean {A : Type} (ambient : A) : State A := ((ambient, ()), none)
inductive Label
| seed
| scan
| restore
| emitter (bit : Bool)
deriving DecidableEq, Fintype
variable {K Λ A : Type} [DecidableEq K]
def instruction (coefficient offset : Nat) (slots : Fin 3 ↪ K)
(labels : Label → Λ) (exit : Option Λ) :
Label → TM2.Stmt (MachineTransducerCopy.Alphabet K) Λ (State A)
| .seed => pushWord (slots 2) (encodeWord offset).reverse
(.goto fun _ => labels .scan)
| .scan => MachineTransducerCopy.scanLoop (slots 0) (slots 1) ()
(fun _ bit => labels (.emitter bit)) (labels .restore)
| .emitter bit => MachineTransducerCopy.emitter (slots 2) transition (emit coefficient)
(labels .scan) () bit
| .restore => MachineTransfer.loopAt (slots 1) (slots 0) id false
(labels .restore) exit
theorem scanTrace (coefficient offset : Nat) (slots : Fin 3 ↪ K)
(labels : Label → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (MachineTransducerCopy.Alphabet K) Λ (State A))
(atLabels : ∀ label,
program (labels label) = instruction coefficient offset slots labels exit label)
(base : K → List Bool) (scratchEmpty : base (slots 1) = []) (ambient : A) :
(MachineComposition.advance (TM2.step program))^[3 * (base (slots 0)).length + 2]
(some ⟨some (labels .scan), clean ambient, base⟩) =
some ⟨exit, clean ambient,
Function.update base (slots 2)
((scanOutput coefficient (base (slots 0))).reverse ++ base (slots 2))⟩ := by
have hd (i j : Fin 3) (hne : i ≠ j) : slots i ≠ slots j := slots.injective.ne hne
have run := MachineTransducerCopy.transduceCopyTrace (slots 0) (slots 1) (slots 2)
(hd 0 1 (by decide)) (hd 0 2 (by decide)) (hd 1 2 (by decide))
() transition (emit coefficient) (labels .scan) (labels .restore)
(fun _ bit => labels (.emitter bit)) exit program
(atLabels .scan) (fun control bit => by
cases control
exact atLabels (.emitter bit))
(atLabels .restore) base scratchEmpty ambient () none
simpa only [scanOutput, clean] using run
theorem tokensTrace (coefficient offset : Nat) (slots : Fin 3 ↪ K)
(labels : Label → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (MachineTransducerCopy.Alphabet K) Λ (State A))
(atLabels : ∀ label,
program (labels label) = instruction coefficient offset slots labels exit label)
(base : K → List Bool) (a : Nat)
(sourceWord : base (slots 0) = List.replicate a true)
(scratchEmpty : base (slots 1) = []) (ambient : A) :
(MachineComposition.advance (TM2.step program))^[3 * a + 2]
(some ⟨some (labels .scan), clean ambient, base⟩) =
some ⟨exit, clean ambient,
Function.update base (slots 2)
(List.replicate (coefficient * a) true ++ base (slots 2))⟩ := by
simpa only [sourceWord, List.length_replicate, scanOutput_replicate,
List.reverse_replicate] using
scanTrace coefficient offset slots labels exit program atLabels base scratchEmpty ambient
theorem seedStep (coefficient offset : Nat) (slots : Fin 3 ↪ K)
(labels : Label → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (MachineTransducerCopy.Alphabet K) Λ (State A))
(atLabels : ∀ label,
program (labels label) = instruction coefficient offset slots labels exit label)
(base : K → List Bool) (state : State A) :
TM2.step program ⟨some (labels .seed), state, base⟩ =
some ⟨some (labels .scan), state,
Function.update base (slots 2) (encodeWord offset ++ base (slots 2))⟩ := by
change some (TM2.stepAux (program (labels .seed)) state base) = _
rw [atLabels .seed]
simp only [instruction, stepAux_pushWord, List.reverse_reverse, TM2.stepAux]
theorem seededScanTrace (coefficient offset : Nat) (slots : Fin 3 ↪ K)
(labels : Label → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (MachineTransducerCopy.Alphabet K) Λ (State A))
(atLabels : ∀ label,
program (labels label) = instruction coefficient offset slots labels exit label)
(base : K → List Bool) (scratchEmpty : base (slots 1) = []) (ambient : A) :
(MachineComposition.advance (TM2.step program))^[3 * (base (slots 0)).length + 3]
(some ⟨some (labels .seed), clean ambient, base⟩) =
some ⟨exit, clean ambient,
Function.update base (slots 2)
((scanOutput coefficient (base (slots 0))).reverse ++
(encodeWord offset ++ base (slots 2)))⟩ := by
let seeded := Function.update base (slots 2) (encodeWord offset ++ base (slots 2))
have sourceDestination : slots 0 ≠ slots 2 := slots.injective.ne (by decide)
have scratchDestination : slots 1 ≠ slots 2 := slots.injective.ne (by decide)
have sourceSame : seeded (slots 0) = base (slots 0) := by
simp [seeded, sourceDestination]
have scratchSame : seeded (slots 1) = [] := by
simp [seeded, scratchDestination, scratchEmpty]
have run := scanTrace coefficient offset slots labels exit program atLabels
seeded scratchSame ambient
rw [sourceSame] at run
simp only [seeded, Function.update_self, Function.update_idem] at run
rw [show 3 * (base (slots 0)).length + 3 =
(3 * (base (slots 0)).length + 2) + 1 by omega,
Function.iterate_succ_apply, MachineComposition.advance_some,
seedStep coefficient offset slots labels exit program atLabels]
exact run
theorem seededTrace (coefficient offset : Nat) (slots : Fin 3 ↪ K)
(labels : Label → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (MachineTransducerCopy.Alphabet K) Λ (State A))
(atLabels : ∀ label,
program (labels label) = instruction coefficient offset slots labels exit label)
(base : K → List Bool) (a : Nat)
(sourceWord : base (slots 0) = List.replicate a true)
(scratchEmpty : base (slots 1) = []) (ambient : A) :
(MachineComposition.advance (TM2.step program))^[3 * a + 3]
(some ⟨some (labels .seed), clean ambient, base⟩) =
some ⟨exit, clean ambient,
Function.update base (slots 2)
(encodeWord (coefficient * a + offset) ++ base (slots 2))⟩ := by
simpa only [sourceWord, List.length_replicate, scanOutput_replicate,
List.reverse_replicate, tokens_encodeWord] using
seededScanTrace coefficient offset slots labels exit program atLabels base scratchEmpty ambient
theorem encodedAccumulateTrace (coefficient offset : Nat) (slots : Fin 3 ↪ K)
(labels : Label → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (MachineTransducerCopy.Alphabet K) Λ (State A))
(atLabels : ∀ label,
program (labels label) = instruction coefficient offset slots labels exit label)
(base : K → List Bool) (a b : Nat) (suffix : List Bool)
(sourceWord : base (slots 0) = encodeWord a)
(scratchEmpty : base (slots 1) = [])
(destinationWord : base (slots 2) = encodeWord b ++ suffix) (ambient : A) :
(MachineComposition.advance (TM2.step program))^[3 * (a + 1) + 2]
(some ⟨some (labels .scan), clean ambient, base⟩) =
some ⟨exit, clean ambient,
Function.update base (slots 2) (encodeWord (coefficient * a + b) ++ suffix)⟩ := by
simpa only [sourceWord, encodeWord_length, scanOutput_encodeWord,
List.reverse_replicate, destinationWord, tokens_encodeWord] using
scanTrace coefficient offset slots labels exit program atLabels base scratchEmpty ambient
def seededInTime (coefficient offset : Nat) (slots : Fin 3 ↪ K)
(labels : Label → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (MachineTransducerCopy.Alphabet K) Λ (State A))
(atLabels : ∀ label,
program (labels label) = instruction coefficient offset slots labels exit label)
(base : K → List Bool) (a : Nat)
(sourceWord : base (slots 0) = List.replicate a true)
(scratchEmpty : base (slots 1) = []) (ambient : A) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some (labels .seed), clean ambient, base⟩
(some ⟨exit, clean ambient,
Function.update base (slots 2)
(encodeWord (coefficient * a + offset) ++ base (slots 2))⟩)
(3 * a + 3) where
steps := 3 * a + 3
evals_in_steps := seededTrace coefficient offset slots labels exit program atLabels
base a sourceWord scratchEmpty ambient
steps_le_m := Nat.le_refl _
def encodedAccumulateInTime (coefficient offset : Nat) (slots : Fin 3 ↪ K)
(labels : Label → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (MachineTransducerCopy.Alphabet K) Λ (State A))
(atLabels : ∀ label,
program (labels label) = instruction coefficient offset slots labels exit label)
(base : K → List Bool) (a b : Nat) (suffix : List Bool)
(sourceWord : base (slots 0) = encodeWord a)
(scratchEmpty : base (slots 1) = [])
(destinationWord : base (slots 2) = encodeWord b ++ suffix) (ambient : A) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some (labels .scan), clean ambient, base⟩
(some ⟨exit, clean ambient,
Function.update base (slots 2) (encodeWord (coefficient * a + b) ++ suffix)⟩)
(3 * (a + 1) + 2) where
steps := 3 * (a + 1) + 2
evals_in_steps := encodedAccumulateTrace coefficient offset slots labels exit program atLabels
base a b suffix sourceWord scratchEmpty destinationWord ambient
steps_le_m := Nat.le_refl _
end BinPackingGap.PackingTokenAffine
end OAI
|