From d0969f4d9c7f00965c6dbccfd372ac76f3fb3cef Mon Sep 17 00:00:00 2001 From: Alex Flom Date: Mon, 9 Nov 2026 19:05:22 -0700 Subject: [PATCH 01/22] test(bootstrap): avoid discarded BS-15 full-library runs Assemble the same ordered evaluator and native corpora directly. Keep the existing full accepted-package recipe assertions, bounded native inputs, all plants, counts and exported-package checks. Add an old/new assembly regression without changing compiler semantics or gate orchestration. Refs #457 --- VERIFICATION.md | 33 ++++ .../src/cases/bootstrap/rust_package.rs | 180 ++++++++++++++---- 2 files changed, 174 insertions(+), 39 deletions(-) diff --git a/VERIFICATION.md b/VERIFICATION.md index 7412633b08..c0e712e2e5 100644 --- a/VERIFICATION.md +++ b/VERIFICATION.md @@ -43,6 +43,38 @@ Outside `vv`: Each gate below was made to fail by planting a defect, running the gate's command, recording the failure, and removing the defect. The observed lines are verbatim gate output (paths abbreviated to the repository root). `cargo xtask release-check` requires a `### can fail` record for every gate and audit named in `repo_model::release::GATES`. +### BS-15 render corpus materialization + +Issue #457 changes only the construction of the two render vectors in +`crates/conformance/src/cases/bootstrap/rust_package.rs`. The previous code +encoded every full-library render, filtered the evaluator vector, then +replaced the large native inputs with their existing 4,096-byte prefixes. +The discarded full runs were inputs to neither differential. The direct +assembler retains the same order: accepted manifests, shaped inputs, then +seeded render mutants. At or below 16,384 bytes it uses the unchanged run and +expected files in both vectors; above that boundary it constructs only the +same native prefix and recipe outcome. The complete-library recipe assertion +against `package::package` in `accepted` is unchanged. + +No corpus generation, seed, count, negative fixture, planted defect, native +comparison, certificate limit, export check, host condition, timeout, or +compiler-semantics input changes. On the base tree's asserted counts, the +native render vector still contains 735 runs and the evaluator vector 241; +494 full-library argument encodings that neither vector consumed are no +longer materialized. This is a work-count deduction, not a measured CI speedup. + +The library unit regression `render_corpora_preserve_inputs_and_outcomes` +compares both vectors with the previous assembler on both profiles, distinct +supplied package outcomes, recipe outcomes, and libraries of 0, 4,096, +16,383, 16,384, 16,385 and 32,768 bytes. Local validation in the authoring +environment passed whitespace checks and an independent assembly model: +24 evaluator and 36 native results were identical in order and bytes across +27 empty/mixed collection cases, and changes to the boundary, prefix and +supplied-outcome routing were detected. That model does not execute Rust or +the package renderer. Rust formatting, Clippy, the library regression and the +complete release gate were not run locally because the environment has no +Rust toolchain; they remain required acceptance evidence on the final tree. + ### Intel host partition custody and complete evidence The three non-Intel auxiliary host legs keep their existing locked, @@ -13607,3 +13639,4 @@ Under separate root admission ca0818e03f221445d77bfab595b64e255f1eb5d0f5cb74df61 The reviewed physical ledger contains 12,551 entries and 10,900 present files, including 8 symlinks. Physical review393 remains detached at c613 with raw index f2a12ce1d4f383096de2ab9913efd25c55a5a3d01913a670429252ea4ee961eb and logical 0328 source tree; the external pre-documentation capture is 6dcf464632e277916cedfacb5656699c89872c47. Current frozen BOTH/probe artifacts, 905 inputs, closed errors, archived selected build metadata, helper ownership and all producer outputs passed entry and terminal preservation guards. The final additive VI is prepared externally, without changing that physical root. This is source/product readiness evidence, not complete combined-head acceptance. The historical281 draft run 41964177567 ran only plan/honesty/rest with seven required host/Atlas groups skipped; repro 41964177626 passed, and the prior ready run 41964083701 was cancelled. None accepts the new currentbase combination. One complete new-head hosted gate, same-run honesty, reproducibility, final review and eventual actual landing/tree equality remain mandatory. Existing d34/WSL PERFORMANCE rows and timing/host/comparison caveats retain their original scope; no new linearity measurement is claimed. Closure scope remains #373 only after its combined proof/BS02 and final gate requirements pass. + diff --git a/crates/conformance/src/cases/bootstrap/rust_package.rs b/crates/conformance/src/cases/bootstrap/rust_package.rs index b10d186835..ae64cebf46 100644 --- a/crates/conformance/src/cases/bootstrap/rust_package.rs +++ b/crates/conformance/src/cases/bootstrap/rust_package.rs @@ -311,6 +311,37 @@ fn render_run(render: &Render, expected: Files) -> Run { ) } +/// The ordered render corpora consumed by the evaluator and rustc. +fn render_corpora( + renders: &[(Render, Files)], + shaped: &[Render], + mutants: &[Render], +) -> (Vec, Vec) { + let mut within = Vec::new(); + let mut compiled = Vec::new(); + let inputs = renders + .iter() + .map(|(render, files)| (render, Some(files))) + .chain(shaped.iter().chain(mutants).map(|render| (render, None))); + for (render, files) in inputs { + if render.library.len() <= EVALUATOR_LIBRARY { + let expected = files.cloned().unwrap_or_else(|| files_of(render)); + let run = render_run(render, expected); + within.push(run.clone()); + compiled.push(run); + } else { + // rustc expands each argument byte into an array element, so + // the native corpus uses the existing 4,096-byte prefix. The + // full-library run feeds neither consumer; accepted() still + // holds its complete library to the package::package recipe. + let mut render = render.clone(); + render.library.truncate(4_096); + compiled.push(render_run(&render, files_of(&render))); + } + } + (within, compiled) +} + /// A manifest the oracle accepts, what `renderPackage` is given for it, and /// the two files `package::package` wrote. fn accepted(manifest: &Manifest) -> Option<(Render, Files)> { @@ -866,45 +897,7 @@ pub(super) fn case() { } render_mutants.push(render); } - let render_runs: Vec = renders - .iter() - .map(|(render, files)| render_run(render, files.clone())) - .chain( - shaped - .iter() - .chain(&render_mutants) - .map(|render| render_run(render, files_of(render))), - ) - .collect(); - let within: Vec = renders - .iter() - .map(|(render, _)| render) - .chain(&shaped) - .chain(&render_mutants) - .zip(&render_runs) - .filter(|(render, _)| render.library.len() <= EVALUATOR_LIBRARY) - .map(|(_, run)| run.clone()) - .collect(); - // rustc compiles each byte of an argument as one array element, and the - // libraries of the committed packages are megabytes together, so a render - // whose library is beyond the evaluator's bound runs under rustc on the - // first 4,096 bytes of it, held to the recipe, which reads it only as - // bytes to digest. - let compiled: Vec = renders - .iter() - .map(|(render, _)| render) - .chain(&shaped) - .chain(&render_mutants) - .zip(&render_runs) - .map(|(render, run)| { - if render.library.len() <= EVALUATOR_LIBRARY { - return run.clone(); - } - let mut render = render.clone(); - render.library.truncate(4_096); - render_run(&render, files_of(&render)) - }) - .collect(); + let (within, compiled) = render_corpora(&renders, &shaped, &render_mutants); eprintln!( "BS-15: {} manifests ({} accepted), {} and {} rule inputs, {} and {} mutants, {} renders under the evaluator ({:?})", manifests.len(), @@ -1034,3 +1027,112 @@ pub(super) fn case() { } eprintln!("BS-15: passed in {:?}", started.elapsed()); } + +#[cfg(test)] +mod tests { + use super::*; + + fn previous_corpora( + renders: &[(Render, Files)], + shaped: &[Render], + render_mutants: &[Render], + ) -> (Vec, Vec) { + let render_runs: Vec = renders + .iter() + .map(|(render, files)| render_run(render, files.clone())) + .chain( + shaped + .iter() + .chain(render_mutants) + .map(|render| render_run(render, files_of(render))), + ) + .collect(); + let within: Vec = renders + .iter() + .map(|(render, _)| render) + .chain(shaped) + .chain(render_mutants) + .zip(&render_runs) + .filter(|(render, _)| render.library.len() <= EVALUATOR_LIBRARY) + .map(|(_, run)| run.clone()) + .collect(); + // rustc compiles each byte of an argument as one array element, and the + // libraries of the committed packages are megabytes together, so a render + // whose library is beyond the evaluator's bound runs under rustc on the + // first 4,096 bytes of it, held to the recipe, which reads it only as + // bytes to digest. + let compiled: Vec = renders + .iter() + .map(|(render, _)| render) + .chain(shaped) + .chain(render_mutants) + .zip(&render_runs) + .map(|(render, run)| { + if render.library.len() <= EVALUATOR_LIBRARY { + return run.clone(); + } + let mut render = render.clone(); + render.library.truncate(4_096); + render_run(&render, files_of(&render)) + }) + .collect(); + (within, compiled) + } + + #[test] + fn render_corpora_preserve_inputs_and_outcomes() { + let mut renders = Vec::new(); + for std in [false, true] { + for length in [ + 0, + 4_096, + EVALUATOR_LIBRARY - 1, + EVALUATOR_LIBRARY, + EVALUATOR_LIBRARY + 1, + EVALUATOR_LIBRARY * 2, + ] { + let render = Render { + scaffold: scaffold::get().expect("the scaffold").clone(), + name: format!("render_{length}"), + version: "1.2.3".to_owned(), + std, + program: "f".repeat(64), + sources: vec!["a".repeat(64)], + effects: vec!["overflow".to_owned()], + exports: vec![("run".to_owned(), 0)], + library: (0u8..=255).cycle().take(length).collect(), + }; + // Distinct supplied files catch replacing the package's + // expected outcome with the recipe for a small library. + let files = ( + format!("package cargo: {std} {length}").into_bytes(), + format!("package provenance: {std} {length}").into_bytes(), + ); + renders.push((render, files)); + } + } + let shaped: Vec = renders + .iter() + .map(|(render, _)| { + let mut render = render.clone(); + render.name = "a\"b\\c".to_owned(); + render.scaffold.library = "A.rs".to_owned(); + render + }) + .collect(); + let mutants: Vec = shaped + .iter() + .map(|render| { + let mut render = render.clone(); + render.exports.reverse(); + render.sources.clear(); + render + }) + .collect(); + let expected = previous_corpora(&renders, &shaped, &mutants); + let observed = render_corpora(&renders, &shaped, &mutants); + assert_eq!(observed, expected, "both ordered vectors are byte-identical"); + assert_eq!(observed.0.len(), 24, "both profiles include the boundary"); + assert_eq!(observed.1.len(), 36, "rustc keeps every render"); + } +} From 5088c0f47d8a15abcb6a2567c857cfa79fd01b39 Mon Sep 17 00:00:00 2001 From: Alex Flom Date: Mon, 9 Nov 2026 19:09:07 -0700 Subject: [PATCH 02/22] style(bootstrap): apply pinned rustfmt to assembly regression --- crates/conformance/src/cases/bootstrap/rust_package.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/conformance/src/cases/bootstrap/rust_package.rs b/crates/conformance/src/cases/bootstrap/rust_package.rs index ae64cebf46..745fcae696 100644 --- a/crates/conformance/src/cases/bootstrap/rust_package.rs +++ b/crates/conformance/src/cases/bootstrap/rust_package.rs @@ -1131,7 +1131,10 @@ mod tests { .collect(); let expected = previous_corpora(&renders, &shaped, &mutants); let observed = render_corpora(&renders, &shaped, &mutants); - assert_eq!(observed, expected, "both ordered vectors are byte-identical"); + assert_eq!( + observed, expected, + "both ordered vectors are byte-identical" + ); assert_eq!(observed.0.len(), 24, "both profiles include the boundary"); assert_eq!(observed.1.len(), 36, "rustc keeps every render"); } From 614c978e4f57d8ece6dae06ec85b0a0ab52d968f Mon Sep 17 00:00:00 2001 From: humuhumu33 <68450279+humuhumu33@users.noreply.github.com> Date: Thu, 12 Nov 2026 12:51:21 +0000 Subject: [PATCH 03/22] docs: proposal for contracts on code LexLean did not generate (external declarations) One closed declaration that binds a foreign function to a stated contract: an opaque constant whose hidden value is a LexLean witness, and one generated contract axiom that every dependent theorem carries in its exact observed set and verify reports with its evidence. Options, a Lean 4.32.1 prototype with its output, an assessment of an Aeneas bridge, and the decisions left to the owners. Proposal only: nothing is implemented. Co-Authored-By: Claude Fable 5.2 --- docs/proposals/external-contracts.md | 260 +++++++++++++++++++++++++++ 1 file changed, 260 insertions(+) create mode 100644 docs/proposals/external-contracts.md diff --git a/docs/proposals/external-contracts.md b/docs/proposals/external-contracts.md new file mode 100644 index 0000000000..84af21cc11 --- /dev/null +++ b/docs/proposals/external-contracts.md @@ -0,0 +1,260 @@ +# Contracts for code LexLean did not generate: design + +Draft for owner acceptance, written 2026-11-12, against `release/v0.4`. Normative text is `SPEC.md`; this document explains and asks. It is a proposal only: this pull request adds this one file. The mechanism it recommends changes the trust model of SPEC §4.2 and §5, which is the owners' decision, so nothing is implemented until they choose. The mechanism was prototyped in the pinned Lean 4.32.1 with scratch files outside the repository; the source and what Lean printed are in section 7. + +This is gap B of three on the way to a verified inference engine written as a LexLean application. Gap A (arrays of fixed-width integers, `SM-39`) is implemented in its own pull request. Gap C (floating point) is a proposal in its own pull request and relies on this one for native execution. + +## 1. Summary + +A fast engine is hand-written code LexLean did not generate: SIMD, WebAssembly, a C kernel. Today a LexLean document cannot mention it. The only choices are to leave it out of the model, so nothing is proved about the system that runs, or to rewrite it in LexLean, so it is no longer fast. + +The proposal is one new declaration, `external`, that names a foreign function, states its contract, and makes the assumption that the function meets the contract a visible, named, audited fact: + +1. The declaration states a signature, a contract (a proposition over the inputs and the output), a **witness** (a LexLean definition with a theorem that it meets the contract), and **evidence** (what discharges the assumption for the real code). +2. Generated Lean declares an opaque constant whose hidden value is the witness, and one axiom, `..contract`, that states the contract of that constant. +3. A caller is proved against the contract and nothing else: the kernel cannot see through the constant. +4. Every theorem that uses the contract has that one axiom in its observed set. LexLean's policy is exact, so the theorem must list it, and `verify` reports it with its evidence. A theorem that does not use the external function does not carry it. +5. No other axiom can be written. The ban on author axioms stands for everything else, and the one axiom this form generates cannot be inconsistent, because the witness proves a function with that contract exists. + +**Prototype result.** In pinned Lean, a caller `score` of an external int8 dot product `dotFast` was proved correct against the contract. `#print axioms` on the caller's theorem printed `[propext, Engine.Main.dotFast.contract]`; on a theorem about the reference alone it printed that it depends on no axioms; and `rfl` could not prove the external function equal to its witness (section 7). + +Decisions that need the owners: + +| # | Decision | Recommendation | +| --- | --- | --- | +| D1 | Whether a document may state an assumption about foreign code at all | Yes, in this one closed form | +| D2 | Mechanism | An opaque constant plus one generated contract axiom (option A of section 5). It is the only option where Lean itself reports every use | +| D3 | Whether a witness is mandatory | Yes. Without it the form is an author axiom under another name | +| D4 | Evidence kinds | `attested` (a content-addressed conformance report bound to the contract's statement ID) first; `proved` (a kernel-checked theorem about a Lean model of the code) when Lean-library imports land (#362) | +| D5 | Project-level opt-in | Yes: `[[external_source]]` in `lexlean.toml`, pinned in the lock, so a reviewer sees every external in the configuration | +| D6 | Emitting `@[extern "symbol"]` so that Lean-compiled C calls the foreign symbol | Not in the first change. Decide the symbol and calling convention with PrismPM first; until then the compiled constant runs the witness | +| D7 | Production (certified Rust) | Formal-only at first, like arrays and text components | +| D8 | An Aeneas bridge | Not now. It is sound in principle and not small (section 8) | + +## 2. What the goal asks and where it stands + +| Asked | Mechanism here | State | +| --- | --- | --- | +| Declare an external function with a stated contract | `external` declaration | Designed; prototyped in Lean; not implemented | +| A model can call it and proofs can use the contract | Ordinary call; `apply` or `simplify` naming `.contract` | Prototyped | +| No hole | Mandatory witness theorem; closed form; no free axiom text | Argued in section 6 | +| Every use visible in the observed axiom or trust set | The exact axiom audit, unchanged | Prototyped: `#print axioms` shows the contract axiom | +| Reported by `verify` | Attestation gains trust records; summary line | Designed | +| Names the evidence that discharges it | `evidence: attested{...}` or `proved{...}` | Designed | +| Does not weaken the ban for everything else | Only this declaration can generate an axiom, of one fixed shape | By construction | +| Example: external int8 dot product, caller proved, trust entry shown | `examples/external-contract` | Prototype in Lean only (section 7) | + +## 3. What exists + +- **Contracts and models** (§17.12). A `contract` has typed input and output and proposition-valued predicates with validators; a `model` binds a contract to a `realization`, which is always a LexLean denotation. `checked_apply` runs validators at run time and returns a `ContractViolation` on refusal. This covers code LexLean generates, and results that are cheap to check. It does not cover a result that is as expensive to check as to compute, which is the case for a dot product. +- **External Lean constants** (§4.1, §13.7). A lexicon entry of kind `lean` names a constant of the Lean workspace with a declared signature, probed at verification. This imports Lean, not foreign code, and the workspace is in the trusted base (§5.1). +- **Lean-library imports** (#362, draft on `release/v0.5`), with a feasibility probe for Aeneas or Hax under #356. +- **The ban** (§4.2): no author-defined axioms, no raw Lean. Every declaration has an exact axiom policy; `verify` refuses a missing, additional or unlisted observed axiom (§17.11, §22). + +## 4. Constraints taken as given + +- §5.4: the axiom audit reports what `#print axioms` returns, nothing more. A mechanism Lean does not report would need a second audit that LexLean alone vouches for. +- §5.3: "verified" is only said of what Lean elaborated, `leanchecker` replayed and the audit accepted. +- AGENTS.md rule 4: a claim about a dependency belongs to that dependency. A claim about hand-written code belongs to whoever attests it, and must be named as such. +- R4: nothing deferred; R5: every failure registered. + +## 5. Options + +**A. Opaque constant and one generated contract axiom (recommended).** Described in section 6. Lean reports every use. One new kind of axiom exists, generated only by this form. + +**B. Parameterised theorems, no axiom.** Every definition that reaches the external function takes the implementation and a proof of its contract as parameters; theorems are quantified over them. Nothing is assumed in Lean at all. The cost: every caller, transitively, changes its signature, a production root cannot be closed, and the "which implementation is running" fact moves entirely outside Lean, where only LexLean's own bookkeeping reports it. + +**C. Check every result at run time.** This is `checked_apply` today. It needs no trust. It is the right answer whenever checking is cheaper than computing, and it is useless for an inference kernel, where the check is the computation. + +**D. Reference body with an unaudited binding.** The external function is defined as the reference, and a compiler attribute (`@[extern]`, `@[implemented_by]`) substitutes foreign code. This is how Lean's own core binds native code. No axiom appears, so `#print axioms` shows nothing: every theorem looks unconditional while the running code is trusted silently. This is the hole the request rules out. + +A is recommended because it is the only one in which the trusted assumption is an object of the logic that the pinned Lean itself tracks through every dependency, with no new audit to trust. + +## 6. Design of option A + +### 6.1 The declaration + +In the authoring notation: + +```text +external{ + name: dotFast, + parameters: [left : array{element: Int8}, right : array{element: Int8}], + result: Int, + contract: DotContract, + witness: {definition: dotReference, theorem: dot_reference_meets}, + evidence: attested{report: dotFastConformance}, + symbol: "engine_dot_i8" +} +``` + +- `contract` names a prior proposition-valued definition whose parameters are the external function's parameters followed by its result. +- `witness.definition` names a prior LexLean definition of exactly the external function's signature. `witness.theorem` names a prior theorem whose statement is exactly the contract applied to the parameters and to the witness applied to them. Linking checks both shapes; Lean checks the theorem. +- `evidence` is one of the closed forms of section 6.4. +- `symbol` is the foreign name, recorded in the build outputs for a host to bind (D6). + +### 6.2 Lowering + +```lean +public opaque dotFast (left : Array Int8) (right : Array Int8) : Int := dotReference left right +public axiom dotFast.contract (left : Array Int8) (right : Array Int8) : + DotContract left right (dotFast left right) +``` + +The hidden value of the constant is the witness. The kernel never unfolds an opaque constant, so nothing about `dotFast` is provable except through the axiom. A document refers to the axiom as the member `dotFast.contract`, in `apply` and `simplify`, exactly as it refers to a theorem. + +The canonical document states the declaration, the contract, the witness and the evidence. The generated-source audit admits `opaque` and `axiom` tokens only at the positions an `external` declaration of the IR accounts for. + +### 6.3 Why this is not a hole + +The axiom says: the constant `dotFast` satisfies `DotContract`. The constant's hidden value is `dotReference`, and `dot_reference_meets` is a kernel-checked proof that `dotReference` satisfies `DotContract`. So the axiom is a true statement about the constant; it is unprovable inside Lean only because the constant is opaque. Replacing `opaque` by a definition turns the axiom into a theorem with the witness theorem as its proof. Any proof that uses the axiom therefore stays valid with the axiom replaced by that theorem: the axiom adds no logical strength. In particular it cannot make `False` provable, whatever contract an author writes, because an unsatisfiable contract has no witness theorem and is refused at verification. + +What remains assumed is not logical. It is the statement that the foreign code, where it runs in place of the constant, computes a function with that contract, and that it is a function: no dependence on anything but its arguments, no effect, termination on every input the contract admits. That statement is what the evidence is for, and it is named in every theorem that needs it. + +### 6.4 Evidence + +- `attested{report: A}`: `A` is an `artifact` declaration of role `conformance`, content-addressed like every artifact. Its bytes are a canonical JSON report that states the statement ID of the external declaration (`SM-38`), the digest of the foreign code tested, the reference it was compared with, the number of cases and the result. Linking refuses a report that names another statement ID. So a change of the contract, the signature or the witness makes old evidence stale by construction. LexLean does not run the foreign code and does not vouch for the report: the attestation records who is trusted for what. +- `proved{theorem: T}`: `T` is a theorem, checked by the pinned Lean, that a Lean model of the foreign code meets the contract. This needs the model to be importable (#362) and belongs to a later change. It does not remove trust; it moves it to whatever produced the model (section 8). + +### 6.5 Policy and reporting + +- A project lists the externals it admits: `[[external_source]]` in `lexlean.toml` with the module, the name and the report digest, pinned in `lexlean.lock`. An `external` declaration without its row is refused. A project with no row accepts no external declaration, so the default is the ban as it is today. +- Each theorem and definition states its exact axioms as now. A theorem that uses `dotFast.contract` lists `Project.Module.dotFast.contract` beside `propext`; a missing or extra one is `LLV7005`, unchanged. +- The attestation gains `trust`: for each contract axiom, its evidence, and the declarations whose observed sets contain it. `verify` prints one line per contract axiom after its summary, for example: + +```text +verified 1 module; attestation +trust: Engine.Main.dotFast.contract, attested by sha256:, used by 1 theorem and 1 definition +``` + +- A status line never says "verified" without the trust lines when any exist. + +### 6.6 Diagnostics + +Malformed declarations are `LLT4001` (contract or witness of the wrong shape, a witness theorem that states something else, a report bound to another statement ID, an external without its configuration row). A witness theorem Lean refuses is `LLV7002`. An unlisted contract axiom is `LLV7005`. No new code is needed for these; whether the missing-row refusal deserves its own code is a detail for the implementation. + +## 7. Evidence: the prototype + +Pinned Lean 4.32.1 (`f054605aea4b840552cca2e725580bffd1e1b704`), Windows x86-64, 2026-11-12, one scratch file outside the repository, written in the shapes the backend emits: + +```lean +@[expose] public def DotContract (left : Array Int8) (right : Array Int8) (result : Int) : Prop := + result = dotList left.toList right.toList + +@[expose] public def dotReference (left : Array Int8) (right : Array Int8) : Int := + dotList left.toList right.toList + +public theorem dot_reference_meets (left : Array Int8) (right : Array Int8) : + DotContract left right (dotReference left right) := rfl + +public opaque dotFast (left : Array Int8) (right : Array Int8) : Int := dotReference left right + +public axiom dotFast.contract (left : Array Int8) (right : Array Int8) : + DotContract left right (dotFast left right) + +@[expose] public def score (left : Array Int8) (right : Array Int8) (bias : Int) : Int := + dotFast left right + bias + +public theorem score_correct (left : Array Int8) (right : Array Int8) (bias : Int) : + score left right bias = dotList left.toList right.toList + bias := by + have h := dotFast.contract left right + simp only [DotContract] at h + simp only [score, h] +``` + +What Lean printed: + +```text +'Engine.Main.score_correct' depends on axioms: [propext, Engine.Main.dotFast.contract] +'Engine.Main.reference_only' does not depend on any axioms +'Engine.Main.dot_reference_meets' does not depend on any axioms +``` + +and, for `example (l r : Array Int8) : dotFast l r = dotReference l r := by rfl`: + +```text +error: Tactic `rfl` failed: The left-hand side + Engine.Main.dotFast l r +is not definitionally equal to the right-hand side + Engine.Main.dotReference l r +``` + +So the caller is proved against the contract, the trust entry is exactly one named axiom in the caller's observed set, a theorem that does not use the external function does not carry it, and the kernel cannot reach the witness through the constant. + +This is a Lean prototype of the lowering. It is not `lexlean verify` output: no `external` declaration exists in the compiler. + +## 8. Could Rust translated to Lean by Aeneas discharge a contract? + +Aeneas translates a subset of safe Rust to pure Lean definitions; a contract would then be a theorem about the translated function, and the evidence would be `proved`. + +What was checked (2026-11-12, the project's repository at `main`): + +- Its Lean backend requires Mathlib and pins `leanprover/lean4:v4.36.0-rc2`. LexLean pins 4.32.1, and the draft that moves the pin (#433) moves it to 4.33.1. +- Generated files import the `Aeneas` Lean package, so its declarations and Mathlib would have to be imported, pinned and audited (#362, with the 7 GB library cost noted there). +- It supports a subset of safe Rust. Unsafe code and concurrency are stated as unsupported; some loop forms are unsupported. +- The translation is described in two papers; the tool that performs it is not itself verified. + +Assessment: + +- **Sound?** As far as Lean is concerned, yes: the theorem is kernel-checked and the axiom audit reports whatever axioms the library uses. But the theorem is about the Lean model, not about the Rust. That the model is faithful is the translator's claim. So an Aeneas bridge does not remove the trust entry; it replaces "this code was tested against the reference" with "this translator is faithful and the compiled Rust behaves as its source", which must be named the same way. +- **Small?** No. It needs Lean-library imports to land, a toolchain where LexLean's pin and Aeneas's meet, Mathlib in the verification of every project that uses it, and a new evidence kind. +- **Useful for this engine?** Only for a scalar, safe-Rust kernel. The fast paths that motivate an external function use SIMD intrinsics, which are outside the supported subset. + +Recommendation: do not build the bridge now. Keep `proved` in the design so it can be added without changing the declaration, and revisit under #356 when #362 has landed. + +## 9. What it costs + +Estimated from the arrays change, which touched the same layers: + +- IR, linker rules, schema (module and snapshot), Lean backend, canonical LaTeX, generated-source audit: one new declaration kind. +- Axiom handling: admit a generated name in exact policies; the attestation's `trust` records; the summary lines. +- Configuration and lock: `[[external_source]]`. +- One artifact role and report schema. +- Production registry row (formal-only), oracle overlap row. +- Register rows: one for the declaration and its refusals, one for the trust report. An example, negative fixtures for each refusal, and planted defects: a witness theorem about another function, a report bound to another statement ID, a theorem that omits the contract axiom from its policy, an external without its configuration row. +- It moves the language-1.2 identity, so it needs one regeneration, and lands one at a time with the other identity changes on the line (arrays, #474). + +## 10. Limits of the design itself + +- The contract is about a mathematical function. Foreign code that reads a clock, keeps state, or does not terminate is outside what the axiom says, and evidence by testing cannot establish totality. +- Attested evidence is as good as the test that produced it. LexLean binds the report to the contract and to the code digest; it does not judge coverage. +- A witness is required. A contract for which no LexLean function can be written (for example one that only bounds an error) needs the reference written first. For an inference kernel the witness is the integer reference engine, which has to exist anyway. +- Until D6 is decided the compiled Lean runs the witness, so the trust entry describes a binding a host makes, not one LexLean emits. + +## 11. Not implemented by this PR + +Everything. The file is the proposal. What would have to be true before work starts: the owners accept D1 to D3; arrays (`SM-39`) are on the line, because the example is over arrays; and the identity change is sequenced with #474. + +## 12. Decisions that belong to the owners + +### D1. An assumption about foreign code in a document + +§4.2 bans author axioms so that a verified status means what Lean checked. This proposal keeps that sentence true by putting the assumption in the theorem's own axiom set, where the status already shows it. The alternative is that systems with a hand-written kernel are never modelled. + +### D2. Mechanism + +Section 5. Option A if the trust must be reported by Lean; option B if the owners prefer that no axiom of any kind is ever generated, at the cost of threading parameters through every caller. + +### D3. Mandatory witness + +Without a witness an unsatisfiable contract proves anything. With it, the form adds no logical strength (section 6.3). + +### D4. Evidence kinds + +`attested` first. The report format and the artifact role are small. `proved` waits for #362. + +### D5. Project opt-in + +A configuration row makes every external visible in a diff of `lexlean.toml` and the lock, and keeps the default at "none". + +### D6. `@[extern]` + +Lean's `@[extern "symbol"]` on the opaque constant makes Lean-compiled C call the symbol. Prism builds applications that way, so this is how the fast engine would actually run. It fixes a calling convention for arrays and integers that PrismPM has to agree to, and the generated-source audit forbids the token today. + +### D7. Production + +Formal-only at first. A Rust realization would be an `extern` item or a trait parameter and is a separate design. + +### D8. Aeneas + +Section 8. From 513ea1534ff9dc0ee2a7fbd4c04c9ff567c15903 Mon Sep 17 00:00:00 2001 From: humuhumu33 Date: Thu, 12 Nov 2026 14:59:10 +0000 Subject: [PATCH 04/22] verify: every part names its toolchain by address A shard writes toolchain.json (lexlean/verify-toolchain/1) beside its output: the SHA-256 of the lean, lake and leanchecker its preflight hashed, of the running lexlean, and the address of the rustc that compiled it (its executable and rustc_driver library, hashed by the build script). A part that reads another refuses it (LLI9001, naming the part, the tool and both addresses) unless all five equal its own. A process record names only lean or leanchecker, so a part that ran another lake, lexlean or rustc was accepted before. VR-23 plants another lake address in a shard's output and, on unix, runs a join whose own lake differs from the pinned one by one byte; both are refused. Published directories, attestations and expected/verify do not change, and neither does the compiler-semantics ID. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 12 ++ CONFORMANCE.md | 2 +- SPEC.md | 6 +- crates/conformance/src/cases/operational.rs | 74 +++++++++ crates/lexlean/Cargo.toml | 5 + crates/lexlean/build.rs | 77 +++++++++ crates/lexlean/src/lib.rs | 7 + crates/lexlean/src/verify/mod.rs | 25 ++- crates/lexlean/src/verify/partition.rs | 174 ++++++++++++++++---- crates/lexlean/src/verify/toolchain.rs | 111 +++++++++++++ features/suites/verification.feature | 3 +- model/ids.toml | 2 +- 12 files changed, 455 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08322fa32a..5af8bd4b4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,18 @@ workspace carried at those versions without a tag. ## Unreleased +- Every part of a verification names its toolchain by address (SPEC.md + §22.11, §9.4). A shard writes `toolchain.json` (`lexlean/verify-toolchain/1`) + beside its output: the SHA-256 of the `lean`, `lake`, and `leanchecker` its + preflight hashed, of the running `lexlean`, and the address of the `rustc` + that compiled it (its executable and `rustc_driver` library, which the build + script hashes). A part that reads another refuses it (`LLI9001`, naming the + part, the tool, and both addresses) unless all five equal its own. A record + names only the executable it ran, so a part that ran another `lake`, + `lexlean`, or `rustc` was accepted before. `VR-23` plants another `lake` + address in a shard's output, and on unix a join whose own `lake` differs by + one byte. Published directories, attestations, and `expected/verify` are + unchanged. - A one-pass writer for the counts of the bootstrap count cases, #92. Under `LEXLEAN_COUNTS=fast` each of `BS-05` to `BS-08` and `BS-11` to `BS-15` records every count it checks in `target/count-report/` instead of diff --git a/CONFORMANCE.md b/CONFORMANCE.md index 5e0010e5e9..438caf2cef 100644 --- a/CONFORMANCE.md +++ b/CONFORMANCE.md @@ -675,7 +675,7 @@ the error registry does not sanction (R5, `ERRORS.md`). | `VR-20` | `build` | The verifier process width is an operational resource profile selected only by the environment, defaulting to 1 and refusing any malformed value with LLV7017, and it enters no lock, identity, or verified artifact. | | `VR-21` | `build` | A multi-module project whose imports form nontrivial frontiers verifies at width 1 and at a larger width to byte-identical verified directories and the same attestation identity, observed axiom sets, and policy rows, and no completion order orders a record or changes which failure is reported. | | `VR-22` | `build` | Performance telemetry is written only to the CI evidence directory the environment names, a verification with telemetry on publishes byte-identical artifacts to one without, and telemetry planted in a verified artifact fails the gate. | -| `VR-23` | `build` | A project verified as the parts of one run, each shard running the processes its plan names with the oleans of the shards it needs and the join reading every shard's records, publishes a verified directory byte-identical to the whole verification's with the same attestation identity, and a shard whose plan leaves a module without its imports, a join missing a record, or a join reading a record of an executable other than its own, is refused. | +| `VR-23` | `build` | A project verified as the parts of one run, each shard running the processes its plan names with the oleans of the shards it needs and the join reading every shard's records, publishes a verified directory byte-identical to the whole verification's with the same attestation identity, and a shard whose plan leaves a module without its imports, a join missing a record, a join reading a record of an executable other than its own, or a part that ran a toolchain other than its own, is refused. | ## Cited authorities diff --git a/SPEC.md b/SPEC.md index fb94dc902f..d2646e6557 100644 --- a/SPEC.md +++ b/SPEC.md @@ -695,7 +695,7 @@ Its image MUST install the pinned Rust and Lean toolchains at build, so that not In a run that is the whole gate, the jobs run the recipes as the rows of `.github/vv-rows.json`, each from its own clean checkout: `examples`, which verifies every example but UOR Atlas; `honesty`, which runs `fmt-check model spec-links bdd` before any expensive root; `rest`, the other recipes in the order of `vv`; and one `bdd-` row for each named part of the `bdd` plan below, which runs `bdd` alone. Each leg runs `just` with its recipes, which stops at the first that fails, as `vv` does. The part of `examples` a job runs is named by its matrix row through `LEXLEAN_EXAMPLES_PART` and by nothing else: `without:uor-atlas` on the leg, `shard:uor-atlas:` and `join:uor-atlas` on the Atlas jobs below; unset or empty, which is what `just vv` runs, the recipe verifies every example whole. After honesty succeeds, the remaining rows run as one matrix and the supported-host matrix, Intel construction and Atlas prefix may start; a failed or skipped honesty part starts none of these expensive roots. Together the legs and the Atlas jobs run every recipe of `vv`, every example, and every test of `bdd` exactly once, and the run passes only when every job passes, so it proves what one run of `just vv` proves; no recipe depends on another having run, and every recipe owns the files it builds. Its wall time is the longest path through the jobs rather than the sum. `just vv` remains the acceptance recipe of §9.2 for every other run, and release acceptance requires the complete same-tree run. -UOR Atlas is verified as the parts of one run. `.github/verify-shards.json` partitions its verification into shards, each naming the shards it needs, the modules it elaborates, and the modules it replays and audits, one of them named `join`; `cargo xtask ci-shards` checks, as the verifier would when a part ran, that every module is elaborated by exactly one shard and replayed by exactly one, that every import of a module a shard elaborates or replays is elaborated by that shard or by one it needs, directly or through another, and that `join` needs every other shard. Each shard is a job of its own clean checkout that runs the verifier's prelude whole (certificate generation, the preflights, staging, the generated-source audit, the probe), then elaborates, replays, and audits only the modules of its plan, reading the `.olean` files of the modules it imports from the output the jobs of the shards it needs uploaded in the same run, and uploads its own `.olean` files and process records as its output. The join runs the modules of its plan, reads every shard's records, requires each to name the executable the join itself hashed for the tool that produced it (`lean` for an elaboration or an audit member, `leanchecker` for a replay), judges each as the stage that produced it judges its own, and continues through policy, the attestation over every record in the canonical order of §22.11, publication, and the comparison of every normalized record with `expected/verify` (§29.5): what it publishes is what the whole verification publishes, record for record and attestation identity included (`VR-23`). A module without a record from any part, a record of a process no module owns, a record whose `executable_sha256` is not the join's digest of its tool (`LLI9001`, naming the record and both digests), or a shard whose plan leaves a module without the `.olean` it imports fails the run. Every process of the verification runs exactly once per run. The executable the Atlas jobs run is the one the `prefix` job built from its clean checkout, carried to the other jobs of the run as an artifact beside the shards' output; it, the output, and the records cross no run, and a job reads the artifacts of its own run only. The Atlas jobs run after the jobs of the shards they need, at the profile of the `examples` leg, and only in a run that is the whole gate. +UOR Atlas is verified as the parts of one run. `.github/verify-shards.json` partitions its verification into shards, each naming the shards it needs, the modules it elaborates, and the modules it replays and audits, one of them named `join`; `cargo xtask ci-shards` checks, as the verifier would when a part ran, that every module is elaborated by exactly one shard and replayed by exactly one, that every import of a module a shard elaborates or replays is elaborated by that shard or by one it needs, directly or through another, and that `join` needs every other shard. Each shard is a job of its own clean checkout that runs the verifier's prelude whole (certificate generation, the preflights, staging, the generated-source audit, the probe), then elaborates, replays, and audits only the modules of its plan, reading the `.olean` files of the modules it imports from the output the jobs of the shards it needs uploaded in the same run, and uploads its own `.olean` files and process records as its output. The join runs the modules of its plan, reads every shard's records, requires each to name the executable the join itself hashed for the tool that produced it (`lean` for an elaboration or an audit member, `leanchecker` for a replay), requires every shard's output to name the join's own toolchain addresses (§22.11), judges each as the stage that produced it judges its own, and continues through policy, the attestation over every record in the canonical order of §22.11, publication, and the comparison of every normalized record with `expected/verify` (§29.5): what it publishes is what the whole verification publishes, record for record and attestation identity included (`VR-23`). A module without a record from any part, a record of a process no module owns, a record whose `executable_sha256` is not the join's digest of its tool (`LLI9001`, naming the record and both digests), a shard whose output names no toolchain addresses or another address of any tool (`LLI9001`, naming the part, the tool, and both addresses), or a shard whose plan leaves a module without the `.olean` it imports fails the run. Every process of the verification runs exactly once per run. The executable the Atlas jobs run is the one the `prefix` job built from its clean checkout, carried to the other jobs of the run as an artifact beside the shards' output; it, the output, and the records cross no run, and a job reads the artifacts of its own run only. The Atlas jobs run after the jobs of the shards they need, at the profile of the `examples` leg, and only in a run that is the whole gate. The `bdd` recipe runs as the parts of one run as well. `.github/bdd-parts.json` names parts by conformance ID, complete integration-test targets in `targets`, and a remainder, `rest`. Cases that share a fixture built once per test process belong to one part. The `honesty` part owns the complete `bdd` meta-gate target, including any tests added to that target; no copied test-name list selects it. `cargo xtask bdd` runs `cargo test -p repo-conformance --all-features` with the Cargo target selectors and harness filters of `LEXLEAN_BDD_PART`. A target part selects its targets through `--test`; an ID part uses `--exact` and the test of each ID. The remainder discovers all integration-test targets from Cargo metadata, selects every target no target part owns, and keeps the library and binary tests and doctests; Cargo requires doctests in a separate invocation when target selectors are present. Its `--exact` and `--skip` filters omit every conformance test an ID part owns. A new test in a target belongs to that target's part; a new integration target and a new conformance ID belong to the remainder until assigned. No case decides whether it runs. `inventory` lists the tests using the same selectors and filters, proves disjointness and equality with the whole `bdd` suite, and requires a target part to own every test of its target (§9.2). Exactly one row runs every part, the remainder included. The initial honesty row names its part; each later `bdd-` row and the `rest` row name theirs. Unset or empty, as in local `just vv`, the recipe runs the whole suite unchanged. Each row builds from its own clean checkout; no build or evidence crosses runs. @@ -10224,7 +10224,7 @@ The profile changes how processes are scheduled and nothing else: For every project, the directory published at any profile equals, byte for byte, the directory published at width 1 without hints and without a budget, on the same host and toolchain; in particular the attestation ID, the observed axiom sets, the policy rows, the canonical compiler input, and every normalized verification record (§29.5) are identical. -A verification may also run as the parts of one run (§9.4): each part runs the processes of its plan at the profile of its own host, a part reads from another only the `.olean` files and process records that part's processes produced, and the join judges every record it reads as the stage that produced it judges its own, after requiring it to name the join's own digest of the executable that stage runs. The directory the join publishes equals, byte for byte, the one the whole verification publishes, attestation ID included (`VR-23`); a part is defined for a project without production roots and without PDF rendering. +A verification may also run as the parts of one run (§9.4): each part runs the processes of its plan at the profile of its own host, a part reads from another only the `.olean` files and process records that part's processes produced, and the join judges every record it reads as the stage that produced it judges its own, after requiring it to name the join's own digest of the executable that stage runs. Every part writes beside its output `toolchain.json` (`lexlean/verify-toolchain/1`): the SHA-256 of the `lean`, `lake`, and `leanchecker` its preflight hashed, of the running `lexlean`, and the address of the `rustc` that compiled that `lexlean`, which is the SHA-256 of one line ` ` per file of the compiler's `rustc` executable and `rustc_driver` library under `bin/` and `lib/`, in path order, as the build of `lexlean` found them. A part that reads another part requires its `toolchain.json` to name its own five addresses, and refuses the part otherwise, before it reads any record (`LLI9001`): a record names only the executable it ran, so a part that ran another `lake`, `lexlean`, or `rustc` is seen only there. The directory the join publishes equals, byte for byte, the one the whole verification publishes, attestation ID included (`VR-23`); a part is defined for a project without production roots and without PDF rendering. ### 22.12 Performance telemetry @@ -11805,7 +11805,7 @@ Every row below is normative, has honesty level `build`, and MUST be copied byte | `VR-20` | `verification` | The verifier process width is an operational resource profile selected only by the environment, defaulting to 1 and refusing any malformed value with LLV7017, and it enters no lock, identity, or verified artifact. | §22.11, §23.1 | | `VR-21` | `verification` | A multi-module project whose imports form nontrivial frontiers verifies at width 1 and at a larger width to byte-identical verified directories and the same attestation identity, observed axiom sets, and policy rows, and no completion order orders a record or changes which failure is reported. | §22.3, §22.4, §22.11 | | `VR-22` | `verification` | Performance telemetry is written only to the CI evidence directory the environment names, a verification with telemetry on publishes byte-identical artifacts to one without, and telemetry planted in a verified artifact fails the gate. | §22.12 | -| `VR-23` | `verification` | A project verified as the parts of one run, each shard running the processes its plan names with the oleans of the shards it needs and the join reading every shard's records, publishes a verified directory byte-identical to the whole verification's with the same attestation identity, and a shard whose plan leaves a module without its imports, a join missing a record, or a join reading a record of an executable other than its own, is refused. | §9.4, §22.11 | +| `VR-23` | `verification` | A project verified as the parts of one run, each shard running the processes its plan names with the oleans of the shards it needs and the join reading every shard's records, publishes a verified directory byte-identical to the whole verification's with the same attestation identity, and a shard whose plan leaves a module without its imports, a join missing a record, a join reading a record of an executable other than its own, or a part that ran a toolchain other than its own, is refused. | §9.4, §22.11 | | `CL-01` | `cli-api` | Global options and upward project discovery obey the exact CLI contract. | §23.1, §23.2 | | `CL-02` | `cli-api` | Init creates the complete canonical skeleton only in an absent or empty destination and never overwrites. | §23.4 | | `CL-03` | `cli-api` | Lock check, local update, and explicit network acquisition obey their exact mutually exclusive behavior. | §23.4 | diff --git a/crates/conformance/src/cases/operational.rs b/crates/conformance/src/cases/operational.rs index 5719b0f8ac..2cd3da1638 100644 --- a/crates/conformance/src/cases/operational.rs +++ b/crates/conformance/src/cases/operational.rs @@ -905,6 +905,80 @@ fn parts_equal_the_whole() { text.contains("from any part of the run") && text.contains(&rest[0]), "{text}" ); + // A join refuses a part that ran another toolchain: the first shard's + // output names the address of every executable it ran or was built by + // (§22.11), and a `lake` other than the join's is refused, naming the + // part, the tool, and both addresses, although no record names `lake`. + let addresses_path = dir("a").join("toolchain.json"); + let addresses = std::fs::read_to_string(addresses_path.as_std_path()) + .expect("a shard names its toolchain addresses"); + let lake = serde_json::from_str::(&addresses).expect("the addresses parse") + ["lake"] + .as_str() + .expect("the addresses name lake") + .to_owned(); + let planted_lake = "f".repeat(lake.len()); + assert_ne!(lake, planted_lake); + std::fs::write( + addresses_path.as_std_path(), + addresses.replacen(&lake, &planted_lake, 1), + ) + .expect("plant another lake"); + let error = run_part(VerifyPart::Join { + plan: plan(&rest, &rest), + parts: vec![dir("a"), dir("b")], + }) + .map(|_| ()) + .expect_err("a part of another lake"); + support::expect_code(&error, "LLI9001"); + let text = error.to_string(); + assert!( + text.contains("ran `lake`") && text.contains(&planted_lake) && text.contains(&lake), + "{text}" + ); + std::fs::write(addresses_path.as_std_path(), &addresses).expect("restore the addresses"); + // The same refusal from a real binary: a join whose own toolchain's + // `lake` differs by one appended byte from the pinned one the shards ran + // refuses them, naming both addresses. Its preflight hashes the changed + // file, so the join never trusts a part of the pinned toolchain. + #[cfg(unix)] + { + let pinned = support::real_elan_home() + .join("toolchains") + .join(support::mangled_toolchain_name()) + .join("bin") + .join("lake"); + let mut changed = std::fs::read(&pinned).expect("the pinned lake"); + changed.push(0); + let changed_address = lexlean::Sha256Digest::of(&changed).to_hex(); + let home = support::fake_elan_home(&[("lake".to_owned(), changed)]); + let home_text = home.path().to_string_lossy().into_owned(); + let project = P::copy_example(example); + let error = support::with_env(&[("ELAN_HOME", Some(&home_text))], || { + Operational::new(1, None).expect("width 1").scoped(|| { + lexlean::verify_part( + &project.engine(), + VerifyRequest { + selection: Selection::Entrypoints, + }, + VerifyPart::Join { + plan: plan(&rest, &rest), + parts: vec![dir("a"), dir("b")], + }, + ) + }) + }) + .map(|_| ()) + .expect_err("a join whose own lake changed"); + support::expect_code(&error, "LLI9001"); + let text = error.to_string(); + assert!( + text.contains("ran `lake`") && text.contains(&lake) && text.contains(&changed_address), + "{text}" + ); + } + #[cfg(not(unix))] + support::unix_only("VR-23", "symlinked toolchain to plant a changed `lake` in"); // A join refuses a record that another `leanchecker` ran: the second // shard's replay record of a module names an executable that is not the // join's, as a part on a different toolchain would have written it. diff --git a/crates/lexlean/Cargo.toml b/crates/lexlean/Cargo.toml index ab72c8f8be..9f9576ea37 100644 --- a/crates/lexlean/Cargo.toml +++ b/crates/lexlean/Cargo.toml @@ -39,6 +39,11 @@ walkdir = { workspace = true } rustix = { workspace = true } signal-hook = { workspace = true } +# The build script records the address of the `rustc` that compiles this crate +# (SPEC.md §22.11): the SHA-256 of its executable and driver library. +[build-dependencies] +sha2 = { workspace = true } + [dev-dependencies] proptest = { workspace = true } diff --git a/crates/lexlean/build.rs b/crates/lexlean/build.rs index ddac668a2e..b8241cf791 100644 --- a/crates/lexlean/build.rs +++ b/crates/lexlean/build.rs @@ -82,6 +82,83 @@ fn main() { errors.display() ); fs::write(out_dir.join("errors.toml"), bytes).expect("write errors.toml"); + + rustc_identity(&out_dir); +} + +/// The address of the compiler that builds this crate (SPEC.md §22.11): the +/// SHA-256 of one line ` ` per +/// file of the `rustc` executable and its driver library, in path order, and +/// what `rustc -vV` reports. The `rustc` executable is a thin launcher; the +/// compiler is the driver library it loads, so both are addressed. A compiler +/// whose sysroot cannot be read is a build failure, never an empty address. +fn rustc_identity(out_dir: &Path) { + use sha2::{Digest, Sha256}; + use std::process::Command; + + println!("cargo:rerun-if-env-changed=RUSTC"); + let rustc = env::var("RUSTC").unwrap_or_else(|_| "rustc".to_owned()); + let run = |argument: &str| { + let output = Command::new(&rustc) + .arg(argument) + .output() + .unwrap_or_else(|error| panic!("running `{rustc} {argument}`: {error}")); + assert!( + output.status.success(), + "`{rustc} {argument}` failed: {}", + String::from_utf8_lossy(&output.stderr) + ); + String::from_utf8(output.stdout).expect("rustc reports UTF-8") + }; + let version = run("-vV").replace("\r\n", "\n"); + let sysroot = PathBuf::from(run("--print=sysroot").trim()); + let mut files: Vec<(String, PathBuf)> = Vec::new(); + for directory in ["bin", "lib"] { + let Ok(entries) = fs::read_dir(sysroot.join(directory)) else { + continue; + }; + for entry in entries.filter_map(Result::ok) { + let name = entry.file_name().to_string_lossy().into_owned(); + let compiler = name == "rustc" + || name == "rustc.exe" + || name.starts_with("rustc_driver-") + || name.starts_with("librustc_driver-"); + if compiler && entry.path().is_file() { + files.push((format!("{directory}/{name}"), entry.path())); + } + } + } + files.sort(); + assert!( + files.iter().any(|(path, _)| path.starts_with("bin/rustc")), + "{}: no `rustc` executable in the compiler's sysroot", + sysroot.display() + ); + let mut listing = String::new(); + for (relative, path) in &files { + println!("cargo:rerun-if-changed={}", path.display()); + let bytes = + fs::read(path).unwrap_or_else(|error| panic!("reading {}: {error}", path.display())); + listing.push_str(&hex(&Sha256::digest(&bytes))); + listing.push_str(" "); + listing.push_str(relative); + listing.push('\n'); + } + let address = hex(&Sha256::digest(listing.as_bytes())); + fs::write( + out_dir.join("rustc_identity.rs"), + format!( + "/// What `rustc -vV` reported for the compiler that built this crate.\n\ + pub const RUSTC_VERSION: &str = {version:?};\n\ + /// The address of that compiler's executable and driver library.\n\ + pub const RUSTC_SHA256: &str = {address:?};\n" + ), + ) + .expect("write rustc_identity.rs"); +} + +fn hex(bytes: &[u8]) -> String { + bytes.iter().map(|byte| format!("{byte:02x}")).collect() } /// Resolve a crate-relative normative path: a directory or file as such; a diff --git a/crates/lexlean/src/lib.rs b/crates/lexlean/src/lib.rs index dd7664959b..0edc6c3780 100644 --- a/crates/lexlean/src/lib.rs +++ b/crates/lexlean/src/lib.rs @@ -106,6 +106,13 @@ pub mod embedded { include!(concat!(env!("OUT_DIR"), "/embedded.rs")); } +/// The compiler that built this crate, as the build script found it +/// (SPEC.md §22.11): what `rustc -vV` reported and the address of its +/// executable and driver library. It is no input of any compiler semantics. +pub mod build_identity { + include!(concat!(env!("OUT_DIR"), "/rustc_identity.rs")); +} + /// The original language identifier. Kept as the compatibility default for /// downstream code that compiled against LexLean 1.0. pub const LANGUAGE_VERSION: &str = "1.0"; diff --git a/crates/lexlean/src/verify/mod.rs b/crates/lexlean/src/verify/mod.rs index 498053eb77..7adcaa1de0 100644 --- a/crates/lexlean/src/verify/mod.rs +++ b/crates/lexlean/src/verify/mod.rs @@ -1333,16 +1333,23 @@ pub fn run_part( // What the parts before this one wrote (§9.4): their `.olean` files into // the staged tree, and, for the part that publishes, their records. stages.enter("part-import"); + // The addresses of everything this run executes or was built by + // (§22.11): a part writes them beside its output and a reader requires + // its own. The running executable must be readable to be named. + let own_addresses = toolchain::Addresses::of( + &toolchain, + running_executable_sha256().map_err(|io_error| { + fail(internal(format!( + "reading the running executable: {io_error}" + ))) + })?, + ); let mut imported = partition::import(part.inputs(), &olean_root, part.publishes()).map_err(fail)?; - // Every record another part wrote was run by the toolchain this run's - // preflight hashed: the attestation lists them under its digests. - partition::check_toolchain( - &imported, - toolchain.lean.sha256, - toolchain.leanchecker.sha256, - ) - .map_err(fail)?; + // Every part before this one ran the toolchain this run's preflight + // hashed, and every record it wrote names it: the attestation lists them + // under its digests. + partition::check_toolchain(&imported, &own_addresses).map_err(fail)?; // The leanchecker identity (module documentation), before any replay: // the preflight's toolchain-relative path and digest line, then the @@ -1932,7 +1939,7 @@ pub fn run_part( .plan() .map(|plan| plan.elaborate.clone()) .unwrap_or_default(); - partition::export(staging.path(), output, &own).map_err(fail)?; + partition::export(staging.path(), output, &own, &own_addresses).map_err(fail)?; telemetry_run.succeeded(); return Ok(PartOutcome::Shard { output: (*output).to_owned(), diff --git a/crates/lexlean/src/verify/partition.rs b/crates/lexlean/src/verify/partition.rs index b50a1c205a..e887693394 100644 --- a/crates/lexlean/src/verify/partition.rs +++ b/crates/lexlean/src/verify/partition.rs @@ -22,9 +22,13 @@ use std::collections::{BTreeMap, BTreeSet}; use camino::{Utf8Path, Utf8PathBuf}; use crate::artifact::canonical_json::Json; -use crate::artifact::content_id::Sha256Digest; use crate::diagnostic::Diagnostic; use crate::verify::child::ChildRecord; +use crate::verify::toolchain::Addresses; + +/// The file of a part's output that names the addresses of the executables +/// the part ran and was built by (§22.11). +pub const TOOLCHAIN_FILE: &str = "toolchain.json"; /// Which modules one part elaborates, and which it replays and audits. /// @@ -116,6 +120,8 @@ pub struct Imported { pub leanchecker: BTreeMap, /// Audit records by audit member name. pub audit: BTreeMap, + /// The toolchain addresses each part named, by its output directory. + pub toolchains: Vec<(Utf8PathBuf, Addresses)>, } fn io(what: &str, error: &std::io::Error) -> Diagnostic { @@ -137,12 +143,15 @@ fn malformed(what: &str, reason: impl std::fmt::Display) -> Diagnostic { /// /// A module's `.olean` may come from one part only, and a record of one /// process from one part only: two parts that both wrote one is a plan that -/// ran a process twice, which is refused rather than resolved. +/// ran a process twice, which is refused rather than resolved. Every part +/// names the addresses of its toolchain ([`TOOLCHAIN_FILE`]), which +/// [`check_toolchain`] compares with the reader's own. /// /// # Errors /// -/// A part that cannot be read, a duplicate `.olean` or record, or a record -/// that does not parse as the verifier wrote it. +/// A part that cannot be read, a duplicate `.olean` or record, a record +/// that does not parse as the verifier wrote it, or a part without its +/// toolchain addresses. pub fn import( parts: &[Utf8PathBuf], olean_root: &Utf8Path, @@ -156,6 +165,22 @@ pub fn import( "the output directory of a part is required and is not a directory", )); } + let addresses_path = part.join(TOOLCHAIN_FILE); + let addresses = std::fs::read(addresses_path.as_std_path()) + .map_err(|error| { + malformed( + addresses_path.as_str(), + format!("a part must name the addresses of its toolchain: {error}"), + ) + }) + .and_then(|bytes| { + Json::parse(&bytes).map_err(|reason| malformed(addresses_path.as_str(), reason)) + }) + .and_then(|json| { + Addresses::from_json(&json) + .map_err(|reason| malformed(addresses_path.as_str(), reason)) + })?; + imported.toolchains.push((part.clone(), addresses)); let oleans = part.join("oleans"); if oleans.as_std_path().is_dir() { for entry in walkdir::WalkDir::new(oleans.as_std_path()) @@ -222,31 +247,49 @@ pub fn import( Ok(imported) } -/// Require every imported record to name the executable the join itself -/// runs: `lean` for an elaboration or an audit member, `leanchecker` for a -/// replay (§9.4, §22.9). +/// Require every part to name the toolchain this run's preflight hashed, and +/// every imported record to name the executable the join itself runs: +/// `lean` for an elaboration or an audit member, `leanchecker` for a replay +/// (§9.4, §22.9, §22.11). /// /// In a whole verification every record carries the digest of the toolchain /// the run's preflight hashed, by construction. Across the parts of a run it /// holds only because every job installs the same pinned toolchain, which is -/// a practice and not a check: a part whose `lean` or `leanchecker` differed -/// from the join's would otherwise contribute records the attestation then -/// lists under the join's toolchain digests. +/// a practice and not a check: a part whose `lean`, `lake`, `leanchecker`, +/// `lexlean`, or the `rustc` that built it differed from the reader's would +/// otherwise contribute records the attestation then lists under the +/// reader's toolchain. A record names only the executable it ran, so the +/// part's own addresses ([`TOOLCHAIN_FILE`]) are compared first, whole. /// /// # Errors /// -/// The first record, in the order elaborations, replays, audit members, and -/// by name within each, whose `executable_sha256` is not the join's digest of -/// the tool that produced it, naming the record and both digests. -pub fn check_toolchain( - imported: &Imported, - lean: Sha256Digest, - leanchecker: Sha256Digest, -) -> Result<(), Diagnostic> { +/// The first part, in the order given, whose addresses differ from `own`, +/// naming the part, the tool, and both addresses; then the first record, in +/// the order elaborations, replays, audit members, and by name within each, +/// whose `executable_sha256` is not the reader's digest of the tool that +/// produced it, naming the record and both digests. +pub fn check_toolchain(imported: &Imported, own: &Addresses) -> Result<(), Diagnostic> { + for (part, addresses) in &imported.toolchains { + if let Some((tool, theirs, mine)) = addresses.first_difference(own) { + return Err(malformed( + &format!("the part `{part}`"), + format!( + "it ran `{tool}` {}, but this run's `{tool}` is {}; every part of a run must run one toolchain", + theirs.to_hex(), + mine.to_hex() + ), + )); + } + } for (what, tool, expected, records) in [ - ("elaboration", "lean", lean, &imported.lean), - ("replay", "leanchecker", leanchecker, &imported.leanchecker), - ("audit", "lean", lean, &imported.audit), + ("elaboration", "lean", own.lean, &imported.lean), + ( + "replay", + "leanchecker", + own.leanchecker, + &imported.leanchecker, + ), + ("audit", "lean", own.lean, &imported.audit), ] { for (name, record) in records { if record.executable_sha256 != expected { @@ -314,7 +357,9 @@ fn read_records( /// intermediates (a later part's elaboration reads them), the elaboration /// and replay records, and the audit records. The `.olean` files the shard /// imported stay behind: they are the part that produced them's to export, -/// once. Nothing else leaves the staging tree, which is removed with it. +/// once. Beside them the part names the addresses of its toolchain +/// ([`TOOLCHAIN_FILE`]). Nothing else leaves the staging tree, which is +/// removed with it. /// /// # Errors /// @@ -323,6 +368,7 @@ pub fn export( staging: &std::path::Path, output: &Utf8Path, own: &BTreeSet, + addresses: &Addresses, ) -> Result<(), Diagnostic> { if output.as_std_path().exists() { let mut entries = std::fs::read_dir(output.as_std_path()) @@ -336,6 +382,11 @@ pub fn export( } std::fs::create_dir_all(output.as_std_path()) .map_err(|error| io("creating the part's output directory", &error))?; + std::fs::write( + output.join(TOOLCHAIN_FILE).as_std_path(), + addresses.to_json().to_file_bytes(), + ) + .map_err(|error| io("writing the part's toolchain addresses", &error))?; let oleans = staging.join("oleans"); if oleans.is_dir() { for entry in walkdir::WalkDir::new(&oleans).sort_by_file_name() { @@ -545,7 +596,15 @@ mod tests { let out = tempfile::tempdir().expect("tempdir"); let output = Utf8PathBuf::from_path_buf(out.path().join("part")).expect("utf8"); let own: BTreeSet = ["X.M".to_owned()].into_iter().collect(); - export(staging.path(), &output, &own).expect("exports"); + let digest = crate::artifact::content_id::Sha256Digest::of; + let addresses = Addresses { + lean: digest(b"lean"), + lake: digest(b"lake"), + leanchecker: digest(b"leanchecker"), + lexlean: digest(b"lexlean"), + rustc: digest(b"rustc"), + }; + export(staging.path(), &output, &own, &addresses).expect("exports"); assert!( staging.path().join("oleans/X/N.olean").exists() && !staging.path().join("oleans/X/M.olean").exists(), @@ -566,11 +625,58 @@ mod tests { assert!(imported.audit.contains_key("LexLeanAudit.X.M")); // The records name the join's `lean`; a join whose `lean` is another // executable refuses them, naming the record and both digests. - let lean = crate::artifact::content_id::Sha256Digest::of(b"lean"); - let other = crate::artifact::content_id::Sha256Digest::of(b"another lean"); - check_toolchain(&imported, lean, other).expect("the records name the join's lean"); - let refused = check_toolchain(&imported, other, other) - .expect_err("a record of another lean is refused"); + let lean = addresses.lean; + let other = digest(b"another lean"); + assert_eq!(imported.toolchains, vec![(output.clone(), addresses)]); + check_toolchain(&imported, &addresses).expect("the part and its records name the join's"); + // A part that ran another executable of any of the five is refused, + // naming the part, the tool, and both addresses, before any record. + for (tool, plant) in [ + ( + "lake", + Addresses { + lake: other, + ..addresses + }, + ), + ( + "lexlean", + Addresses { + lexlean: other, + ..addresses + }, + ), + ( + "rustc", + Addresses { + rustc: other, + ..addresses + }, + ), + ] { + let refused = check_toolchain(&imported, &plant) + .expect_err("a part of another toolchain is refused"); + let message = format!("{refused:?}"); + assert!( + message.contains("LLI9001") + && message.contains(&format!("ran `{tool}`")) + && message.contains(&other.to_hex()), + "{message}" + ); + } + // Records are compared against the reader's own `lean` once the part + // agrees: here the part's addresses are taken as the reader's, and + // the reader's `lean` differs from the one the record names. + let mut reader = imported; + reader.toolchains.clear(); + let refused = check_toolchain( + &reader, + &Addresses { + lean: other, + ..addresses + }, + ) + .expect_err("a record of another lean is refused"); let message = format!("{refused:?}"); assert!( message.contains("LLI9001") @@ -586,9 +692,21 @@ mod tests { let wrong = out.path().join("wrong/process/lean/X.N.json"); std::fs::create_dir_all(wrong.parent().expect("parent")).expect("mkdir"); std::fs::write(&wrong, record.to_json().to_file_bytes()).expect("write"); + std::fs::write( + out.path().join("wrong").join(TOOLCHAIN_FILE), + addresses.to_json().to_file_bytes(), + ) + .expect("write"); let wrong_part = Utf8PathBuf::from_path_buf(out.path().join("wrong")).expect("utf8"); let fresh = tempfile::tempdir().expect("tempdir"); let fresh_root = Utf8PathBuf::from_path_buf(fresh.path().join("oleans")).expect("utf8"); assert!(import(std::slice::from_ref(&wrong_part), &fresh_root, true).is_err()); + // A part that names no toolchain addresses is refused. + std::fs::remove_file(output.join(TOOLCHAIN_FILE)).expect("remove"); + let bare = tempfile::tempdir().expect("tempdir"); + let bare_root = Utf8PathBuf::from_path_buf(bare.path().join("oleans")).expect("utf8"); + let refused = import(std::slice::from_ref(&output), &bare_root, true) + .expect_err("a part without toolchain addresses"); + assert!(format!("{refused:?}").contains("addresses of its toolchain")); } } diff --git a/crates/lexlean/src/verify/toolchain.rs b/crates/lexlean/src/verify/toolchain.rs index 0aae60e21b..ad2ba98784 100644 --- a/crates/lexlean/src/verify/toolchain.rs +++ b/crates/lexlean/src/verify/toolchain.rs @@ -34,6 +34,117 @@ pub struct Toolchain { pub leanchecker: Tool, } +/// The format of [`Addresses`] as a part of a verification writes it. +pub const ADDRESSES_SPEC: &str = "lexlean/verify-toolchain/1"; + +/// The addresses of every executable a verification ran or was built by +/// (SPEC.md §22.11): `lean`, `lake`, and `leanchecker` as the preflight +/// hashed them, the running `lexlean`, and the `rustc` that compiled it. +/// Two parts of one verification agree on all five or neither trusts the +/// other's records. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Addresses { + /// `lean`. + pub lean: Sha256Digest, + /// `lake`. + pub lake: Sha256Digest, + /// `leanchecker`. + pub leanchecker: Sha256Digest, + /// The running `lexlean` executable. + pub lexlean: Sha256Digest, + /// The `rustc` that compiled this `lexlean` (its executable and driver + /// library, [`crate::build_identity::RUSTC_SHA256`]). + pub rustc: Sha256Digest, +} + +impl Addresses { + /// The addresses of this preflighted toolchain, the running executable + /// whose digest is `lexlean`, and the compiler that built it. + #[must_use] + pub fn of(toolchain: &Toolchain, lexlean: Sha256Digest) -> Self { + Self { + lean: toolchain.lean.sha256, + lake: toolchain.lake.sha256, + leanchecker: toolchain.leanchecker.sha256, + lexlean, + rustc: Sha256Digest::from_hex(crate::build_identity::RUSTC_SHA256) + .expect("the build script writes a SHA-256"), + } + } + + /// Every tool with its address, in name order. + #[must_use] + pub fn tools(&self) -> [(&'static str, Sha256Digest); 5] { + [ + ("lake", self.lake), + ("lean", self.lean), + ("leanchecker", self.leanchecker), + ("lexlean", self.lexlean), + ("rustc", self.rustc), + ] + } + + /// The canonical JSON form. + #[must_use] + pub fn to_json(&self) -> crate::artifact::canonical_json::Json { + use crate::artifact::canonical_json::Json; + let mut pairs = vec![("spec", Json::Str(ADDRESSES_SPEC.to_owned()))]; + for (tool, address) in self.tools() { + pairs.push((tool, Json::Str(address.to_hex()))); + } + Json::object(pairs) + } + + /// Read the canonical JSON form, refusing anything else: another + /// format, a missing or malformed address, or an unknown field. + /// + /// # Errors + /// + /// What is wrong with `json`. + pub fn from_json(json: &crate::artifact::canonical_json::Json) -> Result { + use crate::artifact::canonical_json::Json; + let Json::Obj(fields) = json else { + return Err("the toolchain addresses are an object".to_owned()); + }; + if fields.get("spec") != Some(&Json::Str(ADDRESSES_SPEC.to_owned())) { + return Err(format!("`spec` must be `{ADDRESSES_SPEC}`")); + } + let address = |tool: &str| match fields.get(tool) { + Some(Json::Str(hex)) => Sha256Digest::from_hex(hex) + .map_err(|reason| format!("the address of `{tool}`: {reason}")), + _ => Err(format!("the address of `{tool}` is required")), + }; + let addresses = Self { + lean: address("lean")?, + lake: address("lake")?, + leanchecker: address("leanchecker")?, + lexlean: address("lexlean")?, + rustc: address("rustc")?, + }; + if let Some(unknown) = fields + .keys() + .find(|key| *key != "spec" && !addresses.tools().iter().any(|(tool, _)| tool == key)) + { + return Err(format!("unknown field `{unknown}`")); + } + Ok(addresses) + } + + /// The first tool whose address differs between `self` and `other`, with + /// both addresses, in name order. + #[must_use] + pub fn first_difference( + &self, + other: &Self, + ) -> Option<(&'static str, Sha256Digest, Sha256Digest)> { + self.tools() + .into_iter() + .zip(other.tools()) + .find(|((_, mine), (_, theirs))| mine != theirs) + .map(|((tool, mine), (_, theirs))| (tool, mine, theirs)) + } +} + fn environment(message: impl Into) -> Diagnostic { Diagnostic::new(code!("LLV7001"), message) } diff --git a/features/suites/verification.feature b/features/suites/verification.feature index 98b176bec9..b222b31d9f 100644 --- a/features/suites/verification.feature +++ b/features/suites/verification.feature @@ -162,9 +162,10 @@ Feature: verification And a verified directory in which a telemetry key is planted is refused @VR-23 @build - Scenario: A project verified as the parts of one run, each shard running the processes its plan names with the oleans of the shards it needs and the join reading every shard's records, publishes a verified directory byte-identical to the whole verification's with the same attestation identity, and a shard whose plan leaves a module without its imports, a join missing a record, or a join reading a record of an executable other than its own, is refused. + Scenario: A project verified as the parts of one run, each shard running the processes its plan names with the oleans of the shards it needs and the join reading every shard's records, publishes a verified directory byte-identical to the whole verification's with the same attestation identity, and a shard whose plan leaves a module without its imports, a join missing a record, a join reading a record of an executable other than its own, or a part that ran a toolchain other than its own, is refused. Given a committed multi-module example project and a plan of two shards and a join over its import graph When it is verified whole and as the parts of one run Then the join publishes a verified directory byte-identical to the whole verification's, with the same attestation identity And a shard whose plan elaborates a module before its imports, and a join that is handed one shard too few, are refused + And a join reading a part whose toolchain addresses name another lake is refused And a join reading a replay record whose executable digest is not its own leanchecker's is refused diff --git a/model/ids.toml b/model/ids.toml index 7a3ca502f5..129e540642 100644 --- a/model/ids.toml +++ b/model/ids.toml @@ -1349,7 +1349,7 @@ statement = "Performance telemetry is written only to the CI evidence directory id = "VR-23" level = "build" suite = "verification" -statement = "A project verified as the parts of one run, each shard running the processes its plan names with the oleans of the shards it needs and the join reading every shard's records, publishes a verified directory byte-identical to the whole verification's with the same attestation identity, and a shard whose plan leaves a module without its imports, a join missing a record, or a join reading a record of an executable other than its own, is refused." +statement = "A project verified as the parts of one run, each shard running the processes its plan names with the oleans of the shards it needs and the join reading every shard's records, publishes a verified directory byte-identical to the whole verification's with the same attestation identity, and a shard whose plan leaves a module without its imports, a join missing a record, a join reading a record of an executable other than its own, or a part that ran a toolchain other than its own, is refused." [[id]] id = "CL-01" From 8f63545abcd0d34cb9d57048843a9f426a55fe0e Mon Sep 17 00:00:00 2001 From: humuhumu33 Date: Thu, 12 Nov 2026 15:21:03 +0000 Subject: [PATCH 05/22] BS-72: the reasoning and text declarations and whole semantic modules as source MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The logic, inference rule, verifier, reasoner, tokenizer, context window, text generator and text classifier declarations (SPEC.md §17.12) with the eleven types they hold, and SemanticModule of declarations of every kind, as language-1.2 source in bootstrap/, eighth slice of the typed IR (#269): reasoningDeclarationText and semanticModuleText of SemanticReasoning, held to the canonical JSON of their serde form for every committed semantic module. Generated with the case encoders and examples from one description of the oracle types, as BS-71 is; 21 planted defects. Register row (501), SPEC §31 row and §17.23 prose, scenario, bdd part and lane, README count and range, CHANGELOG, VERIFICATION record, roots, the register-count mirrors; BS-71 case helpers made pub(super). 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 | 6 + README.md | 4 +- SPEC.md | 16 +- VERIFICATION.md | 35 + bootstrap/README.md | 3 + bootstrap/lexlean.lock | 2 +- bootstrap/lexlean.toml | 2 +- bootstrap/src/SemanticReasoning.lex.tex | 462 +++++++ crates/conformance/src/cases/bootstrap.rs | 2 + .../src/cases/bootstrap/semantic_decls.rs | 4 +- .../src/cases/bootstrap/semantic_models.rs | 89 +- .../src/cases/bootstrap/semantic_reasoning.rs | 1175 +++++++++++++++++ crates/conformance/src/cases/repository.rs | 2 +- crates/conformance/tests/conformance.rs | 5 + features/suites/bootstrap.feature | 6 + model/ids.toml | 6 + tests/production-export/bootstrap/roots.json | 16 + xtask/src/spec_links.rs | 4 +- 21 files changed, 1798 insertions(+), 55 deletions(-) create mode 100755 bootstrap/src/SemanticReasoning.lex.tex create mode 100755 crates/conformance/src/cases/bootstrap/semantic_reasoning.rs diff --git a/.github/bdd-parts.json b/.github/bdd-parts.json index b360492e62..4ce6f4315e 100644 --- a/.github/bdd-parts.json +++ b/.github/bdd-parts.json @@ -298,6 +298,9 @@ ], "bootstrap-semantic-models": [ "BS-71" + ], + "bootstrap-semantic-reasoning": [ + "BS-72" ] }, "targets": { diff --git a/.github/intel-host-lanes.json b/.github/intel-host-lanes.json index fe339fa80b..e71e2d7bb8 100644 --- a/.github/intel-host-lanes.json +++ b/.github/intel-host-lanes.json @@ -2,7 +2,7 @@ "producer": ["honesty"], "lanes": { "lower": ["bootstrap-lower", "bootstrap-lexicon-lse", "bootstrap-lexicon-lre", "bootstrap-surface-module", "bootstrap-diagnostics", "bootstrap-cli-output"], - "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", "bootstrap-ir-term", "bootstrap-ir-proof", "bootstrap-ir-document", "bootstrap-semantic-types", "bootstrap-semantic-terms", "bootstrap-semantic-decls", "bootstrap-semantic-models"], + "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", "bootstrap-ir-term", "bootstrap-ir-proof", "bootstrap-ir-document", "bootstrap-semantic-types", "bootstrap-semantic-terms", "bootstrap-semantic-decls", "bootstrap-semantic-models", "bootstrap-semantic-reasoning"], "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 7085cc98b2..fc3b3b8d9a 100644 --- a/.github/vv-rows.json +++ b/.github/vv-rows.json @@ -421,5 +421,14 @@ "width": "", "memory_kib": "", "hints": "" + }, + { + "part": "bdd-bootstrap-semantic-reasoning", + "recipes": "bdd", + "examples_part": "", + "bdd_part": "bootstrap-semantic-reasoning", + "width": "", + "memory_kib": "", + "hints": "" } ] diff --git a/CHANGELOG.md b/CHANGELOG.md index 79eb13bc26..c0dab92b84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,12 @@ workspace carried at those versions without a tag. ## Unreleased +- The declarations of reasoning and text components, and semantic modules of every + kind, as language-1.2 source, eighth slice of the typed IR (SPEC.md §17.23, §17.12, + `BS-72`, #269; no new diagnostic code, no change to the language-1.2 identity). + `reasoningDeclarationText` and `semanticModuleText` of `SemanticReasoning` return the + canonical JSON of the serde form of those declarations and of `SemanticModule`, for + every committed semantic module. The register is 501 IDs. - The declarations of artifacts, contracts, realizations, evidence, and models as language-1.2 source, seventh slice of the typed IR (SPEC.md §17.23, §17.12, `BS-71`, #269; no new diagnostic code, no change to the language-1.2 identity). diff --git a/README.md b/README.md index d8a6cdee2d..8785e916dc 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 500 registered conformance IDs have their named tests; `just vv` checks them with the pinned toolchain installed. +All 501 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, the build manifest, the linked term IR in its canonical JSON, the proof and declaration IR in theirs with the axiom policy check, the document IR, the linked project, and the semantic ID, the closed types of the semantic module language, its terms and proofs, its ordinary declarations and modules, and its model declarations, 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-71` | `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, the build manifest, the linked term IR in its canonical JSON, the proof and declaration IR in theirs with the axiom policy check, the document IR, the linked project, and the semantic ID, the closed types of the semantic module language, its terms and proofs, its ordinary declarations and modules, its model declarations, and its reasoning and text declarations and modules of every kind, 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-72` | `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 be04d6f389..20c7baa2a8 100644 --- a/SPEC.md +++ b/SPEC.md @@ -9125,6 +9125,19 @@ None of the twenty types these declarations hold refers to itself, so each is on one writer, and a list of them is a list. The module, the case's encoders, and the case's examples are generated from one description of the oracle's types. The root admits `recursion` through the term and JSON writers. Reading declarations from JSON stays in the host. +The declarations of reasoning and text components (§17.12), and the semantic module of +declarations of every kind, are the eighth component of the typed IR as source (`BS-72`, issue +269), the module `SemanticReasoning` over `SemanticDecls`, `SemanticModels`, `SemanticTerms`, +and `SemanticTypes`, with two production roots for `rust-std`: + +| Root | Takes | Returns | Oracle | +| --- | --- | --- | --- | +| `LexLeanBoot.SemanticReasoning.reasoningDeclarationText` | a logic, inference rule, verifier, reasoner, tokenizer, context window, text generator, or text classifier | its canonical JSON as the linked IR embeds it | the serde form of those variants of `ir::semantic::SemanticDeclaration` | +| `LexLeanBoot.SemanticReasoning.semanticModuleText` | a specification and declarations of every kind | the module's canonical JSON | the serde form of `ir::semantic::SemanticModule` | + +With this module every semantic module the linked IR embeds is written by source, and the module +and the case's encoders and examples are generated from one description of the oracle's types. +Both roots admit `recursion`. Reading declarations and modules from JSON stays in the host. The authoring notation of a tooling module (§17.20, §17.21) is the fifth component of the front end (`BS-27`, issue 269), the module `SurfaceTooling` over the generic notation of @@ -12180,6 +12193,7 @@ Every row below is normative, has honesty level `build`, and MUST be copied byte | `BS-69` | `bootstrap` | The closed terms and proofs of the semantic module language of SPEC.md §17.11 and §17.12 (`ir::semantic::SemanticTerm` and `SemanticProof` in the serde form the linked IR embeds, written as canonical JSON: each term and proof tagged by its `kind`, its member references, assignments, branches, binders, map entries, graph edges, model checks, and reflections in their fields, a list of type arguments, definitions, generalized names, or premises left out when it is empty where the oracle skips it, and its types through `SemanticTypes`) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticTerms`: `termText` and `proofText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 terms and proofs, which reach every constructor and every optional list both empty and not, on every distinct term and proof of every committed semantic module that links, and on seeded terms and proofs; each planted defect in the source is detected by the root and the one vector it names, and the certified packages the production export publishes for the project are the committed ones; reading terms and proofs from JSON is outside the claim. | §17.11, §17.12, §17.23 | | `BS-70` | `bootstrap` | The ordinary declarations of the semantic module language of SPEC.md §17.11 and §17.12 and the module that holds them (`ir::semantic::SemanticDeclaration` for structures, classes, instances, inductives, definitions, and theorems, and `SemanticModule`, in the serde form the linked IR embeds, written as canonical JSON: each declaration tagged by its `kind`, its parameters, fields, constructors, and assignments in order, and a definition's or a theorem's type parameters, recursive argument, axioms, executable flag, mutual group, termination, and production, and an inductive's mutual group, left out exactly where the oracle's serde attributes skip them; and a module's specification and declarations in order) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticDecls` over `SemanticTerms` and `SemanticTypes`: `declarationText` and `moduleText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 on the shorter modules, agree with the oracle to the byte on hand-built declarations, which reach every kind and every optional member both present and absent, on every distinct ordinary declaration of every committed semantic module that links and on every such module whose declarations are all ordinary, among them every module of the `bootstrap/` project and the standard library, and on seeded declarations; each planted defect in the source is detected by the root and the one vector it names, and the certified packages the production export publishes for the project are the committed ones; the declarations of models, reasoning, and text components, and reading declarations from JSON, are outside the claim. | §17.11, §17.12, §17.23 | | `BS-71` | `bootstrap` | The declarations of artifacts, contracts, realizations, evidence, and models of the semantic module language of SPEC.md §17.12 (those variants of `ir::semantic::SemanticDeclaration`, with the artifact roles and schemas, binders, states, validators, uses, rules, the deterministic, rule, statistical, neural, and composite realization descriptors with their layers, decoders, forms, and junctions, and the evidence claims they hold, in the serde form the linked IR embeds, written as canonical JSON: each tagged variant by its `kind`, each closed name as the oracle serializes it, and each optional member left out exactly where the oracle's serde attributes skip it) is language-1.2 source in the `bootstrap/` project, as the production root `modelDeclarationText` of `SemanticModels` over `SemanticTerms` and `SemanticTypes`, generated with the case's encoders and examples from one description of the oracle's types, 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 examples that reach every variant, every optional member present and absent, and every list empty and not, on every distinct such declaration of every committed semantic module that links, and on seeded examples; each planted defect, a misspelled kind or name, is detected by the first example that holds it, and the certified packages the production export publishes for the project are the committed ones; reading declarations from JSON is outside the claim. | §17.12, §17.23 | +| `BS-72` | `bootstrap` | The declarations of logics, inference rules, verifiers, reasoners, tokenizers, context windows, text generators, and text classifiers of the semantic module language of SPEC.md §17.12, and the semantic module of declarations of every kind (those variants of `ir::semantic::SemanticDeclaration`, with the invariants, bindings, answers, search orders, strategies, claims, tokenizer schemes, overflow dispositions, and generation policies they hold, and `SemanticModule`, in the serde form the linked IR embeds, written as canonical JSON: each tagged variant by its `kind`, each closed name as the oracle serializes it, each optional member left out exactly where the oracle's serde attributes skip it, and a module's specification and declarations in order, its ordinary declarations through `SemanticDecls` and its model declarations through `SemanticModels`) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticReasoning`, generated with the case's encoders and examples from one description of the oracle's types: `reasoningDeclarationText` and `semanticModuleText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 on the shorter modules, agree with the oracle to the byte on examples that reach every variant, every optional member present and absent, and every list empty and not, on every distinct such declaration and every distinct semantic module of every committed project that links, and on seeded examples; each planted defect, a misspelled kind or name, is detected by the first example that holds it, and the certified packages the production export publishes for the project are the committed ones; reading declarations and modules from JSON is outside the claim. | §17.12, §17.23 | | `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 | @@ -12196,7 +12210,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:** 500. +**Total required capability IDs:** 501. 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 48bd61c29b..c404657824 100644 --- a/VERIFICATION.md +++ b/VERIFICATION.md @@ -11949,6 +11949,41 @@ Measured with the compiler binary of this tree's compiler sources: `lexlean check` of the `bootstrap/` project passes, with the root eligible for `rust-std` with exactly its declared effects, and `lexlean fmt --check` passes. +### the reasoning declarations and whole semantic modules are source (BS-72) + +`BS-72` holds the two production roots of `LexLeanBoot.SemanticReasoning` +(`bootstrap/src/SemanticReasoning.lex.tex`) to `lexlean::ir::semantic`: +`reasoningDeclarationText` to the canonical JSON of the serde form of the +logic, inference rule, verifier, reasoner, tokenizer, context window, text +generator, and text classifier variants of `SemanticDeclaration`, and +`semanticModuleText` to that of `SemanticModule`, as `DocumentModule::to_json` +embeds a semantic module. The module declares the eleven types those +declarations hold and the declarations themselves, each as one inductive with +one writer, over the uses, binders, and states of `SemanticModels` (`BS-71`), the +terms of `SemanticTerms` (`BS-69`), and the types of `SemanticTypes` (`BS-68`), and +a declaration of any kind, written through `SemanticDecls` (`BS-70`), +`SemanticModels`, or this module. With it every semantic module the linked IR +embeds is written by source. The module, the case's encoders, and the case's +example builders are generated from one description of the oracle's types, as +`BS-71`'s are. Both roots admit exactly `allocation`, `overflow`, and +`recursion`. The oracle is the existing public code of `semantic.rs` and its +serde derivations; no Rust outside the case changes. + +The case holds `reasoningDeclarationText` to the oracle on 150 examples whose +step's digits choose every variant at every depth, each optional member present +and absent and each list empty and not, on every distinct such declaration of +every committed semantic module that links, and on seeded examples; and +`semanticModuleText` on every distinct semantic module of every committed +project that links, the evaluator taking those whose JSON is at most 262,144 +bytes and the rendered package all of them. Each planted defect misspells one +serialized kind or closed name, and the first example that holds it detects it; +the case asserts that the site of each plant occurs once. The row's statement +carries no counts. + +Measured with the compiler binary of this tree's compiler sources: `lexlean +check` of the `bootstrap/` project passes, with each root eligible for `rust-std` +with exactly its declared effects, and `lexlean fmt --check` passes. + ## The `bdd` recipe as the parts of one run (Issue #131) With UOR Atlas verified as the parts of one run, the `rest` leg became the diff --git a/bootstrap/README.md b/bootstrap/README.md index 43072559df..166070a862 100644 --- a/bootstrap/README.md +++ b/bootstrap/README.md @@ -76,3 +76,6 @@ semantic module language and its modules as the linked IR embeds them. `src/SemanticModels.lex.tex` (SPEC.md §17.23, `BS-71`) imports `SemanticTerms`, `SemanticTypes`, and the standard library's `Json` and `Order`, and writes the declarations of artifacts, contracts, realizations, evidence, and models as the linked IR embeds them. +`src/SemanticReasoning.lex.tex` (SPEC.md §17.23, `BS-72`) imports `SemanticDecls`, `SemanticModels`, +`SemanticTerms`, `SemanticTypes`, and the standard library's `Json` and `Order`, and writes the +declarations of reasoning and text components and semantic modules of every kind. diff --git a/bootstrap/lexlean.lock b/bootstrap/lexlean.lock index 5acde89bb2..b58107830e 100644 --- a/bootstrap/lexlean.lock +++ b/bootstrap/lexlean.lock @@ -1,7 +1,7 @@ spec = "lexlean/lock/2" language = "1.2" compiler_semantics = "635a25acb886587f8777525e778092e9599d1f2df26da59403ee39e9e299151a" -project_config_sha256 = "df6d82ce9eab8856a204ffcc661add576602655ec005140c2e9f9fab0045ad01" +project_config_sha256 = "a70abffbcaf11f4e2ff847cc53d831bf5d295570d380e0f5dbc0f946ff44fa4e" lean_toolchain = "leanprover/lean4:v4.32.1" [[workspace_file]] diff --git a/bootstrap/lexlean.toml b/bootstrap/lexlean.toml index 792918d13a..29177bbb13 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/IrDocument.lex.tex", "src/IrProof.lex.tex", "src/IrTerm.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/SemanticDecls.lex.tex", "src/SemanticModels.lex.tex", "src/SemanticTerms.lex.tex", "src/SemanticTypes.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/IrDocument.lex.tex", "src/IrProof.lex.tex", "src/IrTerm.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/SemanticDecls.lex.tex", "src/SemanticModels.lex.tex", "src/SemanticReasoning.lex.tex", "src/SemanticTerms.lex.tex", "src/SemanticTypes.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/SemanticReasoning.lex.tex b/bootstrap/src/SemanticReasoning.lex.tex new file mode 100755 index 0000000000..63dc582726 --- /dev/null +++ b/bootstrap/src/SemanticReasoning.lex.tex @@ -0,0 +1,462 @@ +\begin{lexlean}{SemanticReasoning} +\useglossary{lexlean.std.nat@1.2.0} +\importmodule{SemanticDecls} +\importmodule{SemanticModels} +\importmodule{SemanticTerms} +\importmodule{SemanticTypes} +\importmodule{LexLeanStd.Json} +\importmodule{LexLeanStd.Order} +\title{Natural number addition} +\begin{semanticmodule} +inductive{ + name: LogicInvariant, + type_parameters: [], + parameters: [], + constructors: [ + {name: LogicInvariantOf, fields: [((Option Bytes) * Bytes), ((Option Bytes) * Bytes)]} + ] +} + +inductive{ + name: RuleBinding, + type_parameters: [], + parameters: [], + constructors: [ + {name: RuleBindingOf, fields: [Bytes, SemanticTypes::SemanticType, SemanticTerms::STerm]} + ] +} + +inductive{ + name: ReasoningAnswer, + type_parameters: [], + parameters: [], + constructors: [ + {name: ReasoningAnswerOf, fields: [Bytes, SemanticTypes::SemanticType, SemanticTerms::STerm]} + ] +} + +inductive{ + name: SearchOrder, + type_parameters: [], + parameters: [], + constructors: [ + {name: SearchOrderBreadthFirst, fields: []}, + {name: SearchOrderDepthFirst, fields: []} + ] +} + +inductive{ + name: ReasoningStrategy, + type_parameters: [], + parameters: [], + constructors: [ + {name: ReasoningStrategyForward, fields: [(Option SemanticTerms::STerm)]}, + { + name: ReasoningStrategySearch, + fields: [SearchOrder, (Option SemanticTerms::STerm), (Option SemanticTerms::STerm), Bool] + }, + { + name: ReasoningStrategyGenerateAndVerify, + fields: [SemanticTerms::STerm, (Option SemanticTerms::STerm)] + } + ] +} + +inductive{ + name: ReasoningClaim, + type_parameters: [], + parameters: [], + constructors: [ + {name: ReasoningClaimInitialInvariant, fields: [((Option Bytes) * Bytes)]}, + {name: ReasoningClaimTerminates, fields: [((Option Bytes) * Bytes)]}, + { + name: ReasoningClaimObservationInvariant, + fields: [((Option Bytes) * Bytes), ((Option Bytes) * Bytes), ((Option Bytes) * Bytes)] + }, + {name: ReasoningClaimAnswerCorrect, fields: [((Option Bytes) * Bytes)]} + ] +} + +inductive{ + name: TokenizerScheme, + type_parameters: [], + parameters: [], + constructors: [ + {name: TokenizerSchemeByteLevel, fields: []}, + {name: TokenizerSchemeLongestMatch, fields: []} + ] +} + +inductive{ + name: OverflowDisposition, + type_parameters: [], + parameters: [], + constructors: [ + {name: OverflowDispositionRefuse, fields: []}, + {name: OverflowDispositionKeepLast, fields: []} + ] +} + +inductive{ + name: SamplingGenerator, + type_parameters: [], + parameters: [], + constructors: [{name: SamplingGeneratorXorshift32, fields: []}] +} + +inductive{ + name: SamplingDistribution, + type_parameters: [], + parameters: [], + constructors: [{name: SamplingDistributionProportional, fields: []}] +} + +inductive{ + name: GenerationPolicy, + type_parameters: [], + parameters: [], + constructors: [ + {name: GenerationPolicyGreedy, fields: []}, + {name: GenerationPolicySample, fields: [SamplingGenerator, SamplingDistribution]} + ] +} + +inductive{ + name: ReasoningDeclaration, + type_parameters: [], + parameters: [], + constructors: [ + { + name: ReasoningDeclarationLogic, + fields: [ + Bytes, + (List Bytes), + SemanticModels::ContractState, + ((Option Bytes) * Bytes), + (Option LogicInvariant), + (Option ((Option Bytes) * Bytes)), + (List Bytes) + ] + }, + { + name: ReasoningDeclarationInferenceRule, + fields: [ + Bytes, + (List Bytes), + SemanticModels::ModelUse, + (Option RuleBinding), + SemanticTerms::STerm, + SemanticTerms::STerm, + ((Option Bytes) * Bytes), + (Option ((Option Bytes) * Bytes)), + Bool, + (List Bytes) + ] + }, + { + name: ReasoningDeclarationVerifier, + fields: [ + Bytes, + (List Bytes), + SemanticModels::ModelBinder, + SemanticModels::ModelBinder, + ((Option Bytes) * Bytes), + ((Option Bytes) * Bytes), + ((Option Bytes) * Bytes), + (Option ((Option Bytes) * Bytes)), + (List Bytes) + ] + }, + { + name: ReasoningDeclarationReasoner, + fields: [ + Bytes, + (List Bytes), + (Option SemanticModels::ModelUse), + SemanticModels::ModelBinder, + (Option SemanticTerms::STerm), + (List SemanticModels::ModelUse), + ReasoningStrategy, + (Option ReasoningAnswer), + SemanticModels::ModelUse, + (List ReasoningClaim), + Bool, + (List Bytes) + ] + }, + { + name: ReasoningDeclarationTokenizer, + fields: [Bytes, (List Bytes), TokenizerScheme, SemanticModels::ModelUse, (List Bytes)] + }, + { + name: ReasoningDeclarationContextWindow, + fields: [Bytes, Nat, OverflowDisposition, (List Bytes)] + }, + { + name: ReasoningDeclarationTextGenerator, + fields: [ + Bytes, + ((Option Bytes) * Bytes), + ((Option Bytes) * Bytes), + SemanticModels::ModelUse, + Nat, + ((Option Bytes) * Bytes), + GenerationPolicy, + (List Bytes) + ] + }, + { + name: ReasoningDeclarationTextClassifier, + fields: [Bytes, ((Option Bytes) * Bytes), SemanticModels::ModelUse, (List Bytes)] + } + ] +} + +definition{ + name: lit, + parameters: [literal : String], + result: Bytes, + body: (prim utf8_encode literal : Bytes), + executable: true +} + +definition{ + name: objectOf, + parameters: [entries : (List (Bytes * LexLeanStd.Json::Json))], + result: LexLeanStd.Json::Json, + body: (ctor LexLeanStd.Json::Json.JObj (prim list_fold (fun (rest : LexLeanStd.Json::Json) (entry : (Bytes * LexLeanStd.Json::Json)) => (ctor LexLeanStd.Json::Json.JMember (first entry) (second entry) rest)) (ctor LexLeanStd.Json::Json.JNil) entries : LexLeanStd.Json::Json)), + executable: true +} + +definition{ + name: logicInvariantJson, + parameters: [value : LogicInvariant], + result: LexLeanStd.Json::Json, + body: (match value + | LogicInvariant.LogicInvariantOf part0 part1 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "predicate"), (SemanticTerms::memberJson (first part0) (second part0))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "preserves"), (SemanticTerms::memberJson (first part1) (second part1))) entries1); (objectOf entries2)))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: ruleBindingJson, + parameters: [value : RuleBinding], + result: LexLeanStd.Json::Json, + body: (match value + | RuleBinding.RuleBindingOf part0 part1 part2 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "name"), (ctor LexLeanStd.Json::Json.JStr part0)) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "type"), (SemanticTypes::typeJson part1)) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "candidates"), (SemanticTerms::termJson part2)) entries2); (objectOf entries3))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: reasoningAnswerJson, + parameters: [value : ReasoningAnswer], + result: LexLeanStd.Json::Json, + body: (match value + | ReasoningAnswer.ReasoningAnswerOf part0 part1 part2 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "name"), (ctor LexLeanStd.Json::Json.JStr part0)) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "type"), (SemanticTypes::typeJson part1)) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "value"), (SemanticTerms::termJson part2)) entries2); (objectOf entries3))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: searchOrderJson, + parameters: [value : SearchOrder], + result: LexLeanStd.Json::Json, + body: (match value + | SearchOrder.SearchOrderBreadthFirst => (ctor LexLeanStd.Json::Json.JStr (lit "breadth_first")) + | SearchOrder.SearchOrderDepthFirst => (ctor LexLeanStd.Json::Json.JStr (lit "depth_first"))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: reasoningStrategyJson, + parameters: [value : ReasoningStrategy], + result: LexLeanStd.Json::Json, + body: (match value + | ReasoningStrategy.ReasoningStrategyForward part0 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "forward"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (match part0 + | Option.some present => (cons ((lit "fuel"), (SemanticTerms::termJson present)) entries1) + | Option.none => entries1); (objectOf entries2))) + | ReasoningStrategy.ReasoningStrategySearch part0 part1 part2 part3 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "search"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "order"), (searchOrderJson part0)) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (match part1 + | Option.some present => (cons ((lit "fuel"), (SemanticTerms::termJson present)) entries2) + | Option.none => entries2); (let entries4 : (List (Bytes * LexLeanStd.Json::Json)) := (match part2 + | Option.some present => (cons ((lit "frontier"), (SemanticTerms::termJson present)) entries3) + | Option.none => entries3); (let entries5 : (List (Bytes * LexLeanStd.Json::Json)) := (if part3 then (cons ((lit "deduplicate"), (ctor LexLeanStd.Json::Json.JBool true)) entries4) else entries4); (objectOf entries5)))))) + | ReasoningStrategy.ReasoningStrategyGenerateAndVerify part0 part1 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "generate_and_verify"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "generator"), (SemanticTerms::termJson part0)) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (match part1 + | Option.some present => (cons ((lit "budget"), (SemanticTerms::termJson present)) entries2) + | Option.none => entries2); (objectOf entries3))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: reasoningClaimJson, + parameters: [value : ReasoningClaim], + result: LexLeanStd.Json::Json, + body: (match value + | ReasoningClaim.ReasoningClaimInitialInvariant part0 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "initial_invariant"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "theorem"), (SemanticTerms::memberJson (first part0) (second part0))) entries1); (objectOf entries2))) + | ReasoningClaim.ReasoningClaimTerminates part0 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "terminates"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "theorem"), (SemanticTerms::memberJson (first part0) (second part0))) entries1); (objectOf entries2))) + | ReasoningClaim.ReasoningClaimObservationInvariant part0 part1 part2 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "observation_invariant"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "predicate"), (SemanticTerms::memberJson (first part0) (second part0))) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "initial"), (SemanticTerms::memberJson (first part1) (second part1))) entries2); (let entries4 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "preserved"), (SemanticTerms::memberJson (first part2) (second part2))) entries3); (objectOf entries4))))) + | ReasoningClaim.ReasoningClaimAnswerCorrect part0 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "answer_correct"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "theorem"), (SemanticTerms::memberJson (first part0) (second part0))) entries1); (objectOf entries2)))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: tokenizerSchemeJson, + parameters: [value : TokenizerScheme], + result: LexLeanStd.Json::Json, + body: (match value + | TokenizerScheme.TokenizerSchemeByteLevel => (ctor LexLeanStd.Json::Json.JStr (lit "byte_level")) + | TokenizerScheme.TokenizerSchemeLongestMatch => (ctor LexLeanStd.Json::Json.JStr (lit "longest_match"))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: overflowDispositionJson, + parameters: [value : OverflowDisposition], + result: LexLeanStd.Json::Json, + body: (match value + | OverflowDisposition.OverflowDispositionRefuse => (ctor LexLeanStd.Json::Json.JStr (lit "refuse")) + | OverflowDisposition.OverflowDispositionKeepLast => (ctor LexLeanStd.Json::Json.JStr (lit "keep_last"))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: samplingGeneratorJson, + parameters: [value : SamplingGenerator], + result: LexLeanStd.Json::Json, + body: (match value + | SamplingGenerator.SamplingGeneratorXorshift32 => (ctor LexLeanStd.Json::Json.JStr (lit "xorshift32"))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: samplingDistributionJson, + parameters: [value : SamplingDistribution], + result: LexLeanStd.Json::Json, + body: (match value + | SamplingDistribution.SamplingDistributionProportional => (ctor LexLeanStd.Json::Json.JStr (lit "proportional"))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: generationPolicyJson, + parameters: [value : GenerationPolicy], + result: LexLeanStd.Json::Json, + body: (match value + | GenerationPolicy.GenerationPolicyGreedy => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "greedy"))) (nil (Bytes * LexLeanStd.Json::Json))); (objectOf entries1)) + | GenerationPolicy.GenerationPolicySample part0 part1 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "sample"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "generator"), (samplingGeneratorJson part0)) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "distribution"), (samplingDistributionJson part1)) entries2); (objectOf entries3))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: reasoningDeclarationJson, + parameters: [value : ReasoningDeclaration], + result: LexLeanStd.Json::Json, + body: (match value + | ReasoningDeclaration.ReasoningDeclarationLogic part0 part1 part2 part3 part4 part5 part6 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "logic"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "name"), (ctor LexLeanStd.Json::Json.JStr part0)) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (match part1 + | List.nil => entries2 + | List.cons head tail => (cons ((lit "type_parameters"), (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (item : Bytes) => (cons (ctor LexLeanStd.Json::Json.JStr item) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [Bytes] part1) : (List LexLeanStd.Json::Json))))) entries2)); (let entries4 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "state"), (SemanticModels::contractStateJson part2)) entries3); (let entries5 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "relation"), (SemanticTerms::memberJson (first part3) (second part3))) entries4); (let entries6 : (List (Bytes * LexLeanStd.Json::Json)) := (match part4 + | Option.some present => (cons ((lit "invariant"), (logicInvariantJson present)) entries5) + | Option.none => entries5); (let entries7 : (List (Bytes * LexLeanStd.Json::Json)) := (match part5 + | Option.some present => (cons ((lit "ranking"), (SemanticTerms::memberJson (first present) (second present))) entries6) + | Option.none => entries6); (let entries8 : (List (Bytes * LexLeanStd.Json::Json)) := (match part6 + | List.nil => entries7 + | List.cons head tail => (cons ((lit "axioms"), (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (item : Bytes) => (cons (ctor LexLeanStd.Json::Json.JStr item) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [Bytes] part6) : (List LexLeanStd.Json::Json))))) entries7)); (objectOf entries8))))))))) + | ReasoningDeclaration.ReasoningDeclarationInferenceRule part0 part1 part2 part3 part4 part5 part6 part7 part8 part9 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "inference_rule"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "name"), (ctor LexLeanStd.Json::Json.JStr part0)) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (match part1 + | List.nil => entries2 + | List.cons head tail => (cons ((lit "type_parameters"), (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (item : Bytes) => (cons (ctor LexLeanStd.Json::Json.JStr item) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [Bytes] part1) : (List LexLeanStd.Json::Json))))) entries2)); (let entries4 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "logic"), (SemanticModels::modelUseJson part2)) entries3); (let entries5 : (List (Bytes * LexLeanStd.Json::Json)) := (match part3 + | Option.some present => (cons ((lit "binding"), (ruleBindingJson present)) entries4) + | Option.none => entries4); (let entries6 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "guard"), (SemanticTerms::termJson part4)) entries5); (let entries7 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "conclusion"), (SemanticTerms::termJson part5)) entries6); (let entries8 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "soundness"), (SemanticTerms::memberJson (first part6) (second part6))) entries7); (let entries9 : (List (Bytes * LexLeanStd.Json::Json)) := (match part7 + | Option.some present => (cons ((lit "progress"), (SemanticTerms::memberJson (first present) (second present))) entries8) + | Option.none => entries8); (let entries10 : (List (Bytes * LexLeanStd.Json::Json)) := (if part8 then (cons ((lit "executable"), (ctor LexLeanStd.Json::Json.JBool true)) entries9) else entries9); (let entries11 : (List (Bytes * LexLeanStd.Json::Json)) := (match part9 + | List.nil => entries10 + | List.cons head tail => (cons ((lit "axioms"), (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (item : Bytes) => (cons (ctor LexLeanStd.Json::Json.JStr item) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [Bytes] part9) : (List LexLeanStd.Json::Json))))) entries10)); (objectOf entries11)))))))))))) + | ReasoningDeclaration.ReasoningDeclarationVerifier part0 part1 part2 part3 part4 part5 part6 part7 part8 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "verifier"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "name"), (ctor LexLeanStd.Json::Json.JStr part0)) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (match part1 + | List.nil => entries2 + | List.cons head tail => (cons ((lit "type_parameters"), (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (item : Bytes) => (cons (ctor LexLeanStd.Json::Json.JStr item) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [Bytes] part1) : (List LexLeanStd.Json::Json))))) entries2)); (let entries4 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "subject"), (SemanticModels::modelBinderJson part2)) entries3); (let entries5 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "candidate"), (SemanticModels::modelBinderJson part3)) entries4); (let entries6 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "specification"), (SemanticTerms::memberJson (first part4) (second part4))) entries5); (let entries7 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "check"), (SemanticTerms::memberJson (first part5) (second part5))) entries6); (let entries8 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "sound"), (SemanticTerms::memberJson (first part6) (second part6))) entries7); (let entries9 : (List (Bytes * LexLeanStd.Json::Json)) := (match part7 + | Option.some present => (cons ((lit "complete"), (SemanticTerms::memberJson (first present) (second present))) entries8) + | Option.none => entries8); (let entries10 : (List (Bytes * LexLeanStd.Json::Json)) := (match part8 + | List.nil => entries9 + | List.cons head tail => (cons ((lit "axioms"), (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (item : Bytes) => (cons (ctor LexLeanStd.Json::Json.JStr item) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [Bytes] part8) : (List LexLeanStd.Json::Json))))) entries9)); (objectOf entries10))))))))))) + | ReasoningDeclaration.ReasoningDeclarationReasoner part0 part1 part2 part3 part4 part5 part6 part7 part8 part9 part10 part11 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "reasoner"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "name"), (ctor LexLeanStd.Json::Json.JStr part0)) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (match part1 + | List.nil => entries2 + | List.cons head tail => (cons ((lit "type_parameters"), (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (item : Bytes) => (cons (ctor LexLeanStd.Json::Json.JStr item) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [Bytes] part1) : (List LexLeanStd.Json::Json))))) entries2)); (let entries4 : (List (Bytes * LexLeanStd.Json::Json)) := (match part2 + | Option.some present => (cons ((lit "logic"), (SemanticModels::modelUseJson present)) entries3) + | Option.none => entries3); (let entries5 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "observation"), (SemanticModels::modelBinderJson part3)) entries4); (let entries6 : (List (Bytes * LexLeanStd.Json::Json)) := (match part4 + | Option.some present => (cons ((lit "observe"), (SemanticTerms::termJson present)) entries5) + | Option.none => entries5); (let entries7 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "rules"), (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (item : SemanticModels::ModelUse) => (cons (SemanticModels::modelUseJson item) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [SemanticModels::ModelUse] part5) : (List LexLeanStd.Json::Json))))) entries6); (let entries8 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "strategy"), (reasoningStrategyJson part6)) entries7); (let entries9 : (List (Bytes * LexLeanStd.Json::Json)) := (match part7 + | Option.some present => (cons ((lit "answer"), (reasoningAnswerJson present)) entries8) + | Option.none => entries8); (let entries10 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "verifier"), (SemanticModels::modelUseJson part8)) entries9); (let entries11 : (List (Bytes * LexLeanStd.Json::Json)) := (match part9 + | List.nil => entries10 + | List.cons head tail => (cons ((lit "claims"), (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (item : ReasoningClaim) => (cons (reasoningClaimJson item) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [ReasoningClaim] part9) : (List LexLeanStd.Json::Json))))) entries10)); (let entries12 : (List (Bytes * LexLeanStd.Json::Json)) := (if part10 then (cons ((lit "executable"), (ctor LexLeanStd.Json::Json.JBool true)) entries11) else entries11); (let entries13 : (List (Bytes * LexLeanStd.Json::Json)) := (match part11 + | List.nil => entries12 + | List.cons head tail => (cons ((lit "axioms"), (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (item : Bytes) => (cons (ctor LexLeanStd.Json::Json.JStr item) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [Bytes] part11) : (List LexLeanStd.Json::Json))))) entries12)); (objectOf entries13)))))))))))))) + | ReasoningDeclaration.ReasoningDeclarationTokenizer part0 part1 part2 part3 part4 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "tokenizer"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "name"), (ctor LexLeanStd.Json::Json.JStr part0)) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (match part1 + | List.nil => entries2 + | List.cons head tail => (cons ((lit "type_parameters"), (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (item : Bytes) => (cons (ctor LexLeanStd.Json::Json.JStr item) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [Bytes] part1) : (List LexLeanStd.Json::Json))))) entries2)); (let entries4 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "scheme"), (tokenizerSchemeJson part2)) entries3); (let entries5 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "vocabulary"), (SemanticModels::modelUseJson part3)) entries4); (let entries6 : (List (Bytes * LexLeanStd.Json::Json)) := (match part4 + | List.nil => entries5 + | List.cons head tail => (cons ((lit "axioms"), (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (item : Bytes) => (cons (ctor LexLeanStd.Json::Json.JStr item) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [Bytes] part4) : (List LexLeanStd.Json::Json))))) entries5)); (objectOf entries6))))))) + | ReasoningDeclaration.ReasoningDeclarationContextWindow part0 part1 part2 part3 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "context_window"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "name"), (ctor LexLeanStd.Json::Json.JStr part0)) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "capacity"), (ctor LexLeanStd.Json::Json.JInt false part1)) entries2); (let entries4 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "overflow"), (overflowDispositionJson part2)) entries3); (let entries5 : (List (Bytes * LexLeanStd.Json::Json)) := (match part3 + | List.nil => entries4 + | List.cons head tail => (cons ((lit "axioms"), (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (item : Bytes) => (cons (ctor LexLeanStd.Json::Json.JStr item) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [Bytes] part3) : (List LexLeanStd.Json::Json))))) entries4)); (objectOf entries5)))))) + | ReasoningDeclaration.ReasoningDeclarationTextGenerator part0 part1 part2 part3 part4 part5 part6 part7 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "text_generator"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "name"), (ctor LexLeanStd.Json::Json.JStr part0)) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "tokenizer"), (SemanticTerms::memberJson (first part1) (second part1))) entries2); (let entries4 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "window"), (SemanticTerms::memberJson (first part2) (second part2))) entries3); (let entries5 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "scorer"), (SemanticModels::modelUseJson part3)) entries4); (let entries6 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "max_tokens"), (ctor LexLeanStd.Json::Json.JInt false part4)) entries5); (let entries7 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "stop"), (SemanticTerms::memberJson (first part5) (second part5))) entries6); (let entries8 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "policy"), (generationPolicyJson part6)) entries7); (let entries9 : (List (Bytes * LexLeanStd.Json::Json)) := (match part7 + | List.nil => entries8 + | List.cons head tail => (cons ((lit "axioms"), (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (item : Bytes) => (cons (ctor LexLeanStd.Json::Json.JStr item) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [Bytes] part7) : (List LexLeanStd.Json::Json))))) entries8)); (objectOf entries9)))))))))) + | ReasoningDeclaration.ReasoningDeclarationTextClassifier part0 part1 part2 part3 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "text_classifier"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "name"), (ctor LexLeanStd.Json::Json.JStr part0)) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "tokenizer"), (SemanticTerms::memberJson (first part1) (second part1))) entries2); (let entries4 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "model"), (SemanticModels::modelUseJson part2)) entries3); (let entries5 : (List (Bytes * LexLeanStd.Json::Json)) := (match part3 + | List.nil => entries4 + | List.cons head tail => (cons ((lit "axioms"), (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (item : Bytes) => (cons (ctor LexLeanStd.Json::Json.JStr item) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [Bytes] part3) : (List LexLeanStd.Json::Json))))) entries4)); (objectOf entries5))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: reasoningDeclarationText, + parameters: [value : ReasoningDeclaration], + result: Bytes, + body: (LexLeanStd.Json::writeJson (reasoningDeclarationJson value)), + axioms: [Quot.sound, propext], + executable: true, + production: {effects: [allocation, overflow, recursion], targets: [rust-std]} +} + +inductive{ + name: AnyDeclaration, + type_parameters: [], + parameters: [], + constructors: [ + {name: AnyOrdinary, fields: [SemanticDecls::SDecl]}, + {name: AnyModel, fields: [SemanticModels::ModelDeclaration]}, + {name: AnyReasoning, fields: [ReasoningDeclaration]} + ] +} + +definition{ + name: anyDeclarationJson, + parameters: [value : AnyDeclaration], + result: LexLeanStd.Json::Json, + body: (match value + | AnyDeclaration.AnyOrdinary declared => (SemanticDecls::declarationJson declared) + | AnyDeclaration.AnyModel declared => (SemanticModels::modelDeclarationJson declared) + | AnyDeclaration.AnyReasoning declared => (reasoningDeclarationJson declared)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: semanticModuleText, + parameters: [spec : Bytes, declarations : (List AnyDeclaration)], + result: Bytes, + body: (LexLeanStd.Json::writeJson (objectOf (cons ((lit "spec"), (ctor LexLeanStd.Json::Json.JStr spec)) (cons ((lit "declarations"), (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (declared : AnyDeclaration) => (cons (anyDeclarationJson declared) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [AnyDeclaration] declarations) : (List LexLeanStd.Json::Json))))) (nil (Bytes * LexLeanStd.Json::Json)))))), + axioms: [Quot.sound, propext], + 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 70f735bfbe..45db006510 100644 --- a/crates/conformance/src/cases/bootstrap.rs +++ b/crates/conformance/src/cases/bootstrap.rs @@ -47,6 +47,7 @@ mod rust_validate; mod scan; mod semantic_decls; mod semantic_models; +mod semantic_reasoning; mod semantic_terms; mod semantic_types; mod structural_grammar; @@ -2588,6 +2589,7 @@ fn case(id: &str) { "BS-69" => semantic_terms::case(), "BS-70" => semantic_decls::case(), "BS-71" => semantic_models::case(), + "BS-72" => semantic_reasoning::case(), other => panic!("no bootstrap case is wired for {other}"), } } diff --git a/crates/conformance/src/cases/bootstrap/semantic_decls.rs b/crates/conformance/src/cases/bootstrap/semantic_decls.rs index 215d292f82..5f73443254 100755 --- a/crates/conformance/src/cases/bootstrap/semantic_decls.rs +++ b/crates/conformance/src/cases/bootstrap/semantic_decls.rs @@ -144,7 +144,7 @@ fn text_option(value: Option<&String>) -> Value { option(value.map(|value| text(value))) } -fn is_ordinary(declaration: &SemanticDeclaration) -> bool { +pub(super) fn is_ordinary(declaration: &SemanticDeclaration) -> bool { matches!( declaration, SemanticDeclaration::Structure { .. } @@ -156,7 +156,7 @@ fn is_ordinary(declaration: &SemanticDeclaration) -> bool { ) } -fn enc_declaration(declaration: &SemanticDeclaration) -> Value { +pub(super) fn enc_declaration(declaration: &SemanticDeclaration) -> Value { match declaration { SemanticDeclaration::Structure { name, diff --git a/crates/conformance/src/cases/bootstrap/semantic_models.rs b/crates/conformance/src/cases/bootstrap/semantic_models.rs index 8961e2eda9..0f54b164f9 100755 --- a/crates/conformance/src/cases/bootstrap/semantic_models.rs +++ b/crates/conformance/src/cases/bootstrap/semantic_models.rs @@ -120,7 +120,7 @@ fn enc_member(member: &MemberRef) -> Value { // ------------------------------------------- generated from the type schema -fn enc_artifact_role(value: &ArtifactRole) -> Value { +pub(super) fn enc_artifact_role(value: &ArtifactRole) -> Value { adt( "ArtifactRole", &format!("ArtifactRole{value:?}"), @@ -128,7 +128,7 @@ fn enc_artifact_role(value: &ArtifactRole) -> Value { ) } -const ALL_ARTIFACT_ROLE: &[ArtifactRole] = &[ +pub(super) const ALL_ARTIFACT_ROLE: &[ArtifactRole] = &[ ArtifactRole::Parameters, ArtifactRole::Vocabulary, ArtifactRole::Dataset, @@ -136,11 +136,11 @@ const ALL_ARTIFACT_ROLE: &[ArtifactRole] = &[ ArtifactRole::Binary, ]; -fn example_artifact_role(step: usize) -> ArtifactRole { +pub(super) fn example_artifact_role(step: usize) -> ArtifactRole { ALL_ARTIFACT_ROLE[step % ALL_ARTIFACT_ROLE.len()] } -fn enc_tensor_element(value: &TensorElement) -> Value { +pub(super) fn enc_tensor_element(value: &TensorElement) -> Value { adt( "TensorElement", &format!("TensorElement{value:?}"), @@ -148,7 +148,7 @@ fn enc_tensor_element(value: &TensorElement) -> Value { ) } -const ALL_TENSOR_ELEMENT: &[TensorElement] = &[ +pub(super) const ALL_TENSOR_ELEMENT: &[TensorElement] = &[ TensorElement::Int8, TensorElement::Int16, TensorElement::Int32, @@ -159,11 +159,11 @@ const ALL_TENSOR_ELEMENT: &[TensorElement] = &[ TensorElement::UInt64, ]; -fn example_tensor_element(step: usize) -> TensorElement { +pub(super) fn example_tensor_element(step: usize) -> TensorElement { ALL_TENSOR_ELEMENT[step % ALL_TENSOR_ELEMENT.len()] } -fn enc_artifact_schema(value: &ArtifactSchema) -> Value { +pub(super) fn enc_artifact_schema(value: &ArtifactSchema) -> Value { match value { ArtifactSchema::Bytes => adt("ArtifactSchema", "ArtifactSchemaBytes", vec![]), ArtifactSchema::IntTensor { element, shape } => adt( @@ -179,7 +179,7 @@ fn enc_artifact_schema(value: &ArtifactSchema) -> Value { } #[allow(clippy::too_many_lines)] -fn example_artifact_schema(step: usize) -> ArtifactSchema { +pub(super) fn example_artifact_schema(step: usize) -> ArtifactSchema { let rest = step / 3; match step % 3 { 0 => ArtifactSchema::Bytes, @@ -197,7 +197,7 @@ fn example_artifact_schema(step: usize) -> ArtifactSchema { } } -fn enc_model_binder(value: &ModelBinder) -> Value { +pub(super) fn enc_model_binder(value: &ModelBinder) -> Value { adt( "ModelBinder", "ModelBinderOf", @@ -205,7 +205,7 @@ fn enc_model_binder(value: &ModelBinder) -> Value { ) } -fn example_model_binder(step: usize) -> ModelBinder { +pub(super) fn example_model_binder(step: usize) -> ModelBinder { ModelBinder { name: format!("s{step}"), r#type: if step % 2 == 0 { @@ -216,7 +216,7 @@ fn example_model_binder(step: usize) -> ModelBinder { } } -fn enc_contract_state(value: &ContractState) -> Value { +pub(super) fn enc_contract_state(value: &ContractState) -> Value { adt( "ContractState", "ContractStateOf", @@ -228,7 +228,7 @@ fn enc_contract_state(value: &ContractState) -> Value { ) } -fn example_contract_state(step: usize) -> ContractState { +pub(super) fn example_contract_state(step: usize) -> ContractState { ContractState { name: format!("s{step}"), next: format!("s{step}"), @@ -240,7 +240,7 @@ fn example_contract_state(step: usize) -> ContractState { } } -fn enc_contract_predicate(value: &ContractPredicate) -> Value { +pub(super) fn enc_contract_predicate(value: &ContractPredicate) -> Value { adt( "ContractPredicate", &format!("ContractPredicate{value:?}"), @@ -248,17 +248,17 @@ fn enc_contract_predicate(value: &ContractPredicate) -> Value { ) } -const ALL_CONTRACT_PREDICATE: &[ContractPredicate] = &[ +pub(super) const ALL_CONTRACT_PREDICATE: &[ContractPredicate] = &[ ContractPredicate::Invariant, ContractPredicate::Postcondition, ContractPredicate::Precondition, ]; -fn example_contract_predicate(step: usize) -> ContractPredicate { +pub(super) fn example_contract_predicate(step: usize) -> ContractPredicate { ALL_CONTRACT_PREDICATE[step % ALL_CONTRACT_PREDICATE.len()] } -fn enc_contract_validator(value: &ContractValidator) -> Value { +pub(super) fn enc_contract_validator(value: &ContractValidator) -> Value { adt( "ContractValidator", "ContractValidatorOf", @@ -271,7 +271,7 @@ fn enc_contract_validator(value: &ContractValidator) -> Value { ) } -fn example_contract_validator(step: usize) -> ContractValidator { +pub(super) fn example_contract_validator(step: usize) -> ContractValidator { ContractValidator { predicate: example_contract_predicate(step), validator: MemberRef { @@ -289,7 +289,7 @@ fn example_contract_validator(step: usize) -> ContractValidator { } } -fn enc_model_use(value: &ModelUse) -> Value { +pub(super) fn enc_model_use(value: &ModelUse) -> Value { adt( "ModelUse", "ModelUseOf", @@ -306,7 +306,7 @@ fn enc_model_use(value: &ModelUse) -> Value { ) } -fn example_model_use(step: usize) -> ModelUse { +pub(super) fn example_model_use(step: usize) -> ModelUse { ModelUse { member: MemberRef { module: (step % 2 == 0).then(|| "M".to_owned()), @@ -328,7 +328,7 @@ fn example_model_use(step: usize) -> ModelUse { } } -fn enc_realization_state(value: &RealizationState) -> Value { +pub(super) fn enc_realization_state(value: &RealizationState) -> Value { adt( "RealizationState", "RealizationStateOf", @@ -340,7 +340,7 @@ fn enc_realization_state(value: &RealizationState) -> Value { ) } -fn example_realization_state(step: usize) -> RealizationState { +pub(super) fn example_realization_state(step: usize) -> RealizationState { RealizationState { name: format!("s{step}"), r#type: if step % 2 == 0 { @@ -354,7 +354,7 @@ fn example_realization_state(step: usize) -> RealizationState { } } -fn enc_realization_rule(value: &RealizationRule) -> Value { +pub(super) fn enc_realization_rule(value: &RealizationRule) -> Value { adt( "RealizationRule", "RealizationRuleOf", @@ -366,7 +366,7 @@ fn enc_realization_rule(value: &RealizationRule) -> Value { ) } -fn example_realization_rule(step: usize) -> RealizationRule { +pub(super) fn example_realization_rule(step: usize) -> RealizationRule { RealizationRule { name: format!("s{step}"), guard: SemanticTerm::Nat { @@ -378,7 +378,7 @@ fn example_realization_rule(step: usize) -> RealizationRule { } } -fn enc_statistical_scheme(value: &StatisticalScheme) -> Value { +pub(super) fn enc_statistical_scheme(value: &StatisticalScheme) -> Value { adt( "StatisticalScheme", &format!("StatisticalScheme{value:?}"), @@ -386,13 +386,13 @@ fn enc_statistical_scheme(value: &StatisticalScheme) -> Value { ) } -const ALL_STATISTICAL_SCHEME: &[StatisticalScheme] = &[StatisticalScheme::LinearScoring]; +pub(super) const ALL_STATISTICAL_SCHEME: &[StatisticalScheme] = &[StatisticalScheme::LinearScoring]; -fn example_statistical_scheme(step: usize) -> StatisticalScheme { +pub(super) fn example_statistical_scheme(step: usize) -> StatisticalScheme { ALL_STATISTICAL_SCHEME[step % ALL_STATISTICAL_SCHEME.len()] } -fn enc_neural_architecture(value: &NeuralArchitecture) -> Value { +pub(super) fn enc_neural_architecture(value: &NeuralArchitecture) -> Value { adt( "NeuralArchitecture", &format!("NeuralArchitecture{value:?}"), @@ -400,13 +400,14 @@ fn enc_neural_architecture(value: &NeuralArchitecture) -> Value { ) } -const ALL_NEURAL_ARCHITECTURE: &[NeuralArchitecture] = &[NeuralArchitecture::IntegerFeedforward]; +pub(super) const ALL_NEURAL_ARCHITECTURE: &[NeuralArchitecture] = + &[NeuralArchitecture::IntegerFeedforward]; -fn example_neural_architecture(step: usize) -> NeuralArchitecture { +pub(super) fn example_neural_architecture(step: usize) -> NeuralArchitecture { ALL_NEURAL_ARCHITECTURE[step % ALL_NEURAL_ARCHITECTURE.len()] } -fn enc_neural_layer(value: &NeuralLayer) -> Value { +pub(super) fn enc_neural_layer(value: &NeuralLayer) -> Value { match value { NeuralLayer::Dense { inputs, @@ -437,7 +438,7 @@ fn enc_neural_layer(value: &NeuralLayer) -> Value { } #[allow(clippy::too_many_lines)] -fn example_neural_layer(step: usize) -> NeuralLayer { +pub(super) fn example_neural_layer(step: usize) -> NeuralLayer { let rest = step / 3; match step % 3 { 0 => NeuralLayer::Dense { @@ -461,7 +462,7 @@ fn example_neural_layer(step: usize) -> NeuralLayer { } } -fn enc_neural_decoder(value: &NeuralDecoder) -> Value { +pub(super) fn enc_neural_decoder(value: &NeuralDecoder) -> Value { match value { NeuralDecoder::Argmax { labels } => adt( "NeuralDecoder", @@ -477,7 +478,7 @@ fn enc_neural_decoder(value: &NeuralDecoder) -> Value { } #[allow(clippy::too_many_lines)] -fn example_neural_decoder(step: usize) -> NeuralDecoder { +pub(super) fn example_neural_decoder(step: usize) -> NeuralDecoder { let rest = step / 2; match step % 2 { 0 => NeuralDecoder::Argmax { @@ -500,7 +501,7 @@ fn example_neural_decoder(step: usize) -> NeuralDecoder { } } -fn enc_composite_junction(value: &CompositeJunction) -> Value { +pub(super) fn enc_composite_junction(value: &CompositeJunction) -> Value { match value { CompositeJunction::Unconditional => adt( "CompositeJunction", @@ -517,7 +518,7 @@ fn enc_composite_junction(value: &CompositeJunction) -> Value { } #[allow(clippy::too_many_lines)] -fn example_composite_junction(step: usize) -> CompositeJunction { +pub(super) fn example_composite_junction(step: usize) -> CompositeJunction { let rest = step / 3; match step % 3 { 0 => CompositeJunction::Unconditional, @@ -531,7 +532,7 @@ fn example_composite_junction(step: usize) -> CompositeJunction { } } -fn enc_composite_form(value: &CompositeForm) -> Value { +pub(super) fn enc_composite_form(value: &CompositeForm) -> Value { match value { CompositeForm::Sequence { stages, junctions } => adt( "CompositeForm", @@ -574,7 +575,7 @@ fn enc_composite_form(value: &CompositeForm) -> Value { } #[allow(clippy::too_many_lines)] -fn example_composite_form(step: usize) -> CompositeForm { +pub(super) fn example_composite_form(step: usize) -> CompositeForm { let rest = step / 5; match step % 5 { 0 => CompositeForm::Sequence { @@ -615,7 +616,7 @@ fn example_composite_form(step: usize) -> CompositeForm { } } -fn enc_realization_descriptor(value: &RealizationDescriptor) -> Value { +pub(super) fn enc_realization_descriptor(value: &RealizationDescriptor) -> Value { match value { RealizationDescriptor::Deterministic { body } => adt( "RealizationDescriptor", @@ -684,7 +685,7 @@ fn enc_realization_descriptor(value: &RealizationDescriptor) -> Value { } #[allow(clippy::too_many_lines)] -fn example_realization_descriptor(step: usize) -> RealizationDescriptor { +pub(super) fn example_realization_descriptor(step: usize) -> RealizationDescriptor { let rest = step / 5; match step % 5 { 0 => RealizationDescriptor::Deterministic { @@ -757,7 +758,7 @@ fn example_realization_descriptor(step: usize) -> RealizationDescriptor { } } -fn enc_evidence_claim(value: &EvidenceClaim) -> Value { +pub(super) fn enc_evidence_claim(value: &EvidenceClaim) -> Value { match value { EvidenceClaim::DatasetAgreement { dataset, @@ -802,7 +803,7 @@ fn enc_evidence_claim(value: &EvidenceClaim) -> Value { } #[allow(clippy::too_many_lines)] -fn example_evidence_claim(step: usize) -> EvidenceClaim { +pub(super) fn example_evidence_claim(step: usize) -> EvidenceClaim { let rest = step / 5; match step % 5 { 0 => EvidenceClaim::DatasetAgreement { @@ -853,7 +854,7 @@ fn example_evidence_claim(step: usize) -> EvidenceClaim { } } -fn enc_model_declaration(value: &SemanticDeclaration) -> Value { +pub(super) fn enc_model_declaration(value: &SemanticDeclaration) -> Value { match value { SemanticDeclaration::Artifact { name, @@ -976,7 +977,7 @@ fn enc_model_declaration(value: &SemanticDeclaration) -> Value { } #[allow(clippy::too_many_lines)] -fn example_model_declaration(step: usize) -> SemanticDeclaration { +pub(super) fn example_model_declaration(step: usize) -> SemanticDeclaration { let rest = step / 5; match step % 5 { 0 => SemanticDeclaration::Artifact { @@ -1147,7 +1148,7 @@ const KINDS: &[&str] = &[ // ------------------------------------------------------------- the inputs -fn is_model(declaration: &SemanticDeclaration) -> bool { +pub(super) fn is_model(declaration: &SemanticDeclaration) -> bool { matches!( declaration, SemanticDeclaration::Artifact { .. } diff --git a/crates/conformance/src/cases/bootstrap/semantic_reasoning.rs b/crates/conformance/src/cases/bootstrap/semantic_reasoning.rs new file mode 100755 index 0000000000..d2c3d46ac1 --- /dev/null +++ b/crates/conformance/src/cases/bootstrap/semantic_reasoning.rs @@ -0,0 +1,1175 @@ +//! `BS-72` (SPEC.md §17.23, §17.12): the declarations of logics, inference +//! rules, verifiers, reasoners, tokenizers, context windows, text generators, +//! and text classifiers of the semantic module language, and the semantic +//! module of any declarations, as language-1.2 source, +//! `LexLeanBoot.SemanticReasoning`, held to the byte to the serde form of those +//! variants of `lexlean::ir::semantic::SemanticDeclaration` and of +//! `SemanticModule` in canonical JSON, as the linked IR embeds them. +//! +//! `reasoningDeclarationText` writes one such declaration with every datatype +//! it holds; `semanticModuleText` writes a module of declarations of every +//! kind, the ordinary ones through `SemanticDecls`, the model ones through +//! `SemanticModels`, and these. The module, its encoders here, and the example +//! builders here are generated from one description of the oracle's types. +//! Every such declaration and every semantic module of every committed project +//! that links is a run, and so are examples that reach every variant. +//! +//! Each planted defect renames one serialized kind or name and is detected by +//! the first example that holds it. + +use std::collections::{BTreeMap, BTreeSet}; +use std::sync::OnceLock; + +use lexlean::calculus::Value; +use lexlean::ir::semantic::{ + GenerationPolicy, LogicInvariant, MemberRef, OverflowDisposition, ReasoningAnswer, + ReasoningClaim, ReasoningStrategy, RuleBinding, SamplingDistribution, SamplingGenerator, + SearchOrder, SemanticDeclaration, SemanticModule, SemanticTerm, SemanticType, TokenizerScheme, +}; +use serde_json::Value as Json2; + +use super::ir_document::linked_projects; +use super::rust_validate::{eligibility_of, rustc_differential}; +use super::semantic_decls::{enc_declaration, is_ordinary}; +use super::semantic_models::{ + enc_contract_state, enc_model_binder, enc_model_declaration, enc_model_use, + example_contract_state, example_model_binder, example_model_use, is_model, +}; +use super::semantic_terms::enc_term; +use super::semantic_types::{canonical, enc_type}; +use super::{list, option, pair, text, Run, Seeded}; +use crate::production_export as harness; +use crate::support::{self, repo_root}; + +const MODULE: &str = "LexLeanBoot.SemanticReasoning"; +const SOURCE: &str = "bootstrap/src/SemanticReasoning.lex.tex"; +const ROOTS: [(&str, &[&str]); 2] = [ + ( + "reasoningDeclarationText", + &["allocation", "overflow", "recursion"], + ), + ( + "semanticModuleText", + &["allocation", "overflow", "recursion"], + ), +]; +/// A committed module whose JSON is longer runs under rustc only. +const EVALUATOR_BYTES: usize = 262_144; + +/// The most a part of a certificate of a root may be, as the row states. +const CERTIFICATE_BUDGET: u64 = 3_145_728; +/// The examples: one per step, the step's digits choosing every variant. +const EXAMPLES: usize = 150; +/// Seeded examples under the evaluator, and under rustc. +const EVALUATOR_SEEDED: usize = 100; +const RUSTC_SEEDED: usize = 500; + +// ---------------------------------------------------------------- the sorts + +fn constructors(sort: &str) -> &'static Vec { + static TABLES: OnceLock>> = OnceLock::new(); + let tables = TABLES.get_or_init(|| { + let source = std::fs::read_to_string(repo_root().join(SOURCE).as_std_path()) + .expect("the module's source"); + let mut out = BTreeMap::new(); + for block in source.split("inductive{").skip(1) { + let block = block.split("\n}\n").next().expect("a block"); + let name = block + .split("name: ") + .nth(1) + .and_then(|rest| rest.split(',').next()) + .expect("the inductive's name") + .trim() + .to_owned(); + let names: Vec = block + .split("constructors:") + .nth(1) + .expect("the constructors") + .split("name: ") + .skip(1) + .map(|rest| { + rest.split([',', '\n']) + .next() + .expect("a constructor") + .trim() + .to_owned() + }) + .collect(); + out.insert(name, names); + } + out + }); + tables + .get(sort) + .unwrap_or_else(|| panic!("the module has no inductive `{sort}`")) +} + +fn adt(sort: &str, constructor: &str, fields: Vec) -> Value { + let found = constructors(sort) + .iter() + .position(|name| name == constructor) + .unwrap_or_else(|| panic!("`{sort}` has no constructor `{constructor}`")); + Value::Adt { + constructor: u64::try_from(found).expect("an index"), + fields, + } +} + +fn nat(value: u64) -> Value { + Value::Nat { + value: value.to_string(), + } +} + +fn enc_member(member: &MemberRef) -> Value { + pair( + option(member.module.as_deref().map(text)), + text(&member.name), + ) +} + +// ------------------------------------------- generated from the type schema + +pub(super) fn enc_logic_invariant(value: &LogicInvariant) -> Value { + adt( + "LogicInvariant", + "LogicInvariantOf", + vec![enc_member(&value.predicate), enc_member(&value.preserves)], + ) +} + +pub(super) fn example_logic_invariant(step: usize) -> LogicInvariant { + LogicInvariant { + predicate: MemberRef { + module: (step % 2 == 0).then(|| "M".to_owned()), + name: format!("m{step}"), + }, + preserves: MemberRef { + module: (step % 2 == 0).then(|| "M".to_owned()), + name: format!("m{step}"), + }, + } +} + +pub(super) fn enc_rule_binding(value: &RuleBinding) -> Value { + adt( + "RuleBinding", + "RuleBindingOf", + vec![ + text(&value.name), + enc_type(&value.r#type), + enc_term(&value.candidates), + ], + ) +} + +pub(super) fn example_rule_binding(step: usize) -> RuleBinding { + RuleBinding { + name: format!("s{step}"), + r#type: if step % 2 == 0 { + SemanticType::Nat + } else { + SemanticType::Bytes + }, + candidates: SemanticTerm::Nat { + value: step.to_string(), + }, + } +} + +pub(super) fn enc_reasoning_answer(value: &ReasoningAnswer) -> Value { + adt( + "ReasoningAnswer", + "ReasoningAnswerOf", + vec![ + text(&value.name), + enc_type(&value.r#type), + enc_term(&value.value), + ], + ) +} + +pub(super) fn example_reasoning_answer(step: usize) -> ReasoningAnswer { + ReasoningAnswer { + name: format!("s{step}"), + r#type: if step % 2 == 0 { + SemanticType::Nat + } else { + SemanticType::Bytes + }, + value: SemanticTerm::Nat { + value: step.to_string(), + }, + } +} + +pub(super) fn enc_search_order(value: &SearchOrder) -> Value { + adt("SearchOrder", &format!("SearchOrder{value:?}"), Vec::new()) +} + +pub(super) const ALL_SEARCH_ORDER: &[SearchOrder] = + &[SearchOrder::BreadthFirst, SearchOrder::DepthFirst]; + +pub(super) fn example_search_order(step: usize) -> SearchOrder { + ALL_SEARCH_ORDER[step % ALL_SEARCH_ORDER.len()] +} + +pub(super) fn enc_reasoning_strategy(value: &ReasoningStrategy) -> Value { + match value { + ReasoningStrategy::Forward { fuel } => adt( + "ReasoningStrategy", + "ReasoningStrategyForward", + vec![option(fuel.as_ref().map(|item| enc_term(item)))], + ), + ReasoningStrategy::Search { + order, + fuel, + frontier, + deduplicate, + } => adt( + "ReasoningStrategy", + "ReasoningStrategySearch", + vec![ + enc_search_order(order), + option(fuel.as_ref().map(|item| enc_term(item))), + option(frontier.as_ref().map(|item| enc_term(item))), + Value::Bool { + value: *deduplicate, + }, + ], + ), + ReasoningStrategy::GenerateAndVerify { generator, budget } => adt( + "ReasoningStrategy", + "ReasoningStrategyGenerateAndVerify", + vec![ + enc_term(generator), + option(budget.as_ref().map(|item| enc_term(item))), + ], + ), + } +} + +#[allow(clippy::too_many_lines)] +pub(super) fn example_reasoning_strategy(step: usize) -> ReasoningStrategy { + let rest = step / 3; + match step % 3 { + 0 => ReasoningStrategy::Forward { + fuel: (rest % 3 != 2).then(|| SemanticTerm::Nat { + value: rest.to_string(), + }), + }, + 1 => ReasoningStrategy::Search { + order: example_search_order(rest), + fuel: (rest % 3 != 2).then(|| SemanticTerm::Nat { + value: rest.to_string(), + }), + frontier: (rest % 3 != 2).then(|| SemanticTerm::Nat { + value: rest.to_string(), + }), + deduplicate: rest % 2 == 0, + }, + _ => ReasoningStrategy::GenerateAndVerify { + generator: SemanticTerm::Nat { + value: rest.to_string(), + }, + budget: (rest % 3 != 2).then(|| SemanticTerm::Nat { + value: rest.to_string(), + }), + }, + } +} + +pub(super) fn enc_reasoning_claim(value: &ReasoningClaim) -> Value { + match value { + ReasoningClaim::InitialInvariant { theorem } => adt( + "ReasoningClaim", + "ReasoningClaimInitialInvariant", + vec![enc_member(theorem)], + ), + ReasoningClaim::Terminates { theorem } => adt( + "ReasoningClaim", + "ReasoningClaimTerminates", + vec![enc_member(theorem)], + ), + ReasoningClaim::ObservationInvariant { + predicate, + initial, + preserved, + } => adt( + "ReasoningClaim", + "ReasoningClaimObservationInvariant", + vec![ + enc_member(predicate), + enc_member(initial), + enc_member(preserved), + ], + ), + ReasoningClaim::AnswerCorrect { theorem } => adt( + "ReasoningClaim", + "ReasoningClaimAnswerCorrect", + vec![enc_member(theorem)], + ), + } +} + +#[allow(clippy::too_many_lines)] +pub(super) fn example_reasoning_claim(step: usize) -> ReasoningClaim { + let rest = step / 4; + match step % 4 { + 0 => ReasoningClaim::InitialInvariant { + theorem: MemberRef { + module: (rest % 2 == 0).then(|| "M".to_owned()), + name: format!("m{rest}"), + }, + }, + 1 => ReasoningClaim::Terminates { + theorem: MemberRef { + module: (rest % 2 == 0).then(|| "M".to_owned()), + name: format!("m{rest}"), + }, + }, + 2 => ReasoningClaim::ObservationInvariant { + predicate: MemberRef { + module: (rest % 2 == 0).then(|| "M".to_owned()), + name: format!("m{rest}"), + }, + initial: MemberRef { + module: (rest % 2 == 0).then(|| "M".to_owned()), + name: format!("m{rest}"), + }, + preserved: MemberRef { + module: (rest % 2 == 0).then(|| "M".to_owned()), + name: format!("m{rest}"), + }, + }, + _ => ReasoningClaim::AnswerCorrect { + theorem: MemberRef { + module: (rest % 2 == 0).then(|| "M".to_owned()), + name: format!("m{rest}"), + }, + }, + } +} + +pub(super) fn enc_tokenizer_scheme(value: &TokenizerScheme) -> Value { + adt( + "TokenizerScheme", + &format!("TokenizerScheme{value:?}"), + Vec::new(), + ) +} + +pub(super) const ALL_TOKENIZER_SCHEME: &[TokenizerScheme] = + &[TokenizerScheme::ByteLevel, TokenizerScheme::LongestMatch]; + +pub(super) fn example_tokenizer_scheme(step: usize) -> TokenizerScheme { + ALL_TOKENIZER_SCHEME[step % ALL_TOKENIZER_SCHEME.len()] +} + +pub(super) fn enc_overflow_disposition(value: &OverflowDisposition) -> Value { + adt( + "OverflowDisposition", + &format!("OverflowDisposition{value:?}"), + Vec::new(), + ) +} + +pub(super) const ALL_OVERFLOW_DISPOSITION: &[OverflowDisposition] = + &[OverflowDisposition::Refuse, OverflowDisposition::KeepLast]; + +pub(super) fn example_overflow_disposition(step: usize) -> OverflowDisposition { + ALL_OVERFLOW_DISPOSITION[step % ALL_OVERFLOW_DISPOSITION.len()] +} + +pub(super) fn enc_sampling_generator(value: &SamplingGenerator) -> Value { + adt( + "SamplingGenerator", + &format!("SamplingGenerator{value:?}"), + Vec::new(), + ) +} + +pub(super) const ALL_SAMPLING_GENERATOR: &[SamplingGenerator] = &[SamplingGenerator::Xorshift32]; + +pub(super) fn example_sampling_generator(step: usize) -> SamplingGenerator { + ALL_SAMPLING_GENERATOR[step % ALL_SAMPLING_GENERATOR.len()] +} + +pub(super) fn enc_sampling_distribution(value: &SamplingDistribution) -> Value { + adt( + "SamplingDistribution", + &format!("SamplingDistribution{value:?}"), + Vec::new(), + ) +} + +pub(super) const ALL_SAMPLING_DISTRIBUTION: &[SamplingDistribution] = + &[SamplingDistribution::Proportional]; + +pub(super) fn example_sampling_distribution(step: usize) -> SamplingDistribution { + ALL_SAMPLING_DISTRIBUTION[step % ALL_SAMPLING_DISTRIBUTION.len()] +} + +pub(super) fn enc_generation_policy(value: &GenerationPolicy) -> Value { + match value { + GenerationPolicy::Greedy => adt("GenerationPolicy", "GenerationPolicyGreedy", vec![]), + GenerationPolicy::Sample { + generator, + distribution, + } => adt( + "GenerationPolicy", + "GenerationPolicySample", + vec![ + enc_sampling_generator(generator), + enc_sampling_distribution(distribution), + ], + ), + } +} + +#[allow(clippy::too_many_lines)] +pub(super) fn example_generation_policy(step: usize) -> GenerationPolicy { + let rest = step / 2; + match step % 2 { + 0 => GenerationPolicy::Greedy, + _ => GenerationPolicy::Sample { + generator: example_sampling_generator(rest), + distribution: example_sampling_distribution(rest), + }, + } +} + +pub(super) fn enc_reasoning_declaration(value: &SemanticDeclaration) -> Value { + match value { + SemanticDeclaration::Logic { + name, + type_parameters, + state, + relation, + invariant, + ranking, + axioms, + } => adt( + "ReasoningDeclaration", + "ReasoningDeclarationLogic", + vec![ + text(name), + list(type_parameters.iter().map(|item| text(item)).collect()), + enc_contract_state(state), + enc_member(relation), + option(invariant.as_ref().map(|item| enc_logic_invariant(item))), + option(ranking.as_ref().map(|item| enc_member(item))), + list(axioms.iter().map(|item| text(item)).collect()), + ], + ), + SemanticDeclaration::InferenceRule { + name, + type_parameters, + logic, + binding, + guard, + conclusion, + soundness, + progress, + executable, + axioms, + } => adt( + "ReasoningDeclaration", + "ReasoningDeclarationInferenceRule", + vec![ + text(name), + list(type_parameters.iter().map(|item| text(item)).collect()), + enc_model_use(logic), + option(binding.as_ref().map(|item| enc_rule_binding(item))), + enc_term(guard), + enc_term(conclusion), + enc_member(soundness), + option(progress.as_ref().map(|item| enc_member(item))), + Value::Bool { value: *executable }, + list(axioms.iter().map(|item| text(item)).collect()), + ], + ), + SemanticDeclaration::Verifier { + name, + type_parameters, + subject, + candidate, + specification, + check, + sound, + complete, + axioms, + } => adt( + "ReasoningDeclaration", + "ReasoningDeclarationVerifier", + vec![ + text(name), + list(type_parameters.iter().map(|item| text(item)).collect()), + enc_model_binder(subject), + enc_model_binder(candidate), + enc_member(specification), + enc_member(check), + enc_member(sound), + option(complete.as_ref().map(|item| enc_member(item))), + list(axioms.iter().map(|item| text(item)).collect()), + ], + ), + SemanticDeclaration::Reasoner { + name, + type_parameters, + logic, + observation, + observe, + rules, + strategy, + answer, + verifier, + claims, + executable, + axioms, + } => adt( + "ReasoningDeclaration", + "ReasoningDeclarationReasoner", + vec![ + text(name), + list(type_parameters.iter().map(|item| text(item)).collect()), + option(logic.as_ref().map(|item| enc_model_use(item))), + enc_model_binder(observation), + option(observe.as_ref().map(|item| enc_term(item))), + list(rules.iter().map(|item| enc_model_use(item)).collect()), + enc_reasoning_strategy(strategy), + option(answer.as_ref().map(|item| enc_reasoning_answer(item))), + enc_model_use(verifier), + list( + claims + .iter() + .map(|item| enc_reasoning_claim(item)) + .collect(), + ), + Value::Bool { value: *executable }, + list(axioms.iter().map(|item| text(item)).collect()), + ], + ), + SemanticDeclaration::Tokenizer { + name, + type_parameters, + scheme, + vocabulary, + axioms, + } => adt( + "ReasoningDeclaration", + "ReasoningDeclarationTokenizer", + vec![ + text(name), + list(type_parameters.iter().map(|item| text(item)).collect()), + enc_tokenizer_scheme(scheme), + enc_model_use(vocabulary), + list(axioms.iter().map(|item| text(item)).collect()), + ], + ), + SemanticDeclaration::ContextWindow { + name, + capacity, + overflow, + axioms, + } => adt( + "ReasoningDeclaration", + "ReasoningDeclarationContextWindow", + vec![ + text(name), + nat(*capacity), + enc_overflow_disposition(overflow), + list(axioms.iter().map(|item| text(item)).collect()), + ], + ), + SemanticDeclaration::TextGenerator { + name, + tokenizer, + window, + scorer, + max_tokens, + stop, + policy, + axioms, + } => adt( + "ReasoningDeclaration", + "ReasoningDeclarationTextGenerator", + vec![ + text(name), + enc_member(tokenizer), + enc_member(window), + enc_model_use(scorer), + nat(*max_tokens), + enc_member(stop), + enc_generation_policy(policy), + list(axioms.iter().map(|item| text(item)).collect()), + ], + ), + SemanticDeclaration::TextClassifier { + name, + tokenizer, + model, + axioms, + } => adt( + "ReasoningDeclaration", + "ReasoningDeclarationTextClassifier", + vec![ + text(name), + enc_member(tokenizer), + enc_model_use(model), + list(axioms.iter().map(|item| text(item)).collect()), + ], + ), + other => panic!("not a declaration of this module: {other:?}"), + } +} + +#[allow(clippy::too_many_lines)] +pub(super) fn example_reasoning_declaration(step: usize) -> SemanticDeclaration { + let rest = step / 8; + match step % 8 { + 0 => SemanticDeclaration::Logic { + name: format!("s{rest}"), + type_parameters: if rest % 3 == 2 { + Vec::new() + } else { + (rest..rest + 2).map(|next| format!("s{rest}")).collect() + }, + state: example_contract_state(rest), + relation: MemberRef { + module: (rest % 2 == 0).then(|| "M".to_owned()), + name: format!("m{rest}"), + }, + invariant: (rest % 3 != 2).then(|| example_logic_invariant(rest)), + ranking: (rest % 3 != 2).then(|| MemberRef { + module: (rest % 2 == 0).then(|| "M".to_owned()), + name: format!("m{rest}"), + }), + axioms: if rest % 3 == 2 { + Vec::new() + } else { + (rest..rest + 2).map(|next| format!("s{rest}")).collect() + }, + }, + 1 => SemanticDeclaration::InferenceRule { + name: format!("s{rest}"), + type_parameters: if rest % 3 == 2 { + Vec::new() + } else { + (rest..rest + 2).map(|next| format!("s{rest}")).collect() + }, + logic: example_model_use(rest), + binding: (rest % 3 != 2).then(|| example_rule_binding(rest)), + guard: SemanticTerm::Nat { + value: rest.to_string(), + }, + conclusion: SemanticTerm::Nat { + value: rest.to_string(), + }, + soundness: MemberRef { + module: (rest % 2 == 0).then(|| "M".to_owned()), + name: format!("m{rest}"), + }, + progress: (rest % 3 != 2).then(|| MemberRef { + module: (rest % 2 == 0).then(|| "M".to_owned()), + name: format!("m{rest}"), + }), + executable: rest % 2 == 0, + axioms: if rest % 3 == 2 { + Vec::new() + } else { + (rest..rest + 2).map(|next| format!("s{rest}")).collect() + }, + }, + 2 => SemanticDeclaration::Verifier { + name: format!("s{rest}"), + type_parameters: if rest % 3 == 2 { + Vec::new() + } else { + (rest..rest + 2).map(|next| format!("s{rest}")).collect() + }, + subject: example_model_binder(rest), + candidate: example_model_binder(rest), + specification: MemberRef { + module: (rest % 2 == 0).then(|| "M".to_owned()), + name: format!("m{rest}"), + }, + check: MemberRef { + module: (rest % 2 == 0).then(|| "M".to_owned()), + name: format!("m{rest}"), + }, + sound: MemberRef { + module: (rest % 2 == 0).then(|| "M".to_owned()), + name: format!("m{rest}"), + }, + complete: (rest % 3 != 2).then(|| MemberRef { + module: (rest % 2 == 0).then(|| "M".to_owned()), + name: format!("m{rest}"), + }), + axioms: if rest % 3 == 2 { + Vec::new() + } else { + (rest..rest + 2).map(|next| format!("s{rest}")).collect() + }, + }, + 3 => SemanticDeclaration::Reasoner { + name: format!("s{rest}"), + type_parameters: if rest % 3 == 2 { + Vec::new() + } else { + (rest..rest + 2).map(|next| format!("s{rest}")).collect() + }, + logic: (rest % 3 != 2).then(|| example_model_use(rest)), + observation: example_model_binder(rest), + observe: (rest % 3 != 2).then(|| SemanticTerm::Nat { + value: rest.to_string(), + }), + rules: if rest % 3 == 2 { + Vec::new() + } else { + (rest..rest + 2) + .map(|next| example_model_use(next)) + .collect() + }, + strategy: example_reasoning_strategy(rest), + answer: (rest % 3 != 2).then(|| example_reasoning_answer(rest)), + verifier: example_model_use(rest), + claims: if rest % 3 == 2 { + Vec::new() + } else { + (rest..rest + 2) + .map(|next| example_reasoning_claim(next)) + .collect() + }, + executable: rest % 2 == 0, + axioms: if rest % 3 == 2 { + Vec::new() + } else { + (rest..rest + 2).map(|next| format!("s{rest}")).collect() + }, + }, + 4 => SemanticDeclaration::Tokenizer { + name: format!("s{rest}"), + type_parameters: if rest % 3 == 2 { + Vec::new() + } else { + (rest..rest + 2).map(|next| format!("s{rest}")).collect() + }, + scheme: example_tokenizer_scheme(rest), + vocabulary: example_model_use(rest), + axioms: if rest % 3 == 2 { + Vec::new() + } else { + (rest..rest + 2).map(|next| format!("s{rest}")).collect() + }, + }, + 5 => SemanticDeclaration::ContextWindow { + name: format!("s{rest}"), + capacity: u64::try_from(rest).expect("a rest") + 1, + overflow: example_overflow_disposition(rest), + axioms: if rest % 3 == 2 { + Vec::new() + } else { + (rest..rest + 2).map(|next| format!("s{rest}")).collect() + }, + }, + 6 => SemanticDeclaration::TextGenerator { + name: format!("s{rest}"), + tokenizer: MemberRef { + module: (rest % 2 == 0).then(|| "M".to_owned()), + name: format!("m{rest}"), + }, + window: MemberRef { + module: (rest % 2 == 0).then(|| "M".to_owned()), + name: format!("m{rest}"), + }, + scorer: example_model_use(rest), + max_tokens: u64::try_from(rest).expect("a rest") + 1, + stop: MemberRef { + module: (rest % 2 == 0).then(|| "M".to_owned()), + name: format!("m{rest}"), + }, + policy: example_generation_policy(rest), + axioms: if rest % 3 == 2 { + Vec::new() + } else { + (rest..rest + 2).map(|next| format!("s{rest}")).collect() + }, + }, + _ => SemanticDeclaration::TextClassifier { + name: format!("s{rest}"), + tokenizer: MemberRef { + module: (rest % 2 == 0).then(|| "M".to_owned()), + name: format!("m{rest}"), + }, + model: example_model_use(rest), + axioms: if rest % 3 == 2 { + Vec::new() + } else { + (rest..rest + 2).map(|next| format!("s{rest}")).collect() + }, + }, + } +} + +/// Every serialized kind of a tagged variant. +const KINDS: &[&str] = &[ + "forward", + "search", + "generate_and_verify", + "initial_invariant", + "terminates", + "observation_invariant", + "answer_correct", + "greedy", + "sample", + "logic", + "inference_rule", + "verifier", + "reasoner", + "tokenizer", + "context_window", + "text_generator", + "text_classifier", +]; + +// ------------------------------------------------------------- the inputs + +fn is_reasoning(declaration: &SemanticDeclaration) -> bool { + !is_ordinary(declaration) && !is_model(declaration) +} + +fn enc_any(declaration: &SemanticDeclaration) -> Value { + if is_ordinary(declaration) { + adt( + "AnyDeclaration", + "AnyOrdinary", + vec![enc_declaration(declaration)], + ) + } else if is_model(declaration) { + adt( + "AnyDeclaration", + "AnyModel", + vec![enc_model_declaration(declaration)], + ) + } else { + adt( + "AnyDeclaration", + "AnyReasoning", + vec![enc_reasoning_declaration(declaration)], + ) + } +} + +fn module_run(module: &SemanticModule) -> Run { + ( + vec![ + text(&module.spec), + list(module.declarations.iter().map(enc_any).collect()), + ], + serde_json::to_value(text(&canonical(module))).expect("a value is JSON"), + ) +} + +/// Every distinct semantic module of every committed project that links. +fn committed_modules() -> Vec { + let mut out = BTreeMap::new(); + for project in linked_projects() { + for module in project.modules.values() { + if let Some(semantic) = &module.semantic { + out.insert(canonical(semantic), semantic.clone()); + } + } + } + out.into_values().collect() +} + +fn run(declaration: &SemanticDeclaration) -> Run { + ( + vec![enc_reasoning_declaration(declaration)], + serde_json::to_value(text(&canonical(declaration))).expect("a value is JSON"), + ) +} + +/// Every distinct reasoning or text declaration of every committed semantic +/// module that links. +fn committed() -> Vec { + let mut out = BTreeMap::new(); + for project in linked_projects() { + for module in project.modules.values() { + if let Some(semantic) = &module.semantic { + for declaration in semantic + .declarations + .iter() + .filter(|item| is_reasoning(item)) + { + out.insert(canonical(declaration), declaration.clone()); + } + } + } + } + out.into_values().collect() +} + +fn seeded_examples(count: usize, seed: u64) -> Vec { + let mut seeded = Seeded(seed); + (0..count) + .map(|_| example_reasoning_declaration(seeded.below(1_000_000))) + .collect() +} + +// ------------------------------------------------------------------- plants + +/// The serialized names a plant renames besides the kinds, each with the +/// member that holds it. +const NAMES: &[(&str, &str)] = &[ + ("breadth_first", "order"), + ("longest_match", "scheme"), + ("keep_last", "overflow"), + ("xorshift32", "generator"), +]; + +struct Plant { + label: String, + site: String, + replacement: String, + needle: String, +} + +/// One plant per serialized kind and per name of `NAMES`, each with the +/// JSON fragment its vector must hold. +fn plant_list() -> Vec { + let mut out: Vec = KINDS + .iter() + .map(|kind| Plant { + label: format!("the kind `{kind}` is misspelled"), + site: format!("(lit \"kind\"), (ctor LexLeanStd.Json::Json.JStr (lit \"{kind}\"))"), + replacement: format!( + "(lit \"kind\"), (ctor LexLeanStd.Json::Json.JStr (lit \"{kind}x\"))" + ), + needle: format!("\"kind\":\"{kind}\""), + }) + .collect(); + out.extend(NAMES.iter().map(|(name, holder)| Plant { + label: format!("the name `{name}` is misspelled"), + site: format!("(ctor LexLeanStd.Json::Json.JStr (lit \"{name}\"))"), + replacement: format!("(ctor LexLeanStd.Json::Json.JStr (lit \"{name}x\"))"), + needle: format!("\"{holder}\":\"{name}\""), + })); + out +} + +fn plants(examples: &[SemanticDeclaration]) -> usize { + let mut missed = Vec::new(); + let list = plant_list(); + for plant in &list { + let vector = examples + .iter() + .find(|example| canonical(*example).contains(&plant.needle)) + .unwrap_or_else(|| panic!("{}: no example holds {}", plant.label, plant.needle)); + let planted = support::P::bootstrap(); + let source = planted.read_raw("src/SemanticReasoning.lex.tex"); + assert_eq!( + source.matches(&plant.site).count(), + 1, + "{}: the plant site occurs once", + plant.label + ); + planted.write( + "src/SemanticReasoning.lex.tex", + &source.replacen(&plant.site, &plant.replacement, 1), + ); + let checked = support::checked_project(&planted); + let low = super::lower(&checked, &planted, MODULE, ROOTS[0].0); + match super::evaluate(&low, &[run(vector)], 1).first() { + Some(first) => eprintln!( + "BS-72: plant detected: {}: {}", + plant.label, + first.chars().take(120).collect::() + ), + None => missed.push(plant.label.clone()), + } + } + assert!( + missed.is_empty(), + "planted defects their own vector does not detect: {missed:?}" + ); + list.len() +} + +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-72: {}: 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" + ); + } +} + +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("SemanticReasoning"), + "SemanticReasoning is a linked module of the project" + ); + eligibility_of(&project, "SemanticReasoning", MODULE, &ROOTS); + + let examples: Vec = + (0..EXAMPLES).map(example_reasoning_declaration).collect(); + let distinct: BTreeSet = examples.iter().map(canonical).collect(); + let corpus = committed(); + assert!( + corpus.len() >= 10, + "the committed semantic modules carry {} distinct reasoning and text declarations", + corpus.len() + ); + let modules = committed_modules(); + assert!( + modules.len() >= 100 + && modules + .iter() + .any(|module| module.declarations.iter().any(is_model)), + "{} committed semantic modules, among them one with a model declaration", + modules.len() + ); + let normative = support::normative_host(); + let (evaluator_seeded, rustc_seeded) = if normative { + ( + seeded_examples(EVALUATOR_SEEDED, 0x0B55_0071), + seeded_examples(RUSTC_SEEDED, 0x0B55_1071), + ) + } else { + (Vec::new(), Vec::new()) + }; + eprintln!( + "BS-72: {} examples ({} distinct), {} committed declarations, {} committed modules, {} and {} seeded examples ({:?})", + examples.len(), + distinct.len(), + corpus.len(), + modules.len(), + EVALUATOR_SEEDED, + RUSTC_SEEDED, + 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 root = ROOTS[0].0; + if normative { + let mut runs: Vec = examples.iter().map(run).collect(); + runs.extend(corpus.iter().map(run)); + runs.extend(evaluator_seeded.iter().map(run)); + let disagreements = super::evaluate(&lowered[root], &runs, 5); + assert!( + disagreements.is_empty(), + "{root} and the Rust oracle disagree: {disagreements:?}" + ); + eprintln!( + "BS-72: {root}: the evaluator agrees on {} runs ({:?})", + runs.len(), + started.elapsed() + ); + let module_root = ROOTS[1].0; + let module_runs: Vec = modules + .iter() + .filter(|module| canonical(*module).len() <= EVALUATOR_BYTES) + .map(module_run) + .collect(); + let disagreements = super::evaluate(&lowered[module_root], &module_runs, 5); + assert!( + disagreements.is_empty(), + "{module_root} and the Rust oracle disagree: {disagreements:?}" + ); + eprintln!( + "BS-72: {module_root}: the evaluator agrees on {} modules ({:?})", + module_runs.len(), + started.elapsed() + ); + let planted = plants(&examples); + eprintln!( + "BS-72: {planted} planted defects detected ({:?})", + started.elapsed() + ); + } else { + eprintln!("BS-72: platform-bound host: the evaluator, the seeded runs, and the planted defects run on the normative host"); + } + + let mut compiled: Vec = examples.iter().map(run).collect(); + compiled.extend(corpus.iter().map(run)); + compiled.extend(rustc_seeded.iter().map(run)); + let module_runs: Vec = modules.iter().map(module_run).collect(); + let total = rustc_differential( + "BS-72", + &[ + (root, &lowered[root], compiled.as_slice()), + (ROOTS[1].0, &lowered[ROOTS[1].0], module_runs.as_slice()), + ], + "oracle", + ); + eprintln!("BS-72: {total} rustc runs agree ({:?})", started.elapsed()); + + if !support::lean_backed("BS-72") { + return; + } + export(&lowered); + eprintln!("BS-72: passed in {:?}", started.elapsed()); +} diff --git a/crates/conformance/src/cases/repository.rs b/crates/conformance/src/cases/repository.rs index 584bca732e..8f9110ca76 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(), 500, "§31 has 500 rows"); + assert_eq!(table.len(), 501, "§31 has 501 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 f9080559c5..b3152a2e58 100644 --- a/crates/conformance/tests/conformance.rs +++ b/crates/conformance/tests/conformance.rs @@ -2430,6 +2430,11 @@ fn conformance_bs_71() { repo_conformance::cases::run("BS-71"); } +#[test] +fn conformance_bs_72() { + repo_conformance::cases::run("BS-72"); +} + #[test] fn conformance_pk_01() { repo_conformance::cases::run("PK-01"); diff --git a/features/suites/bootstrap.feature b/features/suites/bootstrap.feature index f3e0006be9..078e1e38b2 100644 --- a/features/suites/bootstrap.feature +++ b/features/suites/bootstrap.feature @@ -250,3 +250,9 @@ Feature: bootstrap When the project is checked and built, and the lowered program and the rendered package of the root are run on those declarations Then the root is eligible for rust-std with exactly its declared effects, no certificate part is beyond 3,145,728 bytes, and the root equals the canonical JSON of the serde form of the declaration on every run And every planted defect is detected by the first example that holds it, and the exported packages are byte-identical to the committed packages and realize the programs the differential ran + @BS-72 @build + Scenario: The declarations of logics, inference rules, verifiers, reasoners, tokenizers, context windows, text generators, and text classifiers of the semantic module language of SPEC.md §17.12, and the semantic module of declarations of every kind (those variants of `ir::semantic::SemanticDeclaration`, with the invariants, bindings, answers, search orders, strategies, claims, tokenizer schemes, overflow dispositions, and generation policies they hold, and `SemanticModule`, in the serde form the linked IR embeds, written as canonical JSON: each tagged variant by its `kind`, each closed name as the oracle serializes it, each optional member left out exactly where the oracle's serde attributes skip it, and a module's specification and declarations in order, its ordinary declarations through `SemanticDecls` and its model declarations through `SemanticModels`) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticReasoning`, generated with the case's encoders and examples from one description of the oracle's types: `reasoningDeclarationText` and `semanticModuleText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 on the shorter modules, agree with the oracle to the byte on examples that reach every variant, every optional member present and absent, and every list empty and not, on every distinct such declaration and every distinct semantic module of every committed project that links, and on seeded examples; each planted defect, a misspelled kind or name, is detected by the first example that holds it, and the certified packages the production export publishes for the project are the committed ones; reading declarations and modules from JSON is outside the claim. + Given the committed `bootstrap` project, examples that reach every variant and every optional member, every reasoning and text declaration and every semantic module of the committed projects that link, and seeded examples + When the project is checked and built, and the lowered program and the rendered package of each root are run on those inputs + Then each root is eligible for rust-std with exactly its declared effects, no certificate part is beyond 3,145,728 bytes, and the two roots equal the canonical JSON of the serde form of the declaration and of `SemanticModule` on every run + And every planted defect is detected by the first example that holds it, 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 69730e3a23..e255920a08 100644 --- a/model/ids.toml +++ b/model/ids.toml @@ -2918,6 +2918,12 @@ level = "build" suite = "bootstrap" statement = "The declarations of artifacts, contracts, realizations, evidence, and models of the semantic module language of SPEC.md §17.12 (those variants of `ir::semantic::SemanticDeclaration`, with the artifact roles and schemas, binders, states, validators, uses, rules, the deterministic, rule, statistical, neural, and composite realization descriptors with their layers, decoders, forms, and junctions, and the evidence claims they hold, in the serde form the linked IR embeds, written as canonical JSON: each tagged variant by its `kind`, each closed name as the oracle serializes it, and each optional member left out exactly where the oracle's serde attributes skip it) is language-1.2 source in the `bootstrap/` project, as the production root `modelDeclarationText` of `SemanticModels` over `SemanticTerms` and `SemanticTypes`, generated with the case's encoders and examples from one description of the oracle's types, 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 examples that reach every variant, every optional member present and absent, and every list empty and not, on every distinct such declaration of every committed semantic module that links, and on seeded examples; each planted defect, a misspelled kind or name, is detected by the first example that holds it, and the certified packages the production export publishes for the project are the committed ones; reading declarations from JSON is outside the claim." +[[id]] +id = "BS-72" +level = "build" +suite = "bootstrap" +statement = "The declarations of logics, inference rules, verifiers, reasoners, tokenizers, context windows, text generators, and text classifiers of the semantic module language of SPEC.md §17.12, and the semantic module of declarations of every kind (those variants of `ir::semantic::SemanticDeclaration`, with the invariants, bindings, answers, search orders, strategies, claims, tokenizer schemes, overflow dispositions, and generation policies they hold, and `SemanticModule`, in the serde form the linked IR embeds, written as canonical JSON: each tagged variant by its `kind`, each closed name as the oracle serializes it, each optional member left out exactly where the oracle's serde attributes skip it, and a module's specification and declarations in order, its ordinary declarations through `SemanticDecls` and its model declarations through `SemanticModels`) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticReasoning`, generated with the case's encoders and examples from one description of the oracle's types: `reasoningDeclarationText` and `semanticModuleText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 on the shorter modules, agree with the oracle to the byte on examples that reach every variant, every optional member present and absent, and every list empty and not, on every distinct such declaration and every distinct semantic module of every committed project that links, and on seeded examples; each planted defect, a misspelled kind or name, is detected by the first example that holds it, and the certified packages the production export publishes for the project are the committed ones; reading declarations and modules from JSON 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 4689a2d9bb..0dbebe02f1 100644 --- a/tests/production-export/bootstrap/roots.json +++ b/tests/production-export/bootstrap/roots.json @@ -655,6 +655,22 @@ "rust-std" ], "version": "1.0.0" + }, + { + "name": "reasoning_declaration_text", + "root": "LexLeanBoot.SemanticReasoning.reasoningDeclarationText", + "targets": [ + "rust-std" + ], + "version": "1.0.0" + }, + { + "name": "whole_semantic_module_text", + "root": "LexLeanBoot.SemanticReasoning.semanticModuleText", + "targets": [ + "rust-std" + ], + "version": "1.0.0" } ] } diff --git a/xtask/src/spec_links.rs b/xtask/src/spec_links.rs index 40df6926ea..fb3364c264 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() != 500 { - return Err(format!("RP-07: the §31 table has {} rows, not 500", rows.len()).into()); + if rows.len() != 501 { + return Err(format!("RP-07: the §31 table has {} rows, not 501", 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 30ad21e110b41331502dd075399e08eb17c633c6 Mon Sep 17 00:00:00 2001 From: humuhumu33 Date: Thu, 12 Nov 2026 15:49:21 +0000 Subject: [PATCH 06/22] BS-72: the generated encoders and examples pass clippy Co-Authored-By: Claude Opus 5.5 --- .../src/cases/bootstrap/semantic_reasoning.rs | 116 ++++++++---------- 1 file changed, 54 insertions(+), 62 deletions(-) diff --git a/crates/conformance/src/cases/bootstrap/semantic_reasoning.rs b/crates/conformance/src/cases/bootstrap/semantic_reasoning.rs index d2c3d46ac1..e585bdfa57 100755 --- a/crates/conformance/src/cases/bootstrap/semantic_reasoning.rs +++ b/crates/conformance/src/cases/bootstrap/semantic_reasoning.rs @@ -141,11 +141,11 @@ pub(super) fn enc_logic_invariant(value: &LogicInvariant) -> Value { pub(super) fn example_logic_invariant(step: usize) -> LogicInvariant { LogicInvariant { predicate: MemberRef { - module: (step % 2 == 0).then(|| "M".to_owned()), + module: step.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{step}"), }, preserves: MemberRef { - module: (step % 2 == 0).then(|| "M".to_owned()), + module: step.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{step}"), }, } @@ -166,7 +166,7 @@ pub(super) fn enc_rule_binding(value: &RuleBinding) -> Value { pub(super) fn example_rule_binding(step: usize) -> RuleBinding { RuleBinding { name: format!("s{step}"), - r#type: if step % 2 == 0 { + r#type: if step.is_multiple_of(2) { SemanticType::Nat } else { SemanticType::Bytes @@ -192,7 +192,7 @@ pub(super) fn enc_reasoning_answer(value: &ReasoningAnswer) -> Value { pub(super) fn example_reasoning_answer(step: usize) -> ReasoningAnswer { ReasoningAnswer { name: format!("s{step}"), - r#type: if step % 2 == 0 { + r#type: if step.is_multiple_of(2) { SemanticType::Nat } else { SemanticType::Bytes @@ -219,7 +219,7 @@ pub(super) fn enc_reasoning_strategy(value: &ReasoningStrategy) -> Value { ReasoningStrategy::Forward { fuel } => adt( "ReasoningStrategy", "ReasoningStrategyForward", - vec![option(fuel.as_ref().map(|item| enc_term(item)))], + vec![option(fuel.as_ref().map(enc_term))], ), ReasoningStrategy::Search { order, @@ -231,8 +231,8 @@ pub(super) fn enc_reasoning_strategy(value: &ReasoningStrategy) -> Value { "ReasoningStrategySearch", vec![ enc_search_order(order), - option(fuel.as_ref().map(|item| enc_term(item))), - option(frontier.as_ref().map(|item| enc_term(item))), + option(fuel.as_ref().map(enc_term)), + option(frontier.as_ref().map(enc_term)), Value::Bool { value: *deduplicate, }, @@ -241,10 +241,7 @@ pub(super) fn enc_reasoning_strategy(value: &ReasoningStrategy) -> Value { ReasoningStrategy::GenerateAndVerify { generator, budget } => adt( "ReasoningStrategy", "ReasoningStrategyGenerateAndVerify", - vec![ - enc_term(generator), - option(budget.as_ref().map(|item| enc_term(item))), - ], + vec![enc_term(generator), option(budget.as_ref().map(enc_term))], ), } } @@ -266,7 +263,7 @@ pub(super) fn example_reasoning_strategy(step: usize) -> ReasoningStrategy { frontier: (rest % 3 != 2).then(|| SemanticTerm::Nat { value: rest.to_string(), }), - deduplicate: rest % 2 == 0, + deduplicate: rest.is_multiple_of(2), }, _ => ReasoningStrategy::GenerateAndVerify { generator: SemanticTerm::Nat { @@ -318,33 +315,33 @@ pub(super) fn example_reasoning_claim(step: usize) -> ReasoningClaim { match step % 4 { 0 => ReasoningClaim::InitialInvariant { theorem: MemberRef { - module: (rest % 2 == 0).then(|| "M".to_owned()), + module: rest.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{rest}"), }, }, 1 => ReasoningClaim::Terminates { theorem: MemberRef { - module: (rest % 2 == 0).then(|| "M".to_owned()), + module: rest.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{rest}"), }, }, 2 => ReasoningClaim::ObservationInvariant { predicate: MemberRef { - module: (rest % 2 == 0).then(|| "M".to_owned()), + module: rest.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{rest}"), }, initial: MemberRef { - module: (rest % 2 == 0).then(|| "M".to_owned()), + module: rest.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{rest}"), }, preserved: MemberRef { - module: (rest % 2 == 0).then(|| "M".to_owned()), + module: rest.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{rest}"), }, }, _ => ReasoningClaim::AnswerCorrect { theorem: MemberRef { - module: (rest % 2 == 0).then(|| "M".to_owned()), + module: rest.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{rest}"), }, }, @@ -457,8 +454,8 @@ pub(super) fn enc_reasoning_declaration(value: &SemanticDeclaration) -> Value { list(type_parameters.iter().map(|item| text(item)).collect()), enc_contract_state(state), enc_member(relation), - option(invariant.as_ref().map(|item| enc_logic_invariant(item))), - option(ranking.as_ref().map(|item| enc_member(item))), + option(invariant.as_ref().map(enc_logic_invariant)), + option(ranking.as_ref().map(enc_member)), list(axioms.iter().map(|item| text(item)).collect()), ], ), @@ -480,11 +477,11 @@ pub(super) fn enc_reasoning_declaration(value: &SemanticDeclaration) -> Value { text(name), list(type_parameters.iter().map(|item| text(item)).collect()), enc_model_use(logic), - option(binding.as_ref().map(|item| enc_rule_binding(item))), + option(binding.as_ref().map(enc_rule_binding)), enc_term(guard), enc_term(conclusion), enc_member(soundness), - option(progress.as_ref().map(|item| enc_member(item))), + option(progress.as_ref().map(enc_member)), Value::Bool { value: *executable }, list(axioms.iter().map(|item| text(item)).collect()), ], @@ -510,7 +507,7 @@ pub(super) fn enc_reasoning_declaration(value: &SemanticDeclaration) -> Value { enc_member(specification), enc_member(check), enc_member(sound), - option(complete.as_ref().map(|item| enc_member(item))), + option(complete.as_ref().map(enc_member)), list(axioms.iter().map(|item| text(item)).collect()), ], ), @@ -533,19 +530,14 @@ pub(super) fn enc_reasoning_declaration(value: &SemanticDeclaration) -> Value { vec![ text(name), list(type_parameters.iter().map(|item| text(item)).collect()), - option(logic.as_ref().map(|item| enc_model_use(item))), + option(logic.as_ref().map(enc_model_use)), enc_model_binder(observation), - option(observe.as_ref().map(|item| enc_term(item))), - list(rules.iter().map(|item| enc_model_use(item)).collect()), + option(observe.as_ref().map(enc_term)), + list(rules.iter().map(enc_model_use).collect()), enc_reasoning_strategy(strategy), - option(answer.as_ref().map(|item| enc_reasoning_answer(item))), + option(answer.as_ref().map(enc_reasoning_answer)), enc_model_use(verifier), - list( - claims - .iter() - .map(|item| enc_reasoning_claim(item)) - .collect(), - ), + list(claims.iter().map(enc_reasoning_claim).collect()), Value::Bool { value: *executable }, list(axioms.iter().map(|item| text(item)).collect()), ], @@ -633,22 +625,22 @@ pub(super) fn example_reasoning_declaration(step: usize) -> SemanticDeclaration type_parameters: if rest % 3 == 2 { Vec::new() } else { - (rest..rest + 2).map(|next| format!("s{rest}")).collect() + (rest..rest + 2).map(|next| format!("s{next}")).collect() }, state: example_contract_state(rest), relation: MemberRef { - module: (rest % 2 == 0).then(|| "M".to_owned()), + module: rest.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{rest}"), }, invariant: (rest % 3 != 2).then(|| example_logic_invariant(rest)), ranking: (rest % 3 != 2).then(|| MemberRef { - module: (rest % 2 == 0).then(|| "M".to_owned()), + module: rest.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{rest}"), }), axioms: if rest % 3 == 2 { Vec::new() } else { - (rest..rest + 2).map(|next| format!("s{rest}")).collect() + (rest..rest + 2).map(|next| format!("s{next}")).collect() }, }, 1 => SemanticDeclaration::InferenceRule { @@ -656,7 +648,7 @@ pub(super) fn example_reasoning_declaration(step: usize) -> SemanticDeclaration type_parameters: if rest % 3 == 2 { Vec::new() } else { - (rest..rest + 2).map(|next| format!("s{rest}")).collect() + (rest..rest + 2).map(|next| format!("s{next}")).collect() }, logic: example_model_use(rest), binding: (rest % 3 != 2).then(|| example_rule_binding(rest)), @@ -667,18 +659,18 @@ pub(super) fn example_reasoning_declaration(step: usize) -> SemanticDeclaration value: rest.to_string(), }, soundness: MemberRef { - module: (rest % 2 == 0).then(|| "M".to_owned()), + module: rest.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{rest}"), }, progress: (rest % 3 != 2).then(|| MemberRef { - module: (rest % 2 == 0).then(|| "M".to_owned()), + module: rest.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{rest}"), }), - executable: rest % 2 == 0, + executable: rest.is_multiple_of(2), axioms: if rest % 3 == 2 { Vec::new() } else { - (rest..rest + 2).map(|next| format!("s{rest}")).collect() + (rest..rest + 2).map(|next| format!("s{next}")).collect() }, }, 2 => SemanticDeclaration::Verifier { @@ -686,30 +678,30 @@ pub(super) fn example_reasoning_declaration(step: usize) -> SemanticDeclaration type_parameters: if rest % 3 == 2 { Vec::new() } else { - (rest..rest + 2).map(|next| format!("s{rest}")).collect() + (rest..rest + 2).map(|next| format!("s{next}")).collect() }, subject: example_model_binder(rest), candidate: example_model_binder(rest), specification: MemberRef { - module: (rest % 2 == 0).then(|| "M".to_owned()), + module: rest.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{rest}"), }, check: MemberRef { - module: (rest % 2 == 0).then(|| "M".to_owned()), + module: rest.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{rest}"), }, sound: MemberRef { - module: (rest % 2 == 0).then(|| "M".to_owned()), + module: rest.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{rest}"), }, complete: (rest % 3 != 2).then(|| MemberRef { - module: (rest % 2 == 0).then(|| "M".to_owned()), + module: rest.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{rest}"), }), axioms: if rest % 3 == 2 { Vec::new() } else { - (rest..rest + 2).map(|next| format!("s{rest}")).collect() + (rest..rest + 2).map(|next| format!("s{next}")).collect() }, }, 3 => SemanticDeclaration::Reasoner { @@ -717,7 +709,7 @@ pub(super) fn example_reasoning_declaration(step: usize) -> SemanticDeclaration type_parameters: if rest % 3 == 2 { Vec::new() } else { - (rest..rest + 2).map(|next| format!("s{rest}")).collect() + (rest..rest + 2).map(|next| format!("s{next}")).collect() }, logic: (rest % 3 != 2).then(|| example_model_use(rest)), observation: example_model_binder(rest), @@ -741,11 +733,11 @@ pub(super) fn example_reasoning_declaration(step: usize) -> SemanticDeclaration .map(|next| example_reasoning_claim(next)) .collect() }, - executable: rest % 2 == 0, + executable: rest.is_multiple_of(2), axioms: if rest % 3 == 2 { Vec::new() } else { - (rest..rest + 2).map(|next| format!("s{rest}")).collect() + (rest..rest + 2).map(|next| format!("s{next}")).collect() }, }, 4 => SemanticDeclaration::Tokenizer { @@ -753,60 +745,60 @@ pub(super) fn example_reasoning_declaration(step: usize) -> SemanticDeclaration type_parameters: if rest % 3 == 2 { Vec::new() } else { - (rest..rest + 2).map(|next| format!("s{rest}")).collect() + (rest..rest + 2).map(|next| format!("s{next}")).collect() }, scheme: example_tokenizer_scheme(rest), vocabulary: example_model_use(rest), axioms: if rest % 3 == 2 { Vec::new() } else { - (rest..rest + 2).map(|next| format!("s{rest}")).collect() + (rest..rest + 2).map(|next| format!("s{next}")).collect() }, }, 5 => SemanticDeclaration::ContextWindow { name: format!("s{rest}"), - capacity: u64::try_from(rest).expect("a rest") + 1, + capacity: u64::try_from(rest).expect("a step") + 1, overflow: example_overflow_disposition(rest), axioms: if rest % 3 == 2 { Vec::new() } else { - (rest..rest + 2).map(|next| format!("s{rest}")).collect() + (rest..rest + 2).map(|next| format!("s{next}")).collect() }, }, 6 => SemanticDeclaration::TextGenerator { name: format!("s{rest}"), tokenizer: MemberRef { - module: (rest % 2 == 0).then(|| "M".to_owned()), + module: rest.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{rest}"), }, window: MemberRef { - module: (rest % 2 == 0).then(|| "M".to_owned()), + module: rest.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{rest}"), }, scorer: example_model_use(rest), - max_tokens: u64::try_from(rest).expect("a rest") + 1, + max_tokens: u64::try_from(rest).expect("a step") + 1, stop: MemberRef { - module: (rest % 2 == 0).then(|| "M".to_owned()), + module: rest.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{rest}"), }, policy: example_generation_policy(rest), axioms: if rest % 3 == 2 { Vec::new() } else { - (rest..rest + 2).map(|next| format!("s{rest}")).collect() + (rest..rest + 2).map(|next| format!("s{next}")).collect() }, }, _ => SemanticDeclaration::TextClassifier { name: format!("s{rest}"), tokenizer: MemberRef { - module: (rest % 2 == 0).then(|| "M".to_owned()), + module: rest.is_multiple_of(2).then(|| "M".to_owned()), name: format!("m{rest}"), }, model: example_model_use(rest), axioms: if rest % 3 == 2 { Vec::new() } else { - (rest..rest + 2).map(|next| format!("s{rest}")).collect() + (rest..rest + 2).map(|next| format!("s{next}")).collect() }, }, } From 3f0096fdc317d5897719fbbeb6f03d4b66f3a51b Mon Sep 17 00:00:00 2001 From: humuhumu33 Date: Thu, 12 Nov 2026 18:48:30 +0000 Subject: [PATCH 07/22] ci: the bootstrap export's custody names executable addresses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The custody record becomes lexlean/ci-bootstrap-export/2. Beside what `rustc -vV` and `lean --version` report, it names the SHA-256 of the pinned toolchain's lake, lean and leanchecker and the address of the rustc that compiled the exporting binary (SPEC.md §22.11). A leg whose own executables differ refuses the export. A version banner is the same for a rebuilt or patched binary; the bytes are not. RP-14 plants another leanchecker and another rustc. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 8 +++- SPEC.md | 2 +- crates/conformance/src/cases/repository.rs | 30 +++++++++++++++ crates/conformance/src/export_custody.rs | 43 +++++++++++++++++++--- 4 files changed, 75 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07b3b22200..0f4c7ee698 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,8 +27,12 @@ workspace carried at those versions without a tag. names only the executable it ran, so a part that ran another `lake`, `lexlean`, or `rustc` was accepted before. `VR-23` plants another `lake` address in a shard's output, and on unix a join whose own `lake` differs by - one byte. Published directories, attestations, and `expected/verify` are - unchanged. + one byte. The bootstrap export's custody record becomes + `lexlean/ci-bootstrap-export/2`: it also names the SHA-256 of the pinned + `lake`, `lean`, and `leanchecker` and the address of the `rustc` that built + the exporting binary, and a leg whose own differ refuses the export (`RP-14` + plants another `leanchecker` and another `rustc`). Published directories, + attestations, and `expected/verify` are unchanged. - A one-pass writer for the counts of the bootstrap count cases, #92. Under `LEXLEAN_COUNTS=fast` each of `BS-05` to `BS-08` and `BS-11` to `BS-15` records every count it checks in `target/count-report/` instead of diff --git a/SPEC.md b/SPEC.md index 21cc5ca7b7..32d68ad314 100644 --- a/SPEC.md +++ b/SPEC.md @@ -703,7 +703,7 @@ The `bdd` recipe runs as the parts of one run as well. `.github/bdd-parts.json` Every normative gate job runs on the standard GitHub-hosted runner, `ubuntu-24.04`; no larger runner is part of the gate. The supported-host execution jobs run on their named hosts (§8.3); the Intel and Windows aggregates run on `ubuntu-24.04` and execute no host test. The verification resource profile (§22.11) of a job is named by its matrix row, or on an Atlas job by the job itself, and by nothing else, no repository or workflow variable and no input: the `examples` leg and every Atlas job verify at width 2, with the scheduling hints committed in `.github/verify-hints.json` (derived from a run's telemetry by `cargo xtask ci-hints`) and a memory budget of 12 GiB (12582912 KiB) for the summed expected peaks of the proof processes running at once; the other legs name no profile and run at the default. A profile is a schedule and never a verdict: every elaboration, replay, and audit member still runs on the pinned toolchain, the directory each project publishes is byte for byte the one of width 1 without hints or a budget (§22.11), and the leg and the join still require every example's normalized verification records to equal `expected/verify` (§29.5), so a profile that changed a byte would fail the gate rather than pass a different one. A stale hint reorders processes and never skips one. Local `just vv` verifies UOR Atlas whole at width 1 with no profile. The budget is below the runner's 16 GB, with the 4 GiB swap file of the job as overflow, so two processes run together only when their expected peaks fit, and a process whose expected peak alone passes the budget runs alone. The gate has no path filter, no cache of a build directory, object, verification tree, attestation, or proof result, and no condition that skips a recipe of `just vv` but the plan's, which decides only what a run is, as above; the artifacts the jobs of one run exchange are read by that run alone, and every job starts from a clean checkout with the exact pinned toolchains. Each proof or fan-out job writes its performance telemetry (§22.12) to `ci-evidence/` and uploads that directory as the artifact named after it, `acceptance-gate-evidence-` or `acceptance-gate-evidence-atlas-`: the gate log (`just --timestamp` stamps each recipe's first command on a leg, which is how a recipe is timed without changing it; an Atlas job's log is the recipe's own output), the host's memory and swap sampled every five seconds, the verifier's telemetry, and a summary of the wall time of each recipe and verification stage, the slowest modules and audit members, and the peak resident set and swap of the proof processes. The summary step also prints that summary to the job log and appends it to the step summary, and runs when the job failed, because an uploaded artifact cannot be read from the log; each table lists only its slowest rows and the part appended to the step summary is cut at 512 KiB, half of what GitHub accepts. The initial honesty job retains its recipe timing and summary in the same evidence format, without Lean processes or proof-resource samples. None of it is an input of any recipe, and a timing is never a claim about a semantic property. -The bootstrap production export (§24.7) is made once per run. Every bootstrap case compares the packages of the bootstrap project's export with the committed packages, and a test process that needs the export exports the project, which verifies it whole; each part that ran a bootstrap case ran that verification. In a run that is the whole gate, the job `bootstrap-export` runs `cargo xtask export-bootstrap` from its clean checkout after honesty, at the profile of the `examples` leg, requires every exported package to equal the committed package, and uploads the bundle as the artifact `bootstrap-export` with a custody record (`lexlean/ci-bootstrap-export/1`) that names the repository, the run and its attempt, the commit and its tree, the SHA-256 of `rust-toolchain.toml`, `lean-toolchain` and `Cargo.lock`, what `rustc -vV` and `lean --version` report, the bundle's project-relative path, and the digest of its files. The legs do not wait for that job to start: each leg of such a run starts `.github/scripts/take-bootstrap-export.sh`, which asks nothing until a case of the leg needs the export, then downloads that artifact of its own run, or records that the export job ended without it, and the case fails with that reason. The harness names the directory it waits in through `LEXLEAN_BOOTSTRAP_EXPORT`; before any case reads the bundle it requires the record to name the leg's own repository, run, commit, tree, pins and toolchains, an attempt at most the leg's (a rerun of failed jobs keeps the export that passed, on the same commit), the digest of the files it received, and a path below the project, and it replays the bundle (§24.7). A record that differs in any of these fails the case. Unset or empty, as in `just vv` and in a run that is not the whole gate, each process that needs the export runs it itself. The export crosses no run, as the Atlas parts do not; `RP-14` checks the job, the legs' fetcher, and the record against planted changes. +The bootstrap production export (§24.7) is made once per run. Every bootstrap case compares the packages of the bootstrap project's export with the committed packages, and a test process that needs the export exports the project, which verifies it whole; each part that ran a bootstrap case ran that verification. In a run that is the whole gate, the job `bootstrap-export` runs `cargo xtask export-bootstrap` from its clean checkout after honesty, at the profile of the `examples` leg, requires every exported package to equal the committed package, and uploads the bundle as the artifact `bootstrap-export` with a custody record (`lexlean/ci-bootstrap-export/2`) that names the repository, the run and its attempt, the commit and its tree, the SHA-256 of `rust-toolchain.toml`, `lean-toolchain` and `Cargo.lock`, what `rustc -vV` and `lean --version` report, the SHA-256 of the pinned toolchain's `lake`, `lean` and `leanchecker` executables and the address of the `rustc` that compiled the job's own binary (§22.11), the bundle's project-relative path, and the digest of its files. The legs do not wait for that job to start: each leg of such a run starts `.github/scripts/take-bootstrap-export.sh`, which asks nothing until a case of the leg needs the export, then downloads that artifact of its own run, or records that the export job ended without it, and the case fails with that reason. The harness names the directory it waits in through `LEXLEAN_BOOTSTRAP_EXPORT`; before any case reads the bundle it requires the record to name the leg's own repository, run, commit, tree, pins, toolchains and executable addresses, an attempt at most the leg's (a rerun of failed jobs keeps the export that passed, on the same commit), the digest of the files it received, and a path below the project, and it replays the bundle (§24.7). A record that differs in any of these fails the case. Unset or empty, as in `just vv` and in a run that is not the whole gate, each process that needs the export runs it itself. The export crosses no run, as the Atlas parts do not; `RP-14` checks the job, the legs' fetcher, and the record against planted changes. The Intel host factory is separate from the normative BDD and Atlas partitions above: it transfers only the same-run host test construction and dependency data of §8.3. Construction requires successful honesty; consumers require construction; the aggregate requires all of them and cannot skip an incomplete result. The factory's behavioral tests run in the initial honesty job, before the expensive roots, and exercise inventory completeness, custody and fail-closed result joins without needing an Intel runner. These tests do not substitute for execution of the complete workload on Intel macOS. diff --git a/crates/conformance/src/cases/repository.rs b/crates/conformance/src/cases/repository.rs index 9c227c41dd..e52b0edccf 100644 --- a/crates/conformance/src/cases/repository.rs +++ b/crates/conformance/src/cases/repository.rs @@ -3496,6 +3496,15 @@ fn bootstrap_custody_decisions() { .into_iter() .map(|(name, text)| (name.to_owned(), text.to_owned())) .collect(); + let executables: BTreeMap = [ + ("lake", "5"), + ("lean", "6"), + ("leanchecker", "7"), + ("rustc", "8"), + ] + .into_iter() + .map(|(name, digit)| (name.to_owned(), digit.repeat(64))) + .collect(); let here = Identity { repository: "UOR-Foundation/LexLean".to_owned(), run_id: "100".to_owned(), @@ -3504,6 +3513,7 @@ fn bootstrap_custody_decisions() { tree: "2".repeat(40), pins: pins.clone(), toolchains: toolchains.clone(), + executables: executables.clone(), }; let bundle = ("d".repeat(64), 162); let record = Custody { @@ -3515,6 +3525,7 @@ fn bootstrap_custody_decisions() { tree: here.tree.clone(), pins, toolchains, + executables, bundle_path: ".lexlean/production/bundle".to_owned(), bundle_sha256: bundle.0.clone(), bundle_files: bundle.1, @@ -3550,6 +3561,25 @@ fn bootstrap_custody_decisions() { .insert("rustc".to_owned(), "rustc 1.97.0".to_owned()); }), ), + // An export whose toolchain reports the pinned versions but whose + // executables are other bytes: a changed `leanchecker`, or a binary + // built by another `rustc` (§22.11). + ( + "executables", + Box::new(|record| { + record + .executables + .insert("leanchecker".to_owned(), "9".repeat(64)); + }), + ), + ( + "executables", + Box::new(|record| { + record + .executables + .insert("rustc".to_owned(), "0".repeat(64)); + }), + ), ( "bundle", Box::new(|record| record.bundle_sha256 = "f".repeat(64)), diff --git a/crates/conformance/src/export_custody.rs b/crates/conformance/src/export_custody.rs index 0f6c5fff54..a1bc4d5101 100644 --- a/crates/conformance/src/export_custody.rs +++ b/crates/conformance/src/export_custody.rs @@ -2,8 +2,9 @@ //! (SPEC.md §9.4). The `bootstrap-export` job exports the bootstrap project //! once, requires its packages to equal the committed packages, and uploads //! the bundle with a custody record that binds it to the repository, the -//! run and its attempt, the commit and its tree, the toolchain pins and the -//! toolchains that ran, and the bundle's bytes. A conformance case of the same +//! run and its attempt, the commit and its tree, the toolchain pins, the +//! toolchains that ran and the addresses of their executables, and the +//! bundle's bytes. A conformance case of the same //! run that needs the export takes it from there instead of exporting the //! project again: the harness names the directory it waits in through //! [`VARIABLE`], checks the record against its own run and checkout, replays @@ -26,7 +27,7 @@ use crate::support::{self, P}; pub const VARIABLE: &str = "LEXLEAN_BOOTSTRAP_EXPORT"; /// The custody record's format. -pub const SPEC: &str = "lexlean/ci-bootstrap-export/1"; +pub const SPEC: &str = "lexlean/ci-bootstrap-export/2"; /// The custody record, beside the bundle in the uploaded directory. pub const CUSTODY_FILE: &str = "custody.json"; @@ -49,6 +50,10 @@ pub const ARTIFACT_DIR: &str = "artifact"; /// The repository files whose bytes pin the toolchains and the dependencies. pub const PINS: [&str; 3] = ["rust-toolchain.toml", "lean-toolchain", "Cargo.lock"]; +/// The executables of the pinned Lean toolchain a custody record addresses, +/// beside `rustc` (SPEC.md §22.11). +pub const LEAN_EXECUTABLES: [&str; 3] = ["lake", "lean", "leanchecker"]; + /// The longest a case waits for the run's export: the export job's own /// limit, after which the job has failed and the fetcher says so. const WAIT: Duration = Duration::from_secs(6 * 60 * 60); @@ -73,6 +78,10 @@ pub struct Custody { pub pins: BTreeMap, /// What the toolchains report: `rustc -vV` and `lean --version`. pub toolchains: BTreeMap, + /// The address of each executable: the SHA-256 of `lake`, `lean`, and + /// `leanchecker` of the pinned toolchain, and the address of the `rustc` + /// that compiled the job's own binary (§22.11). + pub executables: BTreeMap, /// The project-relative bundle directory of the export. pub bundle_path: String, /// [`bundle_digest`] of the bundle. @@ -98,6 +107,8 @@ pub struct Identity { pub pins: BTreeMap, /// What the toolchains report. pub toolchains: BTreeMap, + /// The address of each executable. + pub executables: BTreeMap, } fn sha256_hex(bytes: &[u8]) -> String { @@ -130,7 +141,8 @@ fn run(root: &Utf8Path, program: &str, arguments: &[&str]) -> Result Result { let variable = |name: &str| -> Result { std::env::var(name) @@ -169,6 +181,19 @@ pub fn identity(root: &Utf8Path) -> Result { ("lean".to_owned(), run(root, "lean", &["--version"])?), ("rustc".to_owned(), run(root, "rustc", &["-vV"])?), ]); + let bin = lexlean::verify::toolchain::toolchain_root() + .map_err(|error| format!("the pinned toolchain: {}", error.message))? + .join("bin"); + let mut executables = BTreeMap::new(); + for name in LEAN_EXECUTABLES { + let bytes = std::fs::read(bin.join(name).as_std_path()) + .map_err(|error| format!("{bin}/{name}: {error}"))?; + executables.insert(name.to_owned(), sha256_hex(&bytes)); + } + executables.insert( + "rustc".to_owned(), + lexlean::build_identity::RUSTC_SHA256.to_owned(), + ); Ok(Identity { repository, run_id, @@ -177,6 +202,7 @@ pub fn identity(root: &Utf8Path) -> Result { tree: run(root, "git", &["rev-parse", "HEAD^{tree}"])?, pins, toolchains, + executables, }) } @@ -261,6 +287,12 @@ pub fn check(custody: &Custody, current: &Identity, bundle: &(String, u64)) -> R ¤t.toolchains, custody.toolchains == current.toolchains, ); + same( + "executables", + &custody.executables, + ¤t.executables, + custody.executables == current.executables, + ); same( "bundle", &(&custody.bundle_sha256, custody.bundle_files), @@ -330,6 +362,7 @@ pub fn publish(output: &Utf8Path) -> Result { tree: before.tree, pins: before.pins, toolchains: before.toolchains, + executables: before.executables, bundle_path: exported.output.bundle.to_string(), bundle_sha256, bundle_files, @@ -349,7 +382,7 @@ pub fn publish(output: &Utf8Path) -> Result { /// # Errors /// /// When the run has no export, the record names another run, commit, tree, -/// pin, toolchain, or bundle, or the bundle does not replay. +/// pin, toolchain, executable, or bundle, or the bundle does not replay. pub fn take(directory: &Utf8Path) -> Result { std::fs::create_dir_all(directory.as_std_path()) .and_then(|()| std::fs::write(directory.join(WANTED).as_std_path(), b"")) From 86b2e5026dbf2023b1e684a29ec64a9c4994973b Mon Sep 17 00:00:00 2001 From: humuhumu33 Date: Thu, 12 Nov 2026 18:55:58 +0000 Subject: [PATCH 08/22] BS-72: the writers declare no axioms; the examples map with their builders Co-Authored-By: Claude Opus 5.5 --- bootstrap/src/SemanticReasoning.lex.tex | 13 ------------- .../src/cases/bootstrap/semantic_reasoning.rs | 8 ++------ 2 files changed, 2 insertions(+), 19 deletions(-) diff --git a/bootstrap/src/SemanticReasoning.lex.tex b/bootstrap/src/SemanticReasoning.lex.tex index 63dc582726..28caaaaa18 100755 --- a/bootstrap/src/SemanticReasoning.lex.tex +++ b/bootstrap/src/SemanticReasoning.lex.tex @@ -234,7 +234,6 @@ result: LexLeanStd.Json::Json, body: (match value | LogicInvariant.LogicInvariantOf part0 part1 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "predicate"), (SemanticTerms::memberJson (first part0) (second part0))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "preserves"), (SemanticTerms::memberJson (first part1) (second part1))) entries1); (objectOf entries2)))), - axioms: [Quot.sound, propext], executable: true } @@ -244,7 +243,6 @@ result: LexLeanStd.Json::Json, body: (match value | RuleBinding.RuleBindingOf part0 part1 part2 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "name"), (ctor LexLeanStd.Json::Json.JStr part0)) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "type"), (SemanticTypes::typeJson part1)) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "candidates"), (SemanticTerms::termJson part2)) entries2); (objectOf entries3))))), - axioms: [Quot.sound, propext], executable: true } @@ -254,7 +252,6 @@ result: LexLeanStd.Json::Json, body: (match value | ReasoningAnswer.ReasoningAnswerOf part0 part1 part2 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "name"), (ctor LexLeanStd.Json::Json.JStr part0)) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "type"), (SemanticTypes::typeJson part1)) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "value"), (SemanticTerms::termJson part2)) entries2); (objectOf entries3))))), - axioms: [Quot.sound, propext], executable: true } @@ -265,7 +262,6 @@ body: (match value | SearchOrder.SearchOrderBreadthFirst => (ctor LexLeanStd.Json::Json.JStr (lit "breadth_first")) | SearchOrder.SearchOrderDepthFirst => (ctor LexLeanStd.Json::Json.JStr (lit "depth_first"))), - axioms: [Quot.sound, propext], executable: true } @@ -285,7 +281,6 @@ | ReasoningStrategy.ReasoningStrategyGenerateAndVerify part0 part1 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "generate_and_verify"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "generator"), (SemanticTerms::termJson part0)) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (match part1 | Option.some present => (cons ((lit "budget"), (SemanticTerms::termJson present)) entries2) | Option.none => entries2); (objectOf entries3))))), - axioms: [Quot.sound, propext], executable: true } @@ -298,7 +293,6 @@ | ReasoningClaim.ReasoningClaimTerminates part0 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "terminates"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "theorem"), (SemanticTerms::memberJson (first part0) (second part0))) entries1); (objectOf entries2))) | ReasoningClaim.ReasoningClaimObservationInvariant part0 part1 part2 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "observation_invariant"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "predicate"), (SemanticTerms::memberJson (first part0) (second part0))) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "initial"), (SemanticTerms::memberJson (first part1) (second part1))) entries2); (let entries4 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "preserved"), (SemanticTerms::memberJson (first part2) (second part2))) entries3); (objectOf entries4))))) | ReasoningClaim.ReasoningClaimAnswerCorrect part0 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "answer_correct"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "theorem"), (SemanticTerms::memberJson (first part0) (second part0))) entries1); (objectOf entries2)))), - axioms: [Quot.sound, propext], executable: true } @@ -309,7 +303,6 @@ body: (match value | TokenizerScheme.TokenizerSchemeByteLevel => (ctor LexLeanStd.Json::Json.JStr (lit "byte_level")) | TokenizerScheme.TokenizerSchemeLongestMatch => (ctor LexLeanStd.Json::Json.JStr (lit "longest_match"))), - axioms: [Quot.sound, propext], executable: true } @@ -320,7 +313,6 @@ body: (match value | OverflowDisposition.OverflowDispositionRefuse => (ctor LexLeanStd.Json::Json.JStr (lit "refuse")) | OverflowDisposition.OverflowDispositionKeepLast => (ctor LexLeanStd.Json::Json.JStr (lit "keep_last"))), - axioms: [Quot.sound, propext], executable: true } @@ -330,7 +322,6 @@ result: LexLeanStd.Json::Json, body: (match value | SamplingGenerator.SamplingGeneratorXorshift32 => (ctor LexLeanStd.Json::Json.JStr (lit "xorshift32"))), - axioms: [Quot.sound, propext], executable: true } @@ -340,7 +331,6 @@ result: LexLeanStd.Json::Json, body: (match value | SamplingDistribution.SamplingDistributionProportional => (ctor LexLeanStd.Json::Json.JStr (lit "proportional"))), - axioms: [Quot.sound, propext], executable: true } @@ -351,7 +341,6 @@ body: (match value | GenerationPolicy.GenerationPolicyGreedy => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "greedy"))) (nil (Bytes * LexLeanStd.Json::Json))); (objectOf entries1)) | GenerationPolicy.GenerationPolicySample part0 part1 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "sample"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "generator"), (samplingGeneratorJson part0)) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "distribution"), (samplingDistributionJson part1)) entries2); (objectOf entries3))))), - axioms: [Quot.sound, propext], executable: true } @@ -412,7 +401,6 @@ | ReasoningDeclaration.ReasoningDeclarationTextClassifier part0 part1 part2 part3 => (let entries1 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr (lit "text_classifier"))) (nil (Bytes * LexLeanStd.Json::Json))); (let entries2 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "name"), (ctor LexLeanStd.Json::Json.JStr part0)) entries1); (let entries3 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "tokenizer"), (SemanticTerms::memberJson (first part1) (second part1))) entries2); (let entries4 : (List (Bytes * LexLeanStd.Json::Json)) := (cons ((lit "model"), (SemanticModels::modelUseJson part2)) entries3); (let entries5 : (List (Bytes * LexLeanStd.Json::Json)) := (match part3 | List.nil => entries4 | List.cons head tail => (cons ((lit "axioms"), (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (item : Bytes) => (cons (ctor LexLeanStd.Json::Json.JStr item) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [Bytes] part3) : (List LexLeanStd.Json::Json))))) entries4)); (objectOf entries5))))))), - axioms: [Quot.sound, propext], executable: true } @@ -445,7 +433,6 @@ | AnyDeclaration.AnyOrdinary declared => (SemanticDecls::declarationJson declared) | AnyDeclaration.AnyModel declared => (SemanticModels::modelDeclarationJson declared) | AnyDeclaration.AnyReasoning declared => (reasoningDeclarationJson declared)), - axioms: [Quot.sound, propext], executable: true } diff --git a/crates/conformance/src/cases/bootstrap/semantic_reasoning.rs b/crates/conformance/src/cases/bootstrap/semantic_reasoning.rs index e585bdfa57..665e6a3e24 100755 --- a/crates/conformance/src/cases/bootstrap/semantic_reasoning.rs +++ b/crates/conformance/src/cases/bootstrap/semantic_reasoning.rs @@ -719,9 +719,7 @@ pub(super) fn example_reasoning_declaration(step: usize) -> SemanticDeclaration rules: if rest % 3 == 2 { Vec::new() } else { - (rest..rest + 2) - .map(|next| example_model_use(next)) - .collect() + (rest..rest + 2).map(example_model_use).collect() }, strategy: example_reasoning_strategy(rest), answer: (rest % 3 != 2).then(|| example_reasoning_answer(rest)), @@ -729,9 +727,7 @@ pub(super) fn example_reasoning_declaration(step: usize) -> SemanticDeclaration claims: if rest % 3 == 2 { Vec::new() } else { - (rest..rest + 2) - .map(|next| example_reasoning_claim(next)) - .collect() + (rest..rest + 2).map(example_reasoning_claim).collect() }, executable: rest.is_multiple_of(2), axioms: if rest % 3 == 2 { From d92152fd80e7fca8ceb76265fd6b68d3c2a75331 Mon Sep 17 00:00:00 2001 From: humuhumu33 Date: Thu, 12 Nov 2026 21:19:23 +0000 Subject: [PATCH 09/22] The roots of BS-69, BS-70 and BS-72 are named after their packages The stack-bound step of the regeneration maps a package to its root by name, so a root is the camel case of its package and unique in the project: semanticTermText, semanticProofText, semanticDeclarationText, semanticModuleText, and wholeSemanticModuleText. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 8 ++++---- SPEC.md | 18 +++++++++--------- VERIFICATION.md | 14 +++++++------- bootstrap/src/SemanticDecls.lex.tex | 4 ++-- bootstrap/src/SemanticReasoning.lex.tex | 2 +- bootstrap/src/SemanticTerms.lex.tex | 4 ++-- .../src/cases/bootstrap/semantic_decls.rs | 10 ++++++++-- .../src/cases/bootstrap/semantic_reasoning.rs | 2 +- .../src/cases/bootstrap/semantic_terms.rs | 7 +++++-- features/suites/bootstrap.feature | 6 +++--- model/ids.toml | 6 +++--- tests/production-export/bootstrap/roots.json | 10 +++++----- 12 files changed, 50 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0dab92b84..233b33be98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,7 @@ workspace carried at those versions without a tag. - The declarations of reasoning and text components, and semantic modules of every kind, as language-1.2 source, eighth slice of the typed IR (SPEC.md §17.23, §17.12, `BS-72`, #269; no new diagnostic code, no change to the language-1.2 identity). - `reasoningDeclarationText` and `semanticModuleText` of `SemanticReasoning` return the + `reasoningDeclarationText` and `wholeSemanticModuleText` of `SemanticReasoning` return the canonical JSON of the serde form of those declarations and of `SemanticModule`, for every committed semantic module. The register is 501 IDs. - The declarations of artifacts, contracts, realizations, evidence, and models as @@ -32,14 +32,14 @@ workspace carried at those versions without a tag. - The ordinary declarations of the semantic module language and its modules as language-1.2 source, sixth slice of the typed IR (SPEC.md §17.23, §17.11, §17.12, `BS-70`, #269; no new diagnostic code, no change to the language-1.2 identity). - `declarationText` and `moduleText` of `SemanticDecls` return the canonical JSON of + `semanticDeclarationText` and `semanticModuleText` of `SemanticDecls` return the canonical JSON of the serde form of `SemanticDeclaration` and `SemanticModule`, the latter for every module of the `bootstrap/` project and the standard library, and admit `recursion`. The register is 499 IDs. - The closed terms and proofs of the semantic module language as language-1.2 source, fifth slice of the typed IR (SPEC.md §17.23, §17.11, §17.12, `BS-69`, #269; no new - diagnostic code, no change to the language-1.2 identity). `termText` and - `proofText` of `SemanticTerms` return the canonical JSON of the serde form of + diagnostic code, no change to the language-1.2 identity). `semanticTermText` and + `semanticProofText` of `SemanticTerms` return the canonical JSON of the serde form of `SemanticTerm` and `SemanticProof`, and admit `recursion`. The register is 498 IDs. - The closed types of the semantic module language, its primitives, and its integer families as language-1.2 source, fourth slice of the typed IR (SPEC.md §17.23, §17.11, diff --git a/SPEC.md b/SPEC.md index b8ed83cf08..0cd7282454 100644 --- a/SPEC.md +++ b/SPEC.md @@ -9092,8 +9092,8 @@ component of the typed IR as source (`BS-69`, issue 269), the module `SemanticTe | Root | Takes | Returns | Oracle | | --- | --- | --- | --- | -| `LexLeanBoot.SemanticTerms.termText` | a term | its canonical JSON as the linked IR embeds it | the serde form of `ir::semantic::SemanticTerm` | -| `LexLeanBoot.SemanticTerms.proofText` | a proof | its canonical JSON as the linked IR embeds it | the serde form of `ir::semantic::SemanticProof` | +| `LexLeanBoot.SemanticTerms.semanticTermText` | a term | its canonical JSON as the linked IR embeds it | the serde form of `ir::semantic::SemanticTerm` | +| `LexLeanBoot.SemanticTerms.semanticProofText` | a proof | its canonical JSON as the linked IR embeds it | the serde form of `ir::semantic::SemanticProof` | Arguments, assignments, branches, map entries, and graph edges are chains of cells of the term itself, and proof branches and nested members chains of cells of the proof, so that each root is @@ -9106,11 +9106,11 @@ holds them are the sixth component of the typed IR as source (`BS-70`, issue 269 | Root | Takes | Returns | Oracle | | --- | --- | --- | --- | -| `LexLeanBoot.SemanticDecls.declarationText` | a structure, class, instance, inductive, definition, or theorem | its canonical JSON as the linked IR embeds it | the serde form of `ir::semantic::SemanticDeclaration` | -| `LexLeanBoot.SemanticDecls.moduleText` | a specification and the declarations of a module | the module's canonical JSON | the serde form of `ir::semantic::SemanticModule` | +| `LexLeanBoot.SemanticDecls.semanticDeclarationText` | a structure, class, instance, inductive, definition, or theorem | its canonical JSON as the linked IR embeds it | the serde form of `ir::semantic::SemanticDeclaration` | +| `LexLeanBoot.SemanticDecls.semanticModuleText` | a specification and the declarations of a module | the module's canonical JSON | the serde form of `ir::semantic::SemanticModule` | Every module of the `bootstrap/` project and of the standard library declares only these kinds, -so `moduleText` writes the semantic modules of LexLean's own sources. Both roots admit +so `semanticModuleText` writes the semantic modules of LexLean's own sources. Both roots admit `recursion`. The declarations of models, reasoning, and text components, and reading declarations from JSON, stay in the host. The declarations of artifacts, contracts, realizations, evidence, and models (§17.12) are the @@ -9133,7 +9133,7 @@ and `SemanticTypes`, with two production roots for `rust-std`: | Root | Takes | Returns | Oracle | | --- | --- | --- | --- | | `LexLeanBoot.SemanticReasoning.reasoningDeclarationText` | a logic, inference rule, verifier, reasoner, tokenizer, context window, text generator, or text classifier | its canonical JSON as the linked IR embeds it | the serde form of those variants of `ir::semantic::SemanticDeclaration` | -| `LexLeanBoot.SemanticReasoning.semanticModuleText` | a specification and declarations of every kind | the module's canonical JSON | the serde form of `ir::semantic::SemanticModule` | +| `LexLeanBoot.SemanticReasoning.wholeSemanticModuleText` | a specification and declarations of every kind | the module's canonical JSON | the serde form of `ir::semantic::SemanticModule` | With this module every semantic module the linked IR embeds is written by source, and the module and the case's encoders and examples are generated from one description of the oracle's types. @@ -12190,10 +12190,10 @@ Every row below is normative, has honesty level `build`, and MUST be copied byte | `BS-66` | `bootstrap` | The proof and declaration IR of SPEC.md §17.4 and §17.5 in the canonical JSON of §17.9 (`ir::proof::Proof::to_json` and `ir::declaration::Declaration::to_json` under the hashing renumbering of `BS-65`: each step, rule, case, and calculation step in order, an introduction binding its locals in order, a `have` binding its local after its proposition, a case binding its locals before its proof, a rewrite target resolved as an occurrence, and a declaration's parameters bound before its body, with its kind and its axiom policy; and `AxiomPolicy::permits` of §15.9: no axiom under `none`, a subset under `allow`, and the sorted distinct set equal to the policy's under `exact`) is language-1.2 source in the `bootstrap/` project, as three production roots of `IrProof` over the term JSON of `IrTerm`: `proofText` and `declarationText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, and `permits`, with exactly `allocation` and `overflow`, 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 proofs, declarations, and policies, which reach every proof constructor, every declaration kind and body, every policy, rebound and free locals, duplicates, and order, on every declaration and every nested proof of the linked IR of the committed projects that declare document environments, with each policy against its own axioms and against none, and on seeded inputs; each planted defect in the source is detected by the root and the one vector it names, and the certified packages the production export publishes for the project are the committed ones; elaborating proofs and observing axioms are outside the claim. | §15.9, §17.4, §17.5, §17.9, §17.23 | | `BS-67` | `bootstrap` | The document IR and the linked project of SPEC.md §17.5 in the canonical JSON of §17.9, and the semantic ID of §21.4 (`ir::document::DocumentModule::to_json`: the name, the Lean module, the glossary, the imports, the title and the blocks under one hashing renumbering of `BS-65`, each section's heading, parameters, and blocks in order and each declaration through `BS-66`, the origin only when there is one, an import's Lean module only when it is not the one the module's prefix gives, and an embedded closed-core module as its serialized text and a semantic or tooling module as its JSON; `LinkedProject::to_json`: the specification and the modules in name order; and the semantic ID the linker records, the recipe of `BS-32` over that text) is language-1.2 source in the `bootstrap/` project, as three production roots of `IrDocument`: `moduleText`, `linkedText`, and `projectSemanticId`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, and each with no part of its certificate beyond 3,145,728 bytes; their rendered `rust-std` packages under the pinned rustc on the projects of at most 4,194,304 bytes of linked IR, and on the normative host their lowered programs under the calculus evaluator on the shorter ones, agree with the oracle to the byte on hand-built modules and projects, which reach every phrase item, sections, origins, default and foreign imports, every kind of embedded module, and modules out of order, and on every module, every linked project, and the recorded semantic ID of every committed project outside the negative fixtures and UOR Atlas that links under its committed lock, or under a lock computed from its manifest when it commits none; each planted defect in the source is detected by the root and the one vector it names, and the certified packages the production export publishes for the project are the committed ones; serializing the embedded modules and the lexicon closure is outside the claim. | §17.5, §17.9, §17.23, §21.4 | | `BS-68` | `bootstrap` | The closed types of the semantic module language of SPEC.md §17.11 and §17.12, its primitives, and its integer families (`ir::semantic::SemanticType` in the serde form the linked IR embeds, written as canonical JSON: each type tagged by its `kind`, a named type's module only when it has one, and the fixed-width names `int8` to `uint64`; its `Display`, the spelling diagnostics use; and of `SemanticPrimitive` and `SemanticInteger`, the serialized name, whether a primitive is language 1.2's, whether linking widens it away, and the position of its function argument) is language-1.2 source in the `bootstrap/` project, as four production roots of `SemanticTypes`: `primitiveFacts` and `integerName`, each eligible for `rust-std` with exactly `allocation`, `typeText`, with exactly `allocation`, `overflow`, and `recursion`, and `typeSpelling`, with exactly `allocation` and `recursion`, 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 types, which reach every constructor and every unit type, on every primitive and every integer family the module names, each read back through the oracle's serialized name, on every distinct type of every committed semantic module that links, and on seeded types; each planted defect in the source is detected by the root and the one vector it names, and the certified packages the production export publishes for the project are the committed ones; reading types from JSON is outside the claim. | §17.11, §17.12, §17.23 | -| `BS-69` | `bootstrap` | The closed terms and proofs of the semantic module language of SPEC.md §17.11 and §17.12 (`ir::semantic::SemanticTerm` and `SemanticProof` in the serde form the linked IR embeds, written as canonical JSON: each term and proof tagged by its `kind`, its member references, assignments, branches, binders, map entries, graph edges, model checks, and reflections in their fields, a list of type arguments, definitions, generalized names, or premises left out when it is empty where the oracle skips it, and its types through `SemanticTypes`) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticTerms`: `termText` and `proofText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 terms and proofs, which reach every constructor and every optional list both empty and not, on every distinct term and proof of every committed semantic module that links, and on seeded terms and proofs; each planted defect in the source is detected by the root and the one vector it names, and the certified packages the production export publishes for the project are the committed ones; reading terms and proofs from JSON is outside the claim. | §17.11, §17.12, §17.23 | -| `BS-70` | `bootstrap` | The ordinary declarations of the semantic module language of SPEC.md §17.11 and §17.12 and the module that holds them (`ir::semantic::SemanticDeclaration` for structures, classes, instances, inductives, definitions, and theorems, and `SemanticModule`, in the serde form the linked IR embeds, written as canonical JSON: each declaration tagged by its `kind`, its parameters, fields, constructors, and assignments in order, and a definition's or a theorem's type parameters, recursive argument, axioms, executable flag, mutual group, termination, and production, and an inductive's mutual group, left out exactly where the oracle's serde attributes skip them; and a module's specification and declarations in order) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticDecls` over `SemanticTerms` and `SemanticTypes`: `declarationText` and `moduleText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 on the shorter modules, agree with the oracle to the byte on hand-built declarations, which reach every kind and every optional member both present and absent, on every distinct ordinary declaration of every committed semantic module that links and on every such module whose declarations are all ordinary, among them every module of the `bootstrap/` project and the standard library, and on seeded declarations; each planted defect in the source is detected by the root and the one vector it names, and the certified packages the production export publishes for the project are the committed ones; the declarations of models, reasoning, and text components, and reading declarations from JSON, are outside the claim. | §17.11, §17.12, §17.23 | +| `BS-69` | `bootstrap` | The closed terms and proofs of the semantic module language of SPEC.md §17.11 and §17.12 (`ir::semantic::SemanticTerm` and `SemanticProof` in the serde form the linked IR embeds, written as canonical JSON: each term and proof tagged by its `kind`, its member references, assignments, branches, binders, map entries, graph edges, model checks, and reflections in their fields, a list of type arguments, definitions, generalized names, or premises left out when it is empty where the oracle skips it, and its types through `SemanticTypes`) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticTerms`: `semanticTermText` and `semanticProofText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 terms and proofs, which reach every constructor and every optional list both empty and not, on every distinct term and proof of every committed semantic module that links, and on seeded terms and proofs; each planted defect in the source is detected by the root and the one vector it names, and the certified packages the production export publishes for the project are the committed ones; reading terms and proofs from JSON is outside the claim. | §17.11, §17.12, §17.23 | +| `BS-70` | `bootstrap` | The ordinary declarations of the semantic module language of SPEC.md §17.11 and §17.12 and the module that holds them (`ir::semantic::SemanticDeclaration` for structures, classes, instances, inductives, definitions, and theorems, and `SemanticModule`, in the serde form the linked IR embeds, written as canonical JSON: each declaration tagged by its `kind`, its parameters, fields, constructors, and assignments in order, and a definition's or a theorem's type parameters, recursive argument, axioms, executable flag, mutual group, termination, and production, and an inductive's mutual group, left out exactly where the oracle's serde attributes skip them; and a module's specification and declarations in order) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticDecls` over `SemanticTerms` and `SemanticTypes`: `semanticDeclarationText` and `semanticModuleText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 on the shorter modules, agree with the oracle to the byte on hand-built declarations, which reach every kind and every optional member both present and absent, on every distinct ordinary declaration of every committed semantic module that links and on every such module whose declarations are all ordinary, among them every module of the `bootstrap/` project and the standard library, and on seeded declarations; each planted defect in the source is detected by the root and the one vector it names, and the certified packages the production export publishes for the project are the committed ones; the declarations of models, reasoning, and text components, and reading declarations from JSON, are outside the claim. | §17.11, §17.12, §17.23 | | `BS-71` | `bootstrap` | The declarations of artifacts, contracts, realizations, evidence, and models of the semantic module language of SPEC.md §17.12 (those variants of `ir::semantic::SemanticDeclaration`, with the artifact roles and schemas, binders, states, validators, uses, rules, the deterministic, rule, statistical, neural, and composite realization descriptors with their layers, decoders, forms, and junctions, and the evidence claims they hold, in the serde form the linked IR embeds, written as canonical JSON: each tagged variant by its `kind`, each closed name as the oracle serializes it, and each optional member left out exactly where the oracle's serde attributes skip it) is language-1.2 source in the `bootstrap/` project, as the production root `modelDeclarationText` of `SemanticModels` over `SemanticTerms` and `SemanticTypes`, generated with the case's encoders and examples from one description of the oracle's types, 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 examples that reach every variant, every optional member present and absent, and every list empty and not, on every distinct such declaration of every committed semantic module that links, and on seeded examples; each planted defect, a misspelled kind or name, is detected by the first example that holds it, and the certified packages the production export publishes for the project are the committed ones; reading declarations from JSON is outside the claim. | §17.12, §17.23 | -| `BS-72` | `bootstrap` | The declarations of logics, inference rules, verifiers, reasoners, tokenizers, context windows, text generators, and text classifiers of the semantic module language of SPEC.md §17.12, and the semantic module of declarations of every kind (those variants of `ir::semantic::SemanticDeclaration`, with the invariants, bindings, answers, search orders, strategies, claims, tokenizer schemes, overflow dispositions, and generation policies they hold, and `SemanticModule`, in the serde form the linked IR embeds, written as canonical JSON: each tagged variant by its `kind`, each closed name as the oracle serializes it, each optional member left out exactly where the oracle's serde attributes skip it, and a module's specification and declarations in order, its ordinary declarations through `SemanticDecls` and its model declarations through `SemanticModels`) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticReasoning`, generated with the case's encoders and examples from one description of the oracle's types: `reasoningDeclarationText` and `semanticModuleText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 on the shorter modules, agree with the oracle to the byte on examples that reach every variant, every optional member present and absent, and every list empty and not, on every distinct such declaration and every distinct semantic module of every committed project that links, and on seeded examples; each planted defect, a misspelled kind or name, is detected by the first example that holds it, and the certified packages the production export publishes for the project are the committed ones; reading declarations and modules from JSON is outside the claim. | §17.12, §17.23 | +| `BS-72` | `bootstrap` | The declarations of logics, inference rules, verifiers, reasoners, tokenizers, context windows, text generators, and text classifiers of the semantic module language of SPEC.md §17.12, and the semantic module of declarations of every kind (those variants of `ir::semantic::SemanticDeclaration`, with the invariants, bindings, answers, search orders, strategies, claims, tokenizer schemes, overflow dispositions, and generation policies they hold, and `SemanticModule`, in the serde form the linked IR embeds, written as canonical JSON: each tagged variant by its `kind`, each closed name as the oracle serializes it, each optional member left out exactly where the oracle's serde attributes skip it, and a module's specification and declarations in order, its ordinary declarations through `SemanticDecls` and its model declarations through `SemanticModels`) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticReasoning`, generated with the case's encoders and examples from one description of the oracle's types: `reasoningDeclarationText` and `wholeSemanticModuleText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 on the shorter modules, agree with the oracle to the byte on examples that reach every variant, every optional member present and absent, and every list empty and not, on every distinct such declaration and every distinct semantic module of every committed project that links, and on seeded examples; each planted defect, a misspelled kind or name, is detected by the first example that holds it, and the certified packages the production export publishes for the project are the committed ones; reading declarations and modules from JSON is outside the claim. | §17.12, §17.23 | | `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 | diff --git a/VERIFICATION.md b/VERIFICATION.md index ceaf923b7f..24e324f331 100644 --- a/VERIFICATION.md +++ b/VERIFICATION.md @@ -11857,8 +11857,8 @@ passes. ### the semantic terms and proofs are source (BS-69) `BS-69` holds the two production roots of `LexLeanBoot.SemanticTerms` -(`bootstrap/src/SemanticTerms.lex.tex`) to `lexlean::ir::semantic`: `termText` -to the canonical JSON of the serde form of `SemanticTerm` and `proofText` to +(`bootstrap/src/SemanticTerms.lex.tex`) to `lexlean::ir::semantic`: `semanticTermText` +to the canonical JSON of the serde form of `SemanticTerm` and `semanticProofText` to that of `SemanticProof`, as `DocumentModule::to_json` embeds a semantic module. The module declares the 42 term forms and the 13 proof forms of SPEC.md §17.11 and §17.12 as inductives over the types of `SemanticTypes` (`BS-68`). Arguments, @@ -11888,9 +11888,9 @@ passes. `BS-70` holds the two production roots of `LexLeanBoot.SemanticDecls` (`bootstrap/src/SemanticDecls.lex.tex`) to `lexlean::ir::semantic`: -`declarationText` to the canonical JSON of the serde form of +`semanticDeclarationText` to the canonical JSON of the serde form of `SemanticDeclaration` for structures, classes, instances, inductives, -definitions, and theorems, and `moduleText` to that of `SemanticModule`, as +definitions, and theorems, and `semanticModuleText` to that of `SemanticModule`, as `DocumentModule::to_json` embeds a semantic module. The module declares the six ordinary declaration kinds of SPEC.md §17.11 and §17.12 as one inductive over the terms and proofs of `SemanticTerms` (`BS-69`) and the types of @@ -11898,7 +11898,7 @@ the terms and proofs of `SemanticTerms` (`BS-69`) and the types of recursive argument, axioms, executable flag, mutual group, termination, and production, and an inductive's mutual group, are left out exactly where the oracle's serde attributes skip them. Every module of the `bootstrap/` project and -of the standard library declares only these kinds, so `moduleText` writes the +of the standard library declares only these kinds, so `semanticModuleText` writes the semantic modules of LexLean's own sources. Both roots admit exactly `allocation`, `overflow`, and `recursion`. The oracle is the existing public code of `semantic.rs` and its serde derivations; no Rust outside the case changes. @@ -11957,7 +11957,7 @@ with exactly its declared effects, and `lexlean fmt --check` passes. `reasoningDeclarationText` to the canonical JSON of the serde form of the logic, inference rule, verifier, reasoner, tokenizer, context window, text generator, and text classifier variants of `SemanticDeclaration`, and -`semanticModuleText` to that of `SemanticModule`, as `DocumentModule::to_json` +`wholeSemanticModuleText` to that of `SemanticModule`, as `DocumentModule::to_json` embeds a semantic module. The module declares the eleven types those declarations hold and the declarations themselves, each as one inductive with one writer, over the uses, binders, and states of `SemanticModels` (`BS-71`), the @@ -11974,7 +11974,7 @@ The case holds `reasoningDeclarationText` to the oracle on 150 examples whose step's digits choose every variant at every depth, each optional member present and absent and each list empty and not, on every distinct such declaration of every committed semantic module that links, and on seeded examples; and -`semanticModuleText` on every distinct semantic module of every committed +`wholeSemanticModuleText` on every distinct semantic module of every committed project that links, the evaluator taking those whose JSON is at most 262,144 bytes and the rendered package all of them. Each planted defect misspells one serialized kind or closed name, and the first example that holds it detects it; diff --git a/bootstrap/src/SemanticDecls.lex.tex b/bootstrap/src/SemanticDecls.lex.tex index 090f76c3c0..d0fca84f47 100755 --- a/bootstrap/src/SemanticDecls.lex.tex +++ b/bootstrap/src/SemanticDecls.lex.tex @@ -185,7 +185,7 @@ } definition{ - name: declarationText, + name: semanticDeclarationText, parameters: [declaration : SDecl], result: Bytes, body: (LexLeanStd.Json::writeJson (declarationJson declaration)), @@ -195,7 +195,7 @@ } definition{ - name: moduleText, + name: semanticModuleText, parameters: [spec : Bytes, declarations : (List SDecl)], result: Bytes, body: (LexLeanStd.Json::writeJson (ctor LexLeanStd.Json::Json.JObj (member "spec" (jtext spec) (member "declarations" (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (declaration : SDecl) => (cons (declarationJson declaration) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [SDecl] declarations) : (List LexLeanStd.Json::Json)))) (jnil))))), diff --git a/bootstrap/src/SemanticReasoning.lex.tex b/bootstrap/src/SemanticReasoning.lex.tex index 28caaaaa18..7365044eba 100755 --- a/bootstrap/src/SemanticReasoning.lex.tex +++ b/bootstrap/src/SemanticReasoning.lex.tex @@ -437,7 +437,7 @@ } definition{ - name: semanticModuleText, + name: wholeSemanticModuleText, parameters: [spec : Bytes, declarations : (List AnyDeclaration)], result: Bytes, body: (LexLeanStd.Json::writeJson (objectOf (cons ((lit "spec"), (ctor LexLeanStd.Json::Json.JStr spec)) (cons ((lit "declarations"), (ctor LexLeanStd.Json::Json.JArr (SemanticTerms::chainOf (prim list_fold (fun (acc : (List LexLeanStd.Json::Json)) (declared : AnyDeclaration) => (cons (anyDeclarationJson declared) acc)) (nil LexLeanStd.Json::Json) (LexLeanStd.Order::reverseList [AnyDeclaration] declarations) : (List LexLeanStd.Json::Json))))) (nil (Bytes * LexLeanStd.Json::Json)))))), diff --git a/bootstrap/src/SemanticTerms.lex.tex b/bootstrap/src/SemanticTerms.lex.tex index 590b6c2178..a6b91f3fe9 100755 --- a/bootstrap/src/SemanticTerms.lex.tex +++ b/bootstrap/src/SemanticTerms.lex.tex @@ -323,7 +323,7 @@ } definition{ - name: termText, + name: semanticTermText, parameters: [term : STerm], result: Bytes, body: (LexLeanStd.Json::writeJson (termJson term)), @@ -333,7 +333,7 @@ } definition{ - name: proofText, + name: semanticProofText, parameters: [proof : SProof], result: Bytes, body: (LexLeanStd.Json::writeJson (proofJson proof)), diff --git a/crates/conformance/src/cases/bootstrap/semantic_decls.rs b/crates/conformance/src/cases/bootstrap/semantic_decls.rs index 5f73443254..0fdf90d899 100755 --- a/crates/conformance/src/cases/bootstrap/semantic_decls.rs +++ b/crates/conformance/src/cases/bootstrap/semantic_decls.rs @@ -36,8 +36,14 @@ use crate::support::{self, repo_root}; const MODULE: &str = "LexLeanBoot.SemanticDecls"; const SOURCE: &str = "bootstrap/src/SemanticDecls.lex.tex"; const ROOTS: [(&str, &[&str]); 2] = [ - ("declarationText", &["allocation", "overflow", "recursion"]), - ("moduleText", &["allocation", "overflow", "recursion"]), + ( + "semanticDeclarationText", + &["allocation", "overflow", "recursion"], + ), + ( + "semanticModuleText", + &["allocation", "overflow", "recursion"], + ), ]; /// The most a part of a certificate of a root may be, as the row states. diff --git a/crates/conformance/src/cases/bootstrap/semantic_reasoning.rs b/crates/conformance/src/cases/bootstrap/semantic_reasoning.rs index 665e6a3e24..006e66aac9 100755 --- a/crates/conformance/src/cases/bootstrap/semantic_reasoning.rs +++ b/crates/conformance/src/cases/bootstrap/semantic_reasoning.rs @@ -49,7 +49,7 @@ const ROOTS: [(&str, &[&str]); 2] = [ &["allocation", "overflow", "recursion"], ), ( - "semanticModuleText", + "wholeSemanticModuleText", &["allocation", "overflow", "recursion"], ), ]; diff --git a/crates/conformance/src/cases/bootstrap/semantic_terms.rs b/crates/conformance/src/cases/bootstrap/semantic_terms.rs index 7bdfce3696..c8d1db126c 100755 --- a/crates/conformance/src/cases/bootstrap/semantic_terms.rs +++ b/crates/conformance/src/cases/bootstrap/semantic_terms.rs @@ -33,8 +33,11 @@ use crate::support::{self, repo_root}; const MODULE: &str = "LexLeanBoot.SemanticTerms"; const SOURCE: &str = "bootstrap/src/SemanticTerms.lex.tex"; const ROOTS: [(&str, &[&str]); 2] = [ - ("termText", &["allocation", "overflow", "recursion"]), - ("proofText", &["allocation", "overflow", "recursion"]), + ("semanticTermText", &["allocation", "overflow", "recursion"]), + ( + "semanticProofText", + &["allocation", "overflow", "recursion"], + ), ]; /// The most a part of a certificate of a root may be, as the row states. diff --git a/features/suites/bootstrap.feature b/features/suites/bootstrap.feature index 386ada9c67..31bc753761 100644 --- a/features/suites/bootstrap.feature +++ b/features/suites/bootstrap.feature @@ -233,13 +233,13 @@ Feature: bootstrap Then each root is eligible for rust-std with exactly its declared effects, no certificate part is beyond 3,145,728 bytes, and the four roots equal the facts of `SemanticPrimitive` and `SemanticInteger`, the canonical JSON of the serde form of `SemanticType`, and its `Display` 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 @BS-69 @build - Scenario: The closed terms and proofs of the semantic module language of SPEC.md §17.11 and §17.12 (`ir::semantic::SemanticTerm` and `SemanticProof` in the serde form the linked IR embeds, written as canonical JSON: each term and proof tagged by its `kind`, its member references, assignments, branches, binders, map entries, graph edges, model checks, and reflections in their fields, a list of type arguments, definitions, generalized names, or premises left out when it is empty where the oracle skips it, and its types through `SemanticTypes`) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticTerms`: `termText` and `proofText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 terms and proofs, which reach every constructor and every optional list both empty and not, on every distinct term and proof of every committed semantic module that links, and on seeded terms and proofs; each planted defect in the source is detected by the root and the one vector it names, and the certified packages the production export publishes for the project are the committed ones; reading terms and proofs from JSON is outside the claim. + Scenario: The closed terms and proofs of the semantic module language of SPEC.md §17.11 and §17.12 (`ir::semantic::SemanticTerm` and `SemanticProof` in the serde form the linked IR embeds, written as canonical JSON: each term and proof tagged by its `kind`, its member references, assignments, branches, binders, map entries, graph edges, model checks, and reflections in their fields, a list of type arguments, definitions, generalized names, or premises left out when it is empty where the oracle skips it, and its types through `SemanticTypes`) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticTerms`: `semanticTermText` and `semanticProofText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 terms and proofs, which reach every constructor and every optional list both empty and not, on every distinct term and proof of every committed semantic module that links, and on seeded terms and proofs; each planted defect in the source is detected by the root and the one vector it names, and the certified packages the production export publishes for the project are the committed ones; reading terms and proofs from JSON is outside the claim. Given the committed `bootstrap` project, hand-built terms and proofs, every term and proof of the committed semantic modules that link, and seeded terms and proofs When the project is checked and built, and the lowered program and the rendered package of each root are run on those inputs Then each root is eligible for rust-std with exactly its declared effects, no certificate part is beyond 3,145,728 bytes, and the two roots equal the canonical JSON of the serde form of `SemanticTerm` and `SemanticProof` 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 @BS-70 @build - Scenario: The ordinary declarations of the semantic module language of SPEC.md §17.11 and §17.12 and the module that holds them (`ir::semantic::SemanticDeclaration` for structures, classes, instances, inductives, definitions, and theorems, and `SemanticModule`, in the serde form the linked IR embeds, written as canonical JSON: each declaration tagged by its `kind`, its parameters, fields, constructors, and assignments in order, and a definition's or a theorem's type parameters, recursive argument, axioms, executable flag, mutual group, termination, and production, and an inductive's mutual group, left out exactly where the oracle's serde attributes skip them; and a module's specification and declarations in order) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticDecls` over `SemanticTerms` and `SemanticTypes`: `declarationText` and `moduleText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 on the shorter modules, agree with the oracle to the byte on hand-built declarations, which reach every kind and every optional member both present and absent, on every distinct ordinary declaration of every committed semantic module that links and on every such module whose declarations are all ordinary, among them every module of the `bootstrap/` project and the standard library, and on seeded declarations; each planted defect in the source is detected by the root and the one vector it names, and the certified packages the production export publishes for the project are the committed ones; the declarations of models, reasoning, and text components, and reading declarations from JSON, are outside the claim. + Scenario: The ordinary declarations of the semantic module language of SPEC.md §17.11 and §17.12 and the module that holds them (`ir::semantic::SemanticDeclaration` for structures, classes, instances, inductives, definitions, and theorems, and `SemanticModule`, in the serde form the linked IR embeds, written as canonical JSON: each declaration tagged by its `kind`, its parameters, fields, constructors, and assignments in order, and a definition's or a theorem's type parameters, recursive argument, axioms, executable flag, mutual group, termination, and production, and an inductive's mutual group, left out exactly where the oracle's serde attributes skip them; and a module's specification and declarations in order) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticDecls` over `SemanticTerms` and `SemanticTypes`: `semanticDeclarationText` and `semanticModuleText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 on the shorter modules, agree with the oracle to the byte on hand-built declarations, which reach every kind and every optional member both present and absent, on every distinct ordinary declaration of every committed semantic module that links and on every such module whose declarations are all ordinary, among them every module of the `bootstrap/` project and the standard library, and on seeded declarations; each planted defect in the source is detected by the root and the one vector it names, and the certified packages the production export publishes for the project are the committed ones; the declarations of models, reasoning, and text components, and reading declarations from JSON, are outside the claim. Given the committed `bootstrap` project, hand-built declarations and a module, every ordinary declaration of the committed semantic modules that link, every such module of only ordinary declarations, and seeded declarations When the project is checked and built, and the lowered program and the rendered package of each root are run on those inputs Then each root is eligible for rust-std with exactly its declared effects, no certificate part is beyond 3,145,728 bytes, and the two roots equal the canonical JSON of the serde form of `SemanticDeclaration` and `SemanticModule` on every run @@ -251,7 +251,7 @@ Feature: bootstrap Then the root is eligible for rust-std with exactly its declared effects, no certificate part is beyond 3,145,728 bytes, and the root equals the canonical JSON of the serde form of the declaration on every run And every planted defect is detected by the first example that holds it, and the exported packages are byte-identical to the committed packages and realize the programs the differential ran @BS-72 @build - Scenario: The declarations of logics, inference rules, verifiers, reasoners, tokenizers, context windows, text generators, and text classifiers of the semantic module language of SPEC.md §17.12, and the semantic module of declarations of every kind (those variants of `ir::semantic::SemanticDeclaration`, with the invariants, bindings, answers, search orders, strategies, claims, tokenizer schemes, overflow dispositions, and generation policies they hold, and `SemanticModule`, in the serde form the linked IR embeds, written as canonical JSON: each tagged variant by its `kind`, each closed name as the oracle serializes it, each optional member left out exactly where the oracle's serde attributes skip it, and a module's specification and declarations in order, its ordinary declarations through `SemanticDecls` and its model declarations through `SemanticModels`) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticReasoning`, generated with the case's encoders and examples from one description of the oracle's types: `reasoningDeclarationText` and `semanticModuleText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 on the shorter modules, agree with the oracle to the byte on examples that reach every variant, every optional member present and absent, and every list empty and not, on every distinct such declaration and every distinct semantic module of every committed project that links, and on seeded examples; each planted defect, a misspelled kind or name, is detected by the first example that holds it, and the certified packages the production export publishes for the project are the committed ones; reading declarations and modules from JSON is outside the claim. + Scenario: The declarations of logics, inference rules, verifiers, reasoners, tokenizers, context windows, text generators, and text classifiers of the semantic module language of SPEC.md §17.12, and the semantic module of declarations of every kind (those variants of `ir::semantic::SemanticDeclaration`, with the invariants, bindings, answers, search orders, strategies, claims, tokenizer schemes, overflow dispositions, and generation policies they hold, and `SemanticModule`, in the serde form the linked IR embeds, written as canonical JSON: each tagged variant by its `kind`, each closed name as the oracle serializes it, each optional member left out exactly where the oracle's serde attributes skip it, and a module's specification and declarations in order, its ordinary declarations through `SemanticDecls` and its model declarations through `SemanticModels`) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticReasoning`, generated with the case's encoders and examples from one description of the oracle's types: `reasoningDeclarationText` and `wholeSemanticModuleText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 on the shorter modules, agree with the oracle to the byte on examples that reach every variant, every optional member present and absent, and every list empty and not, on every distinct such declaration and every distinct semantic module of every committed project that links, and on seeded examples; each planted defect, a misspelled kind or name, is detected by the first example that holds it, and the certified packages the production export publishes for the project are the committed ones; reading declarations and modules from JSON is outside the claim. Given the committed `bootstrap` project, examples that reach every variant and every optional member, every reasoning and text declaration and every semantic module of the committed projects that link, and seeded examples When the project is checked and built, and the lowered program and the rendered package of each root are run on those inputs Then each root is eligible for rust-std with exactly its declared effects, no certificate part is beyond 3,145,728 bytes, and the two roots equal the canonical JSON of the serde form of the declaration and of `SemanticModule` on every run diff --git a/model/ids.toml b/model/ids.toml index 52ab06ee7e..c14d40a1d8 100644 --- a/model/ids.toml +++ b/model/ids.toml @@ -2904,13 +2904,13 @@ statement = "The closed types of the semantic module language of SPEC.md §17.11 id = "BS-69" level = "build" suite = "bootstrap" -statement = "The closed terms and proofs of the semantic module language of SPEC.md §17.11 and §17.12 (`ir::semantic::SemanticTerm` and `SemanticProof` in the serde form the linked IR embeds, written as canonical JSON: each term and proof tagged by its `kind`, its member references, assignments, branches, binders, map entries, graph edges, model checks, and reflections in their fields, a list of type arguments, definitions, generalized names, or premises left out when it is empty where the oracle skips it, and its types through `SemanticTypes`) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticTerms`: `termText` and `proofText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 terms and proofs, which reach every constructor and every optional list both empty and not, on every distinct term and proof of every committed semantic module that links, and on seeded terms and proofs; each planted defect in the source is detected by the root and the one vector it names, and the certified packages the production export publishes for the project are the committed ones; reading terms and proofs from JSON is outside the claim." +statement = "The closed terms and proofs of the semantic module language of SPEC.md §17.11 and §17.12 (`ir::semantic::SemanticTerm` and `SemanticProof` in the serde form the linked IR embeds, written as canonical JSON: each term and proof tagged by its `kind`, its member references, assignments, branches, binders, map entries, graph edges, model checks, and reflections in their fields, a list of type arguments, definitions, generalized names, or premises left out when it is empty where the oracle skips it, and its types through `SemanticTypes`) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticTerms`: `semanticTermText` and `semanticProofText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 terms and proofs, which reach every constructor and every optional list both empty and not, on every distinct term and proof of every committed semantic module that links, and on seeded terms and proofs; each planted defect in the source is detected by the root and the one vector it names, and the certified packages the production export publishes for the project are the committed ones; reading terms and proofs from JSON is outside the claim." [[id]] id = "BS-70" level = "build" suite = "bootstrap" -statement = "The ordinary declarations of the semantic module language of SPEC.md §17.11 and §17.12 and the module that holds them (`ir::semantic::SemanticDeclaration` for structures, classes, instances, inductives, definitions, and theorems, and `SemanticModule`, in the serde form the linked IR embeds, written as canonical JSON: each declaration tagged by its `kind`, its parameters, fields, constructors, and assignments in order, and a definition's or a theorem's type parameters, recursive argument, axioms, executable flag, mutual group, termination, and production, and an inductive's mutual group, left out exactly where the oracle's serde attributes skip them; and a module's specification and declarations in order) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticDecls` over `SemanticTerms` and `SemanticTypes`: `declarationText` and `moduleText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 on the shorter modules, agree with the oracle to the byte on hand-built declarations, which reach every kind and every optional member both present and absent, on every distinct ordinary declaration of every committed semantic module that links and on every such module whose declarations are all ordinary, among them every module of the `bootstrap/` project and the standard library, and on seeded declarations; each planted defect in the source is detected by the root and the one vector it names, and the certified packages the production export publishes for the project are the committed ones; the declarations of models, reasoning, and text components, and reading declarations from JSON, are outside the claim." +statement = "The ordinary declarations of the semantic module language of SPEC.md §17.11 and §17.12 and the module that holds them (`ir::semantic::SemanticDeclaration` for structures, classes, instances, inductives, definitions, and theorems, and `SemanticModule`, in the serde form the linked IR embeds, written as canonical JSON: each declaration tagged by its `kind`, its parameters, fields, constructors, and assignments in order, and a definition's or a theorem's type parameters, recursive argument, axioms, executable flag, mutual group, termination, and production, and an inductive's mutual group, left out exactly where the oracle's serde attributes skip them; and a module's specification and declarations in order) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticDecls` over `SemanticTerms` and `SemanticTypes`: `semanticDeclarationText` and `semanticModuleText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 on the shorter modules, agree with the oracle to the byte on hand-built declarations, which reach every kind and every optional member both present and absent, on every distinct ordinary declaration of every committed semantic module that links and on every such module whose declarations are all ordinary, among them every module of the `bootstrap/` project and the standard library, and on seeded declarations; each planted defect in the source is detected by the root and the one vector it names, and the certified packages the production export publishes for the project are the committed ones; the declarations of models, reasoning, and text components, and reading declarations from JSON, are outside the claim." [[id]] id = "BS-71" @@ -2922,7 +2922,7 @@ statement = "The declarations of artifacts, contracts, realizations, evidence, a id = "BS-72" level = "build" suite = "bootstrap" -statement = "The declarations of logics, inference rules, verifiers, reasoners, tokenizers, context windows, text generators, and text classifiers of the semantic module language of SPEC.md §17.12, and the semantic module of declarations of every kind (those variants of `ir::semantic::SemanticDeclaration`, with the invariants, bindings, answers, search orders, strategies, claims, tokenizer schemes, overflow dispositions, and generation policies they hold, and `SemanticModule`, in the serde form the linked IR embeds, written as canonical JSON: each tagged variant by its `kind`, each closed name as the oracle serializes it, each optional member left out exactly where the oracle's serde attributes skip it, and a module's specification and declarations in order, its ordinary declarations through `SemanticDecls` and its model declarations through `SemanticModels`) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticReasoning`, generated with the case's encoders and examples from one description of the oracle's types: `reasoningDeclarationText` and `semanticModuleText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 on the shorter modules, agree with the oracle to the byte on examples that reach every variant, every optional member present and absent, and every list empty and not, on every distinct such declaration and every distinct semantic module of every committed project that links, and on seeded examples; each planted defect, a misspelled kind or name, is detected by the first example that holds it, and the certified packages the production export publishes for the project are the committed ones; reading declarations and modules from JSON is outside the claim." +statement = "The declarations of logics, inference rules, verifiers, reasoners, tokenizers, context windows, text generators, and text classifiers of the semantic module language of SPEC.md §17.12, and the semantic module of declarations of every kind (those variants of `ir::semantic::SemanticDeclaration`, with the invariants, bindings, answers, search orders, strategies, claims, tokenizer schemes, overflow dispositions, and generation policies they hold, and `SemanticModule`, in the serde form the linked IR embeds, written as canonical JSON: each tagged variant by its `kind`, each closed name as the oracle serializes it, each optional member left out exactly where the oracle's serde attributes skip it, and a module's specification and declarations in order, its ordinary declarations through `SemanticDecls` and its model declarations through `SemanticModels`) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticReasoning`, generated with the case's encoders and examples from one description of the oracle's types: `reasoningDeclarationText` and `wholeSemanticModuleText`, each eligible for `rust-std` with exactly its declared effects, `allocation`, `overflow`, and `recursion`, 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 on the shorter modules, agree with the oracle to the byte on examples that reach every variant, every optional member present and absent, and every list empty and not, on every distinct such declaration and every distinct semantic module of every committed project that links, and on seeded examples; each planted defect, a misspelled kind or name, is detected by the first example that holds it, and the certified packages the production export publishes for the project are the committed ones; reading declarations and modules from JSON is outside the claim." [[id]] id = "PK-01" diff --git a/tests/production-export/bootstrap/roots.json b/tests/production-export/bootstrap/roots.json index 0dbebe02f1..3672f67f7a 100644 --- a/tests/production-export/bootstrap/roots.json +++ b/tests/production-export/bootstrap/roots.json @@ -618,7 +618,7 @@ }, { "name": "semantic_term_text", - "root": "LexLeanBoot.SemanticTerms.termText", + "root": "LexLeanBoot.SemanticTerms.semanticTermText", "targets": [ "rust-std" ], @@ -626,7 +626,7 @@ }, { "name": "semantic_proof_text", - "root": "LexLeanBoot.SemanticTerms.proofText", + "root": "LexLeanBoot.SemanticTerms.semanticProofText", "targets": [ "rust-std" ], @@ -634,7 +634,7 @@ }, { "name": "semantic_declaration_text", - "root": "LexLeanBoot.SemanticDecls.declarationText", + "root": "LexLeanBoot.SemanticDecls.semanticDeclarationText", "targets": [ "rust-std" ], @@ -642,7 +642,7 @@ }, { "name": "semantic_module_text", - "root": "LexLeanBoot.SemanticDecls.moduleText", + "root": "LexLeanBoot.SemanticDecls.semanticModuleText", "targets": [ "rust-std" ], @@ -666,7 +666,7 @@ }, { "name": "whole_semantic_module_text", - "root": "LexLeanBoot.SemanticReasoning.semanticModuleText", + "root": "LexLeanBoot.SemanticReasoning.wholeSemanticModuleText", "targets": [ "rust-std" ], From 041b7ab83d43cad13908028c13e66b1aa9fb6360 Mon Sep 17 00:00:00 2001 From: humuhumu33 Date: Thu, 12 Nov 2026 22:58:37 +0000 Subject: [PATCH 10/22] lexlean: build_identity is an internal module (CL-13) Co-Authored-By: Claude Opus 5.5 --- crates/lexlean/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/lexlean/src/lib.rs b/crates/lexlean/src/lib.rs index 0edc6c3780..2996ef667b 100644 --- a/crates/lexlean/src/lib.rs +++ b/crates/lexlean/src/lib.rs @@ -109,6 +109,7 @@ pub mod embedded { /// The compiler that built this crate, as the build script found it /// (SPEC.md §22.11): what `rustc -vV` reported and the address of its /// executable and driver library. It is no input of any compiler semantics. +#[doc(hidden)] pub mod build_identity { include!(concat!(env!("OUT_DIR"), "/rustc_identity.rs")); } From 32f7103e22c981140129f7f26032de2948965eb4 Mon Sep 17 00:00:00 2001 From: humuhumu33 Date: Fri, 13 Nov 2026 05:25:41 +0000 Subject: [PATCH 11/22] Merge bs71-semantic-models (release b8b6e86a9) into bs72-semantic-reasoning Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 +- README.md | 2 +- SPEC.md | 2 +- crates/conformance/src/cases/repository.rs | 2 +- xtask/src/spec_links.rs | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13ceef3b56..c33d5ec1a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ workspace carried at those versions without a tag. `BS-72`, #269; no new diagnostic code, no change to the language-1.2 identity). `reasoningDeclarationText` and `wholeSemanticModuleText` of `SemanticReasoning` return the canonical JSON of the serde form of those declarations and of `SemanticModule`, for - every committed semantic module. The register is 501 IDs. + every committed semantic module. The register is 502 IDs. - The declarations of artifacts, contracts, realizations, evidence, and models as language-1.2 source, seventh slice of the typed IR (SPEC.md §17.23, §17.12, `BS-71`, #269; no new diagnostic code, no change to the language-1.2 identity). diff --git a/README.md b/README.md index b4d914a203..7cb709363c 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 501 registered conformance IDs have their named tests; `just vv` checks them with the pinned toolchain installed. +All 502 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. diff --git a/SPEC.md b/SPEC.md index 62621ffa42..6ae203a31f 100644 --- a/SPEC.md +++ b/SPEC.md @@ -12228,7 +12228,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:** 501. +**Total required capability IDs:** 502. No row may be downgraded to `some-true` or `open`. Upstream Lean facts are ledger/authority rows, not substitutions for these build behaviors. diff --git a/crates/conformance/src/cases/repository.rs b/crates/conformance/src/cases/repository.rs index 8f9110ca76..2e0772ba54 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(), 501, "§31 has 501 rows"); + assert_eq!(table.len(), 502, "§31 has 502 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/xtask/src/spec_links.rs b/xtask/src/spec_links.rs index fb3364c264..ebb4eaa4af 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() != 501 { - return Err(format!("RP-07: the §31 table has {} rows, not 501", rows.len()).into()); + if rows.len() != 502 { + return Err(format!("RP-07: the §31 table has {} rows, not 502", 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 a49071f9c7681fb4452251abf9a66ad8c7ed9caf Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Nov 2026 19:59:41 +0000 Subject: [PATCH 12/22] docs(proposals): address review 5875193015 on external contracts Separate proof-axiom dependencies from executable external uses (call inventory), complete the report-to-implementation binding, narrow the opacity statement. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_013Asa3M8ULnzEQwLLngyBBX --- docs/proposals/external-contracts.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/proposals/external-contracts.md b/docs/proposals/external-contracts.md index 84af21cc11..f82c7a20f9 100644 --- a/docs/proposals/external-contracts.md +++ b/docs/proposals/external-contracts.md @@ -101,7 +101,7 @@ public axiom dotFast.contract (left : Array Int8) (right : Array Int8) : DotContract left right (dotFast left right) ``` -The hidden value of the constant is the witness. The kernel never unfolds an opaque constant, so nothing about `dotFast` is provable except through the axiom. A document refers to the axiom as the member `dotFast.contract`, in `apply` and `simplify`, exactly as it refers to a theorem. +The hidden value of the constant is the witness. The kernel never unfolds an opaque constant, so a theorem cannot unfold `dotFast` to its witness, and cannot derive the intended computational contract from that body; the contract is available only through the axiom. Reflexivity (`dotFast l r = dotFast l r`) and hypotheses about `dotFast` remain available and carry no axiom. A document refers to the axiom as the member `dotFast.contract`, in `apply` and `simplify`, exactly as it refers to a theorem. The canonical document states the declaration, the contract, the witness and the evidence. The generated-source audit admits `opaque` and `axiom` tokens only at the positions an `external` declaration of the IR accounts for. @@ -113,18 +113,19 @@ What remains assumed is not logical. It is the statement that the foreign code, ### 6.4 Evidence -- `attested{report: A}`: `A` is an `artifact` declaration of role `conformance`, content-addressed like every artifact. Its bytes are a canonical JSON report that states the statement ID of the external declaration (`SM-38`), the digest of the foreign code tested, the reference it was compared with, the number of cases and the result. Linking refuses a report that names another statement ID. So a change of the contract, the signature or the witness makes old evidence stale by construction. LexLean does not run the foreign code and does not vouch for the report: the attestation records who is trusted for what. +- `attested{report: A}`: `A` is an `artifact` declaration of role `conformance`, content-addressed like every artifact. Its bytes are a canonical JSON report that states the statement ID of the external declaration (`SM-38`), the digest of the foreign code tested, the reference it was compared with, the number of cases and the result. Linking refuses a report that names another statement ID, and the project row must pin the report's digest. The report's bytes and the contract's identity are thereby authenticated; that is all the hash and the statement ID do. Binding the report to what actually runs is a separate check, which `verify` makes and which refuses (`LLV7007`-class refusal, code to be registered with the implementation) unless all of these hold: the report's code digest equals the digest of the artifact the build selects for the declaration; the report's symbol, ABI and target equal the declaration's `symbol` and the build's target; the report's attester is one the `[[external_source]]` row names as trusted; and the report's result is `pass` with at least the case count the row requires. With D6 undecided there is no selected artifact, so until then `verify` reports the evidence as `unbound` and says so in the trust line. So a change of the contract, the signature or the witness makes old evidence stale by construction. LexLean does not run the foreign code and does not vouch for the report: the attestation records who is trusted for what. The witness (D3) establishes only that the logical contract is satisfiable; a finite conformance report does not establish universal native correctness, purity or termination, and no wording of this document claims it. - `proved{theorem: T}`: `T` is a theorem, checked by the pinned Lean, that a Lean model of the foreign code meets the contract. This needs the model to be importable (#362) and belongs to a later change. It does not remove trust; it moves it to whatever produced the model (section 8). ### 6.5 Policy and reporting - A project lists the externals it admits: `[[external_source]]` in `lexlean.toml` with the module, the name and the report digest, pinned in `lexlean.lock`. An `external` declaration without its row is refused. A project with no row accepts no external declaration, so the default is the ban as it is today. +- **Proof dependencies and executable uses are different facts.** The axiom audit (§5.4) reports what `#print axioms` returns: a theorem that uses `dotFast.contract` lists it. It is not a call graph. A definition can call the opaque `dotFast` without mentioning the axiom, and a reflexive or conditional theorem can mention the call without it. So the axiom set alone does not account for every runtime use of foreign code. The design therefore adds a second, fail-closed inventory, the **external-call inventory**: the linker computes, from the IR, every definition, directly or transitively, that calls an `external` function, and the attestation records it (`external_uses`) beside `trust`. A production or `verify` root that reaches an external call not covered by an admitted `[[external_source]]` row is refused. The inventory is computed by LexLean, not by Lean, and the trust lines say so; the axiom audit is not claimed to be it. - Each theorem and definition states its exact axioms as now. A theorem that uses `dotFast.contract` lists `Project.Module.dotFast.contract` beside `propext`; a missing or extra one is `LLV7005`, unchanged. -- The attestation gains `trust`: for each contract axiom, its evidence, and the declarations whose observed sets contain it. `verify` prints one line per contract axiom after its summary, for example: +- The attestation gains `trust`: for each contract axiom, its evidence, the declarations whose observed sets contain it (proof dependencies), and, from the external-call inventory, the declarations that call the function (executable uses). `verify` prints one line per contract axiom after its summary, for example: ```text verified 1 module; attestation -trust: Engine.Main.dotFast.contract, attested by sha256:, used by 1 theorem and 1 definition +trust: Engine.Main.dotFast.contract, attested by sha256:, proof-used by 1 theorem; called by 1 definition (call inventory: LexLean) ``` - A status line never says "verified" without the trust lines when any exist. @@ -219,6 +220,7 @@ Estimated from the arrays change, which touched the same layers: - The contract is about a mathematical function. Foreign code that reads a clock, keeps state, or does not terminate is outside what the axiom says, and evidence by testing cannot establish totality. - Attested evidence is as good as the test that produced it. LexLean binds the report to the contract and to the code digest; it does not judge coverage. - A witness is required. A contract for which no LexLean function can be written (for example one that only bounds an error) needs the reference written first. For an inference kernel the witness is the integer reference engine, which has to exist anyway. +- Evidence is witness-only until D6: the report is bound to the contract, not to a running artifact, and `verify` says `unbound`. - Until D6 is decided the compiled Lean runs the witness, so the trust entry describes a binding a host makes, not one LexLean emits. ## 11. Not implemented by this PR From 29be144a11b842ddddb564de324a752a9bfdd06b Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Nov 2026 19:59:52 +0000 Subject: [PATCH 13/22] docs(proposals): fold verification-memo (#508) into this docs carrier, record D1 One reviewed documentation carrier, as requested on #492. D1 (SPEC.md 9.4 reuse of verification results) is decided: not adopted now. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_013Asa3M8ULnzEQwLLngyBBX --- docs/proposals/external-contracts.md | 2 +- docs/proposals/verification-memo.md | 135 +++++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 docs/proposals/verification-memo.md diff --git a/docs/proposals/external-contracts.md b/docs/proposals/external-contracts.md index f82c7a20f9..cd8aae3872 100644 --- a/docs/proposals/external-contracts.md +++ b/docs/proposals/external-contracts.md @@ -113,7 +113,7 @@ What remains assumed is not logical. It is the statement that the foreign code, ### 6.4 Evidence -- `attested{report: A}`: `A` is an `artifact` declaration of role `conformance`, content-addressed like every artifact. Its bytes are a canonical JSON report that states the statement ID of the external declaration (`SM-38`), the digest of the foreign code tested, the reference it was compared with, the number of cases and the result. Linking refuses a report that names another statement ID, and the project row must pin the report's digest. The report's bytes and the contract's identity are thereby authenticated; that is all the hash and the statement ID do. Binding the report to what actually runs is a separate check, which `verify` makes and which refuses (`LLV7007`-class refusal, code to be registered with the implementation) unless all of these hold: the report's code digest equals the digest of the artifact the build selects for the declaration; the report's symbol, ABI and target equal the declaration's `symbol` and the build's target; the report's attester is one the `[[external_source]]` row names as trusted; and the report's result is `pass` with at least the case count the row requires. With D6 undecided there is no selected artifact, so until then `verify` reports the evidence as `unbound` and says so in the trust line. So a change of the contract, the signature or the witness makes old evidence stale by construction. LexLean does not run the foreign code and does not vouch for the report: the attestation records who is trusted for what. The witness (D3) establishes only that the logical contract is satisfiable; a finite conformance report does not establish universal native correctness, purity or termination, and no wording of this document claims it. +- `attested{report: A}`: `A` is an `artifact` declaration of role `conformance`, content-addressed like every artifact. Its bytes are a canonical JSON report that states the statement ID of the external declaration (`SM-38`), the digest of the foreign code tested, the reference it was compared with, the number of cases and the result. Linking refuses a report that names another statement ID, and the project row must pin the report's digest. The report's bytes and the contract's identity are thereby authenticated; that is all the hash and the statement ID do. Binding the report to what actually runs is a separate check, which `verify` makes and which refuses (a new diagnostic, registered with the implementation) unless all of these hold: the report's code digest equals the digest of the artifact the build selects for the declaration; the report's symbol, ABI and target equal the declaration's `symbol` and the build's target; the report's attester is one the `[[external_source]]` row names as trusted; and the report's result is `pass` with at least the case count the row requires. With D6 undecided there is no selected artifact, so until then `verify` reports the evidence as `unbound` and says so in the trust line. So a change of the contract, the signature or the witness makes old evidence stale by construction. LexLean does not run the foreign code and does not vouch for the report: the attestation records who is trusted for what. The witness (D3) establishes only that the logical contract is satisfiable; a finite conformance report does not establish universal native correctness, purity or termination, and no wording of this document claims it. - `proved{theorem: T}`: `T` is a theorem, checked by the pinned Lean, that a Lean model of the foreign code meets the contract. This needs the model to be importable (#362) and belongs to a later change. It does not remove trust; it moves it to whatever produced the model (section 8). ### 6.5 Policy and reporting diff --git a/docs/proposals/verification-memo.md b/docs/proposals/verification-memo.md new file mode 100644 index 0000000000..cf27e897c2 --- /dev/null +++ b/docs/proposals/verification-memo.md @@ -0,0 +1,135 @@ +# Verification results reused across runs: design + +Draft for owner acceptance, written 2026-11-13, against `release/v0.4` at `26e5e0ace`. Normative text is `SPEC.md`; this document explains and asks. It is a proposal only: this pull request adds this one file and changes no specification text, register row, workflow, compiler code, or generated document. It builds on two draft pull requests: #498 (every verification part names its toolchain by address) and #506 (one address scheme, `sha256:` over canonical bytes, `AR-15`). + +**Decision recorded on D1 (2026-11-13, maintainer, from SPEC.md §9.4 and the #106 plan): not adopted now.** §9.4's rule that the gate re-proves everything in every run is what makes a pull request's green mean what Lean checked in that run; this proposal's own text says it changes what the gate verifies, and its savings are estimates with no store to measure. §9.4 stands unchanged, no reuse on pull-request runs. The document stays as the design to revisit once the implementation can report measured numbers, and then only in a form that keeps pull-request runs proving their own inputs (for example reuse restricted to release-line runs). D2 to D8 are moot until D1 is reopened. + +## 1. Summary + +Today every CI run proves everything again. SPEC.md §9.4 says the gate has "no cache of a ... verification tree, attestation, or proof result" and that "every process of the verification runs exactly once per run". A slice pull request that changes one bootstrap module therefore re-elaborates, replays, and audits every module of every project, UOR Atlas included. + +The proposal is a content-addressed store of verifier process results: + +- A stored result is reused only when its key matches exactly. The key is the address of everything the process reads and runs: its source bytes, the keys of its imports, the executables and toolchain library, its arguments, its environment, its limits, and the compiler-semantics ID. +- The stored result's own address must also check out. +- Only pushes to `release/*` write the store. +- Every push to `main` or `release/*`, and every release, re-proves everything with the store off. + +**What it saves.** On a typical slice pull request, about 158 runner-minutes of Atlas and about 330 minutes of bdd and examples verification would reuse earlier results. + +**What it changes.** On a pull request, a reused process was proved by an earlier release-line run on byte-identical inputs, not by that pull request's run. That is a change to what the gate verifies, so it is the owner's decision. + +| # | Decision | Recommendation | +| --- | --- | --- | +| D1 | Adopt reuse by exact address on pull-request runs (the §9.4 wording in section 6) | Yes | +| D2 | The unit of reuse | One verifier process (an elaboration, a replay, an audit member), not one LexLean module | +| D3 | What a stored entry holds | The process record and its output files, never a verdict; every reused record is re-judged by this run's code | +| D4 | Who writes the store | Only pushes to `release/*`, which also re-prove everything; pull-request runs only read it | +| D5 | Whether the toolchain library tree is in the key | Yes: `lean` loads `Init`, `Std` and `Lean` from it, and the executable's digest does not fix them | +| D6 | The `lexlean` executable in the key | No: it only judges records, and judging is redone every run. The attestation still names it | +| D7 | Spot check | Each run re-runs one reused process, chosen from the run ID, and requires byte equality | +| D8 | RP-14 | It changes with D1: no cache is still forbidden, except the custody-checked store | + +## 2. The unit, and why + +A LexLean module becomes three processes of the verifier: + +- its elaboration (`lake env lean -o `); +- its replay (`lake env leanchecker`); +- its audit member. + +The partitioned Atlas verification already moves exactly these between the jobs of one run (`verify/partition.rs`). A shard exports its `.olean` files and process records, and the join re-judges every record as the stage that produced it would. `VR-23` requires the join to publish, byte for byte, what the whole verification publishes. The store is the same thing across runs: a part an earlier run produced, found by address instead of by job name. + +## 3. The key, exactly + +`key(p)` is the address (#506) of a framed recipe with its own domain, `lexlean-verify-step-v1`, whose frames are: + +| Frame | Content | +| --- | --- | +| `step` | `elaborate`, `replay`, or `audit` | +| `module` | the full Lean module name | +| `source` | the address of the exact generated `.lean` bytes the process reads; for a replay, the key of the module's elaboration | +| `imports` | the keys of the elaborations of the module's direct imports, sorted by module. Each fixes its `.olean` and, recursively, everything below it | +| `argv`, `env` | the normalized argument vector and allow-listed environment, as the process record writes them | +| `toolchain` | the addresses of `lean`, `lake`, `leanchecker` (#498), and of the toolchain's `lib/` tree | +| `lake-workspace` | the lock hashes the attestation lists | +| `compiler-semantics` | the project language's compiler-semantics ID | +| `limits` | `child_timeout_ms` and `max_child_output_bytes` | +| `host` | operating system and architecture | + +**Left out of the key, on purpose:** +- **Width, hints and the memory budget.** §22.11 already requires that they never change a byte. +- **The `lexlean` executable.** It decides how records are judged, and judging is redone on every run. +- **Timings and the run.** + +**A stored entry** is `{key, record, outputs}`: the normalized process record plus the address of every output file. The output files are the `.olean`, `.ilean` and the intermediates the next elaboration reads. The entry's own address is the address of its canonical bytes. The store is `index/`, which holds the entry's address, plus `objects/
`, which holds entries and output files. + +## 4. Why reuse is sound + +A process is a function of the executable and every file it loads, its arguments, its environment, the bytes it reads, and its limits. The key names all of them, directly or through import keys. + +LexLean already relies on that function being deterministic, in three places: +- `VR-23` requires the partitioned Atlas to publish what the whole verification publishes; +- §22.11 requires width 1 and width N to agree; +- every gate compares normalized records against `expected/verify` (§29.5). + +So equal keys give equal records and equal `.olean` bytes. The attestation built over reused records is byte-identical to a fresh one, and it does not mention reuse; telemetry and the job summary do. + +**The one input that is not a function of bytes is wall time.** A process that passed in 290 s may time out at 300 s on a slower host. A timeout is an environment failure, never a verdict, so reusing the pass changes no proof claim. + +## 5. What it refuses + +- **A stale store.** Any changed input changes the key, so the lookup misses and the process runs. No entry is ever matched by name or path. +- **A changed toolchain.** The toolchain addresses are in the key, so it misses. On a hit, the stored record must still name this run's executables (the join's existing `LLI9001` check, extended by #498 to `lake`, `lexlean` and `rustc`). +- **A corrupted entry.** On every hit the verifier recomputes the entry's address and every output file's address, and requires them to equal the index. It also requires the entry's key to equal the key it asked for. A mismatch fails the verification; it is evidence of tampering, not a miss. +- **A consistent forgery**, by someone who can write the store and recompute the hashes. Addresses give integrity, not authority. Authority comes from custody: + - Only a push to `release/*` writes the store. + - Pull-request runs read it from that push run's artifact, through a custody record checked the way the bootstrap export's record is (#476, #498). The record names the repository, workflow, run, commit, and executable addresses. + - The spot check (D7) re-runs one reused process in every run and requires byte equality. +- **Release evidence that rests on the store.** Every push to `main` or `release/*`, every release, and every `workflow_dispatch` marked `full` verifies with the store off. + +## 6. The SPEC.md §9.4 wording (D1) + +Replace "The gate has no path filter, no cache of a build directory, object, verification tree, attestation, or proof result, and no condition that skips a recipe of `just vv` but the plan's" with: + +> The gate has no path filter, no cache of a build directory or object, and no condition that skips a recipe of `just vv` but the plan's. A verification process (§22.11) may be reused from an earlier run only by exact address: its key is the address of every input it reads, the executables and toolchain library it runs, its argument vector, environment, and limits, and the compiler-semantics ID; the reused record and output files must reproduce their recorded addresses, and the record must name this run's executables. Only a push to `release/*` writes such results, carried to later runs as an artifact with a custody record (`lexlean/ci-verify-store/1`) that `RP-14` checks; a pull request run reads them and writes none. Every push to `main` or to a release line, every release, and every `workflow_dispatch` with `full` verifies with no reuse. The records and attestation a run publishes are byte for byte those of a run with no reuse. + +Two further sentences change: +- "Every process of the verification runs exactly once per run" becomes "Every process of the verification runs at most once per run and is reused only as above". +- "it, the output, and the records cross no run" (Atlas) becomes "they cross runs only through the store above". + +**This changes what the gate verifies.** On a pull request, a reused process was proved by an earlier release-line run on byte-identical inputs, not by that pull request's run. + +## 7. What it saves + +From full gate 43046966603, using the verification seconds each job reports (gate-speed notes of 2026-11-12): + +| Where | Seconds | Reusable on a slice PR | +| --- | ---: | --- | +| Atlas shards | ~9,500 | all | +| `examples` leg | 4,866 | all but the bootstrap module that changed and its importers | +| `rest` | 4,378 | models ×18 = 2,132 of it | +| `preservation` | 4,396 | production ×10 collapse to one | +| `production` | 3,385 | production ×17 collapse to one | + +**Wall clock.** +- `preservation` (66 min) and `examples` (53–85 min) would drop to roughly 10–35 min. +- The Atlas chain would leave pull-request runs. +- The critical path becomes the BS-15 evaluator differential (~70 min) and the macOS AArch64 job. Differentials are not verification and do not speed up. + +**Locally.** +- A second `lexlean verify` of an unchanged project would run no Lean process. +- With one module changed, it would run: + - that module's elaboration and replay; + - the elaborations of the modules that import it whose `.olean` changed; + - every audit member. + + Audit members rerun on any change because the audit module's name embeds the semantic ID (`LexLeanAudit.A`, `backend/lean.rs`), which moves with every source change. + +**These numbers are estimates: the store does not exist to measure.** The pull request that implements it reports measured before and after times for a second verify of an unchanged project, for one changed module, and for a CI run of an unchanged tree. + +## 8. Cost + +- **Hashing the toolchain `lib/` tree.** It is 2.8 GB and took 24 s warm and 36 s cold in a shared 16 GB WSL VM. It is computed once per process, and only when the store is on. +- **Storage.** About as large as the `.olean` files of every verified project, carried as one artifact per release-line push. +- **A compiler-semantics or Lean-pin change invalidates every key,** so such pull requests pay the full price, as today. From c4537d50b53fbc5fe1351173e6099a23fe925f6d Mon Sep 17 00:00:00 2001 From: humuhumu33 <68450279+humuhumu33@users.noreply.github.com> Date: Fri, 13 Nov 2026 21:47:31 +0000 Subject: [PATCH 14/22] The lexicon schema, LSE scopes and LSE type checker as source, batch (BS-50..52, #269) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit One batch branch carrying three independent slices of the lexicon closure, as BS-25..34 landed in #480: - BS-50 LexiconSchema: the category/frame/channel/associativity/feature tables and surface_safety/is_lean_name/toml_comment_at/entry_path, over the atoms of BS-03. Six roots. - BS-51 LseScopes: Lse::check_scopes, referenced_consts, and outer_explicit_binders/result, over the expressions of BS-25. Three roots. - BS-52 LseTypes: the conservative checker, check_signature and check_value, structural over a host-given nesting bound. Two roots. Register rows (model/ids.toml, SPEC §31, VERIFICATION.md, features scenarios) are inserted as one whole block per slice, in register-ID order, after BS-34. Counts and totals that the regeneration itself writes (README.md, CHANGELOG.md, the §31 total line, repository.rs's and spec_links.rs's row counts) are left at release's current values for the regeneration to fix. .github/workflows/regenerate.yml is release's version overwritten with the fixed one from ci/regenerate-relocks (the deliver job and a relock step that survives combine), so this branch's own regeneration lands itself onto the PR opened for this batch branch. #522, #523 and #524 stay open as the per-slice record; they are not what lands. Branched from release/v0.4 at 6a02d2c7d519f1a4d169ebb3496fbad51bd259cb. Co-Authored-By: Claude Sonnet 5 --- .github/bdd-parts.json | 9 + .github/intel-host-lanes.json | 2 +- .github/vv-rows.json | 27 + .github/workflows/regenerate.yml | 12 + SPEC.md | 54 + VERIFICATION.md | 122 ++ bootstrap/README.md | 9 + bootstrap/lexlean.lock | 57 - bootstrap/lexlean.toml | 2 +- bootstrap/src/LexiconSchema.lex.tex | 434 ++++++ bootstrap/src/LseScopes.lex.tex | 248 ++++ bootstrap/src/LseTypes.lex.tex | 1254 +++++++++++++++++ crates/conformance/src/cases/bootstrap.rs | 6 + .../src/cases/bootstrap/lexicon_lse.rs | 4 +- .../src/cases/bootstrap/lexicon_schema.rs | 973 +++++++++++++ .../src/cases/bootstrap/lse_scopes.rs | 670 +++++++++ .../src/cases/bootstrap/lse_types.rs | 833 +++++++++++ crates/conformance/tests/conformance.rs | 15 + features/suites/bootstrap.feature | 21 + model/ids.toml | 18 + tests/production-export/bootstrap/roots.json | 88 ++ 21 files changed, 4797 insertions(+), 61 deletions(-) mode change 100755 => 100644 .github/workflows/regenerate.yml delete mode 100644 bootstrap/lexlean.lock create mode 100644 bootstrap/src/LexiconSchema.lex.tex create mode 100644 bootstrap/src/LseScopes.lex.tex create mode 100644 bootstrap/src/LseTypes.lex.tex mode change 100755 => 100644 crates/conformance/src/cases/bootstrap/lexicon_lse.rs create mode 100644 crates/conformance/src/cases/bootstrap/lexicon_schema.rs create mode 100644 crates/conformance/src/cases/bootstrap/lse_scopes.rs create mode 100644 crates/conformance/src/cases/bootstrap/lse_types.rs diff --git a/.github/bdd-parts.json b/.github/bdd-parts.json index f385106a69..9812b386f5 100644 --- a/.github/bdd-parts.json +++ b/.github/bdd-parts.json @@ -277,6 +277,15 @@ ], "bootstrap-manifest-text": [ "BS-34" + ], + "bootstrap-lexicon-schema": [ + "BS-50" + ], + "bootstrap-lse-scopes": [ + "BS-51" + ], + "bootstrap-lse-types": [ + "BS-52" ] }, "targets": { diff --git a/.github/intel-host-lanes.json b/.github/intel-host-lanes.json index 6a7f0aa94a..6c1fbfdeca 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-lexicon-schema", "bootstrap-lse-scopes", "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..e0da1133de 100644 --- a/.github/vv-rows.json +++ b/.github/vv-rows.json @@ -358,5 +358,32 @@ "width": "", "memory_kib": "", "hints": "" + }, + { + "part": "bdd-bootstrap-lexicon-schema", + "recipes": "bdd", + "examples_part": "", + "bdd_part": "bootstrap-lexicon-schema", + "width": "", + "memory_kib": "", + "hints": "" + }, + { + "part": "bdd-bootstrap-lse-scopes", + "recipes": "bdd", + "examples_part": "", + "bdd_part": "bootstrap-lse-scopes", + "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/.github/workflows/regenerate.yml b/.github/workflows/regenerate.yml old mode 100755 new mode 100644 index 2b1fb67b43..1e93c99731 --- a/.github/workflows/regenerate.yml +++ b/.github/workflows/regenerate.yml @@ -91,6 +91,13 @@ jobs: sudo mkswap "$RUNNER_TEMP/lexlean-regenerate.swap" sudo swapon "$RUNNER_TEMP/lexlean-regenerate.swap" sudo sysctl vm.swappiness=10 + - name: Refresh the project locks + shell: bash + run: | + set -euo pipefail + cargo build -q -p lexlean + L="$PWD/target/debug/lexlean" + for p in bootstrap stdlib compiler; do (cd "$p" && "$L" lock); done - name: Run the write recipes shell: bash env: @@ -114,6 +121,11 @@ jobs: set -euo pipefail mkdir -p "$RUNNER_TEMP/part" git add --all + # Every writer refreshes the locks, because its recipes read them; one + # writer reports them, or `combine` sees three parts write one path. + if [ "${{ matrix.writer }}" != production-export ]; then + git reset -q -- bootstrap/lexlean.lock stdlib/lexlean.lock compiler/lexlean.lock + fi git diff --cached --binary > "$RUNNER_TEMP/part/${{ matrix.writer }}.patch" git diff --cached --name-only | tee "$RUNNER_TEMP/part/${{ matrix.writer }}.files" echo "${{ matrix.writer }}: $(wc -l < "$RUNNER_TEMP/part/${{ matrix.writer }}.files") paths" >> "$GITHUB_STEP_SUMMARY" diff --git a/SPEC.md b/SPEC.md index a4a3468197..9afb2f70b9 100644 --- a/SPEC.md +++ b/SPEC.md @@ -9118,6 +9118,57 @@ 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 tables and predicates of the lexicon entry schema (§13.1 to §13.6) are the first +component of the lexicon closure as source (`BS-50`, issue 269), the module `LexiconSchema` +over the atoms of the primitive scanner (`BS-03`), with six production roots for `rust-std`: + +| Root | Takes | Returns | Oracle | +| --- | --- | --- | --- | +| `LexLeanBoot.LexiconSchema.tokenIndex` | a table and a token | its position in the schema's order, or none | `Category::parse`, `Frame::parse`, `Channel::parse`, `Associativity::parse`, `entry::FEATURES` | +| `LexLeanBoot.LexiconSchema.shapeFacts` | a category, a frame, and a surface arity | whether the frame is permitted, the arity it fixes, and the category's facts | `entry::frame_permitted`, `Frame::fixed_arity`, `Category::core_only`, `has_signature`, `required_channels`, `needs_some_canonical` | +| `LexLeanBoot.LexiconSchema.surfaceSafety` | the atoms of a surface and a channel | nothing, or the message naming the first atom that is not renderer-safe | `entry::surface_safety` | +| `LexLeanBoot.LexiconSchema.isLeanName` | a text | whether it is a conservative ASCII Lean name | `entry::is_lean_name` | +| `LexLeanBoot.LexiconSchema.tomlCommentAt` | a TOML text | the offset of its first comment outside every string form | `package::toml_comment_at` | +| `LexLeanBoot.LexiconSchema.entryPath` | a local entry ID | its file path under `entries/` | `package::entry_path` | + +`surfaceSafety` decodes each scalar of a Unicode atom from its UTF-8 bytes and tests the +allow-list of `is_math_safe_scalar`; `tomlCommentAt` is the oracle's machine of five states over +the bytes. None of the roots admits `recursion`. Reading an entry file and the diagnostics of +the entry validator stay in the host. + +The scopes, the references, and the outer shape of a semantic expression (§13.8) are the +second component of the lexicon closure as source (`BS-51`, issue 269), the module `LseScopes` +over the expressions of `BS-25`, with three production roots for `rust-std`: + +| Root | Takes | Returns | Oracle | +| --- | --- | --- | --- | +| `LexLeanBoot.LseScopes.checkScopes` | an expression, the declared universes, and the length of its text | nothing, or the message of the first local or universe out of scope | `lse::Lse::check_scopes` | +| `LexLeanBoot.LseScopes.referencedConsts` | an expression and the length of its text | its qualified constants in occurrence order | `lse::Lse::referenced_consts` | +| `LexLeanBoot.LseScopes.outerShape` | an expression and the length of its text | the explicit binders of its outermost `pi` and whether its result is a sort | `lse::Lse::outer_explicit_binders`, `lse::Lse::result` | + +Each root walks the expression with an explicit work stack in the oracle's order, a binder's +type before its name and a scope truncated when its `pi`, `lam`, or `let` ends; the length of +the text bounds the walk. None of the roots admits `recursion`. The conservative type checker +of §13.7 stays 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 @@ -12154,6 +12205,9 @@ 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-50` | `bootstrap` | The tables and predicates of the lexicon entry schema of SPEC.md §13.1 to §13.6 (`lexicon::entry`: the category, frame, channel, associativity, and feature tokens, each read as its position in the schema's order; for a category, a frame, and a surface arity, `frame_permitted`, the arity `Frame::fixed_arity` fixes, whether the category is core-only, carries a signature, or needs a canonical source form in some channel, and the channels it requires one in; `is_lean_name`, the conservative ASCII Lean-name grammar; and `surface_safety` over the atoms of the primitive scanner, with the allow-list of `is_math_safe_scalar`, nothing or the message naming the first atom that is not renderer-safe; and `lexicon::package`: `toml_comment_at`, the offset of the first comment outside every TOML string form, and `entry_path`) is language-1.2 source in the `bootstrap/` project, as six production roots of `LexiconSchema` over the atoms of `BS-03`: `tokenIndex`, `shapeFacts`, `surfaceSafety`, `isLeanName`, `tomlCommentAt`, and `entryPath`, 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 vectors, which reach every token of every table, every category with every frame at four arities, every class of atom in every channel, and the edges of the allow-list, on every form surface, Lean name, and entry ID of the committed lexicon entries and on every committed TOML file, 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; reading an entry file and the diagnostics of the entry validator are outside the claim. | §17.23, §13.1, §13.2, §13.3, §13.4, §13.5, §13.6 | +| `BS-51` | `bootstrap` | The scopes, the references, and the outer shape of a LexLean Semantic Expression of SPEC.md §13.8 (`lexicon::lse::Lse::check_scopes`: every local resolves lexically, a binder's type seen before its name and a `let`'s type and value before its name, and every universe variable is declared, or the message of the first that is not, in the oracle's order of visit; `referenced_consts`: the qualified constants in occurrence order; and `outer_explicit_binders` with `result`: the explicit binders of the outermost `pi` and whether its result is a sort) is language-1.2 source in the `bootstrap/` project, as three production roots of `LseScopes` over the expressions of `BS-25`: `checkScopes`, `referencedConsts`, and `outerShape`, each eligible for `rust-std` with exactly its declared effects, `allocation` and `overflow`, none of them `recursion`, 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, which reach both errors, shadowing, the end of every binder's scope, and every universe form, on every signature and defined value of the committed lexicon entries under the universes its entry declares, and on seeded expressions under seeded declarations; 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; the conservative type checker of §13.7 is outside the claim. | §17.23, §13.8 | +| `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 | diff --git a/VERIFICATION.md b/VERIFICATION.md index 685148b770..3f0f036f69 100644 --- a/VERIFICATION.md +++ b/VERIFICATION.md @@ -11552,6 +11552,128 @@ 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 tables of the lexicon entry schema are source (BS-50) + +`BS-50` holds the six production roots of `LexLeanBoot.LexiconSchema` +(`bootstrap/src/LexiconSchema.lex.tex`) to `lexicon::entry` and +`lexicon::package`: `tokenIndex` to the category, frame, channel, and +associativity parsers and the feature list, as a position in the schema's +order; `shapeFacts` to `frame_permitted`, `Frame::fixed_arity`, and the +category's `core_only`, `has_signature`, `required_channels`, and +`needs_some_canonical`; `surfaceSafety` to `surface_safety` over the atoms the +primitive scanner returns, which reads each scalar of a Unicode atom from its +UTF-8 bytes against the allow-list of `is_math_safe_scalar`; `isLeanName` to +`is_lean_name`; `tomlCommentAt` to `toml_comment_at`; and `entryPath` to +`entry_path`. No root admits `recursion`: each scan is a loop under +`iterate_until` or a fold. The oracle is the existing public code of +`entry.rs` and `package.rs`; no Rust outside the case changes. + +The case holds the roots to the oracle on hand-built vectors that reach every +token of every table and near misses, every category with every frame at +arities 0 to 3 and at the largest, every class of atom in every channel and +the edges of every range of the allow-list, Lean names with empty, leading, +and trailing segments, comments inside and after every TOML string form with +escapes, and entry IDs of empty segments; on every form surface in its own +channel and the two others, every Lean name of a denotation or an eliminator, +and every entry ID of the committed lexicon entries, and on every committed +TOML file; and on seeded inputs. Each of the 24 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 was first compiled and run on the normative host over `release/v0.4` +at `26e5e0ace` (`cargo test -p repo-conformance --test conformance -- +conformance_bs_50`). Its corpus is 838 named vectors, 1,064 committed forms, +725 Lean names, 1,007 entry IDs, 1,055 lexicon files, and 1,683 other TOML +files, and 300 and 2,000 seeded runs per root. The evaluator agrees with the +oracle on 8,317 runs, the rendered packages under the pinned rustc on 18,500 +runs, and each of the 24 planted defects is detected by its own vector. The +case then stops at the stack-bound step, `bootstrap/token_index is no package +of the committed exports`, as each bootstrap member does before its +regeneration, which produces the six packages. + +### the scopes and references of a semantic expression are source (BS-51) + +`BS-51` holds the three production roots of `LexLeanBoot.LseScopes` +(`bootstrap/src/LseScopes.lex.tex`) to `lexicon::lse`: `checkScopes` to +`Lse::check_scopes`, `referencedConsts` to `Lse::referenced_consts`, and +`outerShape` to `outer_explicit_binders` with whether `result` is a sort. Each +walks an expression of `BS-25` with an explicit work stack in the oracle's +order of visit, so that the first error is the oracle's first; a universe is +walked on the same stack, and a scope is a list truncated when its binder +ends. The host passes the length of the expression's text, which bounds the +walk. No root admits `recursion`. The oracle is the existing public code of +`lse.rs`; the case reads the encoder and the seeded expressions of `BS-25`'s +case, made visible to its sibling, and no other Rust changes. + +The case holds the roots to the oracle on hand-built expressions that reach +both errors, a binder's type before its name, a `let`'s value before its name, +shadowing, the end of the scope of every binder form, and every universe form; +on every signature and defined value of the committed lexicon entries under +the universes its entry declares, each of which the case asserts is in scope; +and on seeded expressions under seeded declarations. Each of the 14 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 was first compiled and run on the normative host over `release/v0.4` +at `26e5e0ace` (`cargo test -p repo-conformance --test conformance -- +conformance_bs_51`). Its corpus is 43 named vectors, the 417 committed +signatures and defined values, and 300 and 2,000 seeded expressions. The +evaluator agrees with the oracle on 2,194 runs, the rendered packages under +the pinned rustc on 7,294 runs, and each of the 14 planted defects is detected +by its own vector. The case then stops at the stack-bound step, as each +bootstrap member does before its regeneration, which produces the three +packages. + +### 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..a030293c15 100644 --- a/bootstrap/README.md +++ b/bootstrap/README.md @@ -55,3 +55,12 @@ 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/LexiconSchema.lex.tex` (SPEC.md §17.23, `BS-50`) imports `PrimitiveScan` (`BS-03`), whose +atoms it reads, `LexiconLse` (`BS-25`), whose byte classes it uses, and the standard library's +`Text`: the tables and predicates of the lexicon entry schema of SPEC.md §13. +`src/LseScopes.lex.tex` (SPEC.md §17.23, `BS-51`) imports `LexiconLse` (`BS-25`), whose +expressions it walks, and the standard library's `Order`: the scopes, references, and outer +shape of a semantic expression of SPEC.md §13.8. +`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..2026c09f22 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/LexiconSchema.lex.tex", "src/LockText.lex.tex", "src/LseScopes.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/LexiconSchema.lex.tex b/bootstrap/src/LexiconSchema.lex.tex new file mode 100644 index 0000000000..c72a6e54a6 --- /dev/null +++ b/bootstrap/src/LexiconSchema.lex.tex @@ -0,0 +1,434 @@ +\begin{lexlean}{LexiconSchema} +\useglossary{lexlean.std.nat@1.2.0} +\importmodule{LexLeanStd.Text} +\importmodule{LexiconLse} +\importmodule{PrimitiveScan} +\title{Natural number addition} +\begin{semanticmodule} +structure{ + name: Shape, + type_parameters: [], + parameters: [], + fields: [ + {name: permitted, type: Bool}, + {name: fixedArity, type: (Option Nat)}, + {name: coreOnly, type: Bool}, + {name: hasSignature, type: Bool}, + {name: requiredChannels, type: (List Nat)}, + {name: needsSome, type: Bool} + ] +} + +structure{ + name: Cm, + type_parameters: [], + parameters: [], + fields: [{name: pos, type: Nat}, {name: mode, type: Nat}, {name: found, type: (Option Nat)}] +} + +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: indexIn, + parameters: [table : (List Bytes), text : Bytes], + result: (Option Nat), + body: (second (prim list_fold (fun [text] (state : (Nat * (Option Nat))) (row : Bytes) => (match (second state) + | Option.some found => state + | Option.none => (if (prim equal row text : Bool) then ((first state), (ctor Option.some [Nat] (first state))) else (((first state) + 1), (ctor Option.none [Nat]))))) (0, (ctor Option.none [Nat])) table : (Nat * (Option Nat)))), + executable: true +} + +definition{ + name: natAt, + parameters: [table : (List Nat), wanted : Nat], + result: Nat, + body: (second (prim list_fold (fun [wanted] (state : (Nat * Nat)) (row : Nat) => (if ((first state) =? wanted) then (((first state) + 1), row) else (((first state) + 1), (second state)))) (0, 0) table : (Nat * Nat))), + executable: true +} + +definition{ + name: categories, + parameters: [], + result: (List Bytes), + body: (cons (lit "structural") (cons (lit "grammar") (cons (lit "label-word") (cons (lit "type-noun") (cons (lit "term-constant") (cons (lit "function") (cons (lit "prefix-function") (cons (lit "postfix-function") (cons (lit "infix-function") (cons (lit "noun-function") (cons (lit "binary-noun-function") (cons (lit "predicate-constant") (cons (lit "adjective-predicate") (cons (lit "intransitive-predicate") (cons (lit "transitive-predicate") (cons (lit "infix-predicate") (cons (lit "proof-constant") (nil Bytes)))))))))))))))))), + executable: true +} + +definition{ + name: frames, + parameters: [], + result: (List Bytes), + body: (cons (lit "atom") (cons (lit "call") (cons (lit "prefix") (cons (lit "postfix") (cons (lit "infix") (cons (lit "noun-of") (cons (lit "binary-noun-of") (cons (lit "adjective") (cons (lit "intransitive") (cons (lit "transitive") (nil Bytes))))))))))), + executable: true +} + +definition{ + name: channels, + parameters: [], + result: (List Bytes), + body: (cons (lit "text") (cons (lit "math") (cons (lit "both") (nil Bytes)))), + executable: true +} + +definition{ + name: associativities, + parameters: [], + result: (List Bytes), + body: (cons (lit "left") (cons (lit "right") (cons (lit "none") (nil Bytes)))), + executable: true +} + +definition{ + name: features, + parameters: [], + result: (List Bytes), + body: (cons (lit "singular") (cons (lit "plural") (cons (lit "sentence-case") (cons (lit "lower-case") (cons (lit "article-a") (cons (lit "article-an") (nil Bytes))))))), + executable: true +} + +definition{ + name: tableOf, + parameters: [which : Nat], + result: (List Bytes), + body: (if (which =? 0) then (categories) else (if (which =? 1) then (frames) else (if (which =? 2) then (channels) else (if (which =? 3) then (associativities) else (features))))), + executable: true +} + +definition{ + name: tokenIndex, + parameters: [which : Nat, text : Bytes], + result: (Option Nat), + body: (indexIn (tableOf which) text), + executable: true, + production: {effects: [allocation, overflow], targets: [rust-std]} +} + +definition{ + name: framesOfCategories, + parameters: [], + result: (List Nat), + body: (cons 0 (cons 0 (cons 0 (cons 0 (cons 0 (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 (cons 6 (cons 0 (cons 7 (cons 8 (cons 9 (cons 4 (cons 0 (nil Nat)))))))))))))))))), + executable: true +} + +definition{ + name: channelsOfCategories, + parameters: [], + result: (List Nat), + body: (cons 0 (cons 0 (cons 1 (cons 0 (cons 0 (cons 2 (cons 2 (cons 2 (cons 2 (cons 1 (cons 1 (cons 2 (cons 1 (cons 1 (cons 1 (cons 2 (cons 2 (nil Nat)))))))))))))))))), + executable: true +} + +definition{ + name: permittedFrame, + parameters: [category : Nat, arity : Nat], + result: Nat, + body: (if (category =? 16) then (if (arity =? 0) then 0 else 1) else (natAt (framesOfCategories) category)), + executable: true +} + +definition{ + name: fixedArity, + parameters: [frame : Nat], + result: (Option Nat), + body: (if (frame =? 1) then (ctor Option.none [Nat]) else (if (frame =? 0) then (ctor Option.some [Nat] 0) else (if (if (frame =? 4) then true else (if (frame =? 6) then true else (frame =? 9))) then (ctor Option.some [Nat] 2) else (ctor Option.some [Nat] 1)))), + executable: true +} + +definition{ + name: requiredChannels, + parameters: [category : Nat], + result: (List Nat), + body: (let code : Nat := (natAt (channelsOfCategories) category); (if (code =? 1) then (cons 0 (nil Nat)) else (if (code =? 2) then (cons 1 (nil Nat)) else (nil Nat)))), + executable: true +} + +definition{ + name: shapeFacts, + parameters: [category : Nat, frame : Nat, arity : Nat], + result: Shape, + body: (record Shape {permitted := (frame =? (permittedFrame category arity)), fixedArity := (fixedArity frame), coreOnly := (category (mathSafeStep text n position)) (n + 1) 0 : (Nat * Bool))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: textSafePunctuation, + parameters: [], + result: (List Bytes), + body: (cons (lit ":") (cons (lit "-") (cons (lit ",") (cons (lit ".") (cons (lit ";") (cons (lit "'") (nil Bytes))))))), + executable: true +} + +definition{ + name: rendererUnsafeAscii, + parameters: [], + result: (List Bytes), + body: (cons 0x5c (cons 0x25 (cons 0x24 (cons 0x23 (cons 0x26 (cons 0x7e (cons 0x5e (cons 0x5f (cons 0x7b (cons 0x7d (cons 0x22 (cons 0x60 (nil Bytes))))))))))))), + executable: true +} + +definition{ + name: mathDelimiters, + parameters: [], + result: (List Bytes), + body: (cons (lit "(") (cons (lit ")") (cons (lit "[") (cons (lit "]") (nil Bytes))))), + executable: true +} + +definition{ + name: isIn, + parameters: [table : (List Bytes), text : Bytes], + result: Bool, + body: (match (indexIn table text) | Option.some found => true | Option.none => false), + executable: true +} + +definition{ + name: textOk, + parameters: [atom : PrimitiveScan::Atom], + result: Bool, + body: (match (project atom kind) + | PrimitiveScan::AtomClass.Control => false + | PrimitiveScan::AtomClass.Word => true + | PrimitiveScan::AtomClass.Numeral => false + | PrimitiveScan::AtomClass.Delimiter => false + | PrimitiveScan::AtomClass.AsciiSymbol => (isIn (textSafePunctuation) (project atom text)) + | PrimitiveScan::AtomClass.UnicodeSymbol => false + | PrimitiveScan::AtomClass.Whitespace => true), + executable: true +} + +definition{ + name: mathOk, + parameters: [atom : PrimitiveScan::Atom, place : Nat], + result: Bool, + body: (match (project atom kind) + | PrimitiveScan::AtomClass.Control => false + | PrimitiveScan::AtomClass.Word => true + | PrimitiveScan::AtomClass.Numeral => (0 (isIn (mathDelimiters) (project atom text)) + | PrimitiveScan::AtomClass.AsciiSymbol => (if (isIn (rendererUnsafeAscii) (project atom text)) then false else true) + | PrimitiveScan::AtomClass.UnicodeSymbol => (allMathSafe (project atom text)) + | PrimitiveScan::AtomClass.Whitespace => true), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: className, + parameters: [atom : PrimitiveScan::Atom], + result: Bytes, + body: (match (project atom kind) + | PrimitiveScan::AtomClass.Control => (lit "control") + | PrimitiveScan::AtomClass.Word => (lit "word") + | PrimitiveScan::AtomClass.Numeral => (lit "numeral") + | PrimitiveScan::AtomClass.Delimiter => (lit "delimiter") + | PrimitiveScan::AtomClass.AsciiSymbol => (lit "ascii-symbol") + | PrimitiveScan::AtomClass.UnicodeSymbol => (lit "unicode-symbol") + | PrimitiveScan::AtomClass.Whitespace => (lit "whitespace")), + executable: true +} + +definition{ + name: channelName, + parameters: [channel : Nat], + result: Bytes, + body: (if (channel =? 0) then (lit "text") else (if (channel =? 1) then (lit "math") else (lit "text and math"))), + executable: true +} + +definition{ + name: atomSafe, + parameters: [atom : PrimitiveScan::Atom, place : Nat, channel : Nat], + result: Bool, + body: (if (channel =? 0) then (textOk atom) else (if (channel =? 1) then (mathOk atom place) else (if (textOk atom) then (mathOk atom place) else false))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: unsafeMessage, + parameters: [atom : PrimitiveScan::Atom, channel : Nat], + result: Bytes, + body: (cat (cat (cat (cat (cat (cat (lit "`") (project atom text)) (lit "` (")) (className atom)) (lit ") is not renderer-safe in the ")) (channelName channel)) (lit " channel")), + executable: true +} + +definition{ + name: surfaceSafety, + parameters: [atoms : (List PrimitiveScan::Atom), channel : Nat], + result: (Option Bytes), + body: (second (prim list_fold (fun [channel] (state : (Nat * (Option Bytes))) (atom : PrimitiveScan::Atom) => (match (second state) + | Option.some reason => state + | Option.none => (if (atomSafe atom (first state) channel) then (((first state) + 1), (ctor Option.none [Bytes])) else (((first state) + 1), (ctor Option.some [Bytes] (unsafeMessage atom channel)))))) (0, (ctor Option.none [Bytes])) atoms : (Nat * (Option Bytes)))), + axioms: [Quot.sound, propext], + executable: true, + production: {effects: [allocation, overflow], targets: [rust-std]} +} + +definition{ + name: isNameStart, + parameters: [value : Nat], + result: Bool, + body: (if (LexiconLse::isAlphaNum value) then true else (value =? 95)), + executable: true +} + +definition{ + name: isNameRest, + parameters: [value : Nat], + result: Bool, + body: (if (LexiconLse::isAlnumNum value) then true else (value =? 95)), + executable: true +} + +definition{ + name: segAt, + parameters: [st : (Nat * Bool)], + result: Bool, + body: (second st), + executable: true +} + +definition{ + name: leanStep, + parameters: [text : Bytes, n : Nat, st : (Nat * Bool)], + result: (Option (Nat * Bool)), + body: (let position : Nat := (first st); (if (position (leanStep text n st)) (n + 1) (0, true) : ((Nat * Bool) * Bool))); (if ((first final) =? n) then (if (segAt final) then false else true) else false))), + axioms: [Quot.sound, propext], + executable: true, + production: {effects: [allocation, overflow], targets: [rust-std]} +} + +definition{ + name: cmMove, + parameters: [st : Cm, step : Nat, mode : Nat], + result: (Option Cm), + body: (ctor Option.some [Cm] (record Cm {pos := ((project st pos) + step), mode := mode, found := (ctor Option.none [Nat])})), + executable: true +} + +definition{ + name: plainStep, + parameters: [text : Bytes, st : Cm], + result: (Option Cm), + body: (let position : Nat := (project st pos); (if (LexLeanStd.Text::matchAt text 0x222222 position) then (cmMove st 3 3) else (if (LexLeanStd.Text::matchAt text 0x272727 position) then (cmMove st 3 4) else (let value : Nat := (LexiconLse::byteNum text position); (if (value =? 34) then (cmMove st 1 1) else (if (value =? 39) then (cmMove st 1 2) else (if (value =? 35) then (ctor Option.some [Cm] (record Cm {pos := position, mode := 0, found := (ctor Option.some [Nat] position)})) else (cmMove st 1 0)))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: commentStep, + parameters: [text : Bytes, n : Nat, st : Cm], + result: (Option Cm), + body: (match (project st found) + | Option.some spot => (ctor Option.none [Cm]) + | Option.none => (let position : Nat := (project st pos); (if (position (commentStep text n st)) (n + 2) (record Cm {pos := 0, mode := 0, found := (ctor Option.none [Nat])}) : (Cm * Bool))) found)), + axioms: [Quot.sound, propext], + executable: true, + production: {effects: [allocation, overflow], targets: [rust-std]} +} + +definition{ + name: entryPath, + parameters: [localId : Bytes], + result: Bytes, + body: (cat (cat (lit "entries/") (LexLeanStd.Text::joinWith (LexLeanStd.Text::splitOn localId (46 : UInt8)) (lit "/"))) (lit ".toml")), + executable: true, + production: {effects: [allocation, overflow], targets: [rust-std]} +} +\end{semanticmodule} +\end{lexlean} diff --git a/bootstrap/src/LseScopes.lex.tex b/bootstrap/src/LseScopes.lex.tex new file mode 100644 index 0000000000..46918af36e --- /dev/null +++ b/bootstrap/src/LseScopes.lex.tex @@ -0,0 +1,248 @@ +\begin{lexlean}{LseScopes} +\useglossary{lexlean.std.nat@1.2.0} +\importmodule{LexLeanStd.Order} +\importmodule{LexiconLse} +\title{Natural number addition} +\begin{semanticmodule} +inductive{ + name: SWork, + type_parameters: [], + parameters: [], + constructors: [ + {name: SExpr, fields: [LexiconLse::Lse]}, + {name: SBind, fields: [Bytes]}, + {name: STrunc, fields: [Nat]}, + {name: SUni, fields: [LexiconLse::Universe]} + ] +} + +structure{ + name: Sc, + type_parameters: [], + parameters: [], + fields: [ + {name: work, type: (List SWork)}, + {name: scope, type: (List Bytes)}, + {name: depth, type: Nat}, + {name: error, type: (Option Bytes)} + ] +} + +structure{ + name: Rc, + type_parameters: [], + parameters: [], + fields: [{name: stack, type: (List LexiconLse::Lse)}, {name: found, type: (List (Bytes * Bytes))}] +} + +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: member, + parameters: [items : (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 items : Bool), + executable: true +} + +definition{ + name: dropFront, + parameters: [items : (List Bytes), count : Nat], + result: (List Bytes), + body: (first (prim iterate_until (fun (rest : (List Bytes)) => (match rest + | List.nil => (ctor Option.none [(List Bytes)]) + | List.cons head tail => (ctor Option.some [(List Bytes)] tail))) count items : ((List Bytes) * Bool))), + executable: true +} + +definition{ + name: scGo, + parameters: [st : Sc, work : (List SWork)], + result: (Option Sc), + body: (ctor Option.some [Sc] (record Sc {work := work, scope := (project st scope), depth := (project st depth), error := (ctor Option.none [Bytes])})), + executable: true +} + +definition{ + name: scFail, + parameters: [st : Sc, message : Bytes], + result: (Option Sc), + body: (ctor Option.some [Sc] (record Sc {work := (project st work), scope := (project st scope), depth := (project st depth), error := (ctor Option.some [Bytes] message)})), + executable: true +} + +definition{ + name: uniStep, + parameters: [st : Sc, rest : (List SWork), universes : (List Bytes), u : LexiconLse::Universe], + result: (Option Sc), + body: (match u + | LexiconLse::Universe.UNum n => (scGo st rest) + | LexiconLse::Universe.UVar name => (if (member universes name) then (scGo st rest) else (scFail st (cat (cat (lit "universe variable `") name) (lit "` is not declared")))) + | LexiconLse::Universe.USucc inner => (scGo st (cons (ctor SWork.SUni inner) rest)) + | LexiconLse::Universe.UMax items => (scGo st (cons (ctor SWork.SUni items) rest)) + | LexiconLse::Universe.UIMax a b => (scGo st (cons (ctor SWork.SUni a) (cons (ctor SWork.SUni b) rest))) + | LexiconLse::Universe.UNil => (scGo st rest) + | LexiconLse::Universe.UItem head tail => (scGo st (cons (ctor SWork.SUni head) (cons (ctor SWork.SUni tail) rest)))), + executable: true +} + +definition{ + name: exprStep, + parameters: [st : Sc, rest : (List SWork), expr : LexiconLse::Lse], + result: (Option Sc), + body: (match expr + | LexiconLse::Lse.LSortProp => (scGo st rest) + | LexiconLse::Lse.LSortType u => (scGo st (cons (ctor SWork.SUni u) rest)) + | LexiconLse::Lse.LConst p e us => (scGo st (cons (ctor SWork.SUni us) rest)) + | LexiconLse::Lse.LLocal name => (if (member (project st scope) name) then (scGo st rest) else (scFail st (cat (cat (lit "local `") name) (lit "` is not in scope")))) + | LexiconLse::Lse.LApp f args => (scGo st (cons (ctor SWork.SExpr f) (cons (ctor SWork.SExpr args) rest))) + | LexiconLse::Lse.LPi binders body => (scGo st (cons (ctor SWork.SExpr binders) (cons (ctor SWork.SExpr body) (cons (ctor SWork.STrunc (project st depth)) rest)))) + | LexiconLse::Lse.LLam binders body => (scGo st (cons (ctor SWork.SExpr binders) (cons (ctor SWork.SExpr body) (cons (ctor SWork.STrunc (project st depth)) rest)))) + | LexiconLse::Lse.LLet name ty value body => (scGo st (cons (ctor SWork.SExpr ty) (cons (ctor SWork.SExpr value) (cons (ctor SWork.SBind name) (cons (ctor SWork.SExpr body) (cons (ctor SWork.STrunc (project st depth)) rest)))))) + | LexiconLse::Lse.LNat digits => (scGo st rest) + | LexiconLse::Lse.LNil => (scGo st rest) + | LexiconLse::Lse.LArg head tail => (scGo st (cons (ctor SWork.SExpr head) (cons (ctor SWork.SExpr tail) rest))) + | LexiconLse::Lse.LBinder mode name ty more => (scGo st (cons (ctor SWork.SExpr ty) (cons (ctor SWork.SBind name) (cons (ctor SWork.SExpr more) rest))))), + executable: true +} + +definition{ + name: scStep, + parameters: [universes : (List Bytes), st : Sc], + result: (Option Sc), + body: (match (project st error) + | Option.some stopped => (ctor Option.none [Sc]) + | Option.none => (match (project st work) + | List.nil => (ctor Option.none [Sc]) + | List.cons item rest => (match item + | SWork.SExpr expr => (exprStep st rest expr) + | SWork.SBind name => (ctor Option.some [Sc] (record Sc {work := rest, scope := (cons name (project st scope)), depth := ((project st depth) + 1), error := (ctor Option.none [Bytes])})) + | SWork.STrunc wanted => (ctor Option.some [Sc] (record Sc {work := rest, scope := (dropFront (project st scope) (prim subtract (project st depth) wanted : Nat)), depth := wanted, error := (ctor Option.none [Bytes])})) + | SWork.SUni u => (uniStep st rest universes u)))), + executable: true +} + +definition{ + name: checkScopes, + parameters: [expr : LexiconLse::Lse, universes : (List Bytes), bound : Nat], + result: (Option Bytes), + body: (project (first (prim iterate_until (fun [universes] (st : Sc) => (scStep universes st)) ((prim multiply bound 4 : Nat) + 4) (record Sc {work := (cons (ctor SWork.SExpr expr) (nil SWork)), scope := (nil Bytes), depth := 0, error := (ctor Option.none [Bytes])}) : (Sc * Bool))) error), + executable: true, + production: {effects: [allocation, overflow], targets: [rust-std]} +} + +definition{ + name: rcGo, + parameters: [st : Rc, stack : (List LexiconLse::Lse)], + result: (Option Rc), + body: (ctor Option.some [Rc] (record Rc {stack := stack, found := (project st found)})), + executable: true +} + +definition{ + name: rcStep, + parameters: [st : Rc], + result: (Option Rc), + body: (match (project st stack) + | List.nil => (ctor Option.none [Rc]) + | List.cons expr rest => (match expr + | LexiconLse::Lse.LSortProp => (rcGo st rest) + | LexiconLse::Lse.LSortType u => (rcGo st rest) + | LexiconLse::Lse.LConst p e us => (ctor Option.some [Rc] (record Rc {stack := rest, found := (cons (p, e) (project st found))})) + | LexiconLse::Lse.LLocal name => (rcGo st rest) + | LexiconLse::Lse.LApp f args => (rcGo st (cons f (cons args rest))) + | LexiconLse::Lse.LPi binders body => (rcGo st (cons binders (cons body rest))) + | LexiconLse::Lse.LLam binders body => (rcGo st (cons binders (cons body rest))) + | LexiconLse::Lse.LLet name ty value body => (rcGo st (cons ty (cons value (cons body rest)))) + | LexiconLse::Lse.LNat digits => (rcGo st rest) + | LexiconLse::Lse.LNil => (rcGo st rest) + | LexiconLse::Lse.LArg head tail => (rcGo st (cons head (cons tail rest))) + | LexiconLse::Lse.LBinder mode name ty more => (rcGo st (cons ty (cons more rest))))), + executable: true +} + +definition{ + name: referencedConsts, + parameters: [expr : LexiconLse::Lse, bound : Nat], + result: (List (Bytes * Bytes)), + body: (LexLeanStd.Order::reverseList [(Bytes * Bytes)] (project (first (prim iterate_until (fun (st : Rc) => (rcStep st)) ((prim multiply bound 4 : Nat) + 4) (record Rc {stack := (cons expr (nil LexiconLse::Lse)), found := (nil (Bytes * Bytes))}) : (Rc * Bool))) found)), + executable: true, + production: {effects: [allocation, overflow], targets: [rust-std]} +} + +definition{ + name: isSort, + parameters: [expr : LexiconLse::Lse], + result: Bool, + body: (match expr + | LexiconLse::Lse.LSortProp => true + | LexiconLse::Lse.LSortType u => true + | LexiconLse::Lse.LConst p e us => false + | LexiconLse::Lse.LLocal name => false + | LexiconLse::Lse.LApp f args => false + | LexiconLse::Lse.LPi binders body => false + | LexiconLse::Lse.LLam binders body => false + | LexiconLse::Lse.LLet name ty value body => false + | LexiconLse::Lse.LNat digits => false + | LexiconLse::Lse.LNil => false + | LexiconLse::Lse.LArg head tail => false + | LexiconLse::Lse.LBinder mode name ty more => false), + executable: true +} + +definition{ + name: explicitStep, + parameters: [state : (LexiconLse::Lse * Nat)], + result: (Option (LexiconLse::Lse * Nat)), + body: (match (first state) + | LexiconLse::Lse.LBinder mode name ty more => (ctor Option.some [(LexiconLse::Lse * Nat)] (more, (if (mode =? 0) then ((second state) + 1) else (second state)))) + | LexiconLse::Lse.LSortProp => (ctor Option.none [(LexiconLse::Lse * Nat)]) + | LexiconLse::Lse.LSortType u => (ctor Option.none [(LexiconLse::Lse * Nat)]) + | LexiconLse::Lse.LConst p e us => (ctor Option.none [(LexiconLse::Lse * Nat)]) + | LexiconLse::Lse.LLocal name => (ctor Option.none [(LexiconLse::Lse * Nat)]) + | LexiconLse::Lse.LApp f args => (ctor Option.none [(LexiconLse::Lse * Nat)]) + | LexiconLse::Lse.LPi binders body => (ctor Option.none [(LexiconLse::Lse * Nat)]) + | LexiconLse::Lse.LLam binders body => (ctor Option.none [(LexiconLse::Lse * Nat)]) + | LexiconLse::Lse.LLet name ty value body => (ctor Option.none [(LexiconLse::Lse * Nat)]) + | LexiconLse::Lse.LNat digits => (ctor Option.none [(LexiconLse::Lse * Nat)]) + | LexiconLse::Lse.LNil => (ctor Option.none [(LexiconLse::Lse * Nat)]) + | LexiconLse::Lse.LArg head tail => (ctor Option.none [(LexiconLse::Lse * Nat)])), + executable: true +} + +definition{ + name: outerShape, + parameters: [expr : LexiconLse::Lse, bound : Nat], + result: (Nat * Bool), + body: (match expr + | LexiconLse::Lse.LPi binders body => ((second (first (prim iterate_until (fun (state : (LexiconLse::Lse * Nat)) => (explicitStep state)) (bound + 1) (binders, 0) : ((LexiconLse::Lse * Nat) * Bool)))), (isSort body)) + | LexiconLse::Lse.LSortProp => (0, true) + | LexiconLse::Lse.LSortType u => (0, true) + | LexiconLse::Lse.LConst p e us => (0, false) + | LexiconLse::Lse.LLocal name => (0, false) + | LexiconLse::Lse.LApp f args => (0, false) + | LexiconLse::Lse.LLam binders body => (0, false) + | LexiconLse::Lse.LLet name ty value body => (0, false) + | LexiconLse::Lse.LNat digits => (0, false) + | LexiconLse::Lse.LNil => (0, false) + | LexiconLse::Lse.LArg head tail => (0, false) + | LexiconLse::Lse.LBinder mode name ty more => (0, false)), + executable: true, + production: {effects: [allocation, overflow], targets: [rust-std]} +} +\end{semanticmodule} +\end{lexlean} diff --git a/bootstrap/src/LseTypes.lex.tex b/bootstrap/src/LseTypes.lex.tex new file mode 100644 index 0000000000..547c6b59af --- /dev/null +++ b/bootstrap/src/LseTypes.lex.tex @@ -0,0 +1,1254 @@ +\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)))), + axioms: [Quot.sound, propext], + 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)})), + axioms: [Quot.sound, propext], + 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: flagOf, + parameters: [flagged : (Bool * Ck)], + result: Bool, + body: (first flagged), + executable: true +} + +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))))))))), + axioms: [Quot.sound, propext], + 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 (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 +} + +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))), + axioms: [Quot.sound, propext], + 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)))))))))))), + axioms: [Quot.sound, propext], + 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)))))))), + axioms: [Quot.sound, propext], + 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)))))))))))), + axioms: [Quot.sound, propext], + 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))), + axioms: [Quot.sound, propext], + 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))), + axioms: [Quot.sound, propext], + 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))))), + axioms: [Quot.sound, propext], + 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)})))))), + axioms: [Quot.sound, propext], + 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))))))), + axioms: [Quot.sound, propext], + 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")))))))))), + axioms: [Quot.sound, propext], + 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))))), + axioms: [Quot.sound, propext], + 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)))), + axioms: [Quot.sound, propext], + 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 (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 (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 +} + +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)))), + axioms: [Quot.sound, propext], + 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))), + axioms: [Quot.sound, propext], + 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))))))))), + axioms: [Quot.sound, propext], + 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..bed91d6ca6 100644 --- a/crates/conformance/src/cases/bootstrap.rs +++ b/crates/conformance/src/cases/bootstrap.rs @@ -27,7 +27,10 @@ mod diagnostics; mod gnaf_validate; mod lexicon_lre; mod lexicon_lse; +mod lexicon_schema; mod lock_text; +mod lse_scopes; +mod lse_types; mod manifest_text; mod project_text; mod runtime_validate; @@ -2574,6 +2577,9 @@ fn case(id: &str) { "BS-32" => content_id::case(), "BS-33" => project_text::case(), "BS-34" => manifest_text::case(), + "BS-50" => lexicon_schema::case(), + "BS-51" => lse_scopes::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 old mode 100755 new mode 100644 index 8cc5c85c3d..3d252016f3 --- 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/lexicon_schema.rs b/crates/conformance/src/cases/bootstrap/lexicon_schema.rs new file mode 100644 index 0000000000..31bc27f2cf --- /dev/null +++ b/crates/conformance/src/cases/bootstrap/lexicon_schema.rs @@ -0,0 +1,973 @@ +//! `BS-50` (SPEC.md §17.23, §13.1 to §13.6): the tables and predicates of the +//! lexicon entry schema as language-1.2 source, `LexLeanBoot.LexiconSchema`, +//! held to the byte to `lexlean::lexicon::entry` (the category, frame, +//! channel, associativity and feature tokens, `frame_permitted`, +//! `Frame::fixed_arity`, `Category::core_only`, `has_signature`, +//! `required_channels`, `needs_some_canonical`, `is_lean_name`, +//! `surface_safety` with `is_math_safe_scalar`) and to +//! `lexlean::lexicon::package::{toml_comment_at, entry_path}`. +//! +//! `surfaceSafety` reads the atoms of the primitive scanner of `BS-03`, so a +//! form's surface is scanned by the oracle's scanner and handed over as the +//! atoms `scanSource` returns. Each planted defect names the one vector that +//! catches it. + +use std::collections::{BTreeMap, BTreeSet}; + +use lexlean::calculus::Value; +use lexlean::lexicon::entry::{ + frame_permitted, is_lean_name, surface_safety, Associativity, Category, Channel, Frame, + FEATURES, +}; +use lexlean::lexicon::package::{entry_path, toml_comment_at}; +use lexlean::source::atom::Atom; +use serde_json::Value as Json2; + +use super::rust_validate::{eligibility_of, rustc_differential}; +use super::scan::atom_value; +use super::{list, option, text, Run, Seeded}; +use crate::production_export as harness; +use crate::support::{self, repo_root}; + +const MODULE: &str = "LexLeanBoot.LexiconSchema"; +const ROOTS: [(&str, &[&str]); 6] = [ + ("tokenIndex", &["allocation", "overflow"]), + ("shapeFacts", &["allocation", "overflow"]), + ("surfaceSafety", &["allocation", "overflow"]), + ("isLeanName", &["allocation", "overflow"]), + ("tomlCommentAt", &["allocation", "overflow"]), + ("entryPath", &["allocation", "overflow"]), +]; + +/// The most a part of a certificate of a root may be, as the row states. +const CERTIFICATE_BUDGET: u64 = 3_145_728; +/// Seeded runs per root under the evaluator, and under rustc. +const EVALUATOR_SEEDED: usize = 300; +const RUSTC_SEEDED: usize = 2_000; + +/// The schema tokens in the order of the oracle's enums. +const CATEGORIES: [&str; 17] = [ + "structural", + "grammar", + "label-word", + "type-noun", + "term-constant", + "function", + "prefix-function", + "postfix-function", + "infix-function", + "noun-function", + "binary-noun-function", + "predicate-constant", + "adjective-predicate", + "intransitive-predicate", + "transitive-predicate", + "infix-predicate", + "proof-constant", +]; +const FRAMES: [&str; 10] = [ + "atom", + "call", + "prefix", + "postfix", + "infix", + "noun-of", + "binary-noun-of", + "adjective", + "intransitive", + "transitive", +]; +const CHANNELS: [&str; 3] = ["text", "math", "both"]; +const ASSOCIATIVITIES: [&str; 3] = ["left", "right", "none"]; + +// ------------------------------------------------------------- the encoders + +fn nat(value: u64) -> Value { + Value::Nat { + value: value.to_string(), + } +} + +fn boolean(value: bool) -> Value { + Value::Bool { value } +} + +fn index_of(table: &[&str], token: &str) -> u64 { + u64::try_from( + table + .iter() + .position(|row| *row == token) + .unwrap_or_else(|| panic!("`{token}` is in its table")), + ) + .expect("an index") +} + +fn channel_of(index: u64) -> Channel { + Channel::parse(CHANNELS[usize::try_from(index).expect("an index")]).expect("a channel") +} + +fn channel_index(channel: Channel) -> u64 { + match channel { + Channel::Text => 0, + Channel::Math => 1, + Channel::Both => 2, + } +} + +/// What the oracle reads a token of table `which` as: its position in the +/// enum, or nothing. +fn token_value(which: u64, token: &str) -> Value { + let found = match which { + 0 => Category::parse(token).map(|category| index_of(&CATEGORIES, category.as_str())), + 1 => Frame::parse(token).map(|frame| index_of(&FRAMES, frame.as_str())), + 2 => Channel::parse(token).map(channel_index), + 3 => Associativity::parse(token).map(|associativity| match associativity { + Associativity::Left => 0, + Associativity::Right => 1, + Associativity::None => 2, + }), + _ => FEATURES + .iter() + .position(|feature| *feature == token) + .map(|found| u64::try_from(found).expect("an index")), + }; + option(found.map(nat)) +} + +fn token_run(which: u64, token: &str) -> Run { + ( + vec![nat(which), text(token)], + serde_json::to_value(token_value(which, token)).expect("a value is JSON"), + ) +} + +/// What the oracle says of a category, a frame, and a surface arity, as the +/// module's `Shape`. +fn shape_run(category: u64, frame: u64, arity: u32) -> Run { + let category_enum = + Category::parse(CATEGORIES[usize::try_from(category).expect("an index")]).expect("a category"); + let frame_enum = Frame::parse(FRAMES[usize::try_from(frame).expect("an index")]).expect("a frame"); + let shape = Value::Adt { + constructor: 0, + fields: vec![ + boolean(frame_permitted(category_enum, frame_enum, arity)), + option(frame_enum.fixed_arity().map(|fixed| nat(u64::from(fixed)))), + boolean(category_enum.core_only()), + boolean(category_enum.has_signature()), + list( + category_enum + .required_channels() + .iter() + .map(|channel| nat(channel_index(*channel))) + .collect(), + ), + boolean(category_enum.needs_some_canonical()), + ], + }; + ( + vec![nat(category), nat(frame), nat(u64::from(arity))], + serde_json::to_value(shape).expect("a value is JSON"), + ) +} + +/// The atoms of a surface, as the oracle's scanner reads it when a package +/// loads (`entry::parse_entry`). +fn atoms_of(surface: &str) -> Option> { + let scalars = u64::try_from(surface.chars().count()).expect("a count"); + lexlean::source::scan::scan("
", surface, scalars).ok() +} + +fn safety_run(atoms: &[Atom], channel: u64) -> Run { + let outcome = surface_safety(atoms, channel_of(channel)).err(); + ( + vec![list(atoms.iter().map(atom_value).collect()), nat(channel)], + serde_json::to_value(option(outcome.as_deref().map(text))).expect("a value is JSON"), + ) +} + +fn lean_run(name: &str) -> Run { + ( + vec![text(name)], + serde_json::to_value(boolean(is_lean_name(name))).expect("a value is JSON"), + ) +} + +fn comment_run(source: &str) -> Run { + let found = toml_comment_at(source).map(|at| nat(u64::try_from(at).expect("an offset"))); + ( + vec![text(source)], + serde_json::to_value(option(found)).expect("a value is JSON"), + ) +} + +fn path_run(local: &str) -> Run { + ( + vec![text(local)], + serde_json::to_value(text(&entry_path(local))).expect("a value is JSON"), + ) +} + +// ------------------------------------------------------------------ vectors + +struct Vector { + label: String, + root: usize, + run: Run, +} + +fn scanned(surface: &str) -> Vec { + atoms_of(surface).unwrap_or_else(|| panic!("`{surface}` scans")) +} + +#[allow(clippy::too_many_lines)] +fn named() -> Vec { + let mut out: Vec = Vec::new(); + let mut add = |label: String, root: usize, run: Run| { + out.push(Vector { label, root, run }); + }; + + // tokenIndex: every token of every table, and near misses. + let tables: [(u64, Vec<&str>); 5] = [ + (0, CATEGORIES.to_vec()), + (1, FRAMES.to_vec()), + (2, CHANNELS.to_vec()), + (3, ASSOCIATIVITIES.to_vec()), + (4, FEATURES.to_vec()), + ]; + for (which, table) in &tables { + for token in table { + add(format!("the token `{token}` of table {which}"), 0, token_run(*which, token)); + } + for miss in ["", "Structural", "atom ", "noun_of", "both-", "article"] { + add(format!("the near miss `{miss}` in table {which}"), 0, token_run(*which, miss)); + } + } + add("a frame is not a category".to_owned(), 0, token_run(0, "infix")); + add("a category is not a frame".to_owned(), 0, token_run(1, "function")); + + // shapeFacts: every category with every frame, at arities 0, 1, 2, 3. + for category in 0..17u64 { + for frame in 0..10u64 { + for arity in 0..4u32 { + add( + format!( + "the shape of {} as {} at arity {arity}", + CATEGORIES[usize::try_from(category).expect("an index")], + FRAMES[usize::try_from(frame).expect("an index")] + ), + 1, + shape_run(category, frame, arity), + ); + } + } + } + add( + "the shape at the largest arity".to_owned(), + 1, + shape_run(16, 1, u32::MAX), + ); + + // surfaceSafety. + for (label, surface, channel) in [ + ("a text word", "natural number", 0), + ("a text surface with every safe punctuation", "a: b-c, d. e; f'", 0), + ("a text surface with a colon", "is: a", 0), + ("a text surface with a parenthesis", "f(x)", 0), + ("a text surface with a numeral", "x 2", 0), + ("a text surface with a plus", "a + b", 0), + ("a text surface with a Unicode symbol", "\u{2200}", 0), + ("a text surface with a control", "\\N", 0), + ("a math plus", "+", 1), + ("a math surface with an underscore", "a_b", 1), + ("a math surface with every unsafe symbol", "\\%$#&~^_{}\"`", 1), + ("a math surface with brackets", "[a](b)", 1), + ("a math surface with a brace", "{", 1), + ("a math surface led by a numeral", "2x", 1), + ("a math surface with an inner numeral", "x2", 1), + ("a math surface with alpha", "\u{3b1}", 1), + ("a math surface with capital omega", "\u{3a9}", 1), + ("a math surface with final sigma", "\u{3c2}", 1), + ("a math surface with not", "\u{ac}", 1), + ("a math surface with times", "\u{d7}", 1), + ("a math surface with division", "\u{f7}", 1), + ("a math surface with a middle dot", "\u{b7}", 1), + ("a math surface with plus-minus", "\u{b1}", 1), + ("a math surface with an ellipsis", "\u{2026}", 1), + ("a math surface with a prime", "\u{2032}", 1), + ("a math surface with a triple prime", "\u{2034}", 1), + ("a math surface with a quadruple prime", "\u{2057}", 1), + ("a math surface with the naturals", "\u{2115}", 1), + ("a math surface with an arrow", "\u{2192}", 1), + ("a math surface with for all", "\u{2200}", 1), + ("a math surface with the last operator", "\u{22ff}", 1), + ("a math surface with a ceiling", "\u{2308}", 1), + ("a math surface with a floor end", "\u{230b}", 1), + ("a math surface with an angle bracket", "\u{27e8}", 1), + ("a math surface with a white bracket", "\u{2983}", 1), + ("a math surface with a circled plus operator", "\u{2a01}", 1), + ("a math surface of a double-struck letter", "\u{1d538}", 1), + ("a math surface with the last alphanumeric", "\u{1d7ff}", 1), + ("a math surface with e acute", "\u{e9}", 1), + ("a math surface with a euro sign", "\u{20ac}", 1), + ("a math surface with an emoji", "\u{1f600}", 1), + ("a math surface with a control", "\\alpha", 1), + ("a both surface with a plus", "a + b", 2), + ("a both surface of words", "the sum", 2), + ("a both surface with a colon", "a:b", 2), + ("a both surface with a Unicode symbol", "\u{2264}", 2), + ("two unsafe atoms", "a % b $ c", 1), + ("no atoms", "", 1), + ] { + let atoms = if surface.is_empty() { + Vec::new() + } else { + scanned(surface) + }; + add(label.to_owned(), 2, safety_run(&atoms, channel)); + } + + // isLeanName. + for (label, name) in [ + ("a Lean name", "Nat.add"), + ("a Lean name of one segment", "Nat"), + ("a Lean name led by an underscore", "_private.x"), + ("a Lean name with digits", "Nat.add_zero1"), + ("the empty name", ""), + ("a name of one dot", "."), + ("a name with two dots", "Nat..add"), + ("a name with a trailing dot", "Nat."), + ("a name with a leading dot", ".Nat"), + ("a name whose segment starts with a digit", "Nat.1add"), + ("a name with a hyphen", "Nat.add-zero"), + ("a name with a space", "Nat add"), + ("a name with a non-ASCII letter", "Nat.\u{3b1}"), + ("a name with a prime", "Nat.add'"), + ] { + add(label.to_owned(), 3, lean_run(name)); + } + + // tomlCommentAt. + for (label, source) in [ + ("a comment line", "# c"), + ("no comment", "a = 1\n"), + ("a comment after a value", "a = 1 # c\n"), + ("a hash in a basic string", "a = \"#\"\n"), + ("an escaped quote before a hash", "a = \"x\\\"#\""), + ("an escaped backslash before a hash", "a = \"x\\\\\"#"), + ("a hash in a literal string", "a = '#'"), + ("a literal string broken by a line", "a = 'x\n# c"), + ("a basic string broken by a line", "a = \"x\n# c"), + ("a hash in a multi-line string", "a = \"\"\"\n#\n\"\"\""), + ("a quote inside a multi-line string", "a = \"\"\"x\"#\"\"\""), + ("an escape inside a multi-line string", "a = \"\"\"\\\"\"\"#\"\"\""), + ("a hash after a multi-line string", "a = \"\"\"x\"\"\" # c"), + ("a hash in a multi-line literal string", "a = '''\n#'''"), + ("a hash after a multi-line literal string", "a = '''x''' #"), + ("an unclosed basic string", "a = \"#"), + ("a backslash at the end", "a = \"\\"), + ("a non-ASCII byte before a hash", "a = \"\u{e9}\" #"), + ("an empty text", ""), + ] { + add(label.to_owned(), 4, comment_run(source)); + } + + // entryPath. + for (label, local) in [ + ("a nested entry", "nat.add.zero"), + ("an entry of one segment", "zero"), + ("an entry with hyphens", "nat.add-comm"), + ("the empty entry", ""), + ("an entry of empty segments", ".."), + ] { + add(label.to_owned(), 5, path_run(local)); + } + out +} + +// ------------------------------------------------------------- the corpora + +/// Every committed lexicon file and its parsed TOML, by repository path. +fn committed_files() -> Vec<(String, String)> { + 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") + || support::holds_cache_tag(entry))) + }); + let mut out = Vec::new(); + for entry in walker.flatten() { + let path = entry.path(); + let shown = path.to_string_lossy().replace('\\', "/"); + if !entry.file_type().is_file() || !shown.ends_with(".toml") { + continue; + } + let Ok(content) = std::fs::read_to_string(path) else { + continue; + }; + let relative = path + .strip_prefix(root.as_std_path()) + .expect("under the root") + .to_string_lossy() + .replace('\\', "/"); + out.push((relative, content)); + } + out +} + +fn is_entry_file(path: &str) -> bool { + path.contains("/entries/") +} + +fn is_lexicon_file(path: &str) -> bool { + is_entry_file(path) || path.ends_with("/lexicon.toml") +} + +/// The committed corpora: every form surface with its channel, every Lean +/// name of a denotation or an eliminator, every entry ID, and every TOML text. +struct Committed { + surfaces: Vec<(String, u64)>, + names: Vec, + ids: Vec, + lexicon_texts: Vec, + other_texts: Vec, +} + +fn committed() -> Committed { + let mut surfaces = Vec::new(); + let mut names = Vec::new(); + let mut ids = Vec::new(); + let mut lexicon_texts = Vec::new(); + let mut other_texts = Vec::new(); + for (path, content) in committed_files() { + if is_lexicon_file(&path) { + lexicon_texts.push(content.clone()); + } else { + other_texts.push(content.clone()); + } + if !is_entry_file(&path) { + continue; + } + let Ok(value) = content.parse::() else { + continue; + }; + if let Some(local) = value.get("id").and_then(toml::Value::as_str) { + ids.push(local.to_owned()); + } + if let Some(denotation) = value.get("denotation").and_then(toml::Value::as_table) { + for key in ["module", "name"] { + if let Some(name) = denotation.get(key).and_then(toml::Value::as_str) { + names.push(name.to_owned()); + } + } + } + if let Some(eliminator) = value.get("eliminator").and_then(toml::Value::as_table) { + for key in ["cases_lean_name", "induction_lean_name"] { + if let Some(name) = eliminator.get(key).and_then(toml::Value::as_str) { + names.push(name.to_owned()); + } + } + for constructor in eliminator + .get("constructor") + .and_then(toml::Value::as_array) + .into_iter() + .flatten() + { + if let Some(name) = constructor.get("lean_name").and_then(toml::Value::as_str) { + names.push(name.to_owned()); + } + } + } + for form in value + .get("form") + .and_then(toml::Value::as_array) + .into_iter() + .flatten() + { + let (Some(surface), Some(channel)) = ( + form.get("surface").and_then(toml::Value::as_str), + form.get("channel").and_then(toml::Value::as_str), + ) else { + continue; + }; + if let Some(found) = CHANNELS.iter().position(|row| *row == channel) { + surfaces.push((surface.to_owned(), u64::try_from(found).expect("an index"))); + } + } + } + Committed { + surfaces, + names, + ids, + lexicon_texts, + other_texts, + } +} + +// ------------------------------------------------------------------- seeded + +const SURFACE_PIECES: &[&str] = &[ + "a", "nat", "x", "the", "of", "1", "20", " ", " ", ":", "-", ",", ".", ";", "'", "+", "=", + "<", "*", "/", "|", "!", "?", "@", "%", "$", "#", "&", "~", "^", "_", "\"", "`", "(", ")", + "[", "]", "{", "}", "\\N", "\\alpha", "\u{2200}", "\u{3b1}", "\u{3a9}", "\u{2115}", + "\u{2192}", "\u{2264}", "\u{2282}", "\u{2308}", "\u{27e8}", "\u{2983}", "\u{2a01}", + "\u{1d538}", "\u{e9}", "\u{20ac}", "\u{2026}", "\u{2032}", "\u{ac}", "\u{d7}", +]; +const NAME_PIECES: &[&str] = &[ + "Nat", "add", "_", "1", ".", "x9", "-", "\u{3b1}", "'", "Zero", "", " ", +]; +const TOML_PIECES: &[&str] = &[ + "\"", "'", "\"\"\"", "'''", "#", "\\", "\n", "a = ", "x", " ", "\u{e9}", "[t]", +]; +const ID_PIECES: &[&str] = &["a", "b1", ".", "-", "nat", "add", "", "zero"]; +const TOKEN_PIECES: &[&str] = &[ + "infix", "function", "-", "noun", "of", "both", "text", "article", "a", "n", "left", + "plural", "case", "Atom", " ", +]; + +fn joined(seeded: &mut Seeded, pieces: &[&str], most: usize) -> String { + (0..seeded.below(most + 1)) + .map(|_| *seeded.pick(pieces)) + .collect() +} + +/// `count` seeded runs of each root that reads text: surfaces in a channel, +/// Lean names, TOML texts, entry IDs, and tokens. +fn seeded_runs(count: usize) -> [Vec; 6] { + let mut seeded = Seeded(0x0B55_0050); + let mut runs: [Vec; 6] = Default::default(); + while runs[2].len() < count { + let surface = joined(&mut seeded, SURFACE_PIECES, 6); + if let Some(atoms) = atoms_of(&surface) { + let channel = u64::try_from(seeded.below(3)).expect("a channel"); + runs[2].push(safety_run(&atoms, channel)); + } + } + for _ in 0..count { + let which = u64::try_from(seeded.below(5)).expect("a table"); + runs[0].push(token_run(which, &joined(&mut seeded, TOKEN_PIECES, 3))); + runs[3].push(lean_run(&joined(&mut seeded, NAME_PIECES, 6))); + runs[4].push(comment_run(&joined(&mut seeded, TOML_PIECES, 10))); + runs[5].push(path_run(&joined(&mut seeded, ID_PIECES, 5))); + } + runs +} + +// ------------------------------------------------------------------- plants + +struct Plant { + label: &'static str, + root: usize, + site: &'static str, + replacement: &'static str, + vector: &'static str, +} + +const PLANTS: &[Plant] = &[ + Plant { + label: "a category is read as a frame", + root: 0, + site: "(if (which =? 0) then (categories) else", + replacement: "(if (which =? 0) then (frames) else", + vector: "the token `structural` of table 0", + }, + Plant { + label: "the last feature is lost", + root: 0, + site: "(cons (lit \"article-a\") (cons (lit \"article-an\") (nil Bytes)))", + replacement: "(cons (lit \"article-a\") (nil Bytes))", + vector: "the token `article-an` of table 4", + }, + Plant { + label: "the index counts from one", + root: 0, + site: "(0, (ctor Option.none [Nat])) table : (Nat * (Option Nat))", + replacement: "(1, (ctor Option.none [Nat])) table : (Nat * (Option Nat))", + vector: "the token `left` of table 3", + }, + Plant { + label: "a proof constant of arity one is an atom", + root: 1, + site: "(if (arity =? 0) then 0 else 1)", + replacement: "(if (arity =? 0) then 0 else 0)", + vector: "the shape of proof-constant as call at arity 1", + }, + Plant { + label: "a prefix frame fixes two", + root: 1, + site: "(if (frame =? 4) then true else (if (frame =? 6) then true else (frame =? 9)))", + replacement: "(if (frame =? 4) then true else (if (frame =? 6) then true else (if (frame =? 9) then true else (frame =? 2))))", + vector: "the shape of prefix-function as prefix at arity 1", + }, + Plant { + label: "a label word has a signature", + root: 1, + site: "hasSignature := (2 (0 true", + vector: "a math surface led by a numeral", + }, + Plant { + label: "the mathematical alphanumerics end early", + root: 2, + site: "(inRange code 119808 120831)", + replacement: "(inRange code 119808 119999)", + vector: "a math surface of a double-struck letter", + }, + Plant { + label: "a two-byte scalar keeps five bits too few", + root: 2, + site: "(prim multiply (prim remainder lead 32 0 : Nat) 64 : Nat)", + replacement: "(prim multiply (prim remainder lead 32 0 : Nat) 32 : Nat)", + vector: "a math surface with alpha", + }, + Plant { + label: "a delimiter is named an ASCII symbol", + root: 2, + site: "| PrimitiveScan::AtomClass.Delimiter => (lit \"delimiter\")", + replacement: "| PrimitiveScan::AtomClass.Delimiter => (lit \"ascii-symbol\")", + vector: "a text surface with a parenthesis", + }, + Plant { + label: "both channels are the math channel", + root: 2, + site: "(if (textOk atom) then (mathOk atom place) else false)", + replacement: "(mathOk atom place)", + vector: "a both surface with a plus", + }, + Plant { + label: "the last unsafe atom is reported", + root: 2, + site: "| Option.some reason => state", + replacement: "| Option.some reason => (if (atomSafe atom (first state) channel) then (((first state) + 1), (second state)) else (((first state) + 1), (ctor Option.some [Bytes] (unsafeMessage atom channel))))", + vector: "two unsafe atoms", + }, + Plant { + label: "a segment may start with a digit", + root: 3, + site: "(if (isNameStart value) then", + replacement: "(if (isNameRest value) then", + vector: "a name whose segment starts with a digit", + }, + Plant { + label: "an empty segment is a segment", + root: 3, + site: "(if (value =? 46) then (if (segAt st) then (ctor Option.none [(Nat * Bool)])", + replacement: "(if (value =? 46) then (if false then (ctor Option.none [(Nat * Bool)])", + vector: "a name with two dots", + }, + Plant { + label: "a trailing dot ends a name", + root: 3, + site: "(if (segAt final) then false else true)", + replacement: "true", + vector: "a name with a trailing dot", + }, + Plant { + label: "an escape does not skip the next byte", + root: 4, + site: "(if (value =? 92) then (cmMove st 2 1)", + replacement: "(if (value =? 92) then (cmMove st 1 1)", + vector: "an escaped quote before a hash", + }, + Plant { + label: "a literal string ends at a quote only", + root: 4, + site: "(if (if (value =? 39) then true else (value =? 10)) then (cmMove st 1 0) else (cmMove st 1 2))", + replacement: "(if (value =? 39) then (cmMove st 1 0) else (cmMove st 1 2))", + vector: "a literal string broken by a line", + }, + Plant { + label: "one quote ends a multi-line string", + root: 4, + site: "(if (LexLeanStd.Text::matchAt text 0x222222 position) then (cmMove st 3 0) else (if (value =? 92) then (cmMove st 2 3)", + replacement: "(if (value =? 34) then (cmMove st 1 0) else (if (value =? 92) then (cmMove st 2 3)", + vector: "a quote inside a multi-line string", + }, + Plant { + label: "the comment is found one byte late", + root: 4, + site: "found := (ctor Option.some [Nat] position)", + replacement: "found := (ctor Option.some [Nat] (position + 1))", + vector: "a comment line", + }, + Plant { + label: "dots stay dots", + root: 5, + site: "(LexLeanStd.Text::splitOn localId (46 : UInt8))", + replacement: "(LexLeanStd.Text::splitOn localId (47 : UInt8))", + vector: "a nested entry", + }, +]; + +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/LexiconSchema.lex.tex"); + assert_eq!( + source.matches(plant.site).count(), + 1, + "{}: the plant site occurs once", + plant.label + ); + planted.write( + "src/LexiconSchema.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-50: 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-50: {}: 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" + ); + } +} + +#[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("LexiconSchema") + && checked_units.units.contains_key("PrimitiveScan") + && checked_units.units.contains_key("LexiconLse"), + "LexiconSchema, PrimitiveScan and LexiconLse are linked modules of the project" + ); + eligibility_of(&project, "LexiconSchema", MODULE, &ROOTS); + for token in CATEGORIES { + assert_eq!( + Category::parse(token).map(Category::as_str), + Some(token), + "the category table is the oracle's" + ); + } + for token in FRAMES { + assert_eq!( + Frame::parse(token).map(Frame::as_str), + Some(token), + "the frame table is the oracle's" + ); + } + + let vectors = named(); + let labels: BTreeSet<&str> = vectors.iter().map(|vector| vector.label.as_str()).collect(); + assert_eq!(labels.len(), vectors.len(), "each vector has its own label"); + let mut by_root: [Vec; 6] = Default::default(); + for vector in &vectors { + by_root[vector.root].push(vector.run.clone()); + } + let safe = serde_json::to_value(Value::None).expect("a value is JSON"); + let unsafe_named = vectors + .iter() + .filter(|vector| vector.root == 2 && vector.run.1 != safe) + .count(); + assert!(unsafe_named >= 15, "the named surfaces reach refusals"); + + // The committed corpora. + let corpus = committed(); + assert!( + corpus.surfaces.len() >= 300, + "the committed entries carry {} forms", + corpus.surfaces.len() + ); + let mut committed_runs: [Vec; 6] = Default::default(); + for (surface, channel) in &corpus.surfaces { + let atoms = scanned(surface); + committed_runs[2].push(safety_run(&atoms, *channel)); + for other in (0..3).filter(|other| other != channel) { + committed_runs[2].push(safety_run(&atoms, other)); + } + } + for name in &corpus.names { + committed_runs[3].push(lean_run(name)); + } + for local in &corpus.ids { + committed_runs[5].push(path_run(local)); + } + for source in &corpus.lexicon_texts { + committed_runs[4].push(comment_run(source)); + } + let other_comment_runs: Vec = corpus.other_texts.iter().map(|source| comment_run(source)).collect(); + let normative = support::normative_host(); + let (evaluator_seeded, rustc_seeded) = if normative { + (seeded_runs(EVALUATOR_SEEDED), seeded_runs(RUSTC_SEEDED)) + } else { + (Default::default(), Default::default()) + }; + eprintln!( + "BS-50: {} vectors, {} committed forms, {} Lean names, {} entry IDs, {} lexicon files and {} other TOML files ({:?})", + vectors.len(), + corpus.surfaces.len(), + corpus.names.len(), + corpus.ids.len(), + corpus.lexicon_texts.len(), + other_comment_runs.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(); + if normative { + for (index, (root, _)) in ROOTS.iter().enumerate() { + let mut corpus_runs: Vec = by_root[index].clone(); + corpus_runs.extend(committed_runs[index].iter().cloned()); + corpus_runs.extend(evaluator_seeded[index].iter().cloned()); + let disagreements = super::evaluate(&lowered[root], &corpus_runs, 5); + assert!( + disagreements.is_empty(), + "{root} and the Rust oracle disagree: {disagreements:?}" + ); + eprintln!( + "BS-50: {root}: the evaluator agrees on {} runs ({:?})", + corpus_runs.len(), + started.elapsed() + ); + } + plants(&vectors); + } else { + eprintln!("BS-50: platform-bound host: the evaluator, the seeded runs, and the planted defects run on the normative host"); + } + + let mut compiled: [Vec; 6] = Default::default(); + for (index, runs) in compiled.iter_mut().enumerate() { + runs.append(&mut by_root[index]); + runs.append(&mut committed_runs[index]); + runs.extend(rustc_seeded[index].iter().cloned()); + } + compiled[4].extend(other_comment_runs); + 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-50", &suites, "oracle"); + eprintln!("BS-50: {total} rustc runs agree ({:?})", started.elapsed()); + + if !support::lean_backed("BS-50") { + return; + } + export(&lowered); + eprintln!("BS-50: passed in {:?}", started.elapsed()); +} diff --git a/crates/conformance/src/cases/bootstrap/lse_scopes.rs b/crates/conformance/src/cases/bootstrap/lse_scopes.rs new file mode 100644 index 0000000000..6f5cf2dae6 --- /dev/null +++ b/crates/conformance/src/cases/bootstrap/lse_scopes.rs @@ -0,0 +1,670 @@ +//! `BS-51` (SPEC.md §17.23, §13.8): the scopes, the references, and the +//! outer shape of a LexLean Semantic Expression as language-1.2 source, +//! `LexLeanBoot.LseScopes`, held to the byte to `Lse::check_scopes`, +//! `Lse::referenced_consts`, `Lse::outer_explicit_binders`, and `Lse::result`. +//! +//! The roots read the expressions of `BS-25` and walk them with an explicit +//! work stack, so that none admits `recursion`; the host passes the length of +//! the expression's text, which bounds the walk. Each planted defect names the +//! one vector that catches it. + +use std::collections::{BTreeMap, BTreeSet}; + +use lexlean::calculus::Value; +use lexlean::lexicon::lse::{parse, Lse}; +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, pair, text, Run, Seeded}; +use crate::production_export as harness; +use crate::support::{self, repo_root}; + +const MODULE: &str = "LexLeanBoot.LseScopes"; +const ROOTS: [(&str, &[&str]); 3] = [ + ("checkScopes", &["allocation", "overflow"]), + ("referencedConsts", &["allocation", "overflow"]), + ("outerShape", &["allocation", "overflow"]), +]; + +/// 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 committed expressions and the seeded ones are read at. +const DEPTH: u64 = 64; +/// Seeded expressions under the evaluator, and under rustc. +const EVALUATOR_SEEDED: usize = 300; +const RUSTC_SEEDED: usize = 2_000; +const UNIVERSE_NAMES: &[&str] = &["u", "v", "w1"]; + +fn nat(value: u64) -> Value { + Value::Nat { + value: value.to_string(), + } +} + +/// The bound the host passes: the length of the expression's text. +fn bound(expression: &Lse) -> Value { + nat(u64::try_from(expression.print(false).len()).expect("a length")) +} + +fn scopes_run(expression: &Lse, universes: &[&str]) -> Run { + let declared: BTreeSet = universes.iter().map(|name| (*name).to_owned()).collect(); + let outcome = expression.check_scopes(&declared).err(); + ( + vec![ + enc_lse(expression), + list(universes.iter().map(|name| text(name)).collect()), + bound(expression), + ], + serde_json::to_value(option(outcome.as_deref().map(text))).expect("a value is JSON"), + ) +} + +fn consts_run(expression: &Lse) -> Run { + let found = expression + .referenced_consts() + .iter() + .map(|id| pair(text(&id.package), text(&id.entry))) + .collect(); + ( + vec![enc_lse(expression), bound(expression)], + serde_json::to_value(list(found)).expect("a value is JSON"), + ) +} + +fn shape_run(expression: &Lse) -> Run { + let explicit = u64::try_from(expression.outer_explicit_binders()).expect("a count"); + let sort = matches!(expression.result(), Lse::SortProp | Lse::SortType(_)); + ( + vec![enc_lse(expression), bound(expression)], + serde_json::to_value(pair(nat(explicit), Value::Bool { value: sort })) + .expect("a value is JSON"), + ) +} + +fn parsed(source: &str) -> Lse { + parse(source, DEPTH).unwrap_or_else(|error| panic!("`{source}` parses: {error}")) +} + +// ------------------------------------------------------------------ vectors + +struct Vector { + label: &'static str, + root: usize, + run: Run, +} + +const P: &str = "(sort prop)"; + +#[allow(clippy::too_many_lines)] +fn named() -> Vec { + let mut out = Vec::new(); + let scoped: &[(&'static str, &str, &[&str])] = &[ + ( + "a closed signature", + "(pi ((explicit a (const p::nat))) (const p::nat))", + &[], + ), + ("a free local", "(local x)", &[]), + ( + "a local bound by a pi", + "(pi ((explicit x (sort prop))) (local x))", + &[], + ), + ( + "a local used after its pi", + "(app (const f::g) (pi ((explicit x (sort prop))) (local x)) (local x))", + &[], + ), + ( + "a local used after its lam", + "(app (lam ((explicit y (sort prop))) (local y)) (local y))", + &[], + ), + ( + "a binder sees the binders before it", + "(pi ((explicit a (sort prop)) (implicit b (local a))) (local b))", + &[], + ), + ( + "a binder's type does not see itself", + "(pi ((explicit a (local a))) (sort prop))", + &[], + ), + ( + "a binder's type does not see the binders after it", + "(pi ((explicit a (local b)) (explicit b (sort prop))) (sort prop))", + &[], + ), + ( + "a let value does not see its name", + "(let x (sort prop) (local x) (local x))", + &[], + ), + ( + "a let type does not see its name", + "(let x (local x) (sort prop) (local x))", + &[], + ), + ( + "a let body sees its name", + "(let x (sort prop) (sort prop) (local x))", + &[], + ), + ( + "a let's name ends with it", + "(app (const f::g) (let x (sort prop) (sort prop) (local x)) (local x))", + &[], + ), + ( + "an inner binder shadows an outer one", + "(pi ((explicit x (sort prop))) (lam ((explicit x (local x))) (local x)))", + &[], + ), + ("a declared universe variable", "(sort (type u))", &["u"]), + ("an undeclared universe variable", "(sort (type u))", &[]), + ("a universe of a numeral", "(sort (type 3))", &[]), + ( + "an undeclared variable inside a max", + "(sort (type (max u v w1)))", + &["u"], + ), + ( + "two undeclared variables of an imax", + "(sort (type (imax v w1)))", + &[], + ), + ( + "an undeclared variable under a succ", + "(sort (type (succ (succ w1))))", + &["u", "v"], + ), + ( + "an undeclared universe of a constant", + "(const a::b (u v))", + &["u"], + ), + ( + "the declared universes of a constant", + "(const a::b (u (max v 0)))", + &["u", "v"], + ), + ( + "a universe error after a local error", + "(app (local x) (sort (type u)))", + &[], + ), + ( + "a local error after a universe error", + "(app (const a::b (w1)) (local x))", + &[], + ), + ( + "a universe error in a binder type", + "(pi ((explicit a (sort (type v)))) (local z))", + &["u"], + ), + ("a natural literal", "(nat 42)", &[]), + ("a proposition", P, &[]), + ]; + for &(label, source, universes) in scoped { + out.push(Vector { + label, + root: 0, + run: scopes_run(&parsed(source), universes), + }); + } + for (label, source) in [ + ("no constant", "(sort prop)"), + ("one constant", "(const lexlean.std.nat::nat)"), + ( + "the function's constant before its arguments'", + "(app (const f::g) (const a::b) (const c::d))", + ), + ( + "a nested application", + "(app (app (const f::g) (const a::b)) (const c::d))", + ), + ( + "a constant in a binder type", + "(pi ((explicit x (const a::b)) (implicit y (const c::d))) (const e::f))", + ), + ( + "the constants of a lam", + "(lam ((explicit x (const a::b))) (app (const f::g) (local x)))", + ), + ( + "the constants of a let", + "(let x (const a::b) (const c::d) (const e::f))", + ), + ("a constant repeated", "(app (const a::b) (const a::b))"), + ("a constant with universes", "(const a::b (u 1))"), + ] { + out.push(Vector { + label, + root: 1, + run: consts_run(&parsed(source)), + }); + } + for (label, source) in [ + ("a pi of mixed binders", "(pi ((implicit a (sort prop)) (explicit b (local a)) (instance c (sort prop)) (explicit d (sort prop))) (local b))"), + ("a pi into a sort", "(pi ((explicit a (sort prop))) (sort (type 0)))"), + ("a pi into a proposition", "(pi ((explicit a (sort prop))) (sort prop))"), + ("a pi of implicit binders only", "(pi ((implicit a (sort prop))) (local a))"), + ("a pi into a pi", "(pi ((explicit a (sort prop))) (pi ((explicit b (sort prop))) (sort prop)))"), + ("a lam is no pi", "(lam ((explicit a (sort prop))) (sort prop))"), + ("a sort", "(sort (type 1))"), + ("a constant", "(const a::b)"), + ] { + out.push(Vector { + label, + root: 2, + run: shape_run(&parsed(source)), + }); + } + out +} + +// --------------------------------------------------------------- committed + +/// Every signature and defined value of the committed lexicon entries, with +/// the universes its entry declares. +fn committed() -> Vec<(String, Lse, Vec)> { + 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") + || support::holds_cache_tag(entry))) + }); + let mut out = Vec::new(); + for entry in walker.flatten() { + let path = entry.path(); + let shown = path.to_string_lossy().replace('\\', "/"); + if !entry.file_type().is_file() || !shown.ends_with(".toml") || !shown.contains("/entries/") + { + continue; + } + let Ok(content) = std::fs::read_to_string(path) else { + continue; + }; + let Ok(table) = content.parse::() else { + continue; + }; + let universes: Vec = table + .get("universes") + .and_then(toml::Value::as_array) + .into_iter() + .flatten() + .filter_map(|name| name.as_str().map(str::to_owned)) + .collect(); + let mut texts: Vec<&str> = Vec::new(); + if let Some(signature) = table.get("signature").and_then(toml::Value::as_str) { + texts.push(signature); + } + if let Some(value) = table + .get("denotation") + .and_then(|denotation| denotation.get("value")) + .and_then(toml::Value::as_str) + { + texts.push(value); + } + for source in texts { + if let Ok(expression) = parse(source, DEPTH) { + out.push((shown.clone(), expression, universes.clone())); + } + } + } + out +} + +/// `count` seeded expressions: each checked under a seeded set of declared +/// universes, its references, and its outer shape. +fn seeded_runs(count: usize) -> [Vec; 3] { + let mut seeded = Seeded(0x0B55_0051); + let mut runs: [Vec; 3] = Default::default(); + for _ in 0..count { + let depth = 1 + seeded.below(4); + let expression = drawn_lse(&mut seeded, depth); + let universes: Vec<&str> = UNIVERSE_NAMES + .iter() + .copied() + .filter(|_| seeded.chance(60)) + .collect(); + runs[0].push(scopes_run(&expression, &universes)); + runs[1].push(consts_run(&expression)); + runs[2].push(shape_run(&expression)); + } + runs +} + +// ------------------------------------------------------------------- plants + +struct Plant { + label: &'static str, + root: usize, + site: &'static str, + replacement: &'static str, + vector: &'static str, +} + +const PLANTS: &[Plant] = &[ + Plant { + label: "a local is in scope everywhere", + root: 0, + site: "| LexiconLse::Lse.LLocal name => (if (member (project st scope) name) then", + replacement: "| LexiconLse::Lse.LLocal name => (if true then", + vector: "a free local", + }, + Plant { + label: "a binder's type sees itself", + root: 0, + site: "(cons (ctor SWork.SExpr ty) (cons (ctor SWork.SBind name) (cons (ctor SWork.SExpr more) rest))", + replacement: "(cons (ctor SWork.SBind name) (cons (ctor SWork.SExpr ty) (cons (ctor SWork.SExpr more) rest))", + vector: "a binder's type does not see itself", + }, + Plant { + label: "a let value sees its name", + root: 0, + site: "(cons (ctor SWork.SExpr value) (cons (ctor SWork.SBind name)", + replacement: "(cons (ctor SWork.SBind name) (cons (ctor SWork.SExpr value)", + vector: "a let value does not see its name", + }, + Plant { + label: "a binder's scope outlives its pi", + root: 0, + site: "scope := (dropFront (project st scope) (prim subtract (project st depth) wanted : Nat)), depth := wanted", + replacement: "scope := (project st scope), depth := (project st depth)", + vector: "a local used after its pi", + }, + Plant { + label: "an undeclared universe variable passes", + root: 0, + site: "(if (member universes name) then (scGo st rest) else", + replacement: "(if true then (scGo st rest) else", + vector: "an undeclared universe variable", + }, + Plant { + label: "the arguments of a max are skipped", + root: 0, + site: "| LexiconLse::Universe.UMax items => (scGo st (cons (ctor SWork.SUni items) rest))", + replacement: "| LexiconLse::Universe.UMax items => (scGo st rest)", + vector: "an undeclared variable inside a max", + }, + Plant { + label: "an imax checks its right first", + root: 0, + site: "(scGo st (cons (ctor SWork.SUni a) (cons (ctor SWork.SUni b) rest)))", + replacement: "(scGo st (cons (ctor SWork.SUni b) (cons (ctor SWork.SUni a) rest)))", + vector: "two undeclared variables of an imax", + }, + Plant { + label: "an application's arguments are checked first", + root: 0, + site: "| LexiconLse::Lse.LApp f args => (scGo st (cons (ctor SWork.SExpr f) (cons (ctor SWork.SExpr args) rest)))", + replacement: "| LexiconLse::Lse.LApp f args => (scGo st (cons (ctor SWork.SExpr args) (cons (ctor SWork.SExpr f) rest)))", + vector: "a universe error after a local error", + }, + Plant { + label: "the universes of a constant are skipped", + root: 0, + site: "| LexiconLse::Lse.LConst p e us => (scGo st (cons (ctor SWork.SUni us) rest))", + replacement: "| LexiconLse::Lse.LConst p e us => (scGo st rest)", + vector: "an undeclared universe of a constant", + }, + Plant { + label: "an application's arguments are referenced first", + root: 1, + site: "| LexiconLse::Lse.LApp f args => (rcGo st (cons f (cons args rest)))", + replacement: "| LexiconLse::Lse.LApp f args => (rcGo st (cons args (cons f rest)))", + vector: "the function's constant before its arguments'", + }, + Plant { + label: "a let's value is skipped", + root: 1, + site: "(rcGo st (cons ty (cons value (cons body rest))))", + replacement: "(rcGo st (cons ty (cons body rest)))", + vector: "the constants of a let", + }, + Plant { + label: "a binder's type is skipped", + root: 1, + site: "| LexiconLse::Lse.LBinder mode name ty more => (rcGo st (cons ty (cons more rest)))", + replacement: "| LexiconLse::Lse.LBinder mode name ty more => (rcGo st (cons more rest))", + vector: "a constant in a binder type", + }, + Plant { + label: "every binder is explicit", + root: 2, + site: "(if (mode =? 0) then ((second state) + 1) else (second state))", + replacement: "((second state) + 1)", + vector: "a pi of mixed binders", + }, + Plant { + label: "a pi's result is never a sort", + root: 2, + site: "(isSort body))", + replacement: "false)", + vector: "a pi into a sort", + }, +]; + +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/LseScopes.lex.tex"); + assert_eq!( + source.matches(plant.site).count(), + 1, + "{}: the plant site occurs once", + plant.label + ); + planted.write( + "src/LseScopes.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-51: 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-51: {}: 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 `check_scopes` can state, by a part of its message (R2). +const REFUSALS: &[&str] = &["is not in scope", "is not declared"]; + +#[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("LseScopes") + && checked_units.units.contains_key("LexiconLse"), + "LseScopes and LexiconLse are linked modules of the project" + ); + eligibility_of(&project, "LseScopes", 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; 3] = Default::default(); + for vector in &vectors { + by_root[vector.root].push(vector.run.clone()); + } + let messages: Vec = by_root[0].iter().map(|run| run.1.to_string()).collect(); + for refusal in REFUSALS { + assert!( + messages + .iter() + .any(|message| message.contains(&super::hex(refusal.as_bytes()))), + "no vector reaches the error `{refusal}`" + ); + } + + let corpus = committed(); + assert!( + corpus.len() >= 200, + "the committed entries carry {} signatures and values", + corpus.len() + ); + let mut committed_runs: [Vec; 3] = Default::default(); + for (path, expression, universes) in &corpus { + let declared: Vec<&str> = universes.iter().map(String::as_str).collect(); + let run = scopes_run(expression, &declared); + assert_eq!( + run.1, + serde_json::to_value(Value::None).expect("a value is JSON"), + "{path}: a committed expression is in scope" + ); + committed_runs[0].push(run); + committed_runs[1].push(consts_run(expression)); + committed_runs[2].push(shape_run(expression)); + } + let normative = support::normative_host(); + let (evaluator_seeded, rustc_seeded) = if normative { + (seeded_runs(EVALUATOR_SEEDED), seeded_runs(RUSTC_SEEDED)) + } else { + (Default::default(), Default::default()) + }; + eprintln!( + "BS-51: {} vectors, {} committed expressions, {} and {} seeded expressions under the evaluator and rustc ({:?})", + vectors.len(), + corpus.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(); + if normative { + for (index, (root, _)) in ROOTS.iter().enumerate() { + let mut runs: Vec = by_root[index].clone(); + runs.extend(committed_runs[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-51: {root}: the evaluator agrees on {} runs ({:?})", + runs.len(), + started.elapsed() + ); + } + plants(&vectors); + } else { + eprintln!("BS-51: platform-bound host: the evaluator, the seeded runs, and the planted defects run on the normative host"); + } + + let mut compiled: [Vec; 3] = Default::default(); + for (index, runs) in compiled.iter_mut().enumerate() { + runs.append(&mut by_root[index]); + runs.append(&mut committed_runs[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-51", &suites, "oracle"); + eprintln!("BS-51: {total} rustc runs agree ({:?})", started.elapsed()); + + if !support::lean_backed("BS-51") { + return; + } + export(&lowered); + eprintln!("BS-51: passed in {:?}", started.elapsed()); +} diff --git a/crates/conformance/src/cases/bootstrap/lse_types.rs b/crates/conformance/src/cases/bootstrap/lse_types.rs new file mode 100644 index 0000000000..249f7d2b82 --- /dev/null +++ b/crates/conformance/src/cases/bootstrap/lse_types.rs @@ -0,0 +1,833 @@ +//! `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/tests/conformance.rs b/crates/conformance/tests/conformance.rs index c83090918d..cf820f8fc0 100644 --- a/crates/conformance/tests/conformance.rs +++ b/crates/conformance/tests/conformance.rs @@ -2410,6 +2410,21 @@ fn conformance_bs_34() { repo_conformance::cases::run("BS-34"); } +#[test] +fn conformance_bs_50() { + repo_conformance::cases::run("BS-50"); +} + +#[test] +fn conformance_bs_51() { + repo_conformance::cases::run("BS-51"); +} + +#[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..e3106d291f 100644 --- a/features/suites/bootstrap.feature +++ b/features/suites/bootstrap.feature @@ -208,3 +208,24 @@ 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-50 @build + Scenario: The tables and predicates of the lexicon entry schema of SPEC.md §13.1 to §13.6 (`lexicon::entry`: the category, frame, channel, associativity, and feature tokens, each read as its position in the schema's order; for a category, a frame, and a surface arity, `frame_permitted`, the arity `Frame::fixed_arity` fixes, whether the category is core-only, carries a signature, or needs a canonical source form in some channel, and the channels it requires one in; `is_lean_name`, the conservative ASCII Lean-name grammar; and `surface_safety` over the atoms of the primitive scanner, with the allow-list of `is_math_safe_scalar`, nothing or the message naming the first atom that is not renderer-safe; and `lexicon::package`: `toml_comment_at`, the offset of the first comment outside every TOML string form, and `entry_path`) is language-1.2 source in the `bootstrap/` project, as six production roots of `LexiconSchema` over the atoms of `BS-03`: `tokenIndex`, `shapeFacts`, `surfaceSafety`, `isLeanName`, `tomlCommentAt`, and `entryPath`, 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 vectors, which reach every token of every table, every category with every frame at four arities, every class of atom in every channel, and the edges of the allow-list, on every form surface, Lean name, and entry ID of the committed lexicon entries and on every committed TOML file, 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; reading an entry file and the diagnostics of the entry validator are outside the claim. + Given the committed `bootstrap` project, hand-built tokens, shapes, surfaces, names, TOML texts, and entry IDs, the committed lexicon entries and TOML files, and seeded inputs + When the project is checked and built, and the lowered programs and the rendered packages of the six roots are run on those inputs + 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 its oracle in `lexicon::entry` or `lexicon::package` 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 + + @BS-51 @build + Scenario: The scopes, the references, and the outer shape of a LexLean Semantic Expression of SPEC.md §13.8 (`lexicon::lse::Lse::check_scopes`: every local resolves lexically, a binder's type seen before its name and a `let`'s type and value before its name, and every universe variable is declared, or the message of the first that is not, in the oracle's order of visit; `referenced_consts`: the qualified constants in occurrence order; and `outer_explicit_binders` with `result`: the explicit binders of the outermost `pi` and whether its result is a sort) is language-1.2 source in the `bootstrap/` project, as three production roots of `LseScopes` over the expressions of `BS-25`: `checkScopes`, `referencedConsts`, and `outerShape`, each eligible for `rust-std` with exactly its declared effects, `allocation` and `overflow`, none of them `recursion`, 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, which reach both errors, shadowing, the end of every binder's scope, and every universe form, on every signature and defined value of the committed lexicon entries under the universes its entry declares, and on seeded expressions under seeded declarations; 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; the conservative type checker of §13.7 is outside the claim. + Given the committed `bootstrap` project, hand-built expressions, the signatures and defined values of the committed lexicon entries, and seeded expressions + When the project is checked and built, and the lowered programs and the rendered packages of the three 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 `Lse::check_scopes`, `Lse::referenced_consts`, or `outer_explicit_binders` with `result` 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 + + @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..b25385b749 100644 --- a/model/ids.toml +++ b/model/ids.toml @@ -2894,6 +2894,24 @@ 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-50" +level = "build" +suite = "bootstrap" +statement = "The tables and predicates of the lexicon entry schema of SPEC.md §13.1 to §13.6 (`lexicon::entry`: the category, frame, channel, associativity, and feature tokens, each read as its position in the schema's order; for a category, a frame, and a surface arity, `frame_permitted`, the arity `Frame::fixed_arity` fixes, whether the category is core-only, carries a signature, or needs a canonical source form in some channel, and the channels it requires one in; `is_lean_name`, the conservative ASCII Lean-name grammar; and `surface_safety` over the atoms of the primitive scanner, with the allow-list of `is_math_safe_scalar`, nothing or the message naming the first atom that is not renderer-safe; and `lexicon::package`: `toml_comment_at`, the offset of the first comment outside every TOML string form, and `entry_path`) is language-1.2 source in the `bootstrap/` project, as six production roots of `LexiconSchema` over the atoms of `BS-03`: `tokenIndex`, `shapeFacts`, `surfaceSafety`, `isLeanName`, `tomlCommentAt`, and `entryPath`, 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 vectors, which reach every token of every table, every category with every frame at four arities, every class of atom in every channel, and the edges of the allow-list, on every form surface, Lean name, and entry ID of the committed lexicon entries and on every committed TOML file, 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; reading an entry file and the diagnostics of the entry validator are outside the claim." + +[[id]] +id = "BS-51" +level = "build" +suite = "bootstrap" +statement = "The scopes, the references, and the outer shape of a LexLean Semantic Expression of SPEC.md §13.8 (`lexicon::lse::Lse::check_scopes`: every local resolves lexically, a binder's type seen before its name and a `let`'s type and value before its name, and every universe variable is declared, or the message of the first that is not, in the oracle's order of visit; `referenced_consts`: the qualified constants in occurrence order; and `outer_explicit_binders` with `result`: the explicit binders of the outermost `pi` and whether its result is a sort) is language-1.2 source in the `bootstrap/` project, as three production roots of `LseScopes` over the expressions of `BS-25`: `checkScopes`, `referencedConsts`, and `outerShape`, each eligible for `rust-std` with exactly its declared effects, `allocation` and `overflow`, none of them `recursion`, 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, which reach both errors, shadowing, the end of every binder's scope, and every universe form, on every signature and defined value of the committed lexicon entries under the universes its entry declares, and on seeded expressions under seeded declarations; 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; the conservative type checker of §13.7 is 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..a53b31b800 100644 --- a/tests/production-export/bootstrap/roots.json +++ b/tests/production-export/bootstrap/roots.json @@ -495,6 +495,94 @@ "rust-std" ], "version": "1.0.0" + }, + { + "name": "entry_path", + "root": "LexLeanBoot.LexiconSchema.entryPath", + "targets": [ + "rust-std" + ], + "version": "1.0.0" + }, + { + "name": "is_lean_name", + "root": "LexLeanBoot.LexiconSchema.isLeanName", + "targets": [ + "rust-std" + ], + "version": "1.0.0" + }, + { + "name": "shape_facts", + "root": "LexLeanBoot.LexiconSchema.shapeFacts", + "targets": [ + "rust-std" + ], + "version": "1.0.0" + }, + { + "name": "surface_safety", + "root": "LexLeanBoot.LexiconSchema.surfaceSafety", + "targets": [ + "rust-std" + ], + "version": "1.0.0" + }, + { + "name": "token_index", + "root": "LexLeanBoot.LexiconSchema.tokenIndex", + "targets": [ + "rust-std" + ], + "version": "1.0.0" + }, + { + "name": "toml_comment_at", + "root": "LexLeanBoot.LexiconSchema.tomlCommentAt", + "targets": [ + "rust-std" + ], + "version": "1.0.0" + }, + { + "name": "check_scopes", + "root": "LexLeanBoot.LseScopes.checkScopes", + "targets": [ + "rust-std" + ], + "version": "1.0.0" + }, + { + "name": "outer_shape", + "root": "LexLeanBoot.LseScopes.outerShape", + "targets": [ + "rust-std" + ], + "version": "1.0.0" + }, + { + "name": "referenced_consts", + "root": "LexLeanBoot.LseScopes.referencedConsts", + "targets": [ + "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" } ] } From ebe89111620188a5cf51933d9ff57c6aa4d1e837 Mon Sep 17 00:00:00 2001 From: humuhumu33 <68450279+humuhumu33@users.noreply.github.com> Date: Fri, 13 Nov 2026 22:53:32 +0000 Subject: [PATCH 15/22] The three hand-maintained register-count mirrors move to 499 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SPEC.md's "Total required capability IDs", xtask/src/spec_links.rs's RP-07 check, and repository.rs's RP-07 assertion still said 496: none of them is written by a recipe (cargo xtask counts --write carries the counts a bootstrap case reports, not the §31 row count), so the finish job would not have fixed them and RP-07 would fail after the whole regeneration. All three now read 499, matching model/ids.toml's 499 rows and SPEC §31's 499 rows. Co-Authored-By: Claude Sonnet 5 --- SPEC.md | 2 +- crates/conformance/src/cases/repository.rs | 2 +- xtask/src/spec_links.rs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/SPEC.md b/SPEC.md index 9afb2f70b9..e91cae14d4 100644 --- a/SPEC.md +++ b/SPEC.md @@ -12224,7 +12224,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:** 499. No row may be downgraded to `some-true` or `open`. Upstream Lean facts are ledger/authority rows, not substitutions for these build behaviors. diff --git a/crates/conformance/src/cases/repository.rs b/crates/conformance/src/cases/repository.rs index 07acbe46eb..10fd463379 100644 --- a/crates/conformance/src/cases/repository.rs +++ b/crates/conformance/src/cases/repository.rs @@ -1091,7 +1091,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(), 499, "§31 has 499 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/xtask/src/spec_links.rs b/xtask/src/spec_links.rs index f97eb8c07c..01b9b3a3e5 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() != 499 { + return Err(format!("RP-07: the §31 table has {} rows, not 499", 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 cd571f129a254e6ffcd23270c1bbfdc95bac19b3 Mon Sep 17 00:00:00 2001 From: humuhumu33 <68450279+humuhumu33@users.noreply.github.com> Date: Fri, 13 Nov 2026 22:59:29 +0000 Subject: [PATCH 16/22] Batch-land BS-35..43 onto release/v0.4 (issue #269) Carries all eight completed grammar/surface slices (GrammarProof, SemanticPrint, SemanticRead, CoreJson, CorePrint, CoreRead, GrammarMath, PropositionTerms) as one batch, as BS-25..34 landed in #480. Hand-authored register content (SPEC.md Sec31 rows + Sec17.23 prose, model/ids.toml entries, features/suites/bootstrap.feature scenarios, VERIFICATION.md, bootstrap/README.md, bootstrap.rs dispatch wiring, bootstrap/lexlean.toml entrypoints, the two xtask/repository.rs register-count constants, and .github/bdd-parts.json, vv-rows.json, intel-host-lanes.json CI-plan entries) is unioned in register-ID order onto release's current content. Regeneration-written files (CONFORMANCE.md, ERRORS.md, roots.json, lexlean.lock) and README.md/CHANGELOG.md are left untouched -- release's side -- for the one regeneration run to fill in. Co-Authored-By: Claude Sonnet 5 --- .github/bdd-parts.json | 24 + .github/intel-host-lanes.json | 6 +- .github/vv-rows.json | 72 + .github/workflows/regenerate.yml | 12 + SPEC.md | 178 +- VERIFICATION.md | 320 ++++ bootstrap/README.md | 22 + bootstrap/lexlean.toml | 2 +- bootstrap/src/CoreJson.lex.tex | 246 +++ bootstrap/src/CorePrint.lex.tex | 923 ++++++++++ bootstrap/src/CoreRead.lex.tex | 1561 +++++++++++++++++ bootstrap/src/GrammarMath.lex.tex | 751 ++++++++ bootstrap/src/GrammarProof.lex.tex | 445 +++++ bootstrap/src/PropositionTerms.lex.tex | 755 ++++++++ bootstrap/src/SemanticPrint.lex.tex | 1300 ++++++++++++++ bootstrap/src/SemanticRead.lex.tex | 1395 +++++++++++++++ crates/conformance/src/cases/bootstrap.rs | 16 + .../src/cases/bootstrap/core_json.rs | 782 +++++++++ .../src/cases/bootstrap/core_print.rs | 828 +++++++++ .../src/cases/bootstrap/core_read.rs | 921 ++++++++++ .../src/cases/bootstrap/grammar_math.rs | 1197 +++++++++++++ .../src/cases/bootstrap/grammar_proof.rs | 1025 +++++++++++ .../src/cases/bootstrap/proposition_terms.rs | 1208 +++++++++++++ .../src/cases/bootstrap/semantic_print.rs | 1155 ++++++++++++ .../src/cases/bootstrap/semantic_read.rs | 1159 ++++++++++++ crates/conformance/src/cases/repository.rs | 2 +- crates/conformance/tests/conformance.rs | 40 + crates/lexlean/src/surface/core.rs | 88 +- features/suites/bootstrap.feature | 48 + model/ids.toml | 48 + xtask/src/spec_links.rs | 4 +- 31 files changed, 16504 insertions(+), 29 deletions(-) mode change 100755 => 100644 .github/workflows/regenerate.yml create mode 100644 bootstrap/src/CoreJson.lex.tex create mode 100644 bootstrap/src/CorePrint.lex.tex create mode 100644 bootstrap/src/CoreRead.lex.tex create mode 100644 bootstrap/src/GrammarMath.lex.tex create mode 100644 bootstrap/src/GrammarProof.lex.tex create mode 100644 bootstrap/src/PropositionTerms.lex.tex create mode 100644 bootstrap/src/SemanticPrint.lex.tex create mode 100644 bootstrap/src/SemanticRead.lex.tex create mode 100644 crates/conformance/src/cases/bootstrap/core_json.rs create mode 100644 crates/conformance/src/cases/bootstrap/core_print.rs create mode 100644 crates/conformance/src/cases/bootstrap/core_read.rs create mode 100644 crates/conformance/src/cases/bootstrap/grammar_math.rs create mode 100644 crates/conformance/src/cases/bootstrap/grammar_proof.rs create mode 100644 crates/conformance/src/cases/bootstrap/proposition_terms.rs create mode 100644 crates/conformance/src/cases/bootstrap/semantic_print.rs create mode 100644 crates/conformance/src/cases/bootstrap/semantic_read.rs diff --git a/.github/bdd-parts.json b/.github/bdd-parts.json index f385106a69..fb7b035963 100644 --- a/.github/bdd-parts.json +++ b/.github/bdd-parts.json @@ -277,6 +277,30 @@ ], "bootstrap-manifest-text": [ "BS-34" + ], + "bootstrap-grammar-proof": [ + "BS-35" + ], + "bootstrap-semantic-print": [ + "BS-36" + ], + "bootstrap-semantic-read": [ + "BS-37" + ], + "bootstrap-core-json": [ + "BS-38" + ], + "bootstrap-core-print": [ + "BS-39" + ], + "bootstrap-core-read": [ + "BS-40" + ], + "bootstrap-grammar-math": [ + "BS-42" + ], + "bootstrap-proposition-terms": [ + "BS-43" ] }, "targets": { diff --git a/.github/intel-host-lanes.json b/.github/intel-host-lanes.json index 6a7f0aa94a..9559b909dd 100644 --- a/.github/intel-host-lanes.json +++ b/.github/intel-host-lanes.json @@ -1,9 +1,9 @@ { "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-semantic-print", "bootstrap-core-print", "bootstrap-proposition-terms"], "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"] + "validate": ["bootstrap-validate", "compiler", "preservation", "preservation-lists", "preservation-runtime", "preservation-support", "preservation-prims", "preservation-std", "production", "bootstrap-semantic-read", "bootstrap-core-read"], + "moves": ["bootstrap-moves", "verification", "rest", "bootstrap-grammar-proof", "bootstrap-core-json", "bootstrap-grammar-math"] } } diff --git a/.github/vv-rows.json b/.github/vv-rows.json index 7223ecaf22..c2476262f4 100644 --- a/.github/vv-rows.json +++ b/.github/vv-rows.json @@ -358,5 +358,77 @@ "width": "", "memory_kib": "", "hints": "" + }, + { + "part": "bdd-bootstrap-grammar-proof", + "recipes": "bdd", + "examples_part": "", + "bdd_part": "bootstrap-grammar-proof", + "width": "", + "memory_kib": "", + "hints": "" + }, + { + "part": "bdd-bootstrap-semantic-print", + "recipes": "bdd", + "examples_part": "", + "bdd_part": "bootstrap-semantic-print", + "width": "", + "memory_kib": "", + "hints": "" + }, + { + "part": "bdd-bootstrap-semantic-read", + "recipes": "bdd", + "examples_part": "", + "bdd_part": "bootstrap-semantic-read", + "width": "", + "memory_kib": "", + "hints": "" + }, + { + "part": "bdd-bootstrap-core-json", + "recipes": "bdd", + "examples_part": "", + "bdd_part": "bootstrap-core-json", + "width": "", + "memory_kib": "", + "hints": "" + }, + { + "part": "bdd-bootstrap-core-print", + "recipes": "bdd", + "examples_part": "", + "bdd_part": "bootstrap-core-print", + "width": "", + "memory_kib": "", + "hints": "" + }, + { + "part": "bdd-bootstrap-core-read", + "recipes": "bdd", + "examples_part": "", + "bdd_part": "bootstrap-core-read", + "width": "", + "memory_kib": "", + "hints": "" + }, + { + "part": "bdd-bootstrap-grammar-math", + "recipes": "bdd", + "examples_part": "", + "bdd_part": "bootstrap-grammar-math", + "width": "", + "memory_kib": "", + "hints": "" + }, + { + "part": "bdd-bootstrap-proposition-terms", + "recipes": "bdd", + "examples_part": "", + "bdd_part": "bootstrap-proposition-terms", + "width": "", + "memory_kib": "", + "hints": "" } ] diff --git a/.github/workflows/regenerate.yml b/.github/workflows/regenerate.yml old mode 100755 new mode 100644 index 2b1fb67b43..1e93c99731 --- a/.github/workflows/regenerate.yml +++ b/.github/workflows/regenerate.yml @@ -91,6 +91,13 @@ jobs: sudo mkswap "$RUNNER_TEMP/lexlean-regenerate.swap" sudo swapon "$RUNNER_TEMP/lexlean-regenerate.swap" sudo sysctl vm.swappiness=10 + - name: Refresh the project locks + shell: bash + run: | + set -euo pipefail + cargo build -q -p lexlean + L="$PWD/target/debug/lexlean" + for p in bootstrap stdlib compiler; do (cd "$p" && "$L" lock); done - name: Run the write recipes shell: bash env: @@ -114,6 +121,11 @@ jobs: set -euo pipefail mkdir -p "$RUNNER_TEMP/part" git add --all + # Every writer refreshes the locks, because its recipes read them; one + # writer reports them, or `combine` sees three parts write one path. + if [ "${{ matrix.writer }}" != production-export ]; then + git reset -q -- bootstrap/lexlean.lock stdlib/lexlean.lock compiler/lexlean.lock + fi git diff --cached --binary > "$RUNNER_TEMP/part/${{ matrix.writer }}.patch" git diff --cached --name-only | tee "$RUNNER_TEMP/part/${{ matrix.writer }}.files" echo "${{ matrix.writer }}: $(wc -l < "$RUNNER_TEMP/part/${{ matrix.writer }}.files") paths" >> "$GITHUB_STEP_SUMMARY" diff --git a/SPEC.md b/SPEC.md index a4a3468197..ef3dc677e8 100644 --- a/SPEC.md +++ b/SPEC.md @@ -9117,6 +9117,174 @@ module with a state machine over the tokens, reading each member value with the `generic::value` is called, so that the limit on nesting falls where the oracle's does; a 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 text channel and the simple proof sentences (§15.5, §16.2) are a component of the front end as +source (`BS-35`, issue 269), the module `GrammarProof` over the atoms of `PrimitiveScan` (`BS-03`), +with two production roots for `rust-std`: + +| Root | Takes | Returns | Oracle | +| --- | --- | --- | --- | +| `LexLeanBoot.GrammarProof.textTokens` | the path, the atoms, and an atom range | the tokens of the range, or the refusal's code, message, and span | `grammar::chart::text_tokens` | +| `LexLeanBoot.GrammarProof.parseProofSentence` | the path, the atoms, and the tokens of one sentence | the sentence with its keyword occurrences, or the refusal | `grammar::proof::parse_proof_sentence` | + +`textTokens` walks the range under `iterate_until`, one atom or one island a step: white space is +dropped, a dollar sign is refused, an opening control is folded with the first closing control of its +kind into one island token after a bounded scan of its own that stops at that close or at another +opening control, and a closing control met outside an island is refused. `parseProofSentence` +matches the seven registered sentences against the tokens by position, as the oracle's matcher +consumes them, and builds the keyword occurrences of the sentence it accepts. A sentence it refuses +is `LLF5005` at the first token that composes one of the 31 forbidden words of §16.12 with the +byte-adjacent word, numeral, and symbol atoms after it, or that begins the sentence and holds an +underscore, and otherwise at the sentence's first atom. Neither root admits `recursion`. The +oracle's tests for the `<;>` combinator and the focusing dot `·` are kept as written though no +scanned sentence reaches them: a semicolon ends a composed word, and `·` is a Unicode symbol. The +glossary forms, the propositions, and the elaboration of a sentence stay in the host. +The printer of the semantic notation (§17.21) is the sixth component of the front end as source +(`BS-36`, issue 269), the module `SemanticPrint` over `SurfaceDoc` (`BS-23`), `SurfaceGeneric` +(`BS-24`), and the standard library's `Json`, with two production roots for `rust-std`: + +| Root | Takes | Returns | Oracle | +| --- | --- | --- | --- | +| `LexLeanBoot.SemanticPrint.printAt` | the schema, a value, and a position (declaration, term, type, or proof) | its notation at 100 columns | `surface::semantic::print_at` | +| `LexLeanBoot.SemanticPrint.printSemanticModule` | the schema and a module | the notation of its declarations, or the error of a value that is no module | `surface::semantic::print_module` | + +The schema is the host's input, the value the oracle reads from the embedded tree, so that the +module walks it as the oracle does: a reference is followed through `$defs` to the node it names, +the references to `term`, `type`, `member`, and `parameter` are the positions with readable forms, +and the alternative of a union is the one whose `kind` is the value's, by `const` or `enum`. The +printer recurses structurally over the value (one function for a value, one for the items of an +array, and one for the members of an object), so it admits `recursion` and owes a row in +`tests/stack-bounds.json` (`PD-08`). Each object's printing is decided before its members are +printed: a shallow test, which reads no deeper than the members of its members, says whether a +readable form carries it exactly (the members it spells and nothing else, a name or literal the +form can write), and that decision gives each member its position, so each member is printed once. +A readable form's lists (the arguments, the type arguments, the parameters of a `fun`, the +parameter types of `Fn`, the fields of a `record`, and the branches of a `match`) are positions of +their own that join their items as the form writes them. Everything else is the generic notation of +`BS-24` with the members at the positions the schema gives them. The reader of the notation and the +decoding of a module stay in the host. +The reader of the semantic notation (§17.21) is the seventh component of the front end as source +(`BS-37`, issue 269), the module `SemanticRead` over `SurfaceLex` (`BS-22`), `SurfaceGeneric` +(`BS-24`), and the standard library's `Json`, with three production roots for `rust-std`: + +| Root | Takes | Returns | Oracle | +| --- | --- | --- | --- | +| `LexLeanBoot.SemanticRead.parseAt` | the schema, a text, and a position (declaration, term, type, or proof) | the one value the text is at that position, or the offset and message of the first error | `surface::semantic::parse_at` | +| `LexLeanBoot.SemanticRead.parseSemanticModule` | the schema, a text, and a schema tag | the module of the declarations the text is, or the first error | `surface::semantic::parse_module` | +| `LexLeanBoot.SemanticRead.semanticSpans` | the schema and a text | the byte range of each declaration, or the first error | `surface::semantic::declaration_spans` | + +The text is lexed with `lexText` (`BS-22`) first, so that a lexical error is reported before any +error of the reading, as the oracle's `Reader::new` reports it. The tokens are then read by a +machine with an explicit stack of frames under `iterate_until`, so that no root admits `recursion`: +each construct the oracle reads by a recursive call (an array and the members of an object at a +position, a parenthesised type or term, each readable form with the parts it reads one after the +other, a parameter, a list of type arguments) is a frame that waits for the value it asked for, +and a goal names what is read next (a value at a position, a tagged value, a type, a term, a +parameter, a generic value, a list of types) with the depth the oracle passes to that call, so +that the limit of 256 on nesting falls where the oracle's does. A value at a position with no +readable form, and a member written as an object, are read by the generic reader of `BS-24` +(`readFrom`) at the oracle's depth. Positions are found in the schema as `BS-36` finds them, and an +object's members are kept in the order they were read and sorted by key when it closes, so that a +member written twice is refused at its name after its value is read, as the oracle's `BTreeMap` +refuses it. The printer of the notation and the decoding of a module stay in the host. +The canonical JSON of a native core module (§17.10, §21.7) is a component of the front end as +source (`BS-38`, issue 269), the module `CoreJson`, with one production root for `rust-std`: + +| Root | Takes | Returns | Oracle | +| --- | --- | --- | --- | +| `LexLeanBoot.CoreJson.coreJson` | a core module | its canonical JSON text | `surface::core::canonical_json` | + +The module takes a core module as first-order data: its schema tag, its imports, its node table, +its proof nodes, and its declarations. A node, a universe level, a binder's visibility, and an +annotation's value are inductives of the module that mirror the kernel terms of §17.10, and a +declaration is the standard library's `Json` (`SL-11`, `SL-13`) of the value the host serializes +it to, which the root writes with the standard library's writer, as the oracle writes the +serialized value. Every other part is written by the root itself in the oracle's order: the +members of a node and of a level in byte order of key, and every string escaped by `quoteText` +(`BS-22`). A level is written by structural recursion over the level, and the writer of a +declaration recurses over the value, so the root admits `recursion`. The module's types are the +input of the printer of the core notation (`BS-39`). +The printer of the core notation (§17.21.8) is a component of the front end as source (`BS-39`, +issue 269), the module `CorePrint` over `CoreJson` (`BS-38`), whose module types it reads, +`SurfaceDoc` (`BS-23`), and `SurfaceGeneric` (`BS-24`), with one production root for `rust-std`: + +| Root | Takes | Returns | Oracle | +| --- | --- | --- | --- | +| `LexLeanBoot.CorePrint.printCoreModule` | a core module | its notation, or the error of a module the notation does not carry | `surface::core::print_module` | + +The root refuses another schema, then a node pointing to itself or forward, then proof nodes that +are not strictly increasing nodes, then a declaration root outside the module, as the oracle's +`check` does, and writes the imports and the declarations. The statements come from one pass over +the node table in its order. A stack holds the nodes still to be written inside another, and a node +takes a child from the top of the stack when the child is used once (the uses are counted from a +sorted list of every edge and root, so the pass reads them in order), is the top, and is less than +48 deep, which is the oracle's rule that a child ends right before the children its user has taken. +The same pass builds each node's term from the terms it takes and refuses a string literal that +holds no Unicode scalar at its node, the first in the order of the nodes, which is the order the +oracle meets them. A term holds its bound variables by index and its constants by name, and the +binders it writes, so a node's term does not depend on where it is written; a structural walk of +each statement then resolves them against the binders in scope, as the oracle's scope does. The +walk and the levels admit `recursion`. The reader of the notation stays in the host. +The reader of the core notation (§17.21.8) is a component of the front end as source (`BS-40`, +issue 269), the module `CoreRead` over `CoreJson` (`BS-38`), `SurfaceLex` (`BS-22`), and +`SurfaceGeneric` (`BS-24`), with one production root for `rust-std`: + +| Root | Takes | Returns | Oracle | +| --- | --- | --- | --- | +| `LexLeanBoot.CoreRead.readCoreModule` | a text | its imports, nodes, proof nodes, each declaration's members with its roots, and the byte range of each declaration, or the offset and message of the first error | `surface::core::read_module` | + +The oracle reads a module and then decodes each declaration's members into a declaration; the +seam `read_module` returns what it reads before that decoding, and `parse_module` is that seam +followed by the decoding, so the root is held to the reader and the decoding stays in the host. +The root scans a token where the oracle peeks one, so that a lexical error stands where the +oracle meets it. A term is read by a machine with an explicit stack of jobs (a term, a binder and +its close, the arrow and body of a `fun` or a binder form, the parts of a `let`, an arrow after a +head, a prefix form, an application's arguments, a parenthesis, a `proof`), with a stack of the +parts a job holds and of the names in scope, and the universe levels by a second machine of the +same kind, so the root admits no `recursion`; each node is numbered when it finishes, as the +oracle pushes it. A declaration's members are read by `parseGeneric`, and its kind, name, +universe parameters, and roots are added in order of key; a root that names a statement is +resolved after every statement is read. +The grammar of a mathematical island (§15.5) is a component of the front end as source (`BS-42`, +issue 269), the module `GrammarMath` over the atoms of `PrimitiveScan` (`BS-03`), with one +production root for `rust-std`: + +| Root | Takes | Returns | Oracle | +| --- | --- | --- | --- | +| `LexLeanBoot.GrammarMath.parseMath` | the path, the atoms, the island's range, the closure's projection, the limits, and the budget spent before | the covers of the range, or the refusal's code, message, and span | `grammar::math::parse_math` | + +The glossary closure belongs to the lexicon, so the root takes its projection: for each atom +position, the math-channel edges the token lattice holds there (each with its end, and the frame, +precedence, and associativity of its form), with the limits `max_token_lattice_edges`, +`max_parse_states`, and `max_scope_depth` and the states the budget spent before. The root keeps +the lattice's counter and its memo of positions, so an edge is counted once however many passes +read its position, and every `LLS8002` falls where the oracle's does. The Pratt parser is a stack +of frames under `iterate_until` (an expression, its operator loop, a pending infix, a primary, a +grouping, a prefix operand, a call's arguments), one step a frame, so it admits no `recursion`; +each value carries its atom range and an encoding of the node, so the start of a call's head and +the equality of two covers are read without a walk. The worklist of segmentations is the oracle's: +each pass takes the shortest extent at a branch point it has not chosen, records the branch +points it met, and every other extent of each is one more choice to try, once. The token lattice +and the elaboration of a cover stay in the host. +The grammar keywords, the unknown-atom diagnosis, the binders, and the term phrases of the text +channel (§15.3, §15.4, §12.2) are a component of the front end as source (`BS-43`, issue 269), the +module `PropositionTerms` over the tokens of `GrammarProof` (`BS-35`) and the atoms of +`PrimitiveScan` (`BS-03`), with four production roots for `rust-std`: + +| Root | Takes | Returns | Oracle | +| --- | --- | --- | --- | +| `LexLeanBoot.PropositionTerms.keywordEntry` | a word | the core entry of the grammar keyword it spells, or none | `grammar::proposition::keyword_entry` | +| `LexLeanBoot.PropositionTerms.unknownAtom` | a sentence's atoms and tokens, the closure's projection, and the budget | the unknown atom's refusal, or none, or the limit's refusal | `TextParser::unknown_atom_diagnostic` | +| `LexLeanBoot.PropositionTerms.binderAlternatives` | the same, a token, and whether the slot requires an article | every binder there and the token after it | `TextParser::binder` | +| `LexLeanBoot.PropositionTerms.termPhrase` | the same, a token, and a nesting depth | every term phrase there and the token after it | `TextParser::term_phrase` | + +As for `BS-42`, the root takes the closure's projection: the text-channel edges at each atom +position, each with its form's category and whether the form is plural or sentence-case, the +limits, and the states the budget spent before, and it keeps the lattice's counter and memo. A term +phrase whose arguments are term phrases is a stack of frames under `iterate_until`: the start of a +phrase, the noun forms after `the` still to try, the alternatives of a first argument still to +extend, and the second arguments of a binary form, each frame holding the alternatives gathered so +far, so every alternative comes out in the oracle's order and none of the roots admits `recursion`. +The proposition grammar and the elaboration of a phrase stay in the host. ## 18. Lean backend @@ -12154,6 +12322,14 @@ 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-35` | `bootstrap` | The text channel and the simple proof sentences of SPEC.md §15.5 and §16.2 (`grammar::chart::text_tokens`: an atom range with its white space dropped and each mathematical island, from `\(` or `\[` to the first closing control of its kind, folded into one token, or the first refusal in the range: a dollar delimiter (`LLP2001`), an island opened inside another, an island without its close, and a close that closes none (`LLL1006`); and `grammar::proof::parse_proof_sentence`: the tokens of one sentence read as one of the seven registered simple sentences, with the keyword occurrences that cover it, or `LLF5005` at the first token that composes a forbidden proof word with the byte-adjacent atoms after it or begins the sentence with an underscore, and otherwise at the sentence's first atom) is language-1.2 source in the `bootstrap/` project, as two production roots of `GrammarProof` over the atoms of `BS-03`: `textTokens` and `parseProofSentence`, each eligible for `rust-std` with exactly its declared effects, none of them `recursion`, 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 the hand-built vectors, which reach every refusal of the two, every registered sentence, and every forbidden word, on seeded atom ranges of every committed `.lex.tex` source, and on seeded sentences and seeded edits of the registered ones; 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; the glossary forms, the proposition grammar, and the elaboration of a sentence are outside the claim. | §17.23, §15.5, §16.2 | +| `BS-36` | `bootstrap` | The printer of the authoring notation of a semantic module of SPEC.md §17.21 (`surface::semantic::print_at`: a value at the position of a declaration, a term, a type, or a proof, found by walking the semantic-module schema, written in the readable form of a term, a type, a member, or a parameter when that form carries the value exactly and otherwise in the generic notation tagged by its `kind`, with each member at the position the schema gives it, and laid out at 100 columns; and `surface::semantic::print_module`: the declarations of a module so written, each ended by a line feed and separated by an empty line, or the error of a value that is not a module) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticPrint` over the layout algebra of `BS-23`, the generic notation of `BS-24`, and the standard library's `Json`, with the schema as the host's input: `printAt` and `printSemanticModule`, each eligible for `rust-std` with exactly its declared effects, which admit `recursion` as the printer of `BS-24` does, 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 the hand-built vectors, which reach every readable form and each condition that writes a value generically, every error of a module, and the schema's sample of every alternative of the declaration, term, type, and proof unions, on every declaration and every module of the committed semantic modules written in the notation, and on seeded edits of their declarations; 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; the reader of the notation and the decoding of a module are outside the claim. | §17.23, §17.21 | +| `BS-37` | `bootstrap` | The reader of the authoring notation of a semantic module of SPEC.md §17.21 (`surface::semantic::parse_at`: a text lexed by the lexer of the notation and read as exactly one value at the position of a declaration, a term, a type, or a proof, found by walking the semantic-module schema, in the readable form of a term, a type, a member, or a parameter, or in the generic notation tagged by its `kind` with each member at the position the schema gives it, with the nesting the notation bounds at 256, or the offset and message of the first error; `surface::semantic::parse_module`: the declarations of a text read one after the other and made a module under its schema tag; and `surface::semantic::declaration_spans`: the byte range of each declaration so read) is language-1.2 source in the `bootstrap/` project, as three production roots of `SemanticRead` over the lexer of `BS-22`, the generic notation of `BS-24`, and the standard library's `Json`, with the schema as the host's input: `parseAt`, `parseSemanticModule`, and `semanticSpans`, each eligible for `rust-std` with exactly its declared effects, none of them `recursion`, 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 the hand-built vectors, which reach every readable form, every error of the reader, and the limit on nesting, on the schema's sample of every alternative of the declaration, term, type, and proof unions printed and read back, on every committed semantic module written in the notation read and spanned and each of its declarations printed and read back, and on seeded edits of the printed declarations; 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; the printer of the notation and the decoding of a module are outside the claim. | §17.23, §17.21 | +| `BS-38` | `bootstrap` | The canonical JSON of a native core module of SPEC.md §17.10 and §21.7 (`surface::core::canonical_json`: the module's declarations as the values they serialize to, its imports, its node table with each node's members in byte order of key, its proof nodes, and its schema tag, written with object keys in byte order and strings escaped as canonical JSON escapes them) is language-1.2 source in the `bootstrap/` project, as the production root `coreJson` of `CoreJson`, which takes the module as first-order data (its kernel terms, levels, binders, and annotations as inductives of the module, and each declaration as the standard library's `Json`), eligible for `rust-std` with exactly its declared effects, which admit `recursion` as the standard library's writer does, 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 the hand-built modules, which reach every kind of node, level, binder, and annotation and strings that need escaping, on the committed core modules written in the notation, and on seeded edits of them; 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; the serialization of a declaration to its value and the validation of a module are outside the claim. | §17.23, §17.21, §17.10, §21.7 | +| `BS-39` | `bootstrap` | The printer of the notation of a native core module of SPEC.md §17.21.8 (`surface::core::print_module`: a module of the schema `lexlean/core-module/2` whose edges point backward and whose proof nodes and declaration roots are nodes of it, written as its imports, its declarations with their members in the generic notation, and its statements, a node written inside the node that uses it exactly when it is used once, ends right before that node's other written-inside children, and is less than 48 deep, its bound variables written by the name of their binder when no nearer binder has that name and otherwise by index, a constant a binder in scope is named like written `const`, and each statement laid out at 100 columns; or the error of a module the notation does not carry, in the oracle's order) is language-1.2 source in the `bootstrap/` project, as the production root `printCoreModule` of `CorePrint` over the module types of `BS-38`, the layout algebra of `BS-23`, and the generic notation of `BS-24`, eligible for `rust-std` with exactly its declared effects, which admit `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 the hand-built modules, which reach every error and every kind of node, level, binder, annotation, and name, the inlining bound, and lines beyond the width, on the committed core modules written in the notation, and on seeded edits of them; 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; the reader of the notation and the decoding of a module are outside the claim. | §17.23, §17.21 | +| `BS-40` | `bootstrap` | The reader of the notation of a native core module of SPEC.md §17.21.8 up to the decoding of its declarations (`surface::core::read_module`, the reader of `surface::core::parse_module`: a text read as its imports, its declarations with their members in the generic notation and their kind, name, universe parameters, and roots, and its statements, each numbering its nodes in the order they finish and checking its label, with names resolved to the nearest binder in scope, a nesting depth of at most 256 and a level raised by at most 1024, the byte range of each declaration, and the roots a statement's name states resolved; or the offset and message of the first error, in the order the oracle finds them) is language-1.2 source in the `bootstrap/` project, as the production root `readCoreModule` of `CoreRead` over the module types of `BS-38`, the string reader of `BS-22`, and the generic notation of `BS-24`, eligible for `rust-std` with exactly its declared effects, none of them `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 the hand-built texts, which reach every error of the reader and every form of the notation, on the committed core modules written in the notation, and on seeded edits of them; 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; the decoding of a declaration's members into a declaration and the validation of a module are outside the claim. | §17.23, §17.21 | +| `BS-42` | `bootstrap` | The grammar of a mathematical island of SPEC.md §15.5 (`grammar::math::parse_math`: an island's atom range parsed by a Pratt parser over the declared precedence scale, with explicit call syntax, grouping parentheses, `\lexeme` and `\reference` arguments, prefix, postfix, and infix operators whose precedence, associativity, and frame come from their glossary entries, and leaves that are glossary forms or composed identifiers, under every segmentation the token lattice of §14.1 admits, giving the distinct complete covers in the order the oracle's worklist finds them, or the first failure: an expression missing, a grouping or argument unclosed, braces, a malformed reference (`LLR3005`), candidates that disagree on their profile, a surface both a prefix and a leaf (`LLP2002`), an unknown atom (`LLL1004`), a nonassociative chain, text after the expression, and the limits of nesting, parse states, and lattice edges (`LLS8002`)) is language-1.2 source in the `bootstrap/` project, as the production root `parseMath` of `GrammarMath` over the atoms of `BS-03`, with the glossary closure's projection as the host's input (the math-channel edges at each atom position with the frame, precedence, and associativity of each edge's form, and the limits and counters of the parse budget), eligible for `rust-std` with exactly its declared effects, none of them `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 the hand-built islands, which reach every failure, every kind of node, and an island with two covers, over the built-in closure and a lexicon package of the case's own, on the islands of the committed `.lex.tex` sources, and on seeded islands; 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; the token lattice, the glossary closure, and the elaboration of a cover are outside the claim. | §17.23, §15.5, §14.1 | +| `BS-43` | `bootstrap` | The grammar keywords, the unknown-atom diagnosis, the binders, and the term phrases of the text channel of SPEC.md §15.3, §15.4, and §12.2 (`grammar::proposition::keyword_entry`: the core entry a grammar keyword's spelling names; `TextParser::unknown_atom_diagnostic`: the first word or symbol of a sentence that is no grammar or proof keyword and that no text-channel form beginning at or before it covers, as `LLL1004`; `TextParser::binder`: every binder at a token, a type-noun form that is neither plural nor sentence-case or a mathematical island, then an island holding exactly one composed identifier, with or without the article the slot requires; and `TextParser::term_phrase`: every term phrase at a token, an island or `the SELF of ARG [and ARG]` over the noun-of and binary-noun-of forms with term phrases as arguments, in the oracle's order; each failing only by the limits of nesting, parse states, and lattice edges, `LLS8002`) is language-1.2 source in the `bootstrap/` project, as four production roots of `PropositionTerms` over the tokens of `BS-35` and the atoms of `BS-03`, with the glossary closure's projection as the host's input (the text-channel edges at each atom position with each form's category and whether it is plural or sentence-case, and the limits and counters of the parse budget): `keywordEntry`, `unknownAtom`, `binderAlternatives`, and `termPhrase`, each eligible for `rust-std` with exactly its declared effects, none of them `recursion`, 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 the hand-built sentences over the built-in closure and the committed glossary of the Peano example, which reach every limit failure, an unknown word and symbol, every kind of binder and term phrase, and each condition that refuses one, on every keyword spelling, on the sentences of the committed `.lex.tex` sources asked at every token, and on seeded sentences; 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; the token lattice, the glossary closure, the proposition grammar, and elaboration are outside the claim. | §17.23, §15.3, §15.4, §12.2 | | `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 | @@ -12170,7 +12346,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:** 504. 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..763d26e535 100644 --- a/VERIFICATION.md +++ b/VERIFICATION.md @@ -11728,6 +11728,326 @@ committed exports`, as `BS-21` to `BS-26` did before their regeneration, which produces the three packages and measures the row of `print_tooling_module` in `tests/stack-bounds.json` (`PD-08`). The branch is stacked on `BS-24` and lands after it. +### the text channel and the simple proof sentences are source (BS-35) + +`BS-35` holds the production roots `LexLeanBoot.GrammarProof.textTokens` and +`parseProofSentence` (`bootstrap/src/GrammarProof.lex.tex`) to +`grammar::chart::text_tokens` and `grammar::proof::parse_proof_sentence`: the +tokens of an atom range with each mathematical island folded, and one of the +seven registered simple proof sentences with the keywords that cover it, or the +refusal. The module imports `PrimitiveScan`, whose atoms, places, and +`Rejection` it reads and returns, and the standard library's `Text`; neither +root admits `recursion`. The case holds the roots to the oracle on hand-built +sentences and ranges that reach every refusal of the two (the dollar delimiter, +a nested, unclosed, or stray island, a forbidden word, and an unregistered +sentence), every registered sentence, and each of the 31 forbidden words; on +ranges that start inside the atoms and on atoms without their white space, which +hold the composition of a tactic word to bytes; on seeded atom ranges of every +committed `.lex.tex` source, each scanned once and dropped; and on seeded +sentences and seeded edits of the registered ones. Each planted defect names the +one vector that catches it, and the case asserts that the site of each plant +occurs once. The oracle's tests for the `<;>` combinator and the focusing dot +`·` are kept in the module as written but no plant can reach them: a semicolon +ends a composed word, and `·` is a Unicode symbol, so no scanned sentence +decides them. The row's statement carries no counts. + +Measured with the compiler binary of this tree, on a copy of the project whose +only entrypoint is `GrammarProof`: `lexlean check`, `lexlean build`, and +`lexlean verify` pass, and Lean's audit reports no axiom. + +The case was first compiled and run on the normative host on `release/v0.4` at +`aaf845507` (`cargo test -p repo-conformance --test conformance -- +conformance_bs_35`, 278 s), and again after merging `release/v0.4` at `26e5e0ace` +(302 s), with the same counts. Its corpus is 110 named vectors (99 of them read as +sentences), 1,002 committed sources, 600 and 4,000 seeded ranges, and 400 and +3,000 seeded sentences. The evaluator agrees with the oracle on 1,110 runs of +`textTokens` and 1,008 of `parseProofSentence`, the rendered packages under the +pinned rustc on 13,518, and each of the 25 planted defects is detected by its +own vector. The first run found no fault of the module; it found the case's own: +holding the scanned atoms of every committed source at once took 9.4 GB, so the +ranges are drawn one source at a time. The case then stops at the stack-bound +step, `bootstrap/text_tokens is no package of the committed exports`, as each +bootstrap member did before its regeneration, which produces the +`bootstrap/text_tokens` and `bootstrap/parse_proof_sentence` packages. The +branch is based on `release/v0.4` and stacks on nothing. +### the printer of the semantic notation is source (BS-36) + +`BS-36` holds the production roots `LexLeanBoot.SemanticPrint.printAt` and +`printSemanticModule` (`bootstrap/src/SemanticPrint.lex.tex`) to +`surface::semantic::print_at` and `print_module`. The semantic-module schema is +the host's input, the bytes the oracle reads from the embedded tree, which the +case asserts are the committed `schemas/semantic-module-v2.schema.json`. The +module imports `SurfaceGeneric` (`BS-24`), `SurfaceDoc` (`BS-23`), `SurfaceLex` +(`BS-22`), and the standard library's `Json`, `Order`, and `Text`; both roots +admit `recursion`, as the printer of `BS-24` does. The case holds the roots to +the oracle on hand-built values that reach every readable form of a term and a +type and each condition that writes a value generically (a member more, a name +the form cannot write, a non-canonical literal, empty type arguments, a member +spelled like a type), every error of a module, and the schema's sample of every +alternative of the declaration, term, type, and proof unions; on the committed +semantic modules written in the notation, read by the oracle and printed whole +and one declaration at a time; and on seeded edits of their declarations. Each +planted defect names the one vector that catches it, and the case asserts that +the site of each plant occurs once. One test of the oracle cannot be planted: +`plain` refuses a variable named `false`, which `is_bare` already refuses, so +dropping it changes no output. The row's statement carries no counts. + +Measured with the compiler binary of this tree, on a copy of the project whose +only entrypoint is `SemanticPrint`: `lexlean check`, `lexlean build`, and +`lexlean verify` pass once the axioms Lean's audit reports are declared +(`Quot.sound` and `propext` on the definitions that match on the schema's +values). The first verify refused `docAt` for returning its own matched +parameter in a leaf arm (`LLI9001`); the arm rebuilds the constructor. + +The case was compiled and run on the normative host on `release/v0.4` at +`26e5e0ace`. Its corpus is 314 named vectors (306 values at a position and 8 +modules), 408 committed modules with 9,022 declarations (328 declarations and +176 modules under the evaluator, 1,508 and 340 under rustc, sampled because +each run carries the schema), and 200 and 800 seeded edits. The evaluator +agrees with the oracle on 834 runs of `printAt` and 184 of +`printSemanticModule`, and each of the 29 planted defects is detected by its own +vector. The first runs found no fault of the module and three of the case: two +schema samples shared a label, the whole corpus with a copy of the schema in +each run took 9 GB, and one plant's vector used a parameter name that is still +bare. The rustc differential of that run was ended by the host's out-of-memory +killer, which stopped the shared VM's largest processes for another job; it +reruns with the regeneration. The case then stops at the stack-bound step, as +each bootstrap member did before its regeneration, which produces the +`bootstrap/print_at` and `bootstrap/print_semantic_module` packages. The branch +is based on `release/v0.4` and stacks on nothing. +### the reader of the semantic notation is source (BS-37) + +`BS-37` holds the production roots `LexLeanBoot.SemanticRead.parseAt`, +`parseSemanticModule`, and `semanticSpans` (`bootstrap/src/SemanticRead.lex.tex`) +to `surface::semantic::parse_at`, `parse_module`, and `declaration_spans`. The +semantic-module schema is the host's input, as for `BS-36`. The module lexes the +text with `lexText` of `SurfaceLex` (`BS-22`), so that a lexical error comes +first as the oracle's `Reader::new` reports it, and reads the tokens with an +explicit stack of frames under `iterate_until`, at the depths the oracle uses +(a value at depth 1, each member one deeper, 256 at most); generic values go +through the value reader of `SurfaceGeneric` (`BS-24`). None of the roots admits +`recursion`. The case holds the roots to the oracle on hand-built texts that +reach each of the 46 error messages of the reader, every readable form, and the +limit on nesting; on the schema's sample of every alternative printed and read +back; on the committed semantic modules written in the notation, read and +spanned whole, and each declaration printed and read back; and on seeded edits +of printed declarations. Each planted defect 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. + +Measured with the compiler binary of this tree, on a copy of the project whose +only entrypoint is `SemanticRead`: `lexlean check`, `lexlean build`, and +`lexlean verify` pass, with the axioms Lean's audit reports declared on 52 +definitions. + +The case was compiled and run on the normative host on `release/v0.4` at +`26e5e0ace`. Its corpus is 341 named vectors, 408 committed modules with 9,042 +declarations (329 declarations and 112 modules under the evaluator, 760 and 309 +under rustc, sampled because each run carries the schema), and 150 and 400 +seeded edits. The evaluator agrees with the oracle on 812 runs of `parseAt`, 116 +of `parseSemanticModule`, and 116 of `semanticSpans`, and each of the 37 planted +defects is detected by its own vector. The first runs found no fault of the +module and three of the case: two schema samples shared a label, no vector +reached `expected a name after `::``, and the evaluator's helper threads (one per +processor) each holding a reader's run took the case past the host's memory, so +it runs on four processors. The rustc differential of that run was ended when the +host's out-of-memory killer stopped a harness build for another job; it reruns +with the regeneration. The case then stops at the stack-bound step, as each +bootstrap member did before its regeneration. The branch is based on +`release/v0.4` and stacks on nothing. +### the canonical JSON of a core module is source (BS-38) + +`BS-38` holds the production root `LexLeanBoot.CoreJson.coreJson` +(`bootstrap/src/CoreJson.lex.tex`) to `surface::core::canonical_json`: a native +core module as its canonical JSON text. The root takes the module as +first-order data (its schema tag, imports, node table, proof nodes, and +declarations, each declaration as the standard library's `Json` of the value +the host serializes it to) and writes the rest in the oracle's order; it admits +`recursion` over a level and through the writer of a declaration. The case holds +the root to the oracle on hand-built modules that reach every kind of node, +level, binder, and annotation value, on every committed core module of +language 1.2, and on seeded edits of them. Each planted defect 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. + +Measured with the compiler binary of this tree, on a copy of the project whose +only entrypoint is `CoreJson`: `lexlean check`, `lexlean build`, and +`lexlean verify` pass, with the axioms Lean's audit reports declared. + +The case was first compiled and run on the normative host on `release/v0.4` at +`26e5e0ace` (`cargo test -p repo-conformance --test conformance -- +conformance_bs_38`, 1,209 s). Its corpus is 12 named vectors, the 407 committed +core modules (19 under the evaluator, all 407 under rustc), and 60 and 600 +seeded edits. The evaluator agrees with the oracle on 91 runs and the rendered +package under the pinned rustc on 1,019, and each of the 20 planted defects is +detected by its own vector. The first run found no fault of the module. The case +then stops at the stack-bound step, `bootstrap/core_json is no package of the +committed exports`, as each bootstrap member did before its regeneration. The +branch is based on `release/v0.4`; `BS-39` and `BS-40` stack on it. +### the printer of the core notation is source (BS-39) + +`BS-39` holds the production root `LexLeanBoot.CorePrint.printCoreModule` +(`bootstrap/src/CorePrint.lex.tex`) to `surface::core::print_module`: the notation +of a native core module of `lexlean/core-module/2` (§17.21.8). The root takes the +module as the first-order data of `CoreJson` (`BS-38`), checks it as the oracle +does, decides which nodes are written inside the node that uses them, builds the +terms in one pass over the nodes with a stack, resolves each variable to the +name of its binder or to its index and each constant to a bare word or +`const "…"` per statement, and lays the statements out at 100 columns with +`SurfaceDoc` (`BS-23`). It admits `recursion` in its printers of levels and +documents. The case holds the root to the oracle on hand-built modules that reach +every node, level, binder, annotation, and declaration form, a shadowed binder, a +constant named like a binder, and every error of the check; on every committed +core module of language 1.2; and on seeded edits of them. Each planted defect +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. + +Measured with the compiler binary of this tree, on a copy of the project whose +only entrypoint is `CorePrint`: `lexlean check`, `lexlean build`, and +`lexlean verify` pass, with the axioms Lean's audit reports declared. Lean's +certificate check first refused a helper that tested a field of a pair inside a +condition; it reads a record of its own. + +The case was compiled and run on the normative host on `release/v0.4` at +`26e5e0ace` with `BS-38` (1,382 s). Its corpus is 39 named vectors, the 407 +committed core modules (2 under the evaluator, all 407 under rustc), and 25 and +500 seeded edits. The evaluator agrees with the oracle on 66 runs and the +rendered package under the pinned rustc on 946, and each of the 29 planted +defects is detected by its own vector. The first runs found no fault of the +module and two of the case: the evaluator's runs on modules of 14 KB took the +case past the host's memory, so it evaluates those of at most 9 KB on three +processors; and the vector of the shadowed binder put the inner lambda in a +statement of its own, where the variable is free either way, so it was rebuilt +to write both lambdas inside one statement. The case then stops at the +stack-bound step, `bootstrap/print_core_module is no package of the committed +exports`, as each bootstrap member did before its regeneration. The branch is +stacked on `BS-38`. +### the reader of the core notation is source (BS-40) + +`BS-40` holds the production root `LexLeanBoot.CoreRead.readCoreModule` +(`bootstrap/src/CoreRead.lex.tex`) to the reader of `surface::core::parse_module` +up to the decoding of each declaration: the imports, the node table numbered in +the order its nodes finish, the proof nodes, each declaration's members with its +roots resolved, and the byte range of each declaration, or the offset and +message of the first error. The decoding of a declaration's members (serde's +messages) stays in the host and outside the claim; to hold the module to the +reader alone, `core.rs` splits `parse_module_with_spans` into a doc-hidden +`read_module` and the decoding over it, which keeps what the function returns +and the order of its errors. The module reads the text with explicit stacks of +jobs under `iterate_until` and admits no `recursion`; it imports `CoreJson` +(`BS-38`), whose node and level types it builds, `SurfaceGeneric`, whose reader +reads a declaration's members, `SurfaceLex`, and `AxiomOutput`. The case holds the +root to the oracle on hand-built texts that reach the reader's errors, every +term, level, binder, annotation, and declaration form, named statements, a +shadowed and an unnamed binder, and the limits on depth, on `+` beyond 1,024, and +on numbers beyond 64 bits; on the committed core modules of language 1.2 written +in the notation; and on seeded edits of them. Two of the oracle's refusals are +kept in the module but no text reaches them (`the members of a declaration are +an object`: the members are the text between matching braces, which the generic +reader reads as an object or refuses; and `a statement defines the node its term +ends with`: a term's own node is the last one reading it numbers), and one test +cannot be planted (a binder's empty name never matches a word, which is never +empty). Each planted defect 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. + +Measured with the compiler binary of this tree, on a copy of the project whose +only entrypoints are `CoreJson` and `CoreRead`: `lexlean check`, `lexlean build`, +and `lexlean verify` pass, with the axioms Lean's audit reports declared. + +The case was compiled and run on the normative host on `release/v0.4` at +`26e5e0ace` with `BS-38` and `BS-39`. Its corpus is 97 named vectors, the 376 +committed core modules in the notation (1 under the evaluator, all under rustc), +and 80 and 800 seeded edits. The evaluator agrees with the oracle on 178 runs, +and each of the 29 planted defects is detected by its own vector. The first runs +found three faults of the module: it returned a declaration's members as a bare +chain where the oracle has an object; two messages named the node type where the +oracle writes `@N`; and a number beyond 64 bits overflowed before it was refused, +so the module now decides `the number is too large` on the digits. They found +four of the case: a vector's line feed made the oracle refuse a control character +before the end of an unterminated string, two plants named a pair type the module +had since replaced by a record, a vector's statement label was not its node's +number, and a plant needed a reference to the node being written. The rendered package under the pinned rustc agrees with the oracle on 1,273 runs +(520 s). +The case then stops at the stack-bound step, as each bootstrap member did before +its regeneration. The branch is stacked on `BS-39`. +### the grammar of a mathematical island is source (BS-42) + +`BS-42` holds the production root `LexLeanBoot.GrammarMath.parseMath` +(`bootstrap/src/GrammarMath.lex.tex`) to `grammar::math::parse_math`: the covers +of an island's atom range, or the refusal's code, message, and span. The glossary +closure belongs to the lexicon, so the root takes its projection as the host's +input (the math-channel edges at each atom position with their forms' frame, +precedence, and associativity, the limits, and the budget spent before); it keeps +the lattice's edge counter and memo itself, so every `LLS8002` falls where the +oracle's does. The Pratt parser is a stack of frames under `iterate_until` and +the segmentations are the oracle's worklist, so the root admits no `recursion`. +The case builds the projection from a real closure, the built-in packages and a +lexicon package of the case's own whose entries (`lean` denotations of `Not`, +`Eq`, and `rfl`) reach what the committed lexicons never write: a postfix form, +prefix and operator candidates that disagree on their profile, a surface that is +both a prefix operator and a leaf, and leaves of two atoms that branch the token +lattice. It holds the root to the oracle on those vectors, on the committed +islands, and on seeded islands. Each planted defect names the one vector that +catches it, and the case asserts that the site of each plant occurs once. One +test of the oracle cannot be planted: a comma after the expression is bindable by +the comma form of `lexlean.core`, which every closure holds, whatever the +module's own test says. The row's statement carries no counts. + +Measured with the compiler binary of this tree, on a copy of the project whose +only entrypoint is `GrammarMath`: `lexlean check`, `lexlean build`, and +`lexlean verify` pass, and Lean's audit reports no axiom. + +The case was compiled and run on the normative host on `release/v0.4` at +`26e5e0ace` (209 s). Its corpus is 74 named vectors, 509 committed islands (169 +under the evaluator, all under rustc), and 300 and 3,000 seeded islands. The +evaluator agrees with the oracle on 543 runs and the rendered package under the +pinned rustc on 3,583, and each of the 26 planted defects is detected by its own +vector. The first runs found no fault of the module and two of the case: its +lexicon package used core denotations, which only `lexlean.core` may use, and +two of its surfaces were not renderer-safe in the math channel. The case then +stops at the stack-bound step, `bootstrap/parse_math is no package of the +committed exports`, as each bootstrap member did before its regeneration. The +branch is based on `release/v0.4` and stacks on nothing. +### the proposition grammar's keywords, binders, and term phrases are source (BS-43) + +`BS-43` holds four production roots of `LexLeanBoot.PropositionTerms` +(`bootstrap/src/PropositionTerms.lex.tex`) to the term half of +`grammar/proposition.rs`: `keywordEntry` to `keyword_entry`, `unknownAtom` to +`TextParser::unknown_atom_diagnostic`, `binderAlternatives` to +`TextParser::binder`, and `termPhrase` to `TextParser::term_phrase`. The module +reads the text tokens and keyword occurrences of `GrammarProof` (`BS-35`) and the +atoms of `PrimitiveScan` (`BS-03`); the glossary closure belongs to the lexicon, +so the roots take its projection as the host's input, as `BS-42` does, with the +text-channel edges carrying each form's category and whether it is plural or +sentence-case. `termPhrase` gathers every alternative in the oracle's order with +an explicit stack, so no root admits `recursion`. The case builds the projection +from the built-in closure and the committed `peano.arith` glossary and holds the +roots to the oracle on hand-built sentences that reach every refusal and every +shape of a binder and a term phrase, on the 40 keyword spellings, on committed +sentences asked at every token, and on seeded sentences. Each planted defect +names the one vector that catches it, and the case asserts that the site of each +plant occurs once. Two tests of the oracle cannot be planted: an atom that is a +grammar or proof keyword is never reported unknown, but every such keyword, +sentence-case spellings included, is the surface of a text form of +`lexlean.core`, which every closure holds, so the glossary covers it either way. +The row's statement carries no counts. + +Measured with the compiler binary of this tree, on a copy of the project whose +only entrypoints are `GrammarProof` and `PropositionTerms`: `lexlean check`, +`lexlean build`, and `lexlean verify` pass, and Lean's audit reports no axiom. + +The case was compiled and run on the normative host on `release/v0.4` at +`26e5e0ace` with `BS-35` (409 s). Its corpus is 47 named vectors, 40 keywords, +1,500 committed sentences (150 under the evaluator, 20,550 runs under rustc), and +200 and 2,000 seeded runs. The evaluator agrees with the oracle on 40 runs of +`keywordEntry`, 84 of `unknownAtom`, 155 of `binderAlternatives`, and 158 of +`termPhrase`, the rendered packages under the pinned rustc on 22,637, and each of +the 23 planted defects is detected by its own vector. The first runs found no +fault of the module and one of the case: it read acceptance from the run's JSON +by a tag the calculus does not write. The case then stops at the stack-bound +step, `bootstrap/keyword_entry is no package of the committed exports`, as each +bootstrap member did before its regeneration. The branch is stacked on `BS-35`. ## The `bdd` recipe as the parts of one run (Issue #131) diff --git a/bootstrap/README.md b/bootstrap/README.md index c99de3f256..358bd3c526 100644 --- a/bootstrap/README.md +++ b/bootstrap/README.md @@ -55,3 +55,25 @@ 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/GrammarProof.lex.tex` (SPEC.md §17.23, `BS-35`) imports `PrimitiveScan`, whose atoms, places, +and refusals it reads and returns, and the standard library's `Text`. +`src/SemanticPrint.lex.tex` (SPEC.md §17.23, `BS-36`) imports `SurfaceGeneric`, whose generic +notation it writes every value without a readable form in, `SurfaceDoc`, `SurfaceLex`, and the +standard library's `Json`, `Order`, and `Text`. +`src/SemanticRead.lex.tex` (SPEC.md §17.23, `BS-37`) imports `SurfaceLex`, whose lexer it reads +the text with, `SurfaceGeneric`, whose generic reader it reads every value without a readable form +with, and the standard library's `Json`, `Order`, and `Text`. +`src/CoreJson.lex.tex` (SPEC.md §17.23, `BS-38`) defines the kernel terms of a core module as +inductives and imports the standard library's `Json`, whose writer it writes each declaration +with, `Numeral`, `Order`, and `Text`, and `SurfaceLex`, whose `quoteText` escapes its strings. +`src/CorePrint.lex.tex` (SPEC.md §17.23, `BS-39`) imports `CoreJson`, whose module types it +prints, `AxiomOutput`, `SurfaceDoc`, `SurfaceGeneric`, `SurfaceLex`, and the standard library's +`Json`, `Numeral`, `Order`, and `Text`. +`src/CoreRead.lex.tex` (SPEC.md §17.23, `BS-40`) imports `CoreJson`, whose module types it +reads to, `SurfaceLex`, whose string reader it reads strings with, `SurfaceGeneric`, whose +`parseGeneric` reads a declaration's members, `AxiomOutput`, and the standard library's `Json`, +`Numeral`, `Order`, and `Text`. +`src/GrammarMath.lex.tex` (SPEC.md §17.23, `BS-42`) imports `PrimitiveScan`, whose atoms, places, +and refusals it reads and returns, and the standard library's `Numeral` and `Text`. +`src/PropositionTerms.lex.tex` (SPEC.md §17.23, `BS-43`) imports `GrammarProof`, whose text tokens +and keyword occurrences it reads and returns, `PrimitiveScan`, and the standard library's `Numeral`. diff --git a/bootstrap/lexlean.toml b/bootstrap/lexlean.toml index 680666515e..e75e58cc30 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/CoreJson.lex.tex", "src/CorePrint.lex.tex", "src/CoreRead.lex.tex", "src/Diagnostics.lex.tex", "src/GnafValidate.lex.tex", "src/GrammarMath.lex.tex", "src/GrammarProof.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/PropositionTerms.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/SemanticPrint.lex.tex", "src/SemanticRead.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/CoreJson.lex.tex b/bootstrap/src/CoreJson.lex.tex new file mode 100644 index 0000000000..95c7b03e06 --- /dev/null +++ b/bootstrap/src/CoreJson.lex.tex @@ -0,0 +1,246 @@ +\begin{lexlean}{CoreJson} +\useglossary{lexlean.std.nat@1.2.0} +\importmodule{LexLeanStd.Json} +\importmodule{LexLeanStd.Numeral} +\importmodule{LexLeanStd.Order} +\importmodule{LexLeanStd.Text} +\importmodule{SurfaceLex} +\title{Natural number addition} +\begin{semanticmodule} +inductive{ + name: Level, + type_parameters: [], + parameters: [], + constructors: [ + {name: LZero, fields: []}, + {name: LSucc, fields: [Level]}, + {name: LMax, fields: [Level, Level]}, + {name: LIMax, fields: [Level, Level]}, + {name: LParam, fields: [Bytes]} + ] +} + +inductive{ + name: Binder, + type_parameters: [], + parameters: [], + constructors: [ + {name: BExplicit, fields: []}, + {name: BImplicit, fields: []}, + {name: BStrict, fields: []}, + {name: BInstance, fields: []} + ] +} + +inductive{ + name: Meta, + type_parameters: [], + parameters: [], + constructors: [ + {name: MBool, fields: [Bool]}, + {name: MNat, fields: [Bytes]}, + {name: MInt, fields: [Bytes]}, + {name: MStr, fields: [Bytes]}, + {name: MName, fields: [Bytes]} + ] +} + +structure{ + name: Entry, + type_parameters: [], + parameters: [], + fields: [{name: key, type: Bytes}, {name: value, type: Meta}] +} + +inductive{ + name: Node, + type_parameters: [], + parameters: [], + constructors: [ + {name: NBVar, fields: [Nat]}, + {name: NSort, fields: [Level]}, + {name: NConst, fields: [Bytes, (List Level)]}, + {name: NApp, fields: [Nat, Nat]}, + {name: NLambda, fields: [Bytes, Binder, Nat, Nat]}, + {name: NForall, fields: [Bytes, Binder, Nat, Nat]}, + {name: NLet, fields: [Bytes, Nat, Nat, Nat, Bool]}, + {name: NNat, fields: [Bytes]}, + {name: NString, fields: [(List Nat)]}, + {name: NMdata, fields: [(List Entry), Nat]}, + {name: NProj, fields: [Bytes, Bytes, Nat, Nat]} + ] +} + +structure{ + name: Module, + type_parameters: [], + parameters: [], + fields: [ + {name: spec, type: Bytes}, + {name: imports, type: (List Bytes)}, + {name: nodes, type: (List Node)}, + {name: proofNodes, type: (List Nat)}, + {name: declarations, type: (List LexLeanStd.Json::Json)} + ] +} + +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: jStep, + parameters: [text : Bytes, state : (Nat * Bytes)], + result: (Option (Nat * Bytes)), + body: (match (LexLeanStd.Text::byteAt text (first state)) + | Option.some byte => (ctor Option.some [(Nat * Bytes)] (((first state) + 1), (cat (second state) (if ((prim to_nat byte : Nat) =? 39) then 0x22 else (LexLeanStd.Text::sliceOr text (first state) 1))))) + | Option.none => (ctor Option.none [(Nat * Bytes)])), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: j, + parameters: [literal : String], + result: Bytes, + body: (let text : Bytes := (lit literal); (second (first (prim iterate_until (fun [text] (state : (Nat * Bytes)) => (jStep text state)) ((prim length text : Nat) + 1) (0, 0x) : ((Nat * Bytes) * Bool))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: num, + parameters: [number : Nat], + result: Bytes, + body: (LexLeanStd.Numeral::decimal number), + executable: true +} + +definition{ + name: quoted, + parameters: [text : Bytes], + result: Bytes, + body: (SurfaceLex::quoteText text), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: joined, + parameters: [pieces : (List Bytes), separator : Bytes], + result: Bytes, + body: (second (prim list_fold (fun [separator] (state : (Bool * Bytes)) (piece : Bytes) => (false, (if (first state) then piece else (cat (cat (second state) separator) piece)))) (true, (lit "")) pieces : (Bool * Bytes))), + executable: true +} + +definition{ + name: binderLetter, + parameters: [info : Binder], + result: Bytes, + body: (match info + | Binder.BExplicit => (lit "e") + | Binder.BImplicit => (lit "i") + | Binder.BStrict => (lit "s") + | Binder.BInstance => (lit "c")), + executable: true +} + +definition{ + name: levelJson, + parameters: [level : Level], + result: Bytes, + body: (match level + | Level.LZero => (j "{'k':'z'}") + | Level.LSucc inner => (cat (cat (j "{'a':") (levelJson inner)) (j ",'k':'s'}")) + | Level.LMax low high => (cat (cat (cat (cat (j "{'a':[") (levelJson low)) (lit ",")) (levelJson high)) (j "],'k':'m'}")) + | Level.LIMax low high => (cat (cat (cat (cat (j "{'a':[") (levelJson low)) (lit ",")) (levelJson high)) (j "],'k':'i'}")) + | Level.LParam label => (cat (cat (j "{'a':") (quoted label)) (j ",'k':'p'}"))), + axioms: [Quot.sound, propext], + executable: true, + recursive_argument: level +} + +definition{ + name: metaJson, + parameters: [entry : Entry], + result: Bytes, + body: (cat (cat (cat (cat (j "{'n':") (quoted (project entry key))) (j ",'v':")) (match (project entry value) + | Meta.MBool flag => (cat (cat (j "{'t':'b','v':") (if flag then (lit "true") else (lit "false"))) (lit "}")) + | Meta.MNat text => (cat (cat (j "{'t':'n','v':") (quoted text)) (lit "}")) + | Meta.MInt text => (cat (cat (j "{'t':'i','v':") (quoted text)) (lit "}")) + | Meta.MStr text => (cat (cat (j "{'t':'s','v':") (quoted text)) (lit "}")) + | Meta.MName text => (cat (cat (j "{'t':'m','v':") (quoted text)) (lit "}")))) (lit "}")), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: levelsJson, + parameters: [levels : (List Level)], + result: Bytes, + body: (joined (LexLeanStd.Order::reverseList [Bytes] (prim list_fold (fun (out : (List Bytes)) (level : Level) => (cons (levelJson level) out)) (nil Bytes) levels : (List Bytes))) (lit ",")), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: binderJson, + parameters: [letter : Bytes, label : Bytes, info : Binder, domain : Nat, body : Nat], + result: Bytes, + body: (cat (cat (cat (cat (cat (cat (cat (cat (j "{'b':'") (binderLetter info)) (j "','k':'")) letter) (j "','n':")) (quoted label)) (cat (j ",'t':") (num domain))) (cat (j ",'v':") (num body))) (lit "}")), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: nodeJson, + parameters: [node : Node], + result: Bytes, + body: (match node + | Node.NBVar position => (cat (cat (j "{'i':") (num position)) (j ",'k':'b'}")) + | Node.NSort level => (cat (cat (j "{'k':'s','l':") (levelJson level)) (lit "}")) + | Node.NConst label levels => (cat (cat (cat (cat (j "{'k':'c','n':") (quoted label)) (j ",'u':[")) (levelsJson levels)) (lit "]}")) + | Node.NApp function argument => (cat (cat (cat (cat (j "{'f':") (num function)) (j ",'k':'a','x':")) (num argument)) (lit "}")) + | Node.NLambda label info domain body => (binderJson (lit "l") label info domain body) + | Node.NForall label info domain body => (binderJson (lit "p") label info domain body) + | Node.NLet label domain bound rest dependent => (cat (cat (cat (cat (cat (cat (cat (cat (j "{'body':") (num rest)) (j ",'d':")) (if dependent then (lit "true") else (lit "false"))) (j ",'k':'e','n':")) (quoted label)) (cat (j ",'t':") (num domain))) (cat (j ",'v':") (num bound))) (lit "}")) + | Node.NNat text => (cat (cat (j "{'k':'n','v':") (quoted text)) (lit "}")) + | Node.NString scalars => (cat (cat (j "{'k':'t','v':[") (joined (LexLeanStd.Order::reverseList [Bytes] (prim list_fold (fun (out : (List Bytes)) (scalar : Nat) => (cons (num scalar) out)) (nil Bytes) scalars : (List Bytes))) (lit ","))) (lit "]}")) + | Node.NMdata entries inner => (cat (cat (cat (cat (j "{'k':'d','m':[") (joined (LexLeanStd.Order::reverseList [Bytes] (prim list_fold (fun (out : (List Bytes)) (entry : Entry) => (cons (metaJson entry) out)) (nil Bytes) entries : (List Bytes))) (lit ","))) (j "],'x':")) (num inner)) (lit "}")) + | Node.NProj label structure position inner => (cat (cat (cat (cat (cat (cat (cat (cat (j "{'i':") (num position)) (j ",'k':'j','n':")) (quoted label)) (j ",'s':")) (quoted structure)) (j ",'x':")) (num inner)) (lit "}"))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: mapped, + type_parameters: [Item], + parameters: [items : (List 'Item), render : (Fn ('Item) Bytes)], + result: (List Bytes), + body: (LexLeanStd.Order::reverseList [Bytes] (prim list_fold (fun [render] (out : (List Bytes)) (item : 'Item) => (cons (apply render item) out)) (nil Bytes) items : (List Bytes))), + executable: true +} + +definition{ + name: coreJson, + parameters: [module : Module], + result: Bytes, + body: (joined (cons (j "{'declarations':[") (cons (joined (mapped [LexLeanStd.Json::Json] (project module declarations) (fun (declaration : LexLeanStd.Json::Json) => (LexLeanStd.Json::writeJson declaration))) (lit ",")) (cons (j "],'imports':[") (cons (joined (mapped [Bytes] (project module imports) (fun (name : Bytes) => (quoted name))) (lit ",")) (cons (j "],'nodes':[") (cons (joined (mapped [Node] (project module nodes) (fun (node : Node) => (nodeJson node))) (lit ",")) (cons (j "],'proof_nodes':[") (cons (joined (mapped [Nat] (project module proofNodes) (fun (index : Nat) => (num index))) (lit ",")) (cons (j "],'spec':") (cons (quoted (project module spec)) (cons (lit "}") (nil Bytes)))))))))))) (lit "")), + axioms: [Quot.sound, propext], + executable: true, + production: {effects: [allocation, overflow, recursion], targets: [rust-std]} +} +\end{semanticmodule} +\end{lexlean} diff --git a/bootstrap/src/CorePrint.lex.tex b/bootstrap/src/CorePrint.lex.tex new file mode 100644 index 0000000000..445b2beb8e --- /dev/null +++ b/bootstrap/src/CorePrint.lex.tex @@ -0,0 +1,923 @@ +\begin{lexlean}{CorePrint} +\useglossary{lexlean.std.nat@1.2.0} +\importmodule{AxiomOutput} +\importmodule{CoreJson} +\importmodule{LexLeanStd.Json} +\importmodule{LexLeanStd.Numeral} +\importmodule{LexLeanStd.Order} +\importmodule{LexLeanStd.Text} +\importmodule{SurfaceDoc} +\importmodule{SurfaceGeneric} +\importmodule{SurfaceLex} +\title{Natural number addition} +\begin{semanticmodule} +inductive{ + name: Tdoc, + type_parameters: [], + parameters: [], + constructors: [ + {name: TNil, fields: []}, + {name: TText, fields: [Bytes]}, + {name: TSeq, fields: [Tdoc, Tdoc]}, + {name: TNest, fields: [Nat, Tdoc]}, + {name: TLine, fields: []}, + {name: TGroup, fields: [Tdoc]}, + {name: TVar, fields: [Nat]}, + {name: TConst, fields: [Bytes, Bytes]}, + {name: TBind, fields: [Bytes, Tdoc]} + ] +} + +inductive{ + name: Term, + type_parameters: [], + parameters: [], + constructors: [ + {name: Atom, fields: [Tdoc]}, + {name: App, fields: [Tdoc, (List Tdoc)]}, + {name: Prefix, fields: [Tdoc]}, + {name: Low, fields: [Tdoc]} + ] +} + +structure{ + name: Head, + type_parameters: [], + parameters: [], + fields: [{name: head, type: Tdoc}, {name: args, type: (List Tdoc)}] +} + +structure{ + name: Shown, + type_parameters: [], + parameters: [], + fields: [{name: base, type: Bytes}, {name: zero, type: Bool}, {name: count, type: Nat}] +} + +structure{ + name: Slot, + type_parameters: [], + parameters: [], + fields: [ + {name: index, type: Nat}, + {name: start, type: Nat}, + {name: height, type: Nat}, + {name: uses, type: Nat}, + {name: term, type: Term} + ] +} + +structure{ + name: Take, + type_parameters: [], + parameters: [], + fields: [ + {name: stack, type: (List Slot)}, + {name: terms, type: (List Term)}, + {name: tallest, type: Nat} + ] +} + +structure{ + name: Pass, + type_parameters: [], + parameters: [], + fields: [ + {name: index, type: Nat}, + {name: stack, type: (List Slot)}, + {name: refs, type: (List Nat)}, + {name: proofs, type: (List Nat)}, + {name: error, type: (Option Bytes)} + ] +} + +structure{ + name: Zeros, + type_parameters: [], + parameters: [], + fields: [{name: cursor, type: Nat}, {name: found, type: Bool}] +} + +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: both, + parameters: [left : Bool, right : Bool], + result: Bool, + body: (if left then right else false), + executable: true +} + +definition{ + name: same, + parameters: [left : Bytes, right : Bytes], + result: Bool, + body: (prim equal left right : Bool), + executable: true +} + +definition{ + name: num, + parameters: [number : Nat], + result: Bytes, + body: (LexLeanStd.Numeral::decimal number), + executable: true +} + +definition{ + name: listed, + parameters: [needle : Bytes, items : (List Bytes)], + result: Bool, + body: (prim list_fold (fun [needle] (found : Bool) (item : Bytes) => (if found then true else (prim equal item needle : Bool))) false items : Bool), + executable: true +} + +definition{ + name: joined, + parameters: [pieces : (List Bytes), separator : Bytes], + result: Bytes, + body: (second (prim list_fold (fun [separator] (state : (Bool * Bytes)) (piece : Bytes) => (false, (if (first state) then piece else (cat (cat (second state) separator) piece)))) (true, (lit "")) pieces : (Bool * Bytes))), + executable: true +} + +definition{ + name: codeAt, + parameters: [text : Bytes, position : Nat], + result: Nat, + body: (match (LexLeanStd.Text::byteAt text position) + | Option.some byte => (prim to_nat byte : Nat) + | Option.none => 0), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: isDigit, + parameters: [code : Nat], + result: Bool, + body: (both (48 <=? code) (code <=? 57)), + executable: true +} + +definition{ + name: isAlpha, + parameters: [code : Nat], + result: Bool, + body: (if (both (65 <=? code) (code <=? 90)) then true else (both (97 <=? code) (code <=? 122))), + executable: true +} + +definition{ + name: nameByte, + parameters: [text : Bytes, position : Nat], + result: Bool, + body: (let code : Nat := (codeAt text position); (if (if (isAlpha code) then true else (isDigit code)) then true else (if (listed (LexLeanStd.Text::sliceOr text position 1) (cons (lit "_") (cons (lit "'") (cons (lit "!") (cons (lit "?") (cons (lit ".") (cons (lit "@") (cons (lit "#") (nil Bytes))))))))) then true else (if (code (if (both (position (if (both (position (zeroStep text state)) ((prim length text : Nat) + 1) (record Zeros {cursor := 0, found := false}) : (Zeros * Bool))) found), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: keywords, + parameters: [], + result: (List Bytes), + body: (cons (lit "fun") (cons (lit "let") (cons (lit "have") (cons (lit "proof") (cons (lit "Sort") (cons (lit "proj") (cons (lit "annot") (cons (lit "const") (cons (lit "max") (cons (lit "imax") (cons (lit "true") (cons (lit "false") (cons (lit "bool") (cons (lit "nat") (cons (lit "int") (cons (lit "str") (cons (lit "name") (nil Bytes)))))))))))))))))), + executable: true +} + +definition{ + name: bare, + parameters: [text : Bytes], + result: Bool, + body: (both (0 (record Shown {base := (lit ""), zero := true, count := 0}) + | CoreJson::Level.LParam label => (record Shown {base := (spell label), zero := false, count := 0}) + | CoreJson::Level.LSucc inner => (let below : Shown := (levelShown inner); (record Shown {base := (project below base), zero := (project below zero), count := ((project below count) + 1)})) + | CoreJson::Level.LMax low high => (let left : Shown := (levelShown low); (let right : Shown := (levelShown high); (record Shown {base := (cat (cat (cat (cat (lit "max(") (shownText left)) (lit ", ")) (shownText right)) (lit ")")), zero := false, count := 0}))) + | CoreJson::Level.LIMax low high => (let left : Shown := (levelShown low); (let right : Shown := (levelShown high); (record Shown {base := (cat (cat (cat (cat (lit "imax(") (shownText left)) (lit ", ")) (shownText right)) (lit ")")), zero := false, count := 0})))), + axioms: [Quot.sound, propext], + executable: true, + recursive_argument: level +} + +definition{ + name: levelText, + parameters: [level : CoreJson::Level], + result: Bytes, + body: (shownText (levelShown level)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: levelsText, + parameters: [levels : (List CoreJson::Level)], + result: Bytes, + body: (if ((prim length levels : Nat) =? 0) then (lit "") else (cat (cat (lit ".{") (joined (LexLeanStd.Order::reverseList [Bytes] (prim list_fold (fun (out : (List Bytes)) (level : CoreJson::Level) => (cons (levelText level) out)) (nil Bytes) levels : (List Bytes))) (lit ", "))) (lit "}"))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: metaText, + parameters: [entry : CoreJson::Entry], + result: Bytes, + body: (cat (cat (spell (project entry key)) (lit " := ")) (match (project entry value) + | CoreJson::Meta.MBool flag => (if flag then (lit "bool true") else (lit "bool false")) + | CoreJson::Meta.MNat text => (cat (lit "nat ") (if (canonicalNatural text) then text else (SurfaceLex::quoteText text))) + | CoreJson::Meta.MInt text => (cat (lit "int ") (if (canonicalInteger text) then text else (SurfaceLex::quoteText text))) + | CoreJson::Meta.MStr text => (cat (lit "str ") (SurfaceLex::quoteText text)) + | CoreJson::Meta.MName text => (cat (lit "name ") (spell text)))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: tt, + parameters: [text : Bytes], + result: Tdoc, + body: (ctor Tdoc.TText text), + executable: true +} + +definition{ + name: tseq, + parameters: [parts : (List Tdoc)], + result: Tdoc, + body: (prim list_fold (fun (rest : Tdoc) (part : Tdoc) => (ctor Tdoc.TSeq part rest)) (ctor Tdoc.TNil) (LexLeanStd.Order::reverseList [Tdoc] parts) : Tdoc), + executable: true +} + +definition{ + name: paren, + parameters: [inner : Tdoc], + result: Tdoc, + body: (tseq (cons (tt (lit "(")) (cons inner (cons (tt (lit ")")) (nil Tdoc))))), + executable: true +} + +definition{ + name: appDoc, + parameters: [head : Tdoc, argsRev : (List Tdoc)], + result: Tdoc, + body: (match argsRev + | List.nil => head + | List.cons last earlier => (ctor Tdoc.TGroup (tseq (cons head (cons (ctor Tdoc.TNest 2 (tseq (prim list_fold (fun (out : (List Tdoc)) (argument : Tdoc) => (cons (ctor Tdoc.TLine) (cons argument out))) (nil Tdoc) argsRev : (List Tdoc)))) (nil Tdoc)))))), + executable: true +} + +definition{ + name: whole, + parameters: [term : Term], + result: Tdoc, + body: (match term + | Term.Atom inner => inner + | Term.App head argsRev => (appDoc head argsRev) + | Term.Prefix inner => inner + | Term.Low inner => inner), + executable: true +} + +definition{ + name: asArgument, + parameters: [term : Term], + result: Tdoc, + body: (match term + | Term.Atom inner => inner + | Term.App head argsRev => (paren (whole term)) + | Term.Prefix inner => (paren inner) + | Term.Low inner => (paren inner)), + executable: true +} + +definition{ + name: asDomain, + parameters: [term : Term], + result: Tdoc, + body: (match term + | Term.Low inner => (paren inner) + | Term.Atom inner => inner + | Term.App head argsRev => (whole term) + | Term.Prefix inner => inner), + executable: true +} + +definition{ + name: headOf, + parameters: [term : Term], + result: Head, + body: (match term + | Term.Atom inner => (record Head {head := inner, args := (nil Tdoc)}) + | Term.App head argsRev => (record Head {head := head, args := argsRev}) + | Term.Prefix inner => (record Head {head := (paren inner), args := (nil Tdoc)}) + | Term.Low inner => (record Head {head := (paren inner), args := (nil Tdoc)})), + executable: true +} + +definition{ + name: binderDoc, + parameters: [info : CoreJson::Binder, label : Bytes, bound : Term], + result: Tdoc, + body: (let marks : (Bytes * Bytes) := (match info + | CoreJson::Binder.BExplicit => ((lit "("), (lit ")")) + | CoreJson::Binder.BImplicit => ((lit "{"), (lit "}")) + | CoreJson::Binder.BStrict => ((lit "{{"), (lit "}}")) + | CoreJson::Binder.BInstance => ((lit "["), (lit "]"))); (tseq (cons (tt (cat (cat (first marks) (binderName label)) (lit " : "))) (cons (whole bound) (cons (tt (second marks)) (nil Tdoc)))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: isExplicit, + parameters: [info : CoreJson::Binder], + result: Bool, + body: (match info + | CoreJson::Binder.BExplicit => true + | CoreJson::Binder.BImplicit => false + | CoreJson::Binder.BStrict => false + | CoreJson::Binder.BInstance => false), + executable: true +} + +definition{ + name: scalarValid, + parameters: [scalar : Nat], + result: Bool, + body: (both (scalar <=? 1114111) (if (both (55296 <=? scalar) (scalar <=? 57343)) then false else true)), + executable: true +} + +definition{ + name: utf8, + parameters: [scalar : Nat], + result: Bytes, + body: (if (scalar term + | Option.none => (ctor Term.Atom (ctor Tdoc.TNil))), + executable: true +} + +definition{ + name: stringError, + parameters: [index : Nat, scalars : (List Nat)], + result: (Option Bytes), + body: (prim list_fold (fun [index] (found : (Option Bytes)) (scalar : Nat) => (match found + | Option.some message => found + | Option.none => (if (scalarValid scalar) then (ctor Option.none [Bytes]) else (ctor Option.some [Bytes] (cat (cat (cat (cat (lit "node ") (num index)) (lit ": ")) (num scalar)) (lit " is not a Unicode scalar value")))))) (ctor Option.none [Bytes]) scalars : (Option Bytes)), + executable: true +} + +definition{ + name: nodeTerm, + parameters: [node : CoreJson::Node, terms : (List Term)], + result: Term, + body: (match node + | CoreJson::Node.NBVar position => (ctor Term.Atom (ctor Tdoc.TVar position)) + | CoreJson::Node.NSort level => (ctor Term.Prefix (tt (cat (lit "Sort ") (levelText level)))) + | CoreJson::Node.NConst label levels => (ctor Term.Atom (ctor Tdoc.TConst label (levelsText levels))) + | CoreJson::Node.NApp function applied => (let head : Head := (headOf (termAt terms 0)); (ctor Term.App (project head head) (cons (asArgument (termAt terms 1)) (project head args)))) + | CoreJson::Node.NLambda label info bound body => (ctor Term.Low (ctor Tdoc.TGroup (tseq (cons (tt (lit "fun ")) (cons (binderDoc info label (termAt terms 0)) (cons (tt (lit " =>")) (cons (ctor Tdoc.TLine) (cons (ctor Tdoc.TBind label (whole (termAt terms 1))) (nil Tdoc))))))))) + | CoreJson::Node.NForall label info bound body => (ctor Term.Low (ctor Tdoc.TGroup (tseq (cons (if (both ((prim length label : Nat) =? 0) (isExplicit info)) then (asDomain (termAt terms 0)) else (binderDoc info label (termAt terms 0))) (cons (tt (lit " ->")) (cons (ctor Tdoc.TLine) (cons (ctor Tdoc.TBind label (whole (termAt terms 1))) (nil Tdoc)))))))) + | CoreJson::Node.NLet label bound value rest dependent => (ctor Term.Low (ctor Tdoc.TGroup (tseq (cons (tt (cat (cat (if dependent then (lit "have ") else (lit "let ")) (binderName label)) (lit " : "))) (cons (whole (termAt terms 0)) (cons (tt (lit " := ")) (cons (whole (termAt terms 1)) (cons (tt (lit ";")) (cons (ctor Tdoc.TLine) (cons (ctor Tdoc.TBind label (whole (termAt terms 2))) (nil Tdoc))))))))))) + | CoreJson::Node.NNat text => (ctor Term.Atom (tt (if (canonicalNatural text) then text else (cat (lit "nat ") (SurfaceLex::quoteText text))))) + | CoreJson::Node.NString scalars => (ctor Term.Atom (tt (SurfaceLex::quoteText (prim list_fold (fun (out : Bytes) (scalar : Nat) => (cat out (utf8 scalar))) (lit "") scalars : Bytes)))) + | CoreJson::Node.NMdata entries inner => (ctor Term.Prefix (ctor Tdoc.TGroup (tseq (cons (tt (cat (cat (lit "annot [") (joined (LexLeanStd.Order::reverseList [Bytes] (prim list_fold (fun (out : (List Bytes)) (entry : CoreJson::Entry) => (cons (metaText entry) out)) (nil Bytes) entries : (List Bytes))) (lit ", "))) (lit "]"))) (cons (ctor Tdoc.TLine) (cons (asArgument (termAt terms 0)) (nil Tdoc))))))) + | CoreJson::Node.NProj label structure position inner => (ctor Term.Prefix (ctor Tdoc.TGroup (tseq (cons (tt (cat (cat (cat (cat (cat (lit "proj ") (spell structure)) (lit " ")) (spell label)) (lit " ")) (num position))) (cons (ctor Tdoc.TLine) (cons (asArgument (termAt terms 0)) (nil Tdoc)))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: childrenOf, + parameters: [node : CoreJson::Node], + result: (List Nat), + body: (match node + | CoreJson::Node.NApp function applied => (cons function (cons applied (nil Nat))) + | CoreJson::Node.NLambda label info bound body => (cons bound (cons body (nil Nat))) + | CoreJson::Node.NForall label info bound body => (cons bound (cons body (nil Nat))) + | CoreJson::Node.NLet label bound value rest dependent => (cons bound (cons value (cons rest (nil Nat)))) + | CoreJson::Node.NMdata entries inner => (cons inner (nil Nat)) + | CoreJson::Node.NProj label structure position inner => (cons inner (nil Nat)) + | CoreJson::Node.NBVar position => (nil Nat) + | CoreJson::Node.NSort level => (nil Nat) + | CoreJson::Node.NConst label levels => (nil Nat) + | CoreJson::Node.NNat text => (nil Nat) + | CoreJson::Node.NString scalars => (nil Nat)), + executable: true +} + +definition{ + name: asNat, + parameters: [found : (Option LexLeanStd.Json::Json)], + result: (Option Nat), + body: (match found + | Option.some value => (match value + | LexLeanStd.Json::Json.JInt negative magnitude => (if negative then (ctor Option.none [Nat]) else (ctor Option.some [Nat] magnitude)) + | LexLeanStd.Json::Json.JBool flag => (ctor Option.none [Nat]) + | LexLeanStd.Json::Json.JStr text => (ctor Option.none [Nat]) + | LexLeanStd.Json::Json.JArr inner => (ctor Option.none [Nat]) + | LexLeanStd.Json::Json.JObj inner => (ctor Option.none [Nat]) + | LexLeanStd.Json::Json.JNil => (ctor Option.none [Nat]) + | LexLeanStd.Json::Json.JItem element rest => (ctor Option.none [Nat]) + | LexLeanStd.Json::Json.JMember key inner rest => (ctor Option.none [Nat])) + | Option.none => (ctor Option.none [Nat])), + executable: true +} + +definition{ + name: lookStep, + parameters: [key : Bytes, state : (LexLeanStd.Json::Json * (Option LexLeanStd.Json::Json))], + result: (Option (LexLeanStd.Json::Json * (Option LexLeanStd.Json::Json))), + body: (match (second state) + | Option.some hit => (ctor Option.none [(LexLeanStd.Json::Json * (Option LexLeanStd.Json::Json))]) + | Option.none => (match (first state) + | LexLeanStd.Json::Json.JMember name value rest => (ctor Option.some [(LexLeanStd.Json::Json * (Option LexLeanStd.Json::Json))] (if (same name key) then ((ctor LexLeanStd.Json::Json.JNil), (ctor Option.some [LexLeanStd.Json::Json] value)) else (rest, (ctor Option.none [LexLeanStd.Json::Json])))) + | LexLeanStd.Json::Json.JBool flag => (ctor Option.none [(LexLeanStd.Json::Json * (Option LexLeanStd.Json::Json))]) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Option.none [(LexLeanStd.Json::Json * (Option LexLeanStd.Json::Json))]) + | LexLeanStd.Json::Json.JStr text => (ctor Option.none [(LexLeanStd.Json::Json * (Option LexLeanStd.Json::Json))]) + | LexLeanStd.Json::Json.JArr inner => (ctor Option.none [(LexLeanStd.Json::Json * (Option LexLeanStd.Json::Json))]) + | LexLeanStd.Json::Json.JObj inner => (ctor Option.none [(LexLeanStd.Json::Json * (Option LexLeanStd.Json::Json))]) + | LexLeanStd.Json::Json.JNil => (ctor Option.none [(LexLeanStd.Json::Json * (Option LexLeanStd.Json::Json))]) + | LexLeanStd.Json::Json.JItem element rest => (ctor Option.none [(LexLeanStd.Json::Json * (Option LexLeanStd.Json::Json))]))), + executable: true +} + +definition{ + name: getIn, + parameters: [value : LexLeanStd.Json::Json, key : Bytes], + result: (Option LexLeanStd.Json::Json), + body: (match value + | LexLeanStd.Json::Json.JObj chain => (second (first (prim iterate_until (fun [key] (state : (LexLeanStd.Json::Json * (Option LexLeanStd.Json::Json))) => (lookStep key state)) 1000000 (chain, (ctor Option.none [LexLeanStd.Json::Json])) : ((LexLeanStd.Json::Json * (Option LexLeanStd.Json::Json)) * Bool)))) + | LexLeanStd.Json::Json.JBool flag => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JStr text => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JArr inner => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JNil => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JItem element rest => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JMember label inner rest => (ctor Option.none [LexLeanStd.Json::Json])), + executable: true +} + +definition{ + name: getOpt, + parameters: [value : (Option LexLeanStd.Json::Json), key : Bytes], + result: (Option LexLeanStd.Json::Json), + body: (match value + | Option.some inner => (getIn inner key) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json])), + executable: true +} + +definition{ + name: itemStep, + parameters: [state : (LexLeanStd.Json::Json * (List LexLeanStd.Json::Json))], + result: (Option (LexLeanStd.Json::Json * (List LexLeanStd.Json::Json))), + body: (match (first state) + | LexLeanStd.Json::Json.JItem element rest => (ctor Option.some [(LexLeanStd.Json::Json * (List LexLeanStd.Json::Json))] (rest, (cons element (second state)))) + | LexLeanStd.Json::Json.JBool flag => (ctor Option.none [(LexLeanStd.Json::Json * (List LexLeanStd.Json::Json))]) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Option.none [(LexLeanStd.Json::Json * (List LexLeanStd.Json::Json))]) + | LexLeanStd.Json::Json.JStr text => (ctor Option.none [(LexLeanStd.Json::Json * (List LexLeanStd.Json::Json))]) + | LexLeanStd.Json::Json.JArr inner => (ctor Option.none [(LexLeanStd.Json::Json * (List LexLeanStd.Json::Json))]) + | LexLeanStd.Json::Json.JObj inner => (ctor Option.none [(LexLeanStd.Json::Json * (List LexLeanStd.Json::Json))]) + | LexLeanStd.Json::Json.JNil => (ctor Option.none [(LexLeanStd.Json::Json * (List LexLeanStd.Json::Json))]) + | LexLeanStd.Json::Json.JMember key inner rest => (ctor Option.none [(LexLeanStd.Json::Json * (List LexLeanStd.Json::Json))])), + executable: true +} + +definition{ + name: itemsOf, + parameters: [value : (Option LexLeanStd.Json::Json)], + result: (List LexLeanStd.Json::Json), + body: (match value + | Option.some array => (match array + | LexLeanStd.Json::Json.JArr chain => (LexLeanStd.Order::reverseList [LexLeanStd.Json::Json] (second (first (prim iterate_until (fun (state : (LexLeanStd.Json::Json * (List LexLeanStd.Json::Json))) => (itemStep state)) 1000000 (chain, (nil LexLeanStd.Json::Json)) : ((LexLeanStd.Json::Json * (List LexLeanStd.Json::Json)) * Bool))))) + | LexLeanStd.Json::Json.JBool flag => (nil LexLeanStd.Json::Json) + | LexLeanStd.Json::Json.JInt negative magnitude => (nil LexLeanStd.Json::Json) + | LexLeanStd.Json::Json.JStr text => (nil LexLeanStd.Json::Json) + | LexLeanStd.Json::Json.JObj inner => (nil LexLeanStd.Json::Json) + | LexLeanStd.Json::Json.JNil => (nil LexLeanStd.Json::Json) + | LexLeanStd.Json::Json.JItem element rest => (nil LexLeanStd.Json::Json) + | LexLeanStd.Json::Json.JMember key inner rest => (nil LexLeanStd.Json::Json)) + | Option.none => (nil LexLeanStd.Json::Json)), + executable: true +} + +definition{ + name: strOf, + parameters: [value : (Option LexLeanStd.Json::Json)], + result: Bytes, + body: (match value + | Option.some inner => (match inner + | LexLeanStd.Json::Json.JStr text => text + | LexLeanStd.Json::Json.JBool flag => (lit "") + | LexLeanStd.Json::Json.JInt negative magnitude => (lit "") + | LexLeanStd.Json::Json.JArr chain => (lit "") + | LexLeanStd.Json::Json.JObj chain => (lit "") + | LexLeanStd.Json::Json.JNil => (lit "") + | LexLeanStd.Json::Json.JItem element rest => (lit "") + | LexLeanStd.Json::Json.JMember key member rest => (lit "")) + | Option.none => (lit "")), + executable: true +} + +definition{ + name: pushOpt, + parameters: [out : (List Nat), found : (Option Nat)], + result: (List Nat), + body: (match found + | Option.some value => (prim append out (cons value (nil Nat)) : (List Nat)) + | Option.none => out), + executable: true +} + +definition{ + name: rootsOf, + parameters: [declaration : LexLeanStd.Json::Json], + result: (List Nat), + body: (let equations : (Option LexLeanStd.Json::Json) := (getIn declaration (lit "eqn_info")); (let base : (List Nat) := (pushOpt (pushOpt (pushOpt (pushOpt (nil Nat) (asNat (getIn declaration (lit "type")))) (asNat (getIn declaration (lit "value")))) (asNat (getOpt equations (lit "type")))) (asNat (getOpt equations (lit "value")))); (prim list_fold (fun (out : (List Nat)) (field : LexLeanStd.Json::Json) => (pushOpt out (asNat (getIn field (lit "auto_param"))))) base (itemsOf (getOpt (getOpt (getIn declaration (lit "inductive")) (lit "structure")) (lit "fields"))) : (List Nat)))), + executable: true +} + +definition{ + name: edgeError, + parameters: [nodes : (List CoreJson::Node)], + result: (Option Bytes), + body: (second (prim list_fold (fun (state : (Nat * (Option Bytes))) (node : CoreJson::Node) => (let index : Nat := (first state); ((index + 1), (match (second state) + | Option.some message => (second state) + | Option.none => (prim list_fold (fun [index] (found : (Option Bytes)) (child : Nat) => (match found + | Option.some message => found + | Option.none => (if (index <=? child) then (ctor Option.some [Bytes] (cat (cat (cat (cat (lit "node ") (num index)) (lit " points to the node ")) (num child)) (lit ", which is not earlier"))) else (ctor Option.none [Bytes])))) (ctor Option.none [Bytes]) (childrenOf node) : (Option Bytes)))))) (0, (ctor Option.none [Bytes])) nodes : (Nat * (Option Bytes)))), + executable: true +} + +definition{ + name: proofError, + parameters: [proofs : (List Nat), count : Nat], + result: (Option Bytes), + body: (let fine : (Bool * (Option Nat)) := (prim list_fold (fun [count] (state : (Bool * (Option Nat))) (proof : Nat) => ((both (first state) (both (proof (prior true))), (ctor Option.some [Nat] proof))) (true, (ctor Option.none [Nat])) proofs : (Bool * (Option Nat))); (if (first fine) then (ctor Option.none [Bytes]) else (ctor Option.some [Bytes] (lit "the proof nodes are not strictly increasing nodes of the module")))), + executable: true +} + +definition{ + name: rootError, + parameters: [declarations : (List LexLeanStd.Json::Json), count : Nat], + result: (Option Bytes), + body: (prim list_fold (fun [count] (found : (Option Bytes)) (declaration : LexLeanStd.Json::Json) => (match found + | Option.some message => found + | Option.none => (if (prim list_fold (fun [count] (bad : Bool) (root : Nat) => (if bad then true else (count <=? root))) false (rootsOf declaration) : Bool) then (ctor Option.some [Bytes] (cat (cat (lit "`") (strOf (getIn declaration (lit "name")))) (lit "` has a root that is not a node of the module"))) else (ctor Option.none [Bytes])))) (ctor Option.none [Bytes]) declarations : (Option Bytes)), + executable: true +} + +definition{ + name: keyOf, + parameters: [value : Nat], + result: Bytes, + body: (second (prim iterate (fun (state : (Nat * Bytes)) => ((prim quotient (first state) 256 0 : Nat), (cat (SurfaceLex::byteOf (prim remainder (first state) 256 0 : Nat)) (second state)))) 8 (value, (lit "")) : (Nat * Bytes))), + executable: true +} + +definition{ + name: sortedRefs, + parameters: [nodes : (List CoreJson::Node), declarations : (List LexLeanStd.Json::Json)], + result: (List Nat), + body: (let all : (List Nat) := (prim list_fold (fun (out : (List Nat)) (declaration : LexLeanStd.Json::Json) => (prim list_fold (fun (inner : (List Nat)) (root : Nat) => (cons root inner)) out (rootsOf declaration) : (List Nat))) (prim list_fold (fun (out : (List Nat)) (node : CoreJson::Node) => (prim list_fold (fun (inner : (List Nat)) (child : Nat) => (cons child inner)) out (childrenOf node) : (List Nat))) (nil Nat) nodes : (List Nat)) declarations : (List Nat)); (prim list_fold (fun (out : (List Nat)) (entry : (Bytes * Nat)) => (cons (second entry) out)) (nil Nat) (LexLeanStd.Order::reverseList [(Bytes * Nat)] (LexLeanStd.Order::sortByKey [Nat] (prim list_fold (fun (out : (List (Bytes * Nat))) (value : Nat) => (cons ((keyOf value), value) out)) (nil (Bytes * Nat)) all : (List (Bytes * Nat))))) : (List Nat))), + axioms: [propext], + executable: true +} + +definition{ + name: popCount, + parameters: [refs : (List Nat), index : Nat], + result: (Nat * (List Nat)), + body: (first (prim iterate_until (fun [index] (state : (Nat * (List Nat))) => (match (second state) + | List.cons head tail => (if (head =? index) then (ctor Option.some [(Nat * (List Nat))] (((first state) + 1), tail)) else (ctor Option.none [(Nat * (List Nat))])) + | List.nil => (ctor Option.none [(Nat * (List Nat))]))) ((prim length refs : Nat) + 1) (0, refs) : ((Nat * (List Nat)) * Bool))), + executable: true +} + +definition{ + name: takeStep, + parameters: [take : Take, child : Nat], + result: Take, + body: (let fallback : Take := (record Take {stack := (project take stack), terms := (cons (ctor Term.Atom (tt (cat (lit "@") (num child)))) (project take terms)), tallest := (project take tallest)}); (match (project take stack) + | List.cons top below => (if (both ((project top index) =? child) (both ((project top uses) =? 1) ((project top height) fallback)), + executable: true +} + +definition{ + name: wrapProof, + parameters: [term : Term, proof : Bool], + result: Term, + body: (if proof then (ctor Term.Atom (tseq (cons (tt (lit "proof(")) (cons (ctor Tdoc.TNest 2 (whole term)) (cons (tt (lit ")")) (nil Tdoc)))))) else term), + executable: true +} + +definition{ + name: passStep, + parameters: [pass : Pass, node : CoreJson::Node], + result: Pass, + body: (match (project pass error) + | Option.some message => pass + | Option.none => (let index : Nat := (project pass index); (let counted : (Nat * (List Nat)) := (popCount (project pass refs) index); (let proof : Bool := (match (project pass proofs) + | List.cons head tail => (head =? index) + | List.nil => false); (let take : Take := (prim list_fold (fun (state : Take) (child : Nat) => (takeStep state child)) (record Take {stack := (project pass stack), terms := (nil Term), tallest := 0}) (LexLeanStd.Order::reverseList [Nat] (childrenOf node)) : Take); (let failure : (Option Bytes) := (match node + | CoreJson::Node.NString scalars => (stringError index scalars) + | CoreJson::Node.NBVar position => (ctor Option.none [Bytes]) + | CoreJson::Node.NSort level => (ctor Option.none [Bytes]) + | CoreJson::Node.NConst label levels => (ctor Option.none [Bytes]) + | CoreJson::Node.NApp function applied => (ctor Option.none [Bytes]) + | CoreJson::Node.NLambda label info bound body => (ctor Option.none [Bytes]) + | CoreJson::Node.NForall label info bound body => (ctor Option.none [Bytes]) + | CoreJson::Node.NLet label bound value rest dependent => (ctor Option.none [Bytes]) + | CoreJson::Node.NNat text => (ctor Option.none [Bytes]) + | CoreJson::Node.NMdata entries inner => (ctor Option.none [Bytes]) + | CoreJson::Node.NProj label structure position inner => (ctor Option.none [Bytes])); (let start : Nat := (match (project take stack) + | List.cons top below => ((project top index) + 1) + | List.nil => 0); (record Pass {index := (index + 1), stack := (cons (record Slot {index := index, start := start, height := ((project take tallest) + 1), uses := (first counted), term := (wrapProof (nodeTerm node (project take terms)) proof)}) (project take stack)), refs := (second counted), proofs := (if proof then (match (project pass proofs) + | List.cons head tail => tail + | List.nil => (nil Nat)) else (project pass proofs)), error := failure})))))))), + axioms: [Quot.sound, propext], + executable: true +} + +structure{ + name: Near, + type_parameters: [], + parameters: [], + fields: [{name: seen, type: Nat}, {name: hit, type: Bool}] +} + +definition{ + name: nearStep, + parameters: [state : Near, nearer : Bytes, label : Bytes, position : Nat], + result: Near, + body: (record Near {seen := ((project state seen) + 1), hit := (if (project state hit) then true else (both ((project state seen) (if (both (bare label) (if (project (prim list_fold (fun [label, position] (state : Near) (nearer : Bytes) => (nearStep state nearer label position)) (record Near {seen := 0, hit := false}) scope : Near) hit) then false else true)) then label else fallback) + | Option.none => fallback)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: resolve, + parameters: [node : Tdoc, scope : (List Bytes)], + result: SurfaceDoc::Doc, + body: (match node + | Tdoc.TNil => (ctor SurfaceDoc::Doc.DNil) + | Tdoc.TText text => (ctor SurfaceDoc::Doc.DText text) + | Tdoc.TSeq head tail => (ctor SurfaceDoc::Doc.DSeq (resolve head scope) (resolve tail scope)) + | Tdoc.TNest amount inner => (ctor SurfaceDoc::Doc.DNest amount (resolve inner scope)) + | Tdoc.TLine => (ctor SurfaceDoc::Doc.DLine) + | Tdoc.TGroup inner => (ctor SurfaceDoc::Doc.DGroup (resolve inner scope)) + | Tdoc.TVar position => (ctor SurfaceDoc::Doc.DText (variable scope position)) + | Tdoc.TConst label levels => (ctor SurfaceDoc::Doc.DText (if (both (bare label) (if (listed label scope) then false else true)) then (cat label levels) else (cat (cat (lit "const ") (SurfaceLex::quoteText label)) levels))) + | Tdoc.TBind label inner => (resolve inner (cons label scope))), + axioms: [Quot.sound, propext], + executable: true, + recursive_argument: node +} + +definition{ + name: render, + parameters: [node : SurfaceDoc::Doc], + result: Bytes, + body: (SurfaceDoc::renderDoc node 100 (SurfaceGeneric::docNodes node)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: withoutKeys, + parameters: [chain : LexLeanStd.Json::Json], + result: LexLeanStd.Json::Json, + body: (let kept : (List (Bytes * LexLeanStd.Json::Json)) := (second (first (prim iterate_until (fun (state : (LexLeanStd.Json::Json * (List (Bytes * LexLeanStd.Json::Json)))) => (match (first state) + | LexLeanStd.Json::Json.JMember key value rest => (ctor Option.some [(LexLeanStd.Json::Json * (List (Bytes * LexLeanStd.Json::Json)))] (rest, (if (listed key (cons (lit "kind") (cons (lit "name") (cons (lit "levels") (cons (lit "type") (cons (lit "value") (nil Bytes))))))) then (second state) else (cons (key, value) (second state))))) + | LexLeanStd.Json::Json.JBool flag => (ctor Option.none [(LexLeanStd.Json::Json * (List (Bytes * LexLeanStd.Json::Json)))]) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Option.none [(LexLeanStd.Json::Json * (List (Bytes * LexLeanStd.Json::Json)))]) + | LexLeanStd.Json::Json.JStr text => (ctor Option.none [(LexLeanStd.Json::Json * (List (Bytes * LexLeanStd.Json::Json)))]) + | LexLeanStd.Json::Json.JArr inner => (ctor Option.none [(LexLeanStd.Json::Json * (List (Bytes * LexLeanStd.Json::Json)))]) + | LexLeanStd.Json::Json.JObj inner => (ctor Option.none [(LexLeanStd.Json::Json * (List (Bytes * LexLeanStd.Json::Json)))]) + | LexLeanStd.Json::Json.JNil => (ctor Option.none [(LexLeanStd.Json::Json * (List (Bytes * LexLeanStd.Json::Json)))]) + | LexLeanStd.Json::Json.JItem element rest => (ctor Option.none [(LexLeanStd.Json::Json * (List (Bytes * LexLeanStd.Json::Json)))]))) 1000000 (chain, (nil (Bytes * LexLeanStd.Json::Json))) : ((LexLeanStd.Json::Json * (List (Bytes * LexLeanStd.Json::Json))) * Bool)))); (prim list_fold (fun (rest : LexLeanStd.Json::Json) (entry : (Bytes * LexLeanStd.Json::Json)) => (ctor LexLeanStd.Json::Json.JMember (first entry) (second entry) rest)) (ctor LexLeanStd.Json::Json.JNil) kept : LexLeanStd.Json::Json)), + executable: true +} + +definition{ + name: declarationText, + parameters: [declaration : LexLeanStd.Json::Json], + result: (Result Bytes Bytes), + body: (match declaration + | LexLeanStd.Json::Json.JObj chain => (match (getIn declaration (lit "kind")) + | Option.some kind => (match kind + | LexLeanStd.Json::Json.JStr written => (let levels : (List Bytes) := (prim list_fold (fun (out : (List Bytes)) (level : LexLeanStd.Json::Json) => (prim append out (cons (spell (strOf (ctor Option.some [LexLeanStd.Json::Json] level))) (nil Bytes)) : (List Bytes))) (nil Bytes) (itemsOf (getIn declaration (lit "levels"))) : (List Bytes)); (let head : Bytes := (cat (cat (cat (cat (cat written (lit " ")) (spell (strOf (getIn declaration (lit "name"))))) (if ((prim length levels : Nat) =? 0) then (lit "") else (cat (cat (lit ".{") (joined levels (lit ", "))) (lit "}")))) (match (asNat (getIn declaration (lit "type"))) + | Option.some root => (cat (lit " : @") (num root)) + | Option.none => (lit ""))) (match (asNat (getIn declaration (lit "value"))) + | Option.some root => (cat (lit " := @") (num root)) + | Option.none => (lit ""))); (let rest : LexLeanStd.Json::Json := (withoutKeys chain); (ctor Result.ok [Bytes, Bytes] (render (match rest + | LexLeanStd.Json::Json.JNil => (ctor SurfaceDoc::Doc.DText head) + | LexLeanStd.Json::Json.JBool flag => (ctor SurfaceDoc::Doc.DText head) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor SurfaceDoc::Doc.DText head) + | LexLeanStd.Json::Json.JStr text => (ctor SurfaceDoc::Doc.DText head) + | LexLeanStd.Json::Json.JArr inner => (ctor SurfaceDoc::Doc.DText head) + | LexLeanStd.Json::Json.JObj inner => (ctor SurfaceDoc::Doc.DText head) + | LexLeanStd.Json::Json.JItem element tail => (ctor SurfaceDoc::Doc.DText head) + | LexLeanStd.Json::Json.JMember key value tail => (ctor SurfaceDoc::Doc.DSeq (ctor SurfaceDoc::Doc.DText head) (ctor SurfaceDoc::Doc.DSeq (ctor SurfaceDoc::Doc.DText (lit " ")) (ctor SurfaceDoc::Doc.DSeq (SurfaceGeneric::docValue (ctor LexLeanStd.Json::Json.JObj rest)) (ctor SurfaceDoc::Doc.DNil)))))))))) + | LexLeanStd.Json::Json.JBool flag => (ctor Result.error [Bytes, Bytes] (lit "a declaration has a kind")) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Result.error [Bytes, Bytes] (lit "a declaration has a kind")) + | LexLeanStd.Json::Json.JArr inner => (ctor Result.error [Bytes, Bytes] (lit "a declaration has a kind")) + | LexLeanStd.Json::Json.JObj inner => (ctor Result.error [Bytes, Bytes] (lit "a declaration has a kind")) + | LexLeanStd.Json::Json.JNil => (ctor Result.error [Bytes, Bytes] (lit "a declaration has a kind")) + | LexLeanStd.Json::Json.JItem element rest => (ctor Result.error [Bytes, Bytes] (lit "a declaration has a kind")) + | LexLeanStd.Json::Json.JMember key value rest => (ctor Result.error [Bytes, Bytes] (lit "a declaration has a kind"))) + | Option.none => (ctor Result.error [Bytes, Bytes] (lit "a declaration has a kind"))) + | LexLeanStd.Json::Json.JBool flag => (ctor Result.error [Bytes, Bytes] (lit "a declaration is an object")) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Result.error [Bytes, Bytes] (lit "a declaration is an object")) + | LexLeanStd.Json::Json.JStr text => (ctor Result.error [Bytes, Bytes] (lit "a declaration is an object")) + | LexLeanStd.Json::Json.JArr inner => (ctor Result.error [Bytes, Bytes] (lit "a declaration is an object")) + | LexLeanStd.Json::Json.JNil => (ctor Result.error [Bytes, Bytes] (lit "a declaration is an object")) + | LexLeanStd.Json::Json.JItem element rest => (ctor Result.error [Bytes, Bytes] (lit "a declaration is an object")) + | LexLeanStd.Json::Json.JMember key value rest => (ctor Result.error [Bytes, Bytes] (lit "a declaration is an object"))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: refusal, + parameters: [message : Bytes], + result: (Result Bytes SurfaceLex::Failure), + body: (ctor Result.error [Bytes, SurfaceLex::Failure] (record SurfaceLex::Failure {offset := 0, message := message})), + executable: true +} + +definition{ + name: firstOf, + parameters: [options : (List (Option Bytes))], + result: (Option Bytes), + body: (prim list_fold (fun (found : (Option Bytes)) (option : (Option Bytes)) => (match found + | Option.some message => found + | Option.none => option)) (ctor Option.none [Bytes]) options : (Option Bytes)), + executable: true +} + +definition{ + name: statements, + parameters: [module : CoreJson::Module], + result: (Result Bytes Bytes), + body: (let final : Pass := (prim list_fold (fun (pass : Pass) (node : CoreJson::Node) => (passStep pass node)) (record Pass {index := 0, stack := (nil Slot), refs := (sortedRefs (project module nodes) (project module declarations)), proofs := (project module proofNodes), error := (ctor Option.none [Bytes])}) (project module nodes) : Pass); (match (project final error) + | Option.some message => (ctor Result.error [Bytes, Bytes] message) + | Option.none => (ctor Result.ok [Bytes, Bytes] (prim list_fold (fun (out : Bytes) (slot : Slot) => (cat out (cat (render (resolve (tseq (cons (tt (cat (cat (lit "@") (num (project slot index))) (lit " := "))) (cons (ctor Tdoc.TNest 2 (whole (project slot term))) (nil Tdoc)))) (nil Bytes))) (lit "\n")))) (lit "") (LexLeanStd.Order::reverseList [Slot] (project final stack)) : Bytes)))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: printCoreModule, + parameters: [module : CoreJson::Module], + result: (Result Bytes SurfaceLex::Failure), + body: (if (same (project module spec) (lit "lexlean/core-module/2")) then (let count : Nat := (prim length (project module nodes) : Nat); (match (firstOf (cons (edgeError (project module nodes)) (cons (proofError (project module proofNodes) count) (cons (rootError (project module declarations) count) (nil (Option Bytes)))))) + | Option.some message => (refusal message) + | Option.none => (let declared : (Result Bytes Bytes) := (prim list_fold (fun (state : (Result Bytes Bytes)) (declaration : LexLeanStd.Json::Json) => (match state + | Result.ok text => (match (declarationText declaration) + | Result.ok line => (ctor Result.ok [Bytes, Bytes] (cat (cat text line) (lit "\n"))) + | Result.error message => (ctor Result.error [Bytes, Bytes] message)) + | Result.error message => state)) (ctor Result.ok [Bytes, Bytes] (lit "")) (project module declarations) : (Result Bytes Bytes)); (match declared + | Result.error message => (refusal message) + | Result.ok lines => (match (statements module) + | Result.error message => (refusal message) + | Result.ok body => (ctor Result.ok [Bytes, SurfaceLex::Failure] (cat (cat (cat (cat (render (ctor SurfaceDoc::Doc.DSeq (ctor SurfaceDoc::Doc.DText (lit "imports ")) (ctor SurfaceDoc::Doc.DSeq (SurfaceGeneric::delimitedRev (lit "[") (lit "]") (prim list_fold (fun (out : (List SurfaceDoc::Doc)) (name : Bytes) => (cons (ctor SurfaceDoc::Doc.DText (spell name)) out)) (nil SurfaceDoc::Doc) (project module imports) : (List SurfaceDoc::Doc))) (ctor SurfaceDoc::Doc.DNil)))) (lit "\n\n")) lines) (lit "\n")) body))))))) else (refusal (cat (cat (lit "the notation names the schema `lexlean/core-module/2`, not `") (project module spec)) (lit "`")))), + axioms: [Quot.sound, propext], + executable: true, + production: {effects: [allocation, overflow, recursion], targets: [rust-std]} +} +\end{semanticmodule} +\end{lexlean} diff --git a/bootstrap/src/CoreRead.lex.tex b/bootstrap/src/CoreRead.lex.tex new file mode 100644 index 0000000000..0dec1a8275 --- /dev/null +++ b/bootstrap/src/CoreRead.lex.tex @@ -0,0 +1,1561 @@ +\begin{lexlean}{CoreRead} +\useglossary{lexlean.std.nat@1.2.0} +\importmodule{AxiomOutput} +\importmodule{CoreJson} +\importmodule{LexLeanStd.Json} +\importmodule{LexLeanStd.Numeral} +\importmodule{LexLeanStd.Order} +\importmodule{LexLeanStd.Text} +\importmodule{SurfaceGeneric} +\importmodule{SurfaceLex} +\title{Natural number addition} +\begin{semanticmodule} +structure{ + name: Mem, + type_parameters: [], + parameters: [], + fields: [{name: key, type: Bytes}, {name: value, type: LexLeanStd.Json::Json}] +} + +structure{ + name: Lbl, + type_parameters: [], + parameters: [], + fields: [{name: label, type: Bytes}, {name: node, type: Nat}] +} + +structure{ + name: Tok, + type_parameters: [], + parameters: [], + fields: [ + {name: kind, type: Nat}, + {name: start, type: Nat}, + {name: stop, type: Nat}, + {name: col0, type: Bool} + ] +} + +structure{ + name: Cur, + type_parameters: [], + parameters: [], + fields: [{name: at, type: Nat}, {name: lastEnd, type: Nat}] +} + +structure{ + name: Step, + type_parameters: [], + parameters: [], + fields: [{name: tok, type: Tok}, {name: cur, type: Cur}] +} + +structure{ + name: Got, + type_parameters: [], + parameters: [], + fields: [{name: text, type: Bytes}, {name: cur, type: Cur}] +} + +structure{ + name: GotNat, + type_parameters: [], + parameters: [], + fields: [{name: value, type: Nat}, {name: cur, type: Cur}] +} + +inductive{ + name: LJob, + type_parameters: [], + parameters: [], + constructors: [ + {name: LLevel, fields: [Nat]}, + {name: LAtom, fields: [Nat]}, + {name: LPlus, fields: []}, + {name: LParenClose, fields: []}, + {name: LMaxComma, fields: [Nat, Bool]}, + {name: LMaxClose, fields: [Bool]} + ] +} + +structure{ + name: LM, + type_parameters: [], + parameters: [], + fields: [ + {name: cur, type: Cur}, + {name: jobs, type: (List LJob)}, + {name: vals, type: (List CoreJson::Level)}, + {name: error, type: (Option SurfaceLex::Failure)} + ] +} + +structure{ + name: GotLevel, + type_parameters: [], + parameters: [], + fields: [{name: level, type: CoreJson::Level}, {name: cur, type: Cur}] +} + +structure{ + name: GotLevels, + type_parameters: [], + parameters: [], + fields: [{name: levels, type: (List CoreJson::Level)}, {name: cur, type: Cur}] +} + +inductive{ + name: Job, + type_parameters: [], + parameters: [], + constructors: [ + {name: JTerm, fields: [Nat]}, + {name: JBinder, fields: [Nat]}, + {name: JBinderClose, fields: []}, + {name: JFunArrow, fields: [Nat]}, + {name: JFunDone, fields: []}, + {name: JForallArrow, fields: [Nat]}, + {name: JForallDone, fields: []}, + {name: JLetColonEq, fields: [Nat]}, + {name: JLetSemi, fields: [Nat]}, + {name: JLetDone, fields: []}, + {name: JHeadArrow, fields: [Nat]}, + {name: JArrowDone, fields: []}, + {name: JPrefix, fields: [Nat]}, + {name: JAppLoop, fields: [Nat]}, + {name: JAppStep, fields: [Nat]}, + {name: JAtom, fields: [Nat]}, + {name: JClose, fields: []}, + {name: JProofDone, fields: []}, + {name: JProjDone, fields: []}, + {name: JMdataDone, fields: []} + ] +} + +inductive{ + name: Held, + type_parameters: [], + parameters: [], + constructors: [ + {name: HBinder, fields: [Nat, Bytes]}, + {name: HLet, fields: [Bytes, Bool]}, + {name: HProj, fields: [Bytes, Bytes, Nat]}, + {name: HMdata, fields: [(List CoreJson::Entry)]}, + {name: HProof, fields: [Nat]} + ] +} + +structure{ + name: M, + type_parameters: [], + parameters: [], + fields: [ + {name: cur, type: Cur}, + {name: jobs, type: (List Job)}, + {name: vals, type: (List Nat)}, + {name: held, type: (List Held)}, + {name: nodes, type: (List CoreJson::Node)}, + {name: count, type: Nat}, + {name: proof, type: (List Nat)}, + {name: scope, type: (List Bytes)}, + {name: labels, type: (List Lbl)}, + {name: error, type: (Option SurfaceLex::Failure)} + ] +} + +inductive{ + name: Root, + type_parameters: [], + parameters: [], + constructors: [{name: RId, fields: [Nat]}, {name: RLabel, fields: [Bytes, Nat]}] +} + +structure{ + name: Pending, + type_parameters: [], + parameters: [], + fields: [ + {name: members, type: LexLeanStd.Json::Json}, + {name: rootType, type: (Option Root)}, + {name: rootValue, type: (Option Root)}, + {name: start, type: Nat} + ] +} + +structure{ + name: Span, + type_parameters: [], + parameters: [], + fields: [{name: start, type: Nat}, {name: stop, type: Nat}] +} + +structure{ + name: Read, + type_parameters: [], + parameters: [], + fields: [ + {name: imports, type: (List Bytes)}, + {name: nodes, type: (List CoreJson::Node)}, + {name: proofNodes, type: (List Nat)}, + {name: declarations, type: (List LexLeanStd.Json::Json)}, + {name: spans, type: (List Span)} + ] +} + +structure{ + name: Top, + type_parameters: [], + parameters: [], + fields: [ + {name: machine, type: M}, + {name: pending, type: (List Pending)}, + {name: spans, type: (List Span)}, + {name: statements, type: Bool}, + {name: finished, type: Bool} + ] +} + +structure{ + name: Walk, + type_parameters: [], + parameters: [], + fields: [{name: rest, type: LexLeanStd.Json::Json}, {name: items, type: (List Mem)}] +} + +structure{ + name: Place, + type_parameters: [], + parameters: [], + fields: [{name: placed, type: Bool}, {name: out, type: (List Mem)}] +} + +structure{ + name: Find, + type_parameters: [], + parameters: [], + fields: [{name: seen, type: Nat}, {name: found, type: (Option Nat)}] +} + +structure{ + name: Digits, + type_parameters: [], + parameters: [], + fields: [{name: value, type: Nat}, {name: cursor, type: Nat}] +} + +structure{ + name: Scalars, + type_parameters: [], + parameters: [], + fields: [{name: cursor, type: Nat}, {name: items, type: (List Nat)}] +} + +structure{ + name: Braces, + type_parameters: [], + parameters: [], + fields: [ + {name: cursor, type: Nat}, + {name: depth, type: Nat}, + {name: result, type: (Option (Result Nat SurfaceLex::Failure))} + ] +} + +structure{ + name: Names, + type_parameters: [], + parameters: [], + fields: [ + {name: names, type: (List Bytes)}, + {name: cur, type: Cur}, + {name: done, type: Bool}, + {name: error, type: (Option SurfaceLex::Failure)} + ] +} + +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: both, + parameters: [left : Bool, right : Bool], + result: Bool, + body: (if left then right else false), + executable: true +} + +definition{ + name: same, + parameters: [left : Bytes, right : Bytes], + result: Bool, + body: (prim equal left right : Bool), + executable: true +} + +definition{ + name: listed, + parameters: [needle : Bytes, items : (List Bytes)], + result: Bool, + body: (prim list_fold (fun [needle] (found : Bool) (item : Bytes) => (if found then true else (prim equal item needle : Bool))) false items : Bool), + executable: true +} + +definition{ + name: failure, + parameters: [offset : Nat, message : Bytes], + result: SurfaceLex::Failure, + body: (record SurfaceLex::Failure {offset := offset, message := message}), + executable: true +} + +definition{ + name: codeAt, + parameters: [text : Bytes, position : Nat], + result: Nat, + body: (match (LexLeanStd.Text::byteAt text position) + | Option.some byte => (prim to_nat byte : Nat) + | Option.none => 256), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: isDigit, + parameters: [code : Nat], + result: Bool, + body: (both (48 <=? code) (code <=? 57)), + executable: true +} + +definition{ + name: isAlpha, + parameters: [code : Nat], + result: Bool, + body: (if (both (65 <=? code) (code <=? 90)) then true else (both (97 <=? code) (code <=? 122))), + executable: true +} + +definition{ + name: width, + parameters: [code : Nat], + result: Nat, + body: (if (code (if (nameCharAt text position) then (ctor Option.some [Nat] (position + (width (codeAt text position)))) else (ctor Option.none [Nat]))) ((prim length text : Nat) + 1) from : (Nat * Bool))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: digitEnd, + parameters: [text : Bytes, from : Nat], + result: Nat, + body: (first (prim iterate_until (fun [text] (position : Nat) => (if (isDigit (codeAt text position)) then (ctor Option.some [Nat] (position + 1)) else (ctor Option.none [Nat]))) ((prim length text : Nat) + 1) from : (Nat * Bool))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: blankEnd, + parameters: [text : Bytes, from : Nat], + result: Nat, + body: (first (prim iterate_until (fun [text] (position : Nat) => (let code : Nat := (codeAt text position); (if (if (code =? 32) then true else (code =? 10)) then (ctor Option.some [Nat] (position + 1)) else (ctor Option.none [Nat])))) ((prim length text : Nat) + 1) from : (Nat * Bool))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: tokOf, + parameters: [kind : Nat, start : Nat, stop : Nat, col0 : Bool], + result: (Result Tok SurfaceLex::Failure), + body: (ctor Result.ok [Tok, SurfaceLex::Failure] (record Tok {kind := kind, start := start, stop := stop, col0 := col0})), + executable: true +} + +definition{ + name: refuse, + parameters: [offset : Nat, message : Bytes], + result: (Result Tok SurfaceLex::Failure), + body: (ctor Result.error [Tok, SurfaceLex::Failure] (failure offset message)), + executable: true +} + +definition{ + name: stringEnd, + parameters: [text : Bytes, at : Nat], + result: (Result Nat SurfaceLex::Failure), + body: (let scanned : SurfaceLex::Scanned := (SurfaceLex::readString text (prim length text : Nat) at); (match (project scanned error) + | Option.some problem => (ctor Result.error [Nat, SurfaceLex::Failure] problem) + | Option.none => (if (project scanned finished) then (ctor Result.ok [Nat, SurfaceLex::Failure] (project scanned pos)) else (ctor Result.error [Nat, SurfaceLex::Failure] (failure at (lit "unterminated string")))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: stringValue, + parameters: [text : Bytes, at : Nat], + result: Bytes, + body: (let scanned : SurfaceLex::Scanned := (SurfaceLex::readString text (prim length text : Nat) at); (prim list_fold (fun (out : Bytes) (piece : Bytes) => (cat piece out)) (lit "") (project scanned pieces) : Bytes)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: scanAt, + parameters: [text : Bytes, from : Nat], + result: (Result Tok SurfaceLex::Failure), + body: (let at : Nat := (blankEnd text from); (let code : Nat := (codeAt text at); (let col0 : Bool := (if (at =? 0) then true else ((codeAt text (prim subtract at 1 : Nat)) =? 10)); (let next : Nat := (codeAt text (at + 1)); (if (if (code =? 9) then true else (code =? 13)) then (refuse at (lit "a tab or a carriage return is not part of the notation")) else (if (code =? 256) then (tokOf 21 at at col0) else (if (code =? 40) then (tokOf 0 at (at + 1) col0) else (if (code =? 41) then (tokOf 1 at (at + 1) col0) else (if (code =? 123) then (tokOf 2 at (at + 1) col0) else (if (code =? 125) then (tokOf 3 at (at + 1) col0) else (if (code =? 91) then (tokOf 4 at (at + 1) col0) else (if (code =? 93) then (tokOf 5 at (at + 1) col0) else (if (code =? 44) then (tokOf 6 at (at + 1) col0) else (if (code =? 59) then (tokOf 9 at (at + 1) col0) else (if (code =? 43) then (tokOf 12 at (at + 1) col0) else (if (code =? 58) then (if (next =? 61) then (tokOf 8 at (at + 2) col0) else (tokOf 7 at (at + 1) col0)) else (if (code =? 45) then (if (next =? 62) then (tokOf 10 at (at + 2) col0) else (tokOf 13 at (at + 1) col0)) else (if (both (code =? 61) (next =? 62)) then (tokOf 11 at (at + 2) col0) else (if (both (code =? 46) (next =? 123)) then (tokOf 14 at (at + 2) col0) else (if (code =? 34) then (match (stringEnd text at) + | Result.ok stop => (tokOf 19 at stop col0) + | Result.error problem => (ctor Result.error [Tok, SurfaceLex::Failure] problem)) else (if (both (code =? 64) (nameStartAt text (at + 1))) then (tokOf 16 at (nameEnd text (at + 1)) col0) else (if (if (code =? 64) then true else (code =? 35)) then (let stop : Nat := (digitEnd text (at + 1)); (if (stop =? (at + 1)) then (refuse at (lit "`@` and `#` are followed by a number or, after `@`, a name")) else (if (nameCharAt text stop) then (refuse stop (lit "a number ends where its text does")) else (tokOf (if (code =? 64) then 15 else 17) at stop col0)))) else (if (isDigit code) then (let stop : Nat := (digitEnd text at); (if (nameCharAt text stop) then (refuse at (lit "a name does not begin with a digit; quote it")) else (tokOf 18 at stop col0))) else (if (nameStartAt text at) then (let stop : Nat := (nameEnd text at); (tokOf 20 at (if (both ((codeAt text (prim subtract stop 1 : Nat)) =? 46) ((codeAt text stop) =? 123)) then (prim subtract stop 1 : Nat) else stop) col0)) else (refuse at (cat (cat (lit "unexpected `") (LexLeanStd.Text::sliceOr text at (width code))) (lit "`"))))))))))))))))))))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: slice, + parameters: [text : Bytes, tok : Tok], + result: Bytes, + body: (LexLeanStd.Text::sliceOr text (project tok start) (prim subtract (project tok stop) (project tok start) : Nat)), + executable: true +} + +definition{ + name: moved, + parameters: [tok : Tok], + result: Cur, + body: (record Cur {at := (project tok stop), lastEnd := (project tok stop)}), + executable: true +} + +definition{ + name: keywords, + parameters: [], + result: (List Bytes), + body: (cons (lit "fun") (cons (lit "let") (cons (lit "have") (cons (lit "proof") (cons (lit "Sort") (cons (lit "proj") (cons (lit "annot") (cons (lit "const") (cons (lit "max") (cons (lit "imax") (cons (lit "true") (cons (lit "false") (cons (lit "bool") (cons (lit "nat") (cons (lit "int") (cons (lit "str") (cons (lit "name") (nil Bytes)))))))))))))))))), + executable: true +} + +definition{ + name: expectTok, + parameters: [text : Bytes, cur : Cur, kind : Nat, what : Bytes], + result: (Result Step SurfaceLex::Failure), + body: (match (scanAt text (project cur at)) + | Result.ok tok => (if ((project tok kind) =? kind) then (ctor Result.ok [Step, SurfaceLex::Failure] (record Step {tok := tok, cur := (moved tok)})) else (ctor Result.error [Step, SurfaceLex::Failure] (failure (project tok start) (cat (lit "expected ") what)))) + | Result.error problem => (ctor Result.error [Step, SurfaceLex::Failure] problem)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: readName, + parameters: [text : Bytes, cur : Cur, what : Bytes], + result: (Result Got SurfaceLex::Failure), + body: (match (scanAt text (project cur at)) + | Result.ok tok => (let spelled : Bytes := (slice text tok); (if ((project tok kind) =? 20) then (if (listed spelled (keywords)) then (ctor Result.error [Got, SurfaceLex::Failure] (failure (project tok start) (cat (cat (lit "`") spelled) (lit "` is a keyword; quote it to write a name")))) else (ctor Result.ok [Got, SurfaceLex::Failure] (record Got {text := spelled, cur := (moved tok)}))) else (if ((project tok kind) =? 19) then (ctor Result.ok [Got, SurfaceLex::Failure] (record Got {text := (stringValue text (project tok start)), cur := (moved tok)})) else (ctor Result.error [Got, SurfaceLex::Failure] (failure (project tok start) (cat (lit "expected ") what)))))) + | Result.error problem => (ctor Result.error [Got, SurfaceLex::Failure] problem)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: binderName, + parameters: [text : Bytes, cur : Cur], + result: (Result Got SurfaceLex::Failure), + body: (match (scanAt text (project cur at)) + | Result.ok tok => (if (both ((project tok kind) =? 20) (same (slice text tok) (lit "_"))) then (ctor Result.ok [Got, SurfaceLex::Failure] (record Got {text := (lit ""), cur := (moved tok)})) else (readName text cur (lit "a binder's name"))) + | Result.error problem => (ctor Result.error [Got, SurfaceLex::Failure] problem)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: decimalOf, + parameters: [digits : Bytes], + result: Nat, + body: (project (first (prim iterate_until (fun [digits] (state : Digits) => (match (LexLeanStd.Text::byteAt digits (project state cursor)) + | Option.some byte => (ctor Option.some [Digits] (record Digits {value := ((prim multiply (project state value) 10 : Nat) + (prim subtract (prim to_nat byte : Nat) 48 : Nat)), cursor := ((project state cursor) + 1)})) + | Option.none => (ctor Option.none [Digits]))) ((prim length digits : Nat) + 1) (record Digits {value := 0, cursor := 0}) : (Digits * Bool))) value), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: tooLargeDigits, + parameters: [digits : Bytes], + result: Bool, + body: (let lead : Nat := (first (prim iterate_until (fun [digits] (position : Nat) => (match (LexLeanStd.Text::byteAt digits position) + | Option.some byte => (if ((prim to_nat byte : Nat) =? 48) then (ctor Option.some [Nat] (position + 1)) else (ctor Option.none [Nat])) + | Option.none => (ctor Option.none [Nat]))) ((prim length digits : Nat) + 1) 0 : (Nat * Bool))); (let rest : Bytes := (LexLeanStd.Text::sliceOr digits lead (prim subtract (prim length digits : Nat) lead : Nat)); (let size : Nat := (prim length rest : Nat); (if (20 (let tok : Tok := (project step tok); (let digits : Bytes := (slice text tok); (if (both (1 (ctor Result.error [GotNat, SurfaceLex::Failure] problem)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: succeeded, + parameters: [count : Nat, base : CoreJson::Level], + result: CoreJson::Level, + body: (prim iterate (fun (level : CoreJson::Level) => (ctor CoreJson::Level.LSucc level)) count base : CoreJson::Level), + executable: true +} + +definition{ + name: lfail, + parameters: [machine : LM, problem : SurfaceLex::Failure], + result: LM, + body: (record LM {cur := (project machine cur), jobs := (project machine jobs), vals := (project machine vals), error := (ctor Option.some [SurfaceLex::Failure] problem)}), + executable: true +} + +definition{ + name: lnext, + parameters: [machine : LM, cur : Cur, jobs : (List LJob), vals : (List CoreJson::Level)], + result: LM, + body: (record LM {cur := cur, jobs := jobs, vals := vals, error := (project machine error)}), + executable: true +} + +definition{ + name: lpop2, + parameters: [vals : (List CoreJson::Level), isMax : Bool], + result: (List CoreJson::Level), + body: (match vals + | List.cons right rest => (match rest + | List.cons left below => (cons (if isMax then (ctor CoreJson::Level.LMax left right) else (ctor CoreJson::Level.LIMax left right)) below) + | List.nil => vals) + | List.nil => vals), + executable: true +} + +definition{ + name: lraise, + parameters: [vals : (List CoreJson::Level), count : Nat], + result: (List CoreJson::Level), + body: (match vals | List.cons top rest => (cons (succeeded count top) rest) | List.nil => vals), + executable: true +} + +definition{ + name: deep, + parameters: [depth : Nat, at : Nat], + result: (Option SurfaceLex::Failure), + body: (if (256 (lfail machine problem) + | Result.ok tok => (match (deep depth (project tok start)) + | Option.some problem => (lfail machine problem) + | Option.none => (let kind : Nat := (project tok kind); (let spelled : Bytes := (slice text tok); (if (kind =? 18) then (match (readNumber text cur (lit "a level")) + | Result.ok got => (if (1024 (lfail machine problem)) else (if (kind =? 0) then (lnext machine (moved tok) (cons (ctor LJob.LLevel (depth + 1)) (cons (ctor LJob.LParenClose) rest)) (project machine vals)) else (if (both (kind =? 20) (if (same spelled (lit "max")) then true else (same spelled (lit "imax")))) then (match (expectTok text (moved tok) 0 (lit "`(` after `max`")) + | Result.ok step => (lnext machine (project step cur) (cons (ctor LJob.LLevel (depth + 1)) (cons (ctor LJob.LMaxComma depth (same spelled (lit "max"))) rest)) (project machine vals)) + | Result.error problem => (lfail machine problem)) else (if (if (kind =? 20) then true else (kind =? 19)) then (match (readName text cur (lit "a level")) + | Result.ok got => (lnext machine (project got cur) rest (cons (ctor CoreJson::Level.LParam (project got text)) (project machine vals))) + | Result.error problem => (lfail machine problem)) else (lfail machine (failure (project tok start) (lit "expected a universe level")))))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: levelStep, + parameters: [text : Bytes, machine : LM], + result: (Option LM), + body: (match (project machine error) + | Option.some problem => (ctor Option.none [LM]) + | Option.none => (match (project machine jobs) + | List.nil => (ctor Option.none [LM]) + | List.cons job rest => (let cur : Cur := (project machine cur); (ctor Option.some [LM] (match job + | LJob.LLevel depth => (lnext machine cur (cons (ctor LJob.LAtom depth) (cons (ctor LJob.LPlus) rest)) (project machine vals)) + | LJob.LAtom depth => (levelAtom text machine rest depth) + | LJob.LParenClose => (match (expectTok text cur 1 (lit "`)`")) + | Result.ok step => (lnext machine (project step cur) rest (project machine vals)) + | Result.error problem => (lfail machine problem)) + | LJob.LMaxComma depth isMax => (match (expectTok text cur 6 (lit "`,`")) + | Result.ok step => (lnext machine (project step cur) (cons (ctor LJob.LLevel (depth + 1)) (cons (ctor LJob.LMaxClose isMax) rest)) (project machine vals)) + | Result.error problem => (lfail machine problem)) + | LJob.LMaxClose isMax => (match (expectTok text cur 1 (lit "`)`")) + | Result.ok step => (lnext machine (project step cur) rest (lpop2 (project machine vals) isMax)) + | Result.error problem => (lfail machine problem)) + | LJob.LPlus => (match (scanAt text (project cur at)) + | Result.error problem => (lfail machine problem) + | Result.ok plus => (if ((project plus kind) =? 12) then (let after : Cur := (moved plus); (match (scanAt text (project after at)) + | Result.error problem => (lfail machine problem) + | Result.ok next => (match (readNumber text after (lit "a number after `+`")) + | Result.ok got => (if (1024 (lfail machine problem)))) else (lnext machine cur rest (project machine vals))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: readLevel, + parameters: [text : Bytes, cur : Cur, depth : Nat], + result: (Result GotLevel SurfaceLex::Failure), + body: (let final : LM := (first (prim iterate_until (fun [text] (machine : LM) => (levelStep text machine)) ((prim multiply (prim length text : Nat) 4 : Nat) + 16) (record LM {cur := cur, jobs := (cons (ctor LJob.LLevel depth) (nil LJob)), vals := (nil CoreJson::Level), error := (ctor Option.none [SurfaceLex::Failure])}) : (LM * Bool))); (match (project final error) + | Option.some problem => (ctor Result.error [GotLevel, SurfaceLex::Failure] problem) + | Option.none => (match (project final vals) + | List.cons level rest => (ctor Result.ok [GotLevel, SurfaceLex::Failure] (record GotLevel {level := level, cur := (project final cur)})) + | List.nil => (ctor Result.error [GotLevel, SurfaceLex::Failure] (failure (project cur at) (lit "expected a universe level")))))), + axioms: [Quot.sound, propext], + executable: true +} + +structure{ + name: LS, + type_parameters: [], + parameters: [], + fields: [ + {name: levels, type: (List CoreJson::Level)}, + {name: cur, type: Cur}, + {name: done, type: Bool}, + {name: error, type: (Option SurfaceLex::Failure)} + ] +} + +definition{ + name: lsFail, + parameters: [state : LS, problem : SurfaceLex::Failure], + result: LS, + body: (record LS {levels := (project state levels), cur := (project state cur), done := true, error := (ctor Option.some [SurfaceLex::Failure] problem)}), + executable: true +} + +definition{ + name: levelsStep, + parameters: [text : Bytes, depth : Nat, state : LS], + result: (Option LS), + body: (if (project state done) then (ctor Option.none [LS]) else (ctor Option.some [LS] (match (readLevel text (project state cur) (depth + 1)) + | Result.error problem => (lsFail state problem) + | Result.ok got => (let levels : (List CoreJson::Level) := (cons (project got level) (project state levels)); (match (scanAt text (project (project got cur) at)) + | Result.error problem => (lsFail state problem) + | Result.ok comma => (if ((project comma kind) =? 6) then (record LS {levels := levels, cur := (moved comma), done := false, error := (ctor Option.none [SurfaceLex::Failure])}) else (record LS {levels := levels, cur := (project got cur), done := true, error := (ctor Option.none [SurfaceLex::Failure])}))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: readLevels, + parameters: [text : Bytes, cur : Cur, depth : Nat], + result: (Result GotLevels SurfaceLex::Failure), + body: (match (scanAt text (project cur at)) + | Result.error problem => (ctor Result.error [GotLevels, SurfaceLex::Failure] problem) + | Result.ok tok => (if ((project tok kind) =? 14) then (let final : LS := (first (prim iterate_until (fun [depth, text] (state : LS) => (levelsStep text depth state)) ((prim length text : Nat) + 2) (record LS {levels := (nil CoreJson::Level), cur := (moved tok), done := false, error := (ctor Option.none [SurfaceLex::Failure])}) : (LS * Bool))); (match (project final error) + | Option.some problem => (ctor Result.error [GotLevels, SurfaceLex::Failure] problem) + | Option.none => (match (expectTok text (project final cur) 3 (lit "`}` after the levels")) + | Result.ok step => (ctor Result.ok [GotLevels, SurfaceLex::Failure] (record GotLevels {levels := (LexLeanStd.Order::reverseList [CoreJson::Level] (project final levels)), cur := (project step cur)})) + | Result.error problem => (ctor Result.error [GotLevels, SurfaceLex::Failure] problem)))) else (ctor Result.ok [GotLevels, SurfaceLex::Failure] (record GotLevels {levels := (nil CoreJson::Level), cur := cur})))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: mfail, + parameters: [machine : M, problem : SurfaceLex::Failure], + result: M, + body: (record M {cur := (project machine cur), jobs := (project machine jobs), vals := (project machine vals), held := (project machine held), nodes := (project machine nodes), count := (project machine count), proof := (project machine proof), scope := (project machine scope), labels := (project machine labels), error := (ctor Option.some [SurfaceLex::Failure] problem)}), + executable: true +} + +definition{ + name: remake, + parameters: [ + machine : M, + cur : Cur, + jobs : (List Job), + vals : (List Nat), + held : (List Held), + scope : (List Bytes) + ], + result: M, + body: (record M {cur := cur, jobs := jobs, vals := vals, held := held, nodes := (project machine nodes), count := (project machine count), proof := (project machine proof), scope := scope, labels := (project machine labels), error := (project machine error)}), + executable: true +} + +definition{ + name: advance, + parameters: [machine : M, cur : Cur, jobs : (List Job)], + result: M, + body: (remake machine cur jobs (project machine vals) (project machine held) (project machine scope)), + executable: true +} + +definition{ + name: pushNode, + parameters: [ + machine : M, + cur : Cur, + jobs : (List Job), + vals : (List Nat), + held : (List Held), + scope : (List Bytes), + node : CoreJson::Node + ], + result: M, + body: (record M {cur := cur, jobs := jobs, vals := (cons (project machine count) vals), held := held, nodes := (cons node (project machine nodes)), count := ((project machine count) + 1), proof := (project machine proof), scope := scope, labels := (project machine labels), error := (project machine error)}), + executable: true +} + +definition{ + name: pushVal, + parameters: [machine : M, cur : Cur, jobs : (List Job), value : Nat], + result: M, + body: (remake machine cur jobs (cons value (project machine vals)) (project machine held) (project machine scope)), + executable: true +} + +definition{ + name: binderOf, + parameters: [info : Nat], + result: CoreJson::Binder, + body: (if (info =? 0) then (ctor CoreJson::Binder.BExplicit) else (if (info =? 1) then (ctor CoreJson::Binder.BImplicit) else (if (info =? 2) then (ctor CoreJson::Binder.BStrict) else (ctor CoreJson::Binder.BInstance)))), + executable: true +} + +definition{ + name: closerOf, + parameters: [info : Nat], + result: Nat, + body: (if (info =? 0) then 1 else (if (info =? 3) then 5 else 3)), + executable: true +} + +definition{ + name: heldName, + parameters: [held : (List Held)], + result: Bytes, + body: (match held + | List.cons top rest => (match top + | Held.HBinder info label => label + | Held.HLet label dependent => label + | Held.HProj label structure position => (lit "") + | Held.HMdata entries => (lit "") + | Held.HProof before => (lit "")) + | List.nil => (lit "")), + executable: true +} + +definition{ + name: heldInfo, + parameters: [held : (List Held)], + result: Nat, + body: (match held + | List.cons top rest => (match top + | Held.HBinder info label => info + | Held.HLet label dependent => 0 + | Held.HProj label structure position => 0 + | Held.HMdata entries => 0 + | Held.HProof before => 0) + | List.nil => 0), + executable: true +} + +definition{ + name: heldRest, + parameters: [held : (List Held)], + result: (List Held), + body: (match held | List.cons top rest => rest | List.nil => held), + executable: true +} + +definition{ + name: popped, + parameters: [vals : (List Nat)], + result: (List Nat), + body: (match vals | List.cons top rest => rest | List.nil => vals), + executable: true +} + +definition{ + name: topOr, + parameters: [vals : (List Nat)], + result: Nat, + body: (match vals | List.cons top rest => top | List.nil => 0), + executable: true +} + +definition{ + name: scopeRest, + parameters: [scope : (List Bytes)], + result: (List Bytes), + body: (match scope | List.cons top rest => rest | List.nil => scope), + executable: true +} + +definition{ + name: resolveName, + parameters: [scope : (List Bytes), word : Bytes], + result: (Option Nat), + body: (project (prim list_fold (fun [word] (state : Find) (label : Bytes) => (record Find {seen := ((project state seen) + 1), found := (match (project state found) + | Option.some index => (project state found) + | Option.none => (if (both (0 (match found + | Option.some index => found + | Option.none => (if (same (project entry label) word) then (ctor Option.some [Nat] (project entry node)) else (ctor Option.none [Nat])))) (ctor Option.none [Nat]) labels : (Option Nat)), + executable: true +} + +definition{ + name: scalarStep, + parameters: [text : Bytes, state : Scalars], + result: (Option Scalars), + body: (let position : Nat := (project state cursor); (let lead : Nat := (codeAt text position); (if (256 <=? lead) then (ctor Option.none [Scalars]) else (let size : Nat := (width lead); (let tail : Nat := (if (size =? 1) then lead else (if (size =? 2) then (prim subtract lead 192 : Nat) else (if (size =? 3) then (prim subtract lead 224 : Nat) else (prim subtract lead 240 : Nat)))); (ctor Option.some [Scalars] (record Scalars {cursor := (position + size), items := (cons (project (prim iterate (fun [position, text] (inner : Digits) => (record Digits {value := ((prim multiply (project inner value) 64 : Nat) + (prim subtract (codeAt text (position + (project inner cursor))) 128 : Nat)), cursor := ((project inner cursor) + 1)})) (prim subtract size 1 : Nat) (record Digits {value := tail, cursor := 1}) : Digits) value) (project state items))}))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: scalarsOf, + parameters: [text : Bytes], + result: (List Nat), + body: (LexLeanStd.Order::reverseList [Nat] (project (first (prim iterate_until (fun [text] (state : Scalars) => (scalarStep text state)) ((prim length text : Nat) + 1) (record Scalars {cursor := 0, items := (nil Nat)}) : (Scalars * Bool))) items)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: atBinder, + parameters: [text : Bytes, cur : Cur], + result: (Result Bool SurfaceLex::Failure), + body: (match (scanAt text (project cur at)) + | Result.error problem => (ctor Result.error [Bool, SurfaceLex::Failure] problem) + | Result.ok tok => (if (if ((project tok kind) =? 2) then true else ((project tok kind) =? 4)) then (ctor Result.ok [Bool, SurfaceLex::Failure] true) else (if ((project tok kind) =? 0) then (match (scanAt text (project tok stop)) + | Result.error problem => (ctor Result.error [Bool, SurfaceLex::Failure] problem) + | Result.ok following => (if (if ((project following kind) =? 20) then true else ((project following kind) =? 19)) then (match (scanAt text (project following stop)) + | Result.error problem => (ctor Result.error [Bool, SurfaceLex::Failure] problem) + | Result.ok third => (ctor Result.ok [Bool, SurfaceLex::Failure] ((project third kind) =? 7))) else (ctor Result.ok [Bool, SurfaceLex::Failure] false))) else (ctor Result.ok [Bool, SurfaceLex::Failure] false)))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: atAtom, + parameters: [text : Bytes, cur : Cur], + result: (Result Bool SurfaceLex::Failure), + body: (match (scanAt text (project cur at)) + | Result.error problem => (ctor Result.error [Bool, SurfaceLex::Failure] problem) + | Result.ok tok => (let kind : Nat := (project tok kind); (ctor Result.ok [Bool, SurfaceLex::Failure] (if (project tok col0) then false else (if (if (kind =? 0) then true else (both (15 <=? kind) (kind <=? 19))) then true else (if (kind =? 20) then (if (listed (slice text tok) (cons (lit "fun") (cons (lit "let") (cons (lit "have") (cons (lit "Sort") (cons (lit "proj") (cons (lit "annot") (nil Bytes)))))))) then false else true) else false)))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: readDigits, + parameters: [text : Bytes, cur : Cur, signed : Bool], + result: (Result Got SurfaceLex::Failure), + body: (match (scanAt text (project cur at)) + | Result.error problem => (ctor Result.error [Got, SurfaceLex::Failure] problem) + | Result.ok tok => (let kind : Nat := (project tok kind); (if (kind =? 19) then (ctor Result.ok [Got, SurfaceLex::Failure] (record Got {text := (stringValue text (project tok start)), cur := (moved tok)})) else (if (both (kind =? 13) signed) then (match (expectTok text (moved tok) 18 (lit "a number after `-`")) + | Result.error problem => (ctor Result.error [Got, SurfaceLex::Failure] problem) + | Result.ok step => (let digits : Bytes := (slice text (project step tok)); (if (if (both (1 (ctor Result.error [(CoreJson::Meta * Cur), SurfaceLex::Failure] problem) + | Result.ok flag => (if (both ((project flag kind) =? 20) (same (slice text flag) (lit "true"))) then (ctor Result.ok [(CoreJson::Meta * Cur), SurfaceLex::Failure] ((ctor CoreJson::Meta.MBool true), (moved flag))) else (if (both ((project flag kind) =? 20) (same (slice text flag) (lit "false"))) then (ctor Result.ok [(CoreJson::Meta * Cur), SurfaceLex::Failure] ((ctor CoreJson::Meta.MBool false), (moved flag))) else (ctor Result.error [(CoreJson::Meta * Cur), SurfaceLex::Failure] (failure (project flag start) (lit "expected `true` or `false`")))))) else (if (if (same word (lit "nat")) then true else (same word (lit "int"))) then (match (readDigits text after (same word (lit "int"))) + | Result.error problem => (ctor Result.error [(CoreJson::Meta * Cur), SurfaceLex::Failure] problem) + | Result.ok got => (ctor Result.ok [(CoreJson::Meta * Cur), SurfaceLex::Failure] ((if (same word (lit "int")) then (ctor CoreJson::Meta.MInt (project got text)) else (ctor CoreJson::Meta.MNat (project got text))), (project got cur)))) else (if (same word (lit "str")) then (match (expectTok text after 19 (lit "a string")) + | Result.error problem => (ctor Result.error [(CoreJson::Meta * Cur), SurfaceLex::Failure] problem) + | Result.ok step => (ctor Result.ok [(CoreJson::Meta * Cur), SurfaceLex::Failure] ((ctor CoreJson::Meta.MStr (stringValue text (project (project step tok) start))), (project step cur)))) else (if (same word (lit "name")) then (match (readName text after (lit "a name")) + | Result.error problem => (ctor Result.error [(CoreJson::Meta * Cur), SurfaceLex::Failure] problem) + | Result.ok got => (ctor Result.ok [(CoreJson::Meta * Cur), SurfaceLex::Failure] ((ctor CoreJson::Meta.MName (project got text)), (project got cur)))) else (ctor Result.error [(CoreJson::Meta * Cur), SurfaceLex::Failure] (failure (project tag start) (cat (cat (lit "`") word) (lit "` is not the type of an annotation value")))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: annStep, + parameters: [text : Bytes, state : Ann], + result: (Option Ann), + body: (if (project state done) then (ctor Option.none [Ann]) else (ctor Option.some [Ann] (match (scanAt text (project (project state cur) at)) + | Result.error problem => (annFail state problem) + | Result.ok peeked => (if ((project peeked kind) =? 5) then (record Ann {entries := (project state entries), cur := (project state cur), done := true, error := (ctor Option.none [SurfaceLex::Failure])}) else (match (readName text (project state cur) (lit "an entry's key")) + | Result.error problem => (annFail state problem) + | Result.ok key => (match (expectTok text (project key cur) 8 (lit "`:=` after the key")) + | Result.error problem => (annFail state problem) + | Result.ok step => (match (scanAt text (project (project step cur) at)) + | Result.error problem => (annFail state problem) + | Result.ok tag => (if ((project tag kind) =? 20) then (match (annValue text (project step cur) tag) + | Result.error problem => (annFail state problem) + | Result.ok valued => (let entries : (List CoreJson::Entry) := (cons (record CoreJson::Entry {key := (project key text), value := (first valued)}) (project state entries)); (match (scanAt text (project (second valued) at)) + | Result.error problem => (annFail state problem) + | Result.ok comma => (if ((project comma kind) =? 6) then (record Ann {entries := entries, cur := (moved comma), done := false, error := (ctor Option.none [SurfaceLex::Failure])}) else (record Ann {entries := entries, cur := (second valued), done := true, error := (ctor Option.none [SurfaceLex::Failure])}))))) else (annFail state (failure (project tag start) (lit "expected the type of the value"))))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: readAnnotation, + parameters: [text : Bytes, cur : Cur], + result: (Result ((List CoreJson::Entry) * Cur) SurfaceLex::Failure), + body: (match (expectTok text cur 4 (lit "`[` after `annot`")) + | Result.error problem => (ctor Result.error [((List CoreJson::Entry) * Cur), SurfaceLex::Failure] problem) + | Result.ok opened => (let final : Ann := (first (prim iterate_until (fun [text] (state : Ann) => (annStep text state)) ((prim length text : Nat) + 2) (record Ann {entries := (nil CoreJson::Entry), cur := (project opened cur), done := false, error := (ctor Option.none [SurfaceLex::Failure])}) : (Ann * Bool))); (match (project final error) + | Option.some problem => (ctor Result.error [((List CoreJson::Entry) * Cur), SurfaceLex::Failure] problem) + | Option.none => (match (expectTok text (project final cur) 5 (lit "`,` or `]`")) + | Result.error problem => (ctor Result.error [((List CoreJson::Entry) * Cur), SurfaceLex::Failure] problem) + | Result.ok closed => (ctor Result.ok [((List CoreJson::Entry) * Cur), SurfaceLex::Failure] ((LexLeanStd.Order::reverseList [CoreJson::Entry] (project final entries)), (project closed cur))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: termJob, + parameters: [text : Bytes, machine : M, rest : (List Job), depth : Nat], + result: M, + body: (let cur : Cur := (project machine cur); (match (scanAt text (project cur at)) + | Result.error problem => (mfail machine problem) + | Result.ok tok => (match (deep depth (project tok start)) + | Option.some problem => (mfail machine problem) + | Option.none => (let word : Bytes := (if ((project tok kind) =? 20) then (slice text tok) else (lit "")); (if (same word (lit "fun")) then (advance machine (moved tok) (cons (ctor Job.JBinder depth) (cons (ctor Job.JFunArrow depth) rest))) else (if (if (same word (lit "let")) then true else (same word (lit "have"))) then (match (binderName text (moved tok)) + | Result.error problem => (mfail machine problem) + | Result.ok named => (match (expectTok text (project named cur) 7 (lit "`:` after the name")) + | Result.error problem => (mfail machine problem) + | Result.ok step => (remake machine (project step cur) (cons (ctor Job.JTerm (depth + 1)) (cons (ctor Job.JLetColonEq depth) rest)) (project machine vals) (cons (ctor Held.HLet (project named text) (same word (lit "have"))) (project machine held)) (project machine scope)))) else (match (atBinder text cur) + | Result.error problem => (mfail machine problem) + | Result.ok binder => (if binder then (advance machine cur (cons (ctor Job.JBinder depth) (cons (ctor Job.JForallArrow depth) rest))) else (advance machine cur (cons (ctor Job.JPrefix depth) (cons (ctor Job.JHeadArrow depth) rest))))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: binderJob, + parameters: [text : Bytes, machine : M, rest : (List Job), depth : Nat], + result: M, + body: (let cur : Cur := (project machine cur); (match (scanAt text (project cur at)) + | Result.error problem => (mfail machine problem) + | Result.ok opened => (let kind : Nat := (project opened kind); (let after : Cur := (moved opened); (let chosen : (Result (Nat * Cur) SurfaceLex::Failure) := (if (kind =? 0) then (ctor Result.ok [(Nat * Cur), SurfaceLex::Failure] (0, after)) else (if (kind =? 4) then (ctor Result.ok [(Nat * Cur), SurfaceLex::Failure] (3, after)) else (if (kind =? 2) then (match (scanAt text (project after at)) + | Result.error problem => (ctor Result.error [(Nat * Cur), SurfaceLex::Failure] problem) + | Result.ok following => (if ((project following kind) =? 2) then (ctor Result.ok [(Nat * Cur), SurfaceLex::Failure] (2, (moved following))) else (ctor Result.ok [(Nat * Cur), SurfaceLex::Failure] (1, after)))) else (ctor Result.error [(Nat * Cur), SurfaceLex::Failure] (failure (project opened start) (lit "expected a binder")))))); (match chosen + | Result.error problem => (mfail machine problem) + | Result.ok picked => (match (binderName text (second picked)) + | Result.error problem => (mfail machine problem) + | Result.ok named => (match (expectTok text (project named cur) 7 (lit "`:` after the binder's name")) + | Result.error problem => (mfail machine problem) + | Result.ok step => (remake machine (project step cur) (cons (ctor Job.JTerm (depth + 1)) (cons (ctor Job.JBinderClose) rest)) (project machine vals) (cons (ctor Held.HBinder (first picked) (project named text)) (project machine held)) (project machine scope)))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: binderCloseJob, + parameters: [text : Bytes, machine : M, rest : (List Job)], + result: M, + body: (let info : Nat := (heldInfo (project machine held)); (match (expectTok text (project machine cur) (closerOf info) (lit "the binder's closing bracket")) + | Result.error problem => (mfail machine problem) + | Result.ok step => (if (info =? 2) then (match (expectTok text (project step cur) 3 (lit "`}}`")) + | Result.error problem => (mfail machine problem) + | Result.ok again => (advance machine (project again cur) rest)) else (advance machine (project step cur) rest)))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: arrowJob, + parameters: [ + text : Bytes, + machine : M, + rest : (List Job), + depth : Nat, + kind : Nat, + what : Bytes, + done : Job + ], + result: M, + body: (match (expectTok text (project machine cur) kind what) + | Result.error problem => (mfail machine problem) + | Result.ok step => (remake machine (project step cur) (cons (ctor Job.JTerm (depth + 1)) (cons done rest)) (project machine vals) (project machine held) (cons (heldName (project machine held)) (project machine scope)))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: binderDone, + parameters: [machine : M, rest : (List Job), lambda : Bool], + result: M, + body: (let held : (List Held) := (project machine held); (let body : Nat := (topOr (project machine vals)); (let bound : Nat := (topOr (popped (project machine vals))); (let label : Bytes := (heldName held); (let info : CoreJson::Binder := (binderOf (heldInfo held)); (pushNode machine (project machine cur) rest (popped (popped (project machine vals))) (heldRest held) (scopeRest (project machine scope)) (if lambda then (ctor CoreJson::Node.NLambda label info bound body) else (ctor CoreJson::Node.NForall label info bound body)))))))), + executable: true +} + +definition{ + name: letDone, + parameters: [machine : M, rest : (List Job)], + result: M, + body: (let vals : (List Nat) := (project machine vals); (let body : Nat := (topOr vals); (let bound : Nat := (topOr (popped vals)); (let typed : Nat := (topOr (popped (popped vals))); (match (project machine held) + | List.cons top below => (match top + | Held.HLet label dependent => (pushNode machine (project machine cur) rest (popped (popped (popped vals))) below (scopeRest (project machine scope)) (ctor CoreJson::Node.NLet label typed bound body dependent)) + | Held.HBinder info label => machine + | Held.HProj label structure position => machine + | Held.HMdata entries => machine + | Held.HProof before => machine) + | List.nil => machine))))), + executable: true +} + +definition{ + name: prefixJob, + parameters: [text : Bytes, machine : M, rest : (List Job), depth : Nat], + result: M, + body: (let cur : Cur := (project machine cur); (match (scanAt text (project cur at)) + | Result.error problem => (mfail machine problem) + | Result.ok tok => (let word : Bytes := (if ((project tok kind) =? 20) then (slice text tok) else (lit "")); (if (same word (lit "Sort")) then (match (readLevel text (moved tok) (depth + 1)) + | Result.error problem => (mfail machine problem) + | Result.ok got => (pushNode machine (project got cur) rest (project machine vals) (project machine held) (project machine scope) (ctor CoreJson::Node.NSort (project got level)))) else (if (same word (lit "proj")) then (match (readName text (moved tok) (lit "a structure name")) + | Result.error problem => (mfail machine problem) + | Result.ok structure => (match (readName text (project structure cur) (lit "a projection name")) + | Result.error problem => (mfail machine problem) + | Result.ok projection => (match (readNumber text (project projection cur) (lit "a field index")) + | Result.error problem => (mfail machine problem) + | Result.ok field => (remake machine (project field cur) (cons (ctor Job.JAtom (depth + 1)) (cons (ctor Job.JProjDone) rest)) (project machine vals) (cons (ctor Held.HProj (project projection text) (project structure text) (project field value)) (project machine held)) (project machine scope))))) else (if (same word (lit "annot")) then (match (readAnnotation text (moved tok)) + | Result.error problem => (mfail machine problem) + | Result.ok annotated => (remake machine (second annotated) (cons (ctor Job.JAtom (depth + 1)) (cons (ctor Job.JMdataDone) rest)) (project machine vals) (cons (ctor Held.HMdata (first annotated)) (project machine held)) (project machine scope))) else (advance machine cur (cons (ctor Job.JAtom depth) (cons (ctor Job.JAppLoop depth) rest))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: heldDone, + parameters: [machine : M, rest : (List Job)], + result: M, + body: (let inner : Nat := (topOr (project machine vals)); (let below : (List Nat) := (popped (project machine vals)); (match (project machine held) + | List.cons top under => (match top + | Held.HProj label structure position => (pushNode machine (project machine cur) rest below under (project machine scope) (ctor CoreJson::Node.NProj label structure position inner)) + | Held.HMdata entries => (pushNode machine (project machine cur) rest below under (project machine scope) (ctor CoreJson::Node.NMdata entries inner)) + | Held.HBinder info label => machine + | Held.HLet label dependent => machine + | Held.HProof before => machine) + | List.nil => machine))), + executable: true +} + +definition{ + name: numberIn, + parameters: [text : Bytes, tok : Tok], + result: Nat, + body: (decimalOf (LexLeanStd.Text::sliceOr text ((project tok start) + 1) (prim subtract (prim subtract (project tok stop) (project tok start) : Nat) 1 : Nat))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: wordAtom, + parameters: [text : Bytes, machine : M, rest : (List Job), depth : Nat, tok : Tok, word : Bytes], + result: M, + body: (let after : Cur := (moved tok); (match (resolveName (project machine scope) word) + | Option.some index => (match (scanAt text (project after at)) + | Result.error problem => (mfail machine problem) + | Result.ok next => (if ((project next kind) =? 14) then (mfail machine (failure (project tok stop) (cat (cat (lit "`") word) (lit "` is a bound variable and has no universe levels")))) else (pushNode machine after rest (project machine vals) (project machine held) (project machine scope) (ctor CoreJson::Node.NBVar index)))) + | Option.none => (match (readLevels text after depth) + | Result.error problem => (mfail machine problem) + | Result.ok got => (pushNode machine (project got cur) rest (project machine vals) (project machine held) (project machine scope) (ctor CoreJson::Node.NConst word (project got levels)))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: nameAtom, + parameters: [text : Bytes, machine : M, rest : (List Job), depth : Nat, tok : Tok], + result: M, + body: (let word : Bytes := (slice text tok); (let after : Cur := (moved tok); (if (same word (lit "proof")) then (match (expectTok text after 0 (lit "`(` after `proof`")) + | Result.error problem => (mfail machine problem) + | Result.ok step => (remake machine (project step cur) (cons (ctor Job.JTerm (depth + 1)) (cons (ctor Job.JProofDone) rest)) (project machine vals) (cons (ctor Held.HProof (project machine count)) (project machine held)) (project machine scope))) else (if (same word (lit "nat")) then (match (expectTok text after 19 (lit "a quoted number after `nat`")) + | Result.error problem => (mfail machine problem) + | Result.ok step => (pushNode machine (project step cur) rest (project machine vals) (project machine held) (project machine scope) (ctor CoreJson::Node.NNat (stringValue text (project (project step tok) start))))) else (if (same word (lit "const")) then (match (expectTok text after 19 (lit "a quoted name after `const`")) + | Result.error problem => (mfail machine problem) + | Result.ok step => (match (readLevels text (project step cur) depth) + | Result.error problem => (mfail machine problem) + | Result.ok got => (pushNode machine (project got cur) rest (project machine vals) (project machine held) (project machine scope) (ctor CoreJson::Node.NConst (stringValue text (project (project step tok) start)) (project got levels))))) else (if (listed word (keywords)) then (mfail machine (failure (project tok start) (cat (cat (lit "`") word) (lit "` is a keyword; a term that begins with it is written in parentheses")))) else (if (same word (lit "_")) then (mfail machine (failure (project tok start) (lit "`_` is the name of a binder, not a constant"))) else (wordAtom text machine rest depth tok word)))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: atomJob, + parameters: [text : Bytes, machine : M, rest : (List Job), depth : Nat], + result: M, + body: (match (scanAt text (project (project machine cur) at)) + | Result.error problem => (mfail machine problem) + | Result.ok tok => (match (deep depth (project tok start)) + | Option.some problem => (mfail machine problem) + | Option.none => (let kind : Nat := (project tok kind); (let after : Cur := (moved tok); (if (kind =? 15) then (let target : Nat := (if (numberTooLarge text tok) then 0 else (numberIn text tok)); (if (numberTooLarge text tok) then (mfail machine (failure (project tok start) (lit "the number is too large"))) else (if ((project machine count) <=? target) then (mfail machine (failure (project tok start) (cat (cat (lit "`@") (LexLeanStd.Numeral::decimal target)) (lit "` is not a node that has been written")))) else (pushVal machine after rest target)))) else (if (kind =? 16) then (let label : Bytes := (LexLeanStd.Text::sliceOr text ((project tok start) + 1) (prim subtract (prim subtract (project tok stop) (project tok start) : Nat) 1 : Nat)); (match (labelOf (project machine labels) label) + | Option.some target => (pushVal machine after rest target) + | Option.none => (mfail machine (failure (project tok start) (cat (cat (lit "no statement written before this is named `@") label) (lit "`")))))) else (if (kind =? 17) then (let index : Nat := (if (numberTooLarge text tok) then 0 else (numberIn text tok)); (if (numberTooLarge text tok) then (mfail machine (failure (project tok start) (lit "the number is too large"))) else (pushNode machine after rest (project machine vals) (project machine held) (project machine scope) (ctor CoreJson::Node.NBVar index)))) else (if (kind =? 18) then (let digits : Bytes := (slice text tok); (if (both (1 (mfail machine problem) + | Result.ok step => (let inner : Nat := (topOr (project machine vals)); (match (project machine held) + | List.cons top under => (match top + | Held.HProof before => (if (if (both ((inner + 1) =? (project machine count)) (before <=? inner)) then false else true) then (mfail machine (failure (project (project step tok) start) (lit "`proof` marks a node written inside it, not a reference"))) else (record M {cur := (project step cur), jobs := rest, vals := (project machine vals), held := under, nodes := (project machine nodes), count := (project machine count), proof := (cons inner (project machine proof)), scope := (project machine scope), labels := (project machine labels), error := (project machine error)})) + | Held.HBinder info label => machine + | Held.HLet label dependent => machine + | Held.HProj label structure position => machine + | Held.HMdata entries => machine) + | List.nil => machine))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: termStep, + parameters: [text : Bytes, machine : M], + result: (Option M), + body: (match (project machine error) + | Option.some problem => (ctor Option.none [M]) + | Option.none => (match (project machine jobs) + | List.nil => (ctor Option.none [M]) + | List.cons job rest => (let cur : Cur := (project machine cur); (ctor Option.some [M] (match job + | Job.JTerm depth => (termJob text machine rest depth) + | Job.JBinder depth => (binderJob text machine rest depth) + | Job.JBinderClose => (binderCloseJob text machine rest) + | Job.JFunArrow depth => (arrowJob text machine rest depth 11 (lit "`=>` after the binder") (ctor Job.JFunDone)) + | Job.JFunDone => (binderDone machine rest true) + | Job.JForallArrow depth => (arrowJob text machine rest depth 10 (lit "`->` after the binder") (ctor Job.JForallDone)) + | Job.JForallDone => (binderDone machine rest false) + | Job.JLetColonEq depth => (match (expectTok text cur 8 (lit "`:=`")) + | Result.error problem => (mfail machine problem) + | Result.ok step => (advance machine (project step cur) (cons (ctor Job.JTerm (depth + 1)) (cons (ctor Job.JLetSemi depth) rest)))) + | Job.JLetSemi depth => (arrowJob text machine rest depth 9 (lit "`;` after the value") (ctor Job.JLetDone)) + | Job.JLetDone => (letDone machine rest) + | Job.JHeadArrow depth => (match (scanAt text (project cur at)) + | Result.error problem => (mfail machine problem) + | Result.ok tok => (if ((project tok kind) =? 10) then (remake machine (moved tok) (cons (ctor Job.JTerm (depth + 1)) (cons (ctor Job.JArrowDone) rest)) (project machine vals) (project machine held) (cons (lit "") (project machine scope))) else (advance machine cur rest))) + | Job.JArrowDone => (let body : Nat := (topOr (project machine vals)); (let head : Nat := (topOr (popped (project machine vals))); (pushNode machine cur rest (popped (popped (project machine vals))) (project machine held) (scopeRest (project machine scope)) (ctor CoreJson::Node.NForall (lit "") (ctor CoreJson::Binder.BExplicit) head body)))) + | Job.JPrefix depth => (prefixJob text machine rest depth) + | Job.JAppLoop depth => (match (atAtom text cur) + | Result.error problem => (mfail machine problem) + | Result.ok more => (if more then (advance machine cur (cons (ctor Job.JAtom (depth + 1)) (cons (ctor Job.JAppStep depth) rest))) else (advance machine cur rest))) + | Job.JAppStep depth => (let argument : Nat := (topOr (project machine vals)); (let function : Nat := (topOr (popped (project machine vals))); (pushNode machine cur (cons (ctor Job.JAppLoop depth) rest) (popped (popped (project machine vals))) (project machine held) (project machine scope) (ctor CoreJson::Node.NApp function argument)))) + | Job.JAtom depth => (atomJob text machine rest depth) + | Job.JClose => (match (expectTok text cur 1 (lit "`)`")) + | Result.error problem => (mfail machine problem) + | Result.ok step => (advance machine (project step cur) rest)) + | Job.JProofDone => (proofDone text machine rest) + | Job.JProjDone => (heldDone machine rest) + | Job.JMdataDone => (heldDone machine rest)))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: runTerm, + parameters: [text : Bytes, machine : M], + result: M, + body: (first (prim iterate_until (fun [text] (state : M) => (termStep text state)) ((prim multiply (prim length text : Nat) 8 : Nat) + 16) (advance machine (project machine cur) (cons (ctor Job.JTerm 0) (nil Job))) : (M * Bool))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: statement, + parameters: [text : Bytes, machine : M], + result: M, + body: (match (scanAt text (project (project machine cur) at)) + | Result.error problem => (mfail machine problem) + | Result.ok label => (let isRef : Bool := ((project label kind) =? 15); (let target : Nat := (if (numberTooLarge text label) then 0 else (numberIn text label)); (let named : Bytes := (LexLeanStd.Text::sliceOr text ((project label start) + 1) (prim subtract (prim subtract (project label stop) (project label start) : Nat) 1 : Nat)); (if (both isRef (numberTooLarge text label)) then (mfail machine (failure (project label start) (lit "the number is too large"))) else (match (expectTok text (moved label) 8 (lit "`:=` after the label")) + | Result.error problem => (mfail machine problem) + | Result.ok step => (let before : Nat := (project machine count); (let ran : M := (runTerm text (advance machine (project step cur) (nil Job))); (match (project ran error) + | Option.some problem => ran + | Option.none => (let defined : Nat := (topOr (project ran vals)); (let cleared : M := (remake ran (project ran cur) (nil Job) (popped (project ran vals)) (project ran held) (project ran scope)); (if (defined true + | Option.none => false)) then (mfail cleared (failure (project label start) (cat (cat (lit "`@") named) (lit "` names two statements")))) else (let labelled : M := (if isRef then cleared else (record M {cur := (project cleared cur), jobs := (project cleared jobs), vals := (project cleared vals), held := (project cleared held), nodes := (project cleared nodes), count := (project cleared count), proof := (project cleared proof), scope := (project cleared scope), labels := (cons (record Lbl {label := named, node := defined}) (project cleared labels)), error := (project cleared error)})); (match (scanAt text (project (project labelled cur) at)) + | Result.error problem => (mfail labelled problem) + | Result.ok after => (if (both (if ((project after kind) =? 21) then false else true) (if (project after col0) then false else true)) then (mfail labelled (failure (project after start) (lit "a statement ends at the end of its line"))) else labelled)))))))))))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: braced, + parameters: [cursor : Nat, depth : Nat, result : (Option (Result Nat SurfaceLex::Failure))], + result: (Option Braces), + body: (ctor Option.some [Braces] (record Braces {cursor := cursor, depth := depth, result := result})), + executable: true +} + +definition{ + name: braceStep, + parameters: [text : Bytes, start : Nat, state : Braces], + result: (Option Braces), + body: (match (project state result) + | Option.some done => (ctor Option.none [Braces]) + | Option.none => (let position : Nat := (project state cursor); (let depth : Nat := (project state depth); (let code : Nat := (codeAt text position); (if (code =? 256) then (braced position depth (ctor Option.some [(Result Nat SurfaceLex::Failure)] (ctor Result.error [Nat, SurfaceLex::Failure] (failure start (lit "the braces of the declaration are not closed"))))) else (if (code =? 34) then (match (stringEnd text position) + | Result.error problem => (braced position depth (ctor Option.some [(Result Nat SurfaceLex::Failure)] (ctor Result.error [Nat, SurfaceLex::Failure] problem))) + | Result.ok stop => (braced stop depth (ctor Option.none [(Result Nat SurfaceLex::Failure)]))) else (if (code =? 123) then (braced (position + 1) (depth + 1) (ctor Option.none [(Result Nat SurfaceLex::Failure)])) else (if (code =? 125) then (if (depth =? 1) then (braced (position + 1) 0 (ctor Option.some [(Result Nat SurfaceLex::Failure)] (ctor Result.ok [Nat, SurfaceLex::Failure] (position + 1)))) else (braced (position + 1) (prim subtract depth 1 : Nat) (ctor Option.none [(Result Nat SurfaceLex::Failure)]))) else (braced (position + 1) depth (ctor Option.none [(Result Nat SurfaceLex::Failure)])))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: membersEnd, + parameters: [text : Bytes, start : Nat], + result: (Result Nat SurfaceLex::Failure), + body: (match (project (first (prim iterate_until (fun [start, text] (state : Braces) => (braceStep text start state)) ((prim length text : Nat) + 2) (record Braces {cursor := start, depth := 0, result := (ctor Option.none [(Result Nat SurfaceLex::Failure)])}) : (Braces * Bool))) result) + | Option.some result => result + | Option.none => (ctor Result.error [Nat, SurfaceLex::Failure] (failure start (lit "the braces of the declaration are not closed")))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: entriesOf, + parameters: [chain : LexLeanStd.Json::Json], + result: (List Mem), + body: (LexLeanStd.Order::reverseList [Mem] (project (first (prim iterate_until (fun (state : Walk) => (match (project state rest) + | LexLeanStd.Json::Json.JMember key value rest => (ctor Option.some [Walk] (record Walk {rest := rest, items := (cons (record Mem {key := key, value := value}) (project state items))})) + | LexLeanStd.Json::Json.JBool flag => (ctor Option.none [Walk]) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Option.none [Walk]) + | LexLeanStd.Json::Json.JStr written => (ctor Option.none [Walk]) + | LexLeanStd.Json::Json.JArr inner => (ctor Option.none [Walk]) + | LexLeanStd.Json::Json.JObj inner => (ctor Option.none [Walk]) + | LexLeanStd.Json::Json.JNil => (ctor Option.none [Walk]) + | LexLeanStd.Json::Json.JItem element rest => (ctor Option.none [Walk]))) 1000000 (record Walk {rest := chain, items := (nil Mem)}) : (Walk * Bool))) items)), + executable: true +} + +definition{ + name: placeStep, + parameters: [state : Place, entry : Mem, key : Bytes, value : LexLeanStd.Json::Json], + result: Place, + body: (if (same (project entry key) key) then state else (if (both (if (project state placed) then false else true) (LexLeanStd.Order::lessBytes key (project entry key))) then (record Place {placed := true, out := (cons entry (cons (record Mem {key := key, value := value}) (project state out)))}) else (record Place {placed := (project state placed), out := (cons entry (project state out))}))), + axioms: [propext], + executable: true +} + +definition{ + name: withMember, + parameters: [chain : LexLeanStd.Json::Json, key : Bytes, value : LexLeanStd.Json::Json], + result: LexLeanStd.Json::Json, + body: (let placed : Place := (prim list_fold (fun [key, value] (state : Place) (entry : Mem) => (placeStep state entry key value)) (record Place {placed := false, out := (nil Mem)}) (entriesOf chain) : Place); (let reversed : (List Mem) := (if (project placed placed) then (project placed out) else (cons (record Mem {key := key, value := value}) (project placed out))); (prim list_fold (fun (rest : LexLeanStd.Json::Json) (entry : Mem) => (ctor LexLeanStd.Json::Json.JMember (project entry key) (project entry value) rest)) (ctor LexLeanStd.Json::Json.JNil) reversed : LexLeanStd.Json::Json))), + axioms: [propext], + executable: true +} + +definition{ + name: rootOf, + parameters: [text : Bytes, cur : Cur, what : Bytes], + result: (Result (Root * Cur) SurfaceLex::Failure), + body: (match (scanAt text (project cur at)) + | Result.error problem => (ctor Result.error [(Root * Cur), SurfaceLex::Failure] problem) + | Result.ok tok => (if ((project tok kind) =? 15) then (let value : Nat := (if (numberTooLarge text tok) then 0 else (numberIn text tok)); (if (numberTooLarge text tok) then (ctor Result.error [(Root * Cur), SurfaceLex::Failure] (failure (project tok start) (lit "the number is too large"))) else (ctor Result.ok [(Root * Cur), SurfaceLex::Failure] ((ctor Root.RId value), (moved tok))))) else (if ((project tok kind) =? 16) then (ctor Result.ok [(Root * Cur), SurfaceLex::Failure] ((ctor Root.RLabel (LexLeanStd.Text::sliceOr text ((project tok start) + 1) (prim subtract (prim subtract (project tok stop) (project tok start) : Nat) 1 : Nat)) (project tok start)), (moved tok))) else (ctor Result.error [(Root * Cur), SurfaceLex::Failure] (failure (project tok start) (cat (lit "expected `@N`, ") what)))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: namesFail, + parameters: [state : Names, problem : SurfaceLex::Failure], + result: Names, + body: (record Names {names := (project state names), cur := (project state cur), done := true, error := (ctor Option.some [SurfaceLex::Failure] problem)}), + executable: true +} + +definition{ + name: namesStep, + parameters: [text : Bytes, state : Names, what : Bytes, closable : Bool], + result: (Option Names), + body: (if (project state done) then (ctor Option.none [Names]) else (ctor Option.some [Names] (match (scanAt text (project (project state cur) at)) + | Result.error problem => (namesFail state problem) + | Result.ok peeked => (if (both closable ((project peeked kind) =? 5)) then (record Names {names := (project state names), cur := (project state cur), done := true, error := (ctor Option.none [SurfaceLex::Failure])}) else (match (readName text (project state cur) what) + | Result.error problem => (namesFail state problem) + | Result.ok named => (match (scanAt text (project (project named cur) at)) + | Result.error problem => (namesFail state problem) + | Result.ok comma => (if ((project comma kind) =? 6) then (record Names {names := (cons (project named text) (project state names)), cur := (moved comma), done := false, error := (ctor Option.none [SurfaceLex::Failure])}) else (record Names {names := (cons (project named text) (project state names)), cur := (project named cur), done := true, error := (ctor Option.none [SurfaceLex::Failure])})))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: paramsStep, + parameters: [text : Bytes, state : Names], + result: (Option Names), + body: (namesStep text state (lit "a universe parameter") false), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: chainOf, + parameters: [items : (List LexLeanStd.Json::Json)], + result: LexLeanStd.Json::Json, + body: (prim list_fold (fun (rest : LexLeanStd.Json::Json) (item : LexLeanStd.Json::Json) => (ctor LexLeanStd.Json::Json.JItem item rest)) (ctor LexLeanStd.Json::Json.JNil) (LexLeanStd.Order::reverseList [LexLeanStd.Json::Json] items) : LexLeanStd.Json::Json), + executable: true +} + +definition{ + name: declarationRoots, + parameters: [ + text : Bytes, + word : Bytes, + label : Bytes, + levels : (List LexLeanStd.Json::Json), + cur : Cur, + start : Nat + ], + result: (Result (Pending * Cur) SurfaceLex::Failure), + body: (match (scanAt text (project cur at)) + | Result.error problem => (ctor Result.error [(Pending * Cur), SurfaceLex::Failure] problem) + | Result.ok colon => (let typed : (Result ((Option Root) * ((Option Root) * Cur)) SurfaceLex::Failure) := (if ((project colon kind) =? 7) then (match (rootOf text (moved colon) (lit "the type")) + | Result.error problem => (ctor Result.error [((Option Root) * ((Option Root) * Cur)), SurfaceLex::Failure] problem) + | Result.ok rooted => (match (scanAt text (project (second rooted) at)) + | Result.error problem => (ctor Result.error [((Option Root) * ((Option Root) * Cur)), SurfaceLex::Failure] problem) + | Result.ok assign => (if ((project assign kind) =? 8) then (match (rootOf text (moved assign) (lit "the value")) + | Result.error problem => (ctor Result.error [((Option Root) * ((Option Root) * Cur)), SurfaceLex::Failure] problem) + | Result.ok valued => (ctor Result.ok [((Option Root) * ((Option Root) * Cur)), SurfaceLex::Failure] ((ctor Option.some [Root] (first rooted)), ((ctor Option.some [Root] (first valued)), (second valued))))) else (ctor Result.ok [((Option Root) * ((Option Root) * Cur)), SurfaceLex::Failure] ((ctor Option.some [Root] (first rooted)), ((ctor Option.none [Root]), (second rooted))))))) else (ctor Result.ok [((Option Root) * ((Option Root) * Cur)), SurfaceLex::Failure] ((ctor Option.none [Root]), ((ctor Option.none [Root]), cur)))); (match typed + | Result.error problem => (ctor Result.error [(Pending * Cur), SurfaceLex::Failure] problem) + | Result.ok roots => (let after : Cur := (second (second roots)); (let stated : Bool := (match (first roots) + | Option.some root => true + | Option.none => false); (match (scanAt text (project after at)) + | Result.error problem => (ctor Result.error [(Pending * Cur), SurfaceLex::Failure] problem) + | Result.ok brace => (let membered : (Result (LexLeanStd.Json::Json * Cur) SurfaceLex::Failure) := (if (both ((project brace kind) =? 2) (if (project brace col0) then false else true)) then (match (membersEnd text (project brace start)) + | Result.error problem => (ctor Result.error [(LexLeanStd.Json::Json * Cur), SurfaceLex::Failure] problem) + | Result.ok stop => (let from : Nat := (project brace start); (match (SurfaceGeneric::parseGeneric (LexLeanStd.Text::sliceOr text from (prim subtract stop from : Nat))) + | Result.error problem => (ctor Result.error [(LexLeanStd.Json::Json * Cur), SurfaceLex::Failure] (failure (from + (project problem offset)) (project problem message))) + | Result.ok value => (match value + | LexLeanStd.Json::Json.JObj chain => (ctor Result.ok [(LexLeanStd.Json::Json * Cur), SurfaceLex::Failure] (chain, (record Cur {at := stop, lastEnd := stop}))) + | LexLeanStd.Json::Json.JBool flag => (ctor Result.error [(LexLeanStd.Json::Json * Cur), SurfaceLex::Failure] (failure from (lit "the members of a declaration are an object"))) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Result.error [(LexLeanStd.Json::Json * Cur), SurfaceLex::Failure] (failure from (lit "the members of a declaration are an object"))) + | LexLeanStd.Json::Json.JStr written => (ctor Result.error [(LexLeanStd.Json::Json * Cur), SurfaceLex::Failure] (failure from (lit "the members of a declaration are an object"))) + | LexLeanStd.Json::Json.JArr inner => (ctor Result.error [(LexLeanStd.Json::Json * Cur), SurfaceLex::Failure] (failure from (lit "the members of a declaration are an object"))) + | LexLeanStd.Json::Json.JNil => (ctor Result.error [(LexLeanStd.Json::Json * Cur), SurfaceLex::Failure] (failure from (lit "the members of a declaration are an object"))) + | LexLeanStd.Json::Json.JItem element rest => (ctor Result.error [(LexLeanStd.Json::Json * Cur), SurfaceLex::Failure] (failure from (lit "the members of a declaration are an object"))) + | LexLeanStd.Json::Json.JMember key inner rest => (ctor Result.error [(LexLeanStd.Json::Json * Cur), SurfaceLex::Failure] (failure from (lit "the members of a declaration are an object"))))))) else (ctor Result.ok [(LexLeanStd.Json::Json * Cur), SurfaceLex::Failure] ((ctor LexLeanStd.Json::Json.JNil), after))); (match membered + | Result.error problem => (ctor Result.error [(Pending * Cur), SurfaceLex::Failure] problem) + | Result.ok members => (let named : LexLeanStd.Json::Json := (withMember (withMember (first members) (lit "kind") (ctor LexLeanStd.Json::Json.JStr word)) (lit "name") (ctor LexLeanStd.Json::Json.JStr label)); (if stated then (ctor Result.ok [(Pending * Cur), SurfaceLex::Failure] ((record Pending {members := (withMember named (lit "levels") (ctor LexLeanStd.Json::Json.JArr (chainOf levels))), rootType := (first roots), rootValue := (first (second roots)), start := start}), (second members))) else (if (0 (ctor Result.error [(Pending * Cur), SurfaceLex::Failure] problem) + | Result.ok kindTok => (let word : Bytes := (slice text kindTok); (if (listed word (cons (lit "abbrev") (cons (lit "definition") (cons (lit "theorem") (cons (lit "inductive") (cons (lit "constructor") (cons (lit "recursor") (nil Bytes)))))))) then (match (readName text (moved kindTok) (lit "the declaration's name")) + | Result.error problem => (ctor Result.error [(Pending * Cur), SurfaceLex::Failure] problem) + | Result.ok named => (match (scanAt text (project (project named cur) at)) + | Result.error problem => (ctor Result.error [(Pending * Cur), SurfaceLex::Failure] problem) + | Result.ok opener => (if ((project opener kind) =? 14) then (let final : Names := (first (prim iterate_until (fun [text] (state : Names) => (paramsStep text state)) ((prim length text : Nat) + 2) (record Names {names := (nil Bytes), cur := (moved opener), done := false, error := (ctor Option.none [SurfaceLex::Failure])}) : (Names * Bool))); (match (project final error) + | Option.some problem => (ctor Result.error [(Pending * Cur), SurfaceLex::Failure] problem) + | Option.none => (match (expectTok text (project final cur) 3 (lit "`}` after the universe parameters")) + | Result.error problem => (ctor Result.error [(Pending * Cur), SurfaceLex::Failure] problem) + | Result.ok step => (declarationRoots text word (project named text) (prim list_fold (fun (out : (List LexLeanStd.Json::Json)) (name : Bytes) => (cons (ctor LexLeanStd.Json::Json.JStr name) out)) (nil LexLeanStd.Json::Json) (project final names) : (List LexLeanStd.Json::Json)) (project step cur) (project kindTok start))))) else (declarationRoots text word (project named text) (nil LexLeanStd.Json::Json) (project named cur) (project kindTok start))))) else (ctor Result.error [(Pending * Cur), SurfaceLex::Failure] (failure (project kindTok start) (cat (cat (lit "`") word) (lit "` does not begin a declaration or a node"))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: topFail, + parameters: [top : Top, problem : SurfaceLex::Failure], + result: Top, + body: (record Top {machine := (mfail (project top machine) problem), pending := (project top pending), spans := (project top spans), statements := (project top statements), finished := true}), + executable: true +} + +definition{ + name: topStep, + parameters: [text : Bytes, top : Top], + result: (Option Top), + body: (let machine : M := (project top machine); (if (if (project top finished) then true else (match (project machine error) + | Option.some problem => true + | Option.none => false)) then (ctor Option.none [Top]) else (ctor Option.some [Top] (match (scanAt text (project (project machine cur) at)) + | Result.error problem => (topFail top problem) + | Result.ok tok => (if ((project tok kind) =? 21) then (record Top {machine := machine, pending := (project top pending), spans := (project top spans), statements := (project top statements), finished := true}) else (if (if (project tok col0) then false else true) then (topFail top (failure (project tok start) (lit "a declaration or a node begins its line"))) else (if (if ((project tok kind) =? 15) then true else ((project tok kind) =? 16)) then (record Top {machine := (statement text machine), pending := (project top pending), spans := (project top spans), statements := true, finished := false}) else (if (project top statements) then (topFail top (failure (project tok start) (lit "the declarations come before the nodes"))) else (match (declarationAt text (project machine cur)) + | Result.error problem => (topFail top problem) + | Result.ok declared => (record Top {machine := (advance machine (second declared) (project machine jobs)), pending := (cons (first declared) (project top pending)), spans := (cons (record Span {start := (project tok start), stop := (project (second declared) lastEnd)}) (project top spans)), statements := false, finished := false})))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: resolveRoot, + parameters: [labels : (List Lbl), root : (Option Root)], + result: (Result (Option Nat) SurfaceLex::Failure), + body: (match root + | Option.none => (ctor Result.ok [(Option Nat), SurfaceLex::Failure] (ctor Option.none [Nat])) + | Option.some written => (match written + | Root.RId value => (ctor Result.ok [(Option Nat), SurfaceLex::Failure] (ctor Option.some [Nat] value)) + | Root.RLabel label at => (match (labelOf labels label) + | Option.some value => (ctor Result.ok [(Option Nat), SurfaceLex::Failure] (ctor Option.some [Nat] value)) + | Option.none => (ctor Result.error [(Option Nat), SurfaceLex::Failure] (failure at (cat (cat (lit "no statement is named `@") label) (lit "`"))))))), + executable: true +} + +definition{ + name: withRoot, + parameters: [members : LexLeanStd.Json::Json, key : Bytes, root : (Option Nat)], + result: LexLeanStd.Json::Json, + body: (match root + | Option.some value => (withMember members key (ctor LexLeanStd.Json::Json.JInt false value)) + | Option.none => members), + axioms: [propext], + executable: true +} + +definition{ + name: resolvedAll, + parameters: [labels : (List Lbl), pending : (List Pending)], + result: (Result (List LexLeanStd.Json::Json) SurfaceLex::Failure), + body: (prim list_fold (fun [labels] (state : (Result (List LexLeanStd.Json::Json) SurfaceLex::Failure)) (declaration : Pending) => (match state + | Result.error problem => state + | Result.ok done => (match (resolveRoot labels (project declaration rootType)) + | Result.error problem => (ctor Result.error [(List LexLeanStd.Json::Json), SurfaceLex::Failure] problem) + | Result.ok typed => (match (resolveRoot labels (project declaration rootValue)) + | Result.error problem => (ctor Result.error [(List LexLeanStd.Json::Json), SurfaceLex::Failure] problem) + | Result.ok valued => (ctor Result.ok [(List LexLeanStd.Json::Json), SurfaceLex::Failure] (cons (withRoot (withRoot (project declaration members) (lit "type") typed) (lit "value") valued) done)))))) (ctor Result.ok [(List LexLeanStd.Json::Json), SurfaceLex::Failure] (nil LexLeanStd.Json::Json)) pending : (Result (List LexLeanStd.Json::Json) SurfaceLex::Failure)), + axioms: [propext], + executable: true +} + +definition{ + name: importsStep, + parameters: [text : Bytes, state : Names], + result: (Option Names), + body: (namesStep text state (lit "a module name") true), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: readImports, + parameters: [text : Bytes], + result: (Result ((List Bytes) * Cur) SurfaceLex::Failure), + body: (match (scanAt text 0) + | Result.error problem => (ctor Result.error [((List Bytes) * Cur), SurfaceLex::Failure] problem) + | Result.ok tok => (if (both ((project tok kind) =? 20) (same (slice text tok) (lit "imports"))) then (match (expectTok text (moved tok) 4 (lit "`[` after `imports`")) + | Result.error problem => (ctor Result.error [((List Bytes) * Cur), SurfaceLex::Failure] problem) + | Result.ok opened => (let final : Names := (first (prim iterate_until (fun [text] (state : Names) => (importsStep text state)) ((prim length text : Nat) + 2) (record Names {names := (nil Bytes), cur := (project opened cur), done := false, error := (ctor Option.none [SurfaceLex::Failure])}) : (Names * Bool))); (match (project final error) + | Option.some problem => (ctor Result.error [((List Bytes) * Cur), SurfaceLex::Failure] problem) + | Option.none => (match (expectTok text (project final cur) 5 (lit "`,` or `]`")) + | Result.error problem => (ctor Result.error [((List Bytes) * Cur), SurfaceLex::Failure] problem) + | Result.ok closed => (ctor Result.ok [((List Bytes) * Cur), SurfaceLex::Failure] ((LexLeanStd.Order::reverseList [Bytes] (project final names)), (project closed cur))))))) else (ctor Result.error [((List Bytes) * Cur), SurfaceLex::Failure] (failure (project tok start) (lit "a core module begins with its `imports`"))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: readCoreModule, + parameters: [text : Bytes], + result: (Result Read SurfaceLex::Failure), + body: (match (readImports text) + | Result.error problem => (ctor Result.error [Read, SurfaceLex::Failure] problem) + | Result.ok imported => (let start : M := (record M {cur := (second imported), jobs := (nil Job), vals := (nil Nat), held := (nil Held), nodes := (nil CoreJson::Node), count := 0, proof := (nil Nat), scope := (nil Bytes), labels := (nil Lbl), error := (ctor Option.none [SurfaceLex::Failure])}); (let final : Top := (first (prim iterate_until (fun [text] (top : Top) => (topStep text top)) ((prim length text : Nat) + 2) (record Top {machine := start, pending := (nil Pending), spans := (nil Span), statements := false, finished := false}) : (Top * Bool))); (let machine : M := (project final machine); (match (project machine error) + | Option.some problem => (ctor Result.error [Read, SurfaceLex::Failure] problem) + | Option.none => (match (resolvedAll (project machine labels) (LexLeanStd.Order::reverseList [Pending] (project final pending))) + | Result.error problem => (ctor Result.error [Read, SurfaceLex::Failure] problem) + | Result.ok declarations => (ctor Result.ok [Read, SurfaceLex::Failure] (record Read {imports := (first imported), nodes := (LexLeanStd.Order::reverseList [CoreJson::Node] (project machine nodes)), proofNodes := (LexLeanStd.Order::reverseList [Nat] (project machine proof)), declarations := (prim list_fold (fun (out : (List LexLeanStd.Json::Json)) (members : LexLeanStd.Json::Json) => (cons (ctor LexLeanStd.Json::Json.JObj members) out)) (nil LexLeanStd.Json::Json) declarations : (List LexLeanStd.Json::Json)), spans := (LexLeanStd.Order::reverseList [Span] (project final spans))})))))))), + axioms: [Quot.sound, propext], + executable: true, + production: {effects: [allocation, overflow], targets: [rust-std]} +} +\end{semanticmodule} +\end{lexlean} diff --git a/bootstrap/src/GrammarMath.lex.tex b/bootstrap/src/GrammarMath.lex.tex new file mode 100644 index 0000000000..10801c8a3c --- /dev/null +++ b/bootstrap/src/GrammarMath.lex.tex @@ -0,0 +1,751 @@ +\begin{lexlean}{GrammarMath} +\useglossary{lexlean.std.nat@1.2.0} +\importmodule{LexLeanStd.Numeral} +\importmodule{LexLeanStd.Text} +\importmodule{PrimitiveScan} +\title{Natural number addition} +\begin{semanticmodule} +inductive{ + name: LeafKind, + type_parameters: [], + parameters: [], + constructors: [{name: LForm, fields: [Nat]}, {name: LIdent, fields: [Bytes]}] +} + +inductive{ + name: Math, + type_parameters: [], + parameters: [], + constructors: [ + {name: MNumeral, fields: [Bytes, Nat, Nat]}, + {name: MLeaf, fields: [(List LeafKind), Nat, Nat]}, + {name: MLexeme, fields: [Bytes, Nat, Nat]}, + {name: MReference, fields: [Bytes, Bytes, Nat, Nat]}, + {name: MCall, fields: [Math, Math, Nat, Nat, Nat, Nat, (List Nat)]}, + {name: MPrefix, fields: [(List Nat), Nat, Nat, Math]}, + {name: MPostfix, fields: [(List Nat), Nat, Nat, Math]}, + {name: MInfix, fields: [(List Nat), Nat, Nat, Math, Math]}, + {name: MParen, fields: [Math, Nat, Nat]}, + {name: MArgNil, fields: []}, + {name: MArgCons, fields: [Math, Math]} + ] +} + +structure{ + name: Edge, + type_parameters: [], + parameters: [], + fields: [ + {name: form, type: Nat}, + {name: stop, type: Nat}, + {name: frame, type: Nat}, + {name: precedence, type: Nat}, + {name: associativity, type: Nat} + ] +} + +structure{ + name: Val, + type_parameters: [], + parameters: [], + fields: [ + {name: ast, type: Math}, + {name: startsAt, type: Nat}, + {name: endsAt, type: Nat}, + {name: code, type: Bytes} + ] +} + +structure{ + name: Choice, + type_parameters: [], + parameters: [], + fields: [{name: position, type: Nat}, {name: upto, type: Nat}] +} + +structure{ + name: Met, + type_parameters: [], + parameters: [], + fields: [{name: position, type: Nat}, {name: ends, type: (List Nat)}] +} + +structure{ + name: Bud, + type_parameters: [], + parameters: [], + fields: [{name: states, type: Nat}, {name: edges, type: Nat}, {name: memo, type: (List Nat)}] +} + +inductive{ + name: Frame, + type_parameters: [], + parameters: [], + constructors: [ + {name: FExpr, fields: [Nat, Nat]}, + {name: FLoop, fields: [Nat, Nat, (Option Nat)]}, + {name: FInfix, fields: [Nat, Nat, Val, (List Nat), Nat, Nat, (Option Nat)]}, + {name: FPrimary, fields: [Nat]}, + {name: FParen, fields: [Nat, Nat]}, + {name: FPrefix, fields: [(List Nat), Nat, Nat, Nat]}, + {name: FPostfix, fields: [Nat]}, + {name: FCall, fields: [Val, Nat, (List Nat), (List Val), Nat]} + ] +} + +structure{ + name: Ctx, + type_parameters: [], + parameters: [], + fields: [ + {name: path, type: Bytes}, + {name: atoms, type: (List PrimitiveScan::Atom)}, + {name: stop, type: Nat}, + {name: edges, type: (List (List Edge))}, + {name: maxStates, type: Nat}, + {name: maxDepth, type: Nat}, + {name: maxEdges, type: Nat}, + {name: chosen, type: (List Choice)} + ] +} + +structure{ + name: Mach, + type_parameters: [], + parameters: [], + fields: [ + {name: cursor, type: Nat}, + {name: stack, type: (List Frame)}, + {name: value, type: (Option Val)}, + {name: error, type: (Option PrimitiveScan::Rejection)}, + {name: bud, type: Bud}, + {name: met, type: (List Met)} + ] +} + +structure{ + name: Looked, + type_parameters: [], + parameters: [], + fields: [ + {name: bud, type: Bud}, + {name: found, type: (List Edge)}, + {name: error, type: (Option PrimitiveScan::Rejection)} + ] +} + +structure{ + name: Braced, + type_parameters: [], + parameters: [], + fields: [{name: cursor, type: Nat}, {name: spelled, type: Bytes}, {name: closed, type: Bool}] +} + +structure{ + name: Composed, + type_parameters: [], + parameters: [], + fields: [{name: spelled, type: Bytes}, {name: next, type: Nat}, {name: reach, type: Nat}] +} + +structure{ + name: Pass, + type_parameters: [], + parameters: [], + fields: [ + {name: result, type: (Result Val PrimitiveScan::Rejection)}, + {name: bud, type: Bud}, + {name: met, type: (List Met)} + ] +} + +structure{ + name: Outer, + type_parameters: [], + parameters: [], + fields: [ + {name: queue, type: (List (List Choice))}, + {name: enqueued, type: (List Bytes)}, + {name: covers, type: (List Val)}, + {name: failure, type: (Option PrimitiveScan::Rejection)}, + {name: bud, type: Bud} + ] +} + +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: both, + parameters: [left : Bool, right : Bool], + result: Bool, + body: (if left then right else false), + executable: true +} + +definition{ + name: same, + parameters: [left : Bytes, right : Bytes], + result: Bool, + body: (prim equal left right : Bool), + executable: true +} + +definition{ + name: num, + parameters: [number : Nat], + result: Bytes, + body: (LexLeanStd.Numeral::decimal number), + executable: true +} + +definition{ + name: encN, + parameters: [number : Nat], + result: Bytes, + body: (cat (num number) (lit ",")), + executable: true +} + +definition{ + name: encB, + parameters: [piece : Bytes], + result: Bytes, + body: (cat (cat (num (prim length piece : Nat)) (lit ":")) piece), + executable: true +} + +definition{ + name: encNs, + parameters: [items : (List Nat)], + result: Bytes, + body: (prim list_fold (fun (out : Bytes) (item : Nat) => (cat out (encN item))) (encN (prim length items : Nat)) items : Bytes), + executable: true +} + +definition{ + name: reverseNats, + parameters: [items : (List Nat)], + result: (List Nat), + body: (prim list_fold (fun (turned : (List Nat)) (item : Nat) => (cons item turned)) (nil Nat) items : (List Nat)), + executable: true +} + +definition{ + name: reverseVals, + parameters: [items : (List Val)], + result: (List Val), + body: (prim list_fold (fun (turned : (List Val)) (item : Val) => (cons item turned)) (nil Val) items : (List Val)), + executable: true +} + +definition{ + name: reverseMets, + parameters: [items : (List Met)], + result: (List Met), + body: (prim list_fold (fun (turned : (List Met)) (item : Met) => (cons item turned)) (nil Met) items : (List Met)), + executable: true +} + +definition{ + name: natIn, + parameters: [needle : Nat, items : (List Nat)], + result: Bool, + body: (prim list_fold (fun [needle] (found : Bool) (item : Nat) => (if found then true else (item =? needle))) false items : Bool), + executable: true +} + +definition{ + name: bytesIn, + parameters: [needle : Bytes, items : (List Bytes)], + result: Bool, + body: (prim list_fold (fun [needle] (found : Bool) (item : Bytes) => (if found then true else (same item needle))) false items : Bool), + executable: true +} + +definition{ + name: insertNat, + parameters: [sorted : (List Nat), number : Nat], + result: (List Nat), + body: (let folded : (Bool * (List Nat)) := (prim list_fold (fun [number] (state : (Bool * (List Nat))) (item : Nat) => (if (first state) then (true, (cons item (second state))) else (if (number atom + | Option.none => (record PrimitiveScan::Atom {kind := (ctor PrimitiveScan::AtomClass.Whitespace), byteStart := 0, byteEnd := 0, lineStart := 1, columnStart := 1, lineEnd := 1, columnEnd := 1, text := (lit "")})), + executable: true +} + +definition{ + name: classAt, + parameters: [ctx : Ctx, position : Nat], + result: Nat, + body: (match (atomAt ctx position) + | Option.some atom => (PrimitiveScan::classCode atom) + | Option.none => 7), + executable: true +} + +definition{ + name: isAtom, + parameters: [atom : PrimitiveScan::Atom, code : Nat, spelled : Bytes], + result: Bool, + body: (both ((PrimitiveScan::classCode atom) =? code) (same (project atom text) spelled)), + executable: true +} + +definition{ + name: wsStep, + parameters: [ctx : Ctx, position : Nat], + result: (Option Nat), + body: (if (position (wsStep ctx current)) ((prim length (project ctx atoms) : Nat) + 1) position : (Nat * Bool))), + executable: true +} + +definition{ + name: placeAt, + parameters: [ctx : Ctx, position : Nat], + result: PrimitiveScan::Place, + body: (PrimitiveScan::placeOfAtom (project ctx path) (atomOr ctx position)), + executable: true +} + +definition{ + name: spanHere, + parameters: [ctx : Ctx, position : Nat], + result: PrimitiveScan::Place, + body: (let here : Nat := (skipWs ctx position); (if (here listed + | Option.none => (nil Edge)); (if (natIn position (project bud memo)) then (record Looked {bud := bud, found := found, error := (ctor Option.none [PrimitiveScan::Rejection])}) else (let count : Nat := ((project bud edges) + (prim length found : Nat)); (if ((project ctx maxEdges) (cat out (match kind + | LeafKind.LForm form => (cat (lit "F") (encN form)) + | LeafKind.LIdent spelled => (cat (lit "I") (encB spelled))))) (encN (prim length kinds : Nat)) kinds : Bytes), + executable: true +} + +definition{ + name: argChain, + parameters: [args : (List Val)], + result: Math, + body: (prim list_fold (fun (rest : Math) (arg : Val) => (ctor Math.MArgCons (project arg ast) rest)) (ctor Math.MArgNil) (reverseVals args) : Math), + executable: true +} + +definition{ + name: callVal, + parameters: [ + head : Val, + args : (List Val), + upto : Nat, + opening : Nat, + closing : Nat, + commas : (List Nat) + ], + result: Val, + body: (mathVal (ctor Math.MCall (project head ast) (argChain args) (project head startsAt) upto opening closing commas) (project head startsAt) upto (cat (cat (cat (cat (cat (lit "C") (project head code)) (prim list_fold (fun (out : Bytes) (arg : Val) => (cat out (project arg code))) (encN (prim length args : Nat)) args : Bytes)) (encN upto)) (cat (encN opening) (encN closing))) (encNs commas))), + executable: true +} + +definition{ + name: opVal, + parameters: [ + tag : Bytes, + candidates : (List Nat), + opStart : Nat, + opEnd : Nat, + arg : Val, + prefixed : Bool + ], + result: Val, + body: (let code : Bytes := (cat (cat (cat (cat tag (encNs candidates)) (encN opStart)) (encN opEnd)) (project arg code)); (if prefixed then (mathVal (ctor Math.MPrefix candidates opStart opEnd (project arg ast)) opStart (project arg endsAt) code) else (mathVal (ctor Math.MPostfix candidates opStart opEnd (project arg ast)) (project arg startsAt) opEnd code))), + executable: true +} + +definition{ + name: infixVal, + parameters: [candidates : (List Nat), opStart : Nat, opEnd : Nat, lhs : Val, rhs : Val], + result: Val, + body: (mathVal (ctor Math.MInfix candidates opStart opEnd (project lhs ast) (project rhs ast)) (project lhs startsAt) (project rhs endsAt) (cat (cat (cat (cat (cat (lit "X") (encNs candidates)) (encN opStart)) (encN opEnd)) (project lhs code)) (project rhs code))), + executable: true +} + +definition{ + name: mk, + parameters: [m : Mach, cursor : Nat, stack : (List Frame), value : (Option Val)], + result: Mach, + body: (record Mach {cursor := cursor, stack := stack, value := value, error := (project m error), bud := (project m bud), met := (project m met)}), + executable: true +} + +definition{ + name: failed, + parameters: [m : Mach, problem : PrimitiveScan::Rejection], + result: Mach, + body: (record Mach {cursor := (project m cursor), stack := (project m stack), value := (project m value), error := (ctor Option.some [PrimitiveScan::Rejection] problem), bud := (project m bud), met := (project m met)}), + executable: true +} + +definition{ + name: withBud, + parameters: [m : Mach, bud : Bud], + result: Mach, + body: (record Mach {cursor := (project m cursor), stack := (project m stack), value := (project m value), error := (project m error), bud := bud, met := (project m met)}), + executable: true +} + +definition{ + name: braceStep, + parameters: [ctx : Ctx, state : Braced], + result: (Option Braced), + body: (if (project state closed) then (ctor Option.none [Braced]) else (let position : Nat := (project state cursor); (if (position (if (isAtom atom 3 (lit "}")) then (ctor Option.some [Braced] (record Braced {cursor := (position + 1), spelled := (project state spelled), closed := true})) else (if ((PrimitiveScan::classCode atom) =? 6) then (ctor Option.some [Braced] (record Braced {cursor := (position + 1), spelled := (project state spelled), closed := false})) else (ctor Option.some [Braced] (record Braced {cursor := (position + 1), spelled := (cat (project state spelled) (project atom text)), closed := false})))) + | Option.none => (ctor Option.none [Braced])) else (ctor Option.none [Braced])))), + executable: true +} + +definition{ + name: braces, + parameters: [ctx : Ctx, position : Nat], + result: (Result Braced PrimitiveScan::Rejection), + body: (let here : Nat := (skipWs ctx position); (if (both (here (braceStep ctx state)) ((prim length (project ctx atoms) : Nat) + 1) (record Braced {cursor := (here + 1), spelled := (lit ""), closed := false}) : (Braced * Bool))); (if (project final closed) then (ctor Result.ok [Braced, PrimitiveScan::Rejection] final) else (ctor Result.error [Braced, PrimitiveScan::Rejection] (placed (lit "LLP2003") (lit "unclosed `{` argument") (spanHere ctx (project final cursor)))))) else (ctor Result.error [Braced, PrimitiveScan::Rejection] (placed (lit "LLP2003") (lit "expected `{`") (spanHere ctx here))))), + executable: true +} + +definition{ + name: composeStep, + parameters: [ctx : Ctx, state : Composed], + result: (Option Composed), + body: (match (atomAt ctx (project state next)) + | Option.some atom => (let code : Nat := (PrimitiveScan::classCode atom); (if (both ((project atom byteStart) =? (project state reach)) (if (if (code =? 1) then true else (code =? 2)) then true else (both (code =? 4) (if (same (project atom text) (lit "_")) then true else (same (project atom text) (lit "'")))))) then (ctor Option.some [Composed] (record Composed {spelled := (cat (project state spelled) (project atom text)), next := ((project state next) + 1), reach := (project atom byteEnd)})) else (ctor Option.none [Composed]))) + | Option.none => (ctor Option.none [Composed])), + executable: true +} + +definition{ + name: composedAt, + parameters: [ctx : Ctx, atom : PrimitiveScan::Atom, position : Nat], + result: Composed, + body: (first (prim iterate_until (fun [ctx] (state : Composed) => (composeStep ctx state)) ((prim length (project ctx atoms) : Nat) + 1) (record Composed {spelled := (project atom text), next := (position + 1), reach := (project atom byteEnd)}) : (Composed * Bool))), + executable: true +} + +definition{ + name: chosenAt, + parameters: [ctx : Ctx, position : Nat], + result: (Option Nat), + body: (prim list_fold (fun [position] (found : (Option Nat)) (choice : Choice) => (if ((project choice position) =? position) then (ctor Option.some [Nat] (project choice upto)) else found)) (ctor Option.none [Nat]) (project ctx chosen) : (Option Nat)), + executable: true +} + +definition{ + name: leafStep, + parameters: [ + ctx : Ctx, + m : Mach, + rest : (List Frame), + depth : Nat, + position : Nat, + atom : PrimitiveScan::Atom, + leaves : (List Edge) + ], + result: Mach, + body: (let stop : Nat := (project ctx stop); (let identifier : (Option Composed) := (if ((PrimitiveScan::classCode atom) =? 1) then (let composed : Composed := (composedAt ctx atom position); (if ((project composed next) <=? stop) then (ctor Option.some [Composed] composed) else (ctor Option.none [Composed]))) else (ctor Option.none [Composed])); (let leafEnds : (List Nat) := (prim list_fold (fun (sorted : (List Nat)) (edge : Edge) => (insertNat sorted (project edge stop))) (nil Nat) leaves : (List Nat)); (let ends : (List Nat) := (match identifier + | Option.some composed => (insertNat leafEnds (project composed next)) + | Option.none => leafEnds); (match (prim index ends 0 : (Option Nat)) + | Option.none => (failed m (placed (lit "LLL1004") (cat (cat (lit "unknown atom `") (project atom text)) (lit "` in a mathematical island")) (placeAt ctx position))) + | Option.some shortest => (let upto : Nat := (match (chosenAt ctx position) + | Option.some picked => (if (natIn picked ends) then picked else shortest) + | Option.none => shortest); (let met : (List Met) := (if (1 (if ((project edge stop) =? upto) then (cons (ctor LeafKind.LForm (project edge form)) kinds) else kinds)) (nil LeafKind) leaves : (List LeafKind)); (let kinds : (List LeafKind) := (prim list_fold (fun (turned : (List LeafKind)) (kind : LeafKind) => (cons kind turned)) (match identifier + | Option.some composed => (if ((project composed next) =? upto) then (cons (ctor LeafKind.LIdent (project composed spelled)) (nil LeafKind)) else (nil LeafKind)) + | Option.none => (nil LeafKind)) forms : (List LeafKind)); (let leaf : Val := (mathVal (ctor Math.MLeaf kinds position upto) position upto (cat (cat (cat (lit "L") (kindCode kinds)) (encN position)) (encN upto))); (record Mach {cursor := upto, stack := (cons (ctor Frame.FPostfix depth) rest), value := (ctor Option.some [Val] leaf), error := (project m error), bud := (project m bud), met := met}))))))))))), + executable: true +} + +definition{ + name: profileAgrees, + parameters: [head : Edge, edges : (List Edge), withFrame : Bool], + result: Bool, + body: (prim list_fold (fun [head, withFrame] (fine : Bool) (edge : Edge) => (if fine then (both ((project edge stop) =? (project head stop)) (both ((project edge precedence) =? (project head precedence)) (if withFrame then (both ((project edge associativity) =? (project head associativity)) ((project edge frame) =? (project head frame))) else true))) else false)) true edges : Bool), + executable: true +} + +definition{ + name: formsOf, + parameters: [edges : (List Edge)], + result: (List Nat), + body: (reverseNats (prim list_fold (fun (out : (List Nat)) (edge : Edge) => (cons (project edge form) out)) (nil Nat) edges : (List Nat))), + executable: true +} + +definition{ + name: keepEdges, + parameters: [edges : (List Edge), stop : Nat, low : Nat, high : Nat], + result: (List Edge), + body: (prim list_fold (fun [high, low, stop] (out : (List Edge)) (edge : Edge) => (prim append out (if (both ((project edge stop) <=? stop) (both (low <=? (project edge frame)) ((project edge frame) <=? high))) then (cons edge (nil Edge)) else (nil Edge)) : (List Edge))) (nil Edge) edges : (List Edge)), + executable: true +} + +definition{ + name: formStep, + parameters: [ + ctx : Ctx, + m : Mach, + rest : (List Frame), + depth : Nat, + position : Nat, + atom : PrimitiveScan::Atom + ], + result: Mach, + body: (let looked : Looked := (edgesAt ctx (project m bud) position); (let charged : Mach := (withBud m (project looked bud)); (match (project looked error) + | Option.some problem => (failed charged problem) + | Option.none => (let stop : Nat := (project ctx stop); (let prefixes : (List Edge) := (keepEdges (project looked found) stop 2 2); (let leaves : (List Edge) := (keepEdges (project looked found) stop 0 1); (match (prim index prefixes 0 : (Option Edge)) + | Option.some head => (if (profileAgrees head prefixes false) then (if ((prim length leaves : Nat) =? 0) then (mk charged (project head stop) (cons (ctor Frame.FExpr (project head precedence) (depth + 1)) (cons (ctor Frame.FPrefix (formsOf prefixes) position (project head stop) depth) rest)) (ctor Option.none [Val])) else (failed charged (placed (lit "LLP2002") (lit "this surface is both a prefix operator and a leaf; LexLean does not guess") (placeAt ctx position)))) else (failed charged (placed (lit "LLP2002") (lit "prefix candidates disagree on their profile; LexLean does not guess") (placeAt ctx position)))) + | Option.none => (leafStep ctx charged rest depth position atom leaves)))))))), + executable: true +} + +definition{ + name: primaryStep, + parameters: [ctx : Ctx, m : Mach, rest : (List Frame), depth : Nat], + result: Mach, + body: (let charge : (Bud * (Option PrimitiveScan::Rejection)) := (chargeState ctx (project m bud)); (let counted : Mach := (withBud m (first charge)); (match (second charge) + | Option.some problem => (failed counted problem) + | Option.none => (if ((project ctx maxDepth) (mk counted (project braced cursor) (cons (ctor Frame.FPostfix depth) rest) (ctor Option.some [Val] (mathVal (ctor Math.MLexeme (project braced spelled) position (project braced cursor)) position (project braced cursor) (cat (cat (cat (lit "E") (encB (project braced spelled))) (encN position)) (encN (project braced cursor)))))) + | Result.error problem => (failed counted problem)) else (if (isAtom atom 0 (lit "\\reference")) then (match (braces ctx (position + 1)) + | Result.ok braced => (let spelled : Bytes := (project braced spelled); (match (LexLeanStd.Text::find spelled (lit "::")) + | Option.some split => (let qualifier : Bytes := (LexLeanStd.Text::sliceOr spelled 0 split); (let component : Bytes := (LexLeanStd.Text::sliceOr spelled (split + 2) (prim subtract (prim length spelled : Nat) (split + 2) : Nat)); (mk counted (project braced cursor) (cons (ctor Frame.FPostfix depth) rest) (ctor Option.some [Val] (mathVal (ctor Math.MReference qualifier component position (project braced cursor)) position (project braced cursor) (cat (cat (cat (cat (lit "R") (encB qualifier)) (encB component)) (encN position)) (encN (project braced cursor)))))))) + | Option.none => (failed counted (placed (lit "LLR3005") (cat (cat (lit "`") spelled) (lit "` is not a `Module::component` reference")) (placeAt ctx position))))) + | Result.error problem => (failed counted problem)) else (formStep ctx (mk counted position rest (ctor Option.none [Val])) rest depth position atom)))))))) else (failed (mk counted position rest (ctor Option.none [Val])) (placed (lit "LLP2001") (lit "expected a mathematical expression") (spanHere ctx position))))))))), + executable: true +} + +definition{ + name: loopStep, + parameters: [ + ctx : Ctx, + m : Mach, + rest : (List Frame), + minBp : Nat, + depth : Nat, + last : (Option Nat), + lhs : Val + ], + result: Mach, + body: (let position : Nat := (skipWs ctx (project m cursor)); (let done : Mach := (mk m position rest (ctor Option.some [Val] lhs)); (if (position (failed charged problem) + | Option.none => (let operators : (List Edge) := (keepEdges (project looked found) (project ctx stop) 3 4); (match (prim index operators 0 : (Option Edge)) + | Option.none => charged + | Option.some head => (if (profileAgrees head operators true) then (let precedence : Nat := (project head precedence); (if (precedence (held =? precedence) + | Option.none => false) then (failed charged (placed (lit "LLP2004") (lit "a nonassociative chain requires explicit parentheses") (spanHere ctx position))) else (let associativity : Nat := (project head associativity); (let next : Nat := (if (associativity =? 2) then precedence else (precedence + 1)); (let remember : (Option Nat) := (if (if (associativity =? 1) then true else (associativity =? 2)) then (ctor Option.none [Nat]) else (ctor Option.some [Nat] precedence)); (mk charged (project head stop) (cons (ctor Frame.FExpr next (depth + 1)) (cons (ctor Frame.FInfix minBp depth lhs (formsOf operators) position (project head stop) remember) rest)) (ctor Option.none [Val]))))))))) else (failed charged (placed (lit "LLP2002") (lit "operator candidates at this surface disagree on precedence, associativity, or frame; LexLean does not guess") (spanHere ctx position))))))))) else done))), + executable: true +} + +definition{ + name: valueOr, + parameters: [m : Mach], + result: Val, + body: (match (project m value) + | Option.some held => held + | Option.none => (mathVal (ctor Math.MArgNil) 0 0 (lit ""))), + executable: true +} + +definition{ + name: runFrame, + parameters: [ctx : Ctx, m : Mach, rest : (List Frame), frame : Frame], + result: Mach, + body: (let held : Val := (valueOr m); (match frame + | Frame.FExpr minBp depth => (let charge : (Bud * (Option PrimitiveScan::Rejection)) := (chargeState ctx (project m bud)); (let counted : Mach := (withBud m (first charge)); (match (second charge) + | Option.some problem => (failed counted problem) + | Option.none => (mk counted (project m cursor) (cons (ctor Frame.FPrimary depth) (cons (ctor Frame.FLoop minBp depth (ctor Option.none [Nat])) rest)) (ctor Option.none [Val]))))) + | Frame.FPrimary depth => (primaryStep ctx m rest depth) + | Frame.FLoop minBp depth last => (loopStep ctx m rest minBp depth last held) + | Frame.FInfix minBp depth lhs candidates opStart opEnd remember => (mk m (project m cursor) (cons (ctor Frame.FLoop minBp depth remember) rest) (ctor Option.some [Val] (infixVal candidates opStart opEnd lhs held))) + | Frame.FParen start depth => (let position : Nat := (skipWs ctx (project m cursor)); (if (both (position (mk m (project m cursor) (cons (ctor Frame.FPostfix depth) rest) (ctor Option.some [Val] (opVal (lit "Q") candidates opStart opEnd held true))) + | Frame.FPostfix depth => (let position : Nat := (skipWs ctx (project m cursor)); (if (both (position (let gathered : (List Val) := (cons held args); (let position : Nat := (skipWs ctx (project m cursor)); (let here : PrimitiveScan::Atom := (atomOr ctx position); (if (both (position (ctor Option.none [Mach]) + | Option.none => (match (project m stack) + | List.nil => (ctor Option.none [Mach]) + | List.cons frame rest => (ctor Option.some [Mach] (runFrame ctx m rest frame)))), + executable: true +} + +definition{ + name: cover, + parameters: [ctx : Ctx, start : Nat, bud : Bud], + result: Pass, + body: (let final : Mach := (first (prim iterate_until (fun [ctx] (m : Mach) => (machStep ctx m)) (prim multiply ((prim subtract (project ctx stop) start : Nat) + 4) 16 : Nat) (record Mach {cursor := start, stack := (cons (ctor Frame.FExpr 0 1) (nil Frame)), value := (ctor Option.none [Val]), error := (ctor Option.none [PrimitiveScan::Rejection]), bud := bud, met := (nil Met)}) : (Mach * Bool))); (let met : (List Met) := (reverseMets (project final met)); (match (project final error) + | Option.some problem => (record Pass {result := (ctor Result.error [Val, PrimitiveScan::Rejection] problem), bud := (project final bud), met := met}) + | Option.none => (let position : Nat := (skipWs ctx (project final cursor)); (if (position (record Pass {result := (ctor Result.error [Val, PrimitiveScan::Rejection] problem), bud := (project looked bud), met := met}) + | Option.none => (record Pass {result := (ctor Result.error [Val, PrimitiveScan::Rejection] (if (if quick then true else (0 (cat (cat out (encN (project choice position))) (encN (project choice upto)))) (lit "") chosen : Bytes), + executable: true +} + +definition{ + name: insertChoice, + parameters: [chosen : (List Choice), position : Nat, upto : Nat], + result: (List Choice), + body: (let folded : (Bool * (List Choice)) := (prim list_fold (fun [position, upto] (state : (Bool * (List Choice))) (choice : Choice) => (if (first state) then (true, (cons choice (second state))) else (if (position (cons choice turned)) (nil Choice) (if (first folded) then (second folded) else (cons (record Choice {position := position, upto := upto}) (second folded))) : (List Choice))), + executable: true +} + +definition{ + name: choicePresent, + parameters: [chosen : (List Choice), position : Nat], + result: Bool, + body: (prim list_fold (fun [position] (found : Bool) (choice : Choice) => (if found then true else ((project choice position) =? position))) false chosen : Bool), + executable: true +} + +definition{ + name: enqueue, + parameters: [ + chosen : (List Choice), + met : (List Met), + queue : (List (List Choice)), + enqueued : (List Bytes) + ], + result: ((List (List Choice)) * (List Bytes)), + body: (prim list_fold (fun [chosen] (state : ((List (List Choice)) * (List Bytes))) (point : Met) => (if (choicePresent chosen (project point position)) then state else (second (prim list_fold (fun [chosen, point] (inner : (Bool * ((List (List Choice)) * (List Bytes)))) (upto : Nat) => (if (first inner) then (false, (second inner)) else (let next : (List Choice) := (insertChoice chosen (project point position) upto); (let key : Bytes := (choiceKey next); (if (bytesIn key (second (second inner))) then (false, (second inner)) else (false, ((cons next (first (second inner))), (cons key (second (second inner)))))))))) (true, state) (project point ends) : (Bool * ((List (List Choice)) * (List Bytes))))))) (queue, enqueued) met : ((List (List Choice)) * (List Bytes))), + executable: true +} + +definition{ + name: outerStep, + parameters: [ctx : Ctx, start : Nat, state : Outer], + result: (Option Outer), + body: (match (project state queue) + | List.nil => (ctor Option.none [Outer]) + | List.cons chosen rest => (let pass : Pass := (cover (record Ctx {path := (project ctx path), atoms := (project ctx atoms), stop := (project ctx stop), edges := (project ctx edges), maxStates := (project ctx maxStates), maxDepth := (project ctx maxDepth), maxEdges := (project ctx maxEdges), chosen := chosen}) start (project state bud)); (let grown : ((List (List Choice)) * (List Bytes)) := (enqueue chosen (project pass met) rest (project state enqueued)); (ctor Option.some [Outer] (match (project pass result) + | Result.ok found => (record Outer {queue := (first grown), enqueued := (second grown), covers := (if (prim list_fold (fun [found] (seen : Bool) (held : Val) => (if seen then true else (same (project held code) (project found code)))) false (project state covers) : Bool) then (project state covers) else (prim append (project state covers) (cons found (nil Val)) : (List Val))), failure := (project state failure), bud := (project pass bud)}) + | Result.error problem => (record Outer {queue := (first grown), enqueued := (second grown), covers := (project state covers), failure := (match (project state failure) + | Option.some earlier => (project state failure) + | Option.none => (ctor Option.some [PrimitiveScan::Rejection] problem)), bud := (project pass bud)})))))), + executable: true +} + +definition{ + name: parseMath, + parameters: [ + path : Bytes, + atoms : (List PrimitiveScan::Atom), + start : Nat, + stop : Nat, + edges : (List (List Edge)), + maxEdges : Nat, + maxStates : Nat, + maxDepth : Nat, + statesBefore : Nat, + edgesBefore : Nat + ], + result: (Result (List Math) PrimitiveScan::Rejection), + body: (let ctx : Ctx := (record Ctx {path := path, atoms := atoms, stop := stop, edges := edges, maxStates := maxStates, maxDepth := maxDepth, maxEdges := maxEdges, chosen := (nil Choice)}); (let final : Outer := (first (prim iterate_until (fun [ctx, start] (state : Outer) => (outerStep ctx start state)) ((prim multiply (maxStates + 2) ((prim subtract stop start : Nat) + 2) : Nat) + 2) (record Outer {queue := (cons (nil Choice) (nil (List Choice))), enqueued := (cons (lit "") (nil Bytes)), covers := (nil Val), failure := (ctor Option.none [PrimitiveScan::Rejection]), bud := (record Bud {states := statesBefore, edges := edgesBefore, memo := (nil Nat)})}) : (Outer * Bool))); (if ((prim length (project final covers) : Nat) =? 0) then (match (project final failure) + | Option.some problem => (ctor Result.error [(List Math), PrimitiveScan::Rejection] problem) + | Option.none => (ctor Result.error [(List Math), PrimitiveScan::Rejection] (placed (lit "LLI9001") (lit "phase parse: the token lattice produced neither a cover nor a failure") (record PrimitiveScan::Place {path := path, byteStart := 0, byteEnd := 0, lineStart := 1, columnStart := 1, lineEnd := 1, columnEnd := 1})))) else (ctor Result.ok [(List Math), PrimitiveScan::Rejection] (prim list_fold (fun (out : (List Math)) (held : Val) => (prim append out (cons (project held ast) (nil Math)) : (List Math))) (nil Math) (project final covers) : (List Math)))))), + executable: true, + production: {effects: [allocation, overflow], targets: [rust-std]} +} +\end{semanticmodule} +\end{lexlean} diff --git a/bootstrap/src/GrammarProof.lex.tex b/bootstrap/src/GrammarProof.lex.tex new file mode 100644 index 0000000000..9494b49ff7 --- /dev/null +++ b/bootstrap/src/GrammarProof.lex.tex @@ -0,0 +1,445 @@ +\begin{lexlean}{GrammarProof} +\useglossary{lexlean.std.nat@1.2.0} +\importmodule{PrimitiveScan} +\importmodule{LexLeanStd.Text} +\title{Natural number addition} +\begin{semanticmodule} +inductive{ + name: TextToken, + type_parameters: [], + parameters: [], + constructors: [ + {name: TokenAtom, fields: [Nat]}, + {name: TokenIsland, fields: [Nat, Nat, Nat, Nat, Bool]} + ] +} + +structure{ + name: Keyword, + type_parameters: [], + parameters: [], + fields: [{name: atom, type: Nat}, {name: entry, type: Bytes}] +} + +inductive{ + name: SentenceKind, + type_parameters: [], + parameters: [], + constructors: [ + {name: Assume, fields: [(List TextToken)]}, + {name: Apply, fields: [TextToken]}, + {name: CloseWith, fields: [TextToken]}, + {name: CloseByReflexivity, fields: []}, + {name: Witness, fields: [TextToken]}, + {name: SelectLeft, fields: []}, + {name: SelectRight, fields: []} + ] +} + +structure{ + name: Sentence, + type_parameters: [], + parameters: [], + fields: [{name: kind, type: SentenceKind}, {name: keywords, type: (List Keyword)}] +} + +structure{ + name: Scan, + type_parameters: [], + parameters: [], + fields: [ + {name: cursor, type: Nat}, + {name: tokens, type: (List TextToken)}, + {name: error, type: (Option PrimitiveScan::Rejection)} + ] +} + +structure{ + name: Commas, + type_parameters: [], + parameters: [], + fields: [{name: cursor, type: Nat}, {name: islands, type: (List TextToken)}] +} + +structure{ + name: Found, + type_parameters: [], + parameters: [], + fields: [{name: word, type: Bytes}, {name: atom, type: Nat}] +} + +structure{ + name: Forbid, + type_parameters: [], + parameters: [], + fields: [{name: cursor, type: Nat}, {name: found, type: (Option Found)}] +} + +structure{ + name: Compose, + type_parameters: [], + parameters: [], + fields: [{name: spelled, type: Bytes}, {name: reach, type: Nat}, {name: next, type: Nat}] +} + +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: both, + parameters: [left : Bool, right : Bool], + result: Bool, + body: (if left then right else false), + executable: true +} + +definition{ + name: reverseTokens, + parameters: [items : (List TextToken)], + result: (List TextToken), + body: (prim list_fold (fun (turned : (List TextToken)) (item : TextToken) => (cons item turned)) (nil TextToken) items : (List TextToken)), + executable: true +} + +definition{ + name: isControl, + parameters: [atom : PrimitiveScan::Atom, spelled : Bytes], + result: Bool, + body: (both ((PrimitiveScan::classCode atom) =? 0) (prim equal (project atom text) spelled : Bool)), + executable: true +} + +definition{ + name: opens, + parameters: [atom : PrimitiveScan::Atom], + result: Bool, + body: (if (isControl atom (lit "\\(")) then true else (isControl atom (lit "\\["))), + executable: true +} + +definition{ + name: refusal, + parameters: [path : Bytes, code : Bytes, message : Bytes, atom : PrimitiveScan::Atom], + result: PrimitiveScan::Rejection, + body: (record PrimitiveScan::Rejection {code := code, message := message, place := (ctor Option.some [PrimitiveScan::Place] (PrimitiveScan::placeOfAtom path atom)), help := (ctor Option.none [Bytes])}), + executable: true +} + +definition{ + name: failed, + parameters: [scan : Scan, problem : PrimitiveScan::Rejection], + result: Scan, + body: (record Scan {cursor := (project scan cursor), tokens := (project scan tokens), error := (ctor Option.some [PrimitiveScan::Rejection] problem)}), + executable: true +} + +definition{ + name: pushed, + parameters: [scan : Scan, next : Nat, token : TextToken], + result: Scan, + body: (record Scan {cursor := next, tokens := (cons token (project scan tokens)), error := (project scan error)}), + executable: true +} + +definition{ + name: closerStep, + parameters: [atoms : (List PrimitiveScan::Atom), closer : Bytes, stop : Nat, position : Nat], + result: (Option Nat), + body: (if (position (if (isControl atom closer) then (ctor Option.none [Nat]) else (if (opens atom) then (ctor Option.none [Nat]) else (ctor Option.some [Nat] (position + 1)))) + | Option.none => (ctor Option.none [Nat])) else (ctor Option.none [Nat])), + executable: true +} + +definition{ + name: islandAt, + parameters: [ + path : Bytes, + atoms : (List PrimitiveScan::Atom), + stop : Nat, + scan : Scan, + position : Nat, + atom : PrimitiveScan::Atom + ], + result: Scan, + body: (let shown : Bool := (isControl atom (lit "\\[")); (let closer : Bytes := (if shown then (lit "\\]") else (lit "\\)")); (let unclosed : Scan := (failed scan (refusal path (lit "LLL1006") (cat (cat (lit "unclosed mathematical island; expected `") closer) (lit "`")) atom)); (let reached : Nat := (first (prim iterate_until (fun [atoms, closer, stop] (scanned : Nat) => (closerStep atoms closer stop scanned)) ((prim subtract stop position : Nat) + 1) (position + 1) : (Nat * Bool))); (if (reached (if (isControl candidate closer) then (pushed scan (reached + 1) (ctor TextToken.TokenIsland position (position + 1) reached reached shown)) else (failed scan (refusal path (lit "LLL1006") (lit "mathematical islands do not nest") candidate))) + | Option.none => unclosed) else unclosed))))), + executable: true +} + +definition{ + name: tokenAt, + parameters: [ + path : Bytes, + atoms : (List PrimitiveScan::Atom), + stop : Nat, + scan : Scan, + position : Nat, + atom : PrimitiveScan::Atom + ], + result: Scan, + body: (let code : Nat := (PrimitiveScan::classCode atom); (if (code =? 6) then (record Scan {cursor := (position + 1), tokens := (project scan tokens), error := (project scan error)}) else (if (both (code =? 4) (prim equal (project atom text) (lit "$") : Bool)) then (failed scan (refusal path (lit "LLP2001") (lit "dollar math delimiters are forbidden; use \\( ... \\) or \\[ ... \\]") atom)) else (if (opens atom) then (islandAt path atoms stop scan position atom) else (if (if (isControl atom (lit "\\)")) then true else (isControl atom (lit "\\]"))) then (failed scan (refusal path (lit "LLL1006") (cat (cat (lit "`") (project atom text)) (lit "` closes no mathematical island")) atom)) else (pushed scan (position + 1) (ctor TextToken.TokenAtom position))))))), + executable: true +} + +definition{ + name: tokenStep, + parameters: [path : Bytes, atoms : (List PrimitiveScan::Atom), stop : Nat, scan : Scan], + result: (Option Scan), + body: (match (project scan error) + | Option.some problem => (ctor Option.none [Scan]) + | Option.none => (let position : Nat := (project scan cursor); (if (position (ctor Option.some [Scan] (tokenAt path atoms stop scan position atom)) + | Option.none => (ctor Option.none [Scan])) else (ctor Option.none [Scan])))), + executable: true +} + +definition{ + name: textTokens, + parameters: [path : Bytes, atoms : (List PrimitiveScan::Atom), start : Nat, stop : Nat], + result: (Result (List TextToken) PrimitiveScan::Rejection), + body: (let final : Scan := (first (prim iterate_until (fun [atoms, path, stop] (scan : Scan) => (tokenStep path atoms stop scan)) ((prim subtract stop start : Nat) + 1) (record Scan {cursor := start, tokens := (nil TextToken), error := (ctor Option.none [PrimitiveScan::Rejection])}) : (Scan * Bool))); (match (project final error) + | Option.some problem => (ctor Result.error [(List TextToken), PrimitiveScan::Rejection] problem) + | Option.none => (ctor Result.ok [(List TextToken), PrimitiveScan::Rejection] (reverseTokens (project final tokens))))), + executable: true, + production: {effects: [allocation, overflow], targets: [rust-std]} +} + +definition{ + name: firstAtom, + parameters: [token : TextToken], + result: Nat, + body: (match token + | TextToken.TokenAtom which => which + | TextToken.TokenIsland opening innerStart innerEnd closing shown => opening), + executable: true +} + +definition{ + name: atomOr, + parameters: [atoms : (List PrimitiveScan::Atom), which : Nat], + result: PrimitiveScan::Atom, + body: (match (prim index atoms which : (Option PrimitiveScan::Atom)) + | Option.some atom => atom + | Option.none => (record PrimitiveScan::Atom {kind := (ctor PrimitiveScan::AtomClass.Whitespace), byteStart := 0, byteEnd := 0, lineStart := 1, columnStart := 1, lineEnd := 1, columnEnd := 1, text := (lit "")})), + executable: true +} + +definition{ + name: tokenOr, + parameters: [tokens : (List TextToken), position : Nat], + result: TextToken, + body: (match (prim index tokens position : (Option TextToken)) + | Option.some token => token + | Option.none => (ctor TextToken.TokenAtom 0)), + executable: true +} + +definition{ + name: wordIs, + parameters: [ + atoms : (List PrimitiveScan::Atom), + tokens : (List TextToken), + position : Nat, + expected : Bytes + ], + result: Bool, + body: (match (prim index tokens position : (Option TextToken)) + | Option.some token => (match token + | TextToken.TokenAtom which => (match (prim index atoms which : (Option PrimitiveScan::Atom)) + | Option.some atom => (both ((PrimitiveScan::classCode atom) =? 1) (prim equal (project atom text) expected : Bool)) + | Option.none => false) + | TextToken.TokenIsland opening innerStart innerEnd closing shown => false) + | Option.none => false), + executable: true +} + +definition{ + name: islandIs, + parameters: [tokens : (List TextToken), position : Nat], + result: Bool, + body: (match (prim index tokens position : (Option TextToken)) + | Option.some token => (match token + | TextToken.TokenAtom which => false + | TextToken.TokenIsland opening innerStart innerEnd closing shown => true) + | Option.none => false), + executable: true +} + +definition{ + name: commaIs, + parameters: [atoms : (List PrimitiveScan::Atom), tokens : (List TextToken), position : Nat], + result: Bool, + body: (match (prim index tokens position : (Option TextToken)) + | Option.some token => (match token + | TextToken.TokenAtom which => (match (prim index atoms which : (Option PrimitiveScan::Atom)) + | Option.some atom => (both ((PrimitiveScan::classCode atom) =? 4) (prim equal (project atom text) (lit ",") : Bool)) + | Option.none => false) + | TextToken.TokenIsland opening innerStart innerEnd closing shown => false) + | Option.none => false), + executable: true +} + +definition{ + name: keywordAt, + parameters: [tokens : (List TextToken), position : Nat, entry : Bytes], + result: Keyword, + body: (record Keyword {atom := (firstAtom (tokenOr tokens position)), entry := entry}), + executable: true +} + +definition{ + name: forbiddenWords, + parameters: [], + result: (List Bytes), + body: (cons (lit "aesop") (cons (lit "admit") (cons (lit "cases") (cons (lit "constructor") (cons (lit "induction") (cons (lit "all_goals") (cons (lit "any_goals") (cons (lit "apply?") (cons (lit "assumption") (cons (lit "case") (cons (lit "decide") (cons (lit "exact") (cons (lit "exact?") (cons (lit "first") (cons (lit "intro") (cons (lit "linarith") (cons (lit "native_decide") (cons (lit "next") (cons (lit "omega") (cons (lit "refine") (cons (lit "repeat") (cons (lit "rfl") (cons (lit "rw") (cons (lit "rwa") (cons (lit "simp") (cons (lit "simp_all") (cons (lit "simpa") (cons (lit "sorry") (cons (lit "tauto") (cons (lit "trivial") (cons (lit "try") (nil Bytes)))))))))))))))))))))))))))))))), + executable: true +} + +definition{ + name: separator, + parameters: [spelled : Bytes], + result: Bool, + body: (PrimitiveScan::bytesInList spelled (cons (lit ",") (cons (lit ".") (cons (lit ";") (cons (lit ":") (cons (lit "(") (cons (lit ")") (nil Bytes)))))))), + executable: true +} + +definition{ + name: composeStep, + parameters: [atoms : (List PrimitiveScan::Atom), state : Compose], + result: (Option Compose), + body: (match (prim index atoms (project state next) : (Option PrimitiveScan::Atom)) + | Option.some atom => (let code : Nat := (PrimitiveScan::classCode atom); (if (both ((project atom byteStart) =? (project state reach)) (both (if (code =? 1) then true else (if (code =? 2) then true else (code =? 4))) (if (separator (project atom text)) then false else true))) then (ctor Option.some [Compose] (record Compose {spelled := (cat (project state spelled) (project atom text)), reach := (project atom byteEnd), next := ((project state next) + 1)})) else (ctor Option.none [Compose]))) + | Option.none => (ctor Option.none [Compose])), + executable: true +} + +definition{ + name: composedAt, + parameters: [atoms : (List PrimitiveScan::Atom), atom : PrimitiveScan::Atom, which : Nat], + result: Bytes, + body: (project (first (prim iterate_until (fun [atoms] (state : Compose) => (composeStep atoms state)) ((prim length atoms : Nat) + 1) (record Compose {spelled := (project atom text), reach := (project atom byteEnd), next := (which + 1)}) : (Compose * Bool))) spelled), + executable: true +} + +definition{ + name: forbiddenAt, + parameters: [atoms : (List PrimitiveScan::Atom), position : Nat, which : Nat], + result: (Option Found), + body: (let atom : PrimitiveScan::Atom := (atomOr atoms which); (let composed : Bytes := (composedAt atoms atom which); (let hit : Found := (record Found {word := composed, atom := which}); (if (PrimitiveScan::bytesInList composed (forbiddenWords)) then (ctor Option.some [Found] hit) else (if (both (position =? 0) (match (LexLeanStd.Text::find composed (lit "_")) + | Option.some offset => true + | Option.none => false)) then (ctor Option.some [Found] hit) else (if (both ((PrimitiveScan::classCode atom) =? 4) (if (LexLeanStd.Text::startsWith composed (lit "<;>")) then true else (prim equal (project atom text) (lit "·") : Bool))) then (ctor Option.some [Found] hit) else (ctor Option.none [Found]))))))), + executable: true +} + +definition{ + name: forbidStep, + parameters: [atoms : (List PrimitiveScan::Atom), state : Forbid, token : TextToken], + result: Forbid, + body: (let position : Nat := (project state cursor); (match (project state found) + | Option.some hit => (record Forbid {cursor := (position + 1), found := (project state found)}) + | Option.none => (match token + | TextToken.TokenAtom which => (record Forbid {cursor := (position + 1), found := (forbiddenAt atoms position which)}) + | TextToken.TokenIsland opening innerStart innerEnd closing shown => (record Forbid {cursor := (position + 1), found := (ctor Option.none [Found])})))), + executable: true +} + +definition{ + name: forbiddenForm, + parameters: [atoms : (List PrimitiveScan::Atom), tokens : (List TextToken)], + result: (Option Found), + body: (project (prim list_fold (fun [atoms] (state : Forbid) (token : TextToken) => (forbidStep atoms state token)) (record Forbid {cursor := 0, found := (ctor Option.none [Found])}) tokens : Forbid) found), + executable: true +} + +definition{ + name: failure, + parameters: [path : Bytes, atoms : (List PrimitiveScan::Atom), tokens : (List TextToken)], + result: PrimitiveScan::Rejection, + body: (match (forbiddenForm atoms tokens) + | Option.some hit => (refusal path (lit "LLF5005") (cat (cat (lit "`") (project hit word)) (lit "` is a forbidden proof form (§16.12): raw tactics, automation, tactic combinators, and proof holes are never accepted")) (atomOr atoms (project hit atom))) + | Option.none => (let message : Bytes := (lit "not a registered proof sentence; the exact simple sentences are fixed (§16.2)"); (match (prim index tokens 0 : (Option TextToken)) + | Option.some token => (refusal path (lit "LLF5005") message (atomOr atoms (firstAtom token))) + | Option.none => (record PrimitiveScan::Rejection {code := (lit "LLF5005"), message := message, place := (ctor Option.some [PrimitiveScan::Place] (record PrimitiveScan::Place {path := path, byteStart := 0, byteEnd := 0, lineStart := 1, columnStart := 1, lineEnd := 1, columnEnd := 1})), help := (ctor Option.none [Bytes])})))), + executable: true +} + +definition{ + name: accepted, + parameters: [kind : SentenceKind, keywords : (List Keyword)], + result: (Result Sentence PrimitiveScan::Rejection), + body: (ctor Result.ok [Sentence, PrimitiveScan::Rejection] (record Sentence {kind := kind, keywords := keywords})), + executable: true +} + +definition{ + name: commaStep, + parameters: [atoms : (List PrimitiveScan::Atom), tokens : (List TextToken), state : Commas], + result: (Option Commas), + body: (let position : Nat := (project state cursor); (if (both (commaIs atoms tokens position) (islandIs tokens (position + 1))) then (ctor Option.some [Commas] (record Commas {cursor := (position + 2), islands := (cons (tokenOr tokens (position + 1)) (project state islands))})) else (ctor Option.none [Commas]))), + executable: true +} + +definition{ + name: assumed, + parameters: [ + atoms : (List PrimitiveScan::Atom), + tokens : (List TextToken), + count : Nat, + refused : (Result Sentence PrimitiveScan::Rejection) + ], + result: (Result Sentence PrimitiveScan::Rejection), + body: (if (islandIs tokens 1) then (let final : Commas := (first (prim iterate_until (fun [atoms, tokens] (state : Commas) => (commaStep atoms tokens state)) (count + 1) (record Commas {cursor := 2, islands := (cons (tokenOr tokens 1) (nil TextToken))}) : (Commas * Bool))); (if ((project final cursor) =? count) then (accepted (ctor SentenceKind.Assume (reverseTokens (project final islands))) (cons (keywordAt tokens 0 (lit "assume")) (nil Keyword))) else refused)) else refused), + executable: true +} + +definition{ + name: closed, + parameters: [ + atoms : (List PrimitiveScan::Atom), + tokens : (List TextToken), + count : Nat, + refused : (Result Sentence PrimitiveScan::Rejection) + ], + result: (Result Sentence PrimitiveScan::Rejection), + body: (if (both (wordIs atoms tokens 1 (lit "the")) (wordIs atoms tokens 2 (lit "goal"))) then (let opening : (List Keyword) := (cons (keywordAt tokens 0 (lit "close")) (cons (keywordAt tokens 1 (lit "the")) (cons (keywordAt tokens 2 (lit "goal")) (nil Keyword)))); (if (wordIs atoms tokens 3 (lit "with")) then (if (both (islandIs tokens 4) (count =? 5)) then (accepted (ctor SentenceKind.CloseWith (tokenOr tokens 4)) (prim append opening (cons (keywordAt tokens 3 (lit "with")) (nil Keyword)) : (List Keyword))) else refused) else (if (both (wordIs atoms tokens 3 (lit "by")) (both (wordIs atoms tokens 4 (lit "reflexivity")) (count =? 5))) then (accepted (ctor SentenceKind.CloseByReflexivity) (prim append opening (cons (keywordAt tokens 3 (lit "by")) (cons (keywordAt tokens 4 (lit "reflexivity")) (nil Keyword))) : (List Keyword))) else refused))) else refused), + executable: true +} + +definition{ + name: selected, + parameters: [ + atoms : (List PrimitiveScan::Atom), + tokens : (List TextToken), + count : Nat, + refused : (Result Sentence PrimitiveScan::Rejection) + ], + result: (Result Sentence PrimitiveScan::Rejection), + body: (if (wordIs atoms tokens 1 (lit "the")) then (let left : Bool := (wordIs atoms tokens 2 (lit "left")); (let right : Bool := (if left then false else (wordIs atoms tokens 2 (lit "right"))); (if (both (if left then true else right) (both (wordIs atoms tokens 3 (lit "alternative")) (count =? 4))) then (accepted (if left then (ctor SentenceKind.SelectLeft) else (ctor SentenceKind.SelectRight)) (cons (keywordAt tokens 0 (lit "select")) (cons (keywordAt tokens 1 (lit "the")) (cons (keywordAt tokens 2 (if left then (lit "left") else (lit "right"))) (cons (keywordAt tokens 3 (lit "alternative")) (nil Keyword)))))) else refused))) else refused), + executable: true +} + +definition{ + name: parseProofSentence, + parameters: [path : Bytes, atoms : (List PrimitiveScan::Atom), tokens : (List TextToken)], + result: (Result Sentence PrimitiveScan::Rejection), + body: (let count : Nat := (prim length tokens : Nat); (let refused : (Result Sentence PrimitiveScan::Rejection) := (ctor Result.error [Sentence, PrimitiveScan::Rejection] (failure path atoms tokens)); (if (wordIs atoms tokens 0 (lit "Assume")) then (assumed atoms tokens count refused) else (if (wordIs atoms tokens 0 (lit "Apply")) then (if (both (islandIs tokens 1) (count =? 2)) then (accepted (ctor SentenceKind.Apply (tokenOr tokens 1)) (cons (keywordAt tokens 0 (lit "apply-verb")) (nil Keyword))) else refused) else (if (wordIs atoms tokens 0 (lit "Close")) then (closed atoms tokens count refused) else (if (wordIs atoms tokens 0 (lit "Use")) then (if (both (islandIs tokens 1) (both (wordIs atoms tokens 2 (lit "as")) (both (wordIs atoms tokens 3 (lit "the")) (both (wordIs atoms tokens 4 (lit "witness")) (count =? 5))))) then (accepted (ctor SentenceKind.Witness (tokenOr tokens 1)) (cons (keywordAt tokens 0 (lit "use")) (cons (keywordAt tokens 2 (lit "as")) (cons (keywordAt tokens 3 (lit "the")) (cons (keywordAt tokens 4 (lit "witness")) (nil Keyword)))))) else refused) else (if (wordIs atoms tokens 0 (lit "Select")) then (selected atoms tokens count refused) else refused))))))), + executable: true, + production: {effects: [allocation, overflow], targets: [rust-std]} +} +\end{semanticmodule} +\end{lexlean} diff --git a/bootstrap/src/PropositionTerms.lex.tex b/bootstrap/src/PropositionTerms.lex.tex new file mode 100644 index 0000000000..bf5129b5c8 --- /dev/null +++ b/bootstrap/src/PropositionTerms.lex.tex @@ -0,0 +1,755 @@ +\begin{lexlean}{PropositionTerms} +\useglossary{lexlean.std.nat@1.2.0} +\importmodule{GrammarProof} +\importmodule{LexLeanStd.Numeral} +\importmodule{PrimitiveScan} +\title{Natural number addition} +\begin{semanticmodule} +structure{ + name: TEdge, + type_parameters: [], + parameters: [], + fields: [ + {name: form, type: Nat}, + {name: stop, type: Nat}, + {name: category, type: Nat}, + {name: inflected, type: Bool} + ] +} + +inductive{ + name: TypePhrase, + type_parameters: [], + parameters: [], + constructors: [ + {name: TNoun, fields: [(List Nat), Nat, Nat]}, + {name: TMath, fields: [GrammarProof::TextToken]} + ] +} + +structure{ + name: Binder, + type_parameters: [], + parameters: [], + fields: [ + {name: phrase, type: TypePhrase}, + {name: spelling, type: Bytes}, + {name: island, type: GrammarProof::TextToken}, + {name: identStart, type: Nat}, + {name: identEnd, type: Nat}, + {name: keywords, type: (List GrammarProof::Keyword)} + ] +} + +structure{ + name: BAlt, + type_parameters: [], + parameters: [], + fields: [{name: upto, type: Nat}, {name: binder, type: Binder}] +} + +inductive{ + name: Term, + type_parameters: [], + parameters: [], + constructors: [ + {name: TIsland, fields: [GrammarProof::TextToken]}, + {name: TNounOf, fields: [(List Nat), Nat, Nat, Term, (List GrammarProof::Keyword)]}, + {name: TArgNil, fields: []}, + {name: TArgCons, fields: [Term, Term]} + ] +} + +structure{ + name: TAlt, + type_parameters: [], + parameters: [], + fields: [{name: upto, type: Nat}, {name: term, type: Term}] +} + +structure{ + name: Bud, + type_parameters: [], + parameters: [], + fields: [{name: states, type: Nat}, {name: edges, type: Nat}, {name: memo, type: (List Nat)}] +} + +structure{ + name: Ctx, + type_parameters: [], + parameters: [], + fields: [ + {name: path, type: Bytes}, + {name: atoms, type: (List PrimitiveScan::Atom)}, + {name: tokens, type: (List GrammarProof::TextToken)}, + {name: edges, type: (List (List TEdge))}, + {name: maxStates, type: Nat}, + {name: maxDepth, type: Nat}, + {name: maxEdges, type: Nat} + ] +} + +structure{ + name: Looked, + type_parameters: [], + parameters: [], + fields: [ + {name: bud, type: Bud}, + {name: found, type: (List TEdge)}, + {name: error, type: (Option PrimitiveScan::Rejection)} + ] +} + +structure{ + name: Match, + type_parameters: [], + parameters: [], + fields: [ + {name: form, type: Nat}, + {name: category, type: Nat}, + {name: inflected, type: Bool}, + {name: upto, type: Nat} + ] +} + +structure{ + name: Matched, + type_parameters: [], + parameters: [], + fields: [ + {name: bud, type: Bud}, + {name: found, type: (List Match)}, + {name: error, type: (Option PrimitiveScan::Rejection)} + ] +} + +inductive{ + name: Frame, + type_parameters: [], + parameters: [], + constructors: [ + {name: XStart, fields: [Nat, Nat]}, + {name: XMatches, fields: [Nat, Nat, GrammarProof::Keyword, (List TAlt), (List Match)]}, + { + name: XAfterFirst, + fields: [ + Nat, + Nat, + GrammarProof::Keyword, + (List TAlt), + (List Match), + Match, + GrammarProof::Keyword + ] + }, + { + name: XFirsts, + fields: [ + Nat, + Nat, + GrammarProof::Keyword, + (List TAlt), + (List Match), + Match, + GrammarProof::Keyword, + (List TAlt) + ] + }, + { + name: XAfterSecond, + fields: [ + Nat, + Nat, + GrammarProof::Keyword, + (List TAlt), + (List Match), + Match, + GrammarProof::Keyword, + (List TAlt), + TAlt, + GrammarProof::Keyword + ] + } + ] +} + +structure{ + name: Mach, + type_parameters: [], + parameters: [], + fields: [ + {name: stack, type: (List Frame)}, + {name: returned, type: (List TAlt)}, + {name: error, type: (Option PrimitiveScan::Rejection)}, + {name: bud, type: Bud} + ] +} + +structure{ + name: Scan, + type_parameters: [], + parameters: [], + fields: [ + {name: bud, type: Bud}, + {name: found, type: (Option PrimitiveScan::Rejection)}, + {name: error, type: (Option PrimitiveScan::Rejection)} + ] +} + +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: both, + parameters: [left : Bool, right : Bool], + result: Bool, + body: (if left then right else false), + executable: true +} + +definition{ + name: same, + parameters: [left : Bytes, right : Bytes], + result: Bool, + body: (prim equal left right : Bool), + executable: true +} + +definition{ + name: num, + parameters: [number : Nat], + result: Bytes, + body: (LexLeanStd.Numeral::decimal number), + executable: true +} + +definition{ + name: natIn, + parameters: [needle : Nat, items : (List Nat)], + result: Bool, + body: (prim list_fold (fun [needle] (found : Bool) (item : Nat) => (if found then true else (item =? needle))) false items : Bool), + executable: true +} + +definition{ + name: keywordEntry, + parameters: [word : Bytes], + result: (Option Bytes), + body: (if (if (same word (lit "a")) then true else (same word (lit "A"))) then (ctor Option.some [Bytes] (lit "a")) else (if (if (same word (lit "an")) then true else (same word (lit "An"))) then (ctor Option.some [Bytes] (lit "an")) else (if (same word (lit "and")) then (ctor Option.some [Bytes] (lit "and")) else (if (same word (lit "as")) then (ctor Option.some [Bytes] (lit "as")) else (if (same word (lit "defined")) then (ctor Option.some [Bytes] (lit "defined")) else (if (same word (lit "every")) then (ctor Option.some [Bytes] (lit "every")) else (if (same word (lit "exactly")) then (ctor Option.some [Bytes] (lit "exactly")) else (if (same word (lit "exists")) then (ctor Option.some [Bytes] (lit "exists")) else (if (if (same word (lit "for")) then true else (same word (lit "For"))) then (ctor Option.some [Bytes] (lit "for")) else (if (same word (lit "holds")) then (ctor Option.some [Bytes] (lit "holds")) else (if (if (same word (lit "if")) then true else (same word (lit "If"))) then (ctor Option.some [Bytes] (lit "if")) else (if (same word (lit "implies")) then (ctor Option.some [Bytes] (lit "implies")) else (if (same word (lit "is")) then (ctor Option.some [Bytes] (lit "is")) else (if (if (same word (lit "not")) then true else (same word (lit "Not"))) then (ctor Option.some [Bytes] (lit "not")) else (if (same word (lit "one")) then (ctor Option.some [Bytes] (lit "one")) else (if (same word (lit "only")) then (ctor Option.some [Bytes] (lit "only")) else (if (same word (lit "or")) then (ctor Option.some [Bytes] (lit "or")) else (if (same word (lit "such")) then (ctor Option.some [Bytes] (lit "such")) else (if (same word (lit "that")) then (ctor Option.some [Bytes] (lit "that")) else (if (if (same word (lit "the")) then true else (same word (lit "The"))) then (ctor Option.some [Bytes] (lit "the")) else (if (same word (lit "then")) then (ctor Option.some [Bytes] (lit "then")) else (if (if (same word (lit "there")) then true else (same word (lit "There"))) then (ctor Option.some [Bytes] (lit "there")) else (if (same word (lit "when")) then (ctor Option.some [Bytes] (lit "when")) else (if (same word (lit "of")) then (ctor Option.some [Bytes] (lit "of")) else (ctor Option.none [Bytes]))))))))))))))))))))))))), + executable: true, + production: {effects: [allocation], targets: [rust-std]} +} + +definition{ + name: proofKeyword, + parameters: [word : Bytes], + result: Bool, + body: (PrimitiveScan::bytesInList word (cons (lit "Assume") (cons (lit "Apply") (cons (lit "Close") (cons (lit "Select") (cons (lit "Use") (cons (lit "alternative") (cons (lit "as") (cons (lit "by") (cons (lit "goal") (cons (lit "left") (cons (lit "reflexivity") (cons (lit "right") (cons (lit "the") (cons (lit "with") (cons (lit "witness") (nil Bytes))))))))))))))))), + executable: true +} + +definition{ + name: atomOr, + parameters: [ctx : Ctx, position : Nat], + result: PrimitiveScan::Atom, + body: (match (prim index (project ctx atoms) position : (Option PrimitiveScan::Atom)) + | Option.some atom => atom + | Option.none => (record PrimitiveScan::Atom {kind := (ctor PrimitiveScan::AtomClass.Whitespace), byteStart := 0, byteEnd := 0, lineStart := 1, columnStart := 1, lineEnd := 1, columnEnd := 1, text := (lit "")})), + executable: true +} + +definition{ + name: tokenAt, + parameters: [ctx : Ctx, position : Nat], + result: (Option GrammarProof::TextToken), + body: (prim index (project ctx tokens) position : (Option GrammarProof::TextToken)), + executable: true +} + +definition{ + name: lastAtom, + parameters: [token : GrammarProof::TextToken], + result: Nat, + body: (match token + | GrammarProof::TextToken.TokenAtom which => which + | GrammarProof::TextToken.TokenIsland opening innerStart innerEnd closing shown => closing), + executable: true +} + +definition{ + name: placeAt, + parameters: [ctx : Ctx, position : Nat], + result: PrimitiveScan::Place, + body: (PrimitiveScan::placeOfAtom (project ctx path) (atomOr ctx position)), + executable: true +} + +definition{ + name: placed, + parameters: [code : Bytes, message : Bytes, place : (Option PrimitiveScan::Place)], + result: PrimitiveScan::Rejection, + body: (record PrimitiveScan::Rejection {code := code, message := message, place := place, help := (ctor Option.none [Bytes])}), + executable: true +} + +definition{ + name: wordAt, + parameters: [ctx : Ctx, position : Nat], + result: (Option Nat), + body: (match (tokenAt ctx position) + | Option.some token => (match token + | GrammarProof::TextToken.TokenAtom which => (if ((PrimitiveScan::classCode (atomOr ctx which)) =? 1) then (ctor Option.some [Nat] which) else (ctor Option.none [Nat])) + | GrammarProof::TextToken.TokenIsland opening innerStart innerEnd closing shown => (ctor Option.none [Nat])) + | Option.none => (ctor Option.none [Nat])), + executable: true +} + +definition{ + name: isWord, + parameters: [ctx : Ctx, position : Nat, word : Bytes], + result: (Option GrammarProof::Keyword), + body: (match (wordAt ctx position) + | Option.some which => (if (same (project (atomOr ctx which) text) word) then (match (keywordEntry word) + | Option.some entry => (ctor Option.some [GrammarProof::Keyword] (record GrammarProof::Keyword {atom := which, entry := entry})) + | Option.none => (ctor Option.none [GrammarProof::Keyword])) else (ctor Option.none [GrammarProof::Keyword])) + | Option.none => (ctor Option.none [GrammarProof::Keyword])), + executable: true +} + +definition{ + name: islandAt, + parameters: [ctx : Ctx, position : Nat], + result: (Option GrammarProof::TextToken), + body: (match (tokenAt ctx position) + | Option.some token => (match token + | GrammarProof::TextToken.TokenAtom which => (ctor Option.none [GrammarProof::TextToken]) + | GrammarProof::TextToken.TokenIsland opening innerStart innerEnd closing shown => (ctor Option.some [GrammarProof::TextToken] token)) + | Option.none => (ctor Option.none [GrammarProof::TextToken])), + executable: true +} + +definition{ + name: chargeState, + parameters: [ctx : Ctx, bud : Bud], + result: (Bud * (Option PrimitiveScan::Rejection)), + body: (let states : Nat := ((project bud states) + 1); (let after : Bud := (record Bud {states := states, edges := (project bud edges), memo := (project bud memo)}); (if ((project ctx maxStates) (ctor Option.some [PrimitiveScan::Place] (placeAt ctx (GrammarProof::firstAtom token))) + | Option.none => (ctor Option.none [PrimitiveScan::Place])))) else (ctor Option.none [PrimitiveScan::Rejection])), + executable: true +} + +definition{ + name: edgesAt, + parameters: [ctx : Ctx, bud : Bud, position : Nat], + result: Looked, + body: (let found : (List TEdge) := (match (prim index (project ctx edges) position : (Option (List TEdge))) + | Option.some listed => listed + | Option.none => (nil TEdge)); (if (natIn position (project bud memo)) then (record Looked {bud := bud, found := found, error := (ctor Option.none [PrimitiveScan::Rejection])}) else (let count : Nat := ((project bud edges) + (prim length found : Nat)); (if ((project ctx maxEdges) (match (tokenAt ctx position) + | Option.some token => (if ((GrammarProof::firstAtom token) (ctor Option.none [Nat]))) ((prim length (project ctx tokens) : Nat) + 1) from : (Nat * Bool))), + executable: true +} + +definition{ + name: formMatches, + parameters: [ctx : Ctx, bud : Bud, position : Nat, low : Nat, high : Nat], + result: Matched, + body: (match (tokenAt ctx position) + | Option.some token => (match token + | GrammarProof::TextToken.TokenAtom which => (let looked : Looked := (edgesAt ctx bud which); (match (project looked error) + | Option.some problem => (record Matched {bud := (project looked bud), found := (nil Match), error := (ctor Option.some [PrimitiveScan::Rejection] problem)}) + | Option.none => (record Matched {bud := (project looked bud), found := (prim list_fold (fun [ctx, high, low, position] (out : (List Match)) (edge : TEdge) => (prim append out (if (both (low <=? (project edge category)) ((project edge category) <=? high)) then (cons (record Match {form := (project edge form), category := (project edge category), inflected := (project edge inflected), upto := (tokenAfter ctx position (project edge stop))}) (nil Match)) else (nil Match)) : (List Match))) (nil Match) (project looked found) : (List Match)), error := (ctor Option.none [PrimitiveScan::Rejection])}))) + | GrammarProof::TextToken.TokenIsland opening innerStart innerEnd closing shown => (record Matched {bud := bud, found := (nil Match), error := (ctor Option.none [PrimitiveScan::Rejection])})) + | Option.none => (record Matched {bud := bud, found := (nil Match), error := (ctor Option.none [PrimitiveScan::Rejection])})), + executable: true +} + +definition{ + name: atomRange, + parameters: [ctx : Ctx, start : Nat, stop : Nat], + result: (Nat * Nat), + body: (let firstToken : GrammarProof::TextToken := (match (tokenAt ctx start) + | Option.some token => token + | Option.none => (ctor GrammarProof::TextToken.TokenAtom 0)); (let lastIndex : Nat := (if (start token + | Option.none => (ctor GrammarProof::TextToken.TokenAtom 0)); ((GrammarProof::firstAtom firstToken), ((lastAtom lastToken) + 1))))), + executable: true +} + +definition{ + name: wsStep, + parameters: [ctx : Ctx, stop : Nat, position : Nat], + result: (Option Nat), + body: (if (both (position (wsStep ctx stop current)) ((prim length (project ctx atoms) : Nat) + 1) position : (Nat * Bool))), + executable: true +} + +definition{ + name: composeStep, + parameters: [ctx : Ctx, state : (Nat * Nat)], + result: (Option (Nat * Nat)), + body: (match (prim index (project ctx atoms) (first state) : (Option PrimitiveScan::Atom)) + | Option.some atom => (let code : Nat := (PrimitiveScan::classCode atom); (if (both ((project atom byteStart) =? (second state)) (if (if (code =? 1) then true else (code =? 2)) then true else (both (code =? 4) (if (same (project atom text) (lit "_")) then true else (same (project atom text) (lit "'")))))) then (ctor Option.some [(Nat * Nat)] (((first state) + 1), (project atom byteEnd))) else (ctor Option.none [(Nat * Nat)]))) + | Option.none => (ctor Option.none [(Nat * Nat)])), + executable: true +} + +definition{ + name: spelledFrom, + parameters: [ctx : Ctx, start : Nat, stop : Nat], + result: Bytes, + body: (first (first (prim iterate_until (fun [ctx, stop] (state : (Bytes * Nat)) => (if ((second state) (match token + | GrammarProof::TextToken.TokenAtom which => (ctor Option.none [BAlt]) + | GrammarProof::TextToken.TokenIsland opening innerStart innerEnd closing shown => (let index : Nat := (skipWs ctx innerEnd innerStart); (let atom : PrimitiveScan::Atom := (atomOr ctx index); (if (both ((PrimitiveScan::classCode atom) =? 1) (index (composeStep ctx state)) ((prim length (project ctx atoms) : Nat) + 1) ((index + 1), (project atom byteEnd)) : ((Nat * Nat) * Bool)))); (if ((skipWs ctx innerEnd identEnd) =? innerEnd) then (ctor Option.some [BAlt] (record BAlt {upto := (position + 1), binder := (record Binder {phrase := phrase, spelling := (spelledFrom ctx index identEnd), island := token, identStart := index, identEnd := identEnd, keywords := (nil GrammarProof::Keyword)})})) else (ctor Option.none [BAlt]))) else (ctor Option.none [BAlt]))))) + | Option.none => (ctor Option.none [BAlt])), + executable: true +} + +definition{ + name: withKeywords, + parameters: [alternative : BAlt, keywords : (List GrammarProof::Keyword)], + result: BAlt, + body: (let held : Binder := (project alternative binder); (record BAlt {upto := (project alternative upto), binder := (record Binder {phrase := (project held phrase), spelling := (project held spelling), island := (project held island), identStart := (project held identStart), identEnd := (project held identEnd), keywords := (prim append keywords (project held keywords) : (List GrammarProof::Keyword))})})), + executable: true +} + +definition{ + name: binderStart, + parameters: [ + ctx : Ctx, + state : ((List BAlt) * Matched), + start : (Nat * (List GrammarProof::Keyword)) + ], + result: ((List BAlt) * Matched), + body: (let held : Matched := (second state); (match (project held error) + | Option.some problem => state + | Option.none => (let matched : Matched := (formMatches ctx (project held bud) (first start) 0 0); (match (project matched error) + | Option.some problem => ((first state), matched) + | Option.none => (let nouns : (List BAlt) := (prim list_fold (fun [ctx, start] (out : (List BAlt)) (found : Match) => (if (project found inflected) then out else (match (binderLocal ctx (project found upto) (ctor TypePhrase.TNoun (cons (project found form) (nil Nat)) (first (atomRange ctx (first start) (project found upto))) (second (atomRange ctx (first start) (project found upto))))) + | Option.some alternative => (prim append out (cons (withKeywords alternative (second start)) (nil BAlt)) : (List BAlt)) + | Option.none => out))) (first state) (project matched found) : (List BAlt)); (let island : (List BAlt) := (match (islandAt ctx (first start)) + | Option.some token => (match (binderLocal ctx ((first start) + 1) (ctor TypePhrase.TMath token)) + | Option.some alternative => (cons (withKeywords alternative (second start)) (nil BAlt)) + | Option.none => (nil BAlt)) + | Option.none => (nil BAlt)); ((prim append nouns island : (List BAlt)), matched))))))), + executable: true +} + +definition{ + name: context, + parameters: [ + path : Bytes, + atoms : (List PrimitiveScan::Atom), + tokens : (List GrammarProof::TextToken), + edges : (List (List TEdge)), + maxEdges : Nat, + maxStates : Nat, + maxDepth : Nat + ], + result: Ctx, + body: (record Ctx {path := path, atoms := atoms, tokens := tokens, edges := edges, maxStates := maxStates, maxDepth := maxDepth, maxEdges := maxEdges}), + executable: true +} + +definition{ + name: fresh, + parameters: [statesBefore : Nat, edgesBefore : Nat], + result: Bud, + body: (record Bud {states := statesBefore, edges := edgesBefore, memo := (nil Nat)}), + executable: true +} + +definition{ + name: coveredStep, + parameters: [ctx : Ctx, index : Nat, state : (Nat * Scan)], + result: (Option (Nat * Scan)), + body: (let start : Nat := (first state); (let scan : Scan := (second state); (if (both (start <=? index) (match (project scan found) + | Option.some hit => false + | Option.none => (match (project scan error) + | Option.some problem => false + | Option.none => true))) then (let looked : Looked := (edgesAt ctx (project scan bud) start); (match (project looked error) + | Option.some problem => (ctor Option.some [(Nat * Scan)] ((start + 1), (record Scan {bud := (project looked bud), found := (project scan found), error := (ctor Option.some [PrimitiveScan::Rejection] problem)}))) + | Option.none => (if (prim list_fold (fun [index, start] (hit : Bool) (edge : TEdge) => (if hit then true else (both (start <=? index) (index state + | Option.none => (match (project state error) + | Option.some problem => state + | Option.none => (let atom : PrimitiveScan::Atom := (atomOr ctx index); (let code : Nat := (PrimitiveScan::classCode atom); (if (if (code =? 6) then true else (both (code =? 1) (match (keywordEntry (project atom text)) + | Option.some entry => true + | Option.none => (proofKeyword (project atom text))))) then state else (let final : Scan := (second (first (prim iterate_until (fun [ctx, index] (pair : (Nat * Scan)) => (coveredStep ctx index pair)) ((prim subtract index rangeStart : Nat) + 2) (rangeStart, (record Scan {bud := (project state bud), found := (ctor Option.none [PrimitiveScan::Rejection]), error := (ctor Option.none [PrimitiveScan::Rejection])})) : ((Nat * Scan) * Bool)))); (match (project final error) + | Option.some problem => final + | Option.none => (match (project final found) + | Option.some covered => (record Scan {bud := (project final bud), found := (ctor Option.none [PrimitiveScan::Rejection]), error := (ctor Option.none [PrimitiveScan::Rejection])}) + | Option.none => (record Scan {bud := (project final bud), found := (ctor Option.some [PrimitiveScan::Rejection] (placed (lit "LLL1004") (cat (cat (lit "`") (project atom text)) (lit "` is not a declared atom in any visible glossary")) (ctor Option.some [PrimitiveScan::Place] (placeAt ctx index)))), error := (ctor Option.none [PrimitiveScan::Rejection])}))))))))), + executable: true +} + +definition{ + name: unknownAtom, + parameters: [ + path : Bytes, + atoms : (List PrimitiveScan::Atom), + tokens : (List GrammarProof::TextToken), + edges : (List (List TEdge)), + maxEdges : Nat, + maxStates : Nat, + maxDepth : Nat, + statesBefore : Nat, + edgesBefore : Nat + ], + result: (Result (Option PrimitiveScan::Rejection) PrimitiveScan::Rejection), + body: (let ctx : Ctx := (context path atoms tokens edges maxEdges maxStates maxDepth); (let indices : (List Nat) := (prim list_fold (fun (out : (List Nat)) (token : GrammarProof::TextToken) => (match token + | GrammarProof::TextToken.TokenAtom which => (prim append out (cons which (nil Nat)) : (List Nat)) + | GrammarProof::TextToken.TokenIsland opening innerStart innerEnd closing shown => out)) (nil Nat) tokens : (List Nat)); (match (prim index indices 0 : (Option Nat)) + | Option.none => (ctor Result.ok [(Option PrimitiveScan::Rejection), PrimitiveScan::Rejection] (ctor Option.none [PrimitiveScan::Rejection])) + | Option.some rangeStart => (let final : Scan := (prim list_fold (fun [ctx, rangeStart] (state : Scan) (index : Nat) => (unknownStep ctx rangeStart state index)) (record Scan {bud := (fresh statesBefore edgesBefore), found := (ctor Option.none [PrimitiveScan::Rejection]), error := (ctor Option.none [PrimitiveScan::Rejection])}) indices : Scan); (match (project final error) + | Option.some problem => (ctor Result.error [(Option PrimitiveScan::Rejection), PrimitiveScan::Rejection] problem) + | Option.none => (ctor Result.ok [(Option PrimitiveScan::Rejection), PrimitiveScan::Rejection] (project final found))))))), + executable: true, + production: {effects: [allocation, overflow], targets: [rust-std]} +} + +definition{ + name: binderAlternatives, + parameters: [ + path : Bytes, + atoms : (List PrimitiveScan::Atom), + tokens : (List GrammarProof::TextToken), + edges : (List (List TEdge)), + maxEdges : Nat, + maxStates : Nat, + maxDepth : Nat, + statesBefore : Nat, + edgesBefore : Nat, + position : Nat, + required : Bool + ], + result: (Result (List BAlt) PrimitiveScan::Rejection), + body: (let ctx : Ctx := (context path atoms tokens edges maxEdges maxStates maxDepth); (let charge : (Bud * (Option PrimitiveScan::Rejection)) := (chargeState ctx (fresh statesBefore edgesBefore)); (match (second charge) + | Option.some problem => (ctor Result.error [(List BAlt), PrimitiveScan::Rejection] problem) + | Option.none => (let starts : (List (Nat * (List GrammarProof::Keyword))) := (if required then (prim append (match (isWord ctx position (lit "a")) + | Option.some keyword => (cons ((position + 1), (cons keyword (nil GrammarProof::Keyword))) (nil (Nat * (List GrammarProof::Keyword)))) + | Option.none => (nil (Nat * (List GrammarProof::Keyword)))) (match (isWord ctx position (lit "an")) + | Option.some keyword => (cons ((position + 1), (cons keyword (nil GrammarProof::Keyword))) (nil (Nat * (List GrammarProof::Keyword)))) + | Option.none => (nil (Nat * (List GrammarProof::Keyword)))) : (List (Nat * (List GrammarProof::Keyword)))) else (cons (position, (nil GrammarProof::Keyword)) (nil (Nat * (List GrammarProof::Keyword))))); (let final : ((List BAlt) * Matched) := (prim list_fold (fun [ctx] (state : ((List BAlt) * Matched)) (start : (Nat * (List GrammarProof::Keyword))) => (binderStart ctx state start)) ((nil BAlt), (record Matched {bud := (first charge), found := (nil Match), error := (ctor Option.none [PrimitiveScan::Rejection])})) starts : ((List BAlt) * Matched)); (match (project (second final) error) + | Option.some problem => (ctor Result.error [(List BAlt), PrimitiveScan::Rejection] problem) + | Option.none => (ctor Result.ok [(List BAlt), PrimitiveScan::Rejection] (first final)))))))), + executable: true, + production: {effects: [allocation, overflow], targets: [rust-std]} +} + +definition{ + name: nounOf, + parameters: [ + ctx : Ctx, + position : Nat, + found : Match, + args : (List Term), + keywords : (List GrammarProof::Keyword) + ], + result: Term, + body: (let range : (Nat * Nat) := (atomRange ctx (position + 1) (project found upto)); (ctor Term.TNounOf (cons (project found form) (nil Nat)) (first range) (second range) (prim list_fold (fun (rest : Term) (arg : Term) => (ctor Term.TArgCons arg rest)) (ctor Term.TArgNil) (prim list_fold (fun (turned : (List Term)) (arg : Term) => (cons arg turned)) (nil Term) args : (List Term)) : Term) keywords)), + executable: true +} + +definition{ + name: mk, + parameters: [m : Mach, stack : (List Frame), returned : (List TAlt)], + result: Mach, + body: (record Mach {stack := stack, returned := returned, error := (project m error), bud := (project m bud)}), + executable: true +} + +definition{ + name: failed, + parameters: [m : Mach, bud : Bud, problem : PrimitiveScan::Rejection], + result: Mach, + body: (record Mach {stack := (project m stack), returned := (project m returned), error := (ctor Option.some [PrimitiveScan::Rejection] problem), bud := bud}), + executable: true +} + +definition{ + name: startStep, + parameters: [ctx : Ctx, m : Mach, rest : (List Frame), position : Nat, depth : Nat], + result: Mach, + body: (let charge : (Bud * (Option PrimitiveScan::Rejection)) := (chargeState ctx (project m bud)); (match (second charge) + | Option.some problem => (failed m (first charge) problem) + | Option.none => (match (depthRefusal ctx depth position) + | Option.some problem => (failed m (first charge) problem) + | Option.none => (let alts : (List TAlt) := (match (islandAt ctx position) + | Option.some token => (cons (record TAlt {upto := (position + 1), term := (ctor Term.TIsland token)}) (nil TAlt)) + | Option.none => (nil TAlt)); (match (isWord ctx position (lit "the")) + | Option.none => (record Mach {stack := rest, returned := alts, error := (ctor Option.none [PrimitiveScan::Rejection]), bud := (first charge)}) + | Option.some theKw => (let matched : Matched := (formMatches ctx (first charge) (position + 1) 1 2); (match (project matched error) + | Option.some problem => (failed m (project matched bud) problem) + | Option.none => (record Mach {stack := (cons (ctor Frame.XMatches position depth theKw alts (project matched found)) rest), returned := (nil TAlt), error := (ctor Option.none [PrimitiveScan::Rejection]), bud := (project matched bud)})))))))), + executable: true +} + +definition{ + name: matchesStep, + parameters: [ + ctx : Ctx, + m : Mach, + rest : (List Frame), + position : Nat, + depth : Nat, + theKw : GrammarProof::Keyword, + alts : (List TAlt), + pending : (List Match) + ], + result: Mach, + body: (match pending + | List.nil => (mk m rest alts) + | List.cons found later => (match (isWord ctx (project found upto) (lit "of")) + | Option.some ofKw => (mk m (cons (ctor Frame.XStart ((project found upto) + 1) (depth + 1)) (cons (ctor Frame.XAfterFirst position depth theKw alts later found ofKw) rest)) (nil TAlt)) + | Option.none => (mk m (cons (ctor Frame.XMatches position depth theKw alts later) rest) (nil TAlt)))), + executable: true +} + +definition{ + name: firstsStep, + parameters: [ + ctx : Ctx, + m : Mach, + rest : (List Frame), + position : Nat, + depth : Nat, + theKw : GrammarProof::Keyword, + alts : (List TAlt), + pending : (List Match), + found : Match, + ofKw : GrammarProof::Keyword, + firsts : (List TAlt) + ], + result: Mach, + body: (match firsts + | List.nil => (mk m (cons (ctor Frame.XMatches position depth theKw alts pending) rest) (nil TAlt)) + | List.cons held later => (if ((project found category) =? 1) then (mk m (cons (ctor Frame.XFirsts position depth theKw (prim append alts (cons (record TAlt {upto := (project held upto), term := (nounOf ctx position found (cons (project held term) (nil Term)) (cons theKw (cons ofKw (nil GrammarProof::Keyword))))}) (nil TAlt)) : (List TAlt)) pending found ofKw later) rest) (nil TAlt)) else (match (isWord ctx (project held upto) (lit "and")) + | Option.some andKw => (mk m (cons (ctor Frame.XStart ((project held upto) + 1) (depth + 1)) (cons (ctor Frame.XAfterSecond position depth theKw alts pending found ofKw later held andKw) rest)) (nil TAlt)) + | Option.none => (mk m (cons (ctor Frame.XFirsts position depth theKw alts pending found ofKw later) rest) (nil TAlt))))), + executable: true +} + +definition{ + name: secondsStep, + parameters: [ + ctx : Ctx, + m : Mach, + rest : (List Frame), + position : Nat, + depth : Nat, + theKw : GrammarProof::Keyword, + alts : (List TAlt), + pending : (List Match), + found : Match, + ofKw : GrammarProof::Keyword, + firsts : (List TAlt), + held : TAlt, + andKw : GrammarProof::Keyword + ], + result: Mach, + body: (let folded : (Bud * ((List TAlt) * (Option PrimitiveScan::Rejection))) := (prim list_fold (fun [andKw, ctx, found, held, ofKw, position, theKw] (state : (Bud * ((List TAlt) * (Option PrimitiveScan::Rejection)))) (latter : TAlt) => (match (second (second state)) + | Option.some problem => state + | Option.none => (let charge : (Bud * (Option PrimitiveScan::Rejection)) := (chargeState ctx (first state)); (match (second charge) + | Option.some problem => ((first charge), ((first (second state)), (ctor Option.some [PrimitiveScan::Rejection] problem))) + | Option.none => ((first charge), ((prim append (first (second state)) (cons (record TAlt {upto := (project latter upto), term := (nounOf ctx position found (cons (project held term) (cons (project latter term) (nil Term))) (cons theKw (cons ofKw (cons andKw (nil GrammarProof::Keyword)))))}) (nil TAlt)) : (List TAlt)), (ctor Option.none [PrimitiveScan::Rejection]))))))) ((project m bud), (alts, (ctor Option.none [PrimitiveScan::Rejection]))) (project m returned) : (Bud * ((List TAlt) * (Option PrimitiveScan::Rejection)))); (match (second (second folded)) + | Option.some problem => (failed m (first folded) problem) + | Option.none => (record Mach {stack := (cons (ctor Frame.XFirsts position depth theKw (first (second folded)) pending found ofKw firsts) rest), returned := (nil TAlt), error := (ctor Option.none [PrimitiveScan::Rejection]), bud := (first folded)}))), + executable: true +} + +definition{ + name: machStep, + parameters: [ctx : Ctx, m : Mach], + result: (Option Mach), + body: (match (project m error) + | Option.some problem => (ctor Option.none [Mach]) + | Option.none => (match (project m stack) + | List.nil => (ctor Option.none [Mach]) + | List.cons frame rest => (ctor Option.some [Mach] (match frame + | Frame.XStart position depth => (startStep ctx m rest position depth) + | Frame.XMatches position depth theKw alts pending => (matchesStep ctx m rest position depth theKw alts pending) + | Frame.XAfterFirst position depth theKw alts pending found ofKw => (mk m (cons (ctor Frame.XFirsts position depth theKw alts pending found ofKw (project m returned)) rest) (nil TAlt)) + | Frame.XFirsts position depth theKw alts pending found ofKw firsts => (firstsStep ctx m rest position depth theKw alts pending found ofKw firsts) + | Frame.XAfterSecond position depth theKw alts pending found ofKw firsts held andKw => (secondsStep ctx m rest position depth theKw alts pending found ofKw firsts held andKw))))), + executable: true +} + +definition{ + name: termPhrase, + parameters: [ + path : Bytes, + atoms : (List PrimitiveScan::Atom), + tokens : (List GrammarProof::TextToken), + edges : (List (List TEdge)), + maxEdges : Nat, + maxStates : Nat, + maxDepth : Nat, + statesBefore : Nat, + edgesBefore : Nat, + position : Nat, + depth : Nat + ], + result: (Result (List TAlt) PrimitiveScan::Rejection), + body: (let ctx : Ctx := (context path atoms tokens edges maxEdges maxStates maxDepth); (let final : Mach := (first (prim iterate_until (fun [ctx] (m : Mach) => (machStep ctx m)) ((prim multiply (maxStates + 2) 64 : Nat) + 64) (record Mach {stack := (cons (ctor Frame.XStart position depth) (nil Frame)), returned := (nil TAlt), error := (ctor Option.none [PrimitiveScan::Rejection]), bud := (fresh statesBefore edgesBefore)}) : (Mach * Bool))); (match (project final error) + | Option.some problem => (ctor Result.error [(List TAlt), PrimitiveScan::Rejection] problem) + | Option.none => (ctor Result.ok [(List TAlt), PrimitiveScan::Rejection] (project final returned))))), + executable: true, + production: {effects: [allocation, overflow], targets: [rust-std]} +} +\end{semanticmodule} +\end{lexlean} diff --git a/bootstrap/src/SemanticPrint.lex.tex b/bootstrap/src/SemanticPrint.lex.tex new file mode 100644 index 0000000000..7c0223b23a --- /dev/null +++ b/bootstrap/src/SemanticPrint.lex.tex @@ -0,0 +1,1300 @@ +\begin{lexlean}{SemanticPrint} +\useglossary{lexlean.std.nat@1.2.0} +\importmodule{LexLeanStd.Json} +\importmodule{LexLeanStd.Order} +\importmodule{LexLeanStd.Text} +\importmodule{SurfaceDoc} +\importmodule{SurfaceGeneric} +\importmodule{SurfaceLex} +\title{Natural number addition} +\begin{semanticmodule} +inductive{ + name: Pos, + type_parameters: [], + parameters: [], + constructors: [ + {name: PTerm, fields: []}, + {name: PType, fields: []}, + {name: PMember, fields: []}, + {name: PParam, fields: []}, + {name: PNode, fields: [LexLeanStd.Json::Json]}, + {name: PAny, fields: []}, + {name: PArgs, fields: []}, + {name: PTypeArgs, fields: []}, + {name: PFunParams, fields: []}, + {name: PFnTypes, fields: []}, + {name: PNamedArgs, fields: []}, + {name: PAssigns, fields: []}, + {name: PAssign, fields: []}, + {name: PBranches, fields: []}, + {name: PBranch, fields: []} + ] +} + +inductive{ + name: Kids, + type_parameters: [], + parameters: [], + constructors: [ + {name: KSchema, fields: [(Option LexLeanStd.Json::Json)]}, + {name: KTerm, fields: [Bytes]}, + {name: KType, fields: []}, + {name: KParam, fields: []}, + {name: KAssign, fields: []}, + {name: KBranch, fields: []}, + {name: KNone, fields: []} + ] +} + +structure{ + name: Cell, + type_parameters: [], + parameters: [], + fields: [ + {name: key, type: Bytes}, + {name: value, type: LexLeanStd.Json::Json}, + {name: rest, type: LexLeanStd.Json::Json} + ] +} + +structure{ + name: Elem, + type_parameters: [], + parameters: [], + fields: [{name: value, type: LexLeanStd.Json::Json}, {name: rest, type: LexLeanStd.Json::Json}] +} + +structure{ + name: Look, + type_parameters: [], + parameters: [], + fields: [ + {name: rest, type: LexLeanStd.Json::Json}, + {name: found, type: (Option LexLeanStd.Json::Json)} + ] +} + +structure{ + name: Tally, + type_parameters: [], + parameters: [], + fields: [{name: rest, type: LexLeanStd.Json::Json}, {name: count, type: Nat}] +} + +structure{ + name: Gather, + type_parameters: [], + parameters: [], + fields: [ + {name: rest, type: LexLeanStd.Json::Json}, + {name: items, type: (List LexLeanStd.Json::Json)} + ] +} + +structure{ + name: Slash, + type_parameters: [], + parameters: [], + fields: [{name: cursor, type: Nat}, {name: after, type: Nat}] +} + +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: both, + parameters: [left : Bool, right : Bool], + result: Bool, + body: (if left then right else false), + executable: true +} + +definition{ + name: same, + parameters: [left : Bytes, right : Bytes], + result: Bool, + body: (prim equal left right : Bool), + executable: true +} + +definition{ + name: listed, + parameters: [needle : Bytes, items : (List Bytes)], + result: Bool, + body: (prim list_fold (fun [needle] (found : Bool) (item : Bytes) => (if found then true else (prim equal item needle : Bool))) false items : Bool), + executable: true +} + +definition{ + name: nextMember, + parameters: [chain : LexLeanStd.Json::Json], + result: (Option Cell), + body: (match chain + | LexLeanStd.Json::Json.JMember key value rest => (ctor Option.some [Cell] (record Cell {key := key, value := value, rest := rest})) + | LexLeanStd.Json::Json.JBool flag => (ctor Option.none [Cell]) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Option.none [Cell]) + | LexLeanStd.Json::Json.JStr text => (ctor Option.none [Cell]) + | LexLeanStd.Json::Json.JArr inner => (ctor Option.none [Cell]) + | LexLeanStd.Json::Json.JObj inner => (ctor Option.none [Cell]) + | LexLeanStd.Json::Json.JNil => (ctor Option.none [Cell]) + | LexLeanStd.Json::Json.JItem element rest => (ctor Option.none [Cell])), + executable: true +} + +definition{ + name: nextItem, + parameters: [chain : LexLeanStd.Json::Json], + result: (Option Elem), + body: (match chain + | LexLeanStd.Json::Json.JItem element rest => (ctor Option.some [Elem] (record Elem {value := element, rest := rest})) + | LexLeanStd.Json::Json.JBool flag => (ctor Option.none [Elem]) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Option.none [Elem]) + | LexLeanStd.Json::Json.JStr text => (ctor Option.none [Elem]) + | LexLeanStd.Json::Json.JArr inner => (ctor Option.none [Elem]) + | LexLeanStd.Json::Json.JObj inner => (ctor Option.none [Elem]) + | LexLeanStd.Json::Json.JNil => (ctor Option.none [Elem]) + | LexLeanStd.Json::Json.JMember key value rest => (ctor Option.none [Elem])), + executable: true +} + +definition{ + name: asObj, + parameters: [value : LexLeanStd.Json::Json], + result: (Option LexLeanStd.Json::Json), + body: (match value + | LexLeanStd.Json::Json.JObj inner => (ctor Option.some [LexLeanStd.Json::Json] inner) + | LexLeanStd.Json::Json.JBool flag => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JStr text => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JArr inner => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JNil => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JItem element rest => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JMember key member rest => (ctor Option.none [LexLeanStd.Json::Json])), + executable: true +} + +definition{ + name: asArr, + parameters: [value : LexLeanStd.Json::Json], + result: (Option LexLeanStd.Json::Json), + body: (match value + | LexLeanStd.Json::Json.JArr inner => (ctor Option.some [LexLeanStd.Json::Json] inner) + | LexLeanStd.Json::Json.JBool flag => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JStr text => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JObj inner => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JNil => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JItem element rest => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JMember key member rest => (ctor Option.none [LexLeanStd.Json::Json])), + executable: true +} + +definition{ + name: asStr, + parameters: [value : LexLeanStd.Json::Json], + result: (Option Bytes), + body: (match value + | LexLeanStd.Json::Json.JStr text => (ctor Option.some [Bytes] text) + | LexLeanStd.Json::Json.JBool flag => (ctor Option.none [Bytes]) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Option.none [Bytes]) + | LexLeanStd.Json::Json.JArr inner => (ctor Option.none [Bytes]) + | LexLeanStd.Json::Json.JObj inner => (ctor Option.none [Bytes]) + | LexLeanStd.Json::Json.JNil => (ctor Option.none [Bytes]) + | LexLeanStd.Json::Json.JItem element rest => (ctor Option.none [Bytes]) + | LexLeanStd.Json::Json.JMember key member rest => (ctor Option.none [Bytes])), + executable: true +} + +definition{ + name: asBool, + parameters: [value : LexLeanStd.Json::Json], + result: (Option Bool), + body: (match value + | LexLeanStd.Json::Json.JBool flag => (ctor Option.some [Bool] flag) + | LexLeanStd.Json::Json.JStr text => (ctor Option.none [Bool]) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Option.none [Bool]) + | LexLeanStd.Json::Json.JArr inner => (ctor Option.none [Bool]) + | LexLeanStd.Json::Json.JObj inner => (ctor Option.none [Bool]) + | LexLeanStd.Json::Json.JNil => (ctor Option.none [Bool]) + | LexLeanStd.Json::Json.JItem element rest => (ctor Option.none [Bool]) + | LexLeanStd.Json::Json.JMember key member rest => (ctor Option.none [Bool])), + executable: true +} + +definition{ + name: lookStep, + parameters: [key : Bytes, state : Look], + result: (Option Look), + body: (match (project state found) + | Option.some hit => (ctor Option.none [Look]) + | Option.none => (match (nextMember (project state rest)) + | Option.some cell => (if (same (project cell key) key) then (ctor Option.some [Look] (record Look {rest := (ctor LexLeanStd.Json::Json.JNil), found := (ctor Option.some [LexLeanStd.Json::Json] (project cell value))})) else (ctor Option.some [Look] (record Look {rest := (project cell rest), found := (ctor Option.none [LexLeanStd.Json::Json])}))) + | Option.none => (ctor Option.none [Look]))), + executable: true +} + +definition{ + name: memberIn, + parameters: [chain : LexLeanStd.Json::Json, key : Bytes], + result: (Option LexLeanStd.Json::Json), + body: (project (first (prim iterate_until (fun [key] (state : Look) => (lookStep key state)) 1000000 (record Look {rest := chain, found := (ctor Option.none [LexLeanStd.Json::Json])}) : (Look * Bool))) found), + executable: true +} + +definition{ + name: getIn, + parameters: [value : LexLeanStd.Json::Json, key : Bytes], + result: (Option LexLeanStd.Json::Json), + body: (match (asObj value) + | Option.some chain => (memberIn chain key) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json])), + executable: true +} + +definition{ + name: present, + parameters: [found : (Option LexLeanStd.Json::Json)], + result: Bool, + body: (match found | Option.some value => true | Option.none => false), + executable: true +} + +definition{ + name: strOf, + parameters: [found : (Option LexLeanStd.Json::Json)], + result: (Option Bytes), + body: (match found + | Option.some value => (asStr value) + | Option.none => (ctor Option.none [Bytes])), + executable: true +} + +definition{ + name: arrOf, + parameters: [found : (Option LexLeanStd.Json::Json)], + result: (Option LexLeanStd.Json::Json), + body: (match found + | Option.some value => (asArr value) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json])), + executable: true +} + +definition{ + name: countStep, + parameters: [state : Tally], + result: (Option Tally), + body: (match (nextMember (project state rest)) + | Option.some cell => (ctor Option.some [Tally] (record Tally {rest := (project cell rest), count := ((project state count) + 1)})) + | Option.none => (match (nextItem (project state rest)) + | Option.some elem => (ctor Option.some [Tally] (record Tally {rest := (project elem rest), count := ((project state count) + 1)})) + | Option.none => (ctor Option.none [Tally]))), + executable: true +} + +definition{ + name: countOf, + parameters: [chain : LexLeanStd.Json::Json], + result: Nat, + body: (project (first (prim iterate_until (fun (state : Tally) => (countStep state)) 1000000 (record Tally {rest := chain, count := 0}) : (Tally * Bool))) count), + executable: true +} + +definition{ + name: gatherStep, + parameters: [state : Gather], + result: (Option Gather), + body: (match (nextItem (project state rest)) + | Option.some elem => (ctor Option.some [Gather] (record Gather {rest := (project elem rest), items := (cons (project elem value) (project state items))})) + | Option.none => (ctor Option.none [Gather])), + executable: true +} + +definition{ + name: itemsOfChain, + parameters: [chain : LexLeanStd.Json::Json], + result: (List LexLeanStd.Json::Json), + body: (LexLeanStd.Order::reverseList [LexLeanStd.Json::Json] (project (first (prim iterate_until (fun (state : Gather) => (gatherStep state)) 1000000 (record Gather {rest := chain, items := (nil LexLeanStd.Json::Json)}) : (Gather * Bool))) items)), + executable: true +} + +definition{ + name: hasAll, + parameters: [chain : LexLeanStd.Json::Json, keys : (List Bytes)], + result: Bool, + body: (prim list_fold (fun [chain] (fine : Bool) (key : Bytes) => (if fine then (present (memberIn chain key)) else false)) true keys : Bool), + executable: true +} + +definition{ + name: exactly, + parameters: [chain : LexLeanStd.Json::Json, keys : (List Bytes)], + result: Bool, + body: (both ((countOf chain) =? (prim length keys : Nat)) (hasAll chain keys)), + executable: true +} + +definition{ + name: keysWith, + parameters: [chain : LexLeanStd.Json::Json, keys : (List Bytes)], + result: Bool, + body: (both ((countOf chain) =? ((prim length keys : Nat) + (if (present (memberIn chain (lit "type_arguments"))) then 1 else 0))) (hasAll chain keys)), + executable: true +} + +definition{ + name: k1, + parameters: [a : String], + result: (List Bytes), + body: (cons (lit "kind") (cons (lit a) (nil Bytes))), + executable: true +} + +definition{ + name: k2, + parameters: [a : String, b : String], + result: (List Bytes), + body: (cons (lit "kind") (cons (lit a) (cons (lit b) (nil Bytes)))), + executable: true +} + +definition{ + name: k3, + parameters: [a : String, b : String, c : String], + result: (List Bytes), + body: (cons (lit "kind") (cons (lit a) (cons (lit b) (cons (lit c) (nil Bytes))))), + executable: true +} + +definition{ + name: slashStep, + parameters: [text : Bytes, state : Slash], + result: (Option Slash), + body: (let position : Nat := (project state cursor); (match (LexLeanStd.Text::byteAt text position) + | Option.some byte => (ctor Option.some [Slash] (record Slash {cursor := (position + 1), after := (if ((prim to_nat byte : Nat) =? 47) then (position + 1) else (project state after))})) + | Option.none => (ctor Option.none [Slash]))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: lastSegment, + parameters: [text : Bytes], + result: Bytes, + body: (let after : Nat := (project (first (prim iterate_until (fun [text] (state : Slash) => (slashStep text state)) ((prim length text : Nat) + 1) (record Slash {cursor := 0, after := 0}) : (Slash * Bool))) after); (LexLeanStd.Text::sliceOr text after (prim subtract (prim length text : Nat) after : Nat))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: defNamed, + parameters: [schema : LexLeanStd.Json::Json, name : Bytes], + result: (Option LexLeanStd.Json::Json), + body: (match (getIn schema (lit "$defs")) + | Option.some defs => (getIn defs name) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json])), + executable: true +} + +definition{ + name: resolveStep, + parameters: [schema : LexLeanStd.Json::Json, node : LexLeanStd.Json::Json], + result: (Option LexLeanStd.Json::Json), + body: (match (strOf (getIn node (lit "$ref"))) + | Option.some reference => (defNamed schema (lastSegment reference)) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json])), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: resolve, + parameters: [schema : LexLeanStd.Json::Json, node : LexLeanStd.Json::Json], + result: LexLeanStd.Json::Json, + body: (first (prim iterate_until (fun [schema] (current : LexLeanStd.Json::Json) => (resolveStep schema current)) 4096 node : (LexLeanStd.Json::Json * Bool))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: classify, + parameters: [schema : LexLeanStd.Json::Json, node : LexLeanStd.Json::Json], + result: Pos, + body: (match (strOf (getIn node (lit "$ref"))) + | Option.some reference => (let segment : Bytes := (lastSegment reference); (if (same segment (lit "term")) then (ctor Pos.PTerm) else (if (same segment (lit "type")) then (ctor Pos.PType) else (if (same segment (lit "member")) then (ctor Pos.PMember) else (if (same segment (lit "parameter")) then (ctor Pos.PParam) else (ctor Pos.PNode (resolve schema node))))))) + | Option.none => (ctor Pos.PNode node)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: itemsOf, + parameters: [schema : LexLeanStd.Json::Json, node : LexLeanStd.Json::Json], + result: Pos, + body: (match (getIn (resolve schema node) (lit "items")) + | Option.some items => (classify schema items) + | Option.none => (ctor Pos.PAny)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: memberOf, + parameters: [ + schema : LexLeanStd.Json::Json, + object : (Option LexLeanStd.Json::Json), + key : Bytes + ], + result: Pos, + body: (match object + | Option.some node => (match (getIn (resolve schema node) (lit "properties")) + | Option.some properties => (match (getIn properties key) + | Option.some member => (classify schema member) + | Option.none => (ctor Pos.PAny)) + | Option.none => (ctor Pos.PAny)) + | Option.none => (ctor Pos.PAny)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: tagIs, + parameters: [alternative : LexLeanStd.Json::Json, kind : Bytes], + result: Bool, + body: (match (getIn alternative (lit "properties")) + | Option.some properties => (match (getIn properties (lit "kind")) + | Option.some tag => (if (match (strOf (getIn tag (lit "const"))) + | Option.some constant => (same constant kind) + | Option.none => false) then true else (match (arrOf (getIn tag (lit "enum"))) + | Option.some kinds => (prim list_fold (fun [kind] (found : Bool) (item : LexLeanStd.Json::Json) => (if found then true else (match (asStr item) + | Option.some text => (same text kind) + | Option.none => false))) false (itemsOfChain kinds) : Bool) + | Option.none => false)) + | Option.none => false) + | Option.none => false), + executable: true +} + +definition{ + name: alternativeOf, + parameters: [schema : LexLeanStd.Json::Json, union : LexLeanStd.Json::Json, kind : Bytes], + result: (Option LexLeanStd.Json::Json), + body: (match (arrOf (getIn (resolve schema union) (lit "oneOf"))) + | Option.some alternatives => (prim list_fold (fun [kind, schema] (found : (Option LexLeanStd.Json::Json)) (item : LexLeanStd.Json::Json) => (match found + | Option.some hit => found + | Option.none => (let resolved : LexLeanStd.Json::Json := (resolve schema item); (if (tagIs resolved kind) then (ctor Option.some [LexLeanStd.Json::Json] resolved) else (ctor Option.none [LexLeanStd.Json::Json]))))) (ctor Option.none [LexLeanStd.Json::Json]) (itemsOfChain alternatives) : (Option LexLeanStd.Json::Json)) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json])), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: isUnion, + parameters: [schema : LexLeanStd.Json::Json, node : LexLeanStd.Json::Json], + result: Bool, + body: (match (arrOf (getIn (resolve schema node) (lit "oneOf"))) + | Option.some alternatives => (prim list_fold (fun [schema] (fine : Bool) (item : LexLeanStd.Json::Json) => (if fine then (match (getIn (resolve schema item) (lit "properties")) + | Option.some properties => (present (getIn properties (lit "kind"))) + | Option.none => false) else false)) true (itemsOfChain alternatives) : Bool) + | Option.none => false), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: unionOf, + parameters: [schema : LexLeanStd.Json::Json, pos : Pos], + result: (Option LexLeanStd.Json::Json), + body: (match pos + | Pos.PTerm => (defNamed schema (lit "term")) + | Pos.PType => (defNamed schema (lit "type")) + | Pos.PNode node => (if (isUnion schema node) then (ctor Option.some [LexLeanStd.Json::Json] node) else (ctor Option.none [LexLeanStd.Json::Json])) + | Pos.PMember => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PParam => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PAny => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PArgs => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PTypeArgs => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PFunParams => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PFnTypes => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PNamedArgs => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PAssigns => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PAssign => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PBranches => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PBranch => (ctor Option.none [LexLeanStd.Json::Json])), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: schemaOf, + parameters: [schema : LexLeanStd.Json::Json, chain : LexLeanStd.Json::Json, pos : Pos], + result: (Option LexLeanStd.Json::Json), + body: (let tag : (Option Bytes) := (SurfaceGeneric::kindTag chain); (match pos + | Pos.PTerm => (match tag + | Option.some kind => (match (unionOf schema pos) + | Option.some union => (alternativeOf schema union kind) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json])) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json])) + | Pos.PType => (match tag + | Option.some kind => (match (unionOf schema pos) + | Option.some union => (alternativeOf schema union kind) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json])) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json])) + | Pos.PNode node => (match tag + | Option.some kind => (if (isUnion schema node) then (alternativeOf schema node kind) else (ctor Option.some [LexLeanStd.Json::Json] node)) + | Option.none => (ctor Option.some [LexLeanStd.Json::Json] node)) + | Pos.PParam => (defNamed schema (lit "parameter")) + | Pos.PMember => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PAny => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PArgs => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PTypeArgs => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PFunParams => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PFnTypes => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PNamedArgs => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PAssigns => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PAssign => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PBranches => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PBranch => (ctor Option.none [LexLeanStd.Json::Json]))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: reservedTerm, + parameters: [], + result: (List Bytes), + body: (cons (lit "let") (cons (lit "if") (cons (lit "match") (cons (lit "fun") (cons (lit "forall") (cons (lit "nil") (cons (lit "cons") (cons (lit "first") (cons (lit "second") (cons (lit "not") (cons (lit "apply") (cons (lit "ctor") (cons (lit "record") (cons (lit "project") (cons (lit "prim") (cons (lit "ref") (cons (lit "checked") (cons (lit "then") (cons (lit "else") (cons (lit "with") (cons (lit "true") (nil Bytes)))))))))))))))))))))), + executable: true +} + +definition{ + name: plain, + parameters: [name : Bytes], + result: Bool, + body: (both (SurfaceGeneric::isBare name) (both (if (listed name (reservedTerm)) then false else true) (if (same name (lit "false")) then false else true))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: nullarySpelling, + parameters: [kind : Bytes], + result: (Option Bytes), + body: (if (same kind (lit "bool")) then (ctor Option.some [Bytes] (lit "Bool")) else (if (same kind (lit "bytes")) then (ctor Option.some [Bytes] (lit "Bytes")) else (if (same kind (lit "int")) then (ctor Option.some [Bytes] (lit "Int")) else (if (same kind (lit "int8")) then (ctor Option.some [Bytes] (lit "Int8")) else (if (same kind (lit "int16")) then (ctor Option.some [Bytes] (lit "Int16")) else (if (same kind (lit "int32")) then (ctor Option.some [Bytes] (lit "Int32")) else (if (same kind (lit "int64")) then (ctor Option.some [Bytes] (lit "Int64")) else (if (same kind (lit "nat")) then (ctor Option.some [Bytes] (lit "Nat")) else (if (same kind (lit "ordering")) then (ctor Option.some [Bytes] (lit "Ordering")) else (if (same kind (lit "prop")) then (ctor Option.some [Bytes] (lit "Prop")) else (if (same kind (lit "string")) then (ctor Option.some [Bytes] (lit "String")) else (if (same kind (lit "type")) then (ctor Option.some [Bytes] (lit "Type")) else (if (same kind (lit "uint8")) then (ctor Option.some [Bytes] (lit "UInt8")) else (if (same kind (lit "uint16")) then (ctor Option.some [Bytes] (lit "UInt16")) else (if (same kind (lit "uint32")) then (ctor Option.some [Bytes] (lit "UInt32")) else (if (same kind (lit "uint64")) then (ctor Option.some [Bytes] (lit "UInt64")) else (if (same kind (lit "unit")) then (ctor Option.some [Bytes] (lit "Unit")) else (if (same kind (lit "contract_violation")) then (ctor Option.some [Bytes] (lit "ContractViolation")) else (if (same kind (lit "reasoning_failure")) then (ctor Option.some [Bytes] (lit "ReasoningFailure")) else (ctor Option.none [Bytes])))))))))))))))))))), + executable: true +} + +definition{ + name: reservedType, + parameters: [word : Bytes], + result: Bool, + body: (listed word (cons (lit "Bool") (cons (lit "Bytes") (cons (lit "Int") (cons (lit "Int8") (cons (lit "Int16") (cons (lit "Int32") (cons (lit "Int64") (cons (lit "Nat") (cons (lit "Ordering") (cons (lit "Prop") (cons (lit "String") (cons (lit "Type") (cons (lit "UInt8") (cons (lit "UInt16") (cons (lit "UInt32") (cons (lit "UInt64") (cons (lit "Unit") (cons (lit "ContractViolation") (cons (lit "ReasoningFailure") (cons (lit "List") (cons (lit "Option") (cons (lit "Result") (cons (lit "Set") (cons (lit "Map") (cons (lit "Fn") (nil Bytes))))))))))))))))))))))))))), + executable: true +} + +definition{ + name: integerSpelling, + parameters: [representation : Bytes], + result: (Option Bytes), + body: (if (same representation (lit "int")) then (ctor Option.some [Bytes] (lit "Int")) else (if (same representation (lit "int8")) then (ctor Option.some [Bytes] (lit "Int8")) else (if (same representation (lit "int16")) then (ctor Option.some [Bytes] (lit "Int16")) else (if (same representation (lit "int32")) then (ctor Option.some [Bytes] (lit "Int32")) else (if (same representation (lit "int64")) then (ctor Option.some [Bytes] (lit "Int64")) else (if (same representation (lit "uint8")) then (ctor Option.some [Bytes] (lit "UInt8")) else (if (same representation (lit "uint16")) then (ctor Option.some [Bytes] (lit "UInt16")) else (if (same representation (lit "uint32")) then (ctor Option.some [Bytes] (lit "UInt32")) else (if (same representation (lit "uint64")) then (ctor Option.some [Bytes] (lit "UInt64")) else (ctor Option.none [Bytes])))))))))), + executable: true +} + +definition{ + name: allBytes, + parameters: [text : Bytes, from : Nat, hexadecimal : Bool], + result: Bool, + body: (let stop : Nat := (first (prim iterate_until (fun [hexadecimal, text] (position : Nat) => (match (LexLeanStd.Text::byteAt text position) + | Option.some byte => (let code : Nat := (prim to_nat byte : Nat); (if (if (both (48 <=? code) (code <=? 57)) then true else (both hexadecimal (both (97 <=? code) (code <=? 102)))) then (ctor Option.some [Nat] (position + 1)) else (ctor Option.none [Nat]))) + | Option.none => (ctor Option.none [Nat]))) ((prim length text : Nat) + 1) from : (Nat * Bool))); (stop =? (prim length text : Nat))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: canonicalDigits, + parameters: [text : Bytes], + result: Bool, + body: (let size : Nat := (prim length text : Nat); (both (0 (match (strOf (memberIn chain (lit "name"))) + | Option.some name => (let module : (Option LexLeanStd.Json::Json) := (memberIn chain (lit "module")); (if (both ((countOf chain) =? (if (present module) then 2 else 1)) (SurfaceGeneric::isBare name)) then (match module + | Option.some written => (match (asStr written) + | Option.some prefix => (if (SurfaceGeneric::isBare prefix) then (ctor Option.some [Bytes] (cat (cat prefix (lit "::")) name)) else (ctor Option.none [Bytes])) + | Option.none => (ctor Option.none [Bytes])) + | Option.none => (ctor Option.some [Bytes] name)) else (ctor Option.none [Bytes]))) + | Option.none => (ctor Option.none [Bytes])) + | Option.none => (ctor Option.none [Bytes])), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: memberOk, + parameters: [found : (Option LexLeanStd.Json::Json)], + result: Bool, + body: (match found + | Option.some value => (match (memberText value) + | Option.some text => true + | Option.none => false) + | Option.none => false), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: paramOk, + parameters: [value : LexLeanStd.Json::Json], + result: Bool, + body: (match (asObj value) + | Option.some chain => (match (strOf (memberIn chain (lit "name"))) + | Option.some name => (both (present (memberIn chain (lit "type"))) (both ((countOf chain) =? 2) (SurfaceGeneric::isBare name))) + | Option.none => false) + | Option.none => false), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: paramFound, + parameters: [found : (Option LexLeanStd.Json::Json)], + result: Bool, + body: (match found | Option.some value => (paramOk value) | Option.none => false), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: isArray, + parameters: [chain : LexLeanStd.Json::Json, key : Bytes], + result: Bool, + body: (present (arrOf (memberIn chain key))), + executable: true +} + +definition{ + name: everyItem, + parameters: [ + chain : LexLeanStd.Json::Json, + key : Bytes, + test : (Fn (LexLeanStd.Json::Json) Bool) + ], + result: Bool, + body: (match (arrOf (memberIn chain key)) + | Option.some items => (prim list_fold (fun [test] (fine : Bool) (item : LexLeanStd.Json::Json) => (if fine then (apply test item) else false)) true (itemsOfChain items) : Bool) + | Option.none => false), + executable: true +} + +definition{ + name: bareString, + parameters: [value : LexLeanStd.Json::Json], + result: Bool, + body: (match (asStr value) + | Option.some text => (SurfaceGeneric::isBare text) + | Option.none => false), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: assignmentOk, + parameters: [value : LexLeanStd.Json::Json], + result: Bool, + body: (match (asObj value) + | Option.some chain => (both (exactly chain (cons (lit "field") (cons (lit "value") (nil Bytes)))) (match (strOf (memberIn chain (lit "field"))) + | Option.some field => (SurfaceGeneric::isBare field) + | Option.none => false)) + | Option.none => false), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: branchOk, + parameters: [value : LexLeanStd.Json::Json], + result: Bool, + body: (match (asObj value) + | Option.some chain => (both (exactly chain (cons (lit "constructor") (cons (lit "binders") (cons (lit "body") (nil Bytes))))) (both (memberOk (memberIn chain (lit "constructor"))) (everyItem chain (lit "binders") (fun (binder : LexLeanStd.Json::Json) => (match (asStr binder) + | Option.some name => (plain name) + | Option.none => false))))) + | Option.none => false), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: typeArgumentsOk, + parameters: [chain : LexLeanStd.Json::Json], + result: Bool, + body: (match (memberIn chain (lit "type_arguments")) + | Option.some written => (match (asArr written) + | Option.some items => (0 false) + | Option.none => true), + executable: true +} + +definition{ + name: infixOperator, + parameters: [kind : Bytes], + result: (Option Bytes), + body: (if (same kind (lit "add")) then (ctor Option.some [Bytes] (lit "+")) else (if (same kind (lit "eq")) then (ctor Option.some [Bytes] (lit "=")) else (if (same kind (lit "le")) then (ctor Option.some [Bytes] (lit "<=")) else (if (same kind (lit "lt")) then (ctor Option.some [Bytes] (lit "<")) else (if (same kind (lit "beq")) then (ctor Option.some [Bytes] (lit "=?")) else (if (same kind (lit "ble")) then (ctor Option.some [Bytes] (lit "<=?")) else (if (same kind (lit "blt")) then (ctor Option.some [Bytes] (lit "")) else (if (same kind (lit "implies")) then (ctor Option.some [Bytes] (lit "->")) else (ctor Option.none [Bytes]))))))))))))), + executable: true +} + +definition{ + name: infixLeft, + parameters: [kind : Bytes], + result: Bytes, + body: (if (same kind (lit "implies")) then (lit "premise") else (lit "left")), + executable: true +} + +definition{ + name: infixRight, + parameters: [kind : Bytes], + result: Bytes, + body: (if (same kind (lit "implies")) then (lit "conclusion") else (lit "right")), + executable: true +} + +definition{ + name: termReadable, + parameters: [chain : LexLeanStd.Json::Json, kind : Bytes], + result: Bool, + body: (match (infixOperator kind) + | Option.some operator => (exactly chain (cons (lit "kind") (cons (infixLeft kind) (cons (infixRight kind) (nil Bytes))))) + | Option.none => (if (same kind (lit "var")) then (both (exactly chain (k1 "name")) (match (strOf (memberIn chain (lit "name"))) + | Option.some name => (plain name) + | Option.none => false)) else (if (same kind (lit "nat")) then (both (exactly chain (k1 "value")) (match (strOf (memberIn chain (lit "value"))) + | Option.some digits => (canonicalDigits digits) + | Option.none => false)) else (if (same kind (lit "bool")) then (both (exactly chain (k1 "value")) (match (memberIn chain (lit "value")) + | Option.some value => (match (asBool value) + | Option.some flag => true + | Option.none => false) + | Option.none => false)) else (if (same kind (lit "string")) then (both (exactly chain (k1 "value")) (match (strOf (memberIn chain (lit "value"))) + | Option.some text => true + | Option.none => false)) else (if (same kind (lit "bytes")) then (both (exactly chain (k1 "hex")) (match (strOf (memberIn chain (lit "hex"))) + | Option.some hex => (canonicalHex hex) + | Option.none => false)) else (if (same kind (lit "unit")) then (exactly chain (cons (lit "kind") (nil Bytes))) else (if (same kind (lit "integer")) then (both (exactly chain (k2 "representation" "value")) (match (strOf (memberIn chain (lit "value"))) + | Option.some value => (match (strOf (memberIn chain (lit "representation"))) + | Option.some representation => (both (present (match (integerSpelling representation) + | Option.some spelling => (ctor Option.some [LexLeanStd.Json::Json] (ctor LexLeanStd.Json::Json.JNil)) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json]))) (canonicalInteger value)) + | Option.none => false) + | Option.none => false)) else (if (same kind (lit "pair")) then (exactly chain (k2 "left" "right")) else (if (if (same kind (lit "not")) then true else (if (same kind (lit "first")) then true else (same kind (lit "second")))) then (exactly chain (k1 "value")) else (if (same kind (lit "cons")) then (exactly chain (k2 "head" "tail")) else (if (same kind (lit "nil")) then (exactly chain (k1 "element")) else (if (same kind (lit "if")) then (exactly chain (k3 "condition" "then_value" "else_value")) else (if (same kind (lit "let")) then (both (exactly chain (k3 "binder" "value" "body")) (paramFound (memberIn chain (lit "binder")))) else (if (same kind (lit "forall")) then (both (exactly chain (k2 "binder" "body")) (paramFound (memberIn chain (lit "binder")))) else (if (same kind (lit "lambda")) then (both (exactly chain (k3 "parameters" "captures" "body")) (both (everyItem chain (lit "parameters") (fun (parameter : LexLeanStd.Json::Json) => (paramOk parameter))) (everyItem chain (lit "captures") (fun (capture : LexLeanStd.Json::Json) => (bareString capture))))) else (if (same kind (lit "apply")) then (both (exactly chain (k2 "function" "arguments")) (isArray chain (lit "arguments"))) else (if (same kind (lit "project")) then (both (exactly chain (k2 "value" "field")) (match (strOf (memberIn chain (lit "field"))) + | Option.some field => (SurfaceGeneric::isBare field) + | Option.none => false)) else (if (same kind (lit "call")) then (both (keysWith chain (k2 "function" "arguments")) (both (match (memberIn chain (lit "function")) + | Option.some function => (match (memberText function) + | Option.some written => (match (strOf (getIn function (lit "name"))) + | Option.some name => (plain name) + | Option.none => false) + | Option.none => false) + | Option.none => false) (both (typeArgumentsOk chain) (isArray chain (lit "arguments"))))) else (if (same kind (lit "constructor")) then (both (keysWith chain (k2 "constructor" "arguments")) (both (memberOk (memberIn chain (lit "constructor"))) (both (typeArgumentsOk chain) (isArray chain (lit "arguments"))))) else (if (same kind (lit "function_ref")) then (both (keysWith chain (k1 "function")) (both (memberOk (memberIn chain (lit "function"))) (typeArgumentsOk chain))) else (if (same kind (lit "record")) then (both (keysWith chain (k2 "type" "fields")) (both (memberOk (memberIn chain (lit "type"))) (both (typeArgumentsOk chain) (everyItem chain (lit "fields") (fun (assignment : LexLeanStd.Json::Json) => (assignmentOk assignment)))))) else (if (same kind (lit "primitive")) then (both (exactly chain (k3 "operation" "arguments" "result")) (both (match (strOf (memberIn chain (lit "operation"))) + | Option.some operation => (SurfaceGeneric::isBare operation) + | Option.none => false) (isArray chain (lit "arguments")))) else (if (same kind (lit "checked_apply")) then (both (keysWith chain (k3 "model" "arguments" "checks")) (both (memberOk (memberIn chain (lit "model"))) (both (typeArgumentsOk chain) (both (isArray chain (lit "arguments")) (everyItem chain (lit "checks") (fun (check : LexLeanStd.Json::Json) => (bareString check))))))) else (if (same kind (lit "match")) then (both (exactly chain (k2 "scrutinee" "branches")) (everyItem chain (lit "branches") (fun (branch : LexLeanStd.Json::Json) => (branchOk branch)))) else false))))))))))))))))))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: typeReadable, + parameters: [chain : LexLeanStd.Json::Json, kind : Bytes], + result: Bool, + body: (match (nullarySpelling kind) + | Option.some spelling => (exactly chain (cons (lit "kind") (nil Bytes))) + | Option.none => (if (same kind (lit "parameter")) then (both (exactly chain (k1 "name")) (match (strOf (memberIn chain (lit "name"))) + | Option.some name => (SurfaceGeneric::isBare name) + | Option.none => false)) else (if (if (same kind (lit "list")) then true else (same kind (lit "set"))) then (exactly chain (k1 "element")) else (if (same kind (lit "option")) then (exactly chain (k1 "value")) else (if (same kind (lit "result")) then (exactly chain (k2 "ok" "error")) else (if (same kind (lit "map")) then (exactly chain (k2 "key" "value")) else (if (same kind (lit "product")) then (exactly chain (k2 "left" "right")) else (if (same kind (lit "function")) then (both (exactly chain (k2 "parameters" "result")) (isArray chain (lit "parameters"))) else (if (same kind (lit "named")) then (both (exactly chain (k2 "member" "arguments")) (match (memberIn chain (lit "member")) + | Option.some member => (match (asObj member) + | Option.some inner => (match (strOf (memberIn inner (lit "name"))) + | Option.some name => (both (if (both (if (present (memberIn inner (lit "module"))) then false else true) (reservedType name)) then false else true) (both (isArray chain (lit "arguments")) (present (match (memberText member) + | Option.some text => (ctor Option.some [LexLeanStd.Json::Json] (ctor LexLeanStd.Json::Json.JNil)) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json]))))) + | Option.none => false) + | Option.none => false) + | Option.none => false)) else false))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: kindOf, + parameters: [chain : LexLeanStd.Json::Json], + result: (Option Bytes), + body: (strOf (memberIn chain (lit "kind"))), + executable: true +} + +definition{ + name: termKidPos, + parameters: [kind : Bytes, key : Bytes], + result: Pos, + body: (if (same key (lit "element")) then (ctor Pos.PType) else (if (same key (lit "binder")) then (ctor Pos.PParam) else (if (same key (lit "parameters")) then (ctor Pos.PFunParams) else (if (same key (lit "arguments")) then (ctor Pos.PArgs) else (if (same key (lit "type_arguments")) then (ctor Pos.PTypeArgs) else (if (same key (lit "fields")) then (ctor Pos.PAssigns) else (if (same key (lit "branches")) then (ctor Pos.PBranches) else (if (same key (lit "result")) then (ctor Pos.PType) else (if (if (same key (lit "type")) then true else (if (same key (lit "constructor")) then true else (same key (lit "model")))) then (ctor Pos.PMember) else (if (same key (lit "function")) then (if (same kind (lit "apply")) then (ctor Pos.PTerm) else (ctor Pos.PMember)) else (ctor Pos.PTerm))))))))))), + executable: true +} + +definition{ + name: typeKidPos, + parameters: [key : Bytes], + result: Pos, + body: (if (same key (lit "parameters")) then (ctor Pos.PFnTypes) else (if (same key (lit "arguments")) then (ctor Pos.PNamedArgs) else (if (same key (lit "member")) then (ctor Pos.PMember) else (if (same key (lit "name")) then (ctor Pos.PAny) else (ctor Pos.PType))))), + executable: true +} + +definition{ + name: kidPos, + parameters: [schema : LexLeanStd.Json::Json, kids : Kids, key : Bytes], + result: Pos, + body: (match kids + | Kids.KSchema object => (memberOf schema object key) + | Kids.KTerm kind => (termKidPos kind key) + | Kids.KType => (typeKidPos key) + | Kids.KParam => (if (same key (lit "type")) then (ctor Pos.PType) else (ctor Pos.PAny)) + | Kids.KAssign => (if (same key (lit "value")) then (ctor Pos.PTerm) else (ctor Pos.PAny)) + | Kids.KBranch => (if (same key (lit "body")) then (ctor Pos.PTerm) else (if (same key (lit "constructor")) then (ctor Pos.PMember) else (ctor Pos.PAny))) + | Kids.KNone => (ctor Pos.PAny)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: termReadableAt, + parameters: [chain : LexLeanStd.Json::Json], + result: Bool, + body: (match (kindOf chain) + | Option.some kind => (termReadable chain kind) + | Option.none => false), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: typeReadableAt, + parameters: [chain : LexLeanStd.Json::Json], + result: Bool, + body: (match (kindOf chain) + | Option.some kind => (typeReadable chain kind) + | Option.none => false), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: kidsFor, + parameters: [schema : LexLeanStd.Json::Json, chain : LexLeanStd.Json::Json, pos : Pos], + result: Kids, + body: (match pos + | Pos.PTerm => (if (termReadableAt chain) then (ctor Kids.KTerm (match (kindOf chain) + | Option.some kind => kind + | Option.none => (lit ""))) else (ctor Kids.KSchema (schemaOf schema chain pos))) + | Pos.PType => (if (typeReadableAt chain) then (ctor Kids.KType) else (ctor Kids.KSchema (schemaOf schema chain pos))) + | Pos.PParam => (if (paramOk (ctor LexLeanStd.Json::Json.JObj chain)) then (ctor Kids.KParam) else (ctor Kids.KSchema (schemaOf schema chain pos))) + | Pos.PNode node => (ctor Kids.KSchema (schemaOf schema chain pos)) + | Pos.PAssign => (ctor Kids.KAssign) + | Pos.PBranch => (ctor Kids.KBranch) + | Pos.PMember => (ctor Kids.KNone) + | Pos.PAny => (ctor Kids.KNone) + | Pos.PArgs => (ctor Kids.KNone) + | Pos.PTypeArgs => (ctor Kids.KNone) + | Pos.PFunParams => (ctor Kids.KNone) + | Pos.PFnTypes => (ctor Kids.KNone) + | Pos.PNamedArgs => (ctor Kids.KNone) + | Pos.PAssigns => (ctor Kids.KNone) + | Pos.PBranches => (ctor Kids.KNone)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: itemPosFor, + parameters: [schema : LexLeanStd.Json::Json, pos : Pos], + result: Pos, + body: (match pos + | Pos.PNode node => (itemsOf schema node) + | Pos.PArgs => (ctor Pos.PTerm) + | Pos.PTypeArgs => (ctor Pos.PType) + | Pos.PFunParams => (ctor Pos.PParam) + | Pos.PFnTypes => (ctor Pos.PType) + | Pos.PNamedArgs => (ctor Pos.PType) + | Pos.PAssigns => (ctor Pos.PAssign) + | Pos.PBranches => (ctor Pos.PBranch) + | Pos.PTerm => (ctor Pos.PAny) + | Pos.PType => (ctor Pos.PAny) + | Pos.PMember => (ctor Pos.PAny) + | Pos.PParam => (ctor Pos.PAny) + | Pos.PAny => (ctor Pos.PAny) + | Pos.PAssign => (ctor Pos.PAny) + | Pos.PBranch => (ctor Pos.PAny)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: tx, + parameters: [text : Bytes], + result: SurfaceDoc::Doc, + body: (ctor SurfaceDoc::Doc.DText text), + executable: true +} + +definition{ + name: seq, + parameters: [parts : (List SurfaceDoc::Doc)], + result: SurfaceDoc::Doc, + body: (prim list_fold (fun (rest : SurfaceDoc::Doc) (part : SurfaceDoc::Doc) => (ctor SurfaceDoc::Doc.DSeq part rest)) (ctor SurfaceDoc::Doc.DNil) (LexLeanStd.Order::reverseList [SurfaceDoc::Doc] parts) : SurfaceDoc::Doc), + executable: true +} + +definition{ + name: docOf, + parameters: [docs : (List (Bytes * SurfaceDoc::Doc)), key : Bytes], + result: SurfaceDoc::Doc, + body: (prim list_fold (fun [key] (found : SurfaceDoc::Doc) (entry : (Bytes * SurfaceDoc::Doc)) => (if (same (first entry) key) then (second entry) else found)) (ctor SurfaceDoc::Doc.DNil) docs : SurfaceDoc::Doc), + executable: true +} + +definition{ + name: interleave, + parameters: [docs : (List SurfaceDoc::Doc), separator : Bytes], + result: (List SurfaceDoc::Doc), + body: (LexLeanStd.Order::reverseList [SurfaceDoc::Doc] (second (prim list_fold (fun [separator] (state : (Bool * (List SurfaceDoc::Doc))) (part : SurfaceDoc::Doc) => (if (first state) then (false, (cons part (second state))) else (false, (cons part (cons (tx separator) (second state)))))) (true, (nil SurfaceDoc::Doc)) docs : (Bool * (List SurfaceDoc::Doc))))), + executable: true +} + +definition{ + name: prefixed, + parameters: [docs : (List SurfaceDoc::Doc), before : Bytes, after : Bytes], + result: (List SurfaceDoc::Doc), + body: (LexLeanStd.Order::reverseList [SurfaceDoc::Doc] (prim list_fold (fun [after, before] (out : (List SurfaceDoc::Doc)) (part : SurfaceDoc::Doc) => (cons (tx after) (cons part (cons (tx before) out)))) (nil SurfaceDoc::Doc) docs : (List SurfaceDoc::Doc))), + executable: true +} + +definition{ + name: arrayDoc, + parameters: [pos : Pos, docs : (List SurfaceDoc::Doc)], + result: SurfaceDoc::Doc, + body: (match pos + | Pos.PArgs => (seq (prefixed docs (lit " ") (lit ""))) + | Pos.PNamedArgs => (seq (prefixed docs (lit " ") (lit ""))) + | Pos.PTypeArgs => (seq (cons (tx (lit "[")) (prim append (interleave docs (lit ", ")) (cons (tx (lit "]")) (nil SurfaceDoc::Doc)) : (List SurfaceDoc::Doc)))) + | Pos.PFunParams => (seq (prefixed docs (lit "(") (lit ") "))) + | Pos.PFnTypes => (seq (cons (tx (lit "(")) (prim append (interleave docs (lit " ")) (cons (tx (lit ")")) (nil SurfaceDoc::Doc)) : (List SurfaceDoc::Doc)))) + | Pos.PAssigns => (seq (cons (tx (lit "{")) (prim append (interleave docs (lit ", ")) (cons (tx (lit "}")) (nil SurfaceDoc::Doc)) : (List SurfaceDoc::Doc)))) + | Pos.PBranches => (seq (prim list_fold (fun (out : (List SurfaceDoc::Doc)) (part : SurfaceDoc::Doc) => (prim append out (cons (ctor SurfaceDoc::Doc.DLine) (cons part (nil SurfaceDoc::Doc))) : (List SurfaceDoc::Doc))) (nil SurfaceDoc::Doc) docs : (List SurfaceDoc::Doc))) + | Pos.PTerm => (SurfaceGeneric::delimitedRev (lit "[") (lit "]") (LexLeanStd.Order::reverseList [SurfaceDoc::Doc] docs)) + | Pos.PType => (SurfaceGeneric::delimitedRev (lit "[") (lit "]") (LexLeanStd.Order::reverseList [SurfaceDoc::Doc] docs)) + | Pos.PMember => (SurfaceGeneric::delimitedRev (lit "[") (lit "]") (LexLeanStd.Order::reverseList [SurfaceDoc::Doc] docs)) + | Pos.PParam => (SurfaceGeneric::delimitedRev (lit "[") (lit "]") (LexLeanStd.Order::reverseList [SurfaceDoc::Doc] docs)) + | Pos.PNode node => (SurfaceGeneric::delimitedRev (lit "[") (lit "]") (LexLeanStd.Order::reverseList [SurfaceDoc::Doc] docs)) + | Pos.PAny => (SurfaceGeneric::delimitedRev (lit "[") (lit "]") (LexLeanStd.Order::reverseList [SurfaceDoc::Doc] docs)) + | Pos.PAssign => (SurfaceGeneric::delimitedRev (lit "[") (lit "]") (LexLeanStd.Order::reverseList [SurfaceDoc::Doc] docs)) + | Pos.PBranch => (SurfaceGeneric::delimitedRev (lit "[") (lit "]") (LexLeanStd.Order::reverseList [SurfaceDoc::Doc] docs))), + executable: true +} + +definition{ + name: strAt, + parameters: [chain : LexLeanStd.Json::Json, key : Bytes], + result: Bytes, + body: (match (strOf (memberIn chain key)) | Option.some text => text | Option.none => (lit "")), + executable: true +} + +definition{ + name: memberAt, + parameters: [chain : LexLeanStd.Json::Json, key : Bytes], + result: Bytes, + body: (match (memberIn chain key) + | Option.some value => (match (memberText value) + | Option.some text => text + | Option.none => (lit "")) + | Option.none => (lit "")), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: namesAt, + parameters: [chain : LexLeanStd.Json::Json, key : Bytes], + result: (List SurfaceDoc::Doc), + body: (match (arrOf (memberIn chain key)) + | Option.some items => (prim list_fold (fun (out : (List SurfaceDoc::Doc)) (item : LexLeanStd.Json::Json) => (prim append out (cons (tx (match (asStr item) + | Option.some text => text + | Option.none => (lit ""))) (nil SurfaceDoc::Doc)) : (List SurfaceDoc::Doc))) (nil SurfaceDoc::Doc) (itemsOfChain items) : (List SurfaceDoc::Doc)) + | Option.none => (nil SurfaceDoc::Doc)), + executable: true +} + +definition{ + name: typeArgsPart, + parameters: [chain : LexLeanStd.Json::Json, docs : (List (Bytes * SurfaceDoc::Doc))], + result: (List SurfaceDoc::Doc), + body: (if (present (memberIn chain (lit "type_arguments"))) then (cons (tx (lit " ")) (cons (docOf docs (lit "type_arguments")) (nil SurfaceDoc::Doc))) else (nil SurfaceDoc::Doc)), + executable: true +} + +definition{ + name: headed, + parameters: [head : Bytes, parts : (List SurfaceDoc::Doc)], + result: SurfaceDoc::Doc, + body: (seq (cons (tx (cat (lit "(") head)) (prim append parts (cons (tx (lit ")")) (nil SurfaceDoc::Doc)) : (List SurfaceDoc::Doc)))), + executable: true +} + +definition{ + name: termDoc, + parameters: [ + chain : LexLeanStd.Json::Json, + kind : Bytes, + docs : (List (Bytes * SurfaceDoc::Doc)) + ], + result: SurfaceDoc::Doc, + body: (match (infixOperator kind) + | Option.some operator => (seq (cons (tx (lit "(")) (cons (docOf docs (infixLeft kind)) (cons (tx (cat (cat (lit " ") operator) (lit " "))) (cons (docOf docs (infixRight kind)) (cons (tx (lit ")")) (nil SurfaceDoc::Doc))))))) + | Option.none => (if (same kind (lit "var")) then (tx (strAt chain (lit "name"))) else (if (same kind (lit "nat")) then (tx (strAt chain (lit "value"))) else (if (same kind (lit "bool")) then (tx (match (memberIn chain (lit "value")) + | Option.some value => (match (asBool value) + | Option.some flag => (if flag then (lit "true") else (lit "false")) + | Option.none => (lit "")) + | Option.none => (lit ""))) else (if (same kind (lit "string")) then (tx (SurfaceLex::quoteText (strAt chain (lit "value")))) else (if (same kind (lit "bytes")) then (tx (cat (lit "0x") (strAt chain (lit "hex")))) else (if (same kind (lit "unit")) then (tx (lit "()")) else (if (same kind (lit "integer")) then (tx (cat (cat (cat (cat (lit "(") (strAt chain (lit "value"))) (lit " : ")) (match (integerSpelling (strAt chain (lit "representation"))) + | Option.some spelling => spelling + | Option.none => (lit ""))) (lit ")"))) else (if (same kind (lit "pair")) then (seq (cons (tx (lit "(")) (cons (docOf docs (lit "left")) (cons (tx (lit ", ")) (cons (docOf docs (lit "right")) (cons (tx (lit ")")) (nil SurfaceDoc::Doc))))))) else (if (if (same kind (lit "not")) then true else (if (same kind (lit "first")) then true else (same kind (lit "second")))) then (headed kind (cons (tx (lit " ")) (cons (docOf docs (lit "value")) (nil SurfaceDoc::Doc)))) else (if (same kind (lit "cons")) then (headed kind (cons (tx (lit " ")) (cons (docOf docs (lit "head")) (cons (tx (lit " ")) (cons (docOf docs (lit "tail")) (nil SurfaceDoc::Doc)))))) else (if (same kind (lit "nil")) then (headed kind (cons (tx (lit " ")) (cons (docOf docs (lit "element")) (nil SurfaceDoc::Doc)))) else (if (same kind (lit "if")) then (seq (cons (tx (lit "(if ")) (cons (docOf docs (lit "condition")) (cons (tx (lit " then ")) (cons (docOf docs (lit "then_value")) (cons (tx (lit " else ")) (cons (docOf docs (lit "else_value")) (cons (tx (lit ")")) (nil SurfaceDoc::Doc))))))))) else (if (same kind (lit "let")) then (seq (cons (tx (lit "(let ")) (cons (docOf docs (lit "binder")) (cons (tx (lit " := ")) (cons (docOf docs (lit "value")) (cons (tx (lit "; ")) (cons (docOf docs (lit "body")) (cons (tx (lit ")")) (nil SurfaceDoc::Doc))))))))) else (if (same kind (lit "forall")) then (seq (cons (tx (lit "(forall (")) (cons (docOf docs (lit "binder")) (cons (tx (lit "), ")) (cons (docOf docs (lit "body")) (cons (tx (lit ")")) (nil SurfaceDoc::Doc))))))) else (if (same kind (lit "lambda")) then (let captures : (List SurfaceDoc::Doc) := (namesAt chain (lit "captures")); (seq (cons (tx (lit "(fun ")) (prim append (if (0 ")) (cons (docOf docs (lit "body")) (cons (tx (lit ")")) (nil SurfaceDoc::Doc))))) : (List SurfaceDoc::Doc))))) else (if (same kind (lit "apply")) then (headed (lit "apply") (cons (tx (lit " ")) (cons (docOf docs (lit "function")) (cons (docOf docs (lit "arguments")) (nil SurfaceDoc::Doc))))) else (if (same kind (lit "project")) then (headed (lit "project") (cons (tx (lit " ")) (cons (docOf docs (lit "value")) (cons (tx (cat (lit " ") (strAt chain (lit "field")))) (nil SurfaceDoc::Doc))))) else (if (same kind (lit "call")) then (seq (cons (tx (cat (lit "(") (memberAt chain (lit "function")))) (prim append (typeArgsPart chain docs) (cons (docOf docs (lit "arguments")) (cons (tx (lit ")")) (nil SurfaceDoc::Doc))) : (List SurfaceDoc::Doc)))) else (if (same kind (lit "constructor")) then (headed (lit "ctor") (cons (tx (cat (lit " ") (memberAt chain (lit "constructor")))) (prim append (typeArgsPart chain docs) (cons (docOf docs (lit "arguments")) (nil SurfaceDoc::Doc)) : (List SurfaceDoc::Doc)))) else (if (same kind (lit "function_ref")) then (headed (lit "ref") (cons (tx (cat (lit " ") (memberAt chain (lit "function")))) (typeArgsPart chain docs))) else (if (same kind (lit "record")) then (headed (lit "record") (cons (tx (cat (lit " ") (memberAt chain (lit "type")))) (prim append (typeArgsPart chain docs) (cons (tx (lit " ")) (cons (docOf docs (lit "fields")) (nil SurfaceDoc::Doc))) : (List SurfaceDoc::Doc)))) else (if (same kind (lit "primitive")) then (seq (cons (tx (cat (lit "(prim ") (strAt chain (lit "operation")))) (cons (docOf docs (lit "arguments")) (cons (tx (lit " : ")) (cons (docOf docs (lit "result")) (cons (tx (lit ")")) (nil SurfaceDoc::Doc))))))) else (if (same kind (lit "checked_apply")) then (headed (lit "checked") (cons (tx (cat (lit " ") (memberAt chain (lit "model")))) (prim append (typeArgsPart chain docs) (cons (docOf docs (lit "arguments")) (cons (tx (lit " with ")) (cons (tx (lit "[")) (prim append (interleave (namesAt chain (lit "checks")) (lit ", ")) (cons (tx (lit "]")) (nil SurfaceDoc::Doc)) : (List SurfaceDoc::Doc))))) : (List SurfaceDoc::Doc)))) else (ctor SurfaceDoc::Doc.DGroup (ctor SurfaceDoc::Doc.DNest 2 (seq (cons (tx (lit "(match ")) (cons (docOf docs (lit "scrutinee")) (cons (docOf docs (lit "branches")) (cons (tx (lit ")")) (nil SurfaceDoc::Doc)))))))))))))))))))))))))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: typeDoc, + parameters: [ + chain : LexLeanStd.Json::Json, + kind : Bytes, + docs : (List (Bytes * SurfaceDoc::Doc)) + ], + result: SurfaceDoc::Doc, + body: (match (nullarySpelling kind) + | Option.some spelling => (tx spelling) + | Option.none => (if (same kind (lit "parameter")) then (tx (cat (lit "'") (strAt chain (lit "name")))) else (if (same kind (lit "list")) then (headed (lit "List") (cons (tx (lit " ")) (cons (docOf docs (lit "element")) (nil SurfaceDoc::Doc)))) else (if (same kind (lit "set")) then (headed (lit "Set") (cons (tx (lit " ")) (cons (docOf docs (lit "element")) (nil SurfaceDoc::Doc)))) else (if (same kind (lit "option")) then (headed (lit "Option") (cons (tx (lit " ")) (cons (docOf docs (lit "value")) (nil SurfaceDoc::Doc)))) else (if (same kind (lit "result")) then (headed (lit "Result") (cons (tx (lit " ")) (cons (docOf docs (lit "ok")) (cons (tx (lit " ")) (cons (docOf docs (lit "error")) (nil SurfaceDoc::Doc)))))) else (if (same kind (lit "map")) then (headed (lit "Map") (cons (tx (lit " ")) (cons (docOf docs (lit "key")) (cons (tx (lit " ")) (cons (docOf docs (lit "value")) (nil SurfaceDoc::Doc)))))) else (if (same kind (lit "product")) then (seq (cons (tx (lit "(")) (cons (docOf docs (lit "left")) (cons (tx (lit " * ")) (cons (docOf docs (lit "right")) (cons (tx (lit ")")) (nil SurfaceDoc::Doc))))))) else (if (same kind (lit "function")) then (headed (lit "Fn") (cons (tx (lit " ")) (cons (docOf docs (lit "parameters")) (cons (tx (lit " ")) (cons (docOf docs (lit "result")) (nil SurfaceDoc::Doc)))))) else (let head : Bytes := (memberAt chain (lit "member")); (match (arrOf (memberIn chain (lit "arguments"))) + | Option.some arguments => (if ((countOf arguments) =? 0) then (tx head) else (seq (cons (tx (cat (lit "(") head)) (cons (docOf docs (lit "arguments")) (cons (tx (lit ")")) (nil SurfaceDoc::Doc)))))) + | Option.none => (tx head)))))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: branchDoc, + parameters: [chain : LexLeanStd.Json::Json, docs : (List (Bytes * SurfaceDoc::Doc))], + result: SurfaceDoc::Doc, + body: (seq (cons (tx (cat (lit "| ") (memberAt chain (lit "constructor")))) (prim append (prefixed (namesAt chain (lit "binders")) (lit " ") (lit "")) (cons (tx (lit " => ")) (cons (docOf docs (lit "body")) (nil SurfaceDoc::Doc))) : (List SurfaceDoc::Doc)))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: taggedDoc, + parameters: [chain : LexLeanStd.Json::Json, docs : (List (Bytes * SurfaceDoc::Doc))], + result: SurfaceDoc::Doc, + body: (SurfaceGeneric::objectDoc (SurfaceGeneric::kindTag chain) docs), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: objectAt, + parameters: [chain : LexLeanStd.Json::Json, pos : Pos, docs : (List (Bytes * SurfaceDoc::Doc))], + result: SurfaceDoc::Doc, + body: (match pos + | Pos.PTerm => (if (termReadableAt chain) then (termDoc chain (match (kindOf chain) + | Option.some kind => kind + | Option.none => (lit "")) docs) else (taggedDoc chain docs)) + | Pos.PType => (if (typeReadableAt chain) then (typeDoc chain (match (kindOf chain) + | Option.some kind => kind + | Option.none => (lit "")) docs) else (taggedDoc chain docs)) + | Pos.PMember => (match (memberText (ctor LexLeanStd.Json::Json.JObj chain)) + | Option.some text => (tx text) + | Option.none => (SurfaceGeneric::docValue (ctor LexLeanStd.Json::Json.JObj chain))) + | Pos.PParam => (if (paramOk (ctor LexLeanStd.Json::Json.JObj chain)) then (seq (cons (tx (cat (strAt chain (lit "name")) (lit " : "))) (cons (docOf docs (lit "type")) (nil SurfaceDoc::Doc)))) else (taggedDoc chain docs)) + | Pos.PNode node => (taggedDoc chain docs) + | Pos.PAssign => (seq (cons (tx (cat (strAt chain (lit "field")) (lit " := "))) (cons (docOf docs (lit "value")) (nil SurfaceDoc::Doc)))) + | Pos.PBranch => (branchDoc chain docs) + | Pos.PAny => (SurfaceGeneric::docValue (ctor LexLeanStd.Json::Json.JObj chain)) + | Pos.PArgs => (SurfaceGeneric::docValue (ctor LexLeanStd.Json::Json.JObj chain)) + | Pos.PTypeArgs => (SurfaceGeneric::docValue (ctor LexLeanStd.Json::Json.JObj chain)) + | Pos.PFunParams => (SurfaceGeneric::docValue (ctor LexLeanStd.Json::Json.JObj chain)) + | Pos.PFnTypes => (SurfaceGeneric::docValue (ctor LexLeanStd.Json::Json.JObj chain)) + | Pos.PNamedArgs => (SurfaceGeneric::docValue (ctor LexLeanStd.Json::Json.JObj chain)) + | Pos.PAssigns => (SurfaceGeneric::docValue (ctor LexLeanStd.Json::Json.JObj chain)) + | Pos.PBranches => (SurfaceGeneric::docValue (ctor LexLeanStd.Json::Json.JObj chain))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: noKids, + parameters: [kids : Kids], + result: Bool, + body: (match kids + | Kids.KNone => true + | Kids.KSchema object => false + | Kids.KTerm kind => false + | Kids.KType => false + | Kids.KParam => false + | Kids.KAssign => false + | Kids.KBranch => false), + executable: true +} + +definition{ + name: docAt, + parameters: [schema : LexLeanStd.Json::Json, value : LexLeanStd.Json::Json, pos : Pos], + result: SurfaceDoc::Doc, + body: (match value + | LexLeanStd.Json::Json.JObj members => (objectAt members pos (docMembersAt schema members (kidsFor schema members pos))) + | LexLeanStd.Json::Json.JArr items => (arrayDoc pos (docItemsAt schema items (itemPosFor schema pos))) + | LexLeanStd.Json::Json.JBool flag => (SurfaceGeneric::docValue (ctor LexLeanStd.Json::Json.JBool flag)) + | LexLeanStd.Json::Json.JInt negative magnitude => (SurfaceGeneric::docValue (ctor LexLeanStd.Json::Json.JInt negative magnitude)) + | LexLeanStd.Json::Json.JStr text => (SurfaceGeneric::docValue (ctor LexLeanStd.Json::Json.JStr text)) + | LexLeanStd.Json::Json.JNil => (ctor SurfaceDoc::Doc.DNil) + | LexLeanStd.Json::Json.JItem element rest => (ctor SurfaceDoc::Doc.DNil) + | LexLeanStd.Json::Json.JMember key inner rest => (ctor SurfaceDoc::Doc.DNil)), + axioms: [Quot.sound, propext], + executable: true, + mutual: Print, + recursive_argument: value +} + +definition{ + name: docItemsAt, + parameters: [schema : LexLeanStd.Json::Json, items : LexLeanStd.Json::Json, pos : Pos], + result: (List SurfaceDoc::Doc), + body: (match items + | LexLeanStd.Json::Json.JItem element rest => (cons (docAt schema element pos) (docItemsAt schema rest pos)) + | LexLeanStd.Json::Json.JBool flag => (nil SurfaceDoc::Doc) + | LexLeanStd.Json::Json.JInt negative magnitude => (nil SurfaceDoc::Doc) + | LexLeanStd.Json::Json.JStr text => (nil SurfaceDoc::Doc) + | LexLeanStd.Json::Json.JArr inner => (nil SurfaceDoc::Doc) + | LexLeanStd.Json::Json.JObj inner => (nil SurfaceDoc::Doc) + | LexLeanStd.Json::Json.JNil => (nil SurfaceDoc::Doc) + | LexLeanStd.Json::Json.JMember key inner rest => (nil SurfaceDoc::Doc)), + axioms: [Quot.sound, propext], + executable: true, + mutual: Print, + recursive_argument: items +} + +definition{ + name: docMembersAt, + parameters: [schema : LexLeanStd.Json::Json, members : LexLeanStd.Json::Json, kids : Kids], + result: (List (Bytes * SurfaceDoc::Doc)), + body: (match members + | LexLeanStd.Json::Json.JMember key inner rest => (if (noKids kids) then (nil (Bytes * SurfaceDoc::Doc)) else (cons (key, (docAt schema inner (kidPos schema kids key))) (docMembersAt schema rest kids))) + | LexLeanStd.Json::Json.JBool flag => (nil (Bytes * SurfaceDoc::Doc)) + | LexLeanStd.Json::Json.JInt negative magnitude => (nil (Bytes * SurfaceDoc::Doc)) + | LexLeanStd.Json::Json.JStr text => (nil (Bytes * SurfaceDoc::Doc)) + | LexLeanStd.Json::Json.JArr inner => (nil (Bytes * SurfaceDoc::Doc)) + | LexLeanStd.Json::Json.JObj inner => (nil (Bytes * SurfaceDoc::Doc)) + | LexLeanStd.Json::Json.JNil => (nil (Bytes * SurfaceDoc::Doc)) + | LexLeanStd.Json::Json.JItem element rest => (nil (Bytes * SurfaceDoc::Doc))), + axioms: [Quot.sound, propext], + executable: true, + mutual: Print, + recursive_argument: members +} + +definition{ + name: render, + parameters: [node : SurfaceDoc::Doc], + result: Bytes, + body: (SurfaceDoc::renderDoc node 100 (SurfaceGeneric::docNodes node)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: positionOf, + parameters: [schema : LexLeanStd.Json::Json, position : Nat], + result: Pos, + body: (if (position =? 0) then (match (getIn schema (lit "properties")) + | Option.some properties => (match (getIn properties (lit "declarations")) + | Option.some declarations => (itemsOf schema declarations) + | Option.none => (ctor Pos.PAny)) + | Option.none => (ctor Pos.PAny)) else (if (position =? 1) then (ctor Pos.PTerm) else (if (position =? 2) then (ctor Pos.PType) else (match (defNamed schema (lit "proof")) + | Option.some proof => (ctor Pos.PNode proof) + | Option.none => (ctor Pos.PAny))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: printAt, + parameters: [schema : LexLeanStd.Json::Json, value : LexLeanStd.Json::Json, position : Nat], + result: Bytes, + body: (render (docAt schema value (positionOf schema position))), + axioms: [Quot.sound, propext], + executable: true, + production: {effects: [allocation, overflow, recursion], targets: [rust-std]} +} + +definition{ + name: refusal, + parameters: [message : Bytes], + result: (Result Bytes SurfaceLex::Failure), + body: (ctor Result.error [Bytes, SurfaceLex::Failure] (record SurfaceLex::Failure {offset := 0, message := message})), + executable: true +} + +definition{ + name: printSemanticModule, + parameters: [schema : LexLeanStd.Json::Json, given : LexLeanStd.Json::Json], + result: (Result Bytes SurfaceLex::Failure), + body: (match (asObj given) + | Option.some chain => (match (arrOf (memberIn chain (lit "declarations"))) + | Option.some declarations => (if (both ((countOf chain) =? 2) (match (strOf (memberIn chain (lit "spec"))) + | Option.some spec => true + | Option.none => false)) then (let place : Pos := (positionOf schema 0); (ctor Result.ok [Bytes, SurfaceLex::Failure] (second (prim list_fold (fun [place, schema] (state : (Bool * Bytes)) (declaration : LexLeanStd.Json::Json) => (false, (cat (if (first state) then (second state) else (cat (second state) (lit "\n"))) (cat (render (docAt schema declaration place)) (lit "\n"))))) (true, (lit "")) (itemsOfChain declarations) : (Bool * Bytes))))) else (refusal (lit "a module has exactly a `spec` and its `declarations`"))) + | Option.none => (refusal (lit "a module has a `declarations` array"))) + | Option.none => (refusal (lit "a module is an object"))), + axioms: [Quot.sound, propext], + executable: true, + production: {effects: [allocation, overflow, recursion], targets: [rust-std]} +} +\end{semanticmodule} +\end{lexlean} diff --git a/bootstrap/src/SemanticRead.lex.tex b/bootstrap/src/SemanticRead.lex.tex new file mode 100644 index 0000000000..9ca56bb1f0 --- /dev/null +++ b/bootstrap/src/SemanticRead.lex.tex @@ -0,0 +1,1395 @@ +\begin{lexlean}{SemanticRead} +\useglossary{lexlean.std.nat@1.2.0} +\importmodule{LexLeanStd.Json} +\importmodule{LexLeanStd.Order} +\importmodule{LexLeanStd.Text} +\importmodule{SurfaceGeneric} +\importmodule{SurfaceLex} +\title{Natural number addition} +\begin{semanticmodule} +inductive{ + name: Pos, + type_parameters: [], + parameters: [], + constructors: [ + {name: PTerm, fields: []}, + {name: PType, fields: []}, + {name: PMember, fields: []}, + {name: PParam, fields: []}, + {name: PNode, fields: [LexLeanStd.Json::Json]}, + {name: PAny, fields: []} + ] +} + +structure{ + name: Cell, + type_parameters: [], + parameters: [], + fields: [ + {name: key, type: Bytes}, + {name: value, type: LexLeanStd.Json::Json}, + {name: rest, type: LexLeanStd.Json::Json} + ] +} + +structure{ + name: Elem, + type_parameters: [], + parameters: [], + fields: [{name: value, type: LexLeanStd.Json::Json}, {name: rest, type: LexLeanStd.Json::Json}] +} + +structure{ + name: Look, + type_parameters: [], + parameters: [], + fields: [ + {name: rest, type: LexLeanStd.Json::Json}, + {name: found, type: (Option LexLeanStd.Json::Json)} + ] +} + +structure{ + name: Gather, + type_parameters: [], + parameters: [], + fields: [ + {name: rest, type: LexLeanStd.Json::Json}, + {name: items, type: (List LexLeanStd.Json::Json)} + ] +} + +structure{ + name: Slash, + type_parameters: [], + parameters: [], + fields: [{name: cursor, type: Nat}, {name: after, type: Nat}] +} + +structure{ + name: Frame, + type_parameters: [], + parameters: [], + fields: [ + {name: op, type: Nat}, + {name: stage, type: Nat}, + {name: depth, type: Nat}, + {name: offset, type: Nat}, + {name: word, type: Bytes}, + {name: place, type: Pos}, + {name: scope, type: (Option LexLeanStd.Json::Json)}, + {name: values, type: (List LexLeanStd.Json::Json)}, + {name: members, type: (List (Bytes * LexLeanStd.Json::Json))}, + {name: held, type: LexLeanStd.Json::Json}, + {name: heldMore, type: LexLeanStd.Json::Json}, + {name: spans, type: (List (Nat * Nat))} + ] +} + +structure{ + name: Rd, + type_parameters: [], + parameters: [], + fields: [ + {name: pos, type: Nat}, + {name: stack, type: (List Frame)}, + {name: value, type: LexLeanStd.Json::Json}, + {name: mode, type: Nat}, + {name: goal, type: Nat}, + {name: place, type: Pos}, + {name: depth, type: Nat}, + {name: error, type: (Option SurfaceLex::Failure)}, + {name: spans, type: (List (Nat * Nat))} + ] +} + +structure{ + name: Got, + type_parameters: [], + parameters: [], + fields: [ + {name: value, type: LexLeanStd.Json::Json}, + {name: pos, type: Nat}, + {name: error, type: (Option SurfaceLex::Failure)} + ] +} + +structure{ + name: Names, + type_parameters: [], + parameters: [], + fields: [ + {name: items, type: (List LexLeanStd.Json::Json)}, + {name: pos, type: Nat}, + {name: more, type: Bool}, + {name: error, type: (Option SurfaceLex::Failure)} + ] +} + +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: both, + parameters: [left : Bool, right : Bool], + result: Bool, + body: (if left then right else false), + executable: true +} + +definition{ + name: same, + parameters: [left : Bytes, right : Bytes], + result: Bool, + body: (prim equal left right : Bool), + executable: true +} + +definition{ + name: ticked, + parameters: [word : Bytes], + result: Bytes, + body: (cat (cat (lit "`") word) (lit "`")), + executable: true +} + +definition{ + name: listed, + parameters: [needle : Bytes, items : (List Bytes)], + result: Bool, + body: (prim list_fold (fun [needle] (found : Bool) (item : Bytes) => (if found then true else (prim equal item needle : Bool))) false items : Bool), + executable: true +} + +definition{ + name: nextMember, + parameters: [chain : LexLeanStd.Json::Json], + result: (Option Cell), + body: (match chain + | LexLeanStd.Json::Json.JMember key value rest => (ctor Option.some [Cell] (record Cell {key := key, value := value, rest := rest})) + | LexLeanStd.Json::Json.JBool flag => (ctor Option.none [Cell]) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Option.none [Cell]) + | LexLeanStd.Json::Json.JStr text => (ctor Option.none [Cell]) + | LexLeanStd.Json::Json.JArr inner => (ctor Option.none [Cell]) + | LexLeanStd.Json::Json.JObj inner => (ctor Option.none [Cell]) + | LexLeanStd.Json::Json.JNil => (ctor Option.none [Cell]) + | LexLeanStd.Json::Json.JItem element rest => (ctor Option.none [Cell])), + executable: true +} + +definition{ + name: nextItem, + parameters: [chain : LexLeanStd.Json::Json], + result: (Option Elem), + body: (match chain + | LexLeanStd.Json::Json.JItem element rest => (ctor Option.some [Elem] (record Elem {value := element, rest := rest})) + | LexLeanStd.Json::Json.JBool flag => (ctor Option.none [Elem]) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Option.none [Elem]) + | LexLeanStd.Json::Json.JStr text => (ctor Option.none [Elem]) + | LexLeanStd.Json::Json.JArr inner => (ctor Option.none [Elem]) + | LexLeanStd.Json::Json.JObj inner => (ctor Option.none [Elem]) + | LexLeanStd.Json::Json.JNil => (ctor Option.none [Elem]) + | LexLeanStd.Json::Json.JMember key value rest => (ctor Option.none [Elem])), + executable: true +} + +definition{ + name: asObj, + parameters: [value : LexLeanStd.Json::Json], + result: (Option LexLeanStd.Json::Json), + body: (match value + | LexLeanStd.Json::Json.JObj inner => (ctor Option.some [LexLeanStd.Json::Json] inner) + | LexLeanStd.Json::Json.JBool flag => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JStr text => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JArr inner => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JNil => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JItem element rest => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JMember key member rest => (ctor Option.none [LexLeanStd.Json::Json])), + executable: true +} + +definition{ + name: asArr, + parameters: [value : LexLeanStd.Json::Json], + result: (Option LexLeanStd.Json::Json), + body: (match value + | LexLeanStd.Json::Json.JArr inner => (ctor Option.some [LexLeanStd.Json::Json] inner) + | LexLeanStd.Json::Json.JBool flag => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JStr text => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JObj inner => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JNil => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JItem element rest => (ctor Option.none [LexLeanStd.Json::Json]) + | LexLeanStd.Json::Json.JMember key member rest => (ctor Option.none [LexLeanStd.Json::Json])), + executable: true +} + +definition{ + name: asStr, + parameters: [value : LexLeanStd.Json::Json], + result: (Option Bytes), + body: (match value + | LexLeanStd.Json::Json.JStr text => (ctor Option.some [Bytes] text) + | LexLeanStd.Json::Json.JBool flag => (ctor Option.none [Bytes]) + | LexLeanStd.Json::Json.JInt negative magnitude => (ctor Option.none [Bytes]) + | LexLeanStd.Json::Json.JArr inner => (ctor Option.none [Bytes]) + | LexLeanStd.Json::Json.JObj inner => (ctor Option.none [Bytes]) + | LexLeanStd.Json::Json.JNil => (ctor Option.none [Bytes]) + | LexLeanStd.Json::Json.JItem element rest => (ctor Option.none [Bytes]) + | LexLeanStd.Json::Json.JMember key member rest => (ctor Option.none [Bytes])), + executable: true +} + +definition{ + name: lookStep, + parameters: [key : Bytes, state : Look], + result: (Option Look), + body: (match (project state found) + | Option.some hit => (ctor Option.none [Look]) + | Option.none => (match (nextMember (project state rest)) + | Option.some cell => (if (same (project cell key) key) then (ctor Option.some [Look] (record Look {rest := (ctor LexLeanStd.Json::Json.JNil), found := (ctor Option.some [LexLeanStd.Json::Json] (project cell value))})) else (ctor Option.some [Look] (record Look {rest := (project cell rest), found := (ctor Option.none [LexLeanStd.Json::Json])}))) + | Option.none => (ctor Option.none [Look]))), + executable: true +} + +definition{ + name: getIn, + parameters: [value : LexLeanStd.Json::Json, key : Bytes], + result: (Option LexLeanStd.Json::Json), + body: (match (asObj value) + | Option.some chain => (project (first (prim iterate_until (fun [key] (state : Look) => (lookStep key state)) 1000000 (record Look {rest := chain, found := (ctor Option.none [LexLeanStd.Json::Json])}) : (Look * Bool))) found) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json])), + executable: true +} + +definition{ + name: present, + parameters: [found : (Option LexLeanStd.Json::Json)], + result: Bool, + body: (match found | Option.some value => true | Option.none => false), + executable: true +} + +definition{ + name: strOf, + parameters: [found : (Option LexLeanStd.Json::Json)], + result: (Option Bytes), + body: (match found + | Option.some value => (asStr value) + | Option.none => (ctor Option.none [Bytes])), + executable: true +} + +definition{ + name: arrOf, + parameters: [found : (Option LexLeanStd.Json::Json)], + result: (Option LexLeanStd.Json::Json), + body: (match found + | Option.some value => (asArr value) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json])), + executable: true +} + +definition{ + name: gatherStep, + parameters: [state : Gather], + result: (Option Gather), + body: (match (nextItem (project state rest)) + | Option.some elem => (ctor Option.some [Gather] (record Gather {rest := (project elem rest), items := (cons (project elem value) (project state items))})) + | Option.none => (ctor Option.none [Gather])), + executable: true +} + +definition{ + name: itemsOfChain, + parameters: [chain : LexLeanStd.Json::Json], + result: (List LexLeanStd.Json::Json), + body: (LexLeanStd.Order::reverseList [LexLeanStd.Json::Json] (project (first (prim iterate_until (fun (state : Gather) => (gatherStep state)) 1000000 (record Gather {rest := chain, items := (nil LexLeanStd.Json::Json)}) : (Gather * Bool))) items)), + executable: true +} + +definition{ + name: slashStep, + parameters: [text : Bytes, state : Slash], + result: (Option Slash), + body: (let position : Nat := (project state cursor); (match (LexLeanStd.Text::byteAt text position) + | Option.some byte => (ctor Option.some [Slash] (record Slash {cursor := (position + 1), after := (if ((prim to_nat byte : Nat) =? 47) then (position + 1) else (project state after))})) + | Option.none => (ctor Option.none [Slash]))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: lastSegment, + parameters: [text : Bytes], + result: Bytes, + body: (let after : Nat := (project (first (prim iterate_until (fun [text] (state : Slash) => (slashStep text state)) ((prim length text : Nat) + 1) (record Slash {cursor := 0, after := 0}) : (Slash * Bool))) after); (LexLeanStd.Text::sliceOr text after (prim subtract (prim length text : Nat) after : Nat))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: defNamed, + parameters: [schema : LexLeanStd.Json::Json, name : Bytes], + result: (Option LexLeanStd.Json::Json), + body: (match (getIn schema (lit "$defs")) + | Option.some defs => (getIn defs name) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json])), + executable: true +} + +definition{ + name: resolveStep, + parameters: [schema : LexLeanStd.Json::Json, node : LexLeanStd.Json::Json], + result: (Option LexLeanStd.Json::Json), + body: (match (strOf (getIn node (lit "$ref"))) + | Option.some reference => (defNamed schema (lastSegment reference)) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json])), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: resolve, + parameters: [schema : LexLeanStd.Json::Json, node : LexLeanStd.Json::Json], + result: LexLeanStd.Json::Json, + body: (first (prim iterate_until (fun [schema] (current : LexLeanStd.Json::Json) => (resolveStep schema current)) 4096 node : (LexLeanStd.Json::Json * Bool))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: classify, + parameters: [schema : LexLeanStd.Json::Json, node : LexLeanStd.Json::Json], + result: Pos, + body: (match (strOf (getIn node (lit "$ref"))) + | Option.some reference => (let segment : Bytes := (lastSegment reference); (if (same segment (lit "term")) then (ctor Pos.PTerm) else (if (same segment (lit "type")) then (ctor Pos.PType) else (if (same segment (lit "member")) then (ctor Pos.PMember) else (if (same segment (lit "parameter")) then (ctor Pos.PParam) else (ctor Pos.PNode (resolve schema node))))))) + | Option.none => (ctor Pos.PNode node)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: itemsOf, + parameters: [schema : LexLeanStd.Json::Json, node : LexLeanStd.Json::Json], + result: Pos, + body: (match (getIn (resolve schema node) (lit "items")) + | Option.some items => (classify schema items) + | Option.none => (ctor Pos.PAny)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: memberOf, + parameters: [ + schema : LexLeanStd.Json::Json, + object : (Option LexLeanStd.Json::Json), + key : Bytes + ], + result: Pos, + body: (match object + | Option.some node => (match (getIn (resolve schema node) (lit "properties")) + | Option.some properties => (match (getIn properties key) + | Option.some member => (classify schema member) + | Option.none => (ctor Pos.PAny)) + | Option.none => (ctor Pos.PAny)) + | Option.none => (ctor Pos.PAny)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: tagIs, + parameters: [alternative : LexLeanStd.Json::Json, kind : Bytes], + result: Bool, + body: (match (getIn alternative (lit "properties")) + | Option.some properties => (match (getIn properties (lit "kind")) + | Option.some tag => (if (match (strOf (getIn tag (lit "const"))) + | Option.some constant => (same constant kind) + | Option.none => false) then true else (match (arrOf (getIn tag (lit "enum"))) + | Option.some kinds => (prim list_fold (fun [kind] (found : Bool) (item : LexLeanStd.Json::Json) => (if found then true else (match (asStr item) + | Option.some text => (same text kind) + | Option.none => false))) false (itemsOfChain kinds) : Bool) + | Option.none => false)) + | Option.none => false) + | Option.none => false), + executable: true +} + +definition{ + name: alternativeOf, + parameters: [schema : LexLeanStd.Json::Json, union : LexLeanStd.Json::Json, kind : Bytes], + result: (Option LexLeanStd.Json::Json), + body: (match (arrOf (getIn (resolve schema union) (lit "oneOf"))) + | Option.some alternatives => (prim list_fold (fun [kind, schema] (found : (Option LexLeanStd.Json::Json)) (item : LexLeanStd.Json::Json) => (match found + | Option.some hit => found + | Option.none => (let resolved : LexLeanStd.Json::Json := (resolve schema item); (if (tagIs resolved kind) then (ctor Option.some [LexLeanStd.Json::Json] resolved) else (ctor Option.none [LexLeanStd.Json::Json]))))) (ctor Option.none [LexLeanStd.Json::Json]) (itemsOfChain alternatives) : (Option LexLeanStd.Json::Json)) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json])), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: isUnion, + parameters: [schema : LexLeanStd.Json::Json, node : LexLeanStd.Json::Json], + result: Bool, + body: (match (arrOf (getIn (resolve schema node) (lit "oneOf"))) + | Option.some alternatives => (prim list_fold (fun [schema] (fine : Bool) (item : LexLeanStd.Json::Json) => (if fine then (match (getIn (resolve schema item) (lit "properties")) + | Option.some properties => (present (getIn properties (lit "kind"))) + | Option.none => false) else false)) true (itemsOfChain alternatives) : Bool) + | Option.none => false), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: kindScope, + parameters: [schema : LexLeanStd.Json::Json, place : Pos, kind : Bytes], + result: (Option LexLeanStd.Json::Json), + body: (match place + | Pos.PTerm => (match (defNamed schema (lit "term")) + | Option.some union => (alternativeOf schema union kind) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json])) + | Pos.PType => (match (defNamed schema (lit "type")) + | Option.some union => (alternativeOf schema union kind) + | Option.none => (ctor Option.none [LexLeanStd.Json::Json])) + | Pos.PNode node => (if (isUnion schema node) then (alternativeOf schema node kind) else (ctor Option.some [LexLeanStd.Json::Json] node)) + | Pos.PMember => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PParam => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PAny => (ctor Option.none [LexLeanStd.Json::Json])), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: braceScope, + parameters: [schema : LexLeanStd.Json::Json, place : Pos], + result: (Option LexLeanStd.Json::Json), + body: (match place + | Pos.PNode node => (ctor Option.some [LexLeanStd.Json::Json] node) + | Pos.PParam => (defNamed schema (lit "parameter")) + | Pos.PTerm => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PType => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PMember => (ctor Option.none [LexLeanStd.Json::Json]) + | Pos.PAny => (ctor Option.none [LexLeanStd.Json::Json])), + executable: true +} + +definition{ + name: itemPlace, + parameters: [schema : LexLeanStd.Json::Json, place : Pos], + result: Pos, + body: (match place + | Pos.PNode node => (itemsOf schema node) + | Pos.PTerm => (ctor Pos.PAny) + | Pos.PType => (ctor Pos.PAny) + | Pos.PMember => (ctor Pos.PAny) + | Pos.PParam => (ctor Pos.PAny) + | Pos.PAny => (ctor Pos.PAny)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: code, + parameters: [toks : (List SurfaceLex::Spanned), position : Nat], + result: Nat, + body: (SurfaceGeneric::codeAt toks position), + executable: true +} + +definition{ + name: txt, + parameters: [toks : (List SurfaceLex::Spanned), position : Nat], + result: Bytes, + body: (SurfaceGeneric::textAt toks position), + executable: true +} + +definition{ + name: wordIs, + parameters: [toks : (List SurfaceLex::Spanned), position : Nat, expected : Bytes], + result: Bool, + body: (both ((code toks position) =? 12) (same (txt toks position) expected)), + executable: true +} + +definition{ + name: lastEnd, + parameters: [toks : (List SurfaceLex::Spanned), position : Nat], + result: Nat, + body: (if (position =? 0) then 0 else (match (SurfaceGeneric::tokenAt toks (prim subtract position 1 : Nat)) + | Option.some found => (project found stop) + | Option.none => 0)), + executable: true +} + +definition{ + name: allDigits, + parameters: [text : Bytes], + result: Bool, + body: (both (0 (match (LexLeanStd.Text::byteAt text position) + | Option.some byte => (let number : Nat := (prim to_nat byte : Nat); (if (if (both (48 <=? number) (number <=? 57)) then true else (both (97 <=? number) (number <=? 102))) then (ctor Option.some [Nat] (position + 1)) else (ctor Option.none [Nat]))) + | Option.none => (ctor Option.none [Nat]))) ((prim length text : Nat) + 1) 0 : (Nat * Bool))); (both (stop =? (prim length text : Nat)) ((prim remainder (prim length text : Nat) 2 0 : Nat) =? 0))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: tailFrom, + parameters: [text : Bytes, skip : Nat], + result: Bytes, + body: (LexLeanStd.Text::sliceOr text skip (prim subtract (prim length text : Nat) skip : Nat)), + executable: true +} + +definition{ + name: reservedTerm, + parameters: [], + result: (List Bytes), + body: (cons (lit "let") (cons (lit "if") (cons (lit "match") (cons (lit "fun") (cons (lit "forall") (cons (lit "nil") (cons (lit "cons") (cons (lit "first") (cons (lit "second") (cons (lit "not") (cons (lit "apply") (cons (lit "ctor") (cons (lit "record") (cons (lit "project") (cons (lit "prim") (cons (lit "ref") (cons (lit "checked") (cons (lit "then") (cons (lit "else") (cons (lit "with") (cons (lit "true") (nil Bytes)))))))))))))))))))))), + executable: true +} + +definition{ + name: plain, + parameters: [name : Bytes], + result: Bool, + body: (both (SurfaceGeneric::isBare name) (both (if (listed name (reservedTerm)) then false else true) (if (same name (lit "false")) then false else true))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: nullaryKind, + parameters: [word : Bytes], + result: (Option Bytes), + body: (if (same word (lit "Bool")) then (ctor Option.some [Bytes] (lit "bool")) else (if (same word (lit "Bytes")) then (ctor Option.some [Bytes] (lit "bytes")) else (if (same word (lit "Int")) then (ctor Option.some [Bytes] (lit "int")) else (if (same word (lit "Int8")) then (ctor Option.some [Bytes] (lit "int8")) else (if (same word (lit "Int16")) then (ctor Option.some [Bytes] (lit "int16")) else (if (same word (lit "Int32")) then (ctor Option.some [Bytes] (lit "int32")) else (if (same word (lit "Int64")) then (ctor Option.some [Bytes] (lit "int64")) else (if (same word (lit "Nat")) then (ctor Option.some [Bytes] (lit "nat")) else (if (same word (lit "Ordering")) then (ctor Option.some [Bytes] (lit "ordering")) else (if (same word (lit "Prop")) then (ctor Option.some [Bytes] (lit "prop")) else (if (same word (lit "String")) then (ctor Option.some [Bytes] (lit "string")) else (if (same word (lit "Type")) then (ctor Option.some [Bytes] (lit "type")) else (if (same word (lit "UInt8")) then (ctor Option.some [Bytes] (lit "uint8")) else (if (same word (lit "UInt16")) then (ctor Option.some [Bytes] (lit "uint16")) else (if (same word (lit "UInt32")) then (ctor Option.some [Bytes] (lit "uint32")) else (if (same word (lit "UInt64")) then (ctor Option.some [Bytes] (lit "uint64")) else (if (same word (lit "Unit")) then (ctor Option.some [Bytes] (lit "unit")) else (if (same word (lit "ContractViolation")) then (ctor Option.some [Bytes] (lit "contract_violation")) else (if (same word (lit "ReasoningFailure")) then (ctor Option.some [Bytes] (lit "reasoning_failure")) else (ctor Option.none [Bytes])))))))))))))))))))), + executable: true +} + +definition{ + name: heads, + parameters: [], + result: (List Bytes), + body: (cons (lit "List") (cons (lit "Option") (cons (lit "Result") (cons (lit "Set") (cons (lit "Map") (cons (lit "Fn") (nil Bytes))))))), + executable: true +} + +definition{ + name: reservedType, + parameters: [word : Bytes], + result: Bool, + body: (match (nullaryKind word) + | Option.some kind => true + | Option.none => (listed word (heads))), + executable: true +} + +definition{ + name: representationOf, + parameters: [spelling : Bytes], + result: (Option Bytes), + body: (if (same spelling (lit "Int")) then (ctor Option.some [Bytes] (lit "int")) else (if (same spelling (lit "Int8")) then (ctor Option.some [Bytes] (lit "int8")) else (if (same spelling (lit "Int16")) then (ctor Option.some [Bytes] (lit "int16")) else (if (same spelling (lit "Int32")) then (ctor Option.some [Bytes] (lit "int32")) else (if (same spelling (lit "Int64")) then (ctor Option.some [Bytes] (lit "int64")) else (if (same spelling (lit "UInt8")) then (ctor Option.some [Bytes] (lit "uint8")) else (if (same spelling (lit "UInt16")) then (ctor Option.some [Bytes] (lit "uint16")) else (if (same spelling (lit "UInt32")) then (ctor Option.some [Bytes] (lit "uint32")) else (if (same spelling (lit "UInt64")) then (ctor Option.some [Bytes] (lit "uint64")) else (ctor Option.none [Bytes])))))))))), + executable: true +} + +definition{ + name: infixKind, + parameters: [spelling : Bytes], + result: (Option Bytes), + body: (if (same spelling (lit "+")) then (ctor Option.some [Bytes] (lit "add")) else (if (same spelling (lit "=")) then (ctor Option.some [Bytes] (lit "eq")) else (if (same spelling (lit "<=")) then (ctor Option.some [Bytes] (lit "le")) else (if (same spelling (lit "<")) then (ctor Option.some [Bytes] (lit "lt")) else (if (same spelling (lit "=?")) then (ctor Option.some [Bytes] (lit "beq")) else (if (same spelling (lit "<=?")) then (ctor Option.some [Bytes] (lit "ble")) else (if (same spelling (lit "")) then (ctor Option.some [Bytes] (lit "iff")) else (if (same spelling (lit "->")) then (ctor Option.some [Bytes] (lit "implies")) else (ctor Option.none [Bytes]))))))))))))), + executable: true +} + +definition{ + name: isInfix, + parameters: [spelling : Bytes], + result: Bool, + body: (match (infixKind spelling) | Option.some kind => true | Option.none => false), + executable: true +} + +definition{ + name: objectOf, + parameters: [members : (List (Bytes * LexLeanStd.Json::Json))], + result: LexLeanStd.Json::Json, + body: (ctor LexLeanStd.Json::Json.JObj (SurfaceGeneric::memberChain (LexLeanStd.Order::sortByKey [LexLeanStd.Json::Json] members))), + axioms: [propext], + executable: true +} + +definition{ + name: kindNode, + parameters: [kind : Bytes, members : (List (Bytes * LexLeanStd.Json::Json))], + result: LexLeanStd.Json::Json, + body: (objectOf (cons ((lit "kind"), (ctor LexLeanStd.Json::Json.JStr kind)) members)), + axioms: [propext], + executable: true +} + +definition{ + name: str, + parameters: [text : Bytes], + result: LexLeanStd.Json::Json, + body: (ctor LexLeanStd.Json::Json.JStr text), + executable: true +} + +definition{ + name: arr, + parameters: [itemsRev : (List LexLeanStd.Json::Json)], + result: LexLeanStd.Json::Json, + body: (ctor LexLeanStd.Json::Json.JArr (SurfaceGeneric::itemChain itemsRev)), + executable: true +} + +definition{ + name: one, + parameters: [key : String, value : LexLeanStd.Json::Json], + result: (List (Bytes * LexLeanStd.Json::Json)), + body: (cons ((lit key), value) (nil (Bytes * LexLeanStd.Json::Json))), + executable: true +} + +definition{ + name: two, + parameters: [ + key : String, + value : LexLeanStd.Json::Json, + other : String, + more : LexLeanStd.Json::Json + ], + result: (List (Bytes * LexLeanStd.Json::Json)), + body: (cons ((lit key), value) (cons ((lit other), more) (nil (Bytes * LexLeanStd.Json::Json)))), + executable: true +} + +definition{ + name: withTypes, + parameters: [members : (List (Bytes * LexLeanStd.Json::Json)), types : LexLeanStd.Json::Json], + result: (List (Bytes * LexLeanStd.Json::Json)), + body: (match (asArr types) + | Option.some chain => (cons ((lit "type_arguments"), types) members) + | Option.none => members), + executable: true +} + +definition{ + name: failure, + parameters: [offset : Nat, message : Bytes], + result: (Option SurfaceLex::Failure), + body: (ctor Option.some [SurfaceLex::Failure] (record SurfaceLex::Failure {offset := offset, message := message})), + executable: true +} + +definition{ + name: readName, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, position : Nat, what : Bytes], + result: Got, + body: (let word : Bytes := (txt toks position); (if (both ((code toks position) =? 12) (SurfaceGeneric::isBare word)) then (record Got {value := (str word), pos := (position + 1), error := (ctor Option.none [SurfaceLex::Failure])}) else (record Got {value := (ctor LexLeanStd.Json::Json.JNil), pos := position, error := (failure (SurfaceGeneric::offsetAt toks size position) (cat (cat (cat (lit "expected ") what) (lit ", found ")) (SurfaceGeneric::describeAt toks position)))}))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: memberPath, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, position : Nat], + result: Got, + body: (let head : Got := (readName toks size position (lit "a name")); (match (project head error) + | Option.some problem => head + | Option.none => (let after : Nat := (project head pos); (if ((code toks after) =? 9) then (let name : Got := (readName toks size (after + 1) (lit "a name after `::`")); (match (project name error) + | Option.some problem => name + | Option.none => (record Got {value := (objectOf (two "module" (project head value) "name" (project name value))), pos := (project name pos), error := (ctor Option.none [SurfaceLex::Failure])}))) else (record Got {value := (objectOf (one "name" (project head value))), pos := after, error := (ctor Option.none [SurfaceLex::Failure])}))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: namesStep, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, what : Bytes, state : Names], + result: (Option Names), + body: (match (project state error) + | Option.some problem => (ctor Option.none [Names]) + | Option.none => (if (project state more) then (let position : Nat := (project state pos); (if ((code toks position) =? 3) then (ctor Option.none [Names]) else (let name : Got := (readName toks size position what); (match (project name error) + | Option.some problem => (ctor Option.some [Names] (record Names {items := (project state items), pos := position, more := false, error := (project name error)})) + | Option.none => (if ((code toks (project name pos)) =? 6) then (ctor Option.some [Names] (record Names {items := (cons (project name value) (project state items)), pos := ((project name pos) + 1), more := true, error := (ctor Option.none [SurfaceLex::Failure])})) else (ctor Option.some [Names] (record Names {items := (cons (project name value) (project state items)), pos := (project name pos), more := false, error := (ctor Option.none [SurfaceLex::Failure])}))))))) else (ctor Option.none [Names]))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: readNames, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, position : Nat, what : Bytes], + result: Got, + body: (if ((code toks position) =? 2) then (let final : Names := (first (prim iterate_until (fun [size, toks, what] (state : Names) => (namesStep toks size what state)) ((prim length toks : Nat) + 2) (record Names {items := (nil LexLeanStd.Json::Json), pos := (position + 1), more := true, error := (ctor Option.none [SurfaceLex::Failure])}) : (Names * Bool))); (match (project final error) + | Option.some problem => (record Got {value := (ctor LexLeanStd.Json::Json.JNil), pos := (project final pos), error := (project final error)}) + | Option.none => (if ((code toks (project final pos)) =? 3) then (record Got {value := (arr (project final items)), pos := ((project final pos) + 1), error := (ctor Option.none [SurfaceLex::Failure])}) else (record Got {value := (ctor LexLeanStd.Json::Json.JNil), pos := (project final pos), error := (failure (SurfaceGeneric::offsetAt toks size (project final pos)) (lit "expected `,` or `]`"))})))) else (record Got {value := (ctor LexLeanStd.Json::Json.JNil), pos := position, error := (failure (SurfaceGeneric::offsetAt toks size position) (lit "expected `[`"))})), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: frame, + parameters: [op : Nat, depth : Nat], + result: Frame, + body: (record Frame {op := op, stage := 0, depth := depth, offset := 0, word := (lit ""), place := (ctor Pos.PAny), scope := (ctor Option.none [LexLeanStd.Json::Json]), values := (nil LexLeanStd.Json::Json), members := (nil (Bytes * LexLeanStd.Json::Json)), held := (ctor LexLeanStd.Json::Json.JNil), heldMore := (ctor LexLeanStd.Json::Json.JNil), spans := (nil (Nat * Nat))}), + executable: true +} + +definition{ + name: staged, + parameters: [top : Frame, stage : Nat], + result: Frame, + body: (record Frame {op := (project top op), stage := stage, depth := (project top depth), offset := (project top offset), word := (project top word), place := (project top place), scope := (project top scope), values := (project top values), members := (project top members), held := (project top held), heldMore := (project top heldMore), spans := (project top spans)}), + executable: true +} + +definition{ + name: worded, + parameters: [top : Frame, word : Bytes, offset : Nat], + result: Frame, + body: (record Frame {op := (project top op), stage := (project top stage), depth := (project top depth), offset := offset, word := word, place := (project top place), scope := (project top scope), values := (project top values), members := (project top members), held := (project top held), heldMore := (project top heldMore), spans := (project top spans)}), + executable: true +} + +definition{ + name: holding, + parameters: [top : Frame, held : LexLeanStd.Json::Json, heldMore : LexLeanStd.Json::Json], + result: Frame, + body: (record Frame {op := (project top op), stage := (project top stage), depth := (project top depth), offset := (project top offset), word := (project top word), place := (project top place), scope := (project top scope), values := (project top values), members := (project top members), held := held, heldMore := heldMore, spans := (project top spans)}), + executable: true +} + +definition{ + name: adding, + parameters: [top : Frame, value : LexLeanStd.Json::Json], + result: Frame, + body: (record Frame {op := (project top op), stage := (project top stage), depth := (project top depth), offset := (project top offset), word := (project top word), place := (project top place), scope := (project top scope), values := (cons value (project top values)), members := (project top members), held := (project top held), heldMore := (project top heldMore), spans := (project top spans)}), + executable: true +} + +definition{ + name: placed, + parameters: [ + top : Frame, + place : Pos, + scope : (Option LexLeanStd.Json::Json), + members : (List (Bytes * LexLeanStd.Json::Json)) + ], + result: Frame, + body: (record Frame {op := (project top op), stage := (project top stage), depth := (project top depth), offset := (project top offset), word := (project top word), place := place, scope := scope, values := (project top values), members := members, held := (project top held), heldMore := (project top heldMore), spans := (project top spans)}), + executable: true +} + +definition{ + name: spanned, + parameters: [top : Frame, span : (Nat * Nat), offset : Nat], + result: Frame, + body: (record Frame {op := (project top op), stage := (project top stage), depth := (project top depth), offset := offset, word := (project top word), place := (project top place), scope := (project top scope), values := (project top values), members := (project top members), held := (project top held), heldMore := (project top heldMore), spans := (cons span (project top spans))}), + executable: true +} + +definition{ + name: halt, + parameters: [st : Rd, offset : Nat, message : Bytes], + result: Rd, + body: (record Rd {pos := (project st pos), stack := (project st stack), value := (project st value), mode := 9, goal := (project st goal), place := (project st place), depth := (project st depth), error := (failure offset message), spans := (project st spans)}), + executable: true +} + +definition{ + name: haltWith, + parameters: [st : Rd, problem : (Option SurfaceLex::Failure)], + result: Rd, + body: (record Rd {pos := (project st pos), stack := (project st stack), value := (project st value), mode := 9, goal := (project st goal), place := (project st place), depth := (project st depth), error := problem, spans := (project st spans)}), + executable: true +} + +definition{ + name: haltAt, + parameters: [ + toks : (List SurfaceLex::Spanned), + size : Nat, + st : Rd, + position : Nat, + message : Bytes + ], + result: Rd, + body: (halt st (SurfaceGeneric::offsetAt toks size position) message), + executable: true +} + +definition{ + name: give, + parameters: [st : Rd, position : Nat, value : LexLeanStd.Json::Json], + result: Rd, + body: (record Rd {pos := position, stack := (project st stack), value := value, mode := 1, goal := (project st goal), place := (project st place), depth := (project st depth), error := (ctor Option.none [SurfaceLex::Failure]), spans := (project st spans)}), + executable: true +} + +definition{ + name: goto, + parameters: [st : Rd, position : Nat, goal : Nat, place : Pos, depth : Nat], + result: Rd, + body: (record Rd {pos := position, stack := (project st stack), value := (ctor LexLeanStd.Json::Json.JNil), mode := 0, goal := goal, place := place, depth := depth, error := (ctor Option.none [SurfaceLex::Failure]), spans := (project st spans)}), + executable: true +} + +definition{ + name: push, + parameters: [st : Rd, top : Frame, position : Nat, goal : Nat, place : Pos, depth : Nat], + result: Rd, + body: (record Rd {pos := position, stack := (cons top (project st stack)), value := (ctor LexLeanStd.Json::Json.JNil), mode := 0, goal := goal, place := place, depth := depth, error := (ctor Option.none [SurfaceLex::Failure]), spans := (project st spans)}), + executable: true +} + +definition{ + name: wantTerm, + parameters: [st : Rd, top : Frame, position : Nat], + result: Rd, + body: (push st top position 3 (ctor Pos.PTerm) ((project top depth) + 1)), + executable: true +} + +definition{ + name: wantType, + parameters: [st : Rd, top : Frame, position : Nat], + result: Rd, + body: (push st top position 2 (ctor Pos.PType) ((project top depth) + 1)), + executable: true +} + +definition{ + name: wantParam, + parameters: [st : Rd, top : Frame, position : Nat], + result: Rd, + body: (push st top position 4 (ctor Pos.PParam) ((project top depth) + 1)), + executable: true +} + +definition{ + name: finished, + parameters: [st : Rd, value : LexLeanStd.Json::Json, spans : (List (Nat * Nat))], + result: Rd, + body: (record Rd {pos := (project st pos), stack := (project st stack), value := value, mode := 9, goal := (project st goal), place := (project st place), depth := (project st depth), error := (ctor Option.none [SurfaceLex::Failure]), spans := spans}), + executable: true +} + +definition{ + name: fieldsNext, + parameters: [ + toks : (List SurfaceLex::Spanned), + size : Nat, + schema : LexLeanStd.Json::Json, + st : Rd, + top : Frame, + position : Nat + ], + result: Rd, + body: (let token : Nat := (code toks position); (if (token =? 1) then (give st (position + 1) (objectOf (project top members))) else (let offset : Nat := (SurfaceGeneric::offsetAt toks size position); (let key : Bytes := (txt toks position); (if (if (token =? 11) then true else (both (token =? 12) (SurfaceGeneric::isBare key))) then (if ((code toks (position + 1)) =? 7) then (push st (worded top key offset) (position + 2) 0 (memberOf schema (project top scope) key) ((project top depth) + 1)) else (haltAt toks size st (position + 1) (lit "expected `:` after a member name"))) else (halt st offset (lit "expected a member name"))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: startTagged, + parameters: [ + toks : (List SurfaceLex::Spanned), + size : Nat, + schema : LexLeanStd.Json::Json, + st : Rd + ], + result: Rd, + body: (let position : Nat := (project st pos); (let depth : Nat := (project st depth); (let place : Pos := (project st place); (let token : Nat := (code toks position); (if (token =? 2) then (if ((code toks (position + 1)) =? 3) then (give st (position + 2) (arr (nil LexLeanStd.Json::Json))) else (let item : Pos := (itemPlace schema place); (push st (placed (frame 1 depth) item (ctor Option.none [LexLeanStd.Json::Json]) (nil (Bytes * LexLeanStd.Json::Json))) (position + 1) 0 item (depth + 1)))) else (if (token =? 0) then (fieldsNext toks size schema st (placed (frame 2 depth) place (braceScope schema place) (nil (Bytes * LexLeanStd.Json::Json))) (position + 1)) else (let word : Bytes := (txt toks position); (if (both (token =? 12) (both ((code toks (position + 1)) =? 0) (both (SurfaceGeneric::isBare word) (if (allDigits word) then false else true)))) then (fieldsNext toks size schema st (placed (frame 2 depth) place (kindScope schema place word) (cons ((lit "kind"), (str word)) (nil (Bytes * LexLeanStd.Json::Json)))) (position + 2)) else (goto st position 5 place depth))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: startGeneric, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd, depth : Nat], + result: Rd, + body: (let scanned : SurfaceGeneric::Rd := (SurfaceGeneric::readFrom toks size (project st pos) depth); (match (project scanned error) + | Option.some problem => (haltWith st (project scanned error)) + | Option.none => (give st (project scanned pos) (project scanned value)))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: startAt, + parameters: [ + toks : (List SurfaceLex::Spanned), + size : Nat, + schema : LexLeanStd.Json::Json, + st : Rd + ], + result: Rd, + body: (let position : Nat := (project st pos); (let depth : Nat := (project st depth); (if (256 (goto st position 3 (ctor Pos.PTerm) depth) + | Pos.PType => (goto st position 2 (ctor Pos.PType) depth) + | Pos.PMember => (if ((code toks position) =? 0) then (startGeneric toks size st 0) else (let member : Got := (memberPath toks size position); (match (project member error) + | Option.some problem => (haltWith st (project member error)) + | Option.none => (give st (project member pos) (project member value))))) + | Pos.PParam => (goto st position 4 (ctor Pos.PParam) depth) + | Pos.PNode schemaNode => (goto st position 1 (project st place) depth) + | Pos.PAny => (goto st position 5 (ctor Pos.PAny) depth))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: startParam, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd], + result: Rd, + body: (let position : Nat := (project st pos); (let depth : Nat := (project st depth); (if ((code toks position) =? 0) then (goto st position 1 (ctor Pos.PParam) depth) else (let name : Got := (readName toks size position (lit "a parameter name")); (match (project name error) + | Option.some problem => (haltWith st (project name error)) + | Option.none => (if ((code toks (project name pos)) =? 7) then (push st (holding (frame 20 depth) (project name value) (ctor LexLeanStd.Json::Json.JNil)) ((project name pos) + 1) 2 (ctor Pos.PType) (depth + 1)) else (haltAt toks size st (project name pos) (lit "expected `:` after a parameter name")))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: fnParams, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd, top : Frame, position : Nat], + result: Rd, + body: (if ((code toks position) =? 5) then (wantType st (staged top 1) (position + 1)) else (wantType st top position)), + executable: true +} + +definition{ + name: namedArgs, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd, top : Frame, position : Nat], + result: Rd, + body: (if ((code toks position) =? 5) then (give st position (kindNode (lit "named") (two "member" (project top held) "arguments" (arr (project top values))))) else (wantType st top position)), + axioms: [propext], + executable: true +} + +definition{ + name: typeParen, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd, position : Nat, depth : Nat], + result: Rd, + body: (let word : Bytes := (txt toks position); (let isWord : Bool := ((code toks position) =? 12); (if (both isWord (if (same word (lit "List")) then true else (if (same word (lit "Set")) then true else (same word (lit "Option"))))) then (push st (worded (frame 12 depth) word 0) (position + 1) 2 (ctor Pos.PType) (depth + 1)) else (if (both isWord (if (same word (lit "Result")) then true else (same word (lit "Map")))) then (push st (worded (frame 13 depth) word 0) (position + 1) 2 (ctor Pos.PType) (depth + 1)) else (if (both isWord (same word (lit "Fn"))) then (if ((code toks (position + 1)) =? 4) then (fnParams toks size st (frame 14 depth) (position + 2)) else (haltAt toks size st (position + 1) (lit "expected `(` before the parameter types"))) else (let after : Nat := (if ((code toks (position + 1)) =? 9) then 3 else 1); (let infixAfter : Bool := (wordIs toks (position + after) (lit "*")); (if (both isWord (both (if infixAfter then false else true) (both (if (LexLeanStd.Text::startsWith word (lit "'")) then false else true) (if (reservedType word) then false else true)))) then (let member : Got := (memberPath toks size position); (match (project member error) + | Option.some problem => (haltWith st (project member error)) + | Option.none => (namedArgs toks size st (holding (frame 16 depth) (project member value) (ctor LexLeanStd.Json::Json.JNil)) (project member pos)))) else (push st (frame 17 depth) position 2 (ctor Pos.PType) (depth + 1)))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: startType, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd], + result: Rd, + body: (let position : Nat := (project st pos); (let depth : Nat := (project st depth); (if (256 (give st (position + 1) (kindNode kind (nil (Bytes * LexLeanStd.Json::Json)))) + | Option.none => (if (both (LexLeanStd.Text::startsWith word (lit "'")) (SurfaceGeneric::isBare (tailFrom word 1))) then (give st (position + 1) (kindNode (lit "parameter") (one "name" (str (tailFrom word 1))))) else (if (listed word (heads)) then (haltAt toks size st position (cat (cat (cat (cat (lit "`") word) (lit "` takes arguments: write `(")) word) (lit " …)`"))) else (let member : Got := (memberPath toks size position); (match (project member error) + | Option.some problem => (haltWith st (project member error)) + | Option.none => (give st (project member pos) (kindNode (lit "named") (two "member" (project member value) "arguments" (arr (nil LexLeanStd.Json::Json))))))))))) else (if (token =? 4) then (typeParen toks size (push st (frame 11 depth) (position + 1) 2 (ctor Pos.PType) depth) (position + 1) depth) else (haltAt toks size st position (cat (lit "expected a type, found ") (SurfaceGeneric::describeAt toks position)))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: typesNext, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd, top : Frame, position : Nat], + result: Rd, + body: (if ((code toks position) =? 3) then (give st (position + 1) (arr (project top values))) else (wantType st top position)), + executable: true +} + +definition{ + name: startTypes, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd], + result: Rd, + body: (let position : Nat := (project st pos); (if ((code toks position) =? 2) then (typesNext toks size st (frame 18 (project st depth)) (position + 1)) else (haltAt toks size st position (lit "expected `[`")))), + executable: true +} + +definition{ + name: termWord, + parameters: [ + toks : (List SurfaceLex::Spanned), + size : Nat, + st : Rd, + position : Nat, + word : Bytes + ], + result: Rd, + body: (let offset : Nat := (SurfaceGeneric::offsetAt toks size position); (if (same word (lit "true")) then (give st (position + 1) (kindNode (lit "bool") (one "value" (ctor LexLeanStd.Json::Json.JBool true)))) else (if (same word (lit "false")) then (give st (position + 1) (kindNode (lit "bool") (one "value" (ctor LexLeanStd.Json::Json.JBool false)))) else (if (allDigits word) then (if (both (1 (haltWith st (project spelling error)) + | Option.none => (let typeName : Bytes := (match (asStr (project spelling value)) + | Option.some text => text + | Option.none => (lit "")); (match (representationOf typeName) + | Option.some representation => (if (if (same word (lit "-0")) then true else (both (1 (halt st offset (cat (ticked typeName) (lit " is not an integer type"))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: finishUntil, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd, top : Frame, position : Nat], + result: Rd, + body: (let op : Nat := (project top op); (let arguments : LexLeanStd.Json::Json := (arr (project top values)); (if (op =? 38) then (give st position (kindNode (lit "apply") (two "function" (project top held) "arguments" arguments))) else (if (op =? 40) then (give st position (kindNode (lit "constructor") (withTypes (two "constructor" (project top held) "arguments" arguments) (project top heldMore)))) else (if (op =? 46) then (give st position (kindNode (lit "call") (withTypes (two "function" (project top held) "arguments" arguments) (project top heldMore)))) else (let checks : Got := (readNames toks size (position + 1) (lit "a check")); (match (project checks error) + | Option.some problem => (haltWith st (project checks error)) + | Option.none => (give st (project checks pos) (kindNode (lit "checked_apply") (withTypes (cons ((lit "checks"), (project checks value)) (two "model" (project top held) "arguments" arguments)) (project top heldMore))))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: untilNext, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd, top : Frame, position : Nat], + result: Rd, + body: (let stop : Bytes := (project top word); (if (if ((prim length stop : Nat) =? 0) then ((code toks position) =? 5) else (wordIs toks position stop)) then (finishUntil toks size st top position) else (if ((code toks position) =? 99) then (halt st size (lit "expected `)`")) else (wantTerm st top position)))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: recordNext, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd, top : Frame, position : Nat], + result: Rd, + body: (if ((code toks position) =? 1) then (give st (position + 1) (kindNode (lit "record") (withTypes (two "type" (project top held) "fields" (arr (project top values))) (project top heldMore)))) else (let field : Got := (readName toks size position (lit "a field name")); (match (project field error) + | Option.some problem => (haltWith st (project field error)) + | Option.none => (if ((code toks (project field pos)) =? 8) then (wantTerm st (worded top (match (asStr (project field value)) + | Option.some text => text + | Option.none => (lit "")) 0) ((project field pos) + 1)) else (haltAt toks size st (project field pos) (lit "expected `:=` after a field name")))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: resumeTyped, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd, top : Frame], + result: Rd, + body: (let position : Nat := (project st pos); (let op : Nat := (project top op); (let typed : Frame := (holding top (project top held) (project st value)); (if (op =? 41) then (give st position (kindNode (lit "function_ref") (withTypes (one "function" (project top held)) (project st value)))) else (if (op =? 42) then (if ((code toks position) =? 0) then (recordNext toks size st (staged typed 1) (position + 1)) else (haltAt toks size st position (lit "expected `{` before the fields"))) else (untilNext toks size st (staged typed 1) position)))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: afterMember, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd, top : Frame, position : Nat], + result: Rd, + body: (if ((code toks position) =? 2) then (push st top position 6 (ctor Pos.PAny) (project top depth)) else (resumeTyped toks size (record Rd {pos := position, stack := (project st stack), value := (ctor LexLeanStd.Json::Json.JNil), mode := 1, goal := (project st goal), place := (project st place), depth := (project st depth), error := (ctor Option.none [SurfaceLex::Failure]), spans := (project st spans)}) top)), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: primNext, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd, top : Frame, position : Nat], + result: Rd, + body: (let token : Nat := (code toks position); (if (token =? 7) then (wantType st (staged top 1) (position + 1)) else (if (if (token =? 99) then true else (token =? 5)) then (haltAt toks size st position (lit "expected `:` and the result type")) else (wantTerm st top position)))), + executable: true +} + +definition{ + name: matchNext, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd, top : Frame, position : Nat], + result: Rd, + body: (if (wordIs toks position (lit "|")) then (let member : Got := (memberPath toks size (position + 1)); (match (project member error) + | Option.some problem => (haltWith st (project member error)) + | Option.none => (let binders : Names := (first (prim iterate_until (fun [size, toks] (state : Names) => (match (project state error) + | Option.some problem => (ctor Option.none [Names]) + | Option.none => (if (wordIs toks (project state pos) (lit "=>")) then (ctor Option.none [Names]) else (let name : Got := (readName toks size (project state pos) (lit "a binder name")); (match (project name error) + | Option.some problem => (ctor Option.some [Names] (record Names {items := (project state items), pos := (project state pos), more := false, error := (project name error)})) + | Option.none => (ctor Option.some [Names] (record Names {items := (cons (project name value) (project state items)), pos := (project name pos), more := true, error := (ctor Option.none [SurfaceLex::Failure])}))))))) ((prim length toks : Nat) + 2) (record Names {items := (nil LexLeanStd.Json::Json), pos := (project member pos), more := true, error := (ctor Option.none [SurfaceLex::Failure])}) : (Names * Bool))); (match (project binders error) + | Option.some problem => (haltWith st (project binders error)) + | Option.none => (wantTerm st (placed (staged top 1) (project top place) (project top scope) (two "constructor" (project member value) "binders" (arr (project binders items)))) ((project binders pos) + 1)))))) else (match (project top values) + | List.nil => (haltAt toks size st position (lit "a match has at least one `| …` branch")) + | List.cons latest earlier => (give st position (kindNode (lit "match") (two "scrutinee" (project top held) "branches" (arr (project top values))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: funParams, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd, top : Frame, position : Nat], + result: Rd, + body: (if ((code toks position) =? 4) then (wantParam st top (position + 1)) else (match (project top values) + | List.nil => (haltAt toks size st position (lit "a function takes at least one parameter")) + | List.cons latest earlier => (if (wordIs toks position (lit "=>")) then (wantTerm st (staged top 1) (position + 1)) else (haltAt toks size st position (lit "expected `=>`"))))), + executable: true +} + +definition{ + name: callOrOperand, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd, position : Nat, depth : Nat], + result: Rd, + body: (let word : Bytes := (txt toks position); (let after : Nat := (if ((code toks (position + 1)) =? 9) then 3 else 1); (let infixAfter : Bool := (if ((code toks (position + after)) =? 6) then true else (both ((code toks (position + after)) =? 12) (isInfix (txt toks (position + after))))); (let tagged : Bool := ((code toks (position + 1)) =? 0); (if (both ((code toks position) =? 12) (both (plain word) (both (if infixAfter then false else true) (both (if tagged then false else true) (if (allDigits word) then false else true))))) then (let member : Got := (memberPath toks size position); (match (project member error) + | Option.some problem => (haltWith st (project member error)) + | Option.none => (afterMember toks size st (holding (frame 46 depth) (project member value) (ctor LexLeanStd.Json::Json.JNil)) (project member pos)))) else (wantTerm st (frame 47 depth) position)))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: termKeyword, + parameters: [ + toks : (List SurfaceLex::Spanned), + size : Nat, + st : Rd, + position : Nat, + depth : Nat, + keyword : Bytes + ], + result: Rd, + body: (let next : Nat := (position + 1); (if (if (same keyword (lit "not")) then true else (if (same keyword (lit "first")) then true else (same keyword (lit "second")))) then (wantTerm st (worded (frame 31 depth) keyword 0) next) else (if (same keyword (lit "cons")) then (wantTerm st (frame 32 depth) next) else (if (same keyword (lit "nil")) then (wantType st (frame 33 depth) next) else (if (same keyword (lit "if")) then (wantTerm st (frame 34 depth) next) else (if (same keyword (lit "let")) then (wantParam st (frame 35 depth) next) else (if (same keyword (lit "forall")) then (if ((code toks next) =? 4) then (wantParam st (frame 36 depth) (next + 1)) else (haltAt toks size st next (lit "expected `(` before the bound variable"))) else (if (same keyword (lit "fun")) then (if ((code toks next) =? 2) then (let captures : Got := (readNames toks size next (lit "a name")); (match (project captures error) + | Option.some problem => (haltWith st (project captures error)) + | Option.none => (funParams toks size st (holding (frame 37 depth) (project captures value) (ctor LexLeanStd.Json::Json.JNil)) (project captures pos)))) else (funParams toks size st (holding (frame 37 depth) (arr (nil LexLeanStd.Json::Json)) (ctor LexLeanStd.Json::Json.JNil)) next)) else (if (same keyword (lit "apply")) then (wantTerm st (frame 38 depth) next) else (if (same keyword (lit "project")) then (wantTerm st (frame 39 depth) next) else (if (if (same keyword (lit "ctor")) then true else (if (same keyword (lit "ref")) then true else (if (same keyword (lit "record")) then true else (same keyword (lit "checked"))))) then (let member : Got := (memberPath toks size next); (match (project member error) + | Option.some problem => (haltWith st (project member error)) + | Option.none => (afterMember toks size st (worded (holding (frame (if (same keyword (lit "ctor")) then 40 else (if (same keyword (lit "ref")) then 41 else (if (same keyword (lit "record")) then 42 else 44))) depth) (project member value) (ctor LexLeanStd.Json::Json.JNil)) (if (same keyword (lit "checked")) then (lit "with") else (lit "")) 0) (project member pos)))) else (if (same keyword (lit "prim")) then (let operation : Got := (readName toks size next (lit "a primitive operation")); (match (project operation error) + | Option.some problem => (haltWith st (project operation error)) + | Option.none => (primNext toks size st (holding (frame 43 depth) (project operation value) (ctor LexLeanStd.Json::Json.JNil)) (project operation pos)))) else (if (same keyword (lit "match")) then (wantTerm st (frame 45 depth) next) else (callOrOperand toks size st position depth)))))))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: termParen, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd, position : Nat, depth : Nat], + result: Rd, + body: (let token : Nat := (code toks position); (let word : Bytes := (txt toks position); (if (token =? 5) then (give st position (kindNode (lit "unit") (nil (Bytes * LexLeanStd.Json::Json)))) else (if (token =? 12) then (let numeral : Bytes := (if (LexLeanStd.Text::startsWith word (lit "-")) then (tailFrom word 1) else word); (if (both (allDigits numeral) ((code toks (position + 1)) =? 7)) then (integerLiteral toks size st position word) else (let genericBinder : Bool := (both (same word (lit "let")) (wordIs toks (position + 2) (lit "name"))); (let keyword : Bytes := (if (both ((code toks (position + 1)) =? 0) (if genericBinder then false else true)) then (lit "") else word); (termKeyword toks size st position depth keyword))))) else (wantTerm st (frame 47 depth) position))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: startTerm, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd], + result: Rd, + body: (let position : Nat := (project st pos); (let depth : Nat := (project st depth); (if (256 (haltWith st (project field error)) + | Option.none => (give st (project field pos) (kindNode (lit "project") (two "value" value "field" (project field value)))))) else (if (if (op =? 40) then true else (if (op =? 44) then true else (op =? 46))) then (if (stage =? 0) then (resumeTyped toks size st top) else (untilNext toks size st (adding top value) position)) else (if (op =? 41) then (resumeTyped toks size st top) else (if (op =? 42) then (if (stage =? 0) then (resumeTyped toks size st top) else (let assigned : Frame := (adding top (objectOf (two "field" (str (project top word)) "value" value))); (if ((code toks position) =? 6) then (recordNext toks size st assigned (position + 1)) else (if ((code toks position) =? 1) then (recordNext toks size st assigned position) else (haltAt toks size st position (lit "expected `,` or `}`")))))) else (if (op =? 43) then (if (stage =? 0) then (primNext toks size st (adding top value) position) else (give st position (kindNode (lit "primitive") (cons ((lit "result"), value) (two "operation" (project top held) "arguments" (arr (project top values))))))) else (if (op =? 45) then (if (stage =? 0) then (matchNext toks size st (staged (holding top value (ctor LexLeanStd.Json::Json.JNil)) 2) position) else (if (stage =? 1) then (matchNext toks size st (staged (adding top (objectOf (cons ((lit "body"), value) (project top members)))) 2) position) else (matchNext toks size st top position))) else (if (stage =? 0) then (if ((code toks position) =? 6) then (wantTerm st (staged (holding top value (ctor LexLeanStd.Json::Json.JNil)) 1) (position + 1)) else (match (infixKind (txt toks position)) + | Option.some kind => (if ((code toks position) =? 12) then (wantTerm st (worded (staged (holding top value (ctor LexLeanStd.Json::Json.JNil)) 2) kind 0) (position + 1)) else (haltAt toks size st position (lit "expected an operator or `,` after the first operand"))) + | Option.none => (haltAt toks size st position (lit "expected an operator or `,` after the first operand")))) else (if (stage =? 1) then (give st position (kindNode (lit "pair") (two "left" (project top held) "right" value))) else (let kind : Bytes := (project top word); (let implication : Bool := (same kind (lit "implies")); (give st position (kindNode kind (cons ((if implication then (lit "premise") else (lit "left")), (project top held)) (cons ((if implication then (lit "conclusion") else (lit "right")), value) (nil (Bytes * LexLeanStd.Json::Json))))))))))))))))))))))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: resumeType, + parameters: [toks : (List SurfaceLex::Spanned), size : Nat, st : Rd, top : Frame], + result: Rd, + body: (let position : Nat := (project st pos); (let value : LexLeanStd.Json::Json := (project st value); (let op : Nat := (project top op); (let stage : Nat := (project top stage); (if (op =? 11) then (if ((code toks position) =? 5) then (give st (position + 1) value) else (haltAt toks size st position (lit "expected `)`"))) else (if (op =? 12) then (let word : Bytes := (project top word); (give st position (if (same word (lit "List")) then (kindNode (lit "list") (one "element" value)) else (if (same word (lit "Set")) then (kindNode (lit "set") (one "element" value)) else (kindNode (lit "option") (one "value" value)))))) else (if (op =? 13) then (if (stage =? 0) then (wantType st (staged (holding top value (ctor LexLeanStd.Json::Json.JNil)) 1) position) else (give st position (if (same (project top word) (lit "Result")) then (kindNode (lit "result") (two "ok" (project top held) "error" value)) else (kindNode (lit "map") (two "key" (project top held) "value" value))))) else (if (op =? 14) then (if (stage =? 0) then (fnParams toks size st (adding top value) position) else (give st position (kindNode (lit "function") (two "parameters" (arr (project top values)) "result" value)))) else (if (op =? 16) then (namedArgs toks size st (adding top value) position) else (if (op =? 17) then (if (stage =? 0) then (if (wordIs toks position (lit "*")) then (wantType st (staged (holding top value (ctor LexLeanStd.Json::Json.JNil)) 1) (position + 1)) else (haltAt toks size st position (lit "expected `*` between the two types of a product"))) else (give st position (kindNode (lit "product") (two "left" (project top held) "right" value)))) else (if (op =? 18) then (let collected : Frame := (adding top value); (if ((code toks position) =? 6) then (typesNext toks size st collected (position + 1)) else (if ((code toks position) =? 3) then (give st (position + 1) (arr (project collected values))) else (haltAt toks size st position (lit "expected `,` or `]`"))))) else (give st position (objectOf (two "name" (project top held) "type" value)))))))))))))), + axioms: [propext], + executable: true +} + +definition{ + name: resumeTop, + parameters: [ + toks : (List SurfaceLex::Spanned), + size : Nat, + schema : LexLeanStd.Json::Json, + st : Rd, + top : Frame + ], + result: Rd, + body: (let position : Nat := (project st pos); (let value : LexLeanStd.Json::Json := (project st value); (if ((project top stage) =? 0) then (if ((code toks position) =? 99) then (finished st value (nil (Nat * Nat))) else (haltAt toks size st position (lit "unexpected text after the value"))) else (let collected : Frame := (spanned (adding top value) ((project top offset), (lastEnd toks position)) 0); (if ((code toks position) =? 99) then (finished st (arr (project collected values)) (project collected spans)) else (push st (worded collected (lit "") (SurfaceGeneric::offsetAt toks size position)) position 0 (project top place) 1)))))), + executable: true +} + +definition{ + name: resumeGeneric, + parameters: [ + toks : (List SurfaceLex::Spanned), + size : Nat, + schema : LexLeanStd.Json::Json, + st : Rd, + top : Frame + ], + result: Rd, + body: (let position : Nat := (project st pos); (let value : LexLeanStd.Json::Json := (project st value); (if ((project top op) =? 1) then (let collected : Frame := (adding top value); (if ((code toks position) =? 6) then (if ((code toks (position + 1)) =? 3) then (give st (position + 2) (arr (project collected values))) else (push st collected (position + 1) 0 (project top place) ((project top depth) + 1))) else (if ((code toks position) =? 3) then (give st (position + 1) (arr (project collected values))) else (haltAt toks size st position (lit "expected `,` or `]`"))))) else (let key : Bytes := (project top word); (if (SurfaceGeneric::hasKey (project top members) key) then (halt st (project top offset) (cat (cat (lit "the member `") key) (lit "` is written twice"))) else (let collected : Frame := (placed top (project top place) (project top scope) (cons (key, value) (project top members))); (if ((code toks position) =? 6) then (fieldsNext toks size schema st collected (position + 1)) else (if ((code toks position) =? 1) then (give st (position + 1) (objectOf (project collected members))) else (haltAt toks size st position (lit "expected `,` or `}`")))))))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: deliver, + parameters: [ + toks : (List SurfaceLex::Spanned), + size : Nat, + schema : LexLeanStd.Json::Json, + st : Rd + ], + result: Rd, + body: (match (project st stack) + | List.nil => (finished st (project st value) (nil (Nat * Nat))) + | List.cons top below => (let popped : Rd := (record Rd {pos := (project st pos), stack := below, value := (project st value), mode := 1, goal := (project st goal), place := (project st place), depth := (project st depth), error := (ctor Option.none [SurfaceLex::Failure]), spans := (project st spans)}); (let op : Nat := (project top op); (if (op =? 0) then (resumeTop toks size schema popped top) else (if (op (ctor Option.none [Rd]) + | Option.none => (let mode : Nat := (project st mode); (if (mode =? 9) then (ctor Option.none [Rd]) else (ctor Option.some [Rd] (if (mode =? 0) then (start toks size schema st) else (deliver toks size schema st)))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: run, + parameters: [ + toks : (List SurfaceLex::Spanned), + size : Nat, + schema : LexLeanStd.Json::Json, + top : Frame, + place : Pos + ], + result: Rd, + body: (let initial : Rd := (record Rd {pos := 0, stack := (cons top (nil Frame)), value := (ctor LexLeanStd.Json::Json.JNil), mode := 0, goal := 0, place := place, depth := 1, error := (ctor Option.none [SurfaceLex::Failure]), spans := (nil (Nat * Nat))}); (first (prim iterate_until (fun [schema, size, toks] (st : Rd) => (step toks size schema st)) ((prim multiply (prim length toks : Nat) 16 : Nat) + 64) initial : (Rd * Bool)))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: positionOf, + parameters: [schema : LexLeanStd.Json::Json, position : Nat], + result: Pos, + body: (if (position =? 0) then (match (getIn schema (lit "properties")) + | Option.some properties => (match (getIn properties (lit "declarations")) + | Option.some declarations => (itemsOf schema declarations) + | Option.none => (ctor Pos.PAny)) + | Option.none => (ctor Pos.PAny)) else (if (position =? 1) then (ctor Pos.PTerm) else (if (position =? 2) then (ctor Pos.PType) else (match (defNamed schema (lit "proof")) + | Option.some proof => (ctor Pos.PNode proof) + | Option.none => (ctor Pos.PAny))))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: outcome, + parameters: [final : Rd], + result: (Result LexLeanStd.Json::Json SurfaceLex::Failure), + body: (match (project final error) + | Option.some problem => (ctor Result.error [LexLeanStd.Json::Json, SurfaceLex::Failure] problem) + | Option.none => (if ((project final mode) =? 9) then (ctor Result.ok [LexLeanStd.Json::Json, SurfaceLex::Failure] (project final value)) else (ctor Result.error [LexLeanStd.Json::Json, SurfaceLex::Failure] (record SurfaceLex::Failure {offset := 0, message := (lit "the reader ran out of steps")})))), + executable: true +} + +definition{ + name: readAll, + parameters: [schema : LexLeanStd.Json::Json, toks : (List SurfaceLex::Spanned), size : Nat], + result: Rd, + body: (let place : Pos := (positionOf schema 0); (if ((prim length toks : Nat) =? 0) then (record Rd {pos := 0, stack := (nil Frame), value := (arr (nil LexLeanStd.Json::Json)), mode := 9, goal := 0, place := place, depth := 1, error := (ctor Option.none [SurfaceLex::Failure]), spans := (nil (Nat * Nat))}) else (run toks size schema (worded (staged (placed (frame 0 1) place (ctor Option.none [LexLeanStd.Json::Json]) (nil (Bytes * LexLeanStd.Json::Json))) 1) (lit "") (SurfaceGeneric::offsetAt toks size 0)) place))), + axioms: [Quot.sound, propext], + executable: true +} + +definition{ + name: parseAt, + parameters: [schema : LexLeanStd.Json::Json, text : Bytes, position : Nat], + result: (Result LexLeanStd.Json::Json SurfaceLex::Failure), + body: (match (SurfaceLex::lexText text) + | Result.ok toks => (let place : Pos := (positionOf schema position); (outcome (run toks (prim length text : Nat) schema (placed (frame 0 1) place (ctor Option.none [LexLeanStd.Json::Json]) (nil (Bytes * LexLeanStd.Json::Json))) place))) + | Result.error problem => (ctor Result.error [LexLeanStd.Json::Json, SurfaceLex::Failure] problem)), + axioms: [Quot.sound, propext], + executable: true, + production: {effects: [allocation, overflow], targets: [rust-std]} +} + +definition{ + name: parseSemanticModule, + parameters: [schema : LexLeanStd.Json::Json, text : Bytes, spec : Bytes], + result: (Result LexLeanStd.Json::Json SurfaceLex::Failure), + body: (match (SurfaceLex::lexText text) + | Result.ok toks => (match (outcome (readAll schema toks (prim length text : Nat))) + | Result.ok declarations => (ctor Result.ok [LexLeanStd.Json::Json, SurfaceLex::Failure] (objectOf (two "spec" (str spec) "declarations" declarations))) + | Result.error problem => (ctor Result.error [LexLeanStd.Json::Json, SurfaceLex::Failure] problem)) + | Result.error problem => (ctor Result.error [LexLeanStd.Json::Json, SurfaceLex::Failure] problem)), + axioms: [Quot.sound, propext], + executable: true, + production: {effects: [allocation, overflow], targets: [rust-std]} +} + +definition{ + name: semanticSpans, + parameters: [schema : LexLeanStd.Json::Json, text : Bytes], + result: (Result (List (Nat * Nat)) SurfaceLex::Failure), + body: (match (SurfaceLex::lexText text) + | Result.ok toks => (let final : Rd := (readAll schema toks (prim length text : Nat)); (match (outcome final) + | Result.ok declarations => (ctor Result.ok [(List (Nat * Nat)), SurfaceLex::Failure] (LexLeanStd.Order::reverseList [(Nat * Nat)] (project final spans))) + | Result.error problem => (ctor Result.error [(List (Nat * Nat)), SurfaceLex::Failure] problem))) + | Result.error problem => (ctor Result.error [(List (Nat * Nat)), SurfaceLex::Failure] problem)), + axioms: [Quot.sound, propext], + executable: true, + production: {effects: [allocation, overflow], 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..3cf1e0dd84 100644 --- a/crates/conformance/src/cases/bootstrap.rs +++ b/crates/conformance/src/cases/bootstrap.rs @@ -49,6 +49,14 @@ mod surface_lex; mod surface_module; mod surface_tooling; mod tooling_latex; +mod grammar_proof; +mod semantic_print; +mod semantic_read; +mod core_json; +mod core_print; +mod core_read; +mod grammar_math; +mod proposition_terms; /// The module the first component lives in. const MODULE: &str = "LexLeanBoot.AxiomOutput"; @@ -2574,6 +2582,14 @@ fn case(id: &str) { "BS-32" => content_id::case(), "BS-33" => project_text::case(), "BS-34" => manifest_text::case(), + "BS-35" => grammar_proof::case(), + "BS-36" => semantic_print::case(), + "BS-37" => semantic_read::case(), + "BS-38" => core_json::case(), + "BS-39" => core_print::case(), + "BS-40" => core_read::case(), + "BS-42" => grammar_math::case(), + "BS-43" => proposition_terms::case(), other => panic!("no bootstrap case is wired for {other}"), } } diff --git a/crates/conformance/src/cases/bootstrap/core_json.rs b/crates/conformance/src/cases/bootstrap/core_json.rs new file mode 100644 index 0000000000..41b2632d98 --- /dev/null +++ b/crates/conformance/src/cases/bootstrap/core_json.rs @@ -0,0 +1,782 @@ +//! `BS-38` (SPEC.md §17.23, §17.21.8, §21.7): the canonical JSON of a native +//! core module as language-1.2 source, `LexLeanBoot.CoreJson`, held to the byte +//! to `lexlean::surface::core::canonical_json`. +//! +//! The root takes the module as first-order data: its schema tag, its imports, +//! its node table (the kernel terms of §17.10 as an inductive of the module), its +//! proof nodes, and each declaration as the standard library's `Json` of the +//! value the oracle serializes it to. It writes the module's canonical JSON: +//! object keys in byte order, strings escaped as `quoteText` (`BS-22`) does, and +//! each declaration by the standard library's writer (`SL-11`). +//! +//! Each planted defect names the one vector that catches it. + +use std::collections::{BTreeMap, BTreeSet}; +use std::sync::OnceLock; + +use lexlean::artifact::canonical_json::Json; +use lexlean::calculus::Value; +use lexlean::ir::core::{ + CoreBinderInfo, CoreLevel, CoreMetaEntry, CoreMetaValue, CoreModule, CoreNode, +}; +use lexlean::surface::core::{canonical_json, parse_module}; +use lexlean::surface::module::{locate, CORE}; +use serde_json::Value as Json2; + +use super::rust_validate::{eligibility_of, rustc_differential}; +use super::{bool_val, list, nat_val, text, Run, Seeded}; +use crate::production_export as harness; +use crate::support::{self, repo_root}; + +const MODULE: &str = "LexLeanBoot.CoreJson"; +const SOURCE: &str = "src/CoreJson.lex.tex"; +const MODULE_SOURCE: &str = "bootstrap/src/CoreJson.lex.tex"; +const JSON_SOURCE: &str = "stdlib/src/Json.lex.tex"; +const ROOTS: [(&str, &[&str]); 1] = [("coreJson", &["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; +/// Committed core modules whose notation is at most this many bytes run under +/// the evaluator (one in `EVALUATOR_EVERY`), and under rustc those at most the +/// second bound. +const EVALUATOR_BYTES: usize = 16_000; +const EVALUATOR_EVERY: usize = 6; +const RUSTC_BYTES: usize = 300_000; +/// Seeded edits of the committed modules under the evaluator and under rustc. +const EVALUATOR_SEEDED: usize = 60; +const RUSTC_SEEDED: usize = 600; + +// ---------------------------------------------------------------- the sorts + +/// The constructors of each inductive of a module, in declaration order, read +/// from its text, so that an encoder names a constructor and never numbers it. +fn sorts_of(path: &str) -> BTreeMap> { + let source = std::fs::read_to_string(repo_root().join(path).as_std_path()).expect("a module"); + let mut out = BTreeMap::new(); + for block in source.split("inductive{").skip(1) { + let block = block.split("\n}\n").next().expect("a block"); + let name = block + .split("name: ") + .nth(1) + .and_then(|rest| rest.split(',').next()) + .expect("a sort name") + .trim() + .to_owned(); + let constructors = block + .split("{name: ") + .skip(1) + .map(|rest| { + rest.split(',') + .next() + .expect("a constructor") + .trim() + .to_owned() + }) + .collect(); + out.insert(name, constructors); + } + out +} + +fn sorts() -> &'static BTreeMap> { + static SORTS: OnceLock>> = OnceLock::new(); + SORTS.get_or_init(|| { + let mut sorts = sorts_of(MODULE_SOURCE); + let json = sorts_of(JSON_SOURCE); + sorts.insert("Json".to_owned(), json["Json"].clone()); + sorts + }) +} + +pub(super) fn adt(sort: &str, constructor: &str, fields: Vec) -> Value { + let found = sorts() + .get(sort) + .unwrap_or_else(|| panic!("no sort `{sort}`")) + .iter() + .position(|name| name == constructor) + .unwrap_or_else(|| panic!("`{sort}` has no constructor `{constructor}`")); + Value::Adt { + constructor: u64::try_from(found).expect("an index"), + fields, + } +} + +fn record(fields: Vec) -> Value { + Value::Adt { + constructor: 0, + fields, + } +} + +// ------------------------------------------------------------- the encoders + +pub(super) fn enc_json(json: &Json) -> Value { + match json { + Json::Bool(flag) => adt("Json", "JBool", vec![bool_val(*flag)]), + Json::Int(number) => adt( + "Json", + "JInt", + vec![ + bool_val(*number < 0), + Value::Nat { + value: number.unsigned_abs().to_string(), + }, + ], + ), + Json::Str(value) => adt("Json", "JStr", vec![text(value)]), + Json::Arr(items) => adt( + "Json", + "JArr", + vec![items + .iter() + .rev() + .fold(adt("Json", "JNil", Vec::new()), |rest, item| { + adt("Json", "JItem", vec![enc_json(item), rest]) + })], + ), + Json::Obj(members) => adt( + "Json", + "JObj", + vec![members + .iter() + .rev() + .fold(adt("Json", "JNil", Vec::new()), |rest, (key, value)| { + adt("Json", "JMember", vec![text(key), enc_json(value), rest]) + })], + ), + } +} + +fn level_value(level: &CoreLevel) -> Value { + match level { + CoreLevel::Zero => adt("Level", "LZero", Vec::new()), + CoreLevel::Succ(inner) => adt("Level", "LSucc", vec![level_value(inner)]), + CoreLevel::Max(pair) => adt( + "Level", + "LMax", + vec![level_value(&pair.0), level_value(&pair.1)], + ), + CoreLevel::IMax(pair) => adt( + "Level", + "LIMax", + vec![level_value(&pair.0), level_value(&pair.1)], + ), + CoreLevel::Param(name) => adt("Level", "LParam", vec![text(name)]), + } +} + +fn binder_value(info: CoreBinderInfo) -> Value { + let name = match info { + CoreBinderInfo::Explicit => "BExplicit", + CoreBinderInfo::Implicit => "BImplicit", + CoreBinderInfo::StrictImplicit => "BStrict", + CoreBinderInfo::Instance => "BInstance", + }; + adt("Binder", name, Vec::new()) +} + +fn entry_value(entry: &CoreMetaEntry) -> Value { + let value = match &entry.v { + CoreMetaValue::Bool { v } => adt("Meta", "MBool", vec![bool_val(*v)]), + CoreMetaValue::Nat { v } => adt("Meta", "MNat", vec![text(v)]), + CoreMetaValue::Int { v } => adt("Meta", "MInt", vec![text(v)]), + CoreMetaValue::Str { v } => adt("Meta", "MStr", vec![text(v)]), + CoreMetaValue::Name { v } => adt("Meta", "MName", vec![text(v)]), + }; + record(vec![text(&entry.n), value]) +} + +pub(super) fn node_value(node: &CoreNode) -> Value { + match node { + CoreNode::BVar { i } => adt("Node", "NBVar", vec![nat_val(*i)]), + CoreNode::Sort { l } => adt("Node", "NSort", vec![level_value(l)]), + CoreNode::Const { n, u } => adt( + "Node", + "NConst", + vec![text(n), list(u.iter().map(level_value).collect())], + ), + CoreNode::App { f, x } => adt("Node", "NApp", vec![nat_val(*f), nat_val(*x)]), + CoreNode::Lambda { n, b, t, v } => adt( + "Node", + "NLambda", + vec![text(n), binder_value(*b), nat_val(*t), nat_val(*v)], + ), + CoreNode::Forall { n, b, t, v } => adt( + "Node", + "NForall", + vec![text(n), binder_value(*b), nat_val(*t), nat_val(*v)], + ), + CoreNode::Let { n, t, v, body, d } => adt( + "Node", + "NLet", + vec![ + text(n), + nat_val(*t), + nat_val(*v), + nat_val(*body), + bool_val(*d), + ], + ), + CoreNode::Nat { v } => adt("Node", "NNat", vec![text(v)]), + CoreNode::String { v } => adt( + "Node", + "NString", + vec![list( + v.iter() + .map(|scalar| Value::Nat { + value: scalar.to_string(), + }) + .collect(), + )], + ), + CoreNode::Mdata { m, x } => adt( + "Node", + "NMdata", + vec![list(m.iter().map(entry_value).collect()), nat_val(*x)], + ), + CoreNode::Projection { n, s, i, x } => adt( + "Node", + "NProj", + vec![text(n), text(s), nat_val(*i), nat_val(*x)], + ), + } +} + +/// A module as the roots of `CoreJson` and `CorePrint` take it: each +/// declaration as the `Json` of the value the oracle serializes it to. +pub(super) fn module_value(module: &CoreModule) -> Value { + record(vec![ + text(&module.spec), + list(module.imports.iter().map(|name| text(name)).collect()), + list(module.nodes.iter().map(node_value).collect()), + list( + module + .proof_nodes + .iter() + .map(|node| nat_val(*node)) + .collect(), + ), + list( + module + .declarations + .iter() + .map(|declaration| { + let value = serde_json::to_value(declaration).expect("a declaration"); + enc_json(&Json::parse(value.to_string().as_bytes()).expect("canonical JSON")) + }) + .collect(), + ), + ]) +} + +fn run(module: &CoreModule) -> Run { + ( + vec![module_value(module)], + serde_json::to_value(text(&canonical_json(module).expect("the canonical JSON"))) + .expect("a value is JSON"), + ) +} + +// ------------------------------------------------------------------ corpus + +/// The committed core modules written in the notation, read, with the size of +/// their notation. +pub(super) fn committed() -> Vec<(String, CoreModule, usize)> { + let mut out = Vec::new(); + for (path, content) in super::scan::sources() { + if content.len() > RUSTC_BYTES { + continue; + } + let Some(found) = locate(&content) else { + continue; + }; + if !found.kind.is(&CORE) || found.data.is_some() { + continue; + } + let body = &content[found.body.0..found.body.1]; + if let Ok(module) = parse_module(body) { + out.push((path, module, body.len())); + } + } + out +} + +/// The smallest committed module: the declarations the vectors reuse. +fn smallest(modules: &[(String, CoreModule, usize)]) -> CoreModule { + modules + .iter() + .min_by_key(|(_, _, size)| *size) + .map(|(_, module, _)| module.clone()) + .expect("a committed core module") +} + +fn level(text: &str) -> CoreLevel { + serde_json::from_str(text).unwrap_or_else(|error| panic!("{text}: {error}")) +} + +fn node(text: &str) -> CoreNode { + serde_json::from_str(text).unwrap_or_else(|error| panic!("{text}: {error}")) +} + +/// Named vectors: the smallest committed module with its nodes, imports, proof +/// nodes, or spec replaced so that every form of the data is written. +pub(super) fn named(base: &CoreModule) -> Vec<(String, CoreModule)> { + let mut out = Vec::new(); + let mut add = |label: &str, change: &dyn Fn(&mut CoreModule)| { + let mut module = base.clone(); + change(&mut module); + out.push((label.to_owned(), module)); + }; + add("the smallest committed module", &|_| {}); + add("an empty module", &|module| { + module.imports.clear(); + module.nodes.clear(); + module.proof_nodes.clear(); + module.declarations.clear(); + }); + add("another spec", &|module| { + module.spec = "lexlean/core-module/1".to_owned(); + }); + add("imports to escape", &|module| { + module.imports = vec![ + "A".to_owned(), + "a \"b\" \\ c".to_owned(), + "line\nbreak\u{1}".to_owned(), + "é".to_owned(), + ]; + }); + add("every node kind", &|module| { + module.nodes = vec![ + node(r#"{"k":"b","i":3}"#), + node(r#"{"k":"s","l":{"k":"z"}}"#), + node(r#"{"k":"c","n":"Nat.succ","u":[]}"#), + node(r#"{"k":"a","f":2,"x":0}"#), + node(r#"{"k":"l","n":"x","b":"e","t":1,"v":0}"#), + node(r#"{"k":"p","n":"","b":"i","t":1,"v":1}"#), + node(r#"{"k":"e","n":"y","t":1,"v":2,"body":3,"d":true}"#), + node(r#"{"k":"e","n":"y","t":1,"v":2,"body":3,"d":false}"#), + node(r#"{"k":"n","v":"12345678901234567890"}"#), + node(r#"{"k":"t","v":[104,105,34,92,10,0,233,128512]}"#), + node( + r#"{"k":"d","m":[{"n":"b","v":{"t":"b","v":true}},{"n":"c","v":{"t":"b","v":false}},{"n":"n","v":{"t":"n","v":"7"}},{"n":"i","v":{"t":"i","v":"-7"}},{"n":"s","v":{"t":"s","v":"a\"b"}},{"n":"m","v":{"t":"m","v":"Nat.add"}}],"x":3}"#, + ), + node(r#"{"k":"j","n":"Prod.fst","s":"Prod","i":0,"x":3}"#), + ]; + }); + add("every binder", &|module| { + module.nodes = vec![ + node(r#"{"k":"s","l":{"k":"z"}}"#), + node(r#"{"k":"l","n":"a","b":"e","t":0,"v":0}"#), + node(r#"{"k":"l","n":"b","b":"i","t":0,"v":0}"#), + node(r#"{"k":"p","n":"c","b":"s","t":0,"v":0}"#), + node(r#"{"k":"p","n":"d","b":"c","t":0,"v":0}"#), + ]; + }); + add("every level", &|module| { + module.nodes = vec![ + CoreNode::Sort { + l: level(r#"{"k":"s","a":{"k":"s","a":{"k":"p","a":"u"}}}"#), + }, + CoreNode::Sort { + l: level( + r#"{"k":"m","a":[{"k":"z"},{"k":"i","a":[{"k":"p","a":"v"},{"k":"s","a":{"k":"z"}}]}]}"#, + ), + }, + CoreNode::Const { + n: "List.nil".to_owned(), + u: vec![ + level(r#"{"k":"p","a":"u"}"#), + level(r#"{"k":"z"}"#), + level(r#"{"k":"p","a":"a\"b"}"#), + ], + }, + ]; + }); + add("names to escape", &|module| { + module.nodes = vec![ + node(r#"{"k":"c","n":"a \"quoted\" name\t","u":[]}"#), + node(r#"{"k":"l","n":"é\\","b":"e","t":0,"v":0}"#), + node(r#"{"k":"j","n":"x\ny","s":"S\"","i":12,"x":0}"#), + node(r#"{"k":"n","v":"007"}"#), + ]; + }); + add("proof nodes", &|module| { + module.proof_nodes = vec![0, 2, 10, 1234]; + }); + add("one proof node", &|module| { + module.proof_nodes = vec![5]; + }); + add("an empty string node", &|module| { + module.nodes = vec![ + node(r#"{"k":"t","v":[]}"#), + node(r#"{"k":"d","m":[],"x":0}"#), + ]; + }); + add("one declaration", &|module| { + module.declarations.truncate(1); + }); + out +} + +const PIECES: &[&str] = &["x", "", "a\"b", "é", "Nat", "\\", "\n", "0"]; + +fn edit(seeded: &mut Seeded, base: &CoreModule) -> CoreModule { + let mut module = base.clone(); + for _ in 0..=seeded.below(3) { + match seeded.below(5) { + 0 if !module.nodes.is_empty() => { + let at = seeded.below(module.nodes.len()); + module.nodes.remove(at); + } + 1 if !module.nodes.is_empty() => { + let at = seeded.below(module.nodes.len()); + if let CoreNode::Const { n, .. } + | CoreNode::Lambda { n, .. } + | CoreNode::Forall { n, .. } = &mut module.nodes[at] + { + *n = (*seeded.pick(PIECES)).to_owned(); + } + } + 2 if !module.declarations.is_empty() => { + let at = seeded.below(module.declarations.len()); + module.declarations[at].name = (*seeded.pick(PIECES)).to_owned(); + } + 3 => module.imports.push((*seeded.pick(PIECES)).to_owned()), + _ => module.proof_nodes.push(seeded.below(100_000)), + } + } + module +} + +fn seeded_runs(count: usize, bases: &[CoreModule], seed: u64) -> Vec { + let mut seeded = Seeded(seed); + (0..count) + .map(|_| { + let base = seeded.pick(bases).clone(); + run(&edit(&mut seeded, &base)) + }) + .collect() +} + +// ------------------------------------------------------------------- plants + +struct Plant { + label: &'static str, + site: &'static str, + replacement: &'static str, + vector: &'static str, +} + +const PLANTS: &[Plant] = &[ + Plant { + label: "the instance binder is written implicit", + site: "| Binder.BInstance => (lit \"c\")", + replacement: "| Binder.BInstance => (lit \"i\")", + vector: "every binder", + }, + Plant { + label: "the strict binder is written implicit", + site: "| Binder.BStrict => (lit \"s\")", + replacement: "| Binder.BStrict => (lit \"i\")", + vector: "every binder", + }, + Plant { + label: "a lambda is written as a forall", + site: "(binderJson (lit \"l\") label info domain body)", + replacement: "(binderJson (lit \"p\") label info domain body)", + vector: "every binder", + }, + Plant { + label: "a successor level is tagged zero", + site: "(j \",'k':'s'}\")", + replacement: "(j \",'k':'z'}\")", + vector: "every level", + }, + Plant { + label: "an impredicative maximum is tagged maximum", + site: "(j \"],'k':'i'}\")", + replacement: "(j \"],'k':'m'}\")", + vector: "every level", + }, + Plant { + label: "a level parameter is not escaped", + site: "(j \"{'a':\") (quoted label)", + replacement: "(j \"{'a':\") label", + vector: "every level", + }, + Plant { + label: "levels are separated by a comma and a space", + site: "(cons (levelJson level) out)) (nil Bytes) levels : (List Bytes))) (lit \",\"))", + replacement: + "(cons (levelJson level) out)) (nil Bytes) levels : (List Bytes))) (lit \", \"))", + vector: "every level", + }, + Plant { + label: "a constant's levels are dropped", + site: "(j \",'u':[\")) (levelsJson levels))", + replacement: "(j \",'u':[\")) (lit \"\"))", + vector: "every level", + }, + Plant { + label: "let and have are swapped", + site: "(if dependent then (lit \"true\") else (lit \"false\"))", + replacement: "(if dependent then (lit \"false\") else (lit \"true\"))", + vector: "every node kind", + }, + Plant { + label: "a let's value is its body", + site: "(cat (j \",'v':\") (num bound))", + replacement: "(cat (j \",'v':\") (num rest))", + vector: "every node kind", + }, + Plant { + label: "string scalars are separated by a semicolon", + site: "(cons (num scalar) out)) (nil Bytes) scalars : (List Bytes))) (lit \",\"))", + replacement: "(cons (num scalar) out)) (nil Bytes) scalars : (List Bytes))) (lit \";\"))", + vector: "every node kind", + }, + Plant { + label: "an integer annotation is tagged natural", + site: "(j \"{'t':'i','v':\")", + replacement: "(j \"{'t':'n','v':\")", + vector: "every node kind", + }, + Plant { + label: "a false annotation is true", + site: "(if flag then (lit \"true\") else (lit \"false\"))", + replacement: "(lit \"true\")", + vector: "every node kind", + }, + Plant { + label: "a projection's index is its operand", + site: "(j \"{'i':\") (num position)) (j \",'k':'j','n':\")", + replacement: "(j \"{'i':\") (num inner)) (j \",'k':'j','n':\")", + vector: "every node kind", + }, + Plant { + label: "a bound variable is tagged v", + site: "(j \",'k':'b'}\")", + replacement: "(j \",'k':'v'}\")", + vector: "every node kind", + }, + Plant { + label: "a natural literal is not quoted", + site: "(j \"{'k':'n','v':\") (quoted text)", + replacement: "(j \"{'k':'n','v':\") text", + vector: "names to escape", + }, + Plant { + label: "proof nodes are separated by a comma and a space", + site: "(fun (index : Nat) => (num index))) (lit \",\"))", + replacement: "(fun (index : Nat) => (num index))) (lit \", \"))", + vector: "proof nodes", + }, + Plant { + label: "imports are not quoted", + site: "(fun (name : Bytes) => (quoted name))", + replacement: "(fun (name : Bytes) => name)", + vector: "imports to escape", + }, + Plant { + label: "the spec is not quoted", + site: "(cons (quoted (project module spec))", + replacement: "(cons (project module spec)", + vector: "the smallest committed module", + }, + Plant { + label: "a control character is not escaped in a name", + site: "(SurfaceLex::quoteText text)", + replacement: "(cat (cat 0x22 text) 0x22)", + vector: "names to escape", + }, +]; + +fn plants(vectors: &[(String, CoreModule)]) { + let mut missed = Vec::new(); + for plant in PLANTS { + let module = &vectors + .iter() + .find(|(label, _)| label == plant.vector) + .unwrap_or_else(|| panic!("{}: no vector `{}`", plant.label, plant.vector)) + .1; + let planted = support::P::bootstrap(); + let source = planted.read_raw(SOURCE); + assert_eq!( + source.matches(plant.site).count(), + 1, + "{}: the plant site occurs once", + plant.label + ); + planted.write(SOURCE, &source.replacen(plant.site, plant.replacement, 1)); + let checked = support::checked_project(&planted); + let low = super::lower(&checked, &planted, MODULE, ROOTS[0].0); + match super::evaluate(&low, &[run(module)], 1).first() { + Some(first) => eprintln!( + "BS-38: 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-38: {}: 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" + ); + } +} + +#[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("CoreJson"), + "CoreJson is a linked module of the project" + ); + eligibility_of(&project, "CoreJson", MODULE, &ROOTS); + + let modules = committed(); + assert!( + modules.len() >= 100, + "the committed core modules in the notation are {}", + modules.len() + ); + let vectors = named(&smallest(&modules)); + let labels: BTreeSet<&str> = vectors.iter().map(|(label, _)| label.as_str()).collect(); + assert_eq!(labels.len(), vectors.len(), "each vector has its own label"); + let named_runs: Vec = vectors.iter().map(|(_, module)| run(module)).collect(); + + let mut evaluator: Vec = Vec::new(); + let mut compiled: Vec = Vec::new(); + let mut bases: Vec = Vec::new(); + for (index, (_, module, size)) in modules.iter().enumerate() { + if *size <= EVALUATOR_BYTES && index % EVALUATOR_EVERY == 0 { + evaluator.push(run(module)); + bases.push(module.clone()); + } + compiled.push(run(module)); + } + let normative = support::normative_host(); + let (evaluator_seeded, rustc_seeded) = if normative { + ( + seeded_runs(EVALUATOR_SEEDED, &bases, 0x0B55_0038), + seeded_runs(RUSTC_SEEDED, &bases, 0x0B55_1038), + ) + } else { + (Vec::new(), Vec::new()) + }; + eprintln!( + "BS-38: {} vectors, {} committed core modules ({} under the evaluator, {} under rustc), {} and {} seeded edits ({:?})", + vectors.len(), + modules.len(), + evaluator.len(), + compiled.len(), + evaluator_seeded.len(), + rustc_seeded.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(); + if normative { + let mut corpus = named_runs.clone(); + corpus.extend(evaluator); + corpus.extend(evaluator_seeded); + let disagreements = super::evaluate(&lowered[ROOTS[0].0], &corpus, 5); + assert!( + disagreements.is_empty(), + "coreJson and the Rust oracle disagree: {disagreements:?}" + ); + eprintln!( + "BS-38: coreJson: the evaluator agrees on {} runs ({:?})", + corpus.len(), + started.elapsed() + ); + plants(&vectors); + } else { + eprintln!("BS-38: platform-bound host: the evaluator, the seeded runs, and the planted defects run on the normative host"); + } + + let mut all = named_runs; + all.extend(compiled); + all.extend(rustc_seeded); + let total = rustc_differential( + "BS-38", + &[(ROOTS[0].0, &lowered[ROOTS[0].0], all.as_slice())], + "oracle", + ); + eprintln!("BS-38: {total} rustc runs agree ({:?})", started.elapsed()); + + if !support::lean_backed("BS-38") { + return; + } + export(&lowered); + eprintln!("BS-38: passed in {:?}", started.elapsed()); +} diff --git a/crates/conformance/src/cases/bootstrap/core_print.rs b/crates/conformance/src/cases/bootstrap/core_print.rs new file mode 100644 index 0000000000..2889e642f5 --- /dev/null +++ b/crates/conformance/src/cases/bootstrap/core_print.rs @@ -0,0 +1,828 @@ +//! `BS-39` (SPEC.md §17.23, §17.21.8): the printer of the core notation as +//! language-1.2 source, `LexLeanBoot.CorePrint`, held to the byte to +//! `lexlean::surface::core::print_module`. +//! +//! The root takes a core module as `BS-38`'s `CoreJson` takes it. It checks +//! that the notation can number the module, then decides in one pass over the +//! node table, in the order of the nodes, which nodes are written inside the +//! node that uses them: a stack holds the nodes still to be written inside another, +//! and a node takes a child from the top of the stack when the child is used +//! once, ends right before the nodes the node has taken, and is less than 48 +//! deep. The same pass builds each node's term with the terms it takes; a term +//! names its bound variables and its constants by index and by name, which a +//! second walk resolves against the binders a statement writes, so the term of +//! a node does not depend on where it is written. +//! +//! Each planted defect names the one vector that catches it. + +use std::collections::{BTreeMap, BTreeSet}; + +use lexlean::calculus::Value; +use lexlean::ir::core::{ + CoreBinderInfo, CoreEqnInfo, CoreInductive, CoreModule, CoreNode, CoreStructure, + CoreStructureField, +}; +use lexlean::surface::core::print_module; +use serde_json::Value as Json2; + +use super::core_json::{committed, module_value}; +use super::rust_validate::{eligibility_of, rustc_differential}; +use super::{nat_val, text, Run, Seeded}; +use crate::production_export as harness; +use crate::support; + +const MODULE: &str = "LexLeanBoot.CorePrint"; +const SOURCE: &str = "src/CorePrint.lex.tex"; +const ROOTS: [(&str, &[&str]); 1] = [("printCoreModule", &["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; +/// Committed core modules whose notation is at most this many bytes run under +/// the evaluator (one in `EVALUATOR_EVERY`); every committed module the reader +/// of `BS-38`'s case keeps runs under rustc. +const EVALUATOR_BYTES: usize = 9_000; +const EVALUATOR_EVERY: usize = 4; +/// Seeded edits of the committed modules under the evaluator and under rustc. +const EVALUATOR_SEEDED: usize = 25; +const RUSTC_SEEDED: usize = 500; + +fn run(module: &CoreModule) -> (Run, Option) { + let (value, refusal) = match print_module(module) { + Ok(printed) => ( + Value::Ok { + value: Box::new(text(&printed)), + }, + None, + ), + Err(failure) => ( + Value::Error { + value: Box::new(Value::Adt { + constructor: 0, + fields: vec![nat_val(failure.offset), text(&failure.message)], + }), + }, + Some(failure.message), + ), + }; + ( + ( + vec![module_value(module)], + serde_json::to_value(value).expect("a value is JSON"), + ), + refusal, + ) +} + +// ------------------------------------------------------------------ vectors + +fn nodes(text: &str) -> Vec { + serde_json::from_str(text).unwrap_or_else(|error| panic!("{text}: {error}")) +} + +/// A chain of `count` annotations, each holding the one before it. +fn chain(count: usize) -> Vec { + let mut out = vec![CoreNode::Sort { + l: lexlean::ir::core::CoreLevel::Zero, + }]; + for index in 1..count { + out.push(CoreNode::Mdata { + m: Vec::new(), + x: index - 1, + }); + } + out +} + +/// An application of a long-named constant to `count` long-named arguments. +fn long_application(count: usize) -> Vec { + let mut out = vec![CoreNode::Const { + n: "Some.Rather.Long.Function.Name".to_owned(), + u: Vec::new(), + }]; + for index in 0..count { + let head = out.len() - 1; + out.push(CoreNode::Const { + n: format!("argument.with.a.long.name.number{index}"), + u: Vec::new(), + }); + out.push(CoreNode::App { + f: head, + x: head + 1, + }); + } + out +} + +#[allow(clippy::too_many_lines)] +fn named(base: &CoreModule) -> Vec<(String, CoreModule)> { + let mut out: Vec<(String, CoreModule)> = Vec::new(); + let mut add = |label: &str, change: &dyn Fn(&mut CoreModule)| { + let mut module = base.clone(); + change(&mut module); + out.push((label.to_owned(), module)); + }; + let table = |text: &'static str| { + move |module: &mut CoreModule| { + module.declarations.clear(); + module.proof_nodes.clear(); + module.nodes = nodes(text); + } + }; + add("the smallest committed module", &|_| {}); + add("another spec", &|module| { + module.spec = "lexlean/core-module/1".to_owned() + }); + add( + "a node pointing to itself", + &table(r#"[{"k":"a","f":0,"x":0}]"#), + ); + add( + "a node pointing forward", + &table(r#"[{"k":"s","l":{"k":"z"}},{"k":"a","f":0,"x":5}]"#), + ); + add("a repeated proof node", &|module| { + module.proof_nodes = vec![0, 0]; + }); + add("a proof node outside", &|module| { + module.proof_nodes = vec![module.nodes.len()]; + }); + add("a root outside", &|module| { + let count = module.nodes.len(); + module.declarations[0].r#type = Some(count + 3); + }); + add("an equation root outside", &|module| { + let count = module.nodes.len(); + module.declarations[0].eqn_info = Some(CoreEqnInfo { + rec_arg_pos: 0, + declarations: vec!["f".to_owned()], + num_fixed: 0, + perms: Vec::new(), + rev_deps: Vec::new(), + r#type: 0, + value: count, + levels: Vec::new(), + }); + }); + add("an auto parameter outside", &|module| { + let count = module.nodes.len(); + module.declarations[0].inductive = Some(CoreInductive { + num_params: 0, + num_indices: 0, + constructors: vec!["S.mk".to_owned()], + structure: Some(CoreStructure { + fields: vec![CoreStructureField { + name: "a".to_owned(), + projection: "S.a".to_owned(), + subobject: None, + binder: CoreBinderInfo::Explicit, + auto_param: Some(count + 1), + }], + parents: Vec::new(), + }), + }); + }); + add("an auto parameter inside", &|module| { + module.declarations[0].inductive = Some(CoreInductive { + num_params: 0, + num_indices: 0, + constructors: vec!["S.mk".to_owned()], + structure: Some(CoreStructure { + fields: vec![CoreStructureField { + name: "a".to_owned(), + projection: "S.a".to_owned(), + subobject: Some("P".to_owned()), + binder: CoreBinderInfo::Implicit, + auto_param: Some(0), + }], + parents: Vec::new(), + }), + }); + }); + add( + "a surrogate scalar", + &table(r#"[{"k":"t","v":[104,55296]},{"k":"t","v":[1114112]}]"#), + ); + add( + "a scalar beyond Unicode", + &table(r#"[{"k":"t","v":[104]},{"k":"t","v":[1114112,55296]}]"#), + ); + add( + "a bound variable by name", + &table( + r#"[{"k":"s","l":{"k":"z"}},{"k":"b","i":0},{"k":"l","n":"x","b":"e","t":0,"v":1}]"#, + ), + ); + add( + "a bound variable shadowed", + &table( + r#"[{"k":"s","l":{"k":"z"}},{"k":"b","i":1},{"k":"l","n":"x","b":"e","t":0,"v":1},{"k":"l","n":"x","b":"e","t":0,"v":2}]"#, + ), + ); + add( + "a bound variable of no name", + &table( + r#"[{"k":"s","l":{"k":"z"}},{"k":"b","i":0},{"k":"l","n":"a b","b":"e","t":0,"v":1},{"k":"b","i":4}]"#, + ), + ); + add( + "an empty binder name", + &table(r#"[{"k":"s","l":{"k":"z"}},{"k":"b","i":0},{"k":"p","n":"","b":"i","t":0,"v":1}]"#), + ); + add( + "an arrow", + &table( + r#"[{"k":"s","l":{"k":"z"}},{"k":"s","l":{"k":"s","a":{"k":"z"}}},{"k":"p","n":"","b":"e","t":0,"v":1}]"#, + ), + ); + add( + "an arrow whose domain is a binder form", + &table( + r#"[{"k":"s","l":{"k":"z"}},{"k":"b","i":0},{"k":"l","n":"y","b":"e","t":0,"v":1},{"k":"s","l":{"k":"z"}},{"k":"p","n":"","b":"e","t":2,"v":3}]"#, + ), + ); + add( + "a constant named like a binder", + &table( + r#"[{"k":"s","l":{"k":"z"}},{"k":"c","n":"x","u":[]},{"k":"l","n":"x","b":"e","t":0,"v":1}]"#, + ), + ); + add( + "constants with levels and quotes", + &table( + r#"[{"k":"c","n":"a b","u":[{"k":"s","a":{"k":"z"}}]},{"k":"c","n":"Nat.add","u":[{"k":"p","a":"u"},{"k":"m","a":[{"k":"z"},{"k":"p","a":"v"}]}]},{"k":"c","n":"fun","u":[]}]"#, + ), + ); + add( + "an application argument", + &table( + r#"[{"k":"c","n":"f","u":[]},{"k":"c","n":"g","u":[]},{"k":"c","n":"a","u":[]},{"k":"a","f":1,"x":2},{"k":"a","f":0,"x":3},{"k":"c","n":"b","u":[]},{"k":"a","f":4,"x":5}]"#, + ), + ); + add( + "a sort as an argument", + &table(r#"[{"k":"c","n":"f","u":[]},{"k":"s","l":{"k":"z"}},{"k":"a","f":0,"x":1}]"#), + ); + add( + "a binder form as a head and an argument", + &table( + r#"[{"k":"s","l":{"k":"z"}},{"k":"b","i":0},{"k":"l","n":"x","b":"e","t":0,"v":1},{"k":"s","l":{"k":"z"}},{"k":"b","i":0},{"k":"l","n":"y","b":"e","t":3,"v":4},{"k":"a","f":2,"x":5}]"#, + ), + ); + add( + "every binder visibility", + &table( + r#"[{"k":"s","l":{"k":"z"}},{"k":"b","i":0},{"k":"l","n":"a","b":"i","t":0,"v":1},{"k":"s","l":{"k":"z"}},{"k":"l","n":"b","b":"s","t":3,"v":2},{"k":"s","l":{"k":"z"}},{"k":"p","n":"c","b":"c","t":5,"v":4}]"#, + ), + ); + add( + "let and have", + &table( + r#"[{"k":"s","l":{"k":"z"}},{"k":"c","n":"v","u":[]},{"k":"b","i":0},{"k":"e","n":"x","t":0,"v":1,"body":2,"d":false},{"k":"s","l":{"k":"z"}},{"k":"c","n":"w","u":[]},{"k":"e","n":"","t":4,"v":5,"body":3,"d":true}]"#, + ), + ); + add( + "natural literals", + &table( + r#"[{"k":"n","v":"42"},{"k":"n","v":"0"},{"k":"n","v":"007"},{"k":"n","v":""},{"k":"n","v":"1x"}]"#, + ), + ); + add( + "a string literal", + &table(r#"[{"k":"t","v":[97,34,92,10,9,1,233,128512]},{"k":"t","v":[]}]"#), + ); + add( + "an annotation of every value", + &table( + r#"[{"k":"s","l":{"k":"z"}},{"k":"d","m":[{"n":"b","v":{"t":"b","v":true}},{"n":"c","v":{"t":"b","v":false}},{"n":"n","v":{"t":"n","v":"7"}},{"n":"n0","v":{"t":"n","v":"07"}},{"n":"i","v":{"t":"i","v":"-7"}},{"n":"i0","v":{"t":"i","v":"-0"}},{"n":"s","v":{"t":"s","v":"a\"b"}},{"n":"m","v":{"t":"m","v":"Nat.add"}},{"n":"a b","v":{"t":"m","v":"x y"}}],"x":0}]"#, + ), + ); + add( + "a projection", + &table( + r#"[{"k":"c","n":"p","u":[]},{"k":"j","n":"Prod.fst","s":"Prod","i":0,"x":0},{"k":"c","n":"q","u":[]},{"k":"c","n":"r","u":[]},{"k":"a","f":2,"x":3},{"k":"j","n":"a b","s":"S","i":12,"x":4}]"#, + ), + ); + add( + "sorts and levels", + &table( + r#"[{"k":"s","l":{"k":"s","a":{"k":"s","a":{"k":"s","a":{"k":"z"}}}}},{"k":"s","l":{"k":"s","a":{"k":"s","a":{"k":"p","a":"u"}}}},{"k":"s","l":{"k":"m","a":[{"k":"p","a":"u"},{"k":"s","a":{"k":"z"}}]}},{"k":"s","l":{"k":"i","a":[{"k":"s","a":{"k":"m","a":[{"k":"z"},{"k":"z"}]}},{"k":"p","a":"a b"}]}},{"k":"s","l":{"k":"z"}}]"#, + ), + ); + add("proof nodes", &|module| { + module.declarations.clear(); + module.nodes = nodes( + r#"[{"k":"c","n":"f","u":[]},{"k":"c","n":"a","u":[]},{"k":"a","f":0,"x":1},{"k":"c","n":"b","u":[]}]"#, + ); + module.proof_nodes = vec![1, 2, 3]; + }); + add( + "a node used twice", + &table(r#"[{"k":"c","n":"f","u":[]},{"k":"a","f":0,"x":0}]"#), + ); + add("a deep chain", &|module| { + module.declarations.clear(); + module.proof_nodes.clear(); + module.nodes = chain(60); + }); + add("a long statement", &|module| { + module.declarations.clear(); + module.proof_nodes.clear(); + module.nodes = long_application(6); + }); + add("a declaration with levels", &|module| { + module.declarations[0].levels = Some(vec!["u".to_owned(), "a b".to_owned()]); + }); + add("a derived declaration", &|module| { + let declaration = &mut module.declarations[0]; + declaration.levels = None; + declaration.r#type = None; + declaration.value = None; + declaration.derived = true; + }); + add("a declaration whose name is quoted", &|module| { + module.declarations[0].name = "a \"b\"".to_owned(); + }); + add("imports to quote", &|module| { + module.imports = vec![ + "A.B".to_owned(), + "a b".to_owned(), + "fun".to_owned(), + "x.".to_owned(), + "_".to_owned(), + "a01".to_owned(), + "a.01".to_owned(), + "é".to_owned(), + "a\u{a0}b".to_owned(), + "_x!?'@#".to_owned(), + "1a".to_owned(), + ]; + }); + add("no declarations and no imports", &|module| { + module.declarations.clear(); + module.imports.clear(); + module.proof_nodes.clear(); + module.nodes.clear(); + }); + out +} + +const REFUSALS: &[&str] = &[ + "the notation names the schema", + "which is not earlier", + "the proof nodes are not strictly increasing nodes of the module", + "has a root that is not a node of the module", + "is not a Unicode scalar value", +]; + +// ------------------------------------------------------------------- seeded + +const PIECES: &[&str] = &["x", "", "a b", "é", "Nat", "fun", "x.", "0"]; + +fn edit(seeded: &mut Seeded, base: &CoreModule) -> CoreModule { + let mut module = base.clone(); + for _ in 0..=seeded.below(3) { + match seeded.below(6) { + 0 if !module.nodes.is_empty() => { + let at = seeded.below(module.nodes.len()); + if let CoreNode::Const { n, .. } + | CoreNode::Lambda { n, .. } + | CoreNode::Forall { n, .. } + | CoreNode::Let { n, .. } = &mut module.nodes[at] + { + *n = (*seeded.pick(PIECES)).to_owned(); + } + } + 1 if !module.nodes.is_empty() => { + let at = seeded.below(module.nodes.len()); + if let CoreNode::App { f, .. } = &mut module.nodes[at] { + *f = seeded.below(at + 2); + } + } + 2 if !module.declarations.is_empty() => { + let at = seeded.below(module.declarations.len()); + module.declarations[at].name = (*seeded.pick(PIECES)).to_owned(); + } + 3 => module.imports.push((*seeded.pick(PIECES)).to_owned()), + 4 if !module.nodes.is_empty() => { + let at = seeded.below(module.nodes.len()); + if let CoreNode::BVar { i } = &mut module.nodes[at] { + *i = seeded.below(4); + } + } + _ => module + .proof_nodes + .push(seeded.below(module.nodes.len() + 2)), + } + } + module +} + +fn seeded_runs(count: usize, bases: &[CoreModule], seed: u64) -> Vec { + let mut seeded = Seeded(seed); + (0..count) + .map(|_| { + let base = seeded.pick(bases).clone(); + run(&edit(&mut seeded, &base)).0 + }) + .collect() +} + +// ------------------------------------------------------------------- plants + +struct Plant { + label: &'static str, + site: &'static str, + replacement: &'static str, + vector: &'static str, +} + +const PLANTS: &[Plant] = &[ + Plant { + label: "the inline height is 49", + site: "((project top height) (paren inner)\n | Term.Atom inner => inner", + replacement: "| Term.Low inner => inner\n | Term.Atom inner => inner", + vector: "an arrow whose domain is a binder form", + }, + Plant { + label: "an application is a bare argument", + site: "| Term.App head argsRev => (paren (whole term))", + replacement: "| Term.App head argsRev => (whole term)", + vector: "an application argument", + }, + Plant { + label: "a sort is a bare argument", + site: "| Term.Prefix inner => (paren inner)", + replacement: "| Term.Prefix inner => inner", + vector: "a sort as an argument", + }, + Plant { + label: "the instance binder is braced", + site: "| CoreJson::Binder.BInstance => ((lit \"[\"), (lit \"]\")))", + replacement: "| CoreJson::Binder.BInstance => ((lit \"{\"), (lit \"}\")))", + vector: "every binder visibility", + }, + Plant { + label: "have is written let", + site: "(if dependent then (lit \"have \") else (lit \"let \"))", + replacement: "(lit \"let \")", + vector: "let and have", + }, + Plant { + label: "a natural literal is never quoted", + site: "(if (canonicalNatural text) then text else (cat (lit \"nat \") (SurfaceLex::quoteText text)))", + replacement: "text", + vector: "natural literals", + }, + Plant { + label: "a leading zero is canonical", + site: "(if (LexLeanStd.Text::startsWith text (lit \"0\")) then false else true)", + replacement: "true", + vector: "natural literals", + }, + Plant { + label: "minus zero is a canonical integer", + site: "(both (canonicalNatural digits) (if (same text (lit \"-0\")) then false else true))", + replacement: "(canonicalNatural digits)", + vector: "an annotation of every value", + }, + Plant { + label: "a successor of zero is a sum", + site: "(if (project shown zero) then (num (project shown count))", + replacement: "(if false then (num (project shown count))", + vector: "sorts and levels", + }, + Plant { + label: "imax is written max", + site: "(lit \"imax(\")", + replacement: "(lit \"max(\")", + vector: "sorts and levels", + }, + Plant { + label: "a redundant zero is a name", + site: "(if (redundantZero text) then false else true)", + replacement: "true", + vector: "imports to quote", + }, + Plant { + label: "a name may end with a dot", + site: "(if (LexLeanStd.Text::endsWith text (lit \".\")) then false else true)", + replacement: "true", + vector: "imports to quote", + }, + Plant { + label: "a keyword is a name", + site: "(if (listed text (keywords)) then false else true)", + replacement: "true", + vector: "imports to quote", + }, + Plant { + label: "a no-break space continues a name", + site: "(if (code (prior (prior <=? proof)", + vector: "a repeated proof node", + }, + Plant { + label: "an auto parameter is no root", + site: "(pushOpt out (asNat (getIn field (lit \"auto_param\"))))", + replacement: "out", + vector: "an auto parameter outside", + }, + Plant { + label: "the type of a declaration is written with a colon only", + site: "(cat (lit \" : @\") (num root))", + replacement: "(cat (lit \": @\") (num root))", + vector: "the smallest committed module", + }, + Plant { + label: "declaration levels are not braced", + site: "(cat (cat (lit \".{\") (joined levels (lit \", \"))) (lit \"}\"))", + replacement: "(joined levels (lit \", \"))", + vector: "a declaration with levels", + }, + Plant { + label: "arguments are not indented", + site: "(cons head (cons (ctor Tdoc.TNest 2 (tseq", + replacement: "(cons head (cons (ctor Tdoc.TNest 0 (tseq", + vector: "a long statement", + }, + Plant { + label: "a statement is not indented", + site: "(cons (ctor Tdoc.TNest 2 (whole (project slot term)))", + replacement: "(cons (whole (project slot term))", + vector: "a long statement", + }, +]; + +fn plants(vectors: &[(String, CoreModule)]) { + let mut missed = Vec::new(); + for plant in PLANTS { + let module = &vectors + .iter() + .find(|(label, _)| label == plant.vector) + .unwrap_or_else(|| panic!("{}: no vector `{}`", plant.label, plant.vector)) + .1; + let planted = support::P::bootstrap(); + let source = planted.read_raw(SOURCE); + assert_eq!( + source.matches(plant.site).count(), + 1, + "{}: the plant site occurs once", + plant.label + ); + planted.write(SOURCE, &source.replacen(plant.site, plant.replacement, 1)); + let checked = support::checked_project(&planted); + let low = super::lower(&checked, &planted, MODULE, ROOTS[0].0); + match super::evaluate(&low, &[run(module).0], 1).first() { + Some(first) => eprintln!( + "BS-39: 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-39: {}: 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" + ); + } +} + +#[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("CorePrint") + && checked_units.units.contains_key("CoreJson"), + "CorePrint and CoreJson are linked modules of the project" + ); + eligibility_of(&project, "CorePrint", MODULE, &ROOTS); + + let modules = committed(); + assert!( + modules.len() >= 100, + "the committed core modules in the notation are {}", + modules.len() + ); + let base = modules + .iter() + .min_by_key(|(_, _, size)| *size) + .map(|(_, module, _)| module.clone()) + .expect("a committed core module"); + assert!( + base.declarations.len() >= 2 && !base.nodes.is_empty(), + "the smallest committed module has declarations and nodes" + ); + let vectors = named(&base); + let labels: BTreeSet<&str> = vectors.iter().map(|(label, _)| label.as_str()).collect(); + assert_eq!(labels.len(), vectors.len(), "each vector has its own label"); + let mut messages = Vec::new(); + let mut named_runs = Vec::new(); + for (_, module) in &vectors { + let (printed, refusal) = run(module); + named_runs.push(printed); + messages.extend(refusal); + } + for refusal in REFUSALS { + assert!( + messages.iter().any(|message| message.contains(refusal)), + "no vector reaches the refusal `{refusal}`" + ); + } + + let mut evaluator: Vec = Vec::new(); + let mut compiled: Vec = Vec::new(); + let mut bases: Vec = Vec::new(); + for (index, (path, module, size)) in modules.iter().enumerate() { + let (printed, refusal) = run(module); + assert!(refusal.is_none(), "{path}: a committed module prints"); + if *size <= EVALUATOR_BYTES && index % EVALUATOR_EVERY == 0 { + evaluator.push(printed.clone()); + bases.push(module.clone()); + } + compiled.push(printed); + } + let normative = support::normative_host(); + let (evaluator_seeded, rustc_seeded) = if normative { + ( + seeded_runs(EVALUATOR_SEEDED, &bases, 0x0B55_0039), + seeded_runs(RUSTC_SEEDED, &bases, 0x0B55_1039), + ) + } else { + (Vec::new(), Vec::new()) + }; + eprintln!( + "BS-39: {} vectors, {} committed core modules ({} under the evaluator, {} under rustc), {} and {} seeded edits ({:?})", + vectors.len(), + modules.len(), + evaluator.len(), + compiled.len(), + evaluator_seeded.len(), + rustc_seeded.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(); + if normative { + let mut corpus = named_runs.clone(); + corpus.extend(evaluator); + corpus.extend(evaluator_seeded); + let disagreements = super::evaluate(&lowered[ROOTS[0].0], &corpus, 5); + assert!( + disagreements.is_empty(), + "printCoreModule and the Rust oracle disagree: {disagreements:?}" + ); + eprintln!( + "BS-39: printCoreModule: the evaluator agrees on {} runs ({:?})", + corpus.len(), + started.elapsed() + ); + plants(&vectors); + } else { + eprintln!("BS-39: platform-bound host: the evaluator, the seeded runs, and the planted defects run on the normative host"); + } + + let mut all = named_runs; + all.extend(compiled); + all.extend(rustc_seeded); + let total = rustc_differential( + "BS-39", + &[(ROOTS[0].0, &lowered[ROOTS[0].0], all.as_slice())], + "oracle", + ); + eprintln!("BS-39: {total} rustc runs agree ({:?})", started.elapsed()); + + if !support::lean_backed("BS-39") { + return; + } + export(&lowered); + eprintln!("BS-39: passed in {:?}", started.elapsed()); +} diff --git a/crates/conformance/src/cases/bootstrap/core_read.rs b/crates/conformance/src/cases/bootstrap/core_read.rs new file mode 100644 index 0000000000..0f62ab6f2a --- /dev/null +++ b/crates/conformance/src/cases/bootstrap/core_read.rs @@ -0,0 +1,921 @@ +//! `BS-40` (SPEC.md §17.23, §17.21.8): the reader of the core notation as +//! language-1.2 source, `LexLeanBoot.CoreRead`, held to the byte to +//! `lexlean::surface::core::read_module`, the reader of `parse_module` up to +//! the decoding of each declaration's members, which stays in the host. +//! +//! The root reads with an explicit stack of jobs (a term, a binder, the parts +//! of a `fun`, a `let`, an arrow, an application, a `proof`, a projection, an +//! annotation) and a second machine for universe levels, so it admits no +//! `recursion`; it scans a token where the oracle peeks it, so that the first +//! error is the oracle's. A declaration is its members, read by the generic +//! notation of `BS-24`, with its kind, name, levels, and roots added. +//! +//! Each planted defect names the one vector that catches it. + +use std::collections::{BTreeMap, BTreeSet}; + +use lexlean::artifact::canonical_json::Json; +use lexlean::calculus::Value; +use lexlean::surface::core::{parse_module, read_module}; +use lexlean::surface::module::{locate, CORE}; +use serde_json::Value as Json2; + +use super::core_json::{enc_json, node_value}; +use super::rust_validate::{eligibility_of, rustc_differential}; +use super::{list, nat_val, text, Run, Seeded}; +use crate::production_export as harness; +use crate::support; + +const MODULE: &str = "LexLeanBoot.CoreRead"; +const SOURCE: &str = "src/CoreRead.lex.tex"; +const ROOTS: [(&str, &[&str]); 1] = [("readCoreModule", &["allocation", "overflow"])]; + +/// The most a part of a certificate of a root may be, as the row states. +const CERTIFICATE_BUDGET: u64 = 3_145_728; +/// Committed core modules of at most this many bytes of notation are read under +/// the evaluator (one in `EVALUATOR_EVERY`), and under rustc those at most the +/// second bound. +const EVALUATOR_BYTES: usize = 9_000; +const EVALUATOR_EVERY: usize = 10; +const RUSTC_BYTES: usize = 200_000; +/// Seeded edits of committed modules under the evaluator and under rustc. +const EVALUATOR_SEEDED: usize = 80; +const RUSTC_SEEDED: usize = 800; + +fn failure_value(offset: usize, message: &str) -> Value { + Value::Adt { + constructor: 0, + fields: vec![nat_val(offset), text(message)], + } +} + +/// What the oracle's reader says of a text, as the root's `Result Read Failure`. +fn run(source: &str) -> (Run, Option) { + let (value, refusal) = match read_module(source) { + Err(failure) => ( + Value::Error { + value: Box::new(failure_value(failure.offset, &failure.message)), + }, + Some(failure.message), + ), + Ok(read) => { + let first_error = read + .declarations + .iter() + .find_map(|declaration| declaration.as_ref().err()); + match first_error { + Some(failure) => ( + Value::Error { + value: Box::new(failure_value(failure.offset, &failure.message)), + }, + Some(failure.message.clone()), + ), + None => ( + Value::Ok { + value: Box::new(Value::Adt { + constructor: 0, + fields: vec![ + list(read.imports.iter().map(|name| text(name)).collect()), + list(read.nodes.iter().map(node_value).collect()), + list(read.proof_nodes.iter().map(|node| nat_val(*node)).collect()), + list( + read.declarations + .iter() + .map(|declaration| { + let (object, _) = + declaration.as_ref().expect("no error"); + enc_json(&Json::Obj(object.clone())) + }) + .collect(), + ), + list( + read.spans + .iter() + .map(|(start, stop)| Value::Adt { + constructor: 0, + fields: vec![nat_val(*start), nat_val(*stop)], + }) + .collect(), + ), + ], + }), + }, + None, + ), + } + } + }; + ( + ( + vec![text(source)], + serde_json::to_value(value).expect("a value is JSON"), + ), + refusal, + ) +} + +// ------------------------------------------------------------------ vectors + +const VALID: &str = "imports [A.B, \"a b\"] + +definition f.{u, v} : @s := @l { + class: false, + generated: false, + policy: exact{axioms: []}, + transparency: semireducible +} +theorem \"g h\" : @t {class: false, generated: false, policy: exact{axioms: []}, transparency: semireducible} +inductive d {derived: true} + +@s := Sort (max(u, imax(v, 0)) + 2) +@f := fun (x : @s) => fun {y : Sort u+1} => fun {{z : Sort 0}} => fun [w : Sort 1] => x y z w #7 const \"x\" Nat.succ.{u, 0} +@l := let a : @s := @f; have _ : Sort 0 := 0; proof(a) \"s\\n\\u00e9\" nat \"007\" (Sort 0) +@p := (b : Sort 0) -> @s -> b +@t := proj Prod \"fst\" 0 (annot [b := bool true, c := bool false, n := nat 7, m := nat \"07\", i := int -7, j := int \"-0\", s := str \"q\", k := name \"x y\"] @l) +"; + +#[allow(clippy::too_many_lines)] +fn named() -> Vec<(&'static str, String)> { + let mut out: Vec<(&'static str, String)> = Vec::new(); + let mut add = |label: &'static str, source: &str| out.push((label, source.to_owned())); + add("every form", VALID); + add("an empty module", "imports []\n"); + add("an empty text", ""); + add("no imports", "definition f : @0 := @0\n"); + add("imports without a bracket", "imports A\n"); + add("imports without a comma", "imports [A B]\n"); + add("a keyword import", "imports [fun]\n"); + add("a numeral import", "imports [1]\n"); + add("a name beginning with a digit", "imports [1a]\n"); + add("a trailing comma in imports", "imports [A,]\n"); + add("an unterminated string", "imports [\"a]"); + add("a bad escape", "imports [\"\\q\"]\n"); + add("a tab", "imports []\n\t@0 := Sort 0\n"); + add("a carriage return", "imports []\r\n"); + add("an unexpected scalar", "imports [=]\n"); + add("a lone dot", "imports [.]\n"); + add( + "a declaration not at the margin", + "imports []\n definition f : @0\n", + ); + add("an unknown declaration kind", "imports []\nlemma f : @0\n"); + add("a declaration of no name", "imports []\ndefinition : @0\n"); + add( + "universe parameters", + "imports []\ndefinition f.{u, \"v w\"} : @0\n@0 := Sort u\n", + ); + add( + "universe parameters unclosed", + "imports []\ndefinition f.{u, v : @0\n", + ); + add( + "a root that is no reference", + "imports []\ndefinition f : x\n", + ); + add( + "a value root that is no reference", + "imports []\ndefinition f : @0 := x\n@0 := Sort 0\n", + ); + add( + "a root too large", + "imports []\ndefinition f : @99999999999999999999\n", + ); + add( + "a derived row with parameters", + "imports []\ninductive f.{u}\n", + ); + add( + "members that are no object", + "imports []\ndefinition f : @0 [1]\n@0 := Sort 0\n", + ); + add("members unclosed", "imports []\ndefinition f : @0 {a: 1\n"); + add( + "members with a brace in a string", + "imports []\ndefinition f : @0 {a: \"}\", b: \"{\"}\n@0 := Sort 0\n", + ); + add( + "members malformed", + "imports []\ndefinition f : @0 {a 1}\n@0 := Sort 0\n", + ); + add( + "members that write the kind", + "imports []\ndefinition f : @0 {kind: x, name: y, levels: [z], type: 9}\n@0 := Sort 0\n", + ); + add( + "members on the next line", + "imports []\ndefinition f : @0\n{a: 1}\n", + ); + add( + "a declaration after a node", + "imports []\n@0 := Sort 0\ndefinition f : @0\n", + ); + add( + "a statement not at the margin", + "imports []\n@0 := Sort 0 @1 := Sort 0\n", + ); + add("a label of the wrong number", "imports []\n@1 := Sort 0\n"); + add( + "a statement that names an earlier node", + "imports []\n@0 := Sort 0\n@1 := @0\n", + ); + add( + "a statement that ends with an earlier node", + "imports []\n@0 := Sort 0\n@2 := (Sort 1) @0\n", + ); + add("a reference to a later node", "imports []\n@0 := @1\n"); + add( + "a reference to the node being written", + "imports []\n@0 := @0\n", + ); + add( + "a reference too large", + "imports []\n@0 := @99999999999999999999\n", + ); + add( + "a reference to an unknown name", + "imports []\n@0 := @nobody\n", + ); + add( + "named statements", + "imports []\ndefinition f : @ty := @val\n@ty := Sort 1\n@val := fun (x : @ty) => @ty\n", + ); + add( + "a statement named twice", + "imports []\n@a := Sort 0\n@a := Sort 1\n", + ); + add( + "a root naming no statement", + "imports []\ndefinition f : @missing\n@0 := Sort 0\n", + ); + add( + "a second root naming no statement", + "imports []\ndefinition f : @0\ndefinition g : @missing\n@0 := Sort 0\n", + ); + add( + "a statement without its assignment", + "imports []\n@0 Sort 0\n", + ); + add("a label of no number", "imports []\n@ := Sort 0\n"); + add( + "a number followed by letters", + "imports []\n@0x := Sort 0\n", + ); + add( + "a bound variable followed by letters", + "imports []\n@0 := #0x\n", + ); + add( + "a bound variable too large", + "imports []\n@0 := #99999999999999999999\n", + ); + add("a numeral with a leading zero", "imports []\n@0 := 007\n"); + add( + "a term that begins with a keyword", + "imports []\n@0 := max\n", + ); + add("the underscore as a term", "imports []\n@0 := _\n"); + add( + "a bound variable given levels", + "imports []\n@0 := Sort 0\n@1 := fun (x : @0) => x.{u}\n", + ); + add( + "a binder without its colon", + "imports []\n@0 := fun (x @0) => x\n", + ); + add( + "a binder unclosed", + "imports []\n@0 := Sort 0\n@1 := fun (x : @0 => x\n", + ); + add( + "a strict binder half closed", + "imports []\n@0 := Sort 0\n@1 := fun {{x : @0} => x\n", + ); + add("no binder after fun", "imports []\n@0 := fun x => x\n"); + add( + "fun without its arrow", + "imports []\n@0 := Sort 0\n@1 := fun (x : @0) x\n", + ); + add( + "a binder arrow missing", + "imports []\n@0 := Sort 0\n@1 := (x : @0) x\n", + ); + add("let without its colon", "imports []\n@0 := let x := 0; x\n"); + add( + "let without its assignment", + "imports []\n@0 := Sort 0\n@1 := let x : @0 0; x\n", + ); + add( + "let without its semicolon", + "imports []\n@0 := Sort 0\n@1 := let x : @0 := 0 x\n", + ); + add("an unclosed parenthesis", "imports []\n@0 := (Sort 0\n"); + add( + "a proof of a reference", + "imports []\n@0 := Sort 0\n@1 := proof(@0)\n", + ); + add("a proof unclosed", "imports []\n@0 := proof(Sort 0\n"); + add("nat without a string", "imports []\n@0 := nat 7\n"); + add("const without a string", "imports []\n@0 := const x\n"); + add("a level of no form", "imports []\n@0 := Sort [\n"); + add("a level too large", "imports []\n@0 := Sort 1025\n"); + add("a level raised too far", "imports []\n@0 := Sort u+1025\n"); + add("a level at the bound", "imports []\n@0 := Sort u+1024\n"); + add( + "max without a parenthesis", + "imports []\n@0 := Sort max u\n", + ); + add("max without a comma", "imports []\n@0 := Sort max(u v)\n"); + add("max unclosed", "imports []\n@0 := Sort imax(u, v\n"); + add( + "a level parenthesis unclosed", + "imports []\n@0 := Sort (u\n", + ); + add("levels unclosed", "imports []\n@0 := Nat.succ.{u, v\n"); + add("a keyword level", "imports []\n@0 := Sort true\n"); + add( + "an annotation without a bracket", + "imports []\n@0 := annot x @0\n", + ); + add( + "an annotation key without its assignment", + "imports []\n@0 := Sort 0\n@1 := annot [a bool true] @0\n", + ); + add( + "an annotation value of no type", + "imports []\n@0 := Sort 0\n@1 := annot [a := 7] @0\n", + ); + add( + "an annotation of an unknown type", + "imports []\n@0 := Sort 0\n@1 := annot [a := real 7] @0\n", + ); + add( + "an annotation Boolean of no value", + "imports []\n@0 := Sort 0\n@1 := annot [a := bool maybe] @0\n", + ); + add( + "an annotation number of no digits", + "imports []\n@0 := Sort 0\n@1 := annot [a := nat x] @0\n", + ); + add( + "an annotation natural with a minus", + "imports []\n@0 := Sort 0\n@1 := annot [a := nat -1] @0\n", + ); + add( + "an annotation integer of minus zero", + "imports []\n@0 := Sort 0\n@1 := annot [a := int -0] @0\n", + ); + add( + "an annotation integer with a leading zero", + "imports []\n@0 := Sort 0\n@1 := annot [a := int -07] @0\n", + ); + add( + "an annotation string of no string", + "imports []\n@0 := Sort 0\n@1 := annot [a := str x] @0\n", + ); + add( + "an annotation unclosed", + "imports []\n@0 := Sort 0\n@1 := annot [a := bool true @0\n", + ); + add( + "a projection of no index", + "imports []\n@0 := Sort 0\n@1 := proj S n x @0\n", + ); + add( + "a projection index with a leading zero", + "imports []\n@0 := Sort 0\n@1 := proj S n 01 @0\n", + ); + add( + "an application across lines", + "imports []\n@0 := Sort 0\n@1 := f @0\n@2 := g\n", + ); + add( + "an arrow binder with no name", + "imports []\n@0 := Sort 0\n@1 := (_ : @0) -> @0\n", + ); + add( + "a name shadowed by an inner binder", + "imports []\n@0 := Sort 0\n@1 := fun (x : @0) => fun (x : @0) => x\n", + ); + add( + "a name of an outer binder past an arrow", + "imports []\n@0 := Sort 0\n@f := fun (x : @0) => @0 -> x\n", + ); + add("a statement that is no term", "imports []\n@0 := )\n"); + let deep = format!( + "imports []\n@0 := {}Sort 0{}\n", + "(".repeat(260), + ")".repeat(260) + ); + out.push(("nested beyond the bound", deep)); + let at_bound = format!( + "imports []\n@0 := {}Sort 0{}\n", + "(".repeat(120), + ")".repeat(120) + ); + out.push(("nested within the bound", at_bound)); + out +} + +const REFUSALS: &[&str] = &[ + "a tab or a carriage return is not part of the notation", + "a core module begins with its `imports`", + "`@` and `#` are followed by a number or, after `@`, a name", + "a number ends where its text does", + "a name does not begin with a digit; quote it", + "unexpected `", + "unterminated string", + "is a keyword; quote it to write a name", + "the number is too large", + "a numeral has no redundant leading zero", + "nested more deeply than the notation reads", + "a level is raised by at most 1024 at once", + "a level is at most 1024", + "expected a universe level", + "expected a binder", + "is a bound variable and has no universe levels", + "is not a node that has been written", + "no statement written before this is named", + "is a keyword; a term that begins with it is written in parentheses", + "`_` is the name of a binder, not a constant", + "`proof` marks a node written inside it, not a reference", + "expected a term", + "expected the type of the value", + "expected `true` or `false`", + "is not the type of an annotation value", + "expected a number", + "the braces of the declaration are not closed", + "does not begin a declaration or a node", + // `the members of a declaration are an object` is kept in the module as the + // oracle writes it but no text reaches it: the members are the text from a + // `{` to its matching `}`, which the generic reader reads as an object or + // refuses. + "a row without a type states no universe parameters", + "expected `@N`, the type", + "a statement defines a node, not another name for one", + // `a statement defines the node its term ends with` is kept in the module + // as the oracle writes it but no text reaches it: a term's own node is the + // last one reading it numbers, and a term that is an earlier node is the + // refusal above. + "the label is the number of the node", + "names two statements", + "a statement ends at the end of its line", + "a declaration or a node begins its line", + "the declarations come before the nodes", + "no statement is named", +]; + +// ------------------------------------------------------------------- seeded + +const PIECES: &[&str] = &[ + "(", ")", "{", "}", "[", "]", ",", ":", ":=", ";", "->", "=>", "+", "-", ".{", "@0", "@1", + "@a", "#0", "0", "07", "\"s\"", "x", "fun", "let", "have", "Sort", "proj", "annot", "const", + "max", "proof", "_", "\n", " ", "\t", "Nat.succ", +]; + +fn edited(seeded: &mut Seeded, source: &str) -> String { + let mut scalars: Vec = source.chars().collect(); + for _ in 0..=seeded.below(2) { + let at = seeded.below(scalars.len() + 1); + let piece: Vec = seeded.pick(PIECES).chars().collect(); + match seeded.below(3) { + 0 if at < scalars.len() => { + scalars.remove(at); + } + 1 => { + drop(scalars.splice(at..at, piece)); + } + _ if at < scalars.len() => { + drop(scalars.splice(at..=at, piece)); + } + _ => scalars.extend(piece), + } + } + scalars.into_iter().collect() +} + +fn seeded_runs(count: usize, bases: &[String], seed: u64) -> Vec { + let mut seeded = Seeded(seed); + (0..count) + .map(|_| { + let base = seeded.pick(bases).clone(); + run(&edited(&mut seeded, &base)).0 + }) + .collect() +} + +/// The committed core modules written in the notation: the text of each body. +fn committed() -> Vec<(String, String)> { + let mut out = Vec::new(); + for (path, content) in super::scan::sources() { + if content.len() > RUSTC_BYTES { + continue; + } + let Some(found) = locate(&content) else { + continue; + }; + if found.kind.is(&CORE) && found.data.is_none() { + out.push((path, content[found.body.0..found.body.1].to_owned())); + } + } + out +} + +// ------------------------------------------------------------------- plants + +struct Plant { + label: &'static str, + site: &'static str, + replacement: &'static str, + vector: &'static str, +} + +const PLANTS: &[Plant] = &[ + Plant { + label: "a tab is white space", + site: "(if (if (code =? 9) then true else (code =? 13)) then", + replacement: "(if (code =? 13) then", + vector: "a tab", + }, + Plant { + label: "a carriage return is white space", + site: "(if (if (code =? 9) then true else (code =? 13)) then", + replacement: "(if (code =? 9) then", + vector: "a carriage return", + }, + Plant { + label: "a level may be raised by 1025", + site: "(if (1024 eprintln!( + "BS-40: 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-40: {}: 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" + ); + } +} + +#[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("CoreRead"), + "CoreRead is a linked module of the project" + ); + eligibility_of(&project, "CoreRead", MODULE, &ROOTS); + + let vectors = named(); + let labels: BTreeSet<&str> = vectors.iter().map(|(label, _)| *label).collect(); + assert_eq!(labels.len(), vectors.len(), "each vector has its own label"); + let mut messages = Vec::new(); + let mut named_runs = Vec::new(); + for (_, source) in &vectors { + let (outcome, refusal) = run(source); + named_runs.push(outcome); + messages.extend(refusal); + } + let unreached: Vec<&str> = REFUSALS + .iter() + .filter(|refusal| !messages.iter().any(|message| message.contains(*refusal))) + .copied() + .collect(); + assert!( + unreached.is_empty(), + "no vector reaches the refusals {unreached:?}" + ); + + let modules = committed(); + assert!( + modules.len() >= 100, + "the committed core modules in the notation are {}", + modules.len() + ); + let mut evaluator: Vec = Vec::new(); + let mut compiled: Vec = Vec::new(); + let mut bases: Vec = Vec::new(); + for (index, (path, body)) in modules.iter().enumerate() { + // The reader the root is held to is the oracle's: what it reads, the + // decoding makes the module `parse_module` returns. + assert!( + parse_module(body).is_ok(), + "{path}: a committed module reads" + ); + let (outcome, refusal) = run(body); + assert!(refusal.is_none(), "{path}: a committed module reads"); + if body.len() <= EVALUATOR_BYTES && index % EVALUATOR_EVERY == 0 { + evaluator.push(outcome.clone()); + bases.push(body.clone()); + } + compiled.push(outcome); + } + bases.push(VALID.to_owned()); + let normative = support::normative_host(); + let (evaluator_seeded, rustc_seeded) = if normative { + ( + seeded_runs(EVALUATOR_SEEDED, &bases, 0x0B55_0040), + seeded_runs(RUSTC_SEEDED, &bases, 0x0B55_1040), + ) + } else { + (Vec::new(), Vec::new()) + }; + eprintln!( + "BS-40: {} vectors, {} committed core modules ({} under the evaluator, {} under rustc), {} and {} seeded edits ({:?})", + vectors.len(), + modules.len(), + evaluator.len(), + compiled.len(), + evaluator_seeded.len(), + rustc_seeded.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(); + if normative { + let mut corpus = named_runs.clone(); + corpus.extend(evaluator); + corpus.extend(evaluator_seeded); + let disagreements = super::evaluate(&lowered[ROOTS[0].0], &corpus, 5); + assert!( + disagreements.is_empty(), + "readCoreModule and the Rust oracle disagree: {disagreements:?}" + ); + eprintln!( + "BS-40: readCoreModule: the evaluator agrees on {} runs ({:?})", + corpus.len(), + started.elapsed() + ); + plants(&vectors); + } else { + eprintln!("BS-40: platform-bound host: the evaluator, the seeded runs, and the planted defects run on the normative host"); + } + + let mut all = named_runs; + all.extend(compiled); + all.extend(rustc_seeded); + let total = rustc_differential( + "BS-40", + &[(ROOTS[0].0, &lowered[ROOTS[0].0], all.as_slice())], + "oracle", + ); + eprintln!("BS-40: {total} rustc runs agree ({:?})", started.elapsed()); + + if !support::lean_backed("BS-40") { + return; + } + export(&lowered); + eprintln!("BS-40: passed in {:?}", started.elapsed()); +} diff --git a/crates/conformance/src/cases/bootstrap/grammar_math.rs b/crates/conformance/src/cases/bootstrap/grammar_math.rs new file mode 100644 index 0000000000..7bd4c15203 --- /dev/null +++ b/crates/conformance/src/cases/bootstrap/grammar_math.rs @@ -0,0 +1,1197 @@ +//! `BS-42` (SPEC.md §17.23, §15.5, §14.1): the grammar of a mathematical island +//! as language-1.2 source, `LexLeanBoot.GrammarMath`, held to the byte to +//! `lexlean::grammar::math::parse_math`. +//! +//! The root parses an island's atom range by the oracle's Pratt parser, kept +//! as an explicit stack of frames under `iterate_until`, under every +//! segmentation the token lattice admits, with the oracle's worklist of +//! branch points. The glossary closure is the lexicon's: the host gives the +//! root its projection, the math-channel edges at each atom position with the +//! fields of each edge's form the parser reads (frame, precedence, +//! associativity), and the limits and counters of the parse budget, so every +//! limit failure falls where the oracle's does. +//! +//! Each planted defect names the one vector that catches it. + +use std::collections::{BTreeMap, BTreeSet}; +use std::sync::OnceLock; + +use lexlean::calculus::Value; +use lexlean::grammar::chart::Budget; +use lexlean::grammar::math::{parse_math, LeafKind, MathAst}; +use lexlean::lexicon::entry::{Associativity, Channel, Frame}; +use lexlean::lexicon::package::LoadContext; +use lexlean::lexicon::resolve::{Closure, FormRef}; +use lexlean::source::atom::{Atom, AtomClass}; +use serde_json::Value as Json; + +use super::rust_validate::{eligibility_of, rustc_differential}; +use super::scan::{atom_value, rejection_value}; +use super::{list, nat_val, text, Run, Seeded}; +use crate::production_export as harness; +use crate::support; + +const MODULE: &str = "LexLeanBoot.GrammarMath"; +const SOURCE: &str = "src/GrammarMath.lex.tex"; +const ROOTS: [(&str, &[&str]); 1] = [("parseMath", &["allocation", "overflow"])]; + +/// The most a part of a certificate of a root may be, as the row states. +const CERTIFICATE_BUDGET: u64 = 3_145_728; +/// The path every vector is read under. +const PATH: &str = "src/Island.lex.tex"; +/// The limits of the committed projects (`bootstrap/lexlean.toml`). +const MAX_EDGES: u64 = 4_000_000; +const MAX_STATES: u64 = 4_000_000; +const MAX_DEPTH: u64 = 1_024; +/// Islands of the committed sources: at most this many atoms, and how many +/// under the evaluator and under rustc. +const ISLAND_ATOMS: usize = 40; +const EVALUATOR_ISLANDS: usize = 300; +const RUSTC_ISLANDS: usize = 3_000; +/// The atoms after an island's range the root is also given. +const TAIL_ATOMS: usize = 6; +/// Seeded islands under the evaluator and under rustc. +const EVALUATOR_SEEDED: usize = 300; +const RUSTC_SEEDED: usize = 3_000; +/// The packages every parse sees. +const VISIBLE: [&str; 3] = ["lexlean.core", "lexlean.std.nat", "case.math"]; + +// ------------------------------------------------------------ the closure + +/// A lexicon package of the case's own, built from committed entries with +/// their frame, precedence, and surface changed, so that the vectors reach +/// what the committed lexicons never write: a postfix form, prefix and +/// operator candidates that disagree on their profile, a surface that is both +/// a prefix operator and a leaf, and leaves of two atoms, which are branch +/// points of the token lattice (one of them, `p !`, also parses as a postfix, +/// so that an island has two covers). +fn synthetic_files() -> Vec<(String, Vec)> { + let prefix = |id: &str, surface: &str, precedence: u8| { + format!( + "spec = \"lexlean/entry/1\"\nid = \"{id}\"\ncategory = \"prefix-function\"\nsignature = \"(pi ((explicit p (sort prop))) (sort prop))\"\nsurface_arity = 1\nframe = \"prefix\"\nprecedence = {precedence}\n\n[denotation]\nkind = \"lean\"\nmodule = \"Init\"\nname = \"Not\"\n\n[[form]]\nid = \"{id}\"\nchannel = \"math\"\nsurface = \"{surface}\"\ncanonical_source = true\nfeatures = []\n\n[render]\nmath = \"(seq (token logical-not) (slot 0))\"\n" + ) + }; + let postfix = "spec = \"lexlean/entry/1\"\nid = \"bang\"\ncategory = \"postfix-function\"\nsignature = \"(pi ((explicit p (sort prop))) (sort prop))\"\nsurface_arity = 1\nframe = \"postfix\"\nprecedence = 90\n\n[denotation]\nkind = \"lean\"\nmodule = \"Init\"\nname = \"Not\"\n\n[[form]]\nid = \"bang\"\nchannel = \"math\"\nsurface = \"!\"\ncanonical_source = true\nfeatures = []\n\n[render]\nmath = \"(seq (slot 0) (token logical-not))\"\n".to_owned(); + let infix = |id: &str, precedence: u8| { + format!( + "spec = \"lexlean/entry/1\"\nid = \"{id}\"\ncategory = \"infix-predicate\"\nsignature = \"(pi ((implicit a (sort (type u))) (explicit b (local a)) (explicit c (local a))) (sort prop))\"\nuniverses = [\"u\"]\nsurface_arity = 2\nframe = \"infix\"\nprecedence = {precedence}\nassociativity = \"none\"\n\n[denotation]\nkind = \"lean\"\nmodule = \"Init\"\nname = \"Eq\"\n\n[[form]]\nid = \"{id}\"\nchannel = \"math\"\nsurface = \"≈\"\ncanonical_source = true\nfeatures = []\n\n[render]\nmath = \"(seq (slot 0) (space) (token equals) (space) (slot 1))\"\n" + ) + }; + let atom = |id: &str, surface: &str| { + format!( + "spec = \"lexlean/entry/1\"\nid = \"{id}\"\ncategory = \"proof-constant\"\nsignature = \"(pi ((implicit a (sort (type u))) (implicit x (local a))) (app (const lexlean.core::eq) (local x) (local x)))\"\nuniverses = [\"u\"]\nsurface_arity = 0\nframe = \"atom\"\n\n[denotation]\nkind = \"lean\"\nmodule = \"Init\"\nname = \"rfl\"\n\n[[form]]\nid = \"{id}\"\nchannel = \"math\"\nsurface = \"{surface}\"\ncanonical_source = true\nfeatures = []\n\n[render]\nmath = \"(operator-name rfl)\"\n" + ) + }; + let mut files = vec![ + ( + "lexicon.toml".to_owned(), + "spec = \"lexlean/lexicon/1\"\npackage = \"case.math\"\nversion = \"1.0.0\"\nlanguage = \"1.0\"\nimports = [\"lexlean.core@1.0.0\"]\n".to_owned(), + ), + ("entries/approx-high.toml".to_owned(), infix("approx-high", 55)), + ("entries/approx-low.toml".to_owned(), infix("approx-low", 50)), + ("entries/bang.toml".to_owned(), postfix), + ("entries/both-leaf.toml".to_owned(), atom("both-leaf", "bb")), + ("entries/both-prefix.toml".to_owned(), prefix("both-prefix", "bb", 40)), + ("entries/pair-bang.toml".to_owned(), atom("pair-bang", "p !")), + ("entries/pair-leaf.toml".to_owned(), atom("pair-leaf", "p q")), + ("entries/tilde-high.toml".to_owned(), prefix("tilde-high", "tl", 45)), + ("entries/tilde-low.toml".to_owned(), prefix("tilde-low", "tl", 40)), + ]; + files.sort_by(|a, b| a.0.as_bytes().cmp(b.0.as_bytes())); + files + .into_iter() + .map(|(path, content)| (path, content.into_bytes())) + .collect() +} + +/// The built-in closure (`lexlean.core`, `lexlean.std.nat`) with the case's +/// package. +fn closure() -> &'static Closure { + static CLOSURE: OnceLock = OnceLock::new(); + CLOSURE.get_or_init(|| { + let bootstrap = lexlean::lexicon::load_bootstrap().expect("bootstrap loads"); + let ctx = LoadContext { + language: lexlean::LANGUAGE_VERSION, + forbidden_controls: &bootstrap.structural.forbidden_controls, + max_scope_depth: 1024, + }; + let mut packages: Vec<_> = bootstrap + .builtin_packages + .iter() + .map(|row| lexlean::lexicon::load_builtin_package(row, &ctx).expect("a package")) + .collect(); + packages.push( + lexlean::lexicon::package::load_package( + "case:case.math", + &synthetic_files(), + None, + &ctx, + ) + .unwrap_or_else(|diagnostics| panic!("the case's lexicon package: {diagnostics:?}")), + ); + let registry = lexlean::lexicon::load_token_registry().expect("the registry"); + Closure::build(packages, registry, bootstrap, 128) + .unwrap_or_else(|diagnostics| panic!("the case's closure: {diagnostics:?}")) + }) +} + +fn visible() -> BTreeSet { + VISIBLE + .iter() + .map(|package| (*package).to_owned()) + .collect() +} + +// ------------------------------------------------------------- the encoders + +fn frame_code(frame: Frame) -> usize { + match frame { + Frame::Atom => 0, + Frame::Call => 1, + Frame::Prefix => 2, + Frame::Postfix => 3, + Frame::Infix => 4, + _ => 5, + } +} + +fn associativity_code(associativity: Option) -> usize { + match associativity { + None => 0, + Some(Associativity::Left) => 1, + Some(Associativity::Right) => 2, + Some(Associativity::None) => 3, + } +} + +/// The forms of one run, numbered in the order the edges meet them. +#[derive(Default)] +struct Forms(Vec); + +impl Forms { + fn index(&mut self, form: &FormRef) -> usize { + if let Some(found) = self.0.iter().position(|known| known == form) { + return found; + } + self.0.push(form.clone()); + self.0.len() - 1 + } + + fn known(&self, form: &FormRef) -> usize { + self.0 + .iter() + .position(|known| known == form) + .expect("an output form is a form of the edges") + } +} + +/// The projection of the closure the root reads: the math-channel edges at +/// each atom position, each with the fields of its form the parser reads (a +/// form the closure cannot resolve is frame 5, which no rule takes, and is +/// still counted). +fn edges_value(atoms: &[Atom], forms: &mut Forms) -> Value { + let closure = closure(); + let visible = visible(); + list( + (0..atoms.len()) + .map(|position| { + list( + closure + .matches_at(atoms, position, Channel::Math, &visible) + .iter() + .map(|(form, end)| { + let (frame, precedence, associativity) = + closure.form(form).map_or((5, 0, 0), |(entry, _)| { + ( + frame_code(entry.frame), + usize::from(entry.precedence.unwrap_or(0)), + associativity_code(entry.associativity), + ) + }); + Value::Adt { + constructor: 0, + fields: vec![ + nat_val(forms.index(form)), + nat_val(*end), + nat_val(frame), + nat_val(precedence), + nat_val(associativity), + ], + } + }) + .collect(), + ) + }) + .collect(), + ) +} + +fn adt(constructor: u64, fields: Vec) -> Value { + Value::Adt { + constructor, + fields, + } +} + +fn forms_value(candidates: &[FormRef], forms: &Forms) -> Value { + list( + candidates + .iter() + .map(|form| nat_val(forms.known(form))) + .collect(), + ) +} + +fn math_value(ast: &MathAst, forms: &Forms) -> Value { + match ast { + MathAst::Numeral { + text: digits, + atoms, + } => adt(0, vec![text(digits), nat_val(atoms.0), nat_val(atoms.1)]), + MathAst::Leaf { kinds, atoms } => adt( + 1, + vec![ + list( + kinds + .iter() + .map(|kind| match kind { + LeafKind::Form(form) => adt(0, vec![nat_val(forms.known(form))]), + LeafKind::Ident(spelled) => adt(1, vec![text(spelled)]), + }) + .collect(), + ), + nat_val(atoms.0), + nat_val(atoms.1), + ], + ), + MathAst::Lexeme { qualified, atoms } => { + adt(2, vec![text(qualified), nat_val(atoms.0), nat_val(atoms.1)]) + } + MathAst::Reference { + module, + component, + atoms, + } => adt( + 3, + vec![ + text(module), + text(component), + nat_val(atoms.0), + nat_val(atoms.1), + ], + ), + MathAst::Call { + head, + args, + atoms, + open, + close, + commas, + } => adt( + 4, + vec![ + math_value(head, forms), + args.iter().rev().fold(adt(9, Vec::new()), |rest, arg| { + adt(10, vec![math_value(arg, forms), rest]) + }), + nat_val(atoms.0), + nat_val(atoms.1), + nat_val(*open), + nat_val(*close), + list(commas.iter().map(|comma| nat_val(*comma)).collect()), + ], + ), + MathAst::Prefix { + candidates, + op_atoms, + arg, + } => adt( + 5, + vec![ + forms_value(candidates, forms), + nat_val(op_atoms.0), + nat_val(op_atoms.1), + math_value(arg, forms), + ], + ), + MathAst::Postfix { + candidates, + op_atoms, + arg, + } => adt( + 6, + vec![ + forms_value(candidates, forms), + nat_val(op_atoms.0), + nat_val(op_atoms.1), + math_value(arg, forms), + ], + ), + MathAst::Infix { + candidates, + op_atoms, + lhs, + rhs, + } => adt( + 7, + vec![ + forms_value(candidates, forms), + nat_val(op_atoms.0), + nat_val(op_atoms.1), + math_value(lhs, forms), + math_value(rhs, forms), + ], + ), + MathAst::Paren { inner, atoms } => adt( + 8, + vec![math_value(inner, forms), nat_val(atoms.0), nat_val(atoms.1)], + ), + } +} + +/// The limits and the counters the budget holds before the parse. +#[derive(Clone, Copy)] +struct Limits { + edges: u64, + states: u64, + depth: u64, + states_before: u64, +} + +const COMMITTED: Limits = Limits { + edges: MAX_EDGES, + states: MAX_STATES, + depth: MAX_DEPTH, + states_before: 0, +}; + +/// The run of the root on an atom window and a range of it, and the +/// oracle's code and message when it refuses. +fn run_on( + path: &str, + atoms: &[Atom], + range: (usize, usize), + limits: Limits, +) -> (Run, Option) { + let mut forms = Forms::default(); + let edges = edges_value(atoms, &mut forms); + let mut budget = Budget::new(limits.edges, limits.states, limits.depth, u64::MAX); + for _ in 0..limits.states_before { + budget + .state() + .expect("the states before are within the limit"); + } + let (value, refusal) = match parse_math(path, atoms, range, closure(), &visible(), &mut budget) + { + Ok(covers) => ( + Value::Ok { + value: Box::new(list( + covers.iter().map(|ast| math_value(ast, &forms)).collect(), + )), + }, + None, + ), + Err(diagnostic) => ( + Value::Error { + value: Box::new(rejection_value(&diagnostic)), + }, + Some(format!( + "{} {}", + diagnostic.code.as_str(), + diagnostic.message + )), + ), + }; + ( + ( + vec![ + text(path), + list(atoms.iter().map(atom_value).collect()), + nat_val(range.0), + nat_val(range.1), + edges, + Value::Nat { + value: limits.edges.to_string(), + }, + Value::Nat { + value: limits.states.to_string(), + }, + Value::Nat { + value: limits.depth.to_string(), + }, + Value::Nat { + value: limits.states_before.to_string(), + }, + nat_val(0), + ], + serde_json::to_value(value).expect("a value is JSON"), + ), + refusal, + ) +} + +fn scanned(source: &str) -> Vec { + lexlean::source::scan::scan(PATH, source, u64::MAX) + .unwrap_or_else(|failure| panic!("{source:?}: {failure:?}")) +} + +// ------------------------------------------------------------------ vectors + +/// One named vector: an island's text, the atoms at its end left out of the +/// range (still given to the root), and the budget. +struct Vector { + label: &'static str, + source: &'static str, + trim: usize, + limits: Limits, +} + +impl Vector { + fn run(&self) -> (Run, Option) { + let atoms = scanned(self.source); + let stop = atoms.len() - self.trim; + run_on(PATH, &atoms, (0, stop), self.limits) + } +} + +const fn v(label: &'static str, source: &'static str) -> Vector { + Vector { + label, + source, + trim: 0, + limits: COMMITTED, + } +} + +const fn limited(label: &'static str, source: &'static str, limits: Limits) -> Vector { + Vector { + label, + source, + trim: 0, + limits, + } +} + +const fn trimmed(label: &'static str, source: &'static str, trim: usize) -> Vector { + Vector { + label, + source, + trim, + limits: COMMITTED, + } +} + +fn named() -> Vec { + vec![ + v("a numeral", "42"), + v("a variable", "x"), + v("a composed identifier", "x_1'"), + v("white space around", " x "), + v("white space only", " "), + v("a sum", "a + b"), + v("a left chain", "a + b + c"), + v("an equation", "a = b"), + v("a nonassociative chain", "a = b = c"), + v("a right chain", "a → b → c"), + v("a conjunction", "a ∧ b ∧ c"), + v("precedence over equality", "a + b = c"), + v("equality under a sum", "a = b + c"), + v("a negation", "¬ a"), + v("a double negation", "¬ ¬ a"), + v("a negation of an equation", "¬ a = b"), + v("a negation in a conjunction", "a ∧ ¬ b"), + v("a postfix", "a !"), + v("a double postfix", "a ! !"), + v("a postfix before a sum", "a ! + b"), + v("a postfix after a sum", "a + b !"), + v("a postfix of a call", "f(a) !"), + v("a grouping", "(a + b)"), + v("a nested grouping", "((a))"), + v("a grouping and an operator", "(a + b) = c"), + v("an unclosed grouping", "(a + b"), + v("a grouping closed by a brace", "(a}"), + v("an open brace", "{a}"), + v("a close brace", "}"), + v("a call", "f(a, b)"), + v("a call of one argument", "f(a)"), + v("a call of three arguments", "f(a, b, c)"), + v("a call of a call", "f(a)(b)"), + v("a call with nested parentheses", "f((a), g(b, c))"), + v("a call missing a comma", "f(a b)"), + v("an unclosed call", "f(a"), + v("a call of nothing", "f()"), + v("a call after a grouping", "(f)(a)"), + v("a lexeme", "\\lexeme{lexlean.core::eq}"), + v( + "a lexeme with white space", + "\\lexeme{ lexlean.core :: eq }", + ), + v("a lexeme without a brace", "\\lexeme x"), + v("an unclosed lexeme", "\\lexeme{abc"), + v("a reference", "\\reference{M::c}"), + v("a reference of two separators", "\\reference{M::c::d}"), + v("a reference without a separator", "\\reference{Mc}"), + v("a reference called", "\\reference{M::f}(a)"), + v("an operator alone", "+"), + v("an operator at the end", "a +"), + v("two leaves", "a b"), + v("a comma after the expression", "a ,"), + v("an unknown symbol after the expression", "a $"), + v("an unknown symbol first", "$"), + v("a prefix whose candidates disagree", "tl a"), + v("operators that disagree", "a ≈ b"), + v("a prefix that is also a leaf", "bb a"), + v("a leaf of two atoms", "p q"), + v("a leaf of two atoms in a sum", "p q + p"), + v("a leaf of two atoms twice", "p q = p q"), + v("a leaf of two atoms called", "p q(a)"), + v("a leaf or a postfix", "p !"), + trimmed("a sum before the range ends", "a + b c", 2), + trimmed("an identifier running past the range", "x_y", 2), + trimmed("a leaf of two atoms past the range", "p q", 2), + trimmed("an operator past the range", "a + b", 2), + limited( + "nesting beyond the limit", + "((((a))))", + Limits { + depth: 3, + ..COMMITTED + }, + ), + limited( + "negations beyond the limit", + "¬ ¬ ¬ a", + Limits { + depth: 3, + ..COMMITTED + }, + ), + limited( + "edges at the limit at a branch", + "p q", + Limits { + edges: 1, + ..COMMITTED + }, + ), + limited( + "nesting at the limit", + "((a))", + Limits { + depth: 3, + ..COMMITTED + }, + ), + limited( + "a call nested beyond the limit", + "f(g(h(a)))", + Limits { + depth: 3, + ..COMMITTED + }, + ), + limited( + "states beyond the limit", + "a + b + c", + Limits { + states: 4, + ..COMMITTED + }, + ), + limited( + "states spent before", + "a + b", + Limits { + states: 6, + states_before: 3, + ..COMMITTED + }, + ), + limited( + "edges beyond the limit", + "a + b + c", + Limits { + edges: 1, + ..COMMITTED + }, + ), + limited( + "edges beyond the limit at a branch", + "p q + p q", + Limits { + edges: 2, + ..COMMITTED + }, + ), + limited( + "states beyond the limit at a branch", + "p q", + Limits { + states: 3, + ..COMMITTED + }, + ), + ] +} + +/// The refusals a vector must reach, by code and message. +const REFUSALS: &[&str] = &[ + "LLP2001 expected a mathematical expression", + "LLP2004 unclosed grouping parenthesis", + "LLP2004 braces do not group mathematical terms", + "LLP2003 expected `{`", + "LLP2003 unclosed `{` argument", + "LLR3005 `Mc` is not a `Module::component` reference", + "LLP2002 prefix candidates disagree on their profile", + "LLP2002 this surface is both a prefix operator and a leaf", + "LLP2002 operator candidates at this surface disagree", + "LLL1004 unknown atom", + "LLP2004 expected `,` or `)` in a call argument list", + "LLP2004 a nonassociative chain requires explicit parentheses", + "LLP2004 unexpected `b` after the expression", + "LLS8002 max_scope_depth exceeded in phase parse (mathematical grouping)", + "LLS8002 max_parse_states exceeded in phase parse", + "LLS8002 max_token_lattice_edges exceeded in phase lexical resolution", +]; + +// ------------------------------------------------------------------- seeded + +const PIECES: &[&str] = &[ + "a", + "b", + "x_1", + "1", + "+", + "=", + "¬", + "→", + "∧", + "(", + ")", + ",", + "!", + "tl", + "≈", + "bb", + "p q", + "p", + "q", + "f(", + "\\lexeme{x}", + "\\reference{M::c}", + "{", + "$", +]; + +fn seeded_source(seeded: &mut Seeded) -> String { + let count = 1 + seeded.below(9); + let mut out = String::new(); + for index in 0..count { + if index > 0 && seeded.chance(80) { + out.push(' '); + } + out.push_str(seeded.pick(PIECES)); + } + out +} + +fn seeded_runs(count: usize, seed: u64) -> Vec { + let mut seeded = Seeded(seed); + let mut out = Vec::with_capacity(count); + while out.len() < count { + let source = seeded_source(&mut seeded); + let Ok(atoms) = lexlean::source::scan::scan(PATH, &source, u64::MAX) else { + continue; + }; + if atoms.is_empty() { + continue; + } + out.push(run_on(PATH, &atoms, (0, atoms.len()), COMMITTED).0); + } + out +} + +/// The islands of the committed sources: the inner range of each `\(…\)` and +/// `\[…\]` of at most [`ISLAND_ATOMS`] atoms, one source scanned at a time, +/// with [`TAIL_ATOMS`] atoms after the range. +fn island_runs(evaluator: usize, rustc: usize) -> (usize, Vec, Vec) { + let mut found = 0usize; + let mut first = Vec::new(); + let mut second = Vec::new(); + for (path, content) in super::scan::sources() { + if content.len() > 400_000 || second.len() >= rustc { + continue; + } + let Ok(atoms) = lexlean::source::scan::scan(&path, &content, u64::MAX) else { + continue; + }; + let mut index = 0; + while index < atoms.len() && second.len() < rustc { + let atom = &atoms[index]; + let closer = match (atom.class, atom.text.as_str()) { + (AtomClass::Control, "\\(") => "\\)", + (AtomClass::Control, "\\[") => "\\]", + _ => { + index += 1; + continue; + } + }; + let Some(close) = (index + 1..atoms.len().min(index + 2 + ISLAND_ATOMS)) + .find(|at| atoms[*at].class == AtomClass::Control && atoms[*at].text == closer) + else { + index += 1; + continue; + }; + if close > index + 1 { + let window = &atoms[index + 1..(close + TAIL_ATOMS).min(atoms.len())]; + let run = run_on(&path, window, (0, close - index - 1), COMMITTED).0; + found += 1; + if first.len() < evaluator && found.is_multiple_of(3) { + first.push(run.clone()); + } + second.push(run); + } + index = close + 1; + } + } + (found, first, second) +} + +// ------------------------------------------------------------------- plants + +struct Plant { + label: &'static str, + site: &'static str, + replacement: &'static str, + vector: &'static str, +} + +const PLANTS: &[Plant] = &[ + Plant { + label: "left associativity binds to the right", + site: "(let next : Nat := (if (associativity =? 2) then precedence else (precedence + 1));", + replacement: "(let next : Nat := precedence;", + vector: "a left chain", + }, + Plant { + label: "right associativity binds to the left", + site: "(let next : Nat := (if (associativity =? 2) then precedence else (precedence + 1));", + replacement: "(let next : Nat := (precedence + 1);", + vector: "a right chain", + }, + Plant { + label: "a nonassociative chain is accepted", + site: "(match last\n | Option.some held => (held =? precedence)", + replacement: "(match (ctor Option.none [Nat])\n | Option.some held => (held =? precedence)", + vector: "a nonassociative chain", + }, + Plant { + label: "a lower precedence still binds", + site: "(if (precedence (failed counted problem)\n | Option.none => (if ((project ctx maxDepth)", + replacement: "(let charge : (Bud * (Option PrimitiveScan::Rejection)) := ((project m bud), (ctor Option.none [PrimitiveScan::Rejection])); (let counted : Mach := (withBud m (first charge)); (match (second charge)\n | Option.some problem => (failed counted problem)\n | Option.none => (if ((project ctx maxDepth)", + vector: "states beyond the limit", + }, + Plant { + label: "edges are counted again at a known position", + site: "(if (natIn position (project bud memo)) then", + replacement: "(if false then", + vector: "edges at the limit at a branch", + }, + Plant { + label: "the shortest extent is not the first pass", + site: "(let upto : Nat := (match (chosenAt ctx position)\n | Option.some picked => (if (natIn picked ends) then picked else shortest)\n | Option.none => shortest);", + replacement: "(let upto : Nat := (match (chosenAt ctx position)\n | Option.some picked => (if (natIn picked ends) then picked else shortest)\n | Option.none => (match (prim index ends 1 : (Option Nat))\n | Option.some longer => longer\n | Option.none => shortest));", + vector: "a leaf or a postfix", + }, + Plant { + label: "a branch point is not recorded", + site: "(let met : (List Met) := (if (1 (project state failure)", + replacement: "| Option.some earlier => (ctor Option.some [PrimitiveScan::Rejection] problem)", + vector: "states beyond the limit at a branch", + }, + Plant { + label: "an identifier may run past the range", + site: "(if ((project composed next) <=? stop) then (ctor Option.some [Composed] composed)", + replacement: "(if true then (ctor Option.some [Composed] composed)", + vector: "an identifier running past the range", + }, + Plant { + label: "a quote does not compose", + site: "(if (same (project atom text) (lit \"_\")) then true else (same (project atom text) (lit \"'\")))", + replacement: "(same (project atom text) (lit \"_\"))", + vector: "a composed identifier", + }, + Plant { + label: "an operator may run past the range", + site: "(prim list_fold (fun [high, low, stop] (out : (List Edge)) (edge : Edge) => (prim append out (if (both ((project edge stop) <=? stop)", + replacement: "(prim list_fold (fun [high, low] (out : (List Edge)) (edge : Edge) => (prim append out (if (both true", + vector: "a leaf of two atoms past the range", + }, + Plant { + label: "the call's close is the open", + site: "(callVal head (reverseVals gathered) (position + 1) opening position (reverseNats commas))", + replacement: "(callVal head (reverseVals gathered) (position + 1) opening opening (reverseNats commas))", + vector: "a call", + }, + Plant { + label: "the commas are kept in reverse", + site: "(callVal head (reverseVals gathered) (position + 1) opening position (reverseNats commas))", + replacement: "(callVal head (reverseVals gathered) (position + 1) opening position commas)", + vector: "a call of three arguments", + }, + Plant { + label: "a call is not continued", + site: "(if (both (position eprintln!( + "BS-42: 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-42: {}: 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: Json = 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" + ); + } +} + +/// The kinds of node a cover holds. +fn shapes(ast: &MathAst, into: &mut BTreeSet<&'static str>) { + match ast { + MathAst::Numeral { .. } => { + into.insert("numeral"); + } + MathAst::Leaf { kinds, .. } => { + into.insert("leaf"); + for kind in kinds { + into.insert(match kind { + LeafKind::Form(_) => "form leaf", + LeafKind::Ident(_) => "identifier leaf", + }); + } + } + MathAst::Lexeme { .. } => { + into.insert("lexeme"); + } + MathAst::Reference { .. } => { + into.insert("reference"); + } + MathAst::Call { head, args, .. } => { + into.insert("call"); + shapes(head, into); + for arg in args { + shapes(arg, into); + } + } + MathAst::Prefix { arg, .. } => { + into.insert("prefix"); + shapes(arg, into); + } + MathAst::Postfix { arg, .. } => { + into.insert("postfix"); + shapes(arg, into); + } + MathAst::Infix { lhs, rhs, .. } => { + into.insert("infix"); + shapes(lhs, into); + shapes(rhs, into); + } + MathAst::Paren { inner, .. } => { + into.insert("paren"); + shapes(inner, into); + } + } +} + +#[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("GrammarMath"), + "GrammarMath is a linked module of the project" + ); + eligibility_of(&project, "GrammarMath", 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 runs: Vec = Vec::new(); + let mut messages: Vec = Vec::new(); + let mut nodes = BTreeSet::new(); + let mut several = false; + for vector in &vectors { + let atoms = scanned(vector.source); + let stop = atoms.len() - vector.trim; + let mut budget = Budget::new( + vector.limits.edges, + vector.limits.states, + vector.limits.depth, + u64::MAX, + ); + for _ in 0..vector.limits.states_before { + budget.state().expect("within the limit"); + } + if let Ok(covers) = parse_math(PATH, &atoms, (0, stop), closure(), &visible(), &mut budget) + { + several |= covers.len() > 1; + for cover in &covers { + shapes(cover, &mut nodes); + } + } + let (run, refusal) = vector.run(); + messages.extend(refusal); + runs.push(run); + } + for refusal in REFUSALS { + assert!( + messages.iter().any(|message| message.starts_with(refusal)), + "no vector reaches the refusal `{refusal}`" + ); + } + for shape in [ + "numeral", + "leaf", + "form leaf", + "identifier leaf", + "lexeme", + "reference", + "call", + "prefix", + "postfix", + "infix", + "paren", + ] { + assert!(nodes.contains(shape), "no vector's cover holds a {shape}"); + } + assert!(several, "a vector has more than one cover"); + + let (islands, evaluator_islands, rustc_islands) = island_runs(EVALUATOR_ISLANDS, RUSTC_ISLANDS); + assert!(islands >= 100, "the committed islands are {islands}"); + let normative = support::normative_host(); + let (evaluator_seeded, rustc_seeded) = if normative { + ( + seeded_runs(EVALUATOR_SEEDED, 0x0B55_0042), + seeded_runs(RUSTC_SEEDED, 0x0B55_1042), + ) + } else { + (Vec::new(), Vec::new()) + }; + eprintln!( + "BS-42: {} vectors, {} committed islands ({} and {} run), {} and {} seeded islands ({:?})", + vectors.len(), + islands, + evaluator_islands.len(), + rustc_islands.len(), + evaluator_seeded.len(), + rustc_seeded.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(); + if normative { + let mut corpus: Vec = runs.clone(); + corpus.extend(evaluator_islands); + corpus.extend(evaluator_seeded); + let disagreements = super::evaluate(&lowered[ROOTS[0].0], &corpus, 5); + assert!( + disagreements.is_empty(), + "parseMath and the Rust oracle disagree: {disagreements:?}" + ); + eprintln!( + "BS-42: parseMath: the evaluator agrees on {} runs ({:?})", + corpus.len(), + started.elapsed() + ); + plants(&vectors); + } else { + eprintln!("BS-42: platform-bound host: the evaluator, the seeded runs, and the planted defects run on the normative host"); + } + + let mut compiled = runs; + compiled.extend(rustc_islands); + compiled.extend(rustc_seeded); + let total = rustc_differential( + "BS-42", + &[(ROOTS[0].0, &lowered[ROOTS[0].0], compiled.as_slice())], + "oracle", + ); + eprintln!("BS-42: {total} rustc runs agree ({:?})", started.elapsed()); + + if !support::lean_backed("BS-42") { + return; + } + export(&lowered); + eprintln!("BS-42: passed in {:?}", started.elapsed()); +} diff --git a/crates/conformance/src/cases/bootstrap/grammar_proof.rs b/crates/conformance/src/cases/bootstrap/grammar_proof.rs new file mode 100644 index 0000000000..fbb3304a67 --- /dev/null +++ b/crates/conformance/src/cases/bootstrap/grammar_proof.rs @@ -0,0 +1,1025 @@ +//! `BS-35` (SPEC.md §17.23, §15.5, §16.2): the text-channel tokens and the +//! simple proof sentences as language-1.2 source, `LexLeanBoot.GrammarProof`, +//! held to the byte to `lexlean::grammar::chart::text_tokens` and +//! `lexlean::grammar::proof::parse_proof_sentence`. +//! +//! `textTokens` drops white space and folds each mathematical island of an +//! atom range into one token, refusing a dollar delimiter, a nested or unclosed +//! island, and a closing control that closes none; `parseProofSentence` reads +//! the tokens of one sentence as one of the registered simple sentences, with +//! the keywords that cover it, or names the forbidden proof form it holds. Both +//! read the atoms `scanSource` of `BS-03` returns, so the host composes them. +//! +//! Each planted defect names the one vector that catches it. + +use std::collections::{BTreeMap, BTreeSet}; + +use lexlean::calculus::Value; +use lexlean::grammar::chart::{text_tokens, TextToken}; +use lexlean::grammar::proof::{parse_proof_sentence, ProofSentence, SentenceAstKind}; +use lexlean::grammar::proposition::TextParser; +use lexlean::lexicon::package::LoadContext; +use lexlean::lexicon::resolve::Closure; +use lexlean::source::atom::{Atom, AtomClass}; +use serde_json::Value as Json; + +use super::rust_validate::{eligibility_of, rustc_differential}; +use super::scan::{atom_value, rejection_value}; +use super::{bool_val, list, nat_val, text, Run, Seeded}; +use crate::production_export as harness; +use crate::support; + +const MODULE: &str = "LexLeanBoot.GrammarProof"; +const SOURCE: &str = "src/GrammarProof.lex.tex"; +const ROOTS: [(&str, &[&str]); 2] = [ + ("textTokens", &["allocation", "overflow"]), + ("parseProofSentence", &["allocation", "overflow"]), +]; + +/// The most a part of a certificate of a root may be, as the row states. +const CERTIFICATE_BUDGET: u64 = 3_145_728; +/// The atoms a range of a committed source holds at most, and the atoms after +/// it the root is also given, which the composition of a tactic word reads. +const RANGE_ATOMS: usize = 48; +const TAIL_ATOMS: usize = 8; +/// The committed sources whose ranges are drawn: those of at most this many +/// bytes. +const SOURCE_BYTES: usize = 400_000; +/// Ranges of committed sources under the evaluator and under rustc. +const EVALUATOR_RANGES: usize = 600; +const RUSTC_RANGES: usize = 4_000; +/// Seeded sentences under the evaluator and under rustc. +const EVALUATOR_SEEDED: usize = 400; +const RUSTC_SEEDED: usize = 3_000; +/// The path every vector is read under. +const PATH: &str = "src/Proof.lex.tex"; + +// ------------------------------------------------------------- the encoders + +fn token_value(token: &TextToken) -> Value { + match token { + TextToken::Atom(index) => Value::Adt { + constructor: 0, + fields: vec![nat_val(*index)], + }, + TextToken::Island { + open, + inner_start, + inner_end, + close, + display, + } => Value::Adt { + constructor: 1, + fields: vec![ + nat_val(*open), + nat_val(*inner_start), + nat_val(*inner_end), + nat_val(*close), + bool_val(*display), + ], + }, + } +} + +fn tokens_value(tokens: &[TextToken]) -> Value { + list(tokens.iter().map(token_value).collect()) +} + +fn kind_value(kind: &SentenceAstKind) -> Value { + let (constructor, fields) = match kind { + SentenceAstKind::Assume { islands } => (0, vec![tokens_value(islands)]), + SentenceAstKind::Apply { term } => (1, vec![token_value(term)]), + SentenceAstKind::CloseWith { term } => (2, vec![token_value(term)]), + SentenceAstKind::CloseByReflexivity => (3, Vec::new()), + SentenceAstKind::Witness { term } => (4, vec![token_value(term)]), + SentenceAstKind::SelectLeft => (5, Vec::new()), + SentenceAstKind::SelectRight => (6, Vec::new()), + }; + Value::Adt { + constructor, + fields, + } +} + +fn sentence_value(sentence: &ProofSentence) -> Value { + Value::Adt { + constructor: 0, + fields: vec![ + kind_value(&sentence.kind), + list( + sentence + .keywords + .iter() + .map(|keyword| Value::Adt { + constructor: 0, + fields: vec![nat_val(keyword.atom), text(keyword.entry)], + }) + .collect(), + ), + ], + } +} + +fn atoms_value(atoms: &[Atom]) -> Value { + list(atoms.iter().map(atom_value).collect()) +} + +/// The built-in closure (`lexlean.core`, `lexlean.std.nat`), which the +/// sentence parser is given and does not read. +fn closure() -> &'static Closure { + static CLOSURE: std::sync::OnceLock = std::sync::OnceLock::new(); + CLOSURE.get_or_init(|| { + let bootstrap = lexlean::lexicon::load_bootstrap().expect("bootstrap loads"); + let ctx = LoadContext { + language: lexlean::LANGUAGE_VERSION, + forbidden_controls: &bootstrap.structural.forbidden_controls, + max_scope_depth: 1024, + }; + let packages = bootstrap + .builtin_packages + .iter() + .map(|row| lexlean::lexicon::load_builtin_package(row, &ctx).expect("a package")) + .collect(); + let registry = lexlean::lexicon::load_token_registry().expect("the registry"); + Closure::build(packages, registry, bootstrap, 128).expect("the closure") + }) +} + +/// What the roots say of an atom range: the run of `textTokens`, its +/// tokens when it accepts, and the oracle's message when it refuses. +fn tokens_run( + path: &str, + atoms: &[Atom], + start: usize, + end: usize, +) -> (Run, Option>, Option) { + let (value, tokens, refusal) = match text_tokens(path, atoms, start, end) { + Ok(tokens) => ( + Value::Ok { + value: Box::new(tokens_value(&tokens)), + }, + Some(tokens), + None, + ), + Err(diagnostic) => ( + Value::Error { + value: Box::new(rejection_value(&diagnostic)), + }, + None, + Some(diagnostic.message.clone()), + ), + }; + ( + ( + vec![text(path), atoms_value(atoms), nat_val(start), nat_val(end)], + serde_json::to_value(value).expect("a value is JSON"), + ), + tokens, + refusal, + ) +} + +fn sentence_run(path: &str, atoms: &[Atom], tokens: &[TextToken]) -> (Run, Option) { + let visible = BTreeSet::new(); + let parser = TextParser { + path, + atoms, + tokens, + closure: closure(), + visible: &visible, + sentence_initial: true, + }; + let (value, refusal) = match parse_proof_sentence(&parser) { + Ok(sentence) => ( + Value::Ok { + value: Box::new(sentence_value(&sentence)), + }, + None, + ), + Err(diagnostic) => ( + Value::Error { + value: Box::new(rejection_value(&diagnostic)), + }, + Some(diagnostic.message.clone()), + ), + }; + ( + ( + vec![text(path), atoms_value(atoms), tokens_value(tokens)], + serde_json::to_value(value).expect("a value is JSON"), + ), + refusal, + ) +} + +fn scanned(source: &str) -> Vec { + lexlean::source::scan::scan(PATH, source, u64::MAX) + .unwrap_or_else(|failure| panic!("{source:?}: {failure:?}")) +} + +// ------------------------------------------------------------------ vectors + +/// One named vector: a text whose atoms are tokenized whole, and, when they +/// tokenize, read as a sentence. +struct Vector { + label: String, + source: String, + /// The first atom of the range; the range runs to the last atom. + start: usize, + /// The atoms without their white space, which the scanner never returns: + /// neighbours that do not touch hold the composition to bytes. + gapped: bool, +} + +/// The two runs of a vector: the tokens, and the sentence when there are +/// tokens; with the oracle's messages. +fn vector_runs(source: &str, start: usize, gapped: bool) -> (Run, Option, Vec) { + let atoms: Vec = scanned(source) + .into_iter() + .filter(|atom| !gapped || atom.class != AtomClass::Whitespace) + .collect(); + let (run, tokens, refusal) = tokens_run(PATH, &atoms, start, atoms.len()); + let mut messages: Vec = refusal.into_iter().collect(); + let sentence = tokens.map(|tokens| { + let (run, refusal) = sentence_run(PATH, &atoms, &tokens); + messages.extend(refusal); + run + }); + (run, sentence, messages) +} + +/// The forbidden words of §16.12, each of which a vector holds. +const FORBIDDEN: &[&str] = &[ + "aesop", + "admit", + "cases", + "constructor", + "induction", + "all_goals", + "any_goals", + "apply?", + "assumption", + "case", + "decide", + "exact", + "exact?", + "first", + "intro", + "linarith", + "native_decide", + "next", + "omega", + "refine", + "repeat", + "rfl", + "rw", + "rwa", + "simp", + "simp_all", + "simpa", + "sorry", + "tauto", + "trivial", + "try", +]; + +#[allow(clippy::too_many_lines)] +fn named() -> Vec { + let mut out: Vec = Vec::new(); + let mut add = |label: &str, source: &str| { + out.push(Vector { + label: label.to_owned(), + source: source.to_owned(), + start: 0, + gapped: false, + }); + }; + // The tokens. + add("an empty range", ""); + add("white space only", " \n "); + add("words and symbols", "Close the goal, by x."); + add("an inline island", "Apply \\(f x\\)"); + add("a display island", "Apply \\[f x\\]"); + add("two islands", "\\(a\\) and \\[b\\]"); + add("a dollar delimiter", "Apply $x$"); + add("a dollar after an island", "\\(x\\) $"); + add("a nested inline island", "\\(a \\(b\\) c\\)"); + add("a display island in an inline one", "\\(a \\[b\\]\\)"); + add("an unclosed inline island", "Apply \\(x"); + add("an unclosed display island", "Apply \\[x"); + add("an inline island closed as display", "\\(x\\]"); + add("a display island closed as inline", "\\[x\\) y"); + add("a stray inline close", "x \\) y"); + add("a stray display close", "x \\] y"); + add("an empty island", "\\(\\)"); + add("an island at the end", "Use \\(w\\)"); + // The sentences. + add("assume one", "Assume \\(x\\)"); + add("assume two", "Assume \\(x\\), \\(y\\)"); + add("assume three", "Assume \\(x\\), \\(y\\), \\[z\\]"); + add("assume without an island", "Assume x"); + add("assume with a trailing comma", "Assume \\(x\\),"); + add("assume with a comma and a word", "Assume \\(x\\), y"); + add("assume with a word after", "Assume \\(x\\) y"); + add("assume with two commas", "Assume \\(x\\),, \\(y\\)"); + add("assume alone", "Assume"); + add("apply", "Apply \\(h\\)"); + add("apply with a word after", "Apply \\(h\\) now"); + add("apply without an island", "Apply h"); + add("close with", "Close the goal with \\(h\\)"); + add("close by reflexivity", "Close the goal by reflexivity"); + add("close with nothing after", "Close the goal with"); + add("close with a word after", "Close the goal with \\(h\\) x"); + add( + "close by reflexivity and more", + "Close the goal by reflexivity x", + ); + add("close by something else", "Close the goal by induction"); + add("close without the goal", "Close the aim with \\(h\\)"); + add("close without the", "Close goal with \\(h\\)"); + add("close and nothing", "Close the goal"); + add("close with a word", "Close the goal with h"); + add("use as the witness", "Use \\(w\\) as the witness"); + add("use without as", "Use \\(w\\) for the witness"); + add("use without the witness", "Use \\(w\\) as the"); + add("use with more", "Use \\(w\\) as the witness x"); + add("use without an island", "Use w as the witness"); + add("select left", "Select the left alternative"); + add("select right", "Select the right alternative"); + add("select neither", "Select the middle alternative"); + add("select without alternative", "Select the left"); + add("select with more", "Select the right alternative x"); + add("select without the", "Select left alternative"); + add("a lower-case verb", "apply \\(h\\)"); + add("a verb as a symbol", "\\(h\\) Apply"); + add("an island first", "\\(h\\)"); + add("a numeral first", "3 Apply \\(h\\)"); + add("a tactic in the middle", "Close the goal by simp"); + add("a tactic at the end", "Apply \\(h\\) omega"); + add("a composed tactic", "Close the goal with exact?"); + add("a composed underscore tactic", "native_decide"); + add("an underscore word first", "foo_bar the goal"); + add("an underscore word later", "Close the foo_bar"); + add("an underscore word after a space", "Close foo _bar"); + add("a combinator", "Apply \\(h\\) <;> simp"); + add("a combinator alone", "<;>"); + add("a combinator split", "< ;> x"); + add("a focusing dot", "· Apply \\(h\\)"); + add("a tactic broken by a comma", "simp,all"); + add("a tactic broken by a space", "exact ?"); + add("a tactic word joined to a numeral", "rw2 x"); + add("a tactic word in an island", "Apply \\(simp\\)"); + add("a tactic word after a period", "Apply \\(h\\). simp"); + add("a word with an apostrophe", "Apply' \\(h\\)"); + add("use as the something", "Use \\(w\\) as the proof"); + for word in FORBIDDEN { + let source = format!("Close the goal by {word}"); + out.push(Vector { + label: format!("the forbidden word {word}"), + source, + start: 0, + gapped: false, + }); + } + for (label, source, start) in [ + ("a range after a dollar", "$ Apply \\(h\\)", 1), + ("a range inside an island", "\\(a b\\)", 1), + ("a range at its last atom", "x y", 2), + ("a range past its last atom", "x", 1), + ] { + out.push(Vector { + label: label.to_owned(), + source: source.to_owned(), + start, + gapped: false, + }); + } + for (label, source) in [ + ("a tactic split by a dropped space", "exact ?"), + ( + "an island and words without white space", + "Use \\(w\\) as the witness", + ), + ] { + out.push(Vector { + label: label.to_owned(), + source: source.to_owned(), + start: 0, + gapped: true, + }); + } + out +} + +/// The refusals a vector must reach, each by its message. +const REFUSALS: &[&str] = &[ + "dollar math delimiters are forbidden", + "mathematical islands do not nest", + "unclosed mathematical island; expected `\\)`", + "unclosed mathematical island; expected `\\]`", + "`\\)` closes no mathematical island", + "`\\]` closes no mathematical island", + "is a forbidden proof form", + "not a registered proof sentence", +]; + +// ------------------------------------------------------------------- seeded + +const PIECES: &[&str] = &[ + "Assume", + "Apply", + "Close", + "Use", + "Select", + "the", + "goal", + "with", + "by", + "reflexivity", + "as", + "witness", + "left", + "right", + "alternative", + ",", + ",", + "\\(x\\)", + "\\[y\\]", + "\\(", + "\\)", + "\\[", + "\\]", + "$", + "simp", + "exact", + "?", + "_", + "native", + "<;>", + "<", + ";", + ">", + "·", + "x", + "3", + ".", + "(", + ")", + "'", + "rfl", + "first", + "foo_bar", +]; + +fn sentence(seeded: &mut Seeded) -> String { + let count = 1 + seeded.below(7); + let mut out = String::new(); + for index in 0..count { + if index > 0 && seeded.chance(70) { + out.push(' '); + } + out.push_str(seeded.pick(PIECES)); + } + out +} + +/// A registered sentence with one edit, or a sentence drawn from the pieces. +fn seeded_source(seeded: &mut Seeded, bases: &[String]) -> String { + if seeded.chance(50) { + let base = seeded.pick(bases).clone(); + let mut words: Vec = base.split(' ').map(str::to_owned).collect(); + let at = seeded.below(words.len() + 1); + match seeded.below(3) { + 0 if at < words.len() => { + words.remove(at); + } + 1 => words.insert(at, (*seeded.pick(PIECES)).to_owned()), + _ if at < words.len() => words[at] = (*seeded.pick(PIECES)).to_owned(), + _ => words.push((*seeded.pick(PIECES)).to_owned()), + } + words.join(" ") + } else { + sentence(seeded) + } +} + +fn seeded_runs(count: usize, bases: &[String], seed: u64) -> (Vec, Vec) { + let mut seeded = Seeded(seed); + let mut tokens = Vec::with_capacity(count); + let mut sentences = Vec::with_capacity(count); + while tokens.len() < count { + let source = seeded_source(&mut seeded, bases); + let (run, sentence, _) = vector_runs(&source, 0, false); + tokens.push(run); + sentences.extend(sentence); + } + (tokens, sentences) +} + +/// The runs of `textTokens` and of `parseProofSentence` on drawn ranges. +type RangeRuns = (Vec, Vec); + +/// The ranges of one scanned source: up to [`RANGE_ATOMS`] atoms from an atom +/// where a range may start (a period ends the one before it, as the elaborator +/// cuts sentences), with [`TAIL_ATOMS`] atoms after it. +fn ranges_of(seeded: &mut Seeded, path: &str, atoms: &[Atom], count: usize, out: &mut RangeRuns) { + if atoms.is_empty() { + return; + } + for _ in 0..count { + let from = seeded.below(atoms.len()); + let start = (from..atoms.len()) + .find(|index| atoms[*index].text == "." || *index == atoms.len() - 1) + .map_or(from, |index| (index + 1).min(atoms.len() - 1)); + let span = 1 + seeded.below(RANGE_ATOMS); + let end = (start + span).min(atoms.len()); + let end = (start..end) + .find(|index| atoms[*index].text == ".") + .unwrap_or(end); + let window = &atoms[start..(end + TAIL_ATOMS).min(atoms.len())]; + let (run, found, _) = tokens_run(path, window, 0, end - start); + out.0.push(run); + if let Some(found) = found { + out.1.push(sentence_run(path, window, &found).0); + } + } +} + +/// Seeded atom ranges of every committed source of at most [`SOURCE_BYTES`] +/// bytes, each source scanned once and dropped, the same number from each, +/// for the evaluator and for rustc: the scanned atoms of the whole checkout +/// do not fit in memory at once. +fn source_runs() -> (usize, RangeRuns, RangeRuns) { + let sources: Vec<(String, String)> = super::scan::sources() + .into_iter() + .filter(|(_, content)| content.len() <= SOURCE_BYTES) + .collect(); + let mut evaluator = (Vec::new(), Vec::new()); + let mut compiled = (Vec::new(), Vec::new()); + let mut evaluator_seeded = Seeded(0x0B55_0035); + let mut rustc_seeded = Seeded(0x0B55_1035); + let evaluator_each = EVALUATOR_RANGES.div_ceil(sources.len().max(1)); + let rustc_each = RUSTC_RANGES.div_ceil(sources.len().max(1)); + for (path, content) in &sources { + let Ok(atoms) = lexlean::source::scan::scan(path, content, u64::MAX) else { + continue; + }; + if evaluator.0.len() < EVALUATOR_RANGES { + ranges_of( + &mut evaluator_seeded, + path, + &atoms, + evaluator_each, + &mut evaluator, + ); + } + if compiled.0.len() < RUSTC_RANGES { + ranges_of(&mut rustc_seeded, path, &atoms, rustc_each, &mut compiled); + } + } + (sources.len(), evaluator, compiled) +} + +// ------------------------------------------------------------------- plants + +struct Plant { + label: &'static str, + root: usize, + site: &'static str, + replacement: &'static str, + vector: &'static str, +} + +const PLANTS: &[Plant] = &[ + Plant { + label: "white space is a token", + root: 0, + site: "(if (code =? 6) then", + replacement: "(if false then", + vector: "words and symbols", + }, + Plant { + label: "a dollar is a word", + root: 0, + site: "(lit \"$\")", + replacement: "(lit \"$$\")", + vector: "a dollar delimiter", + }, + Plant { + label: "a display island is inline", + root: 0, + site: "(let shown : Bool := (isControl atom (lit \"\\\\[\"));", + replacement: "(let shown : Bool := false;", + vector: "a display island", + }, + Plant { + label: "islands nest", + root: 0, + site: "(if (opens atom) then (ctor Option.none [Nat]) else (ctor Option.some [Nat] (position + 1)))", + replacement: "(ctor Option.some [Nat] (position + 1))", + vector: "a nested inline island", + }, + Plant { + label: "the scan resumes at the close", + root: 0, + site: "(pushed scan (reached + 1) (ctor TextToken.TokenIsland", + replacement: "(pushed scan reached (ctor TextToken.TokenIsland", + vector: "two islands", + }, + Plant { + label: "the inner range begins at the open", + root: 0, + site: "(ctor TextToken.TokenIsland position (position + 1) reached reached shown)", + replacement: "(ctor TextToken.TokenIsland position position reached reached shown)", + vector: "an inline island", + }, + Plant { + label: "an unclosed island names its opener", + root: 0, + site: "(lit \"unclosed mathematical island; expected `\") closer)", + replacement: "(lit \"unclosed mathematical island; expected `\") (project atom text))", + vector: "an unclosed inline island", + }, + Plant { + label: "a stray display close is a word", + root: 0, + site: "(if (if (isControl atom (lit \"\\\\)\")) then true else (isControl atom (lit \"\\\\]\"))) then", + replacement: "(if (isControl atom (lit \"\\\\)\")) then", + vector: "a stray display close", + }, + Plant { + label: "the range starts at its first atom", + root: 0, + site: "(record Scan {cursor := start, tokens := (nil TextToken)", + replacement: "(record Scan {cursor := 0, tokens := (nil TextToken)", + vector: "a range after a dollar", + }, + Plant { + label: "assume needs no island", + root: 1, + site: "(if (islandIs tokens 1) then (let final : Commas", + replacement: "(if true then (let final : Commas", + vector: "assume without an island", + }, + Plant { + label: "assume keeps the first island only", + root: 1, + site: "islands := (cons (tokenOr tokens (position + 1)) (project state islands))", + replacement: "islands := (project state islands)", + vector: "assume two", + }, + Plant { + label: "the islands of assume in reverse", + root: 1, + site: "(ctor SentenceKind.Assume (reverseTokens (project final islands)))", + replacement: "(ctor SentenceKind.Assume (project final islands))", + vector: "assume three", + }, + Plant { + label: "apply takes what follows", + root: 1, + site: "(if (both (islandIs tokens 1) (count =? 2)) then", + replacement: "(if (islandIs tokens 1) then", + vector: "apply with a word after", + }, + Plant { + label: "close with takes what follows", + root: 1, + site: "(if (both (islandIs tokens 4) (count =? 5)) then (accepted (ctor SentenceKind.CloseWith", + replacement: "(if (islandIs tokens 4) then (accepted (ctor SentenceKind.CloseWith", + vector: "close with a word after", + }, + Plant { + label: "close needs no goal", + root: 1, + site: "(if (both (wordIs atoms tokens 1 (lit \"the\")) (wordIs atoms tokens 2 (lit \"goal\"))) then (let opening", + replacement: "(if (wordIs atoms tokens 1 (lit \"the\")) then (let opening", + vector: "close without the goal", + }, + Plant { + label: "the witness word is not read", + root: 1, + site: "(both (wordIs atoms tokens 4 (lit \"witness\")) (count =? 5))", + replacement: "(count =? 5)", + vector: "use as the something", + }, + Plant { + label: "select right is select left", + root: 1, + site: "(if left then (ctor SentenceKind.SelectLeft) else (ctor SentenceKind.SelectRight))", + replacement: "(ctor SentenceKind.SelectLeft)", + vector: "select right", + }, + Plant { + label: "the keyword of apply", + root: 1, + site: "(lit \"apply-verb\")", + replacement: "(lit \"apply\")", + vector: "apply", + }, + Plant { + label: "simp is not forbidden", + root: 1, + site: "(lit \"simp\")", + replacement: "(lit \"simq\")", + vector: "the forbidden word simp", + }, + Plant { + label: "atoms that do not touch compose", + root: 1, + site: "(both ((project atom byteStart) =? (project state reach))", + replacement: "(both true", + vector: "a tactic split by a dropped space", + }, + Plant { + label: "no atoms compose", + root: 1, + site: "(project (first (prim iterate_until (fun [atoms] (state : Compose) => (composeStep atoms state)) ((prim length atoms : Nat) + 1)", + replacement: "(project (first (prim iterate_until (fun [atoms] (state : Compose) => (composeStep atoms state)) 0", + vector: "a composed tactic", + }, + Plant { + label: "a comma joins a composition", + root: 1, + site: "(lit \",\") (cons (lit \".\")", + replacement: "(lit \"!\") (cons (lit \".\")", + vector: "a tactic broken by a comma", + }, + Plant { + label: "an underscore anywhere is a tactic", + root: 1, + site: "(both (position =? 0) (match (LexLeanStd.Text::find composed (lit \"_\"))", + replacement: "(both true (match (LexLeanStd.Text::find composed (lit \"_\"))", + vector: "an underscore word later", + }, + Plant { + label: "an empty sentence has a place", + root: 1, + site: "columnEnd := 1})), help := (ctor Option.none [Bytes])}", + replacement: "columnEnd := 2})), help := (ctor Option.none [Bytes])}", + vector: "an empty range", + }, + Plant { + label: "the refusal is at the last token", + root: 1, + site: "(refusal path (lit \"LLF5005\") message (atomOr atoms (firstAtom token)))", + replacement: "(refusal path (lit \"LLF5005\") message (atomOr atoms ((firstAtom token) + 1)))", + vector: "a lower-case verb", + }, +]; + +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)); + let (tokens, sentence, _) = vector_runs(&vector.source, vector.start, vector.gapped); + let run = if plant.root == 0 { + tokens + } else { + sentence.unwrap_or_else(|| panic!("{}: the vector has tokens", plant.label)) + }; + let planted = support::P::bootstrap(); + let source = planted.read_raw(SOURCE); + assert_eq!( + source.matches(plant.site).count(), + 1, + "{}: the plant site occurs once", + plant.label + ); + planted.write(SOURCE, &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, &[run], 1).first() { + Some(first) => eprintln!( + "BS-35: 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-35: {}: 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: Json = 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" + ); + } +} + +#[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("GrammarProof"), + "GrammarProof is a linked module of the project" + ); + eligibility_of(&project, "GrammarProof", MODULE, &ROOTS); + + let vectors = named(); + let labels: BTreeSet<&str> = vectors.iter().map(|vector| vector.label.as_str()).collect(); + assert_eq!(labels.len(), vectors.len(), "each vector has its own label"); + let mut messages = Vec::new(); + let mut named_runs: [Vec; 2] = [Vec::new(), Vec::new()]; + let mut kinds = BTreeSet::new(); + for vector in &vectors { + let (tokens, sentence, refusals) = vector_runs(&vector.source, vector.start, vector.gapped); + named_runs[0].push(tokens); + named_runs[1].extend(sentence); + messages.extend(refusals); + let atoms: Vec = scanned(&vector.source) + .into_iter() + .filter(|atom| !vector.gapped || atom.class != AtomClass::Whitespace) + .collect(); + if let Ok(tokens) = text_tokens(PATH, &atoms, vector.start, atoms.len()) { + let visible = BTreeSet::new(); + let parser = TextParser { + path: PATH, + atoms: &atoms, + tokens: &tokens, + closure: closure(), + visible: &visible, + sentence_initial: true, + }; + if let Ok(sentence) = parse_proof_sentence(&parser) { + kinds.insert(format!("{:?}", std::mem::discriminant(&sentence.kind))); + } + } + } + assert_eq!(kinds.len(), 7, "a vector reads as each registered sentence"); + for refusal in REFUSALS { + assert!( + messages.iter().any(|message| message.contains(refusal)), + "no vector reaches the refusal `{refusal}`" + ); + } + for word in FORBIDDEN { + assert!( + messages + .iter() + .any(|message| message.starts_with(&format!("`{word}` is a forbidden"))), + "no vector names the forbidden word `{word}`" + ); + } + let bases: Vec = vectors + .iter() + .filter(|vector| { + matches!( + vector.label.as_str(), + "assume two" + | "apply" + | "close with" + | "close by reflexivity" + | "use as the witness" + | "select left" + | "select right" + ) + }) + .map(|vector| vector.source.clone()) + .collect(); + assert_eq!(bases.len(), 7, "a base of each registered sentence"); + + let (source_count, evaluator_ranges, rustc_ranges) = source_runs(); + assert!( + source_count >= 100, + "the committed sources are {source_count}" + ); + let normative = support::normative_host(); + let (evaluator_seeded, rustc_seeded) = if normative { + ( + seeded_runs(EVALUATOR_SEEDED, &bases, 0x0B55_2035), + seeded_runs(RUSTC_SEEDED, &bases, 0x0B55_3035), + ) + } else { + ((Vec::new(), Vec::new()), (Vec::new(), Vec::new())) + }; + eprintln!( + "BS-35: {} vectors ({} tokenized, {} read as sentences), {} committed sources, {} and {} ranges, {} and {} seeded sentences ({:?})", + vectors.len(), + named_runs[0].len(), + named_runs[1].len(), + source_count, + evaluator_ranges.0.len(), + rustc_ranges.0.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(); + if normative { + for (index, (root, _)) in ROOTS.iter().enumerate() { + let mut corpus: Vec = named_runs[index].clone(); + if index == 0 { + corpus.extend(evaluator_ranges.0.iter().cloned()); + corpus.extend(evaluator_seeded.0.iter().cloned()); + } else { + corpus.extend(evaluator_ranges.1.iter().cloned()); + corpus.extend(evaluator_seeded.1.iter().cloned()); + } + let disagreements = super::evaluate(&lowered[root], &corpus, 5); + assert!( + disagreements.is_empty(), + "{root} and the Rust oracle disagree: {disagreements:?}" + ); + eprintln!( + "BS-35: {root}: the evaluator agrees on {} runs ({:?})", + corpus.len(), + started.elapsed() + ); + } + plants(&vectors); + } else { + eprintln!("BS-35: platform-bound host: the evaluator, the seeded runs, and the planted defects run on the normative host"); + } + + let mut compiled: [Vec; 2] = [named_runs[0].clone(), named_runs[1].clone()]; + compiled[0].extend(rustc_ranges.0); + compiled[1].extend(rustc_ranges.1); + compiled[0].extend(rustc_seeded.0); + compiled[1].extend(rustc_seeded.1); + let total = rustc_differential( + "BS-35", + &[ + (ROOTS[0].0, &lowered[ROOTS[0].0], compiled[0].as_slice()), + (ROOTS[1].0, &lowered[ROOTS[1].0], compiled[1].as_slice()), + ], + "oracle", + ); + eprintln!("BS-35: {total} rustc runs agree ({:?})", started.elapsed()); + + if !support::lean_backed("BS-35") { + return; + } + export(&lowered); + eprintln!("BS-35: passed in {:?}", started.elapsed()); +} diff --git a/crates/conformance/src/cases/bootstrap/proposition_terms.rs b/crates/conformance/src/cases/bootstrap/proposition_terms.rs new file mode 100644 index 0000000000..b6f00068be --- /dev/null +++ b/crates/conformance/src/cases/bootstrap/proposition_terms.rs @@ -0,0 +1,1208 @@ +//! `BS-43` (SPEC.md §17.23, §15.3, §15.4, §12.2): the grammar keywords, the +//! unknown-atom diagnosis, the binders, and the term phrases of the text +//! channel as language-1.2 source, `LexLeanBoot.PropositionTerms`, held to the +//! byte to `lexlean::grammar::proposition::keyword_entry` and the +//! `TextParser` methods `unknown_atom_diagnostic`, `binder`, and `term_phrase`. +//! +//! The roots read the tokens of `BS-35` (`GrammarProof`) and the atoms of +//! `BS-03`; the glossary closure is the lexicon's, so the host gives them its +//! projection: the text-channel edges at each atom position, each with its +//! form's category and whether the form is inflected (plural or +//! sentence-case), and the limits and counters of the parse budget. The term +//! phrase, whose arguments are term phrases, is an explicit stack of frames +//! that gathers every alternative in the oracle's order. +//! +//! Each planted defect names the one vector that catches it. + +use std::collections::{BTreeMap, BTreeSet}; +use std::sync::OnceLock; + +use lexlean::calculus::Value; +use lexlean::grammar::chart::{text_tokens, Budget, TextToken}; +use lexlean::grammar::proposition::{ + keyword_entry, ArticleRule, BinderAst, Keyword, TermPhraseAst, TextParser, TypePhraseAst, +}; +use lexlean::lexicon::entry::{Category, Channel}; +use lexlean::lexicon::package::LoadContext; +use lexlean::lexicon::resolve::{Closure, FormRef}; +use lexlean::source::atom::{Atom, AtomClass}; +use serde_json::Value as Json; + +use super::rust_validate::{eligibility_of, rustc_differential}; +use super::scan::{atom_value, rejection_value}; +use super::{bool_val, list, nat_val, option, text, Run, Seeded}; +use crate::production_export as harness; +use crate::support::{self, repo_root}; + +const MODULE: &str = "LexLeanBoot.PropositionTerms"; +const SOURCE: &str = "src/PropositionTerms.lex.tex"; +const ROOTS: [(&str, &[&str]); 4] = [ + ("keywordEntry", &["allocation"]), + ("unknownAtom", &["allocation", "overflow"]), + ("binderAlternatives", &["allocation", "overflow"]), + ("termPhrase", &["allocation", "overflow"]), +]; +/// The glossary package of the case's closure beside the built-in ones: the +/// committed package of the Peano example, whose noun-of and binary-noun-of +/// forms (`the successor of`, `the sum of … and …`) the term phrases read. +const GLOSSARY: &str = "examples/peano-arithmetic/glossary/peano.arith"; +const VISIBLE: [&str; 3] = ["lexlean.core", "lexlean.std.nat", "peano.arith"]; + +/// The most a part of a certificate of a root may be, as the row states. +const CERTIFICATE_BUDGET: u64 = 3_145_728; +const PATH: &str = "src/Prose.lex.tex"; +const MAX_EDGES: u64 = 4_000_000; +const MAX_STATES: u64 = 4_000_000; +const MAX_DEPTH: u64 = 1_024; +/// Sentences of the committed sources: at most this many tokens, and how many +/// under the evaluator and under rustc. +const SENTENCE_TOKENS: usize = 24; +const EVALUATOR_SENTENCES: usize = 150; +const RUSTC_SENTENCES: usize = 1_500; +const EVALUATOR_SEEDED: usize = 200; +const RUSTC_SEEDED: usize = 2_000; + +// ------------------------------------------------------------ the closure + +fn closure() -> &'static Closure { + static CLOSURE: OnceLock = OnceLock::new(); + CLOSURE.get_or_init(|| { + let bootstrap = lexlean::lexicon::load_bootstrap().expect("bootstrap loads"); + let ctx = LoadContext { + language: lexlean::LANGUAGE_VERSION, + forbidden_controls: &bootstrap.structural.forbidden_controls, + max_scope_depth: 1024, + }; + let mut packages: Vec<_> = bootstrap + .builtin_packages + .iter() + .map(|row| lexlean::lexicon::load_builtin_package(row, &ctx).expect("a package")) + .collect(); + let root = repo_root().join(GLOSSARY); + let mut files: Vec<(String, Vec)> = walkdir::WalkDir::new(root.as_std_path()) + .into_iter() + .flatten() + .filter(|entry| entry.file_type().is_file()) + .map(|entry| { + let relative = entry + .path() + .strip_prefix(root.as_std_path()) + .expect("under the package") + .to_string_lossy() + .replace('\\', "/"); + ( + relative, + std::fs::read(entry.path()).expect("a glossary file"), + ) + }) + .filter(|(relative, _)| relative == "lexicon.toml" || relative.starts_with("entries/")) + .collect(); + files.sort_by(|a, b| a.0.as_bytes().cmp(b.0.as_bytes())); + packages.push( + lexlean::lexicon::package::load_package(GLOSSARY, &files, None, &ctx) + .unwrap_or_else(|diagnostics| panic!("the Peano glossary: {diagnostics:?}")), + ); + let registry = lexlean::lexicon::load_token_registry().expect("the registry"); + Closure::build(packages, registry, bootstrap, 128) + .unwrap_or_else(|diagnostics| panic!("the case's closure: {diagnostics:?}")) + }) +} + +fn visible() -> BTreeSet { + VISIBLE + .iter() + .map(|package| (*package).to_owned()) + .collect() +} + +// ------------------------------------------------------------- the encoders + +fn category_code(category: Category) -> usize { + match category { + Category::TypeNoun => 0, + Category::NounFunction => 1, + Category::BinaryNounFunction => 2, + Category::AdjectivePredicate => 3, + Category::IntransitivePredicate => 4, + Category::TransitivePredicate => 5, + _ => 8, + } +} + +#[derive(Default)] +struct Forms(Vec); + +impl Forms { + fn index(&mut self, form: &FormRef) -> usize { + if let Some(found) = self.0.iter().position(|known| known == form) { + return found; + } + self.0.push(form.clone()); + self.0.len() - 1 + } + + fn known(&self, form: &FormRef) -> usize { + self.0 + .iter() + .position(|known| known == form) + .expect("an output form is a form of the edges") + } +} + +/// The text-channel edges at each atom position, each with its form's +/// category (9 for a form the closure cannot resolve, which no rule takes and +/// which is still counted) and whether the form is plural or sentence-case. +fn edges_value(atoms: &[Atom], forms: &mut Forms) -> Value { + let closure = closure(); + let visible = visible(); + list( + (0..atoms.len()) + .map(|position| { + list( + closure + .matches_at(atoms, position, Channel::Text, &visible) + .iter() + .map(|(form, end)| { + let (category, inflected) = + closure.form(form).map_or((9, false), |(entry, shape)| { + ( + category_code(entry.category), + shape.features.iter().any(|feature| { + feature == "plural" || feature == "sentence-case" + }), + ) + }); + Value::Adt { + constructor: 0, + fields: vec![ + nat_val(forms.index(form)), + nat_val(*end), + nat_val(category), + bool_val(inflected), + ], + } + }) + .collect(), + ) + }) + .collect(), + ) +} + +fn adt(constructor: u64, fields: Vec) -> Value { + Value::Adt { + constructor, + fields, + } +} + +fn token_value(token: &TextToken) -> Value { + match token { + TextToken::Atom(index) => adt(0, vec![nat_val(*index)]), + TextToken::Island { + open, + inner_start, + inner_end, + close, + display, + } => adt( + 1, + vec![ + nat_val(*open), + nat_val(*inner_start), + nat_val(*inner_end), + nat_val(*close), + bool_val(*display), + ], + ), + } +} + +fn keywords_value(keywords: &[Keyword]) -> Value { + list( + keywords + .iter() + .map(|keyword| adt(0, vec![nat_val(keyword.atom), text(keyword.entry)])) + .collect(), + ) +} + +fn binder_value(binder: &BinderAst, forms: &Forms) -> Value { + let phrase = match &binder.type_phrase { + TypePhraseAst::Noun { candidates, atoms } => adt( + 0, + vec![ + list( + candidates + .iter() + .map(|form| nat_val(forms.known(form))) + .collect(), + ), + nat_val(atoms.0), + nat_val(atoms.1), + ], + ), + TypePhraseAst::Math(token) => adt(1, vec![token_value(token)]), + }; + adt( + 0, + vec![ + phrase, + text(&binder.spelling), + token_value(&binder.island), + nat_val(binder.ident_atoms.0), + nat_val(binder.ident_atoms.1), + keywords_value(&binder.keywords), + ], + ) +} + +fn term_value(term: &TermPhraseAst, forms: &Forms) -> Value { + match term { + TermPhraseAst::Island(token) => adt(0, vec![token_value(token)]), + TermPhraseAst::NounOf { + candidates, + surface_atoms, + args, + keywords, + } => adt( + 1, + vec![ + list( + candidates + .iter() + .map(|form| nat_val(forms.known(form))) + .collect(), + ), + nat_val(surface_atoms.0), + nat_val(surface_atoms.1), + args.iter().rev().fold(adt(2, Vec::new()), |rest, arg| { + adt(3, vec![term_value(arg, forms), rest]) + }), + keywords_value(keywords), + ], + ), + } +} + +#[derive(Clone, Copy)] +struct Limits { + edges: u64, + states: u64, + depth: u64, + states_before: u64, +} + +const COMMITTED: Limits = Limits { + edges: MAX_EDGES, + states: MAX_STATES, + depth: MAX_DEPTH, + states_before: 0, +}; + +/// What a root is asked. +#[derive(Clone, Copy)] +enum Ask { + Unknown, + Binder(usize, bool), + Term(usize, u64), +} + +impl Ask { + fn root(self) -> usize { + match self { + Self::Unknown => 1, + Self::Binder(..) => 2, + Self::Term(..) => 3, + } + } +} + +fn nat_u64(value: u64) -> Value { + Value::Nat { + value: value.to_string(), + } +} + +/// The run of a root on a sentence's atoms and tokens, and the oracle's code +/// and message when it refuses. +fn run_on( + path: &str, + atoms: &[Atom], + tokens: &[TextToken], + ask: Ask, + limits: Limits, +) -> (Run, Option) { + let mut forms = Forms::default(); + let edges = edges_value(atoms, &mut forms); + let mut budget = Budget::new(limits.edges, limits.states, limits.depth, u64::MAX); + for _ in 0..limits.states_before { + budget + .state() + .expect("the states before are within the limit"); + } + let visible = visible(); + let parser = TextParser { + path, + atoms, + tokens, + closure: closure(), + visible: &visible, + sentence_initial: true, + }; + let refused = |diagnostic: lexlean::diagnostic::Diagnostic| { + ( + Value::Error { + value: Box::new(rejection_value(&diagnostic)), + }, + Some(format!( + "{} {}", + diagnostic.code.as_str(), + diagnostic.message + )), + ) + }; + let (value, refusal) = match ask { + Ask::Unknown => match parser.unknown_atom_diagnostic(&mut budget) { + Ok(found) => { + let message = found.as_ref().map(|diagnostic| { + format!("{} {}", diagnostic.code.as_str(), diagnostic.message) + }); + ( + Value::Ok { + value: Box::new(option(found.as_ref().map(rejection_value))), + }, + message, + ) + } + Err(diagnostic) => refused(diagnostic), + }, + Ask::Binder(position, required) => { + let article = if required { + ArticleRule::Required + } else { + ArticleRule::Forbidden + }; + match parser.binder(position, &mut budget, article) { + Ok(alternatives) => ( + Value::Ok { + value: Box::new(list( + alternatives + .iter() + .map(|(end, binder)| { + adt(0, vec![nat_val(*end), binder_value(binder, &forms)]) + }) + .collect(), + )), + }, + None, + ), + Err(diagnostic) => refused(diagnostic), + } + } + Ask::Term(position, depth) => match parser.term_phrase(position, &mut budget, depth) { + Ok(alternatives) => ( + Value::Ok { + value: Box::new(list( + alternatives + .iter() + .map(|(end, term)| { + adt(0, vec![nat_val(*end), term_value(term, &forms)]) + }) + .collect(), + )), + }, + None, + ), + Err(diagnostic) => refused(diagnostic), + }, + }; + let mut arguments = vec![ + text(path), + list(atoms.iter().map(atom_value).collect()), + list(tokens.iter().map(token_value).collect()), + edges, + nat_u64(limits.edges), + nat_u64(limits.states), + nat_u64(limits.depth), + nat_u64(limits.states_before), + nat_val(0), + ]; + match ask { + Ask::Unknown => {} + Ask::Binder(position, required) => { + arguments.push(nat_val(position)); + arguments.push(bool_val(required)); + } + Ask::Term(position, depth) => { + arguments.push(nat_val(position)); + arguments.push(nat_u64(depth)); + } + } + ( + ( + arguments, + serde_json::to_value(value).expect("a value is JSON"), + ), + refusal, + ) +} + +fn keyword_run(word: &str) -> Run { + ( + vec![text(word)], + serde_json::to_value(option(keyword_entry(word).map(text))).expect("a value is JSON"), + ) +} + +fn sentence(source: &str) -> (Vec, Vec) { + let atoms = lexlean::source::scan::scan(PATH, source, u64::MAX) + .unwrap_or_else(|failure| panic!("{source:?}: {failure:?}")); + let tokens = text_tokens(PATH, &atoms, 0, atoms.len()) + .unwrap_or_else(|failure| panic!("{source:?}: {failure:?}")); + (atoms, tokens) +} + +// ------------------------------------------------------------------ vectors + +struct Vector { + label: &'static str, + source: &'static str, + ask: Ask, + limits: Limits, +} + +impl Vector { + fn run(&self) -> (Run, Option) { + let (atoms, tokens) = sentence(self.source); + run_on(PATH, &atoms, &tokens, self.ask, self.limits) + } +} + +const fn term(label: &'static str, source: &'static str, position: usize) -> Vector { + Vector { + label, + source, + ask: Ask::Term(position, 1), + limits: COMMITTED, + } +} + +const fn binder( + label: &'static str, + source: &'static str, + position: usize, + required: bool, +) -> Vector { + Vector { + label, + source, + ask: Ask::Binder(position, required), + limits: COMMITTED, + } +} + +const fn unknown(label: &'static str, source: &'static str) -> Vector { + Vector { + label, + source, + ask: Ask::Unknown, + limits: COMMITTED, + } +} + +const fn limited(label: &'static str, source: &'static str, ask: Ask, limits: Limits) -> Vector { + Vector { + label, + source, + ask, + limits, + } +} + +fn named() -> Vec { + vec![ + term("an island", "\\(n\\)", 0), + term("a word", "zero", 0), + term("the successor", "the successor of \\(n\\)", 0), + term( + "the successor of the successor", + "the successor of the successor of \\(n\\)", + 0, + ), + term("the sum", "the sum of \\(a\\) and \\(b\\)", 0), + term( + "the sum of sums", + "the sum of the sum of \\(a\\) and \\(b\\) and \\(c\\)", + 0, + ), + term( + "the sum of a successor", + "the sum of the successor of \\(a\\) and \\(b\\)", + 0, + ), + term("the sum without and", "the sum of \\(a\\) or \\(b\\)", 0), + term("the sum of one argument", "the sum of \\(a\\)", 0), + term("the successor without of", "the successor \\(n\\)", 0), + term("the successor of nothing", "the successor of", 0), + term("the without a noun", "the \\(n\\)", 0), + term("a capital the", "The successor of \\(n\\)", 0), + term("the at the end", "the", 0), + term( + "a phrase later in the sentence", + "\\(m\\) is the successor of \\(n\\)", + 2, + ), + term("a position past the tokens", "\\(n\\)", 3), + limited( + "term phrases nested beyond the limit", + "the successor of the successor of the successor of \\(n\\)", + Ask::Term(0, 1), + Limits { + depth: 2, + ..COMMITTED + }, + ), + limited( + "a term phrase at a depth beyond the limit", + "\\(n\\)", + Ask::Term(3, 5), + Limits { + depth: 4, + ..COMMITTED + }, + ), + limited( + "term phrases beyond the states", + "the sum of \\(a\\) and \\(b\\)", + Ask::Term(0, 1), + Limits { + states: 3, + ..COMMITTED + }, + ), + limited( + "term phrases spending states before", + "the successor of \\(n\\)", + Ask::Term(0, 1), + Limits { + states: 4, + states_before: 2, + ..COMMITTED + }, + ), + limited( + "term phrases beyond the edges", + "the successor of the successor of \\(n\\)", + Ask::Term(0, 1), + Limits { + edges: 1, + ..COMMITTED + }, + ), + binder("a binder of a noun", "natural number \\(n\\)", 0, false), + binder( + "a binder of a noun of two words", + "natural number list \\(xs\\)", + 0, + false, + ), + binder("a binder of an island", "\\(T\\) \\(x\\)", 0, false), + binder( + "a binder with an article", + "a natural number \\(n\\)", + 0, + true, + ), + binder("a binder with an", "an natural number \\(n\\)", 0, true), + binder( + "a binder without its article", + "natural number \\(n\\)", + 0, + true, + ), + binder( + "a binder with a forbidden article", + "a natural number \\(n\\)", + 0, + false, + ), + binder( + "a binder of a plural noun", + "natural number lists \\(xs\\)", + 0, + false, + ), + binder( + "a binder of two identifiers", + "natural number \\(n m\\)", + 0, + false, + ), + binder( + "a binder of a composed identifier", + "natural number \\( n_1' \\)", + 0, + false, + ), + binder("a binder of a numeral", "natural number \\(1\\)", 0, false), + binder("a binder of nothing", "natural number \\(\\)", 0, false), + binder("a binder without an island", "natural number n", 0, false), + binder( + "a binder of a display island", + "natural number \\[n\\]", + 0, + false, + ), + binder( + "a binder later in the sentence", + "for every natural number \\(n\\)", + 2, + false, + ), + limited( + "a binder beyond the states", + "natural number \\(n\\)", + Ask::Binder(0, false), + Limits { + states: 0, + ..COMMITTED + }, + ), + limited( + "a binder beyond the edges", + "natural number \\(n\\)", + Ask::Binder(0, false), + Limits { + edges: 0, + ..COMMITTED + }, + ), + unknown("no unknown atom", "the successor of \\(n\\) is positive"), + unknown("an unknown word", "the frobnicator of \\(n\\)"), + unknown("an unknown symbol", "\\(n\\) ¤ \\(m\\)"), + unknown("a proof keyword is known", "Assume \\(n\\)"), + unknown("a grammar keyword is known", "for every \\(n\\)"), + unknown("a word inside a longer form", "natural number list"), + unknown("islands only", "\\(n\\) \\(m\\)"), + unknown( + "the second unknown word is not reported", + "frobnicate and twiddle", + ), + limited( + "the unknown atom beyond the edges", + "natural number frobnicate", + Ask::Unknown, + Limits { + edges: 1, + ..COMMITTED + }, + ), + ] +} + +const KEYWORDS: &[&str] = &[ + "a", "A", "an", "An", "and", "as", "defined", "every", "exactly", "exists", "for", "For", + "holds", "if", "If", "implies", "is", "not", "Not", "one", "only", "or", "such", "that", "the", + "The", "then", "there", "There", "when", "of", "AND", "Every", "Of", "thE", "", "x", "ands", + "of ", "Then", +]; + +const REFUSALS: &[&str] = &[ + "LLS8002 max_scope_depth exceeded in phase parse (proposition nesting)", + "LLS8002 max_parse_states exceeded in phase parse", + "LLS8002 max_token_lattice_edges exceeded in phase lexical resolution", + "LLL1004 `frobnicator` is not a declared atom in any visible glossary", + "LLL1004 `¤` is not a declared atom in any visible glossary", +]; + +// ------------------------------------------------------------------- seeded + +const PIECES: &[&str] = &[ + "the", + "The", + "successor", + "sum", + "of", + "and", + "a", + "an", + "natural", + "number", + "list", + "lists", + "\\(n\\)", + "\\(a b\\)", + "\\(\\)", + "is", + "frobnicate", + ";", + "for", + "every", +]; + +fn seeded_runs(count: usize, seed: u64) -> Vec<(usize, Run)> { + let mut seeded = Seeded(seed); + let mut out = Vec::with_capacity(count); + while out.len() < count { + let pieces = 1 + seeded.below(9); + let source: Vec<&str> = (0..pieces).map(|_| *seeded.pick(PIECES)).collect(); + let source = source.join(" "); + let Ok(atoms) = lexlean::source::scan::scan(PATH, &source, u64::MAX) else { + continue; + }; + let Ok(tokens) = text_tokens(PATH, &atoms, 0, atoms.len()) else { + continue; + }; + let position = seeded.below(tokens.len() + 1); + let ask = match seeded.below(3) { + 0 => Ask::Unknown, + 1 => Ask::Binder(position, seeded.chance(50)), + _ => Ask::Term(position, 1), + }; + out.push((ask.root(), run_on(PATH, &atoms, &tokens, ask, COMMITTED).0)); + } + out +} + +/// Runs of the case, each with the index of the root it runs. +type RootRuns = Vec<(usize, Run)>; + +/// The sentences of the committed sources (each run of atoms between two +/// periods that tokenizes, of at most [`SENTENCE_TOKENS`] tokens), each asked +/// for its unknown atom, and for a binder and a term phrase at every token. +fn sentence_runs(evaluator: usize, rustc: usize) -> (usize, RootRuns, RootRuns) { + let mut found = 0usize; + let mut first = Vec::new(); + let mut second = Vec::new(); + for (path, content) in super::scan::sources() { + if content.len() > 400_000 || found >= rustc { + continue; + } + let Ok(atoms) = lexlean::source::scan::scan(&path, &content, u64::MAX) else { + continue; + }; + let mut start = 0; + while start < atoms.len() && found < rustc { + let end = (start..atoms.len()) + .find(|index| { + atoms[*index].class == AtomClass::AsciiSymbol && atoms[*index].text == "." + }) + .unwrap_or(atoms.len()); + let window = &atoms[start..end]; + start = end + 1; + let Ok(tokens) = text_tokens(&path, window, 0, window.len()) else { + continue; + }; + if tokens.is_empty() || tokens.len() > SENTENCE_TOKENS { + continue; + } + let mut asks = vec![Ask::Unknown]; + for position in 0..tokens.len() { + asks.push(Ask::Term(position, 1)); + asks.push(Ask::Binder(position, position % 2 == 0)); + } + found += 1; + for ask in asks { + let run = run_on(&path, window, &tokens, ask, COMMITTED).0; + if first.len() < evaluator && found.is_multiple_of(7) { + first.push((ask.root(), run.clone())); + } + second.push((ask.root(), run)); + } + } + } + (found, first, second) +} + +// ------------------------------------------------------------------- plants + +struct Plant { + label: &'static str, + site: &'static str, + replacement: &'static str, + vector: &'static str, + /// For a plant of `keywordEntry`, the word its run asks about (its + /// vector is then `keyword`). + word: Option<&'static str>, +} + +const PLANTS: &[Plant] = &[ + Plant { + label: "the capital a is no keyword", + site: "(if (if (same word (lit \"a\")) then true else (same word (lit \"A\"))) then", + replacement: "(if (same word (lit \"a\")) then", + vector: "keyword", + word: Some("A"), + }, + Plant { + label: "of names the entry the", + site: "(if (same word (lit \"of\")) then (ctor Option.some [Bytes] (lit \"of\"))", + replacement: "(if (same word (lit \"of\")) then (ctor Option.some [Bytes] (lit \"the\"))", + vector: "keyword", + word: Some("of"), + }, + Plant { + label: "a form covers only its first atom", + site: "(both (start <=? index) (index state\n | Option.none => (match (project state error)", + replacement: "| Option.some hit => (record Scan {bud := (project state bud), found := (ctor Option.none [PrimitiveScan::Rejection]), error := (project state error)})\n | Option.none => (match (project state error)", + vector: "the second unknown word is not reported", + word: None, + }, + Plant { + label: "the article is optional", + site: "(if required then (prim append", + replacement: "(if false then (prim append", + vector: "a binder with an article", + word: None, + }, + Plant { + label: "an is no article", + site: "(match (isWord ctx position (lit \"an\"))", + replacement: "(match (isWord ctx position (lit \"a\"))", + vector: "a binder with an", + word: None, + }, + Plant { + label: "a plural noun binds", + site: "(if (project found inflected) then out else", + replacement: "(if false then out else", + vector: "a binder of a plural noun", + word: None, + }, + Plant { + label: "a binder identifier may be followed", + site: "(if ((skipWs ctx innerEnd identEnd) =? innerEnd) then", + replacement: "(if true then", + vector: "a binder of two identifiers", + word: None, + }, + Plant { + label: "a binder identifier may be a numeral", + site: "(if (both ((PrimitiveScan::classCode atom) =? 1) (index (match (binderLocal ctx ((first start) + 1) (ctor TypePhrase.TMath token))", + replacement: "| Option.some token => (match (ctor Option.none [BAlt])", + vector: "a binder of an island", + word: None, + }, + Plant { + label: "the article keyword is dropped", + site: "(prim append keywords (project held keywords) : (List GrammarProof::Keyword))", + replacement: "(project held keywords)", + vector: "a binder with an article", + word: None, + }, + Plant { + label: "a binder charges no state", + site: "(let charge : (Bud * (Option PrimitiveScan::Rejection)) := (chargeState ctx (fresh statesBefore edgesBefore));", + replacement: "(let charge : (Bud * (Option PrimitiveScan::Rejection)) := ((fresh statesBefore edgesBefore), (ctor Option.none [PrimitiveScan::Rejection]));", + vector: "a binder beyond the states", + word: None, + }, + Plant { + label: "a noun phrase needs no of", + site: "(match (isWord ctx (project found upto) (lit \"of\"))", + replacement: "(match (isWord ctx (project found upto) (lit \"the\"))", + vector: "the successor", + word: None, + }, + Plant { + label: "the argument is not nested", + site: "(mk m (cons (ctor Frame.XStart ((project found upto) + 1) (depth + 1))", + replacement: "(mk m (cons (ctor Frame.XStart ((project found upto) + 1) depth)", + vector: "term phrases nested beyond the limit", + word: None, + }, + Plant { + label: "a binary noun takes one argument", + site: "(if ((project found category) =? 1) then (mk m", + replacement: "(if true then (mk m", + vector: "the sum", + word: None, + }, + Plant { + label: "the second argument needs no and", + site: "(match (isWord ctx (project held upto) (lit \"and\"))", + replacement: "(match (isWord ctx (project held upto) (lit \"or\"))", + vector: "the sum", + word: None, + }, + Plant { + label: "a binary noun charges no state", + site: "(let charge : (Bud * (Option PrimitiveScan::Rejection)) := (chargeState ctx (first state));", + replacement: "(let charge : (Bud * (Option PrimitiveScan::Rejection)) := ((first state), (ctor Option.none [PrimitiveScan::Rejection]));", + vector: "term phrases beyond the states", + word: None, + }, + Plant { + label: "the island alternative is dropped", + site: "| Option.some token => (cons (record TAlt {upto := (position + 1), term := (ctor Term.TIsland token)}) (nil TAlt))", + replacement: "| Option.some token => (nil TAlt)", + vector: "an island", + word: None, + }, + Plant { + label: "the surface ends at the of", + site: "(let range : (Nat * Nat) := (atomRange ctx (position + 1) (project found upto));", + replacement: "(let range : (Nat * Nat) := (atomRange ctx (position + 1) ((project found upto) + 1));", + vector: "the successor", + word: None, + }, + Plant { + label: "edges are counted again at a known position", + site: "(if (natIn position (project bud memo)) then (record Looked", + replacement: "(if false then (record Looked", + vector: "the unknown atom beyond the edges", + word: None, + }, + Plant { + label: "the depth refusal has no place", + site: "| Option.some token => (ctor Option.some [PrimitiveScan::Place] (placeAt ctx (GrammarProof::firstAtom token)))", + replacement: "| Option.some token => (ctor Option.none [PrimitiveScan::Place])", + vector: "term phrases nested beyond the limit", + word: None, + }, +]; + +fn plants(vectors: &[Vector]) { + let mut missed = Vec::new(); + for plant in PLANTS { + let (root, run) = match plant.word { + Some(word) => (0, keyword_run(word)), + None => { + let vector = vectors + .iter() + .find(|vector| vector.label == plant.vector) + .unwrap_or_else(|| panic!("{}: no vector `{}`", plant.label, plant.vector)); + (vector.ask.root(), vector.run().0) + } + }; + let planted = support::P::bootstrap(); + let source = planted.read_raw(SOURCE); + assert_eq!( + source.matches(plant.site).count(), + 1, + "{}: the plant site occurs once", + plant.label + ); + planted.write(SOURCE, &source.replacen(plant.site, plant.replacement, 1)); + let checked = support::checked_project(&planted); + let low = super::lower(&checked, &planted, MODULE, ROOTS[root].0); + match super::evaluate(&low, &[run], 1).first() { + Some(first) => eprintln!( + "BS-43: 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-43: {}: 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: Json = 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" + ); + } +} + +#[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("PropositionTerms"), + "PropositionTerms is a linked module of the project" + ); + eligibility_of(&project, "PropositionTerms", 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 runs: [Vec; 4] = [Vec::new(), Vec::new(), Vec::new(), Vec::new()]; + runs[0] = KEYWORDS.iter().map(|word| keyword_run(word)).collect(); + let mut messages: Vec = Vec::new(); + let mut shapes = BTreeSet::new(); + for vector in &vectors { + let (run, refusal) = vector.run(); + messages.extend(refusal); + if run.1["kind"] == "ok" { + shapes.insert(match vector.ask { + Ask::Unknown => "unknown", + Ask::Binder(..) => "binder", + Ask::Term(..) => "term", + }); + } + runs[vector.ask.root()].push(run); + } + for refusal in REFUSALS { + assert!( + messages.iter().any(|message| message.starts_with(refusal)), + "no vector reaches the refusal `{refusal}`" + ); + } + assert_eq!(shapes.len(), 3, "a vector of each root is accepted"); + + let (sentences, evaluator_sentences, rustc_sentences) = + sentence_runs(EVALUATOR_SENTENCES, RUSTC_SENTENCES); + assert!(sentences >= 50, "the committed sentences are {sentences}"); + let normative = support::normative_host(); + let (evaluator_seeded, rustc_seeded) = if normative { + ( + seeded_runs(EVALUATOR_SEEDED, 0x0B55_0043), + seeded_runs(RUSTC_SEEDED, 0x0B55_1043), + ) + } else { + (Vec::new(), Vec::new()) + }; + eprintln!( + "BS-43: {} vectors, {} keywords, {} committed sentences ({} and {} runs), {} and {} seeded runs ({:?})", + vectors.len(), + KEYWORDS.len(), + sentences, + evaluator_sentences.len(), + rustc_sentences.len(), + evaluator_seeded.len(), + rustc_seeded.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(); + if normative { + for (index, (root, _)) in ROOTS.iter().enumerate() { + let mut corpus: Vec = runs[index].clone(); + for (at, run) in evaluator_sentences.iter().chain(evaluator_seeded.iter()) { + if *at == index { + corpus.push(run.clone()); + } + } + let disagreements = super::evaluate(&lowered[root], &corpus, 5); + assert!( + disagreements.is_empty(), + "{root} and the Rust oracle disagree: {disagreements:?}" + ); + eprintln!( + "BS-43: {root}: the evaluator agrees on {} runs ({:?})", + corpus.len(), + started.elapsed() + ); + } + plants(&vectors); + } else { + eprintln!("BS-43: platform-bound host: the evaluator, the seeded runs, and the planted defects run on the normative host"); + } + + let mut compiled = runs; + for (at, run) in rustc_sentences.into_iter().chain(rustc_seeded) { + compiled[at].push(run); + } + let total = rustc_differential( + "BS-43", + &[ + (ROOTS[0].0, &lowered[ROOTS[0].0], compiled[0].as_slice()), + (ROOTS[1].0, &lowered[ROOTS[1].0], compiled[1].as_slice()), + (ROOTS[2].0, &lowered[ROOTS[2].0], compiled[2].as_slice()), + (ROOTS[3].0, &lowered[ROOTS[3].0], compiled[3].as_slice()), + ], + "oracle", + ); + eprintln!("BS-43: {total} rustc runs agree ({:?})", started.elapsed()); + + if !support::lean_backed("BS-43") { + return; + } + export(&lowered); + eprintln!("BS-43: passed in {:?}", started.elapsed()); +} diff --git a/crates/conformance/src/cases/bootstrap/semantic_print.rs b/crates/conformance/src/cases/bootstrap/semantic_print.rs new file mode 100644 index 0000000000..39ac94dc2e --- /dev/null +++ b/crates/conformance/src/cases/bootstrap/semantic_print.rs @@ -0,0 +1,1155 @@ +//! `BS-36` (SPEC.md §17.23, §17.21): the printer of the semantic notation as +//! language-1.2 source, `LexLeanBoot.SemanticPrint`, held to the byte to +//! `lexlean::surface::semantic::print_at` and `print_module`. +//! +//! The printer walks a value by structural recursion over the standard +//! library's `Json`, at the position the schema gives each value: a term, a +//! type, a member, and a parameter in their readable forms when a form carries +//! the value exactly, and every other value in the generic notation of +//! `BS-24`, tagged by its `kind` and with its members at the positions the +//! schema gives them. The schema is the host's input, as the oracle reads it +//! from the embedded tree. +//! +//! Each planted defect names the one vector that catches it. + +use std::collections::{BTreeMap, BTreeSet}; +use std::sync::OnceLock; + +use lexlean::artifact::canonical_json::Json; +use lexlean::calculus::Value; +use lexlean::surface::module::{locate, SEMANTIC}; +use lexlean::surface::semantic::{parse_module, print_at, print_module, schema_samples, Position}; +use serde_json::Value as Json2; + +use super::rust_validate::{eligibility_of, rustc_differential}; +use super::{nat_val, text, Run, Seeded}; +use crate::production_export as harness; +use crate::support::{self, repo_root}; + +const MODULE: &str = "LexLeanBoot.SemanticPrint"; +const SOURCE: &str = "src/SemanticPrint.lex.tex"; +const JSON_SOURCE: &str = "stdlib/src/Json.lex.tex"; +const SCHEMA: &str = "schemas/semantic-module-v2.schema.json"; +const ROOTS: [(&str, &[&str]); 2] = [ + ("printAt", &["allocation", "overflow", "recursion"]), + ( + "printSemanticModule", + &["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; +/// Committed modules whose notation is at most this many bytes are printed +/// whole under the evaluator, and under rustc those at most the second. +const EVALUATOR_MODULE: usize = 2_500; +const RUSTC_MODULE: usize = 15_000; +/// Declarations of the committed modules printed one at a time: at most this +/// many bytes of notation, one in every so many, under the evaluator. +const EVALUATOR_DECLARATION: usize = 1_200; +const EVALUATOR_EVERY: usize = 25; +/// Under rustc, one declaration in every so many. Each run carries the +/// schema, so the corpus is sampled to stay within the memory of a host. +const RUSTC_EVERY: usize = 6; +/// Seeded edits of declarations under the evaluator and under rustc. +const EVALUATOR_SEEDED: usize = 200; +const RUSTC_SEEDED: usize = 800; + +// ---------------------------------------------------------------- the sorts + +fn json_constructors() -> &'static Vec { + static CONSTRUCTORS: OnceLock> = OnceLock::new(); + CONSTRUCTORS.get_or_init(|| { + let source = std::fs::read_to_string(repo_root().join(JSON_SOURCE).as_std_path()) + .expect("the standard library's Json"); + let block = source + .split("inductive{") + .nth(1) + .and_then(|rest| rest.split("\n}\n").next()) + .expect("the first inductive"); + assert!(block.contains("name: Json,"), "the Json sort"); + block + .split("{name: ") + .skip(1) + .map(|rest| { + rest.split(',') + .next() + .expect("a constructor") + .trim() + .to_owned() + }) + .collect() + }) +} + +fn adt(constructor: &str, fields: Vec) -> Value { + let found = json_constructors() + .iter() + .position(|name| name == constructor) + .unwrap_or_else(|| panic!("`Json` has no constructor `{constructor}`")); + Value::Adt { + constructor: u64::try_from(found).expect("an index"), + fields, + } +} + +/// A value as the standard library's `Json`. +fn enc_json(json: &Json) -> Value { + match json { + Json::Bool(flag) => adt("JBool", vec![Value::Bool { value: *flag }]), + Json::Int(number) => adt( + "JInt", + vec![ + Value::Bool { value: *number < 0 }, + Value::Nat { + value: number.unsigned_abs().to_string(), + }, + ], + ), + Json::Str(value) => adt("JStr", vec![text(value)]), + Json::Arr(items) => adt( + "JArr", + vec![items + .iter() + .rev() + .fold(adt("JNil", Vec::new()), |rest, item| { + adt("JItem", vec![enc_json(item), rest]) + })], + ), + Json::Obj(members) => adt( + "JObj", + vec![members + .iter() + .rev() + .fold(adt("JNil", Vec::new()), |rest, (key, value)| { + adt("JMember", vec![text(key), enc_json(value), rest]) + })], + ), + } +} + +/// The schema the oracle reads, as the host gives it to the roots. +fn schema_value() -> &'static Value { + static SCHEMA_VALUE: OnceLock = OnceLock::new(); + SCHEMA_VALUE.get_or_init(|| { + let bytes = std::fs::read(repo_root().join(SCHEMA).as_std_path()).expect("the schema"); + let embedded = lexlean::embedded::FILES + .iter() + .find(|(path, _)| *path == SCHEMA) + .map(|(_, bytes)| *bytes) + .expect("the schema is embedded"); + assert_eq!(bytes, embedded, "the committed schema is the embedded one"); + enc_json(&Json::parse(&bytes).expect("the schema is canonical JSON's data model")) + }) +} + +fn position_code(position: Position) -> usize { + match position { + Position::Declaration => 0, + Position::Term => 1, + Position::Type => 2, + Position::Proof => 3, + } +} + +fn at_run(value: &Json, position: Position) -> Run { + ( + vec![ + schema_value().clone(), + enc_json(value), + nat_val(position_code(position)), + ], + serde_json::to_value(text(&print_at(value, position))).expect("a value is JSON"), + ) +} + +fn module_run(module: &Json) -> (Run, Option) { + let (value, refusal) = match print_module(module) { + Ok(printed) => ( + Value::Ok { + value: Box::new(text(&printed)), + }, + None, + ), + Err(failure) => ( + Value::Error { + value: Box::new(Value::Adt { + constructor: 0, + fields: vec![nat_val(failure.offset), text(&failure.message)], + }), + }, + Some(failure.message), + ), + }; + ( + ( + vec![schema_value().clone(), enc_json(module)], + serde_json::to_value(value).expect("a value is JSON"), + ), + refusal, + ) +} + +// ------------------------------------------------------------------ vectors + +fn json(text: &str) -> Json { + Json::parse(text.as_bytes()).unwrap_or_else(|error| panic!("{text}: {error}")) +} + +/// One named vector: a value printed at a position, or a module printed. +struct Vector { + label: String, + value: Json, + /// `None` for a module. + position: Option, +} + +impl Vector { + fn root(&self) -> usize { + usize::from(self.position.is_none()) + } + + fn run(&self) -> Run { + match self.position { + Some(position) => at_run(&self.value, position), + None => module_run(&self.value).0, + } + } +} + +const TERMS: &[(&str, &str)] = &[ + ("a variable", r#"{"kind":"var","name":"x"}"#), + ( + "a variable named like a keyword", + r#"{"kind":"var","name":"match"}"#, + ), + ("a variable named false", r#"{"kind":"var","name":"false"}"#), + ("a variable named with", r#"{"kind":"var","name":"with"}"#), + ( + "a variable that is no name", + r#"{"kind":"var","name":"a b"}"#, + ), + ( + "a variable with a member more", + r#"{"kind":"var","name":"x","type":{"kind":"nat"}}"#, + ), + ("a natural", r#"{"kind":"nat","value":"42"}"#), + ("a natural zero", r#"{"kind":"nat","value":"0"}"#), + ( + "a natural with a leading zero", + r#"{"kind":"nat","value":"042"}"#, + ), + ("a natural that is empty", r#"{"kind":"nat","value":""}"#), + ("a natural with a sign", r#"{"kind":"nat","value":"-1"}"#), + ("true", r#"{"kind":"bool","value":true}"#), + ("false", r#"{"kind":"bool","value":false}"#), + ( + "a Boolean that is a string", + r#"{"kind":"bool","value":"true"}"#, + ), + ("a string", r#"{"kind":"string","value":"a \"b\"\n"}"#), + ( + "a string that is a number", + r#"{"kind":"string","value":3}"#, + ), + ("bytes", r#"{"kind":"bytes","hex":"4a4b"}"#), + ("empty bytes", r#"{"kind":"bytes","hex":""}"#), + ("bytes of odd length", r#"{"kind":"bytes","hex":"4a4"}"#), + ("bytes in capitals", r#"{"kind":"bytes","hex":"4A"}"#), + ("unit", r#"{"kind":"unit"}"#), + ("unit with a member", r#"{"kind":"unit","value":1}"#), + ( + "an integer", + r#"{"kind":"integer","representation":"int8","value":"-3"}"#, + ), + ( + "an unsigned integer", + r#"{"kind":"integer","representation":"uint64","value":"18"}"#, + ), + ( + "an integer minus zero", + r#"{"kind":"integer","representation":"int","value":"-0"}"#, + ), + ( + "an integer with a leading zero", + r#"{"kind":"integer","representation":"int","value":"07"}"#, + ), + ( + "an integer of no representation", + r#"{"kind":"integer","representation":"int128","value":"1"}"#, + ), + ( + "a sum", + r#"{"kind":"add","left":{"kind":"var","name":"a"},"right":{"kind":"nat","value":"1"}}"#, + ), + ( + "an implication", + r#"{"kind":"implies","premise":{"kind":"var","name":"p"},"conclusion":{"kind":"var","name":"q"}}"#, + ), + ( + "an implication with left and right", + r#"{"kind":"implies","left":{"kind":"var","name":"p"},"right":{"kind":"var","name":"q"}}"#, + ), + ( + "a conjunction of propositions", + r#"{"kind":"prop_and","left":{"kind":"var","name":"p"},"right":{"kind":"var","name":"q"}}"#, + ), + ( + "each comparison", + r#"{"kind":"or","left":{"kind":"and","left":{"kind":"beq","left":{"kind":"var","name":"a"},"right":{"kind":"var","name":"b"}},"right":{"kind":"ble","left":{"kind":"var","name":"a"},"right":{"kind":"var","name":"b"}}},"right":{"kind":"iff","left":{"kind":"blt","left":{"kind":"var","name":"a"},"right":{"kind":"var","name":"b"}},"right":{"kind":"le","left":{"kind":"lt","left":{"kind":"var","name":"a"},"right":{"kind":"var","name":"b"}},"right":{"kind":"eq","left":{"kind":"var","name":"a"},"right":{"kind":"var","name":"b"}}}}}"#, + ), + ( + "a sum with a member more", + r#"{"kind":"add","left":{"kind":"var","name":"a"},"right":{"kind":"var","name":"b"},"z":1}"#, + ), + ( + "a pair", + r#"{"kind":"pair","left":{"kind":"var","name":"a"},"right":{"kind":"unit"}}"#, + ), + ( + "not, first and second", + r#"{"kind":"not","value":{"kind":"first","value":{"kind":"second","value":{"kind":"var","name":"p"}}}}"#, + ), + ( + "a list", + r#"{"kind":"cons","head":{"kind":"var","name":"h"},"tail":{"kind":"nil","element":{"kind":"nat"}}}"#, + ), + ( + "an if", + r#"{"kind":"if","condition":{"kind":"var","name":"c"},"then_value":{"kind":"nat","value":"1"},"else_value":{"kind":"nat","value":"2"}}"#, + ), + ( + "a let", + r#"{"kind":"let","binder":{"name":"x","type":{"kind":"nat"}},"value":{"kind":"nat","value":"1"},"body":{"kind":"var","name":"x"}}"#, + ), + ( + "a let whose binder has a member more", + r#"{"kind":"let","binder":{"name":"x","type":{"kind":"nat"},"z":1},"value":{"kind":"nat","value":"1"},"body":{"kind":"var","name":"x"}}"#, + ), + ( + "a let whose binder is no name", + r#"{"kind":"let","binder":{"name":"true","type":{"kind":"nat"}},"value":{"kind":"nat","value":"1"},"body":{"kind":"var","name":"x"}}"#, + ), + ( + "a forall", + r#"{"kind":"forall","binder":{"name":"n","type":{"kind":"nat"}},"body":{"kind":"eq","left":{"kind":"var","name":"n"},"right":{"kind":"var","name":"n"}}}"#, + ), + ( + "a lambda", + r#"{"kind":"lambda","parameters":[{"name":"x","type":{"kind":"nat"}},{"name":"y","type":{"kind":"bool"}}],"captures":[],"body":{"kind":"var","name":"x"}}"#, + ), + ( + "a lambda with captures", + r#"{"kind":"lambda","parameters":[{"name":"x","type":{"kind":"nat"}}],"captures":["a","b"],"body":{"kind":"var","name":"a"}}"#, + ), + ( + "a lambda capturing no name", + r#"{"kind":"lambda","parameters":[],"captures":["a b"],"body":{"kind":"unit"}}"#, + ), + ( + "a lambda with a parameter that is no name", + r#"{"kind":"lambda","parameters":[{"name":"a b","type":{"kind":"nat"}}],"captures":[],"body":{"kind":"unit"}}"#, + ), + ( + "a lambda without parameters", + r#"{"kind":"lambda","parameters":[],"captures":[],"body":{"kind":"unit"}}"#, + ), + ( + "an application", + r#"{"kind":"apply","function":{"kind":"var","name":"f"},"arguments":[{"kind":"var","name":"a"},{"kind":"var","name":"b"}]}"#, + ), + ( + "an application of nothing", + r#"{"kind":"apply","function":{"kind":"var","name":"f"},"arguments":[]}"#, + ), + ( + "a projection", + r#"{"kind":"project","value":{"kind":"var","name":"r"},"field":"size"}"#, + ), + ( + "a projection of no name", + r#"{"kind":"project","value":{"kind":"var","name":"r"},"field":"a-"}"#, + ), + ( + "a call", + r#"{"kind":"call","function":{"name":"f"},"arguments":[{"kind":"var","name":"a"}]}"#, + ), + ( + "a call into a module", + r#"{"kind":"call","function":{"module":"M","name":"f"},"arguments":[]}"#, + ), + ( + "a call with type arguments", + r#"{"kind":"call","function":{"name":"f"},"type_arguments":[{"kind":"nat"},{"kind":"bool"}],"arguments":[{"kind":"var","name":"a"}]}"#, + ), + ( + "a call with no type arguments written", + r#"{"kind":"call","function":{"name":"f"},"type_arguments":[],"arguments":[]}"#, + ), + ( + "a call of a keyword", + r#"{"kind":"call","function":{"name":"let"},"arguments":[]}"#, + ), + ( + "a call whose module is no string", + r#"{"kind":"call","function":{"module":1,"name":"f"},"arguments":[]}"#, + ), + ( + "a call whose function has a member more", + r#"{"kind":"call","function":{"name":"f","z":1},"arguments":[]}"#, + ), + ( + "a constructor", + r#"{"kind":"constructor","constructor":{"name":"Option.some"},"type_arguments":[{"kind":"nat"}],"arguments":[{"kind":"nat","value":"1"}]}"#, + ), + ( + "a constructor without arguments", + r#"{"kind":"constructor","constructor":{"module":"M","name":"T.C"},"arguments":[]}"#, + ), + ( + "a function reference", + r#"{"kind":"function_ref","function":{"name":"f"},"type_arguments":[{"kind":"nat"}]}"#, + ), + ( + "a function reference alone", + r#"{"kind":"function_ref","function":{"name":"f"}}"#, + ), + ( + "a record", + r#"{"kind":"record","type":{"name":"R"},"fields":[{"field":"a","value":{"kind":"nat","value":"1"}},{"field":"b","value":{"kind":"unit"}}]}"#, + ), + ( + "a record of no fields", + r#"{"kind":"record","type":{"name":"R"},"type_arguments":[{"kind":"nat"}],"fields":[]}"#, + ), + ( + "a record whose field is no name", + r#"{"kind":"record","type":{"name":"R"},"fields":[{"field":"a b","value":{"kind":"unit"}}]}"#, + ), + ( + "a record whose assignment has a member more", + r#"{"kind":"record","type":{"name":"R"},"fields":[{"field":"a","value":{"kind":"unit"},"z":1}]}"#, + ), + ( + "a primitive", + r#"{"kind":"primitive","operation":"append","arguments":[{"kind":"var","name":"a"},{"kind":"var","name":"b"}],"result":{"kind":"bytes"}}"#, + ), + ( + "a primitive of no arguments", + r#"{"kind":"primitive","operation":"zero","arguments":[],"result":{"kind":"nat"}}"#, + ), + ( + "a checked application", + r#"{"kind":"checked_apply","model":{"name":"M"},"type_arguments":[{"kind":"nat"}],"arguments":[{"kind":"var","name":"a"}],"checks":["c1","c2"]}"#, + ), + ( + "a checked application of no checks", + r#"{"kind":"checked_apply","model":{"name":"M"},"arguments":[],"checks":[]}"#, + ), + ( + "a checked application whose check is no name", + r#"{"kind":"checked_apply","model":{"name":"M"},"arguments":[],"checks":["1x"]}"#, + ), + ( + "a match", + r#"{"kind":"match","scrutinee":{"kind":"var","name":"o"},"branches":[{"constructor":{"name":"Option.some"},"binders":["v"],"body":{"kind":"var","name":"v"}},{"constructor":{"name":"Option.none"},"binders":[],"body":{"kind":"nat","value":"0"}}]}"#, + ), + ( + "a match too long for a line", + r#"{"kind":"match","scrutinee":{"kind":"var","name":"scrutineeWithALongName"},"branches":[{"constructor":{"name":"Option.some"},"binders":["valueWithAVeryLongName"],"body":{"kind":"var","name":"valueWithAVeryLongName"}},{"constructor":{"name":"Option.none"},"binders":[],"body":{"kind":"nat","value":"1234567890"}}]}"#, + ), + ( + "a match whose binder is a keyword", + r#"{"kind":"match","scrutinee":{"kind":"var","name":"o"},"branches":[{"constructor":{"name":"C"},"binders":["then"],"body":{"kind":"unit"}}]}"#, + ), + ( + "a match of no branches", + r#"{"kind":"match","scrutinee":{"kind":"var","name":"o"},"branches":[]}"#, + ), + ( + "a term of no readable form", + r#"{"kind":"map_literal","entries":[]}"#, + ), + ("a term without a kind", r#"{"name":"x"}"#), + ( + "a term whose kind is no name", + r#"{"kind":"a b","name":"x"}"#, + ), + ("a term that is a string", r#""text""#), + ("a term that is an array", r#"[{"kind":"var","name":"x"}]"#), +]; + +const TYPES: &[(&str, &str)] = &[ + ( + "every nullary type", + r#"{"kind":"function","parameters":[{"kind":"bool"},{"kind":"bytes"},{"kind":"int"},{"kind":"int8"},{"kind":"int16"},{"kind":"int32"},{"kind":"int64"},{"kind":"nat"},{"kind":"ordering"},{"kind":"prop"},{"kind":"string"},{"kind":"type"},{"kind":"uint8"},{"kind":"uint16"},{"kind":"uint32"},{"kind":"uint64"},{"kind":"unit"},{"kind":"contract_violation"},{"kind":"reasoning_failure"}],"result":{"kind":"unit"}}"#, + ), + ( + "a nullary type with a member more", + r#"{"kind":"nat","z":1}"#, + ), + ("a type parameter", r#"{"kind":"parameter","name":"T"}"#), + ( + "a type parameter that is no name", + r#"{"kind":"parameter","name":"1"}"#, + ), + ( + "a list and a set", + r#"{"kind":"product","left":{"kind":"list","element":{"kind":"nat"}},"right":{"kind":"set","element":{"kind":"bool"}}}"#, + ), + ( + "an option, a result and a map", + r#"{"kind":"result","ok":{"kind":"option","value":{"kind":"nat"}},"error":{"kind":"map","key":{"kind":"string"},"value":{"kind":"bytes"}}}"#, + ), + ( + "a function of no parameters", + r#"{"kind":"function","parameters":[],"result":{"kind":"nat"}}"#, + ), + ( + "a named type", + r#"{"kind":"named","member":{"name":"Doc"},"arguments":[]}"#, + ), + ( + "a named type with arguments", + r#"{"kind":"named","member":{"module":"M","name":"Pair"},"arguments":[{"kind":"nat"},{"kind":"parameter","name":"T"}]}"#, + ), + ( + "a named type spelled like a type", + r#"{"kind":"named","member":{"name":"List"},"arguments":[]}"#, + ), + ( + "a named type spelled Map", + r#"{"kind":"named","member":{"name":"Map"},"arguments":[]}"#, + ), + ( + "a named type spelled like a nullary type", + r#"{"kind":"named","member":{"name":"Nat"},"arguments":[]}"#, + ), + ( + "a named type of a module spelled like a type", + r#"{"kind":"named","member":{"module":"M","name":"List"},"arguments":[]}"#, + ), + ( + "a named type whose member has a member more", + r#"{"kind":"named","member":{"name":"Doc","z":1},"arguments":[]}"#, + ), + ( + "a list with a member more", + r#"{"kind":"list","element":{"kind":"nat"},"z":1}"#, + ), + ( + "a type of no readable form", + r#"{"kind":"graph","node":{"kind":"nat"}}"#, + ), +]; + +/// The committed semantic modules written in the notation, read. +fn committed() -> Vec<(String, Json, usize)> { + let spec = lexlean::ir::semantic::semantic_module_spec(lexlean::LANGUAGE_1_2).expect("a spec"); + let mut out = Vec::new(); + for (path, content) in super::scan::sources() { + let Some(found) = locate(&content) else { + continue; + }; + if !found.kind.is(&SEMANTIC) || found.data.is_some() { + continue; + } + let body = &content[found.body.0..found.body.1]; + if let Ok(module) = parse_module(body, spec) { + out.push((path, module, body.len())); + } + } + out +} + +fn named() -> Vec { + let mut out = Vec::new(); + for (label, source) in TERMS { + out.push(Vector { + label: (*label).to_owned(), + value: json(source), + position: Some(Position::Term), + }); + } + for (label, source) in TYPES { + out.push(Vector { + label: (*label).to_owned(), + value: json(source), + position: Some(Position::Type), + }); + } + for (label, source, position) in [ + ( + "a declaration", + r#"{"kind":"definition","name":"f","parameters":[{"name":"x","type":{"kind":"nat"}}],"result":{"kind":"nat"},"body":{"kind":"var","name":"x"},"executable":true}"#, + Position::Declaration, + ), + ( + "a declaration of no kind", + r#"{"name":"f"}"#, + Position::Declaration, + ), + ( + "a declaration whose parameter is generic", + r#"{"kind":"definition","name":"f","parameters":[{"name":"x","type":{"kind":"nat"},"z":1}],"result":{"kind":"nat"},"body":{"kind":"unit"}}"#, + Position::Declaration, + ), + ("a proof", r#"{"kind":"decide"}"#, Position::Proof), + ( + "a term at the proof position", + r#"{"kind":"var","name":"x"}"#, + Position::Proof, + ), + ] { + out.push(Vector { + label: label.to_owned(), + value: json(source), + position: Some(position), + }); + } + for (label, source) in [ + ( + "a module", + r#"{"spec":"s","declarations":[{"kind":"definition","name":"a"},{"kind":"definition","name":"b"}]}"#, + ), + ( + "a module of no declarations", + r#"{"spec":"s","declarations":[]}"#, + ), + ("a module that is no object", r#"[1]"#), + ("a module without declarations", r#"{"spec":"s"}"#), + ( + "a module whose declarations are no array", + r#"{"spec":"s","declarations":{}}"#, + ), + ( + "a module with a member more", + r#"{"spec":"s","declarations":[],"z":1}"#, + ), + ( + "a module whose spec is no string", + r#"{"spec":1,"declarations":[]}"#, + ), + ("a module without a spec", r#"{"declarations":[],"z":1}"#), + ] { + out.push(Vector { + label: label.to_owned(), + value: json(source), + position: None, + }); + } + // A union may list one kind twice (an `enum` alternative beside a + // `const` one); the second sample is labelled by its place. + let mut seen = BTreeSet::new(); + for (position, label, value) in schema_samples() { + let mut label = format!("the schema sample {label}"); + let mut place = 2; + while !seen.insert(label.clone()) { + label = format!("{label} ({place})"); + place += 1; + } + out.push(Vector { + label, + value, + position: Some(position), + }); + } + out +} + +const REFUSALS: &[&str] = &[ + "a module is an object", + "a module has a `declarations` array", + "a module has exactly a `spec` and its `declarations`", +]; + +// ------------------------------------------------------------------- seeded + +const NAMES: &[&str] = &[ + "x", "if", "M", "a b", "List", "Nat", "1", "", "f.g", "false", +]; + +fn edit(seeded: &mut Seeded, value: &Json, depth: usize) -> Json { + match value { + Json::Obj(members) => { + let mut members = members.clone(); + let keys: Vec = members.keys().cloned().collect(); + if !keys.is_empty() && seeded.chance(25) { + let key = seeded.pick(&keys).clone(); + match seeded.below(4) { + 0 => { + members.remove(&key); + } + 1 => { + members.insert(key, Json::Str((*seeded.pick(NAMES)).to_owned())); + } + 2 => { + members.insert("z".to_owned(), Json::Int(1)); + } + _ => { + members.insert(key, Json::Arr(Vec::new())); + } + } + } else if !keys.is_empty() && depth < 40 { + let key = seeded.pick(&keys).clone(); + let inner = edit(seeded, &members[&key], depth + 1); + members.insert(key, inner); + } + Json::Obj(members) + } + Json::Arr(items) if !items.is_empty() && depth < 40 => { + let mut items = items.clone(); + let at = seeded.below(items.len()); + if seeded.chance(20) { + items.remove(at); + } else { + items[at] = edit(seeded, &items[at], depth + 1); + } + Json::Arr(items) + } + Json::Str(_) if seeded.chance(50) => Json::Str((*seeded.pick(NAMES)).to_owned()), + other => other.clone(), + } +} + +fn seeded_runs(count: usize, bases: &[Json], seed: u64) -> Vec { + let mut seeded = Seeded(seed); + let positions = [ + Position::Declaration, + Position::Term, + Position::Type, + Position::Proof, + ]; + (0..count) + .map(|_| { + let base = seeded.pick(bases).clone(); + let edited = edit(&mut seeded, &base, 0); + let position = if seeded.chance(80) { + Position::Declaration + } else { + *seeded.pick(&positions) + }; + at_run(&edited, position) + }) + .collect() +} + +// ------------------------------------------------------------------- plants + +struct Plant { + label: &'static str, + site: &'static str, + replacement: &'static str, + vector: &'static str, +} + +const PLANTS: &[Plant] = &[ + Plant { + label: "a keyword may name a variable", + site: "(both (if (listed name (reservedTerm)) then false else true)", + replacement: "(both true", + vector: "a variable named like a keyword", + }, + Plant { + label: "a leading zero is canonical", + site: "(if (LexLeanStd.Text::startsWith text (lit \"0\")) then false else true)", + replacement: "true", + vector: "a natural with a leading zero", + }, + Plant { + label: "minus zero is canonical", + site: "(both (canonicalDigits digits) (if (same digits (lit \"0\")) then false else true))", + replacement: "(canonicalDigits digits)", + vector: "an integer minus zero", + }, + Plant { + label: "capitals are hexadecimal", + site: "(both (97 <=? code) (code <=? 102))", + replacement: "(both (65 <=? code) (code <=? 102))", + vector: "bytes in capitals", + }, + Plant { + label: "odd hexadecimal is canonical", + site: "(both ((prim remainder (prim length text : Nat) 2 0 : Nat) =? 0) (allBytes text 0 true))", + replacement: "(allBytes text 0 true)", + vector: "bytes of odd length", + }, + Plant { + label: "the integer spelling of int8", + site: "(ctor Option.some [Bytes] (lit \"Int8\")) else (if (same representation (lit \"int16\"))", + replacement: "(ctor Option.some [Bytes] (lit \"Int\")) else (if (same representation (lit \"int16\"))", + vector: "an integer", + }, + Plant { + label: "an implication of left and right", + site: "(if (same kind (lit \"implies\")) then (lit \"premise\") else (lit \"left\"))", + replacement: "(lit \"left\")", + vector: "an implication", + }, + Plant { + label: "the operator of a conjunction of propositions", + site: "(lit \"/\\\\\")", + replacement: "(lit \"&&\")", + vector: "a conjunction of propositions", + }, + Plant { + label: "an extra member keeps the readable form", + site: "(both ((countOf chain) =? (prim length keys : Nat)) (hasAll chain keys))", + replacement: "(hasAll chain keys)", + vector: "a sum with a member more", + }, + Plant { + label: "empty type arguments are readable", + site: "| Option.some items => (0 true", + vector: "a call with no type arguments written", + }, + Plant { + label: "a call may name a keyword", + site: "| Option.some name => (plain name)\n | Option.none => false)", + replacement: "| Option.some name => true\n | Option.none => false)", + vector: "a call of a keyword", + }, + Plant { + label: "the captures are always written", + site: "(if (0 (paramOk parameter)))", + replacement: "true", + vector: "a lambda with a parameter that is no name", + }, + Plant { + label: "a let binder may have a member more", + site: "(both ((countOf chain) =? 2) (SurfaceGeneric::isBare name))", + replacement: "(SurfaceGeneric::isBare name)", + vector: "a let whose binder has a member more", + }, + Plant { + label: "the module of a member is dropped", + site: "(ctor Option.some [Bytes] (cat (cat prefix (lit \"::\")) name))", + replacement: "(ctor Option.some [Bytes] name)", + vector: "a call into a module", + }, + Plant { + label: "a local member spelled like a type is readable", + site: "(if (both (if (present (memberIn inner (lit \"module\"))) then false else true) (reservedType name)) then false else true)", + replacement: "true", + vector: "a named type spelled like a type", + }, + Plant { + label: "a qualified member spelled like a type is generic", + site: "(if (present (memberIn inner (lit \"module\"))) then false else true)", + replacement: "true", + vector: "a named type of a module spelled like a type", + }, + Plant { + label: "Map is no applied type", + site: "(cons (lit \"Map\") (cons (lit \"Fn\")", + replacement: "(cons (lit \"Mapx\") (cons (lit \"Fn\")", + vector: "a named type spelled Map", + }, + Plant { + label: "the product is spelled with a comma", + site: "(tx (lit \" * \"))", + replacement: "(tx (lit \", \"))", + vector: "a list and a set", + }, + Plant { + label: "the parameter types of a function are comma separated", + site: "| Pos.PFnTypes => (seq (cons (tx (lit \"(\")) (prim append (interleave docs (lit \" \"))", + replacement: "| Pos.PFnTypes => (seq (cons (tx (lit \"(\")) (prim append (interleave docs (lit \", \"))", + vector: "every nullary type", + }, + Plant { + label: "a match is not grouped", + site: "(ctor SurfaceDoc::Doc.DGroup (ctor SurfaceDoc::Doc.DNest 2 (seq (cons (tx (lit \"(match \"))", + replacement: "(ctor SurfaceDoc::Doc.DNest 2 (ctor SurfaceDoc::Doc.DNest 0 (seq (cons (tx (lit \"(match \"))", + vector: "a match", + }, + Plant { + label: "a match branch is not indented", + site: "(ctor SurfaceDoc::Doc.DNest 2 (seq (cons (tx (lit \"(match \"))", + replacement: "(ctor SurfaceDoc::Doc.DNest 0 (seq (cons (tx (lit \"(match \"))", + vector: "a match too long for a line", + }, + Plant { + label: "a record field is written with a colon", + site: "(tx (cat (strAt chain (lit \"field\")) (lit \" := \")))", + replacement: "(tx (cat (strAt chain (lit \"field\")) (lit \": \")))", + vector: "a record", + }, + Plant { + label: "the checks are written without with", + site: "(cons (tx (lit \" with \")) (cons (tx (lit \"[\"))", + replacement: "(cons (tx (lit \" \")) (cons (tx (lit \"[\"))", + vector: "a checked application", + }, + Plant { + label: "a declaration's members are at no position", + site: "(itemsOf schema declarations)", + replacement: "(ctor Pos.PAny)", + vector: "a declaration", + }, + Plant { + label: "the proof position is any", + site: "| Option.some proof => (ctor Pos.PNode proof)", + replacement: "| Option.some proof => (ctor Pos.PAny)", + vector: "the schema sample proof apply (every member)", + }, + Plant { + label: "modules are not separated by an empty line", + site: "(cat (second state) (lit \"\\n\"))", + replacement: "(second state)", + vector: "a module", + }, + Plant { + label: "the spec need not be a string", + site: "| Option.some spec => true\n | Option.none => false)", + replacement: "| Option.some spec => true\n | Option.none => true)", + vector: "a module whose spec is no string", + }, + Plant { + label: "a module may have a member more", + site: "(both ((countOf chain) =? 2) (match (strOf (memberIn chain (lit \"spec\")))", + replacement: "(both true (match (strOf (memberIn chain (lit \"spec\")))", + vector: "a module with a member more", + }, +]; + +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)); + let planted = support::P::bootstrap(); + let source = planted.read_raw(SOURCE); + assert_eq!( + source.matches(plant.site).count(), + 1, + "{}: the plant site occurs once", + plant.label + ); + planted.write(SOURCE, &source.replacen(plant.site, plant.replacement, 1)); + let checked = support::checked_project(&planted); + let low = super::lower(&checked, &planted, MODULE, ROOTS[vector.root()].0); + match super::evaluate(&low, &[vector.run()], 1).first() { + Some(first) => eprintln!( + "BS-36: 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-36: {}: 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" + ); + } +} + +#[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("SemanticPrint"), + "SemanticPrint is a linked module of the project" + ); + eligibility_of(&project, "SemanticPrint", MODULE, &ROOTS); + + let vectors = named(); + let labels: BTreeSet<&str> = vectors.iter().map(|vector| vector.label.as_str()).collect(); + assert_eq!(labels.len(), vectors.len(), "each vector has its own label"); + let mut runs: [Vec; 2] = [Vec::new(), Vec::new()]; + let mut messages = Vec::new(); + for vector in &vectors { + if vector.position.is_none() { + messages.extend(module_run(&vector.value).1); + } + runs[vector.root()].push(vector.run()); + } + for refusal in REFUSALS { + assert!( + messages.iter().any(|message| message == refusal), + "no vector reaches the refusal `{refusal}`" + ); + } + + let modules = committed(); + assert!( + modules.len() >= 50, + "the committed semantic modules in the notation are {}", + modules.len() + ); + let mut evaluator: [Vec; 2] = [Vec::new(), Vec::new()]; + let mut compiled: [Vec; 2] = [Vec::new(), Vec::new()]; + let mut bases: Vec = Vec::new(); + let mut declarations = 0usize; + for (path, module, size) in &modules { + if *size <= EVALUATOR_MODULE { + evaluator[1].push(module_run(module).0); + } + if *size <= RUSTC_MODULE { + compiled[1].push(module_run(module).0); + } + let Json::Obj(members) = module else { + panic!("{path}: a module is an object"); + }; + let Some(Json::Arr(items)) = members.get("declarations") else { + panic!("{path}: a module has declarations"); + }; + for (index, declaration) in items.iter().enumerate() { + let printed = print_at(declaration, Position::Declaration).len(); + declarations += 1; + if printed <= EVALUATOR_DECLARATION && (index + path.len()) % EVALUATOR_EVERY == 0 { + evaluator[0].push(at_run(declaration, Position::Declaration)); + bases.push(declaration.clone()); + } + if printed <= RUSTC_MODULE && (index + path.len()) % RUSTC_EVERY == 0 { + compiled[0].push(at_run(declaration, Position::Declaration)); + } + } + } + let normative = support::normative_host(); + let (evaluator_seeded, rustc_seeded) = if normative { + ( + seeded_runs(EVALUATOR_SEEDED, &bases, 0x0B55_0036), + seeded_runs(RUSTC_SEEDED, &bases, 0x0B55_1036), + ) + } else { + (Vec::new(), Vec::new()) + }; + eprintln!( + "BS-36: {} vectors ({} at a position, {} modules), {} committed modules with {} declarations ({} and {} declarations, {} and {} modules under the evaluator and rustc), {} and {} seeded edits ({:?})", + vectors.len(), + runs[0].len(), + runs[1].len(), + modules.len(), + declarations, + evaluator[0].len(), + compiled[0].len(), + evaluator[1].len(), + compiled[1].len(), + evaluator_seeded.len(), + rustc_seeded.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(); + if normative { + for (index, (root, _)) in ROOTS.iter().enumerate() { + let mut corpus: Vec = runs[index].clone(); + corpus.extend(evaluator[index].iter().cloned()); + if index == 0 { + corpus.extend(evaluator_seeded.iter().cloned()); + } + let disagreements = super::evaluate(&lowered[root], &corpus, 5); + assert!( + disagreements.is_empty(), + "{root} and the Rust oracle disagree: {disagreements:?}" + ); + eprintln!( + "BS-36: {root}: the evaluator agrees on {} runs ({:?})", + corpus.len(), + started.elapsed() + ); + } + plants(&vectors); + } else { + eprintln!("BS-36: platform-bound host: the evaluator, the seeded runs, and the planted defects run on the normative host"); + } + + let mut all: [Vec; 2] = [runs[0].clone(), runs[1].clone()]; + all[0].extend(compiled[0].iter().cloned()); + all[1].extend(compiled[1].iter().cloned()); + all[0].extend(rustc_seeded); + let total = rustc_differential( + "BS-36", + &[ + (ROOTS[0].0, &lowered[ROOTS[0].0], all[0].as_slice()), + (ROOTS[1].0, &lowered[ROOTS[1].0], all[1].as_slice()), + ], + "oracle", + ); + eprintln!("BS-36: {total} rustc runs agree ({:?})", started.elapsed()); + + if !support::lean_backed("BS-36") { + return; + } + export(&lowered); + eprintln!("BS-36: passed in {:?}", started.elapsed()); +} diff --git a/crates/conformance/src/cases/bootstrap/semantic_read.rs b/crates/conformance/src/cases/bootstrap/semantic_read.rs new file mode 100644 index 0000000000..697f9331b8 --- /dev/null +++ b/crates/conformance/src/cases/bootstrap/semantic_read.rs @@ -0,0 +1,1159 @@ +//! `BS-37` (SPEC.md §17.23, §17.21): the reader of the semantic notation as +//! language-1.2 source, `LexLeanBoot.SemanticRead`, held to the byte to +//! `lexlean::surface::semantic::parse_at`, `parse_module`, and +//! `declaration_spans`. +//! +//! The reader lexes the text with `lexText` of `BS-22`, so that a lexical error +//! comes first, and reads the tokens with an explicit stack of frames under +//! `iterate_until`, one frame for each construct the oracle reads by recursion +//! (an array, an object's members, each readable form of a term and a type, a +//! parameter, a list of type arguments), at the positions the schema gives, +//! with the oracle's nesting depths; values with no readable form are read by +//! the generic reader of `BS-24`. It admits no `recursion`. +//! +//! Each planted defect names the one vector that catches it. + +use std::collections::{BTreeMap, BTreeSet}; +use std::sync::OnceLock; + +use lexlean::artifact::canonical_json::Json; +use lexlean::calculus::Value; +use lexlean::surface::module::{locate, SEMANTIC}; +use lexlean::surface::semantic::{ + declaration_spans, parse_at, parse_module, print_at, schema_samples, Position, +}; +use lexlean::surface::SurfaceError; +use serde_json::Value as Json2; + +use super::rust_validate::{eligibility_of, rustc_differential}; +use super::{list, nat_val, pair, text, Run, Seeded}; +use crate::production_export as harness; +use crate::support::{self, repo_root}; + +const MODULE: &str = "LexLeanBoot.SemanticRead"; +const SOURCE: &str = "src/SemanticRead.lex.tex"; +const JSON_SOURCE: &str = "stdlib/src/Json.lex.tex"; +const SCHEMA: &str = "schemas/semantic-module-v2.schema.json"; +/// The schema tag a module read here is made under. +const SPEC: &str = "lexlean/semantic-module/2"; +const ROOTS: [(&str, &[&str]); 3] = [ + ("parseAt", &["allocation", "overflow"]), + ("parseSemanticModule", &["allocation", "overflow"]), + ("semanticSpans", &["allocation", "overflow"]), +]; + +/// The most a part of a certificate of a root may be, as the row states. +const CERTIFICATE_BUDGET: u64 = 3_145_728; +/// Committed modules whose notation is at most this many bytes are read and +/// spanned whole under the evaluator, and under rustc those at most the second. +const EVALUATOR_MODULE: usize = 1_200; +const RUSTC_MODULE: usize = 6_000; +/// Declarations of the committed modules printed and read back one at a time: +/// at most this many bytes of notation, one in every so many, under the +/// evaluator. +const EVALUATOR_DECLARATION: usize = 1_200; +const EVALUATOR_EVERY: usize = 25; +/// Under rustc, one declaration in every so many: each run carries the +/// schema, so the corpus is sampled to stay within the memory of a host. +const RUSTC_EVERY: usize = 12; +/// Seeded edits of printed declarations under the evaluator and under rustc. +const EVALUATOR_SEEDED: usize = 150; +const RUSTC_SEEDED: usize = 400; + +// ---------------------------------------------------------------- the sorts + +fn json_constructors() -> &'static Vec { + static CONSTRUCTORS: OnceLock> = OnceLock::new(); + CONSTRUCTORS.get_or_init(|| { + let source = std::fs::read_to_string(repo_root().join(JSON_SOURCE).as_std_path()) + .expect("the standard library's Json"); + let block = source + .split("inductive{") + .nth(1) + .and_then(|rest| rest.split("\n}\n").next()) + .expect("the first inductive"); + assert!(block.contains("name: Json,"), "the Json sort"); + block + .split("{name: ") + .skip(1) + .map(|rest| { + rest.split(',') + .next() + .expect("a constructor") + .trim() + .to_owned() + }) + .collect() + }) +} + +fn adt(constructor: &str, fields: Vec) -> Value { + let found = json_constructors() + .iter() + .position(|name| name == constructor) + .unwrap_or_else(|| panic!("`Json` has no constructor `{constructor}`")); + Value::Adt { + constructor: u64::try_from(found).expect("an index"), + fields, + } +} + +/// A value as the standard library's `Json`. +fn enc_json(json: &Json) -> Value { + match json { + Json::Bool(flag) => adt("JBool", vec![Value::Bool { value: *flag }]), + Json::Int(number) => adt( + "JInt", + vec![ + Value::Bool { value: *number < 0 }, + Value::Nat { + value: number.unsigned_abs().to_string(), + }, + ], + ), + Json::Str(value) => adt("JStr", vec![text(value)]), + Json::Arr(items) => adt( + "JArr", + vec![items + .iter() + .rev() + .fold(adt("JNil", Vec::new()), |rest, item| { + adt("JItem", vec![enc_json(item), rest]) + })], + ), + Json::Obj(members) => adt( + "JObj", + vec![members + .iter() + .rev() + .fold(adt("JNil", Vec::new()), |rest, (key, value)| { + adt("JMember", vec![text(key), enc_json(value), rest]) + })], + ), + } +} + +/// The schema the oracle reads, as the host gives it to the roots. +fn schema_value() -> &'static Value { + static SCHEMA_VALUE: OnceLock = OnceLock::new(); + SCHEMA_VALUE.get_or_init(|| { + let bytes = std::fs::read(repo_root().join(SCHEMA).as_std_path()).expect("the schema"); + let embedded = lexlean::embedded::FILES + .iter() + .find(|(path, _)| *path == SCHEMA) + .map(|(_, bytes)| *bytes) + .expect("the schema is embedded"); + assert_eq!(bytes, embedded, "the committed schema is the embedded one"); + enc_json(&Json::parse(&bytes).expect("the schema is canonical JSON's data model")) + }) +} + +fn position_code(position: Position) -> usize { + match position { + Position::Declaration => 0, + Position::Term => 1, + Position::Type => 2, + Position::Proof => 3, + } +} + +fn failure_value(failure: &SurfaceError) -> Value { + Value::Adt { + constructor: 0, + fields: vec![nat_val(failure.offset), text(&failure.message)], + } +} + +fn outcome( + result: &Result, + ok: impl Fn(&T) -> Value, +) -> (Value, Option) { + match result { + Ok(value) => ( + Value::Ok { + value: Box::new(ok(value)), + }, + None, + ), + Err(failure) => ( + Value::Error { + value: Box::new(failure_value(failure)), + }, + Some(failure.message.clone()), + ), + } +} + +fn at_run(source: &str, position: Position) -> (Run, Option) { + let (value, refusal) = outcome(&parse_at(source, position), enc_json); + ( + ( + vec![ + schema_value().clone(), + text(source), + nat_val(position_code(position)), + ], + serde_json::to_value(value).expect("a value is JSON"), + ), + refusal, + ) +} + +fn module_run(source: &str) -> (Run, Option) { + let (value, refusal) = outcome(&parse_module(source, SPEC), enc_json); + ( + ( + vec![schema_value().clone(), text(source), text(SPEC)], + serde_json::to_value(value).expect("a value is JSON"), + ), + refusal, + ) +} + +fn spans_run(source: &str) -> (Run, Option) { + let (value, refusal) = outcome(&declaration_spans(source), |spans| { + list( + spans + .iter() + .map(|(start, end)| pair(nat_val(*start), nat_val(*end))) + .collect(), + ) + }); + ( + ( + vec![schema_value().clone(), text(source)], + serde_json::to_value(value).expect("a value is JSON"), + ), + refusal, + ) +} + +// ------------------------------------------------------------------ vectors + +/// What a vector runs: a value at a position, a module, or the spans of one. +#[derive(Clone, Copy, PartialEq, Eq)] +enum Root { + At(Position), + Module, + Spans, +} + +/// One named vector: a text and the root it is read by. +struct Vector { + label: String, + source: String, + root: Root, +} + +impl Vector { + fn index(&self) -> usize { + match self.root { + Root::At(_) => 0, + Root::Module => 1, + Root::Spans => 2, + } + } + + fn run(&self) -> (Run, Option) { + match self.root { + Root::At(position) => at_run(&self.source, position), + Root::Module => module_run(&self.source), + Root::Spans => spans_run(&self.source), + } + } +} + +const TERMS: &[(&str, &str)] = &[ + ("a variable", "x"), + ("a string", "\"a\\nb\""), + ("true", "true"), + ("a numeral", "42"), + ("a numeral with a leading zero", "042"), + ("bytes", "0x4a4b"), + ("empty bytes", "0x"), + ("bytes of odd length", "0x4a4"), + ("bytes in capitals", "0x4A"), + ("a keyword as a term", "match"), + ("a word that is no term", "#"), + ("a delimiter as a term", "]"), + ("nothing as a term", ""), + ("text after a term", "x y"), + ("unit", "()"), + ("an integer", "(-3 : Int8)"), + ("an integer minus zero", "(-0 : Int)"), + ("an integer with a leading zero", "(07 : Int)"), + ("an integer of no integer type", "(3 : Nat)"), + ("an integer of no type", "(3 : )"), + ("a sum", "(a + 1)"), + ( + "each operator", + "((a = b) || ((a <= b) && ((a < b) <-> ((a =? b) /\\ ((a <=? b) -> (a q)"), + ("a pair", "(a, b)"), + ("a pair of variables", "(a, (b, c))"), + ("a call of two arguments", "(a b c)"), + ("an operand and a bad operator", "(1 * 2)"), + ("not, first and second", "(not (first (second p)))"), + ("a list", "(cons h (nil Nat))"), + ("an if", "(if c then a else b)"), + ("an if without then", "(if c a else b)"), + ("an if without else", "(if c then a b)"), + ("a let", "(let x : Nat := 1; x)"), + ("a let without its value", "(let x : Nat 1; x)"), + ("a let without its semicolon", "(let x : Nat := 1 x)"), + ( + "a let with a generic binder", + "(let {name: x, type: nat{}} := 1; x)", + ), + ("a let whose binder has no type", "(let x := 1; x)"), + ("a let whose binder is no name", "(let 1 : Nat := 1; x)"), + ("a forall", "(forall (n : Nat), (n = n))"), + ("a forall without its parenthesis", "(forall n : Nat, n)"), + ("a forall without its close", "(forall (n : Nat, n)"), + ("a forall without its comma", "(forall (n : Nat) n)"), + ("a function", "(fun (x : Nat) (y : Bool) => x)"), + ("a function with captures", "(fun [a, b] (x : Nat) => a)"), + ("a function without parameters", "(fun => x)"), + ("a function without its arrow", "(fun (x : Nat) x)"), + ( + "a function with an unclosed parameter", + "(fun (x : Nat => x)", + ), + ("a function capturing a numeral", "(fun [1] (x : Nat) => x)"), + ( + "a function with unclosed captures", + "(fun [a (x : Nat) => x)", + ), + ("an application", "(apply f a b)"), + ("an application without its close", "(apply f a"), + ("a projection", "(project r size)"), + ("a projection of no field", "(project r 1)"), + ("a call", "(f a b)"), + ("a call into a module", "(M::f a)"), + ("a call with a bad module path", "(M:: a)"), + ("a call with type arguments", "(f [Nat, Bool] a)"), + ("a call with no type arguments", "(f [] a)"), + ("a call with unclosed type arguments", "(f [Nat a)"), + ("a constructor", "(ctor Option.some [Nat] 1)"), + ("a constructor of no name", "(ctor 1)"), + ("a function reference", "(ref f [Nat])"), + ("a bare function reference", "(ref M::f)"), + ("a record", "(record R [Nat] {a := 1, b := ()})"), + ("a record with a trailing comma", "(record R {a := 1,})"), + ("a record without fields", "(record R {})"), + ("a record without its brace", "(record R a := 1)"), + ("a record field without its assignment", "(record R {a 1})"), + ("a record of no field name", "(record R {1 := 1})"), + ("a record not closed", "(record R {a := 1 b := 2})"), + ("a primitive", "(prim append a b : Bytes)"), + ("a primitive without its result", "(prim append a)"), + ("a primitive at the end", "(prim append a"), + ("a primitive of no operation", "(prim 1 : Nat)"), + ( + "a checked application", + "(checked M [Nat] a b with [c1, c2])", + ), + ("a checked application without with", "(checked M a)"), + ( + "a checked application without its checks", + "(checked M a with c)", + ), + ( + "a checked application of a numeral check", + "(checked M a with [1])", + ), + ( + "a match", + "(match o | Option.some v => v | Option.none => 0)", + ), + ("a match without branches", "(match o)"), + ("a match with a numeral binder", "(match o | C 1 => x)"), + ("a match without its arrow", "(match o | C x"), + ( + "a match written generically", + "(match{scrutinee: x, branches: []} + 1)", + ), + ("a tagged term", "var{name: if}"), + ("a tagged term with a member twice", "var{name: x, name: y}"), + ("a tagged term without a member name", "var{1: x}"), + ("a tagged term without a colon", "var{name x}"), + ("a tagged term not closed", "var{name: x y: z}"), + ( + "a member written generically", + "call{function: {name: f}, arguments: []}", + ), + ("a term deeper than the notation reads", ""), + ("a term at the limit of the notation", ""), + ("a string that is not closed", "\"abc"), +]; + +const TYPES: &[(&str, &str)] = &[ + ("the type Nat", "Nat"), + ("every nullary type", "(Fn (Bool Bytes Int Int8 Int16 Int32 Int64 Nat Ordering Prop String Type UInt8 UInt16 UInt32 UInt64 Unit ContractViolation ReasoningFailure) Unit)"), + ("a type parameter", "'T"), + ("a type parameter that is no name", "'1"), + ("a head without arguments", "List"), + ("a named type", "Doc"), + ("a named type of a module", "M::Doc"), + ("a named type with arguments", "(Pair Nat 'T)"), + ("a list, a set and an option", "((List Nat) * ((Set Bool) * (Option Nat)))"), + ("a result type", "(Result Nat String)"), + ("a map type", "(Map String Bytes)"), + ("a function type", "(Fn (Nat Bool) Nat)"), + ("a function type without its parameters", "(Fn Nat Nat)"), + ("a product of named types", "(Doc * M::Doc)"), + ("a product without its star", "(Nat Nat)"), + ("a type not closed", "(List Nat"), + ("a delimiter as a type", ","), + ("nothing as a type", ""), + ("a tagged type", "nat{}"), + ("a type deeper than the notation reads", ""), +]; + +/// `(not ` `depth` times around `x`, closed. +fn nested_term(depth: usize) -> String { + format!("{}x{}", "(not ".repeat(depth), ")".repeat(depth)) +} + +fn named() -> Vec { + let mut out = Vec::new(); + for (label, source) in TERMS { + let source = match *label { + "a term deeper than the notation reads" => nested_term(300), + "a term at the limit of the notation" => nested_term(255), + _ => (*source).to_owned(), + }; + out.push(Vector { + label: (*label).to_owned(), + source, + root: Root::At(Position::Term), + }); + } + for (label, source) in TYPES { + let source = if *label == "a type deeper than the notation reads" { + format!("{}Nat{}", "(List ".repeat(300), ")".repeat(300)) + } else { + (*source).to_owned() + }; + out.push(Vector { + label: (*label).to_owned(), + source, + root: Root::At(Position::Type), + }); + } + for (label, source) in [ + ("a module and `::` without a name", "(List M::)"), + ("a module and `::` at the end", "M::"), + ] { + out.push(Vector { + label: label.to_owned(), + source: source.to_owned(), + root: Root::At(Position::Type), + }); + } + let declaration = + "definition{name: f, parameters: [x : Nat], result: Nat, body: x, executable: true}"; + for (label, source, root) in [ + ( + "a declaration", + declaration.to_owned(), + Root::At(Position::Declaration), + ), + ( + "a parameter written generically", + "definition{name: f, parameters: [{name: x, type: Nat}], result: Nat, body: x}" + .to_owned(), + Root::At(Position::Declaration), + ), + ( + "a declaration with an array of items", + "definition{name: f, parameters: [x : Nat, y : Bool,], result: Nat, body: x}" + .to_owned(), + Root::At(Position::Declaration), + ), + ( + "an array not closed", + "definition{name: f, parameters: [x : Nat y : Bool]}".to_owned(), + Root::At(Position::Declaration), + ), + ( + "a declaration without a kind", + "{name: f}".to_owned(), + Root::At(Position::Declaration), + ), + ( + "a declaration of a big integer", + "definition{name: f, size: 99999999999999999999}".to_owned(), + Root::At(Position::Declaration), + ), + ( + "a declaration of a word that is no name", + "definition{name: 1x}".to_owned(), + Root::At(Position::Declaration), + ), + ( + "a declaration of an empty value", + "definition{name: )}".to_owned(), + Root::At(Position::Declaration), + ), + ( + "a declaration cut short", + "definition{name:".to_owned(), + Root::At(Position::Declaration), + ), + ( + "a parameter without its colon", + "definition{name: f, parameters: [x Nat]}".to_owned(), + Root::At(Position::Declaration), + ), + ( + "a parameter of no name", + "definition{name: f, parameters: [1 : Nat]}".to_owned(), + Root::At(Position::Declaration), + ), + ("a proof", "decide{}".to_owned(), Root::At(Position::Proof)), + ( + "a term at the proof position", + "x".to_owned(), + Root::At(Position::Proof), + ), + ( + "two declarations", + format!("{declaration}\n\n{declaration}\n"), + Root::Module, + ), + ("no declarations", " \n".to_owned(), Root::Module), + ( + "a module with a lexical error", + "definition{name: \"f}".to_owned(), + Root::Module, + ), + ( + "a module with a bad declaration", + format!("{declaration}\nx"), + Root::Module, + ), + ( + "two declarations spanned", + format!("{declaration}\n\n {declaration} \n"), + Root::Spans, + ), + ( + "a declaration after white space spanned", + format!("\n\n {declaration}"), + Root::Spans, + ), + ("nothing spanned", String::new(), Root::Spans), + ( + "a bad declaration spanned", + format!("{declaration} ]"), + Root::Spans, + ), + ] { + out.push(Vector { + label: label.to_owned(), + source, + root, + }); + } + // A union may list one kind twice (an `enum` alternative beside a + // `const` one); the second sample is labelled by its place. + let mut seen = BTreeSet::new(); + for (position, label, value) in schema_samples() { + let mut label = format!("the schema sample {label}"); + let mut place = 2; + while !seen.insert(label.clone()) { + label = format!("{label} ({place})"); + place += 1; + } + out.push(Vector { + label, + source: print_at(&value, position), + root: Root::At(position), + }); + } + out +} + +/// The refusals a vector must reach, each by a part of its message. +const REFUSALS: &[&str] = &[ + "nested more deeply than the notation reads", + "expected a member name", + "expected `:` after a member name", + "is written twice", + "expected `,` or `}`", + "expected `,` or `]`", + "does not fit a 64-bit integer", + "is not a name; quote it to write a string", + "expected a value, found", + "expected a name, found", + "expected a name after `::`, found", + "expected a parameter name, found", + "expected a field name, found", + "expected a primitive operation, found", + "expected a binder name, found", + "expected an integer type, found", + "expected a check, found", + "expected `:` after a parameter name", + "expected a type, found", + "takes arguments: write", + "expected `(` before the parameter types", + "expected `*` between the two types of a product", + "expected `)`", + "has a leading zero", + "a byte string is `0x`", + "is a keyword; write `var{name:", + "is not a term", + "expected a term, found", + "is not an integer type", + "is not a canonical integer", + "expected `then`", + "expected `else`", + "expected `:=`", + "expected `;`", + "expected `(` before the bound variable", + "expected `,` before the body", + "a function takes at least one parameter", + "expected `=>`", + "expected `{` before the fields", + "expected `:=` after a field name", + "expected `:` and the result type", + "a match has at least one `| …` branch", + "expected an operator or `,` after the first operand", + "expected `[`", + "unexpected text after the value", + "unterminated string", +]; + +/// The committed semantic modules written in the notation: their bodies. +fn committed() -> Vec<(String, String)> { + let mut out = Vec::new(); + for (path, content) in super::scan::sources() { + let Some(found) = locate(&content) else { + continue; + }; + if !found.kind.is(&SEMANTIC) || found.data.is_some() { + continue; + } + out.push((path, content[found.body.0..found.body.1].to_owned())); + } + out +} + +// ------------------------------------------------------------------- seeded + +const PIECES: &[&str] = &[ + "(", ")", "[", "]", "{", "}", ",", ":", ":=", "::", ";", " ", "x", "Nat", "if", "then", + "match", "|", "=>", "fun", "let", "*", "+", "->", "\"s\"", "0x", "01", "-0", "nat{}", "List", +]; + +fn edited(seeded: &mut Seeded, source: &str) -> String { + let mut scalars: Vec = source.chars().collect(); + for _ in 0..=seeded.below(2) { + let at = seeded.below(scalars.len() + 1); + let piece: Vec = seeded.pick(PIECES).chars().collect(); + match seeded.below(3) { + 0 if at < scalars.len() => { + let to = (at + 1 + seeded.below(4)).min(scalars.len()); + drop(scalars.drain(at..to)); + } + 1 => drop(scalars.splice(at..at, piece)), + _ if at < scalars.len() => drop(scalars.splice(at..=at, piece)), + _ => scalars.extend(piece), + } + } + scalars.into_iter().collect() +} + +fn seeded_runs(count: usize, bases: &[String], seed: u64) -> Vec { + let mut seeded = Seeded(seed); + (0..count) + .map(|_| { + let base = seeded.pick(bases).clone(); + at_run(&edited(&mut seeded, &base), Position::Declaration).0 + }) + .collect() +} + +// ------------------------------------------------------------------- plants + +struct Plant { + label: &'static str, + site: &'static str, + replacement: &'static str, + vector: &'static str, +} + +const PLANTS: &[Plant] = &[ + Plant { + label: "terms nest past the bound", + site: "(if (256 (defNamed schema (lit \"parameter\"))", + replacement: "| Pos.PParam => (ctor Option.none [LexLeanStd.Json::Json])", + vector: "a parameter written generically", + }, + Plant { + label: "array items are at no position", + site: "| Pos.PNode node => (itemsOf schema node)", + replacement: "| Pos.PNode node => (ctor Pos.PAny)", + vector: "a declaration", + }, + Plant { + label: "a trailing comma in an array is refused", + site: "(if ((code toks (position + 1)) =? 3) then (give st (position + 2) (arr (project collected values)))", + replacement: "(if false then (give st (position + 2) (arr (project collected values)))", + vector: "a declaration with an array of items", + }, + Plant { + label: "a reference to a term is no term", + site: "(if (same segment (lit \"term\")) then (ctor Pos.PTerm)", + replacement: "(if (same segment (lit \"term\")) then (ctor Pos.PAny)", + vector: "a declaration", + }, + Plant { + label: "a member written generically is read deeper", + site: "(startGeneric toks size st 0)", + replacement: "(startGeneric toks size st 257)", + vector: "a member written generically", + }, + Plant { + label: "Nat reads as int", + site: "(if (same word (lit \"Nat\")) then (ctor Option.some [Bytes] (lit \"nat\"))", + replacement: "(if (same word (lit \"Nat\")) then (ctor Option.some [Bytes] (lit \"int\"))", + vector: "the type Nat", + }, + Plant { + label: "a head needs no arguments", + site: "(if (listed word (heads)) then", + replacement: "(if false then", + vector: "a head without arguments", + }, + Plant { + label: "a type parameter is a named type", + site: "(both (LexLeanStd.Text::startsWith word (lit \"'\")) (SurfaceGeneric::isBare (tailFrom word 1)))", + replacement: "false", + vector: "a type parameter", + }, + Plant { + label: "a product needs no star", + site: "(if (wordIs toks position (lit \"*\")) then", + replacement: "(if true then", + vector: "a product without its star", + }, + Plant { + label: "the parameter types begin at their parenthesis", + site: "(fnParams toks size st (frame 14 depth) (position + 2))", + replacement: "(fnParams toks size st (frame 14 depth) (position + 1))", + vector: "a function type", + }, + Plant { + label: "a named type may be the left of a product", + site: "(let infixAfter : Bool := (wordIs toks (position + after) (lit \"*\"));", + replacement: "(let infixAfter : Bool := false;", + vector: "a product of named types", + }, + Plant { + label: "ok and error are swapped", + site: "(kindNode (lit \"result\") (two \"ok\" (project top held) \"error\" value))", + replacement: "(kindNode (lit \"result\") (two \"error\" (project top held) \"ok\" value))", + vector: "a result type", + }, + Plant { + label: "a leading zero is a numeral", + site: "(if (both (1 (haltAt toks size st position (lit \"a match has at least one `| …` branch\"))", + replacement: "| List.nil => (give st position (kindNode (lit \"match\") (nil (Bytes * LexLeanStd.Json::Json))))", + vector: "a match without branches", + }, + Plant { + label: "a record field needs no comma", + site: "(if ((code toks position) =? 1) then (recordNext toks size st assigned position)", + replacement: "(if true then (recordNext toks size st assigned position)", + vector: "a record not closed", + }, + Plant { + label: "a primitive may end at its close", + site: "(if (if (token =? 99) then true else (token =? 5)) then", + replacement: "(if (token =? 99) then", + vector: "a primitive without its result", + }, + Plant { + label: "a checked application reads to its close", + site: "(if (same keyword (lit \"checked\")) then (lit \"with\") else (lit \"\"))", + replacement: "(lit \"\")", + vector: "a checked application", + }, + Plant { + label: "an unclosed application is refused at the start", + site: "(halt st size (lit \"expected `)`\"))", + replacement: "(halt st 0 (lit \"expected `)`\"))", + vector: "an application without its close", + }, + Plant { + label: "an implication has a left and a right", + site: "(if implication then (lit \"premise\") else (lit \"left\"))", + replacement: "(lit \"left\")", + vector: "an implication", + }, + Plant { + label: "the module of a member is dropped", + site: "(objectOf (two \"module\" (project head value) \"name\" (project name value)))", + replacement: "(objectOf (one \"name\" (project name value)))", + vector: "a call into a module", + }, + Plant { + label: "a span begins at the previous end", + site: "(push st (worded collected (lit \"\") (SurfaceGeneric::offsetAt toks size position)) position 0 (project top place) 1)", + replacement: "(push st (worded collected (lit \"\") (lastEnd toks position)) position 0 (project top place) 1)", + vector: "two declarations spanned", + }, + Plant { + label: "a span ends at the next token", + site: "((project top offset), (lastEnd toks position))", + replacement: "((project top offset), (SurfaceGeneric::offsetAt toks size position))", + vector: "two declarations spanned", + }, + Plant { + label: "the first span begins at zero", + site: "(SurfaceGeneric::offsetAt toks size 0)", + replacement: "0", + vector: "a declaration after white space spanned", + }, + Plant { + label: "empty type arguments are read as none", + site: "(if ((code toks position) =? 3) then (give st (position + 1) (arr (project top values))) else (wantType st top position))", + replacement: "(wantType st top position)", + vector: "a call with no type arguments", + }, +]; + +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)); + let planted = support::P::bootstrap(); + let source = planted.read_raw(SOURCE); + assert_eq!( + source.matches(plant.site).count(), + 1, + "{}: the plant site occurs once", + plant.label + ); + planted.write(SOURCE, &source.replacen(plant.site, plant.replacement, 1)); + let checked = support::checked_project(&planted); + let low = super::lower(&checked, &planted, MODULE, ROOTS[vector.index()].0); + match super::evaluate(&low, &[vector.run().0], 1).first() { + Some(first) => eprintln!( + "BS-37: 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-37: {}: 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" + ); + } +} + +#[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("SemanticRead"), + "SemanticRead is a linked module of the project" + ); + eligibility_of(&project, "SemanticRead", MODULE, &ROOTS); + + let vectors = named(); + let labels: BTreeSet<&str> = vectors.iter().map(|vector| vector.label.as_str()).collect(); + assert_eq!(labels.len(), vectors.len(), "each vector has its own label"); + let mut runs: [Vec; 3] = [Vec::new(), Vec::new(), Vec::new()]; + let mut messages = Vec::new(); + for vector in &vectors { + let (run, refusal) = vector.run(); + messages.extend(refusal); + runs[vector.index()].push(run); + } + for refusal in REFUSALS { + assert!( + messages.iter().any(|message| message.contains(refusal)), + "no vector reaches the refusal `{refusal}`" + ); + } + + // The committed modules read and spanned whole, and each declaration + // printed and read back at the declaration position. + assert_eq!( + lexlean::ir::semantic::semantic_module_spec(lexlean::LANGUAGE_1_2), + Some(SPEC), + "the schema tag of a language-1.2 semantic module" + ); + let modules = committed(); + assert!( + modules.len() >= 50, + "the committed semantic modules in the notation are {}", + modules.len() + ); + let mut evaluator: [Vec; 3] = [Vec::new(), Vec::new(), Vec::new()]; + let mut compiled: [Vec; 3] = [Vec::new(), Vec::new(), Vec::new()]; + let mut bases: Vec = Vec::new(); + let mut declarations = 0usize; + for (path, body) in &modules { + let module = parse_module(body, SPEC).unwrap_or_else(|error| panic!("{path}: {error:?}")); + if body.len() <= EVALUATOR_MODULE { + evaluator[1].push(module_run(body).0); + evaluator[2].push(spans_run(body).0); + } + if body.len() <= RUSTC_MODULE { + compiled[1].push(module_run(body).0); + compiled[2].push(spans_run(body).0); + } + let Json::Obj(members) = &module else { + panic!("{path}: a module is an object"); + }; + let Some(Json::Arr(items)) = members.get("declarations") else { + panic!("{path}: a module has declarations"); + }; + for (index, declaration) in items.iter().enumerate() { + let printed = print_at(declaration, Position::Declaration); + declarations += 1; + if printed.len() <= EVALUATOR_DECLARATION + && (index + path.len()).is_multiple_of(EVALUATOR_EVERY) + { + evaluator[0].push(at_run(&printed, Position::Declaration).0); + bases.push(printed.clone()); + } + if printed.len() <= RUSTC_MODULE && (index + path.len()).is_multiple_of(RUSTC_EVERY) { + compiled[0].push(at_run(&printed, Position::Declaration).0); + } + } + } + let normative = support::normative_host(); + let (evaluator_seeded, rustc_seeded) = if normative { + ( + seeded_runs(EVALUATOR_SEEDED, &bases, 0x0B55_0037), + seeded_runs(RUSTC_SEEDED, &bases, 0x0B55_1037), + ) + } else { + (Vec::new(), Vec::new()) + }; + eprintln!( + "BS-37: {} vectors ({} at a position, {} modules, {} spanned), {} committed modules with {} declarations ({} and {} declarations, {} and {} modules under the evaluator and rustc), {} and {} seeded edits ({:?})", + vectors.len(), + runs[0].len(), + runs[1].len(), + runs[2].len(), + modules.len(), + declarations, + evaluator[0].len(), + compiled[0].len(), + evaluator[1].len(), + compiled[1].len(), + evaluator_seeded.len(), + rustc_seeded.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(); + if normative { + for (index, (root, _)) in ROOTS.iter().enumerate() { + let mut corpus: Vec = runs[index].clone(); + corpus.extend(evaluator[index].iter().cloned()); + if index == 0 { + corpus.extend(evaluator_seeded.iter().cloned()); + } + let disagreements = super::evaluate(&lowered[root], &corpus, 5); + assert!( + disagreements.is_empty(), + "{root} and the Rust oracle disagree: {disagreements:?}" + ); + eprintln!( + "BS-37: {root}: the evaluator agrees on {} runs ({:?})", + corpus.len(), + started.elapsed() + ); + } + plants(&vectors); + } else { + eprintln!("BS-37: platform-bound host: the evaluator, the seeded runs, and the planted defects run on the normative host"); + } + + let mut all: [Vec; 3] = [runs[0].clone(), runs[1].clone(), runs[2].clone()]; + for (sink, more) in all.iter_mut().zip(&compiled) { + sink.extend(more.iter().cloned()); + } + all[0].extend(rustc_seeded); + let total = rustc_differential( + "BS-37", + &[ + (ROOTS[0].0, &lowered[ROOTS[0].0], all[0].as_slice()), + (ROOTS[1].0, &lowered[ROOTS[1].0], all[1].as_slice()), + (ROOTS[2].0, &lowered[ROOTS[2].0], all[2].as_slice()), + ], + "oracle", + ); + eprintln!("BS-37: {total} rustc runs agree ({:?})", started.elapsed()); + + if !support::lean_backed("BS-37") { + return; + } + export(&lowered); + eprintln!("BS-37: passed in {:?}", started.elapsed()); +} diff --git a/crates/conformance/src/cases/repository.rs b/crates/conformance/src/cases/repository.rs index 07acbe46eb..96819c44e7 100644 --- a/crates/conformance/src/cases/repository.rs +++ b/crates/conformance/src/cases/repository.rs @@ -1091,7 +1091,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(), 504, "§31 has 504 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..5d8dbc979c 100644 --- a/crates/conformance/tests/conformance.rs +++ b/crates/conformance/tests/conformance.rs @@ -2410,6 +2410,46 @@ fn conformance_bs_34() { repo_conformance::cases::run("BS-34"); } +#[test] +fn conformance_bs_35() { + repo_conformance::cases::run("BS-35"); +} + +#[test] +fn conformance_bs_36() { + repo_conformance::cases::run("BS-36"); +} + +#[test] +fn conformance_bs_37() { + repo_conformance::cases::run("BS-37"); +} + +#[test] +fn conformance_bs_38() { + repo_conformance::cases::run("BS-38"); +} + +#[test] +fn conformance_bs_39() { + repo_conformance::cases::run("BS-39"); +} + +#[test] +fn conformance_bs_40() { + repo_conformance::cases::run("BS-40"); +} + +#[test] +fn conformance_bs_42() { + repo_conformance::cases::run("BS-42"); +} + +#[test] +fn conformance_bs_43() { + repo_conformance::cases::run("BS-43"); +} + #[test] fn conformance_pk_01() { repo_conformance::cases::run("PK-01"); diff --git a/crates/lexlean/src/surface/core.rs b/crates/lexlean/src/surface/core.rs index e7c236aafa..4a363f8bd0 100644 --- a/crates/lexlean/src/surface/core.rs +++ b/crates/lexlean/src/surface/core.rs @@ -1715,9 +1715,12 @@ impl<'a> Parser<'a> { }) } - /// A declaration whose roots are known: the statements that carry a + /// A declaration's members with its roots: the statements that carry a /// person's names have all been read. - fn finish(&self, pending: Pending) -> PResult { + fn resolve_roots( + &self, + pending: Pending, + ) -> PResult<(std::collections::BTreeMap, usize)> { let mut object = pending.object; let resolve = |root: Root| -> PResult { match root { @@ -1735,8 +1738,7 @@ impl<'a> Parser<'a> { if let Some(value) = pending.value { object.insert("value".to_owned(), Json::from_usize(resolve(value)?)); } - serde_json::from_str(&Json::Obj(object).to_canonical_string()) - .map_err(|failure| error(pending.start, format!("not a declaration: {failure}"))) + Ok((object, pending.start)) } /// A node a declaration states: `@N`, or the name of a statement. @@ -1817,16 +1819,32 @@ struct Pending { start: usize, } -/// Read the notation of a core module, and the byte range of each of its -/// declarations in the text. -/// -/// # Errors -/// -/// Text that is not the notation of a module, with the byte offset of the -/// first thing that is not. -pub fn parse_module_with_spans( - text: &str, -) -> Result<(CoreModule, Vec<(usize, usize)>), SurfaceError> { +/// A declaration's members with its roots resolved and the offset of its +/// first token, or the error of a root no statement is named. +#[doc(hidden)] +pub type ReadDeclaration = Result<(std::collections::BTreeMap, usize), SurfaceError>; + +/// A module read up to the decoding of its declarations: each declaration's +/// members with its roots resolved and the offset of its first token, or the +/// error of a root no statement is named. `BS-40`'s case holds the bootstrap +/// reader to it; `parse_module_with_spans` decodes it. +#[doc(hidden)] +pub struct ReadModule { + /// The imports. + pub imports: Vec, + /// The node table. + pub nodes: Vec, + /// The proof nodes, in the order they were read. + pub proof_nodes: Vec, + /// Each declaration, before serde decodes it. + pub declarations: Vec, + /// The byte range of each declaration. + pub spans: Vec<(usize, usize)>, +} + +/// The notation read up to the decoding of its declarations (`BS-40`'s seam). +#[doc(hidden)] +pub fn read_module(text: &str) -> Result { let mut parser = Parser::new(text); let imports = parser.imports()?; let mut pending = Vec::new(); @@ -1850,19 +1868,47 @@ pub fn parse_module_with_spans( spans.push((token.start, parser.last_end)); } } - let mut declarations = Vec::with_capacity(pending.len()); - for declaration in pending { - declarations.push(parser.finish(declaration)?); + let declarations = pending + .into_iter() + .map(|declaration| parser.resolve_roots(declaration)) + .collect(); + Ok(ReadModule { + imports, + nodes: parser.nodes, + proof_nodes: parser.proof, + declarations, + spans, + }) +} + +/// Read the notation of a core module, and the byte range of each of its +/// declarations in the text. +/// +/// # Errors +/// +/// Text that is not the notation of a module, with the byte offset of the +/// first thing that is not. +pub fn parse_module_with_spans( + text: &str, +) -> Result<(CoreModule, Vec<(usize, usize)>), SurfaceError> { + let read = read_module(text)?; + let mut declarations = Vec::with_capacity(read.declarations.len()); + for declaration in read.declarations { + let (object, start) = declaration?; + declarations.push( + serde_json::from_str(&Json::Obj(object).to_canonical_string()) + .map_err(|failure| error(start, format!("not a declaration: {failure}")))?, + ); } Ok(( CoreModule { spec: CORE_SPEC_2.to_owned(), - imports, - nodes: parser.nodes, - proof_nodes: parser.proof, + imports: read.imports, + nodes: read.nodes, + proof_nodes: read.proof_nodes, declarations, }, - spans, + read.spans, )) } diff --git a/features/suites/bootstrap.feature b/features/suites/bootstrap.feature index 13e468aa52..28e683643b 100644 --- a/features/suites/bootstrap.feature +++ b/features/suites/bootstrap.feature @@ -208,3 +208,51 @@ 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-35 @build + Scenario: The text channel and the simple proof sentences of SPEC.md §15.5 and §16.2 (`grammar::chart::text_tokens`: an atom range with its white space dropped and each mathematical island, from `\(` or `\[` to the first closing control of its kind, folded into one token, or the first refusal in the range: a dollar delimiter (`LLP2001`), an island opened inside another, an island without its close, and a close that closes none (`LLL1006`); and `grammar::proof::parse_proof_sentence`: the tokens of one sentence read as one of the seven registered simple sentences, with the keyword occurrences that cover it, or `LLF5005` at the first token that composes a forbidden proof word with the byte-adjacent atoms after it or begins the sentence with an underscore, and otherwise at the sentence's first atom) is language-1.2 source in the `bootstrap/` project, as two production roots of `GrammarProof` over the atoms of `BS-03`: `textTokens` and `parseProofSentence`, each eligible for `rust-std` with exactly its declared effects, none of them `recursion`, 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 the hand-built vectors, which reach every refusal of the two, every registered sentence, and every forbidden word, on seeded atom ranges of every committed `.lex.tex` source, and on seeded sentences and seeded edits of the registered ones; 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; the glossary forms, the proposition grammar, and the elaboration of a sentence are outside the claim. + Given the committed `bootstrap` project, hand-built sentences and atom ranges that reach every refusal, every registered sentence, and every forbidden word, seeded atom ranges of every committed `.lex.tex` source, and seeded sentences and edits + When the project is checked and built, and the lowered program and the rendered package of each root are run on those ranges and sentences + Then each root is eligible for rust-std with exactly its declared effects, no certificate part is beyond 3,145,728 bytes, and `textTokens` and `parseProofSentence` equal `text_tokens` and `parse_proof_sentence` 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 + @BS-36 @build + Scenario: The printer of the authoring notation of a semantic module of SPEC.md §17.21 (`surface::semantic::print_at`: a value at the position of a declaration, a term, a type, or a proof, found by walking the semantic-module schema, written in the readable form of a term, a type, a member, or a parameter when that form carries the value exactly and otherwise in the generic notation tagged by its `kind`, with each member at the position the schema gives it, and laid out at 100 columns; and `surface::semantic::print_module`: the declarations of a module so written, each ended by a line feed and separated by an empty line, or the error of a value that is not a module) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticPrint` over the layout algebra of `BS-23`, the generic notation of `BS-24`, and the standard library's `Json`, with the schema as the host's input: `printAt` and `printSemanticModule`, each eligible for `rust-std` with exactly its declared effects, which admit `recursion` as the printer of `BS-24` does, 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 the hand-built vectors, which reach every readable form and each condition that writes a value generically, every error of a module, and the schema's sample of every alternative of the declaration, term, type, and proof unions, on every declaration and every module of the committed semantic modules written in the notation, and on seeded edits of their declarations; 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; the reader of the notation and the decoding of a module are outside the claim. + Given the committed `bootstrap` project and the semantic-module schema, hand-built values that reach every readable form and each condition that writes a value generically, the schema's sample of every alternative, the committed semantic modules written in the notation, and seeded edits of their declarations + When the project is checked and built, and the lowered program and the rendered package of each root are run on those values and modules + Then each root is eligible for rust-std with exactly its declared effects, no certificate part is beyond 3,145,728 bytes, and `printAt` and `printSemanticModule` equal `print_at` and `print_module` 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 + @BS-37 @build + Scenario: The reader of the authoring notation of a semantic module of SPEC.md §17.21 (`surface::semantic::parse_at`: a text lexed by the lexer of the notation and read as exactly one value at the position of a declaration, a term, a type, or a proof, found by walking the semantic-module schema, in the readable form of a term, a type, a member, or a parameter, or in the generic notation tagged by its `kind` with each member at the position the schema gives it, with the nesting the notation bounds at 256, or the offset and message of the first error; `surface::semantic::parse_module`: the declarations of a text read one after the other and made a module under its schema tag; and `surface::semantic::declaration_spans`: the byte range of each declaration so read) is language-1.2 source in the `bootstrap/` project, as three production roots of `SemanticRead` over the lexer of `BS-22`, the generic notation of `BS-24`, and the standard library's `Json`, with the schema as the host's input: `parseAt`, `parseSemanticModule`, and `semanticSpans`, each eligible for `rust-std` with exactly its declared effects, none of them `recursion`, 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 the hand-built vectors, which reach every readable form, every error of the reader, and the limit on nesting, on the schema's sample of every alternative of the declaration, term, type, and proof unions printed and read back, on every committed semantic module written in the notation read and spanned and each of its declarations printed and read back, and on seeded edits of the printed declarations; 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; the printer of the notation and the decoding of a module are outside the claim. + Given the committed `bootstrap` project and the semantic-module schema, hand-built texts that reach every readable form, every error of the reader, and the limit on nesting, the schema's sample of every alternative printed, the committed semantic modules written in the notation and their declarations printed, and seeded edits of the printed declarations + When the project is checked and built, and the lowered program and the rendered package of each root are run on those texts + Then each root is eligible for rust-std with exactly its declared effects, no certificate part is beyond 3,145,728 bytes, and `parseAt`, `parseSemanticModule`, and `semanticSpans` equal `parse_at`, `parse_module`, and `declaration_spans` 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 + @BS-38 @build + Scenario: The canonical JSON of a native core module of SPEC.md §17.10 and §21.7 (`surface::core::canonical_json`: the module's declarations as the values they serialize to, its imports, its node table with each node's members in byte order of key, its proof nodes, and its schema tag, written with object keys in byte order and strings escaped as canonical JSON escapes them) is language-1.2 source in the `bootstrap/` project, as the production root `coreJson` of `CoreJson`, which takes the module as first-order data (its kernel terms, levels, binders, and annotations as inductives of the module, and each declaration as the standard library's `Json`), eligible for `rust-std` with exactly its declared effects, which admit `recursion` as the standard library's writer does, 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 the hand-built modules, which reach every kind of node, level, binder, and annotation and strings that need escaping, on the committed core modules written in the notation, and on seeded edits of them; 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; the serialization of a declaration to its value and the validation of a module are outside the claim. + Given the committed `bootstrap` project, hand-built core modules that reach every kind of node, level, binder, and annotation and strings that need escaping, the committed core modules written in the notation, and seeded edits of them + When the project is checked and built, and the lowered program and the rendered package of the root are run on those modules + Then the root is eligible for rust-std with exactly its declared effects, no certificate part is beyond 3,145,728 bytes, and `coreJson` equals `canonical_json` on every run + And every planted defect is detected by its own vector, and the exported package is byte-identical to the committed package and realizes the program the differential ran + @BS-39 @build + Scenario: The printer of the notation of a native core module of SPEC.md §17.21.8 (`surface::core::print_module`: a module of the schema `lexlean/core-module/2` whose edges point backward and whose proof nodes and declaration roots are nodes of it, written as its imports, its declarations with their members in the generic notation, and its statements, a node written inside the node that uses it exactly when it is used once, ends right before that node's other written-inside children, and is less than 48 deep, its bound variables written by the name of their binder when no nearer binder has that name and otherwise by index, a constant a binder in scope is named like written `const`, and each statement laid out at 100 columns; or the error of a module the notation does not carry, in the oracle's order) is language-1.2 source in the `bootstrap/` project, as the production root `printCoreModule` of `CorePrint` over the module types of `BS-38`, the layout algebra of `BS-23`, and the generic notation of `BS-24`, eligible for `rust-std` with exactly its declared effects, which admit `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 the hand-built modules, which reach every error and every kind of node, level, binder, annotation, and name, the inlining bound, and lines beyond the width, on the committed core modules written in the notation, and on seeded edits of them; 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; the reader of the notation and the decoding of a module are outside the claim. + Given the committed `bootstrap` project, hand-built core modules that reach every error and every kind of node, level, binder, annotation, and name, the inlining bound, and lines beyond the width, the committed core modules written in the notation, and seeded edits of them + When the project is checked and built, and the lowered program and the rendered package of the root are run on those modules + Then the root is eligible for rust-std with exactly its declared effects, no certificate part is beyond 3,145,728 bytes, and `printCoreModule` equals `print_module` on every run + And every planted defect is detected by its own vector, and the exported package is byte-identical to the committed package and realizes the program the differential ran + @BS-40 @build + Scenario: The reader of the notation of a native core module of SPEC.md §17.21.8 up to the decoding of its declarations (`surface::core::read_module`, the reader of `surface::core::parse_module`: a text read as its imports, its declarations with their members in the generic notation and their kind, name, universe parameters, and roots, and its statements, each numbering its nodes in the order they finish and checking its label, with names resolved to the nearest binder in scope, a nesting depth of at most 256 and a level raised by at most 1024, the byte range of each declaration, and the roots a statement's name states resolved; or the offset and message of the first error, in the order the oracle finds them) is language-1.2 source in the `bootstrap/` project, as the production root `readCoreModule` of `CoreRead` over the module types of `BS-38`, the string reader of `BS-22`, and the generic notation of `BS-24`, eligible for `rust-std` with exactly its declared effects, none of them `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 the hand-built texts, which reach every error of the reader and every form of the notation, on the committed core modules written in the notation, and on seeded edits of them; 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; the decoding of a declaration's members into a declaration and the validation of a module are outside the claim. + Given the committed `bootstrap` project, hand-built texts that reach every error of the reader and every form of the notation, the committed core modules written in the notation, and seeded edits of them + When the project is checked and built, and the lowered program and the rendered package of the root are run on those texts + Then the root is eligible for rust-std with exactly its declared effects, no certificate part is beyond 3,145,728 bytes, and `readCoreModule` equals `read_module` on every run + And every planted defect is detected by its own vector, and the exported package is byte-identical to the committed package and realizes the program the differential ran + @BS-42 @build + Scenario: The grammar of a mathematical island of SPEC.md §15.5 (`grammar::math::parse_math`: an island's atom range parsed by a Pratt parser over the declared precedence scale, with explicit call syntax, grouping parentheses, `\lexeme` and `\reference` arguments, prefix, postfix, and infix operators whose precedence, associativity, and frame come from their glossary entries, and leaves that are glossary forms or composed identifiers, under every segmentation the token lattice of §14.1 admits, giving the distinct complete covers in the order the oracle's worklist finds them, or the first failure: an expression missing, a grouping or argument unclosed, braces, a malformed reference (`LLR3005`), candidates that disagree on their profile, a surface both a prefix and a leaf (`LLP2002`), an unknown atom (`LLL1004`), a nonassociative chain, text after the expression, and the limits of nesting, parse states, and lattice edges (`LLS8002`)) is language-1.2 source in the `bootstrap/` project, as the production root `parseMath` of `GrammarMath` over the atoms of `BS-03`, with the glossary closure's projection as the host's input (the math-channel edges at each atom position with the frame, precedence, and associativity of each edge's form, and the limits and counters of the parse budget), eligible for `rust-std` with exactly its declared effects, none of them `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 the hand-built islands, which reach every failure, every kind of node, and an island with two covers, over the built-in closure and a lexicon package of the case's own, on the islands of the committed `.lex.tex` sources, and on seeded islands; 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; the token lattice, the glossary closure, and the elaboration of a cover are outside the claim. + Given the committed `bootstrap` project, hand-built islands over the built-in closure and a lexicon package of the case's own that reach every failure, every kind of node, and two covers, the islands of the committed `.lex.tex` sources, and seeded islands + When the project is checked and built, and the lowered program and the rendered package of the root are run on those islands with the closure's projection + Then the root is eligible for rust-std with exactly its declared effects, no certificate part is beyond 3,145,728 bytes, and `parseMath` equals `parse_math` on every run + And every planted defect is detected by its own vector, and the exported package is byte-identical to the committed package and realizes the program the differential ran + @BS-43 @build + Scenario: The grammar keywords, the unknown-atom diagnosis, the binders, and the term phrases of the text channel of SPEC.md §15.3, §15.4, and §12.2 (`grammar::proposition::keyword_entry`: the core entry a grammar keyword's spelling names; `TextParser::unknown_atom_diagnostic`: the first word or symbol of a sentence that is no grammar or proof keyword and that no text-channel form beginning at or before it covers, as `LLL1004`; `TextParser::binder`: every binder at a token, a type-noun form that is neither plural nor sentence-case or a mathematical island, then an island holding exactly one composed identifier, with or without the article the slot requires; and `TextParser::term_phrase`: every term phrase at a token, an island or `the SELF of ARG [and ARG]` over the noun-of and binary-noun-of forms with term phrases as arguments, in the oracle's order; each failing only by the limits of nesting, parse states, and lattice edges, `LLS8002`) is language-1.2 source in the `bootstrap/` project, as four production roots of `PropositionTerms` over the tokens of `BS-35` and the atoms of `BS-03`, with the glossary closure's projection as the host's input (the text-channel edges at each atom position with each form's category and whether it is plural or sentence-case, and the limits and counters of the parse budget): `keywordEntry`, `unknownAtom`, `binderAlternatives`, and `termPhrase`, each eligible for `rust-std` with exactly its declared effects, none of them `recursion`, 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 the hand-built sentences over the built-in closure and the committed glossary of the Peano example, which reach every limit failure, an unknown word and symbol, every kind of binder and term phrase, and each condition that refuses one, on every keyword spelling, on the sentences of the committed `.lex.tex` sources asked at every token, and on seeded sentences; 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; the token lattice, the glossary closure, the proposition grammar, and elaboration are outside the claim. + Given the committed `bootstrap` project, hand-built sentences over the built-in closure and the committed glossary of the Peano example that reach every limit failure, an unknown word and symbol, and every kind of binder and term phrase, every keyword spelling, the sentences of the committed `.lex.tex` sources, and seeded sentences + When the project is checked and built, and the lowered program and the rendered package of each root are run on those sentences with the closure's projection + 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 its oracle 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..5655aefabe 100644 --- a/model/ids.toml +++ b/model/ids.toml @@ -2894,6 +2894,54 @@ 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-35" +level = "build" +suite = "bootstrap" +statement = "The text channel and the simple proof sentences of SPEC.md §15.5 and §16.2 (`grammar::chart::text_tokens`: an atom range with its white space dropped and each mathematical island, from `\\(` or `\\[` to the first closing control of its kind, folded into one token, or the first refusal in the range: a dollar delimiter (`LLP2001`), an island opened inside another, an island without its close, and a close that closes none (`LLL1006`); and `grammar::proof::parse_proof_sentence`: the tokens of one sentence read as one of the seven registered simple sentences, with the keyword occurrences that cover it, or `LLF5005` at the first token that composes a forbidden proof word with the byte-adjacent atoms after it or begins the sentence with an underscore, and otherwise at the sentence's first atom) is language-1.2 source in the `bootstrap/` project, as two production roots of `GrammarProof` over the atoms of `BS-03`: `textTokens` and `parseProofSentence`, each eligible for `rust-std` with exactly its declared effects, none of them `recursion`, 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 the hand-built vectors, which reach every refusal of the two, every registered sentence, and every forbidden word, on seeded atom ranges of every committed `.lex.tex` source, and on seeded sentences and seeded edits of the registered ones; 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; the glossary forms, the proposition grammar, and the elaboration of a sentence are outside the claim." + +[[id]] +id = "BS-36" +level = "build" +suite = "bootstrap" +statement = "The printer of the authoring notation of a semantic module of SPEC.md §17.21 (`surface::semantic::print_at`: a value at the position of a declaration, a term, a type, or a proof, found by walking the semantic-module schema, written in the readable form of a term, a type, a member, or a parameter when that form carries the value exactly and otherwise in the generic notation tagged by its `kind`, with each member at the position the schema gives it, and laid out at 100 columns; and `surface::semantic::print_module`: the declarations of a module so written, each ended by a line feed and separated by an empty line, or the error of a value that is not a module) is language-1.2 source in the `bootstrap/` project, as two production roots of `SemanticPrint` over the layout algebra of `BS-23`, the generic notation of `BS-24`, and the standard library's `Json`, with the schema as the host's input: `printAt` and `printSemanticModule`, each eligible for `rust-std` with exactly its declared effects, which admit `recursion` as the printer of `BS-24` does, 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 the hand-built vectors, which reach every readable form and each condition that writes a value generically, every error of a module, and the schema's sample of every alternative of the declaration, term, type, and proof unions, on every declaration and every module of the committed semantic modules written in the notation, and on seeded edits of their declarations; 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; the reader of the notation and the decoding of a module are outside the claim." + +[[id]] +id = "BS-37" +level = "build" +suite = "bootstrap" +statement = "The reader of the authoring notation of a semantic module of SPEC.md §17.21 (`surface::semantic::parse_at`: a text lexed by the lexer of the notation and read as exactly one value at the position of a declaration, a term, a type, or a proof, found by walking the semantic-module schema, in the readable form of a term, a type, a member, or a parameter, or in the generic notation tagged by its `kind` with each member at the position the schema gives it, with the nesting the notation bounds at 256, or the offset and message of the first error; `surface::semantic::parse_module`: the declarations of a text read one after the other and made a module under its schema tag; and `surface::semantic::declaration_spans`: the byte range of each declaration so read) is language-1.2 source in the `bootstrap/` project, as three production roots of `SemanticRead` over the lexer of `BS-22`, the generic notation of `BS-24`, and the standard library's `Json`, with the schema as the host's input: `parseAt`, `parseSemanticModule`, and `semanticSpans`, each eligible for `rust-std` with exactly its declared effects, none of them `recursion`, 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 the hand-built vectors, which reach every readable form, every error of the reader, and the limit on nesting, on the schema's sample of every alternative of the declaration, term, type, and proof unions printed and read back, on every committed semantic module written in the notation read and spanned and each of its declarations printed and read back, and on seeded edits of the printed declarations; 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; the printer of the notation and the decoding of a module are outside the claim." + +[[id]] +id = "BS-38" +level = "build" +suite = "bootstrap" +statement = "The canonical JSON of a native core module of SPEC.md §17.10 and §21.7 (`surface::core::canonical_json`: the module's declarations as the values they serialize to, its imports, its node table with each node's members in byte order of key, its proof nodes, and its schema tag, written with object keys in byte order and strings escaped as canonical JSON escapes them) is language-1.2 source in the `bootstrap/` project, as the production root `coreJson` of `CoreJson`, which takes the module as first-order data (its kernel terms, levels, binders, and annotations as inductives of the module, and each declaration as the standard library's `Json`), eligible for `rust-std` with exactly its declared effects, which admit `recursion` as the standard library's writer does, 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 the hand-built modules, which reach every kind of node, level, binder, and annotation and strings that need escaping, on the committed core modules written in the notation, and on seeded edits of them; 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; the serialization of a declaration to its value and the validation of a module are outside the claim." + +[[id]] +id = "BS-39" +level = "build" +suite = "bootstrap" +statement = "The printer of the notation of a native core module of SPEC.md §17.21.8 (`surface::core::print_module`: a module of the schema `lexlean/core-module/2` whose edges point backward and whose proof nodes and declaration roots are nodes of it, written as its imports, its declarations with their members in the generic notation, and its statements, a node written inside the node that uses it exactly when it is used once, ends right before that node's other written-inside children, and is less than 48 deep, its bound variables written by the name of their binder when no nearer binder has that name and otherwise by index, a constant a binder in scope is named like written `const`, and each statement laid out at 100 columns; or the error of a module the notation does not carry, in the oracle's order) is language-1.2 source in the `bootstrap/` project, as the production root `printCoreModule` of `CorePrint` over the module types of `BS-38`, the layout algebra of `BS-23`, and the generic notation of `BS-24`, eligible for `rust-std` with exactly its declared effects, which admit `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 the hand-built modules, which reach every error and every kind of node, level, binder, annotation, and name, the inlining bound, and lines beyond the width, on the committed core modules written in the notation, and on seeded edits of them; 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; the reader of the notation and the decoding of a module are outside the claim." + +[[id]] +id = "BS-40" +level = "build" +suite = "bootstrap" +statement = "The reader of the notation of a native core module of SPEC.md §17.21.8 up to the decoding of its declarations (`surface::core::read_module`, the reader of `surface::core::parse_module`: a text read as its imports, its declarations with their members in the generic notation and their kind, name, universe parameters, and roots, and its statements, each numbering its nodes in the order they finish and checking its label, with names resolved to the nearest binder in scope, a nesting depth of at most 256 and a level raised by at most 1024, the byte range of each declaration, and the roots a statement's name states resolved; or the offset and message of the first error, in the order the oracle finds them) is language-1.2 source in the `bootstrap/` project, as the production root `readCoreModule` of `CoreRead` over the module types of `BS-38`, the string reader of `BS-22`, and the generic notation of `BS-24`, eligible for `rust-std` with exactly its declared effects, none of them `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 the hand-built texts, which reach every error of the reader and every form of the notation, on the committed core modules written in the notation, and on seeded edits of them; 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; the decoding of a declaration's members into a declaration and the validation of a module are outside the claim." + +[[id]] +id = "BS-42" +level = "build" +suite = "bootstrap" +statement = "The grammar of a mathematical island of SPEC.md §15.5 (`grammar::math::parse_math`: an island's atom range parsed by a Pratt parser over the declared precedence scale, with explicit call syntax, grouping parentheses, `\\lexeme` and `\\reference` arguments, prefix, postfix, and infix operators whose precedence, associativity, and frame come from their glossary entries, and leaves that are glossary forms or composed identifiers, under every segmentation the token lattice of §14.1 admits, giving the distinct complete covers in the order the oracle's worklist finds them, or the first failure: an expression missing, a grouping or argument unclosed, braces, a malformed reference (`LLR3005`), candidates that disagree on their profile, a surface both a prefix and a leaf (`LLP2002`), an unknown atom (`LLL1004`), a nonassociative chain, text after the expression, and the limits of nesting, parse states, and lattice edges (`LLS8002`)) is language-1.2 source in the `bootstrap/` project, as the production root `parseMath` of `GrammarMath` over the atoms of `BS-03`, with the glossary closure's projection as the host's input (the math-channel edges at each atom position with the frame, precedence, and associativity of each edge's form, and the limits and counters of the parse budget), eligible for `rust-std` with exactly its declared effects, none of them `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 the hand-built islands, which reach every failure, every kind of node, and an island with two covers, over the built-in closure and a lexicon package of the case's own, on the islands of the committed `.lex.tex` sources, and on seeded islands; 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; the token lattice, the glossary closure, and the elaboration of a cover are outside the claim." + +[[id]] +id = "BS-43" +level = "build" +suite = "bootstrap" +statement = "The grammar keywords, the unknown-atom diagnosis, the binders, and the term phrases of the text channel of SPEC.md §15.3, §15.4, and §12.2 (`grammar::proposition::keyword_entry`: the core entry a grammar keyword's spelling names; `TextParser::unknown_atom_diagnostic`: the first word or symbol of a sentence that is no grammar or proof keyword and that no text-channel form beginning at or before it covers, as `LLL1004`; `TextParser::binder`: every binder at a token, a type-noun form that is neither plural nor sentence-case or a mathematical island, then an island holding exactly one composed identifier, with or without the article the slot requires; and `TextParser::term_phrase`: every term phrase at a token, an island or `the SELF of ARG [and ARG]` over the noun-of and binary-noun-of forms with term phrases as arguments, in the oracle's order; each failing only by the limits of nesting, parse states, and lattice edges, `LLS8002`) is language-1.2 source in the `bootstrap/` project, as four production roots of `PropositionTerms` over the tokens of `BS-35` and the atoms of `BS-03`, with the glossary closure's projection as the host's input (the text-channel edges at each atom position with each form's category and whether it is plural or sentence-case, and the limits and counters of the parse budget): `keywordEntry`, `unknownAtom`, `binderAlternatives`, and `termPhrase`, each eligible for `rust-std` with exactly its declared effects, none of them `recursion`, 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 the hand-built sentences over the built-in closure and the committed glossary of the Peano example, which reach every limit failure, an unknown word and symbol, every kind of binder and term phrase, and each condition that refuses one, on every keyword spelling, on the sentences of the committed `.lex.tex` sources asked at every token, and on seeded sentences; 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; the token lattice, the glossary closure, the proposition grammar, and elaboration are outside the claim." + [[id]] id = "PK-01" level = "build" diff --git a/xtask/src/spec_links.rs b/xtask/src/spec_links.rs index f97eb8c07c..9ac7d83ca8 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() != 504 { + return Err(format!("RP-07: the §31 table has {} rows, not 504", 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 65bfb484c093115fb969139269993d5592cb9ae1 Mon Sep 17 00:00:00 2001 From: humuhumu33 <68450279+humuhumu33@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:06:33 +0000 Subject: [PATCH 17/22] model-write: regenerate CONFORMANCE.md for VR-23's new statement --- CONFORMANCE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONFORMANCE.md b/CONFORMANCE.md index a1d178c889..47cd55c2b7 100644 --- a/CONFORMANCE.md +++ b/CONFORMANCE.md @@ -678,7 +678,7 @@ the error registry does not sanction (R5, `ERRORS.md`). | `VR-20` | `build` | The verifier process width is an operational resource profile selected only by the environment, defaulting to 1 and refusing any malformed value with LLV7017, and it enters no lock, identity, or verified artifact. | | `VR-21` | `build` | A multi-module project whose imports form nontrivial frontiers verifies at width 1 and at a larger width to byte-identical verified directories and the same attestation identity, observed axiom sets, and policy rows, and no completion order orders a record or changes which failure is reported. | | `VR-22` | `build` | Performance telemetry is written only to the CI evidence directory the environment names, a verification with telemetry on publishes byte-identical artifacts to one without, and telemetry planted in a verified artifact fails the gate. | -| `VR-23` | `build` | A project verified as the parts of one run, each shard running the processes its plan names with the oleans of the shards it needs and the join reading every shard's records, publishes a verified directory byte-identical to the whole verification's with the same attestation identity, and a shard whose plan leaves a module without its imports, a join missing a record, or a join reading a record of an executable other than its own, is refused. | +| `VR-23` | `build` | A project verified as the parts of one run, each shard running the processes its plan names with the oleans of the shards it needs and the join reading every shard's records, publishes a verified directory byte-identical to the whole verification's with the same attestation identity, and a shard whose plan leaves a module without its imports, a join missing a record, a join reading a record of an executable other than its own, or a part that ran a toolchain other than its own, is refused. | ## Cited authorities From a06853adfac1c53648be8c901fa27a81bd8592fa Mon Sep 17 00:00:00 2001 From: humuhumu33 <68450279+humuhumu33@users.noreply.github.com> Date: Sat, 14 Nov 2026 08:14:59 +0000 Subject: [PATCH 18/22] ci(bs72-landing): rebalance Intel lanes for the typed-IR parts All eight new bootstrap-{ir-term,ir-proof,ir-document,semantic-types, semantic-terms,semantic-decls,semantic-models,semantic-reasoning} parts had landed in print (already the heaviest lane at 20), leaving lower 12, print 28, validate 11, moves 6. Moves five of the eight lighter/IR-layer parts (ir-term, ir-proof, ir-document, semantic-terms, semantic-decls) to moves, two (semantic-reasoning, semantic-types) to validate, and one (semantic-models) to lower, spreading what is believed the three heaviest cases (semantic-reasoning, semantic-models, semantic-terms) into three different lanes rather than stacking them. New totals: lower 13, print 20, validate 13, moves 11. Every part release or #532 placed is left alone. Co-Authored-By: Claude Sonnet 5 --- .github/intel-host-lanes.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/intel-host-lanes.json b/.github/intel-host-lanes.json index 3f207ca2dd..6343efff5f 100644 --- a/.github/intel-host-lanes.json +++ b/.github/intel-host-lanes.json @@ -1,9 +1,9 @@ { "producer": ["honesty"], "lanes": { - "lower": ["bootstrap-lower", "bootstrap-lexicon-lse", "bootstrap-lexicon-lre", "bootstrap-surface-module", "bootstrap-diagnostics", "bootstrap-cli-output", "bootstrap-semantic-print", "bootstrap-core-print", "bootstrap-proposition-terms", "bootstrap-lexicon-schema", "bootstrap-lse-scopes", "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", "bootstrap-ir-term", "bootstrap-ir-proof", "bootstrap-ir-document", "bootstrap-semantic-types", "bootstrap-semantic-terms", "bootstrap-semantic-decls", "bootstrap-semantic-models", "bootstrap-semantic-reasoning"], - "validate": ["bootstrap-validate", "compiler", "preservation", "preservation-lists", "preservation-runtime", "preservation-support", "preservation-prims", "preservation-std", "production", "bootstrap-semantic-read", "bootstrap-core-read"], - "moves": ["bootstrap-moves", "verification", "rest", "bootstrap-grammar-proof", "bootstrap-core-json", "bootstrap-grammar-math"] + "lower": ["bootstrap-lower", "bootstrap-lexicon-lse", "bootstrap-lexicon-lre", "bootstrap-surface-module", "bootstrap-diagnostics", "bootstrap-cli-output", "bootstrap-semantic-print", "bootstrap-core-print", "bootstrap-proposition-terms", "bootstrap-lexicon-schema", "bootstrap-lse-scopes", "bootstrap-lse-types", "bootstrap-semantic-models"], + "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", "bootstrap-semantic-read", "bootstrap-core-read", "bootstrap-semantic-reasoning", "bootstrap-semantic-types"], + "moves": ["bootstrap-moves", "verification", "rest", "bootstrap-grammar-proof", "bootstrap-core-json", "bootstrap-grammar-math", "bootstrap-ir-term", "bootstrap-ir-proof", "bootstrap-ir-document", "bootstrap-semantic-terms", "bootstrap-semantic-decls"] } } From 785fa64b5c0f05d0c00e15abf707f8a038973cce Mon Sep 17 00:00:00 2001 From: humuhumu33 <68450279+humuhumu33@users.noreply.github.com> Date: Sat, 14 Nov 2026 08:21:23 +0000 Subject: [PATCH 19/22] Fix README.md and CHANGELOG.md for the combined batch (issue #269) Correcting my own earlier instruction: these two files are not regeneration output -- nothing writes them, every slice PR edits both by hand, and the gate's RP-07/RP-11 checks read them. README.md's count sentence and the bootstrap capability row's BS-01..BS-34 claim, and CHANGELOG.md's Unreleased bullets, were all left on release's side in PR #532 and would fail the gate once its regeneration delivers. README.md: the count sentence becomes "All 507 registered conformance IDs ..."; the bootstrap row's range becomes BS-01..BS-52; and the row's prose gains what each of the eleven slice PRs (505, 509-515, 522-524) added to it, in register order, taken verbatim from each PR's own patch against its merge base. CHANGELOG.md: every bullet each of the eleven PRs added under ## Unreleased, in register order, verbatim (including each independent slice's own max_child_output_bytes bullet, since each PR added it as its own change at authoring time). Built as a separate branch on top of batch/bs35-52's current head rather than on it, since #532's regeneration (an hour in) must not be disturbed and neither file is an input to anything it generates. Co-Authored-By: Claude Sonnet 5 --- CHANGELOG.md | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 4 +-- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a02c2550cf..00d879a721 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,96 @@ workspace carried at those versions without a tag. ## Unreleased +- The bootstrap project configures `max_child_output_bytes = 268435456` (SPEC.md + §17.23, §10.2), by the maintainer's decision: the extraction of its export wrote + more than the default 33,554,432 bytes on `lean`'s output once the front end's + components (`BS-35`) joined it. The register is unchanged. +- The text channel and the simple proof sentences as language-1.2 source + (SPEC.md §17.23, §15.5, §16.2, `BS-35`, #269; no new diagnostic code, no + change to the language-1.2 identity). `textTokens` and `parseProofSentence` + of `GrammarProof` return what `grammar::chart::text_tokens` and + `grammar::proof::parse_proof_sentence` return and admit no `recursion`. The + register is 495 IDs. +- The bootstrap project configures `max_child_output_bytes = 268435456` (SPEC.md + §17.23, §10.2), by the maintainer's decision: the extraction of its export wrote + more than the default 33,554,432 bytes on `lean`'s output once the front end's + components (`BS-36`) joined it. The register is unchanged. +- The printer of the semantic notation as language-1.2 source (SPEC.md + §17.23, §17.21, `BS-36`, #269; no new diagnostic code, no change to the + language-1.2 identity). `printAt` and `printSemanticModule` of + `SemanticPrint` return what `surface::semantic::print_at` and `print_module` + return, with the semantic-module schema as their input, and admit + `recursion`. The register is 494 IDs. +- The bootstrap project configures `max_child_output_bytes = 268435456` (SPEC.md + §17.23, §10.2), by the maintainer's decision: the extraction of its export wrote + more than the default 33,554,432 bytes on `lean`'s output once the front end's + components (`BS-37`) joined it. The register is unchanged. +- The reader of the semantic notation as language-1.2 source (SPEC.md + §17.23, §17.21, `BS-37`, #269; no new diagnostic code, no change to the + language-1.2 identity). `parseAt`, `parseSemanticModule`, and + `semanticSpans` of `SemanticRead` return what `surface::semantic::parse_at`, + `parse_module`, and `declaration_spans` return, with the semantic-module + schema as their input, and admit no `recursion`. The register is 494 IDs. +- The bootstrap project configures `max_child_output_bytes = 268435456` (SPEC.md + §17.23, §10.2), by the maintainer's decision: the extraction of its export wrote + more than the default 33,554,432 bytes on `lean`'s output once the front end's + components (`BS-38`) joined it. The register is unchanged. +- The canonical JSON of a native core module as language-1.2 source + (SPEC.md §17.23, §17.10, §21.7, `BS-38`, #269; no new diagnostic code, no + change to the language-1.2 identity). `coreJson` of `CoreJson` returns what + `surface::core::canonical_json` returns and admits `recursion`. The register + is 494 IDs. +- The printer of the core notation as language-1.2 source (SPEC.md §17.23, + §17.21, `BS-39`, #269; no new diagnostic code, no change to the + language-1.2 identity). `printCoreModule` of `CorePrint` returns what + `surface::core::print_module` returns and admits `recursion`. The register + is 495 IDs. +- The reader of the core notation as language-1.2 source (SPEC.md §17.23, + §17.21, `BS-40`, #269; no new diagnostic code, no change to the + language-1.2 identity). `readCoreModule` of `CoreRead` returns what + `surface::core::read_module` returns, the reader of `parse_module` before the + decoding of a declaration, and admits no `recursion`. `parse_module_with_spans` + reads with `read_module` and then decodes, with the same results and errors. + The register is 496 IDs. +- The bootstrap project configures `max_child_output_bytes = 268435456` (SPEC.md + §17.23, §10.2), by the maintainer's decision: the extraction of its export wrote + more than the default 33,554,432 bytes on `lean`'s output once the front end's + components (`BS-42`) joined it. The register is unchanged. +- The grammar of a mathematical island as language-1.2 source (SPEC.md + §17.23, §15.5, `BS-42`, #269; no new diagnostic code, no change to the + language-1.2 identity). `parseMath` of `GrammarMath` returns what + `grammar::math::parse_math` returns, with the glossary closure's projection + as its input, and admits no `recursion`. The register is 494 IDs. +- The grammar keywords, the unknown-atom diagnosis, the binders, and the term + phrases of the text channel as language-1.2 source (SPEC.md §17.23, §15.3, + §15.4, `BS-43`, #269; no new diagnostic code, no change to the language-1.2 + identity). `keywordEntry`, `unknownAtom`, `binderAlternatives`, and + `termPhrase` of `PropositionTerms` return what `keyword_entry` and the + `TextParser` methods return, with the glossary closure's projection as their + input, and admit no `recursion`. The register is 495 IDs. +- The tables and predicates of the lexicon entry schema as language-1.2 source, + the first slice of the lexicon closure (SPEC.md §17.23, §13.1 to §13.6, `BS-50`, + #269; no new diagnostic code, no change to the language-1.2 identity). + `tokenIndex`, `shapeFacts`, `surfaceSafety`, `isLeanName`, `tomlCommentAt`, + and `entryPath` of `LexiconSchema` return what the schema's token tables, + `frame_permitted` with the category and frame facts, `surface_safety`, + `is_lean_name`, `toml_comment_at`, and `entry_path` return; `surfaceSafety` + reads the atoms of `scanSource`. None admits `recursion`. +- The scopes, references, and outer shape of a LexLean Semantic Expression as + language-1.2 source, the second slice of the lexicon closure (SPEC.md §17.23, + §13.8, `BS-51`, #269; no new diagnostic code, no change to the language-1.2 + identity). `checkScopes`, `referencedConsts`, and `outerShape` of `LseScopes` + return what `Lse::check_scopes`, `referenced_consts`, and + `outer_explicit_binders` with `result` return, over the expressions of + `BS-25`. None admits `recursion`. +- 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. - Every part of a verification names its toolchain by address (SPEC.md §22.11, §9.4). A shard writes `toolchain.json` (`lexlean/verify-toolchain/1`) beside its output: the SHA-256 of the `lean`, `lake`, and `leanchecker` its diff --git a/README.md b/README.md index db42958671..9759b6d1dd 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 507 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 text channel and the simple proof sentences, the printer of the semantic notation, the reader of the semantic notation, the canonical JSON of a core module, the printer of the core notation, the reader of the core notation, the grammar of a mathematical island, the binders and term phrases of the text channel, the tables and predicates of the lexicon entry schema, the scopes and references of a semantic expression, 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` | From 9f06458d294982eb86d197b035f469b478e3797c Mon Sep 17 00:00:00 2001 From: humuhumu33 <68450279+humuhumu33@users.noreply.github.com> Date: Sat, 14 Nov 2026 16:31:41 +0000 Subject: [PATCH 20/22] ci(bs72-landing): bound the stack-bound measure step's concurrency Attempt 1 and 2 of r16's finish job both lost the runner outright in counts and stack bounds, neither hitting the steps own 360-minute timeout: The hosted runner lost communication with the server. BS-69, 70 and 72 (semantic_terms, semantic_decls, semantic_reasoning) were still running together, at the default test-threads (4 on this runner), when each attempt died; a single-file verify of a similarly large semantic-module case measured over 6 GiB RSS alone, so four of them at once on a 16 GiB runner swaps the agent to death rather than finishing or timing out cleanly. Splits the already-computed case list into a known-heavy set (BS-69, 70, 71, 72) run one at a time (--test-threads=1) and the rest run at --test-threads=2, instead of one cargo test invocation at the default thread count. For this landings eight cases (BS-52, 66-72), expected total: roughly 60 to 90 minutes for the light four at threads=2 (they ran four-way in about 2 hours before; halving concurrency should cut that substantially, though not precisely predictably), plus the heavy four serialized, each guessed at 15 to 40 minutes from BS-71s one completed run (14.4 min under partial, not zero, contention) -- a rough total of 140 to 250 minutes, comfortably under the steps 360-minute cap even if that guess is off by a wide margin. Co-Authored-By: Claude Sonnet 5 --- .github/workflows/regenerate.yml | 33 ++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/.github/workflows/regenerate.yml b/.github/workflows/regenerate.yml index 1e93c99731..c2b6b1c8d2 100644 --- a/.github/workflows/regenerate.yml +++ b/.github/workflows/regenerate.yml @@ -310,8 +310,37 @@ jobs: exit 0 fi echo "measuring: $cases" | tee -a "$GITHUB_STEP_SUMMARY" - LEXLEAN_MEASURE_STACK=all cargo test -p repo-conformance --all-features --test conformance -- $cases --nocapture 2>&1 \ - | grep -E --line-buffered 'debug [0-9]+ bytes|test result|panicked|^test ' + # Each of these cases alone measured over 6 GiB RSS on 2026-11-14; + # four of them at once on this runner's 16 GiB swapped the agent + # to death twice (neither loss hit the step's own 360-minute + # timeout). Run them one at a time; run the rest at --test-threads=2. + heavy="conformance_bs_69 conformance_bs_70 conformance_bs_71 conformance_bs_72" + light="" + heavy_cases="" + for case in $cases; do + is_heavy=0 + for h in $heavy; do + if [ "$case" = "$h" ]; then + is_heavy=1 + break + fi + done + if [ "$is_heavy" = 1 ]; then + heavy_cases="$heavy_cases $case" + else + light="$light $case" + fi + done + if [ -n "$light" ]; then + echo "measuring (threads=2): $light" | tee -a "$GITHUB_STEP_SUMMARY" + LEXLEAN_MEASURE_STACK=all cargo test -p repo-conformance --all-features --test conformance -- $light --test-threads=2 --nocapture 2>&1 \ + | grep -E --line-buffered 'debug [0-9]+ bytes|test result|panicked|^test ' + fi + if [ -n "$heavy_cases" ]; then + echo "measuring (threads=1): $heavy_cases" | tee -a "$GITHUB_STEP_SUMMARY" + LEXLEAN_MEASURE_STACK=all cargo test -p repo-conformance --all-features --test conformance -- $heavy_cases --test-threads=1 --nocapture 2>&1 \ + | grep -E --line-buffered 'debug [0-9]+ bytes|test result|panicked|^test ' + fi - name: Return the regenerated tree if: ${{ !cancelled() }} shell: bash From 806f20742e037dd770c5a745b0a3859a595a1bfe Mon Sep 17 00:00:00 2001 From: humuhumu33 <68450279+humuhumu33@users.noreply.github.com> Date: Sun, 15 Nov 2026 18:26:02 +0000 Subject: [PATCH 21/22] ci(bs72-landing): bound the stack-bound measure step (copied ahead of #532) Replaces .github/workflows/regenerate.yml with the version prepared for #532 (blob 704df4fda13aae597527fe0ce0003230a01b2bc9): heavy cases (BS-36, 69, 70, 71, 72) run one at a time with explicit started/ended lines and elapsed minutes, the rest at --test-threads=2, and the whole step is bounded by a shared timeout against a 320-minute budget so a run that cannot finish everything fails cleanly (and keeps every row already measured in tests/stack-bounds.json) instead of being cancelled by the jobs own 360-minute limit and losing all of it. Also carries the once-a-minute free-memory trace from the same change. No other change; not a release merge. This copies ahead of #532 landing so #497s own stack-bound rows can start banking now, since measuring all of BS-65..72 is the longest pole and the two PRs rows stay valid through a later merge as long as the packages do not change. Co-Authored-By: Claude Sonnet 5 --- .github/workflows/regenerate.yml | 78 ++++++++++++++++++++++++++------ 1 file changed, 65 insertions(+), 13 deletions(-) diff --git a/.github/workflows/regenerate.yml b/.github/workflows/regenerate.yml index c2b6b1c8d2..704df4fda1 100644 --- a/.github/workflows/regenerate.yml +++ b/.github/workflows/regenerate.yml @@ -260,6 +260,11 @@ jobs: # cores go to a plain file under a zero limit: none is written. sudo sysctl -w kernel.core_pattern=core ulimit -c 0 + # A runner that runs out of memory loses its agent and leaves no log + # of why, so the free memory goes to the log once a minute. + ( while true; do echo "memory $(date -u +%H:%M:%S) $(free -m | awk '/^Mem:/ {print "used=" $3 "M available=" $7 "M"} /^Swap:/ {print "swap=" $3 "M"}' | tr '\n' ' ')"; sleep 60; done ) & + trace=$! + trap 'kill "$trace" 2>/dev/null || true' EXIT # Rows for packages no longer in the exports are dropped; for each # recursive package without a row, the case whose source names its # root runs whole and measures it (`LEXLEAN_MEASURE_STACK=all`). @@ -310,11 +315,11 @@ jobs: exit 0 fi echo "measuring: $cases" | tee -a "$GITHUB_STEP_SUMMARY" - # Each of these cases alone measured over 6 GiB RSS on 2026-11-14; - # four of them at once on this runner's 16 GiB swapped the agent - # to death twice (neither loss hit the step's own 360-minute - # timeout). Run them one at a time; run the rest at --test-threads=2. - heavy="conformance_bs_69 conformance_bs_70 conformance_bs_71 conformance_bs_72" + # Three runs on 2026-11-14 lost their runner while several of these + # cases measured at once. Alone, BS-36 left 7 GiB of this runner's + # 16 GiB available and BS-72 measured 10 to 12 GiB, so these run one + # at a time and the rest two at a time. + heavy="conformance_bs_36 conformance_bs_69 conformance_bs_70 conformance_bs_71 conformance_bs_72" light="" heavy_cases="" for case in $cases; do @@ -331,16 +336,63 @@ jobs: light="$light $case" fi done + # A job's own 360-minute limit cancels this step outright if it is + # reached, and a cancelled job skips every later step, so the tree + # never returns and a row measured seconds before the limit is + # lost with everything else. Each row is instead written to + # tests/stack-bounds.json as it is measured (stack_bounds.rs + # record_row/rewrite), so a step that fails, rather than being + # cancelled, still returns whatever is already on disk. Stop + # ourselves inside a budget comfortably under the job's own limit, + # so a run that cannot finish everything fails cleanly instead of + # being cancelled, and keeps every row it reached. + step_start=$(date +%s) + deadline_s=$((320 * 60)) + overall_status=0 + remaining() { + local now elapsed left + now=$(date +%s) + elapsed=$((now - step_start)) + left=$((deadline_s - elapsed)) + if [ "$left" -lt 0 ]; then + left=0 + fi + echo "$left" + } if [ -n "$light" ]; then - echo "measuring (threads=2): $light" | tee -a "$GITHUB_STEP_SUMMARY" - LEXLEAN_MEASURE_STACK=all cargo test -p repo-conformance --all-features --test conformance -- $light --test-threads=2 --nocapture 2>&1 \ - | grep -E --line-buffered 'debug [0-9]+ bytes|test result|panicked|^test ' - fi - if [ -n "$heavy_cases" ]; then - echo "measuring (threads=1): $heavy_cases" | tee -a "$GITHUB_STEP_SUMMARY" - LEXLEAN_MEASURE_STACK=all cargo test -p repo-conformance --all-features --test conformance -- $heavy_cases --test-threads=1 --nocapture 2>&1 \ - | grep -E --line-buffered 'debug [0-9]+ bytes|test result|panicked|^test ' + left=$(remaining) + if [ "$left" -gt 0 ]; then + echo "measuring (threads=2, budget ${left}s): $light" | tee -a "$GITHUB_STEP_SUMMARY" + if ! timeout "${left}s" env LEXLEAN_MEASURE_STACK=all cargo test -p repo-conformance --all-features --test conformance -- $light --test-threads=2 --nocapture 2>&1 \ + | grep -E --line-buffered 'debug [0-9]+ bytes|test result|panicked|^test '; then + overall_status=1 + fi + else + echo "no time left in the ${deadline_s}s budget; skipping: $light" | tee -a "$GITHUB_STEP_SUMMARY" + overall_status=1 + fi fi + # Heavy cases already run one at a time; looping them individually + # (instead of one --test-threads=1 invocation over all of them) + # costs nothing and prints exactly when each one starts and ends. + for case in $heavy_cases; do + left=$(remaining) + if [ "$left" -le 0 ]; then + echo "no time left in the ${deadline_s}s budget; skipping: $case" | tee -a "$GITHUB_STEP_SUMMARY" + overall_status=1 + continue + fi + started=$(date +%s) + echo "$case: started $(date -u +%H:%M:%S) (threads=1, budget ${left}s)" | tee -a "$GITHUB_STEP_SUMMARY" + if ! timeout "${left}s" env LEXLEAN_MEASURE_STACK=all cargo test -p repo-conformance --all-features --test conformance -- "$case" --test-threads=1 --nocapture 2>&1 \ + | grep -E --line-buffered 'debug [0-9]+ bytes|test result|panicked|^test '; then + overall_status=1 + fi + ended=$(date +%s) + mins=$(( (ended - started) / 60 )) + echo "$case: ended $(date -u +%H:%M:%S), ${mins}m" | tee -a "$GITHUB_STEP_SUMMARY" + done + exit "$overall_status" - name: Return the regenerated tree if: ${{ !cancelled() }} shell: bash From eee6addb7eaa5239da24262c47b8e419da4305b2 Mon Sep 17 00:00:00 2001 From: humuhumu33 <68450279+humuhumu33@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:48:59 +0000 Subject: [PATCH 22/22] ci(bs72-landing): bank eleven stack-bound rows measured by r18 tests/stack-bounds.json only, taken from r18's (43746749297) regenerated tree and checked against this heads own copy first: exactly eleven rows added (bootstrap/check_signature, check_value, declaration_text, linked_text, module_text, project_semantic_id, proof_text, semantic_proof_text, semantic_term_text, type_spelling, type_text), zero removed, zero changed, same spec/headroom/granule/scaling, same sorted row order and pretty-printed format. The new blobs sha (1402fa8) matches the git-diff index line r18s own log recorded for this file, not just a JSON-level comparison. r18s measuring step hit its own 320-minute budget (by design, not the jobs 360-minute cancel) after BS-69 finished and BS-70 was cut off mid-run; BS-70, 71 and 72 still need measuring. Banking these eleven now means the next regeneration only has those three left. Co-Authored-By: Claude Sonnet 5 --- tests/stack-bounds.json | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/stack-bounds.json b/tests/stack-bounds.json index 3d711f6ee4..1402fa89c3 100644 --- a/tests/stack-bounds.json +++ b/tests/stack-bounds.json @@ -45,12 +45,21 @@ "bootstrap/check_program": { "debug": 13697024 }, + "bootstrap/check_signature": { + "debug": 2031616 + }, + "bootstrap/check_value": { + "debug": 2228224 + }, "bootstrap/correspond_crate": { "debug": 4915200 }, "bootstrap/dead_functions": { "debug": 5701632 }, + "bootstrap/declaration_text": { + "debug": 4521984 + }, "bootstrap/eq_key": { "debug": 1835008 }, @@ -66,6 +75,9 @@ "bootstrap/function_shells": { "debug": 3080192 }, + "bootstrap/linked_text": { + "debug": 4128768 + }, "bootstrap/loop_function": { "debug": 2818048 }, @@ -81,6 +93,9 @@ "bootstrap/lower_value_at": { "debug": 4390912 }, + "bootstrap/module_text": { + "debug": 4128768 + }, "bootstrap/node_count": { "debug": 1835008 }, @@ -102,9 +117,21 @@ "bootstrap/print_tooling_module": { "debug": 1179648 }, + "bootstrap/project_semantic_id": { + "debug": 4128768 + }, + "bootstrap/proof_text": { + "debug": 2097152 + }, "bootstrap/render_tooling_latex": { "debug": 3342336 }, + "bootstrap/semantic_proof_text": { + "debug": 2162688 + }, + "bootstrap/semantic_term_text": { + "debug": 6553600 + }, "bootstrap/site_trees": { "debug": 6029312 }, @@ -117,6 +144,12 @@ "bootstrap/term_text": { "debug": 1835008 }, + "bootstrap/type_spelling": { + "debug": 1507328 + }, + "bootstrap/type_text": { + "debug": 1507328 + }, "bootstrap/use_trees": { "debug": 1572864 },