File size: 18,137 Bytes
2f887db | 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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | import Mathlib.Algebra.BigOperators.Fin
import OAI.Computability.PerfectCompleteness.Machines.FiniteBlockMachineLemmas
import OAI.Computability.PerfectCompleteness.Machines.KeyMetadataMachine
import OAI.Computability.UniqueGames.Machines.MachineCompositionLemmas
namespace OAI
namespace PerfectCompleteness.CanonicalKeyMachine
open Turing UniqueGamesTheorem.Foundations.Complexity MachineComposition
open CanonicalKeys CanonicalKeyShape ClauseSupport MixedSupport
open scoped BigOperators Classical
noncomputable section
variable {n : Nat}
def actualSlots (shapes : Fin n → Shape) (occurrences : Fin n → Nat)
(variableIDs : Fin n → Fin 3 → Nat) : Fin n → Slot :=
fun i => KeyMetadataMachine.actualSlot (shapes i) (occurrences i) (variableIDs i)
def recoveredKey (shapes : Fin n → Shape) (modes : Fin n → SupportMode) (side : Side)
(partition : Set (Set (Fin n → ReducedValue)))
(occurrences : Fin n → Nat) (variableIDs : Fin n → Fin 3 → Nat) : Key n where
side := side
retained := retained (actualSlots shapes occurrences variableIDs) modes
partition := partition
def prefixBits (side : Side) (partition : Set (Set (Fin n → ReducedValue))) : List Bool :=
encodeWords (CanonicalKeyEncoding.sideWord side :: CanonicalKeyEncoding.partitionWords partition)
def payload (shapes : Fin n → Shape) (modes : Fin n → SupportMode) (side : Side)
(partition : Set (Set (Fin n → ReducedValue)))
(occurrences : Fin n → Nat) (variableIDs : Fin n → Fin 3 → Nat) : List Bool :=
CanonicalKeyEncoding.bits (recoveredKey shapes modes side partition occurrences variableIDs) ++ [true]
private theorem metadata_words (slots : Fin n → Slot) (modes : Fin n → SupportMode)
(positions : List (Fin n)) :
KeyMetadataEncoding.metadataWords
((positions.map (fun i => (i, recover (slots i) (modes i)))).filter
(fun entry => decide (entry.2 ≠ .dropped))) =
positions.flatMap (fun i => KeyMetadataMachine.retainedWords i (slots i) (modes i)) := by
induction positions with
| nil => rfl
| cons i positions ih =>
by_cases h : recover (slots i) (modes i) = .dropped
· simp [KeyMetadataEncoding.metadataWords, KeyMetadataMachine.retainedWords, h] at ih ⊢
exact ih
· simpa only [List.map_cons, List.filter_cons, h, ne_eq, not_false_eq_true,
decide_true, Bool.cond_true, ite_true, KeyMetadataEncoding.metadataWords_cons,
KeyMetadataMachine.retainedWords, ite_false, List.flatMap_cons] using
congrArg (KeyMetadataEncoding.entryWords (i, recover (slots i) (modes i)) ++ ·) ih
private theorem encodeWords_flatMap {α : Type*} (xs : List α) (words : α → List Nat) :
encodeWords (xs.flatMap words) = xs.flatMap (fun x => encodeWords (words x)) := by
induction xs with
| nil => rfl
| cons x xs ih => simp only [List.flatMap_cons, encodeWords_append, ih]
theorem payload_eq (shapes : Fin n → Shape) (modes : Fin n → SupportMode) (side : Side)
(partition : Set (Set (Fin n → ReducedValue)))
(occurrences : Fin n → Nat) (variableIDs : Fin n → Fin 3 → Nat) :
payload shapes modes side partition occurrences variableIDs =
prefixBits side partition ++
(List.finRange n).flatMap (fun i => KeyMetadataMachine.retainedBits i
(KeyMetadataMachine.actualSlot (shapes i) (occurrences i) (variableIDs i)) (modes i)) ++
[true] := by
change encodeWords
((CanonicalKeyEncoding.sideWord side :: CanonicalKeyEncoding.partitionWords partition) ++
KeyMetadataEncoding.metadataWords (retained (actualSlots shapes occurrences variableIDs) modes)) ++
[true] = _
rw [encodeWords_append]
unfold retained
rw [metadata_words, encodeWords_flatMap]
rfl
inductive Command (n : Nat)
| terminal
| metadata (position : Fin n)
| header
deriving DecidableEq, Fintype
def LocalLabel : Command n → Type
| .terminal => Unit
| .metadata _ => KeyMetadataMachine.Label
| .header => Unit
instance localLabelFintype (command : Command n) : Fintype (LocalLabel command) := by
cases command <;> unfold LocalLabel <;> infer_instance
instance localLabelDecidableEq (command : Command n) : DecidableEq (LocalLabel command) := by
cases command <;> unfold LocalLabel <;> infer_instance
def main : (command : Command n) → LocalLabel command
| .terminal => ()
| .metadata _ => .suffix
| .header => ()
def commands (n : Nat) : List (Command n) :=
.terminal :: ((List.finRange n).reverse.map .metadata ++ [.header])
abbrev Label (n : Nat) := MachineFiniteSequence.Label LocalLabel (commands n)
variable {K Λ σ : Type} [DecidableEq K]
abbrev Alphabet (_ : K) := Bool
abbrev State (σ : Type) := KeyMetadataMachine.State σ
def localInstruction (shapes : Fin n → Shape) (modes : Fin n → SupportMode) (side : Side)
(partition : Set (Set (Fin n → ReducedValue))) (tape : Fin n → Fin 6 → K) (output : K) :
(command : Command n) → (LocalLabel command → Λ) → Option Λ →
LocalLabel command → TM2.Stmt (Alphabet (K := K)) Λ (State σ)
| .terminal, _, exit, _ => KeyMetadataMachine.literal output [true] exit
| .metadata i, labels, exit, label =>
KeyMetadataMachine.instruction i (shapes i) (modes i) (tape i) labels exit label
| .header, _, exit, _ => KeyMetadataMachine.literal output (prefixBits side partition) exit
def entry (labels : Label n → Λ) (exit : Option Λ) : Option Λ :=
MachineFiniteSequence.entry LocalLabel main (commands n) labels exit
def instruction (shapes : Fin n → Shape) (modes : Fin n → SupportMode) (side : Side)
(partition : Set (Set (Fin n → ReducedValue))) (tape : Fin n → Fin 6 → K) (output : K)
(labels : Label n → Λ) (exit : Option Λ) :
Label n → TM2.Stmt (Alphabet (K := K)) Λ (State σ) :=
MachineFiniteSequence.instruction LocalLabel main
(localInstruction shapes modes side partition tape output) (commands n) labels exit
def commandBits (shapes : Fin n → Shape) (modes : Fin n → SupportMode) (side : Side)
(partition : Set (Set (Fin n → ReducedValue)))
(occurrences : Fin n → Nat) (variableIDs : Fin n → Fin 3 → Nat) : Command n → List Bool
| .terminal => [true]
| .metadata i => KeyMetadataMachine.retainedBits i
(KeyMetadataMachine.actualSlot (shapes i) (occurrences i) (variableIDs i)) (modes i)
| .header => prefixBits side partition
def commandCost (shapes : Fin n → Shape) (modes : Fin n → SupportMode)
(tape : Fin n → Fin 6 → K) (base : K → List Bool) : Command n → Nat
| .terminal => 1
| .metadata i => match KeyMetadataMachine.sourceField (shapes i) (modes i) with
| none => 1
| some field => 2 * ((base (KeyMetadataMachine.idTape (tape i) field)).length + 1) + 2
| .header => 1
def sourceLength (tape : Fin n → Fin 6 → K) (base : K → List Bool) : Nat :=
∑ i, KeyMetadataMachine.sourceLength (tape i) base
private theorem resultOf_prepend {α : Type} (bits : α → List Bool)
(cs : List α) (out : List Bool) :
MachineFiniteSequence.resultOf (fun c acc => bits c ++ acc) cs out =
cs.reverse.flatMap bits ++ out := by
induction cs generalizing out with
| nil => rfl
| cons c cs ih =>
rw [MachineFiniteSequence.resultOf, ih]
simp only [List.reverse_cons, List.flatMap_append, List.flatMap_cons, List.flatMap_nil,
List.append_nil, List.append_assoc]
private theorem steps_constant {α Data : Type} (result : α → Data → Data) (cost : α → Nat)
(cs : List α) (data : Data) :
MachineFiniteSequence.steps result (fun c _ => cost c) cs data = (cs.map cost).sum := by
induction cs generalizing data with
| nil => rfl
| cons c cs ih => simp only [MachineFiniteSequence.steps, List.map_cons, List.sum_cons, ih]
theorem commands_bits (shapes : Fin n → Shape) (modes : Fin n → SupportMode) (side : Side)
(partition : Set (Set (Fin n → ReducedValue)))
(occurrences : Fin n → Nat) (variableIDs : Fin n → Fin 3 → Nat) :
(commands n).reverse.flatMap (commandBits shapes modes side partition occurrences variableIDs) =
payload shapes modes side partition occurrences variableIDs := by
rw [payload_eq]
simp only [commands, List.reverse_cons, List.reverse_append, List.reverse_nil,
List.map_reverse, List.reverse_reverse, List.flatMap_append, List.flatMap_cons,
List.flatMap_nil, List.flatMap_map, commandBits, List.nil_append, List.append_nil, List.append_assoc]
omit [DecidableEq K] in
theorem commandCost_metadata_le (shapes : Fin n → Shape) (modes : Fin n → SupportMode)
(tape : Fin n → Fin 6 → K) (base : K → List Bool) (i : Fin n) :
commandCost shapes modes tape base (.metadata i) ≤
2 * KeyMetadataMachine.sourceLength (tape i) base + 4 := by
cases h : KeyMetadataMachine.sourceField (shapes i) (modes i) with
| none => simp only [commandCost, h]; omega
| some field =>
have hb := KeyMetadataMachine.selected_length_le (tape i) base field
simp only [commandCost, h]
omega
private theorem commands_cost (cost : Command n → Nat) :
((commands n).map cost).sum = cost .terminal + (∑ i, cost (.metadata i)) + cost .header := by
simp only [commands, List.map_cons, List.map_append, List.map_map, List.map_reverse,
List.sum_cons, List.sum_append, List.sum_reverse, List.map_nil, List.sum_nil, Nat.add_zero]
rw [← Fin.sum_univ_def]
simp only [Function.comp_def, Nat.add_assoc]
omit [DecidableEq K] in
theorem commands_cost_le (shapes : Fin n → Shape) (modes : Fin n → SupportMode)
(tape : Fin n → Fin 6 → K) (base : K → List Bool) :
((commands n).map (commandCost shapes modes tape base)).sum ≤
2 * sourceLength tape base + 4 * n + 2 := by
have hsum : (∑ i, commandCost shapes modes tape base (.metadata i)) ≤
∑ i : Fin n, (2 * KeyMetadataMachine.sourceLength (tape i) base + 4) :=
Finset.sum_le_sum (fun i _ => commandCost_metadata_le shapes modes tape base i)
have htotal : (∑ i : Fin n, (2 * KeyMetadataMachine.sourceLength (tape i) base + 4)) =
2 * sourceLength tape base + 4 * n := by
rw [Finset.sum_add_distrib, ← Finset.mul_sum]
simp only [sourceLength, Finset.sum_const, Finset.card_univ, Fintype.card_fin,
Nat.nsmul_eq_mul, Nat.mul_comm]
rw [htotal] at hsum
rw [commands_cost]
change 1 + (∑ i, commandCost shapes modes tape base (.metadata i)) + 1 ≤ _
omega
omit [DecidableEq K] in
private theorem source_ne_output (tape : Fin n → Fin 6 → K)
(distinct : ∀ i, Function.Injective (tape i)) (output : K)
(sharedOutput : ∀ i, tape i 5 = output) (i : Fin n) (field : Fin 4) :
KeyMetadataMachine.idTape (tape i) field ≠ output := by
intro h
have hi : KeyMetadataMachine.idIndex field = (5 : Fin 6) :=
distinct i (h.trans (sharedOutput i).symm)
have hv := congrArg Fin.val hi
have hf := field.isLt
change field.val = 5 at hv
omega
def keyInTime (shapes : Fin n → Shape) (modes : Fin n → SupportMode) (side : Side)
(partition : Set (Set (Fin n → ReducedValue)))
(occurrences : Fin n → Nat) (variableIDs : Fin n → Fin 3 → Nat)
(tape : Fin n → Fin 6 → K) (distinct : ∀ i, Function.Injective (tape i))
(scratch output : K) (scratchOutput : scratch ≠ output)
(sharedScratch : ∀ i, tape i 4 = scratch) (sharedOutput : ∀ i, tape i 5 = output)
(labels : Label n → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ))
(atLabels : ∀ label, program (labels label) =
instruction shapes modes side partition tape output labels exit label)
(base : K → List Bool)
(sourceWords : ∀ i field, base (KeyMetadataMachine.idTape (tape i) field) =
encodeWord (KeyMetadataMachine.fieldValue (occurrences i) (variableIDs i) field))
(scratchEmpty : base scratch = []) (ambient : σ) :
StateTransition.EvalsToInTime (TM2.step program)
⟨entry labels exit, KeyMetadataMachine.clean ambient, base⟩
(some ⟨exit, KeyMetadataMachine.clean ambient, Function.update base output
(payload shapes modes side partition occurrences variableIDs ++ base output)⟩)
(2 * sourceLength tape base + 4 * n + 2) := by
let bits := commandBits shapes modes side partition occurrences variableIDs
let result : Command n → List Bool → List Bool := fun command out => bits command ++ out
let cost := commandCost shapes modes tape base
let tapes : List Bool → K → List Bool := fun out => Function.update base output out
have source_preserved (out : List Bool) (i : Fin n) (field : Fin 4) :
tapes out (KeyMetadataMachine.idTape (tape i) field) =
base (KeyMetadataMachine.idTape (tape i) field) := by
exact Function.update_of_ne (source_ne_output tape distinct output sharedOutput i field) out base
have scratch_preserved (out : List Bool) (i : Fin n) : tapes out (tape i 4) = [] := by
rw [sharedScratch i]
exact (Function.update_of_ne scratchOutput out base).trans scratchEmpty
have localTrace : ∀ command ∈ commands n,
∀ (placed : LocalLabel command → Λ) (next : Option Λ),
(∀ l, program (placed l) =
localInstruction shapes modes side partition tape output command placed next l) →
∀ out : List Bool, True →
(advance (TM2.step program))^[cost command]
(some ⟨some (placed (main command)), KeyMetadataMachine.clean ambient, tapes out⟩) =
some ⟨next, KeyMetadataMachine.clean ambient, tapes (result command out)⟩ := by
intro command _ placed next atPlaced out _
cases command with
| terminal =>
have h := KeyMetadataMachine.literal_step output [true] next program (placed ())
(atPlaced ()) (tapes out) ambient
simpa only [cost, commandCost, Function.iterate_one, advance_some, main,
result, bits, commandBits, tapes, Function.update_self, Function.update_idem] using h
| header =>
have h := KeyMetadataMachine.literal_step output (prefixBits side partition) next
program (placed ()) (atPlaced ()) (tapes out) ambient
simpa only [cost, commandCost, Function.iterate_one, advance_some, main,
result, bits, commandBits, tapes, Function.update_self, Function.update_idem] using h
| metadata i =>
have atMetadata : ∀ l, program (placed l) =
KeyMetadataMachine.instruction i (shapes i) (modes i) (tape i) placed next l := atPlaced
cases hfield : KeyMetadataMachine.sourceField (shapes i) (modes i) with
| none =>
have hbits : bits (.metadata i) = [] := by
simp only [bits, commandBits, KeyMetadataMachine.retainedBits,
KeyMetadataMachine.retainedWords_eq, hfield, encodeWords]
have h := KeyMetadataMachine.droppedTrace i (shapes i) (modes i) hfield
(tape i) placed next program atMetadata (tapes out) ambient
simpa only [cost, commandCost, hfield, main, result, hbits, List.nil_append] using h
| some field =>
have hbits : bits (.metadata i) =
encodeWords (KeyMetadataMachine.prefixWords i (shapes i) (modes i)) ++
base (KeyMetadataMachine.idTape (tape i) field) ++
encodeWords (KeyMetadataMachine.suffixWords (shapes i) (modes i)) := by
rw [sourceWords i field]
simp [bits, commandBits, KeyMetadataMachine.retainedBits,
KeyMetadataMachine.retainedWords_eq, hfield, encodeWords, List.append_assoc]
have h := KeyMetadataMachine.selectedTrace i (shapes i) (modes i) field hfield
(tape i) (distinct i) placed next program atMetadata (tapes out)
(scratch_preserved out i) ambient
rw [source_preserved out i field, sharedOutput i] at h
simpa only [cost, commandCost, hfield, main, result, hbits,
tapes, Function.update_self, Function.update_idem] using h
have run := MachineFiniteSequence.trace LocalLabel main
(localInstruction shapes modes side partition tape output) result (fun command _ => cost command)
program (fun _ : List Bool => True) (fun _ => KeyMetadataMachine.clean ambient) tapes
(commands n) (fun _ _ _ _ => True.intro) localTrace labels exit atLabels (base output) True.intro
have finalOutput : MachineFiniteSequence.resultOf result (commands n) (base output) =
payload shapes modes side partition occurrences variableIDs ++ base output := by
rw [resultOf_prepend]
exact congrArg (· ++ base output) (commands_bits shapes modes side partition occurrences variableIDs)
refine
{ steps := MachineFiniteSequence.steps result (fun command _ => cost command) (commands n) (base output)
evals_in_steps := ?_
steps_le_m := ?_ }
· rw [finalOutput] at run
unfold MachineComposition.advance at run
simpa only [entry, tapes, Function.update_eq_self, Function.flip_def,
Option.bind_eq_bind] using run
· rw [steps_constant]
exact commands_cost_le shapes modes tape base
theorem output_frame (output : K) (base : K → List Bool) (bits : List Bool)
(k : K) (hne : k ≠ output) :
Function.update base output (bits ++ base output) k = base k := by
simp only [Function.update_of_ne hne]
theorem output_sources (tape : Fin n → Fin 6 → K)
(distinct : ∀ i, Function.Injective (tape i)) (output : K)
(sharedOutput : ∀ i, tape i 5 = output) (base : K → List Bool) (bits : List Bool)
(i : Fin n) (field : Fin 4) :
Function.update base output (bits ++ base output) (KeyMetadataMachine.idTape (tape i) field) =
base (KeyMetadataMachine.idTape (tape i) field) :=
output_frame output base bits _ (source_ne_output tape distinct output sharedOutput i field)
theorem output_scratch (scratch output : K) (hne : scratch ≠ output)
(base : K → List Bool) (bits : List Bool) (hempty : base scratch = []) :
Function.update base output (bits ++ base output) scratch = [] :=
(output_frame output base bits scratch hne).trans hempty
theorem label_finite : Finite (Label n) := inferInstance
theorem state_finite [Finite σ] : Finite (State σ) := inferInstance
end
end PerfectCompleteness.CanonicalKeyMachine
end OAI
|