Skip to content

Kernel: support nested inductive types - #25

Merged
gimbalzero merged 3 commits into
mainfrom
nested-inductives
Nov 21, 2026
Merged

gimbalzero merged 3 commits into
mainfrom
nested-inductives

Conversation

@gimbalzero

Copy link
Copy Markdown
Owner

Closes #1

What

Certus now checks nested inductive types, following Lean 4's kernel (elim_nested_inductive_fn, restore_nested and environment::add_inductive in src/kernel/inductive.cpp). As before, it never trusts exported recursors.

  1. Eliminate. Every nested occurrence J Ds is replaced by an auxiliary type. Here J is an existing inductive, and its parametric arguments Ds mention the block and have no loose bound variables. The auxiliary type's constructors are J's, instantiated at Ds. J's whole mutual block is copied, and the copies are processed in turn, so deeper nesting works. The traversal is top-down, function before argument, so the auxiliary types come out in Lean's order.
  2. Check the resulting mutual block and generate its recursors with the existing code.
  3. Translate back. aux As is becomes J Ds is, and aux.c As fs becomes J.c Ds fs. The auxiliary recursors are T.rec_1, T.rec_2, … in Lean's order.
  4. Compare exactly (id equality, with first_difference diagnostics) against:
    • the exported T.rec / T.rec_i types and rules;
    • the exported constructor types;
    • numNested.

As in Lean, the nested occurrences J Ds are then type-checked without the auxiliary types, since Ds does not otherwise appear in the auxiliary block. The translated constructor types, recursor types and rules are re-checked too. Auxiliary names are numbered past the export's names, so no declaration can refer to them.

Two more Lean checks are now applied to every block:

  • check_uniform_ind_occs: occurrences of the block's types must be applied to its parameters and levels.
  • Indices must not mention the types being declared (lean4#2125).

A separate small commit makes binder types strip semiOutParam, as Lean's consumeTypeAnnotations does. Without it, MonadReaderOf's recursor did not match. This was found while checking the whole export.

Results (Apple M2, release build)

main this PR
sum_formula_auto, full export (1,341 decls incl. Lean.Syntax) rejected OK, 0.57 s kernel time (export parse ~7 s)
sum_formula_auto certificate (4.2 MB) rejected OK, 148 ms kernel, ~190 ms wall
Race round 1 (83 decls), best of 15 9.3 ms 9.4 ms
Race round 2 (3,080 decls), best of 7, twice 387–388 ms 379–394 ms

nanoda and con-leche both accept the same sum_formula_auto certificate.

Tests

crates/certus-kernel/tests/nested.rs uses a 317 KB certificate (109 declarations), extracted with certus extract from Lean 4.36's export of tests/fixtures/Nested.lean. The fixture nests through:

  • List;
  • Array (List _), a structure, two levels deep;
  • Option;
  • Prod inside List;
  • an indexed type;
  • a parameterised mutual block.

It also has a mutual block with nested occurrences, and theorems proved by computation through Tree.rec_1 and friends.

What the tests check:

  • The fixture is accepted.
  • These forgeries are rejected:
    • swapped rec_1 rule right-hand sides;
    • a rec_1 rule for the wrong constructor;
    • swapped rec_1/rec_2;
    • a missing rec_4;
    • a wrong numNested.

cargo fmt --check, cargo clippy --all-targets -- -D warnings and cargo test pass.

Limitations

  • The only nested type in the Sum export is Lean.Syntax. The other shapes are covered by the fixture, not by Lean's library.
  • Checking every declaration of the Sum export still fails, at String.toByteArray_empty ("value does not have the declared type"). That theorem is not in the sum_formula_auto closure, and the failure is unrelated to inductive types.
  • The kernel's trusted code grows by about 550 lines (inductive.rs).

🤖 Generated with Claude Code

gimbalzero and others added 3 commits November 21, 2026 10:39
Lean's consumeTypeAnnotations removes semiOutParam as well as optParam,
autoParam and outParam when the kernel introduces binders of inductive
types. Without it, the regenerated recursor of a class such as
MonadReaderOf (whose parameter is a semiOutParam) differs from the
exported one and the class is rejected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Follow Lean 4's kernel (elim_nested_inductive_fn, restore_nested and
environment::add_inductive in src/kernel/inductive.cpp):

- Replace every nested occurrence J Ds (an existing inductive applied to
  parameters that mention the block) by an auxiliary type whose
  constructors are J's instantiated at Ds, copying J's whole mutual block
  and recursing into the copies, in Lean's top-down order.
- Check the resulting mutual block and generate its recursors.
- Translate back (aux As to J Ds, aux.c As to J.c Ds) and require the
  result to equal the exported T.rec and T.rec_1, T.rec_2, ... exactly,
  as well as the exported constructor types and numNested.
- Type-check the nested occurrences J Ds, and re-check the translated
  constructor and recursor types and rules, without the auxiliary types.

Also, like Lean: check that every occurrence of a type being declared is
applied to the block's parameters and levels (check_uniform_ind_occs),
and reject indices that mention the types being declared (lean4#2125).
Auxiliary names are numbered past the export's names, so no declaration
can refer to them.

Tests: a 317 KB certificate extracted from Lean 4.36's export of
tests/fixtures/Nested.lean (nesting through List, Array (List _), Option,
Prod, an indexed type and a mutual block; a mutual block with nested
occurrences; computation through Tree.rec_1). It is accepted; forged
rec_1 rules, swapped or missing auxiliary recursors and a wrong
numNested are rejected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Kernel: support nested inductive types

1 participant