Compare commits

..
3 Commits
Author SHA1 Message Date
alexmandClaude 477dffe02c Print a call's result in its own type: a FLOAT result 3.0 is not shown as 3
runPseudo wrote "F returned <value>" without the routine's return type,
so a whole-valued FLOAT result printed as an INTEGER would. It now passes
the type, as every other value the notation shows does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
2026-10-05 12:17:36 +00:00
alexmandClaude 7ddc628597 Add USE and pin pplus-library: routines verified once, used by name
Routines that many programs need (a mean, a gap penalty, the
Lennard-Jones potential, the minimum image convention) were rewritten
and re-verified each time, and there was no way to open one and see
exactly what it does. They now live in pplus-library, pinned here as
the submodule library/, and a program brings them in with USE.

- USE module / USE module.Routine (pseudo-study resolveUses): each
  routine is studied once in its own file (its own USE lines resolved
  the same way, cycles refused) and handed to the checker as a library
  routine; unknown-library, unknown-routine, library-clash, use-place.
- A run steps over a library routine (one step at the call, one at the
  return, none of its lines); a broken contract is reported at the call,
  naming the routine. The prover uses its REQUIRE and ENSURE at the
  call and does not prove it again; it is not a unit of the program.
- pseudo-to-mojo writes each library routine the program calls once,
  dependencies first, labeled as coming from the library.
- src/library: the registry USE reads, generated from library/
  (scripts/library.mjs emit), so the site needs no files at run time.
