roseking's picture
Add lean source and binary files (part 44)
e320e82 verified
Raw History Blame Contribute Delete
9.12 kB
14
abbrev Alphabet (β : Type) (_ : K) := β
16
def appendTapes (source destination : K) (base : K → List β) : K → List β :=
body · 2 lines
  Reduction.MachineTransfer.tapesAt source destination base []
    (base destination ++ base source)
20
@[simp] theorem appendTapes_source (source destination : K)
    (distinct : source ≠ destination) (base : K → List β) :
    appendTapes source destination base source = [] := by
proof · 1 line
  simp [appendTapes, distinct]
25
@[simp] theorem appendTapes_destination (source destination : K) (base : K → List β) :
    appendTapes source destination base destination = base destination ++ base source := by
proof · 1 line
  simp [appendTapes]
29
theorem appendTapes_other (source destination k : K)
    (notSource : k ≠ source) (notDestination : k ≠ destination) (base : K → List β) :
    appendTapes source destination base k = base k := by
proof · 1 line
  simp [appendTapes, Reduction.MachineTransfer.tapesAt, notSource, notDestination]
34
theorem appendTapes_scratch (source destination scratch : K)
    (sourceScratch : source ≠ scratch) (destinationScratch : destination ≠ scratch)
    (base : K → List β) (scratchEmpty : base scratch = []) :
    appendTapes source destination base scratch = [] := by
proof · 2 lines
  rw [appendTapes_other source destination scratch (Ne.symm sourceScratch)
    (Ne.symm destinationScratch) base, scratchEmpty]
41
def firstTapes (destination scratch : K) (base : K → List β) : K → List β :=
body · 1 line
  Reduction.MachineTransfer.tapesAt destination scratch base [] (base destination).reverse
44
private def secondTapes (source destination scratch : K) (base : K → List β) : K → List β :=
body · 2 lines
  Reduction.MachineTransfer.tapesAt source scratch (firstTapes destination scratch base) []
    ((base source).reverse ++ (base destination).reverse)
48
theorem finalTapes_eq (source destination scratch : K)
    (sourceDestination : source ≠ destination) (sourceScratch : source ≠ scratch)
    (destinationScratch : destination ≠ scratch) (base : K → List β)
    (scratchEmpty : base scratch = []) :
    Reduction.MachineTransfer.tapesAt scratch destination
      (secondTapes source destination scratch base) [] (base destination ++ base source) =
        appendTapes source destination base := by
proof · 14 lines
  funext k
  by_cases hs : k = source
  · subst k
    simp [appendTapes, secondTapes, firstTapes, Reduction.MachineTransfer.tapesAt,
      sourceDestination, sourceScratch]
  · by_cases hd : k = destination
    · subst k
      simp [appendTapes, Reduction.MachineTransfer.tapesAt]
    · by_cases ht : k = scratch
      · subst k
        simp [appendTapes, Reduction.MachineTransfer.tapesAt, Ne.symm sourceScratch,
          Ne.symm destinationScratch, scratchEmpty]
      · simp [appendTapes, secondTapes, firstTapes, Reduction.MachineTransfer.tapesAt,
          hs, hd, ht]
70
theorem appendTrace (source destination scratch : K)
    (sourceDestination : source ≠ destination) (sourceScratch : source ≠ scratch)
    (destinationScratch : destination ≠ scratch)
    (fallback : β) (firstLabel secondLabel thirdLabel : Λ) (exit : Option Λ)
    (program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β))
    (atFirst : program firstLabel = Reduction.MachineTransfer.loopAt destination scratch
      id fallback firstLabel (some secondLabel))
    (atSecond : program secondLabel = Reduction.MachineTransfer.loopAt source scratch
      id fallback secondLabel (some thirdLabel))
    (atThird : program thirdLabel = Reduction.MachineTransfer.loopAt scratch destination
      id fallback thirdLabel exit)
    (base : K → List β) (scratchEmpty : base scratch = [])
    (ambient : σ) (register : Option β) :
    (MachineComposition.advance (TM2.step program))^[
        2 * ((base source).length + (base destination).length) + 3]
      (some ⟨some firstLabel, (ambient, register), base⟩) =
      some ⟨exit, (ambient, none), appendTapes source destination base⟩ := by
