Download lean/OAI/Computability/VertexCover/Encoding/BinaryEncoding.lean from roseking/openAI-math-dataset: direct link, hf CLI and curl.
- Browser
- Download file 7.29 kB
-
https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Computability/VertexCover/Encoding/BinaryEncoding.lean
- Command line
-
hf download hf://datasets/roseking/openAI-math-dataset/lean/OAI/Computability/VertexCover/Encoding/BinaryEncoding.lean
-
curl -L -o BinaryEncoding.lean https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Computability/VertexCover/Encoding/BinaryEncoding.lean
7.29 kB
| import Mathlib | |
| import OAI.Computability.VertexCover.Encoding.BinaryFormula | |
| namespace OAI | |
| section | |
| namespace UniqueGames.BinaryEncoding | |
| open BinaryFormula | |
| def bitsValue : List Bool → Nat | |
| | [] => 0 | |
| | b :: bits => Nat.bit b (bitsValue bits) | |
| @[simp] theorem bitsValue_bits (n : Nat) : 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 frame : List Bool → List Bool | |
| | [] => [false] | |
| | b :: bits => true :: b :: frame bits | |
| def parseFrame : List Bool → Option (List Bool × List Bool) | |
| | false :: rest => some ([], rest) | |
| | true :: b :: rest => do | |
| let (bits, trailing) ← parseFrame rest | |
| return (b :: bits, trailing) | |
| | _ => none | |
| @[simp] theorem parseFrame_encoded (bits trailing : List Bool) : | |
| parseFrame (frame bits ++ trailing) = some (bits, trailing) := by | |
| induction bits with | |
| | nil => rfl | |
| | cons b bits ih => simp [frame, parseFrame, ih] | |
| @[simp] theorem frame_length (bits : List Bool) : | |
| (frame bits).length = 2 * bits.length + 1 := by | |
| induction bits with | |
| | nil => rfl | |
| | cons b bits ih => simp [frame, ih]; omega | |
| def nameBits (name : Nat) : List Bool := frame name.bits | |
| def parseName (input : List Bool) : Option (Nat × List Bool) := do | |
| let (digits, rest) ← parseFrame input | |
| let name := bitsValue digits | |
| if digits = name.bits then some (name, rest) else none | |
| @[simp] theorem parseName_encoded (name : Nat) (rest : List Bool) : | |
| parseName (nameBits name ++ rest) = some (name, rest) := by | |
| simp [parseName, nameBits] | |
| @[simp] theorem nameBits_length (name : Nat) : | |
| (nameBits name).length = 2 * name.size + 1 := by | |
| simp [nameBits, Nat.size_eq_bits_len] | |
| theorem nameBits_length_le_of_lt_pow (name width : Nat) (bound : name < 2 ^ width) : | |
| (nameBits name).length ≤ 2 * width + 1 := by | |
| rw [nameBits_length] | |
| have h := Nat.size_le.mpr bound | |
| omega | |
| def literalBits (literal : Literal) : List Bool := | |
| literal.positive :: nameBits literal.name | |
| def parseLiteral : List Bool → Option (Literal × List Bool) | |
| | sign :: input => do | |
| let (name, rest) ← parseName input | |
| return (⟨name, sign⟩, rest) | |
| | [] => none | |
| @[simp] theorem parseLiteral_encoded (literal : Literal) (rest : List Bool) : | |
| parseLiteral (literalBits literal ++ rest) = some (literal, rest) := by | |
| cases literal with | |
| | mk name sign => simp [literalBits, parseLiteral] | |
| @[simp] theorem literalBits_length (literal : Literal) : | |
| (literalBits literal).length = 2 * literal.name.size + 2 := by | |
| simp [literalBits] | |
| def clauseBits (clause : Clause) : List Bool := | |
| literalBits clause[0] ++ literalBits clause[1] ++ literalBits clause[2] | |
| def parseClause (input : List Bool) : Option (Clause × List Bool) := do | |
| let (a, input) ← parseLiteral input | |
| let (b, input) ← parseLiteral input | |
| let (c, rest) ← parseLiteral input | |
| return (#v[a, b, c], rest) | |
| theorem clause_three_entries (clause : Clause) : #v[clause[0], clause[1], clause[2]] = clause := by | |
| apply Vector.ext | |
| intro i hi | |
| have cases_i : i = 0 ∨ i = 1 ∨ i = 2 := by omega | |
| rcases cases_i with rfl | rfl | rfl <;> rfl | |
| @[simp] theorem parseClause_encoded (clause : Clause) (rest : List Bool) : | |
| parseClause (clauseBits clause ++ rest) = some (clause, rest) := by | |
| simp [clauseBits, List.append_assoc, parseClause, clause_three_entries] | |
| def clauseNameSize (clause : Clause) : Nat := | |
| ((clauseNames clause).map Nat.size).sum | |
| @[simp] theorem clauseBits_length (clause : Clause) : | |
| (clauseBits clause).length = 2 * clauseNameSize clause + 6 := by | |
| simp [clauseBits, clauseNameSize, clauseNames] | |
| omega | |
| def clausesBits : List Clause → List Bool | |
| | [] => [false] | |
| | clause :: clauses => true :: (clauseBits clause ++ clausesBits clauses) | |
| def formulaBits (formula : Formula) : List Bool := clausesBits formula.clauses | |
| def namesBitSize (clauses : List Clause) : Nat := | |
| ((clauses.flatMap clauseNames).map Nat.size).sum | |
| @[simp] theorem namesBitSize_nil : namesBitSize [] = 0 := rfl | |
| @[simp] theorem namesBitSize_cons (clause : Clause) (clauses : List Clause) : | |
| namesBitSize (clause :: clauses) = clauseNameSize clause + namesBitSize clauses := by | |
| simp [namesBitSize, clauseNameSize] | |
| theorem clausesBits_length (clauses : List Clause) : | |
| (clausesBits clauses).length = 7 * clauses.length + 2 * namesBitSize clauses + 1 := by | |
| induction clauses with | |
| | nil => rfl | |
| | cons clause clauses ih => | |
| simp only [clausesBits, List.length_cons, List.length_append, clauseBits_length, | |
| namesBitSize_cons, ih] | |
| omega | |
| theorem formulaBits_length (formula : Formula) : | |
| (formulaBits formula).length = | |
| 7 * formula.clauses.length + 2 * namesBitSize formula.clauses + 1 := | |
| clausesBits_length formula.clauses | |
| theorem clauses_length_lt_bits (clauses : List Clause) : | |
| clauses.length < (clausesBits clauses).length := by | |
| rw [clausesBits_length] | |
| omega | |
| def ordinarySize (formula : Formula) : Nat := | |
| 3 * formula.clauses.length + namesBitSize formula.clauses + 1 | |
| theorem ordinarySize_le_bits (formula : Formula) : | |
| ordinarySize formula ≤ (formulaBits formula).length := by | |
| rw [formulaBits_length] | |
| unfold ordinarySize | |
| omega | |
| theorem bits_le_three_ordinarySize (formula : Formula) : | |
| (formulaBits formula).length ≤ 3 * ordinarySize formula := by | |
| rw [formulaBits_length] | |
| unfold ordinarySize | |
| omega | |
| def parseClauses : Nat → List Bool → Option (List Clause × List Bool) | |
| | 0, _ => none | |
| | _fuel + 1, false :: rest => some ([], rest) | |
| | fuel + 1, true :: input => do | |
| let (clause, input) ← parseClause input | |
| let (clauses, rest) ← parseClauses fuel input | |
| return (clause :: clauses, rest) | |
| | _ + 1, [] => none | |
| @[simp] theorem parseClauses_encoded (clauses : List Clause) (rest : List Bool) | |
| (fuel : Nat) (enough : clauses.length < fuel) : | |
| parseClauses fuel (clausesBits clauses ++ rest) = some (clauses, rest) := by | |
| induction clauses generalizing fuel with | |
| | nil => | |
| cases fuel with | |
| | zero => omega | |
| | succ fuel => simp [clausesBits, parseClauses] | |
| | cons clause clauses ih => | |
| cases fuel with | |
| | zero => omega | |
| | succ fuel => | |
| have hrest : clauses.length < fuel := by simp only [List.length_cons] at enough; omega | |
| simp [clausesBits, parseClauses, List.append_assoc, ih fuel hrest] | |
| def decodeFormula (input : List Bool) : Option Formula := do | |
| let (clauses, rest) ← parseClauses (input.length + 1) input | |
| if rest = [] then some ⟨clauses⟩ else none | |
| @[simp] theorem decodeFormula_encoded (formula : Formula) : | |
| decodeFormula (formulaBits formula) = some formula := by | |
| cases formula with | |
| | mk clauses => | |
| have hlen := clauses_length_lt_bits clauses | |
| have parsed := parseClauses_encoded clauses [] ((clausesBits clauses).length + 1) (by omega) | |
| simp only [List.append_nil] at parsed | |
| simp [formulaBits, decodeFormula, parsed] | |
| theorem formulaBits_injective {first second : Formula} | |
| (same : formulaBits first = formulaBits second) : first = second := by | |
| have parsed := congrArg decodeFormula same | |
| simpa only [decodeFormula_encoded, Option.some.injEq] using parsed | |
| end UniqueGames.BinaryEncoding | |
| end | |
| end OAI | |