@earnestjs/model 0.0.0-stage → 0.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/LICENSE +674 -0
- package/LICENSE.LESSER +165 -0
- package/README.md +298 -2
- package/dist/breadth.d.ts +125 -0
- package/dist/breadth.js +150 -0
- package/dist/declarations.d.ts +201 -0
- package/dist/declarations.js +482 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +7 -0
- package/dist/m.d.ts +10 -0
- package/dist/m.js +3 -0
- package/dist/model.d.ts +297 -0
- package/dist/model.js +262 -0
- package/dist/replay.d.ts +41 -0
- package/dist/replay.js +99 -0
- package/dist/runs.d.ts +41 -0
- package/dist/runs.js +325 -0
- package/dist/search.d.ts +130 -0
- package/dist/search.js +484 -0
- package/dist/values.d.ts +296 -0
- package/dist/values.js +695 -0
- package/package.json +51 -4
package/LICENSE.LESSER
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
package/README.md
CHANGED
|
@@ -1,3 +1,299 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @earnestjs/model
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The TypeScript library that models for the `earnest` model checker are written with. A model file
|
|
4
|
+
imports `m` from it and declares fields, actions, rules and ends. `earnest verify` reads the file
|
|
5
|
+
without running it; this library makes the same file type-check under strict `tsc`, with every
|
|
6
|
+
type inferred from the declarations, and run as ordinary code in Node.
|
|
7
|
+
|
|
8
|
+
The [Earnest documentation](https://earnest.cpeaustriajc.dev/reference/typescript/) (`apps/docs/src/content/docs/reference/`) is the contract: what a model file may contain, every primitive and its operations, and the JSON shapes used below.
|
|
9
|
+
|
|
10
|
+
## Installing
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install --save-dev @earnestjs/model
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
The published package holds compiled JavaScript and declarations in `dist/`, so it runs in Node
|
|
17
|
+
and type-checks under a strict `tsc` in a project that installs it. In this repository `exports`
|
|
18
|
+
points at `src/index.ts`, which Node runs by stripping types, and `publishConfig.exports` points
|
|
19
|
+
at `dist/` for the tarball that `bun pm pack` makes; `bun run build` emits it, and `prepack` runs
|
|
20
|
+
the build and swaps `publishConfig.exports` into `exports` for the tarball (`postpack` restores
|
|
21
|
+
`package.json`), since bun does not apply `publishConfig` itself. `test/installed.test.ts` packs the library, installs the tarball into a fresh project
|
|
22
|
+
and runs `explore`, `replay` and a strict type check there.
|
|
23
|
+
|
|
24
|
+
## Writing a model
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { m } from "@earnestjs/model";
|
|
28
|
+
|
|
29
|
+
export const Turnstile = m.model("Turnstile", {
|
|
30
|
+
gate: m.enum(["locked", "open"]).initial("locked"),
|
|
31
|
+
paid: m.boolean().initial(false),
|
|
32
|
+
visitors: m.int(0, 3).initial(0),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export const pay = m.action(Turnstile, "the visitor pays", {
|
|
36
|
+
when: (s) => !s.paid,
|
|
37
|
+
changes: () => ({ paid: true }),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
export const push = m.action(Turnstile, "the visitor pushes", {
|
|
41
|
+
when: (s) => s.paid && s.visitors < 3,
|
|
42
|
+
changes: (s) => ({ gate: "open", visitors: s.visitors + 1 }),
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
export const staysLocked = m.rule(
|
|
46
|
+
Turnstile,
|
|
47
|
+
"The gate stays locked until the visitor pays",
|
|
48
|
+
(s) => s.gate === "locked" || s.paid,
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
export const full = m.end(Turnstile, "Three visitors went through", (s) => s.visitors === 3);
|
|
52
|
+
|
|
53
|
+
export type State = m.Infer<typeof Turnstile>;
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
- Nothing is annotated. `s` is the state type the fields declare, an action's second parameter is
|
|
57
|
+
typed from its `input`, and `changes` gives back only the fields that change. A wrong literal
|
|
58
|
+
(`gate: "ajar"`), a field the model does not have, or a value of the wrong kind in `changes` is
|
|
59
|
+
a compile error. In an action, `input`, `when`, `changes` and `fair` may be written in any
|
|
60
|
+
order.
|
|
61
|
+
- `m.action`, `m.rule`, `m.end` and `m.view` take the model as their first argument. A rule or an
|
|
62
|
+
end then has its sentence and a condition. An action has its sentence and an object with
|
|
63
|
+
`input`, `when` and `changes`. A view has an object.
|
|
64
|
+
- `m.witness(Model, sentence, { steps, expect })` declares a path the model must be able to take
|
|
65
|
+
from every start state, and a condition on the state it ends in. Each step is an action of the
|
|
66
|
+
model declared before the witness; an action with inputs is written
|
|
67
|
+
`m.step(action, { name: value })`. A bare action that takes inputs in `steps`, and `m.step` with
|
|
68
|
+
an input the action does not declare, are compile errors, and `m.witness` throws on a step that
|
|
69
|
+
is not an action declared on the model before it or on an input the action does not declare.
|
|
70
|
+
With `setup: behind`, the path is played from that setup's start states instead.
|
|
71
|
+
See [witnesses](https://earnest.cpeaustriajc.dev/reference/typescript/#witnesses).
|
|
72
|
+
- `m.eventually(Model, sentence, { after, reaches })` declares a rule about runs: from every
|
|
73
|
+
reachable state where `after` is true (without `after`, from every start state), every run comes
|
|
74
|
+
to a state where `reaches` is true. A run never stops while an action can still change the
|
|
75
|
+
state, and `fair: true` on an action says it is not put off forever while it stays possible.
|
|
76
|
+
A `reaches` or `after` that does not give a boolean is a compile error. See
|
|
77
|
+
[rules about runs](https://earnest.cpeaustriajc.dev/reference/typescript/#rules-about-runs).
|
|
78
|
+
- `m.setup(Model, sentence, { field: value })` declares a setup: the model checked once more with
|
|
79
|
+
those fields starting at those values. A field the model lacks, or a value of the wrong type,
|
|
80
|
+
is a compile error, and `m.setup` throws when it names no field, a name that is not a field, or
|
|
81
|
+
a value the field does not declare. See
|
|
82
|
+
[setups](https://earnest.cpeaustriajc.dev/reference/typescript/#setups).
|
|
83
|
+
- Declarations are plain values. `.optional()` and `.initial(value)` give back a new declaration,
|
|
84
|
+
so one `const Press = m.enum([...])` can be a field in two models and an input of an action.
|
|
85
|
+
- State is read-only, and every operation on a set, map, sequence, queue, lock, timer, request or
|
|
86
|
+
retry gives back a new value.
|
|
87
|
+
- `m.Infer<typeof Model>` is the state type, and `m.Infer<typeof Declaration>` the type of a
|
|
88
|
+
declaration's values. `m` is a module namespace (`export * as m`), so `m.Infer` is an ordinary
|
|
89
|
+
exported type and nothing in the library needs more than type stripping to run.
|
|
90
|
+
|
|
91
|
+
## The builders
|
|
92
|
+
|
|
93
|
+
| Builder | Values | Starts at, without `.initial` |
|
|
94
|
+
| --------------------------- | ------------------------------------------------ | ----------------------------- |
|
|
95
|
+
| `m.boolean()` | `false`, `true` | every value |
|
|
96
|
+
| `m.int(lo, hi)` | whole numbers from `lo` to `hi` | every value |
|
|
97
|
+
| `m.enum([...])` | the listed texts | every value |
|
|
98
|
+
| `m.oneOf([...])` | the listed texts, numbers or booleans | every value |
|
|
99
|
+
| `.optional()` | `null`, then the values of the declaration | every value |
|
|
100
|
+
| `m.record({...})` | an object with exactly those fields | every value |
|
|
101
|
+
| `m.set(T)` | `SetValue<T>` | the empty set |
|
|
102
|
+
| `m.map(K, V)` | `MapValue<K, V>` | the empty map |
|
|
103
|
+
| `m.seq(T, { max })` | `SeqValue<T>` of at most `max` items | the empty sequence |
|
|
104
|
+
| `m.queue(T, { capacity })` | `QueueValue<T>` of at most `capacity` items | the empty queue |
|
|
105
|
+
| `m.lock()`, `m.lock(Owner)` | `LockValue`, `OwnedLockValue<Owner>` | free |
|
|
106
|
+
| `m.timer()` | `TimerValue`: idle, running, fired | idle |
|
|
107
|
+
| `m.request()` | `RequestValue`: idle, pending, succeeded, failed | idle |
|
|
108
|
+
| `m.retry({ max })` | `RetryValue` from zero to `max` attempts | zero |
|
|
109
|
+
|
|
110
|
+
A declaration that makes no sense throws when it is written: bounds the wrong way round, a
|
|
111
|
+
value listed twice in `m.enum` or `m.oneOf`, an optional of an optional, and a lock with an
|
|
112
|
+
owner as a map value or a sequence or queue item, whose free state `null` could not be told from
|
|
113
|
+
a missing entry. `earnest verify` refuses the same.
|
|
114
|
+
|
|
115
|
+
`m.rem(a, b)` is JavaScript's `a % b`, with the sign of `a` as Rust's `%` has it (`m.rem(-7, 2)`
|
|
116
|
+
is -1, `m.rem(7, -2)` is 1): `%` is not allowed in a model file, and `m.rem` by zero is a fault
|
|
117
|
+
that `explore` and `replay` report as `earnest verify` does, "takes a division or a remainder by
|
|
118
|
+
zero", where `%` would give `NaN`. `m.div(a, b)` is `Math.trunc(a / b)`, the whole number of
|
|
119
|
+
times `b` goes into `a` as Rust's `/` on integers (`m.div(7, 2)` is 3, `m.div(-7, 2)` is -3),
|
|
120
|
+
exact for every safe integer: `/` is not allowed in a model file, since JavaScript's gives a
|
|
121
|
+
fraction, and `m.div` by zero is the same fault. A `NaN` given to either is a fault too. `m.some(value)` is the value inside an optional: `%`'s sibling for `!`, and for `as`, `satisfies` and `<Type>value`, which a model
|
|
122
|
+
file may not write (only `as const` is read). `m.some(null)` is a fault that `explore` and `replay` report as `earnest
|
|
123
|
+
verify` does ("reads a value that is null"), where JavaScript would go on with `null` as 0, so
|
|
124
|
+
a number read from an optional goes through it. `m.setOf(a, b)`, `m.seqOf(a, b)` and `m.mapOf([k, v])` write collection values; a queue's
|
|
125
|
+
`.initial` takes `m.seqOf(...)`. `m.forall(T, f)` and `m.exists(T, f)` range over a declaration
|
|
126
|
+
or a collection. Every declaration has `values()`, which lists what it declares in the order the
|
|
127
|
+
checker tries them, `has(value)`, `starts()`, `count` and `fromJson(json)`. Its `libraryKind` is
|
|
128
|
+
`null` when every value is plain JavaScript (booleans, numbers, texts, `null` and records of
|
|
129
|
+
those), and otherwise names the part that holds this library's objects, such as `"set"`. Its
|
|
130
|
+
`source` is the `m` expression that declares the same values, such as `m.int(0, 3).optional()`,
|
|
131
|
+
without `.initial`, for code that writes a model file. A record's `fields` gives the declaration
|
|
132
|
+
of each field, and an optional's `inner` the declaration without `null` and without `.initial`;
|
|
133
|
+
both are `null` for other kinds, for code that checks a value one part at a time.
|
|
134
|
+
`combinationsOf(fields)` lists every row of one value per field, in the order `explore` tries an
|
|
135
|
+
action's inputs: the first field varies fastest.
|
|
136
|
+
|
|
137
|
+
## Running a model in Node
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
import { explore, replay } from "@earnestjs/model";
|
|
141
|
+
import * as turnstile from "./turnstile.model.ts";
|
|
142
|
+
|
|
143
|
+
const found = explore(turnstile.Turnstile, { exports: turnstile });
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
`explore` searches breadth first and gives
|
|
147
|
+
`{ status, states, rules: [{ id, says, verdict, path }], findings: [{ kind, message, path }], reason }`
|
|
148
|
+
with the words and the path shape of `earnest verify --json`: `pass`, `fail` or `unfinished`;
|
|
149
|
+
`holds`, `broken` or `unknown`; `stuck`, `out-of-range` or `fault`. Each broken rule has the
|
|
150
|
+
shortest path to a state that breaks it. A rule is `unknown` when it was not seen broken but
|
|
151
|
+
the search is unfinished or the rule could not be evaluated in a state. `findings` has at most
|
|
152
|
+
one of each kind. `reason` is `null`, or why the search is unfinished.
|
|
153
|
+
|
|
154
|
+
Witnesses come after the rules in `rules`, each with `kind: "witness"`, as `earnest verify --json`
|
|
155
|
+
lists them. A witness is played from every start state in order; when one breaks it, its `path`
|
|
156
|
+
is what was played until it broke, from that start, and its `reason` says why, in the words of
|
|
157
|
+
the checker's `why` line. It is `unknown` only when there are more start states than
|
|
158
|
+
`maxStates` and none of the ones played broke it. A rule has no `kind` and no `reason`.
|
|
159
|
+
|
|
160
|
+
Rules about runs come after the rules and before the witnesses, each with
|
|
161
|
+
`kind: "eventually"`. A broken one has the shortest path into a run that never reaches what it
|
|
162
|
+
wants, and a `reason`: `steps A to B can repeat forever`, with `repeatsFrom` the position in
|
|
163
|
+
`path.steps` where the loop starts, or `nothing changes the state from here, so the run can stay
|
|
164
|
+
in it forever`. One is `unknown`, never `holds`, when the search is unfinished, a state is out of
|
|
165
|
+
range, or a fault was found. The search keeps every move of a model with such a rule.
|
|
166
|
+
|
|
167
|
+
`explore(Model, { exports, setup: behind })` searches the model from the start states of one of
|
|
168
|
+
its setups and plays the witnesses that name that setup, as the setup's own result from
|
|
169
|
+
`earnest verify` does. Without `setup` it searches the model as declared and plays the witnesses
|
|
170
|
+
that name none. It throws when the setup is not one declared on this model. The setups of a model
|
|
171
|
+
are in `Model.runners().setups`, each with the `part` its export names.
|
|
172
|
+
|
|
173
|
+
- The id of an action, a rule, an end, a rule about runs, a setup or a witness is the name it is exported under, and a running program
|
|
174
|
+
can't see that name, so pass the model file's exports as `exports`. A part that is not among
|
|
175
|
+
them has its sentence as its id.
|
|
176
|
+
- `maxStates` stops the search before it holds more distinct states than that; the default is
|
|
177
|
+
5,000,000. Every state that is counted has its rules judged, also the states the limit kept
|
|
178
|
+
the search from going on from, so a rule one of them breaks is `broken` with its path. No
|
|
179
|
+
action is tried from those states.
|
|
180
|
+
- The search follows the checker's order, so the two results can be compared: start states in
|
|
181
|
+
field order with the first field varying slowest, actions in declaration order, inputs with
|
|
182
|
+
the first input varying fastest, and states judged in the order they were found.
|
|
183
|
+
- Anything the model asks that can't be evaluated in a state is a `fault` there: a rule, the
|
|
184
|
+
condition or the change of an action, or the `when` of an end. A state is `stuck` only when the
|
|
185
|
+
condition of every action is false and every end is false, so a state where a condition or an end
|
|
186
|
+
faults is not stuck. An end is asked only in a state where no action can happen.
|
|
187
|
+
- A state that is out of range is counted, is not checked against the rules and is not searched
|
|
188
|
+
from.
|
|
189
|
+
|
|
190
|
+
`replay(Model, path, { exports })` applies a `{ start, steps }` path one action at a time and
|
|
191
|
+
gives every state on the way, the start state first, as objects in JSON form. It throws an error
|
|
192
|
+
that names the step when the path does not begin at a start state, a step names no action, an
|
|
193
|
+
input is missing or undeclared, the action's `when` is false, the action can't be evaluated, or
|
|
194
|
+
the state after the action differs from the step's `state`. Without `exports` a step is matched
|
|
195
|
+
by its `label`, the action's sentence.
|
|
196
|
+
|
|
197
|
+
`replay(Model, path, { exports, setup: half })` replays a path of the setup `half`, as the
|
|
198
|
+
setup's own result in `earnest verify --json` gives it: the path must begin at one of the setup's
|
|
199
|
+
start states, the ones `explore(Model, { setup: half })` starts from. Without `setup` the path must
|
|
200
|
+
begin where the model declares. It throws when the setup is not one declared on this model.
|
|
201
|
+
|
|
202
|
+
## Searching states that are not a model
|
|
203
|
+
|
|
204
|
+
`search` runs the same breadth-first search as `explore` on states the library knows nothing
|
|
205
|
+
about, such as plain objects. Give it the start states, a `key` that names a state, and `moves`,
|
|
206
|
+
which lists `{ move, next }` from a state:
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
import { search } from "@earnestjs/model";
|
|
210
|
+
|
|
211
|
+
const found = search({
|
|
212
|
+
starts: [{ gate: "Locked", paid: false }],
|
|
213
|
+
key: (s) => `${s.gate};${s.paid}`,
|
|
214
|
+
moves: (s) =>
|
|
215
|
+
s.gate === "Locked"
|
|
216
|
+
? [
|
|
217
|
+
...(s.paid ? [] : [{ move: "pay", next: { ...s, paid: true } }]),
|
|
218
|
+
{ move: "push", next: { ...s, gate: "Open" } },
|
|
219
|
+
]
|
|
220
|
+
: [{ move: "enter", next: { gate: "Locked", paid: false } }],
|
|
221
|
+
rules: [
|
|
222
|
+
{
|
|
223
|
+
id: "locked",
|
|
224
|
+
says: "The gate stays locked until the visitor pays",
|
|
225
|
+
holds: (s) => s.gate === "Locked" || s.paid,
|
|
226
|
+
},
|
|
227
|
+
],
|
|
228
|
+
});
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
It counts 4 states and fails: `locked` is `broken` after one `push`. The result has the words of
|
|
232
|
+
`explore` (`status`, `states`, `rules`, `findings`, `reason`), but each path is a trail of the
|
|
233
|
+
caller's own values, `{ start, steps: [{ move, state }] }`.
|
|
234
|
+
|
|
235
|
+
- States are judged in the order they are reached: start states in the order given, then the
|
|
236
|
+
moves of each state in the order `moves` lists them. States with the same `key` are one state.
|
|
237
|
+
- `rules` are judged in every counted state that is not out of range, also the states the state
|
|
238
|
+
limit kept the search from going on from. `maxStates` works as in `explore`.
|
|
239
|
+
- A state with no move is `stuck` unless `isEnd` gives `true` for it. `isEnd` is asked only of
|
|
240
|
+
a state with no move.
|
|
241
|
+
- When `outside` gives a message for a state, the state is out of range: it is counted, is not
|
|
242
|
+
checked against the rules and is not searched from. The first one is an `out-of-range` finding
|
|
243
|
+
with that message, and a rule not seen broken is `unknown`.
|
|
244
|
+
- `reached()` gives the shortest trail to every counted state, in the order the search reached
|
|
245
|
+
them. Each trail is built as it is asked for.
|
|
246
|
+
- There are no faults: an error thrown by `key`, `moves`, a rule, `isEnd` or `outside` is thrown
|
|
247
|
+
by `search`.
|
|
248
|
+
|
|
249
|
+
## Where Node and the checker differ
|
|
250
|
+
|
|
251
|
+
- Arithmetic past 2^53 - 1 is a fault in the checker, "computes a number too large to hold",
|
|
252
|
+
wherever a model computes it. In Node the library sees only the numbers it is given back: a
|
|
253
|
+
next state that holds one is the same fault in the change of its action. A number computed
|
|
254
|
+
inside a rule, an end or a `when`, or by a helper on the way to a value that is not a number,
|
|
255
|
+
is the rounded value JavaScript computes, so `s.n + 9007199254740992 - 9007199254740992 ===
|
|
256
|
+
s.n` is judged by what JavaScript makes of it. `m.int` throws on a bound outside the safe
|
|
257
|
+
integers, and `m.oneOf` on a number outside them or one that is not whole, as the checker
|
|
258
|
+
refuses the same file.
|
|
259
|
+
- `===` on two records compares the objects in Node. Compare records field by field. Sets, maps,
|
|
260
|
+
sequences, queues, locks, timers, requests and retries are shared by content, so `===` on them
|
|
261
|
+
compares content.
|
|
262
|
+
- Set members and map keys are walked and printed in one fixed order, the checker's and the Rust
|
|
263
|
+
library's: `null`, booleans, numbers, text by code point (`compareText`, which puts a character
|
|
264
|
+
past U+FFFF after one from U+E000 to U+FFFF, where `<` does not), records field by field in the
|
|
265
|
+
order of the field names, collections item by item. A quantifier over a set stops at the first
|
|
266
|
+
item in that order. `replay` still accepts the members of a set in any order. See the
|
|
267
|
+
[primitives reference](https://earnest.cpeaustriajc.dev/reference/primitives/#order-of-sets-maps-and-records) for the rule for each kind.
|
|
268
|
+
- A fault is a `TypeError` thrown by the model, `m.rem` or `m.div` by zero, or `m.some(null)`:
|
|
269
|
+
reading a field of `null` or `m.some(null)` reports "reads a value that is null", any other
|
|
270
|
+
`TypeError` reports "uses a value of the wrong kind". Where a model file reads a number from
|
|
271
|
+
an optional it must use `m.some`, because JavaScript would give `null < 3` as true and `null +
|
|
272
|
+
1` as 1; the checker refuses the postfix `!` that would hide it.
|
|
273
|
+
- The library does not check that a file keeps to what a model file may contain. `earnest verify`
|
|
274
|
+
does.
|
|
275
|
+
|
|
276
|
+
## Development
|
|
277
|
+
|
|
278
|
+
From this folder, after `bun install --frozen-lockfile` at the repository root:
|
|
279
|
+
|
|
280
|
+
```sh
|
|
281
|
+
bun run typecheck # tsrs --noEmit, over src/, scripts/ and test/
|
|
282
|
+
bun run format # oxfmt --check
|
|
283
|
+
bun run format:fix # oxfmt, rewriting the files in place
|
|
284
|
+
bun run test # vitest run
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
- The package has no runtime dependencies and no build step. Node 22.18 or newer runs the `.ts`
|
|
288
|
+
files by stripping types, so the source uses only erasable syntax.
|
|
289
|
+
- The devDependencies are pinned to exact versions: `tsrs` (the Rust port of the TypeScript
|
|
290
|
+
checker), `oxfmt` (the formatter) and `vitest`, with `@types/node` for the tests. The lockfile
|
|
291
|
+
is `bun.lock` at the repository root.
|
|
292
|
+
- `typecheck` is one project: `tsconfig.json` covers `src/`, `scripts/` and `test/` and excludes
|
|
293
|
+
`test/rejected/`. It sets `types` to `node` so the tests can import `node:` modules.
|
|
294
|
+
- `test/types.ts` only has to compile: it holds the type-level proofs, such as what `m.Infer`
|
|
295
|
+
gives and which values `changes` rejects.
|
|
296
|
+
- `test/rejected/` holds one program for each mistake the compiler must catch, and `accepted.ts`,
|
|
297
|
+
which it must not complain about. `test/rejected.test.ts` runs `tsrs` on the folder and checks
|
|
298
|
+
that each bad program gets exactly one error, on the expected line. The folder is excluded from
|
|
299
|
+
the package's own `tsconfig.json`.
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `unknown` means the rule was not seen broken, but some state was not judged or not reached:
|
|
3
|
+
* the state limit stopped the search, or a state out of range cut it short. In `explore` a
|
|
4
|
+
* fault in an action or an end also cuts the search short, and a rule is also `unknown` when it
|
|
5
|
+
* could not be evaluated in a state it reached.
|
|
6
|
+
*/
|
|
7
|
+
export type Verdict = "holds" | "broken" | "unknown";
|
|
8
|
+
/**
|
|
9
|
+
* `unfinished` means the state limit stopped the search before anything was found wrong. Every
|
|
10
|
+
* counted state that is not out of range has its rules judged, also the ones the limit kept the
|
|
11
|
+
* search from going on from.
|
|
12
|
+
*/
|
|
13
|
+
export type Status = "pass" | "fail" | "unfinished";
|
|
14
|
+
/** The state limit `explore` and `search` use when given none, the same as `earnest verify`. */
|
|
15
|
+
export declare const DEFAULT_MAX_STATES = 5000000;
|
|
16
|
+
/** Checks a caller's `maxStates`, or gives `DEFAULT_MAX_STATES` when it is `undefined` or `null`. */
|
|
17
|
+
export declare const stateLimit: (given: number | undefined) => number;
|
|
18
|
+
/** A move that `moves` lists from a state: the move, in the caller's terms, and where it leads. */
|
|
19
|
+
export interface SearchMove<State, Move> {
|
|
20
|
+
readonly move: Move;
|
|
21
|
+
readonly next: State;
|
|
22
|
+
}
|
|
23
|
+
/** A rule `search` judges in every state it counts that is not out of range. */
|
|
24
|
+
export interface SearchRule<State> {
|
|
25
|
+
readonly id: string;
|
|
26
|
+
readonly says: string;
|
|
27
|
+
holds(state: State): boolean;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* What `search` is given. `key` must give two states the same text exactly when they are the
|
|
31
|
+
* same state. `moves` lists the moves from a state in the order they are tried. `isEnd` is asked
|
|
32
|
+
* only of a state with no move, and a state it does not mark is stuck; without it every such
|
|
33
|
+
* state is stuck. `outside` gives a message for a state that is out of range, or `null`.
|
|
34
|
+
* `maxStates` stops the search before it holds more distinct states than this; the default is
|
|
35
|
+
* `DEFAULT_MAX_STATES`. An error thrown by any of these functions is thrown by `search`.
|
|
36
|
+
*/
|
|
37
|
+
export interface SearchInput<State, Move> {
|
|
38
|
+
readonly starts: Iterable<State>;
|
|
39
|
+
key(state: State): string;
|
|
40
|
+
moves(state: State): readonly SearchMove<State, Move>[];
|
|
41
|
+
readonly rules?: readonly SearchRule<State>[];
|
|
42
|
+
isEnd?(state: State): boolean;
|
|
43
|
+
outside?(state: State): string | null;
|
|
44
|
+
readonly maxStates?: number;
|
|
45
|
+
}
|
|
46
|
+
/** One move on a trail and the state it leads to. */
|
|
47
|
+
export interface TrailStep<State, Move> {
|
|
48
|
+
readonly move: Move;
|
|
49
|
+
readonly state: State;
|
|
50
|
+
}
|
|
51
|
+
/** A start state and the moves that lead from it, each with the state after it. */
|
|
52
|
+
export interface Trail<State, Move> {
|
|
53
|
+
readonly start: State;
|
|
54
|
+
readonly steps: readonly TrailStep<State, Move>[];
|
|
55
|
+
}
|
|
56
|
+
/** One rule's result. `path` is the shortest trail to a state that breaks it, or `null`. */
|
|
57
|
+
export interface SearchRuleResult<State, Move> {
|
|
58
|
+
readonly id: string;
|
|
59
|
+
readonly says: string;
|
|
60
|
+
readonly verdict: Verdict;
|
|
61
|
+
readonly path: Trail<State, Move> | null;
|
|
62
|
+
}
|
|
63
|
+
/** `stuck`: no move and `isEnd` false. `out-of-range`: `outside` gave a message. */
|
|
64
|
+
export type SearchFindingKind = "stuck" | "out-of-range";
|
|
65
|
+
/** A problem that is not a broken rule, with the shortest trail to the first state showing it. */
|
|
66
|
+
export interface SearchFinding<State, Move> {
|
|
67
|
+
readonly kind: SearchFindingKind;
|
|
68
|
+
readonly message: string;
|
|
69
|
+
readonly path: Trail<State, Move>;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* What `search` found, in the words of `explore`. `states` counts distinct states, out-of-range
|
|
73
|
+
* ones included. `findings` holds at most one of each kind, stuck first. `reason` says why an
|
|
74
|
+
* unfinished search stopped, and is `null` otherwise. `reached` gives the shortest trail to
|
|
75
|
+
* every counted state, in the order the search reached them; each trail is built as it is
|
|
76
|
+
* asked for.
|
|
77
|
+
*/
|
|
78
|
+
export interface SearchResult<State, Move> {
|
|
79
|
+
readonly status: Status;
|
|
80
|
+
readonly states: number;
|
|
81
|
+
readonly rules: readonly SearchRuleResult<State, Move>[];
|
|
82
|
+
readonly findings: readonly SearchFinding<State, Move>[];
|
|
83
|
+
readonly reason: string | null;
|
|
84
|
+
reached(): Iterable<Trail<State, Move>>;
|
|
85
|
+
}
|
|
86
|
+
/** A counted state and the move it was first reached by from the state before it. */
|
|
87
|
+
export interface Visit<State, Move> {
|
|
88
|
+
readonly state: State;
|
|
89
|
+
readonly parent: {
|
|
90
|
+
readonly from: Visit<State, Move>;
|
|
91
|
+
readonly move: Move;
|
|
92
|
+
} | null;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* What a breadth-first search does in each counted state, in the order they are counted.
|
|
96
|
+
* `expand` judges a state the search goes on from and gives its moves; `judge` judges one the
|
|
97
|
+
* state limit kept the search from going on from.
|
|
98
|
+
*/
|
|
99
|
+
export interface Visitor<State, Move> {
|
|
100
|
+
expand(at: Visit<State, Move>): Iterable<SearchMove<State, Move>>;
|
|
101
|
+
judge(at: Visit<State, Move>): void;
|
|
102
|
+
}
|
|
103
|
+
/** Every counted state in the order it was counted, and why the search stopped early, or `null`. */
|
|
104
|
+
export interface Reach<State, Move> {
|
|
105
|
+
readonly visits: readonly Visit<State, Move>[];
|
|
106
|
+
readonly reason: string | null;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The one breadth-first loop that `search` and `explore` share. States with the same `key` are
|
|
110
|
+
* one state, counted once. When counting a state would pass `maxStates`, the search stops going
|
|
111
|
+
* on, and every counted state not yet expanded is judged.
|
|
112
|
+
*/
|
|
113
|
+
export declare const breadthFirst: <State, Move>(starts: Iterable<State>, key: (state: State) => string, maxStates: number, visitor: Visitor<State, Move>) => Reach<State, Move>;
|
|
114
|
+
/** The shortest trail to a counted state, from the start state it was reached from. */
|
|
115
|
+
export declare const trailTo: <State, Move>(target: Visit<State, Move>) => Trail<State, Move>;
|
|
116
|
+
/**
|
|
117
|
+
* Searches every state reachable from `starts` through `moves`, breadth first, for states that
|
|
118
|
+
* are not models: a plain object, a snapshot of a state machine, anything `key` can name. It
|
|
119
|
+
* gives what `explore` gives, with trails of the caller's own moves and states as paths: the
|
|
120
|
+
* count of distinct states, each rule's verdict with the shortest trail to a state that breaks
|
|
121
|
+
* it, the first stuck and out-of-range states, and the shortest trail to every state it
|
|
122
|
+
* reached. States are judged in the order they are reached: start states in the order given,
|
|
123
|
+
* then the moves of each state in the order `moves` lists them.
|
|
124
|
+
*/
|
|
125
|
+
export declare const search: <State, Move>(input: SearchInput<State, Move>) => SearchResult<State, Move>;
|