File size: 10,911 Bytes
e320e82 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | import Mathlib
import OAI.Combinatorics.IndependentSets.Machines.RawInitialMachineBudget
import OAI.Combinatorics.IndependentSets.Machines.MachineComposition
namespace OAI
namespace IndependentSetsGames.Foundations.PCP.AlphabetTable.ReadRelation
open Turing
open IndependentSetsGames.Foundations.Complexity
open GenericGraphTables
variable {K Λ σ : Type} [DecidableEq K] {q : Nat}
abbrev Alphabet (_ : K) := Bool
abbrev State (σ : Type) (q : Nat) := (σ × RelationTable q) × Option Bool
def relationEquiv (q : Nat) : RelationTable q ≃ (Fin (q * q) → Bool) where
toFun relation i := relation[i.val]
invFun := Vector.ofFn
left_inv relation := Vector.ofFn_getElem
right_inv bits := by funext i; simp
@[instance_reducible] def relationFintype (q : Nat) : Fintype (RelationTable q) :=
Fintype.ofEquiv (Fin (q * q) → Bool) (relationEquiv q).symm
@[instance_reducible] def stateFintype (σ : Type) (q : Nat) [Fintype σ] :
Fintype (State σ q) := by
letI := relationFintype q
exact inferInstance
def readSlot (source : K) (i : Fin (q * q))
(next : TM2.Stmt (Alphabet (K := K)) Λ (State σ q)) :
TM2.Stmt (Alphabet (K := K)) Λ (State σ q) :=
.pop source
(fun state head =>
((state.1.1, state.1.2.set i.val (head.getD false) i.isLt), head))
(.branch (fun state => state.2.getD false)
(.pop source (fun state _ => (state.1, none)) next)
(.load (fun state => (state.1, none)) next))
theorem stepAux_readSlot (source : K) (i : Fin (q * q)) (bit : Bool)
(next : TM2.Stmt (Alphabet (K := K)) Λ (State σ q))
(base : K → List Bool) (suffix : List Bool) (ambient : σ)
(initial : RelationTable q) (register : Option Bool) :
TM2.stepAux (readSlot source i next) ((ambient, initial), register)
(Function.update base source (encodeWord (bitWord bit) ++ suffix)) =
TM2.stepAux next ((ambient, initial.set i.val bit i.isLt), none)
(Function.update base source suffix) := by
cases bit <;> simp [readSlot, TM2.stepAux, bitWord, encodeWord]
def readSlots (source : K) : List (Fin (q * q)) →
TM2.Stmt (Alphabet (K := K)) Λ (State σ q) →
TM2.Stmt (Alphabet (K := K)) Λ (State σ q)
| [], next => .load (fun state => (state.1, none)) next
| i :: indices, next => readSlot source i (readSlots source indices next)
def indexedWords (relation : RelationTable q) (indices : List (Fin (q * q))) : List Nat :=
indices.map (fun i => bitWord relation[i.val])
def fillSlots (relation : RelationTable q) : List (Fin (q * q)) →
RelationTable q → RelationTable q
| [], initial => initial
| i :: indices, initial =>
fillSlots relation indices (initial.set i.val relation[i.val] i.isLt)
theorem fillSlots_preserves_correct (relation : RelationTable q)
(indices : List (Fin (q * q))) (initial : RelationTable q) (i : Fin (q * q))
(hcorrect : initial[i.val] = relation[i.val]) :
(fillSlots relation indices initial)[i.val] = relation[i.val] := by
induction indices generalizing initial with
| nil => exact hcorrect
| cons j indices ih =>
apply ih
by_cases hji : j.val = i.val
· simp [hji]
· simp [hji, hcorrect]
theorem fillSlots_mem (relation : RelationTable q) (indices : List (Fin (q * q)))
(initial : RelationTable q) (i : Fin (q * q)) (hi : i ∈ indices) :
(fillSlots relation indices initial)[i.val] = relation[i.val] := by
induction indices generalizing initial with
| nil => simp at hi
| cons j indices ih =>
change (fillSlots relation indices (initial.set j.val relation[j.val] j.isLt))[i.val] = _
rcases List.mem_cons.mp hi with h | h
· subst j
apply fillSlots_preserves_correct
simp
· exact ih _ h
theorem fillSlots_finRange (relation initial : RelationTable q) :
fillSlots relation (List.finRange (q * q)) initial = relation := by
apply Vector.ext
intro i hi
exact fillSlots_mem relation (List.finRange (q * q)) initial ⟨i, hi⟩
(List.mem_finRange _)
theorem indexedWords_finRange (relation : RelationTable q) :
indexedWords relation (List.finRange (q * q)) = relationWords relation := by
have h : (List.finRange (q * q)).map (fun i => relation[i.val]) = relation.toList := by
rw [List.finRange, List.map_ofFn]
change List.ofFn (fun i : Fin (q * q) => relation[i.val]) = relation.toList
rw [← Vector.toList_ofFn, Vector.ofFn_getElem]
calc
_ = ((List.finRange (q * q)).map (fun i => relation[i.val])).map bitWord := by
simp only [indexedWords, List.map_map, Function.comp_def]
_ = relation.toList.map bitWord := congrArg (fun bits : List Bool => bits.map bitWord) h
_ = _ := rfl
theorem stepAux_readSlots (source : K) (indices : List (Fin (q * q)))
(relation : RelationTable q)
(next : TM2.Stmt (Alphabet (K := K)) Λ (State σ q))
(base : K → List Bool) (suffix : List Bool) (ambient : σ)
(initial : RelationTable q) (register : Option Bool) :
TM2.stepAux (readSlots source indices next) ((ambient, initial), register)
(Function.update base source (encodeWords (indexedWords relation indices) ++ suffix)) =
TM2.stepAux next ((ambient, fillSlots relation indices initial), none)
(Function.update base source suffix) := by
induction indices generalizing initial register with
| nil => simp [readSlots, fillSlots, indexedWords, encodeWords, TM2.stepAux]
| cons i indices ih =>
simp only [readSlots, indexedWords, List.map_cons, encodeWords, List.append_assoc, fillSlots]
rw [stepAux_readSlot]
exact ih _ none
def readRelation (source : K)
(next : TM2.Stmt (Alphabet (K := K)) Λ (State σ q)) :
TM2.Stmt (Alphabet (K := K)) Λ (State σ q) :=
readSlots source (List.finRange (q * q)) next
theorem stepAux_readRelation (source : K) (relation : RelationTable q)
(next : TM2.Stmt (Alphabet (K := K)) Λ (State σ q))
(base : K → List Bool) (suffix : List Bool) (ambient : σ)
(initial : RelationTable q) (register : Option Bool) :
TM2.stepAux (readRelation source next) ((ambient, initial), register)
(Function.update base source (encodeWords (relationWords relation) ++ suffix)) =
TM2.stepAux next ((ambient, relation), none) (Function.update base source suffix) := by
simpa only [readRelation, indexedWords_finRange, fillSlots_finRange] using
stepAux_readSlots source (List.finRange (q * q)) relation next
base suffix ambient initial register
theorem stepAux_readRelation_fromTapes (source : K) (relation : RelationTable q)
(next : TM2.Stmt (Alphabet (K := K)) Λ (State σ q))
(base : K → List Bool) (suffix : List Bool)
(hinput : base source = encodeWords (relationWords relation) ++ suffix)
(ambient : σ) (initial : RelationTable q) (register : Option Bool) :
TM2.stepAux (readRelation source next) ((ambient, initial), register) base =
TM2.stepAux next ((ambient, relation), none) (Function.update base source suffix) := by
have h := stepAux_readRelation source relation next base suffix ambient initial register
have hbase : Function.update base source (encodeWords (relationWords relation) ++ suffix) =
base := by rw [← hinput]; exact Function.update_eq_self source base
rw [hbase] at h
exact h
omit [DecidableEq K] in
theorem readSlots_pushBound (source : K) (indices : List (Fin (q * q)))
(next : TM2.Stmt (Alphabet (K := K)) Λ (State σ q)) :
Runtime.statementPushBound (readSlots source indices next) =
Runtime.statementPushBound next := by
induction indices with
| nil => rfl
| cons i indices ih =>
simp only [readSlots, readSlot, Runtime.statementPushBound, ih, max_self]
omit [DecidableEq K] in
theorem readRelation_pushBound (source : K)
(next : TM2.Stmt (Alphabet (K := K)) Λ (State σ q)) :
Runtime.statementPushBound (readRelation source next) =
Runtime.statementPushBound next :=
readSlots_pushBound source (List.finRange (q * q)) next
def parser (source : K) (exitLabel : Λ) :
TM2.Stmt (Alphabet (K := K)) Λ (State σ q) :=
readRelation source (.goto fun _ => exitLabel)
omit [DecidableEq K] in
theorem parser_pushBound (source : K) (exitLabel : Λ) :
Runtime.statementPushBound (parser (σ := σ) (q := q) source exitLabel) = 0 := by
rw [parser, readRelation_pushBound]
rfl
theorem parserStep (source : K) (readLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ q))
(atRead : program readLabel = parser source exitLabel)
(base : K → List Bool) (relation : RelationTable q) (suffix : List Bool)
(ambient : σ) (initial : RelationTable q) (register : Option Bool) :
TM2.step program
⟨some readLabel, ((ambient, initial), register),
Function.update base source (encodeWords (relationWords relation) ++ suffix)⟩ =
some ⟨some exitLabel, ((ambient, relation), none), Function.update base source suffix⟩ := by
change some (TM2.stepAux (program readLabel) ((ambient, initial), register)
(Function.update base source (encodeWords (relationWords relation) ++ suffix))) = _
rw [atRead, parser, stepAux_readRelation]
rfl
theorem parserTrace (source : K) (readLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ q))
(atRead : program readLabel = parser source exitLabel)
(base : K → List Bool) (relation : RelationTable q) (suffix : List Bool)
(ambient : σ) (initial : RelationTable q) (register : Option Bool) :
(MachineComposition.advance (TM2.step program))^[1]
(some ⟨some readLabel, ((ambient, initial), register),
Function.update base source (encodeWords (relationWords relation) ++ suffix)⟩) =
some ⟨some exitLabel, ((ambient, relation), none), Function.update base source suffix⟩ := by
simpa only [Function.iterate_one, MachineComposition.advance_some] using
parserStep source readLabel exitLabel program atRead base relation suffix ambient initial register
def parserInTime (source : K) (readLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ q))
(atRead : program readLabel = parser source exitLabel)
(base : K → List Bool) (relation : RelationTable q) (suffix : List Bool)
(ambient : σ) (initial : RelationTable q) (register : Option Bool) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some readLabel, ((ambient, initial), register),
Function.update base source (encodeWords (relationWords relation) ++ suffix)⟩
(some ⟨some exitLabel, ((ambient, relation), none), Function.update base source suffix⟩) 1 where
steps := 1
evals_in_steps := by
change (MachineComposition.advance (TM2.step program))^[1] _ = _
exact parserTrace source readLabel exitLabel program atRead base relation suffix ambient initial register
steps_le_m := Nat.le_refl _
end IndependentSetsGames.Foundations.PCP.AlphabetTable.ReadRelation
end OAI
|