File size: 4,771 Bytes
e320e82 | 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | import Mathlib
import OAI.Combinatorics.IndependentSets.Model
namespace OAI
namespace LargeIndependentSets
namespace BinaryParser
def bitsValue : List Bool → ℕ
| [] => 0
| b::bs => Nat.bit b (bitsValue bs)
@[simp] lemma bitsValue_bits (n : ℕ) : bitsValue n.bits = n := by
induction n using Nat.binaryRec' with
| zero => simp [bitsValue]
| bit b n hn ih => rw [Nat.bits_append_bit n b hn]; simp only [bitsValue,ih]
def parseFrame : List Bool → Option (List Bool × List Bool)
| false::rest => some ([],rest)
| true::b::rest => do
let (bs,tail) ← parseFrame rest
return (b::bs,tail)
| _ => none
@[simp] lemma parseFrame_encoded (bs tail : List Bool) :
parseFrame (frame bs ++ tail) = some (bs,tail) := by
induction bs with
| nil => rfl
| cons b bs ih => simp [frame,parseFrame,ih]
def parseName (b : List Bool) : Option (ℕ × List Bool) := do
let (digits,tail) ← parseFrame b
let n := bitsValue digits
if digits = n.bits then some (n,tail) else none
@[simp] lemma parseName_encoded (n : ℕ) (tail : List Bool) :
parseName (nameBits n ++ tail) = some (n,tail) := by
simp [parseName,nameBits]
def parseLiteral : List Bool → Option (Literal × List Bool)
| sign::b => do
let (name,tail) ← parseName b
return (⟨name,sign⟩,tail)
| [] => none
@[simp] lemma parseLiteral_encoded (l : Literal) (tail : List Bool) :
parseLiteral (literalBits l ++ tail) = some (l,tail) := by
cases l
simp [parseLiteral,literalBits]
def parseMany {α : Type} (parse : List Bool → Option (α × List Bool)) :
ℕ → List Bool → Option (List α × List Bool)
| 0,b => some ([],b)
| n+1,b => do
let (a,b) ← parse b
let (as,tail) ← parseMany parse n b
return (a::as,tail)
lemma parseMany_encoded {α : Type} (parse : List Bool → Option (α × List Bool))
(encode : α → List Bool) (P : α → Prop)
(h : ∀ a, P a → ∀ tail, parse (encode a ++ tail) = some (a,tail))
(as : List α) (has : ∀ a ∈ as, P a) (tail : List Bool) :
parseMany parse as.length (as.flatMap encode ++ tail) = some (as,tail) := by
induction as with
| nil => rfl
| cons a as ih =>
simp only [List.length_cons,List.flatMap_cons,List.append_assoc,parseMany,
h a (has a (by simp)),bind,Option.bind]
rw [ih (fun x hx => has x (by simp [hx]))]
rfl
def parseClause (b : List Bool) : Option (List Literal × List Bool) := do
let (n,b) ← parseName b
if n ≤ 3 then parseMany parseLiteral n b else none
lemma parseClause_encoded (C : List Literal) (hC : C.length ≤ 3) (tail : List Bool) :
parseClause (clauseBits C ++ tail) = some (C,tail) := by
simp only [clauseBits,List.append_assoc,parseClause,parseName_encoded,bind,Option.bind,hC,ite_true]
exact parseMany_encoded parseLiteral literalBits (fun _ => True)
(fun l _ => parseLiteral_encoded l) C (by simp) tail
lemma clauses_length_le_bits (Cs : List (List Literal)) :
Cs.length ≤ (Cs.flatMap clauseBits).length := by
have hp (C : List Literal) : 1 ≤ (clauseBits C).length := by
unfold clauseBits nameBits
have h : ∀ b : List Bool, 1 ≤ (frame b).length := by
intro b
cases b <;> simp [frame]
simpa only [List.length_append] using (h C.length.bits).trans (Nat.le_add_right _ _)
induction Cs with
| nil => simp
| cons C Cs ih => simp only [List.length_cons,List.flatMap_cons,List.length_append]; have := hp C; omega
def core (input : List Bool) : Option Formula := do
let (n,b) ← parseName input
if n ≤ b.length then
let (Cs,tail) ← parseMany parseClause n b
if tail = [] then
if h : ∀ C ∈ Cs, C.length ≤ 3 then some ⟨Cs,h⟩ else none
else none
else none
lemma core_encoded (F : Formula) : core (formulaBits F) = some F := by
unfold core formulaBits
rw [parseName_encoded]
simp only [bind,Option.bind,clauses_length_le_bits,ite_true]
have hm := parseMany_encoded parseClause clauseBits (fun C => C.length ≤ 3)
parseClause_encoded F.clauses F.width []
simp only [List.append_nil] at hm
rw [hm]
simp
exact F.width
def decode (input : List Bool) : Formula :=
match core input with
| none => ⟨[],by simp⟩
| some F => if formulaBits F = input then F else ⟨[],by simp⟩
@[simp] theorem decode_encoded (F : Formula) : decode (formulaBits F) = F := by
simp [decode,core_encoded]
theorem decode_bits_bound (input : List Bool) :
(formulaBits (decode input)).length ≤ input.length+1 := by
unfold decode
cases h : core input with
| none => simp [formulaBits,nameBits,frame]
| some F =>
dsimp only
split_ifs with he
· simp only [he]; omega
· simp [formulaBits,nameBits,frame]
end BinaryParser
end LargeIndependentSets
end OAI
|