import OAI.Computability.BinPacking.PCP.BinaryOccurrenceRename import OAI.Computability.BinPacking.Reductions.BinaryValidatorMachine namespace OAI namespace BinPackingCompleteness.BinaryLanguage open BinPackingGames.Foundations def rejectBinary : BinaryFormula.Formula where clauses := [#v[⟨0, true⟩, ⟨0, true⟩, ⟨0, true⟩], #v[⟨0, false⟩, ⟨0, false⟩, ⟨0, false⟩]] theorem rejectBinary_unsatisfiable : ¬rejectBinary.Satisfiable := by rintro ⟨assignment, satisfied⟩ have positive := satisfied #v[⟨0, true⟩, ⟨0, true⟩, ⟨0, true⟩] (by simp [rejectBinary]) have negative := satisfied #v[⟨0, false⟩, ⟨0, false⟩, ⟨0, false⟩] (by simp [rejectBinary]) cases h : assignment 0 <;> simp [BinaryFormula.Clause.eval, BinaryFormula.Literal.eval, h] at positive negative def rejectFormula : Target.Formula := BinaryOccurrenceRename.renamed rejectBinary theorem reject_unsatisfiable : ¬rejectFormula.Satisfiable := by simpa only [rejectFormula, BinaryOccurrenceRename.renamed_satisfiable_iff] using rejectBinary_unsatisfiable def language (input : List Bool) : Prop := ∃ formula, BinaryEncoding.decodeFormula input = some formula ∧ formula.Satisfiable def totalParsed (input : List Bool) : BinaryFormula.Formula := (BinaryEncoding.decodeFormula input).getD rejectBinary def totalRename (input : List Bool) : Target.Formula := BinaryOccurrenceRename.renamed (totalParsed input) theorem totalParsed_satisfiable_iff (input : List Bool) : (totalParsed input).Satisfiable ↔ language input := by cases parsed : BinaryEncoding.decodeFormula input with | none => simp [totalParsed, parsed, rejectBinary_unsatisfiable, language] | some formula => simp [totalParsed, parsed, language] theorem totalRename_satisfiable_iff (input : List Bool) : (totalRename input).Satisfiable ↔ language input := by exact (BinaryOccurrenceRename.renamed_satisfiable_iff (totalParsed input)).trans (totalParsed_satisfiable_iff input) theorem totalRename_encoded (formula : BinaryFormula.Formula) : totalRename (BinaryEncoding.formulaBits formula) = BinaryOccurrenceRename.renamed formula := by simp [totalRename, totalParsed] theorem language_encoded (formula : BinaryFormula.Formula) : language (BinaryEncoding.formulaBits formula) ↔ formula.Satisfiable := by simp [language] theorem reject_bits_length : (Complexity.formulaBits rejectFormula).length = 25 := by decide theorem totalRename_output_length (input : List Bool) : (Complexity.formulaBits (totalRename input)).length ≤ 9 * input.length * input.length + 10 * input.length + 25 := by cases parsed : BinaryEncoding.decodeFormula input with | none => have output : totalRename input = rejectFormula := by simp [totalRename, totalParsed, parsed, rejectFormula] rw [output, reject_bits_length] omega | some formula => have correct := BinaryParsing.decodeFormula_sound input formula parsed have bound := BinaryOccurrenceRename.renamed_encoding_bound_bits formula simp only [totalRename, totalParsed, parsed, Option.getD_some] rw [correct] omega end BinPackingCompleteness.BinaryLanguage namespace BinPackingCompleteness.BinaryTotalInputMachine open Turing open BinPackingGames.Foundations.Complexity open MachineComposition open BinPackingGames.Reduction.MachineTransfer open BinPackingGames.Reduction.MachineSubstitution open BinaryValidatorMachine (Mode nextMode run) inductive Label | scan | restore | drain | rejectOutput deriving DecidableEq protected abbrev Label.enumList : List Label := [.scan, .restore, .drain, .rejectOutput] protected theorem Label.enumList_getElem?_ctorIdx_eq (x : Label) : Label.enumList[x.ctorIdx]? = some x := by cases x <;> rfl protected theorem Label.enumList_nodup : Label.enumList.Nodup := by decide instance : Fintype Label where elems := ⟨Label.enumList, Label.enumList_nodup⟩ complete x := by cases x <;> decide abbrev Alphabet (_ : Fin 3) := Bool abbrev State := Mode × Option Bool def rejectedWord : List Bool := BinaryEncoding.formulaBits BinaryLanguage.rejectBinary def instruction : Label → TM2.Stmt Alphabet Label State | .scan => .pop 0 (fun state head => (state.1, head)) (.branch (fun state => state.2.isSome) (.push 2 (fun state => state.2.getD false) (.load (fun state => (nextMode state.1 (state.2.getD false), none)) (.goto fun _ => .scan))) (.branch (fun state => decide (state.1 = .done)) (.load (fun _ => (.clause, none)) (.goto fun _ => .restore)) (.load (fun _ => (.clause, none)) (.goto fun _ => .drain)))) | .restore => loopAt 2 1 id false .restore none | .drain => MachineDrain.drain 2 .drain (some .rejectOutput) | .rejectOutput => pushWord 1 rejectedWord.reverse .halt abbrev machine : FinTM2 where K := Fin 3 k₀ := 0 k₁ := 1 Γ := Alphabet Λ := Label main := .scan σ := State initialState := (.clause, none) m := instruction def tapes (input output saved : List Bool) : Fin 3 → List Bool | 0 => input | 1 => output | _ => saved def cfg (label : Option Label) (input output saved : List Bool) (mode : Mode := .clause) (register : Option Bool := none) : machine.Cfg := ⟨label, (mode, register), tapes input output saved⟩ @[simp] private theorem tapes_input (input output saved : List Bool) : tapes input output saved 0 = input := rfl @[simp] private theorem tapes_output (input output saved : List Bool) : tapes input output saved 1 = output := rfl @[simp] private theorem tapes_saved (input output saved : List Bool) : tapes input output saved 2 = saved := rfl private theorem update_input (input output saved replacement : List Bool) : Function.update (tapes input output saved) 0 replacement = tapes replacement output saved := by funext k fin_cases k <;> rfl private theorem update_output (input output saved replacement : List Bool) : Function.update (tapes input output saved) 1 replacement = tapes input replacement saved := by funext k fin_cases k <;> rfl private theorem update_saved (input output saved replacement : List Bool) : Function.update (tapes input output saved) 2 replacement = tapes input output replacement := by funext k fin_cases k <;> rfl def afterScan (mode : Mode) : Label := if mode = .done then .restore else .drain theorem step_scan_empty (output saved : List Bool) (mode : Mode) (register : Option Bool) : machine.step (cfg (some .scan) [] output saved mode register) = some (cfg (some (afterScan mode)) [] output saved) := by change some (TM2.stepAux (instruction .scan) _ _) = _ by_cases accepted : mode = .done <;> simp [instruction, cfg, TM2.stepAux, update_input, afterScan, accepted] <;> rfl theorem step_scan_cons (bit : Bool) (input output saved : List Bool) (mode : Mode) (register : Option Bool) : machine.step (cfg (some .scan) (bit :: input) output saved mode register) = some (cfg (some .scan) input output (bit :: saved) (nextMode mode bit)) := by change some (TM2.stepAux (instruction .scan) _ _) = _ simp [instruction, cfg, TM2.stepAux, update_input, update_saved] rfl theorem scanTrace (input output saved : List Bool) (mode : Mode) (register : Option Bool) : (advance machine.step)^[input.length + 1] (some (cfg (some .scan) input output saved mode register)) = some (cfg (some (afterScan (run mode input))) [] output (input.reverse ++ saved)) := by induction input generalizing saved mode register with | nil => simpa only [List.length_nil, Nat.zero_add, Function.iterate_one, advance_some, run, List.reverse_nil, List.nil_append] using step_scan_empty output saved mode register | cons bit input ih => rw [List.length_cons, Function.iterate_succ_apply] simp only [advance_some] rw [step_scan_cons] simpa only [run, List.reverse_cons, List.append_assoc, List.singleton_append] using ih (bit :: saved) (nextMode mode bit) none theorem restoreTrace (saved output : List Bool) (register : Option Bool) : (advance machine.step)^[saved.length + 1] (some (cfg (some .restore) [] output saved .clause register)) = some (cfg none [] (saved.reverse ++ output) []) := by change (nextAt (1 : Fin 3) instruction)^[saved.length + 1] (some ⟨some .restore, (Mode.clause, register), tapes [] output saved⟩) = some ⟨none, (Mode.clause, none), tapes [] (saved.reverse ++ output) []⟩ have trace := transferAt_steps (2 : Fin 3) 1 (by decide) id false .restore none instruction rfl (tapes [] [] []) saved output Mode.clause register simpa only [tapesAt, update_saved, update_output, List.map_id_fun, id_eq] using trace theorem drainTrace (saved output : List Bool) (register : Option Bool) : (advance machine.step)^[saved.length + 1] (some (cfg (some .drain) [] output saved .clause register)) = some (cfg (some .rejectOutput) [] output []) := by change (advance (TM2.step instruction))^[saved.length + 1] (some ⟨some .drain, (Mode.clause, register), tapes [] output saved⟩) = some ⟨some .rejectOutput, (Mode.clause, none), tapes [] output []⟩ have trace := MachineDrain.drainTrace (2 : Fin 3) .drain (some .rejectOutput) instruction rfl (tapes [] output []) saved Mode.clause register simpa only [update_saved] using trace theorem step_rejectOutput (output : List Bool) : machine.step (cfg (some .rejectOutput) [] output []) = some (cfg none [] (rejectedWord ++ output) []) := by change some (TM2.stepAux (pushWord (Γ := Alphabet) (Λ := Label) (σ := State) (1 : Fin 3) rejectedWord.reverse .halt) _ _) = _ rw [stepAux_pushWord] simp only [cfg, List.reverse_reverse, tapes_output, update_output, TM2.stepAux] 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] theorem acceptedTrace (input : List Bool) (accepted : run .clause input = .done) : (advance machine.step)^[2 * input.length + 2] (some (cfg (some .scan) input [] [])) = some (cfg none [] input []) := by have first := scanTrace input [] [] .clause none simp only [afterScan, accepted, ↓reduceIte, List.append_nil] at first have second := restoreTrace input.reverse [] none simp only [List.length_reverse, List.reverse_reverse, List.append_nil] at second have time : 2 * input.length + 2 = (input.length + 1) + (input.length + 1) := by omega rw [time] exact joinTrace first second theorem rejectedTrace (input : List Bool) (rejected : run .clause input ≠ .done) : (advance machine.step)^[2 * input.length + 3] (some (cfg (some .scan) input [] [])) = some (cfg none [] rejectedWord []) := by have first := scanTrace input [] [] .clause none simp only [afterScan, rejected, ↓reduceIte, List.append_nil] at first have second := drainTrace input.reverse [] none simp only [List.length_reverse] at second have third : (advance machine.step)^[1] (some (cfg (some .rejectOutput) [] [] [])) = some (cfg none [] rejectedWord []) := by simpa only [Function.iterate_one, advance_some, List.append_nil] using step_rejectOutput [] have time : 2 * input.length + 3 = (input.length + 1) + (input.length + 1) + 1 := by omega rw [time] exact joinTrace (joinTrace first second) third theorem initList_eq (input : List Bool) : initList machine input = cfg (some .scan) input [] [] := by unfold initList cfg congr 1 funext k fin_cases k <;> rfl theorem haltList_eq (output : List Bool) : haltList machine output = cfg none [] output [] := by unfold haltList cfg congr 1 funext k fin_cases k <;> rfl def steps (input : List Bool) : Nat := if run .clause input = .done then 2 * input.length + 2 else 2 * input.length + 3 theorem steps_le (input : List Bool) : steps input ≤ 2 * input.length + 3 := by unfold steps split <;> omega theorem totalTrace (input : List Bool) : (advance machine.step)^[steps input] (some (initList machine input)) = some (haltList machine (BinaryEncoding.formulaBits (BinaryLanguage.totalParsed input))) := by rw [initList_eq, haltList_eq] cases parsed : BinaryEncoding.decodeFormula input with | none => have rejected : run .clause input ≠ .done := by intro accepted obtain ⟨formula, encoded⟩ := (BinaryValidatorMachine.run_clause_iff input).mp accepted have roundtrip := BinaryEncoding.decodeFormula_encoded formula rw [← encoded, parsed] at roundtrip cases roundtrip simpa only [steps, rejected, ↓reduceIte, BinaryLanguage.totalParsed, parsed, Option.getD_none, rejectedWord] using rejectedTrace input rejected | some formula => have encoded := BinaryParsing.decodeFormula_sound input formula parsed have accepted := (BinaryValidatorMachine.run_clause_iff input).mpr ⟨formula, encoded⟩ simp only [steps, accepted, ↓reduceIte, BinaryLanguage.totalParsed, parsed, Option.getD_some] rw [← encoded] exact acceptedTrace input accepted def outputsInTime (input : List Bool) : TM2OutputsInTime machine input (some (BinaryEncoding.formulaBits (BinaryLanguage.totalParsed input))) (2 * input.length + 3) where steps := steps input evals_in_steps := totalTrace input steps_le_m := steps_le input noncomputable def computableInPolyTime : TM2ComputableInPolyTime (id : List Bool → List Bool) BinaryEncoding.formulaBits BinaryLanguage.totalParsed where tm := machine inputAlphabet := Equiv.refl Bool outputAlphabet := Equiv.refl Bool time := 2 * Polynomial.X + 3 outputsFun input := by change TM2OutputsInTime machine (input.map id) (some ((BinaryEncoding.formulaBits (BinaryLanguage.totalParsed input)).map id)) ((2 * Polynomial.X + 3 : Polynomial Nat).eval input.length) simpa only [List.map_id_fun, id_eq, Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_X, Polynomial.eval_ofNat] using outputsInTime input theorem machine_finiteAlphabet (k : machine.K) : Finite (machine.Γ k) := by change Finite Bool infer_instance theorem computation_finiteAlphabet : MachineFiniteAlphabet.FiniteAlphabet computableInPolyTime.tm := machine_finiteAlphabet end BinPackingCompleteness.BinaryTotalInputMachine end OAI