import OAI.Computability.BinPacking.Reductions.BinaryLiteralMachine namespace OAI namespace BinPackingCompleteness.BinaryRenameLoop open Turing open BinPackingGames.Foundations.Complexity open MachineComposition abbrev Token := BinaryNameSearch.Token def nextSlot (slot : Fin 3) : Fin 3 := if slot = 0 then 1 else if slot = 1 then 2 else 0 def clauseIncrement (slot : Fin 3) : Nat := if slot = 2 then 1 else 0 def finalSlot : Nat → Fin 3 → Fin 3 | 0, slot => slot | n + 1, slot => finalSlot n (nextSlot slot) def completedClauses : Nat → Fin 3 → Nat | 0, _ => 0 | n + 1, slot => completedClauses n (nextSlot slot) + clauseIncrement slot theorem finalSlot_three (n : Nat) : finalSlot (n + 3) 0 = finalSlot n 0 := by simp [finalSlot, nextSlot] theorem completedClauses_three (n : Nat) : completedClauses (n + 3) 0 = completedClauses n 0 + 1 := by simp [completedClauses, nextSlot, clauseIncrement] @[simp] theorem finalSlot_triples (n : Nat) : finalSlot (3 * n) 0 = 0 := by induction n with | zero => rfl | succ n ih => rw [Nat.mul_succ, finalSlot_three, ih] @[simp] theorem completedClauses_triples (n : Nat) : completedClauses (3 * n) 0 = n := by induction n with | zero => rfl | succ n ih => rw [Nat.mul_succ, completedClauses_three, ih] def outputBody (tokens input : List Token) : List Bool := input.flatMap (fun token => BinaryLiteralMachine.outputWord tokens token.2 token.1) @[simp] theorem outputBody_nil (tokens : List Token) : outputBody tokens [] = [] := rfl @[simp] theorem outputBody_cons (tokens : List Token) (token : Token) (input : List Token) : outputBody tokens (token :: input) = BinaryLiteralMachine.outputWord tokens token.2 token.1 ++ outputBody tokens input := rfl def steps (tokens : List Token) : List Token → Nat | [] => 1 | token :: input => BinaryLiteralMachine.steps tokens token.2 + 2 + steps tokens input theorem payload_length_le_stream (tokens : List Token) (bits : List Bool) (present : bits ∈ BinaryNameSearch.payloads tokens) : bits.length ≤ (BinaryNameSearch.stream tokens).length := by induction tokens with | nil => simp at present | cons token tokens ih => rcases List.mem_cons.mp present with equal | remaining · rw [equal] simp only [BinaryNameSearch.stream_cons, List.length_append, BinaryNameSearch.tokenBits, List.length_cons, BinaryNameMachine.frame_length] omega · have tail := ih remaining simp only [BinaryNameSearch.stream_cons, List.length_append] omega def perLiteralBound (streamLength : Nat) : Nat := 12 * streamLength ^ 2 + 26 * streamLength + 11 theorem literal_steps_le (tokens : List Token) (bits : List Bool) (present : bits ∈ BinaryNameSearch.payloads tokens) : BinaryLiteralMachine.steps tokens bits + 2 ≤ perLiteralBound (BinaryNameSearch.stream tokens).length := by have body := BinaryLiteralMachine.steps_le tokens bits present have size := payload_length_le_stream tokens bits present have sumBound : (BinaryNameSearch.stream tokens).length + bits.length ≤ 2 * (BinaryNameSearch.stream tokens).length := by omega have sq := Nat.mul_self_le_mul_self sumBound unfold perLiteralBound nlinarith theorem steps_le (tokens input : List Token) (included : ∀ token ∈ input, token ∈ tokens) : steps tokens input ≤ perLiteralBound (BinaryNameSearch.stream tokens).length * input.length + 1 := by induction input with | nil => simp [steps] | cons token input ih => have present : token.2 ∈ BinaryNameSearch.payloads tokens := List.mem_map.mpr ⟨token, included token (by simp), rfl⟩ have head := literal_steps_le tokens token.2 present have tail := ih (fun t ht => included t (by simp [ht])) simp only [steps, List.length_cons] nlinarith theorem steps_le_cubic (tokens input : List Token) (included : ∀ token ∈ input, token ∈ tokens) (countBound : input.length ≤ (BinaryNameSearch.stream tokens).length) : steps tokens input ≤ 12 * (BinaryNameSearch.stream tokens).length ^ 3 + 26 * (BinaryNameSearch.stream tokens).length ^ 2 + 11 * (BinaryNameSearch.stream tokens).length + 1 := by calc steps tokens input ≤ perLiteralBound (BinaryNameSearch.stream tokens).length * input.length + 1 := steps_le tokens input included _ ≤ perLiteralBound (BinaryNameSearch.stream tokens).length * (BinaryNameSearch.stream tokens).length + 1 := Nat.add_le_add_right (Nat.mul_le_mul_left _ countBound) _ _ = _ := by unfold perLiteralBound; ring variable {K Λ A : Type} [DecidableEq K] abbrev Alphabet (_ : K) := Bool abbrev State (A : Type) := BinaryLiteralMachine.State (A × Fin 3) abbrev clean (ambient : A) (slot : Fin 3) : State A := BinaryLiteralMachine.clean (ambient, slot) inductive Label | entry | literal (label : BinaryLiteralMachine.Label) | tally deriving DecidableEq, Fintype def literalTapes (tape : Fin 11 → K) : Fin 9 → K := fun i => tape i.castSucc.castSucc omit [DecidableEq K] in theorem literalTapes_injective (tape : Fin 11 → K) (distinct : Function.Injective tape) : Function.Injective (literalTapes tape) := by intro i j h apply Fin.ext exact congrArg (fun i : Fin 11 => i.val) (distinct h) def finish (exit : Option Λ) : TM2.Stmt (Alphabet (K := K)) Λ (State A) := .load (fun state => clean state.1.1.1.1 state.1.1.1.2) (match exit with | none => .halt | some label => .goto fun _ => label) def next (again : Λ) : TM2.Stmt (Alphabet (K := K)) Λ (State A) := .load (fun state => clean state.1.1.1.1 (nextSlot state.1.1.1.2)) (.goto fun _ => again) def instruction (tape : Fin 11 → K) (labels : Label → Λ) (done malformed : Option Λ) : Label → TM2.Stmt (Alphabet (K := K)) Λ (State A) | .entry => .pop (tape 0) (fun state head => (state.1, head)) (.branch (fun state => state.2.isSome) (.push (tape 0) (fun state => state.2.getD false) (finish (some (labels (.literal .readSign))))) (finish done)) | .literal l => BinaryLiteralMachine.instruction (literalTapes tape) (fun l => labels (.literal l)) (some (labels .tally)) malformed l | .tally => .push (tape 9) (fun _ => true) (.branch (fun state => state.1.1.1.2 == (2 : Fin 3)) (.push (tape 10) (fun _ => true) (next (labels .entry))) (next (labels .entry))) def loopTapes (tape : Fin 11 → K) (base : K → List Bool) (cursor output variableCounter clauses : List Bool) : K → List Bool := Function.update (Function.update (Function.update (Function.update base (tape 0) cursor) (tape 8) output) (tape 9) variableCounter) (tape 10) clauses theorem loopTapes_cursor (tape : Fin 11 → K) (distinct : Function.Injective tape) (base : K → List Bool) (cursor output variableCounter clauses : List Bool) : loopTapes tape base cursor output variableCounter clauses (tape 0) = cursor := by have hd (i j : Fin 11) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h) simp [loopTapes, hd] theorem loopTapes_output (tape : Fin 11 → K) (distinct : Function.Injective tape) (base : K → List Bool) (cursor output variableCounter clauses : List Bool) : loopTapes tape base cursor output variableCounter clauses (tape 8) = output := by have hd (i j : Fin 11) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h) simp [loopTapes, hd] theorem loopTapes_variables (tape : Fin 11 → K) (distinct : Function.Injective tape) (base : K → List Bool) (cursor output variableCounter clauses : List Bool) : loopTapes tape base cursor output variableCounter clauses (tape 9) = variableCounter := by have hd (i j : Fin 11) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h) simp [loopTapes, hd] theorem loopTapes_clauses (tape : Fin 11 → K) (base : K → List Bool) (cursor output variableCounter clauses : List Bool) : loopTapes tape base cursor output variableCounter clauses (tape 10) = clauses := by simp [loopTapes] private theorem update_cursor (tape : Fin 11 → K) (distinct : Function.Injective tape) (base : K → List Bool) (cursor output variableCounter clauses replacement : List Bool) : Function.update (loopTapes tape base cursor output variableCounter clauses) (tape 0) replacement = loopTapes tape base replacement output variableCounter clauses := by have hd (i j : Fin 11) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h) funext k by_cases h : k = tape 0 · subst k; simp [loopTapes, hd] · simp [loopTapes, h, Function.update_apply] private theorem update_output (tape : Fin 11 → K) (distinct : Function.Injective tape) (base : K → List Bool) (cursor output variableCounter clauses replacement : List Bool) : Function.update (loopTapes tape base cursor output variableCounter clauses) (tape 8) replacement = loopTapes tape base cursor replacement variableCounter clauses := by have hd (i j : Fin 11) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h) funext k by_cases h : k = tape 8 · subst k; simp [loopTapes, hd] · simp [loopTapes, h, Function.update_apply] private theorem update_variables (tape : Fin 11 → K) (distinct : Function.Injective tape) (base : K → List Bool) (cursor output variableCounter clauses replacement : List Bool) : Function.update (loopTapes tape base cursor output variableCounter clauses) (tape 9) replacement = loopTapes tape base cursor output replacement clauses := by have hd (i j : Fin 11) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h) funext k by_cases h : k = tape 9 · subst k; simp [loopTapes, hd] · simp [loopTapes, h, Function.update_apply] private theorem update_clauses (tape : Fin 11 → K) (base : K → List Bool) (cursor output variableCounter clauses replacement : List Bool) : Function.update (loopTapes tape base cursor output variableCounter clauses) (tape 10) replacement = loopTapes tape base cursor output variableCounter replacement := by simp [loopTapes] theorem loopTapes_other (tape : Fin 11 → K) (base : K → List Bool) (cursor output variableCounter clauses : List Bool) (k : K) (h0 : k ≠ tape 0) (h8 : k ≠ tape 8) (h9 : k ≠ tape 9) (h10 : k ≠ tape 10) : loopTapes tape base cursor output variableCounter clauses k = base k := by simp [loopTapes, h0, h8, h9, h10] @[simp] theorem stepAux_finish (exit : Option Λ) (state : State A) (base : K → List Bool) : TM2.stepAux (finish exit) state base = ⟨exit, clean state.1.1.1.1 state.1.1.1.2, base⟩ := by cases exit <;> rfl 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] variable (tape : Fin 11 → K) (distinct : Function.Injective tape) variable (labels : Label → Λ) (done malformed : Option Λ) variable (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State A)) variable (atLabels : ∀ l, program (labels l) = instruction tape labels done malformed l) variable (base : K → List Bool) (ambient : A) include distinct atLabels theorem peekStep (slot : Fin 3) (head : Bool) (tail output variableCounter clauses : List Bool) : TM2.step program ⟨some (labels .entry), clean ambient slot, loopTapes tape base (head :: tail) output variableCounter clauses⟩ = some ⟨some (labels (.literal .readSign)), clean ambient slot, loopTapes tape base (head :: tail) output variableCounter clauses⟩ := by have hd (i j : Fin 11) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h) change some (TM2.stepAux (program (labels .entry)) _ _) = _ rw [atLabels .entry] simp [instruction, TM2.stepAux, clean, BinaryLiteralMachine.clean, BinaryNameCompare.clean, loopTapes_cursor tape distinct, update_cursor tape distinct] theorem emptyStep (slot : Fin 3) (output variableCounter clauses : List Bool) : TM2.step program ⟨some (labels .entry), clean ambient slot, loopTapes tape base [] output variableCounter clauses⟩ = some ⟨done, clean ambient slot, loopTapes tape base [] output variableCounter clauses⟩ := by have hd (i j : Fin 11) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h) change some (TM2.stepAux (program (labels .entry)) _ _) = _ rw [atLabels .entry] simp [instruction, TM2.stepAux, clean, BinaryLiteralMachine.clean, BinaryNameCompare.clean, loopTapes_cursor tape distinct, update_cursor tape distinct] theorem tallyStep (slot : Fin 3) (cursor output variableCounter clauses : List Bool) : TM2.step program ⟨some (labels .tally), clean ambient slot, loopTapes tape base cursor output variableCounter clauses⟩ = some ⟨some (labels .entry), clean ambient (nextSlot slot), loopTapes tape base cursor output (true :: variableCounter) (List.replicate (clauseIncrement slot) true ++ clauses)⟩ := by have hd (i j : Fin 11) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h) change some (TM2.stepAux (program (labels .tally)) _ _) = _ rw [atLabels .tally] fin_cases slot <;> simp [instruction, TM2.stepAux, clean, BinaryLiteralMachine.clean, BinaryNameCompare.clean, next, nextSlot, clauseIncrement, loopTapes_variables tape distinct, loopTapes_clauses, update_variables tape distinct, update_clauses] theorem bodyTrace (tokens : List Token) (token : Token) (slot : Fin 3) (suffix output variableCounter clauses : List Bool) (canonical : BinaryNameMachine.canonical token.2 = true) (tokensCanonical : ∀ t ∈ tokens, BinaryNameMachine.canonical t.2 = true) (present : token.2 ∈ BinaryNameSearch.payloads tokens) (permanent : base (tape 1) = BinaryNameSearch.stream tokens) (empty : ∀ i : Fin 11, 2 ≤ i.val → i.val ≤ 7 → base (tape i) = []) : (advance (TM2.step program))^[BinaryLiteralMachine.steps tokens token.2 + 2] (some ⟨some (labels .entry), clean ambient slot, loopTapes tape base (BinaryNameSearch.tokenBits token ++ suffix) output variableCounter clauses⟩) = some ⟨some (labels .entry), clean ambient (nextSlot slot), loopTapes tape base suffix ((BinaryLiteralMachine.outputWord tokens token.2 token.1).reverse ++ output) (true :: variableCounter) (List.replicate (clauseIncrement slot) true ++ clauses)⟩ := by have hd (i j : Fin 11) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h) let initial := loopTapes tape base (BinaryNameSearch.tokenBits token ++ suffix) output variableCounter clauses have first : (advance (TM2.step program))^[1] (some ⟨some (labels .entry), clean ambient slot, initial⟩) = some ⟨some (labels (.literal .readSign)), clean ambient slot, initial⟩ := by simpa only [Function.iterate_one, advance_some, initial, BinaryNameSearch.tokenBits, List.cons_append] using peekStep tape distinct labels done malformed program atLabels base ambient slot token.1 (BinaryNameMachine.frame token.2 ++ suffix) output variableCounter clauses have workEmpty (i : Fin 11) (lo : 2 ≤ i.val) (hi : i.val ≤ 7) : initial (tape i) = [] := by have h0 : i ≠ 0 := by intro h; subst i; omega have h8 : i ≠ 8 := by intro h; subst i; omega have h9 : i ≠ 9 := by intro h; subst i; omega have h10 : i ≠ 10 := by intro h; subst i; omega simpa [initial, loopTapes, hd i 0 h0, hd i 8 h8, hd i 9 h9, hd i 10 h10] using empty i lo hi have literal := BinaryLiteralMachine.literalTrace (literalTapes tape) (literalTapes_injective tape distinct) (fun l => labels (.literal l)) (some (labels .tally)) malformed program (fun l => atLabels (.literal l)) initial tokens token.1 token.2 suffix canonical tokensCanonical present (by simp [initial, literalTapes, loopTapes, BinaryNameSearch.tokenBits, hd]) (by simpa [initial, literalTapes, loopTapes, hd] using permanent) (fun i lo hi => workEmpty i.castSucc.castSucc lo hi) (ambient, slot) have finalTapes : Function.update (Function.update initial (literalTapes tape 0) suffix) (literalTapes tape 8) ((BinaryLiteralMachine.outputWord tokens token.2 token.1).reverse ++ initial (literalTapes tape 8)) = loopTapes tape base suffix ((BinaryLiteralMachine.outputWord tokens token.2 token.1).reverse ++ output) variableCounter clauses := by have out : initial (literalTapes tape 8) = output := by simp [initial, literalTapes, loopTapes, hd] rw [out] change Function.update (Function.update (loopTapes tape base (BinaryNameSearch.tokenBits token ++ suffix) output variableCounter clauses) (tape 0) suffix) (tape 8) _ = _ rw [update_cursor tape distinct, update_output tape distinct] rw [finalTapes] at literal have tally : (advance (TM2.step program))^[1] (some ⟨some (labels .tally), clean ambient slot, loopTapes tape base suffix ((BinaryLiteralMachine.outputWord tokens token.2 token.1).reverse ++ output) variableCounter clauses⟩) = some ⟨some (labels .entry), clean ambient (nextSlot slot), loopTapes tape base suffix ((BinaryLiteralMachine.outputWord tokens token.2 token.1).reverse ++ output) (true :: variableCounter) (List.replicate (clauseIncrement slot) true ++ clauses)⟩ := by simpa only [Function.iterate_one, advance_some] using tallyStep tape distinct labels done malformed program atLabels base ambient slot suffix ((BinaryLiteralMachine.outputWord tokens token.2 token.1).reverse ++ output) variableCounter clauses have full := joinTrace (joinTrace first literal) tally simpa only [show 1 + BinaryLiteralMachine.steps tokens token.2 + 1 = BinaryLiteralMachine.steps tokens token.2 + 2 by omega] using full theorem loopTrace (tokens input : List Token) (slot : Fin 3) (output variableCounter clauses : List Bool) (tokensCanonical : ∀ t ∈ tokens, BinaryNameMachine.canonical t.2 = true) (included : ∀ t ∈ input, t ∈ tokens) (permanent : base (tape 1) = BinaryNameSearch.stream tokens) (empty : ∀ i : Fin 11, 2 ≤ i.val → i.val ≤ 7 → base (tape i) = []) : (advance (TM2.step program))^[steps tokens input] (some ⟨some (labels .entry), clean ambient slot, loopTapes tape base (BinaryNameSearch.stream input) output variableCounter clauses⟩) = some ⟨done, clean ambient (finalSlot input.length slot), loopTapes tape base [] ((outputBody tokens input).reverse ++ output) (List.replicate input.length true ++ variableCounter) (List.replicate (completedClauses input.length slot) true ++ clauses)⟩ := by induction input generalizing slot output variableCounter clauses with | nil => simpa only [steps, Function.iterate_one, advance_some, BinaryNameSearch.stream_nil, List.length_nil, outputBody_nil, List.reverse_nil, List.nil_append, finalSlot, completedClauses, List.replicate_zero] using emptyStep tape distinct labels done malformed program atLabels base ambient slot output variableCounter clauses | cons token input ih => have member := included token (by simp) have present : token.2 ∈ BinaryNameSearch.payloads tokens := List.mem_map.mpr ⟨token, member, rfl⟩ have first := bodyTrace tape distinct labels done malformed program atLabels base ambient tokens token slot (BinaryNameSearch.stream input) output variableCounter clauses (tokensCanonical token member) tokensCanonical present permanent empty have rest := ih (nextSlot slot) ((BinaryLiteralMachine.outputWord tokens token.2 token.1).reverse ++ output) (true :: variableCounter) (List.replicate (clauseIncrement slot) true ++ clauses) (fun t ht => included t (by simp [ht])) have full := joinTrace first rest have variableEq : List.replicate input.length true ++ (true :: variableCounter) = List.replicate (input.length + 1) true ++ variableCounter := by rw [List.replicate_succ'] simp only [List.append_assoc, List.singleton_append] have clauseEq : List.replicate (completedClauses input.length (nextSlot slot)) true ++ (List.replicate (clauseIncrement slot) true ++ clauses) = List.replicate (completedClauses (input.length + 1) slot) true ++ clauses := by rw [← List.append_assoc, ← List.replicate_add] rfl simpa only [steps, BinaryNameSearch.stream_cons, List.length_cons, finalSlot, outputBody_cons, List.reverse_append, List.append_assoc, variableEq, clauseEq] using full theorem triplesTrace (tokens input : List Token) (count : Nat) (length_eq : input.length = 3 * count) (output variableCounter clauses : List Bool) (tokensCanonical : ∀ t ∈ tokens, BinaryNameMachine.canonical t.2 = true) (included : ∀ t ∈ input, t ∈ tokens) (permanent : base (tape 1) = BinaryNameSearch.stream tokens) (empty : ∀ i : Fin 11, 2 ≤ i.val → i.val ≤ 7 → base (tape i) = []) : (advance (TM2.step program))^[steps tokens input] (some ⟨some (labels .entry), clean ambient 0, loopTapes tape base (BinaryNameSearch.stream input) output variableCounter clauses⟩) = some ⟨done, clean ambient 0, loopTapes tape base [] ((outputBody tokens input).reverse ++ output) (List.replicate (3 * count) true ++ variableCounter) (List.replicate count true ++ clauses)⟩ := by have h := loopTrace tape distinct labels done malformed program atLabels base ambient tokens input 0 output variableCounter clauses tokensCanonical included permanent empty simpa only [length_eq, finalSlot_triples, completedClauses_triples] using h def triplesInTime (tokens input : List Token) (count : Nat) (length_eq : input.length = 3 * count) (output variableCounter clauses : List Bool) (tokensCanonical : ∀ t ∈ tokens, BinaryNameMachine.canonical t.2 = true) (included : ∀ t ∈ input, t ∈ tokens) (permanent : base (tape 1) = BinaryNameSearch.stream tokens) (empty : ∀ i : Fin 11, 2 ≤ i.val → i.val ≤ 7 → base (tape i) = []) : StateTransition.EvalsToInTime (TM2.step program) ⟨some (labels .entry), clean ambient 0, loopTapes tape base (BinaryNameSearch.stream input) output variableCounter clauses⟩ (some ⟨done, clean ambient 0, loopTapes tape base [] ((outputBody tokens input).reverse ++ output) (List.replicate (3 * count) true ++ variableCounter) (List.replicate count true ++ clauses)⟩) (perLiteralBound (BinaryNameSearch.stream tokens).length * input.length + 1) where steps := steps tokens input evals_in_steps := triplesTrace tape distinct labels done malformed program atLabels base ambient tokens input count length_eq output variableCounter clauses tokensCanonical included permanent empty steps_le_m := steps_le tokens input included end BinPackingCompleteness.BinaryRenameLoop end OAI