- scripts/library.mjs check: every routine at every level: structure,
  cases, prover, fuzzer, generated Mojo, each reference implementation
  (CPython; cc at -O0 and -O2; core's Mojo interpreter; the pinned
  compiler's stored answer) held to the pseudo's results, and the Dafny
  proof with the pinned Dafny. Writes library/status.json and STATUS.md.
  scripts/library_godbolt.py stores the compiler's answers (network).
- pixi tasks library-check, library-emit, library-verify-mojo,
  library-harness; CI checks out the submodule and runs library-check.
- Tests: tests/library.test.mjs, corpus 53-library; docs/library.md and
  a library section in trust.md.

Gate: 332 pass; library-check: no failing check (16 routines; the Mojo
references are unverified until library-verify-mojo runs on the network).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
2026-10-05 12:06:41 +00:00
alexmandClaude be0db2d74f Widen pseudo for numerical work: sized types, FLOAT32, exact built-ins, USE IEEE
Pseudo could say what a procedure computes only up to INTEGER and FLOAT,
so it could not state what a Mojo or Python program with Int32, UInt8 or
Float32 values will compute, and it had no words for rounding itself.

- lang/parse: INT8..INT64, UINT8..UINT64, FLOAT32, FLOAT64 as types
  (INTEGER or FLOAT with a width, so every "whole number" rule accepts
  them); conversions written as Mojo writes them, INT32(X), FLOAT32(X);
  1_000_000, 0xFF and 0b1010 literals; a type name as an argument
  (MAX_VALUE(INT8)); the USE line (USE IEEE now, library modules next).
- check: widths and precisions never mix silently (mixed-width,
  mixed-precision); a literal takes the type of the sized value beside
  it, as in Mojo, and one that does not fit is an error before any run
  (literal-range); typing for every new built-in.
- interp: a sized result, store, parameter, RETURN or conversion that
  does not fit stops the run (kind overflow), WRAP(X, T) wraps on
  purpose; FLOAT32 results are rounded to single precision; USE IEEE
  lets NaN and infinities flow.
- numeric.js: exact helpers: FMA rounded once, EXACT(X) with every digit
  a FLOAT holds, ULP and NEXT_AFTER for both precisions, bit operations
  in two's complement.
- Built-ins: ASIN ACOS ATAN SINH COSH TANH HYPOT POW CBRT EXPM1 LOG1P E
  TRUNC SIGN COPYSIGN CLAMP FMA IS_NAN IS_INF IS_FINITE INF NAN ULP
  NEXT_AFTER IS_CLOSE EXACT MAX_VALUE MIN_VALUE EPSILON GCD LCM BIT_AND
  BIT_OR BIT_XOR BIT_NOT SHIFT_LEFT SHIFT_RIGHT WRAP. They stay usable
  as names (a variable E, a FUNCTION GCD): the program's own wins.
- vcgen: an overflow obligation on every sized result, then the range
  as a fact (also for sized parameters and loop-cut variables); FLOAT32
  arithmetic as its own uninterpreted functions, constants rounded
  exactly, TOFLOAT32(v) otherwise; argument obligations for SQRT, the
  logarithms, ASIN/ACOS, LOG1P, CLAMP and the shifts; nothing proved
  under USE IEEE.
- testing: the fuzzer tries the edges of a sized type and generates
  FLOAT32 values.
- pseudo-to-mojo: Int8..Float32, conversions, the new built-ins through
  std.math (each name in the harvested index), a note where Mojo wraps.
- Tests: numeric.test.mjs; corpus 49-52; prover fixtures int8-overflow,
  float32-store, ieee-nan; the reference documents every new form.

Gate: 324 pass (core). New Mojo uses std.math names the interpreter has
not been verified on (listed in sync-mojo's KNOWN_GAPS); they need
pixi run mojo-verify on the network before the interpreter runs them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
2026-10-05 11:43:53 +00:00
40 changed files with 3919 additions and 110 deletions

No files matched your search

+6
View File
@@ -11,7 +11,10 @@ jobs:
test: test:
runs-on: quarto-deploy runs-on: quarto-deploy
steps: steps:
# library/ is pplus-library, a submodule; tests/library.test.mjs needs it checked out
- uses: actions/checkout@v4 - uses: actions/checkout@v4
with:
submodules: recursive
- name: Toolchain on the host - name: Toolchain on the host
run: | run: |
node --version node --version
@@ -23,3 +26,6 @@ jobs:
run: | run: |
if command -v python3 >/dev/null; then python3 scripts/mojo/corpus.py status --strict if command -v python3 >/dev/null; then python3 scripts/mojo/corpus.py status --strict
else echo "python3 missing: corpus status skipped"; fi else echo "python3 missing: corpus status skipped"; fi
# every library routine at every level the host has tools for (python3, cc); nothing may fail
- name: Library routines pass
run: node scripts/library.mjs check
+4
View File
@@ -0,0 +1,4 @@
[submodule "library"]
path = library
url = ../pplus-library.git
branch = main
+2 -1
View File
@@ -23,7 +23,7 @@ The full suite takes about two minutes. While iterating, build and run one file:
## Hard rules ## Hard rules
1. **Core has no DOM.** What a program means is decided without a browser. A library module never touches `document`, `window`, `localStorage` or `navigator`, never imports one of the course's page widgets, and never imports from outside `src/`; `tests/boundary.test.mjs` holds it to that. The widgets (listed in that test) live beside the engines only so the course and pplus-web take them from one place. 1. **Core has no DOM.** What a program means is decided without a browser. A library module never touches `document`, `window`, `localStorage` or `navigator`, never imports one of the course's page widgets, and never imports from outside `src/`; `tests/boundary.test.mjs` holds it to that. The widgets (listed in that test) live beside the engines only so the course and pplus-web take them from one place.
2. **Do not edit generated files by hand.** `src/mojo/generated/*` and `syntax/mojo.xml` come from the corpus pipeline (`pixi run mojo-emit`, `mojo-status`, `play`). `mojo/corpus/` comes from `mojo-harvest`. `mojo/golden/` holds godbolt's answers and comes only from `mojo-verify`. 2. **Do not edit generated files by hand.** `src/mojo/generated/*` and `syntax/mojo.xml` come from the corpus pipeline (`pixi run mojo-emit`, `mojo-status`, `play`). `src/library/generated.ts` comes from `library/` (`pixi run library-emit`), and the library's `status.json`, `STATUS.md` and `golden/` from `pixi run library-check` and `library-verify-mojo`. `mojo/corpus/` comes from `mojo-harvest`. `mojo/golden/` holds godbolt's answers and comes only from `mojo-verify`.
3. **Do not edit a `.mojo` file's text unless the task needs it.** A stored godbolt answer is keyed by the whole source, header comments included, so any edit (even to a comment) makes the program unverified until it is asked again on the network. If you add or change one, say in your summary that it needs `pixi run mojo-verify-item '<corpus>/<name>'`. 3. **Do not edit a `.mojo` file's text unless the task needs it.** A stored godbolt answer is keyed by the whole source, header comments included, so any edit (even to a comment) makes the program unverified until it is asked again on the network. If you add or change one, say in your summary that it needs `pixi run mojo-verify-item '<corpus>/<name>'`.
4. **Never claim Mojo compiles, runs, or prints something without the real compiler's answer.** Mojo is pinned to 1.1.0. The checker may never report an error on a program the compiler accepts, and the interpreter must print exactly what godbolt printed or decline; `tests/mojo-check.test.mjs` and `tests/mojo-run.test.mjs` hold them to that. Every Mojo construct you write into core must pass `checkMojo`, and every standard-library name must exist in `src/mojo/generated/stdlib.ts` or `types.ts`. 4. **Never claim Mojo compiles, runs, or prints something without the real compiler's answer.** Mojo is pinned to 1.1.0. The checker may never report an error on a program the compiler accepts, and the interpreter must print exactly what godbolt printed or decline; `tests/mojo-check.test.mjs` and `tests/mojo-run.test.mjs` hold them to that. Every Mojo construct you write into core must pass `checkMojo`, and every standard-library name must exist in `src/mojo/generated/stdlib.ts` or `types.ts`.
5. **Never let the prover call something proved that a run breaks.** "Proved" must be a proof. A change to `src/engines/pseudo/logic.ts`, `solver.ts` or `vcgen.ts` keeps `tests/prover-soundness.test.mjs` and `tests/engine-solver.test.mjs` green, and a fact the prover assumes must hold for every value a run can produce, or be guarded by an obligation. When you find a soundness bug, add the program that shows it to `tests/prover/` with a `// MUST-NOT-PROVE:` (or `// RUN-STOPS:`) header first, see the test fail, then fix the rule ([docs/engines.md](docs/engines.md#soundness)). 5. **Never let the prover call something proved that a run breaks.** "Proved" must be a proof. A change to `src/engines/pseudo/logic.ts`, `solver.ts` or `vcgen.ts` keeps `tests/prover-soundness.test.mjs` and `tests/engine-solver.test.mjs` green, and a fact the prover assumes must hold for every value a run can produce, or be guarded by an obligation. When you find a soundness bug, add the program that shows it to `tests/prover/` with a `// MUST-NOT-PROVE:` (or `// RUN-STOPS:`) header first, see the test fail, then fix the rule ([docs/engines.md](docs/engines.md#soundness)).
@@ -49,6 +49,7 @@ The full suite takes about two minutes. While iterating, build and run one file:
| Realistic Mojo for the interpreter | `pixi run new-stress <name> "<what it exercises>"`, with the output you predict in `# expect-stdout:` lines | | Realistic Mojo for the interpreter | `pixi run new-stress <name> "<what it exercises>"`, with the output you predict in `# expect-stdout:` lines |
| A pseudo regression program | a file in `tests/corpus/` whose `// EXPECT:` lines are the output it must print | | A pseudo regression program | a file in `tests/corpus/` whose `// EXPECT:` lines are the output it must print |
| A prover soundness regression | a file in `tests/prover/` whose `// MUST-NOT-PROVE: <kind>@<line>` names an obligation a run breaks, or `// RUN-STOPS: <kind>@<line>` where a run must stop | | A prover soundness regression | a file in `tests/prover/` whose `// MUST-NOT-PROVE: <kind>@<line>` names an obligation a run breaks, or `// RUN-STOPS: <kind>@<line>` where a run must stop |
| A verified routine others can USE | in pplus-library (`library/`), under its AGENTS.md; then `pixi run library-check` and `library-emit` here ([docs/library.md](docs/library.md)) |
| A command | a task in `pixi.toml` and a row in the README's Commands table | | A command | a task in `pixi.toml` and a row in the README's Commands table |
| A language | a `LanguageService` (`src/common/types.ts`) in `src/services/`, registered in `src/services/index.ts` | | A language | a `LanguageService` (`src/common/types.ts`) in `src/services/`, registered in `src/services/index.ts` |
+10 -1
View File
@@ -7,13 +7,14 @@ pplus-core is the part of Programming+ that has to be right: the languages, the
- **Verification:** the prover (`src/engines/pseudo/logic.ts`, `solver.ts`, `vcgen.ts`), sound and held to it by tests; the fuzzer (`src/common/fuzz.ts`); and a pinned Dafny (`scripts/dafny.py`). - **Verification:** the prover (`src/engines/pseudo/logic.ts`, `solver.ts`, `vcgen.ts`), sound and held to it by tests; the fuzzer (`src/common/fuzz.ts`); and a pinned Dafny (`scripts/dafny.py`).
- **Stored answers:** the Mojo corpus (`mojo/`): material harvested from Modular's repositories at pinned commits, our own probes and programs, and godbolt's answers for every one of them (`mojo/golden/`). - **Stored answers:** the Mojo corpus (`mojo/`): material harvested from Modular's repositories at pinned commits, our own probes and programs, and godbolt's answers for every one of them (`mojo/golden/`).
- **A workspace:** `src/common/workspace.ts` is one piece of work and everything its sources mean. pplus-web's session extends it. - **A workspace:** `src/common/workspace.ts` is one piece of work and everything its sources mean. pplus-web's session extends it.
- **A library:** pplus-library, the submodule `library/`: verified routines (statistics, numerics, alignment, molecular simulation, sequences) that a program brings in with `USE`, each checked at every level ([docs/library.md](docs/library.md)).
- **A command line:** `pixi run cli check|run|prove|fuzz|translate <file>`. - **A command line:** `pixi run cli check|run|prove|fuzz|translate <file>`.
Everything it says about Mojo is pinned to **Mojo 1.1.0** and checked against the real compiler on [Compiler Explorer](https://godbolt.org). Everything it says about Mojo is pinned to **Mojo 1.1.0** and checked against the real compiler on [Compiler Explorer](https://godbolt.org).
## Quick start ## Quick start
With [pixi](https://pixi.sh) (it brings the pinned Mojo, Node and Python): Clone with the library submodule (`git clone --recurse-submodules`, or `git submodule update --init` in an existing clone). With [pixi](https://pixi.sh) (it brings the pinned Mojo, Node and Python):
```bash ```bash
pixi run test # build, then every test pixi run test # build, then every test
@@ -46,6 +47,11 @@ Every command is a pixi task (`pixi task list` shows them with their description
| **Dafny, pinned** ([scripts/dafny.py](scripts/dafny.py)) | | | **Dafny, pinned** ([scripts/dafny.py](scripts/dafny.py)) | |
| `dafny-fetch` | Download the pinned Dafny into `.tools/`, checked by SHA-256 (network, once) | | `dafny-fetch` | Download the pinned Dafny into `.tools/`, checked by SHA-256 (network, once) |
| `dafny` | Run the pinned Dafny with the arguments given, e.g. `pixi run dafny verify file.dfy` | | `dafny` | Run the pinned Dafny with the arguments given, e.g. `pixi run dafny verify file.dfy` |
| **The library** ([docs/library.md](docs/library.md)) | |
| `library-check` | Check every library routine at every level; writes `library/status.json` and `STATUS.md` |
| `library-emit` | Regenerate `src/library/generated.ts`, the registry `USE` reads, from `library/` |
| `library-verify-mojo` | Ask the pinned Mojo compiler about every Mojo reference of the library (network) |
| `library-harness <id> <lang> <level>` | Print the program a reference implementation is run as |
| **Write content** | | | **Write content** | |
| `new-exercise <id>` | Start an exercise whose Mojo lives in `mojo/local/<id>.mojo` ([docs/exercises.md](docs/exercises.md)) | | `new-exercise <id>` | Start an exercise whose Mojo lives in `mojo/local/<id>.mojo` ([docs/exercises.md](docs/exercises.md)) |
| `new-probe <name> <question>` | Start `mojo/probes/<name>.mojo`: one question about the compiler ([docs/mojo-corpus.md](docs/mojo-corpus.md)) | | `new-probe <name> <question>` | Start `mojo/probes/<name>.mojo`: one question about the compiler ([docs/mojo-corpus.md](docs/mojo-corpus.md)) |
@@ -79,6 +85,7 @@ AGENTS.md rules for changing core (read first)
src/ src/
common/ the workspace, the problem catalog, the fuzzer, the run view's data, the language common/ the workspace, the problem catalog, the fuzzer, the run view's data, the language
reference, the machine presets, the shared types reference, the machine presets, the shared types
library/ the registry USE reads (generated from library/), and its types
engines/pseudo/ the pseudo language and the prover (logic, solver, vcgen in TypeScript), and the course's engines/pseudo/ the pseudo language and the prover (logic, solver, vcgen in TypeScript), and the course's
page widgets for it (excluded from the build; see docs/engines.md) page widgets for it (excluded from the build; see docs/engines.md)
engines/asm86/ the x86-64 model machine, and the course's widgets that draw it (views, hwviews) engines/asm86/ the x86-64 model machine, and the course's widgets that draw it (views, hwviews)
@@ -89,6 +96,7 @@ src/
exercises/ one file per catalog problem exercises/ one file per catalog problem
curriculum/ a lesson corpus kept as an interpreter fixture curriculum/ a lesson corpus kept as an interpreter fixture
syntax/mojo.xml the Mojo words as a KDE syntax for Pandoc (generated; pplus-web copies it) syntax/mojo.xml the Mojo words as a KDE syntax for Pandoc (generated; pplus-web copies it)
library/ pplus-library, a git submodule: verified routines that programs bring in with USE
tests/ node --test against dist/; corpus/ (pseudo programs), prover/ (soundness regressions) tests/ node --test against dist/; corpus/ (pseudo programs), prover/ (soundness regressions)
mojo/ the Mojo corpus: the pin and lock, harvested material, our programs, godbolt's answers mojo/ the Mojo corpus: the pin and lock, harvested material, our programs, godbolt's answers
scripts/ the tasks above; scripts/mojo/ is the corpus pipeline; dafny.py the Dafny pin; cli.mjs scripts/ the tasks above; scripts/mojo/ is the corpus pipeline; dafny.py the Dafny pin; cli.mjs
@@ -103,6 +111,7 @@ docs/ the documents below
| [docs/engines.md](docs/engines.md) | The engines, the prover and its soundness, and how the course takes them | | [docs/engines.md](docs/engines.md) | The engines, the prover and its soundness, and how the course takes them |
| [docs/trust.md](docs/trust.md) | Every verdict core produces: which tool earns it, what it means, and what it does not mean | | [docs/trust.md](docs/trust.md) | Every verdict core produces: which tool earns it, what it means, and what it does not mean |
| [docs/mojo-corpus.md](docs/mojo-corpus.md) | Writing Mojo of our own and moving to a new Mojo version | | [docs/mojo-corpus.md](docs/mojo-corpus.md) | Writing Mojo of our own and moving to a new Mojo version |
| [docs/library.md](docs/library.md) | The library: USE, what each check of a routine means, and changing it |
| [docs/exercises.md](docs/exercises.md) | Adding a catalog problem | | [docs/exercises.md](docs/exercises.md) | Adding a catalog problem |
| [docs/sync-coverage.md](docs/sync-coverage.md) | What each translator can say, written by the tests | | [docs/sync-coverage.md](docs/sync-coverage.md) | What each translator can say, written by the tests |
| [docs/asm-coverage.md](docs/asm-coverage.md) | Which instructions compiled Mojo uses, against what the machine models | | [docs/asm-coverage.md](docs/asm-coverage.md) | Which instructions compiled Mojo uses, against what the machine models |
+22
View File
@@ -20,6 +20,25 @@ The prover is three TypeScript modules under strict mode; the rest of the engine
"Proved" is about the notation's own number model. INTEGER is a whole number of any size and a FLOAT is a finite number whose rounding the prover does not see into. A run of the interpreter stops where a value leaves that model (a whole number past 2^53 − 1, a FLOAT that is not finite, a division by zero), so every run stays inside what a proof covers. The Mojo made from a program has other limits (a 64-bit `Int` that wraps, a `Float64` that becomes `inf` or `nan`), and a unit whose proof leans on the number model carries an `int-model` or `float-model` note saying so. "Proved" is about the notation's own number model. INTEGER is a whole number of any size and a FLOAT is a finite number whose rounding the prover does not see into. A run of the interpreter stops where a value leaves that model (a whole number past 2^53 − 1, a FLOAT that is not finite, a division by zero), so every run stays inside what a proof covers. The Mojo made from a program has other limits (a 64-bit `Int` that wraps, a `Float64` that becomes `inf` or `nan`), and a unit whose proof leans on the number model carries an `int-model` or `float-model` note saying so.
### Numbers
Pseudo is meant for numerical work, so its number model is stated exactly ([the reference](../src/common/reference.ts) shows every form with a runnable example):
| Type | Holds | When a result does not fit | The prover |
|---|---|---|---|
| `INTEGER` | A whole number of any size; a run holds it exactly to 2^53 − 1 | The run stops past 2^53 − 1 | Exact arithmetic of any size; an `int-model` note says the lab's and Mojo's limits are not part of the proof |
| `INT8` … `INT64`, `UINT8` … `UINT64` | Two's complement or unsigned, n bits (64-bit types held exactly to 2^53 − 1 in this lab) | The run stops (kind `overflow`); `WRAP(X, T)` wraps on purpose (up to 32 bits) | Exact arithmetic plus an `overflow` obligation on every result, store, parameter, RETURN and conversion: "fits INT8". After it, the range is a fact. A sized parameter and a sized variable cut by a loop keep their range |
| `FLOAT`, `FLOAT64` | IEEE 754 double | The run stops on infinity or NaN | `FLOAT+` and its kind are uninterpreted: the same operation on the same values gives the same result, nothing more |
| `FLOAT32` | IEEE 754 single: every result rounded to 24 bits (`Math.fround`, exact for +, −, ×, ÷ of singles) | As FLOAT | Its own uninterpreted functions (`F32+`, ...); a constant is rounded exactly; a value that becomes a FLOAT32 is `TOFLOAT32(v)`, about which nothing is assumed |
Rules the checker holds: two different sized types never mix silently (`mixed-width`, `mixed-precision`; convert with `INT32(X)`, `FLOAT32(X)`); a numeric literal takes the type of the sized value beside it, as in Mojo, and a whole-number literal that does not fit is an error before any run (`literal-range`); INTEGER may go into a sized type (checked when it happens), never a different sized type.
`USE IEEE` lets NaN and the infinities flow as IEEE 754 says (SQRT(−1.0) is NaN, 1.0 / 0.0 is infinity; whole-number division by zero still stops). The prover's reasoning about numbers does not hold for those values, so in such a program nothing is marked proved, and each unit carries an `ieee` note.
The numerical built-ins (`HYPOT`, `EXPM1`, `LOG1P`, `FMA`, `ULP`, `NEXT_AFTER`, `EXACT`, `IS_CLOSE`, `MAX_VALUE`, the bit operations, ...) are exact or correctly rounded: `src/engines/pseudo/numeric.js` computes `FMA` with one rounding by exact binary arithmetic, `EXACT(X)` gives every decimal digit a FLOAT holds, and `ULP` and `NEXT_AFTER` step through the representation. Functions JavaScript has no exact version of (erf, gamma) are not built-ins; they belong in the library as verified pseudo. Built-ins that stop the run for some arguments (SQRT, the logarithms, ASIN and ACOS, LOG1P, CLAMP, the shifts) carry an `argument` obligation. The built-ins added for numerical work stay usable as names (a variable `E`, a `FUNCTION GCD`): the program's own definition wins (`SOFT_BUILTINS` in `lang.js`).
`tests/numeric.test.mjs` pins each rule; the corpus programs `49-sized-integers` to `52-elementary` show them in use.
### Soundness ### Soundness
A "proved" must be a proof. Two test files hold the prover to that, and a change to `logic.ts`, `solver.ts` or `vcgen.ts` must keep both green: A "proved" must be a proof. Two test files hold the prover to that, and a change to `logic.ts`, `solver.ts` or `vcgen.ts` must keep both green:
@@ -36,6 +55,9 @@ When a soundness bug is found, add the program that shows it to `tests/prover/`
| `fill-negative.pseudo`, `randint-empty.pseudo` | `FILL` and `RANDINT` assumed facts about arguments the interpreter checks by stopping, without asking whether they hold | An `argument` obligation, and guarded facts | | `fill-negative.pseudo`, `randint-empty.pseudo` | `FILL` and `RANDINT` assumed facts about arguments the interpreter checks by stopping, without asking whether they hold | An `argument` obligation, and guarded facts |
| `float-overflow.pseudo` | The prover treats FLOAT as numbers (`Y == Y`), while a run went on with infinity and NaN | The interpreter stops on a FLOAT that is not finite | | `float-overflow.pseudo` | The prover treats FLOAT as numbers (`Y == Y`), while a run went on with infinity and NaN | The interpreter stops on a FLOAT that is not finite |
| `foreach-empty.pseudo` | The way out of `FOR EACH` kept "X is in the collection", false after zero passes over an empty list | The fact holds only inside a pass | | `foreach-empty.pseudo` | The way out of `FOR EACH` kept "X is in the collection", false after zero passes over an empty list | The fact holds only inside a pass |
| `int8-overflow.pseudo` | (a guard written with the sized types) Sized arithmetic treated as exact with no range would prove `X + 1` fits INT8 | Every sized result has an `overflow` obligation |
| `float32-store.pseudo` | (a guard) A whole number stored in a FLOAT32 keeping its exact value would prove `X == N` for N = 16777217 | A value becoming a FLOAT32 is rounded (constants) or `TOFLOAT32(v)` |
| `ieee-nan.pseudo` | (a guard) `X == X` for every number fails for NaN | Nothing is proved in a program that says `USE IEEE` |
In the app, an obligation the prover marks proved that a generated input breaks at the same place is shown as broken, not proved. In the app, an obligation the prover marks proved that a generated input breaks at the same place is shown as broken, not proved.
+43
View File
@@ -0,0 +1,43 @@
# The library
pplus-library (the submodule `library/`) holds routines written once in pseudo and checked at every level, so a program can use them by name instead of rewriting and re-verifying them, and a learner can open any of them to see exactly what it does. Core reads it through a generated registry (`src/library/generated.ts`, `pixi run library-emit`), so the site needs no files at run time.
## Using a routine
```
USE md // every routine of the module
USE align.MatchScore // one routine
```
USE lines go at the top of a program. From then on a routine is called like the program's own:
- **The checker** types each call against the routine's parameters. A program may not define a routine of the same name (`library-clash`); an unknown module or routine is an error at the USE line, with the names that exist (`unknown-library`, `unknown-routine`).
- **A run** steps over a routine: one step where it is called, one where it hands back, and none of its own lines (they belong to another file). Its contracts are still checked; a broken REQUIRE is reported at the call, naming the routine. To step through a routine, open it in the library.
- **The prover** uses the routine's REQUIRE as an obligation at each call and its ENSURE as a fact afterwards, the way it treats the program's own routines. It does not prove the routine again: the library did.
- **The Mojo translator** writes each routine the program uses once, before the program's own code, dependencies first, labeled as coming from the library.
A routine file (`routine.pseudo`) holds USE lines and exactly one FUNCTION or PROCEDURE. A routine that calls another brings it in with USE, so every function in the library is checked on its own.
## What "verified" means for a routine
`pixi run library-check` runs every check and records what it found in `library/status.json` and `library/STATUS.md`:
| Check | Passes when | Recorded as |
|---|---|---|
| structure | `routine.pseudo` defines exactly the routine `routine.json` names, and nothing else | pass, fail |
| pseudo | every case runs; its result is recorded and is what the references are held to | pass, fail |
| prover | every obligation of the routine is proved | pass; open (some not proved, none broken); fail (one broken) |
| fuzz | 300 generated inputs break no contract and stop no run | pass, fail, skipped |
| mojo | the Mojo generated from the pseudo checks, runs in core's interpreter and prints what the pseudo prints | pass, fail, skipped (a construct the interpreter declines) |
| a reference, per runner | `cpython`, `cc -O0`, `cc -O2`, core's Mojo `interpreter`, or the pinned compiler on `godbolt` prints the pseudo's results on every case, exactly or within the tolerance `routine.json` states | pass, fail, skipped (the tool is missing), unverified (no stored compiler answer for this exact program) |
| proof | the pinned Dafny verifies the proof file, or its stored verdict matches this exact text | pass, fail, unverified |
A reference implementation is the function alone; the tool writes the program it runs as (the function plus a main that prints each case), and `pixi run library-harness <id> <lang> <level>` prints it. A Mojo reference's answer from the compiler is stored in the routine's `golden/mojo-<level>.json`, keyed by the SHA-256 of that program, by `pixi run library-verify-mojo` (network). A Dafny verdict is stored in `golden/dafny.json`, keyed by the proof's text and the Dafny version.
What each check does not say is in [trust.md](trust.md#library-routines).
## Changing the library
1. Change pplus-library under its [AGENTS.md](../library/AGENTS.md): one routine per file, cases, references with their notes and tolerances, a Dafny proof where one says something.
2. In core: `pixi run library-check` (no check may fail), then `pixi run library-emit`.
3. Commit pplus-library; commit core with the new `library` pointer and the regenerated registry. `tests/library.test.mjs` fails while the registry is out of date with the checkout.
+9 -7
View File
@@ -2,13 +2,13 @@
<!-- Written by tests/sync-coverage.test.mjs. Do not edit; rerun `npm test` to refresh. --> <!-- Written by tests/sync-coverage.test.mjs. Do not edit; rerun `npm test` to refresh. -->
Each translator in `src/sync/` is run over every exercise with pseudo (17), every program in `tests/corpus/` (48), and 17 one-construct fixtures (`tests/helpers/sync-programs.mjs`). 3 of the 82 have pseudo that does not read, by design (they teach the notation's error messages), and are left out below. That leaves **79 programs**. Each translator in `src/sync/` is run over every exercise with pseudo (17), every program in `tests/corpus/` (53), and 17 one-construct fixtures (`tests/helpers/sync-programs.mjs`). 3 of the 87 have pseudo that does not read, by design (they teach the notation's error messages), and are left out below. That leaves **84 programs**.
| From pseudo to | Says it completely | Says it with a gap or a checker error | Cannot say it | | From pseudo to | Says it completely | Says it with a gap or a checker error | Cannot say it |
| --- | ---: | ---: | ---: | | --- | ---: | ---: | ---: |
| Mojo | 77 | 2 | 0 | | Mojo | 81 | 3 | 0 |
| lingo | 31 | 0 | 48 | | lingo | 31 | 0 | 53 |
| matho | 3 | 0 | 76 | | matho | 3 | 0 | 81 |
Mojo is a program-for-program translation that always produces something: a line with no faithful spelling becomes a visible `raise Error("not translated: pseudo line N")` and a warning. Lingo and matho must read back as the same program, so what they cannot say exactly they refuse, with the construct and its line, and the pane keeps its last good text. Mojo is a program-for-program translation that always produces something: a line with no faithful spelling becomes a visible `raise Error("not translated: pseudo line N")` and a warning. Lingo and matho must read back as the same program, so what they cannot say exactly they refuse, with the construct and its line, and the pane keeps its last good text.
@@ -16,7 +16,9 @@ Mojo is a program-for-program translation that always produces something: a line
These programs translate, with the listed lines left as a `raise Error`. These programs translate, with the listed lines left as a `raise Error`.
Nothing. | What it has no form for | Programs | Which |
| --- | ---: | --- |
| Programming+ could not translate this line into Mojo (the built-in EXACT, which Mojo's standard library has no function for). The generated code stops here with an error instead of guessing. | 1 | 50-float32.pseudo |
## Mojo: programs the translator refuses ## Mojo: programs the translator refuses
@@ -30,7 +32,7 @@ English is read by `readSteps`, whose grammar has no sentence for these.
| What it has no form for | Programs | Which | | What it has no form for | Programs | Which |
| --- | ---: | --- | | --- | ---: | --- |
| steps outside a routine (every English step belongs to a routine) | 46 | 01-arithmetic.pseudo, 02-temperature.pseudo, 03-compound-interest.pseudo, 04-gcd-recursion.pseudo, and 42 more | | steps outside a routine (every English step belongs to a routine) | 51 | 01-arithmetic.pseudo, 02-temperature.pseudo, 03-compound-interest.pseudo, 04-gcd-recursion.pseudo, and 47 more |
| a fixed-length ARRAY | 4 | ex:edit-distance, 05-primes-sieve.pseudo, 11-matrix-multiply.pseudo, 15-game-of-life.pseudo | | a fixed-length ARRAY | 4 | ex:edit-distance, 05-primes-sieve.pseudo, 11-matrix-multiply.pseudo, 15-game-of-life.pseudo |
| a BYREF parameter | 3 | 06-fibonacci-memo.pseudo, 10-insertion-sort.pseudo, 26-swap-byref.pseudo | | a BYREF parameter | 3 | 06-fibonacci-memo.pseudo, 10-insertion-sort.pseudo, 26-swap-byref.pseudo |
| a HASHMAP whose values are not counts | 3 | ex:translate-codons, 13-stack-brackets.pseudo, 39-constants-codons.pseudo | | a HASHMAP whose values are not counts | 3 | ex:translate-codons, 13-stack-brackets.pseudo, 39-constants-codons.pseudo |
@@ -62,7 +64,7 @@ An equation lowers to one `FUNCTION` that returns a value; a program with output
| What it has no form for | Programs | Which | | What it has no form for | Programs | Which |
| --- | ---: | --- | | --- | ---: | --- |
| steps outside a function (an equation is a function) | 42 | 01-arithmetic.pseudo, 02-temperature.pseudo, 03-compound-interest.pseudo, 04-gcd-recursion.pseudo, and 38 more | | steps outside a function (an equation is a function) | 47 | 01-arithmetic.pseudo, 02-temperature.pseudo, 03-compound-interest.pseudo, 04-gcd-recursion.pseudo, and 43 more |
| a function that answers LIST (equations answer numbers) | 5 | ex:match, ex:match-proved, ex:match-off-by-one, ex:match-reset, and 1 more | | a function that answers LIST (equations answer numbers) | 5 | ex:match, ex:match-proved, ex:match-off-by-one, ex:match-reset, and 1 more |
| a function that answers STRING (equations answer numbers) | 5 | ex:revcomp, fixture:string-build, fixture:slice, fixture:case, and 1 more | | a function that answers STRING (equations answer numbers) | 5 | ex:revcomp, fixture:string-build, fixture:slice, fixture:case, and 1 more |
| a function that answers BOOL (equations answer numbers) | 3 | ex:binary-search, fixture:early-return, fixture:forall | | a function that answers BOOL (equations answer numbers) | 3 | ex:binary-search, fixture:early-return, fixture:forall |
+15 -1
View File
@@ -29,7 +29,7 @@ The Prove tab reports every obligation in three statuses (`src/engines/pseudo/vc
| Shown | Earned by | Means | Does not mean | | Shown | Earned by | Means | Does not mean |
|---|---|---|---| |---|---|---|---|
| **Proved** | The built-in prover (`solver.ts`), on every path it examined, in a unit it examined completely | The claim follows from what is known, in the notation's number model: INTEGER as whole numbers of any size, FLOAT as finite numbers whose rounding is not seen into. The prover is sound (`tests/prover-soundness.test.mjs`) | That the Mojo made from the program has the same property. A 64-bit `Int` wraps and a `Float64` becomes `inf` or `nan`; the `int-model` and `float-model` notes say when a proof leans on the number model. A unit marked **partial** shows no claim as proved | | **Proved** | The built-in prover (`solver.ts`), on every path it examined, in a unit it examined completely | The claim follows from what is known, in the notation's number model: INTEGER as whole numbers of any size, the sized types (INT8 … UINT64) exactly, with an `overflow` obligation wherever a value could leave its range, FLOAT and FLOAT32 as finite numbers whose rounding is not seen into. The prover is sound (`tests/prover-soundness.test.mjs`). In a program that says `USE IEEE` nothing is marked proved | That the Mojo made from the program has the same property. A 64-bit `Int` wraps and a `Float64` becomes `inf` or `nan`; the `int-model` and `float-model` notes say when a proof leans on the number model. A unit marked **partial** shows no claim as proved |
| **Broken** | A generated input (the fuzzer, seeded) or a run that breaks the claim at that place | There is an input for which the claim is false, and **Step through** shows it | Anything about other claims. A run outranks the prover: a proved obligation that an input breaks is shown as broken (and would be a soundness bug) | | **Broken** | A generated input (the fuzzer, seeded) or a run that breaks the claim at that place | There is an input for which the claim is false, and **Step through** shows it | Anything about other claims. A run outranks the prover: a proved obligation that an input breaks is shown as broken (and would be a soundness bug) |
| **Open** | Neither of the above | The checker could not finish the argument, usually because a loop needs an INVARIANT | That the claim is false | | **Open** | Neither of the above | The checker could not finish the argument, usually because a loop needs an INVARIANT | That the claim is false |
| An invariant "the checker found on its own" | Inference in `vcgen.ts`, kept only at a fixed point | It holds on entry and survives every pass, and the proof used it | That it is the invariant a reader needs | | An invariant "the checker found on its own" | Inference in `vcgen.ts`, kept only at a fixed point | It holds on entry and survives every pass, and the proof used it | That it is the invariant a reader needs |
@@ -55,3 +55,17 @@ The Prove tab reports every obligation in three statuses (`src/engines/pseudo/vc
|---|---|---|---| |---|---|---|---|
| Registers, caches, pipeline and counters in the Hardware area | The model machine (`src/engines/asm86/`), on the preset machine named in Setup | What that model does with these instructions | A measurement of a real processor. Cycle counts are a teaching model; compare a run with itself across edits | | Registers, caches, pipeline and counters in the Hardware area | The model machine (`src/engines/asm86/`), on the preset machine named in Setup | What that model does with these instructions | A measurement of a real processor. Cycle counts are a teaching model; compare a run with itself across edits |
| "stopped: … standard library, not on the model machine" | A call into a function the model does not have | The real program would enter the library here | That the library call does anything in particular | | "stopped: … standard library, not on the model machine" | A call into a function the model does not have | The real program would enter the library here | That the library call does anything in particular |
## Library routines
A routine of pplus-library (`USE md`) carries the record of `pixi run library-check` ([library.md](library.md)).
| Shown | Earned by | Means | Does not mean |
|---|---|---|---|
| **Cases pass** | Running the pseudo on the cases in `routine.json` | The routine gives those results; they are what every reference is held to | That the results are right for other inputs |
| **Prover: pass** | Every obligation of the routine proved, as for any pseudo | Its contracts hold for every input that meets its REQUIRE, in the notation's number model | Anything about the value of FLOAT arithmetic beyond what the contracts say |
| **Prover: open** | Some obligation not proved, none broken | The routine's comment says which and why (usually FLOAT rounding, which is opaque to the prover) | That it is wrong |
| **A reference agrees** | Python, C at -O0 and -O2, core's Mojo interpreter, or the pinned compiler, run on the cases | That implementation, at that level, prints the pseudo's results exactly, or within the stated tolerance | That it agrees on other inputs, or at other optimization flags |
| **Mojo: unverified** | No stored answer from the pinned compiler for that exact program | Not yet asked (`pixi run library-verify-mojo`) | That it fails |
| **Proof: pass** | The pinned Dafny verified the proof file (or its stored verdict for this exact text) | The method is correct over exact numbers, as the proof states it | That the FLOAT version computes the same digits: rounding is outside the proof |
Submodule
+1
Submodule library added at df12b2a008.
+7 -1
View File
@@ -34,6 +34,12 @@ engines-export = { cmd = "node scripts/engines-export.mjs {{ static }}", args =
dafny-fetch = { cmd = "python scripts/dafny.py fetch", description = "Download the pinned Dafny into .tools/, checked by SHA-256 (network, once)" } dafny-fetch = { cmd = "python scripts/dafny.py fetch", description = "Download the pinned Dafny into .tools/, checked by SHA-256 (network, once)" }
dafny = { cmd = "python scripts/dafny.py", description = "Run the pinned Dafny with the arguments given, e.g. pixi run dafny verify file.dfy (fetches it first if missing)" } dafny = { cmd = "python scripts/dafny.py", description = "Run the pinned Dafny with the arguments given, e.g. pixi run dafny verify file.dfy (fetches it first if missing)" }
# --- the library (library/, the pplus-library submodule; docs/library.md) ---
library-check = { cmd = "node scripts/library.mjs check", depends-on = ["build"], description = "Check every library routine at every level (cases, prover, fuzzer, generated Mojo, every reference, the Dafny proof); writes library/status.json and STATUS.md" }
library-emit = { cmd = "node scripts/library.mjs emit", depends-on = ["build"], description = "Regenerate src/library/generated.ts, the registry USE reads, from library/" }
library-verify-mojo = { cmd = "node scripts/library.mjs mojo-sources | python scripts/library_godbolt.py", depends-on = ["build"], description = "Ask the pinned Mojo compiler about every Mojo reference of the library and store the answers (network)" }
library-harness = { cmd = "node scripts/library.mjs harness {{ id }} {{ lang }} {{ level }}", args = ["id", "lang", "level"], depends-on = ["build"], description = "Print the program a reference implementation is run as, e.g. pixi run library-harness md.LennardJones c multiply" }
# --- write content --------------------------------------------------------- # --- write content ---------------------------------------------------------
new-exercise = { cmd = "node scripts/new-exercise.mjs {{ id }}", args = ["id"], description = "Start an exercise: mojo/local/<id>.mojo and src/exercises/<id>.ts, registered in index.ts" } new-exercise = { cmd = "node scripts/new-exercise.mjs {{ id }}", args = ["id"], description = "Start an exercise: mojo/local/<id>.mojo and src/exercises/<id>.ts, registered in index.ts" }
new-probe = { cmd = "node scripts/mojo/new-program.mjs probe {{ name }} \"{{ question }}\"", args = ["name", "question"], description = "Start mojo/probes/<name>.mojo: one question about the pinned compiler" } new-probe = { cmd = "node scripts/mojo/new-program.mjs probe {{ name }} \"{{ question }}\"", args = ["name", "question"], description = "Start mojo/probes/<name>.mojo: one question about the pinned compiler" }
@@ -59,5 +65,5 @@ mojo-probes-local = { cmd = "for f in mojo/probes/*.mojo; do echo \"== $f\"; moj
mojo-update = { depends-on = ["mojo-harvest", "mojo-verify", "mojo-status", "test"], description = "After editing mojo/pin.json: harvest, verify, status, test" } mojo-update = { depends-on = ["mojo-harvest", "mojo-verify", "mojo-status", "test"], description = "After editing mojo/pin.json: harvest, verify, status, test" }
# --- ship ------------------------------------------------------------------ # --- ship ------------------------------------------------------------------
ci = { depends-on = ["test", "mojo-status-strict"], description = "What CI runs: every test, and no program of ours failing" } ci = { depends-on = ["test", "mojo-status-strict"], description = "What CI runs: every test (the library's registry up to date among them), and no program of ours failing" }
package = { cmd = "python scripts/package.py", description = "Zip the files git tracks (and new ones it does not ignore) into package/ for sharing" } package = { cmd = "python scripts/package.py", description = "Zip the files git tracks (and new ones it does not ignore) into package/ for sharing" }
+460
View File
@@ -0,0 +1,460 @@
#!/usr/bin/env node
/**
* The library's tools: check every routine of pplus-library (the submodule library/) at every level, record
* what was found in the library, and generate core's registry from it.
*
* node scripts/library.mjs check [--dir library] [--only stats.Mean] every check; writes status.json, STATUS.md, goldens
* node scripts/library.mjs emit [--check] library/ -> src/library/generated.ts
* node scripts/library.mjs harness <id> <lang> <level> print the program a reference is run as
* node scripts/library.mjs mojo-sources [--out dir] write each Mojo reference's harness, for godbolt
*
* The checks of one routine:
* structure routine.pseudo defines exactly the routine routine.json names, and nothing else (USE lines aside)
* pseudo every case runs; its result is recorded and becomes what the references are held to
* prover every obligation of the routine: pass when all are proved, open when some are not, fail when broken
* fuzz the fuzzer finds no input that breaks a contract or stops the run
* mojo the Mojo generated from the pseudo checks, runs in core's interpreter and prints what the pseudo prints
* and for each reference implementation, per way of running it:
* cpython python3 runs it on every case cc -O0, cc -O2 cc compiles and runs it on every case
* interpreter core's Mojo interpreter runs it godbolt the stored answer of the pinned compiler
* and the proof, if there is one: the pinned Dafny verifies it (or its stored verdict for this exact text).
*
* Needs a build (dist/). Python and a C compiler are used when they are on the PATH; without them those checks
* are recorded as skipped, never as passed.
*/
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import crypto from 'node:crypto';
import { execFileSync, spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
const CORE = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const args = process.argv.slice(2);
const cmd = args[0];
const opt = (name, dflt = null) => { const i = args.indexOf(`--${name}`); return i >= 0 ? args[i + 1] : dflt; };
const flag = (name) => args.includes(`--${name}`);
const LIB = path.resolve(opt('dir', path.join(CORE, 'library')));
const GENERATED = path.join(CORE, 'src', 'library', 'generated.ts');
const sha256 = (s) => crypto.createHash('sha256').update(s).digest('hex');
const readJson = (p) => JSON.parse(fs.readFileSync(p, 'utf8'));
const writeIfChanged = (p, text) => { if (!fs.existsSync(p) || fs.readFileSync(p, 'utf8') !== text) { fs.mkdirSync(path.dirname(p), { recursive: true }); fs.writeFileSync(p, text); return true; } return false; };
/* ------------------------------------------------------------------ */
/* Reading the library */
/* ------------------------------------------------------------------ */
/** Every module and routine on disk, with its sources, and the status last recorded for it. */
export function load(dir = LIB) {
if (!fs.existsSync(path.join(dir, 'modules'))) throw new Error(`no library at ${dir}: check out the submodule (git submodule update --init) or pass --dir`);
const status = fs.existsSync(path.join(dir, 'status.json')) ? readJson(path.join(dir, 'status.json')).routines || {} : {};
const modules = [];
for (const m of fs.readdirSync(path.join(dir, 'modules')).sort()) {
const mdir = path.join(dir, 'modules', m);
if (!fs.existsSync(path.join(mdir, 'module.json'))) continue;
const meta = readJson(path.join(mdir, 'module.json'));
const routines = [];
for (const r of fs.readdirSync(mdir).sort()) {
const rdir = path.join(mdir, r);
if (!fs.existsSync(path.join(rdir, 'routine.json'))) continue;
const rm = readJson(path.join(rdir, 'routine.json'));
const id = `${meta.name}.${rm.name}`;
const st = status[id] || {};
routines.push({
id, module: meta.name, name: rm.name, dir: path.relative(dir, rdir).split(path.sep).join('/'),
summary: rm.summary || '', tags: rm.tags || [], sources: rm.sources || [],
source: fs.readFileSync(path.join(rdir, 'routine.pseudo'), 'utf8'),
cases: (rm.cases || []).map((c, i) => ({ ...c, ...(st.cases?.[i]?.call === c.call ? { result: st.cases[i].result } : {}) })),
references: (rm.references || []).map((ref) => ({
lang: ref.lang, level: ref.level, file: ref.file, note: ref.note || '',
...(ref.rel !== undefined ? { rel: ref.rel } : {}), ...(ref.abs !== undefined ? { abs: ref.abs } : {}),
source: fs.readFileSync(path.join(rdir, ref.file), 'utf8'),
checks: st.references?.[`${ref.lang}/${ref.level}`] || {},
})),
...(rm.proof ? { proof: { lang: rm.proof.lang, file: rm.proof.file, note: rm.proof.note || '', source: fs.readFileSync(path.join(rdir, rm.proof.file), 'utf8'), check: st.proof || { status: 'unverified', detail: 'not checked yet' } } } : {}),
checks: st.checks || {},
});
}
modules.push({ name: meta.name, title: meta.title, summary: meta.summary, routines });
}
return modules;
}
/* ------------------------------------------------------------------ */
/* Values in other languages */
/* ------------------------------------------------------------------ */
const floatText = (v) => { const s = String(v); return /[.e]/.test(s) || !Number.isFinite(v) ? s : `${s}.0`; };
const items = (v) => (Array.isArray(v) ? v : v?.data ? v.data : []);
function pyLiteral(v, t) {
switch (t?.k) {
case 'FLOAT': return floatText(v).replace('e+', 'e');
case 'INTEGER': return String(v);
case 'BOOL': return v ? 'True' : 'False';
case 'CHAR': case 'STRING': return JSON.stringify(v);
case 'LIST': case 'ARRAY': return `[${items(v).map((x) => pyLiteral(x, t.of)).join(', ')}]`;
default: throw new Error(`no Python literal for ${t?.k}`);
}
}
function mojoType(t) {
switch (t?.k) {
case 'FLOAT': return t.bits === 32 ? 'Float32' : 'Float64';
case 'INTEGER': return t.bits ? `${t.signed ? 'Int' : 'UInt'}${t.bits}` : 'Int';
case 'BOOL': return 'Bool';
case 'CHAR': case 'STRING': return 'String';
case 'LIST': case 'ARRAY': return `List[${mojoType(t.of)}]`;
default: throw new Error(`no Mojo type for ${t?.k}`);
}
}
function mojoLiteral(v, t) {
switch (t?.k) {
case 'FLOAT': return floatText(v).replace('e+', 'e');
case 'INTEGER': return String(v);
case 'BOOL': return v ? 'True' : 'False';
case 'CHAR': case 'STRING': return JSON.stringify(v);
case 'LIST': case 'ARRAY': return `${mojoType(t)}(${items(v).map((x) => mojoLiteral(x, t.of)).join(', ')})`;
default: throw new Error(`no Mojo literal for ${t?.k}`);
}
}
/** A C argument list for one call, with the declarations it needs first: a list becomes (array, length). */
function cArgs(vals, params, tag) {
const decls = []; const out = [];
vals.forEach((v, i) => {
const t = params[i].type;
switch (t?.k) {
case 'FLOAT': out.push(floatText(v)); break;
case 'INTEGER': out.push(`${v}L`); break;
case 'BOOL': out.push(v ? '1' : '0'); break;
case 'CHAR': out.push(`'${v.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`); break;
case 'STRING': out.push(JSON.stringify(v)); break;
case 'LIST': case 'ARRAY': {
const xs = items(v);
if (t.of?.k !== 'FLOAT' && t.of?.k !== 'INTEGER') throw new Error('C lists hold numbers only');
const ct = t.of.k === 'FLOAT' ? 'double' : 'long';
const name = `a${tag}_${i}`;
decls.push(`${ct} ${name}[${Math.max(1, xs.length)}] = {${xs.length ? xs.map((x) => (t.of.k === 'FLOAT' ? floatText(x) : `${x}L`)).join(', ') : '0'}};`);
out.push(name, `${xs.length}L`);
break;
}
default: throw new Error(`no C argument for ${t?.k}`);
}
});
return { decls, args: out };
}
/* ------------------------------------------------------------------ */
/* The checks */
/* ------------------------------------------------------------------ */
const ok = (detail) => ({ status: 'pass', detail });
const bad = (detail) => ({ status: 'fail', detail });
const skip = (detail) => ({ status: 'skipped', detail });
const open = (detail) => ({ status: 'open', detail });
const unverified = (detail) => ({ status: 'unverified', detail });
function have(cmdName) { return spawnSync(cmdName, ['--version'], { encoding: 'utf8' }).status === 0; }
/** Does a printed value agree with the pseudo's, within the reference's tolerance? */
function agrees(printed, want, t, rel = 0, abs = 0) {
const s = String(printed).trim();
if (t?.k === 'FLOAT' || t?.k === 'INTEGER') {
const got = Number(s.replace(/^inf$/i, 'Infinity').replace(/^-inf$/i, '-Infinity').replace(/^nan$/i, 'NaN'));
if (Number.isNaN(got) && Number.isNaN(want)) return true;
if (Number.isNaN(got)) return false;
if (got === want) return true;
return Math.abs(got - want) <= Math.max(rel * Math.max(Math.abs(got), Math.abs(want)), abs);
}
if (t?.k === 'BOOL') return ['true', '1'].includes(s.toLowerCase()) === Boolean(want);
return s === String(want);
}
function compareLines(stdout, wants, t, rel, abs) {
const got = stdout.replace(/\n$/, '').split('\n');
if (got.length !== wants.length) return `printed ${got.length} line(s) for ${wants.length} case(s)`;
for (let i = 0; i < wants.length; i++) if (!agrees(got[i], wants[i].value, t, rel, abs)) return `case ${i + 1} (${wants[i].call}) printed ${got[i]}, and the pseudo gives ${wants[i].text}`;
return null;
}
/** The program a reference implementation is run as: its source, plus a main that prints each case. */
function harness(lang, ref, fname, sub, calls) {
const params = sub.params;
if (lang === 'python') {
return `import importlib.util\nspec = importlib.util.spec_from_file_location("ref", __REF__)\nref = importlib.util.module_from_spec(spec)\nspec.loader.exec_module(ref)\n${calls.map((c) => `print(repr(ref.${fname}(${c.args.map((v, i) => pyLiteral(v, params[i].type)).join(', ')})))`).join('\n')}\n`;
}
if (lang === 'mojo') {
return `${ref.source.replace(/\s+$/, '')}\n\n\ndef main() raises:\n${calls.map((c) => ` print(${fname}(${c.args.map((v, i) => mojoLiteral(v, params[i].type)).join(', ')}))`).join('\n')}\n`;
}
if (lang === 'c') {
const fmt = sub.returns?.k === 'FLOAT' ? '%.17g' : '%ld';
const body = calls.map((c, k) => { const a = cArgs(c.args, params, k); return ` { ${a.decls.join(' ')} printf("${fmt}\\n", ${fname}(${a.args.join(', ')})); }`; }).join('\n');
return `#include <stdio.h>\n\n${ref.source.replace(/\s+$/, '')}\n\nint main(void) {\n${body}\n return 0;\n}\n`;
}
throw new Error(`no harness for ${lang}`);
}
export async function checkLibrary(dir = LIB, only = null) {
const { setLibrary } = await import(path.join(CORE, 'dist/library/registry.js'));
const svc = await import(path.join(CORE, 'dist/services/pseudo.js'));
const { parseExpression } = await import(path.join(CORE, 'dist/engines/pseudo/parse.js'));
const { evaluateConstant } = await import(path.join(CORE, 'dist/engines/pseudo/interp.js'));
const { formatValue } = await import(path.join(CORE, 'dist/engines/pseudo/lang.js'));
const { pseudoToMojo, snake } = await import(path.join(CORE, 'dist/sync/pseudo-to-mojo.js'));
const { checkMojo } = await import(path.join(CORE, 'dist/mojo/check.js'));
const { runMojo } = await import(path.join(CORE, 'dist/mojo/interp.js'));
const { sameOutput } = await import(path.join(CORE, 'dist/sync/compare.js'));
const modules = load(dir);
setLibrary(modules);
const pin = readJson(path.join(CORE, 'mojo', 'pin.json'));
const dafny = (() => { try { const p = execFileSync('python3', [path.join(CORE, 'scripts', 'dafny.py'), 'path'], { encoding: 'utf8' }).trim(); return fs.existsSync(p) ? p : null; } catch { return null; } })();
const dafnyVersion = (() => { try { return readJson(path.join(CORE, 'scripts', 'dafny.json')).version; } catch { return null; } })() || (() => { const m = /VERSION = "([^"]+)"/.exec(fs.readFileSync(path.join(CORE, 'scripts', 'dafny.py'), 'utf8')); return m ? m[1] : 'unknown'; })();
const python = have('python3');
const cc = have('cc');
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'pplus-library-'));
const out = {};
for (const m of modules) {
for (const r of m.routines) {
if (only && r.id !== only) { continue; }
const rdir = path.join(dir, r.dir);
const rec = { checks: {}, cases: [], references: {}, proof: undefined };
out[r.id] = rec;
const st = svc.studyOf(r.source);
const program = st.program?.program;
// structure
const subs = program ? [...program.subs.values()] : [];
if (!st.ok) { rec.checks.structure = bad(`the pseudo does not check: ${st.diagnostics.filter((d) => d.severity === 'error').map((d) => `line ${d.line}: ${d.message}`).join('; ')}`); continue; }
if (subs.length !== 1 || subs[0].name.toUpperCase() !== r.name.toUpperCase()) { rec.checks.structure = bad(`routine.pseudo defines ${subs.map((s) => s.name).join(', ') || 'nothing'}; it must define exactly ${r.name} (helpers are routines of their own, brought in with USE)`); continue; }
if (program.main.body.length) { rec.checks.structure = bad(`routine.pseudo has lines outside ${r.name} (line ${program.main.body[0].line}); a routine file holds USE lines and the routine only`); continue; }
const sub = subs[0];
rec.checks.structure = ok(`defines ${sub.kind === 'Function' ? 'FUNCTION' : 'PROCEDURE'} ${sub.name}${program.libs?.size ? `, using ${[...program.libs.values()].map((x) => x.lib.id).join(', ')}` : ''}`);
// pseudo: every case
const calls = [];
let caseFail = null;
for (const c of r.cases) {
const p = parseExpression(c.call);
let entry = { call: c.call };
try {
if (p.diags.some((d) => d.severity === 'error') || p.expr.kind !== 'CallExpr' || p.expr.name.toUpperCase() !== r.name.toUpperCase()) throw new Error(`"${c.call}" is not a call of ${r.name}`);
const vals = p.expr.args.map((a) => evaluateConstant(program, a));
const tr = svc.runPseudo(st, { call: c.call, maxSteps: 2000000 });
if (tr.error) throw new Error(`${c.call} stops: ${tr.error.message}`);
const result = tr.raw.result;
entry = { call: c.call, result: formatValue(result, sub.returns, { max: 1000 }) };
calls.push({ call: c.call, args: vals, value: result, text: entry.result });
} catch (e) { caseFail = caseFail || e.message; }
rec.cases.push(entry);
}
rec.checks.pseudo = caseFail ? bad(caseFail) : ok(`${calls.length} case(s) run`);
// prover
const pr = svc.provePseudo(st);
const obs = pr.units.flatMap((u) => u.obligations);
const broken = obs.filter((o) => o.status === 'broken');
const notProved = obs.filter((o) => o.status !== 'proved');
rec.checks.prover = broken.length ? bad(`${broken.length} obligation(s) broken: ${broken.map((o) => `${o.kind}@${o.line} (${o.label})`).join('; ')}`)
: notProved.length ? open(`${obs.length - notProved.length} of ${obs.length} obligation(s) proved; open: ${notProved.map((o) => `${o.kind}@${o.line} (${o.label})`).join('; ')}`)
: ok(`all ${obs.length} obligation(s) proved`);
// fuzz
const props = svc.propertiesPseudo(st, r.name)[0];
rec.checks.fuzz = !props ? skip('no routine to fuzz') : props.problem ? skip(`the fuzzer cannot generate inputs: ${props.problem}`)
: props.failures.length ? bad(`${props.failures.length} failing input(s), first: ${props.failures[0].call}: ${props.failures[0].message}`)
: ok(`no failure in ${props.checked} generated input(s)`);
// the Mojo generated from the pseudo, on every case
if (calls.length) {
const prog = `USE ${r.module}.${r.name}\n${calls.map((c) => `OUTPUT ${c.call}`).join('\n')}\n`;
const want = svc.runPseudo(svc.studyOf(prog), {}).output.join('\n');
const mj = pseudoToMojo(prog);
if ('unsupported' in mj) rec.checks.mojo = bad(`the translator refused: ${mj.unsupported.map((d) => d.message).join('; ')}`);
else {
const gaps = mj.notes.filter((n) => n.id === 'translate-gap');
const ck = checkMojo(mj.source);
const errs = ck.diagnostics.filter((d) => d.severity === 'error');
if (gaps.length) rec.checks.mojo = skip(`the translator has no Mojo for: ${gaps.map((g) => g.message.replace(/^.*?\(|\)\..*$/g, '')).join('; ')}`);
else if (errs.length) rec.checks.mojo = bad(`the generated Mojo has checker errors: ${errs.map((d) => `L${d.line} ${d.message}`).join(' | ')}`);
else {
const run = runMojo(ck.module, mj.source, { maxSteps: 4000000 });
rec.checks.mojo = run.refused ? skip(`core's Mojo interpreter declines ${run.refused}; the compiler's answer is needed`)
: run.exitCode !== 0 ? bad(`the generated Mojo stopped (exit ${run.exitCode})`)
: sameOutput(want, run.stdout) ? ok('the generated Mojo prints what the pseudo prints on every case, in core\'s interpreter') : bad(`the generated Mojo printed ${JSON.stringify(run.stdout.slice(0, 120))}; the pseudo printed ${JSON.stringify(want.slice(0, 120))}`);
}
}
}
// references
const fname = snake(r.name);
for (const ref of r.references) {
const checks = {};
rec.references[`${ref.lang}/${ref.level}`] = checks;
if (!calls.length) { checks.cases = skip('no case ran in pseudo, so there is nothing to hold it to'); continue; }
const rel = ref.rel ?? 0; const abs = ref.abs ?? 0;
const tol = rel || abs ? ` (within rel ${rel}, abs ${abs})` : ' (exactly)';
try {
if (ref.lang === 'python') {
if (!python) { checks.cpython = skip('python3 is not on the PATH'); continue; }
const h = harness('python', ref, fname, sub, calls).replace('__REF__', JSON.stringify(path.join(rdir, ref.file)));
const p = spawnSync('python3', ['-c', h], { encoding: 'utf8', timeout: 60000 });
if (p.status !== 0) { checks.cpython = bad(`python3 stopped: ${(p.stderr || '').trim().split('\n').pop()}`); continue; }
const diff = compareLines(p.stdout, calls, sub.returns, rel, abs);
checks.cpython = diff ? bad(diff) : ok(`agrees with the pseudo on ${calls.length} case(s)${tol}`);
} else if (ref.lang === 'c') {
if (!cc) { checks['cc -O0'] = skip('no C compiler (cc) on the PATH'); continue; }
const src = harness('c', ref, fname, sub, calls);
const file = path.join(tmp, `${fname}-${ref.level}.c`);
fs.writeFileSync(file, src);
for (const level of ['-O0', '-O2']) {
const bin = path.join(tmp, `${fname}-${ref.level}${level}`);
const c = spawnSync('cc', ['-std=c11', level, '-o', bin, file, '-lm'], { encoding: 'utf8' });
if (c.status !== 0) { checks[`cc ${level}`] = bad(`cc ${level} failed: ${(c.stderr || '').trim().split('\n')[0]}`); continue; }
const p = spawnSync(bin, [], { encoding: 'utf8', timeout: 60000 });
const diff = p.status !== 0 ? `exited ${p.status}` : compareLines(p.stdout, calls, sub.returns, rel, abs);
checks[`cc ${level}`] = diff ? bad(diff) : ok(`agrees with the pseudo on ${calls.length} case(s)${tol}`);
}
} else if (ref.lang === 'mojo') {
const src = harness('mojo', ref, fname, sub, calls);
const ck = checkMojo(src);
const errs = ck.diagnostics.filter((d) => d.severity === 'error');
if (errs.length) checks.interpreter = bad(`core's Mojo checker reports: ${errs.map((d) => `L${d.line} ${d.message}`).join(' | ')}`);
else {
const run = runMojo(ck.module, src, { maxSteps: 4000000 });
const diff = run.refused ? null : run.exitCode !== 0 ? `stopped (exit ${run.exitCode})` : compareLines(run.stdout, calls, sub.returns, rel, abs);
checks.interpreter = run.refused ? skip(`core's Mojo interpreter declines ${run.refused}`) : diff ? bad(diff) : ok(`agrees with the pseudo on ${calls.length} case(s) in core's interpreter${tol}`);
}
// the compiler's stored answer for this exact program
const gfile = path.join(rdir, 'golden', `mojo-${ref.level}.json`);
const g = fs.existsSync(gfile) ? readJson(gfile) : null;
if (!g || g.source_sha256 !== sha256(src) || g.compiler !== pin.godbolt.compiler) checks.godbolt = unverified(`no stored answer from ${pin.godbolt.compiler} for this program: pixi run library-verify-mojo`);
else if (!g.result?.compiled) checks.godbolt = bad(`${pin.godbolt.compiler} did not compile it: ${(g.result?.buildStderr || '').split('\n')[0]}`);
else {
const diff = g.result.exitCode !== 0 ? `exited ${g.result.exitCode}` : compareLines(g.result.stdout, calls, sub.returns, rel, abs);
checks.godbolt = diff ? bad(diff) : ok(`${pin.godbolt.compiler} agrees with the pseudo on ${calls.length} case(s)${tol}`);
}
} else checks.cases = skip(`no runner for ${ref.lang} yet`);
} catch (e) {
checks.harness = bad(`could not build the harness: ${e.message}`);
}
}
// proof
if (r.proof) {
const gfile = path.join(rdir, 'golden', 'dafny.json');
const src = r.proof.source;
const flags = ['verify', '--resource-limit', '20000000'];
if (dafny) {
const p = spawnSync(dafny, [...flags, path.join(rdir, r.proof.file)], { encoding: 'utf8', timeout: 600000 });
const summary = /Dafny program verifier finished with (.*)/.exec(p.stdout || '')?.[1] || (p.stdout || p.stderr || '').trim().split('\n').pop();
const pass = p.status === 0 && / 0 errors/.test(summary);
rec.proof = pass ? ok(`Dafny ${dafnyVersion}: ${summary}`) : bad(`Dafny ${dafnyVersion}: ${summary}`);
writeIfChanged(gfile, `${JSON.stringify({ tool: 'dafny', version: dafnyVersion, flags, source_sha256: sha256(src), verdict: pass ? 'verified' : 'failed', summary }, null, 2)}\n`);
} else {
const g = fs.existsSync(gfile) ? readJson(gfile) : null;
rec.proof = g && g.source_sha256 === sha256(src) ? (g.verdict === 'verified' ? ok(`Dafny ${g.version} (stored answer): ${g.summary}`) : bad(`Dafny ${g.version} (stored answer): ${g.summary}`))
: unverified('the pinned Dafny is not fetched (pixi run dafny-fetch) and there is no stored verdict for this text');
}
}
}
}
fs.rmSync(tmp, { recursive: true, force: true });
return { modules, results: out };
}
const ORDER = ['structure', 'pseudo', 'prover', 'fuzz', 'mojo'];
function statusMarkdown(modules, results) {
const mark = (c) => (!c ? '·' : { pass: 'pass', fail: '**fail**', open: 'open', skipped: 'skipped', unverified: 'unverified' }[c.status] || c.status);
const lines = ['# Library status', '', '<!-- Written by `pixi run library-check` in pplus-core. Do not edit. -->', '',
'Each routine, at each level. *pass*: checked and agrees; *open*: the prover could not finish every obligation (nothing is wrong, nothing is proved); *skipped*: the tool is not available here, or it declines the construct; *unverified*: no stored answer for this exact text yet.', ''];
for (const m of modules) {
lines.push(`## ${m.title} (\`${m.name}\`)`, '', '| Routine | Cases | Prover | Fuzz | Generated Mojo | References | Proof |', '| --- | --- | --- | --- | --- | --- | --- |');
for (const r of m.routines) {
const res = results[r.id];
if (!res) continue;
const refs = Object.entries(res.references).map(([k, checks]) => `${k}: ${Object.entries(checks).map(([how, c]) => `${how} ${mark(c)}`).join(', ')}`).join('<br>');
lines.push(`| \`${r.id}\` | ${mark(res.checks.pseudo)} | ${mark(res.checks.prover)} | ${mark(res.checks.fuzz)} | ${mark(res.checks.mojo)} | ${refs || '·'} | ${res.proof ? `dafny ${mark(res.proof)}` : '·'} |`);
}
lines.push('');
}
lines.push('## Details', '');
for (const m of modules) for (const r of m.routines) {
const res = results[r.id];
if (!res) continue;
lines.push(`### ${r.id}`, '');
for (const k of ORDER) if (res.checks[k]) lines.push(`- ${k}: ${res.checks[k].status}. ${res.checks[k].detail}`);
for (const [k, checks] of Object.entries(res.references)) for (const [how, c] of Object.entries(checks)) lines.push(`- ${k}, ${how}: ${c.status}. ${c.detail}`);
if (res.proof) lines.push(`- proof: ${res.proof.status}. ${res.proof.detail}`);
lines.push('');
}
return lines.join('\n');
}
/* ------------------------------------------------------------------ */
/* Emitting core's registry */
/* ------------------------------------------------------------------ */
function emitText(dir = LIB) {
const modules = load(dir);
let commit = '';
try { commit = execFileSync('git', ['-C', dir, 'rev-parse', '--short=12', 'HEAD'], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim(); } catch { /* not a checkout */ }
return `// Generated by scripts/library.mjs emit from library/ (pplus-library). Do not edit; run pixi run library-emit.\nimport type { LibraryModule } from './types.js';\n\nexport const LIBRARY_COMMIT = ${JSON.stringify(commit)};\nexport const LIBRARY_MODULES: LibraryModule[] = ${JSON.stringify(modules, null, 2)};\n`;
}
/* ------------------------------------------------------------------ */
/* Command line */
/* ------------------------------------------------------------------ */
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
if (cmd === 'check') {
const only = opt('only');
const { modules, results } = await checkLibrary(LIB, only);
const prior = fs.existsSync(path.join(LIB, 'status.json')) ? readJson(path.join(LIB, 'status.json')).routines || {} : {};
const merged = only ? { ...prior, ...results } : results;
const sorted = Object.fromEntries(Object.keys(merged).sort().map((k) => [k, merged[k]]));
writeIfChanged(path.join(LIB, 'status.json'), `${JSON.stringify({ comment: 'Written by pixi run library-check in pplus-core. Do not edit.', routines: sorted }, null, 2)}\n`);
writeIfChanged(path.join(LIB, 'STATUS.md'), `${statusMarkdown(modules, sorted)}\n`);
let failed = 0;
for (const [id, res] of Object.entries(results)) {
const all = [...Object.entries(res.checks), ...Object.entries(res.references).flatMap(([k, c]) => Object.entries(c).map(([h, v]) => [`${k} ${h}`, v])), ...(res.proof ? [['proof', res.proof]] : [])];
for (const [k, c] of all) if (c.status === 'fail') { failed++; console.log(`FAIL ${id} ${k}: ${c.detail}`); }
console.log(`${id}: ${all.map(([k, c]) => `${k}=${c.status}`).join(' ')}`);
}
console.log(failed ? `${failed} failing check(s)` : 'no failing check');
process.exit(failed ? 1 : 0);
} else if (cmd === 'emit') {
const text = emitText(LIB);
if (flag('check')) {
const cur = fs.existsSync(GENERATED) ? fs.readFileSync(GENERATED, 'utf8') : '';
if (cur !== text) { console.error('src/library/generated.ts is out of date with library/: run pixi run library-emit'); process.exit(1); }
console.log('src/library/generated.ts is up to date');
} else console.log(writeIfChanged(GENERATED, text) ? 'wrote src/library/generated.ts' : 'src/library/generated.ts unchanged');
} else if (cmd === 'harness' || cmd === 'mojo-sources') {
const svc = await import(path.join(CORE, 'dist/services/pseudo.js'));
const { setLibrary } = await import(path.join(CORE, 'dist/library/registry.js'));
const { parseExpression } = await import(path.join(CORE, 'dist/engines/pseudo/parse.js'));
const { evaluateConstant } = await import(path.join(CORE, 'dist/engines/pseudo/interp.js'));
const { snake } = await import(path.join(CORE, 'dist/sync/pseudo-to-mojo.js'));
const modules = load(LIB);
setLibrary(modules);
const programs = [];
for (const m of modules) for (const r of m.routines) {
const st = svc.studyOf(r.source);
if (!st.ok) continue;
const sub = [...st.program.program.subs.values()][0];
const calls = r.cases.map((c) => ({ args: parseExpression(c.call).expr.args.map((a) => evaluateConstant(st.program.program, a)) }));
for (const ref of r.references) programs.push({ id: r.id, ref, dir: path.join(LIB, r.dir), make: () => harness(ref.lang, ref, snake(r.name), sub, calls) });
}
if (cmd === 'harness') {
const [id, lang, level] = args.slice(1);
const p = programs.find((x) => x.id === id && x.ref.lang === lang && x.ref.level === level);
if (!p) { console.error(`no reference ${lang}/${level} for ${id}`); process.exit(2); }
process.stdout.write(p.make());
} else {
// one JSON line per Mojo reference: what scripts/library_godbolt.py sends, and where its answer goes
const list = programs.filter((p) => p.ref.lang === 'mojo').map((p) => ({ id: `${p.id} mojo/${p.ref.level}`, source: p.make(), golden: path.join(p.dir, 'golden', `mojo-${p.ref.level}.json`) }));
process.stdout.write(`${JSON.stringify(list, null, 2)}\n`);
}
} else {
console.error('usage: node scripts/library.mjs check [--only id] | emit [--check] | harness <id> <lang> <level> | mojo-sources');
process.exit(2);
}
}
+58
View File
@@ -0,0 +1,58 @@
#!/usr/bin/env python3
"""Ask the pinned Mojo compiler (on Compiler Explorer) about every Mojo reference implementation of the library,
and store each answer beside the routine, in golden/mojo-<level>.json. Network; standard library only.
node scripts/library.mjs mojo-sources | python3 scripts/library_godbolt.py [--force] [--dry-run]
Each program is the reference's source with a main that prints every case (scripts/library.mjs builds it). An
answer is keyed by the SHA-256 of that exact program and the compiler id, so editing a reference or a case makes
it unverified until it is asked again; an answer already stored for the same program is not asked again unless
--force. pixi run library-check then compares the stored output with the pseudo's results.
"""
from __future__ import annotations
import hashlib
import json
import sys
from datetime import date
from pathlib import Path
CORE = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(CORE / "scripts" / "mojo"))
from mojocorpus import godbolt # noqa: E402
def main(argv: list[str]) -> int:
force = "--force" in argv
dry = "--dry-run" in argv
pin = json.loads((CORE / "mojo" / "pin.json").read_text())
base, compiler = pin["godbolt"]["base"], pin["godbolt"]["compiler"]
timeout = int(pin["godbolt"].get("timeoutSeconds", 90))
items = json.load(sys.stdin)
asked = 0
for it in items:
src = it["source"]
sha = hashlib.sha256(src.encode("utf-8")).hexdigest()
out = Path(it["golden"])
if out.exists() and not force:
old = json.loads(out.read_text())
if old.get("source_sha256") == sha and old.get("compiler") == compiler:
print(f"[library] {it['id']}: stored answer is current")
continue
if dry:
print(f"[library] {it['id']}: would ask {base} ({compiler}), {len(src)} bytes")
continue
data = godbolt.post(base, compiler, godbolt.request_body(src, "run"), timeout=timeout)
result = godbolt.normalise("run", data)
out.parent.mkdir(parents=True, exist_ok=True)
out.write_text(json.dumps({"tool": "godbolt", "compiler": compiler, "mojoVersion": pin["mojo"]["version"],
"source_sha256": sha, "asked": date.today().isoformat(), "result": result},
indent=2, ensure_ascii=False) + "\n")
asked += 1
print(f"[library] {it['id']}: compiled={result['compiled']} exit={result['exitCode']}")
print(f"[library] asked {asked} program(s)")
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
+17 -6
View File
@@ -9,7 +9,7 @@
* written here by hand (statement forms, what stops a run, speco syntax) * written here by hand (statement forms, what stops a run, speco syntax)
* carry examples, and `tests/reference.test.mjs` parses every one. * carry examples, and `tests/reference.test.mjs` parses every one.
*/ */
import { KEYWORDS, BUILTINS, PRECEDENCE, CONTEXTUAL } from '../engines/pseudo/lang.js'; import { KEYWORDS, BUILTINS, PRECEDENCE, CONTEXTUAL, CONVERSIONS, SOFT_BUILTINS } from '../engines/pseudo/lang.js';
import { SENTENCE_FORMS, COMPARISONS, IN_PHRASES } from '../engines/pseudo/english.js'; import { SENTENCE_FORMS, COMPARISONS, IN_PHRASES } from '../engines/pseudo/english.js';
import { OPS, CONDITION_TEXT, REG64, SYSCALLS } from '../engines/asm86/interp.js'; import { OPS, CONDITION_TEXT, REG64, SYSCALLS } from '../engines/asm86/interp.js';
import { PRESETS } from '../engines/asm86/machine.js'; import { PRESETS } from '../engines/asm86/machine.js';
@@ -25,7 +25,9 @@ export interface RefLanguage { id: LangId; title: string; blurb: string; section
const KEYWORD_GROUPS: Array<[string, string[]]> = [ const KEYWORD_GROUPS: Array<[string, string[]]> = [
['Declarations', ['DECLARE', 'CONSTANT', 'DEFINE', 'TYPE', 'ENDTYPE', 'OF']], ['Declarations', ['DECLARE', 'CONSTANT', 'DEFINE', 'TYPE', 'ENDTYPE', 'OF']],
['Libraries', ['USE']],
['Types', ['INTEGER', 'FLOAT', 'CHAR', 'STRING', 'BOOL', 'BOOLEAN', 'ARRAY', 'LIST', 'HASHMAP', 'SET']], ['Types', ['INTEGER', 'FLOAT', 'CHAR', 'STRING', 'BOOL', 'BOOLEAN', 'ARRAY', 'LIST', 'HASHMAP', 'SET']],
['Sized numbers', ['INT8', 'INT16', 'INT32', 'INT64', 'UINT8', 'UINT16', 'UINT32', 'UINT64', 'FLOAT32', 'FLOAT64']],
['Selection', ['IF', 'THEN', 'ELSEIF', 'ELSE', 'ENDIF', 'CASE', 'OTHERWISE', 'ENDCASE']], ['Selection', ['IF', 'THEN', 'ELSEIF', 'ELSE', 'ENDIF', 'CASE', 'OTHERWISE', 'ENDCASE']],
['Iteration', ['FOR', 'TO', 'STEP', 'NEXT', 'PARALLEL', 'WHILE', 'ENDWHILE', 'REPEAT', 'UNTIL', 'BREAK', 'CONTINUE']], ['Iteration', ['FOR', 'TO', 'STEP', 'NEXT', 'PARALLEL', 'WHILE', 'ENDWHILE', 'REPEAT', 'UNTIL', 'BREAK', 'CONTINUE']],
['Subroutines', ['FUNCTION', 'ENDFUNCTION', 'PROCEDURE', 'ENDPROCEDURE', 'RETURNS', 'RETURN', 'CALL', 'BYREF', 'BYVAL']], ['Subroutines', ['FUNCTION', 'ENDFUNCTION', 'PROCEDURE', 'ENDPROCEDURE', 'RETURNS', 'RETURN', 'CALL', 'BYREF', 'BYVAL']],
@@ -38,7 +40,11 @@ const KEYWORD_GROUPS: Array<[string, string[]]> = [
/** What each keyword means, for hover. */ /** What each keyword means, for hover. */
export const KEYWORD_MEANING: Record<string, string> = { export const KEYWORD_MEANING: Record<string, string> = {
DECLARE: 'Creates a name and fixes its type, with or without a starting value: DECLARE Counter : INTEGER, or DECLARE Counter : INTEGER <- 0', CONSTANT: 'Names a value that never changes: CONSTANT BasesPerCodon = 3', DEFINE: 'Makes one fixed set of a named set type: DEFINE Purines (\'A\', \'G\') : BaseSet', TYPE: 'Defines a record (TYPE … ENDTYPE with one DECLARE per field) or names a set type (TYPE BaseSet = SET OF CHAR)', ENDTYPE: 'Closes a TYPE block', OF: 'Joins a collection to its element type: LIST OF INTEGER', DECLARE: 'Creates a name and fixes its type, with or without a starting value: DECLARE Counter : INTEGER, or DECLARE Counter : INTEGER <- 0', CONSTANT: 'Names a value that never changes: CONSTANT BasesPerCodon = 3', DEFINE: 'Makes one fixed set of a named set type: DEFINE Purines (\'A\', \'G\') : BaseSet', TYPE: 'Defines a record (TYPE … ENDTYPE with one DECLARE per field) or names a set type (TYPE BaseSet = SET OF CHAR)', ENDTYPE: 'Closes a TYPE block', OF: 'Joins a collection to its element type: LIST OF INTEGER',
INTEGER: 'A whole number', FLOAT: 'A number with a fractional part; / always produces one', CHAR: 'Exactly one character, in single quotes', STRING: 'Any number of characters, in double quotes; positions count from 0', BOOL: 'TRUE or FALSE', ARRAY: 'A fixed number of values of one type, declared by length: ARRAY[4] OF INTEGER, or ARRAY[R, C] for two dimensions', LIST: 'Any number of values of one type, in order; starts empty and grows with APPEND', HASHMAP: 'Values reached by key: HASHMAP OF STRING : CHAR', SET: 'Distinct values in no order; ask IN', USE: 'Brings routines from the library into the program: USE stats (the whole module) or USE stats.Mean (one routine). USE IEEE lets NaN and infinities flow as IEEE 754 says instead of stopping the run',
INTEGER: 'A whole number of any size (a run holds it exactly to 9,007,199,254,740,991)', FLOAT: 'A number with a fractional part, in IEEE 754 double precision; / always produces one',
INT8: 'A whole number from -128 to 127; a result outside that stops the run', INT16: 'A whole number from -32,768 to 32,767', INT32: 'A whole number from -2,147,483,648 to 2,147,483,647', INT64: 'A 64-bit whole number (held exactly in this lab to ±9,007,199,254,740,991)',
UINT8: 'A whole number from 0 to 255: a byte', UINT16: 'A whole number from 0 to 65,535', UINT32: 'A whole number from 0 to 4,294,967,295', UINT64: 'A 64-bit whole number of 0 or more (held exactly in this lab to 9,007,199,254,740,991)',
FLOAT32: 'A FLOAT in single precision (about 7 significant digits): every result is rounded to the nearest single', FLOAT64: 'Another name for FLOAT: double precision, about 16 significant digits', CHAR: 'Exactly one character, in single quotes', STRING: 'Any number of characters, in double quotes; positions count from 0', BOOL: 'TRUE or FALSE', ARRAY: 'A fixed number of values of one type, declared by length: ARRAY[4] OF INTEGER, or ARRAY[R, C] for two dimensions', LIST: 'Any number of values of one type, in order; starts empty and grows with APPEND', HASHMAP: 'Values reached by key: HASHMAP OF STRING : CHAR', SET: 'Distinct values in no order; ask IN',
IF: 'Runs its body when the condition is TRUE', THEN: 'Ends the condition of an IF', ELSEIF: 'One word: another condition under the same IF, tested only if the ones above failed', ELSE: 'The body that runs when no condition above was TRUE', ENDIF: 'Closes an IF', CASE: 'CASE OF X: picks the first arm whose value, list of values, or half-open range matches X', OTHERWISE: 'The arm of a CASE that runs when nothing else matched; must come last', ENDCASE: 'Closes a CASE', IF: 'Runs its body when the condition is TRUE', THEN: 'Ends the condition of an IF', ELSEIF: 'One word: another condition under the same IF, tested only if the ones above failed', ELSE: 'The body that runs when no condition above was TRUE', ENDIF: 'Closes an IF', CASE: 'CASE OF X: picks the first arm whose value, list of values, or half-open range matches X', OTHERWISE: 'The arm of a CASE that runs when nothing else matched; must come last', ENDCASE: 'Closes a CASE',
FOR: 'A counted loop: FOR I <- 0 TO N runs with I = 0, 1, …, N-1. The bound is not reached. FOR EACH X IN C visits every value', PARALLEL: 'PARALLEL FOR I <- 0 TO N: a counted loop whose passes are independent, as on a GPU where each runs on its own thread. The lab runs them and stops with a data race if one pass writes a location another pass touches', TO: 'The stop value of a FOR, which the counter never reaches (half-open), or the upper end of a CASE range', STEP: 'The increment of a FOR counter; negative counts down', NEXT: 'Closes a FOR, naming its counter: NEXT I', WHILE: 'Tests before each pass; the body may run zero times', ENDWHILE: 'Closes a WHILE', REPEAT: 'Runs the body, then tests; the body runs at least once', UNTIL: 'Closes a REPEAT with the condition that stops it', BREAK: 'Leaves the innermost loop at once', CONTINUE: 'Skips the rest of this pass and starts the next', FOR: 'A counted loop: FOR I <- 0 TO N runs with I = 0, 1, …, N-1. The bound is not reached. FOR EACH X IN C visits every value', PARALLEL: 'PARALLEL FOR I <- 0 TO N: a counted loop whose passes are independent, as on a GPU where each runs on its own thread. The lab runs them and stops with a data race if one pass writes a location another pass touches', TO: 'The stop value of a FOR, which the counter never reaches (half-open), or the upper end of a CASE range', STEP: 'The increment of a FOR counter; negative counts down', NEXT: 'Closes a FOR, naming its counter: NEXT I', WHILE: 'Tests before each pass; the body may run zero times', ENDWHILE: 'Closes a WHILE', REPEAT: 'Runs the body, then tests; the body runs at least once', UNTIL: 'Closes a REPEAT with the condition that stops it', BREAK: 'Leaves the innermost loop at once', CONTINUE: 'Skips the rest of this pass and starts the next',
FUNCTION: 'A subroutine that hands back one value: FUNCTION F(X : INTEGER) RETURNS INTEGER', ENDFUNCTION: 'Closes a FUNCTION', PROCEDURE: 'A subroutine that does something and returns nothing; called with CALL', ENDPROCEDURE: 'Closes a PROCEDURE', RETURNS: 'States the type of a FUNCTION\'s answer', RETURN: 'Hands back the answer and ends the subroutine at once', CALL: 'Runs a PROCEDURE on a line of its own: CALL REPORT("ATT", Found)', BYREF: 'Marks a parameter the procedure may change in the caller; everything else is a copy', BYVAL: 'States the default explicitly: the parameter is a copy', FUNCTION: 'A subroutine that hands back one value: FUNCTION F(X : INTEGER) RETURNS INTEGER', ENDFUNCTION: 'Closes a FUNCTION', PROCEDURE: 'A subroutine that does something and returns nothing; called with CALL', ENDPROCEDURE: 'Closes a PROCEDURE', RETURNS: 'States the type of a FUNCTION\'s answer', RETURN: 'Hands back the answer and ends the subroutine at once', CALL: 'Runs a PROCEDURE on a line of its own: CALL REPORT("ATT", Found)', BYREF: 'Marks a parameter the procedure may change in the caller; everything else is a copy', BYVAL: 'States the default explicitly: the parameter is a copy',
@@ -59,6 +65,8 @@ const OPERATOR_MEANING: Record<string, string> = {
const PSEUDO_STATEMENTS: RefEntry[] = [ const PSEUDO_STATEMENTS: RefEntry[] = [
{ term: 'DECLARE <name> : <type>', meaning: 'Creates a name of that type with no value yet. Several names may share a line.', example: 'DECLARE Counter : INTEGER\nDECLARE N, M : INTEGER' }, { term: 'DECLARE <name> : <type>', meaning: 'Creates a name of that type with no value yet. Several names may share a line.', example: 'DECLARE Counter : INTEGER\nDECLARE N, M : INTEGER' },
{ term: 'DECLARE <name> : <type> <- <expr>', meaning: 'Creates a name and gives it its starting value on the same line, as Mojo\'s var total: Int = 0 does. One name per line in this form; the two-line form (DECLARE, then an assignment) means the same.', example: 'DECLARE T : STRING <- "GATTACA"\nDECLARE N : INTEGER <- LENGTH(T)\nDECLARE Total : INTEGER <- 0\nOUTPUT N, " ", Total' }, { term: 'DECLARE <name> : <type> <- <expr>', meaning: 'Creates a name and gives it its starting value on the same line, as Mojo\'s var total: Int = 0 does. One name per line in this form; the two-line form (DECLARE, then an assignment) means the same.', example: 'DECLARE T : STRING <- "GATTACA"\nDECLARE N : INTEGER <- LENGTH(T)\nDECLARE Total : INTEGER <- 0\nOUTPUT N, " ", Total' },
{ term: 'USE <module> / USE <module>.<Routine>', meaning: 'Brings verified routines from the library into the program, to be called like your own. A run does not step into them (open the routine in the library to do that); the prover uses their REQUIRE and ENSURE lines at each call. Lines go at the top of the program.', example: 'USE IEEE\nDECLARE X : FLOAT\nX <- 1.0 / 0.0\nOUTPUT IS_INF(X)' },
{ term: 'DECLARE <name> : INT32 (or another sized type)', meaning: 'A number of a fixed width. A whole-number result that does not fit stops the run rather than wrapping around; a FLOAT32 result is rounded to single precision. Two different sized types meet only through a conversion such as INT32(X) or FLOAT32(X), and a literal takes the type beside it.', example: 'DECLARE Count : UINT8 <- 250\nCount <- Count + 5\nOUTPUT Count, " ", WRAP(Count + 1, UINT8)\nDECLARE X : FLOAT32 <- 0.1\nOUTPUT X, " holds ", EXACT(X)' },
{ term: 'CONSTANT <name> = <value>', meaning: 'A value fixed for the whole program; literals and other constants only.', example: 'CONSTANT BasesPerCodon = 3' }, { term: 'CONSTANT <name> = <value>', meaning: 'A value fixed for the whole program; literals and other constants only.', example: 'CONSTANT BasesPerCodon = 3' },
{ term: '<name> <- <expr>', meaning: 'Assignment. The right side is worked out first, then stored.', example: 'DECLARE Counter : INTEGER\nCounter <- 0\nCounter <- Counter + 1' }, { term: '<name> <- <expr>', meaning: 'Assignment. The right side is worked out first, then stored.', example: 'DECLARE Counter : INTEGER\nCounter <- 0\nCounter <- Counter + 1' },
{ term: 'OUTPUT <expr>, <expr>, ...', meaning: 'Shows values in order. A collection prints the way you would type it.', example: 'OUTPUT "Found ", 3, " matches"' }, { term: 'OUTPUT <expr>, <expr>, ...', meaning: 'Shows values in order. A collection prints the way you would type it.', example: 'OUTPUT "Found ", 3, " matches"' },
@@ -81,6 +89,8 @@ const PSEUDO_STATEMENTS: RefEntry[] = [
const PSEUDO_LITERALS: RefEntry[] = [ const PSEUDO_LITERALS: RefEntry[] = [
{ term: '42, -3', meaning: 'an INTEGER' }, { term: '42, -3', meaning: 'an INTEGER' },
{ term: '0.5, 2.0, 1.5e-3', meaning: 'a FLOAT; a digit on each side of the point is required' }, { term: '0.5, 2.0, 1.5e-3', meaning: 'a FLOAT; a digit on each side of the point is required' },
{ term: '1_000_000, 0xFF, 0b1010', meaning: 'an INTEGER with underscores between digits, in hexadecimal, or in binary' },
{ term: '0.1 next to a FLOAT32', meaning: 'a literal takes the type of the sized value beside it: 0.1 becomes the nearest FLOAT32, and 1 next to an INT8 is an INT8' },
{ term: "'A'", meaning: 'a CHAR: exactly one character in single quotes' }, { term: "'A'", meaning: 'a CHAR: exactly one character in single quotes' },
{ term: '"GATTACA", ""', meaning: 'a STRING in double quotes; \\n, \\t, \\" and \\\\ are escapes' }, { term: '"GATTACA", ""', meaning: 'a STRING in double quotes; \\n, \\t, \\" and \\\\ are escapes' },
{ term: 'TRUE, FALSE', meaning: 'a BOOL' }, { term: 'TRUE, FALSE', meaning: 'a BOOL' },
@@ -97,8 +107,8 @@ const PSEUDO_STOPS: RefEntry[] = [
{ term: 'No value yet', meaning: 'reading a name, a position, or a field before anything is stored in it, or a FOR counter after its loop' }, { term: 'No value yet', meaning: 'reading a name, a position, or a field before anything is stored in it, or a FOR counter after its loop' },
{ term: 'Missing key', meaning: 'reading H[K] when K is not a key of H; test with IN first' }, { term: 'Missing key', meaning: 'reading H[K] when K is not a key of H; test with IN first' },
{ term: 'Division by zero', meaning: 'dividing by 0 with /, DIV, or MOD' }, { term: 'Division by zero', meaning: 'dividing by 0 with /, DIV, or MOD' },
{ term: 'No answer', meaning: 'a whole number to a negative power, SQRT of a negative, LOG of a non-positive, MIN of an empty list, text TO_INTEGER cannot read' }, { term: 'No answer', meaning: 'a whole number to a negative power, SQRT of a negative, LOG of a non-positive, ASIN or ACOS outside [-1, 1], MIN of an empty list, text TO_INTEGER cannot read (the FLOAT cases give NaN or an infinity instead when the program says USE IEEE)' },
{ term: 'Too large', meaning: 'a whole number beyond ±9,007,199,254,740,991' }, { term: 'Too large', meaning: 'a whole number beyond ±9,007,199,254,740,991, a result outside its sized type (an INT8 past 127), a conversion such as INT32(X) that does not fit, or a FLOAT past about 1.8 × 10^308 (unless the program says USE IEEE)' },
{ term: 'Input', meaning: 'INPUT with nothing left, or input that does not convert' }, { term: 'Input', meaning: 'INPUT with nothing left, or input that does not convert' },
{ term: 'A broken promise', meaning: 'a REQUIRE, ENSURE, INVARIANT, DECREASES, or ASSERT that is FALSE' }, { term: 'A broken promise', meaning: 'a REQUIRE, ENSURE, INVARIANT, DECREASES, or ASSERT that is FALSE' },
{ term: 'No end in sight', meaning: 'more steps than the lab allows (20,000 here); the lab names the loop and says whether its condition has stopped changing' }, { term: 'No end in sight', meaning: 'more steps than the lab allows (20,000 here); the lab names the loop and says whether its condition has stopped changing' },
@@ -172,12 +182,13 @@ export const PSEUDO_IDIOMS: RefEntry[] = [
export function reservedNames(): RefEntry[] { export function reservedNames(): RefEntry[] {
const kw = [...(KEYWORDS as Set<string>)].sort().map((w) => ({ term: w, meaning: 'keyword', note: KEYWORD_MEANING[w] ? KEYWORD_MEANING[w].split(/[:;.]/)[0] : '' })); const kw = [...(KEYWORDS as Set<string>)].sort().map((w) => ({ term: w, meaning: 'keyword', note: KEYWORD_MEANING[w] ? KEYWORD_MEANING[w].split(/[:;.]/)[0] : '' }));
const ctx = Object.keys(CONTEXTUAL as Record<string, string>).sort().map((w) => ({ term: w, meaning: 'contextual keyword', note: KEYWORD_MEANING[w] ? KEYWORD_MEANING[w].split(/[:;.]/)[0] : '' })); const ctx = Object.keys(CONTEXTUAL as Record<string, string>).sort().map((w) => ({ term: w, meaning: 'contextual keyword', note: KEYWORD_MEANING[w] ? KEYWORD_MEANING[w].split(/[:;.]/)[0] : '' }));
const bi = Object.keys(BUILTINS as Record<string, any>).sort().map((w) => ({ term: w, meaning: 'built-in function', note: ((BUILTINS as any)[w].doc || '').split(/[:;.]/)[0] })); const bi = Object.keys(BUILTINS as Record<string, any>).filter((w) => !(SOFT_BUILTINS as Set<string>).has(w)).sort().map((w) => ({ term: w, meaning: 'built-in function', note: ((BUILTINS as any)[w].doc || '').split(/[:;.]/)[0] }));
return [...kw, ...ctx, ...bi]; return [...kw, ...ctx, ...bi];
} }
function builtinEntries(): RefEntry[] { function builtinEntries(): RefEntry[] {
return Object.entries(BUILTINS as Record<string, any>).map(([name, b]) => ({ term: b.sig || name, meaning: b.doc || '', note: b.group || '' })); return Object.entries(BUILTINS as Record<string, any>).map(([name, b]) => ({ term: b.sig || name, meaning: b.doc || '', note: b.group || '' }))
.concat(Object.values(CONVERSIONS as Record<string, any>).map((c) => ({ term: c.sig, meaning: c.doc, note: c.group })));
} }
export const PSEUDO_REF: RefLanguage = { export const PSEUDO_REF: RefLanguage = {
+190 -25
View File
@@ -18,6 +18,7 @@
import { import {
T, BUILTINS, MUTATORS, typeName, sameType, assignable, isNumeric, isAny, isText, T, BUILTINS, MUTATORS, typeName, sameType, assignable, isNumeric, isAny, isText,
SIZED, isSizedInt, isFloat32, intRange, intRangeText, TYPE_ARGS, SOFT_BUILTINS,
} from './lang.js'; } from './lang.js';
import { diag, sourceOf } from './parse.js'; import { diag, sourceOf } from './parse.js';
import { editDistance } from '../shared/text.js'; import { editDistance } from '../shared/text.js';
@@ -50,8 +51,8 @@ for (const k of Object.keys(FOREIGN_FUNCTIONS)) if (!FOREIGN_FUNCTIONS[k]) delet
/** Type names from Python and Mojo, and this notation's word for each. */ /** Type names from Python and Mojo, and this notation's word for each. */
const FOREIGN_TYPES = { const FOREIGN_TYPES = {
INT: 'INTEGER', INT64: 'INTEGER', LONG: 'INTEGER', NUMBER: 'INTEGER or FLOAT', INT: 'INTEGER (or INT64 for a 64-bit whole number)', LONG: 'INT64', NUMBER: 'INTEGER or FLOAT',
STR: 'STRING', TEXT: 'STRING', FLOAT64: 'FLOAT', DOUBLE: 'FLOAT', REAL: 'FLOAT', STR: 'STRING', TEXT: 'STRING', DOUBLE: 'FLOAT64', REAL: 'FLOAT', SINGLE: 'FLOAT32',
BOOLEAN: 'BOOL', DICT: 'HASHMAP OF <key type> TO <value type>', MAP: 'HASHMAP OF <key type> TO <value type>', BOOLEAN: 'BOOL', DICT: 'HASHMAP OF <key type> TO <value type>', MAP: 'HASHMAP OF <key type> TO <value type>',
ARRAYLIST: 'LIST OF <type>', ARRAYLIST: 'LIST OF <type>',
}; };
@@ -85,11 +86,23 @@ function elementOf(t) {
} }
} }
/** Two numbers combine to FLOAT unless both are INTEGER. */ /**
* The type two numbers combine to. A FLOAT with anything is a FLOAT (FLOAT32 stays FLOAT32 next to a whole
* number; FLOAT32 next to FLOAT64 is a clash the caller reports). Two whole numbers are INTEGER, or the sized
* type when one is sized (two different widths are a clash the caller reports).
*/
function numericResult(a, b) { function numericResult(a, b) {
if (isAny(a) || isAny(b)) return isAny(a) ? (isAny(b) ? T.ANY : b) : a; if (isAny(a) || isAny(b)) return isAny(a) ? (isAny(b) ? T.ANY : b) : a;
return a.k === 'FLOAT' || b.k === 'FLOAT' ? T.FLOAT : T.INTEGER; if (a.k === 'FLOAT' || b.k === 'FLOAT') {
if (a.k === 'FLOAT' && b.k === 'FLOAT') return isFloat32(a) && isFloat32(b) ? a : (a.bits === 64 ? a : b.bits === 64 ? b : T.FLOAT);
return a.k === 'FLOAT' ? a : b;
} }
if (a.bits) return a;
return b.bits ? b : T.INTEGER;
}
/** A numeric literal, which takes the type of the sized value beside it: X + 1 for an INT8 X is INT8 arithmetic. */
const isLiteral = (n) => !!n && n.kind === 'Num';
/** Does a list of statements end every path in RETURN? */ /** Does a list of statements end every path in RETURN? */
export function alwaysReturns(body) { export function alwaysReturns(body) {
@@ -124,8 +137,10 @@ class Scope {
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
class Checker { class Checker {
constructor(parsed) { constructor(parsed, options = {}) {
this.parsed = parsed; this.parsed = parsed;
this.libs = options.libs || new Map(); // routines from the library: UPPER name -> checked routine
this.libOrigin = options.libOrigin || new Map(); // UPPER name -> { module, line } of the USE that brought it
this.diags = []; this.diags = [];
this.types = new Map(); // record types this.types = new Map(); // record types
this.setTypes = new Map(); // TYPE X = SET OF ... this.setTypes = new Map(); // TYPE X = SET OF ...
@@ -138,6 +153,9 @@ class Checker {
this.ctx = null; this.ctx = null;
} }
/** A routine defined in this program, or brought in from the library with USE. */
findSub(name) { const k = up(name); return this.subs.get(k) || this.libs.get(k); }
error(code, node, message, extra = {}) { this.diags.push(diag('error', code, node?.line ?? 1, message, { col: node?.col ?? 1, ...extra })); } error(code, node, message, extra = {}) { this.diags.push(diag('error', code, node?.line ?? 1, message, { col: node?.col ?? 1, ...extra })); }
warn(code, node, message, extra = {}) { this.diags.push(diag('warning', code, node?.line ?? 1, message, { col: node?.col ?? 1, ...extra })); } warn(code, node, message, extra = {}) { this.diags.push(diag('warning', code, node?.line ?? 1, message, { col: node?.col ?? 1, ...extra })); }
note(code, node, message, extra = {}) { this.diags.push(diag('note', code, node?.line ?? 1, message, { col: node?.col ?? 1, ...extra })); } note(code, node, message, extra = {}) { this.diags.push(diag('note', code, node?.line ?? 1, message, { col: node?.col ?? 1, ...extra })); }
@@ -158,7 +176,11 @@ class Checker {
} else if (s.kind === 'Function' || s.kind === 'Procedure') { } else if (s.kind === 'Function' || s.kind === 'Procedure') {
if (s.name.startsWith('<')) this.subs.set(`${up(s.name)}#${s.line}`, { name: s.name, kind: s.kind, node: s, params: [], returns: null, requires: [], ensures: [], decreases: null }); if (s.name.startsWith('<')) this.subs.set(`${up(s.name)}#${s.line}`, { name: s.name, kind: s.kind, node: s, params: [], returns: null, requires: [], ensures: [], decreases: null });
else if (this.subs.has(up(s.name))) this.error('duplicate', s, `${s.name} is already defined on line ${this.subs.get(up(s.name)).node.line}.`); else if (this.subs.has(up(s.name))) this.error('duplicate', s, `${s.name} is already defined on line ${this.subs.get(up(s.name)).node.line}.`);
else if (BUILTINS[up(s.name)]) this.error('builtin-name', s, `${up(s.name)} is a built-in, so a ${s.kind.toUpperCase()} cannot take its name.`); else if ((BUILTINS[up(s.name)] && !SOFT_BUILTINS.has(up(s.name))) || SIZED[up(s.name)]) this.error('builtin-name', s, `${up(s.name)} is a built-in, so a ${s.kind.toUpperCase()} cannot take its name.`);
else if (this.libs.has(up(s.name))) {
this.error('library-clash', s, `${s.name} also comes from the library (USE ${this.libOrigin.get(up(s.name))?.module} on line ${this.libOrigin.get(up(s.name))?.line}). Rename this one, or bring in only the routines you need: USE ${this.libOrigin.get(up(s.name))?.module}.<Name>.`, { guide: 'libraries' });
this.subs.set(up(s.name), { name: s.name, kind: s.kind, node: s, params: [], returns: null, requires: [], ensures: [], decreases: null });
}
else this.subs.set(up(s.name), { name: s.name, kind: s.kind, node: s, params: [], returns: null, requires: [], ensures: [], decreases: null }); else this.subs.set(up(s.name), { name: s.name, kind: s.kind, node: s, params: [], returns: null, requires: [], ensures: [], decreases: null });
} }
} }
@@ -250,7 +272,7 @@ class Checker {
this.collect(ast.body); this.collect(ast.body);
const mainBody = []; const mainBody = [];
for (const s of ast.body) { for (const s of ast.body) {
if (['TypeDef', 'SetTypeDef', 'Constant', 'Define'].includes(s.kind)) continue; if (['TypeDef', 'SetTypeDef', 'Constant', 'Define', 'Use'].includes(s.kind)) continue;
if (s.kind === 'Function' || s.kind === 'Procedure') { this.subroutine(this.subs.get(up(s.name)) || this.subs.get(`${up(s.name)}#${s.line}`) || { node: s, params: [], returns: T.ANY, requires: [], ensures: [] }); continue; } if (s.kind === 'Function' || s.kind === 'Procedure') { this.subroutine(this.subs.get(up(s.name)) || this.subs.get(`${up(s.name)}#${s.line}`) || { node: s, params: [], returns: T.ANY, requires: [], ensures: [] }); continue; }
mainBody.push(s); mainBody.push(s);
} }
@@ -265,6 +287,9 @@ class Checker {
constants: this.constants, constants: this.constants,
defines: this.defines, defines: this.defines,
subs: this.subs, subs: this.subs,
libs: this.libs,
uses: ast.body.filter((s) => s.kind === 'Use'),
ieee: ast.body.some((s) => s.kind === 'Use' && s.ieee),
main: { body: mainBody, scope: this.mainScope, symbols: [...this.mainScope.map.values()] }, main: { body: mainBody, scope: this.mainScope, symbols: [...this.mainScope.map.values()] },
blanks: this.blanks, blanks: this.blanks,
}; };
@@ -337,8 +362,8 @@ class Checker {
} }
const g = this.global.get(name); const g = this.global.get(name);
if (g) this.error('duplicate', node, `${name} is already the name of a ${g.kind === 'const' ? 'CONSTANT' : 'set'} defined on line ${g.line}.`); if (g) this.error('duplicate', node, `${name} is already the name of a ${g.kind === 'const' ? 'CONSTANT' : 'set'} defined on line ${g.line}.`);
if (this.subs.has(k)) this.error('duplicate', node, `${name} is already the name of a ${this.subs.get(k).kind.toUpperCase()}.`); if (this.findSub(k)) this.error('duplicate', node, `${name} is already the name of a ${this.findSub(k).kind.toUpperCase()}${this.libs.has(k) ? ' from the library' : ''}.`);
if (BUILTINS[k]) this.error('builtin-name', node, `${k} is a built-in, so a variable cannot take its name.`); if ((BUILTINS[k] && !SOFT_BUILTINS.has(k)) || SIZED[k]) this.error('builtin-name', node, `${k} is a built-in, so a variable cannot take its name.`);
if (this.types.has(k) || this.setTypes.has(k)) this.error('duplicate', node, `${name} is already the name of a type.`); if (this.types.has(k) || this.setTypes.has(k)) this.error('duplicate', node, `${name} is already the name of a type.`);
return scope.set(name, sym); return scope.set(name, sym);
} }
@@ -359,6 +384,7 @@ class Checker {
this.error('global-only', s, `${s.kind === 'TypeDef' || s.kind === 'SetTypeDef' ? 'TYPE' : s.kind === 'Constant' ? 'CONSTANT' : 'DEFINE'} lines go at the top level, outside every block.`); this.error('global-only', s, `${s.kind === 'TypeDef' || s.kind === 'SetTypeDef' ? 'TYPE' : s.kind === 'Constant' ? 'CONSTANT' : 'DEFINE'} lines go at the top level, outside every block.`);
return; return;
case 'Function': case 'Procedure': return; case 'Function': case 'Procedure': return;
case 'Use': this.error('use-place', s, 'USE lines go at the top of the program, outside every block.', { guide: 'libraries' }); return;
case 'Assign': this.assign(s, scope); return; case 'Assign': this.assign(s, scope); return;
case 'Input': { case 'Input': {
if (!s.target) return; if (!s.target) return;
@@ -553,11 +579,13 @@ class Checker {
return; return;
} }
if (!s.value) { this.error('return-value', s, `FUNCTION ${sub.name} has to hand back ${article(sub.returns)}, so its RETURN needs a value.`, { guide: 'function' }); return; } if (!s.value) { this.error('return-value', s, `FUNCTION ${sub.name} has to hand back ${article(sub.returns)}, so its RETURN needs a value.`, { guide: 'function' }); return; }
const t = this.expr(s.value, scope); const t = this.adopt(s.value, sub.returns, this.expr(s.value, scope));
if (!assignable(sub.returns, t)) this.error('type', s.value, `FUNCTION ${sub.name} promises ${article(sub.returns)} with RETURNS, and ${sourceOf(s.value)} is ${article(t)}.${this.narrowHint(sub.returns, t, s.value)}`, { guide: 'function' }); if (!assignable(sub.returns, t)) this.error('type', s.value, `FUNCTION ${sub.name} promises ${article(sub.returns)} with RETURNS, and ${sourceOf(s.value)} is ${article(t)}.${this.narrowHint(sub.returns, t, s.value)}`, { guide: 'function' });
} }
narrowHint(to, from, value) { narrowHint(to, from, value) {
if (to.k === 'INTEGER' && from.k === 'INTEGER' && to.bits && from.bits) return ` Two different sized types do not mix silently: convert with ${to.name}(${sourceOf(value)}), which stops the run if the value does not fit.`;
if (isFloat32(to) && from.k === 'FLOAT') return ` A FLOAT64 rounds when it becomes a FLOAT32, so the conversion is written out: FLOAT32(${sourceOf(value)}).`;
if (to.k === 'INTEGER' && from.k === 'FLOAT') { if (to.k === 'INTEGER' && from.k === 'FLOAT') {
if (value && value.kind === 'Binary' && value.op === '/') return ' / always gives a FLOAT, even when both sides are whole. Use DIV for a whole-number answer.'; if (value && value.kind === 'Binary' && value.op === '/') return ' / always gives a FLOAT, even when both sides are whole. Use DIV for a whole-number answer.';
return ' Use INT or ROUND to turn a FLOAT into a whole number, and decide which one you mean.'; return ' Use INT or ROUND to turn a FLOAT into a whole number, and decide which one you mean.';
@@ -568,7 +596,7 @@ class Checker {
assign(s, scope) { assign(s, scope) {
const tt = this.target(s.target, scope, 'assign'); const tt = this.target(s.target, scope, 'assign');
const vt = this.expr(s.value, scope, { expect: tt }); const vt = this.adopt(s.value, tt, this.expr(s.value, scope, { expect: tt }));
if (!tt || isAny(tt) || isAny(vt)) return; if (!tt || isAny(tt) || isAny(vt)) return;
if (vt.k === 'VOID') { this.error('type', s.value, `${sourceOf(s.value)} hands nothing back, so there is nothing to store.`); return; } if (vt.k === 'VOID') { this.error('type', s.value, `${sourceOf(s.value)} hands nothing back, so there is nothing to store.`); return; }
if (tt.k === 'STRING' && vt.k === 'CHAR') return; if (tt.k === 'STRING' && vt.k === 'CHAR') return;
@@ -642,7 +670,7 @@ class Checker {
e.sym = mainSym; e.sym = mainSym;
return mainSym; return mainSym;
} }
if (this.subs.has(up(name))) { this.error('sub-as-value', e, `${name} is a ${this.subs.get(up(name)).kind.toUpperCase()}, and it needs brackets and its arguments to be called.`); return null; } if (this.findSub(name)) { this.error('sub-as-value', e, `${name} is a ${this.findSub(name).kind.toUpperCase()}, and it needs brackets and its arguments to be called.`); return null; }
if (up(name) === 'RESULT') { this.error('result-place', e, 'RESULT means the value a FUNCTION returns, so it can appear only in that function\'s ENSURE lines.', { guide: 'contracts' }); return null; } if (up(name) === 'RESULT') { this.error('result-place', e, 'RESULT means the value a FUNCTION returns, so it can appear only in that function\'s ENSURE lines.', { guide: 'contracts' }); return null; }
const pool = [...scope.all().values()].map((x) => x.name); const pool = [...scope.all().values()].map((x) => x.name);
const s = suggest(name, pool); const s = suggest(name, pool);
@@ -662,7 +690,7 @@ class Checker {
} }
callStmt(s, scope) { callStmt(s, scope) {
const sub = this.subs.get(up(s.name)); const sub = this.findSub(s.name);
if (!sub) { if (!sub) {
if (BUILTINS[up(s.name)]?.kind === 'procedure') { if (BUILTINS[up(s.name)]?.kind === 'procedure') {
this.note('builtin-call', s, `${up(s.name)} is a built-in procedure and needs no CALL: write ${up(s.name)}(...) on its own.`); this.note('builtin-call', s, `${up(s.name)} is a built-in procedure and needs no CALL: write ${up(s.name)}(...) on its own.`);
@@ -670,7 +698,7 @@ class Checker {
s.builtin = true; s.builtin = true;
return; return;
} }
const hint = suggest(s.name, [...this.subs.values()].map((x) => x.name)); const hint = suggest(s.name, [...this.subs.values(), ...this.libs.values()].map((x) => x.name));
this.error('unknown-sub', s, `No PROCEDURE is called ${s.name}.${hint.length ? ` Did you mean ${hint.join(' or ')}?` : ''}`, { guide: 'procedure' }); this.error('unknown-sub', s, `No PROCEDURE is called ${s.name}.${hint.length ? ` Did you mean ${hint.join(' or ')}?` : ''}`, { guide: 'procedure' });
for (const a of s.args) this.expr(a, scope); for (const a of s.args) this.expr(a, scope);
return; return;
@@ -698,8 +726,9 @@ class Checker {
} }
args.forEach((a, i) => { args.forEach((a, i) => {
const p = sub.params[i]; const p = sub.params[i];
const t = this.expr(a, scope); let t = this.expr(a, scope);
if (!p) return; if (!p) return;
if (p.mode !== 'BYREF') t = this.adopt(a, p.type);
if (p.mode === 'BYREF') { if (p.mode === 'BYREF') {
if (a.kind !== 'Name') this.error('byref-arg', a, `Parameter ${p.name} of ${sub.name} is BYREF, so the call has to pass a variable it can change, not ${sourceOf(a)}.`, { guide: 'passing-by-value-or-by-reference' }); if (a.kind !== 'Name') this.error('byref-arg', a, `Parameter ${p.name} of ${sub.name} is BYREF, so the call has to pass a variable it can change, not ${sourceOf(a)}.`, { guide: 'passing-by-value-or-by-reference' });
else { else {
@@ -719,7 +748,7 @@ class Checker {
exprStmt(s, scope) { exprStmt(s, scope) {
const e = s.expr; const e = s.expr;
if (e.kind === 'CallExpr') { if (e.kind === 'CallExpr') {
const sub = this.subs.get(up(e.name)); const sub = e.convert ? null : this.findSub(e.name);
if (sub && sub.kind === 'Procedure') { if (sub && sub.kind === 'Procedure') {
this.error('missing-call', s, `Call a PROCEDURE with CALL, on a line of its own: CALL ${sourceOf(e)}.`, { guide: 'procedure', fix: { line: s.line, col: s.col, endCol: s.col, text: 'CALL ' } }); this.error('missing-call', s, `Call a PROCEDURE with CALL, on a line of its own: CALL ${sourceOf(e)}.`, { guide: 'procedure', fix: { line: s.line, col: s.col, endCol: s.col, text: 'CALL ' } });
s.sub = sub; s.sub = sub;
@@ -810,6 +839,9 @@ class Checker {
case 'Bool': return T.BOOL; case 'Bool': return T.BOOL;
case 'Blank': if (e.text !== '...') this.blank(e, e.text); return T.ANY; case 'Blank': if (e.text !== '...') this.blank(e, e.text); return T.ANY;
case 'Error': return T.ANY; case 'Error': return T.ANY;
case 'TypeRef':
if (!opts.typeArg) { this.error('type-as-value', e, `${e.name} is a type, and a value was expected here. To convert a number to it, write ${e.name}(X)${['INTEGER', 'FLOAT'].includes(e.name) ? ` with a sized type, or ${e.name === 'INTEGER' ? 'INT(X)' : 'TO_FLOAT(X)'}` : ''}.`, { guide: 'the-types' }); return T.ANY; }
return T[e.name] || T.ANY;
case 'Name': { case 'Name': {
if (this.ctx?.constOnly) { if (this.ctx?.constOnly) {
const sym = scope.get(e.name); const sym = scope.get(e.name);
@@ -849,8 +881,9 @@ class Checker {
case 'CallExpr': return this.call(e, scope); case 'CallExpr': return this.call(e, scope);
case 'ListLit': { case 'ListLit': {
let el = T.ANY; let el = T.ANY;
const want = opts.expect && ['LIST', 'ARRAY'].includes(opts.expect.k) ? opts.expect.of : null;
for (const it of e.items) { for (const it of e.items) {
const t = this.expr(it, scope); const t = this.adopt(it, want, this.expr(it, scope));
if (isAny(el)) el = t; if (isAny(el)) el = t;
else if (isNumeric(el) && isNumeric(t)) el = numericResult(el, t); else if (isNumeric(el) && isNumeric(t)) el = numericResult(el, t);
else if (!isAny(t) && !sameType(el, t)) this.error('type', it, `Every value in a list has one type, and ${sourceOf(it)} is ${article(t)} where the others are ${typeName(el)}.`); else if (!isAny(t) && !sameType(el, t)) this.error('type', it, `Every value in a list has one type, and ${sourceOf(it)} is ${article(t)} where the others are ${typeName(el)}.`);
@@ -920,29 +953,37 @@ class Checker {
return T.BOOL; return T.BOOL;
case '+': case '-': case '*': case '+': case '-': case '*':
if (any) return isNumeric(lt) ? lt : isNumeric(rt) ? rt : T.ANY; if (any) return isNumeric(lt) ? lt : isNumeric(rt) ? rt : T.ANY;
if (isNumeric(lt) && isNumeric(rt)) return this.mix(e, lt, rt);
if (op === '+' && isText(lt) && isText(rt)) { this.error('type', e, `Text is joined with &, not +: ${sourceOf(e.left)} & ${sourceOf(e.right)}.`, { guide: 'joining-and-cutting-text' }); return T.STRING; } if (op === '+' && isText(lt) && isText(rt)) { this.error('type', e, `Text is joined with &, not +: ${sourceOf(e.left)} & ${sourceOf(e.right)}.`, { guide: 'joining-and-cutting-text' }); return T.STRING; }
if (!isNumeric(lt) || !isNumeric(rt)) { this.error('type', e, `${op} works on numbers, and ${!isNumeric(lt) ? sourceOf(e.left) : sourceOf(e.right)} is ${article(!isNumeric(lt) ? lt : rt)}.${isText(lt) && isNumeric(rt) ? ' TO_STRING turns a number into text, and & joins text.' : ''}`); return T.ANY; } if (!isNumeric(lt) || !isNumeric(rt)) { this.error('type', e, `${op} works on numbers, and ${!isNumeric(lt) ? sourceOf(e.left) : sourceOf(e.right)} is ${article(!isNumeric(lt) ? lt : rt)}.${isText(lt) && isNumeric(rt) ? ' TO_STRING turns a number into text, and & joins text.' : ''}`); return T.ANY; }
return numericResult(lt, rt); return numericResult(lt, rt);
case '/': case '/':
if (!any && (!isNumeric(lt) || !isNumeric(rt))) { this.error('type', e, `/ divides numbers, and ${!isNumeric(lt) ? sourceOf(e.left) : sourceOf(e.right)} is ${article(!isNumeric(lt) ? lt : rt)}.`); } if (!any && (!isNumeric(lt) || !isNumeric(rt))) { this.error('type', e, `/ divides numbers, and ${!isNumeric(lt) ? sourceOf(e.left) : sourceOf(e.right)} is ${article(!isNumeric(lt) ? lt : rt)}.`); }
return T.FLOAT; if (!any && isNumeric(lt) && isNumeric(rt) && (isFloat32(lt) || isFloat32(rt))) { const m = this.mix(e, lt, rt); return isFloat32(m) ? m : T.FLOAT; }
return lt.bits === 64 || rt.bits === 64 ? T.FLOAT64 : T.FLOAT;
case 'DIV': case 'MOD': case 'DIV': case 'MOD':
for (const [t, n] of [[lt, e.left], [rt, e.right]]) { for (const [t, n] of [[lt, e.left], [rt, e.right]]) {
if (!isAny(t) && t.k !== 'INTEGER') this.error('type', n, `${op} works on whole numbers, and ${sourceOf(n)} is ${article(t)}.${t.k === 'FLOAT' ? ' Use / to divide FLOAT values.' : ''}`, { guide: 'arithmetic' }); if (!isAny(t) && t.k !== 'INTEGER') this.error('type', n, `${op} works on whole numbers, and ${sourceOf(n)} is ${article(t)}.${t.k === 'FLOAT' ? ' Use / to divide FLOAT values.' : ''}`, { guide: 'arithmetic' });
} }
if (!any && lt.k === 'INTEGER' && rt.k === 'INTEGER') return this.mix(e, lt, rt);
return T.INTEGER; return T.INTEGER;
case '^': case '^':
if (!any && (!isNumeric(lt) || !isNumeric(rt))) this.error('type', e, '^ raises a number to a power, and both sides have to be numbers.'); if (!any && (!isNumeric(lt) || !isNumeric(rt))) { this.error('type', e, '^ raises a number to a power, and both sides have to be numbers.'); return T.ANY; }
return any ? T.ANY : numericResult(lt, rt); if (any) return T.ANY;
// the power keeps the base's width: INT32 ^ 2 is INT32, whatever the exponent's type
if (lt.bits && rt.k === 'INTEGER') { this.adopt(e.right, lt.k === 'INTEGER' ? lt : null); return lt; }
return this.mix(e, lt, rt);
case '&': case '&':
for (const [t, n] of [[lt, e.left], [rt, e.right]]) { for (const [t, n] of [[lt, e.left], [rt, e.right]]) {
if (!isAny(t) && !isText(t)) this.error('type', n, `& joins text, and ${sourceOf(n)} is ${article(t)}. TO_STRING(${sourceOf(n)}) writes it as text first.`); if (!isAny(t) && !isText(t)) this.error('type', n, `& joins text, and ${sourceOf(n)} is ${article(t)}. TO_STRING(${sourceOf(n)}) writes it as text first.`);
} }
return T.STRING; return T.STRING;
case '==': case '!=': case '==': case '!=':
this.adoptPair(e, lt, rt);
this.comparable(e, lt, rt, true); this.comparable(e, lt, rt, true);
return T.BOOL; return T.BOOL;
case '<': case '<=': case '>': case '>=': case '<': case '<=': case '>': case '>=':
this.adoptPair(e, lt, rt);
this.comparable(e, lt, rt, false); this.comparable(e, lt, rt, false);
return T.BOOL; return T.BOOL;
case 'IN': { case 'IN': {
@@ -964,6 +1005,58 @@ class Checker {
} }
} }
/**
* A numeric literal next to a sized value takes that value's type: X + 1 with an INT8 X is INT8 arithmetic,
* and 0.1 next to a FLOAT32 is the FLOAT32 nearest 0.1, as in Mojo. A whole-number literal that does not fit
* the type is an error here, before any run. Returns the node's type afterwards.
*/
adopt(node, want, current = node?.type) {
if (!isLiteral(node) || !want || isAny(want) || !isNumeric(want)) return current;
if (isSizedInt(want) && !node.float) {
const [lo, hi] = intRange(want);
if (node.value < lo || node.value > hi) this.error('literal-range', node, `${sourceOf(node)} does not fit ${want.name}, which holds ${intRangeText(want)}.`, { guide: 'sized-types' });
node.type = want;
return want;
}
if (isFloat32(want)) { node.type = want; return want; }
return current;
}
/** In a binary operation, a literal on either side adopts the other side's sized type. */
adoptPair(e, lt, rt) {
if (isLiteral(e.left) && !isLiteral(e.right)) return [this.adopt(e.left, rt, lt), rt];
if (isLiteral(e.right) && !isLiteral(e.left)) return [lt, this.adopt(e.right, lt, rt)];
return [lt, rt];
}
/** The type of +, -, *, DIV, MOD on two numbers, after literals adopt; two widths that do not mix are an error. */
mix(e, lt0, rt0) {
const [lt, rt] = this.adoptPair(e, lt0, rt0);
if (lt.k === 'INTEGER' && rt.k === 'INTEGER' && lt.bits && rt.bits && !sameType(lt, rt)) {
const [narrow, wide] = lt.bits <= rt.bits ? [e.left, rt] : [e.right, lt];
this.error('mixed-width', e, `${sourceOf(e.left)} is ${lt.name} and ${sourceOf(e.right)} is ${rt.name}, and two different sized types do not mix silently. Convert one so both sides agree, as in ${wide.name}(${sourceOf(narrow)}).`, { guide: 'sized-types' });
return T.ANY;
}
if (lt.k === 'FLOAT' && rt.k === 'FLOAT' && isFloat32(lt) !== isFloat32(rt)) {
const [s32, s64] = isFloat32(lt) ? [e.left, e.right] : [e.right, e.left];
this.error('mixed-precision', e, `${sourceOf(s32)} is FLOAT32 and ${sourceOf(s64)} is a FLOAT64, and the two precisions do not mix silently. Write FLOAT32(${sourceOf(s64)}) to round it to single precision, or FLOAT64(${sourceOf(s32)}) to widen the other side.`, { guide: 'sized-types' });
return T.ANY;
}
return numericResult(lt, rt);
}
/** INT32(X), FLOAT32(X), ...: one number, converted to the named type. */
conversion(e, scope) {
const to = SIZED[up(e.name)];
const ts = e.args.map((a) => this.expr(a, scope));
if (ts.length !== 1) { this.error('arg-count', e, `${up(e.name)}(X) converts one number, and this call gives it ${ts.length}.`); return to; }
const t = ts[0];
if (!isAny(t) && !isNumeric(t)) this.error('type', e.args[0], `${up(e.name)}(X) converts a number, and ${sourceOf(e.args[0])} is ${article(t)}.${isText(t) ? ' TO_INTEGER or TO_FLOAT reads a number out of text first.' : ''}`, { guide: 'sized-types' });
else if (!isAny(t) && sameType(t, to) && t.name === to.name) this.note('already-type', e, `${sourceOf(e.args[0])} is already ${to.name}, so ${to.name}(...) changes nothing.`);
if (isLiteral(e.args[0]) && to.k === 'INTEGER' && !e.args[0].float) this.adopt(e.args[0], to);
return to;
}
comparable(e, lt, rt, equality) { comparable(e, lt, rt, equality) {
if (isAny(lt) || isAny(rt)) return; if (isAny(lt) || isAny(rt)) return;
if (isNumeric(lt) && isNumeric(rt)) return; if (isNumeric(lt) && isNumeric(rt)) return;
@@ -1032,7 +1125,8 @@ class Checker {
} }
return t; return t;
} }
const sub = this.subs.get(name); if (e.convert) return this.conversion(e, scope);
const sub = this.findSub(name);
if (sub) { if (sub) {
e.sub = sub; e.sub = sub;
if (sub.kind === 'Procedure') { if (sub.kind === 'Procedure') {
@@ -1047,7 +1141,7 @@ class Checker {
if (!b) { if (!b) {
const sym = scope.get(e.name); const sym = scope.get(e.name);
if (sym) { this.error('not-callable', e, `${e.name} is ${sym.kind === 'var' ? 'a variable' : 'not a subroutine'}, so it cannot be called. Square brackets reach a position: ${e.name}[I].`); return T.ANY; } if (sym) { this.error('not-callable', e, `${e.name} is ${sym.kind === 'var' ? 'a variable' : 'not a subroutine'}, so it cannot be called. Square brackets reach a position: ${e.name}[I].`); return T.ANY; }
const s = suggest(e.name, [...this.subs.values()].map((x) => x.name).concat(Object.keys(BUILTINS))); const s = suggest(e.name, [...this.subs.values(), ...this.libs.values()].map((x) => x.name).concat(Object.keys(BUILTINS)));
const other = FOREIGN_FUNCTIONS[name]; const other = FOREIGN_FUNCTIONS[name];
if (other) this.error('unknown-function', e, `No FUNCTION is called ${e.name}. ${other}`, { guide: 'function' }); if (other) this.error('unknown-function', e, `No FUNCTION is called ${e.name}. ${other}`, { guide: 'function' });
else this.error('unknown-function', e, `No FUNCTION is called ${e.name}.${s.length ? ` Did you mean ${s.join(' or ')}?` : ' Define it with FUNCTION ... ENDFUNCTION.'}`, { guide: 'function' }); else this.error('unknown-function', e, `No FUNCTION is called ${e.name}.${s.length ? ` Did you mean ${s.join(' or ')}?` : ' Define it with FUNCTION ... ENDFUNCTION.'}`, { guide: 'function' });
@@ -1060,7 +1154,7 @@ class Checker {
return T.VOID; return T.VOID;
} }
e.builtin = name; e.builtin = name;
const ts = e.args.map((a) => this.expr(a, scope)); const ts = e.args.map((a, i) => this.expr(a, scope, { typeArg: TYPE_ARGS[name] === i }));
const argc = (n) => { const argc = (n) => {
if (ts.length !== n) { this.error('arg-count', e, `${b.sig} takes ${n} ${n === 1 ? 'value' : 'values'}, and this call gives it ${ts.length}.`); return false; } if (ts.length !== n) { this.error('arg-count', e, `${b.sig} takes ${n} ${n === 1 ? 'value' : 'values'}, and this call gives it ${ts.length}.`); return false; }
return true; return true;
@@ -1181,13 +1275,84 @@ class Checker {
case 'SORTED': case 'SORTED':
if (argc(1) && !isAny(ts[0]) && !['LIST', 'SET', 'ARRAY'].includes(ts[0].k)) { this.error('type', e.args[0], `SORTED puts the values of a LIST or SET in order, and ${sourceOf(e.args[0])} is ${article(ts[0])}.`); return T.list(T.ANY); } if (argc(1) && !isAny(ts[0]) && !['LIST', 'SET', 'ARRAY'].includes(ts[0].k)) { this.error('type', e.args[0], `SORTED puts the values of a LIST or SET in order, and ${sourceOf(e.args[0])} is ${article(ts[0])}.`); return T.list(T.ANY); }
return T.list(isAny(ts[0]) ? T.ANY : ts[0].of); return T.list(isAny(ts[0]) ? T.ANY : ts[0].of);
// ---- numerical work
case 'ASIN': case 'ACOS': case 'ATAN': case 'SINH': case 'COSH': case 'TANH': case 'CBRT': case 'EXPM1': case 'LOG1P':
if (argc(1)) num(0);
return T.FLOAT;
case 'HYPOT': case 'POW': case 'COPYSIGN': if (argc(2)) { num(0); num(1); } return T.FLOAT;
case 'E': case 'INF': case 'NAN': argc(0); return T.FLOAT;
case 'TRUNC': if (argc(1)) { num(0); return ts[0].k === 'FLOAT' ? ts[0] : T.FLOAT; } return T.FLOAT;
case 'SIGN': if (argc(1)) return isAny(num(0)) ? T.ANY : ts[0]; return T.ANY;
case 'CLAMP': {
if (!argc(3)) return T.ANY;
ts.forEach((_, i) => num(i));
if (ts.some((x) => isAny(x))) return T.ANY;
for (const i of [1, 2]) this.adopt(e.args[i], ts[0]);
return ts[0];
}
case 'FMA': {
if (!argc(3)) return T.FLOAT;
ts.forEach((_, i) => num(i));
const f32 = ts.some((x) => isFloat32(x));
if (f32) e.args.forEach((a) => this.adopt(a, T.FLOAT32));
return f32 && e.args.every((a) => isFloat32(a.type) || a.type?.k === 'INTEGER') ? T.FLOAT32 : T.FLOAT;
}
case 'IS_NAN': case 'IS_INF': case 'IS_FINITE': if (argc(1)) num(0); return T.BOOL;
case 'IS_CLOSE': if (argc(4)) ts.forEach((_, i) => num(i)); return T.BOOL;
case 'ULP': case 'NEXT_AFTER': {
if (!argc(name === 'ULP' ? 1 : 2)) return T.FLOAT;
ts.forEach((_, i) => num(i));
return isFloat32(ts[0]) ? ts[0] : T.FLOAT;
}
case 'EXACT': if (argc(1)) num(0); return T.STRING;
case 'MAX_VALUE': case 'MIN_VALUE': case 'EPSILON': {
if (!argc(1)) return T.ANY;
const a = e.args[0];
if (a.kind !== 'TypeRef') { this.error('type', a, `${name} takes the name of a numeric type, as in ${name}(${name === 'EPSILON' ? 'FLOAT64' : 'INT32'}).`, { guide: 'sized-types' }); return T.ANY; }
const ty = T[a.name];
if (name === 'EPSILON' && ty.k !== 'FLOAT') { this.error('type', a, `EPSILON measures the spacing of FLOAT values near 1.0; ${a.name} has none. For whole numbers the step is always 1.`, { guide: 'sized-types' }); return T.ANY; }
if (ty.k === 'INTEGER' && !ty.bits) { this.error('type', a, `INTEGER has no ${name === 'MAX_VALUE' ? 'largest' : 'lowest'} value: it is a whole number of any size. Name a sized type, such as INT32.`, { guide: 'sized-types' }); return T.INTEGER; }
if (ty.k === 'INTEGER' && ty.bits === 64 && (name === 'MAX_VALUE' || ty.signed)) { this.error('exact-limit', a, `${name}(${ty.name}) is ${name === 'MAX_VALUE' ? (ty.signed ? '9,223,372,036,854,775,807' : '18,446,744,073,709,551,615') : '-9,223,372,036,854,775,808'}, more than this lab holds exactly (9,007,199,254,740,991). Name a type up to 32 bits, or write the limit you need as a constant.`, { guide: 'sized-types' }); return ty; }
return ty.k === 'FLOAT' && !ty.bits ? T.FLOAT : ty;
}
case 'GCD': case 'LCM': case 'BIT_AND': case 'BIT_OR': case 'BIT_XOR': {
if (!argc(2)) return T.INTEGER;
int(0); int(1);
if (isAny(ts[0]) || isAny(ts[1])) return T.INTEGER;
return this.mix({ ...e, left: e.args[0], right: e.args[1], op: name }, ts[0], ts[1]);
}
case 'BIT_NOT': if (argc(1)) { int(0); return ts[0].k === 'INTEGER' ? ts[0] : T.INTEGER; } return T.INTEGER;
case 'SHIFT_LEFT': case 'SHIFT_RIGHT':
if (argc(2)) { int(0); int(1); return ts[0].k === 'INTEGER' ? ts[0] : T.INTEGER; }
return T.INTEGER;
case 'WRAP': {
if (!argc(2)) return T.ANY;
int(0);
// the arithmetic inside WRAP is exact (INTEGER), so that WRAP(Count + 1, UINT8) wraps instead of stopping at Count + 1
const exact = (n) => {
if (!n || typeof n !== 'object') return;
if ((n.kind === 'Binary' || n.kind === 'Unary' || n.kind === 'Num') && isSizedInt(n.type)) n.type = T.INTEGER;
if (n.kind === 'Binary') { exact(n.left); exact(n.right); } else if (n.kind === 'Unary') exact(n.arg);
};
exact(e.args[0]);
const a = e.args[1];
if (a.kind !== 'TypeRef' || !isSizedInt(T[a.name])) { this.error('type', a, 'WRAP takes a sized whole-number type as its second value, as in WRAP(X, UINT8).', { guide: 'sized-types' }); return T.ANY; }
const ty = T[a.name];
if (ty.bits > 32) { this.error('wrap-64', a, `WRAP works on types up to 32 bits in this lab, which holds whole numbers exactly only to 2^53 - 1; ${ty.name} wraps at 2^64.`, { guide: 'sized-types' }); return ty; }
return ty;
}
default: return T.ANY; default: return T.ANY;
} }
} }
} }
export function check(parsed) { /**
const c = new Checker(parsed); * @param {any} parsed
* @param {{ libs?: Map<string, any>, libOrigin?: Map<string, { module: string, line: number }> }} [options]
* libs: routines brought in with USE, already checked in their own files (UPPER name -> routine)
*/
export function check(parsed, options = {}) {
const c = new Checker(parsed, options);
const program = c.run(); const program = c.run();
program.source = parsed.source; program.source = parsed.source;
program.lines = parsed.lines; program.lines = parsed.lines;
+1 -1
View File
@@ -355,7 +355,7 @@ function exprChecks(e, say) {
say('note', 'bool-compare', x, `${sourceOf(other)} already holds TRUE or FALSE, so comparing it with ${lit.value ? 'TRUE' : 'FALSE'} asks the same question twice. Write ${positive ? sourceOf(other) : `NOT ${sourceOf(other)}`}.`, { guide: 'if' }); say('note', 'bool-compare', x, `${sourceOf(other)} already holds TRUE or FALSE, so comparing it with ${lit.value ? 'TRUE' : 'FALSE'} asks the same question twice. Write ${positive ? sourceOf(other) : `NOT ${sourceOf(other)}`}.`, { guide: 'if' });
} }
if ((a.type?.k === 'FLOAT' || b.type?.k === 'FLOAT') && isNumeric(a.type) && isNumeric(b.type)) { if ((a.type?.k === 'FLOAT' || b.type?.k === 'FLOAT') && isNumeric(a.type) && isNumeric(b.type)) {
say('warning', 'float-equal', x, `${sourceOf(x)} compares FLOAT values with ${x.op}. Two measurements that should be equal often differ in the last digit, so compare the difference against a tolerance: ABS(${sourceOf(a)} - ${sourceOf(b)}) < 0.0001.`, { guide: 'comparison' }); say('warning', 'float-equal', x, `${sourceOf(x)} compares FLOAT values with ${x.op}. Two measurements that should be equal often differ in the last digit, so compare the difference against a tolerance: ABS(${sourceOf(a)} - ${sourceOf(b)}) < 0.0001, or IS_CLOSE(${sourceOf(a)}, ${sourceOf(b)}, 1e-9, 0.0) to say the tolerance as a fraction of the values.`, { guide: 'comparison' });
} }
} }
if (x.kind === 'Unary' && x.op === 'NOT' && !x.arg.paren && (x.arg.kind === 'Binary' && ['==', '!=', '<', '<=', '>', '>='].includes(x.arg.op))) { if (x.kind === 'Unary' && x.op === 'NOT' && !x.arg.paren && (x.arg.kind === 'Binary' && ['==', '!=', '<', '<=', '>', '>='].includes(x.arg.op))) {
+195 -28
View File
@@ -29,8 +29,10 @@
import { import {
T, UNSET, PArray, PRecord, cloneValue, chars, textLength, sortedKeys, compareValues, T, UNSET, PArray, PRecord, cloneValue, chars, textLength, sortedKeys, compareValues,
valuesEqual, formatValue, outputText, typeName, isNumeric, valuesEqual, formatValue, outputText, typeName, isNumeric,
SIZED, isSizedInt, isFloat32, intRange, intRangeText, EXACT_MAX,
} from './lang.js'; } from './lang.js';
import { sourceOf } from './parse.js'; import { sourceOf } from './parse.js';
import { fma, exactDecimal, nextAfter, ulp, copySign, gcd, bitOp, wrap } from './numeric.js';
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* Control flow and errors */ /* Control flow and errors */
@@ -110,8 +112,8 @@ PArray.prototype.fillWith = function fillWith(of, interp, at) {
export function valueFits(type, v) { export function valueFits(type, v) {
if (!type || type.k === 'ANY') return true; if (!type || type.k === 'ANY') return true;
switch (type.k) { switch (type.k) {
case 'INTEGER': return Number.isInteger(v); case 'INTEGER': { if (!Number.isInteger(v)) return false; const [lo, hi] = intRange(type); return v >= lo && v <= hi; }
case 'FLOAT': return typeof v === 'number'; case 'FLOAT': return typeof v === 'number' && (!isFloat32(type) || Math.fround(v) === v || !Number.isFinite(v));
case 'CHAR': return typeof v === 'string' && textLength(v) === 1; case 'CHAR': return typeof v === 'string' && textLength(v) === 1;
case 'STRING': return typeof v === 'string'; case 'STRING': return typeof v === 'string';
case 'BOOL': return typeof v === 'boolean'; case 'BOOL': return typeof v === 'boolean';
@@ -162,6 +164,8 @@ class Interpreter {
this.lastSnapshot = null; this.lastSnapshot = null;
this.loopStack = []; this.loopStack = [];
this.quiet = 0; // >0 while evaluating contracts: their reads are not the program's this.quiet = 0; // >0 while evaluating contracts: their reads are not the program's
this.ieee = !!program.ieee; // USE IEEE: NaN and infinities flow instead of stopping the run
this.inLib = 0; // >0 while a library routine runs without being stepped into
} }
get frame() { return this.frames[this.frames.length - 1]; } get frame() { return this.frames[this.frames.length - 1]; }
@@ -188,6 +192,13 @@ class Interpreter {
/** One recorded step. `node` places it on a line. */ /** One recorded step. `node` places it on a line. */
step(node, kind, text, extra = {}) { step(node, kind, text, extra = {}) {
this.work.steps++; this.work.steps++;
if (this.inLib) {
// inside a library routine the run is not stepped into: its lines are not this program's
if (this.work.steps > this.opts.maxSteps) throw this.limitError(node);
this.reads = [];
this.writes = [];
return;
}
const line = extra.line ?? node?.line ?? null; const line = extra.line ?? node?.line ?? null;
if (line != null) this.hits.set(line, (this.hits.get(line) || 0) + 1); if (line != null) this.hits.set(line, (this.hits.get(line) || 0) + 1);
if (this.work.steps > this.opts.maxSteps) throw this.limitError(node); if (this.work.steps > this.opts.maxSteps) throw this.limitError(node);
@@ -278,7 +289,7 @@ class Interpreter {
runCall(name, args) { runCall(name, args) {
this.start(); this.start();
const sub = this.program.subs.get(up(name)); const sub = this.program.subs.get(up(name)) || this.program.libs?.get(up(name));
if (!sub) throw new PseudoError('value', `No FUNCTION or PROCEDURE is called ${name}.`, null); if (!sub) throw new PseudoError('value', `No FUNCTION or PROCEDURE is called ${name}.`, null);
if (args.length !== sub.params.length) throw new PseudoError('value', `${sub.name} takes ${plural(sub.params.length, 'value')}, and ${args.length} ${args.length === 1 ? 'was' : 'were'} given.`, sub.node); if (args.length !== sub.params.length) throw new PseudoError('value', `${sub.name} takes ${plural(sub.params.length, 'value')}, and ${args.length} ${args.length === 1 ? 'was' : 'were'} given.`, sub.node);
sub.params.forEach((p, i) => { sub.params.forEach((p, i) => {
@@ -423,8 +434,47 @@ class Interpreter {
this.noteWrite(root.name, targetPath(target, key)); this.noteWrite(root.name, targetPath(target, key));
} }
/** A whole number stored under a FLOAT name stays a number; nothing to convert. */ /**
fitNumber(type, v) { return v; } * A value going into a name, a position, a parameter or a RETURN of a sized type: a whole number that does not
* fit stops the run, and a number going into a FLOAT32 is rounded to the nearest single. Collections of sized
* numbers are checked value by value. Everything else is stored as it is.
*/
fitNumber(type, v, at) {
if (!type) return v;
if (typeof v === 'number') {
if (isSizedInt(type)) return this.fitInt(type, v, at, 'store');
if (isFloat32(type)) return this.finiteCheck(Math.fround(v), at);
return v;
}
if ((type.k === 'LIST' || type.k === 'ARRAY') && type.of && (isSizedInt(type.of) || isFloat32(type.of))) {
if (Array.isArray(v)) return v.map((x) => (x === UNSET ? x : this.fitNumber(type.of, x, at)));
if (v instanceof PArray) { v.data = v.data.map((x) => (x === UNSET ? x : this.fitNumber(type.of, x, at))); return v; }
}
return v;
}
/**
* A whole number checked against a sized type. `how` is 'store' (a value put into a name or position),
* 'result' (what an operator computed) or 'convert' (INT32(X) and its kind).
*/
fitInt(type, v, at, how) {
if (!Number.isFinite(v) || !Number.isInteger(v)) throw new PseudoError('value', `${formatValue(v)} is not a whole number, and ${type.name} holds whole numbers only.`, at);
const [lo, hi] = intRange(type);
if (v >= lo && v <= hi) return v;
const big = Math.abs(v) > EXACT_MAX ? ' (past what this lab holds exactly)' : '';
const what = how === 'result' ? `${sourceOf(at)} comes to ${formatValue(v)}${big}`
: how === 'convert' ? `${sourceOf(at)} gets ${formatValue(v)}${big}`
: `${at?.target ? sourceOf(at.target) : 'The value'} would receive ${formatValue(v)}${big}`;
throw new PseudoError('overflow', `${what}, and ${type.name} holds ${intRangeText(type)}. A sized type stops the run instead of wrapping around; write WRAP(..., ${type.name}) when wrapping is what you mean, or use a wider type.`, at, { type: type.name });
}
/** What an arithmetic operator's result becomes in its type: checked for a sized whole number, rounded for FLOAT32. */
sized(e, v) {
const t = e.type;
if (isSizedInt(t)) return this.fitInt(t, v, e, 'result');
if (isFloat32(t)) return this.finiteCheck(Math.fround(v), e);
return v;
}
/** The container a target writes into, and the key within it. */ /** The container a target writes into, and the key within it. */
locate(target, at) { locate(target, at) {
@@ -784,6 +834,7 @@ class Interpreter {
} }
invoke(sub, argSlots, at, entry) { invoke(sub, argSlots, at, entry) {
if (sub.lib && !entry && !this.opts.stepIntoLibrary && !this.inLib) return this.invokeLibrary(sub, argSlots, at);
if (this.frames.length > this.opts.maxDepth) throw new PseudoError('depth', `${sub.name} has been called ${this.opts.maxDepth} levels deep without returning. A recursive subroutine needs a case that stops calling itself, and every call has to move toward it.`, at); if (this.frames.length > this.opts.maxDepth) throw new PseudoError('depth', `${sub.name} has been called ${this.opts.maxDepth} levels deep without returning. A recursive subroutine needs a case that stops calling itself, and every call has to move toward it.`, at);
this.work.calls++; this.work.calls++;
const caller = this.frame; const caller = this.frame;
@@ -791,7 +842,7 @@ class Interpreter {
sub.params.forEach((p, i) => { sub.params.forEach((p, i) => {
const a = argSlots[i]; const a = argSlots[i];
if (a.byref) { f.vars.set(up(p.name), a.byref); f.dirty.add(up(p.name)); } if (a.byref) { f.vars.set(up(p.name), a.byref); f.dirty.add(up(p.name)); }
else f.vars.set(up(p.name), { name: p.name, type: p.type, value: a.value, line: sub.node.line, kind: 'param' }); else f.vars.set(up(p.name), { name: p.name, type: p.type, value: this.fitNumber(p.type, a.value, { line: at.line, col: at.col, target: { kind: 'Name', name: `Parameter ${p.name} of ${sub.name}` } }), line: sub.node.line, kind: 'param' });
}); });
const shown = sub.params.map((p) => `${p.name} = ${formatValue(f.vars.get(up(p.name)).value, p.type, { bareChar: true, max: 10 })}`).join(', '); const shown = sub.params.map((p) => `${p.name} = ${formatValue(f.vars.get(up(p.name)).value, p.type, { bareChar: true, max: 10 })}`).join(', ');
this.frames.push(f); this.frames.push(f);
@@ -818,7 +869,7 @@ class Interpreter {
this.step({ line: sub.node.endLine }, 'return', `${sub.name} ends and control goes back to line ${at.line}.`); this.step({ line: sub.node.endLine }, 'return', `${sub.name} ends and control goes back to line ${at.line}.`);
} catch (e) { } catch (e) {
if (!(e instanceof ReturnSignal)) throw e; if (!(e instanceof ReturnSignal)) throw e;
result = e.value; result = sub.returns ? this.fitNumber(sub.returns, e.value, { line: e.line, col: 1, target: { kind: 'Name', name: `the RESULT of ${sub.name}` } }) : e.value;
} }
if (this.opts.contracts && sub.ensures.length) { if (this.opts.contracts && sub.ensures.length) {
for (const en of sub.ensures) { for (const en of sub.ensures) {
@@ -838,6 +889,32 @@ class Interpreter {
return result; return result;
} }
/**
* A routine from the library, run without stepping into it: one step where it is called, one where it hands
* back, and nothing of its own lines in this program's trace (they belong to another file). Its contracts are
* still checked; a failure is reported at the call, naming the routine.
*/
invokeLibrary(sub, argSlots, at) {
const origin = sub.lib ? `${sub.lib.module}.${sub.name}` : sub.name;
const shown = sub.params.map((p, i) => `${p.name} = ${formatValue(argSlots[i].byref ? argSlots[i].byref.value : argSlots[i].value, p.type, { bareChar: true, max: 10 })}`).join(', ');
this.step(at, 'call', `${sub.name} from the library (${origin}) runs with ${shown || 'no values'}; open it in the library to step through it.`, { callee: sub.name, library: origin });
this.inLib++;
let result;
try {
result = this.invoke(sub, argSlots, at, false);
} catch (e) {
if (e instanceof PseudoError) {
const where = e.line != null ? ` (line ${e.line} of ${origin})` : '';
throw new PseudoError(e.kind, `In the library routine ${origin}${where}: ${e.message}`, at, { library: origin, libraryLine: e.line });
}
throw e;
} finally {
this.inLib--;
}
if (sub.kind === 'Function') this.step(at, 'return', `${origin} hands back ${formatValue(result, sub.returns)}.`, { value: result, library: origin });
return result;
}
/** /**
* Evaluate an ENSURE: RESULT is the returned value, and a by-value * Evaluate an ENSURE: RESULT is the returned value, and a by-value
* parameter means the value it was called with, even if the body changed * parameter means the value it was called with, even if the body changed
@@ -919,7 +996,7 @@ class Interpreter {
/* ---------- expressions ---------- */ /* ---------- expressions ---------- */
evaluate(e) { evaluate(e) {
switch (e.kind) { switch (e.kind) {
case 'Num': return this.finiteCheck(e.value, e); case 'Num': return this.finiteCheck(isFloat32(e.type) ? Math.fround(e.value) : e.value, e);
case 'Str': case 'Char': case 'Bool': return e.value; case 'Str': case 'Char': case 'Bool': return e.value;
case 'Name': { case 'Name': {
if (up(e.name) === 'RESULT' && e.sym?.kind === 'result') { if (up(e.name) === 'RESULT' && e.sym?.kind === 'result') {
@@ -933,7 +1010,7 @@ class Interpreter {
case 'Unary': { case 'Unary': {
const v = this.evaluate(e.arg); const v = this.evaluate(e.arg);
if (e.op === 'NOT') return !v; if (e.op === 'NOT') return !v;
return this.intCheck(-v, e); return this.sized(e, this.intCheck(-v, e));
} }
case 'Binary': return this.binary(e); case 'Binary': return this.binary(e);
case 'Chain': { case 'Chain': {
@@ -994,7 +1071,7 @@ class Interpreter {
* run inside what a proof covers (tests/prover/float-overflow.pseudo). * run inside what a proof covers (tests/prover/float-overflow.pseudo).
*/ */
finiteCheck(v, e) { finiteCheck(v, e) {
if (typeof v === 'number' && !Number.isFinite(v)) { if (typeof v === 'number' && !Number.isFinite(v) && !this.ieee) {
throw new PseudoError('overflow', Number.isNaN(v) throw new PseudoError('overflow', Number.isNaN(v)
? `${sourceOf(e)} has no number as its answer: the calculation went past the largest FLOAT this lab can hold, about 1.8 × 10^308, along the way.` ? `${sourceOf(e)} has no number as its answer: the calculation went past the largest FLOAT this lab can hold, about 1.8 × 10^308, along the way.`
: `${sourceOf(e)} comes to ${v > 0 ? 'more than' : 'less than minus'} about 1.8 × 10^308, beyond the largest FLOAT this lab can hold.`, e); : `${sourceOf(e)} comes to ${v > 0 ? 'more than' : 'less than minus'} about 1.8 × 10^308, beyond the largest FLOAT this lab can hold.`, e);
@@ -1006,24 +1083,26 @@ class Interpreter {
const op = e.op; const op = e.op;
if (op === 'AND') { const l = this.evaluate(e.left); return l ? Boolean(this.evaluate(e.right)) : false; } if (op === 'AND') { const l = this.evaluate(e.left); return l ? Boolean(this.evaluate(e.right)) : false; }
if (op === 'OR') { const l = this.evaluate(e.left); return l ? true : Boolean(this.evaluate(e.right)); } if (op === 'OR') { const l = this.evaluate(e.left); return l ? true : Boolean(this.evaluate(e.right)); }
const l = this.evaluate(e.left); let l = this.evaluate(e.left);
const r = this.evaluate(e.right); let r = this.evaluate(e.right);
// FLOAT32 arithmetic: both sides are singles (a whole number is converted first), and the result is rounded
if (isFloat32(e.type) && ['+', '-', '*', '/', '^'].includes(op)) { l = Math.fround(l); r = Math.fround(r); }
switch (op) { switch (op) {
case '+': return this.intCheck(l + r, e); case '+': return this.sized(e, this.intCheck(l + r, e));
case '-': return this.intCheck(l - r, e); case '-': return this.sized(e, this.intCheck(l - r, e));
case '*': return this.intCheck(l * r, e); case '*': return this.sized(e, this.intCheck(l * r, e));
case '/': case '/':
if (r === 0) throw this.zeroError(e); if (r === 0 && !(this.ieee && e.type?.k === 'FLOAT')) throw this.zeroError(e);
return this.finiteCheck(l / r, e); return this.sized(e, this.finiteCheck(l / r, e));
case 'DIV': case 'DIV':
if (r === 0) throw this.zeroError(e); if (r === 0) throw this.zeroError(e);
return Math.floor(l / r); return this.sized(e, Math.floor(l / r));
case 'MOD': case 'MOD':
if (r === 0) throw this.zeroError(e); if (r === 0) throw this.zeroError(e);
return l - r * Math.floor(l / r); return this.sized(e, l - r * Math.floor(l / r));
case '^': { case '^': {
if (e.type?.k === 'INTEGER' && r < 0) throw new PseudoError('value', `${sourceOf(e)} raises a whole number to the power ${r}, which is not whole. Write ${sourceOf(e.left)} as a FLOAT, such as ${l}.0, for a fractional answer.`, e); if (e.type?.k === 'INTEGER' && r < 0) throw new PseudoError('value', `${sourceOf(e)} raises a whole number to the power ${r}, which is not whole. Write ${sourceOf(e.left)} as a FLOAT, such as ${l}.0, for a fractional answer.`, e);
return this.intCheck(l ** r, e); return this.sized(e, this.intCheck(l ** r, e));
} }
case '&': return String(l) + String(r); case '&': return String(l) + String(r);
default: return this.compare(op, l, r, e, e.left, e.right); default: return this.compare(op, l, r, e, e.left, e.right);
@@ -1142,23 +1221,38 @@ class Interpreter {
if (old && n.kind === 'Name' && old.has(up(n.name))) return old.get(up(n.name)); if (old && n.kind === 'Name' && old.has(up(n.name))) return old.get(up(n.name));
return this.evaluate(n); return this.evaluate(n);
} }
if (e.sub || this.program.subs.get(up(e.name))) { if (e.sub || this.program.subs.get(up(e.name)) || this.program.libs?.get(up(e.name))) {
const sub = e.sub || this.program.subs.get(up(e.name)); const sub = e.sub || this.program.subs.get(up(e.name)) || this.program.libs.get(up(e.name));
const slots = this.argSlots(sub, e.args); const slots = this.argSlots(sub, e.args);
return this.invoke(sub, slots, e, false); return this.invoke(sub, slots, e, false);
} }
const args = e.args.map((a) => this.evaluate(a)); if (e.convert) return this.convert(e, this.evaluate(e.args[0]));
const args = e.args.map((a) => (a.kind === 'TypeRef' ? T[a.name] : this.evaluate(a)));
return this.finiteCheck(this.builtin(up(e.name), args, e), e); return this.finiteCheck(this.builtin(up(e.name), args, e), e);
} }
/** INT32(X), FLOAT32(X), ...: a FLOAT loses its fraction toward zero for a whole-number type; out of range stops. */
convert(e, v) {
const to = SIZED[up(e.name)];
if (to.k === 'INTEGER') {
if (!Number.isFinite(v)) throw new PseudoError('value', `${sourceOf(e)} has no answer: ${formatValue(v)} is not a number that has a whole part.`, e);
return this.fitInt(to, Math.trunc(v) || 0, e, 'convert');
}
return isFloat32(to) ? this.finiteCheck(Math.fround(v), e) : v;
}
/** A whole-number result that must still fit exactly. */ /** A whole-number result that must still fit exactly. */
checkInt(v, e) { checkInt(v, e) {
if (!Number.isSafeInteger(v)) throw new PseudoError('overflow', `${sourceOf(e)} comes to more than 9,007,199,254,740,991, the largest whole number this lab can hold exactly.`, e); if (!Number.isSafeInteger(v)) throw new PseudoError('overflow', `${sourceOf(e)} comes to more than 9,007,199,254,740,991, the largest whole number this lab can hold exactly.`, e);
return v; return v;
} }
/** A whole-number result checked against its type: a sized type's range, or what INTEGER holds exactly. */
intResult(e, v) { return isSizedInt(e.type) ? this.fitInt(e.type, v, e, 'result') : this.checkInt(v, e); }
builtin(name, a, e) { builtin(name, a, e) {
const [x, y, z] = a; const [x, y, z] = a;
const a4 = () => a[3];
switch (name) { switch (name) {
case 'LENGTH': case 'LENGTH':
if (typeof x === 'string') return textLength(x); if (typeof x === 'string') return textLength(x);
@@ -1177,10 +1271,10 @@ class Interpreter {
if (!list.length) throw new PseudoError('value', `${name} of an empty collection has no answer.`, e); if (!list.length) throw new PseudoError('value', `${name} of an empty collection has no answer.`, e);
return list.reduce((m, v) => ((name === 'MIN' ? compareValues(v, m) < 0 : compareValues(v, m) > 0) ? v : m)); return list.reduce((m, v) => ((name === 'MIN' ? compareValues(v, m) < 0 : compareValues(v, m) > 0) ? v : m));
} }
case 'SQRT': if (x < 0) throw new PseudoError('value', `SQRT(${formatValue(x)}) has no real answer: the number is negative.`, e); return Math.sqrt(x); case 'SQRT': if (x < 0 && !this.ieee) throw new PseudoError('value', `SQRT(${formatValue(x)}) has no real answer: the number is negative.`, e); return Math.sqrt(x);
case 'EXP': return Math.exp(x); case 'EXP': return Math.exp(x);
case 'LOG': case 'LOG2': case 'LOG': case 'LOG2':
if (x <= 0) throw new PseudoError('value', `${name}(${formatValue(x)}) has no answer: a logarithm needs a number above 0.${x === 0 ? ' A probability of 0 often means a pseudocount is missing.' : ''}`, e); if (x <= 0 && !this.ieee) throw new PseudoError('value', `${name}(${formatValue(x)}) has no answer: a logarithm needs a number above 0.${x === 0 ? ' A probability of 0 often means a pseudocount is missing.' : ''}`, e);
return name === 'LOG' ? Math.log(x) : Math.log2(x); return name === 'LOG' ? Math.log(x) : Math.log2(x);
case 'RAND': return this.random() * x; case 'RAND': return this.random() * x;
case 'RANDINT': case 'RANDINT':
@@ -1231,7 +1325,7 @@ class Interpreter {
case 'FLOOR': return Math.floor(x); case 'FLOOR': return Math.floor(x);
case 'CEIL': return Math.ceil(x); case 'CEIL': return Math.ceil(x);
case 'LOG10': case 'LOG10':
if (x <= 0) throw new PseudoError('value', `LOG10(${formatValue(x)}) has no answer: a logarithm needs a number above 0.`, e); if (x <= 0 && !this.ieee) throw new PseudoError('value', `LOG10(${formatValue(x)}) has no answer: a logarithm needs a number above 0.`, e);
return Math.log10(x); return Math.log10(x);
case 'SIN': return Math.sin(x); case 'SIN': return Math.sin(x);
case 'COS': return Math.cos(x); case 'COS': return Math.cos(x);
@@ -1251,7 +1345,14 @@ class Interpreter {
const xs = x instanceof PArray ? x.data : x; const xs = x instanceof PArray ? x.data : x;
for (const v of xs) if (v === UNSET) throw new PseudoError('unset', `${name}(${sourceOf(e.args[0])}): a position of ${sourceOf(e.args[0])} has not been given a value yet.`, e); for (const v of xs) if (v === UNSET) throw new PseudoError('unset', `${name}(${sourceOf(e.args[0])}): a position of ${sourceOf(e.args[0])} has not been given a value yet.`, e);
const n = xs.length; const n = xs.length;
const isFloat = e.args[0].type?.of?.k === 'FLOAT' || xs.some((v) => !Number.isInteger(v)); const el = e.args[0].type?.of;
const isFloat = el?.k === 'FLOAT' || xs.some((v) => !Number.isInteger(v));
if (name === 'SUM_OF' && (isFloat32(el) || isSizedInt(el))) {
// a running total kept in the values' own type, as a loop with a total of that type would keep it
let t = 0;
for (const v of xs) t = isFloat32(el) ? Math.fround(t + v) : this.fitInt(el, t + v, e, 'result');
return this.finiteCheck(t, e);
}
if (name === 'SUM_OF') { const t = xs.reduce((m, v) => m + v, 0); return isFloat ? t : this.checkInt(t, e); } if (name === 'SUM_OF') { const t = xs.reduce((m, v) => m + v, 0); return isFloat ? t : this.checkInt(t, e); }
if (name === 'NORM_OF') return Math.sqrt(xs.reduce((m, v) => m + v * v, 0)); if (name === 'NORM_OF') return Math.sqrt(xs.reduce((m, v) => m + v * v, 0));
if (n === 0) throw new PseudoError('value', `${name} of an empty collection has no answer.`, e); if (n === 0) throw new PseudoError('value', `${name} of an empty collection has no answer.`, e);
@@ -1265,6 +1366,11 @@ class Interpreter {
case 'DOT': { case 'DOT': {
const xs = x instanceof PArray ? x.data : x; const ys = y instanceof PArray ? y.data : y; const xs = x instanceof PArray ? x.data : x; const ys = y instanceof PArray ? y.data : y;
if (xs.length !== ys.length) throw new PseudoError('value', `DOT needs two collections of the same length, and these hold ${xs.length} and ${ys.length} values.`, e); if (xs.length !== ys.length) throw new PseudoError('value', `DOT needs two collections of the same length, and these hold ${xs.length} and ${ys.length} values.`, e);
if (isFloat32(e.type) || isSizedInt(e.type)) {
let t = 0;
for (let i = 0; i < xs.length; i++) t = isFloat32(e.type) ? Math.fround(t + Math.fround(xs[i] * ys[i])) : this.fitInt(e.type, t + this.fitInt(e.type, xs[i] * ys[i], e, 'result'), e, 'result');
return t;
}
let t = 0; for (let i = 0; i < xs.length; i++) t += xs[i] * ys[i]; let t = 0; for (let i = 0; i < xs.length; i++) t += xs[i] * ys[i];
const isFloat = e.type?.k === 'FLOAT' || !Number.isInteger(t); const isFloat = e.type?.k === 'FLOAT' || !Number.isInteger(t);
return isFloat ? t : this.checkInt(t, e); return isFloat ? t : this.checkInt(t, e);
@@ -1273,6 +1379,67 @@ class Interpreter {
if (!Number.isInteger(x) || x < 0) throw new PseudoError('value', `FILL(${formatValue(x)}, ...) needs a whole number of copies, 0 or more.`, e); if (!Number.isInteger(x) || x < 0) throw new PseudoError('value', `FILL(${formatValue(x)}, ...) needs a whole number of copies, 0 or more.`, e);
const out = []; for (let i = 0; i < x; i++) out.push(cloneValue(y)); return out; const out = []; for (let i = 0; i < x; i++) out.push(cloneValue(y)); return out;
} }
// ---- numerical work
case 'ASIN': case 'ACOS':
if ((x < -1 || x > 1) && !this.ieee) throw new PseudoError('value', `${name}(${formatValue(x)}) has no answer: ${name === 'ASIN' ? 'a sine' : 'a cosine'} lies between -1 and 1.`, e);
return name === 'ASIN' ? Math.asin(x) : Math.acos(x);
case 'ATAN': return Math.atan(x);
case 'SINH': return Math.sinh(x);
case 'COSH': return Math.cosh(x);
case 'TANH': return Math.tanh(x);
case 'HYPOT': return Math.hypot(x, y);
case 'POW':
if (x < 0 && !Number.isInteger(y) && !this.ieee) throw new PseudoError('value', `POW(${formatValue(x)}, ${formatValue(y)}) has no real answer: a negative number raised to a power that is not whole.`, e);
if (x === 0 && y < 0 && !this.ieee) throw new PseudoError('value', `POW(0, ${formatValue(y)}) has no answer: it divides by zero.`, e);
return x ** y;
case 'CBRT': return Math.cbrt(x);
case 'EXPM1': return Math.expm1(x);
case 'LOG1P':
if (x <= -1 && !this.ieee) throw new PseudoError('value', `LOG1P(${formatValue(x)}) has no answer: 1 + X has to be above 0.`, e);
return Math.log1p(x);
case 'E': return Math.E;
case 'TRUNC': return Math.trunc(x);
case 'SIGN': return x > 0 ? 1 : x < 0 ? -1 : 0;
case 'COPYSIGN': return copySign(x, y);
case 'CLAMP':
if (y > z) throw new PseudoError('value', `CLAMP(${formatValue(x)}, ${formatValue(y)}, ${formatValue(z)}) has no answer: the low end is above the high end.`, e);
return x < y ? y : x > z ? z : x;
case 'FMA': return fma(x, y, z, isFloat32(e.type));
case 'IS_NAN': return Number.isNaN(x);
case 'IS_INF': return x === Infinity || x === -Infinity;
case 'IS_FINITE': return Number.isFinite(x);
case 'INF': return Infinity;
case 'NAN': return NaN;
case 'ULP': return ulp(x, isFloat32(e.type));
case 'NEXT_AFTER': return nextAfter(isFloat32(e.type) ? Math.fround(x) : x, y, isFloat32(e.type));
case 'IS_CLOSE': {
const rel = z; const tol = a4();
if (rel < 0 || tol < 0) throw new PseudoError('value', 'IS_CLOSE needs tolerances of 0 or more.', e);
if (x === y) return true;
if (!Number.isFinite(x) || !Number.isFinite(y)) return false;
return Math.abs(x - y) <= Math.max(rel * Math.max(Math.abs(x), Math.abs(y)), tol);
}
case 'EXACT': return exactDecimal(x);
case 'MAX_VALUE': case 'MIN_VALUE': {
const ty = x;
if (ty.k === 'INTEGER') { const [lo, hi] = intRange(ty); return name === 'MAX_VALUE' ? hi : lo; }
const m = isFloat32(ty) ? 3.4028234663852886e38 : Number.MAX_VALUE;
return name === 'MAX_VALUE' ? m : -m;
}
case 'EPSILON': return isFloat32(x) ? 2 ** -23 : Number.EPSILON;
case 'GCD': return gcd(x, y);
case 'LCM': return x === 0 || y === 0 ? 0 : this.intResult(e, Math.abs((x / gcd(x, y)) * y));
case 'BIT_AND': return this.intResult(e, bitOp('AND', x, y));
case 'BIT_OR': return this.intResult(e, bitOp('OR', x, y));
case 'BIT_XOR': return this.intResult(e, bitOp('XOR', x, y));
case 'BIT_NOT': {
const ty = e.args[0].type;
return isSizedInt(ty) && !ty.signed ? intRange(ty)[1] - x : -x - 1;
}
case 'SHIFT_LEFT': case 'SHIFT_RIGHT':
if (!Number.isInteger(y) || y < 0) throw new PseudoError('value', `${name} moves bits by 0 or more places, and ${formatValue(y)} is not that.`, e);
return this.intResult(e, bitOp(name === 'SHIFT_LEFT' ? 'SHL' : 'SHR', x, y));
case 'WRAP': return wrap(x, y.bits, y.signed);
default: throw new PseudoError('value', `${name} is not a built-in function.`, e); default: throw new PseudoError('value', `${name} is not a built-in function.`, e);
} }
} }
@@ -1369,7 +1536,7 @@ export function run(program, options = {}) {
throw e; throw e;
} }
} }
const sub = options.call ? program.subs.get(up(options.call.name)) : null; const sub = options.call ? (program.subs.get(up(options.call.name)) || program.libs?.get(up(options.call.name))) : null;
return { return {
program, program,
steps: it.steps, steps: it.steps,
+147 -3
View File
@@ -16,10 +16,13 @@
/** Words that belong to the notation and can never be names. */ /** Words that belong to the notation and can never be names. */
export const KEYWORDS = new Set([ export const KEYWORDS = new Set([
'PARALLEL', 'PARALLEL',
// libraries
'USE',
// declarations // declarations
'DECLARE', 'CONSTANT', 'DEFINE', 'TYPE', 'ENDTYPE', 'OF', 'DECLARE', 'CONSTANT', 'DEFINE', 'TYPE', 'ENDTYPE', 'OF',
// types // types
'INTEGER', 'FLOAT', 'CHAR', 'STRING', 'BOOL', 'BOOLEAN', 'ARRAY', 'LIST', 'HASHMAP', 'SET', 'INTEGER', 'FLOAT', 'CHAR', 'STRING', 'BOOL', 'BOOLEAN', 'ARRAY', 'LIST', 'HASHMAP', 'SET',
'INT8', 'INT16', 'INT32', 'INT64', 'UINT8', 'UINT16', 'UINT32', 'UINT64', 'FLOAT32', 'FLOAT64',
// selection // selection
'IF', 'THEN', 'ELSEIF', 'ELSE', 'ENDIF', 'CASE', 'OTHERWISE', 'ENDCASE', 'IF', 'THEN', 'ELSEIF', 'ELSE', 'ENDIF', 'CASE', 'OTHERWISE', 'ENDCASE',
// iteration // iteration
@@ -77,10 +80,10 @@ export const KEYWORD_CLASS = (() => {
const m = {}; const m = {};
const put = (cls, words) => { for (const w of words.split(' ')) m[w] = cls; }; const put = (cls, words) => { for (const w of words.split(' ')) m[w] = cls; };
put('flow', 'IF THEN ELSEIF ELSE ENDIF CASE OTHERWISE ENDCASE FOR TO STEP NEXT WHILE ENDWHILE REPEAT UNTIL RETURN BREAK CONTINUE EACH'); put('flow', 'IF THEN ELSEIF ELSE ENDIF CASE OTHERWISE ENDCASE FOR TO STEP NEXT WHILE ENDWHILE REPEAT UNTIL RETURN BREAK CONTINUE EACH');
put('decl', 'DECLARE CONSTANT DEFINE TYPE ENDTYPE FUNCTION ENDFUNCTION PROCEDURE ENDPROCEDURE RETURNS'); put('decl', 'DECLARE CONSTANT DEFINE TYPE ENDTYPE FUNCTION ENDFUNCTION PROCEDURE ENDPROCEDURE RETURNS USE');
put('kw', 'AND OR NOT DIV MOD OF CALL IN BYREF BYVAL'); put('kw', 'AND OR NOT DIV MOD OF CALL IN BYREF BYVAL');
put('io', 'INPUT OUTPUT'); put('io', 'INPUT OUTPUT');
put('type', 'INTEGER FLOAT CHAR STRING BOOL BOOLEAN ARRAY LIST HASHMAP SET'); put('type', 'INTEGER FLOAT CHAR STRING BOOL BOOLEAN ARRAY LIST HASHMAP SET INT8 INT16 INT32 INT64 UINT8 UINT16 UINT32 UINT64 FLOAT32 FLOAT64');
put('const', 'TRUE FALSE'); put('const', 'TRUE FALSE');
put('proof', 'REQUIRE ENSURE INVARIANT DECREASES ASSERT FORALL EXISTS SUM RESULT OLD'); put('proof', 'REQUIRE ENSURE INVARIANT DECREASES ASSERT FORALL EXISTS SUM RESULT OLD');
return m; return m;
@@ -96,9 +99,61 @@ export const CLOSERS = {
/* Types */ /* Types */
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/**
* The sized numeric types. Each is an INTEGER or a FLOAT with a width, so every rule that asks for "a whole
* number" (a position, a FOR counter, DIV) accepts INT32 as readily as INTEGER, and only the rules about
* range, rounding and mixing look at the width.
*
* INTEGER a whole number of any size (the teaching integer; a run holds it exactly to 2^53 - 1)
* INT8 ... INT64 two's complement, n bits; a result outside the range stops the run (obligation 'overflow')
* UINT8 ... UINT64 0 to 2^n - 1, the same way
* FLOAT, FLOAT64 IEEE 754 double precision (the two names are one type)
* FLOAT32 IEEE 754 single precision: every result is rounded to the nearest single
*
* INT64 and UINT64 are held exactly only to 2^53 - 1 in this lab (a JavaScript number); a result past that
* stops the run with a message that says so, and the prover checks against the same limit.
*/
export const SIZED = {
INT8: { k: 'INTEGER', bits: 8, signed: true, name: 'INT8' },
INT16: { k: 'INTEGER', bits: 16, signed: true, name: 'INT16' },
INT32: { k: 'INTEGER', bits: 32, signed: true, name: 'INT32' },
INT64: { k: 'INTEGER', bits: 64, signed: true, name: 'INT64' },
UINT8: { k: 'INTEGER', bits: 8, signed: false, name: 'UINT8' },
UINT16: { k: 'INTEGER', bits: 16, signed: false, name: 'UINT16' },
UINT32: { k: 'INTEGER', bits: 32, signed: false, name: 'UINT32' },
UINT64: { k: 'INTEGER', bits: 64, signed: false, name: 'UINT64' },
FLOAT32: { k: 'FLOAT', bits: 32, name: 'FLOAT32' },
FLOAT64: { k: 'FLOAT', bits: 64, name: 'FLOAT64' },
};
export const SIZED_NAMES = new Set(Object.keys(SIZED));
/** The largest whole number a run holds exactly. */
export const EXACT_MAX = Number.MAX_SAFE_INTEGER;
/** A whole-number type with a width (INT32, UINT8, ...), as opposed to INTEGER. */
export const isSizedInt = (t) => !!t && t.k === 'INTEGER' && !!t.bits;
/** FLOAT32, the one FLOAT that rounds to fewer bits than FLOAT. */
export const isFloat32 = (t) => !!t && t.k === 'FLOAT' && t.bits === 32;
/**
* The values a sized whole-number type holds in a run: [lowest, highest]. For INT64 and UINT64 the run's range
* is cut at 2^53 - 1, the largest whole number it holds exactly; `full` gives the type's own range as text.
*/
export function intRange(t) {
if (!isSizedInt(t)) return [-EXACT_MAX, EXACT_MAX];
if (t.bits >= 64) return t.signed ? [-EXACT_MAX, EXACT_MAX] : [0, EXACT_MAX];
return t.signed ? [-(2 ** (t.bits - 1)), 2 ** (t.bits - 1) - 1] : [0, 2 ** t.bits - 1];
}
export function intRangeText(t) {
const full = { INT64: ['-9,223,372,036,854,775,808', '9,223,372,036,854,775,807'], UINT64: ['0', '18,446,744,073,709,551,615'] }[t.name];
if (full) return `${full[0]} to ${full[1]}, held exactly in this lab only from ${t.signed ? '-9,007,199,254,740,991' : '0'} to 9,007,199,254,740,991`;
const [lo, hi] = intRange(t);
return `${lo.toLocaleString('en-US')} to ${hi.toLocaleString('en-US')}`;
}
export const T = { export const T = {
INTEGER: { k: 'INTEGER' }, INTEGER: { k: 'INTEGER' },
FLOAT: { k: 'FLOAT' }, FLOAT: { k: 'FLOAT' },
...SIZED,
CHAR: { k: 'CHAR' }, CHAR: { k: 'CHAR' },
STRING: { k: 'STRING' }, STRING: { k: 'STRING' },
BOOL: { k: 'BOOL' }, BOOL: { k: 'BOOL' },
@@ -129,6 +184,7 @@ export function typeName(t) {
case 'RANGE': return 'a range of positions'; case 'RANGE': return 'a range of positions';
case 'ANY': return 'any type'; case 'ANY': return 'any type';
case 'VOID': return 'nothing'; case 'VOID': return 'nothing';
case 'INTEGER': case 'FLOAT': return t.name || t.k;
default: return t.k; default: return t.k;
} }
} }
@@ -142,6 +198,8 @@ export function sameType(a, b) {
case 'ARRAY': return a.dims === b.dims && sameType(a.of, b.of); case 'ARRAY': return a.dims === b.dims && sameType(a.of, b.of);
case 'HASHMAP': return sameType(a.key, b.key) && sameType(a.val, b.val); case 'HASHMAP': return sameType(a.key, b.key) && sameType(a.val, b.val);
case 'RECORD': return a.name.toUpperCase() === b.name.toUpperCase(); case 'RECORD': return a.name.toUpperCase() === b.name.toUpperCase();
case 'INTEGER': return (a.bits || 0) === (b.bits || 0) && (!a.bits || a.signed === b.signed);
case 'FLOAT': return isFloat32(a) === isFloat32(b);
default: return true; default: return true;
} }
} }
@@ -154,6 +212,11 @@ export function sameType(a, b) {
export function assignable(to, from) { export function assignable(to, from) {
if (isAny(to) || isAny(from)) return true; if (isAny(to) || isAny(from)) return true;
if (to.k === 'FLOAT' && from.k === 'INTEGER') return true; if (to.k === 'FLOAT' && from.k === 'INTEGER') return true;
// INTEGER into a sized type is checked when it happens (the run stops if it does not fit, and the prover has
// an obligation for it); a sized value always fits INTEGER; two different widths need a conversion: INT32(X).
if (to.k === 'INTEGER' && from.k === 'INTEGER') return !to.bits || !from.bits || sameType(to, from);
// FLOAT32 widens into FLOAT exactly; the other way rounds, so it is written FLOAT32(X).
if (to.k === 'FLOAT' && from.k === 'FLOAT') return !isFloat32(to) || isFloat32(from);
if (to.k === 'ARRAY' && from.k === 'LIST') return assignable(to.of, from.of); if (to.k === 'ARRAY' && from.k === 'LIST') return assignable(to.of, from.of);
if (to.k === 'LIST' && from.k === 'LIST') return assignable(to.of, from.of) && (isAny(from.of) || sameType(to.of, from.of)); if (to.k === 'LIST' && from.k === 'LIST') return assignable(to.of, from.of) && (isAny(from.of) || sameType(to.of, from.of));
if (to.k === 'SET' && from.k === 'SET') return isAny(from.of) || sameType(to.of, from.of); if (to.k === 'SET' && from.k === 'SET') return isAny(from.of) || sameType(to.of, from.of);
@@ -264,13 +327,26 @@ export function formatFloat(x) {
* "GATTACA" with its quotes, [4, 17] for a list. `type` decides INTEGER * "GATTACA" with its quotes, [4, 17] for a list. `type` decides INTEGER
* versus FLOAT, since both are JavaScript numbers underneath. * versus FLOAT, since both are JavaScript numbers underneath.
*/ */
/**
* A FLOAT32 as the shortest decimal that rounds back to the same single: FLOAT32(0.1) shows as 0.1, though the
* value it holds is 0.100000001490116119384765625 (EXACT shows that).
*/
export function formatFloat32(x) {
if (!Number.isFinite(x)) return formatFloat(x);
for (let p = 1; p <= 9; p++) {
const s = x.toPrecision(p);
if (Math.fround(Number(s)) === x) return formatFloat(Number(s));
}
return formatFloat(x);
}
export function formatValue(v, type, opts = {}) { export function formatValue(v, type, opts = {}) {
const max = opts.max ?? 40; const max = opts.max ?? 40;
if (v === UNSET) return opts.unset ?? '(no value)'; if (v === UNSET) return opts.unset ?? '(no value)';
if (v === undefined || v === null) return '(nothing)'; if (v === undefined || v === null) return '(nothing)';
if (typeof v === 'boolean') return v ? 'TRUE' : 'FALSE'; if (typeof v === 'boolean') return v ? 'TRUE' : 'FALSE';
if (typeof v === 'number') { if (typeof v === 'number') {
if (type && type.k === 'FLOAT') return formatFloat(v); if (type && type.k === 'FLOAT') return isFloat32(type) ? formatFloat32(v) : formatFloat(v);
if (type && type.k === 'INTEGER') return String(v); if (type && type.k === 'INTEGER') return String(v);
return Number.isInteger(v) ? String(v) : formatFloat(v); return Number.isInteger(v) ? String(v) : formatFloat(v);
} }
@@ -364,6 +440,48 @@ export const BUILTINS = {
TO_STRING: { kind: 'function', sig: 'TO_STRING(X)', doc: 'Any value written as a STRING, the way OUTPUT would print it.', group: 'Converting' }, TO_STRING: { kind: 'function', sig: 'TO_STRING(X)', doc: 'Any value written as a STRING, the way OUTPUT would print it.', group: 'Converting' },
TO_INTEGER: { kind: 'function', sig: 'TO_INTEGER(S)', doc: 'The INTEGER a STRING of digits spells, or the code of a CHAR digit.', group: 'Converting' }, TO_INTEGER: { kind: 'function', sig: 'TO_INTEGER(S)', doc: 'The INTEGER a STRING of digits spells, or the code of a CHAR digit.', group: 'Converting' },
TO_FLOAT: { kind: 'function', sig: 'TO_FLOAT(X)', doc: 'The FLOAT a STRING spells, or an INTEGER as a FLOAT.', group: 'Converting' }, TO_FLOAT: { kind: 'function', sig: 'TO_FLOAT(X)', doc: 'The FLOAT a STRING spells, or an INTEGER as a FLOAT.', group: 'Converting' },
// ---- more numbers, for numerical work
ASIN: { kind: 'function', sig: 'ASIN(X)', doc: 'The angle in radians whose sine is X, in [-PI()/2, PI()/2]; X must lie in [-1, 1].', group: 'Numbers' },
ACOS: { kind: 'function', sig: 'ACOS(X)', doc: 'The angle in radians whose cosine is X, in [0, PI()]; X must lie in [-1, 1].', group: 'Numbers' },
ATAN: { kind: 'function', sig: 'ATAN(X)', doc: 'The angle in radians whose tangent is X, in (-PI()/2, PI()/2).', group: 'Numbers' },
SINH: { kind: 'function', sig: 'SINH(X)', doc: 'The hyperbolic sine of X, as a FLOAT.', group: 'Numbers' },
COSH: { kind: 'function', sig: 'COSH(X)', doc: 'The hyperbolic cosine of X, as a FLOAT.', group: 'Numbers' },
TANH: { kind: 'function', sig: 'TANH(X)', doc: 'The hyperbolic tangent of X, as a FLOAT, in (-1, 1).', group: 'Numbers' },
HYPOT: { kind: 'function', sig: 'HYPOT(X, Y)', doc: 'SQRT(X * X + Y * Y), computed without overflowing on the way when X or Y is very large.', group: 'Numbers' },
POW: { kind: 'function', sig: 'POW(X, Y)', doc: 'X raised to the power Y, as a FLOAT; X must not be negative unless Y is whole.', group: 'Numbers' },
CBRT: { kind: 'function', sig: 'CBRT(X)', doc: 'The cube root of X, as a FLOAT; defined for negative X too.', group: 'Numbers' },
EXPM1: { kind: 'function', sig: 'EXPM1(X)', doc: 'EXP(X) - 1, accurate when X is close to 0, where the subtraction would lose every digit.', group: 'Numbers' },
LOG1P: { kind: 'function', sig: 'LOG1P(X)', doc: 'LOG(1 + X), accurate when X is close to 0; X must be above -1.', group: 'Numbers' },
E: { kind: 'function', sig: 'E()', doc: 'The number e, 2.71828…, as a FLOAT.', group: 'Numbers' },
TRUNC: { kind: 'function', sig: 'TRUNC(X)', doc: 'X with its fraction dropped toward zero, kept as a FLOAT: TRUNC(-2.7) is -2.0. INT does the same and gives an INTEGER.', group: 'Numbers' },
SIGN: { kind: 'function', sig: 'SIGN(X)', doc: '-1, 0 or 1, as X is negative, zero or positive, in the type of X.', group: 'Numbers' },
COPYSIGN: { kind: 'function', sig: 'COPYSIGN(X, Y)', doc: 'The size of X with the sign of Y, as a FLOAT.', group: 'Numbers' },
CLAMP: { kind: 'function', sig: 'CLAMP(X, Lo, Hi)', doc: 'X held inside [Lo, Hi]: Lo when X is below it, Hi when X is above it, X otherwise; Lo must not exceed Hi.', group: 'Numbers' },
FMA: { kind: 'function', sig: 'FMA(A, B, C)', doc: 'A * B + C with a single rounding at the end, as the processor\'s fused multiply-add computes it.', group: 'Precision' },
// ---- floating point itself
IS_NAN: { kind: 'function', sig: 'IS_NAN(X)', doc: 'TRUE when X is not a number. Only a program that says USE IEEE can hold one.', group: 'Precision' },
IS_INF: { kind: 'function', sig: 'IS_INF(X)', doc: 'TRUE when X is infinity or minus infinity. Only a program that says USE IEEE can hold one.', group: 'Precision' },
IS_FINITE: { kind: 'function', sig: 'IS_FINITE(X)', doc: 'TRUE when X is an ordinary number, neither infinite nor not a number.', group: 'Precision' },
INF: { kind: 'function', sig: 'INF()', doc: 'Positive infinity. A program has to say USE IEEE to hold it; otherwise the run stops.', group: 'Precision' },
NAN: { kind: 'function', sig: 'NAN()', doc: 'Not a number. A program has to say USE IEEE to hold it; otherwise the run stops.', group: 'Precision' },
ULP: { kind: 'function', sig: 'ULP(X)', doc: 'The gap between X and the next FLOAT away from zero: one unit in the last place, the size of X\'s rounding step. For a FLOAT32 X, the gap between singles.', group: 'Precision' },
NEXT_AFTER: { kind: 'function', sig: 'NEXT_AFTER(X, Y)', doc: 'The FLOAT next to X in the direction of Y (a FLOAT32 next to X, for a FLOAT32 X).', group: 'Precision' },
IS_CLOSE: { kind: 'function', sig: 'IS_CLOSE(A, B, RelTol, AbsTol)', doc: 'TRUE when ABS(A - B) <= MAX(RelTol * MAX(ABS(A), ABS(B)), AbsTol): equality for measured values, with the tolerance said out loud.', group: 'Precision' },
EXACT: { kind: 'function', sig: 'EXACT(X)', doc: 'The exact decimal value a FLOAT holds, as a STRING: EXACT(0.1) is "0.1000000000000000055511151231257827021181583404541015625".', group: 'Precision' },
// ---- the limits of a type
MAX_VALUE: { kind: 'function', sig: 'MAX_VALUE(T)', doc: 'The largest value of the numeric type T: MAX_VALUE(INT8) is 127, MAX_VALUE(FLOAT64) about 1.8 × 10^308.', group: 'Precision' },
MIN_VALUE: { kind: 'function', sig: 'MIN_VALUE(T)', doc: 'The lowest value of the numeric type T: MIN_VALUE(INT8) is -128, MIN_VALUE(FLOAT64) is -MAX_VALUE(FLOAT64).', group: 'Precision' },
EPSILON: { kind: 'function', sig: 'EPSILON(T)', doc: 'For FLOAT32 or FLOAT64, the gap between 1.0 and the next value above it: 2^-23 or 2^-52.', group: 'Precision' },
// ---- whole numbers bit by bit, and arithmetic that wraps
GCD: { kind: 'function', sig: 'GCD(A, B)', doc: 'The greatest common divisor of two whole numbers; GCD(0, 0) is 0.', group: 'Whole numbers' },
LCM: { kind: 'function', sig: 'LCM(A, B)', doc: 'The least common multiple of two whole numbers; 0 when either is 0.', group: 'Whole numbers' },
BIT_AND: { kind: 'function', sig: 'BIT_AND(A, B)', doc: 'The bits set in both A and B (two\'s complement for negative values).', group: 'Whole numbers' },
BIT_OR: { kind: 'function', sig: 'BIT_OR(A, B)', doc: 'The bits set in A or B or both.', group: 'Whole numbers' },
BIT_XOR: { kind: 'function', sig: 'BIT_XOR(A, B)', doc: 'The bits set in exactly one of A and B.', group: 'Whole numbers' },
BIT_NOT: { kind: 'function', sig: 'BIT_NOT(A)', doc: 'Every bit of A flipped, in A\'s type: BIT_NOT of a UINT8 0 is 255, of an INT8 or INTEGER 0 is -1.', group: 'Whole numbers' },
SHIFT_LEFT: { kind: 'function', sig: 'SHIFT_LEFT(A, N)', doc: 'A times 2^N: the bits moved N places up. A result outside A\'s type stops the run, as any overflow does.', group: 'Whole numbers' },
SHIFT_RIGHT: { kind: 'function', sig: 'SHIFT_RIGHT(A, N)', doc: 'A divided by 2^N, rounded down: the bits moved N places down (the sign kept, for a negative A).', group: 'Whole numbers' },
WRAP: { kind: 'function', sig: 'WRAP(A, T)', doc: 'The whole number A brought into the sized type T (up to 32 bits) by wrapping around, as hardware does: WRAP(300, UINT8) is 44. Arithmetic that is meant to wrap says so with WRAP.', group: 'Whole numbers' },
KEYS: { kind: 'function', sig: 'KEYS(H)', doc: 'The keys of a HASHMAP as a LIST, smallest first.', group: 'Collections' }, KEYS: { kind: 'function', sig: 'KEYS(H)', doc: 'The keys of a HASHMAP as a LIST, smallest first.', group: 'Collections' },
SORTED: { kind: 'function', sig: 'SORTED(X)', doc: 'The values of a LIST or SET as a new LIST, smallest first.', group: 'Collections' }, SORTED: { kind: 'function', sig: 'SORTED(X)', doc: 'The values of a LIST or SET as a new LIST, smallest first.', group: 'Collections' },
APPEND: { kind: 'procedure', sig: 'APPEND(L, V)', doc: 'Adds V to the end of the LIST L.', group: 'Collections' }, APPEND: { kind: 'procedure', sig: 'APPEND(L, V)', doc: 'Adds V to the end of the LIST L.', group: 'Collections' },
@@ -374,6 +492,32 @@ export const BUILTINS = {
SORT: { kind: 'procedure', sig: 'SORT(L)', doc: 'Puts the values of the LIST L in order, smallest first. Equal values keep their order.', group: 'Collections' }, SORT: { kind: 'procedure', sig: 'SORT(L)', doc: 'Puts the values of the LIST L in order, smallest first. Equal values keep their order.', group: 'Collections' },
}; };
/**
* A sized type's name followed by brackets converts a number to that type, as Mojo writes Int32(x):
* INT32(X) and the other whole-number types stop the run when X does not fit (and drop a FLOAT's fraction toward
* zero first); FLOAT32(X) rounds to the nearest single; FLOAT64(X) gives a FLOAT.
*/
export const CONVERSIONS = Object.fromEntries(Object.keys(SIZED).map((n) => [n, {
sig: `${n}(X)`,
doc: SIZED[n].k === 'INTEGER' ? `X as ${n}, from ${intRangeText(SIZED[n])}; a FLOAT loses its fraction toward zero first, and a value outside the range stops the run.` : n === 'FLOAT32' ? 'X rounded to the nearest FLOAT32 (single precision, about 7 significant digits).' : 'X as a FLOAT64 (the same type as FLOAT).',
group: 'Converting',
}]));
/** Built-ins that take a type's name as an argument, and at which position. */
export const TYPE_ARGS = { MAX_VALUE: 0, MIN_VALUE: 0, EPSILON: 0, WRAP: 1 };
/**
* Built-ins added for numerical work after programs were already being written. A program may still name its
* own variable E or define its own FUNCTION GCD: that definition wins inside the program, and the built-in of
* the same name is simply not reachable there. The older built-ins stay reserved.
*/
export const SOFT_BUILTINS = new Set([
'ASIN', 'ACOS', 'ATAN', 'SINH', 'COSH', 'TANH', 'HYPOT', 'POW', 'CBRT', 'EXPM1', 'LOG1P', 'E', 'TRUNC', 'SIGN',
'COPYSIGN', 'CLAMP', 'FMA', 'IS_NAN', 'IS_INF', 'IS_FINITE', 'INF', 'NAN', 'ULP', 'NEXT_AFTER', 'IS_CLOSE', 'EXACT',
'MAX_VALUE', 'MIN_VALUE', 'EPSILON', 'GCD', 'LCM', 'BIT_AND', 'BIT_OR', 'BIT_XOR', 'BIT_NOT', 'SHIFT_LEFT',
'SHIFT_RIGHT', 'WRAP',
]);
/** Built-ins that change their first argument in place. */ /** Built-ins that change their first argument in place. */
export const MUTATORS = new Set(['APPEND', 'INSERT', 'REMOVE', 'ADD', 'DELETE', 'SORT']); export const MUTATORS = new Set(['APPEND', 'INSERT', 'REMOVE', 'ADD', 'DELETE', 'SORT']);
+3 -2
View File
@@ -466,8 +466,9 @@ export function show(x: Term, names: Map<string, string> = new Map(), prec = 0):
} }
function showApp(x: App, names: Map<string, string>, p: (inner: string, level: number) => string): string { function showApp(x: App, names: Map<string, string>, p: (inner: string, level: number) => string): string {
if (/^FLOAT[-+*/^]$/.test(x.f)) { if (x.f === 'TOFLOAT32') return `FLOAT32(${show(x.args[0], names)})`;
const op = x.f.slice(5); if (/^(FLOAT|F32)[-+*/^]$/.test(x.f)) {
const op = x.f.slice(-1);
const lv = ({ '+': 6, '-': 6, '*': 7, '/': 7, '^': 9 } as Record<string, number>)[op] ?? 7; const lv = ({ '+': 6, '-': 6, '*': 7, '/': 7, '^': 9 } as Record<string, number>)[op] ?? 7;
return p(`${show(x.args[0], names, lv)} ${op} ${show(x.args[1], names, lv + (op === '^' ? 0 : 1))}`, lv); return p(`${show(x.args[0], names, lv)} ${op} ${show(x.args[1], names, lv + (op === '^' ? 0 : 1))}`, lv);
} }
+156
View File
@@ -0,0 +1,156 @@
/**
* pseudo/numeric.js
*
* The exact arithmetic behind the numerical built-ins: what a FLOAT holds digit for digit, the next value
* after it, a fused multiply-add rounded once, and whole numbers bit by bit. Every function here is exact
* (or correctly rounded) for the values a run can hold, so a lesson about rounding is never itself rounded.
*
* Nothing here depends on the DOM.
*/
const view = new DataView(new ArrayBuffer(8));
/** A finite double as M * 2^E with M a BigInt (signed). 0 is { m: 0n, e: 0 }. */
export function decompose(x) {
if (x === 0) return { m: 0n, e: 0 };
view.setFloat64(0, x);
const hi = view.getUint32(0);
const lo = view.getUint32(4);
const exp = (hi >>> 20) & 0x7ff;
let mant = (BigInt(hi & 0xfffff) << 32n) | BigInt(lo);
let e;
if (exp === 0) e = -1074;
else { mant |= 1n << 52n; e = exp - 1075; }
return { m: x < 0 ? -mant : mant, e };
}
const bitLength = (a) => (a === 0n ? 0 : a.toString(2).length);
/** x times 2^e, for an x and e whose product is representable: never rounds twice. */
function scale(x, e) {
while (e > 1000) { x *= 2 ** 1000; e -= 1000; }
while (e < -1000) { x *= 2 ** -1000; e += 1000; }
return x * 2 ** e;
}
/**
* The value M * 2^E rounded to the nearest binary floating-point number with `p` bits of precision and smallest
* exponent `emin` (ties to even): p = 53, emin = -1074 for FLOAT64; p = 24, emin = -149 for FLOAT32.
*/
export function roundDyadic(m, e, p = 53, emin = -1074) {
if (m === 0n) return 0;
const neg = m < 0n;
let a = neg ? -m : m;
const lead = bitLength(a) - 1 + e;
const q = Math.max(lead - (p - 1), emin);
const shift = q - e;
if (shift > 0) {
const s = BigInt(shift);
let quot = a >> s;
const rem = a - (quot << s);
const half = 1n << (s - 1n);
if (rem > half || (rem === half && (quot & 1n) === 1n)) quot += 1n;
a = quot;
e = q;
}
const r = scale(Number(a), e);
return neg ? -r : r;
}
/** A * B + C with one rounding at the end, in FLOAT64 or (f32) FLOAT32 precision. */
export function fma(a, b, c, f32 = false) {
if (![a, b, c].every(Number.isFinite)) { const r = a * b + c; return f32 ? Math.fround(r) : r; }
const x = decompose(a); const y = decompose(b); const z = decompose(c);
const pm = x.m * y.m; const pe = x.e + y.e;
if (pm === 0n && z.m === 0n) return f32 ? Math.fround(a * b + c) : a * b + c; // keeps the sign of zero
let m; let e;
if (pm === 0n) { m = z.m; e = z.e; } else if (z.m === 0n) { m = pm; e = pe; } else {
e = Math.min(pe, z.e);
m = (pm << BigInt(pe - e)) + (z.m << BigInt(z.e - e));
}
if (m === 0n) return 0;
const r = f32 ? roundDyadic(m, e, 24, -149) : roundDyadic(m, e, 53, -1074);
return f32 ? Math.fround(r) : r;
}
/** The exact decimal value of a finite FLOAT: 0.1 is 0.1000000000000000055511151231257827021181583404541015625. */
export function exactDecimal(x) {
if (Number.isNaN(x)) return 'not a number';
if (!Number.isFinite(x)) return x > 0 ? 'infinity' : '-infinity';
if (x === 0) return Object.is(x, -0) ? '-0.0' : '0.0';
const { m, e } = decompose(x);
const neg = m < 0n;
const a = neg ? -m : m;
let s;
if (e >= 0) s = `${(a << BigInt(e)).toString()}.0`;
else {
const digits = (a * 5n ** BigInt(-e)).toString().padStart(-e + 1, '0');
const cut = digits.length + e;
s = `${digits.slice(0, cut)}.${digits.slice(cut)}`.replace(/0+$/, '');
if (s.endsWith('.')) s += '0';
}
return neg ? `-${s}` : s;
}
const f32buf = new Float32Array(1);
const u32buf = new Uint32Array(f32buf.buffer);
/** The next representable value after x toward y, in FLOAT64 or (f32) FLOAT32. */
export function nextAfter(x, y, f32 = false) {
if (Number.isNaN(x) || Number.isNaN(y)) return NaN;
if (x === y) return y;
if (f32) {
if (x === 0) { u32buf[0] = 1; return y > 0 ? f32buf[0] : -f32buf[0]; }
f32buf[0] = x;
if ((y > x) === (x > 0)) u32buf[0] += 1; else u32buf[0] -= 1;
return f32buf[0];
}
if (x === 0) return y > 0 ? Number.MIN_VALUE : -Number.MIN_VALUE;
view.setFloat64(0, x);
let bits = view.getBigUint64(0);
if ((y > x) === (x > 0)) bits += 1n; else bits -= 1n;
view.setBigUint64(0, bits);
return view.getFloat64(0);
}
/** One unit in the last place of x: the gap from |x| to the next value away from zero (or below it, at the top). */
export function ulp(x, f32 = false) {
if (!Number.isFinite(x)) return NaN;
const a = Math.abs(x);
const up = nextAfter(a, Infinity, f32);
if (Number.isFinite(up)) return up - a;
return a - nextAfter(a, 0, f32);
}
/** The sign of y on the size of x, as IEEE 754 copySign (the sign of zero counts). */
export function copySign(x, y) {
const neg = y < 0 || Object.is(y, -0);
return neg ? -Math.abs(x) : Math.abs(x);
}
export function gcd(a, b) {
a = Math.abs(a); b = Math.abs(b);
while (b) [a, b] = [b, a % b];
return a;
}
/** Bitwise operations on whole numbers held exactly, two's complement for negative values. */
export function bitOp(op, a, b) {
const x = BigInt(a); const y = BigInt(b);
switch (op) {
case 'AND': return Number(x & y);
case 'OR': return Number(x | y);
case 'XOR': return Number(x ^ y);
case 'SHL': return Number(x << y);
case 'SHR': return Number(x >> y);
default: throw new Error(op);
}
}
/** A whole number brought into an n-bit type by wrapping around (n up to 32). */
export function wrap(a, bits, signed) {
const m = 2 ** bits;
let r = ((a % m) + m) % m;
if (signed && r >= m / 2) r -= m;
return r;
}
+50 -6
View File
@@ -13,7 +13,10 @@
* closing keyword. * closing keyword.
*/ */
import { KEYWORDS, FOREIGN_WORDS, CLOSERS, BUILTINS } from './lang.js'; import { KEYWORDS, FOREIGN_WORDS, CLOSERS, BUILTINS, SIZED_NAMES } from './lang.js';
/** Keywords that name a type, so they can stand as an argument: MAX_VALUE(INT8), WRAP(X, UINT8). */
const TYPE_WORDS = new Set(['INTEGER', 'FLOAT', ...SIZED_NAMES]);
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* Diagnostics */ /* Diagnostics */
@@ -127,10 +130,20 @@ export function lex(source) {
if (m) { push('blank', m[1].trim(), i, i + m[0].length); i += m[0].length; continue; } if (m) { push('blank', m[1].trim(), i, i + m[0].length); i += m[0].length; continue; }
} }
// numbers // numbers: 255, 2.5, 6.022e23, 1_000_000, 0xFF, 0b1010
if (/[0-9]/.test(c)) { if (/[0-9]/.test(c)) {
const m = /^[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?/.exec(text.slice(i)); const based = /^0([xX][0-9A-Fa-f_]+|[bB][01_]+)/.exec(text.slice(i));
const s = m[0]; if (based) {
const s = based[0];
if (/_$|__|^0[xXbB]_/.test(s)) diags.push(diag('error', 'bad-number', line, `An underscore goes between two digits, as in 0xFF_FF; ${s} has one elsewhere.`, { col: i + 1 }));
if (/[A-Za-z0-9_]/.test(text[i + s.length] || '')) diags.push(diag('error', 'bad-number', line, `${/^[0-9A-Za-z_]+/.exec(text.slice(i))[0]} is not a number: ${/[xX]/.test(s[1]) ? 'hexadecimal digits are 0 to 9 and A to F' : 'binary digits are 0 and 1'}.`, { col: i + 1 }));
push('int', Number(s.replace(/_/g, '')), i, i + s.length);
i += s.length;
continue;
}
const m = /^[0-9][0-9_]*(\.[0-9][0-9_]*)?([eE][+-]?[0-9]+)?/.exec(text.slice(i));
let s = m[0];
if (/_$|__|_\.|\._/.test(s)) diags.push(diag('error', 'bad-number', line, `An underscore goes between two digits, as in 1_000_000; ${s} has one elsewhere.`, { col: i + 1 }));
const isFloat = Boolean(m[1] || m[2]); const isFloat = Boolean(m[1] || m[2]);
if (text[i + s.length] === '.' && /[0-9]/.test(text[i + s.length + 1] || '') === false && !m[1]) { if (text[i + s.length] === '.' && /[0-9]/.test(text[i + s.length + 1] || '') === false && !m[1]) {
diags.push(diag('error', 'bad-number', line, `Write a FLOAT with a digit on each side of the point, as in ${s}.0.`, { col: i + 1 })); diags.push(diag('error', 'bad-number', line, `Write a FLOAT with a digit on each side of the point, as in ${s}.0.`, { col: i + 1 }));
@@ -138,7 +151,7 @@ export function lex(source) {
if (/[A-Za-z_]/.test(text[i + s.length] || '')) { if (/[A-Za-z_]/.test(text[i + s.length] || '')) {
diags.push(diag('error', 'bad-name', line, `A name must start with a letter, and "${/^[0-9A-Za-z_]+/.exec(text.slice(i))[0]}" starts with a digit.`, { col: i + 1, guide: 'names' })); diags.push(diag('error', 'bad-name', line, `A name must start with a letter, and "${/^[0-9A-Za-z_]+/.exec(text.slice(i))[0]}" starts with a digit.`, { col: i + 1, guide: 'names' }));
} }
push(isFloat ? 'float' : 'int', Number(s), i, i + s.length); push(isFloat ? 'float' : 'int', Number(s.replace(/_/g, '')), i, i + s.length);
i += s.length; i += s.length;
continue; continue;
} }
@@ -446,6 +459,7 @@ class Parser {
return s; return s;
} }
case 'CALL': return this.callStmt(); case 'CALL': return this.callStmt();
case 'USE': return this.useStmt();
case 'REQUIRE': case 'ENSURE': case 'INVARIANT': case 'DECREASES': case 'ASSERT': { case 'REQUIRE': case 'ENSURE': case 'INVARIANT': case 'DECREASES': case 'ASSERT': {
this.next(); this.next();
const kind = t.v[0] + t.v.slice(1).toLowerCase(); const kind = t.v[0] + t.v.slice(1).toLowerCase();
@@ -552,6 +566,27 @@ class Parser {
return s; return s;
} }
/** USE stats, USE stats.Mean, or USE IEEE: routines from the library, or IEEE 754 arithmetic. */
useStmt() {
const t = this.next();
if (this.tok.t !== 'name') {
this.error('expected', this.tok, 'USE names a module of the library, as in USE stats, or one routine of it, as in USE stats.Mean. USE IEEE asks for IEEE 754 arithmetic.', { guide: 'libraries' });
this.skipLine(); this.endLine('USE'); return null;
}
const m = this.next();
let routine = null;
let routineCol = null;
if (this.isOp('.')) {
this.next();
if (this.tok.t !== 'name') { this.error('expected', this.tok, `After USE ${m.v}. comes the name of one routine, as in USE ${m.v}.Mean.`, { guide: 'libraries' }); this.skipLine(); this.endLine('USE'); return null; }
const r = this.next();
routine = r.v; routineCol = r.col;
}
const s = this.node('Use', t, { module: m.v, moduleCol: m.col, routine, routineCol, ieee: m.v.toUpperCase() === 'IEEE' && !routine });
this.endLine('USE');
return s;
}
declare() { declare() {
const t = this.next(); const t = this.next();
const names = []; const names = [];
@@ -583,6 +618,7 @@ class Parser {
if (t.t !== 'kw') { this.error('expected', t, `Expected a type here${this.describeFound()}, such as INTEGER, STRING, or LIST OF INTEGER.`, { guide: 'the-types' }); return { tk: 'error', line: t.line, col: t.col }; } if (t.t !== 'kw') { this.error('expected', t, `Expected a type here${this.describeFound()}, such as INTEGER, STRING, or LIST OF INTEGER.`, { guide: 'the-types' }); return { tk: 'error', line: t.line, col: t.col }; }
switch (t.v) { switch (t.v) {
case 'INTEGER': case 'FLOAT': case 'CHAR': case 'STRING': case 'BOOL': case 'INTEGER': case 'FLOAT': case 'CHAR': case 'STRING': case 'BOOL':
case 'INT8': case 'INT16': case 'INT32': case 'INT64': case 'UINT8': case 'UINT16': case 'UINT32': case 'UINT64': case 'FLOAT32': case 'FLOAT64':
this.next(); return { tk: 'scalar', name: t.v, line: t.line, col: t.col }; this.next(); return { tk: 'scalar', name: t.v, line: t.line, col: t.col };
case 'LIST': case 'SET': { case 'LIST': case 'SET': {
this.next(); this.next();
@@ -613,7 +649,7 @@ class Parser {
return { tk: 'map', key, val: this.typeExpr(), line: t.line, col: t.col }; return { tk: 'map', key, val: this.typeExpr(), line: t.line, col: t.col };
} }
default: default:
this.error('expected', t, `${t.v} is not a type. The types are INTEGER, FLOAT, CHAR, STRING, BOOL, ARRAY, LIST, HASHMAP, SET, and records you define with TYPE.`, { guide: 'the-types' }); this.error('expected', t, `${t.v} is not a type. The types are INTEGER, FLOAT, CHAR, STRING, BOOL, the sized numbers (INT8 to INT64, UINT8 to UINT64, FLOAT32, FLOAT64), ARRAY, LIST, HASHMAP, SET, and records you define with TYPE.`, { guide: 'the-types' });
this.next(); this.next();
return { tk: 'error', line: t.line, col: t.col }; return { tk: 'error', line: t.line, col: t.col };
} }
@@ -1177,6 +1213,13 @@ class Parser {
} }
case 'kw': case 'kw':
if (t.v === 'TRUE' || t.v === 'FALSE') { this.next(); return { kind: 'Bool', value: t.v === 'TRUE', line: t.line, col: t.col }; } if (t.v === 'TRUE' || t.v === 'FALSE') { this.next(); return { kind: 'Bool', value: t.v === 'TRUE', line: t.line, col: t.col }; }
if (SIZED_NAMES.has(t.v) && this.isOp('(', this.peek())) {
// INT32(X), FLOAT32(X): a conversion, written the way Mojo writes Int32(x)
this.next();
const open = this.next();
return { kind: 'CallExpr', name: t.v, args: this.args(')'), convert: true, line: t.line, col: t.col, openCol: open.col };
}
if (TYPE_WORDS.has(t.v)) { this.next(); return { kind: 'TypeRef', name: t.v, line: t.line, col: t.col }; }
if (t.v === 'FORALL' || t.v === 'EXISTS') return this.quantifier(t.v); if (t.v === 'FORALL' || t.v === 'EXISTS') return this.quantifier(t.v);
if (t.v === 'NOT') return this.notExpr(); if (t.v === 'NOT') return this.notExpr();
this.error('expected', t, BUILTINS[t.v] this.error('expected', t, BUILTINS[t.v]
@@ -1264,6 +1307,7 @@ export function sourceOf(e) {
case 'Result': return 'RESULT'; case 'Result': return 'RESULT';
case 'Old': return `OLD(${sourceOf(e.arg)})`; case 'Old': return `OLD(${sourceOf(e.arg)})`;
case 'Blank': return `<${e.text}>`; case 'Blank': return `<${e.text}>`;
case 'TypeRef': return e.name;
case 'Unary': return e.op === 'NOT' ? `NOT ${wrap(e.arg)}` : `-${wrap(e.arg)}`; case 'Unary': return e.op === 'NOT' ? `NOT ${wrap(e.arg)}` : `-${wrap(e.arg)}`;
case 'Binary': return `${sideOf(e.left, e.op, 'l')} ${e.op} ${sideOf(e.right, e.op, 'r')}`; case 'Binary': return `${sideOf(e.left, e.op, 'l')} ${e.op} ${sideOf(e.right, e.op, 'r')}`;
case 'Chain': return e.operands.map((o, i) => (i ? `${e.ops[i - 1]} ${sourceOf(o)}` : sourceOf(o))).join(' '); case 'Chain': return e.operands.map((o, i) => (i ? `${e.ops[i - 1]} ${sourceOf(o)}` : sourceOf(o))).join(' ');
+15 -3
View File
@@ -24,7 +24,7 @@
import { parseExpression, sourceOf } from './parse.js'; import { parseExpression, sourceOf } from './parse.js';
import { run, evaluateConstant, makeRandom, valueFits } from './interp.js'; import { run, evaluateConstant, makeRandom, valueFits } from './interp.js';
import { valuesEqual, formatValue, outputText, typeName, T, cloneValue, PArray } from './lang.js'; import { valuesEqual, formatValue, outputText, typeName, T, cloneValue, PArray, intRange } from './lang.js';
const up = (s) => String(s).toUpperCase(); const up = (s) => String(s).toUpperCase();
@@ -175,8 +175,20 @@ function generatorFor(param, rnd, opts) {
const int = (lo, hi) => lo + Math.floor(rnd() * (hi - lo + 1)); const int = (lo, hi) => lo + Math.floor(rnd() * (hi - lo + 1));
const make = (t) => { const make = (t) => {
switch (t?.k) { switch (t?.k) {
case 'INTEGER': return () => (rnd() < 0.3 ? pick([0, 1, 2, -1]) : int(opts.intMin ?? -3, opts.intMax ?? 12)); case 'INTEGER': {
case 'FLOAT': return () => Math.round((rnd() * 12 - 2) * 100) / 100; if (t.bits) {
// a sized type: small values, and the edges of its range, where overflow lives
const [lo, hi] = intRange(t);
const small = () => Math.min(hi, Math.max(lo, int(opts.intMin ?? -3, opts.intMax ?? 12)));
const edges = [lo, lo + 1, hi - 1, hi, 0, 1].filter((v) => v >= lo && v <= hi);
return () => (rnd() < 0.3 ? pick(edges) : small());
}
return () => (rnd() < 0.3 ? pick([0, 1, 2, -1]) : int(opts.intMin ?? -3, opts.intMax ?? 12));
}
case 'FLOAT': {
const f = () => Math.round((rnd() * 12 - 2) * 100) / 100;
return t.bits === 32 ? () => Math.fround(f()) : f;
}
case 'BOOL': return () => rnd() < 0.5; case 'BOOL': return () => rnd() < 0.5;
case 'CHAR': return () => (rnd() < 0.08 ? 'N' : pick(alphabet)); case 'CHAR': return () => (rnd() < 0.08 ? 'N' : pick(alphabet));
case 'STRING': return () => { case 'STRING': return () => {
+116 -9
View File
@@ -34,6 +34,7 @@ import type { Term, Sort, TypeLike, CmpOp } from './logic.js';
import { prove } from './solver.js'; import { prove } from './solver.js';
import { sourceOf } from './parse.js'; import { sourceOf } from './parse.js';
import { walkStatements, rootName, statementExprs, walkExpr } from './analyze.js'; import { walkStatements, rootName, statementExprs, walkExpr } from './analyze.js';
import { isSizedInt, isFloat32, intRange } from './lang.js';
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* Types */ /* Types */
@@ -246,10 +247,41 @@ class Exec {
fresh(name: string, sort: Sort): Term { return sym(name, sort); } fresh(name: string, sort: Sort): Term { return sym(name, sort); }
/** The range of a sized whole-number type as a fact about a term: lowest <= v <= highest. */
rangeOf(type: TypeLike, v: Term): Term {
const [lo, hi] = intRange(type);
return and(cmp('<=', num(lo), v), cmp('<=', v, num(hi)));
}
/**
* A value going into a sized type (a store, a parameter, a RETURN, a conversion, an operator's result). A whole
* number gets an obligation that it fits: the run stops right there when it does not, so from then on the
* range is a fact. A number going into a FLOAT32 is rounded: a constant is rounded here, exactly; anything
* else that is not already a FLOAT32 becomes TOFLOAT32(v), about which nothing is assumed.
*/
fit(type: TypeLike | undefined, v: Term, from: TypeLike | undefined, node: PNode, st: State, g: Term[], what: string, ctx: Ctx = {}): Term {
if (!type) return v;
if (isSizedInt(type)) {
if (isSizedInt(from) && (from as any).bits === (type as any).bits && (from as any).signed === (type as any).signed) return v;
if (!ctx.spec) {
this.obligation('overflow', node, `${what} fits ${(type as any).name}`, st, g, this.rangeOf(type, v));
st.facts.push(this.rangeOf(type, v));
}
return v;
}
if (isFloat32(type)) {
if (isFloat32(from)) return v;
if (v.k === 'num') return num(Math.fround(v.v), 'float');
return app('TOFLOAT32', [v], 'float');
}
return v;
}
/* ---------- expressions ---------- */ /* ---------- expressions ---------- */
tr(e: PNode, st: State, g: Term[] = [], ctx: Ctx = {}): Term { tr(e: PNode, st: State, g: Term[] = [], ctx: Ctx = {}): Term {
switch (e.kind) { switch (e.kind) {
case 'Num': return num(e.value, e.float ? 'float' : 'int'); case 'Num': return isFloat32(e.type) ? num(Math.fround(e.value), 'float') : num(e.value, e.float ? 'float' : 'int');
case 'TypeRef': return this.fresh(e.name, 'any');
case 'Str': return lit(e.value, 'string'); case 'Str': return lit(e.value, 'string');
case 'Char': return lit(e.value, 'char'); case 'Char': return lit(e.value, 'char');
case 'Bool': return e.value ? TRUE : FALSE; case 'Bool': return e.value ? TRUE : FALSE;
@@ -257,7 +289,14 @@ class Exec {
case 'Name': return this.name(e, st, ctx); case 'Name': return this.name(e, st, ctx);
case 'Unary': { case 'Unary': {
const a = this.tr(e.arg, st, g, ctx); const a = this.tr(e.arg, st, g, ctx);
return e.op === 'NOT' ? not(a) : neg(a); if (e.op === 'NOT') return not(a);
if (isFloat32(e.type)) return neg(a); // negation is exact in floating point
const r = neg(a);
if (isSizedInt(e.type) && !ctx.spec) {
this.obligation('overflow', e, `${sourceOf(e)} fits ${e.type.name}`, st, g, this.rangeOf(e.type, r));
st.facts.push(this.rangeOf(e.type, r));
}
return r;
} }
case 'Binary': return this.binary(e, st, g, ctx); case 'Binary': return this.binary(e, st, g, ctx);
case 'Chain': { case 'Chain': {
@@ -321,10 +360,17 @@ class Exec {
// again; comparisons of FLOAT values stay exact. // again; comparisons of FLOAT values stay exact.
if (['+', '-', '*', '/', '^'].includes(op) && (op === '/' || sortOf(e.type) === 'float' || e.left.type?.k === 'FLOAT' || e.right.type?.k === 'FLOAT')) { if (['+', '-', '*', '/', '^'].includes(op) && (op === '/' || sortOf(e.type) === 'float' || e.left.type?.k === 'FLOAT' || e.right.type?.k === 'FLOAT')) {
if (!ctx.spec) this.floatArith = true; if (!ctx.spec) this.floatArith = true;
return app(`FLOAT${op}`, [l, r], 'float'); // FLOAT32 arithmetic is a different function: the same values rounded to single precision
return app(isFloat32(e.type) ? `F32${op}` : `FLOAT${op}`, [l, r], 'float');
} }
if (!ctx.spec && ['+', '-', '*', '^'].includes(op) && sortOf(e.type) === 'int') this.intArith = e.line ?? e.left?.line ?? true; if (!ctx.spec && ['+', '-', '*', '^'].includes(op) && sortOf(e.type) === 'int' && !isSizedInt(e.type)) this.intArith = e.line ?? e.left?.line ?? true;
return arith(op, l, r, sortOf(e.type)); const res = arith(op, l, r, sortOf(e.type));
// a sized whole number: the exact result, and an obligation that it fits (the run stops where it does not)
if (isSizedInt(e.type) && !ctx.spec && ['+', '-', '*', '^', 'DIV', 'MOD'].includes(op)) {
this.obligation('overflow', e, `${sourceOf(e)} fits ${e.type.name}`, st, g, this.rangeOf(e.type, res));
st.facts.push(this.rangeOf(e.type, res));
}
return res;
} }
membership(x: Term, rightNode: PNode, st: State, g: Term[], ctx: Ctx): Term { membership(x: Term, rightNode: PNode, st: State, g: Term[], ctx: Ctx): Term {
@@ -383,8 +429,44 @@ class Exec {
} }
const args: Term[] = e.args.map((a: PNode) => this.tr(a, st, g, ctx)); const args: Term[] = e.args.map((a: PNode) => this.tr(a, st, g, ctx));
const s = sortOf(e.type); const s = sortOf(e.type);
if (e.convert) {
const from = e.args[0]?.type;
const x = args[0] ?? this.fresh('?', 'any');
if (isSizedInt(e.type)) return this.fit(e.type, from?.k === 'FLOAT' ? app('TRUNC', [x], 'int') : x, from?.k === 'FLOAT' ? undefined : from, e, st, g, sourceOf(e), ctx);
if (isFloat32(e.type)) return this.fit(e.type, x, from, e, st, g, sourceOf(e), ctx);
return x;
}
if (e.builtin) { if (e.builtin) {
const [x, y, z] = args; const [x, y, z] = args;
// A built-in the run stops on for some arguments has an obligation that the arguments are allowed
// (kind 'argument'); without USE IEEE these are the FLOAT functions with a domain.
const domain = (goal: Term, label: string) => { if (!ctx.spec) this.obligation('argument', e, label, st, g, goal); };
const call = `${e.builtin}(${e.args.map(sourceOf).join(', ')})`;
const zero = num(0, x?.t === 'float' ? 'float' : 'int');
switch (e.builtin) {
case 'SQRT': domain(cmp('>=', x, zero), `${call} takes a number of 0 or more`); return app('SQRT', args, s);
case 'LOG': case 'LOG2': case 'LOG10': domain(cmp('>', x, zero), `${call} takes a number above 0`); return app(e.builtin, args, s);
case 'ASIN': case 'ACOS': domain(and(cmp('<=', num(-1), x), cmp('<=', x, num(1))), `${call} takes a number in [-1, 1]`); return app(e.builtin, args, s);
case 'LOG1P': domain(cmp('>', x, num(-1)), `${call} takes a number above -1`); return app(e.builtin, args, s);
case 'CLAMP': domain(cmp('<=', y, z), `${call} has its low end at or below its high end`); return ite(cmp('<', x, y), y, ite(cmp('>', x, z), z, x));
case 'SHIFT_LEFT': case 'SHIFT_RIGHT': {
domain(cmp('>=', y, num(0)), `${call} moves bits by 0 or more places`);
const r = app(e.builtin, args, s);
if (e.builtin === 'SHIFT_LEFT') this.fitResult(e, r, st, g, ctx);
return r;
}
case 'LCM': { const r = app('LCM', args, s); this.fitResult(e, r, st, g, ctx); return r; }
case 'MAX_VALUE': case 'MIN_VALUE': case 'EPSILON': {
const ty = e.args[0]?.kind === 'TypeRef' ? e.type : null;
if (!ty) return app(e.builtin, args, s);
if (e.builtin === 'EPSILON') return num(isFloat32(ty) ? 2 ** -23 : Number.EPSILON, 'float');
if (ty.k === 'INTEGER') { const [lo, hi] = intRange(ty); return num(e.builtin === 'MAX_VALUE' ? hi : lo, 'int'); }
const m = isFloat32(ty) ? 3.4028234663852886e38 : Number.MAX_VALUE;
return num(e.builtin === 'MAX_VALUE' ? m : -m, 'float');
}
case 'SUM_OF': case 'DOT': if (isSizedInt(e.type)) { const r = app(e.builtin, args, s); this.fitResult(e, r, st, g, ctx); return r; } break;
default: break;
}
switch (e.builtin) { switch (e.builtin) {
case 'LENGTH': return length(x); case 'LENGTH': return length(x);
case 'ROWS': return length(x); case 'ROWS': return length(x);
@@ -433,6 +515,7 @@ class Exec {
} }
const sub = e.sub; const sub = e.sub;
if (!sub) return this.fresh(e.name, s); if (!sub) return this.fresh(e.name, s);
sub.params.forEach((p: PNode, i: number) => { if (args[i] && e.args[i]) args[i] = this.fit(p.type, args[i], e.args[i].type, e.args[i], st, g, `the value for ${p.name}`, ctx); });
const result = app(`FN:${sub.name}`, args, s); const result = app(`FN:${sub.name}`, args, s);
const paramEnv: Env = new Map(sub.params.map((p: PNode, i: number): [string, Val] => [up(p.name), args[i]])); const paramEnv: Env = new Map(sub.params.map((p: PNode, i: number): [string, Val] => [up(p.name), args[i]]));
if (!ctx.spec) { if (!ctx.spec) {
@@ -448,6 +531,13 @@ class Exec {
return result; return result;
} }
/** A built-in's sized whole-number result: an obligation that it fits, then the range as a fact. */
fitResult(e: PNode, r: Term, st: State, g: Term[], ctx: Ctx): void {
if (!isSizedInt(e.type) || ctx.spec) return;
this.obligation('overflow', e, `${sourceOf(e)} fits ${e.type.name}`, st, g, this.rangeOf(e.type, r));
st.facts.push(this.rangeOf(e.type, r));
}
/** A recursive call has to make the DECREASES measure smaller, and keep it at 0 or above. */ /** A recursive call has to make the DECREASES measure smaller, and keep it at 0 or above. */
recursion(sub: Routine, at: PNode, args: Term[], st: State, g: Term[]): void { recursion(sub: Routine, at: PNode, args: Term[], st: State, g: Term[]): void {
const cur = this.unit.sub; const cur = this.unit.sub;
@@ -496,14 +586,16 @@ class Exec {
return [n]; return [n];
} }
case 'Assign': { case 'Assign': {
const v = this.tr(s.value, st, []); const v = this.fit(s.target.type, this.tr(s.value, st, []), s.value.type, s.value, st, [], sourceOf(s.value));
const n = st.clone(); const n = st.clone();
this.store(s.target, v, n, st); this.store(s.target, v, n, st);
return [n]; return [n];
} }
case 'Input': { case 'Input': {
const raw = this.fresh(rootName(s.target) || 'input', sortOf(s.target.type));
const v = this.fit(s.target.type, raw, undefined, s, st, [], `the value INPUT reads into ${sourceOf(s.target)}`);
const n = st.clone(); const n = st.clone();
this.store(s.target, this.fresh(rootName(s.target) || 'input', sortOf(s.target.type)), n, st); this.store(s.target, v, n, st);
return [n]; return [n];
} }
case 'Output': case 'ExprStmt': { case 'Output': case 'ExprStmt': {
@@ -519,7 +611,7 @@ class Exec {
case 'Break': ctx.breaks?.push(st); return []; case 'Break': ctx.breaks?.push(st); return [];
case 'Continue': ctx.continues?.push(st); return []; case 'Continue': ctx.continues?.push(st); return [];
case 'Return': { case 'Return': {
const v = s.value ? this.tr(s.value, st, []) : null; const v = s.value ? this.fit(this.unit.sub?.returns, this.tr(s.value, st, []), s.value.type, s.value, st, [], sourceOf(s.value)) : null;
this.ensure(s, st, v); this.ensure(s, st, v);
return []; return [];
} }
@@ -595,6 +687,7 @@ class Exec {
const sub = s.sub; const sub = s.sub;
if (!sub) return st; if (!sub) return st;
const args: Term[] = s.args.map((a: PNode) => this.tr(a, st, [])); const args: Term[] = s.args.map((a: PNode) => this.tr(a, st, []));
sub.params.forEach((p: PNode, i: number) => { if (args[i] && s.args[i] && p.mode !== 'BYREF') args[i] = this.fit(p.type, args[i], s.args[i].type, s.args[i], st, [], `the value for ${p.name}`); });
const paramEnv: Env = new Map(sub.params.map((p: PNode, i: number): [string, Val] => [up(p.name), args[i]])); const paramEnv: Env = new Map(sub.params.map((p: PNode, i: number): [string, Val] => [up(p.name), args[i]]));
for (const r of sub.requires) { for (const r of sub.requires) {
this.obligation('require', s, `this call to ${sub.name} meets its REQUIRE, ${sourceOf(r.expr)}`, st, [], this.tr(r.expr, st, [], { paramEnv, spec: true }), { target: r.line }); this.obligation('require', s, `this call to ${sub.name} meets its REQUIRE, ${sourceOf(r.expr)}`, st, [], this.tr(r.expr, st, [], { paramEnv, spec: true }), { target: r.line });
@@ -668,6 +761,9 @@ class Exec {
n.env.set(k, fresh); n.env.set(k, fresh);
// an ARRAY keeps its dimensions, however its contents change // an ARRAY keeps its dimensions, however its contents change
if (known && known.t === 'array') n.facts.push(cmp('==', length(fresh), length(known)), cmp('==', columns(fresh), columns(known))); if (known && known.t === 'array') n.facts.push(cmp('==', length(fresh), length(known)), cmp('==', columns(fresh), columns(known)));
// a sized whole number stays in its range: every store into it is checked, and the run stops otherwise
const ty = this.types.get(k);
if (isSizedInt(ty)) n.facts.push(this.rangeOf(ty!, fresh));
} }
n.trail.push(`the top of the loop on line ${loopLine}`); n.trail.push(`the top of the loop on line ${loopLine}`);
return n; return n;
@@ -948,6 +1044,8 @@ class Exec {
const v = this.fresh(p.name, sortOf(p.type)); const v = this.fresh(p.name, sortOf(p.type));
st.env.set(up(p.name), v); st.env.set(up(p.name), v);
this.entryEnv.set(up(p.name), v); this.entryEnv.set(up(p.name), v);
// a parameter of a sized type holds a value of that type: the call checked it on the way in
if (isSizedInt(p.type)) st.facts.push(this.rangeOf(p.type, v));
} }
for (const r of u.requires || []) st.facts.push(this.tr(r.expr, st, [], { spec: true })); for (const r of u.requires || []) st.facts.push(this.tr(r.expr, st, [], { spec: true }));
const ends = this.run(u.body, [st], { breaks: [], continues: [] }); const ends = this.run(u.body, [st], { breaks: [], continues: [] });
@@ -1052,7 +1150,7 @@ export function verifyUnit(program: Program, unit: Unit, options: VerifyOptions
return { name: unit.name, kind: unit.kind, line: unit.node?.line ?? 1, obligations, inferred: ex.inferred, notes: ex.notes, partial: ex.partial }; return { name: unit.name, kind: unit.kind, line: unit.node?.line ?? 1, obligations, inferred: ex.inferred, notes: ex.notes, partial: ex.partial };
} }
const KIND_ORDER = ['require', 'index', 'key', 'slice', 'divide', 'argument', 'invariant-init', 'invariant-keep', 'decreases-bound', 'decreases-step', 'terminates', 'assert', 'ensure']; const KIND_ORDER = ['require', 'index', 'key', 'slice', 'divide', 'overflow', 'argument', 'invariant-init', 'invariant-keep', 'decreases-bound', 'decreases-step', 'terminates', 'assert', 'ensure'];
export function verify(program: Program, options: VerifyOptions = {}): Report { export function verify(program: Program, options: VerifyOptions = {}): Report {
const units: UnitReport[] = []; const units: UnitReport[] = [];
@@ -1060,6 +1158,14 @@ export function verify(program: Program, options: VerifyOptions = {}): Report {
units.push(verifyUnit(program, { name: sub.name, kind: sub.kind, node: sub.node, body: sub.node.body, params: sub.params, symbols: sub.symbols || [], requires: sub.requires, ensures: sub.ensures, sub }, options)); units.push(verifyUnit(program, { name: sub.name, kind: sub.kind, node: sub.node, body: sub.node.body, params: sub.params, symbols: sub.symbols || [], requires: sub.requires, ensures: sub.ensures, sub }, options));
} }
if (program.main.body.length) units.push(verifyUnit(program, { name: 'main program', kind: 'main', node: null, body: program.main.body, params: [], symbols: program.main.symbols, requires: [], ensures: [] }, options)); if (program.main.body.length) units.push(verifyUnit(program, { name: 'main program', kind: 'main', node: null, body: program.main.body, params: [], symbols: program.main.symbols, requires: [], ensures: [] }, options));
// USE IEEE lets NaN and infinities into the run, and the prover's reasoning about numbers (X == X; X < Y or
// X >= Y) does not hold for them, so nothing in such a program is called proved.
if ((program as any).ieee) {
for (const u of units) {
u.notes.push({ line: 1, kind: 'ieee', text: 'This program says USE IEEE, so a FLOAT may be NaN or infinite, and the prover\'s reasoning about numbers does not cover those values. Nothing here is marked proved; the obligations are listed so you can see what each line relies on.' });
for (const o of u.obligations) if (o.status === 'proved') (o as any).status = 'unknown';
}
}
const all = units.flatMap((u) => u.obligations); const all = units.flatMap((u) => u.obligations);
return { units, counts: { proved: all.filter((o) => o.status === 'proved').length, unknown: all.filter((o) => o.status !== 'proved').length, total: all.length } }; return { units, counts: { proved: all.filter((o) => o.status === 'proved').length, unknown: all.filter((o) => o.status !== 'proved').length, total: all.length } };
} }
@@ -1069,6 +1175,7 @@ export const OBLIGATION_WORDS: Record<string, string> = {
key: 'Key present', key: 'Key present',
slice: 'Slice in range', slice: 'Slice in range',
divide: 'No division by zero', divide: 'No division by zero',
overflow: 'Fits its type',
argument: 'Argument allowed', argument: 'Argument allowed',
require: 'Precondition met', require: 'Precondition met',
ensure: 'Postcondition holds', ensure: 'Postcondition holds',
File diff suppressed because it is too large. Load diff
+40
View File
@@ -0,0 +1,40 @@
/**
* The library as core sees it: modules of verified routines that a program brings in with USE.
*
* The data comes from the generated module (scripts/library.mjs emit, from the library/ submodule), so the
* site needs no files at run time. The library's own tools read the routines from disk instead and install
* them here with setLibrary() before checking them.
*/
import { LIBRARY_MODULES } from './generated.js';
import type { LibraryModule, LibraryRoutine } from './types.js';
export type { LibraryModule, LibraryRoutine } from './types.js';
let modules: LibraryModule[] = LIBRARY_MODULES;
let byModule = index(modules);
function index(mods: LibraryModule[]): Map<string, LibraryModule> {
return new Map(mods.map((m) => [m.name.toUpperCase(), m]));
}
/** Replace the library (the library's own tools, and tests). Returns the one it replaced. */
export function setLibrary(mods: LibraryModule[]): LibraryModule[] {
const old = modules;
modules = mods;
byModule = index(mods);
generation++;
return old;
}
/** Bumped by setLibrary, so a cache of studied routines knows to start again. */
export let generation = 0;
export function libraryModules(): LibraryModule[] { return modules; }
export function libraryModule(name: string): LibraryModule | undefined { return byModule.get(String(name).toUpperCase()); }
/** A routine by its id (stats.Mean), or by module and name. Names ignore case, as in pseudo. */
export function libraryRoutine(module: string, name?: string): LibraryRoutine | undefined {
if (name === undefined) { const i = module.indexOf('.'); if (i < 0) return undefined; name = module.slice(i + 1); module = module.slice(0, i); }
return libraryModule(module)?.routines.find((r) => r.name.toUpperCase() === String(name).toUpperCase());
}
+70
View File
@@ -0,0 +1,70 @@
/**
* The shapes of the library (pplus-library, the submodule library/): modules of verified pseudo routines, each
* with its cases, its reference implementations in other languages, an optional proof, and the status of every
* check, as `pixi run library-check` last recorded it.
*/
/** One check of one routine: what it found, and when the stored answer was made. */
export interface LibraryCheck {
/**
* 'pass'; 'fail'; 'open' (the prover could not finish every obligation: nothing is wrong, nothing is proved);
* 'unverified' (no stored answer for this exact text); 'skipped' (the tool is not available, or declines).
*/
status: 'pass' | 'fail' | 'open' | 'unverified' | 'skipped';
/** One line: what was checked and what it found. */
detail: string;
}
export interface LibraryReference {
/** 'python', 'mojo', 'c', ... */
lang: string;
/** 'plain', 'unrolled', 'fsum', ...: one implementation of the routine, at one level of care or optimization. */
level: string;
/** The file, relative to the routine's folder. */
file: string;
/** What this level does differently, in a sentence, and why it needs a tolerance if it has one. */
note: string;
/** The tolerance this level is held to against the pseudo, relative and absolute; exact when absent. */
rel?: number;
abs?: number;
source: string;
/** Per way of running it: 'interpreter' (core's Mojo interpreter), 'godbolt', 'cpython', 'cc -O0', 'cc -O2'. */
checks: Record<string, LibraryCheck>;
}
export interface LibraryCase {
/** A call in pseudo, such as Mean([1.0, 2.0, 3.0]). */
call: string;
/** What the pseudo returns, written as pseudo writes values; filled in by library-check from a run. */
result?: string;
/** The tolerance a reference implementation is held to, relative and absolute; 0 for exact. */
rel?: number;
abs?: number;
}
export interface LibraryRoutine {
/** module.Name, as USE writes it: stats.Mean. */
id: string;
module: string;
name: string;
/** The routine's folder in pplus-library, such as modules/stats/mean. */
dir: string;
summary: string;
tags: string[];
/** Where the method comes from: a paper, a textbook section, a manual page. */
sources: Array<{ title: string; url?: string }>;
/** The routine file, routine.pseudo: one FUNCTION or PROCEDURE with its contracts, and USE lines for what it calls. */
source: string;
cases: LibraryCase[];
references: LibraryReference[];
proof?: { lang: string; file: string; note: string; source: string; check: LibraryCheck };
/** pseudo (cases run), prover, fuzz, mojo (the generated Mojo), each a LibraryCheck. */
checks: Record<string, LibraryCheck>;
}
export interface LibraryModule {
name: string;
title: string;
summary: string;
routines: LibraryRoutine[];
}
+68 -3
View File
@@ -22,6 +22,8 @@ import { parse, parseExpression } from '../engines/pseudo/parse.js';
import { check } from '../engines/pseudo/check.js'; import { check } from '../engines/pseudo/check.js';
import { analyze } from '../engines/pseudo/analyze.js'; import { analyze } from '../engines/pseudo/analyze.js';
import { coach } from '../engines/pseudo/coach.js'; import { coach } from '../engines/pseudo/coach.js';
import { libraryModule, libraryModules, generation as libraryGeneration } from '../library/registry.js';
import type { LibraryRoutine } from '../library/registry.js';
const RANK: Record<string, number> = { error: 0, warning: 1, note: 2 }; const RANK: Record<string, number> = { error: 0, warning: 1, note: 2 };
@@ -118,7 +120,66 @@ const diag = (severity: string, code: string, line: number, col: number, message
* `source` is the source as written; `parsed` is the engine's parse of the blanked source, with the assignments * `source` is the source as written; `parsed` is the engine's parse of the blanked source, with the assignments
* put in. * put in.
*/ */
export function studyPseudo(source: string, options: { coach?: boolean; strict?: boolean } = {}): any { /** Studied library routines, by generation, id and source, so a routine is checked once however many programs use it. */
const routineCache = new Map<string, any>();
/** A library routine, studied as a program of its own (its own USE lines resolved the same way). */
function studyRoutine(r: LibraryRoutine, stack: string[]): any {
const key = `${libraryGeneration}\u0000${r.id}\u0000${r.source}`;
let st = routineCache.get(key);
if (!st) {
st = studyPseudo(r.source, { coach: false, libraryStack: [...stack, r.id] });
routineCache.set(key, st);
}
return st;
}
/**
* The routines a program's USE lines bring in: UPPER name -> the checked routine (its own program's FUNCTION or
* PROCEDURE, marked with `lib`), where each came from, and what could not be found.
*/
export function resolveUses(body: any[], stack: string[] = []): { libs: Map<string, any>; origin: Map<string, { module: string; line: number }>; diags: any[] } {
const libs = new Map<string, any>();
const origin = new Map<string, { module: string; line: number }>();
const diags: any[] = [];
for (const u of body) {
if (u?.kind !== 'Use' || u.ieee) continue;
const m = libraryModule(u.module);
if (!m) {
const names = libraryModules().map((x) => x.name);
diags.push(diag('error', 'unknown-library', u.line, u.moduleCol ?? u.col, `No module of the library is called ${u.module}.${names.length ? ` The modules are ${names.join(', ')}.` : ' The library has no modules in this build.'}`, { guide: 'libraries' }));
continue;
}
let routines = m.routines;
if (u.routine) {
const r = m.routines.find((x) => x.name.toUpperCase() === String(u.routine).toUpperCase());
if (!r) { diags.push(diag('error', 'unknown-routine', u.line, u.routineCol ?? u.col, `The module ${m.name} has no routine called ${u.routine}. Its routines are ${m.routines.map((x) => x.name).join(', ')}.`, { guide: 'libraries' })); continue; }
routines = [r];
}
for (const r of routines) {
if (stack.includes(r.id)) { diags.push(diag('error', 'library-cycle', u.line, u.col, `${r.id} is already being brought in by ${stack[stack.length - 1]}, so the routines would use each other in a circle.`, { guide: 'libraries' })); continue; }
const st = studyRoutine(r, stack);
const sub = st.ok ? st.program.subs.get(r.name.toUpperCase()) : null;
if (!sub) {
const why = st.errors?.[0] ? `line ${st.errors[0].line}: ${st.errors[0].message}` : `it defines no routine called ${r.name}`;
diags.push(diag('error', 'library-broken', u.line, u.col, `The library routine ${r.id} does not check (${why}). That is a fault in the library, not in this program.`, { guide: 'libraries' }));
continue;
}
sub.lib = { module: m.name, id: r.id, program: st.program, source: r.source };
const k = r.name.toUpperCase();
const prior = origin.get(k);
if (prior && prior.module.toUpperCase() !== m.name.toUpperCase()) {
diags.push(diag('error', 'library-clash', u.line, u.col, `${r.name} comes from both ${prior.module} (line ${prior.line}) and ${m.name}. Bring in only the one you mean: USE ${m.name}.<Name>.`, { guide: 'libraries' }));
continue;
}
libs.set(k, sub);
origin.set(k, { module: m.name, line: u.line });
}
}
return { libs, origin, diags };
}
export function studyPseudo(source: string, options: { coach?: boolean; strict?: boolean; libraryStack?: string[] } = {}): any {
const original = String(source ?? ''); const original = String(source ?? '');
const { source: blanked, inits } = splitDeclareInits(original); const { source: blanked, inits } = splitDeclareInits(original);
const parsed: any = parse(blanked); const parsed: any = parse(blanked);
@@ -149,8 +210,12 @@ export function studyPseudo(source: string, options: { coach?: boolean; strict?:
} }
if (extra.length) parsed.diags = [...parsed.diags, ...extra].sort((a: any, b: any) => a.line - b.line || a.col - b.col); if (extra.length) parsed.diags = [...parsed.diags, ...extra].sort((a: any, b: any) => a.line - b.line || a.col - b.col);
// from here on, exactly what study() does (src/engines/pseudo/study.js) // the routines USE brings in, checked in their own files first
const { program, diags } = check(parsed); const uses = resolveUses(parsed.ast.body, options.libraryStack || []);
if (uses.diags.length) parsed.diags = [...parsed.diags, ...uses.diags].sort((a: any, b: any) => a.line - b.line || a.col - b.col);
// from here on, what study() does (src/engines/pseudo/study.js), with the library's routines in scope
const { program, diags } = check(parsed, { libs: uses.libs, libOrigin: uses.origin });
const errors = diags.filter((d: any) => d.severity === 'error'); const errors = diags.filter((d: any) => d.severity === 'error');
let analysis = null; let analysis = null;
let coached: any[] = []; let coached: any[] = [];
+2 -2
View File
@@ -47,7 +47,7 @@ export function studyOf(source: string): Parsed<Study> {
function resolveCall(program: any, call: string) { function resolveCall(program: any, call: string) {
const p = parseExpression(call); const p = parseExpression(call);
if (p.diags.some((d: any) => d.severity === 'error') || p.expr.kind !== 'CallExpr') throw new Error(`"${call}" is not a call. Write the subroutine's name and its arguments, as in COUNT_BASE('A', "GATTACA").`); if (p.diags.some((d: any) => d.severity === 'error') || p.expr.kind !== 'CallExpr') throw new Error(`"${call}" is not a call. Write the subroutine's name and its arguments, as in COUNT_BASE('A', "GATTACA").`);
const sub = program.subs.get(p.expr.name.toUpperCase()); const sub = program.subs.get(p.expr.name.toUpperCase()) || program.libs?.get(p.expr.name.toUpperCase());
if (!sub) throw new Error(`The program has no FUNCTION or PROCEDURE called ${p.expr.name}.`); if (!sub) throw new Error(`The program has no FUNCTION or PROCEDURE called ${p.expr.name}.`);
if (p.expr.args.length !== sub.params.length) throw new Error(`${sub.name} takes ${sub.params.length} value(s), and the call gives ${p.expr.args.length}.`); if (p.expr.args.length !== sub.params.length) throw new Error(`${sub.name} takes ${sub.params.length} value(s), and the call gives ${p.expr.args.length}.`);
const args = p.expr.args.map((a: any, i: number) => { const args = p.expr.args.map((a: any, i: number) => {
@@ -89,7 +89,7 @@ export function runPseudo(parsed: Parsed<Study>, options: RunOptions): Trace<Pse
const call = text ? resolveCall(s.program, text) : null; const call = text ? resolveCall(s.program, text) : null;
const tr = runProgram(s.program, { call, input: options.input || [], maxSteps: options.maxSteps ?? 20000, contracts: true, seed: 1 }); const tr = runProgram(s.program, { call, input: options.input || [], maxSteps: options.maxSteps ?? 20000, contracts: true, seed: 1 });
const output = tr.output.slice(); const output = tr.output.slice();
if (call && tr.result !== undefined && !tr.error) output.push(`${call.name} returned ${formatValue(tr.result)}`); if (call && tr.result !== undefined && !tr.error) output.push(`${call.name} returned ${formatValue(tr.result, tr.resultType)}`);
return { return {
lang: 'pseudo', lang: 'pseudo',
steps: tr.steps, steps: tr.steps,
+174 -9
View File
@@ -39,6 +39,10 @@ export { GENERATED_HEADER };
export interface MojoOptions { export interface MojoOptions {
/** The call to run, such as COUNT_BASE('A', "GATTACA"); becomes the body of `main` when the pseudo has no lines of its own. */ /** The call to run, such as COUNT_BASE('A', "GATTACA"); becomes the body of `main` when the pseudo has no lines of its own. */
call?: string | null; call?: string | null;
/** Internal: write the definitions only, no imports and no main (a library routine). */
defsOnly?: boolean;
/** Internal: the library routines' Mojo, written before the program's own definitions. */
libraryBlocks?: Array<{ id: string; lines: string[]; imports: string[] }>;
} }
export interface MojoResult { source: string; map: LineMap; notes: Diagnostic[] } export interface MojoResult { source: string; map: LineMap; notes: Diagnostic[] }
@@ -49,10 +53,24 @@ export const USED_STDLIB: Readonly<Record<string, string>> = {
sqrt: 'std.math', exp: 'std.math', log: 'std.math', log2: 'std.math', log10: 'std.math', sqrt: 'std.math', exp: 'std.math', log: 'std.math', log2: 'std.math', log10: 'std.math',
sin: 'std.math', cos: 'std.math', tan: 'std.math', atan2: 'std.math', pi: 'std.math', sin: 'std.math', cos: 'std.math', tan: 'std.math', atan2: 'std.math', pi: 'std.math',
floor: 'std.math', ceil: 'std.math', comb: 'std.math', floor: 'std.math', ceil: 'std.math', comb: 'std.math',
asin: 'std.math', acos: 'std.math', atan: 'std.math', sinh: 'std.math', cosh: 'std.math', tanh: 'std.math',
hypot: 'std.math', cbrt: 'std.math', expm1: 'std.math', log1p: 'std.math', trunc: 'std.math', copysign: 'std.math',
fma: 'std.math', isnan: 'std.math', isinf: 'std.math', isfinite: 'std.math', inf: 'std.math', nan: 'std.math',
ulp: 'std.math', nextafter: 'std.math', gcd: 'std.math', lcm: 'std.math',
random_float64: 'std.random', random_si64: 'std.random', random_float64: 'std.random', random_si64: 'std.random',
}; };
/** Names the translator writes that Mojo provides without an import. */ /** Names the translator writes that Mojo provides without an import. */
export const USED_PRELUDE: readonly string[] = ['Int', 'Float64', 'String', 'Bool', 'List', 'Dict', 'print', 'range', 'len', 'min', 'max', 'abs', 'round', 'input', 'atol', 'atof', 'chr', 'ord', 'Error']; export const USED_PRELUDE: readonly string[] = ['Int', 'Float64', 'String', 'Bool', 'List', 'Dict', 'print', 'range', 'len', 'min', 'max', 'abs', 'round', 'input', 'atol', 'atof', 'chr', 'ord', 'Error',
'Int8', 'Int16', 'Int32', 'Int64', 'UInt8', 'UInt16', 'UInt32', 'UInt64', 'Float32', 'DType'];
/** The Mojo type for a pseudo number type: INT8 is Int8, FLOAT32 is Float32, INTEGER is Int, FLOAT and FLOAT64 are Float64. */
export function mojoNumber(t: any): string | null {
if (!t) return null;
if (t.k === 'INTEGER') return t.bits ? `${t.signed ? 'Int' : 'UInt'}${t.bits}` : 'Int';
if (t.k === 'FLOAT') return t.bits === 32 ? 'Float32' : 'Float64';
return null;
}
const isSized = (t: any): boolean => !!t && ((t.k === 'INTEGER' && !!t.bits) || (t.k === 'FLOAT' && t.bits === 32));
const up = (s: unknown): string => String(s).toUpperCase(); const up = (s: unknown): string => String(s).toUpperCase();
@@ -71,6 +89,7 @@ const RESERVED = new Set([
'and', 'or', 'not', 'in', 'is', 'if', 'elif', 'else', 'for', 'while', 'break', 'continue', 'return', 'pass', 'del', 'assert', 'class', 'lambda', 'global', 'yield', 'match', 'case', 'self', 'and', 'or', 'not', 'in', 'is', 'if', 'elif', 'else', 'for', 'while', 'break', 'continue', 'return', 'pass', 'del', 'assert', 'class', 'lambda', 'global', 'yield', 'match', 'case', 'self',
'len', 'min', 'max', 'abs', 'round', 'print', 'range', 'input', 'sum', 'type', 'list', 'str', 'int', 'float', 'set', 'dict', 'chr', 'ord', 'pow', 'sort', 'sorted', 'copy', 'main', 'len', 'min', 'max', 'abs', 'round', 'print', 'range', 'input', 'sum', 'type', 'list', 'str', 'int', 'float', 'set', 'dict', 'chr', 'ord', 'pow', 'sort', 'sorted', 'copy', 'main',
'atol', 'atof', 'floor', 'ceil', 'sqrt', 'exp', 'log', 'log2', 'log10', 'sin', 'cos', 'tan', 'atan2', 'pi', 'comb', 'true', 'false', 'none', 'atol', 'atof', 'floor', 'ceil', 'sqrt', 'exp', 'log', 'log2', 'log10', 'sin', 'cos', 'tan', 'atan2', 'pi', 'comb', 'true', 'false', 'none',
'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64', 'float32', 'float64', 'dtype',
...Object.keys(USED_STDLIB), ...Object.keys(USED_STDLIB),
]); ]);
@@ -168,7 +187,7 @@ class Gen {
constructor(private program: any, private source: string, private raising: Set<string>, private opts: MojoOptions) { constructor(private program: any, private source: string, private raising: Set<string>, private opts: MojoOptions) {
for (const c of program.comments || []) this.commentsByLine.set(c.line, c); for (const c of program.comments || []) this.commentsByLine.set(c.line, c);
this.srcLines = source.split('\n'); this.srcLines = source.split('\n');
for (const sub of program.subs.values()) { for (const sub of [...program.subs.values(), ...(program.libs?.values() ?? [])]) {
const changed = this.changedIn(sub.node.body); const changed = this.changedIn(sub.node.body);
const assigned = this.assignedIn(sub.node.body); const assigned = this.assignedIn(sub.node.body);
this.varParams.set(key(sub.name), sub.params.map((p: any) => p.mode !== 'BYREF' && ((changed.has(up(p.name)) && isCollection(p.type)) || assigned.has(up(p.name))))); this.varParams.set(key(sub.name), sub.params.map((p: any) => p.mode !== 'BYREF' && ((changed.has(up(p.name)) && isCollection(p.type)) || assigned.has(up(p.name)))));
@@ -253,8 +272,7 @@ class Gen {
type(t: any): string { type(t: any): string {
if (!t) this.gap('a value whose type is not known'); if (!t) this.gap('a value whose type is not known');
switch (t.k) { switch (t.k) {
case 'INTEGER': return 'Int'; case 'INTEGER': case 'FLOAT': return mojoNumber(t)!;
case 'FLOAT': return 'Float64';
case 'CHAR': case 'STRING': return 'String'; case 'CHAR': case 'STRING': return 'String';
case 'BOOL': return 'Bool'; case 'BOOL': return 'Bool';
case 'ARRAY': if (t.dims === 2) return `List[List[${this.type(t.of)}]]`; return `List[${this.type(t.of)}]`; case 'ARRAY': if (t.dims === 2) return `List[List[${this.type(t.of)}]]`; return `List[${this.type(t.of)}]`;
@@ -286,10 +304,17 @@ class Gen {
return `${text}.copy()`; return `${text}.copy()`;
} }
/** An INTEGER value going where a FLOAT belongs becomes a Float64, as the notation widens it. */ /**
* A value going where another number type belongs, converted as the notation converts it: an INTEGER into a
* FLOAT becomes Float64(...) (Float32(...) for a FLOAT32), an INTEGER into a sized type becomes Int32(...), a
* FLOAT32 into a FLOAT becomes Float64(...). A literal needs nothing: Mojo gives it the type it lands in.
*/
widened(target: any, e: any, text: string): string { widened(target: any, e: any, text: string): string {
if (target?.k === 'FLOAT' && e?.type?.k === 'INTEGER' && e.kind !== 'Num') return `Float64(${text})`; if (!target || !e?.type || e.kind === 'Num') return text;
return text; const want = mojoNumber(target); const have = mojoNumber(e.type);
if (!want || !have || want === have) return text;
if (isSized(target) && target.k === 'INTEGER') this.note('sized', 'In the notation, a value that does not fit a sized whole-number type stops the run. In Mojo, Int8 and the other fixed-width types wrap around silently, and a conversion such as Int8(x) keeps only the low bits.');
return `${want}(${text})`;
} }
/* ---------- program ---------- */ /* ---------- program ---------- */
@@ -323,7 +348,15 @@ class Gen {
this.emit(0, '', null); this.emit(0, '', null);
this.emit(0, '', null); this.emit(0, '', null);
} }
for (const b of this.opts.libraryBlocks || []) {
this.emit(0, `# From the Programming+ library: ${b.id} (verified in pplus-library; generated from its pseudo)`, null);
for (const l of b.lines) this.emit(0, l, null);
for (const n of b.imports) this.imports.add(n);
this.emit(0, '', null);
this.emit(0, '', null);
}
for (const sub of p.subs.values()) { this.subroutine(sub); this.emit(0, '', null); this.emit(0, '', null); } for (const sub of p.subs.values()) { this.subroutine(sub); this.emit(0, '', null); this.emit(0, '', null); }
if (this.opts.defsOnly) return this.assemble();
this.mainFunction(); this.mainFunction();
this.leadingComments(Infinity, 0); this.leadingComments(Infinity, 0);
return this.assemble(); return this.assemble();
@@ -650,6 +683,7 @@ class Gen {
case 'Str': case 'Char': return JSON.stringify(e.value); case 'Str': case 'Char': return JSON.stringify(e.value);
case 'Bool': return e.value ? 'True' : 'False'; case 'Bool': return e.value ? 'True' : 'False';
case 'Blank': return this.gap(`a blank to fill in: <${e.text}>`); case 'Blank': return this.gap(`a blank to fill in: <${e.text}>`);
case 'TypeRef': return e.name === 'INTEGER' ? 'Int' : e.name === 'FLOAT' || e.name === 'FLOAT64' ? 'Float64' : e.name === 'FLOAT32' ? 'Float32' : e.name.startsWith('UINT') ? `UInt${e.name.slice(4)}` : `Int${e.name.slice(3)}`;
case 'Name': case 'Name':
if (e.sym?.kind === 'const') return this.constName(e.name); if (e.sym?.kind === 'const') return this.constName(e.name);
if (e.sym?.kind === 'define') return `${this.name(e.name)}()`; if (e.sym?.kind === 'define') return `${this.name(e.name)}()`;
@@ -659,7 +693,12 @@ class Gen {
case 'Binary': { case 'Binary': {
const map: Record<string, string> = { AND: 'and', OR: 'or', DIV: '//', MOD: '%', '^': '**', '&': '+', IN: 'in' }; const map: Record<string, string> = { AND: 'and', OR: 'or', DIV: '//', MOD: '%', '^': '**', '&': '+', IN: 'in' };
if (e.op === '/') this.note('divide', 'In the notation, dividing by zero stops the program. In Mojo a Float64 division by zero gives inf or nan and the program goes on.'); if (e.op === '/') this.note('divide', 'In the notation, dividing by zero stops the program. In Mojo a Float64 division by zero gives inf or nan and the program goes on.');
if (e.op === '/' && mojoNumber(e.type) === 'Float32') {
const side = (x: any) => (x.kind === 'Num' || mojoNumber(x.type) === 'Float32' ? this.sideExpr(x, e, x === e.left ? 'l' : 'r') : `Float32(${this.expr(x)})`);
return `${side(e.left)} / ${side(e.right)}`;
}
if (e.op === '/') return `Float64(${this.expr(e.left)}) / Float64(${this.expr(e.right)})`; if (e.op === '/') return `Float64(${this.expr(e.left)}) / Float64(${this.expr(e.right)})`;
if (isSized(e.type) && e.type.k === 'INTEGER' && ['+', '-', '*', '^', 'DIV', 'MOD'].includes(e.op)) this.note('sized', 'In the notation, a value that does not fit a sized whole-number type stops the run. In Mojo, Int8 and the other fixed-width types wrap around silently, and a conversion such as Int8(x) keeps only the low bits.');
if (e.op === 'IN' && e.right.kind === 'Range') return `${this.expr(e.right.lo)} <= ${P(e.left)} < ${this.expr(e.right.hi)}`; if (e.op === 'IN' && e.right.kind === 'Range') return `${this.expr(e.right.lo)} <= ${P(e.left)} < ${this.expr(e.right.hi)}`;
if (e.op === '&') { if (e.op === '&') {
const side = (x: any) => (x.type?.k === 'CHAR' || x.type?.k === 'STRING' ? P(x) : `String(${this.expr(x)})`); const side = (x: any) => (x.type?.k === 'CHAR' || x.type?.k === 'STRING' ? P(x) : `String(${this.expr(x)})`);
@@ -720,7 +759,17 @@ class Gen {
const BIND: Record<string, number> = { OR: 1, AND: 2, '==': 4, '!=': 4, '<': 4, '<=': 4, '>': 4, '>=': 4, IN: 4, '&': 5, '+': 6, '-': 6, '*': 7, '/': 7, DIV: 7, MOD: 7, '^': 9 }; const BIND: Record<string, number> = { OR: 1, AND: 2, '==': 4, '!=': 4, '<': 4, '<=': 4, '>': 4, '>=': 4, IN: 4, '&': 5, '+': 6, '-': 6, '*': 7, '/': 7, DIV: 7, MOD: 7, '^': 9 };
let s = this.expr(x); let s = this.expr(x);
const other = side === 'l' ? parent.right : parent.left; const other = side === 'l' ? parent.right : parent.left;
if (['+', '-', '*', '==', '!=', '<', '<=', '>', '>='].includes(parent.op) && x.type?.k === 'INTEGER' && other?.type?.k === 'FLOAT' && x.kind !== 'Num') s = `Float64(${s})`; if (['+', '-', '*', '==', '!=', '<', '<=', '>', '>=', 'DIV', 'MOD', '^'].includes(parent.op) && x.kind !== 'Num' && other?.type) {
// two sides of different number types meet as the notation has them meet
const mine = mojoNumber(x.type); const theirs = mojoNumber(other.type);
const compare = ['==', '!=', '<', '<=', '>', '>='].includes(parent.op);
if (mine && theirs && mine !== theirs) {
if (x.type.k === 'INTEGER' && other.type.k === 'FLOAT') s = `${theirs}(${s})`;
else if (x.type.k === 'INTEGER' && other.type.k === 'INTEGER' && !x.type.bits) s = `${theirs}(${s})`;
else if (compare && x.type.k === 'FLOAT' && other.type.k === 'FLOAT' && mine === 'Float32') s = `Float64(${s})`;
else if (compare && x.type.k === 'INTEGER' && other.type.k === 'INTEGER') s = `Int(${s})`;
}
}
if (x.kind === 'Chain' || x.kind === 'Quant') return `(${s})`; if (x.kind === 'Chain' || x.kind === 'Quant') return `(${s})`;
if (x.kind === 'Unary' && x.op === 'NOT' && BIND[parent.op] > 3) return `(${s})`; if (x.kind === 'Unary' && x.op === 'NOT' && BIND[parent.op] > 3) return `(${s})`;
if (x.kind !== 'Binary') return s; if (x.kind !== 'Binary') return s;
@@ -729,6 +778,18 @@ class Gen {
return s; return s;
} }
/** A value as the Mojo number type T: unchanged when it already is one, or a literal; converted otherwise. */
as(T: string, node: any, text: string): string {
if (mojoNumber(node?.type) === T) return text;
return `${T}(${text})`;
}
/** IS_CLOSE's four values, as Float64 unless they are already FLOATs of one precision. */
args4(e: any): string[] {
const f32 = e.args.slice(0, 2).every((x: any) => mojoNumber(x.type) === 'Float32');
return e.args.map((x: any) => (x.kind === 'Num' || mojoNumber(x.type) === (f32 ? 'Float32' : 'Float64') ? this.expr(x) : `${f32 ? 'Float32' : 'Float64'}(${this.expr(x)})`));
}
/** A built-in that is a short loop: write it before the statement and name its result. */ /** A built-in that is a short loop: write it before the statement and name its result. */
loopResult(stem: string, type: string, init: string, lines: Array<[number, string]>): string { loopResult(stem: string, type: string, init: string, lines: Array<[number, string]>): string {
const t = this.fresh(stem); const t = this.fresh(stem);
@@ -738,6 +799,13 @@ class Gen {
call(e: any): string { call(e: any): string {
if (e.isOld) return this.gap('OLD(...) outside a contract'); if (e.isOld) return this.gap('OLD(...) outside a contract');
if (e.convert) {
const to = mojoNumber(e.type)!;
if (e.type.k === 'INTEGER') this.note('sized', 'In the notation, a value that does not fit a sized whole-number type stops the run. In Mojo, Int8 and the other fixed-width types wrap around silently, and a conversion such as Int8(x) keeps only the low bits.');
const arg = e.args[0];
// a FLOAT literal becomes a Float64 first, so the conversion is from a runtime value, as in the notation
return `${to}(${arg.kind === 'Num' && arg.float ? `Float64(${this.expr(arg)})` : this.expr(arg)})`;
}
if (!e.builtin) { if (!e.builtin) {
const flags = this.varParams.get(key(e.name)) || []; const flags = this.varParams.get(key(e.name)) || [];
const args = e.args.map((a: any, i: number) => this.callArg(a, e.sub?.params?.[i], flags[i])); const args = e.args.map((a: any, i: number) => this.callArg(a, e.sub?.params?.[i], flags[i]));
@@ -808,6 +876,7 @@ class Gen {
} }
case 'SUM_OF': { case 'SUM_OF': {
const x = this.fresh('x'); const x = this.fresh('x');
if (isSized(e.type)) return this.loopResult('total', this.type(e.type), '0', [[0, `for ${x} in ${a}:`], [1, `$T += ${x}`]]);
const zero = e.type?.k === 'FLOAT' ? '0.0' : '0'; const zero = e.type?.k === 'FLOAT' ? '0.0' : '0';
return this.loopResult('total', '', zero, [[0, `for ${x} in ${a}:`], [1, `$T += ${x}`]]); return this.loopResult('total', '', zero, [[0, `for ${x} in ${a}:`], [1, `$T += ${x}`]]);
} }
@@ -818,6 +887,7 @@ class Gen {
} }
case 'DOT': { case 'DOT': {
const i = this.fresh('i'); const i = this.fresh('i');
if (isSized(e.type)) return this.loopResult('dot', this.type(e.type), '0', [[0, `for ${i} in range(len(${a})):`], [1, `$T += ${a}[${i}] * ${b}[${i}]`]]);
return this.loopResult('dot', '', e.type?.k === 'FLOAT' ? '0.0' : '0', [[0, `for ${i} in range(len(${a})):`], [1, `$T += ${a}[${i}] * ${b}[${i}]`]]); return this.loopResult('dot', '', e.type?.k === 'FLOAT' ? '0.0' : '0', [[0, `for ${i} in range(len(${a})):`], [1, `$T += ${a}[${i}] * ${b}[${i}]`]]);
} }
case 'VARIANCE_OF': case 'STDEV_OF': { case 'VARIANCE_OF': case 'STDEV_OF': {
@@ -864,6 +934,59 @@ class Gen {
this.hoist([[0, `sort(${items})`]]); this.hoist([[0, `sort(${items})`]]);
return items; return items;
} }
// ---- numerical work
case 'ASIN': return math('asin');
case 'ACOS': return math('acos');
case 'ATAN': return math('atan');
case 'SINH': return math('sinh');
case 'COSH': return math('cosh');
case 'TANH': return math('tanh');
case 'CBRT': return math('cbrt');
case 'EXPM1': return math('expm1');
case 'LOG1P': return math('log1p');
case 'HYPOT': this.need('hypot'); return `hypot(Float64(${a}), Float64(${b}))`;
case 'POW': return `Float64(${a}) ** Float64(${b})`;
case 'COPYSIGN': this.need('copysign'); return `copysign(Float64(${a}), Float64(${b}))`;
case 'E': return '2.718281828459045';
case 'TRUNC': this.need('trunc'); return `trunc(${e.args[0].type?.k === 'FLOAT' ? a : `Float64(${a})`})`;
case 'SIGN': { const T = this.type(e.type); return `(${T}(1) if ${a} > 0 else (${T}(-1) if ${a} < 0 else ${T}(0)))`; }
case 'CLAMP': return `min(max(${a}, ${b}), ${c})`;
case 'FMA': { const T = mojoNumber(e.type)!; this.need('fma'); return `fma(${this.as(T, e.args[0], a)}, ${this.as(T, e.args[1], b)}, ${this.as(T, e.args[2], c)})`; }
case 'IS_NAN': this.need('isnan'); return `isnan(${e.args[0].type?.k === 'FLOAT' ? a : `Float64(${a})`})`;
case 'IS_INF': this.need('isinf'); return `isinf(${e.args[0].type?.k === 'FLOAT' ? a : `Float64(${a})`})`;
case 'IS_FINITE': this.need('isfinite'); return `isfinite(${e.args[0].type?.k === 'FLOAT' ? a : `Float64(${a})`})`;
case 'INF': this.need('inf'); return 'inf[DType.float64]()';
case 'NAN': this.need('nan'); return 'nan[DType.float64]()';
case 'ULP': { const T = mojoNumber(e.type)!; this.need('ulp'); return `ulp(${this.as(T, e.args[0], a)})`; }
case 'NEXT_AFTER': { const T = mojoNumber(e.type)!; this.need('nextafter'); return `nextafter(${this.as(T, e.args[0], a)}, ${this.as(T, e.args[1], b)})`; }
case 'IS_CLOSE': {
const d = this.args4(e);
return `(${d[0]} == ${d[1]} or abs(${d[0]} - ${d[1]}) <= max(${d[2]} * max(abs(${d[0]}), abs(${d[1]})), ${d[3]}))`;
}
case 'MAX_VALUE': case 'MIN_VALUE': case 'EPSILON': {
const T = mojoNumber(e.type)!;
const ty = e.type;
if (e.builtin === 'EPSILON') return `${T}(${ty.bits === 32 ? '1.1920928955078125e-07' : '2.220446049250313e-16'})`;
if (ty.k === 'INTEGER') {
const hi = ty.signed ? 2 ** (ty.bits - 1) - 1 : 2 ** ty.bits - 1; const lo = ty.signed ? -(2 ** (ty.bits - 1)) : 0;
return `${T}(${e.builtin === 'MAX_VALUE' ? hi : lo})`;
}
const m = ty.bits === 32 ? '3.4028234663852886e38' : '1.7976931348623157e308';
return `${T}(${e.builtin === 'MAX_VALUE' ? m : `-${m}`})`;
}
case 'GCD': this.need('gcd'); return `gcd(${a}, ${b})`;
case 'LCM': this.need('lcm'); return `lcm(${a}, ${b})`;
case 'BIT_AND': return `(${a} & ${b})`;
case 'BIT_OR': return `(${a} | ${b})`;
case 'BIT_XOR': return `(${a} ^ ${b})`;
case 'BIT_NOT': return `~${a}`;
case 'SHIFT_LEFT': case 'SHIFT_RIGHT': {
const T = this.type(e.type);
const n = isSized(e.type) ? `${T}(${b})` : b;
if (e.builtin === 'SHIFT_LEFT' && isSized(e.type)) this.note('sized', 'In the notation, a value that does not fit a sized whole-number type stops the run. In Mojo, Int8 and the other fixed-width types wrap around silently, and a conversion such as Int8(x) keeps only the low bits.');
return `(${a} ${e.builtin === 'SHIFT_LEFT' ? '<<' : '>>'} ${n})`;
}
case 'WRAP': return `${this.type(e.type)}(${a})`;
default: default:
return this.gap(`the built-in ${e.builtin}, which Mojo's standard library has no function for`); return this.gap(`the built-in ${e.builtin}, which Mojo's standard library has no function for`);
} }
@@ -886,10 +1009,24 @@ export function pseudoToMojo(pseudo: string, opts: MojoOptions = {}): MojoResult
return { unsupported: errors.length ? errors : [{ lang: 'pseudo', severity: 'error', id: 'translate-no-program', line: 1, message: 'The pseudo does not read yet, so there is nothing to translate.' }] }; return { unsupported: errors.length ? errors : [{ lang: 'pseudo', severity: 'error', id: 'translate-no-program', line: 1, message: 'The pseudo does not read yet, so there is nothing to translate.' }] };
} }
const program = st.program; const program = st.program;
// the library routines the program uses, each once, the ones they use first
const libs = libraryOrder(program);
const libRaising = new Set<string>();
const libraryBlocks: Array<{ id: string; lines: string[]; imports: string[] }> = [];
for (const lib of libs) {
const raising = inferRaising(lib.program, libRaising);
const gen = new Gen(lib.program, lib.source, raising, { defsOnly: true });
gen.run();
for (const sub of lib.program.subs.values()) if (raising.has(up(sub.name))) libRaising.add(up(sub.name));
const lines = gen.out.map((l) => l.text);
while (lines.length && !lines[lines.length - 1].trim()) lines.pop();
libraryBlocks.push({ id: lib.id, lines, imports: [...gen.imports] });
}
opts = { ...opts, libraryBlocks };
const callText = (opts.call || '').trim(); const callText = (opts.call || '').trim();
const callName = callText ? /^\s*([A-Za-z_][A-Za-z0-9_]*)/.exec(callText)?.[1] : null; const callName = callText ? /^\s*([A-Za-z_][A-Za-z0-9_]*)/.exec(callText)?.[1] : null;
// Inference, then translation; a line that could not be translated becomes a raise, so its routine raises too, and the pass is run again. // Inference, then translation; a line that could not be translated becomes a raise, so its routine raises too, and the pass is run again.
const extra = new Set<string>(); const extra = new Set<string>(libRaising);
for (let pass = 0; ; pass++) { for (let pass = 0; ; pass++) {
const raising = inferRaising(program, extra); const raising = inferRaising(program, extra);
if (bodyRaises(program.main.body, raising)) raising.add('MAIN'); if (bodyRaises(program.main.body, raising)) raising.add('MAIN');
@@ -903,6 +1040,34 @@ export function pseudoToMojo(pseudo: string, opts: MojoOptions = {}): MojoResult
} }
} }
/** The names of every routine a program calls, anywhere in it. */
function calledNames(program: any): Set<string> {
const out = new Set<string>();
const seen = new Set<any>();
const visit = (x: any): void => {
if (!x || typeof x !== 'object' || seen.has(x)) return;
seen.add(x);
if (Array.isArray(x)) { for (const y of x) visit(y); return; }
if ((x.kind === 'CallExpr' || x.kind === 'Call') && x.name) out.add(up(x.name));
for (const [k, v] of Object.entries(x)) if (k !== 'sub' && k !== 'sym' && k !== 'type' && k !== 'resolved' && v && typeof v === 'object') visit(v);
};
visit(program.main?.body);
for (const s of program.subs.values()) { visit(s.node.body); visit(s.requires); visit(s.ensures); }
return out;
}
/** Every library routine a program calls, directly or through another routine, dependencies first, each once. */
function libraryOrder(program: any, out: Map<string, any> = new Map()): any[] {
const called = calledNames(program);
for (const sub of program.libs?.values() ?? []) {
if (!called.has(up(sub.name))) continue;
if (out.has(sub.lib.id)) continue;
libraryOrder(sub.lib.program, out);
out.set(sub.lib.id, sub.lib);
}
return [...out.values()];
}
/** The set of names a generated source may use from the standard library, for the grounding test. */ /** The set of names a generated source may use from the standard library, for the grounding test. */
export function stdlibNamesUsed(): string[] { return Object.keys(USED_STDLIB); } export function stdlibNamesUsed(): string[] { return Object.keys(USED_STDLIB); }
export const STD_INDEX = MOJO_STD_EXPORTS; export const STD_INDEX = MOJO_STD_EXPORTS;
+14
View File
@@ -0,0 +1,14 @@
// Sized whole numbers: arithmetic that stays inside its type, wrapping only when asked, and conversions that check.
// EXPECT: 255 0 255
// EXPECT: 1500000000 -2 256
// EXPECT: 8 14 6 255 1024 -4
// EXPECT: 12 12 -128 2147483647
DECLARE Count : UINT8 <- 250
Count <- Count + 5
OUTPUT Count, " ", WRAP(Count + 1, UINT8), " ", MAX_VALUE(UINT8)
DECLARE Big : INT64 <- 3_000_000_000
DECLARE Small : INT32
Small <- INT32(Big DIV 2)
OUTPUT Small, " ", INT16(-2.9), " ", 0xFF + 0b1
OUTPUT BIT_AND(0b1100, 0b1010), " ", BIT_OR(12, 10), " ", BIT_XOR(12, 10), " ", BIT_NOT(UINT8(0)), " ", SHIFT_LEFT(1, 10), " ", SHIFT_RIGHT(-8, 1)
OUTPUT GCD(84, 36), " ", LCM(4, 6), " ", MIN_VALUE(INT8), " ", MAX_VALUE(INT32)
+20
View File
@@ -0,0 +1,20 @@
// Single precision: every FLOAT32 result is rounded to 24 bits, and a literal next to a FLOAT32 is a FLOAT32.
// EXPECT: 1.0000001 0.9999999999999999
// EXPECT: TRUE FALSE
// EXPECT: 0.100000001490116119384765625
// EXPECT: 16777216.0 1.1920929e-7 1.1920929e-7
// EXPECT: 5.551115123125783e-17 0.0
// EXPECT: TRUE TRUE
DECLARE X : FLOAT32 <- 0.1
DECLARE Total32 : FLOAT32 <- 0.0
DECLARE Total64 : FLOAT <- 0.0
FOR I <- 0 TO 10
Total32 <- Total32 + X
Total64 <- Total64 + 0.1
NEXT I
OUTPUT Total32, " ", Total64
OUTPUT X == 0.1, " ", FLOAT64(X) == 0.1
OUTPUT EXACT(X)
OUTPUT FLOAT32(16777217), " ", ULP(FLOAT32(1.0)), " ", EPSILON(FLOAT32)
OUTPUT FMA(0.1, 10.0, -1.0), " ", 0.1 * 10.0 - 1.0
OUTPUT IS_CLOSE(Total32, 1.0, 1e-6, 0.0), " ", IS_CLOSE(Total64, 1.0, 1e-15, 0.0)
+12
View File
@@ -0,0 +1,12 @@
// USE IEEE: NaN and the infinities flow through a calculation as IEEE 754 says, instead of stopping the run.
// EXPECT: infinity -infinity not a number
// EXPECT: TRUE TRUE FALSE TRUE
// EXPECT: not a number -infinity FALSE
USE IEEE
DECLARE Zero : FLOAT <- 0.0
DECLARE Inf : FLOAT
Inf <- 1.0 / Zero
DECLARE NotANumber : FLOAT <- Inf - Inf
OUTPUT Inf, " ", -Inf, " ", NotANumber
OUTPUT IS_INF(Inf), " ", IS_NAN(NotANumber), " ", NotANumber == NotANumber, " ", NotANumber != NotANumber
OUTPUT SQRT(-1.0), " ", LOG(0.0), " ", IS_FINITE(MAX_VALUE(FLOAT64) * 2.0)
+11
View File
@@ -0,0 +1,11 @@
// Elementary functions for numerical work, and the accurate forms near 0.
// EXPECT: 5.0 TRUE -3.0 1024.0
// EXPECT: 1.00000000005e-10 1.000000082740371e-10 9.999999999500001e-11
// EXPECT: -2.0 -1 -2.0 10 0.0
// EXPECT: 3.142 3.142 0.0 0.0 2.718
// EXPECT: TRUE 2.220446049250313e-16
OUTPUT HYPOT(3.0, 4.0), " ", HYPOT(1e200, 1e200) > 1e200, " ", CBRT(-27.0), " ", POW(2.0, 10.0)
OUTPUT EXPM1(1e-10), " ", EXP(1e-10) - 1.0, " ", LOG1P(1e-10)
OUTPUT TRUNC(-2.7), " ", SIGN(-3), " ", COPYSIGN(2.0, -0.0), " ", CLAMP(15, 0, 10), " ", CLAMP(-0.5, 0.0, 1.0)
OUTPUT ROUND(ASIN(1.0) * 2 * 1000) / 1000, " ", ROUND(ACOS(-1.0) * 1000) / 1000, " ", ATAN(0.0), " ", TANH(0.0), " ", ROUND(E() * 1000) / 1000
OUTPUT NEXT_AFTER(1.0, 2.0) - 1.0 == EPSILON(FLOAT64), " ", ULP(1.0)
+12
View File
@@ -0,0 +1,12 @@
// Routines from the library, called like the program's own: a run steps over each one, and the
// prover uses each one's REQUIRE and ENSURE at the call.
// EXPECT: 5.0 4.571428571428571
// EXPECT: 0.0 -0.0615234375
// EXPECT: 0
USE stats
USE md.LennardJones
USE align.NeedlemanWunschScore
DECLARE Samples : LIST OF FLOAT <- [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]
OUTPUT Mean(Samples), " ", Variance(Samples)
OUTPUT LennardJones(1.0, 1.0, 1.0), " ", LennardJones(2.0, 1.0, 1.0)
OUTPUT NeedlemanWunschScore("GATTACA", "GCATGCU", 1, -1, -1)
+85
View File
@@ -0,0 +1,85 @@
// The library (library/, pplus-library): the registry core builds from it is current, every routine checks and
// runs as its record says, and USE behaves as docs/library.md describes: names resolve, runs step over the
// routines, the prover uses their contracts, and the Mojo translator writes each routine once.
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { spawnSync } from 'node:child_process';
import { libraryModules, libraryRoutine } from '../dist/library/registry.js';
import { studyOf, runPseudo, provePseudo } from '../dist/services/pseudo.js';
import { pseudoToMojo } from '../dist/sync/pseudo-to-mojo.js';
import { checkMojo } from '../dist/mojo/check.js';
import { runMojo } from '../dist/mojo/interp.js';
test('the registry is generated from the library checkout and is up to date', () => {
assert.ok(fs.existsSync('library/modules'), 'library/ is not checked out: git submodule update --init');
const r = spawnSync(process.execPath, ['scripts/library.mjs', 'emit', '--check'], { encoding: 'utf8' });
assert.equal(r.status, 0, r.stderr || r.stdout);
});
test('every routine: one routine per file, its cases run, and its record is current and has no failing check', () => {
const mods = libraryModules();
assert.ok(mods.length >= 6, `only ${mods.length} modules`);
let routines = 0;
for (const m of mods) {
for (const r of m.routines) {
routines++;
const st = studyOf(r.source);
assert.ok(st.ok, `${r.id}: ${st.diagnostics.filter((d) => d.severity === 'error').map((d) => d.message).join('; ')}`);
const subs = [...st.program.program.subs.values()];
assert.deepEqual(subs.map((s) => s.name), [r.name], `${r.id} defines ${subs.map((s) => s.name)}`);
assert.ok(r.cases.length >= 2, `${r.id} has fewer than two cases`);
for (const c of r.cases) {
const tr = runPseudo(st, { call: c.call, maxSteps: 2000000 });
assert.equal(tr.error, null, `${r.id}: ${c.call}: ${tr.error?.message}`);
assert.ok(c.result !== undefined, `${r.id}: ${c.call} has no recorded result: run pixi run library-check`);
}
const all = [...Object.entries(r.checks), ...r.references.flatMap((x) => Object.entries(x.checks).map(([k, v]) => [`${x.lang}/${x.level} ${k}`, v])), ...(r.proof ? [['proof', r.proof.check]] : [])];
for (const [k, c] of all) assert.notEqual(c.status, 'fail', `${r.id} ${k}: ${c.detail}`);
for (const k of ['structure', 'pseudo', 'prover', 'fuzz']) assert.ok(r.checks[k], `${r.id} has no ${k} check recorded: run pixi run library-check`);
}
}
assert.ok(routines >= 16, `only ${routines} routines`);
});
test('USE: a module or one routine; unknown names are errors at the USE line', () => {
assert.ok(libraryRoutine('md.LennardJones'));
assert.ok(libraryRoutine('MD', 'lennardjones'), 'names ignore case');
const ids = (src) => studyOf(src).diagnostics.filter((d) => d.severity === 'error').map((d) => `${d.id}@${d.line}`);
assert.deepEqual(ids('USE nosuchmodule\n'), ['unknown-library@1']);
assert.deepEqual(ids('USE md.NoSuchRoutine\n'), ['unknown-routine@1']);
assert.deepEqual(ids('USE md.MinimumImage\nFUNCTION MinimumImage(X : FLOAT) RETURNS FLOAT\n RETURN X\nENDFUNCTION\n'), ['library-clash@2']);
assert.deepEqual(ids('USE align.MatchScore\nOUTPUT LinearGapPenalty(2, -1)\n'), ['unknown-function@2'], 'USE of one routine brings in only that one');
assert.deepEqual(ids('IF TRUE THEN\n USE md\nENDIF\n'), ['use-place@2']);
});
test('a run steps over a library routine, and a broken REQUIRE is reported at the call', () => {
const st = studyOf('USE md\nOUTPUT MinimumImage(2.3, 1.0)\nOUTPUT MinimumImage(1.0, 0.0)\n');
const tr = runPseudo(st, {});
assert.equal(tr.output[0], '0.2999999999999998');
assert.ok(tr.steps.every((s) => s.line === null || s.line <= 3), 'a step carries a line of the library file');
assert.ok(tr.steps.some((s) => s.kind === 'call' && /from the library \(md\.MinimumImage\)/.test(s.text)));
assert.equal(tr.error?.kind, 'require');
assert.equal(tr.error.line, 3);
assert.match(tr.error.message, /In the library routine md\.MinimumImage/);
});
test('the prover uses a routine\'s REQUIRE at the call and its ENSURE afterwards, without proving it again', () => {
const r = provePseudo(studyOf('USE seq.HammingDistance\nFUNCTION F(S : STRING) RETURNS INTEGER\n ENSURE RESULT <= LENGTH(S)\n RETURN HammingDistance(S, S)\nENDFUNCTION\n'));
assert.deepEqual(r.units.map((u) => u.name), ['F'], 'library routines are not units of the program');
const obs = r.units[0].obligations;
assert.equal(obs.find((o) => o.kind === 'require').status, 'proved');
assert.equal(obs.find((o) => o.kind === 'ensure').status, 'proved', 'the ENSURE of HammingDistance is a fact after the call');
});
test('the Mojo made from a program that uses the library holds each routine once, dependencies first, and agrees', () => {
const src = 'USE align\nOUTPUT NeedlemanWunschScore("GATTACA", "GCATGCU", 1, -1, -1), " ", MatchScore(\'A\', \'A\', 2, -1)\n';
const mj = pseudoToMojo(src);
const defs = [...mj.source.matchAll(/^def (\w+)/gm)].map((m) => m[1]);
assert.deepEqual(defs, ['match_score', 'linear_gap_penalty', 'needleman_wunsch_score', 'main']);
assert.match(mj.source, /# From the Programming\+ library: align\.MatchScore/);
const ck = checkMojo(mj.source);
assert.deepEqual(ck.diagnostics.filter((d) => d.severity === 'error'), []);
const run = runMojo(ck.module, mj.source, { maxSteps: 4000000 });
assert.equal(run.stdout.trim(), runPseudo(studyOf(src), {}).output.join('\n'));
});
+152
View File
@@ -0,0 +1,152 @@
// Pseudo for numerical work: sized types, FLOAT32, conversions, the numerical built-ins, USE IEEE, and what the
// prover may and may not say about them. The corpus programs 49 to 52 show the same features in use; this file
// pins each rule on its own.
import test from 'node:test';
import assert from 'node:assert/strict';
import { studyOf, runPseudo, provePseudo } from '../dist/services/pseudo.js';
import { fma, exactDecimal, nextAfter, ulp, roundDyadic, wrap } from '../dist/engines/pseudo/numeric.js';
import { intRange, SIZED, formatFloat32 } from '../dist/engines/pseudo/lang.js';
const run = (src) => {
const st = studyOf(src);
assert.ok(st.ok, st.diagnostics.filter((d) => d.severity === 'error').map((d) => `L${d.line} ${d.id}: ${d.message}`).join(' | '));
return runPseudo(st, {});
};
const out = (src) => { const r = run(src); assert.equal(r.error, null, r.error?.message); return r.output; };
const errors = (src) => studyOf(src).diagnostics.filter((d) => d.severity === 'error').map((d) => d.id);
test('the sized types have the ranges of their names (64-bit types cut at what a run holds exactly)', () => {
assert.deepEqual(intRange(SIZED.INT8), [-128, 127]);
assert.deepEqual(intRange(SIZED.UINT16), [0, 65535]);
assert.deepEqual(intRange(SIZED.INT32), [-2147483648, 2147483647]);
assert.deepEqual(intRange(SIZED.UINT32), [0, 4294967295]);
assert.deepEqual(intRange(SIZED.INT64), [-Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]);
});
test('literals: underscores, hexadecimal and binary', () => {
assert.deepEqual(out('OUTPUT 1_000_000, " ", 0xFF, " ", 0b1010, " ", 1_000.5'), ['1000000 255 10 1000.5']);
assert.ok(errors('OUTPUT 1__0').includes('bad-number'));
assert.ok(errors('OUTPUT 0xFG').includes('bad-number'));
});
test('a sized result that leaves its range stops the run; WRAP wraps on purpose', () => {
const r = run('DECLARE X : INT8 <- 127\nX <- X + 1\n');
assert.equal(r.error?.kind, 'overflow');
assert.equal(r.error.line, 2);
assert.match(r.error.message, /INT8 holds -128 to 127/);
assert.deepEqual(out('DECLARE X : INT8 <- 127\nOUTPUT WRAP(X + 1, INT8), " ", WRAP(-1, UINT8), " ", WRAP(70000, UINT16)'), ['-128 255 4464']);
assert.equal(run('DECLARE U : UINT8 <- 0\nU <- U - 1\n').error?.kind, 'overflow');
assert.equal(run('DECLARE X : INT8\nX <- -128\nX <- -X\n').error?.kind, 'overflow');
});
test('storing, passing and returning check the range of the receiving type', () => {
assert.equal(run('DECLARE N : INTEGER <- 300\nDECLARE B : UINT8\nB <- N\n').error?.kind, 'overflow');
const src = 'FUNCTION Id(X : UINT8) RETURNS UINT8\n RETURN X\nENDFUNCTION\nDECLARE N : INTEGER <- 256\nOUTPUT Id(N)\n';
const r = run(src);
assert.equal(r.error?.kind, 'overflow');
assert.match(r.error.message, /Parameter X of Id/);
assert.equal(run('FUNCTION Big(N : INTEGER) RETURNS INT16\n RETURN N * 1000\nENDFUNCTION\nOUTPUT Big(40)\n').error?.kind, 'overflow');
});
test('conversions: a FLOAT loses its fraction toward zero, and a value that does not fit stops the run', () => {
assert.deepEqual(out('OUTPUT INT32(-2.9), " ", UINT8(200), " ", INT64(2.5e9), " ", FLOAT32(0.1) == FLOAT32(0.1)'), ['-2 200 2500000000 TRUE']);
assert.deepEqual(errors('OUTPUT INT8(200)'), ['literal-range']); // a literal is checked before any run
assert.equal(run('DECLARE N : INTEGER <- 200\nOUTPUT INT8(N)').error?.kind, 'overflow');
assert.equal(run('DECLARE F : FLOAT <- 3.0e9\nOUTPUT INT32(F)').error?.kind, 'overflow');
});
test('the checker: widths and precisions do not mix silently, and literals take the type beside them', () => {
assert.deepEqual(errors('DECLARE A : INT8 <- 1\nDECLARE B : INT32 <- 2\nOUTPUT A + B'), ['mixed-width']);
assert.deepEqual(errors('DECLARE A : FLOAT32 <- 1.0\nDECLARE B : FLOAT <- 2.0\nOUTPUT A * B'), ['mixed-precision']);
assert.deepEqual(errors('DECLARE A : INT8\nA <- 300'), ['literal-range']);
assert.deepEqual(errors('DECLARE A : INT8 <- 1\nDECLARE B : INT32 <- 2\nB <- A'), ['type']);
assert.deepEqual(errors('DECLARE A : FLOAT32\nDECLARE D : FLOAT <- 0.5\nA <- D'), ['type']);
assert.deepEqual(errors('DECLARE A : FLOAT32\nDECLARE D : FLOAT <- 0.5\nA <- FLOAT32(D)'), []);
assert.deepEqual(errors('DECLARE A : FLOAT32\nA <- 0.1'), []);
assert.deepEqual(errors('OUTPUT INT8'), ['type-as-value']);
assert.deepEqual(errors('OUTPUT MAX_VALUE(INT64)'), ['exact-limit']);
assert.deepEqual(errors('OUTPUT WRAP(5, UINT64)'), ['wrap-64']);
assert.deepEqual(errors('DECLARE X : INTEGER <- 1\nOUTPUT EPSILON(INT32)'), ['type']);
});
test('FLOAT32: results round to single precision, a literal next to one is a single, and it prints as the shortest decimal', () => {
assert.deepEqual(out('DECLARE X : FLOAT32 <- 0.1\nOUTPUT X, " ", X == 0.1, " ", FLOAT64(X) == 0.1, " ", X + 0.2'), ['0.1 TRUE FALSE 0.3']);
assert.deepEqual(out('DECLARE X : FLOAT32 <- 16777216.0\nOUTPUT X + 1.0, " ", FLOAT64(X) + 1.0'), ['16777216.0 16777217.0']);
assert.equal(formatFloat32(Math.fround(0.1)), '0.1');
assert.equal(formatFloat32(Math.fround(1 / 3)), '0.33333334');
});
test('exact helpers: FMA rounds once, EXACT shows every digit, ULP and NEXT_AFTER step by one value', () => {
assert.equal(fma(0.1, 10, -1), 5.551115123125783e-17);
assert.equal(0.1 * 10 - 1, 0);
assert.equal(fma(2, 3, 4), 10);
assert.equal(fma(Math.fround(0.1), 10, -1, true), Math.fround(1.4901161193847656e-8));
assert.equal(exactDecimal(0.1), '0.1000000000000000055511151231257827021181583404541015625');
assert.equal(exactDecimal(-2.5), '-2.5');
assert.equal(exactDecimal(1e21), '1000000000000000000000.0');
assert.equal(exactDecimal(Number.MIN_VALUE).length, 1076); // 4.94...e-324 has 1074 digits after the point
assert.equal(nextAfter(1, 2), 1 + Number.EPSILON);
assert.equal(nextAfter(0, -1), -Number.MIN_VALUE);
assert.equal(nextAfter(1, 2, true), 1 + 2 ** -23);
assert.equal(ulp(1), Number.EPSILON);
assert.equal(ulp(Number.MAX_VALUE), 2 ** 971);
assert.equal(ulp(1, true), 2 ** -23);
assert.equal(roundDyadic(3n, -1), 1.5);
assert.equal(roundDyadic((1n << 54n) + 1n, 0), 2 ** 54); // ties and below round to even
assert.equal(wrap(-129, 8, true), 127);
assert.deepEqual(out('OUTPUT FMA(0.1, 10.0, -1.0), " ", ULP(1.0) == EPSILON(FLOAT64), " ", EXACT(0.5)'), ['5.551115123125783e-17 TRUE 0.5']);
});
test('whole numbers bit by bit, two\'s complement for negative values', () => {
assert.deepEqual(out('OUTPUT BIT_AND(-1, 255), " ", BIT_OR(-8, 1), " ", BIT_XOR(5, 3), " ", BIT_NOT(5), " ", BIT_NOT(UINT8(5))'), ['255 -7 6 -6 250']);
assert.deepEqual(out('OUTPUT SHIFT_LEFT(3, 4), " ", SHIFT_RIGHT(-9, 1), " ", SHIFT_RIGHT(UINT8(255), 4), " ", GCD(-12, 18), " ", LCM(6, 8)'), ['48 -5 15 6 24']);
assert.equal(run('DECLARE B : UINT8 <- 200\nOUTPUT SHIFT_LEFT(B, 1)').error?.kind, 'overflow');
});
test('the limits of each type', () => {
assert.deepEqual(out('OUTPUT MAX_VALUE(INT8), " ", MIN_VALUE(INT16), " ", MAX_VALUE(UINT32), " ", MIN_VALUE(UINT64)'), ['127 -32768 4294967295 0']);
assert.deepEqual(out('OUTPUT MAX_VALUE(FLOAT64) > 1e308, " ", EPSILON(FLOAT64), " ", MAX_VALUE(FLOAT32)'), ['TRUE 2.220446049250313e-16 3.4028235e38']);
});
test('domains: without USE IEEE the run stops where IEEE 754 would give NaN or an infinity; with it, the values flow', () => {
for (const [e, kind] of [['SQRT(-1.0)', 'value'], ['LOG(0.0)', 'value'], ['ASIN(2.0)', 'value'], ['1.0 / 0.0', 'divide'], ['MAX_VALUE(FLOAT64) * 2.0', 'overflow'], ['INF()', 'overflow'], ['LOG1P(-1.0)', 'value']]) {
assert.equal(run(`OUTPUT ${e}`).error?.kind, kind, e);
}
assert.deepEqual(out('USE IEEE\nOUTPUT SQRT(-1.0), " ", 1.0 / 0.0, " ", -1.0 / 0.0, " ", IS_NAN(NAN()), " ", NAN() == NAN()'), ['not a number infinity -infinity TRUE FALSE']);
// whole-number division by zero still stops: IEEE 754 is about FLOAT values
assert.equal(run('USE IEEE\nOUTPUT 1 DIV 0').error?.kind, 'divide');
});
test('names: a program may still call its variable E or its function GCD, and its own definition wins', () => {
assert.deepEqual(out('DECLARE E : FLOAT <- 2.0\nOUTPUT E * 2.0, " ", ROUND(E() * 100)'), ['4.0 272']);
assert.deepEqual(out('FUNCTION GCD(A : INTEGER, B : INTEGER) RETURNS INTEGER\n RETURN 7\nENDFUNCTION\nOUTPUT GCD(12, 18)'), ['7']);
assert.ok(errors('DECLARE LENGTH : INTEGER').includes('builtin-name'));
assert.ok(errors('DECLARE INT8 : INTEGER').length > 0);
});
test('the prover: sized arithmetic carries an overflow obligation; FLOAT32 is a function of its own; USE IEEE proves nothing', () => {
const report = (src) => provePseudo(studyOf(src)).units.flatMap((u) => u.obligations.map((o) => ({ ...o, unit: u.name, notes: u.notes })));
const guarded = report('FUNCTION Inc(X : INT8) RETURNS INT8\n REQUIRE X < 127\n RETURN X + 1\nENDFUNCTION\n');
assert.deepEqual(guarded.filter((o) => o.kind === 'overflow').map((o) => o.status), ['proved']);
const open = report('FUNCTION Inc(X : INT8) RETURNS INT8\n RETURN X + 1\nENDFUNCTION\n');
assert.deepEqual(open.filter((o) => o.kind === 'overflow').map((o) => o.status), ['broken']); // the fuzzer tries 127
const narrow = report('FUNCTION N(K : INTEGER) RETURNS UINT8\n REQUIRE 0 <= K AND K < 256\n RETURN K\nENDFUNCTION\n');
assert.deepEqual(narrow.filter((o) => o.kind === 'overflow').map((o) => o.status), ['proved']);
const loop = report('FUNCTION Count(N : UINT8) RETURNS UINT8\n DECLARE C : UINT8 <- 0\n FOR I <- 0 TO N\n INVARIANT C == I\n C <- C + 1\n NEXT I\n RETURN C\nENDFUNCTION\n');
assert.ok(loop.filter((o) => o.kind === 'overflow').every((o) => o.status === 'proved'), JSON.stringify(loop.map((o) => [o.kind, o.status])));
const half = report('FUNCTION Half(X : FLOAT32) RETURNS FLOAT32\n ENSURE RESULT == X / 2.0\n RETURN X / 2.0\nENDFUNCTION\n');
assert.ok(half.find((o) => o.kind === 'ensure').status === 'proved');
const roots = report('FUNCTION R(X : FLOAT) RETURNS FLOAT\n RETURN SQRT(X)\nENDFUNCTION\n');
assert.deepEqual(roots.filter((o) => o.kind === 'argument').map((o) => o.status), ['broken']);
const ieee = report('USE IEEE\nFUNCTION F(X : INTEGER) RETURNS INTEGER\n REQUIRE X > 0\n RETURN 10 DIV X\nENDFUNCTION\n');
assert.ok(ieee.length && ieee.every((o) => o.status !== 'proved'));
assert.ok(ieee[0].notes.some((n) => /USE IEEE/.test(n)));
});
test('the fuzzer reaches the edges of a sized type and generates FLOAT32 values', async () => {
const { checkProperties } = await import('../dist/engines/pseudo/testing.js');
const st = studyOf('FUNCTION F(X : UINT8, Y : FLOAT32) RETURNS BOOL\n ASSERT X != 255\n ASSERT FLOAT32(Y) == Y\n RETURN TRUE\nENDFUNCTION\n');
const fails = checkProperties(st.program.program, 'F', { count: 300 }).failures;
assert.ok(fails.some((f) => f.kind === 'assert' && f.line === 2), 'X = 255 is never tried');
assert.ok(!fails.some((f) => f.line === 3), 'a generated FLOAT32 is not a single');
});
+8
View File
@@ -0,0 +1,8 @@
// A whole number stored in a FLOAT32 is rounded to the nearest single:
// 16777217 becomes 16777216. A rule that kept the INTEGER's value for the
// FLOAT32 would prove this ASSERT, and the run breaks it.
// MUST-NOT-PROVE: assert@8
DECLARE N : INTEGER <- 16777217
DECLARE X : FLOAT32
X <- N
ASSERT X == N
+7
View File
@@ -0,0 +1,7 @@
// With USE IEEE a FLOAT can be NaN, and NaN == NaN is FALSE. The prover's
// rule that every number equals itself does not hold for NaN, so nothing in
// a program that says USE IEEE may be marked proved.
// MUST-NOT-PROVE: assert@7
USE IEEE
DECLARE X : FLOAT <- NAN()
ASSERT X == X
+7
View File
@@ -0,0 +1,7 @@
// INT8 holds -128 to 127. X + 1 for X = 127 is 128, which does not fit, so
// the run stops there. A rule that treated the sum as an exact whole number
// with no range would prove it fits without a REQUIRE to guarantee it.
// MUST-NOT-PROVE: overflow@6
FUNCTION Inc(X : INT8) RETURNS INT8
RETURN X + 1
ENDFUNCTION
+3 -2
View File
@@ -7,7 +7,7 @@ import { studyOf } from '../dist/services/pseudo.js';
import { readLingo } from '../dist/services/lingo.js'; import { readLingo } from '../dist/services/lingo.js';
import { parseSpeco, toAscii, toMath } from '../dist/services/speco.js'; import { parseSpeco, toAscii, toMath } from '../dist/services/speco.js';
import { parseAsm } from '../dist/services/asm.js'; import { parseAsm } from '../dist/services/asm.js';
import { KEYWORDS, BUILTINS } from '../dist/engines/pseudo/lang.js'; import { KEYWORDS, BUILTINS, SOFT_BUILTINS } from '../dist/engines/pseudo/lang.js';
import { OPS } from '../dist/engines/asm86/interp.js'; import { OPS } from '../dist/engines/asm86/interp.js';
test('every pseudocode statement example parses and checks', () => { test('every pseudocode statement example parses and checks', () => {
@@ -109,7 +109,8 @@ test('every pseudo idiom runs and prints what its note says', async () => {
const words = new Set(reserved.entries.map((e) => e.term)); const words = new Set(reserved.entries.map((e) => e.term));
for (const k of KW) assert.ok(words.has(k), `reserved list misses keyword ${k}`); for (const k of KW) assert.ok(words.has(k), `reserved list misses keyword ${k}`);
for (const k of Object.keys(CONTEXTUAL)) assert.ok(words.has(k), `reserved list misses ${k}`); for (const k of Object.keys(CONTEXTUAL)) assert.ok(words.has(k), `reserved list misses ${k}`);
for (const k of Object.keys(BI)) assert.ok(words.has(k), `reserved list misses built-in ${k}`); // the built-ins added for numerical work stay usable as names (a variable E, a FUNCTION GCD), so they are not listed
for (const k of Object.keys(BI)) assert.ok(SOFT_BUILTINS.has(k) ? !words.has(k) : words.has(k), SOFT_BUILTINS.has(k) ? `${k} can be a name, so it is not reserved` : `reserved list misses built-in ${k}`);
}); });
test('every matho example translates to pseudo that checks, and every refusal is refused', () => { test('every matho example translates to pseudo that checks, and every refusal is refused', () => {
+4
View File
@@ -41,6 +41,10 @@ const KNOWN_GAPS = {
'33-stops-div.pseudo': ['differs', 'dividing by zero stops the notation; a Float64 division in Mojo gives nan'], '33-stops-div.pseudo': ['differs', 'dividing by zero stops the notation; a Float64 division in Mojo gives nan'],
'44-parallel-race.pseudo': ['differs', 'the notation detects a data race in PARALLEL FOR; Mojo runs the passes in order and prints a result'], '44-parallel-race.pseudo': ['differs', 'the notation detects a data race in PARALLEL FOR; Mojo runs the passes in order and prints a result'],
'45-parallel-neighbour-race.pseudo': ['differs', 'the notation detects a data race in PARALLEL FOR; Mojo runs the passes in order and prints a result'], '45-parallel-neighbour-race.pseudo': ['differs', 'the notation detects a data race in PARALLEL FOR; Mojo runs the passes in order and prints a result'],
'49-sized-integers.pseudo': ['declined', 'gcd and lcm from std.math are not run by the interpreter yet'],
'50-float32.pseudo': ['gap', 'EXACT has no Mojo spelling: Mojo prints the shortest decimal that rounds back, never the exact value'],
'51-ieee.pseudo': ['declined', 'isinf, isnan and inf[DType.float64]() are not run by the interpreter yet'],
'52-elementary.pseudo': ['declined', 'hypot and the other math functions are not run by the interpreter yet'],
'35-stops-unset.pseudo': ['checker', 'the pseudo reads a variable that was never given a value; the checker reports exactly that, as the compiler would'], '35-stops-unset.pseudo': ['checker', 'the pseudo reads a variable that was never given a value; the checker reports exactly that, as the compiler would'],
'36-stops-counter.pseudo': ['checker', 'the pseudo reads a FOR counter after its loop, where it has no value; the checker reports exactly that'], '36-stops-counter.pseudo': ['checker', 'the pseudo reads a FOR counter after its loop, where it has no value; the checker reports exactly that'],
}; };