Download lean/OAI/Computability/BinPacking/Reductions/MachineReverse.lean from roseking/openAI-math-dataset: direct link, hf CLI and curl.
- Browser
- Download file 4.4 kB
-
https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Computability/BinPacking/Reductions/MachineReverse.lean
- Command line
-
hf download hf://datasets/roseking/openAI-math-dataset/lean/OAI/Computability/BinPacking/Reductions/MachineReverse.lean
-
curl -L -o MachineReverse.lean https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Computability/BinPacking/Reductions/MachineReverse.lean
4.4 kB
| import Mathlib.Computability.TuringMachine.Computable | |
| namespace OAI | |
| namespace BinPackingGames.Reduction.MachineReverse | |
| open Turing | |
| def loop : TM2.Stmt (fun _ : Bool => Bool) Unit (Option Bool) := | |
| .pop false (fun _ head => head) | |
| (.branch Option.isSome | |
| (.push true (fun state => state.getD false) (.goto fun _ => ())) | |
| .halt) | |
| def machine : FinTM2 where | |
| K := Bool | |
| k₀ := false | |
| k₁ := true | |
| Γ _ := Bool | |
| Λ := Unit | |
| main := () | |
| σ := Option Bool | |
| initialState := none | |
| m _ := loop | |
| def tapeStacks (input output : List Bool) : Bool → List Bool := | |
| fun side => if side then output else input | |
| def running (input output : List Bool) (state : Option Bool) : machine.Cfg := | |
| ⟨some (), state, tapeStacks input output⟩ | |
| def halted (output : List Bool) : machine.Cfg := | |
| ⟨none, none, tapeStacks [] output⟩ | |
| 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] | |
| theorem step_empty (output : List Bool) (state : Option Bool) : | |
| machine.step (running [] output state) = some (halted output) := by | |
| change some (TM2.stepAux loop state (tapeStacks [] output)) = some (halted output) | |
| simp [loop, TM2.stepAux, tapeStacks, halted, Function.update] | |
| rw [update_input] | |
| rfl | |
| theorem step_cons (head : Bool) (input output : List Bool) (state : Option Bool) : | |
| machine.step (running (head :: input) output state) = | |
| some (running input (head :: output) (some head)) := by | |
| change some (TM2.stepAux loop state (tapeStacks (head :: input) output)) = _ | |
| simp [loop, TM2.stepAux, tapeStacks, running, Function.update] | |
| rw [update_input, update_output] | |
| rfl | |
| def next (configuration : Option machine.Cfg) : Option machine.Cfg := | |
| configuration.bind machine.step | |
| theorem reverse_steps (input output : List Bool) (state : Option Bool) : | |
| next^[input.length + 1] (some (running input output state)) = | |
| some (halted (input.reverse ++ output)) := by | |
| induction input generalizing output state with | |
| | nil => | |
| simpa only [List.length_nil, Nat.zero_add, Function.iterate_one, next, | |
| Option.bind_some, List.reverse_nil, List.nil_append] using step_empty output state | |
| | cons head input ih => | |
| rw [List.length_cons, Function.iterate_succ_apply] | |
| change next^[input.length + 1] | |
| (machine.step (running (head :: input) output state)) = _ | |
| rw [step_cons, ih] | |
| simp only [List.reverse_cons, List.append_assoc, List.singleton_append] | |
| theorem initList_eq (input : List Bool) : | |
| initList machine input = running input [] none := by | |
| unfold initList running | |
| congr 1 | |
| funext side | |
| cases side <;> rfl | |
| theorem haltList_eq (output : List Bool) : haltList machine output = halted output := by | |
| unfold haltList halted | |
| congr 1 | |
| theorem reverse_init_steps (input : List Bool) : | |
| next^[input.length + 1] (some (initList machine input)) = | |
| some (haltList machine input.reverse) := by | |
| rw [initList_eq, haltList_eq] | |
| simpa only [List.append_nil] using reverse_steps input [] none | |
| def outputsInTime (input : List Bool) : | |
| TM2OutputsInTime machine input (some input.reverse) (input.length + 1) where | |
| steps := input.length + 1 | |
| evals_in_steps := reverse_init_steps input | |
| steps_le_m := Nat.le_refl _ | |
| @[simp] theorem outputsInTime_steps (input : List Bool) : | |
| (outputsInTime input).steps = input.length + 1 := rfl | |
| noncomputable def computableInPolyTime : | |
| TM2ComputableInPolyTime (id : List Bool → List Bool) id List.reverse where | |
| tm := machine | |
| inputAlphabet := Equiv.refl Bool | |
| outputAlphabet := Equiv.refl Bool | |
| time := Polynomial.X + 1 | |
| outputsFun input := by | |
| change TM2OutputsInTime machine (input.map id) (some (input.reverse.map id)) | |
| ((Polynomial.X + 1 : Polynomial Nat).eval input.length) | |
| have hi := @List.map_id (machine.Γ machine.k₀) input | |
| have ho := @List.map_id (machine.Γ machine.k₁) input.reverse | |
| rw [hi, ho] | |
| simpa only [Polynomial.eval_add, Polynomial.eval_X, Polynomial.eval_one] | |
| using outputsInTime input | |
| end BinPackingGames.Reduction.MachineReverse | |
| end OAI | |