Repository navigation
Kernel: support nested inductive types - #25
Merged
Merged
Conversation
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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Nov 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1
What
Certus now checks nested inductive types, following Lean 4's kernel (
elim_nested_inductive_fn,restore_nestedandenvironment::add_inductiveinsrc/kernel/inductive.cpp). As before, it never trusts exported recursors.J Dsis replaced by an auxiliary type. HereJis an existing inductive, and its parametric argumentsDsmention the block and have no loose bound variables. The auxiliary type's constructors areJ's, instantiated atDs.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.aux As isbecomesJ Ds is, andaux.c As fsbecomesJ.c Ds fs. The auxiliary recursors areT.rec_1,T.rec_2, … in Lean's order.first_differencediagnostics) against:T.rec/T.rec_itypes and rules;numNested.As in Lean, the nested occurrences
J Dsare then type-checked without the auxiliary types, sinceDsdoes 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.A separate small commit makes binder types strip
semiOutParam, as Lean'sconsumeTypeAnnotationsdoes. Without it,MonadReaderOf's recursor did not match. This was found while checking the whole export.Results (Apple M2, release build)
sum_formula_auto, full export (1,341 decls incl.Lean.Syntax)sum_formula_autocertificate (4.2 MB)nanoda and con-leche both accept the same
sum_formula_autocertificate.Tests
crates/certus-kernel/tests/nested.rsuses a 317 KB certificate (109 declarations), extracted withcertus extractfrom Lean 4.36's export oftests/fixtures/Nested.lean. The fixture nests through:List;Array (List _), a structure, two levels deep;Option;ProdinsideList;It also has a mutual block with nested occurrences, and theorems proved by computation through
Tree.rec_1and friends.What the tests check:
rec_1rule right-hand sides;rec_1rule for the wrong constructor;rec_1/rec_2;rec_4;numNested.cargo fmt --check,cargo clippy --all-targets -- -D warningsandcargo testpass.Limitations
Lean.Syntax. The other shapes are covered by the fixture, not by Lean's library.String.toByteArray_empty("value does not have the declared type"). That theorem is not in thesum_formula_autoclosure, and the failure is unrelated to inductive types.inductive.rs).🤖 Generated with Claude Code