Download lean/OAI/Computability/BinPacking/Reductions/MachineTransducer.lean from roseking/openAI-math-dataset: direct link, hf CLI and curl.
- Browser
- Download file 12.3 kB
-
https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Computability/BinPacking/Reductions/MachineTransducer.lean
- Command line
-
hf download hf://datasets/roseking/openAI-math-dataset/lean/OAI/Computability/BinPacking/Reductions/MachineTransducer.lean
-
curl -L -o MachineTransducer.lean https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Computability/BinPacking/Reductions/MachineTransducer.lean
12.3 kB
| import Mathlib.Data.Finset.Lattice.Fold | |
| import OAI.Computability.BinPacking.Computation.MachineFiniteAlphabet | |
| import OAI.Computability.BinPacking.Reductions.MachineReverse | |
| namespace OAI | |
| namespace BinPackingGames.Reduction.MachineTransducer | |
| open Turing | |
| open BinPackingGames.Foundations.Complexity | |
| open MachineSubstitution (pushWord stepAux_pushWord statementPushBound_pushWord) | |
| def tapeStacks (input output : List Bool) : Bool → List Bool := | |
| fun side => if side then output else input | |
| private theorem update_input (input output replacement : List Bool) : | |
| Function.update (tapeStacks input output) false replacement = tapeStacks replacement output := by | |
| funext side | |
| cases side <;> simp [tapeStacks] | |
| private theorem update_output (input output replacement : List Bool) : | |
| Function.update (tapeStacks input output) true replacement = tapeStacks input replacement := by | |
| funext side | |
| cases side <;> simp [tapeStacks] | |
| variable {Q : Type} [Fintype Q] | |
| def output (transition : Q → Bool → Q) (emit : Q → Bool → List Bool) : | |
| Q → List Bool → List Bool | |
| | _, [] => [] | |
| | state, symbol :: input => | |
| emit state symbol ++ output transition emit (transition state symbol) input | |
| def loop (initial : Q) : | |
| TM2.Stmt (fun _ : Bool => Bool) (Option (Q × Bool)) (Q × Option Bool) := | |
| .pop false (fun state head => (state.1, head)) | |
| (.branch (fun state => state.2.isSome) | |
| (.goto fun state => some (state.1, state.2.getD false)) | |
| (.load (fun _ => (initial, none)) .halt)) | |
| def program (initial : Q) (transition : Q → Bool → Q) (emit : Q → Bool → List Bool) : | |
| Option (Q × Bool) → | |
| TM2.Stmt (fun _ : Bool => Bool) (Option (Q × Bool)) (Q × Option Bool) | |
| | none => loop initial | |
| | some (state, symbol) => | |
| .load (fun register => (transition state symbol, register.2)) | |
| (pushWord true (emit state symbol) (.goto fun _ => none)) | |
| def machine (initial : Q) (transition : Q → Bool → Q) (emit : Q → Bool → List Bool) : | |
| FinTM2 where | |
| K := Bool | |
| k₀ := false | |
| k₁ := true | |
| Γ _ := Bool | |
| Λ := Option (Q × Bool) | |
| main := none | |
| σ := Q × Option Bool | |
| initialState := (initial, none) | |
| m := program initial transition emit | |
| noncomputable def maxEmission (emit : Q → Bool → List Bool) : Nat := | |
| Finset.univ.sup (fun pair : Q × Bool => (emit pair.1 pair.2).length) | |
| theorem emission_le (emit : Q → Bool → List Bool) (state : Q) (symbol : Bool) : | |
| (emit state symbol).length ≤ maxEmission emit := by | |
| exact Finset.le_sup (f := fun pair : Q × Bool => (emit pair.1 pair.2).length) | |
| (Finset.mem_univ (state, symbol)) | |
| theorem statementPushBound_le (initial : Q) (transition : Q → Bool → Q) | |
| (emit : Q → Bool → List Bool) (label : Option (Q × Bool)) : | |
| Runtime.statementPushBound (program initial transition emit label) ≤ maxEmission emit := by | |
| cases label with | |
| | none => simp [program, loop, Runtime.statementPushBound] | |
| | some pair => | |
| simpa only [program, Runtime.statementPushBound, statementPushBound_pushWord, | |
| Nat.add_zero] using emission_le emit pair.1 pair.2 | |
| theorem programPushBound_le (initial : Q) (transition : Q → Bool → Q) | |
| (emit : Q → Bool → List Bool) : | |
| Runtime.programPushBound (machine initial transition emit) ≤ maxEmission emit := by | |
| have allLabels (labels : List (Option (Q × Bool))) : | |
| Runtime.maxLabelPushes (program initial transition emit) labels ≤ maxEmission emit := by | |
| induction labels with | |
| | nil => exact Nat.zero_le _ | |
| | cons label rest ih => | |
| exact max_le (statementPushBound_le initial transition emit label) ih | |
| exact allLabels (machine initial transition emit).ΛFin.elems.toList | |
| def running (initial : Q) (transition : Q → Bool → Q) (emit : Q → Bool → List Bool) | |
| (state : Q) (input accumulator : List Bool) (register : Option Bool) : | |
| (machine initial transition emit).Cfg := | |
| ⟨some none, (state, register), tapeStacks input accumulator⟩ | |
| def emitting (initial : Q) (transition : Q → Bool → Q) (emit : Q → Bool → List Bool) | |
| (oldState : Q) (symbol : Bool) (input accumulator : List Bool) | |
| (state : Q × Option Bool) : (machine initial transition emit).Cfg := | |
| ⟨some (some (oldState, symbol)), state, tapeStacks input accumulator⟩ | |
| def halted (initial : Q) (transition : Q → Bool → Q) (emit : Q → Bool → List Bool) | |
| (accumulator : List Bool) : (machine initial transition emit).Cfg := | |
| ⟨none, (initial, none), tapeStacks [] accumulator⟩ | |
| theorem step_empty (initial : Q) (transition : Q → Bool → Q) | |
| (emit : Q → Bool → List Bool) (state : Q) (accumulator : List Bool) | |
| (register : Option Bool) : | |
| (machine initial transition emit).step | |
| (running initial transition emit state [] accumulator register) = | |
| some (halted initial transition emit accumulator) := by | |
| change some (TM2.stepAux (loop initial) (state, register) (tapeStacks [] accumulator)) = _ | |
| simp [loop, TM2.stepAux, tapeStacks, halted] | |
| exact congrArg (fun tapes => some (⟨none, (initial, none), tapes⟩ : | |
| (machine initial transition emit).Cfg)) (update_input [] accumulator []) | |
| theorem step_cons (initial : Q) (transition : Q → Bool → Q) | |
| (emit : Q → Bool → List Bool) (state : Q) (symbol : Bool) | |
| (input accumulator : List Bool) (register : Option Bool) : | |
| (machine initial transition emit).step | |
| (running initial transition emit state (symbol :: input) accumulator register) = | |
| some (emitting initial transition emit state symbol input accumulator (state, some symbol)) := by | |
| change some (TM2.stepAux (loop initial) (state, register) | |
| (tapeStacks (symbol :: input) accumulator)) = _ | |
| simp [loop, TM2.stepAux, tapeStacks, emitting] | |
| rw [update_input] | |
| rfl | |
| theorem step_emit (initial : Q) (transition : Q → Bool → Q) | |
| (emit : Q → Bool → List Bool) (oldState : Q) (symbol : Bool) | |
| (input accumulator : List Bool) (state : Q × Option Bool) : | |
| (machine initial transition emit).step | |
| (emitting initial transition emit oldState symbol input accumulator state) = | |
| some (running initial transition emit (transition oldState symbol) input | |
| ((emit oldState symbol).reverse ++ accumulator) state.2) := by | |
| change some (TM2.stepAux (pushWord true (emit oldState symbol) (.goto fun _ => none)) | |
| (transition oldState symbol, state.2) (tapeStacks input accumulator)) = _ | |
| rw [stepAux_pushWord] | |
| simp only [TM2.stepAux] | |
| change some (⟨some none, (transition oldState symbol, state.2), | |
| Function.update (tapeStacks input accumulator) true | |
| ((emit oldState symbol).reverse ++ accumulator)⟩ : | |
| (machine initial transition emit).Cfg) = _ | |
| exact congrArg (fun tapes => some (⟨some none, (transition oldState symbol, state.2), tapes⟩ : | |
| (machine initial transition emit).Cfg)) | |
| (update_output input accumulator ((emit oldState symbol).reverse ++ accumulator)) | |
| def next (initial : Q) (transition : Q → Bool → Q) (emit : Q → Bool → List Bool) | |
| (configuration : Option (machine initial transition emit).Cfg) : | |
| Option (machine initial transition emit).Cfg := | |
| configuration.bind (machine initial transition emit).step | |
| theorem two_steps_cons (initial : Q) (transition : Q → Bool → Q) | |
| (emit : Q → Bool → List Bool) (state : Q) (symbol : Bool) | |
| (input accumulator : List Bool) (register : Option Bool) : | |
| (next initial transition emit)^[2] | |
| (some (running initial transition emit state (symbol :: input) accumulator register)) = | |
| some (running initial transition emit (transition state symbol) input | |
| ((emit state symbol).reverse ++ accumulator) (some symbol)) := by | |
| change (machine initial transition emit).step | |
| (running initial transition emit state (symbol :: input) accumulator register) >>= | |
| (machine initial transition emit).step = _ | |
| rw [step_cons] | |
| exact step_emit initial transition emit state symbol input accumulator (state, some symbol) | |
| theorem transduce_steps (initial : Q) (transition : Q → Bool → Q) | |
| (emit : Q → Bool → List Bool) (state : Q) (input accumulator : List Bool) | |
| (register : Option Bool) : | |
| (next initial transition emit)^[2 * input.length + 1] | |
| (some (running initial transition emit state input accumulator register)) = | |
| some (halted initial transition emit ((output transition emit state input).reverse ++ accumulator)) := by | |
| induction input generalizing state accumulator register with | |
| | nil => | |
| simpa only [List.length_nil, Nat.mul_zero, Nat.zero_add, Function.iterate_one, next, | |
| Option.bind_some, output, List.reverse_nil, List.nil_append] | |
| using step_empty initial transition emit state accumulator register | |
| | cons symbol input ih => | |
| rw [List.length_cons] | |
| rw [show 2 * (input.length + 1) + 1 = (2 * input.length + 1) + 2 by omega] | |
| rw [Function.iterate_add_apply, two_steps_cons, ih] | |
| simp only [output, List.reverse_append, List.append_assoc] | |
| theorem initList_eq (initial : Q) (transition : Q → Bool → Q) | |
| (emit : Q → Bool → List Bool) (input : List Bool) : | |
| initList (machine initial transition emit) input = | |
| running initial transition emit initial input [] none := by | |
| unfold initList running | |
| congr 1 | |
| funext side | |
| cases side <;> rfl | |
| theorem haltList_eq (initial : Q) (transition : Q → Bool → Q) | |
| (emit : Q → Bool → List Bool) (accumulator : List Bool) : | |
| haltList (machine initial transition emit) accumulator = | |
| halted initial transition emit accumulator := by | |
| unfold haltList halted | |
| congr 1 | |
| theorem transduce_init_steps (initial : Q) (transition : Q → Bool → Q) | |
| (emit : Q → Bool → List Bool) (input : List Bool) : | |
| (next initial transition emit)^[2 * input.length + 1] | |
| (some (initList (machine initial transition emit) input)) = | |
| some (haltList (machine initial transition emit) (output transition emit initial input).reverse) := by | |
| rw [initList_eq, haltList_eq] | |
| simpa only [List.append_nil] using transduce_steps initial transition emit initial input [] none | |
| def outputsInTime (initial : Q) (transition : Q → Bool → Q) | |
| (emit : Q → Bool → List Bool) (input : List Bool) : | |
| TM2OutputsInTime (machine initial transition emit) input | |
| (some (output transition emit initial input).reverse) (2 * input.length + 1) where | |
| steps := 2 * input.length + 1 | |
| evals_in_steps := transduce_init_steps initial transition emit input | |
| steps_le_m := Nat.le_refl _ | |
| @[simp] theorem outputsInTime_steps (initial : Q) (transition : Q → Bool → Q) | |
| (emit : Q → Bool → List Bool) (input : List Bool) : | |
| (outputsInTime initial transition emit input).steps = 2 * input.length + 1 := rfl | |
| noncomputable def reversedComputableInPolyTime (initial : Q) (transition : Q → Bool → Q) | |
| (emit : Q → Bool → List Bool) : | |
| TM2ComputableInPolyTime (id : List Bool → List Bool) id | |
| (fun input => (output transition emit initial input).reverse) where | |
| tm := machine initial transition emit | |
| inputAlphabet := Equiv.refl Bool | |
| outputAlphabet := Equiv.refl Bool | |
| time := 2 * Polynomial.X + 1 | |
| outputsFun input := by | |
| change TM2OutputsInTime (machine initial transition emit) (input.map id) | |
| (some ((output transition emit initial input).reverse.map id)) | |
| ((2 * Polynomial.X + 1 : Polynomial Nat).eval input.length) | |
| have hi := .map_id ((machine initial transition emit).Γ | |
| (machine initial transition emit).k₀) input | |
| have ho := .map_id ((machine initial transition emit).Γ | |
| (machine initial transition emit).k₁) (output transition emit initial input).reverse | |
| rw [hi, ho] | |
| simpa only [Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_X, | |
| Polynomial.eval_ofNat, Polynomial.eval_one] using outputsInTime initial transition emit input | |
| noncomputable def computableInPolyTime (initial : Q) (transition : Q → Bool → Q) | |
| (emit : Q → Bool → List Bool) : | |
| TM2ComputableInPolyTime (id : List Bool → List Bool) id | |
| (output transition emit initial) := by | |
| simpa only [List.reverse_reverse] using | |
| MachineSequential.composeBits (reversedComputableInPolyTime initial transition emit) | |
| MachineReverse.computableInPolyTime | |
| end BinPackingGames.Reduction.MachineTransducer | |
| end OAI | |