Download data/lean/OAI/Computability/SpaceSimulation/Machine.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 7.49 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/SpaceSimulation/Machine.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Computability/SpaceSimulation/Machine.lean
-
curl -L -o Machine.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/SpaceSimulation/Machine.lean
7.49 kB
| import Mathlib | |
| namespace OAI | |
| /-! | |
| Finite table machines for the result. X is the public input alphabet (all | |
| uses for machines as space bounds have finite types); J indexes a fixed | |
| number of other read-only tapes, on a binary alphabet. Each read-only tape | |
| is endmarked and has one confined head. Work tapes for the simulator and | |
| accessor are two-way, with unit moves. Terminal output in a finite set | |
| may be specified in the transition table (equivalently transition to the | |
| associated halt state). TM transitions with writes write before moving. | |
| Space convention is visited work cells (including the origin of each work | |
| tape), weighted by bits/symbol in the fixed alphabets, excluding read-only | |
| tapes. Since visited sets are intervals, we use a window certificate including | |
| all head positions during the run. | |
| -/ | |
| namespace Fifths | |
| inductive Dir | |
| | L | N | R | |
| deriving DecidableEq | |
| instance : Fintype Dir := β¨{.L, .N, .R}, by intro d; cases d <;> simpβ© | |
| namespace Dir | |
| @[simp] def delta : Dir β β€ | |
| | L => -1 | |
| | N => 0 | |
| | R => 1 | |
| theorem bounds (d : Dir) : -1 β€ d.delta β§ d.delta β€ 1 := by | |
| cases d <;> simp | |
| end Dir | |
| /-- Left endmarker at 0, then the word, then right endmarker. -/ | |
| inductive Mark (A : Type) | |
| | left | |
| | sym (a : A) | |
| | right | |
| deriving DecidableEq, Fintype | |
| namespace Mark | |
| variable {A : Type} | |
| def see (w : List A) : β β Mark A | |
| | 0 => left | |
| | p+1 => | |
| match w[p]? with | |
| | some a => sym a | |
| | none => right | |
| @[simp] theorem see_zero (w : List A) : see w 0 = left := rfl | |
| @[simp] theorem see_left (w : List A) (p) : see w p = left β p = 0 := by | |
| cases p with | |
| | zero => simp | |
| | succ p => cases e : w[p]? <;> simp [see, e] | |
| @[simp] theorem see_right (w : List A) (p) : see w p = right β w.length < p := by | |
| cases p with | |
| | zero => simp | |
| | succ p => | |
| by_cases h : p < w.length | |
| Β· simp [see, h, Nat.not_lt.mpr (Nat.succ_le_of_lt h)] | |
| Β· simp only [see, getElem?_neg w p h, true_iff] | |
| omega | |
| def walk (w : List A) (p : β) : Dir β β | |
| | .N => p | |
| | .L => p - 1 | |
| | .R => if w.length < p then p else p + 1 | |
| theorem walk_bound (w : List A) (p : β) (d : Dir) (h : p β€ w.length + 1) : | |
| walk w p d β€ w.length + 1 := by | |
| cases d | |
| Β· simp only [walk] | |
| omega | |
| Β· exact h | |
| Β· simp only [walk] | |
| split <;> omega | |
| end Mark | |
| /-- Deterministic halting runs. `f s = inl y` means the transition | |
| specifies halt with that label (no more working moves). `P` holds | |
| also at that final configuration. -/ | |
| inductive Runs {Ο Y : Type} (f : Ο β Y β Ο) (P : Ο β Prop) : | |
| Ο β Ο β Y β Prop | |
| | stop {s y} (ok : P s) (eqn : f s = Sum.inl y) : Runs f P s s y | |
| | step {s s' t y} (ok : P s) (eqn : f s = Sum.inr s') | |
| (tail : Runs f P s' t y) : Runs f P s t y | |
| namespace Runs | |
| variable {Ο Y : Type} {f : Ο β Y β Ο} {P Q : Ο β Prop} {s t : Ο} {y : Y} | |
| theorem mono (h : β z, P z β Q z) (r : Runs f P s t y) : Runs f Q s t y := by | |
| induction r with | |
| | stop o e => exact .stop (h _ o) e | |
| | step o e r ih => exact .step (h _ o) e ih | |
| theorem startOk (r : Runs f P s t y) : P s := by cases r <;> assumption | |
| theorem endOk (r : Runs f P s t y) : P t := by induction r <;> assumption | |
| theorem endStop (r : Runs f P s t y) : f t = Sum.inl y := by induction r <;> assumption | |
| theorem unique (r : Runs f P s t y) {u v} (r' : Runs f Q s u v) : (t, y) = (u, v) := by | |
| induction r generalizing u v with | |
| | stop o e => | |
| cases r' with | |
| | stop _ e' => | |
| rw [e] at e' | |
| congr | |
| injection e' | |
| | step _ e' _ => rw [e] at e'; contradiction | |
| | step _ e _ ih => | |
| cases r' with | |
| | stop _ e' => rw [e] at e'; contradiction | |
| | step _ e' tail => | |
| rw [e] at e' | |
| obtain rfl : _ = _ := Sum.inr.inj e' | |
| exact ih tail | |
| end Runs | |
| /-- Common read-only inputs. -/ | |
| structure Inputs (X J : Type) where | |
| publicWord : List X | |
| arg : J β List Bool | |
| structure Readers (J : Type) where | |
| px : β := 0 | |
| parg : J β β := fun _ => 0 | |
| structure Reads (X J : Type) where | |
| sx : Mark X | |
| sarg : J β Mark Bool | |
| structure Advances (J : Type) where | |
| dx : Dir := .N | |
| darg : J β Dir := fun _ => .N | |
| namespace Readers | |
| variable {X J : Type} | |
| def home : Readers J := {} | |
| def see (r : Readers J) (i : Inputs X J) : Reads X J := | |
| β¨Mark.see i.publicWord r.px, fun j => Mark.see (i.arg j) (r.parg j)β© | |
| def walk (r : Readers J) (i : Inputs X J) (a : Advances J) : Readers J := | |
| β¨Mark.walk i.publicWord r.px a.dx, fun j => Mark.walk (i.arg j) (r.parg j) (a.darg j)β© | |
| @[simp] theorem stationary (r : Readers J) (i : Inputs X J) : r.walk i {} = r := rfl | |
| end Readers | |
| structure Tape (A : Type) where | |
| pos : β€ | |
| sym : β€ β A | |
| structure WriteMove (A : Type) where | |
| sym : A | |
| dir : Dir | |
| namespace Tape | |
| variable {A : Type} | |
| def init (a : A) : Tape A := β¨0, fun _ => aβ© | |
| def see (w : Tape A) : A := w.sym w.pos | |
| def perform (w : Tape A) (m : WriteMove A) : Tape A := | |
| β¨w.pos + m.dir.delta, Function.update w.sym w.pos m.symβ© | |
| end Tape | |
| structure TAction (Q J K : Type) (W : K β Type) where | |
| next : Q | |
| advance : Advances J | |
| moves : β k : K, WriteMove (W k) | |
| /-- Finiteness of X, J and Y supplied in main theorem. Finite functions | |
| in the table present no uniformity issue; all are fixed for the machine. -/ | |
| structure TM (X J Y : Type) where | |
| Q : Type | |
| [finiteQ : Finite Q] | |
| K : Type | |
| [tapes : Fintype K] | |
| W : K β Type | |
| [symbols : β k, Fintype (W k)] | |
| blank : β k, W k | |
| start : Q | |
| table : Q β Reads X J β ((k : K) β W k) β Y β TAction Q J K W | |
| attribute [instance] TM.finiteQ TM.tapes TM.symbols | |
| namespace TM | |
| variable {X J Y : Type} (M : TM X J Y) | |
| structure Cfg where | |
| state : M.Q | |
| ro : Readers J | |
| work : β k, Tape (M.W k) | |
| def initial : M.Cfg := | |
| β¨M.start, .home, fun k => .init (M.blank k)β© | |
| def step (i : Inputs X J) (s : M.Cfg) : Y β M.Cfg := | |
| match M.table s.state (s.ro.see i) (fun k => (s.work k).see) with | |
| | Sum.inl y => .inl y | |
| | Sum.inr a => | |
| .inr β¨a.next, s.ro.walk i a.advance, fun k => (s.work k).perform (a.moves k)β© | |
| /-- Per tape visited positions contained in [-l,h], both nonnegative. -/ | |
| structure Window where | |
| l : M.K β β | |
| h : M.K β β | |
| /-- Bits per symbol (at least one). -/ | |
| def width (k : M.K) : β := max 1 (Fintype.card (M.W k) - 1).size | |
| theorem width_pos (k : M.K) : 0 < M.width k := | |
| Nat.succ_le_iff.mp (le_max_left ..) | |
| namespace Window | |
| variable {M} | |
| def ok (w : M.Window) (s : M.Cfg) : Prop := | |
| β k, -(w.l k : β€) β€ (s.work k).pos β§ (s.work k).pos β€ (w.h k : β€) | |
| open BigOperators | |
| def bits (w : M.Window) : β := | |
| β k, (w.l k + w.h k + 1) * M.width k | |
| theorem span_le (w : M.Window) (k : M.K) : w.l k + w.h k + 1 β€ w.bits := calc | |
| _ β€ (w.l k + w.h k + 1) * M.width k := | |
| Nat.le_mul_of_pos_right _ (M.width_pos k) | |
| _ β€ w.bits := | |
| Finset.single_le_sum (f := fun k => (w.l k + w.h k + 1) * M.width k) | |
| (fun _ _ => Nat.zero_le _) (Finset.mem_univ k) | |
| end Window | |
| /-- `M` outputs `y` using at most `S` bits. -/ | |
| def Within (i : Inputs X J) (y : Y) (S : β) : Prop := | |
| β w : M.Window, w.bits β€ S β§ β t, Runs (M.step i) w.ok M.initial t y | |
| theorem Within.mono {M : TM X J Y} {i y S R} (r : M.Within i y S) (h : S β€ R) : | |
| M.Within i y R := by | |
| obtain β¨w, hw, gβ© := r | |
| exact β¨w, hw.trans h, gβ© | |
| theorem Within.unique {M : TM X J Y} {i y S z R} (r : M.Within i y S) | |
| (g : M.Within i z R) : y = z := by | |
| obtain β¨_, -, _, rβ© := r | |
| obtain β¨_, -, _, gβ© := g | |
| exact congrArg Prod.snd (r.unique g) | |
| end TM | |
| end Fifths | |
| end OAI | |