openAI-math-dataset / lean /OAI /Computability /DirectedFeedback /Machines /MachineBinaryTotalInputMachine.lean
roseking's picture
Add lean source and binary files (part 61)
ea76aac verified
Raw History Blame Contribute Delete
37.6 kB
import Mathlib
import OAI.Computability.DirectedFeedback.RankGraph.MachineClone100Finish
namespace OAI
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
namespace DFVSGames.BinaryTotalInputMachine
open Turing
open DFVSGames.Foundations.Complexity
open MachineComposition
open DFVSGames.Reduction.MachineTransfer
open DFVSGames.Reduction.MachineSubstitution
open BinaryValidatorMachine (Mode nextMode run)
inductive Label
| scan
| restore
| drain
| rejectOutput
deriving DecidableEq
instance : Fintype Label := derive_fintype% Label
abbrev Alphabet (_ : Fin 3) := Bool
abbrev State := Mode × Option Bool
def rejectedWord : List Bool := BinaryEncoding.formulaBits BinaryLanguage.rejectBinary
def instruction : Label → TM2.Stmt Alphabet Label State
| .scan =>
.pop 0 (fun state head => (state.1, head))
(.branch (fun state => state.2.isSome)
(.push 2 (fun state => state.2.getD false)
(.load (fun state => (nextMode state.1 (state.2.getD false), none))
(.goto fun _ => .scan)))
(.branch (fun state => decide (state.1 = .done))
(.load (fun _ => (.clause, none)) (.goto fun _ => .restore))
(.load (fun _ => (.clause, none)) (.goto fun _ => .drain))))
| .restore => loopAt 2 1 id false .restore none
| .drain => MachineDrain.drain 2 .drain (some .rejectOutput)
| .rejectOutput => pushWord 1 rejectedWord.reverse .halt
abbrev machine : FinTM2 where
K := Fin 3
k₀ := 0
k₁ := 1
Γ := Alphabet
Λ := Label
main := .scan
σ := State
initialState := (.clause, none)
m := instruction
def tapes (input output saved : List Bool) : Fin 3 → List Bool
| 0 => input
| 1 => output
| _ => saved
def cfg (label : Option Label) (input output saved : List Bool) (mode : Mode := .clause)
(register : Option Bool := none) : machine.Cfg :=
⟨label, (mode, register), tapes input output saved⟩
@[simp] theorem tapes_input (input output saved : List Bool) :
tapes input output saved 0 = input := rfl
@[simp] theorem tapes_output (input output saved : List Bool) :
tapes input output saved 1 = output := rfl
@[simp] theorem tapes_saved (input output saved : List Bool) :
tapes input output saved 2 = saved := rfl
private theorem update_input_inline_MachineBinaryTotalInputMachine (input output saved replacement : List Bool) :
Function.update (tapes input output saved) 0 replacement =
tapes replacement output saved := by
funext k
fin_cases k <;> rfl
private theorem update_output_inline_MachineBinaryTotalInputMachine (input output saved replacement : List Bool) :
Function.update (tapes input output saved) 1 replacement =
tapes input replacement saved := by
funext k
fin_cases k <;> rfl
private theorem update_saved_inline_MachineBinaryTotalInputMachine (input output saved replacement : List Bool) :
Function.update (tapes input output saved) 2 replacement =
tapes input output replacement := by
funext k
fin_cases k <;> rfl
def afterScan (mode : Mode) : Label := if mode = .done then .restore else .drain
theorem step_scan_empty (output saved : List Bool) (mode : Mode) (register : Option Bool) :
machine.step (cfg (some .scan) [] output saved mode register) =
some (cfg (some (afterScan mode)) [] output saved) := by
change some (TM2.stepAux (instruction .scan) _ _) = _
by_cases accepted : mode = .done <;>
simp [instruction, cfg, TM2.stepAux, update_input_inline_MachineBinaryTotalInputMachine, afterScan, accepted]
all_goals rfl
theorem step_scan_cons (bit : Bool) (input output saved : List Bool)
(mode : Mode) (register : Option Bool) :
machine.step (cfg (some .scan) (bit :: input) output saved mode register) =
some (cfg (some .scan) input output (bit :: saved) (nextMode mode bit)) := by
change some (TM2.stepAux (instruction .scan) _ _) = _
simp [instruction, cfg, TM2.stepAux, update_input_inline_MachineBinaryTotalInputMachine, update_saved_inline_MachineBinaryTotalInputMachine]
rfl
theorem scanTrace (input output saved : List Bool) (mode : Mode) (register : Option Bool) :
(advance machine.step)^[input.length + 1]
(some (cfg (some .scan) input output saved mode register)) =
some (cfg (some (afterScan (run mode input))) [] output (input.reverse ++ saved)) := by
induction input generalizing saved mode register with
| nil =>
simpa only [List.length_nil, Nat.zero_add, Function.iterate_one,
advance_some, run, List.reverse_nil, List.nil_append] using
step_scan_empty output saved mode register
| cons bit input ih =>
rw [List.length_cons, Function.iterate_succ_apply]
simp only [advance_some]
rw [step_scan_cons]
simpa only [run, List.reverse_cons, List.append_assoc, List.singleton_append] using
ih (bit :: saved) (nextMode mode bit) none
theorem restoreTrace (saved output : List Bool) (register : Option Bool) :
(advance machine.step)^[saved.length + 1]
(some (cfg (some .restore) [] output saved .clause register)) =
some (cfg none [] (saved.reverse ++ output) []) := by
change (nextAt (1 : Fin 3) instruction)^[saved.length + 1]
(some ⟨some .restore, (Mode.clause, register), tapes [] output saved⟩) =
some ⟨none, (Mode.clause, none), tapes [] (saved.reverse ++ output) []⟩
have trace := transferAt_steps (2 : Fin 3) 1 (by decide) id false .restore none
instruction rfl (tapes [] [] []) saved output Mode.clause register
simpa only [tapesAt, update_saved_inline_MachineBinaryTotalInputMachine, update_output_inline_MachineBinaryTotalInputMachine,
List.map_id_fun, id_eq] using trace
theorem drainTrace (saved output : List Bool) (register : Option Bool) :
(advance machine.step)^[saved.length + 1]
(some (cfg (some .drain) [] output saved .clause register)) =
some (cfg (some .rejectOutput) [] output []) := by
change (advance (TM2.step instruction))^[saved.length + 1]
(some ⟨some .drain, (Mode.clause, register), tapes [] output saved⟩) =
some ⟨some .rejectOutput, (Mode.clause, none), tapes [] output []⟩
have trace := MachineDrain.drainTrace (2 : Fin 3) .drain (some .rejectOutput)
instruction rfl (tapes [] output []) saved Mode.clause register
simpa only [update_saved_inline_MachineBinaryTotalInputMachine] using trace
theorem step_rejectOutput (output : List Bool) :
machine.step (cfg (some .rejectOutput) [] output []) =
some (cfg none [] (rejectedWord ++ output) []) := by
change some (TM2.stepAux
(pushWord (Γ := Alphabet) (Λ := Label) (σ := State)
(1 : Fin 3) rejectedWord.reverse .halt) _ _) = _
rw [stepAux_pushWord]
simp only [cfg, List.reverse_reverse, tapes_output, update_output_inline_MachineBinaryTotalInputMachine, TM2.stepAux]
rfl
private theorem joinTrace_inline_MachineBinaryTotalInputMachine {X : Type*} {f : X → X} {a b c : X} {n m : Nat}
(first : f^[n] a = b) (second : f^[m] b = c) : f^[n + m] a = c := by
rw [Nat.add_comm, Function.iterate_add_apply, first, second]
theorem acceptedTrace (input : List Bool) (accepted : run .clause input = .done) :
(advance machine.step)^[2 * input.length + 2]
(some (cfg (some .scan) input [] [])) = some (cfg none [] input []) := by
have first := scanTrace input [] [] .clause none
simp only [afterScan, accepted, ↓reduceIte, List.append_nil] at first
have second := restoreTrace input.reverse [] none
simp only [List.length_reverse, List.reverse_reverse, List.append_nil] at second
have time : 2 * input.length + 2 = (input.length + 1) + (input.length + 1) := by omega
rw [time]
exact joinTrace_inline_MachineBinaryTotalInputMachine first second
theorem rejectedTrace (input : List Bool) (rejected : run .clause input ≠ .done) :
(advance machine.step)^[2 * input.length + 3]
(some (cfg (some .scan) input [] [])) = some (cfg none [] rejectedWord []) := by
have first := scanTrace input [] [] .clause none
simp only [afterScan, rejected, ↓reduceIte, List.append_nil] at first
have second := drainTrace input.reverse [] none
simp only [List.length_reverse] at second
have third : (advance machine.step)^[1]
(some (cfg (some .rejectOutput) [] [] [])) = some (cfg none [] rejectedWord []) := by
simpa only [Function.iterate_one, advance_some, List.append_nil] using
step_rejectOutput []
have time : 2 * input.length + 3 = (input.length + 1) + (input.length + 1) + 1 := by omega
rw [time]
exact joinTrace_inline_MachineBinaryTotalInputMachine (joinTrace_inline_MachineBinaryTotalInputMachine first second) third
theorem initList_eq (input : List Bool) :
initList machine input = cfg (some .scan) input [] [] := by
unfold initList cfg
congr 1
funext k
fin_cases k <;> rfl
theorem haltList_eq (output : List Bool) :
haltList machine output = cfg none [] output [] := by
unfold haltList cfg
congr 1
funext k
fin_cases k <;> rfl
def steps (input : List Bool) : Nat :=
if run .clause input = .done then 2 * input.length + 2 else 2 * input.length + 3
theorem steps_le (input : List Bool) : steps input ≤ 2 * input.length + 3 := by
unfold steps
split <;> omega
theorem totalTrace (input : List Bool) :
(advance machine.step)^[steps input] (some (initList machine input)) =
some (haltList machine (BinaryEncoding.formulaBits (BinaryLanguage.totalParsed input))) := by
rw [initList_eq, haltList_eq]
cases parsed : BinaryEncoding.decodeFormula input with
| none =>
have rejected : run .clause input ≠ .done := by
intro accepted
obtain ⟨formula, encoded⟩ := (BinaryValidatorMachine.run_clause_iff input).mp accepted
have roundtrip := BinaryEncoding.decodeFormula_encoded formula
rw [← encoded, parsed] at roundtrip
cases roundtrip
simpa only [steps, rejected, ↓reduceIte, BinaryLanguage.totalParsed, parsed,
Option.getD_none, rejectedWord] using rejectedTrace input rejected
| some formula =>
have encoded := BinaryParsing.decodeFormula_sound input formula parsed
have accepted := (BinaryValidatorMachine.run_clause_iff input).mpr ⟨formula, encoded⟩
simp only [steps, accepted, ↓reduceIte, BinaryLanguage.totalParsed, parsed,
Option.getD_some]
rw [← encoded]
exact acceptedTrace input accepted
def outputsInTime (input : List Bool) :
TM2OutputsInTime machine input
(some (BinaryEncoding.formulaBits (BinaryLanguage.totalParsed input)))
(2 * input.length + 3) where
steps := steps input
evals_in_steps := totalTrace input
steps_le_m := steps_le input
noncomputable def computableInPolyTime :
TM2ComputableInPolyTime (id : List Bool → List Bool) BinaryEncoding.formulaBits
BinaryLanguage.totalParsed where
tm := machine
inputAlphabet := Equiv.refl Bool
outputAlphabet := Equiv.refl Bool
time := 2 * Polynomial.X + 3
outputsFun input := by
change TM2OutputsInTime machine (input.map id)
(some ((BinaryEncoding.formulaBits (BinaryLanguage.totalParsed input)).map id))
((2 * Polynomial.X + 3 : Polynomial Nat).eval input.length)
simpa only [List.map_id_fun, id_eq, Polynomial.eval_add, Polynomial.eval_mul,
Polynomial.eval_X, Polynomial.eval_ofNat] using outputsInTime input
theorem machine_finiteAlphabet (k : machine.K) : Finite (machine.Γ k) := by
change Finite Bool
infer_instance
theorem computation_finiteAlphabet : MachineFiniteAlphabet.FiniteAlphabet computableInPolyTime.tm :=
machine_finiteAlphabet
end DFVSGames.BinaryTotalInputMachine
end
section
namespace DFVSGames.BinaryNameCompare
open Turing
open DFVSGames.Foundations.Complexity
open MachineComposition
variable {K Λ A : Type} [DecidableEq K]
abbrev Alphabet (_ : K) := Bool
abbrev State (A : Type) := (A × Bool × Option Bool) × Option Bool
def clean (ambient : A) : State A := ((ambient, false, none), none)
def scanStateEquiv (A : Type) : BinaryNameMachine.State (A × Bool) ≃ State A where
toFun s := ((s.1.1, s.1.2, s.2.1), s.2.2)
invFun s := ((s.1.1, s.1.2.1), s.1.2.2, s.2)
left_inv := by rintro ⟨⟨a, b⟩, c, d⟩; rfl
right_inv := by rintro ⟨⟨a, b, c⟩, d⟩; rfl
def compareStateEquiv (A : Type) : MachineCompare.State A ≃ State A where
toFun s := ((s.1, s.2.1, s.2.2.1), s.2.2.2)
invFun s := (s.1.1, s.1.2.1, s.1.2.2, s.2)
left_inv := by rintro ⟨a, b, c, d⟩; rfl
right_inv := by rintro ⟨⟨a, b, c⟩, d⟩; rfl
inductive Label
| scan | copyOut | copyBack | compare
deriving DecidableEq
instance : Fintype Label := derive_fintype% Label
def instruction (tape : Fin 5 → K) (labels : Label → Λ)
(equalExit differentExit malformedExit : Option Λ) :
Label → TM2.Stmt (Alphabet (K := K)) Λ (State A)
| .scan => MachineStateEquiv.statement (scanStateEquiv A)
(BinaryNameMachine.scan (tape 0) (tape 2) (labels .scan)
(some (labels .copyOut)) malformedExit)
| .copyOut => DFVSGames.Reduction.MachineTransfer.loopAt (tape 1) (tape 4) id false
(labels .copyOut) (some (labels .copyBack))
| .copyBack => MachineCopy.forkLoop (tape 4) (tape 1) (tape 3) false
(labels .copyBack) (some (labels .compare))
| .compare => MachineStateEquiv.statement (compareStateEquiv A)
(MachineCompare.loop (tape 2) (tape 3) (labels .compare) equalExit differentExit)
def steps (payloadLength keyLength : Nat) : Nat :=
(payloadLength + 1) + 2 * (keyLength + 1) + (max payloadLength keyLength + 1)
theorem steps_le (payloadLength keyLength : Nat) :
steps payloadLength keyLength ≤ 2 * payloadLength + 3 * keyLength + 4 := by
unfold steps
omega
private theorem joinTrace_inline_MachineBinaryNameCompare {X : Type*} {f : X → X} {a b c : X} {n m : Nat}
(first : f^[n] a = b) (second : f^[m] b = c) : f^[n + m] a = c := by
rw [Nat.add_comm, Function.iterate_add_apply, first, second]
theorem compareTrace (tape : Fin 5 → K) (distinct : Function.Injective tape)
(labels : Label → Λ) (equalExit differentExit malformedExit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State A))
(atLabels : ∀ l, program (labels l) =
instruction tape labels equalExit differentExit malformedExit l)
(base : K → List Bool) (bits suffix key : List Bool)
(canonical : BinaryNameMachine.canonical bits = true)
(sourceWord : base (tape 0) = BinaryNameMachine.frame bits ++ suffix)
(keyWord : base (tape 1) = key)
(candidateEmpty : base (tape 2) = []) (copyEmpty : base (tape 3) = [])
(scratchEmpty : base (tape 4) = []) (ambient : A) :
(advance (TM2.step program))^[steps bits.length key.length]
(some ⟨some (labels .scan), clean ambient, base⟩) =
some ⟨if bits.reverse = key then equalExit else differentExit,
clean ambient, Function.update base (tape 0) suffix⟩ := by
have hd (i j : Fin 5) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h)
let finalBase := Function.update base (tape 0) suffix
let afterScan := Function.update finalBase (tape 2) bits.reverse
let afterCopy := Function.update afterScan (tape 3) key
have scanRun : (advance (TM2.step program))^[bits.length + 1]
(some ⟨some (labels .scan), clean ambient, base⟩) =
some ⟨some (labels .copyOut), clean ambient, afterScan⟩ := by
let back := MachineStateEquiv.program (scanStateEquiv A).symm program
have atScan : back (labels .scan) =
BinaryNameMachine.scan (tape 0) (tape 2) (labels .scan)
(some (labels .copyOut)) malformedExit := by
change MachineStateEquiv.statement (scanStateEquiv A).symm
(program (labels .scan)) = _
rw [atLabels .scan]
exact MachineStateEquiv.statement_symm_statement (scanStateEquiv A) _
have backForward : MachineStateEquiv.program (scanStateEquiv A) back = program := by
funext l
change MachineStateEquiv.statement (scanStateEquiv A)
(MachineStateEquiv.statement (scanStateEquiv A).symm (program l)) = _
simpa only [Equiv.symm_symm] using
MachineStateEquiv.statement_symm_statement (scanStateEquiv A).symm (program l)
have initialTapes : DFVSGames.Reduction.MachineTransfer.tapesAt (tape 0) (tape 2)
base (BinaryNameMachine.frame bits ++ suffix) [] = base := by
rw [← sourceWord, ← candidateEmpty]
exact DFVSGames.Reduction.MachineTransfer.tapesAt_self _ _ _
have native := BinaryNameMachine.framedTrace (tape 0) (tape 2) (hd 0 2 (by decide))
(labels .scan) (some (labels .copyOut)) malformedExit back atScan base (ambient, false)
bits suffix [] none
simp only [canonical, ↓reduceIte, initialTapes, List.append_nil] at native
have transported := MachineStateEquiv.trace (scanStateEquiv A) back _ _ _ native
rw [backForward] at transported
simpa only [MachineStateEquiv.configuration, scanStateEquiv, Equiv.coe_fn_mk, BinaryNameMachine.clean,
clean, afterScan, finalBase, DFVSGames.Reduction.MachineTransfer.tapesAt] using transported
have afterScanKey : afterScan (tape 1) = key := by
simpa [afterScan, finalBase, hd 1 2 (by decide), hd 1 0 (by decide)] using keyWord
have afterScanCopy : afterScan (tape 3) = [] := by
simpa [afterScan, finalBase, hd 3 2 (by decide), hd 3 0 (by decide)] using copyEmpty
have afterScanScratch : afterScan (tape 4) = [] := by
simpa [afterScan, finalBase, hd 4 2 (by decide), hd 4 0 (by decide)] using scratchEmpty
have copyRun : (advance (TM2.step program))^[2 * (key.length + 1)]
(some ⟨some (labels .copyOut), clean ambient, afterScan⟩) =
some ⟨some (labels .compare), clean ambient, afterCopy⟩ := by
have h := MachineCopy.copyTrace (tape 1) (tape 3) (tape 4)
(hd 1 3 (by decide)) (hd 1 4 (by decide)) (hd 3 4 (by decide)) false
(labels .copyOut) (labels .copyBack) (some (labels .compare)) program
(atLabels .copyOut) (atLabels .copyBack) afterScan afterScanScratch
(ambient, false, none) none
simpa only [afterScanKey, afterScanCopy, List.append_nil, clean, afterCopy] using h
have candidateWord : afterCopy (tape 2) = bits.reverse := by
simp [afterCopy, afterScan, hd 2 3 (by decide)]
have copiedWord : afterCopy (tape 3) = key := by simp [afterCopy]
have restored : DFVSGames.Reduction.MachineTransfer.tapesAt (tape 2) (tape 3)
afterCopy [] [] = finalBase := by
funext k
by_cases hc : k = tape 2
· subst k
simp [DFVSGames.Reduction.MachineTransfer.tapesAt, afterCopy, afterScan, finalBase,
hd 2 3 (by decide), hd 2 0 (by decide), candidateEmpty]
· by_cases hk : k = tape 3
· subst k
simp [DFVSGames.Reduction.MachineTransfer.tapesAt, afterCopy, afterScan, finalBase,
hd 3 0 (by decide), copyEmpty]
· simp [DFVSGames.Reduction.MachineTransfer.tapesAt, afterCopy, afterScan, hc, hk]
have comparisonRun : (advance (TM2.step program))^[max bits.length key.length + 1]
(some ⟨some (labels .compare), clean ambient, afterCopy⟩) =
some ⟨if bits.reverse = key then equalExit else differentExit,
clean ambient, finalBase⟩ := by
let back := MachineStateEquiv.program (compareStateEquiv A).symm program
have atCompare : back (labels .compare) =
MachineCompare.loop (tape 2) (tape 3) (labels .compare) equalExit differentExit := by
change MachineStateEquiv.statement (compareStateEquiv A).symm
(program (labels .compare)) = _
rw [atLabels .compare]
exact MachineStateEquiv.statement_symm_statement (compareStateEquiv A) _
have backForward : MachineStateEquiv.program (compareStateEquiv A) back = program := by
funext l
change MachineStateEquiv.statement (compareStateEquiv A)
(MachineStateEquiv.statement (compareStateEquiv A).symm (program l)) = _
simpa only [Equiv.symm_symm] using
MachineStateEquiv.statement_symm_statement (compareStateEquiv A).symm (program l)
have native := MachineCompare.compareTrace_fromTapes (tape 2) (tape 3)
(hd 2 3 (by decide)) (labels .compare) equalExit differentExit back atCompare afterCopy
ambient none none
rw [candidateWord, copiedWord, restored, List.length_reverse] at native
have transported := MachineStateEquiv.trace (compareStateEquiv A) back _ _ _ native
rw [backForward] at transported
simpa only [MachineStateEquiv.configuration, compareStateEquiv, Equiv.coe_fn_mk, clean] using transported
exact joinTrace_inline_MachineBinaryNameCompare (joinTrace_inline_MachineBinaryNameCompare scanRun copyRun) comparisonRun
def compareInTime (tape : Fin 5 → K) (distinct : Function.Injective tape)
(labels : Label → Λ) (equalExit differentExit malformedExit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State A))
(atLabels : ∀ l, program (labels l) =
instruction tape labels equalExit differentExit malformedExit l)
(base : K → List Bool) (bits suffix key : List Bool)
(canonical : BinaryNameMachine.canonical bits = true)
(sourceWord : base (tape 0) = BinaryNameMachine.frame bits ++ suffix)
(keyWord : base (tape 1) = key)
(candidateEmpty : base (tape 2) = []) (copyEmpty : base (tape 3) = [])
(scratchEmpty : base (tape 4) = []) (ambient : A) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some (labels .scan), clean ambient, base⟩
(some ⟨if bits.reverse = key then equalExit else differentExit,
clean ambient, Function.update base (tape 0) suffix⟩)
(2 * bits.length + 3 * key.length + 4) where
steps := steps bits.length key.length
evals_in_steps := compareTrace tape distinct labels equalExit differentExit malformedExit
program atLabels base bits suffix key canonical sourceWord keyWord
candidateEmpty copyEmpty scratchEmpty ambient
steps_le_m := steps_le bits.length key.length
def machine : FinTM2 where
K := Fin 5
k₀ := 0
k₁ := 1
Γ _ := Bool
Λ := Label ⊕ Fin 3
main := .inl .scan
σ := State Unit
initialState := clean ()
m label := match label with
| .inl l => instruction id Sum.inl (some (.inr 0)) (some (.inr 1)) (some (.inr 2)) l
| .inr _ => .halt
theorem machine_finiteAlphabet (k : machine.K) : Finite (machine.Γ k) := by
change Finite Bool
infer_instance
end DFVSGames.BinaryNameCompare
end
section
namespace DFVSGames.BinaryNameSearch
open Turing
open DFVSGames.Foundations.Complexity
open MachineComposition
open DFVSGames.Reduction.MachineTransfer
abbrev Token := Bool × List Bool
def tokenBits (token : Token) : List Bool :=
token.1 :: BinaryNameMachine.frame token.2
def stream : List Token → List Bool
| [] => []
| token :: tokens => tokenBits token ++ stream tokens
def payloads (tokens : List Token) : List (List Bool) := tokens.map Prod.snd
def afterMatch : List Token → List Bool → List Token
| [], _ => []
| token :: tokens, key => if token.2 = key then tokens else afterMatch tokens key
def steps : List Token → List Bool → Nat
| [], _ => 0
| token :: tokens, key =>
1 + BinaryNameCompare.steps token.2.length key.length +
if token.2 = key then 0 else 1 + steps tokens key
def payloadSize (tokens : List Token) : Nat := (payloads tokens |>.map List.length).sum
@[simp] theorem stream_nil : stream [] = [] := rfl
@[simp] theorem stream_cons (token : Token) (tokens : List Token) :
stream (token :: tokens) = tokenBits token ++ stream tokens := rfl
@[simp] theorem payloads_nil : payloads [] = [] := rfl
@[simp] theorem payloads_cons (token : Token) (tokens : List Token) :
payloads (token :: tokens) = token.2 :: payloads tokens := rfl
@[simp] theorem payloadSize_nil : payloadSize [] = 0 := rfl
@[simp] theorem payloadSize_cons (token : Token) (tokens : List Token) :
payloadSize (token :: tokens) = token.2.length + payloadSize tokens := rfl
@[simp] theorem stream_length (tokens : List Token) :
(stream tokens).length = 2 * payloadSize tokens + 2 * tokens.length := by
induction tokens with
| nil => rfl
| cons token tokens ih =>
simp only [stream_cons, List.length_append, tokenBits, List.length_cons,
BinaryNameMachine.frame_length, ih, payloadSize_cons]
omega
theorem steps_le (tokens : List Token) (key : List Bool) :
steps tokens key ≤ 2 * payloadSize tokens + (3 * key.length + 6) * tokens.length := by
induction tokens with
| nil => simp [steps]
| cons token tokens ih =>
have h := BinaryNameCompare.steps_le token.2.length key.length
simp only [steps, payloadSize_cons, List.length_cons]
split <;> nlinarith
theorem steps_le_stream (tokens : List Token) (key : List Bool) :
steps tokens key ≤ (3 * key.length + 7) * (stream tokens).length := by
have h := steps_le tokens key
rw [stream_length]
nlinarith
section Program
variable {K Λ A : Type} [DecidableEq K]
abbrev Alphabet (_ : K) := Bool
abbrev State (A : Type) := BinaryNameCompare.State A
abbrev clean (ambient : A) : State A := BinaryNameCompare.clean ambient
inductive Label
| sign
| comparison (label : BinaryNameCompare.Label)
| increment
deriving DecidableEq, Fintype
def compareTapes (tape : Fin 6 → K) : Fin 5 → K := fun i => tape i.castSucc
omit [DecidableEq K] in
theorem compareTapes_injective (tape : Fin 6 → K) (distinct : Function.Injective tape) :
Function.Injective (compareTapes tape) := by
intro i j h
apply Fin.ext
exact congrArg (fun i : Fin 6 => i.val) (distinct h)
def exitAt (exit : Option Λ) : TM2.Stmt (Alphabet (K := K)) Λ (State A) :=
match exit with
| none => .halt
| some label => .goto fun _ => label
def finish (exit : Option Λ) : TM2.Stmt (Alphabet (K := K)) Λ (State A) :=
.load (fun state => clean state.1.1) (exitAt exit)
def instruction (tape : Fin 6 → K) (labels : Label → Λ)
(foundExit missingExit malformedExit : Option Λ) :
Label → TM2.Stmt (Alphabet (K := K)) Λ (State A)
| .sign =>
.pop (tape 0) (fun state head => (state.1, head))
(.branch (fun state => state.2.isSome)
(finish (some (labels (.comparison .scan)))) (finish missingExit))
| .comparison l => BinaryNameCompare.instruction (compareTapes tape)
(fun l => labels (.comparison l)) foundExit (some (labels .increment)) malformedExit l
| .increment => .push (tape 5) (fun _ => true) (.goto fun _ => labels .sign)
@[simp] theorem stepAux_finish (exit : Option Λ) (state : State A) (base : K → List Bool) :
TM2.stepAux (finish exit) state base = ⟨exit, clean state.1.1, base⟩ := by
cases exit <;> rfl
private theorem update_source_inline_MachineBinaryNameSearch (source counter : K) (distinct : source ≠ counter)
(base : K → List Bool) (input count replacement : List Bool) :
Function.update (tapesAt source counter base input count) source replacement =
tapesAt source counter base replacement count := by
funext k
by_cases hs : k = source
· subst k; simp [tapesAt, distinct]
· by_cases hc : k = counter
· subst k; simp [tapesAt, Ne.symm distinct]
· simp [tapesAt, hs, hc]
private theorem update_counter_inline_MachineBinaryNameSearch (source counter : K)
(base : K → List Bool) (input count replacement : List Bool) :
Function.update (tapesAt source counter base input count) counter replacement =
tapesAt source counter base input replacement := by
simp [tapesAt]
private theorem joinTrace_inline_MachineBinaryNameSearch {X : Type*} {f : X → X} {a b c : X} {n m : Nat}
(first : f^[n] a = b) (second : f^[m] b = c) : f^[n + m] a = c := by
rw [Nat.add_comm, Function.iterate_add_apply, first, second]
variable (tape : Fin 6 → K) (distinct : Function.Injective tape)
variable (labels : Label → Λ) (foundExit missingExit malformedExit : Option Λ)
variable (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State A))
variable (atLabels : ∀ l, program (labels l) =
instruction tape labels foundExit missingExit malformedExit l)
variable (base : K → List Bool) (ambient : A)
include distinct atLabels
theorem signStep (sign : Bool) (input counter : List Bool) :
TM2.step program
⟨some (labels .sign), clean ambient,
tapesAt (tape 0) (tape 5) base (sign :: input) counter⟩ =
some ⟨some (labels (.comparison .scan)), clean ambient,
tapesAt (tape 0) (tape 5) base input counter⟩ := by
have hd : tape 0 ≠ tape 5 := fun h => (by decide : (0 : Fin 6) ≠ 5) (distinct h)
change some (TM2.stepAux (program (labels .sign)) _ _) = _
rw [atLabels .sign]
simp [instruction, TM2.stepAux, clean, BinaryNameCompare.clean, hd, update_source_inline_MachineBinaryNameSearch]
theorem emptyStep (counter : List Bool) :
TM2.step program
⟨some (labels .sign), clean ambient,
tapesAt (tape 0) (tape 5) base [] counter⟩ =
some ⟨missingExit, clean ambient, tapesAt (tape 0) (tape 5) base [] counter⟩ := by
have hd : tape 0 ≠ tape 5 := fun h => (by decide : (0 : Fin 6) ≠ 5) (distinct h)
change some (TM2.stepAux (program (labels .sign)) _ _) = _
rw [atLabels .sign]
simp [instruction, TM2.stepAux, clean, BinaryNameCompare.clean, hd, update_source_inline_MachineBinaryNameSearch]
omit distinct in
theorem incrementStep (input counter : List Bool) :
TM2.step program
⟨some (labels .increment), clean ambient,
tapesAt (tape 0) (tape 5) base input counter⟩ =
some ⟨some (labels .sign), clean ambient,
tapesAt (tape 0) (tape 5) base input (true :: counter)⟩ := by
change some (TM2.stepAux (program (labels .increment)) _ _) = _
rw [atLabels .increment]
simp [instruction, TM2.stepAux, update_counter_inline_MachineBinaryNameSearch]
theorem tokenTrace (token : Token) (key suffix counter : List Bool)
(canonical : BinaryNameMachine.canonical token.2 = true)
(keyWord : base (tape 1) = key.reverse)
(candidateEmpty : base (tape 2) = []) (copyEmpty : base (tape 3) = [])
(scratchEmpty : base (tape 4) = []) :
(advance (TM2.step program))^[1 + BinaryNameCompare.steps token.2.length key.length]
(some ⟨some (labels .sign), clean ambient,
tapesAt (tape 0) (tape 5) base (tokenBits token ++ suffix) counter⟩) =
some ⟨if token.2 = key then foundExit else some (labels .increment), clean ambient,
tapesAt (tape 0) (tape 5) base suffix counter⟩ := by
have hd (i j : Fin 6) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h)
let initial := tapesAt (tape 0) (tape 5) base
(BinaryNameMachine.frame token.2 ++ suffix) counter
have first : (advance (TM2.step program))^[1]
(some ⟨some (labels .sign), clean ambient,
tapesAt (tape 0) (tape 5) base (tokenBits token ++ suffix) counter⟩) =
some ⟨some (labels (.comparison .scan)), clean ambient, initial⟩ := by
simpa only [Function.iterate_one, advance_some, tokenBits, List.cons_append] using
signStep tape distinct labels foundExit missingExit malformedExit program atLabels
base ambient token.1 (BinaryNameMachine.frame token.2 ++ suffix) counter
have second := BinaryNameCompare.compareTrace (compareTapes tape)
(compareTapes_injective tape distinct) (fun l => labels (.comparison l))
foundExit (some (labels .increment)) malformedExit program
(fun l => atLabels (.comparison l)) initial token.2 suffix key.reverse canonical
(by simp [initial, compareTapes, hd 0 5 (by decide)])
(by simpa [initial, compareTapes, tapesAt,
hd 1 0 (by decide), hd 1 5 (by decide)] using keyWord)
(by simpa [initial, compareTapes, tapesAt, hd 2 0 (by decide), hd 2 5 (by decide)]
using candidateEmpty)
(by simpa [initial, compareTapes, tapesAt,
hd 3 0 (by decide), hd 3 5 (by decide)] using copyEmpty)
(by simpa [initial, compareTapes, tapesAt,
hd 4 0 (by decide), hd 4 5 (by decide)] using scratchEmpty)
ambient
have heq : token.2.reverse = key.reverse ↔ token.2 = key := by
constructor
· intro h
have := congrArg List.reverse h
simpa only [List.reverse_reverse] using this
· intro h; rw [h]
simp only [List.length_reverse, heq] at second
rw [show Function.update initial (compareTapes tape 0) suffix =
tapesAt (tape 0) (tape 5) base suffix counter by
exact update_source_inline_MachineBinaryNameSearch _ _ (hd 0 5 (by decide)) base _ counter suffix] at second
exact joinTrace_inline_MachineBinaryNameSearch first second
theorem searchTrace (tokens : List Token) (key suffix counter : List Bool)
(canonical : ∀ token ∈ tokens, BinaryNameMachine.canonical token.2 = true)
(present : key ∈ payloads tokens)
(keyWord : base (tape 1) = key.reverse)
(candidateEmpty : base (tape 2) = []) (copyEmpty : base (tape 3) = [])
(scratchEmpty : base (tape 4) = []) :
(advance (TM2.step program))^[steps tokens key]
(some ⟨some (labels .sign), clean ambient,
tapesAt (tape 0) (tape 5) base (stream tokens ++ suffix) counter⟩) =
some ⟨foundExit, clean ambient,
tapesAt (tape 0) (tape 5) base (stream (afterMatch tokens key) ++ suffix)
(List.replicate ((payloads tokens).idxOf key) true ++ counter)⟩ := by
induction tokens generalizing counter with
| nil => simp at present
| cons token tokens ih =>
have headCanonical := canonical token (by simp)
have tailCanonical : ∀ t ∈ tokens, BinaryNameMachine.canonical t.2 = true :=
fun t ht => canonical t (by simp [ht])
have first := tokenTrace tape distinct labels foundExit missingExit malformedExit
program atLabels base ambient token key (stream tokens ++ suffix) counter
headCanonical keyWord candidateEmpty copyEmpty scratchEmpty
by_cases same : token.2 = key
· simpa only [steps, same, ite_true, Nat.add_zero, stream_cons,
List.append_assoc, afterMatch, payloads_cons, List.idxOf_cons_self,
List.replicate_zero, List.nil_append] using first
· have remaining : key ∈ payloads tokens := by
rcases List.mem_cons.mp present with equal | remaining
· exact False.elim (same equal.symm)
· exact remaining
have increment : (advance (TM2.step program))^[1]
(some ⟨some (labels .increment), clean ambient,
tapesAt (tape 0) (tape 5) base (stream tokens ++ suffix) counter⟩) =
some ⟨some (labels .sign), clean ambient,
tapesAt (tape 0) (tape 5) base (stream tokens ++ suffix) (true :: counter)⟩ := by
simpa only [Function.iterate_one, advance_some] using
incrementStep tape labels foundExit missingExit malformedExit program
atLabels base ambient (stream tokens ++ suffix) counter
have rest := ih (true :: counter) tailCanonical remaining
simp only [same, ite_false] at first
have full := joinTrace_inline_MachineBinaryNameSearch first (joinTrace_inline_MachineBinaryNameSearch increment rest)
have counterEq : List.replicate ((payloads tokens).idxOf key) true ++
(true :: counter) =
List.replicate ((payloads tokens).idxOf key + 1) true ++ counter := by
rw [List.replicate_succ']
simp only [List.append_assoc, List.singleton_append]
have indexEq : (token.2 :: payloads tokens).idxOf key =
(payloads tokens).idxOf key + 1 := by
exact List.idxOf_cons_ne _ same
simpa only [steps, same, ite_false, stream_cons, List.append_assoc, afterMatch,
payloads_cons, indexEq, counterEq] using full
def searchInTime (tokens : List Token) (key suffix counter : List Bool)
(canonical : ∀ token ∈ tokens, BinaryNameMachine.canonical token.2 = true)
(present : key ∈ payloads tokens)
(keyWord : base (tape 1) = key.reverse)
(candidateEmpty : base (tape 2) = []) (copyEmpty : base (tape 3) = [])
(scratchEmpty : base (tape 4) = []) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some (labels .sign), clean ambient,
tapesAt (tape 0) (tape 5) base (stream tokens ++ suffix) counter⟩
(some ⟨foundExit, clean ambient,
tapesAt (tape 0) (tape 5) base (stream (afterMatch tokens key) ++ suffix)
(List.replicate ((payloads tokens).idxOf key) true ++ counter)⟩)
((3 * key.length + 7) * (stream tokens).length) where
steps := steps tokens key
evals_in_steps := searchTrace tape distinct labels foundExit missingExit malformedExit
program atLabels base ambient tokens key suffix counter canonical present keyWord
candidateEmpty copyEmpty scratchEmpty
steps_le_m := steps_le_stream tokens key
end Program
def machine : FinTM2 where
K := Fin 6
k₀ := 0
k₁ := 5
Γ _ := Bool
Λ := Label ⊕ Fin 3
main := .inl .sign
σ := State Unit
initialState := clean ()
m label := match label with
| .inl l => instruction id Sum.inl (some (.inr 0)) (some (.inr 1)) (some (.inr 2)) l
| .inr _ => .halt
theorem machine_finiteAlphabet (k : machine.K) : Finite (machine.Γ k) := by
change Finite Bool
infer_instance
end DFVSGames.BinaryNameSearch
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end OAI