cfahlgren1's picture
download
raw
20.9 kB
import Mathlib.Tactic.Linarith
import OAI.Computability.BinPacking.PCP.BinaryOccurrenceRename
namespace OAI
namespace BinPackingCompleteness.BinaryNameSearch
open Turing
open BinPackingGames.Foundations.Complexity
open MachineComposition
open BinPackingGames.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 [DecidableEq K]
(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 (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 (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 {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]
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]
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
have hd : tape 0 ≠ tape 5 := fun equal => (by decide : (0 : Fin 6) ≠ 5) (distinct equal)
have reorder (count : List Bool) :
tapesAt (tape 0) (tape 5) base input count =
tapesAt (tape 5) (tape 0) base count input := by
simpa only [tapesAt] using
(Function.update_comm (β := fun _ : K => List Bool) hd input count base)
change some (TM2.stepAux (program (labels .increment)) _ _) = _
rw [atLabels .increment]
simp [instruction, TM2.stepAux, reorder, hd.symm, update_source]
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 _ _ (hd 0 5 (by decide)) base _ counter suffix] at second
exact joinTrace 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 distinct 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 first (joinTrace 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
simp only [List.idxOf_cons, beq_false_of_ne same, Bool.false_eq_true, ite_false]
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 BinPackingCompleteness.BinaryNameSearch
namespace BinPackingCompleteness.BinaryRenameWords
open BinaryFormula BinPackingGames.Foundations
def clauseLiterals (c : Clause) : List Literal := [(c)[0], (c)[1], (c)[2]]
def literals (clauses : List Clause) : List Literal := clauses.flatMap clauseLiterals
def token (literal : Literal) : BinaryNameSearch.Token :=
(literal.positive, literal.name.bits)
def tokens (clauses : List Clause) : List BinaryNameSearch.Token :=
(literals clauses).map token
@[simp] theorem literals_nil : literals [] = [] := rfl
@[simp] theorem literals_cons (c : Clause) (cs : List Clause) :
literals (c :: cs) = (c)[0] :: (c)[1] :: (c)[2] :: literals cs := rfl
@[simp] theorem literals_length (cs : List Clause) :
(literals cs).length = 3 * cs.length := by
induction cs with
| nil => rfl
| cons c cs ih => simp only [literals_cons, List.length_cons, ih]; omega
@[simp] theorem tokens_length (cs : List Clause) :
(tokens cs).length = 3 * cs.length := by simp [tokens]
theorem literals_names (F : Formula) :
(literals F.clauses).map Literal.name = sourceNames F := by
simp only [literals, sourceNames, List.map_flatMap]
rfl
theorem tokens_payloads (F : Formula) :
BinaryNameSearch.payloads (tokens F.clauses) = (sourceNames F).map Nat.bits := by
rw [← literals_names]
simp only [BinaryNameSearch.payloads, tokens, List.map_map, token, Function.comp_def]
theorem token_bits (literal : Literal) :
BinaryNameSearch.tokenBits (token literal) = BinaryEncoding.literalBits literal := by
simp only [BinaryNameSearch.tokenBits, token, BinaryEncoding.literalBits,
BinaryEncoding.nameBits, BinaryParsing.frame_eq]
theorem tokens_stream (cs : List Clause) :
BinaryNameSearch.stream (tokens cs) = BinaryTokenMachine.tokens cs := by
induction cs with
| nil => rfl
| cons c cs ih =>
simp only [tokens, literals_cons, List.map_cons, BinaryNameSearch.stream_cons,
token_bits, BinaryTokenMachine.tokens_cons, BinaryEncoding.clauseBits]
simpa only [tokens, List.append_assoc] using
congrArg (fun tail => BinaryEncoding.literalBits (c)[0] ++
BinaryEncoding.literalBits (c)[1] ++ BinaryEncoding.literalBits (c)[2] ++ tail) ih
theorem tokens_canonical (cs : List Clause) :
∀ t ∈ tokens cs, BinaryNameMachine.canonical t.2 = true := by
intro t mem
obtain ⟨literal, _, rfl⟩ := List.mem_map.mp mem
exact BinaryParsing.canonical_nat_bits literal.name
theorem payload_index (F : Formula) (name : Nat) :
(BinaryNameSearch.payloads (tokens F.clauses)).idxOf name.bits =
(sourceNames F).idxOf name := by
rw [tokens_payloads, BinaryOccurrenceRename.idxOf_binary_payloads]
theorem token_payload_mem (cs : List Clause) (literal : Literal)
(mem : literal ∈ literals cs) :
literal.name.bits ∈ BinaryNameSearch.payloads (tokens cs) := by
apply List.mem_map.mpr
refine ⟨token literal, ?_, rfl⟩
exact List.mem_map.mpr ⟨literal, mem, rfl⟩
theorem payloadSize_tokens (cs : List Clause) :
BinaryNameSearch.payloadSize (tokens cs) = BinaryEncoding.namesBitSize cs := by
induction cs with
| nil => rfl
| cons c cs ih =>
simp only [tokens, literals_cons, List.map_cons,
BinaryNameSearch.payloadSize_cons, token, Nat.size_eq_bits_len,
BinaryEncoding.namesBitSize_cons] at *
simpa only [BinaryEncoding.clauseNameSize, BinaryFormula.clauseNames,
List.map_cons, List.map_nil, List.sum_cons, List.sum_nil,
Nat.add_zero, Nat.add_assoc] using congrArg
(fun tail => (c)[0].name.size + ((c)[1].name.size + ((c)[2].name.size + tail))) ih
def outputLiteral (names : List Nat) (literal : Literal) : List Bool :=
Complexity.encodeWord (names.idxOf literal.name) ++
Complexity.encodeWord (if literal.positive then 1 else 0)
def body (F : Formula) : List Bool :=
(literals F.clauses).flatMap (outputLiteral (sourceNames F))
theorem encoded_clause (F : Formula) (c : Clause) (mem : c ∈ F.clauses) :
Complexity.encodeWords (Complexity.clauseWords (BinaryOccurrenceRename.clause F c mem)) =
(clauseLiterals c).flatMap (outputLiteral (sourceNames F)) := by
simp [Complexity.clauseWords, Complexity.literalWords, Complexity.encodeWords,
BinaryOccurrenceRename.clause, BinaryOccurrenceRename.literal,
BinaryOccurrenceRename.nameIndex, clauseLiterals, outputLiteral, List.append_assoc]
rfl
private theorem encodeWords_flatMap {α : Type} (xs : List α) (f : α → List Nat) :
Complexity.encodeWords (xs.flatMap f) =
xs.flatMap (fun x => Complexity.encodeWords (f x)) := by
induction xs with
| nil => rfl
| cons x xs ih => simp only [List.flatMap_cons, Complexity.encodeWords_append, ih]
theorem encoded_body (F : Formula) :
Complexity.encodeWords ((BinaryOccurrenceRename.renamed F).clauses.flatMap
Complexity.clauseWords) = body F := by
rw [encodeWords_flatMap]
change (F.clauses.attach.map (fun c => BinaryOccurrenceRename.clause F c.val c.property)).flatMap
(fun c => Complexity.encodeWords (Complexity.clauseWords c)) = body F
rw [List.flatMap_map]
simp only [encoded_clause]
have h := congrArg (fun cs : List Clause =>
cs.flatMap (fun c => (clauseLiterals c).flatMap (outputLiteral (sourceNames F))))
(List.attach_map_subtype_val F.clauses)
simpa only [List.flatMap_map, body, literals, List.flatMap_assoc] using h
theorem encoded_renamed (F : Formula) :
Complexity.formulaBits (BinaryOccurrenceRename.renamed F) =
Complexity.encodeWord (3 * F.clauses.length) ++
Complexity.encodeWord F.clauses.length ++ body F := by
simp only [Complexity.formulaBits, Complexity.formulaWords, Complexity.encodeWords_append,
Complexity.encodeWords, List.append_nil,
BinaryOccurrenceRename.renamed_variable_count,
BinaryOccurrenceRename.renamed_clause_count, encoded_body, List.append_assoc]
end BinPackingCompleteness.BinaryRenameWords
end OAI

Xet Storage Details

Size:
20.9 kB
·
Xet hash:
0b3d797e7161250d29eef696fc3e630fc5b983b404769e2ee8cba0491372c581

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