File size: 15,350 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 | import Mathlib.Tactic.DeriveFintype
import OAI.Computability.BinPacking.Information.SourceFieldArray
namespace OAI
namespace BinPackingCompleteness.NormalizationReadMachine
open Turing
open BinPackingGames.Foundations
open Complexity Complexity.MachineComposition
open BinPackingGames.Reduction.MachineTransfer
abbrev Signs := Fin 3 → Bool
abbrev Counters := Fin 3 → List Bool
abbrev State (A : Type) := (A × Signs) × Option Bool
abbrev Alphabet {K : Type} (_ : K) := Bool
def clean {A : Type} (ambient : A) (signs : Signs) : State A := ((ambient, signs), none)
def clauseSigns {n : Nat} (clause : Target.Clause n) : Signs := fun i => clause[i].positive
def clauseCounters {n : Nat} (clause : Target.Clause n) : Counters :=
fun i => List.replicate clause[i].variableIndex.val true
inductive Label where
| field (slot : Fin 3)
| sign (slot : Fin 3)
deriving DecidableEq, Fintype
section Placement
variable {K Λ A : Type} [DecidableEq K]
def afterSign (labels : Label → Λ) (exit : Option Λ) (slot : Fin 3) : Option Λ :=
if slot = 0 then some (labels (.field 1))
else if slot = 1 then some (labels (.field 2)) else exit
@[simp] theorem afterSign_zero (labels : Label → Λ) (exit : Option Λ) :
afterSign labels exit 0 = some (labels (.field 1)) := rfl
@[simp] theorem afterSign_one (labels : Label → Λ) (exit : Option Λ) :
afterSign labels exit 1 = some (labels (.field 2)) := rfl
@[simp] theorem afterSign_two (labels : Label → Λ) (exit : Option Λ) :
afterSign labels exit 2 = exit := rfl
def saveSign (source : K) (slot : Fin 3) (bit : Bool) (exit : Option Λ) :
TM2.Stmt (Alphabet (K := K)) Λ (State A) :=
.load (fun state => clean state.1.1 (Function.update state.1.2 slot bit))
(exitAt source exit)
def rejectSign (source : K) (exit : Option Λ) :
TM2.Stmt (Alphabet (K := K)) Λ (State A) :=
.load (fun state => clean state.1.1 state.1.2) (exitAt source exit)
def signInstruction (source : K) (slot : Fin 3) (exit rejected : Option Λ) :
TM2.Stmt (Alphabet (K := K)) Λ (State A) :=
.pop source (fun state head => (state.1, head))
(.branch (fun state => state.2.isNone)
(rejectSign source rejected)
(.branch (fun state => state.2.getD false)
(.pop source (fun state head => (state.1, head))
(.branch (fun state => state.2 == some false)
(saveSign source slot true exit) (rejectSign source rejected)))
(saveSign source slot false exit)))
def instruction (tape : Fin 4 → K) (labels : Label → Λ) (exit rejected : Option Λ) :
Label → TM2.Stmt (Alphabet (K := K)) Λ (State A)
| .field slot => Hastad.SourceMachine.fieldLoop (tape 0) (tape slot.succ)
(labels (.field slot)) (some (labels (.sign slot)))
| .sign slot => signInstruction (tape 0) slot (afterSign labels exit slot) rejected
def tapes (tape : Fin 4 → K) (base : K → List Bool) (input : List Bool)
(counter : Counters) : K → List Bool :=
Function.update (Function.update (Function.update (Function.update base
(tape 0) input) (tape 1) (counter 0)) (tape 2) (counter 1)) (tape 3) (counter 2)
theorem tapes_input (tape : Fin 4 → K) (distinct : Function.Injective tape)
(base : K → List Bool) (input : List Bool) (counter : Counters) :
tapes tape base input counter (tape 0) = input := by
have hd (i j : Fin 4) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h)
simp [tapes, hd]
theorem tapes_counter (tape : Fin 4 → K) (distinct : Function.Injective tape)
(base : K → List Bool) (input : List Bool) (counter : Counters) (slot : Fin 3) :
tapes tape base input counter (tape slot.succ) = counter slot := by
have hd (i j : Fin 4) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h)
fin_cases slot <;> simp [tapes, hd]
theorem tapes_other (tape : Fin 4 → K) (base : K → List Bool)
(input : List Bool) (counter : Counters) (k : K) (other : ∀ i, k ≠ tape i) :
tapes tape base input counter k = base k := by
simp [tapes, other]
theorem update_input (tape : Fin 4 → K) (distinct : Function.Injective tape)
(base : K → List Bool) (input replacement : List Bool) (counter : Counters) :
Function.update (tapes tape base input counter) (tape 0) replacement =
tapes tape base replacement counter := by
have hd (i j : Fin 4) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h)
funext k
by_cases hk : k = tape 0
· subst k
simp [tapes, hd]
· simp [tapes, hk, Function.update_apply]
theorem update_counter (tape : Fin 4 → K) (distinct : Function.Injective tape)
(base : K → List Bool) (input replacement : List Bool) (counter : Counters) (slot : Fin 3) :
Function.update (tapes tape base input counter) (tape slot.succ) replacement =
tapes tape base input (Function.update counter slot replacement) := by
have hd (i j : Fin 4) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h)
funext k
by_cases hk : k = tape slot.succ
· subst k
fin_cases slot <;> simp [tapes, hd]
· fin_cases slot
· change k ≠ tape 1 at hk
simp [tapes, hk, Function.update_apply]
· change k ≠ tape 2 at hk
simp [tapes, hk, Function.update_apply]
· change k ≠ tape 3 at hk
simp [tapes, hk, Function.update_apply]
theorem fieldTapes_eq (tape : Fin 4 → K) (distinct : Function.Injective tape)
(base : K → List Bool) (input replacement output : List Bool)
(counter : Counters) (slot : Fin 3) :
Hastad.SourceMachine.fieldTapes (tape 0) (tape slot.succ)
(tapes tape base input counter) replacement output =
tapes tape base replacement (Function.update counter slot output) := by
unfold Hastad.SourceMachine.fieldTapes
rw [update_input tape distinct, update_counter tape distinct]
variable (tape : Fin 4 → K) (distinct : Function.Injective tape)
variable (labels : Label → Λ) (exit rejected : Option Λ)
variable (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State A))
variable (atLabels : ∀ label, program (labels label) = instruction tape labels exit rejected label)
variable (base : K → List Bool) (ambient : A)
include distinct atLabels
theorem fieldTrace (slot : Fin 3) (value : Nat) (suffix : List Bool)
(counter : Counters) (signs : Signs) (register : Option Bool) :
(advance (TM2.step program))^[value + 1]
(some ⟨some (labels (.field slot)), ((ambient, signs), register),
tapes tape base (encodeWord value ++ suffix) counter⟩) =
some ⟨some (labels (.sign slot)), clean ambient signs,
tapes tape base suffix
(Function.update counter slot (List.replicate value true ++ counter slot))⟩ := by
have distinctSlot : tape 0 ≠ tape slot.succ := by
intro h
have same := congrArg Fin.val (distinct h)
simp only [Fin.val_zero, Fin.val_succ] at same
omega
have h := Hastad.SourceMachine.fieldLoopTrace (tape 0) (tape slot.succ) distinctSlot
(labels (.field slot)) (some (labels (.sign slot))) program (atLabels (.field slot))
(tapes tape base (encodeWord value ++ suffix) counter) value suffix (counter slot)
(ambient, signs) register
simpa only [fieldTapes_eq tape distinct, Function.update_eq_self, clean] using h
theorem signStep (slot : Fin 3) (sign : Bool) (suffix : List Bool)
(counter : Counters) (signs : Signs) (register : Option Bool) :
TM2.step program
⟨some (labels (.sign slot)), ((ambient, signs), register),
tapes tape base (encodeWord (if sign then 1 else 0) ++ suffix) counter⟩ =
some ⟨afterSign labels exit slot, clean ambient (Function.update signs slot sign),
tapes tape base suffix counter⟩ := by
change some (TM2.stepAux (program (labels (.sign slot))) _ _) = _
rw [atLabels (.sign slot)]
cases sign <;> cases h : afterSign labels exit slot <;>
simp [instruction, signInstruction, saveSign, rejectSign, encodeWord,
TM2.stepAux, exitAt, h, clean, tapes_input tape distinct, update_input tape distinct]
omit [DecidableEq K] distinct atLabels in
private theorem joinTrace {X : Type*} {f : X → X} {a b c : X} {n m : Nat}
(first : f^[n] a = b) (second : f^[m] b = c) : f^[n + m] a = c := by
rw [Nat.add_comm, Function.iterate_add_apply, first, second]
theorem literalTrace {n : Nat} (slot : Fin 3) (literal : Target.Literal n)
(suffix : List Bool) (counter : Counters) (signs : Signs) (register : Option Bool) :
(advance (TM2.step program))^[literal.variableIndex.val + 2]
(some ⟨some (labels (.field slot)), ((ambient, signs), register),
tapes tape base (encodeWords (literalWords literal) ++ suffix) counter⟩) =
some ⟨afterSign labels exit slot, clean ambient (Function.update signs slot literal.positive),
tapes tape base suffix (Function.update counter slot
(List.replicate literal.variableIndex.val true ++ counter slot))⟩ := by
have first := fieldTrace tape distinct labels exit rejected program atLabels base ambient
slot literal.variableIndex.val (encodeWord (if literal.positive then 1 else 0) ++ suffix)
counter signs register
have second := signStep tape distinct labels exit rejected program atLabels base ambient
slot literal.positive suffix
(Function.update counter slot (List.replicate literal.variableIndex.val true ++ counter slot))
signs none
change (advance (TM2.step program))^[1]
(some ⟨some (labels (.sign slot)), clean ambient signs,
tapes tape base (encodeWord (if literal.positive then 1 else 0) ++ suffix)
(Function.update counter slot (List.replicate literal.variableIndex.val true ++ counter slot))⟩) = _
at second
have full := joinTrace first second
simpa only [literalWords, encodeWords, List.append_nil, List.append_assoc,
Nat.add_assoc] using full
theorem clauseTrace {n : Nat} (clause : Target.Clause n) (suffix : List Bool)
(signs : Signs) (register : Option Bool) :
(advance (TM2.step program))^[clause[0].variableIndex.val +
clause[1].variableIndex.val + clause[2].variableIndex.val + 6]
(some ⟨some (labels (.field 0)), ((ambient, signs), register),
tapes tape base (encodeWords (clauseWords clause) ++ suffix) (fun _ => [])⟩) =
some ⟨exit, clean ambient (clauseSigns clause),
tapes tape base suffix (clauseCounters clause)⟩ := by
let counter₀ : Counters := Function.update (fun _ => []) 0
(List.replicate clause[0].variableIndex.val true)
let counter₁ : Counters := Function.update counter₀ 1
(List.replicate clause[1].variableIndex.val true)
let counter₂ : Counters := Function.update counter₁ 2
(List.replicate clause[2].variableIndex.val true)
let signs₀ := Function.update signs 0 clause[0].positive
let signs₁ := Function.update signs₀ 1 clause[1].positive
let signs₂ := Function.update signs₁ 2 clause[2].positive
have counter₀_one : counter₀ 1 = [] := rfl
have counter₁_two : counter₁ 2 = [] := rfl
have first := literalTrace tape distinct labels exit rejected program atLabels base ambient
0 clause[0] (encodeWords (literalWords clause[1]) ++
(encodeWords (literalWords clause[2]) ++ suffix)) (fun _ => []) signs register
have second := literalTrace tape distinct labels exit rejected program atLabels base ambient
1 clause[1] (encodeWords (literalWords clause[2]) ++ suffix) counter₀ signs₀ none
have third := literalTrace tape distinct labels exit rejected program atLabels base ambient
2 clause[2] suffix counter₁ signs₁ none
simp only [counter₀_one, counter₁_two, List.append_nil,
afterSign_zero, afterSign_one, afterSign_two] at first second third
have full := joinTrace (joinTrace first second) third
have counters_eq : counter₂ = clauseCounters clause := by
funext i
fin_cases i <;> simp [counter₂, counter₁, counter₀, clauseCounters]
have signs_eq : signs₂ = clauseSigns clause := by
funext i
fin_cases i <;> simp [signs₂, signs₁, signs₀, clauseSigns]
have htime : (clause[0].variableIndex.val + 2 + (clause[1].variableIndex.val + 2)) +
(clause[2].variableIndex.val + 2) =
clause[0].variableIndex.val + clause[1].variableIndex.val + clause[2].variableIndex.val + 6 := by omega
rw [htime] at full
change (advance (TM2.step program))^[_]
(some ⟨some (labels (.field 0)), ((ambient, signs), register),
tapes tape base (encodeWords (literalWords clause[0]) ++
(encodeWords (literalWords clause[1]) ++ (encodeWords (literalWords clause[2]) ++ suffix)))
(fun _ => [])⟩) =
some ⟨exit, clean ambient signs₂, tapes tape base suffix counter₂⟩ at full
rw [signs_eq, counters_eq] at full
simpa only [clauseWords, encodeWords_append, List.append_assoc] using full
def clauseInTime {n : Nat} (clause : Target.Clause n) (suffix : List Bool)
(signs : Signs) (register : Option Bool) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some (labels (.field 0)), ((ambient, signs), register),
tapes tape base (encodeWords (clauseWords clause) ++ suffix) (fun _ => [])⟩
(some ⟨exit, clean ambient (clauseSigns clause),
tapes tape base suffix (clauseCounters clause)⟩)
(clause[0].variableIndex.val + clause[1].variableIndex.val + clause[2].variableIndex.val + 6) where
steps := clause[0].variableIndex.val + clause[1].variableIndex.val + clause[2].variableIndex.val + 6
evals_in_steps := clauseTrace tape distinct labels exit rejected program atLabels base ambient
clause suffix signs register
steps_le_m := Nat.le_refl _
end Placement
theorem steps_le_encoded_length {n : Nat} (clause : Target.Clause n) :
clause[0].variableIndex.val + clause[1].variableIndex.val + clause[2].variableIndex.val + 6 ≤
(encodeWords (clauseWords clause)).length := by
simp only [clauseWords, encodeWords_append, List.length_append,
literalWords, encodeWords, List.length_append, encodeWord_length, List.length_nil]
omega
abbrev ConcreteLabel := Label ⊕ Bool
def concreteProgram : ConcreteLabel → TM2.Stmt (Alphabet (K := Fin 4)) ConcreteLabel (State Unit)
| .inl label => instruction id Sum.inl (some (.inr true)) (some (.inr false)) label
| .inr _ => .halt
abbrev machine : FinTM2 where
K := Fin 4
k₀ := 0
k₁ := 1
Γ := Alphabet
Λ := ConcreteLabel
main := .inl (.field 0)
σ := State Unit
initialState := clean () (fun _ => false)
m := concreteProgram
def machineInTime {n : Nat} (clause : Target.Clause n) (suffix : List Bool) :
StateTransition.EvalsToInTime machine.step
⟨some (.inl (.field 0)), clean () (fun _ => false),
tapes id (fun _ : Fin 4 => []) (encodeWords (clauseWords clause) ++ suffix) (fun _ => [])⟩
(some ⟨some (.inr true), clean () (clauseSigns clause),
tapes id (fun _ : Fin 4 => []) suffix (clauseCounters clause)⟩)
(clause[0].variableIndex.val + clause[1].variableIndex.val + clause[2].variableIndex.val + 6) :=
clauseInTime id (fun _ _ h => h) Sum.inl (some (.inr true)) (some (.inr false))
concreteProgram (fun _ => rfl) (fun _ => []) () clause suffix (fun _ => false) none
theorem machine_finiteAlphabet (k : machine.K) : Finite (machine.Γ k) := by
change Finite Bool
infer_instance
end BinPackingCompleteness.NormalizationReadMachine
end OAI
|