From 68e4628908fc7c1cae2ebffa0b6816f5089ac662 Mon Sep 17 00:00:00 2001 From: Alex Flom Date: Thu, 22 Oct 2026 11:43:25 -0600 Subject: [PATCH 1/8] register DF-19: structural recursion over the recursive family of a nested inductive type (#278, R3: row, scenario, named test) R3: the register row, the SPEC.md section 31 row, the scenario, and the failing named test conformance_df_19. No implementation. The count moves 445 to 446. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01F4DJnZorC2Jd4MJfN9DbZk --- CONFORMANCE.md | 1 + README.md | 2 +- SPEC.md | 3 ++- crates/conformance/src/cases/repository.rs | 2 +- crates/conformance/tests/conformance.rs | 5 +++++ features/suites/declarations.feature | 7 +++++++ model/ids.toml | 6 ++++++ xtask/src/spec_links.rs | 4 ++-- 8 files changed, 25 insertions(+), 5 deletions(-) diff --git a/CONFORMANCE.md b/CONFORMANCE.md index c799ee45b7..c368ff3ac1 100644 --- a/CONFORMANCE.md +++ b/CONFORMANCE.md @@ -151,6 +151,7 @@ the error registry does not sanction (R5, `ERRORS.md`). | `DF-16` | `build` | Language 1.2 mutual definition groups recurse structurally over one recursive family, including nested and mutual inductives, and every call between members passes a structurally smaller family binder. | | `DF-17` | `build` | Language 1.2 well-founded definitions carry a binder-free natural-number measure and, per recursive call site, a prior theorem stating exactly that call's decrease obligation; linking checks the statements and Lean checks the proofs. | | `DF-18` | `build` | Language 1.2 state threading is explicit: ordered folds and bounded iteration pass the state through a direct closure, executable definitions may use them, and every iteration carries a natural-number bound. | +| `DF-19` | `build` | Language 1.2 mutual definition groups recurse structurally over the whole recursive family of a nested inductive, the types the pinned Lean takes as the motives of its recursor: the type itself, then each distinct application of `List`, `Option`, `Result`, or a product that holds the type in a constructor field of an earlier member, in the order of discovery, so that a group may decrease on the type, on a list of its values, on a list of pairs whose second component is one, and on such a pair, and in a member's top-level match every pattern binder whose type is a member of the family is structurally smaller; a group that decreases on a type outside the family and a call that passes anything but a smaller binder of the family are each refused as LLT4001, and each group lowers to one Lean mutual block of structural definitions that the pinned Lean accepts. | ## examples diff --git a/README.md b/README.md index 3d776caa0a..203c554f81 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ just vv # the complete normative acceptance gate (SPEC.md §9.2) just release # vv, then the §30 release criterion; refused until 1.0.0 ``` -All 445 registered conformance IDs are implemented and pass; `just vv` runs clean from a checkout with the pinned toolchain installed. +All 446 registered conformance IDs are implemented and pass; `just vv` runs clean from a checkout with the pinned toolchain installed. The differential against the independent generator `lean4-prod` (SPEC.md §17.19, `OR-01`..`OR-08`) acquires it at its pinned commit: from the checkout `LEXLEAN_ORACLE_DIR` names, or by fetching the commit with `git` into the Cargo target directory (`LEXLEAN_ORACLE_CACHE` overrides), and builds it with the pinned Lean and Rust toolchains. It is validation evidence only and no input to any identity. An oracle that cannot be acquired, is not the pinned files, or fails is unavailable: that fails the gate on Linux x86-64 and is printed as `NOT VALIDATED` elsewhere, and is never reported as agreement. diff --git a/SPEC.md b/SPEC.md index b5627d9cb8..c088926458 100644 --- a/SPEC.md +++ b/SPEC.md @@ -10337,6 +10337,7 @@ Every row below is normative, has honesty level `build`, and MUST be copied byte | `DF-16` | `declarations` | Language 1.2 mutual definition groups recurse structurally over one recursive family, including nested and mutual inductives, and every call between members passes a structurally smaller family binder. | §17.12 | | `DF-17` | `declarations` | Language 1.2 well-founded definitions carry a binder-free natural-number measure and, per recursive call site, a prior theorem stating exactly that call's decrease obligation; linking checks the statements and Lean checks the proofs. | §17.12 | | `DF-18` | `declarations` | Language 1.2 state threading is explicit: ordered folds and bounded iteration pass the state through a direct closure, executable definitions may use them, and every iteration carries a natural-number bound. | §17.12 | +| `DF-19` | `declarations` | Language 1.2 mutual definition groups recurse structurally over the whole recursive family of a nested inductive, the types the pinned Lean takes as the motives of its recursor: the type itself, then each distinct application of `List`, `Option`, `Result`, or a product that holds the type in a constructor field of an earlier member, in the order of discovery, so that a group may decrease on the type, on a list of its values, on a list of pairs whose second component is one, and on such a pair, and in a member's top-level match every pattern binder whose type is a member of the family is structurally smaller; a group that decreases on a type outside the family and a call that passes anything but a smaller binder of the family are each refused as LLT4001, and each group lowers to one Lean mutual block of structural definitions that the pinned Lean accepts. | §17.12 | | `PF-01` | `proofs` | Assume and exact-style simple proof sentences create scoped introductions and exact proof nodes. | §16.2 | | `PF-02` | `proofs` | Simple Apply is accepted only when its declared signature yields exactly one residual premise. | §16.2 | | `PF-03` | `proofs` | Structured apply requires every numbered residual premise exactly once and in signature order. | §16.6 | @@ -10648,7 +10649,7 @@ Every row below is normative, has honesty level `build`, and MUST be copied byte | `LM-09` | `language-models` | No prompt is code: no member of the closed language maps `string` or `bytes` to a function, a term, or source — every operation of the primitive registry that takes a `string` or `bytes` operand is classified by the audit with a data result, the registry's schema enumeration and the production registry's primitive rows equal one another and equal that classified set, and the text declarations of `lexlean/semantic-module/2` state no `string`-typed member, so a prompt reaches a model only as the tokens `encodeText` makes of it; a planted operation or text member of that shape is caught by the audit with its name. | §17.12 | | `LM-13` | `language-models` | The text laws a tokenizer elaborates through are LexLean source and not authored text: the module that states them is one language-1.2 native core module of the embedded `language/text-1.2` project, the project holds no file of Lean source, no independently authored Lean file anywhere in the release tree declares the `LexLeanText` namespace or lies under its module root, and the one-time migration input is absent; the registry lists only that module, the declarations and their exact axiom policies are read from its native data, which the compiler embeds and hashes into the language-1.2 semantics, and a module that declares a text component imports the generated module rather than splicing any Lean text. | §17.10, §17.12 | -**Total required capability IDs:** 445. +**Total required capability IDs:** 446. No row may be downgraded to `some-true` or `open`. Upstream Lean facts are ledger/authority rows, not substitutions for these build behaviors. diff --git a/crates/conformance/src/cases/repository.rs b/crates/conformance/src/cases/repository.rs index 6ec3f20aec..6c31fe69ff 100644 --- a/crates/conformance/src/cases/repository.rs +++ b/crates/conformance/src/cases/repository.rs @@ -760,7 +760,7 @@ pub(crate) fn run(id: &str) { let model = repo_model::Model::load(&root.join("model").into_std_path_buf()) .expect("the model loads"); let table = spec_table(); - assert_eq!(table.len(), 445, "§31 has 445 rows"); + assert_eq!(table.len(), 446, "§31 has 446 rows"); assert_eq!(model.ids.id.len(), table.len(), "register row count"); for ((spec_id, spec_suite, spec_statement), row) in table.iter().zip(model.ids.id.iter()) diff --git a/crates/conformance/tests/conformance.rs b/crates/conformance/tests/conformance.rs index 5c133077aa..d898f76d70 100644 --- a/crates/conformance/tests/conformance.rs +++ b/crates/conformance/tests/conformance.rs @@ -685,6 +685,11 @@ fn conformance_df_18() { repo_conformance::cases::run("DF-18"); } +#[test] +fn conformance_df_19() { + repo_conformance::cases::run("DF-19"); +} + #[test] fn conformance_pf_01() { repo_conformance::cases::run("PF-01"); diff --git a/features/suites/declarations.feature b/features/suites/declarations.feature index 1b61480d43..e40b7cda56 100644 --- a/features/suites/declarations.feature +++ b/features/suites/declarations.feature @@ -127,3 +127,10 @@ Feature: declarations When it is rendered and verified, and copies drop the bound or mistype a fold step Then the generated Lean passes the step as a lambda to the fixed iteration runtime and the kernel computes the closure and its convergence flag And the unbounded iteration and the mistyped step fail with LLT4001 before any build output + + @DF-19 @build + Scenario: Language 1.2 mutual definition groups recurse structurally over the whole recursive family of a nested inductive, the types the pinned Lean takes as the motives of its recursor: the type itself, then each distinct application of `List`, `Option`, `Result`, or a product that holds the type in a constructor field of an earlier member, in the order of discovery, so that a group may decrease on the type, on a list of its values, on a list of pairs whose second component is one, and on such a pair, and in a member's top-level match every pattern binder whose type is a member of the family is structurally smaller; a group that decreases on a type outside the family and a call that passes anything but a smaller binder of the family are each refused as LLT4001, and each group lowers to one Lean mutual block of structural definitions that the pinned Lean accepts. + Given the committed nested-family project, whose type holds a `List` of itself and a `List` of pairs that end in itself and whose two mutual definition groups have four members each, one for every member of the type's family + When it is rendered and verified, and copies change a decreasing type, a call, or a pattern binder + Then each group lowers to one Lean mutual block whose members declare structural termination and verify with real Lean + And a group that decreases on a type outside the family and a call that passes a binder outside it fail with LLT4001 before any build output diff --git a/model/ids.toml b/model/ids.toml index efb799cbf3..3d4c210961 100644 --- a/model/ids.toml +++ b/model/ids.toml @@ -823,6 +823,12 @@ level = "build" suite = "declarations" statement = "Language 1.2 state threading is explicit: ordered folds and bounded iteration pass the state through a direct closure, executable definitions may use them, and every iteration carries a natural-number bound." +[[id]] +id = "DF-19" +level = "build" +suite = "declarations" +statement = "Language 1.2 mutual definition groups recurse structurally over the whole recursive family of a nested inductive, the types the pinned Lean takes as the motives of its recursor: the type itself, then each distinct application of `List`, `Option`, `Result`, or a product that holds the type in a constructor field of an earlier member, in the order of discovery, so that a group may decrease on the type, on a list of its values, on a list of pairs whose second component is one, and on such a pair, and in a member's top-level match every pattern binder whose type is a member of the family is structurally smaller; a group that decreases on a type outside the family and a call that passes anything but a smaller binder of the family are each refused as LLT4001, and each group lowers to one Lean mutual block of structural definitions that the pinned Lean accepts." + [[id]] id = "PF-01" level = "build" diff --git a/xtask/src/spec_links.rs b/xtask/src/spec_links.rs index 69f48b028a..2bcb1a9062 100644 --- a/xtask/src/spec_links.rs +++ b/xtask/src/spec_links.rs @@ -61,8 +61,8 @@ fn parse_table(spec: &str) -> Result, Fail> { pub fn validate(root: &Path) -> Result<(), Fail> { let spec = std::fs::read_to_string(root.join("SPEC.md"))?; let rows = parse_table(&spec)?; - if rows.len() != 445 { - return Err(format!("RP-07: the §31 table has {} rows, not 445", rows.len()).into()); + if rows.len() != 446 { + return Err(format!("RP-07: the §31 table has {} rows, not 446", rows.len()).into()); } // The prose total under the table is part of the contract a reader // relies on; a row added without it would leave §31 contradicting itself. From d9617269b47390d33f943f1cd97def4310c60867 Mon Sep 17 00:00:00 2001 From: Alex Flom Date: Thu, 22 Oct 2026 09:49:30 -0600 Subject: [PATCH 2/8] feat(ir): one recursive-family computation for structural recursion over nested inductive types (DF-19, #278) A mutual definition group may now decrease on any member of the recursive family of an inductive group: the group's members, then each maximal List, Option, Result or product application holding a member, discovered in the order of the pinned Lean 4.32.1 recursor's motives. A pattern binder is structurally smaller exactly when its type is a family member, so size and leaves over all four members of Json now recurse through object (List (String * Json)). TypeInfo keeps the constructor declaration order, which the name-sorted constructor_types does not. The SPEC 17.12 recursion rule, the VERIFICATION record, the DF-19 scenario and the committed tests/nested-family project are updated. No file of the 21.2 identity digest changes. SM-37 is not included. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01F4DJnZorC2Jd4MJfN9DbZk --- CHANGELOG.md | 8 + SPEC.md | 19 +- VERIFICATION.md | 47 +++ crates/conformance/src/cases/declarations.rs | 70 ++++ crates/lexlean/src/ir/semantic.rs | 377 ++++++++++++++++--- tests/nested-family/lake-manifest.json | 6 + tests/nested-family/lakefile.toml | 2 + tests/nested-family/lean-toolchain | 1 + tests/nested-family/lexlean.lock | 47 +++ tests/nested-family/lexlean.toml | 27 ++ tests/nested-family/src/Main.lex.tex | 117 ++++++ 11 files changed, 658 insertions(+), 63 deletions(-) create mode 100644 tests/nested-family/lake-manifest.json create mode 100644 tests/nested-family/lakefile.toml create mode 100644 tests/nested-family/lean-toolchain create mode 100644 tests/nested-family/lexlean.lock create mode 100644 tests/nested-family/lexlean.toml create mode 100644 tests/nested-family/src/Main.lex.tex diff --git a/CHANGELOG.md b/CHANGELOG.md index a2ddb0ce42..e736308a23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,14 @@ and Unreleased holds what has landed since. ## Unreleased +- Structural recursion over the recursive family of a nested inductive type, + #278 (SPEC.md §17.12, `DF-19`). A mutual definition group may decrease on any + member of the family the pinned Lean takes as the motives of the type's + recursor (the type, then each `List`, `Option`, `Result`, or product that + holds it, in the order of discovery), so a function over a type with a + constructor that holds `List (String * Json)` recurses through the list and + through the pair. One checker change; no file of the compiler-semantics + digest moves, so no identity moves. The register is 446 IDs. - The Rust runtime as source, #91 stage 4 (SPEC.md §17.16, §17.17, `SP-21`, `RB-15`, `SP-23`, `SM-35`, `SM-36`, `RB-16`, `SP-22`, `RB-17`, `SP-25`). The declared Rust machine grows by the primitives the runtime is written in diff --git a/SPEC.md b/SPEC.md index c088926458..05af5a00e5 100644 --- a/SPEC.md +++ b/SPEC.md @@ -3009,10 +3009,21 @@ Termination evidence is semantic data, never tactic or backend text; no or references; every member calls into the group and the graph is strongly connected, so no member is outside the mutual recursion. For a structural group: Every member's decreasing type belongs to one *recursive - family*: the naturals; the lists of one element type; or one inductive - group at one instantiation of its type parameters, together with `List` - or `Option` of its members exactly when the group's constructors nest a - member under that container. In a member's + family*: the naturals; the lists of one element type; or the family of one + inductive group at one instantiation of its type parameters (`DF-19`). + That family is the members of the group, then every other type that + holds a member: scanning the members in order, and the constructor fields + of each in the order the pinned Lean takes the constructors (declaration + order for a document inductive; `nil`, `cons`; `none`, `some`; `error`, + `ok`; `mk`), each field that mentions the group without being a member is + a maximal application of `List`, `Option`, `Result`, or a product (by + strict positivity, §17.12 recursive-data rule 3) and joins the family + once, at its first occurrence. These are the motives of the recursor the + pinned Lean 4.32.1 generates for the group (`#check @T.rec`), in its + order: for `Json` with `array (List Json)` and `object (List (String * + Json))`, `Json`, `List Json`, `List (String * Json)`, and `String * + Json`, so a group may decrease on any of the four, and a list of pairs + whose first component is another type is not one of them. In a member's top-level match, every pattern binder whose type belongs to the family is structurally smaller, and every call to a group member passes a smaller binder in that member's decreasing position. Members see each other; the diff --git a/VERIFICATION.md b/VERIFICATION.md index 8306c65dab..691b21b159 100644 --- a/VERIFICATION.md +++ b/VERIFICATION.md @@ -792,6 +792,53 @@ test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 238 filtered out Removed: the test was restored; `conformance_df_16` passes and the `recursion-wrong-argument` negative fixture fails with `LLT4001`. +### a mutual group over the family of a nested inductive can fail (DF-19) + +A mutual definition group may decrease on any member of the recursive family +of a nested inductive (`DF-19`, §17.12, #278): `Json` with `array (List Json)` +and `object (List (String * Json))` has the four members `Json`, `List Json`, +`List (String * Json)`, and `String * Json`, and `tests/nested-family` holds +two groups of four members over it, `size` and `leaves`. The family is one +computation (`family_members`) shared by the group's family test and by the +rule that a pattern binder is smaller exactly when its type is a member; the +unit tests `the_family_is_discovered_in_the_order_of_the_pinned_recursors_motives` +and `a_member_found_inside_a_product_joins_the_family` pin the order against +the motives `#check @A.rec` reports under Lean 4.32.1 for a type with seven. + +Command and host: `cargo test -p repo-conformance --test conformance -- +conformance_df_19 conformance_df_13 conformance_df_14 conformance_df_16 +conformance_df_17` and `cargo test -p lexlean --lib family_` on the 2-core +Linux x86-64 machine the series is prepared on, Lean 4.32.1, rustc stable +1.97.0. `conformance_df_19` verifies `tests/nested-family` under the pinned +Lean (two theorems evaluate both groups on a value nesting all four members), +and refuses four plants with `LLT4001`: a call on a `Json` built from the +pair's string, a call on the matched list itself, a list of pairs whose first +component is `Nat`, and a pair whose first component is `Nat`. + +Planted: a pattern binder whose type is a product never smaller +(`in_family(field, family) && !matches!(field, SemanticType::Product { .. })`), +so `sizeEntry` could not call `size` on the pair's `Json`. Command: +`cargo test -p repo-conformance --test conformance -- conformance_df_19`. + +```text +check succeeds: LexLeanError { class: Language, diagnostics: [Diagnostic { code: DiagnosticCode("LLT4001"), message: "phase link: recursive call `sizeEntry` is not on a structurally smaller value", ... +test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 444 filtered out +``` + +Planted: the arm of `family_constructor_fields` that gives a product its two +fields removed. Command: `cargo test -p lexlean --lib family_`. Expected: a +family whose fourth member is found only inside a product loses it. + +```text +test ir::semantic::family_order_tests::a_member_found_inside_a_product_joins_the_family ... FAILED +test result: FAILED. 2 passed; 1 failed; 0 ignored; 0 measured; 209 filtered out +``` + +Removed: both plants were restored; the cases pass. Before the change the +same project is refused at its first group: `mutual group `Size` members +decrease on different recursive families` (the old `family_of` saw a list of +pairs as a list of one element type). + ### recursion call graph check can fail Planted: `check_group_call_graph` returned `Ok(())` at once, so a mutual diff --git a/crates/conformance/src/cases/declarations.rs b/crates/conformance/src/cases/declarations.rs index da470144f7..3b9cf62732 100644 --- a/crates/conformance/src/cases/declarations.rs +++ b/crates/conformance/src/cases/declarations.rs @@ -1540,6 +1540,76 @@ pub(crate) fn run(id: &str) { "primitive ListFold argument 0 has type ((Nat) -> (List (Nat)) -> (List (Nat))), expected ((List (Nat)) -> (Nat) -> (List (Nat)))", ); } + // §17.12: a mutual definition group recurses over the whole recursive + // family of a nested inductive. + "DF-19" => { + // `Json` holds a `List` of itself and a `List` of pairs that end in + // itself, so its family is `Json`, `List Json`, + // `List (String * Json)` and `String * Json`; two groups of four + // members range over it. + let project = P::copy_tests_project("nested-family"); + project.check_ok(); + let rendered = support::rendered(&project); + let main = support::lean_text(&rendered, "Main"); + for expected in [ + "mutual\n@[expose] public def size : (value : Json) -> Nat\n", + " | Json.object entries => (1 + sizeEntries (entries))\ntermination_by structural value => value\n", + "termination_by structural items => items\n", + "@[expose] public def sizeEntries : (entries : List ((Prod (String) (Json)))) -> Nat\n", + " | List.cons head tail => (sizeEntry (head) + sizeEntries (tail))\ntermination_by structural entries => entries\n", + "@[expose] public def sizeEntry : (entry : (Prod (String) (Json))) -> Nat\n | Prod.mk _ value => size (value)\ntermination_by structural entry => entry\nend\n", + " | Prod.mk _ value => leaves (value)\ntermination_by structural entry => entry\nend\n", + ] { + assert!(main.contains(expected), "missing {expected:?} in:\n{main}"); + } + assert_eq!( + main.matches("termination_by structural").count(), + 8, + "one structural termination clause per member:\n{main}" + ); + assert_eq!(main.matches("\nmutual\n").count(), 2, "{main}"); + // The pinned Lean accepts every group and evaluates both on a + // value that nests all four members (the two theorems). + let _ = support::verify_ok_backed("DF-19", &project); + let reject = |from: &str, to: &str, message: &str| { + let copy = P::copy_tests_project("nested-family"); + let source = copy.read("src/Main.lex.tex"); + assert_eq!(source.matches(from).count(), 1, "{from}"); + copy.write("src/Main.lex.tex", &source.replacen(from, to, 1)); + let error = copy.check_fails_with("LLT4001"); + assert!( + error.to_string().contains(message), + "expected {message:?}, got {error}" + ); + copy.assert_no_backend_output(); + }; + // A pair's string is not a member of the family, so a `Json` made + // from it is not a smaller value of the pair. + reject( + r#""binders":["key","value"],"body":{"arguments":[{"kind":"var","name":"value"}],"function":{"name":"size"},"kind":"call"}"#, + r#""binders":["key","value"],"body":{"arguments":[{"arguments":[{"kind":"var","name":"key"}],"constructor":{"name":"Json.text"},"kind":"constructor"}],"function":{"name":"size"},"kind":"call"}"#, + "recursive call `size` is not on a structurally smaller value", + ); + // The matched list itself is not smaller. + reject( + r#"{"arguments":[{"kind":"var","name":"tail"}],"function":{"name":"sizeEntries"},"kind":"call"}"#, + r#"{"arguments":[{"kind":"var","name":"entries"}],"function":{"name":"sizeEntries"},"kind":"call"}"#, + "recursive call `sizeEntries` is not on a structurally smaller value", + ); + // A list of pairs whose first component is another type is a list + // of one element type, not a member of the family of `Json`. + reject( + r#""name":"sizeEntries","parameters":[{"name":"entries","type":{"element":{"kind":"product","left":{"kind":"string"}"#, + r#""name":"sizeEntries","parameters":[{"name":"entries","type":{"element":{"kind":"product","left":{"kind":"nat"}"#, + "mutual group `Size` members decrease on different recursive families", + ); + // Such a pair is outside every family. + reject( + r#""name":"sizeEntry","parameters":[{"name":"entry","type":{"kind":"product","left":{"kind":"string"}"#, + r#""name":"sizeEntry","parameters":[{"name":"entry","type":{"kind":"product","left":{"kind":"nat"}"#, + "member `sizeEntry` decreases on a type outside every recursive family", + ); + } other => panic!("no declarations case is wired for {other}"), } } diff --git a/crates/lexlean/src/ir/semantic.rs b/crates/lexlean/src/ir/semantic.rs index 9ef7d22731..428600f32a 100644 --- a/crates/lexlean/src/ir/semantic.rs +++ b/crates/lexlean/src/ir/semantic.rs @@ -3703,6 +3703,11 @@ struct TypeInfo { /// Language 1.2: the environment keys of every member of this type's /// inductive group, itself included. group: Vec, + /// Language 1.2: the constructor names (`Owner.ctor`) in declaration + /// order, which `constructor_types` (sorted by name) does not keep. The + /// recursive family is discovered in this order, as the pinned Lean + /// discovers the motives of a recursor. + constructor_order: Vec, } #[derive(Clone)] @@ -4672,6 +4677,11 @@ fn register_inductive_group( ) }) .collect(), + constructor_order: row + .constructors + .iter() + .map(|constructor| format!("{}.{}", row.name, constructor.name)) + .collect(), ..TypeInfo::default() }, ); @@ -5225,81 +5235,206 @@ fn check_executable_term( } /// The recursive family a mutual definition group decreases over (§17.12): -/// the naturals, lists of one element type, or one inductive group with its -/// `List` and `Option` containers. +/// the naturals, lists of one element type, or the family of one inductive +/// group (`family_members`). #[derive(Clone, Debug, PartialEq, Eq)] enum Family { Nat, List(SemanticType), - /// An inductive group at one instantiation of its type parameters. - Group(BTreeSet, Vec), -} - -/// The containers (`List`, `Option`) under which a group's constructors -/// actually nest a member of the group: exactly those a mutual definition -/// may recurse through. -fn group_containers(group: &BTreeSet, env: &Environment<'_>) -> (bool, bool) { - let in_group = |ty: &SemanticType| matches!(ty, SemanticType::Named { member, .. } if group.contains(&member_key(member))); - let (mut list, mut option) = (false, false); - for key in group { - let Some(info) = env.types.get(key) else { - continue; + /// The members of an inductive group's recursive family at one + /// instantiation of its type parameters, in the order the pinned Lean + /// discovers the motives of the group's recursor. + Group(Vec), +} + +/// The types a type is made of: the arguments of a document type, the element +/// of a list, set or option, both sides of a result, product or map, and the +/// parameters and result of a function. +fn type_children(ty: &SemanticType) -> Vec<&SemanticType> { + match ty { + SemanticType::Named { arguments, .. } => arguments.iter().collect(), + SemanticType::List { element: inner } + | SemanticType::Option { value: inner } + | SemanticType::Set { element: inner } => vec![inner.as_ref()], + SemanticType::Result { + ok: left, + error: right, + } + | SemanticType::Product { left, right } + | SemanticType::Map { + key: left, + value: right, + } => vec![left.as_ref(), right.as_ref()], + SemanticType::Function { parameters, result } => parameters + .iter() + .chain(std::iter::once(result.as_ref())) + .collect(), + SemanticType::Type + | SemanticType::Parameter { .. } + | SemanticType::Nat + | SemanticType::Bool + | SemanticType::Prop + | SemanticType::Unit + | SemanticType::Int + | SemanticType::Int8 + | SemanticType::Int16 + | SemanticType::Int32 + | SemanticType::Int64 + | SemanticType::UInt8 + | SemanticType::UInt16 + | SemanticType::UInt32 + | SemanticType::UInt64 + | SemanticType::String + | SemanticType::Bytes + | SemanticType::Ordering + | SemanticType::ContractViolation + | SemanticType::ReasoningFailure => Vec::new(), + } +} + +/// Whether `ty` mentions a member of the group named by `keys`. +fn mentions_family_group(ty: &SemanticType, keys: &[String]) -> bool { + if matches!(ty, SemanticType::Named { member, .. } if keys.contains(&member_key(member))) { + return true; + } + type_children(ty) + .into_iter() + .any(|child| mentions_family_group(child, keys)) +} + +/// The constructor field types of one member of a recursive family, at the +/// member's own type arguments and in the order the pinned Lean takes the +/// constructors: the declaration order for a document inductive, `nil` and +/// `cons` for a list, `none` and `some` for an option, `error` and `ok` for a +/// result, and `mk` for a product. +fn family_constructor_fields(ty: &SemanticType, env: &Environment<'_>) -> Vec { + if let SemanticType::Named { member, arguments } = ty { + let Some(info) = type_info(member, env) else { + return Vec::new(); }; - for field in info.constructor_types.values().flatten() { - match field { - SemanticType::List { element } if in_group(element) => list = true, - SemanticType::Option { value } if in_group(value) => option = true, - _ => {} + let map = substitutions(info, arguments); + let names: Vec<&String> = if info.constructor_order.is_empty() { + info.constructor_types.keys().collect() + } else { + info.constructor_order.iter().collect() + }; + return names + .into_iter() + .filter_map(|name| info.constructor_types.get(name)) + .flatten() + .map(|field| substitute_type(field, &map)) + .collect(); + } + if let SemanticType::List { element } = ty { + return vec![element.as_ref().clone(), ty.clone()]; + } + if let SemanticType::Option { value } = ty { + return vec![value.as_ref().clone()]; + } + if let SemanticType::Result { ok, error } = ty { + return vec![error.as_ref().clone(), ok.as_ref().clone()]; + } + if let SemanticType::Product { left, right } = ty { + return vec![left.as_ref().clone(), right.as_ref().clone()]; + } + Vec::new() +} + +/// §17.12: the recursive family of the inductive group `keys` at `arguments` +/// is the types the pinned Lean takes as the motives of the group's +/// recursor. It starts with the members of the group; then, scanning the +/// members in order and the constructor fields of each in order, every field +/// that mentions the group without being a member is a maximal application +/// of `List`, `Option`, `Result`, or a product (strict positivity), and +/// joins the family once, at its first occurrence. A mutual definition group +/// decreases on one family, and the binders of a match on a member that are +/// themselves members are the structurally smaller values. +fn family_members( + keys: &[String], + arguments: &[SemanticType], + env: &Environment<'_>, +) -> Vec { + let mut members: Vec = keys + .iter() + .map(|key| SemanticType::Named { + member: member_from_key(key), + arguments: arguments.to_vec(), + }) + .collect(); + let mut index = 0; + while index < members.len() { + for field in family_constructor_fields(&members[index], env) { + if mentions_family_group(&field, keys) && !members.contains(&field) { + members.push(field); } } + index += 1; } - (list, option) + members } -fn group_key_of(member: &MemberRef, env: &Environment<'_>) -> Option> { +fn group_key_of(member: &MemberRef, env: &Environment<'_>) -> Option> { let info = type_info(member, env)?; if info.constructors.is_empty() || !info.fields.is_empty() { return None; } - Some(info.group.iter().cloned().collect()) + Some(info.group.clone()) +} + +/// Every document type `ty` names, outermost first. +fn named_types(ty: &SemanticType, out: &mut Vec<(MemberRef, Vec)>) { + if let SemanticType::Named { member, arguments } = ty { + out.push((member.clone(), arguments.clone())); + } + for child in type_children(ty) { + named_types(child, out); + } } /// The family of one decreasing parameter type. fn family_of(ty: &SemanticType, env: &Environment<'_>) -> Option { - match ty { - SemanticType::Nat => Some(Family::Nat), - SemanticType::Named { member, arguments } => { - group_key_of(member, env).map(|group| Family::Group(group, arguments.clone())) - } - SemanticType::List { element } | SemanticType::Option { value: element } => { - if let SemanticType::Named { member, arguments } = element.as_ref() { - if let Some(group) = group_key_of(member, env) { - let (list, option) = group_containers(&group, env); - let nested = match ty { - SemanticType::List { .. } => list, - _ => option, - }; - return nested.then(|| Family::Group(group, arguments.clone())); - } - } - match ty { - SemanticType::List { element } => Some(Family::List(element.as_ref().clone())), - _ => None, - } + if matches!(ty, SemanticType::Nat) { + return Some(Family::Nat); + } + if let SemanticType::Named { member, arguments } = ty { + return group_key_of(member, env) + .map(|keys| Family::Group(family_members(&keys, arguments, env))); + } + if !matches!( + ty, + SemanticType::List { .. } + | SemanticType::Option { .. } + | SemanticType::Result { .. } + | SemanticType::Product { .. } + ) { + return None; + } + let mut named = Vec::new(); + named_types(ty, &mut named); + for (member, arguments) in named { + let Some(keys) = group_key_of(&member, env) else { + continue; + }; + let members = family_members(&keys, &arguments, env); + if members.contains(ty) { + return Some(Family::Group(members)); } - _ => None, } + if let SemanticType::List { element } = ty { + return Some(Family::List(element.as_ref().clone())); + } + None } /// Whether a pattern binder of type `ty` belongs to `family` and is /// therefore a structurally smaller subvalue of the matched argument. -fn in_family(ty: &SemanticType, family: &Family, env: &Environment<'_>) -> bool { +fn in_family(ty: &SemanticType, family: &Family) -> bool { match (family, ty) { (Family::Nat, SemanticType::Nat) => true, (Family::List(element), SemanticType::List { element: observed }) => { observed.as_ref() == element } - (Family::Group(..), _) => family_of(ty, env).as_ref() == Some(family), + (Family::Group(members), _) => members.contains(ty), _ => false, } } @@ -6759,20 +6894,13 @@ fn check_term( // §17.12 mutual recursion: every binder of the matched // constructor that belongs to the group's family is a // structurally smaller subvalue. - let type_arguments: Vec = - match Some(decreasing) { - Some(SemanticType::Named { arguments, .. }) => arguments.clone(), - Some(SemanticType::List { element }) - | Some(SemanticType::Option { value: element }) => { - vec![element.as_ref().clone()] - } - _ => Vec::new(), - }; - if let Ok(Some((_, fields))) = - constructor_signature(&branch.constructor, &type_arguments, env) - { + // The field types are those the pinned Lean gives the + // constructor of the matched family member, whether + // that member is a document type, a list, an option, + // a result, or a product. + if let Ok(fields) = branch_binder_types(decreasing, branch, env) { for (binder, field) in branch.binders.iter().zip(&fields) { - if in_family(field, family, env) { + if in_family(field, family) { branch_smaller.insert(binder.clone()); } } @@ -9061,6 +9189,12 @@ impl SemanticModule { }) .collect(), class: false, + constructor_order: constructors + .iter() + .map(|constructor| { + format!("{}.{}", declaration.name(), constructor.name) + }) + .collect(), ..TypeInfo::default() }, ); @@ -9728,6 +9862,131 @@ mod application_head_tests { } } +#[cfg(test)] +mod family_order_tests { + use super::{ + family_members, family_of, Environment, Family, MemberRef, SemanticType, TypeInfo, + }; + + fn named(name: &str) -> SemanticType { + SemanticType::Named { + member: MemberRef { + module: None, + name: name.to_owned(), + }, + arguments: Vec::new(), + } + } + + fn list(element: SemanticType) -> SemanticType { + SemanticType::List { + element: Box::new(element), + } + } + + fn option(value: SemanticType) -> SemanticType { + SemanticType::Option { + value: Box::new(value), + } + } + + /// `A | c1 : List (Option A) -> List A -> A | c2 : Option (List A) -> A * A -> A + /// | c3 : Result A Nat -> A`: the motives of the recursor the pinned Lean + /// 4.32.1 generates for it are, in order, `A`, `List (Option A)`, + /// `List A`, `Option (List A)`, `A * A`, `Except Nat A`, `Option A` + /// (checked with `#check @A.rec`): the maximal nested applications of the + /// fields of `A`'s constructors in field order, and only then those found + /// in the constructors of the members found before. The constructors are + /// named out of alphabetical order, to show that declaration order decides. + #[test] + fn the_family_is_discovered_in_the_order_of_the_pinned_recursors_motives() { + let a = named("A"); + let pair = SemanticType::Product { + left: Box::new(a.clone()), + right: Box::new(a.clone()), + }; + let result = SemanticType::Result { + ok: Box::new(a.clone()), + error: Box::new(SemanticType::Nat), + }; + let order = ["A.z1", "A.y2", "A.x3"]; + let fields = [ + vec![list(option(a.clone())), list(a.clone())], + vec![option(list(a.clone())), pair.clone()], + vec![result.clone()], + ]; + let mut env = Environment::default(); + env.types.insert( + "A".to_owned(), + TypeInfo { + constructors: order.iter().map(|name| ((*name).to_owned(), 0)).collect(), + constructor_types: order + .iter() + .map(|name| (*name).to_owned()) + .zip(fields) + .collect(), + constructor_order: order.iter().map(|name| (*name).to_owned()).collect(), + group: vec!["A".to_owned()], + ..TypeInfo::default() + }, + ); + let expected = vec![ + a.clone(), + list(option(a.clone())), + list(a.clone()), + option(list(a.clone())), + pair.clone(), + result, + option(a.clone()), + ]; + assert_eq!(family_members(&["A".to_owned()], &[], &env), expected); + // Every member, whichever container it is, belongs to the family of + // the type; a list of another element type does not. + for member in &expected { + assert_eq!( + family_of(member, &env), + Some(Family::Group(expected.clone())) + ); + } + assert_eq!( + family_of(&list(SemanticType::Nat), &env), + Some(Family::List(SemanticType::Nat)) + ); + } + + /// A product's own constructor matters when the product holds a nested + /// application: `B | mk : List (Nat * List B) -> B` has the members `B`, + /// `List (Nat * List B)`, `Nat * List B` and `List B`, the last found only + /// through the product's field. + #[test] + fn a_member_found_inside_a_product_joins_the_family() { + let b = named("B"); + let inner = list(b.clone()); + let pair = SemanticType::Product { + left: Box::new(SemanticType::Nat), + right: Box::new(inner.clone()), + }; + let outer = list(pair.clone()); + let mut env = Environment::default(); + env.types.insert( + "B".to_owned(), + TypeInfo { + constructors: [("B.mk".to_owned(), 1)].into_iter().collect(), + constructor_types: [("B.mk".to_owned(), vec![outer.clone()])] + .into_iter() + .collect(), + constructor_order: vec!["B.mk".to_owned()], + group: vec!["B".to_owned()], + ..TypeInfo::default() + }, + ); + assert_eq!( + family_members(&["B".to_owned()], &[], &env), + vec![b, outer, pair, inner] + ); + } +} + #[cfg(test)] mod alpha_order_tests { use std::collections::BTreeMap; diff --git a/tests/nested-family/lake-manifest.json b/tests/nested-family/lake-manifest.json new file mode 100644 index 0000000000..c1792f8cb5 --- /dev/null +++ b/tests/nested-family/lake-manifest.json @@ -0,0 +1,6 @@ +{"version": "1.2.0", + "packagesDir": ".lake/packages", + "packages": [], + "name": "nested_family_host", + "lakeDir": ".lake", + "fixedToolchain": false} diff --git a/tests/nested-family/lakefile.toml b/tests/nested-family/lakefile.toml new file mode 100644 index 0000000000..f316520dcb --- /dev/null +++ b/tests/nested-family/lakefile.toml @@ -0,0 +1,2 @@ +name = "nested_family_host" +version = "0.1.0" diff --git a/tests/nested-family/lean-toolchain b/tests/nested-family/lean-toolchain new file mode 100644 index 0000000000..2b61957be5 --- /dev/null +++ b/tests/nested-family/lean-toolchain @@ -0,0 +1 @@ +leanprover/lean4:v4.32.1 diff --git a/tests/nested-family/lexlean.lock b/tests/nested-family/lexlean.lock new file mode 100644 index 0000000000..000ea85e60 --- /dev/null +++ b/tests/nested-family/lexlean.lock @@ -0,0 +1,47 @@ +spec = "lexlean/lock/2" +language = "1.2" +compiler_semantics = "30c5829bdd3f072456985b81111b40c8d20bfeaaa66c908df679c034ec32e032" +project_config_sha256 = "c768044c483ee615bc8470523ad6c606151bc22ba532d551717190c2e578cba3" +lean_toolchain = "leanprover/lean4:v4.32.1" + +[[workspace_file]] +path = "lake-manifest.json" +sha256 = "93b88863850321d497a73cc41ece16a5296e9b3540b8cdf82e7168c44d8821e8" + +[[workspace_file]] +path = "lakefile.toml" +sha256 = "55ab24f850b4b7b2c9d46a6b0265af45fdbff1c14b090448b86d6e88cb68bffc" + +[[workspace_file]] +path = "lean-toolchain" +sha256 = "8e3538e0ab5f81a3ee04927d8838c8c674e0e112838b4b3ce87ec218143276af" + +[[package]] +id = "lexlean.core" +version = "1.2.0" +kind = "builtin" +source = "embedded" +revision = "compiler-semantics" +tree_sha256 = "827317cae68c162f3a96c8a8de4e53ab348e7d3724fd2b834fa85ecde1a1b392" +manifest_sha256 = "271079763d269cc873531f515e56da960cd6321393b9460ff0e617666fa01d71" +imports = [] + +[[package]] +id = "lexlean.std.bool" +version = "1.2.0" +kind = "builtin" +source = "embedded" +revision = "compiler-semantics" +tree_sha256 = "3bc0fb09d706ffa7d5a1363302ea87c049acc7c7dcafde63c62682620c11be9b" +manifest_sha256 = "de58de2cf18a7a5a62d8c20649139d6dfe1f7b16b3130c75f150fbac5440a71e" +imports = ["lexlean.core@1.2.0"] + +[[package]] +id = "lexlean.std.nat" +version = "1.2.0" +kind = "builtin" +source = "embedded" +revision = "compiler-semantics" +tree_sha256 = "e0db021e01b91d651c43921d66f6b0023c8efbf17cac2ec53192ce5305e4e1ac" +manifest_sha256 = "6fb446b39e3c1c18a5f10cfa1f7aa2bd431837719eb0eb7489aadf452c9420a7" +imports = ["lexlean.core@1.2.0", "lexlean.std.bool@1.2.0"] diff --git a/tests/nested-family/lexlean.toml b/tests/nested-family/lexlean.toml new file mode 100644 index 0000000000..9a630b2d7c --- /dev/null +++ b/tests/nested-family/lexlean.toml @@ -0,0 +1,27 @@ +spec = "lexlean/project/1" +name = "nested-family" +language = "1.2" +module_prefix = "NestedFamily" +source_roots = ["src"] +entrypoints = ["src/Main.lex.tex"] +build_root = ".lexlean" +lockfile = "lexlean.lock" +lean_workspace = "." +lean_toolchain = "leanprover/lean4:v4.32.1" + +[[lexicon_source]] +package = "lexlean.std.nat" +kind = "builtin" + +[limits] +max_file_bytes = 4194304 +max_total_source_bytes = 67108864 +max_primitive_atoms = 2000000 +max_token_lattice_edges = 4000000 +max_parse_states = 4000000 +max_ir_nodes = 2000000 +max_scope_depth = 1024 +max_import_depth = 128 +max_diagnostics = 256 +max_child_output_bytes = 16777216 +child_timeout_ms = 300000 diff --git a/tests/nested-family/src/Main.lex.tex b/tests/nested-family/src/Main.lex.tex new file mode 100644 index 0000000000..0b19d11875 --- /dev/null +++ b/tests/nested-family/src/Main.lex.tex @@ -0,0 +1,117 @@ +\begin{lexlean}{Main} +\useglossary{lexlean.std.nat@1.2.0} +\title{Natural number addition} +\begin{semanticmodule} +inductive{ + name: Json, + type_parameters: [], + parameters: [], + constructors: [ + {name: null, fields: []}, + {name: text, fields: [String]}, + {name: array, fields: [(List Json)]}, + {name: object, fields: [(List (String * Json))]} + ] +} + +definition{ + name: size, + parameters: [value : Json], + result: Nat, + body: (match value + | Json.null => 1 + | Json.text content => 1 + | Json.array items => (1 + (sizeList items)) + | Json.object entries => (1 + (sizeEntries entries))), + mutual: Size, + recursive_argument: value +} + +definition{ + name: sizeList, + parameters: [items : (List Json)], + result: Nat, + body: (match items | List.nil => 0 | List.cons head tail => ((size head) + (sizeList tail))), + mutual: Size, + recursive_argument: items +} + +definition{ + name: sizeEntries, + parameters: [entries : (List (String * Json))], + result: Nat, + body: (match entries + | List.nil => 0 + | List.cons head tail => ((sizeEntry head) + (sizeEntries tail))), + mutual: Size, + recursive_argument: entries +} + +definition{ + name: sizeEntry, + parameters: [entry : (String * Json)], + result: Nat, + body: (match entry | Prod.mk key value => (size value)), + mutual: Size, + recursive_argument: entry +} + +definition{ + name: leaves, + parameters: [value : Json], + result: Nat, + body: (match value + | Json.null => 1 + | Json.text content => 1 + | Json.array items => (leavesList items) + | Json.object entries => (leavesEntries entries)), + mutual: Leaves, + recursive_argument: value +} + +definition{ + name: leavesList, + parameters: [items : (List Json)], + result: Nat, + body: (match items | List.nil => 0 | List.cons head tail => ((leaves head) + (leavesList tail))), + mutual: Leaves, + recursive_argument: items +} + +definition{ + name: leavesEntries, + parameters: [entries : (List (String * Json))], + result: Nat, + body: (match entries + | List.nil => 0 + | List.cons head tail => ((leavesEntry head) + (leavesEntries tail))), + mutual: Leaves, + recursive_argument: entries +} + +definition{ + name: leavesEntry, + parameters: [entry : (String * Json)], + result: Nat, + body: (match entry | Prod.mk key value => (leaves value)), + mutual: Leaves, + recursive_argument: entry +} + +definition{ + name: sample, + parameters: [], + result: Json, + body: (ctor Json.object (cons ("a", (ctor Json.array (cons (ctor Json.null) (cons (ctor Json.text "x") (nil Json))))) (cons ("b", (ctor Json.null)) (nil (String * Json))))) +} + +theorem{name: sample_size, parameters: [], statement: ((size (sample)) = 5), proof: reflexivity{}} + +theorem{ + name: sample_leaves, + parameters: [], + statement: ((leaves (sample)) = 3), + proof: reflexivity{} +} +\end{semanticmodule} +\end{lexlean} From 91551e95aa6f4aba162fd030fa06796a1fbcb093 Mon Sep 17 00:00:00 2001 From: Alex Flom Date: Thu, 22 Oct 2026 20:01:27 -0600 Subject: [PATCH 3/8] fix(ir): a recursive family counts a group only at its own type arguments (DF-19, #278 review) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit mentions_family_group ignored type arguments, so a group used at one of its own types got a family the recursor does not have: `Rose Item | node : Item -> List (Rose Item)` at `Rose (Rose Nat)` yielded four members, while `Rose.rec` has two motives at any instantiation. It now takes the family's arguments and counts a named type of the group only at those arguments, searching the arguments of any other application. Also drops the unreachable constructor_order.is_empty() fallback of family_constructor_fields (with the reason) and comments why a branch whose binder types cannot be taken is harmless in the recursion check. Unit test a_group_instantiated_at_its_own_type_has_two_motives; SPEC §17.12 recursion rule 2 and VERIFICATION.md state the rule and the plant. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01F4DJnZorC2Jd4MJfN9DbZk --- SPEC.md | 8 ++- VERIFICATION.md | 16 ++++++ crates/lexlean/src/ir/semantic.rs | 93 +++++++++++++++++++++++++++---- 3 files changed, 103 insertions(+), 14 deletions(-) diff --git a/SPEC.md b/SPEC.md index 05af5a00e5..d598a2da30 100644 --- a/SPEC.md +++ b/SPEC.md @@ -3015,10 +3015,14 @@ Termination evidence is semantic data, never tactic or backend text; no holds a member: scanning the members in order, and the constructor fields of each in the order the pinned Lean takes the constructors (declaration order for a document inductive; `nil`, `cons`; `none`, `some`; `error`, - `ok`; `mk`), each field that mentions the group without being a member is + `ok`; `mk`), each field that mentions a member without being one is a maximal application of `List`, `Option`, `Result`, or a product (by strict positivity, §17.12 recursive-data rule 3) and joins the family - once, at its first occurrence. These are the motives of the recursor the + once, at its first occurrence. A member is the group at these type + arguments: the group applied to others is another type and joins nothing, + so `Rose Item | node : Item -> List (Rose Item)` at `Rose (Rose Nat)` has + the two members `Rose (Rose Nat)` and `List (Rose (Rose Nat))`, as + `Rose.rec` has two motives at any instantiation. These are the motives of the recursor the pinned Lean 4.32.1 generates for the group (`#check @T.rec`), in its order: for `Json` with `array (List Json)` and `object (List (String * Json))`, `Json`, `List Json`, `List (String * Json)`, and `String * diff --git a/VERIFICATION.md b/VERIFICATION.md index 691b21b159..fff42d3ebd 100644 --- a/VERIFICATION.md +++ b/VERIFICATION.md @@ -839,6 +839,22 @@ same project is refused at its first group: `mutual group `Size` members decrease on different recursive families` (the old `family_of` saw a list of pairs as a list of one element type). +A group used at one of its own types has the family of its own arguments +only (maintainer review of this change): `Rose Item | node : Item -> List +(Rose Item)` at `Rose (Rose Nat)` has the two members `Rose (Rose Nat)` and +`List (Rose (Rose Nat))`, as `Rose.rec` has two motives at any +instantiation. `mentions_family_group` takes the type arguments of the +family and counts a named type of the group only at those arguments. The unit +test `a_group_instantiated_at_its_own_type_has_two_motives` pins it. + +Planted: `mentions_family_group` ignored the arguments again (the comparison +`observed.as_slice() == arguments` replaced by `true`). Command: +`cargo test -p lexlean --lib family_`. + +```text +test ir::semantic::family_order_tests::a_group_instantiated_at_its_own_type_has_two_motives ... FAILED +``` + ### recursion call graph check can fail Planted: `check_group_call_graph` returned `Ok(())` at once, so a mutual diff --git a/crates/lexlean/src/ir/semantic.rs b/crates/lexlean/src/ir/semantic.rs index 428600f32a..e96d675e0e 100644 --- a/crates/lexlean/src/ir/semantic.rs +++ b/crates/lexlean/src/ir/semantic.rs @@ -5292,14 +5292,22 @@ fn type_children(ty: &SemanticType) -> Vec<&SemanticType> { } } -/// Whether `ty` mentions a member of the group named by `keys`. -fn mentions_family_group(ty: &SemanticType, keys: &[String]) -> bool { - if matches!(ty, SemanticType::Named { member, .. } if keys.contains(&member_key(member))) { +/// Whether `ty` mentions a member of the group named by `keys` at +/// `arguments`: a type of the group applied to other arguments is another +/// type, not a member (`Rose Nat` inside `Rose (Rose Nat)`), and the +/// recursor of the group has no motive for it. The search goes on into the +/// arguments of such a type, which may hold a member. +fn mentions_family_group(ty: &SemanticType, keys: &[String], arguments: &[SemanticType]) -> bool { + if matches!( + ty, + SemanticType::Named { member, arguments: observed } + if keys.contains(&member_key(member)) && observed.as_slice() == arguments + ) { return true; } type_children(ty) .into_iter() - .any(|child| mentions_family_group(child, keys)) + .any(|child| mentions_family_group(child, keys, arguments)) } /// The constructor field types of one member of a recursive family, at the @@ -5313,13 +5321,12 @@ fn family_constructor_fields(ty: &SemanticType, env: &Environment<'_>) -> Vec = if info.constructor_order.is_empty() { - info.constructor_types.keys().collect() - } else { - info.constructor_order.iter().collect() - }; - return names - .into_iter() + // Every member of a recursive family is a document inductive, and + // those keep their declaration order; a structure, the only type + // without one, is in no group (`group_key_of`) and is never a member. + return info + .constructor_order + .iter() .filter_map(|name| info.constructor_types.get(name)) .flatten() .map(|field| substitute_type(field, &map)) @@ -5364,7 +5371,7 @@ fn family_members( let mut index = 0; while index < members.len() { for field in family_constructor_fields(&members[index], env) { - if mentions_family_group(&field, keys) && !members.contains(&field) { + if mentions_family_group(&field, keys, arguments) && !members.contains(&field) { members.push(field); } } @@ -6898,6 +6905,12 @@ fn check_term( // constructor of the matched family member, whether // that member is a document type, a list, an option, // a result, or a product. + // A branch whose binder types cannot be taken (a + // constructor of another type, a wrong binder count) + // marks no binder smaller. That is harmless: a call + // that needed such a binder is refused LLT4001 as not + // decreasing, and the branch itself is refused by the + // match check that reports the mismatch. if let Ok(fields) = branch_binder_types(decreasing, branch, env) { for (binder, field) in branch.binders.iter().zip(&fields) { if in_family(field, family) { @@ -9985,6 +9998,62 @@ mod family_order_tests { vec![b, outer, pair, inner] ); } + + /// `Rose Item | node : Item -> List (Rose Item) -> Rose Item`, used at + /// one of its own types: `Rose.rec` has two motives at any instantiation, + /// `Rose A` and `List (Rose A)`, so at `Rose (Rose Nat)` the family is + /// `Rose (Rose Nat)` and `List (Rose (Rose Nat))`, and `Rose Nat`, which + /// the field `Item` becomes, is another type that joins nothing. + #[test] + fn a_group_instantiated_at_its_own_type_has_two_motives() { + let item = SemanticType::Parameter { + name: "Item".to_owned(), + }; + let rose = |argument: SemanticType| SemanticType::Named { + member: MemberRef { + module: None, + name: "Rose".to_owned(), + }, + arguments: vec![argument], + }; + let mut env = Environment::default(); + env.types.insert( + "Rose".to_owned(), + TypeInfo { + parameters: 1, + type_parameters: vec!["Item".to_owned()], + constructors: [("Rose.node".to_owned(), 2)].into_iter().collect(), + constructor_types: [("Rose.node".to_owned(), vec![item.clone(), list(rose(item))])] + .into_iter() + .collect(), + constructor_order: vec!["Rose.node".to_owned()], + group: vec!["Rose".to_owned()], + ..TypeInfo::default() + }, + ); + let keys = ["Rose".to_owned()]; + let nat = SemanticType::Nat; + let rose_nat = rose(nat.clone()); + let rose_rose_nat = rose(rose_nat.clone()); + assert_eq!( + family_members(&keys, &[nat], &env), + vec![rose_nat.clone(), list(rose_nat.clone())] + ); + let expected = vec![rose_rose_nat.clone(), list(rose_rose_nat.clone())]; + assert_eq!(family_members(&keys, &[rose_nat.clone()], &env), expected); + // The members of that family belong to it, and `List (Rose Nat)` is + // the other family's. + for member in &expected { + assert_eq!( + family_of(member, &env), + Some(Family::Group(expected.clone())) + ); + } + assert_eq!( + family_of(&list(rose_nat.clone()), &env), + Some(Family::Group(vec![rose_nat.clone(), list(rose_nat)])) + ); + } } #[cfg(test)] From 9d0896b18ce4be24eb2d2f808a80c177785305a2 Mon Sep 17 00:00:00 2001 From: Alex Flom Date: Thu, 22 Oct 2026 20:30:49 -0600 Subject: [PATCH 4/8] test(ir): clippy cloned_ref_to_slice_refs in the Rose (Rose Nat) family test (DF-19, #278) Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01F4DJnZorC2Jd4MJfN9DbZk --- crates/lexlean/src/ir/semantic.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/lexlean/src/ir/semantic.rs b/crates/lexlean/src/ir/semantic.rs index e96d675e0e..9619675101 100644 --- a/crates/lexlean/src/ir/semantic.rs +++ b/crates/lexlean/src/ir/semantic.rs @@ -10040,7 +10040,10 @@ mod family_order_tests { vec![rose_nat.clone(), list(rose_nat.clone())] ); let expected = vec![rose_rose_nat.clone(), list(rose_rose_nat.clone())]; - assert_eq!(family_members(&keys, &[rose_nat.clone()], &env), expected); + assert_eq!( + family_members(&keys, std::slice::from_ref(&rose_nat), &env), + expected + ); // The members of that family belong to it, and `List (Rose Nat)` is // the other family's. for member in &expected { From 4eb5d9b296a1612273cfa341ecc4ac194f886a42 Mon Sep 17 00:00:00 2001 From: Alexander Flom Date: Tue, 3 Nov 2026 14:36:43 +0000 Subject: [PATCH 5/8] Refresh DF-19 fixture lock and record bounded integration evidence --- VERIFICATION.md | 27 +++++++++++++++++++++++++++ tests/nested-family/lexlean.lock | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/VERIFICATION.md b/VERIFICATION.md index a3c67f6efc..74e4e9a5c9 100644 --- a/VERIFICATION.md +++ b/VERIFICATION.md @@ -12634,3 +12634,30 @@ weighting makes its partition assertion fail. This is a harness correction, not a change to the generated packages or a resolution of #164's consumer compiler-nesting question. A Windows rerun is required to establish whether this bounded partition removes the observed compiler failure. + +## DF-19 preparation against the current release line (#284) + +Local integration `7bf1ce27b0561482ee9e6ca4d3e8745ae2dc42f7` combines the +accepted argument-aware DF-19 head `9d0896b18` with release `0a01df682` and +the reviewed maintenance integration `215a9d591`. Source conflicts were +absent; the register and its count sites are 470. The official model writer +produced no document delta. Formatting, model audits (470 IDs, 86 codes), +specification links, all five BDD meta-tests, and all three family-order unit +tests pass on this integration. + +The exact-tree CLI was copied before any writer ran; its SHA-256 is +`88b47a00bde41bd291af5e23781e82bc460fc721057dbbd20f41dfb42cb094d5`. +Its official `lock` command changes only `tests/nested-family/lexlean.lock`'s +compiler-semantics pin from the historical `30c5829b` identity to the current +release identity `aac30cb0`. No embedded identity input or renderer source +changes, and no global identity or oracle regeneration was performed. + +That frozen CLI's direct `check`, `build`, and `verify` commands on +`tests/nested-family` pass with pinned Lean 4.32.1 from the configured +`ELAN_HOME`. Verification accepts one module and publishes attestation +`ac3f24866cca9e7477eeccc610f6b7c8637d383e1104c38d2b7e43e906a2743b`. +This is direct CLI evidence for the committed fixture, including its sample +size and leaf-count theorems. The Lean-backed DF-19 conformance harness and +its four refusal plants were not rerun in this phase: its host helper expects +`HOME/.elan`, absent on this environment. Neither home nor the helper was +changed. No full acceptance gate or inventory run is claimed here. diff --git a/tests/nested-family/lexlean.lock b/tests/nested-family/lexlean.lock index 000ea85e60..63ef04b82f 100644 --- a/tests/nested-family/lexlean.lock +++ b/tests/nested-family/lexlean.lock @@ -1,6 +1,6 @@ spec = "lexlean/lock/2" language = "1.2" -compiler_semantics = "30c5829bdd3f072456985b81111b40c8d20bfeaaa66c908df679c034ec32e032" +compiler_semantics = "aac30cb0bda069a55aca6ce2417be48726fd578218b3e8fbaf31634694695b64" project_config_sha256 = "c768044c483ee615bc8470523ad6c606151bc22ba532d551717190c2e578cba3" lean_toolchain = "leanprover/lean4:v4.32.1" From ca5d375bd7dc8890b682ea8dc874c9e71beb76a7 Mon Sep 17 00:00:00 2001 From: Alexander Flom Date: Tue, 3 Nov 2026 14:55:17 +0000 Subject: [PATCH 6/8] Record complete Lean-backed DF-19 integration validation --- VERIFICATION.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/VERIFICATION.md b/VERIFICATION.md index 74e4e9a5c9..f7abe928cd 100644 --- a/VERIFICATION.md +++ b/VERIFICATION.md @@ -12661,3 +12661,21 @@ size and leaf-count theorems. The Lean-backed DF-19 conformance harness and its four refusal plants were not rerun in this phase: its host helper expects `HOME/.elan`, absent on this environment. Neither home nor the helper was changed. No full acceptance gate or inventory run is claimed here. + +### DF-19's complete Lean-backed case after toolchain registration + +After the existing pinned toolchain was registered through official elan in +this user's standard elan home, the previously unrun exact +`cargo test --offline --locked -p repo-conformance --test conformance +conformance_df_19 -- --exact --nocapture` passes on prepared integration +`4eb5d9b296a1612273cfa341ecc4ac194f886a42`: one passed, zero failed or +ignored, in 3.56 seconds (compilation 57.55 seconds). + +This Linux x86-64 run enters the unchanged `lean_backed` helper's real-Lean +branch: the standard-home Lean, Lake and leanchecker paths resolve to the +existing pinned installation under `/workspace/.setup/elan`. The case verifies +the committed nested-family fixture, checks its rendered mutual structural +recursion, and executes all four existing `LLT4001` refusal plants and their +no-backend-output assertions. No helper, gate, home value or source assertion +was changed. This completes that focused case; no full acceptance run is +claimed by this record. From 177c499a1bd45c60e2712a5f375b9b4633e1387d Mon Sep 17 00:00:00 2001 From: Alexander Flom Date: Wed, 4 Nov 2026 02:34:53 +0000 Subject: [PATCH 7/8] docs: include DF-19 in README coverage range --- README.md | 2 +- VERIFICATION.md | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bf6bb814c3..b89ef03f23 100644 --- a/README.md +++ b/README.md @@ -173,7 +173,7 @@ Every row is validated by `just vv`; the IDs link the claim to its register row, | Versioned lexicon packages with closed schemas, denotations, and renderer tokens | `GL-01`..`GL-18` | `build` | | Fixed structural, mathematical, and proposition grammar with closed ambiguity handling | `GR-01`..`GR-16` | `build` | | Typed closed IR with canonical serialization, native core modules (and the second schema of them that lossless migration of the proof library needs), portable language-1.1 application data and operations, versioned language-1.2 semantic modules, products, first-class functions, and widening conversions, ordered collections and graphs, semantic snapshots, alpha identities, recursion evidence, a split on a document term or a Boolean in a proof, and content identities | `SM-01`..`SM-36` | `build` | -| Document and generic semantic declarations with exact self-application, type checking, structural recursion, recursive and mutual language-1.2 data, generic and executable higher-order definitions, mutual and well-founded recursion with semantic evidence, explicit state threading, and acyclicity rules | `DF-01`..`DF-18` | `build` | +| Document and generic semantic declarations with exact self-application, type checking, structural recursion, recursive and mutual language-1.2 data, generic and executable higher-order definitions, mutual and well-founded recursion with semantic evidence, explicit state threading, and acyclicity rules | `DF-01`..`DF-19` | `build` | | The structured proof language with pinned Lean lowerings, including closed linear arithmetic, integer order, and the fold laws | `PF-01`..`PF-23` | `build` | | Prose-free deterministic generated Lean with complete token traceability | `LN-01`..`LN-12` | `build` | | Canonical LaTeX regeneration and the optional hash-checked PDF provider | `TX-01`..`TX-12` | `build` | diff --git a/VERIFICATION.md b/VERIFICATION.md index 4153a8b546..0e0fc4b0b9 100644 --- a/VERIFICATION.md +++ b/VERIFICATION.md @@ -12762,3 +12762,16 @@ recursion, and executes all four existing `LLT4001` refusal plants and their no-backend-output assertions. No helper, gate, home value or source assertion was changed. This completes that focused case; no full acceptance run is claimed by this record. + +### README coverage correction found by final-head CI + +Run `41753808309` at `9f3ca0006fce13c2a61285fe3cc9e4088b797bce` +found that README's whole-family `DF-01`..`DF-18` range omitted registered +`DF-19`. Windows job `126326526519` and ARM Linux job `126326526543` +each passed 469 conformance cases and failed only `RP-11`; the rest partition, +job `126326527233`, passed 283 and failed only `RP-11`. + +The README range now ends at `DF-19`, retaining the whole-family coverage +check. This correction changes no implementation, model, schema or identity +input. No local validation of this correction is claimed here; targeted +`RP-11` validation and complete changed-head CI remain required. From 708f86dea06194070b59fae57cbb83aaf8c81f00 Mon Sep 17 00:00:00 2001 From: Alexander Flom Date: Wed, 4 Nov 2026 04:35:52 +0000 Subject: [PATCH 8/8] Bound generated differential debug information to repository policy (#436) --- VERIFICATION.md | 26 +++++++++++++++++++ .../src/cases/bootstrap/rust_validate.rs | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/VERIFICATION.md b/VERIFICATION.md index 0e0fc4b0b9..b395e61c62 100644 --- a/VERIFICATION.md +++ b/VERIFICATION.md @@ -12775,3 +12775,29 @@ The README range now ends at `DF-19`, retaining the whole-family coverage check. This correction changes no implementation, model, schema or identity input. No local validation of this correction is claimed here; targeted `RP-11` validation and complete changed-head CI remain required. + +### BS-15 generated workspace disk exhaustion in final-head CI + +Run `41761755141` at `177c499a1bd45c60e2712a5f375b9b4633e1387d` +passed `conformance_rp_11` in rest job `126352276554` and ARM Linux job +`126352276081`; their conformance totals were 284 and 470 passed, respectively, +with no failures. Bootstrap-package job `126352276702` instead failed +`conformance_bs_15` after 5481.51 seconds while Cargo built its generated +workspace. Writing the dependency information for harness +`run_boot_render_package_std_494` failed with `No space left on device +(os error 28)`; another concurrent harness link reported a bus error. The +last disk sample showed 1.6 GiB available. Before the build failure, the +evaluator agreed on 2,484 manifests and 241 renders and reported the existing +plants detected. The failed case is not acceptance evidence. + +The differential workspace is independent of the repository workspace and +did not inherit its `[profile.dev]` setting. Its generated manifest now sets +`debug = "line-tables-only"`, exactly as the repository does, so every harness +retains file and line information without full variable debug information. +This changes no corpus, harness chunking, assertions, optimization setting, +overflow behavior, Rust stack setting or stack-bound observation. Cargo still +builds every generated workspace member and every outcome remains compared. + +This is a source-only resource correction. Its disk savings have not been +measured, and no local build or runtime validation of the correction is +claimed. Exact BS-15 validation and complete changed-head CI remain required. diff --git a/crates/conformance/src/cases/bootstrap/rust_validate.rs b/crates/conformance/src/cases/bootstrap/rust_validate.rs index 586b27f0a3..a0746ed816 100644 --- a/crates/conformance/src/cases/bootstrap/rust_validate.rs +++ b/crates/conformance/src/cases/bootstrap/rust_validate.rs @@ -2689,7 +2689,7 @@ pub(super) fn rustc_differential( std::fs::write( dir.path().join("Cargo.toml"), format!( - "[workspace]\nresolver = \"2\"\nmembers = [{}]\n", + "[workspace]\nresolver = \"2\"\nmembers = [{}]\n\n[profile.dev]\ndebug = \"line-tables-only\"\n", members.join(", ") ), )