@earnestjs/model 0.0.0-stage → 0.1.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.
@@ -0,0 +1,391 @@
1
+ import { LockValue, MapValue, ModelValue, OwnedLockValue, QueueValue, REQUEST_STAGES, RequestValue, RetryValue, SeqValue, SetValue, TIMER_STAGES, TimerValue, frozen, isJsonList, isJsonObject, isRecord, toJson, } from "./values.js";
2
+ /** The most values `Declaration.values` will list before it throws instead. */
3
+ export const MOST_VALUES_LISTED = 1_000_000;
4
+ const shown = (value) => value === undefined || typeof value === "function" || typeof value === "symbol"
5
+ ? String(value)
6
+ : JSON.stringify(isRecord(value) || value instanceof ModelValue ? toJson(value) : value);
7
+ const unreadable = (kind, json) => new Error(`${JSON.stringify(json)} can't be read as a ${kind}`);
8
+ const optionalOf = (inner) => ({
9
+ kind: "optional",
10
+ count: inner.count + 1,
11
+ rest: null,
12
+ values: () => [null, ...inner.values()],
13
+ has: (value) => value === null || inner.has(value),
14
+ adopt: (value) => (value === null ? null : inner.adopt(value)),
15
+ decode: (json) => (json === null ? null : inner.decode(json)),
16
+ });
17
+ /**
18
+ * The finite set of values a field, an input or a collection member can take. Build one with
19
+ * `m.boolean()`, `m.int(lo, hi)` and the other builders; it is a plain value, so one declaration
20
+ * can be shared by several fields and models. `.optional()` and `.initial(value)` give back a
21
+ * new declaration and leave this one as it was.
22
+ */
23
+ export class Declaration {
24
+ #shape;
25
+ #initial;
26
+ /** Model files use the `m` builders instead of calling this. */
27
+ constructor(shape, initial = null) {
28
+ this.#shape = shape;
29
+ this.#initial = initial;
30
+ }
31
+ /** What was declared, in words: `"enum"`, `"number"`, `"set"` and so on. */
32
+ get kind() {
33
+ return this.#shape.kind;
34
+ }
35
+ /** How many values the declaration has. Very large counts are approximate. */
36
+ get count() {
37
+ return this.#shape.count;
38
+ }
39
+ /**
40
+ * Every value, in the order the checker tries them: `false` before `true`, numbers upward,
41
+ * enum values as listed, `null` before the values of an optional, a record's fields in the
42
+ * order declared with the first one changing slowest. Throws when there are more than
43
+ * `MOST_VALUES_LISTED`.
44
+ */
45
+ values() {
46
+ if (this.#shape.count > MOST_VALUES_LISTED) {
47
+ throw new Error(`this ${this.kind} has ${this.#shape.count} values, too many to list; declare a smaller one`);
48
+ }
49
+ return this.#shape.values();
50
+ }
51
+ /** Whether `value` is one of the declared values. Any value may be asked about. */
52
+ has(value) {
53
+ return this.#shape.has(value);
54
+ }
55
+ /**
56
+ * The values a field declared this way starts at: the `.initial` value when there is one;
57
+ * otherwise the empty, free, idle or zero value of a collection, lock, timer, request or
58
+ * retry; otherwise every value.
59
+ */
60
+ starts() {
61
+ if (this.#initial !== null) {
62
+ return [this.#initial.value];
63
+ }
64
+ return this.#shape.rest === null ? this.values() : [this.#shape.rest.value];
65
+ }
66
+ /**
67
+ * Reads a value from the form `earnest verify --json` prints. The result may lie outside the
68
+ * declaration, as a value on an out-of-range path does, so check it with `has`. Throws when
69
+ * the JSON is of another kind altogether, such as text where a set is declared.
70
+ */
71
+ fromJson(json) {
72
+ return this.#shape.decode(json);
73
+ }
74
+ /** The same declaration, starting at `value`. Throws when `value` is not a declared value. */
75
+ initial(value) {
76
+ const adopted = frozen(this.#shape.adopt(value));
77
+ if (!this.#shape.has(adopted)) {
78
+ throw new Error(`${shown(adopted)} is not a value of this ${this.kind}`);
79
+ }
80
+ return new Declaration(this.#shape, { value: adopted });
81
+ }
82
+ /**
83
+ * The same declaration with `null` as one more value. Without `.initial` it then starts at
84
+ * every value, `null` included. Throws for a declaration that is already optional and for a
85
+ * lock with an owner, whose free state is already `null`.
86
+ */
87
+ optional() {
88
+ if (this.kind === "optional" || this.kind === "owned lock") {
89
+ throw new Error(`an ${this.kind} can't be made optional: it already has a null value`);
90
+ }
91
+ return new Declaration(optionalOf(this.#shape), this.#initial);
92
+ }
93
+ }
94
+ const plain = (kind, json) => {
95
+ if (typeof json === "object") {
96
+ throw unreadable(kind, json);
97
+ }
98
+ return json;
99
+ };
100
+ const same = (value) => value;
101
+ /** Declares a field that is `true` or `false`. */
102
+ export const booleanOf = () => new Declaration({
103
+ kind: "boolean",
104
+ count: 2,
105
+ rest: null,
106
+ values: () => [false, true],
107
+ has: (value) => typeof value === "boolean",
108
+ adopt: same,
109
+ decode: (json) => plain("boolean", json),
110
+ });
111
+ /**
112
+ * Declares a whole number from `lo` to `hi`, both included. Arithmetic that leaves the range is
113
+ * not an error in the action: the checker reports the state as out of range. Throws when a bound
114
+ * is not a whole number or `lo` is above `hi`.
115
+ */
116
+ export const int = (lo, hi) => {
117
+ if (!Number.isSafeInteger(lo) || !Number.isSafeInteger(hi) || lo > hi) {
118
+ throw new Error(`m.int(${lo}, ${hi}) needs two whole numbers, the lower one first`);
119
+ }
120
+ return new Declaration({
121
+ kind: "number",
122
+ count: hi - lo + 1,
123
+ rest: null,
124
+ values: () => Array.from({ length: hi - lo + 1 }, (_, position) => lo + position),
125
+ has: (value) => typeof value === "number" && Number.isInteger(value) && lo <= value && value <= hi,
126
+ adopt: same,
127
+ decode: (json) => plain("number", json),
128
+ });
129
+ };
130
+ const choice = (builder, options) => {
131
+ const listed = [...options];
132
+ if (listed.length === 0 || new Set(listed).size !== listed.length) {
133
+ throw new Error(`${builder} needs a list of one or more different values`);
134
+ }
135
+ if (new Set(listed.map((option) => typeof option)).size !== 1) {
136
+ throw new Error(`${builder} needs values that are all text, all numbers or all booleans`);
137
+ }
138
+ if (listed.some((option) => typeof option === "number" && !Number.isSafeInteger(option))) {
139
+ throw new Error(`${builder} needs whole numbers that JavaScript holds exactly`);
140
+ }
141
+ return new Declaration({
142
+ kind: "enum",
143
+ count: listed.length,
144
+ rest: null,
145
+ values: () => listed,
146
+ has: (value) => listed.some((option) => option === value),
147
+ adopt: same,
148
+ decode: (json) => plain("enum", json),
149
+ });
150
+ };
151
+ /** Declares a field that is one of the listed texts. Its type is the union of those literals. */
152
+ export const enumOf = (options) => choice("m.enum", options);
153
+ /**
154
+ * Declares a field that is one of the listed values, which are all text, all numbers or all
155
+ * booleans. Its type is the union of those literals.
156
+ */
157
+ export const oneOf = (options) => choice("m.oneOf", options);
158
+ const product = (prefixes, options) => prefixes.flatMap((prefix) => options.map((option) => [...prefix, option]));
159
+ /**
160
+ * Declares a record: an object with exactly the given fields. Read a field with `r.field` and
161
+ * change one with `{ ...r, field: value }`. Compare records field by field: `===` on two records
162
+ * compares the objects, not their content.
163
+ */
164
+ export const record = (fields) => {
165
+ const entries = Object.entries(fields);
166
+ const has = (value) => isRecord(value) &&
167
+ Object.keys(value).length === entries.length &&
168
+ entries.every(([name, declared]) => declared.has(value[name]));
169
+ return new Declaration({
170
+ kind: "record",
171
+ count: entries.reduce((total, [, declared]) => total * declared.count, 1),
172
+ rest: null,
173
+ values: () => entries
174
+ .reduce((partial, [name, declared]) => partial.flatMap((prefix) => declared.values().map((value) => ({ ...prefix, [name]: value }))), [{}])
175
+ .map(frozen)
176
+ .filter(has),
177
+ has,
178
+ adopt: same,
179
+ decode: (json) => {
180
+ if (!isJsonObject(json) || Object.keys(json).length !== entries.length) {
181
+ throw unreadable(`record with the fields ${entries.map(([name]) => name).join(", ")}`, json);
182
+ }
183
+ return Object.fromEntries(entries.map(([name, declared]) => {
184
+ const found = json[name];
185
+ if (found === undefined) {
186
+ throw unreadable(`record with the field ${name}`, json);
187
+ }
188
+ return [name, declared.fromJson(found)];
189
+ }));
190
+ },
191
+ });
192
+ };
193
+ const listOf = (kind, json) => {
194
+ if (!isJsonList(json)) {
195
+ throw unreadable(kind, json);
196
+ }
197
+ return json;
198
+ };
199
+ const subsets = (items) => items.reduce((smaller, item) => [...smaller, ...smaller.map((subset) => subset.add(item))], [SetValue.of([])]);
200
+ const mapsOver = (keys, values) => keys.reduce((smaller, key) => [
201
+ ...smaller,
202
+ ...smaller.flatMap((known) => values.map((value) => known.set(key, value))),
203
+ ], [MapValue.of([])]);
204
+ /** Declares a set of values of `item`. Without `.initial` it starts empty. */
205
+ export const set = (item) => new Declaration({
206
+ kind: "set",
207
+ count: 2 ** item.count,
208
+ rest: { value: SetValue.of([]) },
209
+ values: () => subsets(item.values()),
210
+ has: (value) => value instanceof SetValue && value.every((each) => item.has(each)),
211
+ adopt: same,
212
+ decode: (json) => SetValue.of(listOf("set", json).map((each) => item.fromJson(each))),
213
+ });
214
+ const holdable = (builder, inside) => {
215
+ if (inside.kind === "owned lock") {
216
+ throw new Error(`${builder} can't hold a lock with an owner: a free lock is already null, so reading it could not be told from reading nothing`);
217
+ }
218
+ return inside;
219
+ };
220
+ /**
221
+ * Declares a map from values of `key` to values of `value`. Without `.initial` it starts empty.
222
+ * Throws when `value` is a lock with an owner.
223
+ */
224
+ export const map = (key, valueDeclared) => {
225
+ const value = holdable("m.map", valueDeclared);
226
+ return new Declaration({
227
+ kind: "map",
228
+ count: (value.count + 1) ** key.count,
229
+ rest: { value: MapValue.of([]) },
230
+ values: () => mapsOver(key.values(), value.values()),
231
+ has: (found) => found instanceof MapValue &&
232
+ [...found].every(([each, held]) => key.has(each) && value.has(held)),
233
+ adopt: same,
234
+ decode: (json) => MapValue.of(listOf("map", json).map((pair) => {
235
+ const [each, held, ...rest] = listOf("map entry", pair);
236
+ if (each === undefined || held === undefined || rest.length > 0) {
237
+ throw unreadable("map entry", pair);
238
+ }
239
+ return [key.fromJson(each), value.fromJson(held)];
240
+ })),
241
+ });
242
+ };
243
+ const lists = (items, longest) => {
244
+ const all = [[]];
245
+ let last = [[]];
246
+ for (let length = 0; length < longest; length += 1) {
247
+ last = product(last, items);
248
+ all.push(...last);
249
+ }
250
+ return all;
251
+ };
252
+ const listCount = (each, longest) => Array.from({ length: longest + 1 }, (_, length) => each ** length).reduce((total, ofLength) => total + ofLength, 0);
253
+ const wholeLimit = (builder, limit) => {
254
+ if (!Number.isSafeInteger(limit) || limit < 0) {
255
+ throw new Error(`${builder} needs a whole number that is not negative, not ${limit}`);
256
+ }
257
+ return limit;
258
+ };
259
+ /**
260
+ * Declares a sequence of at most `max` values of `item`. Without `.initial` it starts empty.
261
+ * Pushing past `max` is not an error in the action: the checker reports the state as out of
262
+ * range. Throws when `item` is a lock with an owner.
263
+ */
264
+ export const seq = (itemDeclared, limit) => {
265
+ const item = holdable("m.seq", itemDeclared);
266
+ const max = wholeLimit("m.seq's max", limit.max);
267
+ return new Declaration({
268
+ kind: "sequence",
269
+ count: listCount(item.count, max),
270
+ rest: { value: SeqValue.of([]) },
271
+ values: () => lists(item.values(), max).map((items) => SeqValue.of(items)),
272
+ has: (value) => value instanceof SeqValue && value.size <= max && value.every((each) => item.has(each)),
273
+ adopt: same,
274
+ decode: (json) => SeqValue.of(listOf("sequence", json).map((each) => item.fromJson(each))),
275
+ });
276
+ };
277
+ /**
278
+ * Declares a first-in, first-out queue of at most `capacity` values of `item`. Without
279
+ * `.initial` it starts empty; `.initial(m.seqOf(a, b))` starts it with `a` at the head. Sending
280
+ * to a full queue is not an error in the action: the checker reports the state as out of range.
281
+ * Throws when `item` is a lock with an owner.
282
+ */
283
+ export const queue = (itemDeclared, limit) => {
284
+ const item = holdable("m.queue", itemDeclared);
285
+ const capacity = wholeLimit("m.queue's capacity", limit.capacity);
286
+ return new Declaration({
287
+ kind: "queue",
288
+ count: listCount(item.count, capacity),
289
+ rest: { value: QueueValue.of(capacity, []) },
290
+ values: () => lists(item.values(), capacity).map((items) => QueueValue.of(capacity, items)),
291
+ has: (value) => value instanceof QueueValue &&
292
+ value.capacity === capacity &&
293
+ value.size <= capacity &&
294
+ value.every((each) => item.has(each)),
295
+ adopt: (value) => QueueValue.of(capacity, value),
296
+ decode: (json) => QueueValue.of(capacity, listOf("queue", json).map((each) => item.fromJson(each))),
297
+ });
298
+ };
299
+ const plainLock = () => new Declaration({
300
+ kind: "lock",
301
+ count: 2,
302
+ rest: { value: LockValue.of(false) },
303
+ values: () => [LockValue.of(false), LockValue.of(true)],
304
+ has: (value) => value instanceof LockValue,
305
+ adopt: same,
306
+ decode: (json) => {
307
+ if (typeof json !== "boolean") {
308
+ throw unreadable("lock", json);
309
+ }
310
+ return LockValue.of(json);
311
+ },
312
+ });
313
+ const ownedLock = (owner) => {
314
+ if (owner.kind === "optional") {
315
+ throw new Error("a free lock already has no owner, so the owner can't be optional");
316
+ }
317
+ return new Declaration({
318
+ kind: "owned lock",
319
+ count: owner.count + 1,
320
+ rest: { value: OwnedLockValue.of(null) },
321
+ values: () => [
322
+ OwnedLockValue.of(null),
323
+ ...owner.values().map((each) => OwnedLockValue.of(each)),
324
+ ],
325
+ has: (value) => value instanceof OwnedLockValue && (value.holder === null || owner.has(value.holder)),
326
+ adopt: same,
327
+ decode: (json) => OwnedLockValue.of(json === null ? null : owner.fromJson(json)),
328
+ });
329
+ };
330
+ export function lock(owner) {
331
+ return owner === undefined ? plainLock() : ownedLock(owner);
332
+ }
333
+ const staged = (kind, stages, at, has) => {
334
+ const values = stages.map(at);
335
+ return new Declaration({
336
+ kind,
337
+ count: values.length,
338
+ rest: values[0] === undefined ? null : { value: values[0] },
339
+ values: () => values,
340
+ has,
341
+ adopt: same,
342
+ decode: (json) => {
343
+ const stage = stages.find((each) => each === json);
344
+ if (stage === undefined) {
345
+ throw unreadable(kind, json);
346
+ }
347
+ return at(stage);
348
+ },
349
+ });
350
+ };
351
+ /** Declares a timer that is idle, running or fired. Without `.initial` it starts idle. */
352
+ export const timer = () => staged("timer", TIMER_STAGES, TimerValue.at, (value) => value instanceof TimerValue);
353
+ /**
354
+ * Declares an async request that is idle, pending, succeeded or failed. Without `.initial` it
355
+ * starts idle.
356
+ */
357
+ export const request = () => staged("request", REQUEST_STAGES, RequestValue.at, (value) => value instanceof RequestValue);
358
+ /**
359
+ * Declares a count of attempts from zero to `max`. Without `.initial` it starts at zero.
360
+ * Counting past `max` is not an error in the action: the checker reports the state as out of
361
+ * range, so guard `again()` with `canRetry`.
362
+ */
363
+ export const retry = (limit) => {
364
+ const max = wholeLimit("m.retry's max", limit.max);
365
+ return new Declaration({
366
+ kind: "retry",
367
+ count: max + 1,
368
+ rest: { value: RetryValue.of(max, 0) },
369
+ values: () => Array.from({ length: max + 1 }, (_, attempts) => RetryValue.of(max, attempts)),
370
+ has: (value) => value instanceof RetryValue && value.max === max && value.attempts <= max,
371
+ adopt: same,
372
+ decode: (json) => {
373
+ if (typeof json !== "number") {
374
+ throw unreadable("retry", json);
375
+ }
376
+ return RetryValue.of(max, json);
377
+ },
378
+ });
379
+ };
380
+ const itemsOf = (over) => over instanceof Declaration ? over.values() : [...over];
381
+ /**
382
+ * Whether `test` is true for every value of a declaration, or for every member of a set,
383
+ * sequence or queue. Over a map it is given `[key, value]` pairs. True when there is nothing to
384
+ * test.
385
+ */
386
+ export const forall = (over, test) => itemsOf(over).every((item) => test(item));
387
+ /**
388
+ * Whether `test` is true for some value of a declaration, or for some member of a set, sequence
389
+ * or queue. Over a map it is given `[key, value]` pairs. False when there is nothing to test.
390
+ */
391
+ export const exists = (over, test) => itemsOf(over).some((item) => test(item));
@@ -0,0 +1,11 @@
1
+ export * as m from "./m.ts";
2
+ export { Declaration, MOST_VALUES_LISTED } from "./declarations.ts";
3
+ export type { Fields, StateOf, ValueOf } from "./declarations.ts";
4
+ export { Model } from "./model.ts";
5
+ export type { Action, ActionRunner, ActionSpec, AnyModel, End, EndRunner, NoInput, Rule, RuleRunner, Runners, View, ViewSpec, } from "./model.ts";
6
+ export { replay } from "./replay.ts";
7
+ export type { ReplayOptions, ReplayPath, ReplayStep, StateJson } from "./replay.ts";
8
+ export { DEFAULT_MAX_STATES, explore } from "./search.ts";
9
+ export type { Change, Exploration, ExploreOptions, Exports, Finding, FindingKind, Named, Path, RuleResult, Status, Step, Verdict, } from "./search.ts";
10
+ export { LockValue, MapValue, ModelValue, OwnedLockValue, QueueValue, RequestValue, RetryValue, SeqValue, SetValue, TimerValue, compareText, } from "./values.ts";
11
+ export type { Json, RequestStage, Row, TimerStage } from "./values.ts";
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export * as m from "./m.js";
2
+ export { Declaration, MOST_VALUES_LISTED } from "./declarations.js";
3
+ export { Model } from "./model.js";
4
+ export { replay } from "./replay.js";
5
+ export { DEFAULT_MAX_STATES, explore } from "./search.js";
6
+ export { LockValue, MapValue, ModelValue, OwnedLockValue, QueueValue, RequestValue, RetryValue, SeqValue, SetValue, TimerValue, compareText, } from "./values.js";
package/dist/m.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import type { Declaration, Fields, StateOf } from "./declarations.ts";
2
+ import type { Model } from "./model.ts";
3
+ export { booleanOf as boolean, enumOf as enum, exists, forall, int, lock, map, oneOf, queue, record, request, retry, seq, set, timer, } from "./declarations.ts";
4
+ export { action, end, model, rule, view } from "./model.ts";
5
+ export { div, mapOf, rem, seqOf, setOf, some } from "./values.ts";
6
+ /**
7
+ * The type a model's state has, or the type of a declaration's values:
8
+ * `type State = m.Infer<typeof Cart>` and `type Press = m.Infer<typeof Press>`.
9
+ */
10
+ export type Infer<Declared> = Declared extends Model<infer F extends Fields> ? StateOf<F> : Declared extends Declaration<infer T, infer _In> ? T : never;
package/dist/m.js ADDED
@@ -0,0 +1,3 @@
1
+ export { booleanOf as boolean, enumOf as enum, exists, forall, int, lock, map, oneOf, queue, record, request, retry, seq, set, timer, } from "./declarations.js";
2
+ export { action, end, model, rule, view } from "./model.js";
3
+ export { div, mapOf, rem, seqOf, setOf, some } from "./values.js";
@@ -0,0 +1,164 @@
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.
18
+ */
19
+ export interface ActionSpec<F extends Fields, I extends Fields, Changes extends Partial<StateOf<F>> = Partial<StateOf<F>>> {
20
+ readonly input?: I;
21
+ readonly when?: (state: StateOf<F>, input: StateOf<I>) => boolean;
22
+ readonly changes: (state: StateOf<F>, input: StateOf<I>) => OnlyFields<F, Changes>;
23
+ }
24
+ /**
25
+ * One thing that can happen in a model. Export it from the model file: the name of the export is
26
+ * its id. `when` is always present here, and always true for an action declared without one.
27
+ */
28
+ export interface Action<F extends Fields, I extends Fields = NoInput> {
29
+ readonly kind: "action";
30
+ readonly sentence: string;
31
+ readonly input: I | undefined;
32
+ readonly when: (state: StateOf<F>, input: StateOf<I>) => boolean;
33
+ readonly changes: (state: StateOf<F>, input: StateOf<I>) => Partial<StateOf<F>>;
34
+ }
35
+ /** A rule every reachable state must meet. The name of its export is its id. */
36
+ export interface Rule<F extends Fields> {
37
+ readonly kind: "rule";
38
+ readonly says: string;
39
+ readonly holds: (state: StateOf<F>) => boolean;
40
+ }
41
+ /**
42
+ * States where stopping is correct. A state with no possible action that no end covers is
43
+ * stuck. The name of its export is its id.
44
+ */
45
+ export interface End<F extends Fields> {
46
+ readonly kind: "end";
47
+ readonly says: string;
48
+ readonly when: (state: StateOf<F>) => boolean;
49
+ }
50
+ /**
51
+ * What `m.view` is given, all of it optional: the chart's title, the label of a state, the
52
+ * group a state is drawn in, arrow labels by action id (the default is the action's sentence)
53
+ * and colors.
54
+ */
55
+ export interface ViewSpec<F extends Fields> {
56
+ readonly title?: string;
57
+ readonly state?: (state: StateOf<F>) => string;
58
+ readonly group?: (state: StateOf<F>) => string;
59
+ readonly step?: Readonly<Record<string, string>>;
60
+ readonly colors?: {
61
+ readonly broken?: string;
62
+ readonly start?: string;
63
+ readonly end?: string;
64
+ };
65
+ }
66
+ /** How `earnest chart` draws a model. It changes nothing about what is checked. */
67
+ export interface View<F extends Fields> extends ViewSpec<F> {
68
+ readonly kind: "view";
69
+ }
70
+ /**
71
+ * An action as a search runs it: on states and inputs whose types are not known statically.
72
+ * `part` is the `Action` the model file exports. `when` and `changes` throw when the state or
73
+ * the input is outside what was declared; `changes` gives back the whole next state.
74
+ */
75
+ export interface ActionRunner {
76
+ readonly part: object;
77
+ readonly sentence: string;
78
+ readonly inputs: readonly (readonly [string, Declaration<unknown, never>])[];
79
+ when(state: Row, input: Row): boolean;
80
+ changes(state: Row, input: Row): Row;
81
+ }
82
+ /** A rule as a search runs it. `holds` throws when the state is outside what was declared. */
83
+ export interface RuleRunner {
84
+ readonly part: object;
85
+ readonly says: string;
86
+ holds(state: Row): boolean;
87
+ }
88
+ /** An end as a search runs it. `when` throws when the state is outside what was declared. */
89
+ export interface EndRunner {
90
+ readonly part: object;
91
+ readonly says: string;
92
+ when(state: Row): boolean;
93
+ }
94
+ /** Everything declared on a model so far, each list in declaration order. */
95
+ export interface Runners {
96
+ readonly actions: readonly ActionRunner[];
97
+ readonly rules: readonly RuleRunner[];
98
+ readonly ends: readonly EndRunner[];
99
+ }
100
+ /**
101
+ * What `explore` and `replay` need of a model, whatever its fields are. Every `Model` is one.
102
+ */
103
+ export interface AnyModel {
104
+ readonly name: string;
105
+ readonly fields: Fields;
106
+ starts(): readonly Row[];
107
+ runners(): Runners;
108
+ }
109
+ /**
110
+ * A model: named fields, and the actions, rules, ends and views that `m.action`, `m.rule`,
111
+ * `m.end` and `m.view` have declared on it, in the order they were called, which is the order a
112
+ * search tries actions in.
113
+ */
114
+ export declare class Model<F extends Fields> {
115
+ #private;
116
+ readonly name: string;
117
+ readonly fields: F;
118
+ /** Model files call `m.model(name, fields)` instead. */
119
+ constructor(name: string, fields: F);
120
+ /** Whether `state` has exactly the model's fields, each at a value its declaration has. */
121
+ has(state: unknown): state is StateOf<F>;
122
+ /**
123
+ * Every start state, in the order the checker visits them: fields in declaration order, the
124
+ * first field varying slowest.
125
+ */
126
+ starts(): readonly StateOf<F>[];
127
+ /** The views declared so far, in declaration order. */
128
+ get views(): readonly View<F>[];
129
+ /**
130
+ * The actions, rules and ends declared so far, in a form a search can run without knowing
131
+ * the model's types. `explore` and `replay` are built on this.
132
+ */
133
+ runners(): Runners;
134
+ }
135
+ /**
136
+ * Declares a model: a name and its fields, each built with an `m` builder. The state type is
137
+ * inferred from the fields; `m.Infer<typeof Model>` names it.
138
+ */
139
+ export declare const model: <F extends Fields>(name: string, fields: F) => Model<F>;
140
+ /**
141
+ * Declares something that can happen in `model`. `sentence` says it in the reviewer's words.
142
+ * Export the result from the model file: the name of the export is the action's id. Actions are
143
+ * tried in the order they are declared. `input`, `when` and `changes` may be written in any
144
+ * order; `changes` is the only one that is required.
145
+ */
146
+ 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>;
147
+ /**
148
+ * Declares a rule of `model`: `holds` must be true in every state the model can reach.
149
+ * `sentence` says it in the reviewer's words. Export the result from the model file: the name
150
+ * of the export is the rule's id.
151
+ */
152
+ export declare const rule: <F extends Fields>(model: Model<F>, sentence: string, holds: (state: StateOf<F>) => boolean) => Rule<F>;
153
+ /**
154
+ * Declares where stopping is correct in `model`, so that those states are not reported as
155
+ * stuck. `sentence` says it in the reviewer's words. Export the result from the model file: the
156
+ * name of the export is the end's id.
157
+ */
158
+ export declare const end: <F extends Fields>(model: Model<F>, sentence: string, when: (state: StateOf<F>) => boolean) => End<F>;
159
+ /**
160
+ * Declares how `earnest chart` draws `model`. A model needs no view, and a view has no id, so
161
+ * its constant needs no export. In a model file, write the second argument, even as `{}`: the
162
+ * checker refuses `m.view(model)`.
163
+ */
164
+ export declare const view: <F extends Fields>(model: Model<F>, spec?: ViewSpec<F>) => View<F>;