Expand description
Nested-inductive compiler (K3) — the UNTRUSTED front-end for inductives that recur
nested inside a container, RTree := rnode : TList RTree → RTree.
Such a type is not directly a strict-positivity-checkable inductive: RTree appears as
an ARGUMENT of TList, not to the right of an arrow. The Lean/Coq resolution is
SPECIALIZATION: replace the container TList RTree by a fresh sibling RTree$TList
defined MUTUALLY with RTree, mirroring TList’s constructors with the element type
fixed to RTree:
RTree := rnode : RTree$TList → RTree
RTree$TList := RTree$TList_TNil | RTree$TList_TCons : RTree → RTree$TList → RTree$TListThis block is an ordinary mutual inductive (K3b), so its recursor recurses through the
specialized list — the whole point. To relate the specialized form back to the generic
TList RTree, the compiler emits CONVERSION ISOS RTree$TList ↔ TList RTree.
Crucially this module is UNTRUSTED: it only produces terms. The caller registers the mutual block through the trusted mutual machinery and TYPE-CHECKS every iso through the trusted kernel — a mis-compiled sibling or iso is rejected, never trusted. Zero new trusted code, exactly as Lean compiles nested inductives to mutual ones.
§The specialisable fragment
A container C is specialisable when its declared arity is a telescope of n ≥ 1 SORT
parameters with no indices (TList : Type → Type, TPair : Type → Type → Type), and
every constructor of C has the shape
c : Π(p₁:Sort) … Π(pₙ:Sort). ft₁ → … → ftₖ → C p₁ … pₙwhere each value field ftᵢ is one of
- a bare parameter
pⱼ— the slot that carries parameterj’s value; - the SPINE
C p₁ … pₙ— the container recurring on itself; or - a CARRIED field — any type CLOSED over
p₁ … pₙthat does not mentionC(Textinlcons : Π(A). Text → A → Labelled A → Labelled A,Natin a counted container).
Compound fields are rewritten by the same recursive specialization used for explicit nested arguments. Each inner field gets checked conversion definitions, emitted before the outer wrapper uses them. Negative occurrences, dependent fields and nonuniform container recursion are refused before a mutual block is registered.
Registered container applications in fields, such as List A inside an array wrapper, are specialized after fixing the constructor parameters.
Carrying a closed field is not an unchecked pass. The emitted block goes through
Context::add_mutual_inductives, which runs check_positivity_mutual over the WHOLE block
and check_constructor_universes on every member — so a carried field whose universe does not
fit the sibling’s sort is refused there, transactionally, by the trusted kernel rather than
by this untrusted front end. That is strictly stronger than refusing the shape, which only
made Lean’s labelled containers unreachable.
Lean agrees, and it was asked rather than assumed. Run on this box against Lean 4.14.0:
inductive Labelled (α : Type) | lnil | lcons : String → α → Labelled α → Labelled α
inductive LTree | node : Labelled LTree → LTree -- ACCEPTED
inductive Labelled2 (α : Type) | lnil | lcons : α → Nat → Labelled2 α → Labelled2 α
inductive LTree2 | node : Labelled2 LTree2 → LTree2 -- ACCEPTEDand #check @LTree.rec prints exactly the specialised mutual block this module emits —
{motive_1 : LTree → Sort u_1} → {motive_2 : Labelled LTree → Sort u_1} → … with the carried
(a : String) riding through lcons’s minor premise in its own slot. Both FIELD ORDERS are
accepted, which matters because FieldKind here is assigned by matching a field’s TYPE
against the parameter telescope and reads no position at all — so Π(A). Nat → A → C A → C A
and Π(A). A → Nat → C A → C A are the same shape to this compiler and to Lean, and no fence
can separate them.
G_recursor_extra::a_carried_field_after_the_element_mirrors_into_the_same_slot_it_came_from
takes that by computation: the sibling carries the field at its own index and the isos still
invert.
Nesting C a₁ … aₙ then specialises each aⱼ that mentions the inductive (recursively,
so TList (TList RTree) becomes two siblings) and mirrors C’s constructors with
parameter j fixed to aⱼ’s representative. Every parameter position is treated
alike — the element may sit in any of them, or in several.
What is deliberately OUTSIDE the fragment, refused fail-closed rather than mis-compiled:
- a field mentioning a container parameter in a DOMAIN position, where the occurrence is outright negative, and a field mentioning the container itself other than as its own spine.
- an indexed container, a permuted or partially-applied result type, a
Sort-valued value field (which the kernel’s ι-reduction would mistake for a parameter), and a dependent field (one mentioning an earlier field’s binder), which the emitted non-dependent sibling could not express.
Explicit universe instances are retained in generic types, constructor references and generated conversion bodies. Sibling identities include their complete universe instance; malformed instances are refused by ordinary kernel inference before specialization.
§Parameters and universes of the nested family itself
A family may take uniform parameters (PTree α := node : α → List (PTree α) → PTree α) and
universe parameters (UTree.{u} (α : Type u) : Type u). The parameters are split onto one
canonical telescope that every emitted sibling shares (PTree$List α), every member is
universe polymorphic over exactly the family’s levels and lives in the family’s own sort, and
the base leaf of specialization is the family at exactly its own parameters and levels. An
occurrence at anything else (PTree (List α) inside PTree α, UTree.{0} inside
UTree.{u}) is an invalid declaration, refused as Lean refuses it. Recursors, motives and
minor premises follow Lean’s auxiliary-type discovery order (outermost occurrence first),
which is not the post-order siblings are emitted in.
Structs§
- Compiled
- The compiled artifacts of a nested inductive: the mutual block to register, and the
iso DEFINITIONS
(name, type, body)to kernel-check and add. Nothing here is trusted until the caller checks it. - Elimination
- The auxiliary mutual block Lean’s kernel reduces a nested block to.
- IsoNames
- One container specialized for this inductive, with the names of its conversion isos.
- Native
Recursor - A recursor whose motive and constructor terms retain the source container applications.
- Nested
Block - A block of source families, each nesting itself or a sibling inside registered containers:
one family, or an ordinary mutual block. The members share
num_paramsuniform parameters and the universe parameterslevels, and may each carry indices of their own. - Nested
Decl - A nested inductive declaration: a name, its sort, and constructors whose argument types
may mention the inductive nested inside a registered container (
TList RTree). - Nested
Info - What a nested-inductive registration produced: the specialized sibling type names and the conversion isos relating each to its generic container.
Functions§
- compile_
nested - Compile a nested inductive into a mutual block plus conversion isos. Untrusted: every
artifact is checked by the caller. Handles arbitrary nesting DEPTH (
TList (TList RTree)) and arbitrary container ARITY (TPair Nat PTree) by recursive specialization — each container level becomes its own mutual sibling. - compile_
nested_ poly compile_nested_with_paramsfor a universe-polymorphic family (UTree.{u} (α : Type u)).- compile_
nested_ with_ params compile_nestedfor an inductive withnum_paramsuniform parameters (PTree α := node : α → List (PTree α) → PTree α).- derive_
native_ recursors - Reconstruct checked generic recursors from the existing mutual generator.
- derive_
native_ recursors_ poly derive_native_recursors_with_paramsfor a universe-polymorphic family. Each restored recursor’s levels are its motive universes followed by the family’s ownlevels, in order.- derive_
native_ recursors_ with_ params derive_native_recursorsfor a nested inductive withnum_paramsuniform parameters. The restored recursors abstract the parameters first, exactly as Lean’s generated nested recursors do (PTree.rec : {α : Type} → {motive_1 …} → {motive_2 …} → …).- derive_
nested_ block_ recursors - Reconstruct Lean’s recursors for a nested block: the elimination’s mutual recursors, each
restored to the source families and container instances and checked again in
ctx. - eliminate_
nested - Reduce a nested block to an ordinary mutual block exactly as Lean’s kernel does
(
elim_nested_inductive).