cfahlgren1/math / lean /OAI /Combinatorics /IndependentSets /Machines /MachineFiniteSequence.lean
cfahlgren1's picture
download
raw
12.2 kB
import Mathlib
import OAI.Combinatorics.IndependentSets.Expansion.PoweringFieldPlan
import OAI.Combinatorics.IndependentSets.Machines.PoweringMachineField
namespace OAI
namespace IndependentSetsGames.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 IndependentSetsGames.Foundations.Complexity.MachineFiniteSequence
namespace IndependentSetsGames.Foundations.Complexity.PoweringMachinePlan
open Turing MachineComposition PCP PoweringFieldPlan
variable {K Λ A : Type} [DecidableEq K] {vertices d max : Nat}
abbrev Command (d max : Nat) := {op : Instruction d // op.radius ≤ max}
abbrev LocalLabel (op : Command d max) := PoweringMachineField.Label op.val
abbrev Label (commands : List (Command d max)) := MachineFiniteSequence.Label LocalLabel commands
def entry (commands : List (Command d max)) (labels : Label commands → Λ) (exit : Option Λ) : Option Λ :=
MachineFiniteSequence.entry LocalLabel (fun op => PoweringMachineField.entry op.val)
commands labels exit
def instruction (placement : PoweringMachineTapes.Tape max → K)
(commands : List (Command d max)) (labels : Label commands → Λ) (exit : Option Λ) :
Label commands → TM2.Stmt (fun _ : K => Bool) Λ (MachineUnaryEqualityBit.State A) :=
MachineFiniteSequence.instruction LocalLabel (fun op => PoweringMachineField.entry op.val)
(fun op => PoweringMachineField.instruction placement op.val op.property)
commands labels exit
def result (graph : PortTables.Table vertices d) (placement : PoweringMachineTapes.Tape max → K)
(vertex : Fin vertices) (op : Command d max) (base : K → List Bool) : K → List Bool :=
PoweringMachineField.finalTapes graph placement vertex op.val op.property base
def finalTapes (graph : PortTables.Table vertices d) (placement : PoweringMachineTapes.Tape max → K)
(vertex : Fin vertices) (commands : List (Command d max)) (base : K → List Bool) : K → List Bool :=
MachineFiniteSequence.resultOf (result graph placement vertex) commands base
def steps (graph : PortTables.Table vertices d) (placement : PoweringMachineTapes.Tape max → K)
(vertex : Fin vertices) (commands : List (Command d max)) (base : K → List Bool) : Nat :=
MachineFiniteSequence.steps (result graph placement vertex)
(fun op _ => PoweringMachineField.steps graph vertex op.val) commands base
theorem planTrace (graph : PortTables.Table vertices d)
(placement : PoweringMachineTapes.Tape max → K) (distinct : Function.Injective placement)
(vertex : Fin vertices) (commands : List (Command d max))
(labels : Label commands → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (MachineUnaryEqualityBit.State A))
(atLabels : ∀ l, program (labels l) = instruction placement commands labels exit l)
(base : K → List Bool) (suffix : List Bool)
(ready : PoweringMachineField.Ready graph placement vertex suffix base) (ambient : A) :
(advance (TM2.step program))^[steps graph placement vertex commands base]
(some ⟨entry commands labels exit, MachineUnaryEqualityBit.clean ambient, base⟩) =
some ⟨exit, MachineUnaryEqualityBit.clean ambient,
finalTapes graph placement vertex commands base⟩ := by
apply MachineFiniteSequence.trace LocalLabel (fun op => PoweringMachineField.entry op.val)
(fun op => PoweringMachineField.instruction placement op.val op.property)
(result graph placement vertex) (fun op _ => PoweringMachineField.steps graph vertex op.val)
program (PoweringMachineField.Ready graph placement vertex suffix)
(fun _ => MachineUnaryEqualityBit.clean ambient) id commands
· intro op _ tapes good
exact PoweringMachineField.ready_finalTapes graph placement distinct vertex op.val
op.property tapes suffix good
· intro op _ localLabels localExit atLocal tapes good
exact PoweringMachineField.fieldTrace graph placement distinct vertex op.val op.property
localLabels localExit program atLocal tapes suffix good ambient
· exact atLabels
· exact ready
theorem ready_finalTapes (graph : PortTables.Table vertices d)
(placement : PoweringMachineTapes.Tape max → K) (distinct : Function.Injective placement)
(vertex : Fin vertices) (commands : List (Command d max)) (base : K → List Bool)
(suffix : List Bool) (ready : PoweringMachineField.Ready graph placement vertex suffix base) :
PoweringMachineField.Ready graph placement vertex suffix
(finalTapes graph placement vertex commands base) := by
induction commands generalizing base with
| nil => exact ready
| cons op commands ih =>
apply ih
exact PoweringMachineField.ready_finalTapes graph placement distinct vertex op.val
op.property base suffix ready
theorem finalTapes_output (graph : PortTables.Table vertices d)
(placement : PoweringMachineTapes.Tape max → K) (distinct : Function.Injective placement)
(vertex : Fin vertices) (commands : List (Command d max)) (base : K → List Bool)
(suffix : List Bool) (ready : PoweringMachineField.Ready graph placement vertex suffix base) :
finalTapes graph placement vertex commands base (placement (.inl 10)) =
PoweringMachineRow.encodeBits ((commands.map (fun op => evaluate graph vertex op.val)).reverse) ++
base (placement (.inl 10)) := by
induction commands generalizing base with
| nil => rfl
| cons op commands ih =>
have nextReady := PoweringMachineField.ready_finalTapes graph placement distinct vertex
op.val op.property base suffix ready
change finalTapes graph placement vertex commands
(result graph placement vertex op base) (placement (.inl 10)) = _
dsimp only [result]
rw [ih _ nextReady, PoweringMachineField.finalTapes_output graph placement distinct vertex
op.val op.property base suffix ready]
simp only [List.map_cons, List.reverse_cons, PoweringMachineRow.encodeBits_append,
PoweringMachineRow.encodeBits, List.append_nil,
List.append_assoc]
def boundedRowPlan (n : Nat) (ports : Fin (n + 1) → Fin d) (direction : Bool) :
List (Command d (2 * (n + 1))) :=
List.ofFn fun i : Fin (PoweringMachineRow.inputSize (n + 1) (PoweringRowData.slotCount d n)) =>
let pair := (PoweringMachineRow.inputEquiv (n + 1) (PoweringRowData.slotCount d n)).symm i
⟨directedFieldPlan n ports direction pair.1 pair.2,
directedFieldPlan_radius n ports direction pair.1 pair.2⟩
theorem boundedRowPlan_values (n : Nat) (ports : Fin (n + 1) → Fin d) (direction : Bool) :
(boundedRowPlan n ports direction).map Subtype.val = rowPlan n ports direction := by
rw [boundedRowPlan, rowPlan, List.map_ofFn]
rfl
theorem rowPlan_output (graph : PortTables.Table vertices d)
(n : Nat) (ports : Fin (n + 1) → Fin d) (direction : Bool)
(placement : PoweringMachineTapes.Tape (2 * (n + 1)) → K)
(distinct : Function.Injective placement) (vertex : Fin vertices) (base : K → List Bool)
(suffix : List Bool) (ready : PoweringMachineField.Ready graph placement vertex suffix base) :
finalTapes graph placement vertex (boundedRowPlan n ports direction).reverse base
(placement (.inl 10)) =
PoweringRowData.dataTape graph n (direction, vertex, ports) ++ base (placement (.inl 10)) := by
rw [finalTapes_output graph placement distinct vertex _ base suffix ready]
rw [List.map_reverse, List.reverse_reverse, PoweringFieldPlan.dataTape_eq_plan]
have h := congrArg (List.map (evaluate graph vertex)) (boundedRowPlan_values n ports direction)
simpa only [List.map_map, Function.comp_def] using
congrArg (fun bits => PoweringMachineRow.encodeBits bits ++ base (placement (.inl 10))) h
end IndependentSetsGames.Foundations.Complexity.PoweringMachinePlan
end OAI

Xet Storage Details

Size:
12.2 kB
·
Xet hash:
aa415f06bedad3df0dd2f76f97d505fa574a5e8145e0a63e3506de00cdac0ba9

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.