cfahlgren1's picture
cfahlgren1 HF Staff
mirror at 11b6c7e3c927df2658f513b6b68ea7f8929c62c5
e639140 verified
Raw History Blame Contribute Delete
7.69 kB

The clause written in a block of k slots of w bits.

37
@[expose] public def clauseDec (k w : ℕ) (b : List Bool) : List (ℕ × Bool) :=
body · 1 line
  (List.range k).filterMap fun j => slotDec (ofWord (block w j b))

The number of variables and the clauses written in a bit string, k slots per clause.

41
@[expose] public def decK (k : ℕ) (x : List Bool) : ℕ × NCNF :=
body · 4 lines
  let n := (x.takeWhile id).length
  let body := (x.dropWhile id).tail
  let w := width n
  (n, ((List.range body.length).take (body.length / (k * w))).map fun c => clauseDec k w (block (k * w) c body))

A clause of at most k literals is determined by its entries at the places 0 to k − 1.

50
theorem eq_of_slots {k n : ℕ} {C D : List (Lit n)} (hC : C.length ≤ k) (hD : D.length ≤ k)
    (h : ∀ j < k, C[j]? = D[j]?) : C = D :=
proof · 4 lines
  List.ext_getElem? fun j => by
    by_cases hj : j < k
    · exact h j hj
    · rw [List.getElem?_eq_none (by omega), List.getElem?_eq_none (by omega)]

An instance on n variables has at most (2n+1)^k clauses, so its code is polynomially long.

58
public theorem IsKCNF.length_le {k n : ℕ} {φ : CNF n} (h : IsKCNF k φ) : φ.length ≤ (2 * n + 1) ^ k := by
proof · 3 lines
  have := (h.2.map_on (f := fun C (j : Fin k) => C[(j : ℕ)]?) fun C hC D hD e =>
    eq_of_slots (h.1 C hC) (h.1 D hD) fun j hj => congrFun e ⟨j, hj⟩).length_le_card
  simpa [mul_comm] using this

Reading the first k places of a list one by one gives its first k members.

66
theorem filterMap_range_getElem? {α : Type} (l : List α) (k : ℕ) : (List.range k).filterMap (fun j => l[j]?) = l.take k := by
proof · 5 lines
  induction k with
  | zero => simp
  | succ k ih =>
    rw [List.range_succ, List.filterMap_append, ih, List.take_add_one]
    congr 1

The bits of clause C in the code.

74
def blk (k n : ℕ) (C : List (Lit n)) : List Bool :=
body · 1 line
  (List.range k).flatMap fun j => (List.range ((Nat.log2 (2 * n) + 1))).map (slot C[j]?).testBit

The bits of a clause are the words of its k slots, laid end to end.

78
theorem blk_eq (k n : ℕ) (C : List (Lit n)) : blk k n C =
    ((List.range k).map fun j => toWord (Nat.log2 (2 * n) + 1) (slot C[j]?)).flatten := by
proof · 1 line
  simp only [blk, toWord_eq, List.flatMap_def]

Every clause takes k·(⌊log₂ (2n)⌋ + 1) bits.

83
theorem length_blk (k n : ℕ) (C : List (Lit n)) : (blk k n C).length = k * (Nat.log2 (2 * n) + 1) := by
proof · 1 line
  simp [blk]

Reading the bits of a clause of at most k literals gives the clause back, with its variables as natural numbers.

88
theorem clauseDec_blk {k n : ℕ} {C : List (Lit n)} (hC : C.length ≤ k) :
    clauseDec k (Nat.log2 (2 * n) + 1) (blk k n C) = C.map fun l => (l.1.val, l.2) := by
proof · 6 lines
  have key : ∀ j ∈ List.range k, slotDec (ofWord (block (Nat.log2 (2 * n) + 1) j (blk k n C))) =
      (C.map fun l => (l.1.val, l.2))[j]? := fun j hj => by
    have hj : j < k := List.mem_range.1 hj
    rw [blk_eq, block_flatten (fun l hl => by obtain ⟨_, _, rfl⟩ := List.mem_map.1 hl; simp) j (by simpa using hj)]
    simp only [List.getElem_map, List.getElem_range, ofWord_toWord (slot_lt _), slotDec_slot, List.getElem?_map]
  rw [clauseDec, List.filterMap_congr key, filterMap_range_getElem?, List.take_of_length_le (by simpa using hC)]

