Encodings of inductive types #
enc_inductive eT : T where | c₁ : e₁₁, e₁₂ | c₂ | … declares the encoding of an inductive type
whose constructors take explicit, non-recursive fields. A value is mapped to a right-nested sum
with one summand per constructor, and the sum is encoded by encSum. The summand of a constructor
is the framed tuple of its fields, encoded as in enc_structure: the field encoding itself for
one field, encUnit for none. Constructor i of n is therefore tagged by i leading true
bits, followed by a false bit unless it is the last.
The command declares:
eTPayload, the encoding of the sum, andeT_toSum, the map fromTto the sum;eT, the encoding ofT, andeT_apply, unfoldingeT xtoeTPayload (eT_toSum x);eT_toSum_deg, the linear map to the sum;- for each constructor
c, the ruleeT_c_inO, which adds up the bounds of its arguments,eT_c_inSpace, retaining their independent workspace bounds, andeT_c_polyTime; eT_elim, case analysis by constructor on the payloads, and its ruleeT_elim_inO, which charges only the case taken, together witheT_elim_inSpaceat the same workspace bound andeT_elim_polyTime;- for each constructor
c,eT_c_matches,eT_c_payload?, andeT_c_fieldJ?for each zero-based field indexJ, with_deg,_inO,_inSpaceand_polyTimecertificates. Matchers have cost degree one and output degree zero; projections have degrees one/one. eT_c_matches_iffcharacterizes the constructor range;eT_c_payload?_eq_some_iffreconstructs the constructor from its payload, andeT_c_payload?_rangecharacterizes payload existence. These public facts apply to unknown input values without unfolding matches.
The generated matcher and projection definitions retain the encoding/constructor prefix naming
convention above, with a declaration-specific defsWithUnderscore exemption.
The toSum and constructor rules are scoped fun_prop rules, as for enc_structure. The case
rule is stated for an input paired with the value, as ComputableInO.sumElimSnd is.
The original form preserves its sum-of-tuples words and all its rules. Supplying an existing
encoding for a recursive field, such as encUnary for Nat.succ, retains this original behavior.
Use _ to request the flat recursive representation described below.
For the original form, the command checks that T is an inductive type without parameters or
indices, that every constructor is listed once in declaration order, and that each has no
implicit field and one encoding per field, of the field's type. A wrong type or constructor list
is reported at T, with the expected list; a wrong field is reported at its constructor or
encoding.
Direct recursion with uniform parameters #
Write explicit uniform parameters before the colon, as for enc_structure, apply the family to
them, and mark directly recursive fields with _:
enc_inductive encTerm (n : ℕ) : Term n where
| const : encIntBin
| var : (encFinEnum (Fin n))
| add : _, _
| mul : _, _
The encoded value type currently lies in Type, as required by the native codec and loop APIs.
Higher-universe phantom parameters are allowed when the value type stays small.
Type parameters and their encodings are also explicit binders, for example (α : Type) and
(eα : α ↪ List Bool). Every generated declaration takes these arguments in the same order.
Parameters select the family and scalar encodings; they are not serialized. Nonrecursive fields,
including previously encoded families such as Term n inside Formula n, always name their
chosen embeddings. No encoding or decoder is inferred from a typeclass.
Each constructor contributes one token containing its nonrecursive fields in declaration order.
The token uses a right-nested sum tag: constructor i of c has i leading true bits and a
false bit unless it is last. Its scalar tuple frames every field except the last; an empty
payload is encUnit. The preorder stream then lists the recursive children's streams in
recursive-field order. encList frames each token once; entire child streams are never framed
again. Zero-child constructors still contribute one token.
eT_tokens, eT_nodes, and eT_leafSize expose the stream, constructor count, and sum of the
chosen scalar word lengths. eT_length gives the exact sum of token-frame lengths.
eT_length_le bounds bit size by (2 * (c + m) + 1) * nodes + 4 * leafSize, where m is the
maximum number of scalar fields in one constructor. The bound counts scalar words, including
a cross-family encoding's complete word, and is independent of recursive depth.
Constructor equations and native assembly rules preserve separate argument time and charged
workspace bounds. Recognition and field extraction operate on encoded values without scalar
decoders. eT_decodeTokens parses typed tokens; strict bit-word decoding additionally requires
explicit PartialDecoder witnesses for the scalar embeddings. These supply both decoding and
native polynomial-time certificates, including output space, on malformed as well as valid words.
An arbitrary field embedding supplies neither. No generated rule certifies arbitrary recursive
functions, evaluation, folds, or induction hypotheses merely from the existence of the encoding.
Preflight rejects genuine indices, fields depending on earlier constructor fields, proof or implicit fields, mutual recursion, nested recursion, and recursive fields with changed parameters, before emitting any declarations. Constructor arity is unrestricted; both empty constructors and multiple recursive fields mixed with scalar fields are supported.
enc_inductive eT : T where | c₁ : e₁, e₂ | c₂ declares the encoding eT of the inductive
type T: constructor c₁ with fields encoded by e₁ and e₂, and constructor c₂ without
fields. Constructors are listed in declaration order.
Explicit uniform parameters and field encodings for a flat recursive inductive encoding.
Write _ for every directly recursive field.
Elaborate explicit uniform parameters and field choices through the recursive generator.
Elaborate enc_inductive.