Download data/lean/OAI/Computability/MinUncut/Machines/MachineFiniteSequence.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 13.7 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/MinUncut/Machines/MachineFiniteSequence.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Computability/MinUncut/Machines/MachineFiniteSequence.lean
-
curl -L -o MachineFiniteSequence.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/MinUncut/Machines/MachineFiniteSequence.lean
13.7 kB
| import Mathlib | |
| import OAI.Computability.MinUncut.Machines.MachineDrain | |
| namespace OAI | |
| namespace MinUncutGames.Foundations.Complexity.MachineFiniteSequence | |
| open Turing MachineComposition | |
| variable {Command : Type} (LocalLabel : Command β Type) | |
| def Label : List Command β Type | |
| | [] => Empty | |
| | command :: commands => LocalLabel command β Label commands | |
| instance labelFintype [β command, Fintype (LocalLabel command)] | |
| (commands : List Command) : Fintype (Label LocalLabel commands) := by | |
| induction commands with | |
| | nil => exact inferInstanceAs (Fintype Empty) | |
| | cons command commands ih => | |
| letI := ih | |
| exact inferInstanceAs (Fintype (LocalLabel command β Label LocalLabel commands)) | |
| instance labelDecidableEq [β command, DecidableEq (LocalLabel command)] | |
| (commands : List Command) : DecidableEq (Label LocalLabel commands) := by | |
| induction commands with | |
| | nil => exact inferInstanceAs (DecidableEq Empty) | |
| | cons command commands ih => | |
| letI := ih | |
| exact inferInstanceAs (DecidableEq (LocalLabel command β Label LocalLabel commands)) | |
| variable (main : β command, LocalLabel command) | |
| variable {K Ξ Ο : Type} {Ξ : K β Type} | |
| def entry : (commands : List Command) β (Label LocalLabel commands β Ξ) β Option Ξ β Option Ξ | |
| | [], _, exit => exit | |
| | command :: _, labels, _ => some (labels (.inl (main command))) | |
| variable (localInstruction : β command, (LocalLabel command β Ξ) β Option Ξ β | |
| LocalLabel command β TM2.Stmt Ξ Ξ Ο) | |
| def instruction : (commands : List Command) β (Label LocalLabel commands β Ξ) β Option Ξ β | |
| Label LocalLabel commands β TM2.Stmt Ξ Ξ Ο | |
| | [], _, _, label => nomatch label | |
| | command :: commands, labels, exit, .inl label => | |
| localInstruction command (fun l => labels (.inl l)) | |
| (entry LocalLabel main commands (fun l => labels (.inr l)) exit) label | |
| | _ :: commands, labels, exit, .inr label => | |
| instruction commands (fun l => labels (.inr l)) exit label | |
| variable {Data : Type} (result : Command β Data β Data) (cost : Command β Data β Nat) | |
| def resultOf : List Command β Data β Data | |
| | [], data => data | |
| | command :: commands, data => resultOf commands (result command data) | |
| def steps : List Command β Data β Nat | |
| | [], _ => 0 | |
| | command :: commands, data => cost command data + steps commands (result command data) | |
| variable [DecidableEq K] | |
| theorem trace (program : Ξ β TM2.Stmt Ξ Ξ Ο) | |
| (invariant : Data β Prop) (state : Data β Ο) (tapes : Data β β k, List (Ξ k)) | |
| (commands : List Command) | |
| (localInvariant : β command β commands, β data, invariant data β | |
| invariant (result command data)) | |
| (localTrace : β command β commands, β (labels : LocalLabel command β Ξ) (exit : Option Ξ), | |
| (β l, program (labels l) = localInstruction command labels exit l) β | |
| β data, invariant data β | |
| (advance (TM2.step program))^[cost command data] | |
| (some β¨some (labels (main command)), state data, tapes dataβ©) = | |
| some β¨exit, state (result command data), tapes (result command data)β©) | |
| (labels : Label LocalLabel commands β Ξ) (exit : Option Ξ) | |
| (atLabels : β l, program (labels l) = | |
| instruction LocalLabel main localInstruction commands labels exit l) | |
| (data : Data) (valid : invariant data) : | |
| (advance (TM2.step program))^[steps result cost commands data] | |
| (some β¨entry LocalLabel main commands labels exit, state data, tapes dataβ©) = | |
| some β¨exit, state (resultOf result commands data), tapes (resultOf result commands data)β© := by | |
| induction commands generalizing data with | |
| | nil => rfl | |
| | cons command commands ih => | |
| have firstRun := localTrace command (by simp) | |
| (fun l => labels (.inl l)) | |
| (entry LocalLabel main commands (fun l => labels (.inr l)) exit) | |
| (fun l => atLabels (.inl l)) data valid | |
| have nextValid := localInvariant command (by simp) data valid | |
| have tailRun := ih | |
| (fun c hc d hd => localInvariant c (by simp [hc]) d hd) | |
| (fun c hc => localTrace c (by simp [hc])) | |
| (fun l => labels (.inr l)) (fun l => atLabels (.inr l)) | |
| (result command data) nextValid | |
| rw [steps, Nat.add_comm, Function.iterate_add_apply] | |
| change (advance (TM2.step program))^[steps result cost commands (result command data)] | |
| ((advance (TM2.step program))^[cost command data] | |
| (some β¨some (labels (.inl (main command))), state data, tapes dataβ©)) = _ | |
| rw [firstRun] | |
| exact tailRun | |
| end MinUncutGames.Foundations.Complexity.MachineFiniteSequence | |
| namespace MinUncutGames.Foundations.Complexity.MachineDrainMany | |
| open Turing MachineComposition | |
| variable {K Ξ Ο : Type} [DecidableEq K] | |
| abbrev Alphabet (_ : K) := Bool | |
| abbrev Tapes (K : Type) := K β List Bool | |
| abbrev LocalLabel (_ : K) := Unit | |
| abbrev Label (chosen : List K) := MachineFiniteSequence.Label (LocalLabel (K := K)) chosen | |
| abbrev Data (K : Type) := Tapes K Γ Option Bool | |
| def entry (chosen : List K) (labels : Label chosen β Ξ) (exit : Option Ξ) : Option Ξ := | |
| MachineFiniteSequence.entry (LocalLabel (K := K)) (fun _ => ()) chosen labels exit | |
| def localInstruction (source : K) (labels : Unit β Ξ) (exit : Option Ξ) (_ : Unit) : | |
| TM2.Stmt (Alphabet (K := K)) Ξ (Ο Γ Option Bool) := | |
| MachineDrain.drain source (labels ()) exit | |
| def instruction (chosen : List K) (labels : Label chosen β Ξ) (exit : Option Ξ) : | |
| Label chosen β TM2.Stmt (Alphabet (K := K)) Ξ (Ο Γ Option Bool) := | |
| MachineFiniteSequence.instruction (LocalLabel (K := K)) (fun _ => ()) | |
| localInstruction chosen labels exit | |
| def result (source : K) (data : Data K) : Data K := | |
| (Function.update data.1 source [], none) | |
| def cost (source : K) (data : Data K) : Nat := (data.1 source).length + 1 | |
| def finalTapes : List K β Tapes K β Tapes K | |
| | [], base => base | |
| | source :: chosen, base => finalTapes chosen (Function.update base source []) | |
| def finalRegister : List K β Option Bool β Option Bool | |
| | [], register => register | |
| | _ :: _, _ => none | |
| omit [DecidableEq K] in | |
| @[simp] theorem finalRegister_none (chosen : List K) : finalRegister chosen none = none := by | |
| cases chosen <;> rfl | |
| def steps : List K β Tapes K β Nat | |
| | [], _ => 0 | |
| | source :: chosen, base => (base source).length + 1 + | |
| steps chosen (Function.update base source []) | |
| @[simp] theorem sequence_result (chosen : List K) (base : Tapes K) (register : Option Bool) : | |
| MachineFiniteSequence.resultOf result chosen (base, register) = | |
| (finalTapes chosen base, finalRegister chosen register) := by | |
| induction chosen generalizing base register with | |
| | nil => rfl | |
| | cons source chosen ih => | |
| change MachineFiniteSequence.resultOf result chosen (Function.update base source [], none) = | |
| (finalTapes chosen (Function.update base source []), none) | |
| rw [ih, finalRegister_none] | |
| @[simp] theorem sequence_steps (chosen : List K) (base : Tapes K) (register : Option Bool) : | |
| MachineFiniteSequence.steps result cost chosen (base, register) = steps chosen base := by | |
| induction chosen generalizing base register with | |
| | nil => rfl | |
| | cons source chosen ih => | |
| simp only [MachineFiniteSequence.steps, result, cost, steps, ih] | |
| theorem finalTapes_apply (chosen : List K) (base : Tapes K) (k : K) : | |
| finalTapes chosen base k = if k β chosen then [] else base k := by | |
| induction chosen generalizing base with | |
| | nil => simp [finalTapes] | |
| | cons source chosen ih => | |
| rw [finalTapes, ih] | |
| by_cases h : k = source | |
| Β· subst k | |
| simp | |
| Β· simp [List.mem_cons, h] | |
| theorem finalTapes_mem (chosen : List K) (base : Tapes K) (k : K) (h : k β chosen) : | |
| finalTapes chosen base k = [] := by rw [finalTapes_apply, ite_eq_left h] | |
| theorem finalTapes_not_mem (chosen : List K) (base : Tapes K) (k : K) (h : k β chosen) : | |
| finalTapes chosen base k = base k := by rw [finalTapes_apply, ite_eq_right h] | |
| def lengthSum (chosen : List K) (base : Tapes K) : Nat := | |
| (chosen.map (fun k => (base k).length)).sum | |
| omit [DecidableEq K] in | |
| theorem lengthSum_mono (chosen : List K) (first second : Tapes K) | |
| (h : β k, (first k).length β€ (second k).length) : | |
| lengthSum chosen first β€ lengthSum chosen second := by | |
| induction chosen with | |
| | nil => exact Nat.le_refl 0 | |
| | cons source chosen ih => | |
| simp only [lengthSum, List.map_cons, List.sum_cons] at ih β’ | |
| exact Nat.add_le_add (h source) ih | |
| theorem steps_le (chosen : List K) (base : Tapes K) : | |
| steps chosen base β€ lengthSum chosen base + chosen.length := by | |
| induction chosen generalizing base with | |
| | nil => simp [steps, lengthSum] | |
| | cons source chosen ih => | |
| have hshort : β k, ((Function.update base source []) k).length β€ (base k).length := by | |
| intro k | |
| by_cases h : k = source | |
| Β· subst k | |
| simp | |
| Β· simp [Function.update_of_ne h] | |
| have hsum := lengthSum_mono chosen (Function.update base source []) base hshort | |
| have htail := ih (Function.update base source []) | |
| simp only [steps, lengthSum, List.map_cons, List.sum_cons, List.length_cons] at hsum htail β’ | |
| omega | |
| theorem steps_le_uniform (chosen : List K) (base : Tapes K) (bound : Nat) | |
| (h : β k, (base k).length β€ bound) : | |
| steps chosen base β€ chosen.length * (bound + 1) := by | |
| have hsum : lengthSum chosen base β€ chosen.length * bound := by | |
| induction chosen with | |
| | nil => simp [lengthSum] | |
| | cons source chosen ih => | |
| simp only [lengthSum, List.map_cons, List.sum_cons, List.length_cons, Nat.add_mul, | |
| Nat.one_mul] at ih β’ | |
| have hs := h source | |
| omega | |
| have hc := steps_le chosen base | |
| rw [Nat.mul_add, Nat.mul_one] | |
| omega | |
| theorem trace (chosen : List K) (labels : Label chosen β Ξ) (exit : Option Ξ) | |
| (program : Ξ β TM2.Stmt (Alphabet (K := K)) Ξ (Ο Γ Option Bool)) | |
| (atLabels : β l, program (labels l) = instruction chosen labels exit l) | |
| (base : Tapes K) (ambient : Ο) (register : Option Bool) : | |
| (advance (TM2.step program))^[steps chosen base] | |
| (some β¨entry chosen labels exit, (ambient, register), baseβ©) = | |
| some β¨exit, (ambient, finalRegister chosen register), finalTapes chosen baseβ© := by | |
| have run := MachineFiniteSequence.trace | |
| (LocalLabel := LocalLabel (K := K)) (main := fun _ => ()) | |
| (localInstruction := localInstruction (Ο := Ο)) (result := result) (cost := cost) | |
| program (fun _ : Data K => True) (fun data => (ambient, data.2)) (fun data => data.1) | |
| chosen (by intros; trivial) | |
| (by | |
| intro source _ localLabels localExit atLocal data _ | |
| rcases data with β¨tapes, regβ© | |
| simpa only [cost, result, Function.update_eq_self] using | |
| MachineDrain.drainTrace source (localLabels ()) localExit program (atLocal ()) | |
| tapes (tapes source) ambient reg) | |
| labels exit atLabels (base, register) trivial | |
| simpa only [sequence_steps, sequence_result, entry] using run | |
| def execution (chosen : List K) (labels : Label chosen β Ξ) (exit : Option Ξ) | |
| (program : Ξ β TM2.Stmt (Alphabet (K := K)) Ξ (Ο Γ Option Bool)) | |
| (atLabels : β l, program (labels l) = instruction chosen labels exit l) | |
| (base : Tapes K) (ambient : Ο) (register : Option Bool) : | |
| StateTransition.EvalsToInTime (TM2.step program) | |
| β¨entry chosen labels exit, (ambient, register), baseβ© | |
| (some β¨exit, (ambient, finalRegister chosen register), finalTapes chosen baseβ©) | |
| (lengthSum chosen base + chosen.length) where | |
| steps := steps chosen base | |
| evals_in_steps := trace chosen labels exit program atLabels base ambient register | |
| steps_le_m := steps_le chosen base | |
| def workTapes {N : Nat} (enumeration : Fin N β K) (output : K) : List K := | |
| (List.ofFn (fun i => enumeration i)).filter (fun k => decide (k β output)) | |
| @[simp] theorem mem_workTapes {N : Nat} (enumeration : Fin N β K) (output k : K) : | |
| k β workTapes enumeration output β k β output := by | |
| have hmem : k β List.ofFn (fun i => enumeration i) := by | |
| apply List.mem_ofFn.mpr | |
| exact β¨enumeration.symm k, enumeration.apply_symm_apply kβ© | |
| simp [workTapes, hmem] | |
| theorem workTapes_length_le {N : Nat} (enumeration : Fin N β K) (output : K) : | |
| (workTapes enumeration output).length β€ N := by | |
| simpa only [workTapes, List.length_ofFn] using | |
| List.length_filter_le (fun k => decide (k β output)) (List.ofFn (fun i => enumeration i)) | |
| def haltTapes (output : K) (word : List Bool) : Tapes K := | |
| fun k => if k = output then word else [] | |
| theorem finalTapes_workTapes {N : Nat} (enumeration : Fin N β K) (output : K) | |
| (base : Tapes K) : | |
| finalTapes (workTapes enumeration output) base = haltTapes output (base output) := by | |
| funext k | |
| rw [finalTapes_apply] | |
| by_cases h : k = output | |
| Β· subst k | |
| simp [haltTapes] | |
| Β· simp [haltTapes, h] | |
| theorem cleanupTrace {N : Nat} (enumeration : Fin N β K) (output : K) | |
| (labels : Label (workTapes enumeration output) β Ξ) | |
| (program : Ξ β TM2.Stmt (Alphabet (K := K)) Ξ (Ο Γ Option Bool)) | |
| (atLabels : β l, program (labels l) = | |
| instruction (workTapes enumeration output) labels none l) | |
| (base : Tapes K) (ambient : Ο) : | |
| (advance (TM2.step program))^[steps (workTapes enumeration output) base] | |
| (some β¨entry (workTapes enumeration output) labels none, (ambient, none), baseβ©) = | |
| some β¨none, (ambient, none), haltTapes output (base output)β© := by | |
| simpa only [finalRegister_none, finalTapes_workTapes] using | |
| trace (workTapes enumeration output) labels none program atLabels base ambient none | |
| end MinUncutGames.Foundations.Complexity.MachineDrainMany | |
| end OAI | |