proof · 52 lines
  have first := Reduction.MachineTransfer.transferAt_fromTapes destination scratch
    destinationScratch id fallback firstLabel (some secondLabel) program atFirst
    base ambient register
  change (MachineComposition.advance (TM2.step program))^[(base destination).length + 1]
    (some ⟨some firstLabel, (ambient, register), base⟩) = _ at first
  simp only [List.map_id, scratchEmpty, List.append_nil] at first
  change (MachineComposition.advance (TM2.step program))^[(base destination).length + 1]
    (some ⟨some firstLabel, (ambient, register), base⟩) =
      some ⟨some secondLabel, (ambient, none), firstTapes destination scratch base⟩ at first
  have firstSource : firstTapes destination scratch base source = base source := by
    simp [firstTapes, Reduction.MachineTransfer.tapesAt, sourceDestination, sourceScratch]
  have firstScratch : firstTapes destination scratch base scratch =
      (base destination).reverse := by
    simp [firstTapes]
  have second := Reduction.MachineTransfer.transferAt_fromTapes source scratch
    sourceScratch id fallback secondLabel (some thirdLabel) program atSecond
    (firstTapes destination scratch base) ambient none
  change (MachineComposition.advance (TM2.step program))^[
      (firstTapes destination scratch base source).length + 1]
    (some ⟨some secondLabel, (ambient, none), firstTapes destination scratch base⟩) = _
      at second
  rw [firstSource, firstScratch] at second
  simp only [List.map_id] at second
  change (MachineComposition.advance (TM2.step program))^[(base source).length + 1]
    (some ⟨some secondLabel, (ambient, none), firstTapes destination scratch base⟩) =
      some ⟨some thirdLabel, (ambient, none), secondTapes source destination scratch base⟩
        at second
  have secondScratch : secondTapes source destination scratch base scratch =
      (base source).reverse ++ (base destination).reverse := by
    simp [secondTapes]
  have secondDestination : secondTapes source destination scratch base destination = [] := by
    simp [secondTapes, firstTapes, Reduction.MachineTransfer.tapesAt,
      Ne.symm sourceDestination, destinationScratch]
  have third := Reduction.MachineTransfer.transferAt_fromTapes scratch destination
    (Ne.symm destinationScratch) id fallback thirdLabel exit program atThird
    (secondTapes source destination scratch base) ambient none
  change (MachineComposition.advance (TM2.step program))^[
      (secondTapes source destination scratch base scratch).length + 1]
    (some ⟨some thirdLabel, (ambient, none), secondTapes source destination scratch base⟩) = _
      at third
  rw [secondScratch, secondDestination] at third
  simp only [List.map_id, List.reverse_append, List.reverse_reverse, List.append_nil,
    List.length_append, List.length_reverse] at third
  rw [finalTapes_eq source destination scratch sourceDestination sourceScratch
    destinationScratch base scratchEmpty] at third
  rw [show 2 * ((base source).length + (base destination).length) + 3 =
      ((base source).length + (base destination).length + 1) +
        (((base source).length + 1) + ((base destination).length + 1)) by omega]
  rw [Function.iterate_add_apply]
  rw [Function.iterate_add_apply (m := (base source).length + 1)
    (n := (base destination).length + 1), first, second]
  exact third
140
def appendInTime (source destination scratch : K)
    (sourceDestination : source ≠ destination) (sourceScratch : source ≠ scratch)
    (destinationScratch : destination ≠ scratch)
    (fallback : β) (firstLabel secondLabel thirdLabel : Λ) (exit : Option Λ)
    (program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β))
    (atFirst : program firstLabel = Reduction.MachineTransfer.loopAt destination scratch
      id fallback firstLabel (some secondLabel))
    (atSecond : program secondLabel = Reduction.MachineTransfer.loopAt source scratch
      id fallback secondLabel (some thirdLabel))
    (atThird : program thirdLabel = Reduction.MachineTransfer.loopAt scratch destination
      id fallback thirdLabel exit)
    (base : K → List β) (scratchEmpty : base scratch = [])
    (ambient : σ) (register : Option β) :
    StateTransition.EvalsToInTime (TM2.step program)
      ⟨some firstLabel, (ambient, register), base⟩
      (some ⟨exit, (ambient, none), appendTapes source destination base⟩)
      (2 * ((base source).length + (base destination).length) + 3) where
body · 5 lines
  steps := 2 * ((base source).length + (base destination).length) + 3
  evals_in_steps := appendTrace source destination scratch sourceDestination sourceScratch
    destinationScratch fallback firstLabel secondLabel thirdLabel exit program atFirst
    atSecond atThird base scratchEmpty ambient register
  steps_le_m := Nat.le_refl _