diff --git a/CHANGELOG.md b/CHANGELOG.md index 21f26d365c..531dadd819 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,15 @@ workspace carried at those versions without a tag. ## 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 470 IDs. + - The Intel macOS host-gate cost correction for #235 keeps SP-02's complete certificate scaling families and TC-03's seeded argument/fuel sweep on the normative Linux host, with fixed certificate vectors and committed fixture diff --git a/CONFORMANCE.md b/CONFORMANCE.md index 61e2c8d1ea..cc7adff552 100644 --- a/CONFORMANCE.md +++ b/CONFORMANCE.md @@ -163,6 +163,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 0fa8e56613..b89ef03f23 100644 --- a/README.md +++ b/README.md @@ -153,7 +153,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 469 registered conformance IDs are implemented and pass; `just vv` runs clean from a checkout with the pinned toolchain installed. +All 470 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. @@ -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/SPEC.md b/SPEC.md index e0bfaa464e..55948a66d2 100644 --- a/SPEC.md +++ b/SPEC.md @@ -3012,10 +3012,25 @@ 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 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. 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 * + 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 @@ -11122,6 +11137,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 | @@ -11456,7 +11472,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:** 469. +**Total required capability IDs:** 470. 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/VERIFICATION.md b/VERIFICATION.md index 9a8e40ff1c..b395e61c62 100644 --- a/VERIFICATION.md +++ b/VERIFICATION.md @@ -794,6 +794,69 @@ 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). + +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 @@ -12654,3 +12717,87 @@ 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. + +### 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. + +### 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. + +### 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(", ") ), ) 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/conformance/src/cases/repository.rs b/crates/conformance/src/cases/repository.rs index e8c834af3c..3bdbdda10d 100644 --- a/crates/conformance/src/cases/repository.rs +++ b/crates/conformance/src/cases/repository.rs @@ -873,7 +873,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(), 469, "§31 has 469 rows"); + assert_eq!(table.len(), 470, "§31 has 470 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 709f156ea3..1b0ce42c22 100644 --- a/crates/conformance/tests/conformance.rs +++ b/crates/conformance/tests/conformance.rs @@ -690,6 +690,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/crates/lexlean/src/ir/semantic.rs b/crates/lexlean/src/ir/semantic.rs index d8563ffe68..0d028cabf6 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,213 @@ 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` 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, arguments)) +} + +/// 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); + // 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)) + .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, arguments) && !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 +6901,19 @@ 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. + // 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, env) { + if in_family(field, family) { branch_smaller.insert(binder.clone()); } } @@ -9068,6 +9209,12 @@ impl SemanticModule { }) .collect(), class: false, + constructor_order: constructors + .iter() + .map(|constructor| { + format!("{}.{}", declaration.name(), constructor.name) + }) + .collect(), ..TypeInfo::default() }, ); @@ -9735,6 +9882,190 @@ 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] + ); + } + + /// `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, 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 { + 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)] mod alpha_order_tests { use std::collections::BTreeMap; 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 1311d8b0b9..da20301608 100644 --- a/model/ids.toml +++ b/model/ids.toml @@ -829,6 +829,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/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..63ef04b82f --- /dev/null +++ b/tests/nested-family/lexlean.lock @@ -0,0 +1,47 @@ +spec = "lexlean/lock/2" +language = "1.2" +compiler_semantics = "aac30cb0bda069a55aca6ce2417be48726fd578218b3e8fbaf31634694695b64" +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} diff --git a/xtask/src/spec_links.rs b/xtask/src/spec_links.rs index 6eea6aecad..1957d248c8 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() != 469 { - return Err(format!("RP-07: the §31 table has {} rows, not 469", rows.len()).into()); + if rows.len() != 470 { + return Err(format!("RP-07: the §31 table has {} rows, not 470", 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.