@earnestjs/model 0.1.0 → 0.2.1
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 +110 -10
- package/dist/breadth.d.ts +125 -0
- package/dist/breadth.js +150 -0
- package/dist/declarations.d.ts +52 -2
- package/dist/declarations.js +94 -3
- package/dist/index.d.ts +6 -4
- package/dist/index.js +3 -2
- package/dist/m.d.ts +1 -1
- package/dist/m.js +1 -1
- package/dist/model.d.ts +135 -2
- package/dist/model.js +127 -5
- package/dist/replay.d.ts +8 -2
- package/dist/replay.js +6 -4
- package/dist/runs.d.ts +41 -0
- package/dist/runs.js +325 -0
- package/dist/search.d.ts +22 -13
- package/dist/search.js +236 -79
- package/dist/values.d.ts +12 -9
- package/dist/values.js +26 -15
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -56,10 +56,30 @@ export type State = m.Infer<typeof Turnstile>;
|
|
|
56
56
|
- Nothing is annotated. `s` is the state type the fields declare, an action's second parameter is
|
|
57
57
|
typed from its `input`, and `changes` gives back only the fields that change. A wrong literal
|
|
58
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` and `
|
|
59
|
+
a compile error. In an action, `input`, `when`, `changes` and `fair` may be written in any
|
|
60
|
+
order.
|
|
60
61
|
- `m.action`, `m.rule`, `m.end` and `m.view` take the model as their first argument. A rule or an
|
|
61
62
|
end then has its sentence and a condition. An action has its sentence and an object with
|
|
62
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).
|
|
63
83
|
- Declarations are plain values. `.optional()` and `.initial(value)` give back a new declaration,
|
|
64
84
|
so one `const Press = m.enum([...])` can be a field in two models and an input of an action.
|
|
65
85
|
- State is read-only, and every operation on a set, map, sequence, queue, lock, timer, request or
|
|
@@ -92,18 +112,27 @@ value listed twice in `m.enum` or `m.oneOf`, an optional of an optional, and a l
|
|
|
92
112
|
owner as a map value or a sequence or queue item, whose free state `null` could not be told from
|
|
93
113
|
a missing entry. `earnest verify` refuses the same.
|
|
94
114
|
|
|
95
|
-
`m.rem(a, b)` is
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
`
|
|
100
|
-
|
|
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
|
|
101
122
|
file may not write (only `as const` is read). `m.some(null)` is a fault that `explore` and `replay` report as `earnest
|
|
102
123
|
verify` does ("reads a value that is null"), where JavaScript would go on with `null` as 0, so
|
|
103
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
|
|
104
125
|
`.initial` takes `m.seqOf(...)`. `m.forall(T, f)` and `m.exists(T, f)` range over a declaration
|
|
105
126
|
or a collection. Every declaration has `values()`, which lists what it declares in the order the
|
|
106
|
-
checker tries them, `has(value)`, `starts()`, `count` and `fromJson(json)`.
|
|
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.
|
|
107
136
|
|
|
108
137
|
## Running a model in Node
|
|
109
138
|
|
|
@@ -122,7 +151,26 @@ shortest path to a state that breaks it. A rule is `unknown` when it was not see
|
|
|
122
151
|
the search is unfinished or the rule could not be evaluated in a state. `findings` has at most
|
|
123
152
|
one of each kind. `reason` is `null`, or why the search is unfinished.
|
|
124
153
|
|
|
125
|
-
|
|
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
|
|
126
174
|
can't see that name, so pass the model file's exports as `exports`. A part that is not among
|
|
127
175
|
them has its sentence as its id.
|
|
128
176
|
- `maxStates` stops the search before it holds more distinct states than that; the default is
|
|
@@ -146,6 +194,58 @@ input is missing or undeclared, the action's `when` is false, the action can't b
|
|
|
146
194
|
the state after the action differs from the step's `state`. Without `exports` a step is matched
|
|
147
195
|
by its `label`, the action's sentence.
|
|
148
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
|
+
|
|
149
249
|
## Where Node and the checker differ
|
|
150
250
|
|
|
151
251
|
- Arithmetic past 2^53 - 1 is a fault in the checker, "computes a number too large to hold",
|
|
@@ -165,7 +265,7 @@ by its `label`, the action's sentence.
|
|
|
165
265
|
order of the field names, collections item by item. A quantifier over a set stops at the first
|
|
166
266
|
item in that order. `replay` still accepts the members of a set in any order. See the
|
|
167
267
|
[primitives reference](https://earnest.cpeaustriajc.dev/reference/primitives/#order-of-sets-maps-and-records) for the rule for each kind.
|
|
168
|
-
- A fault is a `TypeError` thrown by the model, `m.rem`
|
|
268
|
+
- A fault is a `TypeError` thrown by the model, `m.rem` or `m.div` by zero, or `m.some(null)`:
|
|
169
269
|
reading a field of `null` or `m.some(null)` reports "reads a value that is null", any other
|
|
170
270
|
`TypeError` reports "uses a value of the wrong kind". Where a model file reads a number from
|
|
171
271
|
an optional it must use `m.some`, because JavaScript would give `null < 3` as true and `null +
|
|
@@ -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>;
|
package/dist/breadth.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/** The state limit `explore` and `search` use when given none, the same as `earnest verify`. */
|
|
2
|
+
export const DEFAULT_MAX_STATES = 5_000_000;
|
|
3
|
+
/** Checks a caller's `maxStates`, or gives `DEFAULT_MAX_STATES` when it is `undefined` or `null`. */
|
|
4
|
+
export const stateLimit = (given) => {
|
|
5
|
+
const maxStates = given ?? DEFAULT_MAX_STATES;
|
|
6
|
+
if (!Number.isInteger(maxStates) || maxStates < 0) {
|
|
7
|
+
throw new Error(`maxStates must be a whole number that is not negative, not ${maxStates}`);
|
|
8
|
+
}
|
|
9
|
+
return maxStates;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* The one breadth-first loop that `search` and `explore` share. States with the same `key` are
|
|
13
|
+
* one state, counted once. When counting a state would pass `maxStates`, the search stops going
|
|
14
|
+
* on, and every counted state not yet expanded is judged.
|
|
15
|
+
*/
|
|
16
|
+
export const breadthFirst = (starts, key, maxStates, visitor) => {
|
|
17
|
+
const seen = new Set();
|
|
18
|
+
const visits = [];
|
|
19
|
+
const hasRoom = (state, parent) => {
|
|
20
|
+
const found = key(state);
|
|
21
|
+
if (seen.has(found)) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
if (seen.size >= maxStates) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
seen.add(found);
|
|
28
|
+
visits.push({ state, parent });
|
|
29
|
+
return true;
|
|
30
|
+
};
|
|
31
|
+
let isWithin = true;
|
|
32
|
+
for (const start of starts) {
|
|
33
|
+
isWithin = hasRoom(start, null);
|
|
34
|
+
if (!isWithin) {
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
let expanded = 0;
|
|
39
|
+
for (const from of visits) {
|
|
40
|
+
if (!isWithin) {
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
expanded += 1;
|
|
44
|
+
for (const { move, next } of visitor.expand(from)) {
|
|
45
|
+
isWithin = hasRoom(next, { from, move });
|
|
46
|
+
if (!isWithin) {
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
for (const counted of visits.slice(expanded)) {
|
|
52
|
+
visitor.judge(counted);
|
|
53
|
+
}
|
|
54
|
+
return { visits, reason: isWithin ? null : `state limit of ${maxStates}` };
|
|
55
|
+
};
|
|
56
|
+
/** The shortest trail to a counted state, from the start state it was reached from. */
|
|
57
|
+
export const trailTo = (target) => {
|
|
58
|
+
const steps = [];
|
|
59
|
+
let at = target;
|
|
60
|
+
while (at.parent !== null) {
|
|
61
|
+
steps.push({ move: at.parent.move, state: at.state });
|
|
62
|
+
at = at.parent.from;
|
|
63
|
+
}
|
|
64
|
+
return { start: at.state, steps: steps.reverse() };
|
|
65
|
+
};
|
|
66
|
+
const STUCK = "no move can happen here, and the caller does not mark it as an end";
|
|
67
|
+
class Judge {
|
|
68
|
+
#input;
|
|
69
|
+
#rules;
|
|
70
|
+
#firstBroken = new Map();
|
|
71
|
+
#stuck = null;
|
|
72
|
+
#outside = null;
|
|
73
|
+
constructor(input) {
|
|
74
|
+
this.#input = input;
|
|
75
|
+
this.#rules = input.rules ?? [];
|
|
76
|
+
}
|
|
77
|
+
expand(at) {
|
|
78
|
+
if (!this.#isInRange(at)) {
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
const moves = this.#input.moves(at.state);
|
|
82
|
+
if (moves.length === 0 && !(this.#input.isEnd?.(at.state) ?? false)) {
|
|
83
|
+
this.#stuck ??= at;
|
|
84
|
+
}
|
|
85
|
+
return moves;
|
|
86
|
+
}
|
|
87
|
+
judge(at) {
|
|
88
|
+
this.#isInRange(at);
|
|
89
|
+
}
|
|
90
|
+
#isInRange(at) {
|
|
91
|
+
const message = this.#input.outside?.(at.state) ?? null;
|
|
92
|
+
if (message !== null) {
|
|
93
|
+
this.#outside ??= { at, message };
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
for (const [index, rule] of this.#rules.entries()) {
|
|
97
|
+
if (!rule.holds(at.state) && !this.#firstBroken.has(index)) {
|
|
98
|
+
this.#firstBroken.set(index, at);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
outcome({ visits, reason }) {
|
|
104
|
+
const findings = [];
|
|
105
|
+
if (this.#stuck !== null) {
|
|
106
|
+
findings.push({ kind: "stuck", message: STUCK, path: trailTo(this.#stuck) });
|
|
107
|
+
}
|
|
108
|
+
if (this.#outside !== null) {
|
|
109
|
+
const { at, message } = this.#outside;
|
|
110
|
+
findings.push({ kind: "out-of-range", message, path: trailTo(at) });
|
|
111
|
+
}
|
|
112
|
+
const unbroken = reason === null && this.#outside === null ? "holds" : "unknown";
|
|
113
|
+
const rules = this.#rules.map((rule, index) => {
|
|
114
|
+
const first = this.#firstBroken.get(index);
|
|
115
|
+
return {
|
|
116
|
+
id: rule.id,
|
|
117
|
+
says: rule.says,
|
|
118
|
+
verdict: first === undefined ? unbroken : "broken",
|
|
119
|
+
path: first === undefined ? null : trailTo(first),
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
const hasFailed = findings.length > 0 || rules.some((rule) => rule.verdict === "broken");
|
|
123
|
+
const unfailed = reason === null ? "pass" : "unfinished";
|
|
124
|
+
return {
|
|
125
|
+
status: hasFailed ? "fail" : unfailed,
|
|
126
|
+
states: visits.length,
|
|
127
|
+
rules,
|
|
128
|
+
findings,
|
|
129
|
+
reason,
|
|
130
|
+
*reached() {
|
|
131
|
+
for (const visit of visits) {
|
|
132
|
+
yield trailTo(visit);
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Searches every state reachable from `starts` through `moves`, breadth first, for states that
|
|
140
|
+
* are not models: a plain object, a snapshot of a state machine, anything `key` can name. It
|
|
141
|
+
* gives what `explore` gives, with trails of the caller's own moves and states as paths: the
|
|
142
|
+
* count of distinct states, each rule's verdict with the shortest trail to a state that breaks
|
|
143
|
+
* it, the first stuck and out-of-range states, and the shortest trail to every state it
|
|
144
|
+
* reached. States are judged in the order they are reached: start states in the order given,
|
|
145
|
+
* then the moves of each state in the order `moves` lists them.
|
|
146
|
+
*/
|
|
147
|
+
export const search = (input) => {
|
|
148
|
+
const judge = new Judge(input);
|
|
149
|
+
return judge.outcome(breadthFirst(input.starts, (state) => input.key(state), stateLimit(input.maxStates), judge));
|
|
150
|
+
};
|
package/dist/declarations.d.ts
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
|
-
import { type Json, LockValue, MapValue, OwnedLockValue, QueueValue, RequestValue, RetryValue, SeqValue, SetValue, TimerValue } from "./values.ts";
|
|
1
|
+
import { type Json, LockValue, MapValue, OwnedLockValue, QueueValue, RequestValue, RetryValue, type Row, SeqValue, SetValue, TimerValue } from "./values.ts";
|
|
2
2
|
/** The most values `Declaration.values` will list before it throws instead. */
|
|
3
3
|
export declare const MOST_VALUES_LISTED = 1000000;
|
|
4
4
|
/**
|
|
5
5
|
* What one kind of declaration knows about its values. `rest` is where a field of this kind
|
|
6
6
|
* starts when it has no `.initial`; `null` means it starts at every value. `adopt` turns what
|
|
7
7
|
* `.initial` accepts into a value, and `decode` reads a value back from its JSON form, throwing
|
|
8
|
-
* when the JSON is of another kind.
|
|
8
|
+
* when the JSON is of another kind. `libraryKind` is the kind of the first part whose values are
|
|
9
|
+
* objects of this library, or `null` when every value is plain JavaScript. `source` is the `m`
|
|
10
|
+
* expression that builds it. `fields` is a record's declaration of each field, and `inner` the
|
|
11
|
+
* declaration an optional adds `null` to; other kinds leave both out.
|
|
9
12
|
*/
|
|
10
13
|
export interface Shape<T, In = T> {
|
|
11
14
|
readonly kind: string;
|
|
15
|
+
readonly source: string;
|
|
12
16
|
readonly count: number;
|
|
17
|
+
readonly libraryKind: string | null;
|
|
18
|
+
readonly fields?: Fields;
|
|
19
|
+
readonly inner?: Declaration<unknown, never>;
|
|
13
20
|
readonly rest: {
|
|
14
21
|
readonly value: T;
|
|
15
22
|
} | null;
|
|
@@ -34,6 +41,32 @@ export declare class Declaration<T, In = T> {
|
|
|
34
41
|
get kind(): string;
|
|
35
42
|
/** How many values the declaration has. Very large counts are approximate. */
|
|
36
43
|
get count(): number;
|
|
44
|
+
/**
|
|
45
|
+
* Where the values are not plain JavaScript, the kind of the first part that holds objects of
|
|
46
|
+
* this library, such as `SetValue`: `"set"` for `m.set(item)`, also inside `.optional()` or a
|
|
47
|
+
* record. `null` when every value is a boolean, a number, a text, `null` or a record of those,
|
|
48
|
+
* values that any JavaScript code can hold, copy and compare by content.
|
|
49
|
+
*/
|
|
50
|
+
get libraryKind(): string | null;
|
|
51
|
+
/**
|
|
52
|
+
* The `m` expression that declares the same values, as a model file writes it, such as
|
|
53
|
+
* `m.int(0, 3).optional()` or `m.record({ seat: m.enum(["A", "B"]) })`. It leaves out
|
|
54
|
+
* `.initial`. Code that writes model files puts it after `m.model`'s field names.
|
|
55
|
+
*/
|
|
56
|
+
get source(): string;
|
|
57
|
+
/**
|
|
58
|
+
* For a declaration made with `m.record`, the declaration of each field, as given to
|
|
59
|
+
* `m.record`; `null` for every other kind, an optional record included. Code that checks a
|
|
60
|
+
* value field by field reads it, as `@earnestjs/xstate` does to let an optional field inside a
|
|
61
|
+
* record be missing.
|
|
62
|
+
*/
|
|
63
|
+
get fields(): Fields | null;
|
|
64
|
+
/**
|
|
65
|
+
* For a declaration made with `.optional()`, the same declaration without `null` and without
|
|
66
|
+
* `.initial`; `null` for every other kind. Code that checks a value against the declaration
|
|
67
|
+
* step by step reads it, as `fields` is read for a record.
|
|
68
|
+
*/
|
|
69
|
+
get inner(): Declaration<unknown, never> | null;
|
|
37
70
|
/**
|
|
38
71
|
* Every value, in the order the checker tries them: `false` before `true`, numbers upward,
|
|
39
72
|
* enum values as listed, `null` before the values of an optional, a record's fields in the
|
|
@@ -64,8 +97,25 @@ export declare class Declaration<T, In = T> {
|
|
|
64
97
|
*/
|
|
65
98
|
optional(): Declaration<T | null, In | null>;
|
|
66
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* The value a field declared as `declared` starts at when it is given `value` as `.initial`
|
|
102
|
+
* would be. Throws when `value` is not a value of the declaration.
|
|
103
|
+
*/
|
|
104
|
+
export declare const startingAt: (declared: Declaration<unknown, never>, value: unknown) => unknown;
|
|
67
105
|
/** The fields of a model or a record, or the inputs of an action: a declaration for each name. */
|
|
68
106
|
export type Fields = Readonly<Record<string, Declaration<unknown, never>>>;
|
|
107
|
+
/**
|
|
108
|
+
* Every row of values the declarations in `entries` give, in the order the checker tries an
|
|
109
|
+
* action's inputs: the first name varies fastest.
|
|
110
|
+
*/
|
|
111
|
+
export declare const rowsOf: (entries: readonly (readonly [string, Declaration<unknown, never>])[]) => readonly Row[];
|
|
112
|
+
/**
|
|
113
|
+
* Every combination of one value for each of `fields`, in the order `explore` tries an action's
|
|
114
|
+
* inputs: the first field varies fastest, each through its values in the order of `values()`.
|
|
115
|
+
* Gives one empty row when there are no fields. Throws, before it lists any, when there are more
|
|
116
|
+
* than `MOST_VALUES_LISTED` combinations.
|
|
117
|
+
*/
|
|
118
|
+
export declare const combinationsOf: (fields: Fields) => readonly Row[];
|
|
69
119
|
/** The type of the values a declaration has. */
|
|
70
120
|
export type ValueOf<D> = D extends Declaration<infer T, infer _In> ? T : never;
|
|
71
121
|
/** The object type with one read-only property per declared name. */
|