The code is the header 1ⁿ 0 followed by the bits of the clauses.

98
theorem enc_eq (k : ℕ) {n : ℕ} (φ : CNF n) : enc k φ = List.replicate n true ++ false :: (φ.map (blk k n)).flatten := by
proof · 2 lines
  rw [enc, List.flatMap_def]
  rfl

The number of bits after the header.

103
theorem length_body (k : ℕ) {n : ℕ} (φ : CNF n) :
    (φ.map (blk k n)).flatten.length = φ.length * (k * (Nat.log2 (2 * n) + 1)) := by
proof · 3 lines
  induction φ with
  | nil => simp
  | cons C φ ih => simp only [List.map_cons, List.flatten_cons, List.length_append, ih, length_blk, List.length_cons]; ring

For k ≥ 1, decoding the code of a formula with at most k literals in every clause gives back n and the formula, with its variables as natural numbers.

112
public theorem decK_enc {k n : ℕ} (hk : 1 ≤ k) {φ : CNF n} (hφ : ∀ C ∈ φ, C.length ≤ k) :
    decK k (enc k φ) = (n, toN φ) := by
proof · 10 lines
  have hB : 0 < k * (Nat.log2 (2 * n) + 1) := Nat.mul_pos hk (Nat.succ_pos _)
  simp only [decK, enc_eq, takeWhile_header, dropWhile_header, List.length_replicate, List.tail_cons, width_eq,
    length_body, Nat.mul_div_cancel _ hB, List.take_range, toN]
  congr 1
  rw [min_eq_left (Nat.le_mul_of_pos_right _ hB)]
  refine List.ext_getElem (by simp) fun c h1 h2 => ?_
  have hc : c < φ.length := by simpa using h1
  simp only [List.getElem_map, List.getElem_range]
  rw [block_flatten (fun l hl => by obtain ⟨C, _, rfl⟩ := List.mem_map.1 hl; exact length_blk k n C) c (by simpa using hc),
    List.getElem_map, clauseDec_blk (hφ _ (List.getElem_mem hc))]

The exact length of the code: the header, then k slots for every clause.

128
public theorem length_enc (k : ℕ) {n : ℕ} (φ : CNF n) :
    (enc k φ).length = n + 1 + φ.length * (k * (Nat.log2 (2 * n) + 1)) := by
proof · 1 line
  simp only [enc_eq, List.length_append, List.length_replicate, List.length_cons, length_body]; omega

The code of an instance of k-SAT has at most n + 1 + k·(⌊log₂ (2n)⌋ + 1)·(2n+1)^k bits.

133
public theorem length_enc_le {k n : ℕ} {φ : CNF n} (h : IsKCNF k φ) :
    (enc k φ).length ≤ n + 1 + k * (Nat.log2 (2 * n) + 1) * (2 * n + 1) ^ k := by
proof · 2 lines
  rw [length_enc, Nat.mul_comm φ.length]
  exact Nat.add_le_add_left (Nat.mul_le_mul_left _ (IsKCNF.length_le h)) _

The length of the code of an instance of k-SAT, plus one, is at most (k·2^k + 1)·(n+2)^(k+1): polynomial in n for fixed k.

140
public theorem length_enc_succ_le {k n : ℕ} {φ : CNF n} (h : IsKCNF k φ) :
    (enc k φ).length + 1 ≤ (k * 2 ^ k + 1) * (n + 2) ^ (k + 1) := by
proof · 7 lines
  have h1 := length_enc_le h
  have h2 : k * (Nat.log2 (2 * n) + 1) * (2 * n + 1) ^ k ≤ k * (n + 2) * (2 * (n + 2)) ^ k :=
    Nat.mul_le_mul (Nat.mul_le_mul_left _ ((bitLen_le n).trans (by omega))) (Nat.pow_le_pow_left (by omega) k)
  have h3 : k * (n + 2) * (2 * (n + 2)) ^ k = k * 2 ^ k * (n + 2) ^ (k + 1) := by rw [mul_pow]; ring
  have h4 : n + 2 ≤ (n + 2) ^ (k + 1) := Nat.le_self_pow (by omega) _
  rw [Nat.add_mul, Nat.one_mul]
  omega