Bitstring encodings
DefinitionPvsNP_bitstring_encodingA class-based version of Mathlib's Computability.Encoding specialised to the alphabet : a bitstring encoding of a type is an injective-by-construction pair of maps and with for all .
Booleans are encoded as one-bit strings. Composite data is encoded with self-delimiting blocks: a bitstring is written as , which doubles its length plus one and lets a parser recover where it ends. A pair is then the self-delimiting block of the first component followed by the encoding of the second, and a list is the concatenation of the self-delimiting blocks of its entries. These instances give canonical encodings for the types used throughout the mission: bitstrings themselves, and pairs of bitstrings (instance–witness pairs).
Any other reasonable binary encoding of these types is interconvertible with this one in polynomial time, so the notion of polynomial-time computability built on top of it is the intended one.
import Mathlib.Computability.Encoding
/-!
# Bitstring encodings
A `class` version of Mathlib's `Computability.Encoding`, specialised to the alphabet `Bool`,
together with the instances needed to talk about polynomial-time computable functions on
bitstrings, pairs of bitstrings and lists.
Adapted from the `FormalConjecturesForMathlib.Computability.BitstringEncoding` file of the
`google-deepmind/formal-conjectures` project (Apache License 2.0).
-/
namespace PvsNP
/-- A canonical encoding of a type as bitstrings (`List Bool`). -/
class BitstringEncoding (α : Type) extends Computability.Encoding α Bool
namespace BitstringEncoding
variable {α β : Type}
/-- The encoding function of the canonical bitstring encoding of `α`. -/
def bitEncode [BitstringEncoding α] (a : α) : List Bool := toEncoding.encode a
/-- The decoding function of the canonical bitstring encoding of `α`. -/
def bitDecode [BitstringEncoding α] (l : List Bool) : Option α := toEncoding.decode l
@[simp]
theorem bitDecode_bitEncode [BitstringEncoding α] (a : α) : bitDecode (bitEncode a) = some a :=
toEncoding.decode_encode a
theorem bitEncode_injective [BitstringEncoding α] :
Function.Injective (bitEncode : α → List Bool) :=
(toEncoding (α := α)).encode_injective
/-- `Bool` is encoded as a singleton bitstring. -/
instance : BitstringEncoding Bool where
encode b := [b]
decode l := match l with
| [b] => some b
| _ => none
decode_encode _ := rfl
/-! ### Self-delimiting blocks -/
/-- Make a bitstring self-delimiting: each payload bit `b` becomes the two bits `[true, b]`,
and the block is terminated by `false`. -/
def delimit : List Bool → List Bool
| [] => [false]
| b :: l => true :: b :: delimit l
/-- Parse one self-delimiting block from the front of the input, returning the payload and
the remaining input. -/
def undelimit : List Bool → Option (List Bool × List Bool)
| false :: rest => some ([], rest)
| true :: b :: input => (undelimit input).map fun p => (b :: p.1, p.2)
| _ => none
@[simp]
theorem undelimit_delimit (l rest : List Bool) :
undelimit (delimit l ++ rest) = some (l, rest) := by
induction l with
| nil => rfl
| cons b l ih => simp [delimit, undelimit, ih]
@[simp]
theorem length_delimit (l : List Bool) : (delimit l).length = 2 * l.length + 1 := by
induction l with
| nil => rfl
| cons b l ih => simp only [delimit, List.length_cons, ih]; omega
/-- Parse a sequence of self-delimiting blocks, using `fuel` to bound the number of blocks. -/
def undelimitBlocksAux : ℕ → List Bool → Option (List (List Bool))
| _, [] => some []
| 0, _ :: _ => none
| fuel + 1, input =>
(undelimit input).bind fun p => (undelimitBlocksAux fuel p.2).map (p.1 :: ·)
/-- Parse a sequence of self-delimiting blocks off the front of the input. -/
def undelimitBlocks (input : List Bool) : Option (List (List Bool)) :=
undelimitBlocksAux input.length input
theorem length_le_length_flatten_delimit (l : List (List Bool)) :
l.length ≤ ((l.map delimit).flatten).length := by
induction l with
| nil => simp
| cons b t ih =>
simp only [List.map_cons, List.flatten_cons, List.length_append, List.length_cons,
length_delimit]
omega
private theorem undelimitBlocksAux_flatten_delimit (l : List (List Bool)) (fuel : ℕ)
(hfuel : l.length ≤ fuel) : undelimitBlocksAux fuel ((l.map delimit).flatten) = some l := by
induction l generalizing fuel with
| nil => cases fuel <;> rfl
| cons b t ih =>
rw [List.length_cons] at hfuel
cases fuel <;> cases b <;> grind [delimit, undelimitBlocksAux, undelimit_delimit]
theorem undelimitBlocks_flatten_delimit (l : List (List Bool)) :
undelimitBlocks ((l.map delimit).flatten) = some l :=
undelimitBlocksAux_flatten_delimit l _ (length_le_length_flatten_delimit l)
@[simp]
theorem mapM_bitDecode_map_bitEncode [BitstringEncoding α] (l : List α) :
(l.map bitEncode).mapM bitDecode = some l := by
induction l with
| nil => rfl
| cons a t ih => simp [ih]
/-! ### Derived instances -/
/-- A pair is encoded as a self-delimiting block for the first component followed by the
encoding of the second. -/
instance [BitstringEncoding α] [BitstringEncoding β] : BitstringEncoding (α × β) where
encode p := delimit (bitEncode p.1) ++ bitEncode p.2
decode input :=
match undelimit input with
| none => none
| some (block, rest) =>
match bitDecode block, bitDecode rest with
| some a, some b => some (a, b)
| _, _ => none
decode_encode p := by simp
/-- A list is encoded as the concatenation of self-delimiting blocks for its elements. -/
instance [BitstringEncoding α] : BitstringEncoding (List α) where
encode l := ((l.map bitEncode).map delimit).flatten
decode input := (undelimitBlocks input).bind (·.mapM bitDecode)
decode_encode l := by
rw [undelimitBlocks_flatten_delimit (l.map bitEncode)]
exact mapM_bitDecode_map_bitEncode l
end BitstringEncoding
end PvsNP
Read-back
What the Lean code literally says, in plain math · aristotle-harmonic
This file declares a type class for a type in the lowest universe. It extends Mathlib's structure , so an instance is the data of an encoding map (a finite list of Booleans), a decoding map , and a proof that for every . Nothing requires to fail on strings that are not encodings; may return an arbitrary value on such strings, and no computability or efficiency condition of any kind is imposed on either map.
Two abbreviations are introduced for an arbitrary instance: is the instance's encoding of , and its decoding of the bitstring . Two consequences are stated and proved: for all , and is injective.
A concrete instance is given for : a Boolean encodes as the one-element list ; decoding returns exactly on one-element lists , and on the empty list and on every list of length at least two.
Next, three auxiliary total functions on bitstrings are defined.
- sends the empty list to and sends to ; that is, each payload bit becomes the two bits , and the block is terminated by a single .
- is a partial parser: on a list beginning with it returns the pair (empty payload, the remaining tail); on a list beginning it recursively parses and, if that succeeds with payload and remainder , returns ; on the empty list and on the one-element list it returns .
- takes a natural-number fuel and an input: on the empty input it returns of the empty list of blocks regardless of fuel; on a nonempty input with fuel it returns ; with fuel it parses one block with and recurses on the remainder with fuel , consing the parsed payload onto the result. is applied to the fuel and .
The following facts about these functions are asserted and proved: for all bitstrings and , ; ; for every list of bitstrings , the number of entries of is at most the length of the concatenation of the delimited blocks of its entries; and applied to that concatenation returns . Also, for any type with a bitstring encoding and any list of its elements, decoding each of the encodings of the entries of in sequence returns .
Finally two derived instances are given. For types with bitstring encodings, a pair encodes as followed by ; decoding parses one delimited block and decodes the block as an and the remainder as a , returning if either step fails. For a type with a bitstring encoding, a list of elements of encodes as the concatenation of the delimited blocks of the encodings of its entries; decoding parses the blocks and decodes each one, returning if any step fails. In both cases the required round-trip property is proved, so the instances are legitimate; no uniqueness, minimality or canonicity of these encodings is claimed, and the empty list and the pair of empty strings are ordinary, non-degenerate cases of them.