File size: 14,319 Bytes
8df23f4 | 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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | import OAI.Computability.BinPacking.CookLevin.ClashMachine
namespace OAI
namespace BinPackingGames.Foundations.Complexity.CookLevin.InitializationMachine
open Turing
open Reduction.MachineSubstitution (pushWord stepAux_pushWord)
open PostfixModel
inductive Tape where
| remaining | current | threshold | scratch | reversed
| rawInput | witnessBound | capacity | closure
deriving DecidableEq
protected abbrev Tape.enumList : List Tape := [.remaining, .current, .threshold, .scratch,
.reversed, .rawInput, .witnessBound, .capacity, .closure]
protected theorem Tape.enumList_getElem?_ctorIdx_eq (x : Tape) :
Tape.enumList[x.ctorIdx]? = some x := by
cases x <;> rfl
protected theorem Tape.enumList_nodup : Tape.enumList.Nodup := by decide
instance : Fintype Tape where
elems := ⟨Tape.enumList, Tape.enumList_nodup⟩
complete x := by cases x <;> decide
inductive Label where
| guard | inputTag | scan | emit (symbol : Bool) | restore | test
| result (value : Bool) | done
deriving DecidableEq, Fintype
abbrev Alphabet (_ : Tape) := Bool
abbrev State := (Unit × Unit) × Option Bool
def initialState : State := (((), ()), none)
def program : Label → TM2.Stmt Alphabet Label State
| .guard => MachineUnaryCounter.guard .remaining .inputTag .done
| .inputTag => pushWord .reversed (encodeWord 5) (.goto fun _ => .scan)
| .scan => MachineTransducerCopy.scanLoop .current .scratch () (fun _ b => .emit b) .restore
| .emit b => MachineTransducerCopy.emitter .reversed (fun _ _ => ())
(fun _ symbol => [symbol]) .scan () b
| .restore => Reduction.MachineTransfer.loopAt .scratch .current id false .restore (some .test)
| .test => MachineUnaryCounter.guard .threshold (.result false) (.result true)
| .result b => pushWord .reversed (encodeWords [if b then 1 else 0, 3])
(.push .current (fun _ => true) (.goto fun _ => .guard))
| .done => .halt
def machine : FinTM2 where
K := Tape
k₀ := .rawInput
k₁ := .reversed
Γ _ := Bool
Λ := Label
main := .guard
σ := State
initialState := initialState
m := program
def tapes (base : Tape → List Bool) (remaining current threshold : Nat)
(output : List Bool) : Tape → List Bool
| .remaining => encodeWord remaining
| .current => encodeWord current
| .threshold => encodeWord threshold
| .scratch => []
| .reversed => output
| tape => base tape
@[simp] theorem tapes_current (base : Tape → List Bool) (m i d : Nat) (output : List Bool) :
tapes base m i d output .current = encodeWord i := rfl
@[simp] theorem tapes_reversed (base : Tape → List Bool) (m i d : Nat) (output : List Bool) :
tapes base m i d output .reversed = output := rfl
theorem update_reversed (base : Tape → List Bool) (m i d : Nat)
(output replacement : List Bool) :
Function.update (tapes base m i d output) .reversed replacement =
tapes base m i d replacement := by
funext tape
cases tape <;> simp [tapes]
theorem push_current (base : Tape → List Bool) (m i d : Nat) (output : List Bool) :
Function.update (tapes base m i d output) .current (true :: encodeWord i) =
tapes base m (i + 1) d output := by
funext tape
cases tape <;> simp [tapes, encodeWord, List.replicate_succ]
theorem counterTapes_remaining (base : Tape → List Bool) (m i d r : Nat)
(output : List Bool) :
MachineUnaryCounter.counterTapes .remaining (tapes base m i d output) r [] =
tapes base r i d output := by
funext tape
cases tape <;> simp [MachineUnaryCounter.counterTapes, tapes]
theorem counterTapes_threshold (base : Tape → List Bool) (m i d r : Nat)
(output : List Bool) :
MachineUnaryCounter.counterTapes .threshold (tapes base m i d output) r [] =
tapes base m i r output := by
funext tape
cases tape <;> simp [MachineUnaryCounter.counterTapes, tapes]
private theorem chain {α : Type*} {f : α → α} {x y z : α} {m n : Nat}
(first : f^[m] x = y) (second : f^[n] y = z) : f^[m + n] x = z := by
rw [Nat.add_comm m n, Function.iterate_add_apply, first, second]
theorem inputTagStep (base : Tape → List Bool) (m i d : Nat) (output : List Bool) :
TM2.step program ⟨some .inputTag, initialState, tapes base m i d output⟩ =
some ⟨some .scan, initialState,
tapes base m i d ((encodeWord 5).reverse ++ output)⟩ := by
change some (TM2.stepAux (program .inputTag) initialState (tapes base m i d output)) = _
rw [program, stepAux_pushWord]
change some (⟨some .scan, initialState, Function.update (tapes base m i d output)
.reversed ((encodeWord 5).reverse ++ output)⟩ : TM2.Cfg Alphabet Label State) = _
rw [update_reversed]
private theorem identity_output (input : List Bool) :
Reduction.MachineTransducer.output (fun (_ : Unit) _ => ())
(fun _ symbol => [symbol]) () input = input := by
induction input with
| nil => rfl
| cons b input ih => simp [Reduction.MachineTransducer.output, ih]
theorem indexTrace (base : Tape → List Bool) (m i d : Nat) (output : List Bool) :
(MachineComposition.advance (TM2.step program))^[3 * (i + 1) + 2]
(some ⟨some .scan, initialState, tapes base m i d output⟩) =
some ⟨some .test, initialState,
tapes base m i d ((encodeWord i).reverse ++ output)⟩ := by
have h := MachineTransducerCopy.transduceCopyTrace Tape.current Tape.scratch Tape.reversed
(by decide) (by decide) (by decide) () (fun _ _ => ()) (fun _ symbol => [symbol])
Label.scan Label.restore (fun _ b => Label.emit b) (some Label.test)
program rfl (by intro control symbol; cases control; rfl) rfl
(tapes base m i d output) rfl () () none
simpa only [tapes_current, tapes_reversed, encodeWord_length, identity_output,
update_reversed, initialState] using h
theorem thresholdStep (base : Tape → List Bool) (m i d : Nat) (output : List Bool) :
TM2.step program ⟨some .test, initialState, tapes base m i d output⟩ =
some ⟨some (.result (decide (d = 0))), initialState,
tapes base m i (d - 1) output⟩ := by
cases d with
| zero =>
simpa only [counterTapes_threshold, Nat.zero_sub, decide_true, initialState] using
MachineUnaryCounter.guardStep_zero Tape.threshold Label.test (.result false) (.result true)
program rfl (tapes base m i 0 output) [] ((), ()) none
| succ d =>
simpa only [counterTapes_threshold, Nat.add_sub_cancel, Nat.succ_ne_zero,
decide_false, initialState] using
MachineUnaryCounter.guardStep_succ Tape.threshold Label.test (.result false) (.result true)
program rfl (tapes base m i (d + 1) output) d [] ((), ()) none
theorem resultStep (base : Tape → List Bool) (m i d : Nat) (output : List Bool) (b : Bool) :
TM2.step program ⟨some (.result b), initialState, tapes base m i d output⟩ =
some ⟨some .guard, initialState, tapes base m (i + 1) d
((encodeWords [if b then 1 else 0, 3]).reverse ++ output)⟩ := by
change some (TM2.stepAux (program (.result b)) initialState (tapes base m i d output)) = _
rw [program, stepAux_pushWord]
simp only [TM2.stepAux, tapes_reversed]
rw [update_reversed, tapes_current, push_current]
def termTokens (i d : Nat) : List Token :=
[.input i, .const (decide (d = 0)), .and]
theorem termTokens_bits (i d : Nat) :
tokenBits (termTokens i d) = encodeWord 5 ++ encodeWord i ++
encodeWords [if decide (d = 0) then 1 else 0, 3] := by
by_cases hd : d = 0 <;>
simp [termTokens, tokenBits, tokenWords, Token.words, hd, encodeWords, List.append_assoc]
def bodySteps (i : Nat) : Nat := 3 * (i + 1) + 5
theorem bodyTrace (base : Tape → List Bool) (m i d : Nat) (output : List Bool) :
(MachineComposition.advance (TM2.step program))^[bodySteps i]
(some ⟨some .inputTag, initialState, tapes base m i d output⟩) =
some ⟨some .guard, initialState, tapes base m (i + 1) (d - 1)
((tokenBits (termTokens i d)).reverse ++ output)⟩ := by
have tag : (MachineComposition.advance (TM2.step program))^[1]
(some ⟨some .inputTag, initialState, tapes base m i d output⟩) =
some ⟨some .scan, initialState,
tapes base m i d ((encodeWord 5).reverse ++ output)⟩ := by
simpa only [Function.iterate_one, MachineComposition.advance_some] using
inputTagStep base m i d output
have index := indexTrace base m i d ((encodeWord 5).reverse ++ output)
let out := (encodeWord i).reverse ++ ((encodeWord 5).reverse ++ output)
have test : (MachineComposition.advance (TM2.step program))^[1]
(some ⟨some .test, initialState, tapes base m i d out⟩) =
some ⟨some (.result (decide (d = 0))), initialState,
tapes base m i (d - 1) out⟩ := by
simpa only [Function.iterate_one, MachineComposition.advance_some] using
thresholdStep base m i d out
have result : (MachineComposition.advance (TM2.step program))^[1]
(some ⟨some (.result (decide (d = 0))), initialState,
tapes base m i (d - 1) out⟩) =
some ⟨some .guard, initialState, tapes base m (i + 1) (d - 1)
((encodeWords [if decide (d = 0) then 1 else 0, 3]).reverse ++ out)⟩ := by
simpa only [Function.iterate_one, MachineComposition.advance_some] using
resultStep base m i (d - 1) out (decide (d = 0))
have whole := chain (chain (chain tag index) test) result
have htime : 1 + (3 * (i + 1) + 2) + 1 + 1 = bodySteps i := by
unfold bodySteps
omega
rw [htime] at whole
simpa only [termTokens_bits, List.reverse_append, List.append_assoc, out] using whole
theorem guardStep_succ (base : Tape → List Bool) (m i d : Nat) (output : List Bool) :
TM2.step program ⟨some .guard, initialState, tapes base (m + 1) i d output⟩ =
some ⟨some .inputTag, initialState, tapes base m i d output⟩ := by
simpa only [counterTapes_remaining, initialState] using
MachineUnaryCounter.guardStep_succ Tape.remaining Label.guard Label.inputTag Label.done
program rfl (tapes base (m + 1) i d output) m [] ((), ()) none
theorem guardStep_zero (base : Tape → List Bool) (i d : Nat) (output : List Bool) :
TM2.step program ⟨some .guard, initialState, tapes base 0 i d output⟩ =
some ⟨some .done, initialState, tapes base 0 i d output⟩ := by
simpa only [counterTapes_remaining, initialState] using
MachineUnaryCounter.guardStep_zero Tape.remaining Label.guard Label.inputTag Label.done
program rfl (tapes base 0 i d output) [] ((), ()) none
theorem tokens_append_bits (xs ys : List Token) :
tokenBits (xs ++ ys) = tokenBits xs ++ tokenBits ys := by
simp [tokenBits, tokenWords, List.flatMap_append]
def selectorSteps : Nat → Nat → Nat
| _, 0 => 1
| i, m + 1 => 1 + bodySteps i + selectorSteps (i + 1) m
theorem selectorTrace (base : Tape → List Bool) (m i d : Nat) (output : List Bool) :
(MachineComposition.advance (TM2.step program))^[selectorSteps i m]
(some ⟨some .guard, initialState, tapes base m i d output⟩) =
some ⟨some .done, initialState, tapes base 0 (i + m) (d - m)
((tokenBits (InitializationTemplate.selectorLoopTokens m i d)).reverse ++ output)⟩ := by
induction m generalizing i d output with
| zero =>
simpa only [selectorSteps, InitializationTemplate.selectorLoopTokens,
InitializationTemplate.forTokens, tokenBits, tokenWords, List.flatMap_nil,
encodeWords, List.reverse_nil, List.nil_append, Nat.add_zero, Nat.sub_zero,
Function.iterate_one, MachineComposition.advance_some] using
guardStep_zero base i d output
| succ m ih =>
have guard : (MachineComposition.advance (TM2.step program))^[1]
(some ⟨some .guard, initialState, tapes base (m + 1) i d output⟩) =
some ⟨some .inputTag, initialState, tapes base m i d output⟩ := by
simpa only [Function.iterate_one, MachineComposition.advance_some] using
guardStep_succ base m i d output
have body := bodyTrace base m i d output
have rest := ih (i + 1) (d - 1) ((tokenBits (termTokens i d)).reverse ++ output)
have whole := chain (chain guard body) rest
have hs : InitializationTemplate.selectorLoopTokens (m + 1) i d =
termTokens i d ++ InitializationTemplate.selectorLoopTokens m (i + 1) (d - 1) :=
InitializationTemplate.selectorLoopTokens_succ m i d
rw [hs, tokens_append_bits, List.reverse_append, List.append_assoc]
simpa only [selectorSteps, Nat.add_assoc, Nat.add_comm, Nat.add_left_comm,
Nat.sub_sub] using whole
theorem selectorSteps_le (i m : Nat) :
selectorSteps i m ≤ m * (3 * (i + m) + 6) + 1 := by
induction m generalizing i with
| zero => simp [selectorSteps]
| succ m ih =>
let B := 3 * (i + (m + 1)) + 6
have hbody : 1 + bodySteps i ≤ B := by unfold bodySteps B; omega
have hrest : selectorSteps (i + 1) m ≤ m * B + 1 := by
simpa only [B, Nat.add_assoc, Nat.add_comm, Nat.add_left_comm] using ih (i + 1)
calc
selectorSteps i (m + 1) = (1 + bodySteps i) + selectorSteps (i + 1) m := rfl
_ ≤ B + (m * B + 1) := Nat.add_le_add hbody hrest
_ = (m + 1) * B + 1 := by rw [Nat.succ_mul]; omega
def selectorInTime (base : Tape → List Bool) (m i d : Nat) (output : List Bool) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some .guard, initialState, tapes base m i d output⟩
(some ⟨some .done, initialState, tapes base 0 (i + m) (d - m)
((tokenBits (InitializationTemplate.selectorLoopTokens m i d)).reverse ++ output)⟩)
(m * (3 * (i + m) + 6) + 1) where
steps := selectorSteps i m
evals_in_steps := selectorTrace base m i d output
steps_le_m := selectorSteps_le i m
theorem tapes_rawInput (base : Tape → List Bool) (m i d : Nat) (output : List Bool) :
tapes base m i d output .rawInput = base .rawInput := rfl
theorem tapes_witnessBound (base : Tape → List Bool) (m i d : Nat) (output : List Bool) :
tapes base m i d output .witnessBound = base .witnessBound := rfl
theorem tapes_capacity (base : Tape → List Bool) (m i d : Nat) (output : List Bool) :
tapes base m i d output .capacity = base .capacity := rfl
theorem tapes_closure (base : Tape → List Bool) (m i d : Nat) (output : List Bool) :
tapes base m i d output .closure = base .closure := rfl
end BinPackingGames.Foundations.Complexity.CookLevin.InitializationMachine
end OAI
|