From 53237fa2168a7dd7c4b86619adf83cefbd20d9bb Mon Sep 17 00:00:00 2001 From: humuhumu33 Date: Thu, 12 Nov 2026 21:10:15 +0000 Subject: [PATCH 1/3] BS-52: the conservative checker of LSE expressions as source (#269) LseTypes holds checkSignature and checkValue to lse::check_signature and check_value: the zonked type and the type of every natural literal, or the first error, with the oracle's metavariable numbering and capture-avoiding renames. The lookup closure is handed over as rows and a fallback (Missing for a closure, Opaque for a package as it loads). Each recursive definition is structural over a chain built from a bound on nesting the host passes, so both roots admit recursion. 20 planted defects are each detected by their own vector. Co-Authored-By: Claude Opus 5.5 --- .github/bdd-parts.json | 3 + .github/intel-host-lanes.json | 2 +- .github/vv-rows.json | 9 + CHANGELOG.md | 8 + CONFORMANCE.md | 1 + README.md | 4 +- SPEC.md | 20 +- VERIFICATION.md | 50 + bootstrap/README.md | 3 + bootstrap/lexlean.lock | 57 - bootstrap/lexlean.toml | 2 +- bootstrap/src/LseTypes.lex.tex | 1217 +++++++++++++++++ crates/conformance/src/cases/bootstrap.rs | 2 + .../src/cases/bootstrap/lexicon_lse.rs | 4 +- .../src/cases/bootstrap/lse_types.rs | 916 +++++++++++++ crates/conformance/src/cases/repository.rs | 2 +- crates/conformance/tests/conformance.rs | 5 + features/suites/bootstrap.feature | 7 + model/ids.toml | 6 + tests/production-export/bootstrap/roots.json | 16 + xtask/src/spec_links.rs | 4 +- 21 files changed, 2271 insertions(+), 67 deletions(-) delete mode 100644 bootstrap/lexlean.lock create mode 100755 bootstrap/src/LseTypes.lex.tex create mode 100755 crates/conformance/src/cases/bootstrap/lse_types.rs diff --git a/.github/bdd-parts.json b/.github/bdd-parts.json index f385106a69..1a69193ab0 100644 --- a/.github/bdd-parts.json +++ b/.github/bdd-parts.json @@ -277,6 +277,9 @@ ], "bootstrap-manifest-text": [ "BS-34" + ], + "bootstrap-lse-types": [ + "BS-52" ] }, "targets": { diff --git a/.github/intel-host-lanes.json b/.github/intel-host-lanes.json index 6a7f0aa94a..647032cdbb 100644 --- a/.github/intel-host-lanes.json +++ b/.github/intel-host-lanes.json @@ -1,7 +1,7 @@ { "producer": ["honesty"], "lanes": { - "lower": ["bootstrap-lower", "bootstrap-lexicon-lse", "bootstrap-lexicon-lre", "bootstrap-surface-module", "bootstrap-diagnostics", "bootstrap-cli-output"], + "lower": ["bootstrap-lower", "bootstrap-lexicon-lse", "bootstrap-lexicon-lre", "bootstrap-surface-module", "bootstrap-diagnostics", "bootstrap-cli-output", "bootstrap-lse-types"], "print": ["bootstrap-print", "bootstrap-bodies", "bootstrap-succeed", "bootstrap-loops", "bootstrap-assemble", "bootstrap-package", "bootstrap-check", "bootstrap-gnaf", "bootstrap-runtime", "bootstrap-terms", "bootstrap-tooling-latex", "bootstrap-structural-grammar", "bootstrap-surface-lex", "bootstrap-surface-doc", "bootstrap-surface-generic", "bootstrap-surface-tooling", "bootstrap-lock-text", "bootstrap-content-id", "bootstrap-project-text", "bootstrap-manifest-text"], "validate": ["bootstrap-validate", "compiler", "preservation", "preservation-lists", "preservation-runtime", "preservation-support", "preservation-prims", "preservation-std", "production"], "moves": ["bootstrap-moves", "verification", "rest"] diff --git a/.github/vv-rows.json b/.github/vv-rows.json index 7223ecaf22..c3faa20f76 100644 --- a/.github/vv-rows.json +++ b/.github/vv-rows.json @@ -358,5 +358,14 @@ "width": "", "memory_kib": "", "hints": "" + }, + { + "part": "bdd-bootstrap-lse-types", + "recipes": "bdd", + "examples_part": "", + "bdd_part": "bootstrap-lse-types", + "width": "", + "memory_kib": "", + "hints": "" } ] diff --git a/CHANGELOG.md b/CHANGELOG.md index d9e3754e69..3d894ce24b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,14 @@ workspace carried at those versions without a tag. ## Unreleased +- The conservative checker of LexLean Semantic Expressions as language-1.2 + source, the third slice of the lexicon closure (SPEC.md §17.23, §13.7, + `BS-52`, #269; no new diagnostic code, no change to the language-1.2 + identity). `checkSignature` and `checkValue` of `LseTypes` return what + `lse::check_signature` and `check_value` return, the zonked type and the + literal types or the first error, with what the lookup answers given as a + table. Both admit `recursion`, structural over a bound on nesting the host + passes. - Decision regions (SPEC.md §17.12, `PF-24`, UOR-Foundation/PrismPM#605; no new diagnostic code; changes the language-1.2 identity). The runtime gains a sixth native module, `LexLeanRuntime.Regions`: a checker of local box diff --git a/CONFORMANCE.md b/CONFORMANCE.md index a1d178c889..ca1e70a1de 100644 --- a/CONFORMANCE.md +++ b/CONFORMANCE.md @@ -91,6 +91,7 @@ the error registry does not sanction (R5, `ERRORS.md`). | `BS-32` | `build` | The labeled hash recipes of content identity of SPEC.md §21.1 to §21.5, §11.5, §19.8, and §22.9 (`artifact::content_id`: the frame of a label and a payload, each preceded by its big-endian length in four and eight bytes, under a domain and a NUL; `tree_digest` over path and content rows; `source_id` over the project manifest, the lock, and the sources; `semantic_id_for` over the compiler-semantics digest, the language, the pinned Lean toolchain, the linked IR, and the lexicon closure; `build_id`; `attestation_id`; and `pdf_recipe_id`) is language-1.2 source in the `bootstrap/` project, as six production roots of `ContentId` over the standard library's SHA-256: `treeDigest`, `sourceId`, `semanticId`, `buildId`, `attestationId`, and `pdfRecipeId`, each eligible for `rust-std` with exactly its declared effects, `allocation` and `overflow`, and each with no part of its certificate beyond 3,145,728 bytes; their rendered `rust-std` packages under the pinned rustc, and on the normative host their lowered programs under the calculus evaluator, agree with the oracle to the byte on hand-built inputs that reach every frame of every recipe, lengths beyond one byte, and messages across blocks, on the trees and source inputs of the committed projects, on the source and semantic IDs of every committed build manifest, whose recorded build ID the oracle reproduces, and on seeded inputs; each planted defect in the source is detected by the one vector it names, and the certified packages the production export publishes for the project are the committed ones; choosing and normalizing the files of a tree or a source ID, and serializing the IR, the closure, and the attestation body, are outside the claim. | | `BS-33` | `build` | The canonical text of the project manifest of SPEC.md §10.1 and its digest (`config::ProjectConfig::canonical_toml`: the ten header keys in their fixed order with the pinned Lean toolchain, the lexicon sources sorted by package or the empty array when there is none, the artifact sources and the source packages in their order, the eleven limits in their fixed order, and the PDF provider; and `config_sha256`: the lower-case hex SHA-256 of that text) is language-1.2 source in the `bootstrap/` project, as two production roots of `ProjectText` over the TOML writers of `BS-31`: `canonicalProject` and `configSha256`, each eligible for `rust-std` with exactly its declared effects, `allocation` and `overflow`, and each with no part of its certificate beyond 3,145,728 bytes; their rendered `rust-std` packages under the pinned rustc, and on the normative host their lowered programs under the calculus evaluator, agree with the oracle to the byte on hand-built configurations, which reach every kind of lexicon source, the empty source list, equal packages, every optional table, escapes, and the largest limits, on every committed `lexlean.toml` that parses, and on seeded configurations; each planted defect in the source is detected by the one vector it names, and the certified packages the production export publishes for the project are the committed ones; reading and validating a manifest is outside the claim. | | `BS-34` | `build` | The build manifest of SPEC.md §21.6 in the canonical JSON of §21.7 (`artifact::manifest::BuildManifest::to_json` written as a file: the specification, the compiler version and semantics ID, the language, the project, the source, semantic, and build IDs, the pinned Lean toolchain, the selection and the module rows in their order, a module's origin only when it has one, and the input and output rows sorted by kind, path, length, and digest, each digest in lower-case hex) is language-1.2 source in the `bootstrap/` project, as the production root `buildManifest` of `ManifestText` over the standard library's `Json` writer, eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, and with no part of its certificate beyond 3,145,728 bytes; its rendered `rust-std` package under the pinned rustc, and on the normative host its lowered program under the calculus evaluator, agree with the oracle to the byte on hand-built manifests, which reach every key of the row order, lengths across a byte, a module's origin, and escapes, on every committed build manifest, each of which is the oracle's text of its own fields, and on seeded manifests; each planted defect in the source is detected by the one vector it names, and the certified packages the production export publishes for the project are the committed ones; gathering the rows of a build and hashing its files are outside the claim. | +| `BS-52` | `build` | The conservative checker of LexLean Semantic Expressions of SPEC.md §13.6 to §13.8 and §14.4 (`lexicon::lse::check_signature`: a signature is a type, its applications respect the explicit arities of the signatures they reach, and its natural literals take the types an explicit binder or a solved metavariable gives them; and `check_value`: a defined value is well-typed under the same rules and, with a signature, unifies with it, binder by binder and mode by mode; each the zonked type and the type of every literal in occurrence order, or the message of the first error, with the oracle's numbering of metavariables and its capture-avoiding renames, and with what the oracle's lookup answers for a constant given as a table of rows and the answer for a constant no row names) is language-1.2 source in the `bootstrap/` project, as two production roots of `LseTypes` over the expressions of `BS-25`: `checkSignature` and `checkValue`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, the last structural over a bound on the checker's nesting the host passes, and each with no part of its certificate beyond 3,145,728 bytes; their rendered `rust-std` packages under the pinned rustc, and on the normative host their lowered programs under the calculus evaluator, agree with the oracle to the byte on hand-built expressions over a hand-built glossary, which reach every error of the checker, opaque and defined constants, partial application, and both renames, on every signature and defined value of the committed lexicon packages checked against its own package as a package loads and against the builtin packages as a closure does, and on seeded expressions; each planted defect in the source is detected by the one vector it names, and the certified packages the production export publishes for the project are the committed ones; building the lookup from a package or a closure is outside the claim. | ## calculus diff --git a/README.md b/README.md index db42958671..fa3ac4cc49 100644 --- a/README.md +++ b/README.md @@ -154,7 +154,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 496 registered conformance IDs have their named tests; `just vv` checks them with the pinned toolchain installed. +All 497 registered conformance IDs have their named tests; `just vv` checks them 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. @@ -197,7 +197,7 @@ Every row is validated by `just vv`; the IDs link the claim to its register row, | The language-1.2 authoring notation: a readable `.lex.tex` text for a semantic module, in bijection with its canonical JSON, whose every position and variant is read from the schema, with readable forms for types and terms and a generic form for every other value, formatted canonically by `fmt` failing closed, the only form in which a language-1.2 source of the product is written (the data form is refused under language 1.2, and `lexlean::sdk` is the explicit conversion), the same for the compiler-tooling modules in the generic notation, with no compiler-semantics ID that depends on which form an embedded module is written in, with no product source owned by a generator but five named records, read and written by programs only through the authoring API (`lexlean::sdk`), and, for a native core module, a readable program over the shared table of kernel terms whose numbering is the order of its text | `AU-01`..`AU-13` | `build` | | The runtime of generated Lean as native modules: the portable, collection, model, and reasoning runtimes of language 1.2 are LexLean source that every generated module imports, migrated losslessly and audited against their exact axioms, the released runtime text of languages 1.0 and 1.1 is frozen data equal to their released output, and no other Lean runtime text remains in the compiler but the decoder of core module 2 | `RT-01`..`RT-04` | `build` | | The standard library: language-1.2 source beside the compiler whose members are certified production roots, each validated against its oracle by a seeded differential of its evaluator and rustc realizations; bytes and text over `Bytes`, an output buffer of a given capacity written in order; decimal and hexadecimal; the byte order and stable sorts over it; canonical JSON reading and writing; maps and sets over byte keys; SHA-256 against FIPS 180-4, the NIST CAVP vectors, and the `sha2` crate; and the `std-models` and `std-reasoning` source packages, generic over types, whose laws are kernel-checked theorems and whose application is the `helpdesk` example | `SL-01`, `SL-02`, `SL-03`, `SL-04`, `SL-05`, `SL-09`, `SL-10`, `SL-11`, `SL-13`, `SL-14` | `build` | -| The bootstrap project: the compiler's own components as language-1.2 source compiled by LexLean's production pipeline to certified Rust packages, the exact axiom-output parser and its closed Lean-name check, the primitive scanner of §12.2, the preservation lexer and token audit, the target calculus's checker, the Rust crate validator, the Rust printer, the runtime syntax validator, the failure typing and the types of the Rust lowering, its last-use moves, its literal values and its terms, the package renderer, the GNAF request validation, the LaTeX of a tooling module, the structural grammar, the lexer of the authoring notation, its layout algebra, its generic notation, the notation of a tooling module, the reader and printer of LexLean Semantic Expressions, the reader of LexLean Rendering Expressions, where the module of a source is, the canonical diagnostic and its terminal output, the canonical text of the lock file and of the project manifest, the hash recipes of content identity, and the build manifest, each held to the Rust it replaces (the stage-0 oracle) by the committed vectors and a seeded differential of its evaluator and rustc realizations, with planted defects detected and the production export committed | `BS-01`..`BS-34` | `build` | +| The bootstrap project: the compiler's own components as language-1.2 source compiled by LexLean's production pipeline to certified Rust packages, the exact axiom-output parser and its closed Lean-name check, the primitive scanner of §12.2, the preservation lexer and token audit, the target calculus's checker, the Rust crate validator, the Rust printer, the runtime syntax validator, the failure typing and the types of the Rust lowering, its last-use moves, its literal values and its terms, the package renderer, the GNAF request validation, the LaTeX of a tooling module, the structural grammar, the lexer of the authoring notation, its layout algebra, its generic notation, the notation of a tooling module, the reader and printer of LexLean Semantic Expressions, the reader of LexLean Rendering Expressions, where the module of a source is, the canonical diagnostic and its terminal output, the canonical text of the lock file and of the project manifest, the hash recipes of content identity, and the build manifest, the conservative checker of semantic expressions, each held to the Rust it replaces (the stage-0 oracle) by the committed vectors and a seeded differential of its evaluator and rustc realizations, with planted defects detected and the production export committed | `BS-01`..`BS-52` | `build` | | Source packages: a language-1.2 project imports the modules of a locked, content-addressed package of its workspace: `[[source_package]]` and `lexlean-workspace.toml`, the pins in `lexlean.lock` recomputed by every command, imports closed at link, no re-export of a package's production roots, and the package's modules replayed by the importer's `verify` with their origin in the records | `PK-01`..`PK-05` | `build` | | The literal `nat-add-zero` example, the Lean-verified feature examples, and the complete negative fixture suite | `EX-01`..`EX-09` | `build` | | Language-1.2 text components: tokenizers and their round-trip laws, with the laws carried as one native core module the generated modules import rather than authored Lean text | `LM-01`, `LM-02`, `LM-03`, `LM-13` | `build` | diff --git a/SPEC.md b/SPEC.md index 7c7fedf619..a6a0b9fd1a 100644 --- a/SPEC.md +++ b/SPEC.md @@ -9116,6 +9116,23 @@ module with a state machine over the tokens, reading each member value with the span runs from the first token of the element to the end of its last. Neither of the last two admits `recursion`. The decoder of a tooling module and the checker stay in the host. +The conservative checker of semantic expressions (§13.7, §14.4) is the third component of the +lexicon closure as source (`BS-52`, issue 269), the module `LseTypes` over the expressions of +`BS-25`, with two production roots for `rust-std`: + +| Root | Takes | Returns | Oracle | +| --- | --- | --- | --- | +| `LexLeanBoot.LseTypes.checkSignature` | a signature, the lookup's rows and fallback, and a bound on nesting | its zonked type and the type of each literal, or the first error | `lse::check_signature` | +| `LexLeanBoot.LseTypes.checkValue` | a defined value, its signature if any, the lookup's rows and fallback, and a bound on nesting | its zonked type and the type of each literal, or the first error | `lse::check_value` | + +The checker threads the oracle's state, the solved metavariables, their counter, the literal +metavariables, and the counter of capture-avoiding renames, through inference, unification, and +substitution, each structural recursion over a chain of the bound the host passes; a root whose +bound runs out says so, and the case passes a bound no committed or seeded expression reaches. A +row of the table names a constant, whether it has a signature, the signature, and whether it is +a defined value; the fallback is `Missing` for a closure and `Opaque` for the check of a package +as it loads. Building the lookup from a package or a closure stays in the host. + ## 18. Lean backend ### 18.1 Output contract @@ -12152,6 +12169,7 @@ Every row below is normative, has honesty level `build`, and MUST be copied byte | `BS-32` | `bootstrap` | The labeled hash recipes of content identity of SPEC.md §21.1 to §21.5, §11.5, §19.8, and §22.9 (`artifact::content_id`: the frame of a label and a payload, each preceded by its big-endian length in four and eight bytes, under a domain and a NUL; `tree_digest` over path and content rows; `source_id` over the project manifest, the lock, and the sources; `semantic_id_for` over the compiler-semantics digest, the language, the pinned Lean toolchain, the linked IR, and the lexicon closure; `build_id`; `attestation_id`; and `pdf_recipe_id`) is language-1.2 source in the `bootstrap/` project, as six production roots of `ContentId` over the standard library's SHA-256: `treeDigest`, `sourceId`, `semanticId`, `buildId`, `attestationId`, and `pdfRecipeId`, each eligible for `rust-std` with exactly its declared effects, `allocation` and `overflow`, and each with no part of its certificate beyond 3,145,728 bytes; their rendered `rust-std` packages under the pinned rustc, and on the normative host their lowered programs under the calculus evaluator, agree with the oracle to the byte on hand-built inputs that reach every frame of every recipe, lengths beyond one byte, and messages across blocks, on the trees and source inputs of the committed projects, on the source and semantic IDs of every committed build manifest, whose recorded build ID the oracle reproduces, and on seeded inputs; each planted defect in the source is detected by the one vector it names, and the certified packages the production export publishes for the project are the committed ones; choosing and normalizing the files of a tree or a source ID, and serializing the IR, the closure, and the attestation body, are outside the claim. | §17.23, §21.1, §21.5 | | `BS-33` | `bootstrap` | The canonical text of the project manifest of SPEC.md §10.1 and its digest (`config::ProjectConfig::canonical_toml`: the ten header keys in their fixed order with the pinned Lean toolchain, the lexicon sources sorted by package or the empty array when there is none, the artifact sources and the source packages in their order, the eleven limits in their fixed order, and the PDF provider; and `config_sha256`: the lower-case hex SHA-256 of that text) is language-1.2 source in the `bootstrap/` project, as two production roots of `ProjectText` over the TOML writers of `BS-31`: `canonicalProject` and `configSha256`, each eligible for `rust-std` with exactly its declared effects, `allocation` and `overflow`, and each with no part of its certificate beyond 3,145,728 bytes; their rendered `rust-std` packages under the pinned rustc, and on the normative host their lowered programs under the calculus evaluator, agree with the oracle to the byte on hand-built configurations, which reach every kind of lexicon source, the empty source list, equal packages, every optional table, escapes, and the largest limits, on every committed `lexlean.toml` that parses, and on seeded configurations; each planted defect in the source is detected by the one vector it names, and the certified packages the production export publishes for the project are the committed ones; reading and validating a manifest is outside the claim. | §10.1, §17.23 | | `BS-34` | `bootstrap` | The build manifest of SPEC.md §21.6 in the canonical JSON of §21.7 (`artifact::manifest::BuildManifest::to_json` written as a file: the specification, the compiler version and semantics ID, the language, the project, the source, semantic, and build IDs, the pinned Lean toolchain, the selection and the module rows in their order, a module's origin only when it has one, and the input and output rows sorted by kind, path, length, and digest, each digest in lower-case hex) is language-1.2 source in the `bootstrap/` project, as the production root `buildManifest` of `ManifestText` over the standard library's `Json` writer, eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, and with no part of its certificate beyond 3,145,728 bytes; its rendered `rust-std` package under the pinned rustc, and on the normative host its lowered program under the calculus evaluator, agree with the oracle to the byte on hand-built manifests, which reach every key of the row order, lengths across a byte, a module's origin, and escapes, on every committed build manifest, each of which is the oracle's text of its own fields, and on seeded manifests; each planted defect in the source is detected by the one vector it names, and the certified packages the production export publishes for the project are the committed ones; gathering the rows of a build and hashing its files are outside the claim. | §17.23, §21.6, §21.7 | +| `BS-52` | `bootstrap` | The conservative checker of LexLean Semantic Expressions of SPEC.md §13.6 to §13.8 and §14.4 (`lexicon::lse::check_signature`: a signature is a type, its applications respect the explicit arities of the signatures they reach, and its natural literals take the types an explicit binder or a solved metavariable gives them; and `check_value`: a defined value is well-typed under the same rules and, with a signature, unifies with it, binder by binder and mode by mode; each the zonked type and the type of every literal in occurrence order, or the message of the first error, with the oracle's numbering of metavariables and its capture-avoiding renames, and with what the oracle's lookup answers for a constant given as a table of rows and the answer for a constant no row names) is language-1.2 source in the `bootstrap/` project, as two production roots of `LseTypes` over the expressions of `BS-25`: `checkSignature` and `checkValue`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, the last structural over a bound on the checker's nesting the host passes, and each with no part of its certificate beyond 3,145,728 bytes; their rendered `rust-std` packages under the pinned rustc, and on the normative host their lowered programs under the calculus evaluator, agree with the oracle to the byte on hand-built expressions over a hand-built glossary, which reach every error of the checker, opaque and defined constants, partial application, and both renames, on every signature and defined value of the committed lexicon packages checked against its own package as a package loads and against the builtin packages as a closure does, and on seeded expressions; each planted defect in the source is detected by the one vector it names, and the certified packages the production export publishes for the project are the committed ones; building the lookup from a package or a closure is outside the claim. | §17.23, §13.6, §13.7, §13.8, §14.4 | | `PK-01` | `packages` | A language-1.2 project that declares a source package by its identifier, version, kind `path`, and the member directory a `lexlean-workspace.toml` lists locks it by the tree digest of its manifest and source files, its manifest digest, its version, its module prefix, and its own packages, and the configuration and the lock read back to the bytes written; a project without a source package keeps its bytes, and a package of another kind, a source package in another language, a changed byte or version (LLC0105), an undeclared package (LLC0106), a module prefix claimed twice (LLC0107), a project or a package outside the workspace (LLC0108), a path that escapes it, and a symlink (LLS8001) are each refused with the named code. | §10.5, §11.1, §25.1 | | `PK-02` | `packages` | A module of a project imports only modules of its own project and modules of the packages the project declares, and a module of a package only modules of itself and of the packages its own manifest declares: a package module is a module of the importing project under its own module prefix, a module of a package the project does not declare fails with LLR3009, and an import cycle through a package fails with LLR3003. | §10.5, §15.1 | | `PK-03` | `packages` | An importing project builds and verifies under the pinned Lean offline, and its package modules are generated, elaborated, replayed with `leanchecker`, and audited for their exact axioms under the package's module prefix with the package as their origin in the records; the source ID of the project changes with the pins, and no file of the package is written by `fmt`, `build`, or `verify`. | §10.5, §21.3, §22 | @@ -12168,7 +12186,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:** 496. +**Total required capability IDs:** 497. 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 685148b770..56461064df 100644 --- a/VERIFICATION.md +++ b/VERIFICATION.md @@ -11552,6 +11552,56 @@ axioms the module now declares. The case then stops at the stack-bound step, bootstrap member did before its regeneration, which produces the package and measures the root's stack bound. +### the checker of semantic expressions is source (BS-52) + +`BS-52` holds the two production roots of `LexLeanBoot.LseTypes` +(`bootstrap/src/LseTypes.lex.tex`) to `lexicon::lse::check_signature` and +`check_value`: the zonked type and the type of every natural literal in +occurrence order, or the message of the first error. The module threads the +oracle's state through inference, unification, zonking, and capture-avoiding +substitution: solved metavariables are named `?n` in the order the oracle +makes them, a renamed binder is `x#n` with the oracle's counter, and every +message prints its expressions with `printLse` of `BS-25`. Each recursive +definition is structural over a chain the host's bound on nesting builds, so +the roots admit `recursion`; a root whose bound runs out returns an error of +its own, which no run of the case reaches. What the oracle's lookup closure +answers is handed over as rows and a fallback. The oracle is the existing +public code of `lse.rs`; the case reads the encoder and the seeded +expressions of `BS-25`'s case, and no other Rust changes. + +The case holds the roots to the oracle on hand-built expressions over a +hand-built glossary that reach every error of the checker, an opaque head, a +defined result type, partial application, literals of an inferred and an +undetermined type, a binder and a `let` renamed to avoid capture, and binders +named apart; on every signature and defined value of the committed lexicon +packages, checked against its own package with every other constant opaque +as a package loads and against the builtin packages with every other +constant missing as a closure does; and on seeded expressions. Each of the 20 +planted defects names the one vector that catches it, and the case asserts +that the site of each plant occurs once. The row's statement carries no +counts. + +The case passes a bound of 192: the evaluator copies the chain the bound +builds at each call, so a bound of 4,096 made each run cost seconds, and no +committed or seeded expression nests near 192. Two rules of the Rust rendering +shaped the source. A call that can overflow is bound with `let` or is the +value the function returns, never an operand, so each message prints its +expressions into names first. And a definition that calls itself as its last +step is rendered as a loop whose exit does not propagate the overflow of a +call to another definition, so `inferBinding`, `inferRest`, and `unifyPiLoop` +bind their own recursive call and rebuild its record. + +The case was first compiled and run on the normative host over `release/v0.4` +at `26e5e0ace` (`cargo test -p repo-conformance --test conformance -- +conformance_bs_52`). Its corpus is 39 named vectors, the signatures and +defined values of the 43 committed packages that load (224 signature and 236 +value runs), and 200 and 1,500 seeded expressions. The evaluator agrees with +the oracle on 899 runs, the rendered packages under the pinned rustc on 3,499 +runs, and each of the 20 planted defects is detected by its own vector. The +case then stops at the stack-bound step, `bootstrap/check_signature is no +package of the committed exports`, as each bootstrap member does before its +regeneration, which produces the two packages and measures their stack bounds. + ### the lexer of the authoring notation is source (BS-22) `BS-22` holds the two production roots of `LexLeanBoot.SurfaceLex` diff --git a/bootstrap/README.md b/bootstrap/README.md index c99de3f256..34d7c8a004 100644 --- a/bootstrap/README.md +++ b/bootstrap/README.md @@ -55,3 +55,6 @@ writers it uses, and the standard library's `Numeral`, `Order`, and `Sha256`, an canonical project manifest of SPEC.md §10.1 and its digest. `src/ManifestText.lex.tex` (SPEC.md §17.23, `BS-34`) imports the standard library's `Json`, `Numeral`, `Order`, and `Text`, and writes the build manifest of SPEC.md §21.6. +`src/LseTypes.lex.tex` (SPEC.md §17.23, `BS-52`) imports `LexiconLse` (`BS-25`), whose +expressions it checks and prints, and the standard library's `Numeral` and `Order`: the +conservative checker of semantic expressions of SPEC.md §13.7. diff --git a/bootstrap/lexlean.lock b/bootstrap/lexlean.lock deleted file mode 100644 index 9a9a6f8899..0000000000 --- a/bootstrap/lexlean.lock +++ /dev/null @@ -1,57 +0,0 @@ -spec = "lexlean/lock/2" -language = "1.2" -compiler_semantics = "a82990b56aa082f57e3bf3e2d6494d75a8170f26e269448de19110f5459655ad" -project_config_sha256 = "cb6f1d4f33f8c6a8b7e593d6c53094526bd477d565600f42c530fc1e4adaa5d4" -lean_toolchain = "leanprover/lean4:v4.32.1" - -[[workspace_file]] -path = "lake-manifest.json" -sha256 = "736b7fb038a81af813e3e29b7c1ea7575ceefb442c8fb2e4df805d4bd87010b6" - -[[workspace_file]] -path = "lakefile.toml" -sha256 = "4faafd773dfa00e427b139318e863070b818aa6e229ce493b24ec906b60fa5df" - -[[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"] - -[[source_package]] -id = "std" -version = "1.0.0" -kind = "path" -source = "stdlib" -module_prefix = "LexLeanStd" -tree_sha256 = "316ac824e54957e79f4bdd5ed1f3228b7c5a817333134bddff9552d254434a08" -manifest_sha256 = "1dbb7c6933b8f2a4a9d4af7746eefcb077ea1b339d9b88a63eadf64b6a348f4f" -imports = [] diff --git a/bootstrap/lexlean.toml b/bootstrap/lexlean.toml index 680666515e..ed5bcf1178 100644 --- a/bootstrap/lexlean.toml +++ b/bootstrap/lexlean.toml @@ -3,7 +3,7 @@ name = "bootstrap" language = "1.2" module_prefix = "LexLeanBoot" source_roots = ["src"] -entrypoints = ["src/AxiomOutput.lex.tex", "src/CalculusCheck.lex.tex", "src/CliOutput.lex.tex", "src/ContentId.lex.tex", "src/Diagnostics.lex.tex", "src/GnafValidate.lex.tex", "src/LexiconLre.lex.tex", "src/LexiconLse.lex.tex", "src/LockText.lex.tex", "src/ManifestText.lex.tex", "src/PrimitiveScan.lex.tex", "src/ProjectText.lex.tex", "src/RuntimeValidate.lex.tex", "src/RustAssemble.lex.tex", "src/RustAst.lex.tex", "src/RustBodies.lex.tex", "src/RustLoops.lex.tex", "src/RustLower.lex.tex", "src/RustMoves.lex.tex", "src/RustPackage.lex.tex", "src/RustPrint.lex.tex", "src/RustSucceed.lex.tex", "src/RustTerms.lex.tex", "src/RustValidate.lex.tex", "src/StructuralGrammar.lex.tex", "src/SurfaceDoc.lex.tex", "src/SurfaceGeneric.lex.tex", "src/SurfaceLex.lex.tex", "src/SurfaceModule.lex.tex", "src/SurfaceTooling.lex.tex", "src/TokenAudit.lex.tex", "src/ToolingAst.lex.tex", "src/ToolingLatex.lex.tex"] +entrypoints = ["src/AxiomOutput.lex.tex", "src/CalculusCheck.lex.tex", "src/CliOutput.lex.tex", "src/ContentId.lex.tex", "src/Diagnostics.lex.tex", "src/GnafValidate.lex.tex", "src/LexiconLre.lex.tex", "src/LexiconLse.lex.tex", "src/LockText.lex.tex", "src/LseTypes.lex.tex", "src/ManifestText.lex.tex", "src/PrimitiveScan.lex.tex", "src/ProjectText.lex.tex", "src/RuntimeValidate.lex.tex", "src/RustAssemble.lex.tex", "src/RustAst.lex.tex", "src/RustBodies.lex.tex", "src/RustLoops.lex.tex", "src/RustLower.lex.tex", "src/RustMoves.lex.tex", "src/RustPackage.lex.tex", "src/RustPrint.lex.tex", "src/RustSucceed.lex.tex", "src/RustTerms.lex.tex", "src/RustValidate.lex.tex", "src/StructuralGrammar.lex.tex", "src/SurfaceDoc.lex.tex", "src/SurfaceGeneric.lex.tex", "src/SurfaceLex.lex.tex", "src/SurfaceModule.lex.tex", "src/SurfaceTooling.lex.tex", "src/TokenAudit.lex.tex", "src/ToolingAst.lex.tex", "src/ToolingLatex.lex.tex"] build_root = ".lexlean" lockfile = "lexlean.lock" lean_workspace = "." diff --git a/bootstrap/src/LseTypes.lex.tex b/bootstrap/src/LseTypes.lex.tex new file mode 100755 index 0000000000..7df17c803c --- /dev/null +++ b/bootstrap/src/LseTypes.lex.tex @@ -0,0 +1,1217 @@ +\begin{lexlean}{LseTypes} +\useglossary{lexlean.std.nat@1.2.0} +\importmodule{LexLeanStd.Numeral} +\importmodule{LexLeanStd.Order} +\importmodule{LexiconLse} +\title{Natural number addition} +\begin{semanticmodule} +inductive{ + name: Fuel, + type_parameters: [], + parameters: [], + constructors: [{name: FZ, fields: []}, {name: FS, fields: [Fuel]}] +} + +structure{ + name: CRow, + type_parameters: [], + parameters: [], + fields: [ + {name: package, type: Bytes}, + {name: entry, type: Bytes}, + {name: kind, type: Nat}, + {name: signature, type: LexiconLse::Lse}, + {name: defined, type: Bool} + ] +} + +structure{ + name: Ck, + type_parameters: [], + parameters: [], + fields: [ + {name: metas, type: (List (Bytes * LexiconLse::Lse))}, + {name: nextMeta, type: Nat}, + {name: literals, type: (List Bytes)}, + {name: freshCounter, type: Nat}, + {name: exhausted, type: Bool}, + {name: rows, type: (List CRow)}, + {name: fallback, type: Nat} + ] +} + +structure{ + name: Bd, + type_parameters: [], + parameters: [], + fields: [{name: mode, type: Nat}, {name: name, type: Bytes}, {name: ty, type: LexiconLse::Lse}] +} + +structure{ + name: Sr, + type_parameters: [], + parameters: [], + fields: [{name: out, type: LexiconLse::Lse}, {name: ck, type: Ck}] +} + +structure{ + name: Sb, + type_parameters: [], + parameters: [], + fields: [ + {name: chain, type: LexiconLse::Lse}, + {name: renames, type: (List (Bytes * Bytes))}, + {name: shadowed, type: Bool}, + {name: ck, type: Ck} + ] +} + +structure{ + name: Ls, + type_parameters: [], + parameters: [], + fields: [{name: items, type: (List Bd)}, {name: ck, type: Ck}] +} + +structure{ + name: Ur, + type_parameters: [], + parameters: [], + fields: [{name: error, type: (Option Bytes)}, {name: ck, type: Ck}] +} + +structure{ + name: Ir, + type_parameters: [], + parameters: [], + fields: [ + {name: ty, type: LexiconLse::Lse}, + {name: error, type: (Option Bytes)}, + {name: ck, type: Ck} + ] +} + +structure{ + name: Checked, + type_parameters: [], + parameters: [], + fields: [ + {name: ty, type: LexiconLse::Lse}, + {name: literals, type: (List (Option LexiconLse::Lse))} + ] +} + +structure{ + name: Fl, + type_parameters: [], + parameters: [], + fields: [{name: binders, type: (List Bd)}, {name: body, type: LexiconLse::Lse}] +} + +definition{ + name: lit, + parameters: [literal : String], + result: Bytes, + body: (prim utf8_encode literal : Bytes), + executable: true +} + +definition{ + name: cat, + parameters: [left : Bytes, right : Bytes], + result: Bytes, + body: (prim append left right : Bytes), + executable: true +} + +definition{ + name: shown, + parameters: [expr : LexiconLse::Lse], + result: Bytes, + body: (cat (cat (lit "`") (LexiconLse::printLse expr false)) (lit "`")), + executable: true +} + +definition{ + name: mkFuel, + parameters: [depth : Nat], + result: Fuel, + body: (first (prim iterate_until (fun (rest : Fuel) => (ctor Option.some [Fuel] (ctor Fuel.FS rest))) depth (ctor Fuel.FZ) : (Fuel * Bool))), + executable: true +} + +definition{ + name: isMeta, + parameters: [name : Bytes], + result: Bool, + body: ((LexiconLse::byteNum name 0) =? 63), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: metaOf, + parameters: [expr : LexiconLse::Lse], + result: (Option Bytes), + body: (match expr + | LexiconLse::Lse.LLocal name => (if (isMeta name) then (ctor Option.some [Bytes] name) else (ctor Option.none [Bytes])) + | LexiconLse::Lse.LSortProp => (ctor Option.none [Bytes]) + | LexiconLse::Lse.LSortType u => (ctor Option.none [Bytes]) + | LexiconLse::Lse.LConst p e us => (ctor Option.none [Bytes]) + | LexiconLse::Lse.LApp g args => (ctor Option.none [Bytes]) + | LexiconLse::Lse.LPi bs body => (ctor Option.none [Bytes]) + | LexiconLse::Lse.LLam bs body => (ctor Option.none [Bytes]) + | LexiconLse::Lse.LLet n t v body => (ctor Option.none [Bytes]) + | LexiconLse::Lse.LNat digits => (ctor Option.none [Bytes]) + | LexiconLse::Lse.LNil => (ctor Option.none [Bytes]) + | LexiconLse::Lse.LArg h t => (ctor Option.none [Bytes]) + | LexiconLse::Lse.LBinder m n t r => (ctor Option.none [Bytes])), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: kindOf, + parameters: [expr : LexiconLse::Lse], + result: Nat, + body: (match expr + | LexiconLse::Lse.LSortProp => 0 + | LexiconLse::Lse.LSortType u => 1 + | LexiconLse::Lse.LConst p e us => 2 + | LexiconLse::Lse.LLocal name => 3 + | LexiconLse::Lse.LApp g args => 4 + | LexiconLse::Lse.LPi bs body => 5 + | LexiconLse::Lse.LLam bs body => 6 + | LexiconLse::Lse.LLet n t v body => 7 + | LexiconLse::Lse.LNat digits => 8 + | LexiconLse::Lse.LNil => 9 + | LexiconLse::Lse.LArg h t => 9 + | LexiconLse::Lse.LBinder m n t r => 9), + executable: true +} + +definition{ + name: atomKey, + parameters: [expr : LexiconLse::Lse], + result: Bytes, + body: (match expr + | LexiconLse::Lse.LConst p e us => (cat (cat p (lit "::")) e) + | LexiconLse::Lse.LLocal name => name + | LexiconLse::Lse.LNat digits => digits + | LexiconLse::Lse.LSortProp => 0x + | LexiconLse::Lse.LSortType u => 0x + | LexiconLse::Lse.LApp g args => 0x + | LexiconLse::Lse.LPi bs body => 0x + | LexiconLse::Lse.LLam bs body => 0x + | LexiconLse::Lse.LLet n t v body => 0x + | LexiconLse::Lse.LNil => 0x + | LexiconLse::Lse.LArg h t => 0x + | LexiconLse::Lse.LBinder m n t r => 0x), + executable: true +} + +definition{ + name: ckWith, + parameters: [ + st : Ck, + metas : (List (Bytes * LexiconLse::Lse)), + nextMeta : Nat, + literals : (List Bytes), + freshCounter : Nat, + exhausted : Bool + ], + result: Ck, + body: (record Ck {metas := metas, nextMeta := nextMeta, literals := literals, freshCounter := freshCounter, exhausted := exhausted, rows := (project st rows), fallback := (project st fallback)}), + executable: true +} + +definition{ + name: ckExhaust, + parameters: [st : Ck], + result: Ck, + body: (ckWith st (project st metas) (project st nextMeta) (project st literals) (project st freshCounter) true), + executable: true +} + +definition{ + name: ckSolve, + parameters: [st : Ck, name : Bytes, solution : LexiconLse::Lse], + result: Ck, + body: (ckWith st (cons (name, solution) (project st metas)) (project st nextMeta) (project st literals) (project st freshCounter) (project st exhausted)), + executable: true +} + +definition{ + name: freshMeta, + parameters: [st : Ck], + result: Sr, + body: (record Sr {out := (ctor LexiconLse::Lse.LLocal (cat (lit "?") (LexLeanStd.Numeral::decimal (project st nextMeta)))), ck := (ckWith st (project st metas) ((project st nextMeta) + 1) (project st literals) (project st freshCounter) (project st exhausted))}), + executable: true +} + +definition{ + name: literalMeta, + parameters: [st : Ck], + result: Sr, + body: (let made : Sr := (freshMeta st); (let after : Ck := (project made ck); (record Sr {out := (project made out), ck := (ckWith after (project after metas) (project after nextMeta) (cons (cat (lit "?") (LexLeanStd.Numeral::decimal (project st nextMeta))) (project after literals)) (project after freshCounter) (project after exhausted))}))), + executable: true +} + +definition{ + name: freshLocal, + parameters: [st : Ck, base : Bytes], + result: (Bytes * Ck), + body: (let counter : Nat := ((project st freshCounter) + 1); ((cat (cat base 0x23) (LexLeanStd.Numeral::decimal counter)), (ckWith st (project st metas) (project st nextMeta) (project st literals) counter (project st exhausted)))), + executable: true +} + +definition{ + name: solvedOf, + parameters: [metas : (List (Bytes * LexiconLse::Lse)), name : Bytes], + result: (Option LexiconLse::Lse), + body: (prim list_fold (fun [name] (found : (Option LexiconLse::Lse)) (row : (Bytes * LexiconLse::Lse)) => (match found + | Option.some kept => found + | Option.none => (if (prim equal (first row) name : Bool) then (ctor Option.some [LexiconLse::Lse] (second row)) else (ctor Option.none [LexiconLse::Lse])))) (ctor Option.none [LexiconLse::Lse]) metas : (Option LexiconLse::Lse)), + executable: true +} + +definition{ + name: lookupRow, + parameters: [st : Ck, package : Bytes, entry : Bytes], + result: (Nat * Bool), + body: (match (prim list_fold (fun [entry, package] (found : (Option CRow)) (row : CRow) => (match found + | Option.some kept => found + | Option.none => (if (if (prim equal (project row package) package : Bool) then (prim equal (project row entry) entry : Bool) else false) then (ctor Option.some [CRow] row) else (ctor Option.none [CRow])))) (ctor Option.none [CRow]) (project st rows) : (Option CRow)) + | Option.some row => ((project row kind), (project row defined)) + | Option.none => ((project st fallback), false)), + executable: true +} + +definition{ + name: signatureOf, + parameters: [st : Ck, package : Bytes, entry : Bytes], + result: LexiconLse::Lse, + body: (match (prim list_fold (fun [entry, package] (found : (Option CRow)) (row : CRow) => (match found + | Option.some kept => found + | Option.none => (if (if (prim equal (project row package) package : Bool) then (prim equal (project row entry) entry : Bool) else false) then (ctor Option.some [CRow] row) else (ctor Option.none [CRow])))) (ctor Option.none [CRow]) (project st rows) : (Option CRow)) + | Option.some row => (project row signature) + | Option.none => (ctor LexiconLse::Lse.LSortProp)), + executable: true +} + +definition{ + name: unfoldsConst, + parameters: [st : Ck, package : Bytes, entry : Bytes], + result: Bool, + body: (let found : (Nat * Bool) := (lookupRow st package entry); (if ((first found) =? 1) then true else (if ((first found) =? 3) then (second found) else false))), + executable: true +} + +definition{ + name: mayUnfold, + parameters: [fuel : Fuel, st : Ck, expr : LexiconLse::Lse], + result: Bool, + body: (match fuel + | Fuel.FZ => false + | Fuel.FS f => (match expr + | LexiconLse::Lse.LConst p e us => (unfoldsConst st p e) + | LexiconLse::Lse.LApp g args => (mayUnfold f st g) + | LexiconLse::Lse.LSortProp => false + | LexiconLse::Lse.LSortType u => false + | LexiconLse::Lse.LLocal name => false + | LexiconLse::Lse.LPi bs body => false + | LexiconLse::Lse.LLam bs body => false + | LexiconLse::Lse.LLet n t v body => false + | LexiconLse::Lse.LNat digits => false + | LexiconLse::Lse.LNil => false + | LexiconLse::Lse.LArg h t => false + | LexiconLse::Lse.LBinder m n t r => false)), + executable: true, + recursive_argument: fuel +} + +definition{ + name: both, + parameters: [left : (Option LexiconLse::Lse), right : (Option LexiconLse::Lse)], + result: (Option (LexiconLse::Lse * LexiconLse::Lse)), + body: (match left + | Option.none => (ctor Option.none [(LexiconLse::Lse * LexiconLse::Lse)]) + | Option.some l => (match right + | Option.none => (ctor Option.none [(LexiconLse::Lse * LexiconLse::Lse)]) + | Option.some r => (ctor Option.some [(LexiconLse::Lse * LexiconLse::Lse)] (l, r)))), + executable: true +} + +definition{ + name: zonk, + parameters: [fuel : Fuel, metas : (List (Bytes * LexiconLse::Lse)), expr : LexiconLse::Lse], + result: (Option LexiconLse::Lse), + body: (match fuel + | Fuel.FZ => (ctor Option.none [LexiconLse::Lse]) + | Fuel.FS f => (match expr + | LexiconLse::Lse.LLocal name => (if (isMeta name) then (match (solvedOf metas name) + | Option.some solution => (zonk f metas solution) + | Option.none => (ctor Option.some [LexiconLse::Lse] (ctor LexiconLse::Lse.LLocal name))) else (ctor Option.some [LexiconLse::Lse] (ctor LexiconLse::Lse.LLocal name))) + | LexiconLse::Lse.LApp g args => (match (both (zonk f metas g) (zonk f metas args)) + | Option.some zs => (ctor Option.some [LexiconLse::Lse] (ctor LexiconLse::Lse.LApp (first zs) (second zs))) + | Option.none => (ctor Option.none [LexiconLse::Lse])) + | LexiconLse::Lse.LArg h t => (match (both (zonk f metas h) (zonk f metas t)) + | Option.some zs => (ctor Option.some [LexiconLse::Lse] (ctor LexiconLse::Lse.LArg (first zs) (second zs))) + | Option.none => (ctor Option.none [LexiconLse::Lse])) + | LexiconLse::Lse.LPi bs body => (match (both (zonk f metas bs) (zonk f metas body)) + | Option.some zs => (ctor Option.some [LexiconLse::Lse] (ctor LexiconLse::Lse.LPi (first zs) (second zs))) + | Option.none => (ctor Option.none [LexiconLse::Lse])) + | LexiconLse::Lse.LLam bs body => (match (both (zonk f metas bs) (zonk f metas body)) + | Option.some zs => (ctor Option.some [LexiconLse::Lse] (ctor LexiconLse::Lse.LLam (first zs) (second zs))) + | Option.none => (ctor Option.none [LexiconLse::Lse])) + | LexiconLse::Lse.LBinder m n t r => (match (both (zonk f metas t) (zonk f metas r)) + | Option.some zs => (ctor Option.some [LexiconLse::Lse] (ctor LexiconLse::Lse.LBinder m n (first zs) (second zs))) + | Option.none => (ctor Option.none [LexiconLse::Lse])) + | LexiconLse::Lse.LLet n t v body => (match (both (zonk f metas t) (zonk f metas v)) + | Option.some zs => (match (zonk f metas body) + | Option.some zbody => (ctor Option.some [LexiconLse::Lse] (ctor LexiconLse::Lse.LLet n (first zs) (second zs) zbody)) + | Option.none => (ctor Option.none [LexiconLse::Lse])) + | Option.none => (ctor Option.none [LexiconLse::Lse])) + | LexiconLse::Lse.LSortProp => (ctor Option.some [LexiconLse::Lse] (ctor LexiconLse::Lse.LSortProp)) + | LexiconLse::Lse.LSortType u => (ctor Option.some [LexiconLse::Lse] (ctor LexiconLse::Lse.LSortType u)) + | LexiconLse::Lse.LConst p e us => (ctor Option.some [LexiconLse::Lse] (ctor LexiconLse::Lse.LConst p e us)) + | LexiconLse::Lse.LNat digits => (ctor Option.some [LexiconLse::Lse] (ctor LexiconLse::Lse.LNat digits)) + | LexiconLse::Lse.LNil => (ctor Option.some [LexiconLse::Lse] (ctor LexiconLse::Lse.LNil)))), + executable: true, + recursive_argument: fuel +} + +definition{ + name: zonkSt, + parameters: [fuel : Fuel, st : Ck, expr : LexiconLse::Lse], + result: Sr, + body: (match (zonk fuel (project st metas) expr) + | Option.some done => (record Sr {out := done, ck := st}) + | Option.none => (record Sr {out := expr, ck := (ckExhaust st)})), + executable: true +} + +definition{ + name: unionOf, + parameters: [left : (Option (List Bytes)), right : (Option (List Bytes))], + result: (Option (List Bytes)), + body: (match left + | Option.none => (ctor Option.none [(List Bytes)]) + | Option.some l => (match right + | Option.none => (ctor Option.none [(List Bytes)]) + | Option.some r => (ctor Option.some [(List Bytes)] (prim list_fold (fun (acc : (List Bytes)) (item : Bytes) => (cons item acc)) r l : (List Bytes))))), + executable: true +} + +definition{ + name: boundIn, + parameters: [names : (List Bytes), wanted : Bytes], + result: Bool, + body: (prim list_fold (fun [wanted] (found : Bool) (item : Bytes) => (if found then true else (prim equal item wanted : Bool))) false names : Bool), + executable: true +} + +definition{ + name: freeLocals, + parameters: [fuel : Fuel, expr : LexiconLse::Lse, bound : (List Bytes)], + result: (Option (List Bytes)), + body: (match fuel + | Fuel.FZ => (ctor Option.none [(List Bytes)]) + | Fuel.FS f => (match expr + | LexiconLse::Lse.LLocal name => (if (boundIn bound name) then (ctor Option.some [(List Bytes)] (nil Bytes)) else (ctor Option.some [(List Bytes)] (cons name (nil Bytes)))) + | LexiconLse::Lse.LApp g args => (unionOf (freeLocals f g bound) (freeLocals f args bound)) + | LexiconLse::Lse.LArg h t => (unionOf (freeLocals f h bound) (freeLocals f t bound)) + | LexiconLse::Lse.LPi bs body => (freeBinders f bs bound body) + | LexiconLse::Lse.LLam bs body => (freeBinders f bs bound body) + | LexiconLse::Lse.LLet n t v body => (unionOf (unionOf (freeLocals f t bound) (freeLocals f v bound)) (freeLocals f body (cons n bound))) + | LexiconLse::Lse.LSortProp => (ctor Option.some [(List Bytes)] (nil Bytes)) + | LexiconLse::Lse.LSortType u => (ctor Option.some [(List Bytes)] (nil Bytes)) + | LexiconLse::Lse.LConst p e us => (ctor Option.some [(List Bytes)] (nil Bytes)) + | LexiconLse::Lse.LNat digits => (ctor Option.some [(List Bytes)] (nil Bytes)) + | LexiconLse::Lse.LNil => (ctor Option.some [(List Bytes)] (nil Bytes)) + | LexiconLse::Lse.LBinder m n t r => (ctor Option.some [(List Bytes)] (nil Bytes)))), + executable: true, + mutual: Free, + recursive_argument: fuel +} + +definition{ + name: freeBinders, + parameters: [fuel : Fuel, chain : LexiconLse::Lse, bound : (List Bytes), body : LexiconLse::Lse], + result: (Option (List Bytes)), + body: (match fuel + | Fuel.FZ => (ctor Option.none [(List Bytes)]) + | Fuel.FS f => (match chain + | LexiconLse::Lse.LBinder m n t r => (unionOf (freeLocals f t bound) (freeBinders f r (cons n bound) body)) + | LexiconLse::Lse.LNil => (freeLocals f body bound) + | LexiconLse::Lse.LSortProp => (freeLocals f body bound) + | LexiconLse::Lse.LSortType u => (freeLocals f body bound) + | LexiconLse::Lse.LConst p e us => (freeLocals f body bound) + | LexiconLse::Lse.LLocal name => (freeLocals f body bound) + | LexiconLse::Lse.LApp g args => (freeLocals f body bound) + | LexiconLse::Lse.LPi bs inner => (freeLocals f body bound) + | LexiconLse::Lse.LLam bs inner => (freeLocals f body bound) + | LexiconLse::Lse.LLet n t v inner => (freeLocals f body bound) + | LexiconLse::Lse.LNat digits => (freeLocals f body bound) + | LexiconLse::Lse.LArg h t => (freeLocals f body bound))), + executable: true, + mutual: Free, + recursive_argument: fuel +} + +definition{ + name: hasUnsolved, + parameters: [fuel : Fuel, st : Ck, expr : LexiconLse::Lse], + result: (Bool * Ck), + body: (let zonked : Sr := (zonkSt fuel st expr); (match (freeLocals fuel (project zonked out) (nil Bytes)) + | Option.some names => ((prim list_fold (fun (found : Bool) (name : Bytes) => (if found then true else (isMeta name))) false names : Bool), (project zonked ck)) + | Option.none => (true, (ckExhaust (project zonked ck))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: substIn, + parameters: [ + fuel : Fuel, + st : Ck, + expr : LexiconLse::Lse, + name : Bytes, + replacement : LexiconLse::Lse, + avoid : (List Bytes) + ], + result: Sr, + body: (match fuel + | Fuel.FZ => (record Sr {out := expr, ck := (ckExhaust st)}) + | Fuel.FS f => (match expr + | LexiconLse::Lse.LLocal lname => (if (prim equal lname name : Bool) then (record Sr {out := replacement, ck := st}) else (record Sr {out := (ctor LexiconLse::Lse.LLocal lname), ck := st})) + | LexiconLse::Lse.LApp g args => (let sg : Sr := (substIn f st g name replacement avoid); (let sa : Sr := (substIn f (project sg ck) args name replacement avoid); (record Sr {out := (ctor LexiconLse::Lse.LApp (project sg out) (project sa out)), ck := (project sa ck)}))) + | LexiconLse::Lse.LArg h t => (let sh : Sr := (substIn f st h name replacement avoid); (let stl : Sr := (substIn f (project sh ck) t name replacement avoid); (record Sr {out := (ctor LexiconLse::Lse.LArg (project sh out) (project stl out)), ck := (project stl ck)}))) + | LexiconLse::Lse.LPi bs body => (let sb : Sb := (substChain f st bs name replacement avoid (nil (Bytes * Bytes)) false); (let sbody : Sr := (substBody f sb body name replacement avoid); (record Sr {out := (ctor LexiconLse::Lse.LPi (project sb chain) (project sbody out)), ck := (project sbody ck)}))) + | LexiconLse::Lse.LLam bs body => (let sb : Sb := (substChain f st bs name replacement avoid (nil (Bytes * Bytes)) false); (let sbody : Sr := (substBody f sb body name replacement avoid); (record Sr {out := (ctor LexiconLse::Lse.LLam (project sb chain) (project sbody out)), ck := (project sbody ck)}))) + | LexiconLse::Lse.LLet bound t v body => (let stt : Sr := (substIn f st t name replacement avoid); (let sv : Sr := (substIn f (project stt ck) v name replacement avoid); (if (prim equal bound name : Bool) then (record Sr {out := (ctor LexiconLse::Lse.LLet bound (project stt out) (project sv out) body), ck := (project sv ck)}) else (if (boundIn avoid bound) then (let named : (Bytes * Ck) := (freshLocal (project sv ck) bound); (let renamed : Sr := (substIn f (second named) body bound (ctor LexiconLse::Lse.LLocal (first named)) (nil Bytes)); (let sbody : Sr := (substIn f (project renamed ck) (project renamed out) name replacement avoid); (record Sr {out := (ctor LexiconLse::Lse.LLet (first named) (project stt out) (project sv out) (project sbody out)), ck := (project sbody ck)})))) else (let sbody : Sr := (substIn f (project sv ck) body name replacement avoid); (record Sr {out := (ctor LexiconLse::Lse.LLet bound (project stt out) (project sv out) (project sbody out)), ck := (project sbody ck)})))))) + | LexiconLse::Lse.LBinder m n t r => (record Sr {out := (ctor LexiconLse::Lse.LBinder m n t r), ck := st}) + | LexiconLse::Lse.LSortProp => (record Sr {out := (ctor LexiconLse::Lse.LSortProp), ck := st}) + | LexiconLse::Lse.LSortType u => (record Sr {out := (ctor LexiconLse::Lse.LSortType u), ck := st}) + | LexiconLse::Lse.LConst p e us => (record Sr {out := (ctor LexiconLse::Lse.LConst p e us), ck := st}) + | LexiconLse::Lse.LNat digits => (record Sr {out := (ctor LexiconLse::Lse.LNat digits), ck := st}) + | LexiconLse::Lse.LNil => (record Sr {out := (ctor LexiconLse::Lse.LNil), ck := st}))), + executable: true, + mutual: Subst, + recursive_argument: fuel +} + +definition{ + name: substChain, + parameters: [ + fuel : Fuel, + st : Ck, + chain : LexiconLse::Lse, + name : Bytes, + replacement : LexiconLse::Lse, + avoid : (List Bytes), + renames : (List (Bytes * Bytes)), + shadowed : Bool + ], + result: Sb, + body: (match fuel + | Fuel.FZ => (record Sb {chain := chain, renames := renames, shadowed := shadowed, ck := (ckExhaust st)}) + | Fuel.FS f => (match chain + | LexiconLse::Lse.LBinder m n t r => (let renamedTy : Sr := (substBody f (record Sb {chain := chain, renames := renames, shadowed := shadowed, ck := st}) t name replacement avoid); (let substituted : Sr := renamedTy; (if (prim equal n name : Bool) then (let rest : Sb := (substChain f (project substituted ck) r name replacement avoid renames true); (record Sb {chain := (ctor LexiconLse::Lse.LBinder m n (project substituted out) (project rest chain)), renames := (project rest renames), shadowed := (project rest shadowed), ck := (project rest ck)})) else (if (boundIn avoid n) then (let named : (Bytes * Ck) := (freshLocal (project substituted ck) n); (let rest : Sb := (substChain f (second named) r name replacement avoid (cons (n, (first named)) renames) shadowed); (record Sb {chain := (ctor LexiconLse::Lse.LBinder m (first named) (project substituted out) (project rest chain)), renames := (project rest renames), shadowed := (project rest shadowed), ck := (project rest ck)}))) else (let rest : Sb := (substChain f (project substituted ck) r name replacement avoid renames shadowed); (record Sb {chain := (ctor LexiconLse::Lse.LBinder m n (project substituted out) (project rest chain)), renames := (project rest renames), shadowed := (project rest shadowed), ck := (project rest ck)})))))) + | LexiconLse::Lse.LNil => (record Sb {chain := (ctor LexiconLse::Lse.LNil), renames := renames, shadowed := shadowed, ck := st}) + | LexiconLse::Lse.LSortProp => (record Sb {chain := (ctor LexiconLse::Lse.LNil), renames := renames, shadowed := shadowed, ck := st}) + | LexiconLse::Lse.LSortType u => (record Sb {chain := (ctor LexiconLse::Lse.LNil), renames := renames, shadowed := shadowed, ck := st}) + | LexiconLse::Lse.LConst p e us => (record Sb {chain := (ctor LexiconLse::Lse.LNil), renames := renames, shadowed := shadowed, ck := st}) + | LexiconLse::Lse.LLocal lname => (record Sb {chain := (ctor LexiconLse::Lse.LNil), renames := renames, shadowed := shadowed, ck := st}) + | LexiconLse::Lse.LApp g args => (record Sb {chain := (ctor LexiconLse::Lse.LNil), renames := renames, shadowed := shadowed, ck := st}) + | LexiconLse::Lse.LPi bs body => (record Sb {chain := (ctor LexiconLse::Lse.LNil), renames := renames, shadowed := shadowed, ck := st}) + | LexiconLse::Lse.LLam bs body => (record Sb {chain := (ctor LexiconLse::Lse.LNil), renames := renames, shadowed := shadowed, ck := st}) + | LexiconLse::Lse.LLet bound t v body => (record Sb {chain := (ctor LexiconLse::Lse.LNil), renames := renames, shadowed := shadowed, ck := st}) + | LexiconLse::Lse.LNat digits => (record Sb {chain := (ctor LexiconLse::Lse.LNil), renames := renames, shadowed := shadowed, ck := st}) + | LexiconLse::Lse.LArg h t => (record Sb {chain := (ctor LexiconLse::Lse.LNil), renames := renames, shadowed := shadowed, ck := st}))), + executable: true, + mutual: Subst, + recursive_argument: fuel +} + +definition{ + name: substBody, + parameters: [ + fuel : Fuel, + sb : Sb, + body : LexiconLse::Lse, + name : Bytes, + replacement : LexiconLse::Lse, + avoid : (List Bytes) + ], + result: Sr, + body: (match fuel + | Fuel.FZ => (record Sr {out := body, ck := (ckExhaust (project sb ck))}) + | Fuel.FS f => (let renamedBody : Sr := (applyRenames f (project sb ck) body (LexLeanStd.Order::reverseList [(Bytes * Bytes)] (project sb renames))); (if (project sb shadowed) then renamedBody else (substIn f (project renamedBody ck) (project renamedBody out) name replacement avoid)))), + executable: true, + mutual: Subst, + recursive_argument: fuel +} + +definition{ + name: applyRenames, + parameters: [fuel : Fuel, st : Ck, expr : LexiconLse::Lse, renames : (List (Bytes * Bytes))], + result: Sr, + body: (match fuel + | Fuel.FZ => (record Sr {out := expr, ck := (ckExhaust st)}) + | Fuel.FS f => (match renames + | List.nil => (record Sr {out := expr, ck := st}) + | List.cons rename rest => (let once : Sr := (substIn f st expr (first rename) (ctor LexiconLse::Lse.LLocal (second rename)) (nil Bytes)); (applyRenames f (project once ck) (project once out) rest)))), + executable: true, + mutual: Subst, + recursive_argument: fuel +} + +definition{ + name: subst, + parameters: [ + fuel : Fuel, + st : Ck, + expr : LexiconLse::Lse, + name : Bytes, + replacement : LexiconLse::Lse + ], + result: Sr, + body: (match (freeLocals fuel replacement (nil Bytes)) + | Option.some avoid => (substIn fuel st expr name replacement avoid) + | Option.none => (record Sr {out := expr, ck := (ckExhaust st)})), + executable: true +} + +definition{ + name: substTypes, + parameters: [ + fuel : Fuel, + st : Ck, + items : (List Bd), + name : Bytes, + replacement : LexiconLse::Lse + ], + result: Ls, + body: (match fuel + | Fuel.FZ => (record Ls {items := items, ck := (ckExhaust st)}) + | Fuel.FS f => (match items + | List.nil => (record Ls {items := (nil Bd), ck := st}) + | List.cons item rest => (let changed : Sr := (subst f st (project item ty) name replacement); (let others : Ls := (substTypes f (project changed ck) rest name replacement); (record Ls {items := (cons (record Bd {mode := (project item mode), name := (project item name), ty := (project changed out)}) (project others items)), ck := (project others ck)}))))), + executable: true, + recursive_argument: fuel +} + +definition{ + name: chainItems, + parameters: [fuel : Fuel, chain : LexiconLse::Lse], + result: (List LexiconLse::Lse), + body: (match fuel + | Fuel.FZ => (nil LexiconLse::Lse) + | Fuel.FS f => (match chain + | LexiconLse::Lse.LArg h t => (cons h (chainItems f t)) + | LexiconLse::Lse.LNil => (nil LexiconLse::Lse) + | LexiconLse::Lse.LSortProp => (nil LexiconLse::Lse) + | LexiconLse::Lse.LSortType u => (nil LexiconLse::Lse) + | LexiconLse::Lse.LConst p e us => (nil LexiconLse::Lse) + | LexiconLse::Lse.LLocal name => (nil LexiconLse::Lse) + | LexiconLse::Lse.LApp g args => (nil LexiconLse::Lse) + | LexiconLse::Lse.LPi bs body => (nil LexiconLse::Lse) + | LexiconLse::Lse.LLam bs body => (nil LexiconLse::Lse) + | LexiconLse::Lse.LLet n t v body => (nil LexiconLse::Lse) + | LexiconLse::Lse.LNat digits => (nil LexiconLse::Lse) + | LexiconLse::Lse.LBinder m n t r => (nil LexiconLse::Lse))), + executable: true, + recursive_argument: fuel +} + +definition{ + name: binderItems, + parameters: [fuel : Fuel, chain : LexiconLse::Lse], + result: (List Bd), + body: (match fuel + | Fuel.FZ => (nil Bd) + | Fuel.FS f => (match chain + | LexiconLse::Lse.LBinder m n t r => (cons (record Bd {mode := m, name := n, ty := t}) (binderItems f r)) + | LexiconLse::Lse.LNil => (nil Bd) + | LexiconLse::Lse.LSortProp => (nil Bd) + | LexiconLse::Lse.LSortType u => (nil Bd) + | LexiconLse::Lse.LConst p e us => (nil Bd) + | LexiconLse::Lse.LLocal name => (nil Bd) + | LexiconLse::Lse.LApp g args => (nil Bd) + | LexiconLse::Lse.LPi bs body => (nil Bd) + | LexiconLse::Lse.LLam bs body => (nil Bd) + | LexiconLse::Lse.LLet n t v body => (nil Bd) + | LexiconLse::Lse.LNat digits => (nil Bd) + | LexiconLse::Lse.LArg h t => (nil Bd))), + executable: true, + recursive_argument: fuel +} + +definition{ + name: chainOf, + parameters: [items : (List Bd)], + result: LexiconLse::Lse, + body: (prim list_fold (fun (chain : LexiconLse::Lse) (item : Bd) => (ctor LexiconLse::Lse.LBinder (project item mode) (project item name) (project item ty) chain)) (ctor LexiconLse::Lse.LNil) (LexLeanStd.Order::reverseList [Bd] items) : LexiconLse::Lse), + executable: true +} + +definition{ + name: flattenInto, + parameters: [fuel : Fuel, expr : LexiconLse::Lse, pi : Bool], + result: Fl, + body: (match fuel + | Fuel.FZ => (record Fl {binders := (nil Bd), body := expr}) + | Fuel.FS f => (match expr + | LexiconLse::Lse.LPi bs body => (if pi then (let inner : Fl := (flattenInto f body pi); (record Fl {binders := (prim list_fold (fun (acc : (List Bd)) (item : Bd) => (cons item acc)) (project inner binders) (LexLeanStd.Order::reverseList [Bd] (binderItems f bs)) : (List Bd)), body := (project inner body)})) else (record Fl {binders := (nil Bd), body := (ctor LexiconLse::Lse.LPi bs body)})) + | LexiconLse::Lse.LLam bs body => (if pi then (record Fl {binders := (nil Bd), body := (ctor LexiconLse::Lse.LLam bs body)}) else (let inner : Fl := (flattenInto f body pi); (record Fl {binders := (prim list_fold (fun (acc : (List Bd)) (item : Bd) => (cons item acc)) (project inner binders) (LexLeanStd.Order::reverseList [Bd] (binderItems f bs)) : (List Bd)), body := (project inner body)}))) + | LexiconLse::Lse.LSortProp => (record Fl {binders := (nil Bd), body := (ctor LexiconLse::Lse.LSortProp)}) + | LexiconLse::Lse.LSortType u => (record Fl {binders := (nil Bd), body := (ctor LexiconLse::Lse.LSortType u)}) + | LexiconLse::Lse.LConst p e us => (record Fl {binders := (nil Bd), body := (ctor LexiconLse::Lse.LConst p e us)}) + | LexiconLse::Lse.LLocal name => (record Fl {binders := (nil Bd), body := (ctor LexiconLse::Lse.LLocal name)}) + | LexiconLse::Lse.LApp g args => (record Fl {binders := (nil Bd), body := (ctor LexiconLse::Lse.LApp g args)}) + | LexiconLse::Lse.LLet n t v body => (record Fl {binders := (nil Bd), body := (ctor LexiconLse::Lse.LLet n t v body)}) + | LexiconLse::Lse.LNat digits => (record Fl {binders := (nil Bd), body := (ctor LexiconLse::Lse.LNat digits)}) + | LexiconLse::Lse.LNil => (record Fl {binders := (nil Bd), body := (ctor LexiconLse::Lse.LNil)}) + | LexiconLse::Lse.LArg h t => (record Fl {binders := (nil Bd), body := (ctor LexiconLse::Lse.LArg h t)}) + | LexiconLse::Lse.LBinder m n t r => (record Fl {binders := (nil Bd), body := (ctor LexiconLse::Lse.LBinder m n t r)}))), + executable: true, + recursive_argument: fuel +} + +definition{ + name: modeName, + parameters: [mode : Nat], + result: Bytes, + body: (LexiconLse::modeText mode), + executable: true +} + +definition{ + name: rigidClass, + parameters: [fuel : Fuel, e : LexiconLse::Lse, a : LexiconLse::Lse], + result: Nat, + body: (let ke : Nat := (kindOf e); (if (ke =? (kindOf a)) then (if (ke (match a + | LexiconLse::Lse.LApp h ys => (if ((prim length (chainItems fuel xs) : Nat) =? (prim length (chainItems fuel ys) : Nat)) then 1 else 3) + | LexiconLse::Lse.LSortProp => 3 + | LexiconLse::Lse.LSortType u => 3 + | LexiconLse::Lse.LConst p q us => 3 + | LexiconLse::Lse.LLocal name => 3 + | LexiconLse::Lse.LPi bs body => 3 + | LexiconLse::Lse.LLam bs body => 3 + | LexiconLse::Lse.LLet n t v body => 3 + | LexiconLse::Lse.LNat digits => 3 + | LexiconLse::Lse.LNil => 3 + | LexiconLse::Lse.LArg h t => 3 + | LexiconLse::Lse.LBinder m n t r => 3) + | LexiconLse::Lse.LSortProp => 3 + | LexiconLse::Lse.LSortType u => 3 + | LexiconLse::Lse.LConst p q us => 3 + | LexiconLse::Lse.LLocal name => 3 + | LexiconLse::Lse.LPi bs body => 3 + | LexiconLse::Lse.LLam bs body => 3 + | LexiconLse::Lse.LLet n t v body => 3 + | LexiconLse::Lse.LNat digits => 3 + | LexiconLse::Lse.LNil => 3 + | LexiconLse::Lse.LArg h t => 3 + | LexiconLse::Lse.LBinder m n t r => 3) else (if (if (ke =? 5) then true else (ke =? 6)) then 2 else 3)))) else 3)), + executable: true +} + +definition{ + name: appParts, + parameters: [expr : LexiconLse::Lse], + result: (LexiconLse::Lse * LexiconLse::Lse), + body: (match expr + | LexiconLse::Lse.LApp g args => (g, args) + | LexiconLse::Lse.LSortProp => ((ctor LexiconLse::Lse.LNil), (ctor LexiconLse::Lse.LNil)) + | LexiconLse::Lse.LSortType u => ((ctor LexiconLse::Lse.LNil), (ctor LexiconLse::Lse.LNil)) + | LexiconLse::Lse.LConst p e us => ((ctor LexiconLse::Lse.LNil), (ctor LexiconLse::Lse.LNil)) + | LexiconLse::Lse.LLocal name => ((ctor LexiconLse::Lse.LNil), (ctor LexiconLse::Lse.LNil)) + | LexiconLse::Lse.LPi bs body => ((ctor LexiconLse::Lse.LNil), (ctor LexiconLse::Lse.LNil)) + | LexiconLse::Lse.LLam bs body => ((ctor LexiconLse::Lse.LNil), (ctor LexiconLse::Lse.LNil)) + | LexiconLse::Lse.LLet n t v body => ((ctor LexiconLse::Lse.LNil), (ctor LexiconLse::Lse.LNil)) + | LexiconLse::Lse.LNat digits => ((ctor LexiconLse::Lse.LNil), (ctor LexiconLse::Lse.LNil)) + | LexiconLse::Lse.LNil => ((ctor LexiconLse::Lse.LNil), (ctor LexiconLse::Lse.LNil)) + | LexiconLse::Lse.LArg h t => ((ctor LexiconLse::Lse.LNil), (ctor LexiconLse::Lse.LNil)) + | LexiconLse::Lse.LBinder m n t r => ((ctor LexiconLse::Lse.LNil), (ctor LexiconLse::Lse.LNil))), + executable: true +} + +definition{ + name: urOk, + parameters: [st : Ck], + result: Ur, + body: (record Ur {error := (ctor Option.none [Bytes]), ck := st}), + executable: true +} + +definition{ + name: urFail, + parameters: [st : Ck, message : Bytes], + result: Ur, + body: (record Ur {error := (ctor Option.some [Bytes] message), ck := st}), + executable: true +} + +definition{ + name: unify, + parameters: [fuel : Fuel, st : Ck, expected : LexiconLse::Lse, actual : LexiconLse::Lse], + result: Ur, + body: (match fuel + | Fuel.FZ => (urOk (ckExhaust st)) + | Fuel.FS f => (let ze : Sr := (zonkSt f st expected); (let za : Sr := (zonkSt f (project ze ck) actual); (let e : LexiconLse::Lse := (project ze out); (let a : LexiconLse::Lse := (project za out); (let st2 : Ck := (project za ck); (match (metaOf e) + | Option.some m => (if (prim equal (atomKey a) m : Bool) then (if ((kindOf a) =? 3) then (urOk st2) else (urOk (ckSolve st2 m a))) else (urOk (ckSolve st2 m a))) + | Option.none => (match (metaOf a) + | Option.some m => (urOk (ckSolve st2 m e)) + | Option.none => (unifyRigid f st2 e a))))))))), + executable: true, + mutual: Unify, + recursive_argument: fuel +} + +definition{ + name: unifyRigid, + parameters: [fuel : Fuel, st : Ck, e : LexiconLse::Lse, a : LexiconLse::Lse], + result: Ur, + body: (match fuel + | Fuel.FZ => (urOk (ckExhaust st)) + | Fuel.FS f => (let cls : Nat := (rigidClass f e a); (if (cls =? 0) then (urOk st) else (if (cls =? 1) then (if (if (mayUnfold f st e) then true else (mayUnfold f st a)) then (urOk st) else (let pe : (LexiconLse::Lse * LexiconLse::Lse) := (appParts e); (let pa : (LexiconLse::Lse * LexiconLse::Lse) := (appParts a); (let head : Ur := (unify f st (first pe) (first pa)); (match (project head error) + | Option.some message => head + | Option.none => (unifyArgs f (project head ck) (second pe) (second pa))))))) else (if (cls =? 2) then (unifyBinding f st e a) else (if (if (mayUnfold f st e) then true else (mayUnfold f st a)) then (urOk st) else (let ue : (Bool * Ck) := (hasUnsolved f st e); (if (first ue) then (urOk (second ue)) else (let ua : (Bool * Ck) := (hasUnsolved f (second ue) a); (if (first ua) then (urOk (second ua)) else (let txtE : Bytes := (shown e); (let txtA : Bytes := (shown a); (urFail (second ua) (cat (cat (cat (lit "expected ") txtE) (lit ", found ")) txtA)))))))))))))), + executable: true, + mutual: Unify, + recursive_argument: fuel +} + +definition{ + name: unifyArgs, + parameters: [fuel : Fuel, st : Ck, xs : LexiconLse::Lse, ys : LexiconLse::Lse], + result: Ur, + body: (match fuel + | Fuel.FZ => (urOk (ckExhaust st)) + | Fuel.FS f => (match xs + | LexiconLse::Lse.LArg x xt => (match ys + | LexiconLse::Lse.LArg y yt => (let once : Ur := (unify f st x y); (match (project once error) + | Option.some message => once + | Option.none => (unifyArgs f (project once ck) xt yt))) + | LexiconLse::Lse.LNil => (urOk st) + | LexiconLse::Lse.LSortProp => (urOk st) + | LexiconLse::Lse.LSortType u => (urOk st) + | LexiconLse::Lse.LConst p e us => (urOk st) + | LexiconLse::Lse.LLocal name => (urOk st) + | LexiconLse::Lse.LApp g args => (urOk st) + | LexiconLse::Lse.LPi bs body => (urOk st) + | LexiconLse::Lse.LLam bs body => (urOk st) + | LexiconLse::Lse.LLet n t v body => (urOk st) + | LexiconLse::Lse.LNat digits => (urOk st) + | LexiconLse::Lse.LBinder m n t r => (urOk st)) + | LexiconLse::Lse.LNil => (urOk st) + | LexiconLse::Lse.LSortProp => (urOk st) + | LexiconLse::Lse.LSortType u => (urOk st) + | LexiconLse::Lse.LConst p e us => (urOk st) + | LexiconLse::Lse.LLocal name => (urOk st) + | LexiconLse::Lse.LApp g args => (urOk st) + | LexiconLse::Lse.LPi bs body => (urOk st) + | LexiconLse::Lse.LLam bs body => (urOk st) + | LexiconLse::Lse.LLet n t v body => (urOk st) + | LexiconLse::Lse.LNat digits => (urOk st) + | LexiconLse::Lse.LBinder m n t r => (urOk st))), + executable: true, + mutual: Unify, + recursive_argument: fuel +} + +definition{ + name: unifyBinding, + parameters: [fuel : Fuel, st : Ck, e : LexiconLse::Lse, a : LexiconLse::Lse], + result: Ur, + body: (match fuel + | Fuel.FZ => (urOk (ckExhaust st)) + | Fuel.FS f => (let pi : Bool := ((kindOf e) =? 5); (let fe : Fl := (flattenInto f e pi); (let fa : Fl := (flattenInto f a pi); (let ne : Nat := (prim length (project fe binders) : Nat); (let na : Nat := (prim length (project fa binders) : Nat); (if (ne =? na) then (unifyPiLoop f st (project fe binders) (project fa binders) (project fa body) (project fe body) e a) else (let txtE : Bytes := (shown e); (let txtA : Bytes := (shown a); (urFail st (cat (cat (cat (cat (cat (cat txtE (lit " binds ")) (LexLeanStd.Numeral::decimal ne)) (lit " arguments but ")) txtA) (lit " binds ")) (LexLeanStd.Numeral::decimal na)))))))))))), + executable: true, + mutual: Unify, + recursive_argument: fuel +} + +definition{ + name: unifyPiLoop, + parameters: [ + fuel : Fuel, + st : Ck, + expectedBinders : (List Bd), + laterBinders : (List Bd), + actualBody : LexiconLse::Lse, + expectedBody : LexiconLse::Lse, + e : LexiconLse::Lse, + a : LexiconLse::Lse + ], + result: Ur, + body: (match fuel + | Fuel.FZ => (urOk (ckExhaust st)) + | Fuel.FS f => (match expectedBinders + | List.nil => (unify f st expectedBody actualBody) + | List.cons be restE => (match laterBinders + | List.nil => (unify f st expectedBody actualBody) + | List.cons ba restA => (if ((project be mode) =? (project ba mode)) then (let once : Ur := (unify f st (project be ty) (project ba ty)); (match (project once error) + | Option.some message => once + | Option.none => (let renamed : LexiconLse::Lse := (ctor LexiconLse::Lse.LLocal (project be name)); (let laterTyped : Ls := (substTypes f (project once ck) restA (project ba name) renamed); (let bodyRenamed : Sr := (subst f (project laterTyped ck) actualBody (project ba name) renamed); (let next : Ur := (unifyPiLoop f (project bodyRenamed ck) restE (project laterTyped items) (project bodyRenamed out) expectedBody e a); (record Ur {error := (project next error), ck := (project next ck)}))))))) else (let txtE : Bytes := (shown e); (let txtA : Bytes := (shown a); (urFail st (cat (cat (cat (cat (cat (cat (cat (cat (cat (cat (cat (lit "binder `") (project be name)) (lit "` is ")) (modeName (project be mode))) (lit " in ")) txtE) (lit " but `")) (project ba name)) (lit "` is ")) (modeName (project ba mode))) (lit " in ")) txtA)))))))), + executable: true, + mutual: Unify, + recursive_argument: fuel +} + +definition{ + name: irOk, + parameters: [ty : LexiconLse::Lse, st : Ck], + result: Ir, + body: (record Ir {ty := ty, error := (ctor Option.none [Bytes]), ck := st}), + executable: true +} + +definition{ + name: irFail, + parameters: [st : Ck, message : Bytes], + result: Ir, + body: (record Ir {ty := (ctor LexiconLse::Lse.LSortProp), error := (ctor Option.some [Bytes] message), ck := st}), + executable: true +} + +definition{ + name: irMeta, + parameters: [st : Ck], + result: Ir, + body: (let made : Sr := (freshMeta st); (irOk (project made out) (project made ck))), + executable: true +} + +definition{ + name: scopeFind, + parameters: [scope : (List (Bytes * LexiconLse::Lse)), name : Bytes], + result: (Option LexiconLse::Lse), + body: (prim list_fold (fun [name] (found : (Option LexiconLse::Lse)) (row : (Bytes * LexiconLse::Lse)) => (match found + | Option.some kept => found + | Option.none => (if (prim equal (first row) name : Bool) then (ctor Option.some [LexiconLse::Lse] (second row)) else (ctor Option.none [LexiconLse::Lse])))) (ctor Option.none [LexiconLse::Lse]) scope : (Option LexiconLse::Lse)), + executable: true +} + +definition{ + name: plural, + parameters: [count : Nat], + result: Bytes, + body: (if (count =? 1) then 0x else (lit "s")), + executable: true +} + +definition{ + name: requireSort, + parameters: [fuel : Fuel, st : Ck, expr : LexiconLse::Lse, ty : LexiconLse::Lse], + result: Ir, + body: (let zonked : Sr := (zonkSt fuel st ty); (let z : LexiconLse::Lse := (project zonked out); (let st2 : Ck := (project zonked ck); (let ke : Nat := (kindOf expr); (if (if (ke =? 8) then true else (ke =? 6)) then (let txtExpr : Bytes := (shown expr); (irFail st2 (cat txtExpr (lit " is a term, not a type")))) else (if ((kindOf z) (irOk z st2) + | Option.none => (if (mayUnfold fuel st2 z) then (irOk z st2) else (let txtExpr : Bytes := (shown expr); (let txtZ : Bytes := (shown z); (irFail st2 (cat (cat (cat txtExpr (lit " is not a type: its type is ")) txtZ) 0x)))))))))))), + executable: true +} + +definition{ + name: infer, + parameters: [ + fuel : Fuel, + st : Ck, + expr : LexiconLse::Lse, + scope : (List (Bytes * LexiconLse::Lse)) + ], + result: Ir, + body: (match fuel + | Fuel.FZ => (irFail (ckExhaust st) 0x) + | Fuel.FS f => (match expr + | LexiconLse::Lse.LSortProp => (irOk (ctor LexiconLse::Lse.LSortType (ctor LexiconLse::Universe.UNum 1)) st) + | LexiconLse::Lse.LSortType u => (irOk (ctor LexiconLse::Lse.LSortType (ctor LexiconLse::Universe.USucc u)) st) + | LexiconLse::Lse.LConst p e us => (let found : (Nat * Bool) := (lookupRow st p e); (let k : Nat := (first found); (if (k =? 0) then (irFail st (cat (cat (cat (cat (lit "`") p) (lit "::")) e) (lit "` does not resolve"))) else (if (k =? 1) then (irMeta st) else (if (k =? 2) then (irFail st (cat (cat (cat (cat (lit "`") p) (lit "::")) e) (lit "` has no signature and cannot appear in an expression"))) else (irOk (signatureOf st p e) st)))))) + | LexiconLse::Lse.LLocal name => (if (isMeta name) then (irMeta st) else (match (scopeFind scope name) + | Option.some ty => (irOk ty st) + | Option.none => (irFail st (cat (cat (lit "local `") name) (lit "` is not in scope"))))) + | LexiconLse::Lse.LNat digits => (let made : Sr := (literalMeta st); (irOk (project made out) (project made ck))) + | LexiconLse::Lse.LApp g args => (let kg : Nat := (kindOf g); (if (if (kg fnTy + | Option.none => (let items : (List LexiconLse::Lse) := (chainItems f args); (inferApp f (project fnTy ck) expr g items (prim length items : Nat) (project fnTy ty) 0 scope)))))) + | LexiconLse::Lse.LPi bs body => (inferBinding f st bs body scope false bs) + | LexiconLse::Lse.LLam bs body => (inferBinding f st bs body scope true bs) + | LexiconLse::Lse.LLet n t v body => (let declared : Ir := (expectType f st t scope); (match (project declared error) + | Option.some message => declared + | Option.none => (let bound : Ir := (infer f (project declared ck) v scope); (match (project bound error) + | Option.some message => bound + | Option.none => (let agreed : Ur := (unify f (project bound ck) t (project bound ty)); (match (project agreed error) + | Option.some message => (irFail (project agreed ck) message) + | Option.none => (infer f (project agreed ck) body (cons (n, t) scope)))))))) + | LexiconLse::Lse.LNil => (irOk (ctor LexiconLse::Lse.LSortProp) st) + | LexiconLse::Lse.LArg h t => (irOk (ctor LexiconLse::Lse.LSortProp) st) + | LexiconLse::Lse.LBinder m n t r => (irOk (ctor LexiconLse::Lse.LSortProp) st))), + executable: true, + mutual: Infer, + recursive_argument: fuel +} + +definition{ + name: inferBinding, + parameters: [ + fuel : Fuel, + st : Ck, + chain : LexiconLse::Lse, + body : LexiconLse::Lse, + scope : (List (Bytes * LexiconLse::Lse)), + lam : Bool, + binders : LexiconLse::Lse + ], + result: Ir, + body: (match fuel + | Fuel.FZ => (irFail (ckExhaust st) 0x) + | Fuel.FS f => (match chain + | LexiconLse::Lse.LBinder m n t r => (let typedBinder : Ir := (expectType f st t scope); (match (project typedBinder error) + | Option.some message => typedBinder + | Option.none => (let next : Ir := (inferBinding f (project typedBinder ck) r body (cons (n, t) scope) lam binders); (record Ir {ty := (project next ty), error := (project next error), ck := (project next ck)})))) + | LexiconLse::Lse.LNil => (if lam then (let inner : Ir := (infer f st body scope); (match (project inner error) + | Option.some message => inner + | Option.none => (irOk (ctor LexiconLse::Lse.LPi binders (project inner ty)) (project inner ck)))) else (expectType f st body scope)) + | LexiconLse::Lse.LSortProp => (expectType f st body scope) + | LexiconLse::Lse.LSortType u => (expectType f st body scope) + | LexiconLse::Lse.LConst p e us => (expectType f st body scope) + | LexiconLse::Lse.LLocal name => (expectType f st body scope) + | LexiconLse::Lse.LApp g args => (expectType f st body scope) + | LexiconLse::Lse.LPi bs inner => (expectType f st body scope) + | LexiconLse::Lse.LLam bs inner => (expectType f st body scope) + | LexiconLse::Lse.LLet n t v inner => (expectType f st body scope) + | LexiconLse::Lse.LNat digits => (expectType f st body scope) + | LexiconLse::Lse.LArg h t => (expectType f st body scope))), + executable: true, + mutual: Infer, + recursive_argument: fuel +} + +definition{ + name: expectType, + parameters: [ + fuel : Fuel, + st : Ck, + expr : LexiconLse::Lse, + scope : (List (Bytes * LexiconLse::Lse)) + ], + result: Ir, + body: (match fuel + | Fuel.FZ => (irFail (ckExhaust st) 0x) + | Fuel.FS f => (let inferred : Ir := (infer f st expr scope); (match (project inferred error) + | Option.some message => inferred + | Option.none => (requireSort f (project inferred ck) expr (project inferred ty))))), + executable: true, + mutual: Infer, + recursive_argument: fuel +} + +definition{ + name: inferRest, + parameters: [ + fuel : Fuel, + st : Ck, + remaining : (List LexiconLse::Lse), + scope : (List (Bytes * LexiconLse::Lse)) + ], + result: Ir, + body: (match fuel + | Fuel.FZ => (irFail (ckExhaust st) 0x) + | Fuel.FS f => (match remaining + | List.nil => (irMeta st) + | List.cons argument more => (let seen : Ir := (infer f st argument scope); (match (project seen error) + | Option.some message => seen + | Option.none => (let next : Ir := (inferRest f (project seen ck) more scope); (record Ir {ty := (project next ty), error := (project next error), ck := (project next ck)})))))), + executable: true, + mutual: Infer, + recursive_argument: fuel +} + +definition{ + name: inferApp, + parameters: [ + fuel : Fuel, + st : Ck, + expr : LexiconLse::Lse, + g : LexiconLse::Lse, + remaining : (List LexiconLse::Lse), + total : Nat, + fty : LexiconLse::Lse, + supplied : Nat, + scope : (List (Bytes * LexiconLse::Lse)) + ], + result: Ir, + body: (match fuel + | Fuel.FZ => (irFail (ckExhaust st) 0x) + | Fuel.FS f => (match remaining + | List.nil => (irOk fty st) + | List.cons headArg rest => (let zonked : Sr := (zonkSt f st fty); (let z : LexiconLse::Lse := (project zonked out); (let st2 : Ck := (project zonked ck); (match z + | LexiconLse::Lse.LPi bs body => (inferBinders f st2 expr g (binderItems f bs) body remaining total supplied scope) + | LexiconLse::Lse.LSortProp => (inferStuck f st2 expr g remaining total supplied z scope) + | LexiconLse::Lse.LSortType u => (inferStuck f st2 expr g remaining total supplied z scope) + | LexiconLse::Lse.LConst p e us => (inferStuck f st2 expr g remaining total supplied z scope) + | LexiconLse::Lse.LLocal name => (inferStuck f st2 expr g remaining total supplied z scope) + | LexiconLse::Lse.LApp h args => (inferStuck f st2 expr g remaining total supplied z scope) + | LexiconLse::Lse.LLam bs body => (inferStuck f st2 expr g remaining total supplied z scope) + | LexiconLse::Lse.LLet n t v body => (inferStuck f st2 expr g remaining total supplied z scope) + | LexiconLse::Lse.LNat digits => (inferStuck f st2 expr g remaining total supplied z scope) + | LexiconLse::Lse.LNil => (inferStuck f st2 expr g remaining total supplied z scope) + | LexiconLse::Lse.LArg h t => (inferStuck f st2 expr g remaining total supplied z scope) + | LexiconLse::Lse.LBinder m n t r => (inferStuck f st2 expr g remaining total supplied z scope))))))), + executable: true, + mutual: Infer, + recursive_argument: fuel +} + +definition{ + name: inferStuck, + parameters: [ + fuel : Fuel, + st : Ck, + expr : LexiconLse::Lse, + g : LexiconLse::Lse, + remaining : (List LexiconLse::Lse), + total : Nat, + supplied : Nat, + z : LexiconLse::Lse, + scope : (List (Bytes * LexiconLse::Lse)) + ], + result: Ir, + body: (match fuel + | Fuel.FZ => (irFail (ckExhaust st) 0x) + | Fuel.FS f => (match (metaOf z) + | Option.some m => (inferRest f st remaining scope) + | Option.none => (if (mayUnfold f st z) then (inferRest f st remaining scope) else (let txtExpr : Bytes := (shown expr); (let txtG : Bytes := (shown g); (let txtZ : Bytes := (shown z); (irFail st (cat (cat (cat (cat (cat (cat (cat (cat (cat txtExpr (lit " supplies ")) (LexLeanStd.Numeral::decimal total)) (lit " explicit argument")) (plural total)) (lit " but ")) txtG) (lit " accepts ")) (LexLeanStd.Numeral::decimal supplied)) (cat (cat (lit ": after them its type is ") txtZ) (lit ", not a function")))))))))), + executable: true, + mutual: Infer, + recursive_argument: fuel +} + +definition{ + name: inferBinders, + parameters: [ + fuel : Fuel, + st : Ck, + expr : LexiconLse::Lse, + g : LexiconLse::Lse, + binders : (List Bd), + body : LexiconLse::Lse, + remaining : (List LexiconLse::Lse), + total : Nat, + supplied : Nat, + scope : (List (Bytes * LexiconLse::Lse)) + ], + result: Ir, + body: (match fuel + | Fuel.FZ => (irFail (ckExhaust st) 0x) + | Fuel.FS f => (match binders + | List.nil => (inferApp f st expr g remaining total body supplied scope) + | List.cons binder rest => (if ((project binder mode) =? 0) then (match remaining + | List.nil => (irOk (ctor LexiconLse::Lse.LPi (chainOf binders) body) st) + | List.cons argument more => (let actual : Ir := (infer f st argument scope); (match (project actual error) + | Option.some message => actual + | Option.none => (let agreed : Ur := (unify f (project actual ck) (project binder ty) (project actual ty)); (match (project agreed error) + | Option.some message => (irFail (project agreed ck) message) + | Option.none => (inferInstantiate f (project agreed ck) expr g binder argument rest body more total (supplied + 1) scope)))))) else (let made : Sr := (freshMeta st); (inferInstantiate f (project made ck) expr g binder (project made out) rest body remaining total supplied scope))))), + executable: true, + mutual: Infer, + recursive_argument: fuel +} + +definition{ + name: inferInstantiate, + parameters: [ + fuel : Fuel, + st : Ck, + expr : LexiconLse::Lse, + g : LexiconLse::Lse, + binder : Bd, + value : LexiconLse::Lse, + rest : (List Bd), + body : LexiconLse::Lse, + remaining : (List LexiconLse::Lse), + total : Nat, + supplied : Nat, + scope : (List (Bytes * LexiconLse::Lse)) + ], + result: Ir, + body: (match fuel + | Fuel.FZ => (irFail (ckExhaust st) 0x) + | Fuel.FS f => (let later : Ls := (substTypes f st rest (project binder name) value); (let newBody : Sr := (subst f (project later ck) body (project binder name) value); (inferBinders f (project newBody ck) expr g (project later items) (project newBody out) remaining total supplied scope)))), + executable: true, + mutual: Infer, + recursive_argument: fuel +} + +definition{ + name: literalTypes, + parameters: [fuel : Fuel, st : Ck, metas : (List Bytes)], + result: ((List (Option LexiconLse::Lse)) * Ck), + body: (match fuel + | Fuel.FZ => ((nil (Option LexiconLse::Lse)), (ckExhaust st)) + | Fuel.FS f => (match metas + | List.nil => ((nil (Option LexiconLse::Lse)), st) + | List.cons metaName more => (let solved : Sr := (zonkSt f st (ctor LexiconLse::Lse.LLocal metaName)); (let pending : (Bool * Ck) := (hasUnsolved f (project solved ck) (project solved out)); (let others : ((List (Option LexiconLse::Lse)) * Ck) := (literalTypes f (second pending) more); ((cons (if (first pending) then (ctor Option.none [LexiconLse::Lse]) else (ctor Option.some [LexiconLse::Lse] (project solved out))) (first others)), (second others))))))), + executable: true, + recursive_argument: fuel +} + +definition{ + name: finish, + parameters: [fuel : Fuel, st : Ck, ty : LexiconLse::Lse], + result: (Result Checked Bytes), + body: (let zonked : Sr := (zonkSt fuel st ty); (let typed : ((List (Option LexiconLse::Lse)) * Ck) := (literalTypes fuel (project zonked ck) (LexLeanStd.Order::reverseList [Bytes] (project st literals))); (if (project (second typed) exhausted) then (ctor Result.error [Checked, Bytes] (lit "the checker's bound on nesting is exhausted")) else (ctor Result.ok [Checked, Bytes] (record Checked {ty := (project zonked out), literals := (first typed)}))))), + executable: true +} + +definition{ + name: start, + parameters: [rows : (List CRow), fallback : Nat], + result: Ck, + body: (record Ck {metas := (nil (Bytes * LexiconLse::Lse)), nextMeta := 0, literals := (nil Bytes), freshCounter := 0, exhausted := false, rows := rows, fallback := fallback}), + executable: true +} + +definition{ + name: outcome, + parameters: [fuel : Fuel, result : Ir], + result: (Result Checked Bytes), + body: (if (project (project result ck) exhausted) then (ctor Result.error [Checked, Bytes] (lit "the checker's bound on nesting is exhausted")) else (match (project result error) + | Option.some message => (ctor Result.error [Checked, Bytes] message) + | Option.none => (finish fuel (project result ck) (project result ty)))), + executable: true +} + +definition{ + name: checkSignature, + parameters: [signature : LexiconLse::Lse, rows : (List CRow), fallback : Nat, depth : Nat], + result: (Result Checked Bytes), + body: (let fuel : Fuel := (mkFuel depth); (let typed : Ir := (expectType fuel (start rows fallback) signature (nil (Bytes * LexiconLse::Lse))); (outcome fuel typed))), + executable: true, + production: {effects: [allocation, overflow, recursion], targets: [rust-std]} +} + +definition{ + name: checkValue, + parameters: [ + value : LexiconLse::Lse, + signature : (Option LexiconLse::Lse), + rows : (List CRow), + fallback : Nat, + depth : Nat + ], + result: (Result Checked Bytes), + body: (let fuel : Fuel := (mkFuel depth); (let inferred : Ir := (infer fuel (start rows fallback) value (nil (Bytes * LexiconLse::Lse))); (match signature + | Option.none => (outcome fuel inferred) + | Option.some sig => (if (if (project (project inferred ck) exhausted) then true else (match (project inferred error) + | Option.some message => true + | Option.none => false)) then (outcome fuel inferred) else (let agreed : Ur := (unify fuel (project inferred ck) sig (project inferred ty)); (match (project agreed error) + | Option.some message => (let txtValue : Bytes := (shown value); (let txtSig : Bytes := (shown sig); (outcome fuel (irFail (project agreed ck) (cat (cat (cat (cat (cat (lit "defined value ") txtValue) (lit " does not match the signature ")) txtSig) (lit ": ")) message))))) + | Option.none => (outcome fuel (irOk (project inferred ty) (project agreed ck))))))))), + executable: true, + production: {effects: [allocation, overflow, recursion], targets: [rust-std]} +} +\end{semanticmodule} +\end{lexlean} diff --git a/crates/conformance/src/cases/bootstrap.rs b/crates/conformance/src/cases/bootstrap.rs index ac797da29e..e90572e413 100644 --- a/crates/conformance/src/cases/bootstrap.rs +++ b/crates/conformance/src/cases/bootstrap.rs @@ -28,6 +28,7 @@ mod gnaf_validate; mod lexicon_lre; mod lexicon_lse; mod lock_text; +mod lse_types; mod manifest_text; mod project_text; mod runtime_validate; @@ -2574,6 +2575,7 @@ fn case(id: &str) { "BS-32" => content_id::case(), "BS-33" => project_text::case(), "BS-34" => manifest_text::case(), + "BS-52" => lse_types::case(), other => panic!("no bootstrap case is wired for {other}"), } } diff --git a/crates/conformance/src/cases/bootstrap/lexicon_lse.rs b/crates/conformance/src/cases/bootstrap/lexicon_lse.rs index 8cc5c85c3d..3d252016f3 100755 --- a/crates/conformance/src/cases/bootstrap/lexicon_lse.rs +++ b/crates/conformance/src/cases/bootstrap/lexicon_lse.rs @@ -162,7 +162,7 @@ fn binder_chain(binders: &[LseBinder]) -> Value { } /// An expression as the module's `Lse`. -fn enc_lse(expression: &Lse) -> Value { +pub(super) fn enc_lse(expression: &Lse) -> Value { match expression { Lse::SortProp => adt("Lse", "LSortProp", Vec::new()), Lse::SortType(universe) => adt("Lse", "LSortType", vec![enc_universe(universe)]), @@ -576,7 +576,7 @@ fn drawn_binders(seeded: &mut Seeded, depth: usize) -> Vec { } /// An expression of at most `depth` levels. -fn drawn_lse(seeded: &mut Seeded, depth: usize) -> Lse { +pub(super) fn drawn_lse(seeded: &mut Seeded, depth: usize) -> Lse { let kind = if depth == 0 { seeded.below(5) } else { diff --git a/crates/conformance/src/cases/bootstrap/lse_types.rs b/crates/conformance/src/cases/bootstrap/lse_types.rs new file mode 100755 index 0000000000..53dbe84086 --- /dev/null +++ b/crates/conformance/src/cases/bootstrap/lse_types.rs @@ -0,0 +1,916 @@ +//! `BS-52` (SPEC.md §17.23, §13.6 to §13.8, §14.4): the conservative checker +//! of LexLean Semantic Expressions as language-1.2 source, +//! `LexLeanBoot.LseTypes`, held to the byte to `lexicon::lse::check_signature` +//! and `check_value`: the inferred type and the type of every natural literal, +//! or the message of the first error, with the oracle's metavariables and +//! capture-avoiding renames. +//! +//! What the oracle's lookup closure answers is handed over as a table of rows +//! and the answer for a constant no row names (`Missing` in the closure, +//! `Opaque` in the package-local check). The checker is structural recursion +//! over a bound on its nesting the host passes, so both roots admit +//! `recursion`. Each planted defect names the one vector that catches it. + +use std::collections::{BTreeMap, BTreeSet}; + +use lexlean::calculus::Value; +use lexlean::lexicon::entry::Denotation; +use lexlean::lexicon::lse::{ + check_signature, check_value, parse, Checked, ConstInfo, Lse, QualifiedId, TypeError, +}; +use lexlean::lexicon::package::{load_package, LexiconPackage, LoadContext}; +use serde_json::Value as Json2; + +use super::lexicon_lse::{drawn_lse, enc_lse}; +use super::rust_validate::{eligibility_of, rustc_differential}; +use super::{list, option, text, Run, Seeded}; +use crate::production_export as harness; +use crate::support::{self, repo_root}; + +const MODULE: &str = "LexLeanBoot.LseTypes"; +const ROOTS: [(&str, &[&str]); 2] = [ + ("checkSignature", &["allocation", "overflow", "recursion"]), + ("checkValue", &["allocation", "overflow", "recursion"]), +]; + +/// The most a part of a certificate of a root may be, as the row states. +const CERTIFICATE_BUDGET: u64 = 3_145_728; +/// The depth the expressions are read at, and the bound on the checker's +/// nesting the host passes. +const DEPTH: u64 = 64; +const NESTING: u64 = 192; +/// Seeded expressions under the evaluator, and under rustc. +const EVALUATOR_SEEDED: usize = 200; +const RUSTC_SEEDED: usize = 1_500; + +const MISSING: u64 = 0; +const OPAQUE: u64 = 1; + +// ---------------------------------------------------------------- the table + +/// One row of the lookup table: what the oracle's closure answers for one +/// constant. +#[derive(Clone)] +struct Row { + id: QualifiedId, + signature: Option, + defined: bool, +} + +#[derive(Clone)] +struct Table { + rows: Vec, + fallback: u64, +} + +impl Table { + fn info(&self, id: &QualifiedId) -> ConstInfo<'_> { + match self.rows.iter().find(|row| row.id == *id) { + Some(row) => match &row.signature { + Some(signature) => ConstInfo::Signature { + signature, + defined: row.defined, + }, + None => ConstInfo::NoSignature, + }, + None if self.fallback == OPAQUE => ConstInfo::Opaque, + None => ConstInfo::Missing, + } + } + + /// The rows the checker can consult for these expressions: the + /// constants they reference and, through the signatures of those, every + /// constant reachable from them. No other row is ever looked up. + fn reachable(&self, roots: &[&Lse]) -> Vec<&Row> { + let mut wanted: Vec = roots + .iter() + .flat_map(|root| root.referenced_consts()) + .collect(); + let mut seen: BTreeSet = BTreeSet::new(); + let mut out = Vec::new(); + while let Some(id) = wanted.pop() { + if !seen.insert(id.to_string()) { + continue; + } + if let Some(row) = self.rows.iter().find(|row| row.id == id) { + if let Some(signature) = &row.signature { + wanted.extend(signature.referenced_consts()); + } + out.push(row); + } + } + out.sort_by_key(|row| row.id.to_string()); + out + } + + fn value(&self, roots: &[&Lse]) -> (Value, Value) { + let rows = self + .reachable(roots) + .into_iter() + .map(|row| Value::Adt { + constructor: 0, + fields: vec![ + text(&row.id.package), + text(&row.id.entry), + nat(if row.signature.is_some() { 3 } else { 2 }), + enc_lse(row.signature.as_ref().unwrap_or(&Lse::SortProp)), + Value::Bool { value: row.defined }, + ], + }) + .collect(); + (list(rows), nat(self.fallback)) + } +} + +fn nat(value: u64) -> Value { + Value::Nat { + value: value.to_string(), + } +} + +fn outcome(result: Result) -> Value { + match result { + Ok(checked) => Value::Ok { + value: Box::new(Value::Adt { + constructor: 0, + fields: vec![ + enc_lse(&checked.ty), + list( + checked + .literal_types + .iter() + .map(|literal| option(literal.as_ref().map(enc_lse))) + .collect(), + ), + ], + }), + }, + Err(error) => Value::Error { + value: Box::new(text(&error.message)), + }, + } +} + +fn signature_run(signature: &Lse, table: &Table) -> Run { + let lookup = |id: &QualifiedId| table.info(id); + let (rows, fallback) = table.value(&[signature]); + ( + vec![enc_lse(signature), rows, fallback, nat(NESTING)], + serde_json::to_value(outcome(check_signature(signature, &lookup))) + .expect("a value is JSON"), + ) +} + +fn value_run(value: &Lse, signature: Option<&Lse>, table: &Table) -> Run { + let lookup = |id: &QualifiedId| table.info(id); + let mut roots = vec![value]; + roots.extend(signature); + let (rows, fallback) = table.value(&roots); + ( + vec![ + enc_lse(value), + option(signature.map(enc_lse)), + rows, + fallback, + nat(NESTING), + ], + serde_json::to_value(outcome(check_value(value, signature, &lookup))) + .expect("a value is JSON"), + ) +} + +fn parsed(source: &str) -> Lse { + parse(source, DEPTH).unwrap_or_else(|error| panic!("`{source}` parses: {error}")) +} + +fn q(text: &str) -> QualifiedId { + QualifiedId::parse(text).expect("a qualified ID") +} + +/// The hand-built glossary `t` the named vectors are checked against. +fn glossary(fallback: u64) -> Table { + let rows = [ + ("t::nat", Some("(sort (type 0))"), false), + ("t::zero", Some("(const t::nat)"), false), + ("t::succ", Some("(pi ((explicit n (const t::nat))) (const t::nat))"), false), + ("t::add", Some("(pi ((explicit a (const t::nat)) (explicit b (const t::nat))) (const t::nat))"), false), + ("t::eq", Some("(pi ((implicit A (sort (type 0))) (explicit a (local A)) (explicit b (local A))) (sort prop))"), false), + ("t::double", Some("(pi ((explicit n (const t::nat))) (const t::nat))"), true), + ("t::nattype", Some("(sort (type 0))"), true), + ("t::mk", Some("(pi ((explicit x (const t::nat))) (const t::nattype))"), false), + ("t::word", None, false), + ("t::list", Some("(pi ((explicit A (sort (type 0)))) (sort (type 0)))"), false), + ("t::pair", Some("(pi ((explicit A (sort (type 0))) (explicit B (sort (type 0)))) (sort (type 0)))"), false), + ("t::prop", Some("(sort prop)"), false), + ("t::k", Some("(pi ((explicit y (const t::nat))) (let x (const t::nat) (local y) (app (const t::eq) (local x) (local y))))"), false), + ]; + Table { + rows: rows + .iter() + .map(|(id, signature, defined)| Row { + id: q(id), + signature: signature.map(parsed), + defined: *defined, + }) + .collect(), + fallback, + } +} + +// ------------------------------------------------------------------ vectors + +struct Vector { + label: &'static str, + root: usize, + run: Run, +} + +#[allow(clippy::too_many_lines)] +fn named() -> Vec { + let closed = glossary(MISSING); + let open = glossary(OPAQUE); + let mut out = Vec::new(); + let signatures: &[(&'static str, &str, bool)] = &[ + ("a sort", "(sort prop)", false), + ("a constant type", "(const t::nat)", false), + ( + "a pi of types", + "(pi ((explicit a (const t::nat))) (const t::nat))", + false, + ), + ("an unresolved constant", "(const t::missing)", false), + ("a constant without a signature", "(const t::word)", false), + ("a term is no type", "(const t::zero)", false), + ("a natural literal is a term", "(nat 3)", false), + ( + "a lam is a term", + "(lam ((explicit x (sort prop))) (local x))", + false, + ), + ("a local out of scope", "(local x)", false), + ( + "an application of a sort", + "(app (sort prop) (const t::zero))", + false, + ), + ( + "an application of a literal", + "(app (nat 1) (const t::zero))", + false, + ), + ( + "two arguments too many", + "(app (const t::succ) (const t::zero) (const t::zero))", + false, + ), + ( + "one argument too many", + "(app (const t::zero) (const t::zero))", + false, + ), + ( + "an argument of the wrong type", + "(app (const t::succ) (sort prop))", + false, + ), + ( + "a partial application", + "(app (const t::add) (const t::zero))", + false, + ), + ( + "an equation is a proposition", + "(app (const t::eq) (const t::zero) (const t::zero))", + false, + ), + ( + "a literal with an inferred type", + "(app (const t::eq) (const t::zero) (nat 1))", + false, + ), + ( + "literals of an undetermined type", + "(app (const t::eq) (nat 1) (nat 2))", + false, + ), + ( + "an opaque head", + "(app (const other::f) (const t::zero))", + true, + ), + ( + "a defined result type unfolds", + "(app (const t::mk) (const t::zero) (const t::zero))", + false, + ), + ( + "a let", + "(let x (sort prop) (const t::prop) (local x))", + false, + ), + ( + "a let whose value disagrees", + "(let x (const t::nat) (sort prop) (local x))", + false, + ), + ( + "a binder that is no type", + "(pi ((explicit a (const t::zero))) (sort prop))", + false, + ), + ( + "a body that is no type", + "(pi ((explicit a (const t::nat))) (local a))", + false, + ), + ( + "a type of a list", + "(app (const t::list) (const t::nat))", + false, + ), + ("a sort of a universe", "(sort (type (max u 1)))", false), + ]; + for &(label, source, opaque) in signatures { + let table = if opaque { &open } else { &closed }; + out.push(Vector { + label, + root: 0, + run: signature_run(&parsed(source), table), + }); + } + let values: &[(&'static str, &str, Option<&str>)] = &[ + ("a value matching its signature", "(lam ((explicit n (const t::nat))) (app (const t::succ) (local n)))", Some("(pi ((explicit m (const t::nat))) (const t::nat))")), + ("a value that is no function", "(const t::zero)", Some("(pi ((explicit m (const t::nat))) (const t::nat))")), + ("a value of more binders", "(lam ((explicit a (const t::nat)) (explicit b (const t::nat))) (local a))", Some("(pi ((explicit m (const t::nat))) (const t::nat))")), + ("a value of another binder mode", "(lam ((implicit n (const t::nat))) (local n))", Some("(pi ((explicit n (const t::nat))) (const t::nat))")), + ("a value without a signature", "(app (const t::succ) (const t::zero))", None), + ("a value of an unresolved head", "(app (const t::nope) (nat 1))", None), + ("a renamed binder avoids capture", "(lam ((explicit c (sort (type 0))) (explicit b (pi ((implicit a (sort prop))) (local c)))) (sort prop))", Some("(pi ((explicit a (sort (type 0))) (explicit b (pi ((explicit c (sort prop))) (local a)))) (sort (type 1)))")), + ("a renamed let avoids capture", "(lam ((explicit x (const t::nat))) (app (const t::k) (local x)))", None), + ("applications of different arity", "(lam ((explicit x (app (const t::list) (const t::nat)))) (sort prop))", Some("(pi ((explicit x (app (const t::pair) (const t::nat) (const t::nat)))) (sort (type 1)))")), + ("a value whose binders are named apart", "(lam ((explicit n (sort (type 0))) (explicit z (local n))) (local z))", Some("(pi ((explicit m (sort (type 0))) (explicit y (local m))) (local m))")), + ("a value matched by a defined signature", "(const t::zero)", Some("(const t::nattype)")), + ("a literal value", "(nat 7)", Some("(const t::nat)")), + ("a proposition value", "(app (const t::eq) (const t::zero) (const t::zero))", Some("(sort prop)")), + ]; + for &(label, value, signature) in values { + let signature = signature.map(parsed); + out.push(Vector { + label, + root: 1, + run: value_run(&parsed(value), signature.as_ref(), &closed), + }); + } + out +} + +// --------------------------------------------------------------- committed + +/// Every lexicon package the repository commits that loads: the builtin +/// packages and every directory with a `lexicon.toml`. +fn committed_packages() -> Vec { + let bootstrap = lexlean::lexicon::load_bootstrap().expect("the bootstrap loads"); + let mut out: Vec = Vec::new(); + for language in ["1.0", "1.1", "1.2"] { + let Ok(bootstrap) = lexlean::lexicon::load_bootstrap_for(language) else { + continue; + }; + let ctx = LoadContext { + language, + forbidden_controls: &bootstrap.structural.forbidden_controls, + max_scope_depth: DEPTH, + }; + for row in &bootstrap.builtin_packages { + if let Ok(package) = lexlean::lexicon::load_builtin_package(row, &ctx) { + out.push(package); + } + } + } + let root = repo_root(); + let walker = walkdir::WalkDir::new(root.as_std_path()) + .sort_by_file_name() + .into_iter() + .filter_entry(|entry| { + let name = entry.file_name().to_string_lossy(); + !(entry.file_type().is_dir() + && (matches!(name.as_ref(), "target" | ".git" | ".lexlean" | "language") + || support::holds_cache_tag(entry))) + }); + for entry in walker.flatten() { + if entry.file_name() != "lexicon.toml" { + continue; + } + let Some(directory) = entry.path().parent() else { + continue; + }; + let Ok(manifest) = std::fs::read_to_string(entry.path()) else { + continue; + }; + let Some(language) = manifest.parse::().ok().and_then(|table| { + table + .get("language") + .and_then(|v| v.as_str().map(str::to_owned)) + }) else { + continue; + }; + let mut files: Vec<(String, Vec)> = walkdir::WalkDir::new(directory) + .into_iter() + .flatten() + .filter(|file| file.file_type().is_file()) + .filter_map(|file| { + let relative = file + .path() + .strip_prefix(directory) + .ok()? + .to_string_lossy() + .replace('\\', "/"); + (relative == "lexicon.toml" || relative.starts_with("entries/")) + .then(|| { + std::fs::read(file.path()) + .ok() + .map(|bytes| (relative, bytes)) + }) + .flatten() + }) + .collect(); + files.sort_by(|a, b| a.0.as_bytes().cmp(b.0.as_bytes())); + let ctx = LoadContext { + language: &language, + forbidden_controls: &bootstrap.structural.forbidden_controls, + max_scope_depth: DEPTH, + }; + if let Ok(package) = load_package("committed", &files, None, &ctx) { + out.push(package); + } + } + out +} + +fn rows_of(package: &LexiconPackage) -> Vec { + package + .entries + .values() + .map(|entry| Row { + id: QualifiedId { + package: package.id.clone(), + entry: entry.id.clone(), + }, + signature: entry.signature.clone(), + defined: matches!(entry.denotation, Denotation::Defined { .. }), + }) + .collect() +} + +/// The runs of every committed signature and defined value: against its own +/// package with every other constant opaque, as a package loads, and against +/// the package and the builtin packages with every other constant missing. +fn committed_runs() -> (Vec, Vec, usize) { + let packages = committed_packages(); + let builtin_ids: BTreeSet = lexlean::lexicon::load_bootstrap() + .expect("the bootstrap loads") + .builtin_packages + .iter() + .map(|row| row.id.clone()) + .collect(); + let builtin_rows: Vec = packages + .iter() + .filter(|package| builtin_ids.contains(&package.id)) + .flat_map(rows_of) + .collect(); + let mut signatures = Vec::new(); + let mut values = Vec::new(); + let mut seen = BTreeSet::new(); + for package in &packages { + let local = Table { + rows: rows_of(package), + fallback: OPAQUE, + }; + let mut wide_rows = rows_of(package); + for row in &builtin_rows { + if row.id.package != package.id { + wide_rows.push(row.clone()); + } + } + let wide = Table { + rows: wide_rows, + fallback: MISSING, + }; + for entry in package.entries.values() { + let key = format!("{}::{}", package.id, entry.id); + if !seen.insert((key, entry.signature.as_ref().map(|s| s.print(false)))) { + continue; + } + if let Some(signature) = &entry.signature { + signatures.push(signature_run(signature, &local)); + signatures.push(signature_run(signature, &wide)); + } + if let Denotation::Defined { value, .. } = &entry.denotation { + values.push(value_run(value, entry.signature.as_ref(), &local)); + values.push(value_run(value, entry.signature.as_ref(), &wide)); + } + } + } + (signatures, values, packages.len()) +} + +/// `count` seeded expressions, each checked as a signature and as a value +/// against a signature of its own, under the hand-built glossary. +fn seeded_runs(count: usize) -> (Vec, Vec) { + let mut seeded = Seeded(0x0B55_0052); + let tables = [glossary(MISSING), glossary(OPAQUE)]; + let mut signatures = Vec::with_capacity(count); + let mut values = Vec::with_capacity(count); + for _ in 0..count { + let table = seeded.pick(&tables); + let depth = 1 + seeded.below(3); + let expression = drawn_lse(&mut seeded, depth); + signatures.push(signature_run(&expression, table)); + let against = if seeded.chance(70) { + { + let depth = 1 + seeded.below(3); + Some(drawn_lse(&mut seeded, depth)) + } + } else { + None + }; + values.push(value_run(&expression, against.as_ref(), table)); + } + (signatures, values) +} + +// ------------------------------------------------------------------- plants + +struct Plant { + label: &'static str, + root: usize, + site: &'static str, + replacement: &'static str, + vector: &'static str, +} + +const PLANTS: &[Plant] = &[ + Plant { + label: "the actual side's metavariable is not solved", + root: 0, + site: "| Option.some m => (urOk (ckSolve st2 m e))", + replacement: "| Option.some m => (urOk st2)", + vector: "a literal with an inferred type", + }, + Plant { + label: "a sort of a universe is no type", + root: 0, + site: "(if ((kindOf z) (let made : Sr := (literalMeta st);", + replacement: "| LexiconLse::Lse.LNat digits => (let made : Sr := (freshMeta st);", + vector: "a literal with an inferred type", + }, + Plant { + label: "a defined constant does not unfold", + root: 0, + site: "(if ((first found) =? 3) then (second found) else false)", + replacement: "false", + vector: "a defined result type unfolds", + }, + Plant { + label: "a constant no row names is missing", + root: 0, + site: "| Option.none => ((project st fallback), false)),", + replacement: "| Option.none => (0, false)),", + vector: "an opaque head", + }, + Plant { + label: "a lam's type forgets its binders", + root: 1, + site: "(irOk (ctor LexiconLse::Lse.LPi binders (project inner ty)) (project inner ck))", + replacement: "(irOk (project inner ty) (project inner ck))", + vector: "a value matching its signature", + }, + Plant { + label: "a let's body does not see its name", + root: 0, + site: "(infer f (project agreed ck) body (cons (n, t) scope))", + replacement: "(infer f (project agreed ck) body scope)", + vector: "a let", + }, + Plant { + label: "a partial application drops the next binder", + root: 0, + site: "| List.nil => (irOk (ctor LexiconLse::Lse.LPi (chainOf binders) body) st)", + replacement: "| List.nil => (irOk (ctor LexiconLse::Lse.LPi (chainOf rest) body) st)", + vector: "a partial application", + }, + Plant { + label: "zonking forgets solutions", + root: 0, + site: "| Option.some solution => (zonk f metas solution)", + replacement: "| Option.some solution => (ctor Option.some [LexiconLse::Lse] (ctor LexiconLse::Lse.LLocal name))", + vector: "a literal with an inferred type", + }, + Plant { + label: "applications of different arity unify their heads", + root: 1, + site: "(if ((prim length (chainItems fuel xs) : Nat) =? (prim length (chainItems fuel ys) : Nat)) then 1 else 3)", + replacement: "1", + vector: "applications of different arity", + }, + Plant { + label: "the actual binder keeps its name", + root: 1, + site: "(let renamed : LexiconLse::Lse := (ctor LexiconLse::Lse.LLocal (project be name));", + replacement: "(let renamed : LexiconLse::Lse := (ctor LexiconLse::Lse.LLocal (project ba name));", + vector: "a value whose binders are named apart", + }, + Plant { + label: "a constant without a signature resolves", + root: 0, + site: "(if (k =? 2) then (irFail st", + replacement: "(if false then (irFail st", + vector: "a constant without a signature", + }, +]; + +fn plants(vectors: &[Vector]) { + let mut missed = Vec::new(); + for plant in PLANTS { + let vector = vectors + .iter() + .find(|vector| vector.label == plant.vector) + .unwrap_or_else(|| panic!("{}: no vector `{}`", plant.label, plant.vector)); + assert_eq!( + vector.root, plant.root, + "{}: the vector is a run of the root planted", + plant.label + ); + let planted = support::P::bootstrap(); + let source = planted.read_raw("src/LseTypes.lex.tex"); + assert_eq!( + source.matches(plant.site).count(), + 1, + "{}: the plant site occurs once", + plant.label + ); + planted.write( + "src/LseTypes.lex.tex", + &source.replacen(plant.site, plant.replacement, 1), + ); + let checked = support::checked_project(&planted); + let low = super::lower(&checked, &planted, MODULE, ROOTS[plant.root].0); + match super::evaluate(&low, std::slice::from_ref(&vector.run), 1).first() { + Some(first) => eprintln!( + "BS-52: plant detected: {}: {}", + plant.label, + first.chars().take(160).collect::() + ), + None => missed.push(plant.label), + } + } + assert!( + missed.is_empty(), + "planted defects their own vector does not detect: {missed:?}" + ); +} + +fn export(lowered: &BTreeMap<&str, super::Lowered>) { + let exported = harness::exported("bootstrap"); + let bundle = exported.bundle(); + let mut realized = 0usize; + for exported_root in &exported.output.result.roots { + let Some((member, low)) = exported_root + .root + .strip_prefix(&format!("{MODULE}.")) + .and_then(|member| lowered.get(member).map(|low| (member, low))) + else { + continue; + }; + let largest = exported_root + .outputs + .iter() + .flat_map(|output| &output.certification.certificates) + .map(|certificate| certificate.file.byte_length) + .max() + .expect("a certificate"); + eprintln!( + "BS-52: {}: largest certificate part {largest} bytes", + exported_root.root + ); + assert!( + largest <= CERTIFICATE_BUDGET, + "{}: a certificate part of {largest} bytes is beyond {CERTIFICATE_BUDGET}", + exported_root.root + ); + for output in &exported_root.outputs { + let package: Json2 = serde_json::from_slice( + &std::fs::read( + bundle + .join(&output.package.directory) + .join("package.json") + .as_std_path(), + ) + .expect("package.json"), + ) + .expect("package JSON"); + assert_eq!( + package["program"], + serde_json::to_value(&low.program).expect("a program is JSON"), + "{member}: the exported package realizes the program the differential ran" + ); + realized += 1; + } + } + assert_eq!(realized, ROOTS.len(), "one certified package per root"); + let packages = harness::tree(&bundle.join("packages")); + let committed_packages = harness::tree(&harness::fixture_dir("bootstrap").join("packages")); + assert_eq!( + packages.keys().collect::>(), + committed_packages.keys().collect::>(), + "the exported package files are the committed ones" + ); + for (relative, content) in &packages { + assert!( + committed_packages.get(relative) == Some(content), + "packages/{relative} differs from the committed package" + ); + } +} + +/// Every error the checker can state, by a part of its message (R2). +const REFUSALS: &[&str] = &[ + "does not resolve", + "has no signature and cannot appear in an expression", + "is not in scope", + "which is not a function", + "explicit argument but", + "explicit arguments but", + "is a term, not a type", + "is not a type: its type is", + "arguments but", + "binder `", + "expected `", + "does not match the signature", +]; + +#[allow(clippy::too_many_lines)] +pub(super) fn case() { + let started = std::time::Instant::now(); + let project = support::P::bootstrap(); + let checked_units = project.check_ok(); + assert!( + checked_units.units.contains_key("LseTypes") + && checked_units.units.contains_key("LexiconLse"), + "LseTypes and LexiconLse are linked modules of the project" + ); + eligibility_of(&project, "LseTypes", MODULE, &ROOTS); + + let vectors = named(); + let labels: BTreeSet<&str> = vectors.iter().map(|vector| vector.label).collect(); + assert_eq!(labels.len(), vectors.len(), "each vector has its own label"); + let mut by_root: [Vec; 2] = Default::default(); + for vector in &vectors { + by_root[vector.root].push(vector.run.clone()); + } + let outputs: Vec = vectors + .iter() + .map(|vector| vector.run.1.to_string()) + .collect(); + for refusal in REFUSALS { + let needle = super::hex(refusal.as_bytes()); + assert!( + outputs.iter().any(|output| output.contains(&needle)), + "no vector reaches the error `{refusal}`" + ); + } + + let (committed_signatures, committed_values, package_count) = committed_runs(); + assert!( + committed_signatures.len() >= 200, + "the committed packages carry {} signature runs", + committed_signatures.len() + ); + let normative = support::normative_host(); + let (evaluator_seeded, rustc_seeded) = if normative { + (seeded_runs(EVALUATOR_SEEDED), seeded_runs(RUSTC_SEEDED)) + } else { + Default::default() + }; + eprintln!( + "BS-52: {} vectors, {} committed packages with {} signature and {} value runs, {} and {} seeded expressions under the evaluator and rustc ({:?})", + vectors.len(), + package_count, + committed_signatures.len(), + committed_values.len(), + evaluator_seeded.0.len(), + rustc_seeded.0.len(), + started.elapsed() + ); + + let checked = support::checked_project(&project); + let lowered: BTreeMap<&str, super::Lowered> = ROOTS + .iter() + .map(|(root, _)| (*root, super::lower(&checked, &project, MODULE, root))) + .collect(); + let mut committed = [committed_signatures, committed_values]; + let evaluator_seeded = [evaluator_seeded.0, evaluator_seeded.1]; + let rustc_seeded = [rustc_seeded.0, rustc_seeded.1]; + if normative { + for (index, (root, _)) in ROOTS.iter().enumerate() { + let mut runs: Vec = by_root[index].clone(); + runs.extend(committed[index].iter().cloned()); + runs.extend(evaluator_seeded[index].iter().cloned()); + let disagreements = super::evaluate(&lowered[root], &runs, 5); + assert!( + disagreements.is_empty(), + "{root} and the Rust oracle disagree: {disagreements:?}" + ); + eprintln!( + "BS-52: {root}: the evaluator agrees on {} runs ({:?})", + runs.len(), + started.elapsed() + ); + } + plants(&vectors); + } else { + eprintln!("BS-52: platform-bound host: the evaluator, the seeded runs, and the planted defects run on the normative host"); + } + + let mut compiled: [Vec; 2] = Default::default(); + for (index, runs) in compiled.iter_mut().enumerate() { + runs.append(&mut by_root[index]); + runs.append(&mut committed[index]); + runs.extend(rustc_seeded[index].iter().cloned()); + } + let suites: Vec<(&str, &super::Lowered, &[Run])> = ROOTS + .iter() + .enumerate() + .map(|(index, (root, _))| (*root, &lowered[root], compiled[index].as_slice())) + .collect(); + let total = rustc_differential("BS-52", &suites, "oracle"); + eprintln!("BS-52: {total} rustc runs agree ({:?})", started.elapsed()); + + if !support::lean_backed("BS-52") { + return; + } + export(&lowered); + eprintln!("BS-52: passed in {:?}", started.elapsed()); +} diff --git a/crates/conformance/src/cases/repository.rs b/crates/conformance/src/cases/repository.rs index b08d25beb8..aa4b828b91 100644 --- a/crates/conformance/src/cases/repository.rs +++ b/crates/conformance/src/cases/repository.rs @@ -898,7 +898,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(), 496, "§31 has 496 rows"); + assert_eq!(table.len(), 497, "§31 has 497 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 c83090918d..5f946736e1 100644 --- a/crates/conformance/tests/conformance.rs +++ b/crates/conformance/tests/conformance.rs @@ -2410,6 +2410,11 @@ fn conformance_bs_34() { repo_conformance::cases::run("BS-34"); } +#[test] +fn conformance_bs_52() { + repo_conformance::cases::run("BS-52"); +} + #[test] fn conformance_pk_01() { repo_conformance::cases::run("PK-01"); diff --git a/features/suites/bootstrap.feature b/features/suites/bootstrap.feature index 13e468aa52..3b4b9d05c9 100644 --- a/features/suites/bootstrap.feature +++ b/features/suites/bootstrap.feature @@ -208,3 +208,10 @@ Feature: bootstrap When the project is checked and built, and the lowered program and the rendered package of the root are run on those manifests Then the root is eligible for rust-std with exactly its declared effects, no certificate part is beyond 3,145,728 bytes, the root equals the file bytes of `BuildManifest::to_json` on every run, and each committed manifest is the oracle's text of its own fields And every planted defect is detected by its own vector, and the exported packages are byte-identical to the committed packages and realize the programs the differential ran + + @BS-52 @build + Scenario: The conservative checker of LexLean Semantic Expressions of SPEC.md §13.6 to §13.8 and §14.4 (`lexicon::lse::check_signature`: a signature is a type, its applications respect the explicit arities of the signatures they reach, and its natural literals take the types an explicit binder or a solved metavariable gives them; and `check_value`: a defined value is well-typed under the same rules and, with a signature, unifies with it, binder by binder and mode by mode; each the zonked type and the type of every literal in occurrence order, or the message of the first error, with the oracle's numbering of metavariables and its capture-avoiding renames, and with what the oracle's lookup answers for a constant given as a table of rows and the answer for a constant no row names) is language-1.2 source in the `bootstrap/` project, as two production roots of `LseTypes` over the expressions of `BS-25`: `checkSignature` and `checkValue`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, the last structural over a bound on the checker's nesting the host passes, and each with no part of its certificate beyond 3,145,728 bytes; their rendered `rust-std` packages under the pinned rustc, and on the normative host their lowered programs under the calculus evaluator, agree with the oracle to the byte on hand-built expressions over a hand-built glossary, which reach every error of the checker, opaque and defined constants, partial application, and both renames, on every signature and defined value of the committed lexicon packages checked against its own package as a package loads and against the builtin packages as a closure does, and on seeded expressions; each planted defect in the source is detected by the one vector it names, and the certified packages the production export publishes for the project are the committed ones; building the lookup from a package or a closure is outside the claim. + Given the committed `bootstrap` project, hand-built expressions over a hand-built glossary, the signatures and defined values of the committed lexicon packages, and seeded expressions + When the project is checked and built, and the lowered programs and the rendered packages of the two roots are run on those expressions + Then each root is eligible for rust-std with exactly its declared effects, no certificate part is beyond 3,145,728 bytes, and each root equals `check_signature` or `check_value` on every run + And every planted defect is detected by its own vector, and the exported packages are byte-identical to the committed packages and realize the programs the differential ran diff --git a/model/ids.toml b/model/ids.toml index 81d8d603d1..f022b79b80 100644 --- a/model/ids.toml +++ b/model/ids.toml @@ -2894,6 +2894,12 @@ level = "build" suite = "bootstrap" statement = "The build manifest of SPEC.md §21.6 in the canonical JSON of §21.7 (`artifact::manifest::BuildManifest::to_json` written as a file: the specification, the compiler version and semantics ID, the language, the project, the source, semantic, and build IDs, the pinned Lean toolchain, the selection and the module rows in their order, a module's origin only when it has one, and the input and output rows sorted by kind, path, length, and digest, each digest in lower-case hex) is language-1.2 source in the `bootstrap/` project, as the production root `buildManifest` of `ManifestText` over the standard library's `Json` writer, eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, and with no part of its certificate beyond 3,145,728 bytes; its rendered `rust-std` package under the pinned rustc, and on the normative host its lowered program under the calculus evaluator, agree with the oracle to the byte on hand-built manifests, which reach every key of the row order, lengths across a byte, a module's origin, and escapes, on every committed build manifest, each of which is the oracle's text of its own fields, and on seeded manifests; each planted defect in the source is detected by the one vector it names, and the certified packages the production export publishes for the project are the committed ones; gathering the rows of a build and hashing its files are outside the claim." +[[id]] +id = "BS-52" +level = "build" +suite = "bootstrap" +statement = "The conservative checker of LexLean Semantic Expressions of SPEC.md §13.6 to §13.8 and §14.4 (`lexicon::lse::check_signature`: a signature is a type, its applications respect the explicit arities of the signatures they reach, and its natural literals take the types an explicit binder or a solved metavariable gives them; and `check_value`: a defined value is well-typed under the same rules and, with a signature, unifies with it, binder by binder and mode by mode; each the zonked type and the type of every literal in occurrence order, or the message of the first error, with the oracle's numbering of metavariables and its capture-avoiding renames, and with what the oracle's lookup answers for a constant given as a table of rows and the answer for a constant no row names) is language-1.2 source in the `bootstrap/` project, as two production roots of `LseTypes` over the expressions of `BS-25`: `checkSignature` and `checkValue`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, the last structural over a bound on the checker's nesting the host passes, and each with no part of its certificate beyond 3,145,728 bytes; their rendered `rust-std` packages under the pinned rustc, and on the normative host their lowered programs under the calculus evaluator, agree with the oracle to the byte on hand-built expressions over a hand-built glossary, which reach every error of the checker, opaque and defined constants, partial application, and both renames, on every signature and defined value of the committed lexicon packages checked against its own package as a package loads and against the builtin packages as a closure does, and on seeded expressions; each planted defect in the source is detected by the one vector it names, and the certified packages the production export publishes for the project are the committed ones; building the lookup from a package or a closure is outside the claim." + [[id]] id = "PK-01" level = "build" diff --git a/tests/production-export/bootstrap/roots.json b/tests/production-export/bootstrap/roots.json index 32674a57bb..c52b9f017c 100644 --- a/tests/production-export/bootstrap/roots.json +++ b/tests/production-export/bootstrap/roots.json @@ -495,6 +495,22 @@ "rust-std" ], "version": "1.0.0" + }, + { + "name": "check_signature", + "root": "LexLeanBoot.LseTypes.checkSignature", + "targets": [ + "rust-std" + ], + "version": "1.0.0" + }, + { + "name": "check_value", + "root": "LexLeanBoot.LseTypes.checkValue", + "targets": [ + "rust-std" + ], + "version": "1.0.0" } ] } diff --git a/xtask/src/spec_links.rs b/xtask/src/spec_links.rs index f97eb8c07c..8bc0b72d97 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() != 496 { - return Err(format!("RP-07: the §31 table has {} rows, not 496", rows.len()).into()); + if rows.len() != 497 { + return Err(format!("RP-497: the §497 table has {} rows, not 497", 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 0a3d95f357f97842e2c28fc5c799195a0f8bdf79 Mon Sep 17 00:00:00 2001 From: humuhumu33 Date: Fri, 13 Nov 2026 00:15:14 +0000 Subject: [PATCH 2/3] BS-52: declare the axioms Lean observes, and read pair flags through definitions Regeneration run 43316854893 stopped on LLV7005: 21 definitions that reach the standard library's numerals observe Quot.sound and propext; they now declare them. Conditions on a component of a pair go through flagOf and typedCk, the shape BS-50's certificate needed. Co-Authored-By: Claude Opus 5.5 --- bootstrap/src/LseTypes.lex.tex | 43 +++++- .../src/cases/bootstrap/lse_types.rs | 143 ++++-------------- 2 files changed, 70 insertions(+), 116 deletions(-) diff --git a/bootstrap/src/LseTypes.lex.tex b/bootstrap/src/LseTypes.lex.tex index 7df17c803c..547c6b59af 100755 --- a/bootstrap/src/LseTypes.lex.tex +++ b/bootstrap/src/LseTypes.lex.tex @@ -377,6 +377,7 @@ | LexiconLse::Lse.LConst p e us => (ctor Option.some [LexiconLse::Lse] (ctor LexiconLse::Lse.LConst p e us)) | LexiconLse::Lse.LNat digits => (ctor Option.some [LexiconLse::Lse] (ctor LexiconLse::Lse.LNat digits)) | LexiconLse::Lse.LNil => (ctor Option.some [LexiconLse::Lse] (ctor LexiconLse::Lse.LNil)))), + axioms: [Quot.sound, propext], executable: true, recursive_argument: fuel } @@ -388,6 +389,7 @@ body: (match (zonk fuel (project st metas) expr) | Option.some done => (record Sr {out := done, ck := st}) | Option.none => (record Sr {out := expr, ck := (ckExhaust st)})), + axioms: [Quot.sound, propext], executable: true } @@ -459,6 +461,14 @@ recursive_argument: fuel } +definition{ + name: flagOf, + parameters: [flagged : (Bool * Ck)], + result: Bool, + body: (first flagged), + executable: true +} + definition{ name: hasUnsolved, parameters: [fuel : Fuel, st : Ck, expr : LexiconLse::Lse], @@ -766,6 +776,7 @@ | Option.none => (match (metaOf a) | Option.some m => (urOk (ckSolve st2 m e)) | Option.none => (unifyRigid f st2 e a))))))))), + axioms: [Quot.sound, propext], executable: true, mutual: Unify, recursive_argument: fuel @@ -779,7 +790,8 @@ | Fuel.FZ => (urOk (ckExhaust st)) | Fuel.FS f => (let cls : Nat := (rigidClass f e a); (if (cls =? 0) then (urOk st) else (if (cls =? 1) then (if (if (mayUnfold f st e) then true else (mayUnfold f st a)) then (urOk st) else (let pe : (LexiconLse::Lse * LexiconLse::Lse) := (appParts e); (let pa : (LexiconLse::Lse * LexiconLse::Lse) := (appParts a); (let head : Ur := (unify f st (first pe) (first pa)); (match (project head error) | Option.some message => head - | Option.none => (unifyArgs f (project head ck) (second pe) (second pa))))))) else (if (cls =? 2) then (unifyBinding f st e a) else (if (if (mayUnfold f st e) then true else (mayUnfold f st a)) then (urOk st) else (let ue : (Bool * Ck) := (hasUnsolved f st e); (if (first ue) then (urOk (second ue)) else (let ua : (Bool * Ck) := (hasUnsolved f (second ue) a); (if (first ua) then (urOk (second ua)) else (let txtE : Bytes := (shown e); (let txtA : Bytes := (shown a); (urFail (second ua) (cat (cat (cat (lit "expected ") txtE) (lit ", found ")) txtA)))))))))))))), + | Option.none => (unifyArgs f (project head ck) (second pe) (second pa))))))) else (if (cls =? 2) then (unifyBinding f st e a) else (if (if (mayUnfold f st e) then true else (mayUnfold f st a)) then (urOk st) else (let ue : (Bool * Ck) := (hasUnsolved f st e); (if (flagOf ue) then (urOk (second ue)) else (let ua : (Bool * Ck) := (hasUnsolved f (second ue) a); (if (flagOf ua) then (urOk (second ua)) else (let txtE : Bytes := (shown e); (let txtA : Bytes := (shown a); (urFail (second ua) (cat (cat (cat (lit "expected ") txtE) (lit ", found ")) txtA)))))))))))))), + axioms: [Quot.sound, propext], executable: true, mutual: Unify, recursive_argument: fuel @@ -818,6 +830,7 @@ | LexiconLse::Lse.LLet n t v body => (urOk st) | LexiconLse::Lse.LNat digits => (urOk st) | LexiconLse::Lse.LBinder m n t r => (urOk st))), + axioms: [Quot.sound, propext], executable: true, mutual: Unify, recursive_argument: fuel @@ -830,6 +843,7 @@ body: (match fuel | Fuel.FZ => (urOk (ckExhaust st)) | Fuel.FS f => (let pi : Bool := ((kindOf e) =? 5); (let fe : Fl := (flattenInto f e pi); (let fa : Fl := (flattenInto f a pi); (let ne : Nat := (prim length (project fe binders) : Nat); (let na : Nat := (prim length (project fa binders) : Nat); (if (ne =? na) then (unifyPiLoop f st (project fe binders) (project fa binders) (project fa body) (project fe body) e a) else (let txtE : Bytes := (shown e); (let txtA : Bytes := (shown a); (urFail st (cat (cat (cat (cat (cat (cat txtE (lit " binds ")) (LexLeanStd.Numeral::decimal ne)) (lit " arguments but ")) txtA) (lit " binds ")) (LexLeanStd.Numeral::decimal na)))))))))))), + axioms: [Quot.sound, propext], executable: true, mutual: Unify, recursive_argument: fuel @@ -857,6 +871,7 @@ | List.cons ba restA => (if ((project be mode) =? (project ba mode)) then (let once : Ur := (unify f st (project be ty) (project ba ty)); (match (project once error) | Option.some message => once | Option.none => (let renamed : LexiconLse::Lse := (ctor LexiconLse::Lse.LLocal (project be name)); (let laterTyped : Ls := (substTypes f (project once ck) restA (project ba name) renamed); (let bodyRenamed : Sr := (subst f (project laterTyped ck) actualBody (project ba name) renamed); (let next : Ur := (unifyPiLoop f (project bodyRenamed ck) restE (project laterTyped items) (project bodyRenamed out) expectedBody e a); (record Ur {error := (project next error), ck := (project next ck)}))))))) else (let txtE : Bytes := (shown e); (let txtA : Bytes := (shown a); (urFail st (cat (cat (cat (cat (cat (cat (cat (cat (cat (cat (cat (lit "binder `") (project be name)) (lit "` is ")) (modeName (project be mode))) (lit " in ")) txtE) (lit " but `")) (project ba name)) (lit "` is ")) (modeName (project ba mode))) (lit " in ")) txtA)))))))), + axioms: [Quot.sound, propext], executable: true, mutual: Unify, recursive_argument: fuel @@ -911,6 +926,7 @@ body: (let zonked : Sr := (zonkSt fuel st ty); (let z : LexiconLse::Lse := (project zonked out); (let st2 : Ck := (project zonked ck); (let ke : Nat := (kindOf expr); (if (if (ke =? 8) then true else (ke =? 6)) then (let txtExpr : Bytes := (shown expr); (irFail st2 (cat txtExpr (lit " is a term, not a type")))) else (if ((kindOf z) (irOk z st2) | Option.none => (if (mayUnfold fuel st2 z) then (irOk z st2) else (let txtExpr : Bytes := (shown expr); (let txtZ : Bytes := (shown z); (irFail st2 (cat (cat (cat txtExpr (lit " is not a type: its type is ")) txtZ) 0x)))))))))))), + axioms: [Quot.sound, propext], executable: true } @@ -948,6 +964,7 @@ | LexiconLse::Lse.LNil => (irOk (ctor LexiconLse::Lse.LSortProp) st) | LexiconLse::Lse.LArg h t => (irOk (ctor LexiconLse::Lse.LSortProp) st) | LexiconLse::Lse.LBinder m n t r => (irOk (ctor LexiconLse::Lse.LSortProp) st))), + axioms: [Quot.sound, propext], executable: true, mutual: Infer, recursive_argument: fuel @@ -984,6 +1001,7 @@ | LexiconLse::Lse.LLet n t v inner => (expectType f st body scope) | LexiconLse::Lse.LNat digits => (expectType f st body scope) | LexiconLse::Lse.LArg h t => (expectType f st body scope))), + axioms: [Quot.sound, propext], executable: true, mutual: Infer, recursive_argument: fuel @@ -1003,6 +1021,7 @@ | Fuel.FS f => (let inferred : Ir := (infer f st expr scope); (match (project inferred error) | Option.some message => inferred | Option.none => (requireSort f (project inferred ck) expr (project inferred ty))))), + axioms: [Quot.sound, propext], executable: true, mutual: Infer, recursive_argument: fuel @@ -1024,6 +1043,7 @@ | List.cons argument more => (let seen : Ir := (infer f st argument scope); (match (project seen error) | Option.some message => seen | Option.none => (let next : Ir := (inferRest f (project seen ck) more scope); (record Ir {ty := (project next ty), error := (project next error), ck := (project next ck)})))))), + axioms: [Quot.sound, propext], executable: true, mutual: Infer, recursive_argument: fuel @@ -1060,6 +1080,7 @@ | LexiconLse::Lse.LNil => (inferStuck f st2 expr g remaining total supplied z scope) | LexiconLse::Lse.LArg h t => (inferStuck f st2 expr g remaining total supplied z scope) | LexiconLse::Lse.LBinder m n t r => (inferStuck f st2 expr g remaining total supplied z scope))))))), + axioms: [Quot.sound, propext], executable: true, mutual: Infer, recursive_argument: fuel @@ -1084,6 +1105,7 @@ | Fuel.FS f => (match (metaOf z) | Option.some m => (inferRest f st remaining scope) | Option.none => (if (mayUnfold f st z) then (inferRest f st remaining scope) else (let txtExpr : Bytes := (shown expr); (let txtG : Bytes := (shown g); (let txtZ : Bytes := (shown z); (irFail st (cat (cat (cat (cat (cat (cat (cat (cat (cat txtExpr (lit " supplies ")) (LexLeanStd.Numeral::decimal total)) (lit " explicit argument")) (plural total)) (lit " but ")) txtG) (lit " accepts ")) (LexLeanStd.Numeral::decimal supplied)) (cat (cat (lit ": after them its type is ") txtZ) (lit ", not a function")))))))))), + axioms: [Quot.sound, propext], executable: true, mutual: Infer, recursive_argument: fuel @@ -1115,6 +1137,7 @@ | Option.none => (let agreed : Ur := (unify f (project actual ck) (project binder ty) (project actual ty)); (match (project agreed error) | Option.some message => (irFail (project agreed ck) message) | Option.none => (inferInstantiate f (project agreed ck) expr g binder argument rest body more total (supplied + 1) scope)))))) else (let made : Sr := (freshMeta st); (inferInstantiate f (project made ck) expr g binder (project made out) rest body remaining total supplied scope))))), + axioms: [Quot.sound, propext], executable: true, mutual: Infer, recursive_argument: fuel @@ -1140,6 +1163,7 @@ body: (match fuel | Fuel.FZ => (irFail (ckExhaust st) 0x) | Fuel.FS f => (let later : Ls := (substTypes f st rest (project binder name) value); (let newBody : Sr := (subst f (project later ck) body (project binder name) value); (inferBinders f (project newBody ck) expr g (project later items) (project newBody out) remaining total supplied scope)))), + axioms: [Quot.sound, propext], executable: true, mutual: Infer, recursive_argument: fuel @@ -1153,16 +1177,26 @@ | Fuel.FZ => ((nil (Option LexiconLse::Lse)), (ckExhaust st)) | Fuel.FS f => (match metas | List.nil => ((nil (Option LexiconLse::Lse)), st) - | List.cons metaName more => (let solved : Sr := (zonkSt f st (ctor LexiconLse::Lse.LLocal metaName)); (let pending : (Bool * Ck) := (hasUnsolved f (project solved ck) (project solved out)); (let others : ((List (Option LexiconLse::Lse)) * Ck) := (literalTypes f (second pending) more); ((cons (if (first pending) then (ctor Option.none [LexiconLse::Lse]) else (ctor Option.some [LexiconLse::Lse] (project solved out))) (first others)), (second others))))))), + | List.cons metaName more => (let solved : Sr := (zonkSt f st (ctor LexiconLse::Lse.LLocal metaName)); (let pending : (Bool * Ck) := (hasUnsolved f (project solved ck) (project solved out)); (let others : ((List (Option LexiconLse::Lse)) * Ck) := (literalTypes f (second pending) more); ((cons (if (flagOf pending) then (ctor Option.none [LexiconLse::Lse]) else (ctor Option.some [LexiconLse::Lse] (project solved out))) (first others)), (second others))))))), + axioms: [Quot.sound, propext], executable: true, recursive_argument: fuel } +definition{ + name: typedCk, + parameters: [typed : ((List (Option LexiconLse::Lse)) * Ck)], + result: Ck, + body: (second typed), + executable: true +} + definition{ name: finish, parameters: [fuel : Fuel, st : Ck, ty : LexiconLse::Lse], result: (Result Checked Bytes), - body: (let zonked : Sr := (zonkSt fuel st ty); (let typed : ((List (Option LexiconLse::Lse)) * Ck) := (literalTypes fuel (project zonked ck) (LexLeanStd.Order::reverseList [Bytes] (project st literals))); (if (project (second typed) exhausted) then (ctor Result.error [Checked, Bytes] (lit "the checker's bound on nesting is exhausted")) else (ctor Result.ok [Checked, Bytes] (record Checked {ty := (project zonked out), literals := (first typed)}))))), + body: (let zonked : Sr := (zonkSt fuel st ty); (let typed : ((List (Option LexiconLse::Lse)) * Ck) := (literalTypes fuel (project zonked ck) (LexLeanStd.Order::reverseList [Bytes] (project st literals))); (if (project (typedCk typed) exhausted) then (ctor Result.error [Checked, Bytes] (lit "the checker's bound on nesting is exhausted")) else (ctor Result.ok [Checked, Bytes] (record Checked {ty := (project zonked out), literals := (first typed)}))))), + axioms: [Quot.sound, propext], executable: true } @@ -1181,6 +1215,7 @@ body: (if (project (project result ck) exhausted) then (ctor Result.error [Checked, Bytes] (lit "the checker's bound on nesting is exhausted")) else (match (project result error) | Option.some message => (ctor Result.error [Checked, Bytes] message) | Option.none => (finish fuel (project result ck) (project result ty)))), + axioms: [Quot.sound, propext], executable: true } @@ -1189,6 +1224,7 @@ parameters: [signature : LexiconLse::Lse, rows : (List CRow), fallback : Nat, depth : Nat], result: (Result Checked Bytes), body: (let fuel : Fuel := (mkFuel depth); (let typed : Ir := (expectType fuel (start rows fallback) signature (nil (Bytes * LexiconLse::Lse))); (outcome fuel typed))), + axioms: [Quot.sound, propext], executable: true, production: {effects: [allocation, overflow, recursion], targets: [rust-std]} } @@ -1210,6 +1246,7 @@ | Option.none => false)) then (outcome fuel inferred) else (let agreed : Ur := (unify fuel (project inferred ck) sig (project inferred ty)); (match (project agreed error) | Option.some message => (let txtValue : Bytes := (shown value); (let txtSig : Bytes := (shown sig); (outcome fuel (irFail (project agreed ck) (cat (cat (cat (cat (cat (lit "defined value ") txtValue) (lit " does not match the signature ")) txtSig) (lit ": ")) message))))) | Option.none => (outcome fuel (irOk (project inferred ty) (project agreed ck))))))))), + axioms: [Quot.sound, propext], executable: true, production: {effects: [allocation, overflow, recursion], targets: [rust-std]} } diff --git a/crates/conformance/src/cases/bootstrap/lse_types.rs b/crates/conformance/src/cases/bootstrap/lse_types.rs index 53dbe84086..249f7d2b82 100755 --- a/crates/conformance/src/cases/bootstrap/lse_types.rs +++ b/crates/conformance/src/cases/bootstrap/lse_types.rs @@ -82,10 +82,7 @@ impl Table { /// constants they reference and, through the signatures of those, every /// constant reachable from them. No other row is ever looked up. fn reachable(&self, roots: &[&Lse]) -> Vec<&Row> { - let mut wanted: Vec = roots - .iter() - .flat_map(|root| root.referenced_consts()) - .collect(); + let mut wanted: Vec = roots.iter().flat_map(|root| root.referenced_consts()).collect(); let mut seen: BTreeSet = BTreeSet::new(); let mut out = Vec::new(); while let Some(id) = wanted.pop() { @@ -156,8 +153,7 @@ fn signature_run(signature: &Lse, table: &Table) -> Run { let (rows, fallback) = table.value(&[signature]); ( vec![enc_lse(signature), rows, fallback, nat(NESTING)], - serde_json::to_value(outcome(check_signature(signature, &lookup))) - .expect("a value is JSON"), + serde_json::to_value(outcome(check_signature(signature, &lookup))).expect("a value is JSON"), ) } @@ -233,101 +229,29 @@ fn named() -> Vec { let signatures: &[(&'static str, &str, bool)] = &[ ("a sort", "(sort prop)", false), ("a constant type", "(const t::nat)", false), - ( - "a pi of types", - "(pi ((explicit a (const t::nat))) (const t::nat))", - false, - ), + ("a pi of types", "(pi ((explicit a (const t::nat))) (const t::nat))", false), ("an unresolved constant", "(const t::missing)", false), ("a constant without a signature", "(const t::word)", false), ("a term is no type", "(const t::zero)", false), ("a natural literal is a term", "(nat 3)", false), - ( - "a lam is a term", - "(lam ((explicit x (sort prop))) (local x))", - false, - ), + ("a lam is a term", "(lam ((explicit x (sort prop))) (local x))", false), ("a local out of scope", "(local x)", false), - ( - "an application of a sort", - "(app (sort prop) (const t::zero))", - false, - ), - ( - "an application of a literal", - "(app (nat 1) (const t::zero))", - false, - ), - ( - "two arguments too many", - "(app (const t::succ) (const t::zero) (const t::zero))", - false, - ), - ( - "one argument too many", - "(app (const t::zero) (const t::zero))", - false, - ), - ( - "an argument of the wrong type", - "(app (const t::succ) (sort prop))", - false, - ), - ( - "a partial application", - "(app (const t::add) (const t::zero))", - false, - ), - ( - "an equation is a proposition", - "(app (const t::eq) (const t::zero) (const t::zero))", - false, - ), - ( - "a literal with an inferred type", - "(app (const t::eq) (const t::zero) (nat 1))", - false, - ), - ( - "literals of an undetermined type", - "(app (const t::eq) (nat 1) (nat 2))", - false, - ), - ( - "an opaque head", - "(app (const other::f) (const t::zero))", - true, - ), - ( - "a defined result type unfolds", - "(app (const t::mk) (const t::zero) (const t::zero))", - false, - ), - ( - "a let", - "(let x (sort prop) (const t::prop) (local x))", - false, - ), - ( - "a let whose value disagrees", - "(let x (const t::nat) (sort prop) (local x))", - false, - ), - ( - "a binder that is no type", - "(pi ((explicit a (const t::zero))) (sort prop))", - false, - ), - ( - "a body that is no type", - "(pi ((explicit a (const t::nat))) (local a))", - false, - ), - ( - "a type of a list", - "(app (const t::list) (const t::nat))", - false, - ), + ("an application of a sort", "(app (sort prop) (const t::zero))", false), + ("an application of a literal", "(app (nat 1) (const t::zero))", false), + ("two arguments too many", "(app (const t::succ) (const t::zero) (const t::zero))", false), + ("one argument too many", "(app (const t::zero) (const t::zero))", false), + ("an argument of the wrong type", "(app (const t::succ) (sort prop))", false), + ("a partial application", "(app (const t::add) (const t::zero))", false), + ("an equation is a proposition", "(app (const t::eq) (const t::zero) (const t::zero))", false), + ("a literal with an inferred type", "(app (const t::eq) (const t::zero) (nat 1))", false), + ("literals of an undetermined type", "(app (const t::eq) (nat 1) (nat 2))", false), + ("an opaque head", "(app (const other::f) (const t::zero))", true), + ("a defined result type unfolds", "(app (const t::mk) (const t::zero) (const t::zero))", false), + ("a let", "(let x (sort prop) (const t::prop) (local x))", false), + ("a let whose value disagrees", "(let x (const t::nat) (sort prop) (local x))", false), + ("a binder that is no type", "(pi ((explicit a (const t::zero))) (sort prop))", false), + ("a body that is no type", "(pi ((explicit a (const t::nat))) (local a))", false), + ("a type of a list", "(app (const t::list) (const t::nat))", false), ("a sort of a universe", "(sort (type (max u 1)))", false), ]; for &(label, source, opaque) in signatures { @@ -406,11 +330,11 @@ fn committed_packages() -> Vec { let Ok(manifest) = std::fs::read_to_string(entry.path()) else { continue; }; - let Some(language) = manifest.parse::().ok().and_then(|table| { - table - .get("language") - .and_then(|v| v.as_str().map(str::to_owned)) - }) else { + let Some(language) = manifest + .parse::() + .ok() + .and_then(|table| table.get("language").and_then(|v| v.as_str().map(str::to_owned))) + else { continue; }; let mut files: Vec<(String, Vec)> = walkdir::WalkDir::new(directory) @@ -425,11 +349,7 @@ fn committed_packages() -> Vec { .to_string_lossy() .replace('\\', "/"); (relative == "lexicon.toml" || relative.starts_with("entries/")) - .then(|| { - std::fs::read(file.path()) - .ok() - .map(|bytes| (relative, bytes)) - }) + .then(|| std::fs::read(file.path()).ok().map(|bytes| (relative, bytes))) .flatten() }) .collect(); @@ -527,9 +447,9 @@ fn seeded_runs(count: usize) -> (Vec, Vec) { signatures.push(signature_run(&expression, table)); let against = if seeded.chance(70) { { - let depth = 1 + seeded.below(3); - Some(drawn_lse(&mut seeded, depth)) - } + let depth = 1 + seeded.below(3); + Some(drawn_lse(&mut seeded, depth)) + } } else { None }; @@ -830,10 +750,7 @@ pub(super) fn case() { for vector in &vectors { by_root[vector.root].push(vector.run.clone()); } - let outputs: Vec = vectors - .iter() - .map(|vector| vector.run.1.to_string()) - .collect(); + let outputs: Vec = vectors.iter().map(|vector| vector.run.1.to_string()).collect(); for refusal in REFUSALS { let needle = super::hex(refusal.as_bytes()); assert!( From abb614a83d3208329ab21060c7270cc0363c94be Mon Sep 17 00:00:00 2001 From: humuhumu33 Date: Fri, 13 Nov 2026 17:55:48 +0000 Subject: [PATCH 3/3] Relock bootstrap and refresh model documents after the release merge Co-Authored-By: Claude Sonnet 5 --- bootstrap/lexlean.lock | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 bootstrap/lexlean.lock diff --git a/bootstrap/lexlean.lock b/bootstrap/lexlean.lock new file mode 100644 index 0000000000..3b2d9e8af2 --- /dev/null +++ b/bootstrap/lexlean.lock @@ -0,0 +1,57 @@ +spec = "lexlean/lock/2" +language = "1.2" +compiler_semantics = "a82990b56aa082f57e3bf3e2d6494d75a8170f26e269448de19110f5459655ad" +project_config_sha256 = "3a93797a479987bfb7a953acec2c92e7a06e78416ab2c15367e248cd4ed115cf" +lean_toolchain = "leanprover/lean4:v4.32.1" + +[[workspace_file]] +path = "lake-manifest.json" +sha256 = "736b7fb038a81af813e3e29b7c1ea7575ceefb442c8fb2e4df805d4bd87010b6" + +[[workspace_file]] +path = "lakefile.toml" +sha256 = "4faafd773dfa00e427b139318e863070b818aa6e229ce493b24ec906b60fa5df" + +[[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"] + +[[source_package]] +id = "std" +version = "1.0.0" +kind = "path" +source = "stdlib" +module_prefix = "LexLeanStd" +tree_sha256 = "316ac824e54957e79f4bdd5ed1f3228b7c5a817333134bddff9552d254434a08" +manifest_sha256 = "1dbb7c6933b8f2a4a9d4af7746eefcb077ea1b339d9b88a63eadf64b6a348f4f" +imports = []