math / data /lean /OAI /Computability /RationalHitting /FiniteCompiler.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
17.6 kB
import OAI.Computability.RationalHitting.StackSimulation
namespace OAI
namespace RationalHitting.StackLanguage.Compiler
open Turing RationalHitting.TMTime
open scoped BigOperators
variable {K : Type} [DecidableEq K]
noncomputable def stackConstant (p : Program K) : ℕ :=
1 + ∑ l ∈ support p, (StackToOne.stackWeight (machine p l)+1)
lemma stackConstant_bound (p : Program K) (l : Labels K) (hl : l∈support p) :
StackToOne.stackWeight (machine p l)+1 ≤ stackConstant p := by
have hh : StackToOne.stackWeight (machine p l)+1 ≤ ∑ j ∈ support p, (StackToOne.stackWeight (machine p j)+1) :=
Finset.single_le_sum (f:=fun j => StackToOne.stackWeight (machine p j)+1) (fun _ _ => Nat.zero_le _) hl
unfold stackConstant
omega
abbrev TapeAlphabet (K : Type) := TM2to1.Γ' K (fun _ => Bool)
abbrev TapeLabels (K : Type) := TM2to1.Λ' K (fun _ => Bool) (Labels K) (Option Bool)
noncomputable def tapeSupport (p : Program K) : Finset (TapeLabels K) :=
TM2to1.trSupp (machine p) (support p)
noncomputable def tapeConstant (p : Program K) : ℕ := by
classical
exact 1+∑ l ∈ tapeSupport p, OneToZero.weight (TM2to1.tr (machine p) l)
lemma tapeConstant_bound (p : Program K) (l : TapeLabels K) (hl : l∈tapeSupport p) :
OneToZero.weight (TM2to1.tr (machine p) l) ≤ tapeConstant p := by
classical
have hh : OneToZero.weight (TM2to1.tr (machine p) l) ≤ ∑ j ∈ tapeSupport p, OneToZero.weight (TM2to1.tr (machine p) j) :=
Finset.single_le_sum (f:=fun j => OneToZero.weight (TM2to1.tr (machine p) j)) (fun _ _ => Nat.zero_le _) hl
unfold tapeConstant
omega
lemma tapeSupported (p : Program K) :
TM1.Supports (TM2to1.tr (machine p)) (tapeSupport p) :=
TM2to1.tr_supports (machine p) (machine_supported p)
/-- A single finite-state TM0, independent of the input word, with
quadratic cost in stack execution time plus input length. The output
is the encoded family of stacks. -/
theorem finite_compilation (p : Program K) :
∃ (m : ℕ) (G : TM0.Machine (TapeAlphabet K) (Fin (m+1))) (C : ℕ), 0<C ∧
∀ (k : K) (w : List Bool) (v : Store K) (t : ℕ),
Eval p (Function.update (fun _ => []) k w) v t →
∃ u ≤ C*(w.length+t+3)^2, ∃ a,
exec (TM0.step G) u (TM0.init (TM2to1.trInit k w))=some a ∧
TM0.step G a=none ∧
∃ b : TM1.Cfg (TapeAlphabet K) (TapeLabels K) (Option Bool),
TM2to1.TrCfg (⟨none,none,v⟩ : TM2.Cfg (fun _ : K => Bool) (Labels K) (Option Bool)) b ∧
a.Tape=b.Tape := by
classical
let M := TM2to1.tr (machine p)
let S := TM1to0.trStmts M (tapeSupport p)
have hs : TM0.Supports (TM1to0.tr M) (S : Set (TM1to0.Λ' M)) :=
TM1to0.tr_supports M (tapeSupported p)
obtain ⟨m,G,hG⟩ := FiniteStates.finite_machine (TM1to0.tr M) S hs
let A := stackConstant p
let B := tapeConstant p
have hA : 1≤A := by simp [A,stackConstant]
have hB : 1≤B := by simp [B,tapeConstant]
refine ⟨m,G,10*B*(A+1)^2,by positivity,?_⟩
intro k w v t he
have hr := complete_run he
let a₂ : TM2.Cfg (fun _ : K => Bool) (Labels K) (Option Bool) := TM2.init k w
let b₂ : TM2.Cfg (fun _ : K => Bool) (Labels K) (Option Bool) := ⟨none,none,v⟩
let a₁ : TM1.Cfg (TapeAlphabet K) (TapeLabels K) (Option Bool) := TM1.init (TM2to1.trInit k w)
have hr' : exec (TM2.step (machine p)) (t+2) a₂=some b₂ := hr
have hlen : ∀ j, (a₂.stk j).length ≤ w.length := by
intro j
simp only [a₂,TM2.init]
by_cases hj : j=k <;> simp [hj]
obtain ⟨u,b,hu,htr,hx⟩ := StackToOne.execution_time (machine p) (support p)
(machine_supported p) A (stackConstant_bound p) (t+2) a₂ b₂ a₁ w.length
(Finset.some_mem_insertNone.mpr (machine_supported p).1) hlen (TM2to1.trCfg_init (Γ:=fun _ : K => Bool) (Λ:=Labels K) (σ:=Option Bool) k w) hr'
have hhal : TM1.step M b=none := StackToOne.halts (machine p) b₂ b htr rfl
obtain ⟨u',hu',hx'⟩ := OneToZero.execution_time M (tapeSupport p) (tapeSupported p)
B (tapeConstant_bound p) u a₁ b
(Finset.some_mem_insertNone.mpr (tapeSupported p).1) hx
have hinit : TM1to0.trCfg M a₁=TM0.init (TM2to1.trInit k w) := rfl
rw [hinit] at hx'
obtain ⟨a,ha,hh,htape⟩ := hG (TM2to1.trInit k w) u' (TM1to0.trCfg M b) hx'
(OneToZero.halts M b hhal)
refine ⟨u',?_,a,ha,hh,b,htr,htape⟩
have hx₁ : t+2≤w.length+t+3 := by omega
have hx₂ : w.length+A*(t+2)≤(A+1)*(w.length+t+3) := by nlinarith only [Nat.zero_le (A*w.length)]
have hx₃ : 2*(w.length+A*(t+2))+4≤4*(A+1)*(w.length+t+3) := by nlinarith only [hx₂,hA,Nat.zero_le w.length,Nat.zero_le t]
calc u' ≤ B*u := hu'
_ ≤ B*(A*(t+2)*(2*(w.length+A*(t+2))+4)) := Nat.mul_le_mul_left _ hu
_ ≤ B*(A*(w.length+t+3)*(4*(A+1)*(w.length+t+3))) :=
Nat.mul_le_mul_left _ (Nat.mul_le_mul (Nat.mul_le_mul_left _ hx₁) hx₃)
_ ≤ 10*B*(A+1)^2*(w.length+t+3)^2 := by nlinarith [Nat.zero_le (B*(w.length+t+3)^2)]
end RationalHitting.StackLanguage.Compiler
namespace RationalHitting.TMTime.Alphabet
open Turing
open TM1 TM1to1
variable {Γ Λ σ : Type*} [Inhabited Γ]
variable {n : ℕ} (enc : Γ → List.Vector Bool n) (dec : List.Vector Bool n → Γ)
variable (enc0 : enc default = List.Vector.replicate n false)
variable (encdec : ∀ a, dec (enc a) = a)
/-- Number of deferred writes along the longest branch. -/
def writesWeight : TM1.Stmt Γ Λ σ → ℕ
| .move _ q | .load _ q => writesWeight q
| .write _ q => writesWeight q+1
| .branch _ q r => max (writesWeight q) (writesWeight r)
| .goto _ | .halt => 0
include encdec
/-- Alphabet coding adds at most one macrostep per deferred write. -/
theorem continuation_time (M : Λ → TM1.Stmt Γ Λ σ) (q : TM1.Stmt Γ Λ σ)
(v : σ) (L R : ListBlank Γ) :
∃ t ≤ writesWeight q,
exec (TM1.step (TM1to1.tr enc dec M)) t
(TM1.stepAux (TM1to1.trNormal dec q) v (TM1to1.trTape' enc0 L R)) =
some (TM1to1.trCfg enc enc0 (TM1.stepAux q v (Tape.mk' L R))) := by
induction q generalizing v L R with
| move d q ih =>
cases d <;>
simp only [trNormal,stepAux_move,stepAux,Tape.move_left_mk',Tape.move_right_mk',
trTape'_move_left enc0,trTape'_move_right enc0,writesWeight] <;> apply ih
| write f q ih =>
simp only [trNormal,stepAux_read dec enc0 encdec,stepAux]
obtain ⟨a,R,rfl⟩ := R.exists_cons
obtain ⟨t,ht,he⟩ := ih v L (R.cons (f a v))
refine ⟨t+1,by simpa [writesWeight] using Nat.add_le_add_right ht 1,?_⟩
rw [exec]
simp only [TM1.step,Option.bind_some,TM1to1.tr,Tape.mk'_head,ListBlank.head_cons,
stepAux_write,stepAux_move,trTape'_move_left enc0,ListBlank.head_cons,
ListBlank.tail_cons]
simpa only [Tape.write_mk',trTape_mk',ListBlank.cons_head_tail,ListBlank.tail_cons] using he
| load f q ih =>
simp only [trNormal,stepAux_read dec enc0 encdec,stepAux,Tape.mk'_head,writesWeight]
apply ih
| branch p q r ihq ihr =>
simp only [trNormal,stepAux_read dec enc0 encdec,stepAux,Tape.mk'_head]
cases h : p R.head v with
| false =>
simp only [Bool.cond_false]
obtain ⟨t,ht,he⟩ := ihr v L R
exact ⟨t,ht.trans (Nat.le_max_right _ _),he⟩
| true =>
simp only [Bool.cond_true]
obtain ⟨t,ht,he⟩ := ihq v L R
exact ⟨t,ht.trans (Nat.le_max_left _ _),he⟩
| goto f =>
refine ⟨0,by simp [writesWeight],?_⟩
simp only [exec,trNormal,stepAux_read dec enc0 encdec,stepAux,trCfg,trTape_mk',Option.map_some,Tape.mk'_head]
| halt =>
refine ⟨0,by simp [writesWeight],?_⟩
simp only [exec,trNormal,stepAux,trCfg,trTape_mk',Option.map_none]
/-- Linear macrostep time overhead for the finite-alphabet compiler. Physical
moves and reads are subsequently counted by the quantitative TM1->TM0 compiler. -/
theorem execution_time [Inhabited Λ] (M : Λ → TM1.Stmt Γ Λ σ) (S : Finset Λ)
(hs : TM1.Supports M S) (C : ℕ) (hC : ∀ l ∈ S, writesWeight (M l)+1 ≤ C)
(t : ℕ) (a b : TM1.Cfg Γ Λ σ) (ha : a.l ∈ Finset.insertNone S)
(hr : exec (TM1.step M) t a = some b) :
∃ u ≤ C*t, exec (TM1.step (TM1to1.tr enc dec M)) u
(TM1to1.trCfg enc enc0 a) = some (TM1to1.trCfg enc enc0 b) := by
induction t generalizing a with
| zero =>
simp only [exec,Option.some.injEq] at hr
subst b
exact ⟨0,by omega,rfl⟩
| succ t ih =>
obtain ⟨l,v,T⟩ := a
cases l with
| none => simp [exec,TM1.step] at hr
| some l =>
obtain ⟨L,R,rfl⟩ := T.exists_mk'
have hl : l ∈ S := Finset.some_mem_insertNone.mp ha
have hn : (TM1.stepAux (M l) v (Tape.mk' L R)).l ∈ Finset.insertNone S :=
TM1.step_supports M hs (by rfl) ha
change exec (TM1.step M) t (TM1.stepAux (M l) v (Tape.mk' L R)) = some b at hr
obtain ⟨u,hu,he⟩ := ih _ hn hr
obtain ⟨w,hw,hwx⟩ := continuation_time enc dec enc0 encdec M (M l) v L R
have hx : exec (TM1.step (TM1to1.tr enc dec M)) (w+1)
(TM1to1.trCfg enc enc0 ⟨some l,v,Tape.mk' L R⟩) =
some (TM1to1.trCfg enc enc0 (TM1.stepAux (M l) v (Tape.mk' L R))) := by
simpa only [exec,TM1.step,trCfg,Option.map_some,Option.bind_some,tr,trTape_mk'] using hwx
refine ⟨(w+1)+u,?_,exec_trans hx he⟩
have hb := hC l hl
nlinarith
omit encdec in
theorem halts.{v_1, v_2, v_3} {Γ : Type v_1} {Λ : Type v_2} {σ : Type v_3}
[Inhabited Γ] {n : ℕ} (enc : Γ → List.Vector Bool n) (dec : List.Vector Bool n → Γ)
(enc0 : enc default = List.Vector.replicate n false) (_encdec : ∀ (a : Γ), dec (enc a) = a)
(M : Λ → Turing.TM1.Stmt Γ Λ σ) (a : Turing.TM1.Cfg Γ Λ σ) (ha : Turing.TM1.step M a = none) :
Turing.TM1.step (Turing.TM1to1.tr enc dec M) (Turing.TM1to1.trCfg enc enc0 a) = none := by
rcases a with ⟨l,v,T⟩
cases l <;> simp_all [TM1.step,trCfg]
end RationalHitting.TMTime.Alphabet
namespace RationalHitting.TMTime.Alphabet
open Turing
lemma compiled_init {Γ Λ σ : Type*} [Inhabited Γ] [Inhabited Λ] [Inhabited σ]
{b : ℕ} (enc : Γ → List.Vector Bool b) (h0 : enc default=List.Vector.replicate b false)
(w : List Γ) :
TM1to1.trCfg enc h0 (TM1.init w : TM1.Cfg Γ Λ σ)=
TM1.init (w.flatMap (fun a => (enc a).toList)) := by
simp [TM1to1.trCfg,TM1.init,Tape.mk₁,Tape.mk₂,TM1to1.trTape_mk',TM1to1.trTape']
rfl
end RationalHitting.TMTime.Alphabet
namespace RationalHitting.StackLanguage.Compiler
open Turing RationalHitting.TMTime
open scoped BigOperators
variable {K : Type} [DecidableEq K] [Fintype K]
/- The alphabet is finite because the number of stacks is fixed. -/
noncomputable instance finiteTapeAlphabet : Fintype (TapeAlphabet K) := by
unfold TapeAlphabet TM2to1.Γ'
infer_instance
/-- Polynomial-time compilation to a binary finite-state TM0,
preserving an explicit fixed block encoding. -/
theorem binary_compilation (p : Program K) :
∃ (b : ℕ) (enc : TapeAlphabet K → List.Vector Bool b)
(dec : List.Vector Bool b → TapeAlphabet K)
(h0 : enc default=List.Vector.replicate b false),
(∀ a,dec (enc a)=a) ∧
∃ (m : ℕ) (G : TM0.Machine Bool (Fin (m+1))) (C : ℕ), 0<C ∧
∀ (k : K) (w : List Bool) (v : Store K) (t : ℕ),
Eval p (Function.update (fun _ => []) k w) v t →
∃ u ≤ C*(w.length+t+3)^2, ∃ a,
exec (TM0.step G) u (TM0.init ((TM2to1.trInit k w).flatMap (fun z => (enc z).toList)))=some a ∧
TM0.step G a=none ∧
∃ c : TM1.Cfg (TapeAlphabet K) (TapeLabels K) (Option Bool),
TM2to1.TrCfg (⟨none,none,v⟩ : TM2.Cfg (fun _ : K => Bool) (Labels K) (Option Bool)) c ∧
a.Tape=TM1to1.trTape h0 c.Tape := by
classical
obtain ⟨b,enc,dec,h0,hd⟩ := TM1to1.exists_enc_dec (Γ:=TapeAlphabet K)
let M := TM2to1.tr (machine p)
let F := tapeSupport p
have hF : TM1.Supports M F := tapeSupported p
let N := TM1to1.tr enc dec M
let H := TM1to1.trSupp M F
have hH : TM1.Supports N H := TM1to1.tr_supports enc dec M hF
let A := stackConstant p
let B := 1+∑ l ∈ F, (Alphabet.writesWeight (M l)+1)
let D := 1+∑ l ∈ H, OneToZero.weight (N l)
have hA : 1≤A := by simp [A,stackConstant]
have hB : 1≤B := by simp [B]
have hD : 1≤D := by simp [D]
have hbnd : ∀ l ∈ F, Alphabet.writesWeight (M l)+1≤B := by
intro l hl
have hh := Finset.single_le_sum (f:=fun j => Alphabet.writesWeight (M j)+1)
(fun _ _ => Nat.zero_le _) hl
exact hh.trans (Nat.le_add_left _ _)
have hdnd : ∀ l ∈ H, OneToZero.weight (N l)≤D := by
intro l hl
have hh := Finset.single_le_sum (f:=fun j => OneToZero.weight (N j))
(fun _ _ => Nat.zero_le _) hl
exact hh.trans (Nat.le_add_left _ _)
let S := TM1to0.trStmts N H
have hs : TM0.Supports (TM1to0.tr N) (S : Set (TM1to0.Λ' N)) := TM1to0.tr_supports N hH
obtain ⟨m,G,hG⟩ := FiniteStates.finite_machine (TM1to0.tr N) S hs
refine ⟨b,enc,dec,h0,hd,m,G,10*D*B*(A+1)^2,by positivity,?_⟩
intro k w v t he
let a₂ : TM2.Cfg (fun _ : K => Bool) (Labels K) (Option Bool) := TM2.init k w
let b₂ : TM2.Cfg (fun _ : K => Bool) (Labels K) (Option Bool) := ⟨none,none,v⟩
let a₁ : TM1.Cfg (TapeAlphabet K) (TapeLabels K) (Option Bool) := TM1.init (TM2to1.trInit k w)
have hr : exec (TM2.step (machine p)) (t+2) a₂=some b₂ := complete_run he
have hlen : ∀ j, (a₂.stk j).length≤w.length := by
intro j
simp only [a₂,TM2.init]
by_cases hj : j=k <;> simp [hj]
obtain ⟨u,c,hu,htr,hx⟩ := StackToOne.execution_time (machine p) (support p)
(machine_supported p) A (stackConstant_bound p) (t+2) a₂ b₂ a₁ w.length
(Finset.some_mem_insertNone.mpr (machine_supported p).1) hlen
(TM2to1.trCfg_init (Γ:=fun _ : K => Bool) (Λ:=Labels K) (σ:=Option Bool) k w) hr
have hhal : TM1.step M c=none := StackToOne.halts (machine p) b₂ c htr rfl
obtain ⟨u',hu',hx'⟩ := Alphabet.execution_time enc dec h0 hd M F hF B hbnd u a₁ c
(Finset.some_mem_insertNone.mpr hF.1) hx
have hi : TM1to1.trCfg enc h0 a₁ =
TM1.init ((TM2to1.trInit k w).flatMap (fun z => (enc z).toList)) :=
Alphabet.compiled_init enc h0 _
rw [hi] at hx'
have hh' := Alphabet.halts enc dec h0 hd M c hhal
obtain ⟨u'',hu'',hx''⟩ := OneToZero.execution_time N H hH D hdnd u'
(TM1.init ((TM2to1.trInit k w).flatMap (fun z => (enc z).toList)))
(TM1to1.trCfg enc h0 c) (Finset.some_mem_insertNone.mpr hH.1) hx'
have hz : TM1to0.trCfg N (TM1.init ((TM2to1.trInit k w).flatMap (fun z => (enc z).toList)))=
TM0.init ((TM2to1.trInit k w).flatMap (fun z => (enc z).toList)) := rfl
rw [hz] at hx''
obtain ⟨a,ha,hh,htape⟩ := hG _ u'' _ hx'' (OneToZero.halts N _ hh')
refine ⟨u'',?_,a,ha,hh,c,htr,htape⟩
have hx₁ : t+2≤w.length+t+3 := by omega
have hx₂ : w.length+A*(t+2)≤(A+1)*(w.length+t+3) := by nlinarith only [Nat.zero_le (A*w.length)]
have hx₃ : 2*(w.length+A*(t+2))+4≤4*(A+1)*(w.length+t+3) := by nlinarith only [hx₂,hA,Nat.zero_le w.length,Nat.zero_le t]
calc u'' ≤ D*u' := hu''
_ ≤ D*(B*u) := Nat.mul_le_mul_left _ hu'
_ ≤ D*(B*(A*(t+2)*(2*(w.length+A*(t+2))+4))) := Nat.mul_le_mul_left _ (Nat.mul_le_mul_left _ hu)
_ ≤ D*(B*(A*(w.length+t+3)*(4*(A+1)*(w.length+t+3)))) :=
Nat.mul_le_mul_left _ (Nat.mul_le_mul_left _ (Nat.mul_le_mul (Nat.mul_le_mul_left _ hx₁) hx₃))
_ ≤ 10*D*B*(A+1)^2*(w.length+t+3)^2 := by nlinarith [Nat.zero_le (D*B*(w.length+t+3)^2)]
end RationalHitting.StackLanguage.Compiler
namespace RationalHitting.TMTime.OneToZero
open Turing
open scoped BigOperators
lemma supports_univ {Γ Λ σ : Type*} [Fintype Λ] (q : TM1.Stmt Γ Λ σ) :
TM1.SupportsStmt Finset.univ q := by
induction q <;> simp_all [TM1.SupportsStmt]
/-- Every fixed finite-control TM1 has an ordinary finite-state TM0 compiling
all runs at a fixed linear cost and preserving the literal tape. -/
theorem finite_compiler {Γ Λ σ : Type*} [Inhabited Γ] [Inhabited Λ] [Inhabited σ]
[Fintype Λ] [Fintype σ] (M : Λ → TM1.Stmt Γ Λ σ) :
∃ (m : ℕ) (G : TM0.Machine Γ (Fin (m+1))) (C : ℕ), 0<C ∧
∀ (w : List Γ) (t : ℕ) (a : TM1.Cfg Γ Λ σ),
exec (TM1.step M) t (TM1.init w)=some a → TM1.step M a=none →
∃ u≤C*t, ∃ b : TM0.Cfg Γ (Fin (m+1)),
exec (TM0.step G) u (TM0.init w)=some b ∧ TM0.step G b=none ∧ b.Tape=a.Tape := by
classical
let F : Finset Λ := Finset.univ
have hs : TM1.Supports M F := ⟨Finset.mem_univ _,fun q _ => supports_univ (M q)⟩
let S := TM1to0.trStmts M F
have hs' : TM0.Supports (TM1to0.tr M) (S : Set (TM1to0.Λ' M)) := TM1to0.tr_supports M hs
obtain ⟨m,G,hG⟩ := FiniteStates.finite_machine (TM1to0.tr M) S hs'
let C := 1+∑ q∈F, weight (M q)
have hC : ∀ q∈F, weight (M q)≤C := by
intro q hq
exact (Finset.single_le_sum (f:=fun q => weight (M q)) (fun _ _ => Nat.zero_le _) hq).trans (Nat.le_add_left _ _)
refine ⟨m,G,C,by simp [C],?_⟩
intro w t a hr hh
obtain ⟨u,hu,hx⟩ := execution_time M F hs C hC t (TM1.init w) a
(Finset.some_mem_insertNone.mpr hs.1) hr
have hi : TM1to0.trCfg M (TM1.init w)=TM0.init w := rfl
rw [hi] at hx
obtain ⟨b,hb,hhb,ht⟩ := hG w u _ hx (halts M a hh)
exact ⟨u,hu,b,hb,hhb,ht⟩
end RationalHitting.TMTime.OneToZero
/-! Raw unary input adapter for the finite block-coded stack compiler.
The alphabet is exactly Fin 4. No list-processing oracle is an instruction. -/
end OAI