@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/dist/model.d.ts
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { type Declaration, type Fields, type StateOf } from "./declarations.ts";
|
|
2
|
+
import { type Row } from "./values.ts";
|
|
3
|
+
/** The inputs of an action that takes none. */
|
|
4
|
+
export type NoInput = Readonly<Record<never, never>>;
|
|
5
|
+
/**
|
|
6
|
+
* The fields an action's `changes` gives back, with every name that is not a field of the model
|
|
7
|
+
* typed as `never`, so that a misspelt field is a compile error.
|
|
8
|
+
*/
|
|
9
|
+
export type OnlyFields<F extends Fields, Changes> = Changes & {
|
|
10
|
+
readonly [Stray in Exclude<keyof Changes, keyof F>]: never;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* What `m.action` is given, with its keys in any order. `input` declares what the action is
|
|
14
|
+
* given when it happens, and types the second parameter of `when` and `changes`; leave it out
|
|
15
|
+
* for an action that takes none. `when` says in which states, and for which inputs, the action
|
|
16
|
+
* can happen; leave it out for an action that always can. `changes` gives back only the fields
|
|
17
|
+
* that change, and must not change the state it is given. `fair: true` says the action is not
|
|
18
|
+
* put off forever: a run in which it stays possible and never changes the state breaks no
|
|
19
|
+
* `m.eventually` rule.
|
|
20
|
+
*/
|
|
21
|
+
export interface ActionSpec<F extends Fields, I extends Fields, Changes extends Partial<StateOf<F>> = Partial<StateOf<F>>> {
|
|
22
|
+
readonly input?: I;
|
|
23
|
+
readonly when?: (state: StateOf<F>, input: StateOf<I>) => boolean;
|
|
24
|
+
readonly changes: (state: StateOf<F>, input: StateOf<I>) => OnlyFields<F, Changes>;
|
|
25
|
+
readonly fair?: boolean;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* One thing that can happen in a model. Export it from the model file: the name of the export is
|
|
29
|
+
* its id. `when` is always present here, and always true for an action declared without one.
|
|
30
|
+
*/
|
|
31
|
+
export interface Action<F extends Fields, I extends Fields = NoInput> {
|
|
32
|
+
readonly kind: "action";
|
|
33
|
+
readonly sentence: string;
|
|
34
|
+
readonly input: I | undefined;
|
|
35
|
+
readonly when: (state: StateOf<F>, input: StateOf<I>) => boolean;
|
|
36
|
+
readonly changes: (state: StateOf<F>, input: StateOf<I>) => Partial<StateOf<F>>;
|
|
37
|
+
}
|
|
38
|
+
/** A rule every reachable state must meet. The name of its export is its id. */
|
|
39
|
+
export interface Rule<F extends Fields> {
|
|
40
|
+
readonly kind: "rule";
|
|
41
|
+
readonly says: string;
|
|
42
|
+
readonly holds: (state: StateOf<F>) => boolean;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* States where stopping is correct. A state with no possible action that no end covers is
|
|
46
|
+
* stuck. The name of its export is its id.
|
|
47
|
+
*/
|
|
48
|
+
export interface End<F extends Fields> {
|
|
49
|
+
readonly kind: "end";
|
|
50
|
+
readonly says: string;
|
|
51
|
+
readonly when: (state: StateOf<F>) => boolean;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Other values for some fields to start at: `earnest verify` checks the model once more from
|
|
55
|
+
* them, and `explore` does when it is given the setup. The name of its export is its id.
|
|
56
|
+
*/
|
|
57
|
+
export interface Setup<F extends Fields> {
|
|
58
|
+
readonly kind: "setup";
|
|
59
|
+
readonly says: string;
|
|
60
|
+
readonly starts: Partial<StateOf<F>>;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* What `m.eventually` is given. `reaches` is the condition every run must come to. `after` says
|
|
64
|
+
* where the rule starts: in every reachable state where it is true. Without `after` the rule
|
|
65
|
+
* starts in every start state.
|
|
66
|
+
*/
|
|
67
|
+
export interface EventuallySpec<F extends Fields> {
|
|
68
|
+
readonly after?: (state: StateOf<F>) => boolean;
|
|
69
|
+
readonly reaches: (state: StateOf<F>) => boolean;
|
|
70
|
+
}
|
|
71
|
+
/** A rule about whole runs: a state every run reaches. The name of its export is its id. */
|
|
72
|
+
export interface Eventually<F extends Fields> {
|
|
73
|
+
readonly kind: "eventually";
|
|
74
|
+
readonly says: string;
|
|
75
|
+
readonly after: ((state: StateOf<F>) => boolean) | undefined;
|
|
76
|
+
readonly reaches: (state: StateOf<F>) => boolean;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* One step of a witness: an action with the value of each of its inputs. Make one with
|
|
80
|
+
* `m.step(action, { name: value })`; an action without inputs is a step as it is.
|
|
81
|
+
*/
|
|
82
|
+
export interface Step<F extends Fields> {
|
|
83
|
+
readonly kind: "step";
|
|
84
|
+
readonly action: ActionOf<F>;
|
|
85
|
+
readonly input: Row;
|
|
86
|
+
}
|
|
87
|
+
/** An action of a model with the fields `F`, whatever its inputs are. */
|
|
88
|
+
export interface ActionOf<F extends Fields> {
|
|
89
|
+
readonly kind: "action";
|
|
90
|
+
readonly sentence: string;
|
|
91
|
+
readonly when: (state: StateOf<F>, input: never) => boolean;
|
|
92
|
+
readonly changes: (state: StateOf<F>, input: never) => Partial<StateOf<F>>;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* What `m.witness` is given: the actions to take in order, and the condition that must be true
|
|
96
|
+
* after the last one. With `setup`, the path is played from that setup's start states and the
|
|
97
|
+
* witness is reported with that setup; without it, from the model's own start states.
|
|
98
|
+
*/
|
|
99
|
+
export interface WitnessSpec<F extends Fields> {
|
|
100
|
+
readonly setup?: Setup<F>;
|
|
101
|
+
readonly steps: readonly (Action<F> | Step<F>)[];
|
|
102
|
+
readonly expect: (state: StateOf<F>) => boolean;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* A path the model must be able to take from every start state, with a condition on the state
|
|
106
|
+
* it ends in. The name of its export is its id.
|
|
107
|
+
*/
|
|
108
|
+
export interface Witness<F extends Fields> {
|
|
109
|
+
readonly kind: "witness";
|
|
110
|
+
readonly says: string;
|
|
111
|
+
readonly setup: Setup<F> | undefined;
|
|
112
|
+
readonly steps: readonly Step<F>[];
|
|
113
|
+
readonly expect: (state: StateOf<F>) => boolean;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* What `m.view` is given, all of it optional: the chart's title, the label of a state, the
|
|
117
|
+
* group a state is drawn in, arrow labels by action id (the default is the action's sentence)
|
|
118
|
+
* and colors.
|
|
119
|
+
*/
|
|
120
|
+
export interface ViewSpec<F extends Fields> {
|
|
121
|
+
readonly title?: string;
|
|
122
|
+
readonly state?: (state: StateOf<F>) => string;
|
|
123
|
+
readonly group?: (state: StateOf<F>) => string;
|
|
124
|
+
readonly step?: Readonly<Record<string, string>>;
|
|
125
|
+
readonly colors?: {
|
|
126
|
+
readonly broken?: string;
|
|
127
|
+
readonly start?: string;
|
|
128
|
+
readonly end?: string;
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/** How `earnest chart` draws a model. It changes nothing about what is checked. */
|
|
132
|
+
export interface View<F extends Fields> extends ViewSpec<F> {
|
|
133
|
+
readonly kind: "view";
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* An action as a search runs it: on states and inputs whose types are not known statically.
|
|
137
|
+
* `part` is the `Action` the model file exports. `when` and `changes` throw when the state or
|
|
138
|
+
* the input is outside what was declared; `changes` gives back the whole next state.
|
|
139
|
+
*/
|
|
140
|
+
export interface ActionRunner {
|
|
141
|
+
readonly part: object;
|
|
142
|
+
readonly sentence: string;
|
|
143
|
+
readonly fair?: boolean;
|
|
144
|
+
readonly inputs: readonly (readonly [string, Declaration<unknown, never>])[];
|
|
145
|
+
when(state: Row, input: Row): boolean;
|
|
146
|
+
changes(state: Row, input: Row): Row;
|
|
147
|
+
}
|
|
148
|
+
/** A rule as a search runs it. `holds` throws when the state is outside what was declared. */
|
|
149
|
+
export interface RuleRunner {
|
|
150
|
+
readonly part: object;
|
|
151
|
+
readonly says: string;
|
|
152
|
+
holds(state: Row): boolean;
|
|
153
|
+
}
|
|
154
|
+
/** An end as a search runs it. `when` throws when the state is outside what was declared. */
|
|
155
|
+
export interface EndRunner {
|
|
156
|
+
readonly part: object;
|
|
157
|
+
readonly says: string;
|
|
158
|
+
when(state: Row): boolean;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* A rule about runs as a search judges it. `after` is `null` for a rule that starts in every
|
|
162
|
+
* start state. Both throw when the state is outside what was declared.
|
|
163
|
+
*/
|
|
164
|
+
export interface EventuallyRunner {
|
|
165
|
+
readonly part: object;
|
|
166
|
+
readonly says: string;
|
|
167
|
+
readonly after: ((state: Row) => boolean) | null;
|
|
168
|
+
reaches(state: Row): boolean;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* A witness as a search plays it: each step is an action of the same `Runners` with its input.
|
|
172
|
+
* `setup` is the `part` of the setup it is played in, when it names one. `expect` throws when
|
|
173
|
+
* the state is outside what was declared.
|
|
174
|
+
*/
|
|
175
|
+
export interface WitnessRunner {
|
|
176
|
+
readonly part: object;
|
|
177
|
+
readonly says: string;
|
|
178
|
+
readonly setup?: object;
|
|
179
|
+
readonly steps: readonly {
|
|
180
|
+
readonly action: ActionRunner;
|
|
181
|
+
readonly input: Row;
|
|
182
|
+
}[];
|
|
183
|
+
expect(state: Row): boolean;
|
|
184
|
+
}
|
|
185
|
+
/** A setup as a search starts in it: `starts` holds the fields that start elsewhere. */
|
|
186
|
+
export interface SetupRunner {
|
|
187
|
+
readonly part: object;
|
|
188
|
+
readonly says: string;
|
|
189
|
+
readonly starts: Row;
|
|
190
|
+
}
|
|
191
|
+
/** Everything declared on a model so far, each list in declaration order. */
|
|
192
|
+
export interface Runners {
|
|
193
|
+
readonly actions: readonly ActionRunner[];
|
|
194
|
+
readonly rules: readonly RuleRunner[];
|
|
195
|
+
readonly ends: readonly EndRunner[];
|
|
196
|
+
readonly eventually?: readonly EventuallyRunner[];
|
|
197
|
+
readonly witnesses?: readonly WitnessRunner[];
|
|
198
|
+
readonly setups?: readonly SetupRunner[];
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* What `explore` and `replay` need of a model, whatever its fields are. Every `Model` is one.
|
|
202
|
+
*/
|
|
203
|
+
export interface AnyModel {
|
|
204
|
+
readonly name: string;
|
|
205
|
+
readonly fields: Fields;
|
|
206
|
+
starts(): readonly Row[];
|
|
207
|
+
runners(): Runners;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* A model: named fields, and the actions, rules, ends and views that `m.action`, `m.rule`,
|
|
211
|
+
* `m.end` and `m.view` have declared on it, in the order they were called, which is the order a
|
|
212
|
+
* search tries actions in.
|
|
213
|
+
*/
|
|
214
|
+
export declare class Model<F extends Fields> {
|
|
215
|
+
#private;
|
|
216
|
+
readonly name: string;
|
|
217
|
+
readonly fields: F;
|
|
218
|
+
/** Model files call `m.model(name, fields)` instead. */
|
|
219
|
+
constructor(name: string, fields: F);
|
|
220
|
+
/** Whether `state` has exactly the model's fields, each at a value its declaration has. */
|
|
221
|
+
has(state: unknown): state is StateOf<F>;
|
|
222
|
+
/**
|
|
223
|
+
* Every start state, in the order the checker visits them: fields in declaration order, the
|
|
224
|
+
* first field varying slowest.
|
|
225
|
+
*/
|
|
226
|
+
starts(): readonly StateOf<F>[];
|
|
227
|
+
/** The views declared so far, in declaration order. */
|
|
228
|
+
get views(): readonly View<F>[];
|
|
229
|
+
/**
|
|
230
|
+
* The actions, rules, ends, rules about runs, witnesses and setups declared so far, in a form a search can run without knowing
|
|
231
|
+
* the model's types. `explore` and `replay` are built on this.
|
|
232
|
+
*/
|
|
233
|
+
runners(): Runners;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Declares a model: a name and its fields, each built with an `m` builder. The state type is
|
|
237
|
+
* inferred from the fields; `m.Infer<typeof Model>` names it.
|
|
238
|
+
*/
|
|
239
|
+
export declare const model: <F extends Fields>(name: string, fields: F) => Model<F>;
|
|
240
|
+
/**
|
|
241
|
+
* Declares something that can happen in `model`. `sentence` says it in the reviewer's words.
|
|
242
|
+
* Export the result from the model file: the name of the export is the action's id. Actions are
|
|
243
|
+
* tried in the order they are declared. `input`, `when` and `changes` may be written in any
|
|
244
|
+
* order; `changes` is the only one that is required.
|
|
245
|
+
*/
|
|
246
|
+
export declare const action: <F extends Fields, I extends Fields = NoInput, const Changes extends Partial<StateOf<F>> = Partial<StateOf<F>>>(model: Model<F>, sentence: string, spec: ActionSpec<F, I, Changes>) => Action<F, I>;
|
|
247
|
+
/**
|
|
248
|
+
* Declares a rule of `model`: `holds` must be true in every state the model can reach.
|
|
249
|
+
* `sentence` says it in the reviewer's words. Export the result from the model file: the name
|
|
250
|
+
* of the export is the rule's id.
|
|
251
|
+
*/
|
|
252
|
+
export declare const rule: <F extends Fields>(model: Model<F>, sentence: string, holds: (state: StateOf<F>) => boolean) => Rule<F>;
|
|
253
|
+
/**
|
|
254
|
+
* Declares where stopping is correct in `model`, so that those states are not reported as
|
|
255
|
+
* stuck. `sentence` says it in the reviewer's words. Export the result from the model file: the
|
|
256
|
+
* name of the export is the end's id.
|
|
257
|
+
*/
|
|
258
|
+
export declare const end: <F extends Fields>(model: Model<F>, sentence: string, when: (state: StateOf<F>) => boolean) => End<F>;
|
|
259
|
+
/**
|
|
260
|
+
* Declares a rule about whole runs of `model`: every run comes to a state where `reaches` is
|
|
261
|
+
* true. With `after`, the rule starts in every reachable state where `after` is true, and the
|
|
262
|
+
* state `reaches` wants may be that state or any later one; without it, the rule starts in every
|
|
263
|
+
* start state. A run never stops while an action can still change the state, and never puts off
|
|
264
|
+
* forever an action declared with `fair: true` that stays possible. `sentence` says the rule in
|
|
265
|
+
* the reviewer's words. Export the result from the model file: the name of the export is the
|
|
266
|
+
* rule's id.
|
|
267
|
+
*/
|
|
268
|
+
export declare const eventually: <F extends Fields>(model: Model<F>, sentence: string, spec: EventuallySpec<F>) => Eventually<F>;
|
|
269
|
+
/**
|
|
270
|
+
* Declares a setup of `model`: the model checked once more, with the fields in `starts` starting
|
|
271
|
+
* at the values given and every other field starting where the model declares. A field no action
|
|
272
|
+
* changes works as a parameter of the model this way. `sentence` says what the setup stands for.
|
|
273
|
+
* Export the result from the model file: the name of the export is the setup's id. Throws when
|
|
274
|
+
* `starts` is empty, names something that is not a field, or gives a field a value it does not
|
|
275
|
+
* declare.
|
|
276
|
+
*/
|
|
277
|
+
export declare const setup: <F extends Fields, const Starts extends Partial<StateOf<F>>>(model: Model<F>, sentence: string, starts: OnlyFields<F, Starts>) => Setup<F>;
|
|
278
|
+
/**
|
|
279
|
+
* Makes a step of a witness from an action that takes inputs: `m.step(press, { by: "mouse" })`.
|
|
280
|
+
* `input` gives every input of the action a value it declares.
|
|
281
|
+
*/
|
|
282
|
+
export declare const step: <F extends Fields, I extends Fields>(taken: Action<F, I>, input: StateOf<I>) => Step<F>;
|
|
283
|
+
/**
|
|
284
|
+
* Declares a witness of `model`: a path it must be able to take. From every start state, each
|
|
285
|
+
* step in `steps` must be able to happen in turn, and `expect` must be true in the state after
|
|
286
|
+
* the last one. `sentence` says it in the reviewer's words. Export the result from the model
|
|
287
|
+
* file: the name of the export is the witness's id. `earnest verify` and `explore` play it and
|
|
288
|
+
* report it with the rules. Throws when a step is not an action declared on `model` before the
|
|
289
|
+
* witness, or gives an input the action does not declare.
|
|
290
|
+
*/
|
|
291
|
+
export declare const witness: <F extends Fields>(model: Model<F>, sentence: string, spec: WitnessSpec<F>) => Witness<F>;
|
|
292
|
+
/**
|
|
293
|
+
* Declares how `earnest chart` draws `model`. A model needs no view, and a view has no id, so
|
|
294
|
+
* its constant needs no export. In a model file, write the second argument, even as `{}`: the
|
|
295
|
+
* checker refuses `m.view(model)`.
|
|
296
|
+
*/
|
|
297
|
+
export declare const view: <F extends Fields>(model: Model<F>, spec?: ViewSpec<F>) => View<F>;
|
package/dist/model.js
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { record, startingAt } from "./declarations.js";
|
|
2
|
+
import { frozen, isRecord } from "./values.js";
|
|
3
|
+
let partsOf;
|
|
4
|
+
/**
|
|
5
|
+
* A model: named fields, and the actions, rules, ends and views that `m.action`, `m.rule`,
|
|
6
|
+
* `m.end` and `m.view` have declared on it, in the order they were called, which is the order a
|
|
7
|
+
* search tries actions in.
|
|
8
|
+
*/
|
|
9
|
+
export class Model {
|
|
10
|
+
name;
|
|
11
|
+
fields;
|
|
12
|
+
#state;
|
|
13
|
+
#parts = {
|
|
14
|
+
actions: [],
|
|
15
|
+
rules: [],
|
|
16
|
+
ends: [],
|
|
17
|
+
eventually: [],
|
|
18
|
+
witnesses: [],
|
|
19
|
+
setups: [],
|
|
20
|
+
views: [],
|
|
21
|
+
};
|
|
22
|
+
static {
|
|
23
|
+
partsOf = (model) => model.#parts;
|
|
24
|
+
}
|
|
25
|
+
/** Model files call `m.model(name, fields)` instead. */
|
|
26
|
+
constructor(name, fields) {
|
|
27
|
+
this.name = name;
|
|
28
|
+
this.fields = fields;
|
|
29
|
+
this.#state = record(fields);
|
|
30
|
+
}
|
|
31
|
+
/** Whether `state` has exactly the model's fields, each at a value its declaration has. */
|
|
32
|
+
has(state) {
|
|
33
|
+
return this.#state.has(state);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Every start state, in the order the checker visits them: fields in declaration order, the
|
|
37
|
+
* first field varying slowest.
|
|
38
|
+
*/
|
|
39
|
+
starts() {
|
|
40
|
+
const isState = (row) => this.has(row);
|
|
41
|
+
return Object.entries(this.fields)
|
|
42
|
+
.reduce((partial, [name, declared]) => partial.flatMap((prefix) => declared.starts().map((value) => ({ ...prefix, [name]: value }))), [{}])
|
|
43
|
+
.map(frozen)
|
|
44
|
+
.filter(isState);
|
|
45
|
+
}
|
|
46
|
+
/** The views declared so far, in declaration order. */
|
|
47
|
+
get views() {
|
|
48
|
+
return [...this.#parts.views];
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The actions, rules, ends, rules about runs, witnesses and setups declared so far, in a form a search can run without knowing
|
|
52
|
+
* the model's types. `explore` and `replay` are built on this.
|
|
53
|
+
*/
|
|
54
|
+
runners() {
|
|
55
|
+
const { actions, rules, ends, eventually, witnesses, setups } = this.#parts;
|
|
56
|
+
return {
|
|
57
|
+
actions: [...actions],
|
|
58
|
+
rules: [...rules],
|
|
59
|
+
ends: [...ends],
|
|
60
|
+
eventually: [...eventually],
|
|
61
|
+
witnesses: [...witnesses],
|
|
62
|
+
setups: [...setups],
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const known = (model, state) => {
|
|
67
|
+
if (!model.has(state)) {
|
|
68
|
+
throw new Error(`${model.name}: the state is outside what the model declares`);
|
|
69
|
+
}
|
|
70
|
+
return state;
|
|
71
|
+
};
|
|
72
|
+
const changed = (model, state, changes, sentence) => {
|
|
73
|
+
if (!isRecord(changes)) {
|
|
74
|
+
throw new Error(`${model.name}: the change of "${sentence}" must give back an object holding the fields that change`);
|
|
75
|
+
}
|
|
76
|
+
const stray = Object.keys(changes).find((name) => !Object.hasOwn(model.fields, name));
|
|
77
|
+
if (stray !== undefined) {
|
|
78
|
+
throw new Error(`${model.name}: the change of "${sentence}" gives \`${stray}\`, which is not a field of the model`);
|
|
79
|
+
}
|
|
80
|
+
return frozen({ ...state, ...changes });
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Declares a model: a name and its fields, each built with an `m` builder. The state type is
|
|
84
|
+
* inferred from the fields; `m.Infer<typeof Model>` names it.
|
|
85
|
+
*/
|
|
86
|
+
export const model = (name, fields) => new Model(name, fields);
|
|
87
|
+
/**
|
|
88
|
+
* Declares something that can happen in `model`. `sentence` says it in the reviewer's words.
|
|
89
|
+
* Export the result from the model file: the name of the export is the action's id. Actions are
|
|
90
|
+
* tried in the order they are declared. `input`, `when` and `changes` may be written in any
|
|
91
|
+
* order; `changes` is the only one that is required.
|
|
92
|
+
*/
|
|
93
|
+
export const action = (model, sentence, spec) => {
|
|
94
|
+
const declared = spec.input === undefined ? null : record(spec.input);
|
|
95
|
+
const isInput = (input) => declared === null ? isRecord(input) && Object.keys(input).length === 0 : declared.has(input);
|
|
96
|
+
const knownInput = (input) => {
|
|
97
|
+
if (!isInput(input)) {
|
|
98
|
+
throw new Error(`${model.name}: "${sentence}" was given an input it does not declare`);
|
|
99
|
+
}
|
|
100
|
+
return input;
|
|
101
|
+
};
|
|
102
|
+
const declaredAction = Object.freeze({
|
|
103
|
+
kind: "action",
|
|
104
|
+
sentence,
|
|
105
|
+
input: spec.input,
|
|
106
|
+
when: spec.when ?? (() => true),
|
|
107
|
+
changes: spec.changes,
|
|
108
|
+
});
|
|
109
|
+
partsOf(model).actions.push({
|
|
110
|
+
part: declaredAction,
|
|
111
|
+
sentence,
|
|
112
|
+
fair: spec.fair ?? false,
|
|
113
|
+
inputs: spec.input === undefined ? [] : Object.entries(spec.input),
|
|
114
|
+
when: (state, input) => declaredAction.when(known(model, state), knownInput(input)),
|
|
115
|
+
changes: (state, input) => changed(model, state, declaredAction.changes(known(model, state), knownInput(input)), sentence),
|
|
116
|
+
});
|
|
117
|
+
return declaredAction;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Declares a rule of `model`: `holds` must be true in every state the model can reach.
|
|
121
|
+
* `sentence` says it in the reviewer's words. Export the result from the model file: the name
|
|
122
|
+
* of the export is the rule's id.
|
|
123
|
+
*/
|
|
124
|
+
export const rule = (model, sentence, holds) => {
|
|
125
|
+
const declaredRule = Object.freeze({ kind: "rule", says: sentence, holds });
|
|
126
|
+
partsOf(model).rules.push({
|
|
127
|
+
part: declaredRule,
|
|
128
|
+
says: sentence,
|
|
129
|
+
holds: (state) => declaredRule.holds(known(model, state)),
|
|
130
|
+
});
|
|
131
|
+
return declaredRule;
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* Declares where stopping is correct in `model`, so that those states are not reported as
|
|
135
|
+
* stuck. `sentence` says it in the reviewer's words. Export the result from the model file: the
|
|
136
|
+
* name of the export is the end's id.
|
|
137
|
+
*/
|
|
138
|
+
export const end = (model, sentence, when) => {
|
|
139
|
+
const declaredEnd = Object.freeze({ kind: "end", says: sentence, when });
|
|
140
|
+
partsOf(model).ends.push({
|
|
141
|
+
part: declaredEnd,
|
|
142
|
+
says: sentence,
|
|
143
|
+
when: (state) => declaredEnd.when(known(model, state)),
|
|
144
|
+
});
|
|
145
|
+
return declaredEnd;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Declares a rule about whole runs of `model`: every run comes to a state where `reaches` is
|
|
149
|
+
* true. With `after`, the rule starts in every reachable state where `after` is true, and the
|
|
150
|
+
* state `reaches` wants may be that state or any later one; without it, the rule starts in every
|
|
151
|
+
* start state. A run never stops while an action can still change the state, and never puts off
|
|
152
|
+
* forever an action declared with `fair: true` that stays possible. `sentence` says the rule in
|
|
153
|
+
* the reviewer's words. Export the result from the model file: the name of the export is the
|
|
154
|
+
* rule's id.
|
|
155
|
+
*/
|
|
156
|
+
export const eventually = (model, sentence, spec) => {
|
|
157
|
+
const declaredRule = Object.freeze({
|
|
158
|
+
kind: "eventually",
|
|
159
|
+
says: sentence,
|
|
160
|
+
after: spec.after,
|
|
161
|
+
reaches: spec.reaches,
|
|
162
|
+
});
|
|
163
|
+
const { after } = declaredRule;
|
|
164
|
+
partsOf(model).eventually.push({
|
|
165
|
+
part: declaredRule,
|
|
166
|
+
says: sentence,
|
|
167
|
+
after: after === undefined ? null : (state) => after(known(model, state)),
|
|
168
|
+
reaches: (state) => declaredRule.reaches(known(model, state)),
|
|
169
|
+
});
|
|
170
|
+
return declaredRule;
|
|
171
|
+
};
|
|
172
|
+
/**
|
|
173
|
+
* Declares a setup of `model`: the model checked once more, with the fields in `starts` starting
|
|
174
|
+
* at the values given and every other field starting where the model declares. A field no action
|
|
175
|
+
* changes works as a parameter of the model this way. `sentence` says what the setup stands for.
|
|
176
|
+
* Export the result from the model file: the name of the export is the setup's id. Throws when
|
|
177
|
+
* `starts` is empty, names something that is not a field, or gives a field a value it does not
|
|
178
|
+
* declare.
|
|
179
|
+
*/
|
|
180
|
+
export const setup = (model, sentence, starts) => {
|
|
181
|
+
const names = Object.keys(starts);
|
|
182
|
+
if (names.length === 0) {
|
|
183
|
+
throw new Error(`${model.name}: the setup "${sentence}" starts no field at another value`);
|
|
184
|
+
}
|
|
185
|
+
const adopted = {};
|
|
186
|
+
for (const [name, value] of Object.entries(starts)) {
|
|
187
|
+
const declared = Object.hasOwn(model.fields, name)
|
|
188
|
+
? model.fields[name]
|
|
189
|
+
: undefined;
|
|
190
|
+
if (declared === undefined) {
|
|
191
|
+
throw new Error(`${model.name}: the setup "${sentence}" starts \`${name}\`, which is not a field of the model`);
|
|
192
|
+
}
|
|
193
|
+
try {
|
|
194
|
+
adopted[name] = startingAt(declared, value);
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
const why = error instanceof Error ? error.message : String(error);
|
|
198
|
+
throw new Error(`${model.name}: the setup "${sentence}" starts \`${name}\` at a value the field does not declare: ${why}`, { cause: error });
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
const declaredSetup = Object.freeze({ kind: "setup", says: sentence, starts });
|
|
202
|
+
partsOf(model).setups.push({ part: declaredSetup, says: sentence, starts: frozen(adopted) });
|
|
203
|
+
return declaredSetup;
|
|
204
|
+
};
|
|
205
|
+
/**
|
|
206
|
+
* Makes a step of a witness from an action that takes inputs: `m.step(press, { by: "mouse" })`.
|
|
207
|
+
* `input` gives every input of the action a value it declares.
|
|
208
|
+
*/
|
|
209
|
+
export const step = (taken, input) => Object.freeze({ kind: "step", action: taken, input: frozen({ ...input }) });
|
|
210
|
+
/**
|
|
211
|
+
* Declares a witness of `model`: a path it must be able to take. From every start state, each
|
|
212
|
+
* step in `steps` must be able to happen in turn, and `expect` must be true in the state after
|
|
213
|
+
* the last one. `sentence` says it in the reviewer's words. Export the result from the model
|
|
214
|
+
* file: the name of the export is the witness's id. `earnest verify` and `explore` play it and
|
|
215
|
+
* report it with the rules. Throws when a step is not an action declared on `model` before the
|
|
216
|
+
* witness, or gives an input the action does not declare.
|
|
217
|
+
*/
|
|
218
|
+
export const witness = (model, sentence, spec) => {
|
|
219
|
+
const { actions, witnesses, setups } = partsOf(model);
|
|
220
|
+
if (spec.setup !== undefined && !setups.some((declared) => declared.part === spec.setup)) {
|
|
221
|
+
throw new Error(`${model.name}: the setup of "${sentence}" is not one declared on this model before the witness`);
|
|
222
|
+
}
|
|
223
|
+
const played = spec.steps.map((written, position) => {
|
|
224
|
+
const taken = written.kind === "step"
|
|
225
|
+
? written
|
|
226
|
+
: Object.freeze({ kind: "step", action: written, input: frozen({}) });
|
|
227
|
+
const runner = actions.find((declared) => declared.part === taken.action);
|
|
228
|
+
const number = position + 1;
|
|
229
|
+
if (runner === undefined) {
|
|
230
|
+
throw new Error(`${model.name}: step ${number} of "${sentence}" is not an action declared on this model before the witness`);
|
|
231
|
+
}
|
|
232
|
+
if (!record(Object.fromEntries(runner.inputs)).has(taken.input)) {
|
|
233
|
+
throw new Error(`${model.name}: step ${number} of "${sentence}" gives "${runner.sentence}" an input it does not declare`);
|
|
234
|
+
}
|
|
235
|
+
return { taken, runner };
|
|
236
|
+
});
|
|
237
|
+
const declaredWitness = Object.freeze({
|
|
238
|
+
kind: "witness",
|
|
239
|
+
says: sentence,
|
|
240
|
+
setup: spec.setup,
|
|
241
|
+
steps: Object.freeze(played.map(({ taken }) => taken)),
|
|
242
|
+
expect: spec.expect,
|
|
243
|
+
});
|
|
244
|
+
witnesses.push({
|
|
245
|
+
part: declaredWitness,
|
|
246
|
+
says: sentence,
|
|
247
|
+
...(spec.setup === undefined ? {} : { setup: spec.setup }),
|
|
248
|
+
steps: played.map(({ taken, runner }) => ({ action: runner, input: taken.input })),
|
|
249
|
+
expect: (state) => declaredWitness.expect(known(model, state)),
|
|
250
|
+
});
|
|
251
|
+
return declaredWitness;
|
|
252
|
+
};
|
|
253
|
+
/**
|
|
254
|
+
* Declares how `earnest chart` draws `model`. A model needs no view, and a view has no id, so
|
|
255
|
+
* its constant needs no export. In a model file, write the second argument, even as `{}`: the
|
|
256
|
+
* checker refuses `m.view(model)`.
|
|
257
|
+
*/
|
|
258
|
+
export const view = (model, spec = {}) => {
|
|
259
|
+
const declaredView = Object.freeze({ ...spec, kind: "view" });
|
|
260
|
+
partsOf(model).views.push(declaredView);
|
|
261
|
+
return declaredView;
|
|
262
|
+
};
|
package/dist/replay.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { AnyModel } from "./model.ts";
|
|
2
|
+
import { type Exports, type Named } from "./search.ts";
|
|
3
|
+
import { type Json } from "./values.ts";
|
|
4
|
+
/**
|
|
5
|
+
* One step of a path to replay. `action` is the action's id and `label` its sentence; the
|
|
6
|
+
* action is found by id when the model file's exports are passed, and by sentence otherwise.
|
|
7
|
+
* `state` is the whole state the step must lead to.
|
|
8
|
+
*/
|
|
9
|
+
export interface ReplayStep {
|
|
10
|
+
readonly action: string;
|
|
11
|
+
readonly label?: string;
|
|
12
|
+
readonly input?: readonly Named[];
|
|
13
|
+
readonly state: readonly Named[];
|
|
14
|
+
}
|
|
15
|
+
/** A path as `earnest verify --json` and `explore` give it; `changes` is not needed. */
|
|
16
|
+
export interface ReplayPath {
|
|
17
|
+
readonly start: readonly Named[];
|
|
18
|
+
readonly steps: readonly ReplayStep[];
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* `exports` is the model file's exports, which let a step's `action` id be matched. `setup` is
|
|
22
|
+
* a setup declared on the model with `m.setup`, as `explore` takes it: the path must then begin
|
|
23
|
+
* at one of the setup's start states, as a path of the setup's result in `earnest verify
|
|
24
|
+
* --json` does. Without it the path must begin where the model declares.
|
|
25
|
+
*/
|
|
26
|
+
export interface ReplayOptions {
|
|
27
|
+
readonly exports?: Exports;
|
|
28
|
+
readonly setup?: object;
|
|
29
|
+
}
|
|
30
|
+
/** A state with each field in JSON form. */
|
|
31
|
+
export type StateJson = {
|
|
32
|
+
readonly [field: string]: Json;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Applies a path one action at a time and gives every state it passes through, the start state
|
|
36
|
+
* first, each with its fields in JSON form. Throws an error that names the step when the path
|
|
37
|
+
* does not begin at a start state of the model, or of `options.setup` when it is given, a step names no action of the model, an input is missing or
|
|
38
|
+
* undeclared, the action's `when` is false, the action can't be evaluated, or the state after
|
|
39
|
+
* the action differs from the step's `state`. Set members may be listed in any order.
|
|
40
|
+
*/
|
|
41
|
+
export declare const replay: (model: AnyModel, path: ReplayPath, options?: ReplayOptions) => StateJson[];
|