openAI-math-dataset / lean /OAI /Computability /UniqueGames /Machines /MachineBinaryLiteralMachine.lean
Download lean/OAI/Computability/UniqueGames/Machines/MachineBinaryLiteralMachine.lean from roseking/openAI-math-dataset: direct link, hf CLI and curl.
- Browser
- Download file 18.5 kB
-
https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Computability/UniqueGames/Machines/MachineBinaryLiteralMachine.lean
- Command line
-
hf download hf://datasets/roseking/openAI-math-dataset/lean/OAI/Computability/UniqueGames/Machines/MachineBinaryLiteralMachine.lean
-
curl -L -o MachineBinaryLiteralMachine.lean https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Computability/UniqueGames/Machines/MachineBinaryLiteralMachine.lean
18.5 kB
| import Mathlib.Tactic.Linarith | |
| import Mathlib.Tactic.NormNum | |
| import OAI.Computability.UniqueGames.Machines.MachineBinaryNameSearch | |
| import OAI.Computability.UniqueGames.Machines.MachineDrain | |
| import OAI.Computability.UniqueGames.Machines.MachineSubroutineLemmas | |
| namespace OAI | |
| namespace UniqueGamesTheorem.BinaryLiteralMachine | |
| open Turing | |
| open UniqueGamesTheorem.Foundations.Complexity | |
| open MachineComposition | |
| open UniqueGamesTheorem.Reduction.MachineTransfer | |
| variable {K Λ A : Type} [DecidableEq K] | |
| abbrev Alphabet (_ : K) := Bool | |
| abbrev State (A : Type) := BinaryNameCompare.State (A × Bool) | |
| abbrev clean (ambient : A) (sign : Bool := false) : State A := | |
| BinaryNameCompare.clean (ambient, sign) | |
| inductive Label | |
| | readSign | scanKey | copyOut | copyBack | |
| | search (label : BinaryNameSearch.Label) | |
| | drainSearch | drainKey | emitIndex | emitSign | |
| deriving DecidableEq, Fintype | |
| /-- Cursor, permanent stream, key, search stream, candidate, key-copy, | |
| copy scratch, index counter, reversed output. -/ | |
| def searchTapes (tape : Fin 9 → K) : Fin 6 → K | |
| | 0 => tape 3 | |
| | 1 => tape 2 | |
| | 2 => tape 4 | |
| | 3 => tape 5 | |
| | 4 => tape 6 | |
| | 5 => tape 7 | |
| omit [DecidableEq K] in | |
| theorem searchTapes_injective (tape : Fin 9 → K) (distinct : Function.Injective tape) : | |
| Function.Injective (searchTapes tape) := by | |
| intro i j h | |
| fin_cases i <;> fin_cases j <;> simp only [searchTapes] at h | |
| all_goals first | rfl | have heq := congrArg Fin.val (distinct h); norm_num at heq | |
| def finish (exit : Option Λ) : TM2.Stmt (Alphabet (K := K)) Λ (State A) := | |
| .load (fun state => clean state.1.1.1) | |
| (match exit with | |
| | none => .halt | |
| | some label => .goto fun _ => label) | |
| def readSign (tape : Fin 9 → K) (next : Λ) (malformed : Option Λ) : | |
| TM2.Stmt (Alphabet (K := K)) Λ (State A) := | |
| .push (tape 7) (fun _ => false) | |
| (.pop (tape 0) (fun state head => (state.1, head)) | |
| (.branch (fun state => state.2.isSome) | |
| (.load (fun state => clean state.1.1.1 (state.2.getD false)) (.goto fun _ => next)) | |
| (finish malformed))) | |
| def emitSign (output : K) (exit : Option Λ) : | |
| TM2.Stmt (Alphabet (K := K)) Λ (State A) := | |
| .branch (fun state => state.1.1.2) | |
| (.push output (fun _ => true) (.push output (fun _ => false) (finish exit))) | |
| (.push output (fun _ => false) (finish exit)) | |
| def instruction (tape : Fin 9 → K) (labels : Label → Λ) | |
| (exit malformed : Option Λ) : Label → TM2.Stmt (Alphabet (K := K)) Λ (State A) | |
| | .readSign => readSign tape (labels .scanKey) malformed | |
| | .scanKey => MachineStateEquiv.statement (BinaryNameCompare.scanStateEquiv (A × Bool)) | |
| (BinaryNameMachine.scan (tape 0) (tape 2) (labels .scanKey) | |
| (some (labels .copyOut)) malformed) | |
| | .copyOut => loopAt (tape 1) (tape 6) id false | |
| (labels .copyOut) (some (labels .copyBack)) | |
| | .copyBack => MachineCopy.forkLoop (tape 6) (tape 1) (tape 3) false | |
| (labels .copyBack) (some (labels (.search .sign))) | |
| | .search l => BinaryNameSearch.instruction (searchTapes tape) | |
| (fun l => labels (.search l)) (some (labels .drainSearch)) malformed malformed l | |
| | .drainSearch => MachineDrain.drain (tape 3) (labels .drainSearch) | |
| (some (labels .drainKey)) | |
| | .drainKey => MachineDrain.drain (tape 2) (labels .drainKey) | |
| (some (labels .emitIndex)) | |
| | .emitIndex => loopAt (tape 7) (tape 8) id false | |
| (labels .emitIndex) (some (labels .emitSign)) | |
| | .emitSign => emitSign (tape 8) exit | |
| def signWord (sign : Bool) : List Bool := encodeWord (if sign then 1 else 0) | |
| def index (tokens : List BinaryNameSearch.Token) (bits : List Bool) : Nat := | |
| (BinaryNameSearch.payloads tokens).idxOf bits | |
| def outputWord (tokens : List BinaryNameSearch.Token) (bits : List Bool) (sign : Bool) : | |
| List Bool := encodeWord (index tokens bits) ++ signWord sign | |
| def steps (tokens : List BinaryNameSearch.Token) (bits : List Bool) : Nat := | |
| 1 + (bits.length + 1) + 2 * ((BinaryNameSearch.stream tokens).length + 1) + | |
| BinaryNameSearch.steps tokens bits + | |
| ((BinaryNameSearch.stream (BinaryNameSearch.afterMatch tokens bits)).length + 1) + | |
| (bits.length + 1) + (index tokens bits + 2) + 1 | |
| theorem afterMatch_length_le (tokens : List BinaryNameSearch.Token) (bits : List Bool) : | |
| (BinaryNameSearch.stream (BinaryNameSearch.afterMatch tokens bits)).length ≤ | |
| (BinaryNameSearch.stream tokens).length := by | |
| induction tokens with | |
| | nil => simp [BinaryNameSearch.afterMatch] | |
| | cons token tokens ih => | |
| simp only [BinaryNameSearch.afterMatch, BinaryNameSearch.stream_cons, List.length_append] | |
| split <;> omega | |
| theorem steps_le (tokens : List BinaryNameSearch.Token) (bits : List Bool) | |
| (present : bits ∈ BinaryNameSearch.payloads tokens) : | |
| steps tokens bits ≤ | |
| 3 * ((BinaryNameSearch.stream tokens).length + bits.length) ^ 2 + | |
| 13 * ((BinaryNameSearch.stream tokens).length + bits.length) + 9 := by | |
| have search := BinaryNameSearch.steps_le_stream tokens bits | |
| have suffix := afterMatch_length_le tokens bits | |
| have idx : index tokens bits < tokens.length := by | |
| have h := List.idxOf_lt_length_iff.mpr present | |
| simpa [index, BinaryNameSearch.payloads] using h | |
| have count : tokens.length ≤ (BinaryNameSearch.stream tokens).length := by | |
| rw [BinaryNameSearch.stream_length] | |
| omega | |
| unfold steps | |
| nlinarith [Nat.zero_le ((BinaryNameSearch.stream tokens).length * bits.length)] | |
| @[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, 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] | |
| theorem literalTrace (tape : Fin 9 → K) (distinct : Function.Injective tape) | |
| (labels : Label → Λ) (exit malformed : Option Λ) | |
| (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State A)) | |
| (atLabels : ∀ l, program (labels l) = instruction tape labels exit malformed l) | |
| (base : K → List Bool) (tokens : List BinaryNameSearch.Token) | |
| (sign : Bool) (bits suffix : List Bool) | |
| (canonical : BinaryNameMachine.canonical bits = true) | |
| (tokensCanonical : ∀ token ∈ tokens, BinaryNameMachine.canonical token.2 = true) | |
| (present : bits ∈ BinaryNameSearch.payloads tokens) | |
| (cursor : base (tape 0) = sign :: (BinaryNameMachine.frame bits ++ suffix)) | |
| (permanent : base (tape 1) = BinaryNameSearch.stream tokens) | |
| (empty : ∀ i : Fin 9, 2 ≤ i.val → i.val ≤ 7 → base (tape i) = []) | |
| (ambient : A) : | |
| (advance (TM2.step program))^[steps tokens bits] | |
| (some ⟨some (labels .readSign), clean ambient, base⟩) = | |
| some ⟨exit, clean ambient, | |
| Function.update (Function.update base (tape 0) suffix) (tape 8) | |
| ((outputWord tokens bits sign).reverse ++ base (tape 8))⟩ := by | |
| have hd (i j : Fin 9) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h) | |
| have he (i : Fin 9) (hlo : 2 ≤ i.val := by decide) (hhi : i.val ≤ 7 := by decide) : | |
| base (tape i) = [] := empty i hlo hhi | |
| let s₁ := Function.update (Function.update base (tape 7) [false]) (tape 0) | |
| (BinaryNameMachine.frame bits ++ suffix) | |
| let s₂ := Function.update (Function.update s₁ (tape 0) suffix) (tape 2) bits.reverse | |
| let s₃ := Function.update s₂ (tape 3) (BinaryNameSearch.stream tokens) | |
| let tail := BinaryNameSearch.stream (BinaryNameSearch.afterMatch tokens bits) | |
| let s₄ := tapesAt (tape 3) (tape 7) s₃ tail (encodeWord (index tokens bits)) | |
| let s₅ := Function.update s₄ (tape 3) [] | |
| let s₆ := Function.update s₅ (tape 2) [] | |
| let s₇ := tapesAt (tape 7) (tape 8) s₆ [] | |
| ((encodeWord (index tokens bits)).reverse ++ s₆ (tape 8)) | |
| let s₈ := Function.update s₇ (tape 8) ((signWord sign).reverse ++ s₇ (tape 8)) | |
| have signRun : (advance (TM2.step program))^[1] | |
| (some ⟨some (labels .readSign), clean ambient, base⟩) = | |
| some ⟨some (labels .scanKey), clean ambient sign, s₁⟩ := by | |
| change some (TM2.stepAux (program (labels .readSign)) _ _) = _ | |
| rw [atLabels .readSign] | |
| simp [instruction, readSign, TM2.stepAux, clean, BinaryNameCompare.clean, | |
| cursor, he 7, hd 0 7 (by decide), s₁] | |
| have s₁cursor : s₁ (tape 0) = BinaryNameMachine.frame bits ++ suffix := by simp [s₁] | |
| have s₁key : s₁ (tape 2) = [] := by | |
| simp [s₁, hd 2 0 (by decide), hd 2 7 (by decide), he 2] | |
| have keyRun : (advance (TM2.step program))^[bits.length + 1] | |
| (some ⟨some (labels .scanKey), clean ambient sign, s₁⟩) = | |
| some ⟨some (labels .copyOut), clean ambient sign, s₂⟩ := by | |
| let e := BinaryNameCompare.scanStateEquiv (A × Bool) | |
| let back := MachineStateEquiv.program e.symm program | |
| have atScan : back (labels .scanKey) = | |
| BinaryNameMachine.scan (tape 0) (tape 2) (labels .scanKey) | |
| (some (labels .copyOut)) malformed := by | |
| change MachineStateEquiv.statement e.symm (program (labels .scanKey)) = _ | |
| rw [atLabels .scanKey] | |
| exact MachineStateEquiv.statement_symm_statement e _ | |
| have backForward : MachineStateEquiv.program e back = program := by | |
| funext l | |
| change MachineStateEquiv.statement e (MachineStateEquiv.statement e.symm (program l)) = _ | |
| exact MachineStateEquiv.statement_symm_statement e.symm _ | |
| have initial : tapesAt (tape 0) (tape 2) s₁ | |
| (BinaryNameMachine.frame bits ++ suffix) [] = s₁ := by | |
| rw [← s₁cursor, ← s₁key] | |
| exact tapesAt_self _ _ _ | |
| have native := BinaryNameMachine.framedTrace (tape 0) (tape 2) (hd 0 2 (by decide)) | |
| (labels .scanKey) (some (labels .copyOut)) malformed back atScan s₁ | |
| ((ambient, sign), false) bits suffix [] none | |
| simp only [canonical, ↓reduceIte, initial, List.append_nil] at native | |
| have transported := MachineStateEquiv.trace e back _ _ _ native | |
| rw [backForward] at transported | |
| simpa only [MachineStateEquiv.configuration, e, BinaryNameCompare.scanStateEquiv, Equiv.coe_fn_mk, | |
| BinaryNameMachine.clean, clean, BinaryNameCompare.clean, s₂, tapesAt] using transported | |
| have s₂permanent : s₂ (tape 1) = BinaryNameSearch.stream tokens := by | |
| simpa [s₂, s₁, hd 1 0 (by decide), hd 1 2 (by decide), hd 1 7 (by decide)] using permanent | |
| have s₂empty (i : Fin 9) (hi : i = 3 ∨ i = 4 ∨ i = 5 ∨ i = 6) : s₂ (tape i) = [] := by | |
| rcases hi with rfl | rfl | rfl | rfl <;> | |
| simp [s₂, s₁, hd, he] | |
| have copyRun : (advance (TM2.step program))^[2 * ((BinaryNameSearch.stream tokens).length + 1)] | |
| (some ⟨some (labels .copyOut), clean ambient sign, s₂⟩) = | |
| some ⟨some (labels (.search .sign)), clean ambient sign, s₃⟩ := by | |
| have h := MachineCopy.copyTrace (tape 1) (tape 3) (tape 6) | |
| (hd 1 3 (by decide)) (hd 1 6 (by decide)) (hd 3 6 (by decide)) false | |
| (labels .copyOut) (labels .copyBack) (some (labels (.search .sign))) program | |
| (atLabels .copyOut) (atLabels .copyBack) s₂ | |
| (s₂empty 6 (by simp)) ((ambient, sign), false, none) none | |
| simpa only [s₂permanent, s₂empty 3 (by simp), List.append_nil, | |
| clean, BinaryNameCompare.clean, s₃] using h | |
| have s₃key : s₃ (tape 2) = bits.reverse := by | |
| simp [s₃, s₂, hd 2 3 (by decide)] | |
| have s₃counter : s₃ (tape 7) = [false] := by | |
| simp [s₃, s₂, s₁, hd 7 3 (by decide), hd 7 2 (by decide), hd 7 0 (by decide)] | |
| have s₃stream : s₃ (tape 3) = BinaryNameSearch.stream tokens := by simp [s₃] | |
| have searchRun : (advance (TM2.step program))^[BinaryNameSearch.steps tokens bits] | |
| (some ⟨some (labels (.search .sign)), clean ambient sign, s₃⟩) = | |
| some ⟨some (labels .drainSearch), clean ambient sign, s₄⟩ := by | |
| have h := BinaryNameSearch.searchTrace (searchTapes tape) | |
| (searchTapes_injective tape distinct) (fun l => labels (.search l)) | |
| (some (labels .drainSearch)) malformed malformed program | |
| (fun l => atLabels (.search l)) s₃ (ambient, sign) tokens bits [] [false] | |
| tokensCanonical present s₃key | |
| (by simpa [s₃, searchTapes, hd 4 3 (by decide)] using s₂empty 4 (by simp)) | |
| (by simpa [s₃, searchTapes, hd 5 3 (by decide)] using s₂empty 5 (by simp)) | |
| (by simpa [s₃, searchTapes, hd 6 3 (by decide)] using s₂empty 6 (by simp)) | |
| have initial : tapesAt (tape 3) (tape 7) s₃ (BinaryNameSearch.stream tokens) [false] = s₃ := by | |
| rw [← s₃stream, ← s₃counter] | |
| exact tapesAt_self _ _ _ | |
| simpa only [searchTapes, List.append_nil, initial, clean, BinaryNameSearch.clean, | |
| s₄, tail, encodeWord, index] using h | |
| have s₄stream : s₄ (tape 3) = tail := by simp [s₄, hd 3 7 (by decide)] | |
| have drainSearchRun : (advance (TM2.step program))^[tail.length + 1] | |
| (some ⟨some (labels .drainSearch), clean ambient sign, s₄⟩) = | |
| some ⟨some (labels .drainKey), clean ambient sign, s₅⟩ := by | |
| have h := MachineDrain.drainTrace (tape 3) (labels .drainSearch) | |
| (some (labels .drainKey)) program (atLabels .drainSearch) s₄ tail | |
| ((ambient, sign), false, none) none | |
| have initial : Function.update s₄ (tape 3) tail = s₄ := by | |
| rw [← s₄stream]; exact Function.update_eq_self _ _ | |
| simpa only [initial, clean, BinaryNameCompare.clean, s₅] using h | |
| have s₅key : s₅ (tape 2) = bits.reverse := by | |
| simp [s₅, s₄, tapesAt, hd 2 3 (by decide), hd 2 7 (by decide), s₃key] | |
| have drainKeyRun : (advance (TM2.step program))^[bits.length + 1] | |
| (some ⟨some (labels .drainKey), clean ambient sign, s₅⟩) = | |
| some ⟨some (labels .emitIndex), clean ambient sign, s₆⟩ := by | |
| have h := MachineDrain.drainTrace (tape 2) (labels .drainKey) | |
| (some (labels .emitIndex)) program (atLabels .drainKey) s₅ bits.reverse | |
| ((ambient, sign), false, none) none | |
| have initial : Function.update s₅ (tape 2) bits.reverse = s₅ := by | |
| rw [← s₅key]; exact Function.update_eq_self _ _ | |
| simpa only [initial, List.length_reverse, clean, BinaryNameCompare.clean, s₆] using h | |
| have s₆index : s₆ (tape 7) = encodeWord (index tokens bits) := by | |
| simp [s₆, s₅, s₄, hd 7 2 (by decide), hd 7 3 (by decide)] | |
| have indexRun : (advance (TM2.step program))^[index tokens bits + 2] | |
| (some ⟨some (labels .emitIndex), clean ambient sign, s₆⟩) = | |
| some ⟨some (labels .emitSign), clean ambient sign, s₇⟩ := by | |
| change (nextAt (Γ := Alphabet) (tape 8) program)^[index tokens bits + 2] | |
| (some ⟨some (labels .emitIndex), (((ambient, sign), false, none), none), s₆⟩) = | |
| some ⟨some (labels .emitSign), (((ambient, sign), false, none), none), s₇⟩ | |
| have h := transferAt_fromTapes (Γ := Alphabet) (tape 7) (tape 8) | |
| (hd 7 8 (by decide)) id false (labels .emitIndex) (some (labels .emitSign)) program | |
| (atLabels .emitIndex) s₆ ((ambient, sign), false, none) none | |
| have time : index tokens bits + 1 + 1 = index tokens bits + 2 := by omega | |
| simpa only [s₆index, encodeWord_length, List.map_id_fun, id_eq, time, s₇] using h | |
| have emitRun : (advance (TM2.step program))^[1] | |
| (some ⟨some (labels .emitSign), clean ambient sign, s₇⟩) = | |
| some ⟨exit, clean ambient, s₈⟩ := by | |
| change some (TM2.stepAux (program (labels .emitSign)) _ _) = _ | |
| rw [atLabels .emitSign] | |
| cases sign <;> simp [instruction, emitSign, clean, BinaryNameCompare.clean, | |
| TM2.stepAux, signWord, encodeWord, s₈, Function.update_idem] | |
| have full := joinTrace (joinTrace (joinTrace (joinTrace (joinTrace (joinTrace | |
| (joinTrace signRun keyRun) copyRun) searchRun) drainSearchRun) drainKeyRun) indexRun) emitRun | |
| have finalTapes : s₈ = Function.update (Function.update base (tape 0) suffix) (tape 8) | |
| ((outputWord tokens bits sign).reverse ++ base (tape 8)) := by | |
| funext k | |
| by_cases h8 : k = tape 8 | |
| · subst k | |
| simp [s₈, s₇, s₆, s₅, s₄, s₃, s₂, s₁, tapesAt, outputWord, | |
| List.reverse_append, List.append_assoc, hd] | |
| · by_cases h0 : k = tape 0 | |
| · subst k | |
| simp [s₈, s₇, s₆, s₅, s₄, s₃, s₂, s₁, tapesAt, hd] | |
| · by_cases h2 : k = tape 2 | |
| · subst k | |
| simp [s₈, s₇, s₆, s₅, s₄, s₃, s₂, s₁, tapesAt, hd, he 2] | |
| · by_cases h3 : k = tape 3 | |
| · subst k | |
| simp [s₈, s₇, s₆, s₅, s₄, s₃, s₂, s₁, tapesAt, hd, he 3] | |
| · by_cases h7 : k = tape 7 | |
| · subst k | |
| simp [s₈, s₇, s₆, s₅, s₄, s₃, s₂, s₁, tapesAt, hd, he 7] | |
| · simp [s₈, s₇, s₆, s₅, s₄, s₃, s₂, s₁, tapesAt, h8, h0, h2, h3, h7] | |
| simpa only [steps, tail, finalTapes] using full | |
| /-- Polynomial resource bound attached to the proved literal execution. -/ | |
| def literalInTime (tape : Fin 9 → K) (distinct : Function.Injective tape) | |
| (labels : Label → Λ) (exit malformed : Option Λ) | |
| (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State A)) | |
| (atLabels : ∀ l, program (labels l) = instruction tape labels exit malformed l) | |
| (base : K → List Bool) (tokens : List BinaryNameSearch.Token) | |
| (sign : Bool) (bits suffix : List Bool) | |
| (canonical : BinaryNameMachine.canonical bits = true) | |
| (tokensCanonical : ∀ token ∈ tokens, BinaryNameMachine.canonical token.2 = true) | |
| (present : bits ∈ BinaryNameSearch.payloads tokens) | |
| (cursor : base (tape 0) = sign :: (BinaryNameMachine.frame bits ++ suffix)) | |
| (permanent : base (tape 1) = BinaryNameSearch.stream tokens) | |
| (empty : ∀ i : Fin 9, 2 ≤ i.val → i.val ≤ 7 → base (tape i) = []) | |
| (ambient : A) : | |
| StateTransition.EvalsToInTime (TM2.step program) | |
| ⟨some (labels .readSign), clean ambient, base⟩ | |
| (some ⟨exit, clean ambient, | |
| Function.update (Function.update base (tape 0) suffix) (tape 8) | |
| ((outputWord tokens bits sign).reverse ++ base (tape 8))⟩) | |
| (3 * ((BinaryNameSearch.stream tokens).length + bits.length) ^ 2 + | |
| 13 * ((BinaryNameSearch.stream tokens).length + bits.length) + 9) where | |
| steps := steps tokens bits | |
| evals_in_steps := literalTrace tape distinct labels exit malformed program atLabels base | |
| tokens sign bits suffix canonical tokensCanonical present cursor permanent empty ambient | |
| steps_le_m := steps_le tokens bits present | |
| end UniqueGamesTheorem.BinaryLiteralMachine | |
| end OAI | |