FormalConjecturesForMathlib/Computability/BitstringEncoding.lean
Explanation legend Lean4 Domain Explanation Language (L4DEL)
L4DEL explanations are a separate layer. They do not change the Lean source. Intuition is explanatory prose; numbered items are a reading path; sources link the wording back to evidence.
- L4DEL
- Lean4 Domain Explanation Language
Credit: The upstream authors and contributors retain credit for the project and mathematics. lean4-explainer only renders their pinned source and adds the separately disclosed explanation layer.
Project sources: Pinned project documentation, Formal Conjectures paper, arXiv v1
24 declarations, 0 contain sorry, 0 declared as axioms
Bitstring encodings
This section provides a typeclass-inferrable version of
Mathlib's Computability.Encoding, specialized to the alphabet Bool.
Making it a class makes it easier to quickly ask if a function is computable in polynomial time,
without having to explicitly pass around the encoding (See IsPolyTime).
We set up instances for common types like Bool, ℕ, ℤ, ℚ,
and instance derivations for Prod and List types,
so that we obtain instances for many common types appearing in algorithms and complexity theory.
While different references may choose different encodings, generally our encodings should be
polytime-transcodable with any other reasonable binary encoding for a given type.
Thus, while it may not be obvious without further examination
which of several essentially equivalent encodings of a type is being used,
we can at least be sure that for functions between types with BitstringEncoding instances,
formalizations of questions of polynomial-time computability will capture the intended meaning.
Imported by 2 files
- class
BitstringEncodingA canonical encoding of a type as bitstrings (List Bool) line 52 sourceSource documentation
A canonical encoding of a type as bitstrings (
List Bool).This is a class version of Mathlib's
Computability.Encoding, specialized to the alphabetBool.no axioms what this means
class BitstringEncoding α extends Computability.Encoding α Bool namespace BitstringEncoding variable {α β : Type*} /-- The encoding function of the canonical `BitstringEncoding` of `α`. -/ - def
BitstringEncoding.bitEncodeThe encoding function of the canonicalBitstringEncodingofαline 59 sourceSource documentation
The encoding function of the canonical
BitstringEncodingofα.no axioms what this means
Statement uses: BitstringEncoding,
List,BoolProof uses:
Computability.Encoding.encode,BitstringEncoding.toEncodingdef bitEncode [BitstringEncoding α] (a : α) : List Bool := toEncoding.encode a
Mentions: BitstringEncoding
- def
BitstringEncoding.bitDecodeThe decoding function of the canonicalBitstringEncodingofαline 62 sourceSource documentation
The decoding function of the canonical
BitstringEncodingofα.no axioms what this means
Statement uses: BitstringEncoding,
List,Bool,OptionProof uses:
Computability.Encoding.decode,BitstringEncoding.toEncodingdef bitDecode [BitstringEncoding α] (l : List Bool) : Option α := toEncoding.decode l …
Mentions: BitstringEncoding
- theorem
BitstringEncoding.bitDecode_bitEncodeDecoding is a left inverse of encoding line 66 sourceSource documentation
Decoding is a left inverse of encoding.
no axioms what this means
Statement uses: BitstringEncoding,
Eq,Option, BitstringEncoding.bitDecode, BitstringEncoding.bitEncode,Option.someProof uses:
Computability.Encoding.decode_encode,Bool,BitstringEncoding.toEncodingtheorem bitDecode_bitEncode [BitstringEncoding α] (a : α) : bitDecode (bitEncode a) = some a := …
Mentions: BitstringEncoding, BitstringEncoding.bitDecode, BitstringEncoding.bitEncode
- theorem
BitstringEncoding.bitEncode_injectiveline 69 sourcestandard axioms: propext what this means
Statement uses: BitstringEncoding,
Function.Injective,List,Bool, BitstringEncoding.bitEncodeProof uses:
Computability.Encoding.encode_injective,BitstringEncoding.toEncoding - def
BitstringEncoding.ofLeftInverseTransport aBitstringEncodingalong an injectionfwith partial inversegline 75 sourceSource documentation
Transport a
BitstringEncodingalong an injectionfwith partial inverseg.standard axioms: Quot.sound, propext what this means
Statement uses: BitstringEncoding,
Option,Eq,Option.someProof uses:
BitstringEncoding.mk,Computability.Encoding.mk,Bool, BitstringEncoding.bitEncode,List,Option.bind, BitstringEncoding.bitDecode1 instances, coercions and auxiliary constants left out.
Tactic trace: 1 steps
Open to load the checked before-and-after goal states.
def ofLeftInverse [BitstringEncoding β] (f : α → β) (g : β → Option α) (h : ∀ x, g (f x) = some x) : BitstringEncoding α where …Mentions: BitstringEncoding
- instance
BitstringEncoding.(instance at line 85)line 85 sourceSource documentation
ℕis encoded by its (little-endian) binary representation, as inComputability.encodeNat.instance : BitstringEncoding ℕ where …
Mentions: BitstringEncoding
- instance
BitstringEncoding.(instance at line 91)line 91 sourceSource documentation
Boolis encoded as a singleton bitstring.instance : BitstringEncoding Bool where …
Mentions: BitstringEncoding
- def
BitstringEncoding.delimitMake a bitstring self-delimiting: each payload bitbbecomes the two bits[true, b], and the block is terminated byfalseline 107 sourceSource documentation
Make a bitstring self-delimiting: each payload bit
bbecomes the two bits[true, b], and the block is terminated byfalse.no axioms what this means
Statement uses:
List,Bool2 instances, coercions and auxiliary constants left out.
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
BitstringEncoding.undelimitParse one self-delimiting block from the front of the input, returning the payload and the remaining input line 113 sourceSource documentation
Parse one self-delimiting block from the front of the input, returning the payload and the remaining input.
standard axioms: propext what this means
Statement uses:
List,Bool,Option,Prod2 instances, coercions and auxiliary constants left out.
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
BitstringEncoding.undelimit_delimitline 119 sourcestandard axioms: propext what this means
Statement uses:
List,Bool,Eq,Option,Prod, BitstringEncoding.undelimit,HAppend.hAppend, BitstringEncoding.delimit,Option.some,Prod.mkProof uses:
List.rec,Eq.refl,List.nil,of_eq_true,List.cons,Eq.trans,True,congrFun',congrArg,Bool.true,Option.map,Prod.fst,Prod.snd,BitstringEncoding.undelimit.eq_2,eq_self4 instances, coercions and auxiliary constants left out.
Tactic trace: 3 steps
Open to load the checked before-and-after goal states.
- theorem
BitstringEncoding.length_delimitline 126 sourcestandard axioms: Quot.sound, propext what this means
Statement uses:
List,Bool,Eq,Nat,List.length, BitstringEncoding.delimit,HAdd.hAdd,HMul.hMul,OfNat.ofNatProof uses:
List.rec,Eq.refl,List.nil,Eq.mpr,List.cons,id,Eq.trans,congrFun',congrArg,Decidable.byContradiction,Not13 instances, coercions and auxiliary constants left out.
Tactic trace: 4 steps
Open to load the checked before-and-after goal states.
- def
BitstringEncoding.undelimitBlocksAuxParse a sequence of self-delimiting blocks, usingfuelto bound the number of blocks line 135 sourceSource documentation
Parse a sequence of self-delimiting blocks, using
fuelto bound the number of blocks.This is the auxiliary, fuel-carrying implementation of
undelimitBlocks; since every block is nonempty,input.lengthis always enough fuel.standard axioms: propext what this means
Statement uses:
Nat,List,Bool,Option2 instances, coercions and auxiliary constants left out.
def undelimitBlocksAux : ℕ → List Bool → Option (List (List Bool)) | _, [] => some [] | 0, _ :: _ => none | fuel + 1, input => -- `p.1` is the parsed block and `p.2` the remaining input; using projections rather than a -- pattern-matching lambda keeps the body free of matchers. (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. Since every block is nonempty, `input.length` bounds the number of blocks, so it always suffices as fuel for `undelimitBlocksAux`. -/Mentions: BitstringEncoding.undelimit
- def
BitstringEncoding.undelimitBlocksParse a sequence of self-delimiting blocks off the front of the input line 147 sourceSource documentation
Parse a sequence of self-delimiting blocks off the front of the input.
Since every block is nonempty,
input.lengthbounds the number of blocks, so it always suffices as fuel forundelimitBlocksAux.standard axioms: propext what this means
Statement uses:
List,Bool,OptionProof uses: BitstringEncoding.undelimitBlocksAux,
List.lengthdef undelimitBlocks (input : List Bool) : Option (List (List Bool)) := …
- theorem
BitstringEncoding.length_le_length_flatten_delimitline 150 sourcestandard axioms: Quot.sound, propext what this means
Statement uses:
List,Bool,LE.le,Nat,List.length,List.flatten,List.map, BitstringEncoding.delimitProof uses:
List.rec,of_eq_true,OfNat.ofNat,List.nil,Eq.trans,True,congrArg,List.map_nil,Eq.mpr,List.cons,HAdd.hAdd,HMul.hMul,id,Eq,HAppend.hAppend,List.map_cons,List.length_append,congrFun', BitstringEncoding.length_delimit,Decidable.byContradictionand 1 more14 instances, coercions and auxiliary constants left out.
Tactic trace: 4 steps
Open to load the checked before-and-after goal states.
- theorem
BitstringEncoding.undelimitBlocksAux_flatten_delimitline 159 source - theorem
BitstringEncoding.undelimitBlocks_flatten_delimitline 167 sourcestandard axioms: Classical.choice, Quot.sound, propext what this means
Statement uses:
List,Bool,Eq,Option, BitstringEncoding.undelimitBlocks,List.flatten,List.map, BitstringEncoding.delimit,Option.someProof uses:
List.length, BitstringEncoding.length_le_length_flatten_delimit1 instances, coercions and auxiliary constants left out.
- theorem
BitstringEncoding.mapM_bitDecode_map_bitEncodeline 172 sourcestandard axioms: Quot.sound, propext what this means
Statement uses: BitstringEncoding,
List,Eq,Option,List.mapM,Bool, BitstringEncoding.bitDecode,List.map, BitstringEncoding.bitEncode,Option.someProof uses:
List.rec,Eq.refl,List.nil,of_eq_true,List.cons,Eq.trans,True,congrFun',congrArg,Bind.bind,Pure.pure,List.map_cons,List.mapM_cons,congr, BitstringEncoding.bitDecode_bitEncode,funext,eq_self6 instances, coercions and auxiliary constants left out.
Tactic trace: 6 steps
Open to load the checked before-and-after goal states.
- instance
BitstringEncoding.(instance at line 182)line 182 sourceSource documentation
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 …
Mentions: BitstringEncoding
- instance
BitstringEncoding.(instance at line 194)line 194 sourceSource documentation
A list is encoded as the concatenation of self-delimiting blocks for its elements.
instance [BitstringEncoding α] : BitstringEncoding (List α) where …
Mentions: BitstringEncoding
- def
BitstringEncoding.ofSubtypeA subtype inherits the encoding of the ambient type; decoding additionally checks the defining predicate line 204 sourceSource documentation
A subtype inherits the encoding of the ambient type; decoding additionally checks the defining predicate.
standard axioms: Quot.sound, propext what this means
Statement uses: BitstringEncoding,
DecidablePred,SubtypeProof uses:
BitstringEncoding.mk,Computability.Encoding.mk,Bool, BitstringEncoding.bitEncode,List,Option.bind, BitstringEncoding.bitDecode,dite,Option,Option.some,Subtype.mk,Not,Option.none2 instances, coercions and auxiliary constants left out.
Tactic trace: 4 steps
Open to load the checked before-and-after goal states.
def ofSubtype {p : α → Prop} [BitstringEncoding α] [DecidablePred p] : BitstringEncoding (Subtype p) where …Mentions: BitstringEncoding
- instance
BitstringEncoding.(instance at line 211)line 211 sourceSource documentation
ℕ+is encoded as the subtype{n : ℕ // 0 < n}it is defined to be.instance : BitstringEncoding ℕ+ := …
Mentions: BitstringEncoding
- instance
BitstringEncoding.(instance at line 215)line 215 sourceSource documentation
ℤis encoded via the pair(n.toNat, (-n).toNat)(one component is always0).instance : BitstringEncoding ℤ := …
Mentions: BitstringEncoding
- instance
BitstringEncoding.(instance at line 220)line 220 sourceSource documentation
ℚis encoded as its (reduced) numerator-denominator pair.instance : BitstringEncoding ℚ := …
Mentions: BitstringEncoding