math / data /lean /OAI /Computability /PerfectCompleteness /Machines /BinaryRenameWords.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
6.22 kB
14
def clauseLiterals (c : Clause) : List Literal := [(c)[0], (c)[1], (c)[2]]
16
def literals (clauses : List Clause) : List Literal := clauses.flatMap clauseLiterals
18
def token (literal : Literal) : BinaryNameSearch.Token :=
body · 1 line
  (literal.positive, literal.name.bits)
21
def tokens (clauses : List Clause) : List BinaryNameSearch.Token :=
body · 1 line
  (literals clauses).map token
24
@[simp] theorem literals_nil : literals [] = [] := rfl
26
@[simp] theorem literals_cons (c : Clause) (cs : List Clause) :
    literals (c :: cs) = (c)[0] :: (c)[1] :: (c)[2] :: literals cs := rfl
29
@[simp] theorem literals_length (cs : List Clause) :
    (literals cs).length = 3 * cs.length := by
proof · 3 lines
  induction cs with
  | nil => rfl
  | cons c cs ih => simp only [literals_cons, List.length_cons, ih]; omega
35
@[simp] theorem tokens_length (cs : List Clause) :
    (tokens cs).length = 3 * cs.length := by simp [tokens]
38
theorem literals_names (F : Formula) :
    (literals F.clauses).map Literal.name = sourceNames F := by
proof · 2 lines
  simp only [literals, sourceNames, List.map_flatMap]
  rfl
43
theorem tokens_payloads (F : Formula) :
    BinaryNameSearch.payloads (tokens F.clauses) = (sourceNames F).map Nat.bits := by
proof · 2 lines
  rw [← literals_names]
  simp only [BinaryNameSearch.payloads, tokens, List.map_map, token, Function.comp_def]
48
theorem token_bits (literal : Literal) :
    BinaryNameSearch.tokenBits (token literal) = BinaryEncoding.literalBits literal := by
proof · 2 lines
  simp only [BinaryNameSearch.tokenBits, token, BinaryEncoding.literalBits,
    BinaryEncoding.nameBits, BinaryParsing.frame_eq]
53
theorem tokens_stream (cs : List Clause) :
    BinaryNameSearch.stream (tokens cs) = BinaryTokenMachine.tokens cs := by
proof · 8 lines
  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
64
theorem tokens_canonical (cs : List Clause) :
    ∀ t ∈ tokens cs, BinaryNameMachine.canonical t.2 = true := by
proof · 3 lines
  intro t mem
  obtain ⟨literal, _, rfl⟩ := List.mem_map.mp mem
  exact BinaryParsing.canonical_nat_bits literal.name
70
theorem payload_index (F : Formula) (name : Nat) :
    (BinaryNameSearch.payloads (tokens F.clauses)).idxOf name.bits =
      (sourceNames F).idxOf name := by
proof · 1 line
  rw [tokens_payloads, BinaryOccurrenceRename.idxOf_binary_payloads]
75
theorem token_payload_mem (cs : List Clause) (literal : Literal)
    (mem : literal ∈ literals cs) :
    literal.name.bits ∈ BinaryNameSearch.payloads (tokens cs) := by
proof · 3 lines
  apply List.mem_map.mpr
  refine ⟨token literal, ?_, rfl⟩
  exact List.mem_map.mpr ⟨literal, mem, rfl⟩
82
theorem payloadSize_tokens (cs : List Clause) :
    BinaryNameSearch.payloadSize (tokens cs) = BinaryEncoding.namesBitSize cs := by
proof · 10 lines
  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
95
def outputLiteral (names : List Nat) (literal : Literal) : List Bool :=
body · 2 lines
  Complexity.encodeWord (names.idxOf literal.name) ++
    Complexity.encodeWord (if literal.positive then 1 else 0)
99
def body (F : Formula) : List Bool :=
body · 1 line
  (literals F.clauses).flatMap (outputLiteral (sourceNames F))
102
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
proof · 4 lines
  simp [Complexity.clauseWords, Complexity.literalWords, Complexity.encodeWords,
    BinaryOccurrenceRename.clause, BinaryOccurrenceRename.literal,
    BinaryOccurrenceRename.nameIndex, clauseLiterals, outputLiteral, List.append_assoc]
  rfl
110
private theorem encodeWords_flatMap {α : Type} (xs : List α) (f : α → List Nat) :
    Complexity.encodeWords (xs.flatMap f) =
      xs.flatMap (fun x => Complexity.encodeWords (f x)) := by
proof · 3 lines
  induction xs with
  | nil => rfl
  | cons x xs ih => simp only [List.flatMap_cons, Complexity.encodeWords_append, ih]
117
theorem encoded_body (F : Formula) :
    Complexity.encodeWords ((BinaryOccurrenceRename.renamed F).clauses.flatMap
      Complexity.clauseWords) = body F := by
proof · 9 lines
  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
130
theorem encoded_renamed (F : Formula) :
    Complexity.formulaBits (BinaryOccurrenceRename.renamed F) =
      Complexity.encodeWord (3 * F.clauses.length) ++
        Complexity.encodeWord F.clauses.length ++ body F := by
proof · 4 lines
  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]