File size: 4,490 Bytes
4a56cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Algorithms.SubexponentialSAT.BinaryArithmetic
import Mathlib.Data.Nat.Digits.Lemmas
import Mathlib.Data.Nat.Size

/-! Canonical count words and the conversion from encoded length to declared input size. -/

namespace OAI.Algorithms.SubexponentialSAT.SqrtLogSpace

private theorem digits_two_eq_map_toNat (a : β„•) :
    Nat.digits 2 a = a.bits.map Bool.toNat := by
  rw [Nat.digits_two_eq_bits]
  induction a.bits with
  | nil => rfl
  | cons b bits ih => cases b <;> simp_all

/-- Least-significant-bit-first binary, retaining one false bit for zero. -/
def countWord (a : β„•) : List Bool := if a = 0 then [false] else a.bits

@[simp] theorem countWord_zero : countWord 0 = [false] := rfl

@[simp] theorem countWord_decode (a : β„•) :
    Nat.ofDigits 2 ((countWord a).map Bool.toNat) = a := by
  by_cases h : a = 0
  Β· subst a
    rfl
  Β· rw [countWord, ite_eq_right h, ← digits_two_eq_map_toNat]
    exact Nat.ofDigits_digits 2 a

@[simp] theorem countWord_toNat (a : β„•) : BinaryArithmetic.toNat (countWord a) = a := by
  have h (word : List Bool) :
      BinaryArithmetic.toNat word = Nat.ofDigits 2 (word.map Bool.toNat) := by
    induction word with
    | nil => rfl
    | cons b word ih =>
        cases b <;> simp [BinaryArithmetic.toNat, BinaryArithmetic.bitVal, ih,
          Nat.ofDigits]
  rw [h, countWord_decode]

theorem powerWord_countWord (n : β„•) :
    List.replicate n false ++ [true] = countWord (2 ^ n) := by
  rw [countWord, ite_eq_right (pow_ne_zero _ (by decide : (2 : β„•) β‰  0))]
  induction n with
  | zero => simp
  | succ n ih =>
      rw [pow_succ', Nat.bit0_bits _ (pow_ne_zero _ (by decide : (2 : β„•) β‰  0))]
      simpa [List.replicate_succ] using congrArg (List.cons false) ih

theorem countWord_length_le {a n : β„•} (ha : a ≀ 2 ^ n) :
    (countWord a).length ≀ n + 1 := by
  by_cases h : a = 0
  Β· simp [countWord, h]
  Β· simpa only [countWord, ite_eq_right h, Nat.size_eq_bits_len, Nat.size_pow] using
      Nat.size_le_size ha

theorem countWord_count_length {n : β„•} (F : SAT.CNF (Fin n)) :
    (countWord (SAT.count F)).length ≀ n + 1 :=
  countWord_length_le (by simpa using SAT.count_le F)

@[simp] theorem count_empty (n : β„•) : SAT.count (βˆ… : SAT.CNF (Fin n)) = 2 ^ n := by
  simp [SAT.count, SAT.satisfyingAssignments, SAT.Assignment]

theorem count_zero_variables (F : SAT.CNF (Fin 0)) :
    SAT.count F = if F = βˆ… then 1 else 0 := by
  by_cases hF : F = βˆ…
  Β· simp [hF]
  Β· rw [ite_eq_right hF]
    obtain ⟨C, hC⟩ := Finset.nonempty_iff_ne_empty.mpr hF
    have hCempty : C = βˆ… := by
      apply Finset.eq_empty_iff_forall_notMem.mpr
      intro literal _
      exact Fin.elim0 literal.variable
    exact (SAT.count_eq_zero_iff F).mpr
      (SAT.not_satisfiable_of_empty_mem (hCempty β–Έ hC))

theorem encoding_le_declaredSize_sq {n k : β„•} (F : SAT.CNF (Fin n))
    (hw : SAT.WidthAtMost k F) :
    Encoding.formulaBitLength F + 1 ≀ (8 * k + 7) * (n + F.card + 1) ^ 2 := by
  let N := n + F.card + 1
  have hN : N ≀ N ^ 2 := Nat.le_self_pow (by decide) N
  have hsmall : n + 3 + 4 * F.card ≀ 7 * N ^ 2 := by
    have : n + 3 + 4 * F.card ≀ 7 * N := by dsimp [N]; omega
    exact this.trans (Nat.mul_le_mul_left 7 hN)
  have hproduct : (n + 1) * F.card ≀ N ^ 2 := by
    simpa [pow_two] using Nat.mul_le_mul
      (show n + 1 ≀ N by dsimp [N]; omega)
      (show F.card ≀ N by dsimp [N]; omega)
  calc
    Encoding.formulaBitLength F + 1 ≀ n + 3 + (4 + (8 * n + 8) * k) * F.card := by
      have h := Encoding.formulaBitLength_upper F hw
      omega
    _ = (n + 3 + 4 * F.card) + 8 * k * ((n + 1) * F.card) := by ring
    _ ≀ 7 * N ^ 2 + 8 * k * N ^ 2 :=
      Nat.add_le_add hsmall (Nat.mul_le_mul_left (8 * k) hproduct)
    _ = (8 * k + 7) * N ^ 2 := by ring

theorem encoding_pow_le_declaredSize {n k : β„•} (F : SAT.CNF (Fin n))
    (hw : SAT.WidthAtMost k F) (d : β„•) :
    ((Encoding.formulaBitLength F : ℝ) + 1) ^ d ≀
      ((8 * k + 7 : β„•) : ℝ) ^ d * ((n + F.card + 1 : β„•) : ℝ) ^ (2 * d) := by
  have h : (Encoding.formulaBitLength F : ℝ) + 1 ≀
      ((8 * k + 7 : β„•) : ℝ) * ((n + F.card + 1 : β„•) : ℝ) ^ 2 := by
    exact_mod_cast encoding_le_declaredSize_sq F hw
  calc
    ((Encoding.formulaBitLength F : ℝ) + 1) ^ d ≀
        (((8 * k + 7 : β„•) : ℝ) * ((n + F.card + 1 : β„•) : ℝ) ^ 2) ^ d :=
      pow_le_pow_leftβ‚€ (by positivity) h d
    _ = _ := by rw [mul_pow, ← pow_mul]

end OAI.Algorithms.SubexponentialSAT.SqrtLogSpace