tslaws 1.0.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +183 -187
- package/dist/api.d.ts +86 -0
- package/dist/api.js +78 -0
- package/dist/badge.d.ts +111 -0
- package/dist/badge.js +288 -0
- package/dist/browser.d.ts +5 -2
- package/dist/browser.js +403 -173
- package/dist/browserApi.d.ts +305 -0
- package/dist/browserApi.js +456 -0
- package/dist/browserServer.d.ts +13 -0
- package/dist/browserServer.js +406 -0
- package/dist/certificates.js +50 -28
- package/dist/chc.d.ts +13 -1
- package/dist/chc.js +40 -8
- package/dist/check.js +64 -36
- package/dist/cli.d.ts +16 -0
- package/dist/cli.js +78 -1
- package/dist/compile.js +46 -5
- package/dist/coverage.d.ts +17 -0
- package/dist/coverage.js +297 -24
- package/dist/derived.d.ts +2 -0
- package/dist/derived.js +9 -4
- package/dist/generators.d.ts +5 -1
- package/dist/generators.js +50 -4
- package/dist/githubCli.js +88 -15
- package/dist/isolate.d.ts +5 -0
- package/dist/isolate.js +1 -1
- package/dist/lawHost.d.ts +2 -0
- package/dist/lawHost.js +32 -21
- package/dist/lawRuntime.d.ts +6 -0
- package/dist/lawRuntime.js +86 -7
- package/dist/nativeZ3.d.ts +11 -0
- package/dist/nativeZ3.js +162 -0
- package/dist/plan.d.ts +7 -0
- package/dist/plan.js +11 -0
- package/dist/pluginHooks.d.ts +4 -0
- package/dist/pluginHooks.js +13 -4
- package/dist/proofCache.d.ts +24 -0
- package/dist/proofCache.js +135 -0
- package/dist/property.d.ts +2 -2
- package/dist/property.js +33 -8
- package/dist/propertyWorker.js +14 -8
- package/dist/sandbox.d.ts +1 -1
- package/dist/sandbox.js +48 -8
- package/dist/seal.js +47 -8
- package/dist/smt.d.ts +16 -2
- package/dist/smt.js +241 -31
- package/dist/symbolic.d.ts +38 -3
- package/dist/symbolic.js +504 -47
- package/dist/types.d.ts +9 -0
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -1,13 +1,23 @@
|
|
|
1
1
|
# tslaws
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/tslaws)
|
|
4
|
+
[](https://github.com/Jordan-Hall/tsc-laws/actions/workflows/ci.yml)
|
|
5
|
+
[](https://github.com/Jordan-Hall/tsc-laws/blob/main/LICENSE)
|
|
6
|
+
[](https://github.com/Jordan-Hall/tsc-laws/blob/main/docs/certificates/README.md)
|
|
7
|
+
[](https://github.com/Jordan-Hall/tsc-laws/blob/main/docs/certificates/README.md)
|
|
8
|
+
|
|
9
|
+
Laws for TypeScript. A person writes `law` declarations, an agent writes the code, and one command says whether the code
|
|
10
|
+
satisfies every law and whether any exported code is left unchecked.
|
|
11
|
+
|
|
12
|
+
## Install and quick start
|
|
13
|
+
|
|
3
14
|
```sh
|
|
4
15
|
npm i -D tslaws # also installs the compiler for your platform (tslaws-compiler-<platform>)
|
|
5
|
-
npx tslaws init
|
|
16
|
+
npx tslaws init # tslaws.json, agent rules in AGENTS.md, a Claude Code hook, a starter law; never writes the lock
|
|
17
|
+
npx tslaws accept # a person, at a terminal: records the laws in tsc-laws.lock
|
|
18
|
+
npx tslaws check # exit 0 only when everything holds
|
|
6
19
|
```
|
|
7
20
|
|
|
8
|
-
Laws for TypeScript, checked the way Bend 2 checks its `LAWS.bend`: a person writes the laws, an agent writes the code,
|
|
9
|
-
and one command says whether the code satisfies every law and whether any code is left unchecked.
|
|
10
|
-
|
|
11
21
|
```ts
|
|
12
22
|
// src/math.laws.ts
|
|
13
23
|
import type { int } from 'tslaws';
|
|
@@ -27,212 +37,198 @@ export law sortOrders(xs: int[]) {
|
|
|
27
37
|
}
|
|
28
38
|
```
|
|
29
39
|
|
|
30
|
-
`law` is syntax of the `tsc-laws` compiler
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
ordinary identifier.
|
|
40
|
+
`law` is syntax of the `tsc-laws` compiler, a fork of [tsc-ts](https://github.com/vercel-labs/tsc-ts) that `tslaws`
|
|
41
|
+
installs. A law is a function declaration the compiler
|
|
42
|
+
type-checks like any other. It holds for an input when it returns anything but `false` and does not throw. `assume(cond)`
|
|
43
|
+
skips inputs a law does not cover. Everywhere except at the start of a statement, `law` is still an ordinary identifier.
|
|
44
|
+
|
|
45
|
+
Prefer plain TypeScript with no custom syntax? Import `law` from `tslaws` and write `law('name', (x: int) => ...)` or
|
|
46
|
+
`law.proved('name', ...)` — every feature (refinement types, `induct`, `calc`, contracts, browser laws, and proofs) works
|
|
47
|
+
identically with stock TypeScript editors, linters, and bundlers.
|
|
48
|
+
|
|
49
|
+
## Output
|
|
34
50
|
|
|
35
51
|
```
|
|
36
52
|
$ tslaws check
|
|
37
|
-
PROVED
|
|
38
|
-
|
|
39
|
-
BOUNDED
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
53
|
+
PROVED clampWithinBounds (src/math.laws.ts:5) [smt]
|
|
54
|
+
assuming: number parameters exclude NaN and ±Infinity
|
|
55
|
+
BOUNDED sortOrders (src/math.laws.ts:11) [smt]
|
|
56
|
+
holds within bounds: int values stay within ±2^26; arrays hold at most 4 elements; loops run at most 4 times
|
|
57
|
+
hint: Proved only within bounds (...). Passing; no action needed unless a person wants a full proof.
|
|
58
|
+
FAILED sortKeepsLength (src/math.laws.ts:17) [property]
|
|
59
|
+
returned false
|
|
60
|
+
counterexample: [[3,3]]
|
|
61
|
+
reproduce with --seed 1090953994
|
|
62
|
+
hint: The code breaks this law. The counterexample is [[3,3]]. Fix the code, not the law, then rerun. ...
|
|
45
63
|
UNCOVERED sub (src/math.ts) [coverage]
|
|
46
|
-
|
|
64
|
+
no law reaches this exported function; add a law, or a person can mark it @tslaws-exempt and accept
|
|
65
|
+
hint: No law reaches this exported code. Ask a person for a law, ...
|
|
66
|
+
|
|
67
|
+
4 laws: 1 proved, 1 bounded, 0 tested, 0 open, 1 failed, 0 tampered, 0 errors
|
|
47
68
|
```
|
|
48
69
|
|
|
49
|
-
|
|
70
|
+
Each result is `STATUS name (file:line) [method]`, then its detail lines. Every result that is not `proved` ends with a
|
|
71
|
+
`hint:`, the next action for an agent. The last line counts the results. `--summary` prints only the results that do not
|
|
72
|
+
pass, and `--reporter text|json|junit|github|ndjson` (or `--json`) picks another format: `junit` is JUnit XML, `github`
|
|
73
|
+
prints workflow annotations and appends a table to `GITHUB_STEP_SUMMARY`, and `ndjson` streams one JSON event per line for
|
|
74
|
+
editors.
|
|
75
|
+
|
|
76
|
+
| Status | Meaning |
|
|
77
|
+
| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
78
|
+
| `proved` | Z3 shows no input makes the law return `false`, with no bound involved. |
|
|
79
|
+
| `bounded` | Proved for every input within stated bounds: array length (`--bound`, default 4), loop iterations, recursion depth, `int` values within ±2^26. |
|
|
80
|
+
| `tested` | Property tests found no counterexample in `--runs` inputs (default 200); the code does not lower to the prover. Not a proof. |
|
|
81
|
+
| `open` | Declared `law proved …` but not proved for every input (a bounded proof does not count). Fails the check. |
|
|
82
|
+
| `failed` | A counterexample, shrunk to a minimal input, with the seed to reproduce it. |
|
|
83
|
+
| `uncovered` | Exported code no law reaches. Fails the check. |
|
|
84
|
+
| `tampered` | A law changed, was added or removed since `tsc-laws.lock` was accepted, or a coverage exemption is not in it. Fails the check. |
|
|
85
|
+
| `error` | The law cannot run: not exported, a parameter has no type, no generator for a type, a load failure, or a Z3 crash (see below). |
|
|
86
|
+
|
|
87
|
+
`tslaws check` exits 0 only when every law is `proved`, `bounded` or `tested`, nothing is `uncovered`, the files compile
|
|
88
|
+
and the lock matches. Inputs are generated from each parameter's type; register a generator for a type the runner cannot
|
|
89
|
+
derive with `arbitrary('UserId', fc.string(...))` (`arbitrary` and `fc` come from `tslaws`).
|
|
90
|
+
|
|
91
|
+
## Coverage
|
|
92
|
+
|
|
93
|
+
Every exported function or class in the covered modules must be reached by a law, directly or through the functions the
|
|
94
|
+
law calls. The covered modules are the `cover` globs of `tslaws.json`
|
|
95
|
+
(`{ "cover": ["src/**/*.ts", "!src/**/*.test.ts"] }`), or, without that file, every project module the laws import.
|
|
96
|
+
Coverage follows ES `import`s as well as `require`, including the interop helpers a compiler wraps around them
|
|
97
|
+
(`__importStar`, `__importDefault`, `__toESM`), directory index imports (`./dir` -> `./dir/index.js`), barrel re-exports,
|
|
98
|
+
and files transformed by `bundlerPlugins`, so code you import with `import x from './y'` counts as reached. Mark
|
|
99
|
+
code that needs no law `@tslaws-exempt`; a person accepts the exemption into the lock.
|
|
100
|
+
|
|
101
|
+
## Levels
|
|
102
|
+
|
|
103
|
+
A laws file's level comes from its name: `*.browser.laws.ts` runs in a real browser (Chrome, Edge or Chromium, or
|
|
104
|
+
`--browser firefox|webkit`) against a live DOM, `*.e2e.laws.ts` drives your running app, and any other `*.laws.ts` runs in
|
|
105
|
+
Node. Set `"levels": "imports"` in `tslaws.json` and the level comes from the file's imports instead (`tslaws/browser`,
|
|
106
|
+
`tslaws/adapters/*`, `tslaws/e2e`). Browser laws run in parallel isolated `BrowserContext`s on a real local HTTP origin
|
|
107
|
+
(top-level pages by default, or `--browser-mode iframe`, plus `--headed` for interactive debugging), execute `fast-check`
|
|
108
|
+
in-browser to eliminate per-input IPC, and provide a built-in real-browser testing API:
|
|
109
|
+
chainable, auto-waiting Playwright-backed `page` locators (`page.getByRole`, `page.getByText`, `page.getByLabelText`,
|
|
110
|
+
`page.getByPlaceholder`, `page.getByTestId`, `page.locator`, `.filter()`, `.first()`, `.nth()`, `page.viewport()`,
|
|
111
|
+
`page.screenshot()`), auto-retrying `expectElement(locator)` assertions (`.toBeVisible()`, `.toHaveTextContent()`,
|
|
112
|
+
`.toHaveValue()`, `.toBeChecked()`, `.toHaveAttribute()`, `.toHaveCount()`, `.not.*`), trusted `userEvent` interactions
|
|
113
|
+
(`userEvent.click`, `userEvent.fill`, `userEvent.type`, `userEvent.keyboard`, `userEvent.tab`, `userEvent.selectOptions`,
|
|
114
|
+
`userEvent.check`, `userEvent.dragAndDrop`), `waitFor`, and `mock(urlPattern, response)` network interception. Browser laws are `tested`, never `proved`. Node laws run in a
|
|
115
|
+
permission sandbox (Node 22.15 or later; `--no-sandbox` opts out), and an end-to-end law needs `--allow-e2e` because it
|
|
116
|
+
cannot run inside it. `tslaws list` shows each laws file, its level and whether it is accepted.
|
|
117
|
+
|
|
118
|
+
## The lock
|
|
119
|
+
|
|
120
|
+
`tsc-laws.lock` holds a hash of every law and the list of coverage exemptions. A law's hash covers its declaration, the
|
|
121
|
+
helpers and generators it reaches, every module its file imports, and the effective `tslaws.json` apart from `github`,
|
|
122
|
+
`security` and `levels`, so weakening a helper reads as tampering too. Comments and layout do not.
|
|
123
|
+
|
|
124
|
+
- A change, an added law, a removed law or a new `@tslaws-exempt` fails the check until a person runs `tslaws accept`. It
|
|
125
|
+
refuses to run without a terminal, under `CI`, or with `TSLAWS_AGENT` set, unless a person lent it with
|
|
126
|
+
`tslaws delegate grant [--for 1h|8h|1d|7d]` (revoke with `delegate revoke`; never under `CI`).
|
|
127
|
+
- Agents propose laws in `*.proposed.laws.ts` files (or with `tslaws propose` or the MCP tool) and try them with
|
|
128
|
+
`tslaws check --proposed`. A person moves one into the tree with `tslaws promote`, then accepts.
|
|
129
|
+
- `tslaws mcp` serves check, explain, list and propose to agents over MCP. It cannot accept or touch the lock.
|
|
130
|
+
- `hooks/claude-hook.mjs` is a Claude Code hook that blocks `git commit` while the check fails; `tslaws init` wires it up.
|
|
131
|
+
|
|
132
|
+
## Certificates
|
|
133
|
+
|
|
134
|
+
```sh
|
|
135
|
+
npx tslaws check --certify # or "certify": true in tslaws.json
|
|
136
|
+
npx tslaws check --require-certify # a proof that cvc5 and Carcara cannot certify fails the check
|
|
137
|
+
```
|
|
50
138
|
|
|
51
|
-
|
|
139
|
+
For a law Z3 proved, cvc5 writes a proof and Carcara checks it, so a proof is more than a solver's say-so. Loops are
|
|
140
|
+
certified through their `invariant`s. Install `cvc5` and `carcara` on the PATH (or set `TSLAWS_CVC5` and `TSLAWS_CARCARA`);
|
|
141
|
+
without them a certificate is `unavailable`, which fails the check only with `--require-certify`. Real certificates, with
|
|
142
|
+
the command that checks each, are in the
|
|
143
|
+
[certificate gallery](https://github.com/Jordan-Hall/tsc-laws/blob/main/docs/certificates/README.md).
|
|
52
144
|
|
|
53
|
-
|
|
54
|
-
2. **No code escapes the laws.** Every exported function or class in the covered modules is reached by a law, directly or
|
|
55
|
-
through the functions the law calls. Covered modules are the `cover` globs of `tslaws.json`
|
|
56
|
-
(`{ "cover": ["src/**/*.ts", "!src/**/*.test.ts"] }`), or, without that file, every project module the laws import.
|
|
145
|
+
### Badges
|
|
57
146
|
|
|
58
|
-
|
|
147
|
+
```sh
|
|
148
|
+
npx tslaws badge --certify
|
|
149
|
+
```
|
|
59
150
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
| `open` | Declared `law proved …` but not proved for every input (a bounded proof does not count), or `assume` discards nearly every input. Fails the check. |
|
|
66
|
-
| `failed` | A counterexample, shrunk to a minimal input, with the seed to reproduce it. |
|
|
67
|
-
| `uncovered` | Exported code no law reaches. Fails the check. |
|
|
68
|
-
| `tampered` | A law changed, was added or removed since `tsc-laws.lock` was accepted, or a coverage exemption is not in it. Fails the check. |
|
|
69
|
-
| `error` | The law cannot run: not exported, a parameter has no type, no generator for a type, load failure. |
|
|
151
|
+
`tslaws badge [files...] [--out DIR] [--report FILE] [--repo OWNER/NAME] [--branch NAME] [--link URL] [--certify]` checks
|
|
152
|
+
the project (or reads a `check --json` report from `--report`), writes the shields.io endpoint files `badge-laws.json` and
|
|
153
|
+
`badge-certificates.json` into `badges/` (default; commit them), and prints the markdown that shows them. When a law is
|
|
154
|
+
certified it also writes `badges/README.md`, a page of your laws, their status and proof files, which the badges link to.
|
|
155
|
+
See [Proof certificates](https://github.com/Jordan-Hall/tsc-laws/blob/main/docs/certificates.md#badges).
|
|
70
156
|
|
|
71
|
-
|
|
72
|
-
only: `number` parameters exclude NaN and ±Infinity, `int` parameters stay within ±(2^53 − 1), and `string` parameters use
|
|
73
|
-
only characters up to U+FFFF with a length cap. They show as `assuming: …` in text output, as `assumptions` in JSON and
|
|
74
|
-
ndjson, and in the step-summary Detail cell and the `::notice` of a `bounded` result. A law whose parameters need none
|
|
75
|
-
(`boolean`, `bigint`) prints as before.
|
|
157
|
+
## GitHub integration
|
|
76
158
|
|
|
77
|
-
`tslaws
|
|
78
|
-
|
|
159
|
+
`tslaws github report|command|merge|verify` accepts laws on GitHub (CI only; configure the `github` section of
|
|
160
|
+
`tslaws.json`). A pull request that adds, changes or removes a law is a proposal: `report` runs each one and comments with
|
|
161
|
+
what it would be if accepted, `command` records `/tslaws accept` comments, and `merge` writes `tsc-laws.lock` on your
|
|
162
|
+
branch after the merge, or on demand from `workflow_dispatch`.
|
|
79
163
|
|
|
80
|
-
|
|
164
|
+
The merge fast path: `merge` compares the laws on the merged code with the lock first, and runs only the laws the lock
|
|
165
|
+
does not record (new laws, edited laws, and laws whose dependencies changed). When the lock already records every law,
|
|
166
|
+
which is the usual merge, no law runs and nothing is written. Nothing from the pull request counts as proof; the merge
|
|
167
|
+
re-proves each law it runs. See
|
|
168
|
+
[Accepting laws on GitHub](https://github.com/Jordan-Hall/tsc-laws/blob/main/docs/github.md).
|
|
81
169
|
|
|
82
|
-
|
|
83
|
-
generation: primitives and literals, unions, enums, template literals, arrays, tuples, interfaces and type literals (optional
|
|
84
|
-
properties included), index signatures, `Date`, and type parameters through their constraints. Parameters typed `number` are
|
|
85
|
-
finite doubles (no NaN, no infinities). Register a generator for a type the runner cannot derive, by its name:
|
|
170
|
+
## Bundler plugins
|
|
86
171
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
172
|
+
`tslaws.json` `bundlerPlugins` lists esbuild plugin modules (paths relative to the file) that teach tslaws source formats
|
|
173
|
+
Node and the browser bundler cannot read, such as `.tsrx`, `.svelte` or `.vue`. Browser laws bundle through them. Node
|
|
174
|
+
laws use them too: project source that imports such a file loads it through the plugin, and a relative import from that
|
|
175
|
+
file to a project `.ts` module resolves to the compiled twin in `.tslaws/out`, the same copy the law's own imports get.
|
|
176
|
+
See [Bundler plugins](https://github.com/Jordan-Hall/tsc-laws/blob/main/docs/laws.md#bundler-plugins).
|
|
91
177
|
|
|
92
178
|
## The prover
|
|
93
179
|
|
|
94
|
-
`tslaws` symbolically executes the compiled JavaScript of a law, inlining the project functions it calls
|
|
95
|
-
|
|
96
|
-
reported as `failed`.
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
invariant } from 'tslaws'`; it throws at runtime when false, so property tests check it on every iteration). The prover
|
|
116
|
-
proves the invariant on entry, assumes it at the top of an arbitrary iteration, proves it again at the end, and gives the
|
|
117
|
-
code after the loop an arbitrary state where the invariant holds and the condition is false. No unrolling, so no bound:
|
|
118
|
-
|
|
119
|
-
```ts
|
|
120
|
-
export function sumTo(n: int): int {
|
|
121
|
-
let s = 0,
|
|
122
|
-
i = 0;
|
|
123
|
-
while (i < n) {
|
|
124
|
-
invariant(0 <= i && i <= n);
|
|
125
|
-
invariant(2 * s === i * (i - 1));
|
|
126
|
-
s += i;
|
|
127
|
-
i += 1;
|
|
128
|
-
}
|
|
129
|
-
return s as int;
|
|
130
|
-
}
|
|
131
|
-
// law proved: 2 * sumTo(n) === n * (n - 1), for every n >= 0
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
Invariants live in the implementation, so the agent writes them; the laws stay the person's. Restrictions: the loop may
|
|
135
|
-
change numbers, booleans and strings but only read arrays and records, and may not `return`, `break` or `continue`.
|
|
136
|
-
This is partial correctness (a loop that never ends is not flagged).
|
|
137
|
-
|
|
138
|
-
Anything else leaves the law `tested`, or a `law proved` `open`, with the reason. Z3 runs in a worker thread that is terminated if a query outlives its timeout, so a hard query cannot hang the check; property tests run in a worker too, and a law that outlives `--property-timeout` (default 30000 ms) is reported `error`: the law or the code it calls may not terminate. `TSLAWS_DEBUG=1` keeps each query as an
|
|
139
|
-
`.smt2` file under `.tslaws/out/smt` to replay in `z3`.
|
|
140
|
-
|
|
141
|
-
## Browser laws
|
|
142
|
-
|
|
143
|
-
A laws file that imports `tslaws/browser` (directly or through a helper module of yours) runs in a real browser (Chrome, Edge or Chromium; `TSLAWS_BROWSER` names another
|
|
144
|
-
executable). Node generates and shrinks the inputs; each one is sent to a page where the compiled law and your code run
|
|
145
|
-
against a live DOM. The law may be `async`. `tslaws/browser` has `mount`, `settle`, `click`, `type` and `fire`, and works with
|
|
146
|
-
any framework or none: `mount(el => render(el))` accepts whatever renders into an element and may return its unmount.
|
|
147
|
-
`a11y` and `accessible` audit a mounted component with the optional `axe-core` peer dependency, and `--browser firefox|webkit` picks another engine (`npx playwright install firefox`).
|
|
148
|
-
`mountComponent` with the React, Angular, DOM and custom-element adapters tests one component in isolation with props generated from its props type (function-typed props included), plus `spy`, `rendersSame` and Testing Library style queries.
|
|
149
|
-
Browser laws are `tested`, never `proved`. See [docs/laws.md](../../docs/laws.md#browser-laws).
|
|
150
|
-
|
|
151
|
-
A laws file that imports `tslaws/e2e` instead is an end-to-end law (a file that reaches both is an error; by default the level comes from the name, `.browser.laws.ts` or `.e2e.laws.ts`, and `"levels": "imports"` in `tslaws.json` makes the imports decide instead): it runs in Node and drives your whole running app through a real browser (`page()`, `open`, `newPage`, `route`, `settle`, `accessible`). Configure `e2e.server` in `tslaws.json` and the runner starts and stops your dev server. See [End-to-end laws](../../docs/laws.md#end-to-end-laws).
|
|
152
|
-
|
|
153
|
-
- **Frameworks:** `tslaws.json` `compilerOptions` (`jsx`, `jsxFactory`, `jsxFragmentFactory`, `jsxImportSource`, `experimentalDecorators`,
|
|
154
|
-
`emitDecoratorMetadata`, `useDefineForClassFields`, `lib`, `types`, `strict`, ...) and `.tsx` sources let React, Angular and any DOM
|
|
155
|
-
library be tested. See [docs/laws.md](../../docs/laws.md#frameworks).
|
|
156
|
-
- **Stateful laws:** a law over `ops: Op[]` (a union type) can use `runModel` or `runModelAsync` to apply the operations to the
|
|
157
|
-
real thing and to a simple model and compare them after every step. Failing sequences shrink to the shortest one. See
|
|
158
|
-
[docs/laws.md](../../docs/laws.md#stateful-laws).
|
|
159
|
-
|
|
160
|
-
## Dashboard
|
|
161
|
-
|
|
162
|
-
`tslaws ui [files...] [--port N] [--no-open]` serves a local dashboard (127.0.0.1 only): a tree of files and laws,
|
|
163
|
-
live progress, a live view of the browser page during browser laws, status filters, search, a report with a copyable reproduce command, the law's source, and live reruns on save.
|
|
164
|
-
Laws are timed (the five slowest are listed), the last 20 runs show as a row of coloured squares, laws that flip between passing and failing are marked `flaky`, and any single law can be rerun.
|
|
165
|
-
See [docs/laws.md](../../docs/laws.md#the-dashboard).
|
|
166
|
-
|
|
167
|
-
`tslaws mutate [files...] [--max N] [--seed N] [--json]` mutation-tests the laws: it mutates the covered code, re-runs the laws against each mutant, and lists the survivors no law caught. It handles `.tsx`/`.jsx` and projects with a lock, and takes the run flags of `check`. See [docs/laws.md](../../docs/laws.md#mutation-testing).
|
|
168
|
-
|
|
169
|
-
- **Other source formats:** `tslaws.json` `bundlerPlugins` lists esbuild plugin modules (paths relative to the file) that teach the browser bundler formats like `.tsrx`. See [docs/laws.md](../../docs/laws.md#bundler-plugins).
|
|
170
|
-
- **Bundler-style source:** `import.meta.env` works in laws, in the code they reach and in browser pages (`tslaws.json` `env` adds or overrides entries), and a TypeScript dependency may import its own files without extensions. See [docs/laws.md](../../docs/laws.md#source-written-for-a-bundler).
|
|
171
|
-
- **Settings:** `tslaws.json` `runs`, `bound`, `proofTimeoutMs` and `propertyTimeoutMs` set the run defaults; a command line flag beats them. `"solvers": ["z3", "cvc5"]` makes cvc5 confirm every Z3 proof (set `TSLAWS_CVC5` to its path, or put it on the PATH). See [docs/laws.md](../../docs/laws.md#settings).
|
|
172
|
-
|
|
173
|
-
## Agents cannot move the goalposts
|
|
174
|
-
|
|
175
|
-
`tsc-laws.lock` holds a hash of every law and the list of coverage exemptions. A law's hash covers its declaration, the helpers,
|
|
176
|
-
generators and top-level code of its file that it reaches, every module the file imports (a registry package by name and
|
|
177
|
-
version), and the whole effective `tslaws.json` apart from `github`, `security` and `levels`, so weakening a helper or setting
|
|
178
|
-
`runs: 1` reads as tampering too; comments, layout and edits no law reaches do not. A change, an added law, a removed
|
|
179
|
-
law or a new `@tslaws-exempt` fails the check until a person runs `tslaws accept`, which refuses to run without a terminal,
|
|
180
|
-
under `CI`, or with `TSLAWS_AGENT` set, unless a person has lent it for a limited time with `tslaws delegate grant`. Agents propose laws in `*.proposed.laws.ts` files (or optionally in `.tslaws/proposals/`, outside the tree and git-ignored; `tslaws propose` or the MCP tool) and try them with `tslaws check
|
|
181
|
-
--proposed` (no lock, no coverage gate); a person moves one into the tree with `tslaws promote` and accepts. Every laws file in the tree must be in the lock. A project that accepts laws on GitHub (a `github` section in `tslaws.json`) still lets a person run `tslaws accept` by hand, and a maintainer can run the workflow on demand; set `github.local` to false to leave the lock to the workflow alone. See [docs/github.md](../../docs/github.md).
|
|
182
|
-
|
|
183
|
-
`hooks/claude-hook.mjs` is a Claude Code hook that blocks `git commit` and sends an agent back to work when the check fails;
|
|
184
|
-
`hooks/claude-settings.example.json` wires it up and `hooks/AGENTS.snippet.md` tells the agent the rules.
|
|
180
|
+
`tslaws` symbolically executes the compiled JavaScript of a law, inlining the project functions it calls (including through
|
|
181
|
+
directory imports and barrel re-exports), and asks Z3 for an input that returns `false`; only a failure it can replay in
|
|
182
|
+
JavaScript is reported as `failed`. `int` is an exact whole number and `bigint` is exact with no bound. Built-in refinement types — `nat`, `posInt`, `nonZeroInt`, `uint8`, `uint16`, `uint32`, `int32`, `intRange<Lo, Hi>` (and
|
|
183
|
+
user aliases like `type Port = intRange<1, 65535>`), `nonEmpty<T>`, `nonEmptyString`, `asciiString`, and `finite` —
|
|
184
|
+
narrow both generated property inputs and SMT assumptions at the type level. Arrays, loops and recursion unroll up to
|
|
185
|
+
`--bound` (a `bounded` result); `invariant(cond)` first in a loop body proves the loop for every iteration count,
|
|
186
|
+
`decreases(measure)` checks loop and recursion termination, `induct(n, k => P(k))` proves natural-number claims by
|
|
187
|
+
induction, `calc(a, b, c)` and `lemma(cond)` split larger proofs into proved subgoals, `requires(cond)` / `ensures(cond)`
|
|
188
|
+
check caller preconditions and postconditions, `unreachable()` proves dead branches impossible, and
|
|
189
|
+
`implies`/`iff`/`distinct` lower directly to SMT. Automatic vacuity detection ensures unsatisfiable `assume()`
|
|
190
|
+
preconditions are caught as `open` rather than falsely proved. What does not lower stays `tested`, or a `law proved` is
|
|
191
|
+
`open`, with the reason. `TSLAWS_DEBUG=1` keeps each query as an `.smt2` file under `.tslaws/out/smt`.
|
|
192
|
+
|
|
193
|
+
When native `z3` is installed on the PATH (or via `TSLAWS_Z3`), `tslaws` uses a persistent pooled native Z3 backend
|
|
194
|
+
(opt out with `TSLAWS_Z3=wasm`) and caches proved SMT results on disk in `.tslaws/proof-cache.json` keyed by query hash
|
|
195
|
+
and solver fingerprint so unchanged checks return in milliseconds.
|
|
196
|
+
|
|
197
|
+
If the Z3 worker crashes (it throws, runs out of memory or exits), the query is retried twice on a fresh worker. If every
|
|
198
|
+
try crashes, the law is an `error` whose message says `the Z3 solver crashed` and why. It is never quietly downgraded to
|
|
199
|
+
a bounded proof or a test. A solver that gives up or runs out of time still falls back to a bounded proof or to tests,
|
|
200
|
+
and says so.
|
|
185
201
|
|
|
186
202
|
## Commands
|
|
187
203
|
|
|
188
204
|
```
|
|
189
205
|
tslaws init [--force]
|
|
206
|
+
tslaws check [files...] [--reporter ...] [--summary] [--watch] [--certify] [--require-certify] [--runs N] [--seed N] [--bound N] [--browser-mode page|iframe] [--headed] [--proposed] ...
|
|
190
207
|
tslaws accept [files...]
|
|
191
|
-
tslaws delegate grant
|
|
192
|
-
tslaws
|
|
193
|
-
tslaws
|
|
194
|
-
tslaws
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
`.claude/settings.json`, and a starter law if none exists. It never writes the lock; a person then runs `accept`.
|
|
202
|
-
- `accept` records the laws and the `@tslaws-exempt` functions in `tsc-laws.lock`. It refuses to run without a terminal,
|
|
203
|
-
under `CI`, or with `TSLAWS_AGENT` set, unless a live delegation covers the project (never under `CI`).
|
|
204
|
-
- `delegate grant` lends `accept` to agents for 1h, 8h (default), 1d or 7d (the most), for one project root. `delegate revoke`
|
|
205
|
-
withdraws it and `delegate status` lists grants; `grant` and `revoke` need a terminal and refuse under `CI` or
|
|
206
|
-
`TSLAWS_AGENT`. The grant lives in `~/.tslaws/delegation.json`, outside the repository (found through the operating
|
|
207
|
-
system's user record, not `HOME`; an agent running as the same user can still write that file). A delegated `accept` appends to `.tslaws/accept.log` and prints `DELEGATED ACCEPT`.
|
|
208
|
-
A grant is a person's decision, bounded in time, outside the repo, audited in `accept.log`. See [docs/laws.md](../../docs/laws.md#lending-accept-to-an-agent).
|
|
209
|
-
- `check` exits 0 only when every law is `proved`, `bounded` or `tested`, nothing is `uncovered`, the files compile and the
|
|
210
|
-
lock matches. `--json` adds a `hint` to each result, the concrete next action for an agent. `--summary` prints only the
|
|
211
|
-
results that do not pass, plus the totals. `--watch` re-runs on every `.ts` change.
|
|
212
|
-
- `check --reporter text|json|junit|github` picks the output: `junit` is JUnit XML for CI test reports, `github` prints workflow
|
|
213
|
-
annotations and appends a table to `GITHUB_STEP_SUMMARY`. See "CI reporters" in `docs/laws.md`.
|
|
214
|
-
|
|
215
|
-
- `tslaws mcp` serves `tslaws_check`, `tslaws_check_proposed`, `tslaws_explain`, `tslaws_list` and `tslaws_propose` over MCP (stdio) for
|
|
216
|
-
agents; it cannot accept laws, promote a proposal or touch the lock. `tslaws promote <proposal> [--to PATH]` is the person's step that moves a proposal into the
|
|
217
|
-
tree; `tslaws propose <file.ts>` writes one; `tslaws list [--json]` says which files are laws files, at what level, and whether each is accepted. See [docs/laws.md](../../docs/laws.md#agent-integration-mcp).
|
|
218
|
-
- Counterexamples are kept in `.tslaws/regressions` (committable) and replayed before the random search, so a fixed bug cannot
|
|
219
|
-
return unnoticed. `check --no-regressions` skips the corpus; `tslaws regressions [list|clear]` shows or deletes it.
|
|
220
|
-
|
|
221
|
-
Without files, every `*.laws.ts` under the current directory is used, and every file the `"laws"` globs of `tslaws.json` pick that declares a law. The compiler is `--compiler`, `TSC_LAWS_BIN`, or
|
|
222
|
-
`tsc-laws` on `PATH`; it finds its lib files through `TSC_TS_LIB_DIR`, or the TypeScript 7 libs scriptc installs in
|
|
223
|
-
`node_modules/@scriptc/*`.
|
|
224
|
-
|
|
225
|
-
See [docs/laws.md](../../docs/laws.md) for the language, how it compares with Bend 2, and the limits.
|
|
226
|
-
|
|
227
|
-
## Build and test
|
|
228
|
-
|
|
229
|
-
```
|
|
230
|
-
npm ci
|
|
231
|
-
npm run fetch:upstream && npm run build:native:dev -w tsc-ts # needs clang (and zig on Windows)
|
|
232
|
-
npm run build -w tslaws
|
|
233
|
-
TSC_LAWS_BIN=packages/tsc-ts/dist/tsc npm test -w tslaws
|
|
234
|
-
TSC_TS_BIN=packages/tsc-ts/dist/tsc node --test packages/tsc-ts/tests/laws.test.mjs
|
|
208
|
+
tslaws delegate grant|revoke|status
|
|
209
|
+
tslaws propose <file.ts> tslaws promote <proposal> tslaws list [--json]
|
|
210
|
+
tslaws badge [files...] [--certify]
|
|
211
|
+
tslaws github report|command|merge|verify
|
|
212
|
+
tslaws ui [files...] [--port N] [--no-open] [--tab]
|
|
213
|
+
tslaws mutate [files...] [--max N] [--seed N] [--json]
|
|
214
|
+
tslaws regressions [list|clear]
|
|
215
|
+
tslaws security deps [--offline] [--fail-on LEVEL]
|
|
216
|
+
tslaws migrate <source> [files...] [--write]
|
|
217
|
+
tslaws mcp
|
|
235
218
|
```
|
|
236
219
|
|
|
237
|
-
`
|
|
238
|
-
|
|
220
|
+
Run `tslaws --help` for every flag. `tslaws ui` opens a dashboard (tree, filters, counterexamples, source, live
|
|
221
|
+
reruns on save). `tslaws mutate` changes your code in small ways and lists the changes no law notices. Counterexamples are
|
|
222
|
+
kept in `.tslaws/regressions` and replayed first on every later run. In a monorepo, run from the root and each project
|
|
223
|
+
checks with its own config and lock (`--project`, `--filter`, `--changed`).
|
|
224
|
+
|
|
225
|
+
## Docs
|
|
226
|
+
|
|
227
|
+
- [Laws for TypeScript](https://github.com/Jordan-Hall/tsc-laws/blob/main/docs/laws.md): the language, levels, the prover, settings
|
|
228
|
+
- [Proof certificates](https://github.com/Jordan-Hall/tsc-laws/blob/main/docs/certificates.md) and the [gallery](https://github.com/Jordan-Hall/tsc-laws/blob/main/docs/certificates/README.md)
|
|
229
|
+
- [Accepting laws on GitHub](https://github.com/Jordan-Hall/tsc-laws/blob/main/docs/github.md)
|
|
230
|
+
- [tslaws in a monorepo](https://github.com/Jordan-Hall/tsc-laws/blob/main/docs/monorepo.md)
|
|
231
|
+
- [Security laws](https://github.com/Jordan-Hall/tsc-laws/blob/main/docs/security-laws.md)
|
|
232
|
+
- [Editor law syntax](https://github.com/Jordan-Hall/tsc-laws/blob/main/docs/editor-law-syntax.md)
|
|
233
|
+
- [Build from source](https://github.com/Jordan-Hall/tsc-laws#readme): clone the repository and follow its build steps
|
|
234
|
+
- [Changelog](https://github.com/Jordan-Hall/tsc-laws/blob/main/CHANGELOG-LAWS.md)
|
package/dist/api.d.ts
CHANGED
|
@@ -7,6 +7,50 @@ export { fc };
|
|
|
7
7
|
export type int = number & {
|
|
8
8
|
readonly __tslawsInt: true;
|
|
9
9
|
};
|
|
10
|
+
/** A non-negative exact integer (`n >= 0`). Generated and proved in `[0, MAX_SAFE_INTEGER]`. */
|
|
11
|
+
export type nat = int & {
|
|
12
|
+
readonly __tslawsNat: true;
|
|
13
|
+
};
|
|
14
|
+
/** A strictly positive exact integer (`n >= 1`). Generated and proved in `[1, MAX_SAFE_INTEGER]`. */
|
|
15
|
+
export type posInt = int & {
|
|
16
|
+
readonly __tslawsPosInt: true;
|
|
17
|
+
};
|
|
18
|
+
/** A non-zero exact integer (`n !== 0`). Generated and proved with `n !== 0`. */
|
|
19
|
+
export type nonZeroInt = int & {
|
|
20
|
+
readonly __tslawsNonZeroInt: true;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* An exact integer bounded between `Lo` and `Hi` inclusive (like a LiquidHaskell refinement type): fast-check generates
|
|
24
|
+
* directly inside `[Lo, Hi]` without wasted `assume()` skips, and Z3 assumes `Lo <= x <= Hi` automatically.
|
|
25
|
+
*/
|
|
26
|
+
export type intRange<Lo extends number, Hi extends number> = int & {
|
|
27
|
+
readonly __tslawsMin: Lo;
|
|
28
|
+
readonly __tslawsMax: Hi;
|
|
29
|
+
};
|
|
30
|
+
/** An unsigned 8-bit integer (`0..255`, like Ada/SPARK & Rust `u8`). */
|
|
31
|
+
export type uint8 = intRange<0, 255>;
|
|
32
|
+
/** An unsigned 16-bit integer (`0..65535`, like Ada/SPARK & Rust `u16`). */
|
|
33
|
+
export type uint16 = intRange<0, 65535>;
|
|
34
|
+
/** An unsigned 32-bit integer (`0..4294967295`, like Ada/SPARK & Rust `u32`). */
|
|
35
|
+
export type uint32 = intRange<0, 4294967295>;
|
|
36
|
+
/** A signed 32-bit integer (`-2147483648..2147483647`, like Ada/SPARK & Rust `i32`). */
|
|
37
|
+
export type int32 = intRange<-2147483648, 2147483647>;
|
|
38
|
+
/** A non-empty array (`xs.length >= 1`). Generated non-empty and proved with `xs.length >= 1`. */
|
|
39
|
+
export type nonEmpty<T> = T[] & {
|
|
40
|
+
readonly __tslawsNonEmpty: true;
|
|
41
|
+
};
|
|
42
|
+
/** A non-empty string (`s.length >= 1`). Generated non-empty and proved with `(str.len s) >= 1`. */
|
|
43
|
+
export type nonEmptyString = string & {
|
|
44
|
+
readonly __tslawsNonEmptyString: true;
|
|
45
|
+
};
|
|
46
|
+
/** An ASCII string (characters U+0000..U+007F). Generated and proved over ASCII. */
|
|
47
|
+
export type asciiString = string & {
|
|
48
|
+
readonly __tslawsAscii: true;
|
|
49
|
+
};
|
|
50
|
+
/** A finite IEEE-754 number (never `NaN`, `Infinity`, or `-Infinity`). */
|
|
51
|
+
export type finite = number & {
|
|
52
|
+
readonly __tslawsFinite: true;
|
|
53
|
+
};
|
|
10
54
|
/** Thrown by `assume`; the runner skips the input. */
|
|
11
55
|
export declare class LawDiscard extends Error {
|
|
12
56
|
constructor();
|
|
@@ -19,6 +63,48 @@ export declare function assume(condition: boolean): asserts condition;
|
|
|
19
63
|
* loop's result a proof for every number of iterations instead of a bounded one.
|
|
20
64
|
*/
|
|
21
65
|
export declare function invariant(condition: boolean): void;
|
|
66
|
+
/**
|
|
67
|
+
* Intermediate proof step (like Dafny `assert`): throws at runtime if false; in the prover, checked first as its own
|
|
68
|
+
* SMT subgoal and then assumed for the rest of the law, helping Z3 discharge multi-step proofs.
|
|
69
|
+
*/
|
|
70
|
+
export declare function lemma(condition: boolean): asserts condition;
|
|
71
|
+
/**
|
|
72
|
+
* Function precondition (like Dafny/Verus `requires`): throws at runtime if false. Inside a called function, the prover
|
|
73
|
+
* verifies that every caller satisfies it; at the top level of a law it acts as an assumption.
|
|
74
|
+
*/
|
|
75
|
+
export declare function requires(condition: boolean): asserts condition;
|
|
76
|
+
/**
|
|
77
|
+
* Function postcondition (like Dafny/Verus `ensures`): throws at runtime if false; in the prover, verified as an
|
|
78
|
+
* obligation before the function returns.
|
|
79
|
+
*/
|
|
80
|
+
export declare function ensures(condition: boolean): asserts condition;
|
|
81
|
+
/**
|
|
82
|
+
* Termination measure for a loop (with `invariant`) or recursive function (like Dafny/Verus `decreases`): must be a
|
|
83
|
+
* non-negative integer that strictly decreases on every iteration or recursive call.
|
|
84
|
+
*/
|
|
85
|
+
export declare function decreases(measure: number): void;
|
|
86
|
+
/** Logical implication (`premise ==> conclusion`, as in Dafny/Lean/SPARK): holds when `premise` is false or `conclusion` is true. */
|
|
87
|
+
export declare function implies(premise: boolean, conclusion: boolean): boolean;
|
|
88
|
+
/** Logical biconditional (`a <==> b`, as in Dafny/Lean): holds when `a` and `b` have the same truth value. */
|
|
89
|
+
export declare function iff(a: boolean, b: boolean): boolean;
|
|
90
|
+
/** True when all arguments are pairwise distinct (`!==`), lowered directly to SMT-LIB `(distinct ...)`. */
|
|
91
|
+
export declare function distinct(...values: unknown[]): boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Asserts that this branch is mathematically unreachable for every input (like Verus/Rust/Dafny `assert false`).
|
|
94
|
+
* Throws at runtime; the prover fails with a concrete counterexample if any input can reach it.
|
|
95
|
+
*/
|
|
96
|
+
export declare function unreachable(reason?: string): never;
|
|
97
|
+
/**
|
|
98
|
+
* Calculational equational proof chain (like Dafny/Lean `calc`): proves each adjacent step `steps[i] === steps[i + 1]`
|
|
99
|
+
* as its own SMT subgoal and chains them transitively so multi-step algebraic proofs succeed without solver blowup.
|
|
100
|
+
*/
|
|
101
|
+
export declare function calc(...steps: unknown[]): boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Natural-number mathematical induction (like Bend 2 / Lean 4): proves `predicate(k)` holds for every `k >= 0`
|
|
104
|
+
* unboundedly by proving the base case `predicate(0)` and the inductive step `predicate(k) ==> predicate(k + 1)`.
|
|
105
|
+
* At runtime, checks `predicate(n)`.
|
|
106
|
+
*/
|
|
107
|
+
export declare function induct(n: number, predicate: (k: number) => boolean): boolean;
|
|
22
108
|
/**
|
|
23
109
|
* True when `predicate(x, i)` holds for every element of `xs`. In a law the prover lowers it over the array's slots, so
|
|
24
110
|
* a claim that relies on it is `bounded` by `--bound` like any array claim. The predicate must be an arrow function
|
package/dist/api.js
CHANGED
|
@@ -23,6 +23,84 @@ export function invariant(condition) {
|
|
|
23
23
|
if (!condition)
|
|
24
24
|
throw new Error('loop invariant violated');
|
|
25
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Intermediate proof step (like Dafny `assert`): throws at runtime if false; in the prover, checked first as its own
|
|
28
|
+
* SMT subgoal and then assumed for the rest of the law, helping Z3 discharge multi-step proofs.
|
|
29
|
+
*/
|
|
30
|
+
export function lemma(condition) {
|
|
31
|
+
if (!condition)
|
|
32
|
+
throw new Error('lemma failed');
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Function precondition (like Dafny/Verus `requires`): throws at runtime if false. Inside a called function, the prover
|
|
36
|
+
* verifies that every caller satisfies it; at the top level of a law it acts as an assumption.
|
|
37
|
+
*/
|
|
38
|
+
export function requires(condition) {
|
|
39
|
+
if (!condition)
|
|
40
|
+
throw new Error('precondition violated');
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Function postcondition (like Dafny/Verus `ensures`): throws at runtime if false; in the prover, verified as an
|
|
44
|
+
* obligation before the function returns.
|
|
45
|
+
*/
|
|
46
|
+
export function ensures(condition) {
|
|
47
|
+
if (!condition)
|
|
48
|
+
throw new Error('postcondition violated');
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Termination measure for a loop (with `invariant`) or recursive function (like Dafny/Verus `decreases`): must be a
|
|
52
|
+
* non-negative integer that strictly decreases on every iteration or recursive call.
|
|
53
|
+
*/
|
|
54
|
+
export function decreases(measure) {
|
|
55
|
+
if (typeof measure !== 'number' || measure < 0 || !Number.isFinite(measure))
|
|
56
|
+
throw new Error('termination measure must be a non-negative finite number');
|
|
57
|
+
}
|
|
58
|
+
/** Logical implication (`premise ==> conclusion`, as in Dafny/Lean/SPARK): holds when `premise` is false or `conclusion` is true. */
|
|
59
|
+
export function implies(premise, conclusion) {
|
|
60
|
+
return !premise || conclusion;
|
|
61
|
+
}
|
|
62
|
+
/** Logical biconditional (`a <==> b`, as in Dafny/Lean): holds when `a` and `b` have the same truth value. */
|
|
63
|
+
export function iff(a, b) {
|
|
64
|
+
return Boolean(a) === Boolean(b);
|
|
65
|
+
}
|
|
66
|
+
/** True when all arguments are pairwise distinct (`!==`), lowered directly to SMT-LIB `(distinct ...)`. */
|
|
67
|
+
export function distinct(...values) {
|
|
68
|
+
for (let i = 0; i < values.length; i += 1) {
|
|
69
|
+
for (let j = i + 1; j < values.length; j += 1) {
|
|
70
|
+
if (values[i] === values[j])
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Asserts that this branch is mathematically unreachable for every input (like Verus/Rust/Dafny `assert false`).
|
|
78
|
+
* Throws at runtime; the prover fails with a concrete counterexample if any input can reach it.
|
|
79
|
+
*/
|
|
80
|
+
export function unreachable(reason = 'unreachable branch reached') {
|
|
81
|
+
throw new Error(reason);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Calculational equational proof chain (like Dafny/Lean `calc`): proves each adjacent step `steps[i] === steps[i + 1]`
|
|
85
|
+
* as its own SMT subgoal and chains them transitively so multi-step algebraic proofs succeed without solver blowup.
|
|
86
|
+
*/
|
|
87
|
+
export function calc(...steps) {
|
|
88
|
+
for (let i = 0; i + 1 < steps.length; i += 1) {
|
|
89
|
+
if (steps[i] !== steps[i + 1])
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Natural-number mathematical induction (like Bend 2 / Lean 4): proves `predicate(k)` holds for every `k >= 0`
|
|
96
|
+
* unboundedly by proving the base case `predicate(0)` and the inductive step `predicate(k) ==> predicate(k + 1)`.
|
|
97
|
+
* At runtime, checks `predicate(n)`.
|
|
98
|
+
*/
|
|
99
|
+
export function induct(n, predicate) {
|
|
100
|
+
if (n < 0 || !Number.isInteger(n))
|
|
101
|
+
return false;
|
|
102
|
+
return predicate(n);
|
|
103
|
+
}
|
|
26
104
|
/**
|
|
27
105
|
* True when `predicate(x, i)` holds for every element of `xs`. In a law the prover lowers it over the array's slots, so
|
|
28
106
|
* a claim that relies on it is `bounded` by `--bound` like any array claim. The predicate must be an arrow function
|