Default encodings #
Provides default encodings and encoding combinators for basic Lean types such as natural numbers, finite types, products, dependent pairs, sums, options and lists into Boolean words.
Since the complexity of a function depends on the exact encoding used, and thus the encoding is part of the definition of the function, we do not use a class but instead provide encodings merely as a convenience.
The delimit function is used as the basic building block here: It creates a self-delimiting,
prefix-free code by replacing each bit b of a word with [true, b] and appending [false].
The product encoding delimits the first component and appends the second, and lists are encoded by
delimiting each element. Dependent pairs are encoded like products, with the second component
encoded under the encoding selected by the first one. Sums prefix the code of the component with a
tag bit, and options are encoded as the sum Unit ⊕ α. Booleans are their single bit, natural
numbers use the little-endian binary code Nat.bits, and Fin n uses a fixed-width binary code of
Nat.clog 2 n bits, which encFintype transfers to any finite type via Fintype.equivFin.
Subsingletons, including propositions, are encoded as the empty word by encSubsingleton.
Products delimit only their first component because the second one extends to the end of the code word, so a delimiter would add nothing for parsing; this keeps the second component undoubled and the size of right-nested products linear. Lists delimit every element since their number is not known in advance, and sums use a tag bit rather than a delimiter since it has a fixed length.
In general, a code word followed by further data cannot be split again; for example,
encList enc [a] is a prefix of encList enc [a, b]. Wrap an encoding with encDelimited (as
encList does for its elements) when its end has to be recognizable, see delimit_append_inj.
We do not use Mathlib's Computability.Encoding combinators since they change the alphabet
(e.g. encodingProd encodes into Γ₁ ⊕ Γ₂), while here every encoding targets List Bool.
Basic types #
Nat.bits is injective. Candidate for upstreaming to Mathlib.Data.Nat.Bits.
Finite types #
A finite type is encoded with the fixed width Nat.clog 2 (Fintype.card α) through
Fintype.equivFin. The enumeration is not computable and depends on the Fintype instance, so
this encoding serves existence results where only the code length matters. Concrete machines
should instead compose an explicit embedding α ↪ Fin n with encFin n.
Subsingletons are encoded as the empty word via encFintype.
A type with at most one element, such as a proposition, is encoded as the empty word. Unlike
encFintype, this is computable and needs no Fintype instance.
On finite subsingletons, encSubsingleton agrees with encFintype.
Combinators #
A product is encoded as the delimited first component followed by the second component. Only the first component is delimited since the second one extends to the end of the code word; this keeps the second component undoubled and right-nested products linear in size.
Instances For
A dependent pair is encoded as the delimited first component followed by the second component,
encoded under the encoding selected by the first component. As for encProd, only the first
component is delimited, which is exactly what is needed to know the encoding of the second one.
A sum is encoded by a tag bit, false for inl and true for inr, followed by the code of
the component. A tag bit is used instead of a delimiter since it has a fixed length and does not
double the component.