@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.
package/dist/search.js ADDED
@@ -0,0 +1,327 @@
1
+ import { NullFault, DivisionFault, RemainderFault, isJsonList, keyOf, toJson, } from "./values.js";
2
+ /** The state limit `explore` uses when it is given none, the same as `earnest verify`. */
3
+ export const DEFAULT_MAX_STATES = 5_000_000;
4
+ /**
5
+ * Gives the function that names a part of a model: the name it is exported under in `exports`,
6
+ * or `fallback`, its sentence, when it is not exported there.
7
+ */
8
+ export const namer = (exports = {}) => {
9
+ const names = new Map();
10
+ for (const [name, exported] of Object.entries(exports)) {
11
+ if (!names.has(exported)) {
12
+ names.set(exported, name);
13
+ }
14
+ }
15
+ return (part, fallback) => names.get(part) ?? fallback;
16
+ };
17
+ const NULL_READ = "reads a value that is null";
18
+ const OVERFLOW = "computes a number too large to hold";
19
+ const REMAINDER = "takes a remainder with a negative number or a divisor below 1";
20
+ const DIVISION = "takes a division with a negative number or a divisor below 1";
21
+ const WRONG_KIND = "uses a value of the wrong kind";
22
+ /**
23
+ * Runs part of a model and turns a fault into a value: reading a field of `null`, or using a
24
+ * value as something it is not. Any other error is a mistake in the model file and is thrown.
25
+ */
26
+ export const attempt = (run) => {
27
+ try {
28
+ return { fault: null, value: run() };
29
+ }
30
+ catch (thrown) {
31
+ if (thrown instanceof RemainderFault) {
32
+ return { fault: REMAINDER, value: null };
33
+ }
34
+ if (thrown instanceof DivisionFault) {
35
+ return { fault: DIVISION, value: null };
36
+ }
37
+ if (thrown instanceof NullFault) {
38
+ return { fault: NULL_READ, value: null };
39
+ }
40
+ if (thrown instanceof TypeError) {
41
+ return { fault: /\bnull\b/.test(thrown.message) ? NULL_READ : WRONG_KIND, value: null };
42
+ }
43
+ throw thrown;
44
+ }
45
+ };
46
+ const numberFault = (json) => {
47
+ if (typeof json === "number") {
48
+ if (Number.isNaN(json)) {
49
+ return REMAINDER;
50
+ }
51
+ return Math.abs(json) > Number.MAX_SAFE_INTEGER ? OVERFLOW : null;
52
+ }
53
+ if (json === null || typeof json !== "object") {
54
+ return null;
55
+ }
56
+ const inside = isJsonList(json) ? json : Object.values(json);
57
+ return inside.map(numberFault).find((fault) => fault !== null) ?? null;
58
+ };
59
+ const stateLimit = (maxStates) => {
60
+ if (!Number.isInteger(maxStates) || maxStates < 0) {
61
+ throw new Error(`maxStates must be a whole number that is not negative, not ${maxStates}`);
62
+ }
63
+ return maxStates;
64
+ };
65
+ /**
66
+ * Applies an action and gives the next state, or the fault that stops it. Besides the faults of
67
+ * `attempt`, a next state that holds `NaN`, which `%` by zero gives, or a number too large for
68
+ * JavaScript to hold exactly is a fault.
69
+ */
70
+ export const nextState = (action, state, input) => {
71
+ const tried = attempt(() => action.changes(state, input));
72
+ if (tried.fault !== null) {
73
+ return tried;
74
+ }
75
+ const fault = numberFault(toJson(tried.value));
76
+ return fault === null ? tried : { fault, value: null };
77
+ };
78
+ /** Every input an action is tried with, in the checker's order: the first input varies fastest. */
79
+ export const inputsOf = (action) => action.inputs.reduceRight((rest, [name, declared]) => rest.flatMap((later) => declared.values().map((value) => ({ [name]: value, ...later }))), [{}]);
80
+ class Search {
81
+ #fields;
82
+ #parts;
83
+ #inputs;
84
+ #name;
85
+ #maxStates;
86
+ #seen = new Set();
87
+ #firstBroken = new Map();
88
+ #unjudged = new Set();
89
+ #stuck = null;
90
+ #outside = null;
91
+ #trouble = null;
92
+ #reason = null;
93
+ #isCutShort = false;
94
+ constructor(fields, parts, options) {
95
+ this.#fields = fields;
96
+ this.#parts = parts;
97
+ this.#inputs = new Map(parts.actions.map((action) => [action, inputsOf(action)]));
98
+ this.#name = namer(options.exports);
99
+ this.#maxStates = stateLimit(options.maxStates ?? DEFAULT_MAX_STATES);
100
+ }
101
+ run(starts) {
102
+ const waiting = [];
103
+ for (const start of starts) {
104
+ this.#visit(start, null, waiting);
105
+ }
106
+ let searched = 0;
107
+ for (const visited of waiting) {
108
+ if (this.#reason !== null) {
109
+ break;
110
+ }
111
+ searched += 1;
112
+ const expanded = this.#expand(visited.row);
113
+ this.#record(visited, expanded);
114
+ for (const successor of expanded.successors) {
115
+ const parent = { from: visited, action: successor.action, input: successor.input };
116
+ if (!this.#visit(successor.next, parent, waiting)) {
117
+ break;
118
+ }
119
+ }
120
+ }
121
+ for (const counted of waiting.slice(searched)) {
122
+ this.#record(counted, this.#judge(counted.row));
123
+ }
124
+ return this.#outcome();
125
+ }
126
+ #keyOf(row) {
127
+ return this.#fields.map(([name]) => keyOf(row[name])).join(";");
128
+ }
129
+ #visit(row, parent, waiting) {
130
+ const key = this.#keyOf(row);
131
+ if (this.#seen.has(key)) {
132
+ return true;
133
+ }
134
+ if (this.#seen.size >= this.#maxStates) {
135
+ this.#reason = `state limit of ${this.#maxStates}`;
136
+ return false;
137
+ }
138
+ this.#seen.add(key);
139
+ waiting.push({ row, key, parent });
140
+ return true;
141
+ }
142
+ #judge(state) {
143
+ const outside = this.#fields.find(([name, declared]) => !declared.has(state[name]));
144
+ const found = {
145
+ outside: outside === undefined ? null : outside[0],
146
+ broken: [],
147
+ unjudged: [],
148
+ successors: [],
149
+ trouble: null,
150
+ isCutShort: outside !== undefined,
151
+ isEnabled: false,
152
+ isStuck: false,
153
+ };
154
+ if (found.outside !== null) {
155
+ return found;
156
+ }
157
+ for (const rule of this.#parts.rules) {
158
+ const held = attempt(() => rule.holds(state));
159
+ if (held.fault !== null) {
160
+ found.trouble ??= `the rule \`${this.#name(rule.part, rule.says)}\` ${held.fault}`;
161
+ found.unjudged.push(rule);
162
+ }
163
+ else if (!held.value) {
164
+ found.broken.push(rule);
165
+ }
166
+ }
167
+ return found;
168
+ }
169
+ #expand(state) {
170
+ const found = this.#judge(state);
171
+ if (found.outside !== null) {
172
+ return found;
173
+ }
174
+ let canTell = true;
175
+ for (const action of this.#parts.actions) {
176
+ canTell = this.#expandAction(action, state, found) && canTell;
177
+ }
178
+ if (found.isEnabled) {
179
+ return found;
180
+ }
181
+ let hasEnded = false;
182
+ for (const end of this.#parts.ends) {
183
+ const ended = attempt(() => end.when(state));
184
+ if (ended.fault === null) {
185
+ hasEnded ||= ended.value;
186
+ }
187
+ else {
188
+ found.trouble ??= `the end \`${this.#name(end.part, end.says)}\` ${ended.fault}`;
189
+ found.isCutShort = true;
190
+ canTell = false;
191
+ }
192
+ }
193
+ found.isStuck = canTell && !hasEnded;
194
+ return found;
195
+ }
196
+ #expandAction(action, state, found) {
197
+ const id = this.#name(action.part, action.sentence);
198
+ let canTell = true;
199
+ for (const input of this.#inputs.get(action) ?? []) {
200
+ const can = attempt(() => action.when(state, input));
201
+ if (can.fault !== null) {
202
+ found.trouble ??= `the condition of the action \`${id}\` ${can.fault}`;
203
+ found.isCutShort = true;
204
+ canTell = false;
205
+ continue;
206
+ }
207
+ if (!can.value) {
208
+ continue;
209
+ }
210
+ found.isEnabled = true;
211
+ const next = nextState(action, state, input);
212
+ if (next.fault === null) {
213
+ found.successors.push({ action, input, next: next.value });
214
+ }
215
+ else {
216
+ found.trouble ??= `the change of the action \`${id}\` ${next.fault}`;
217
+ found.isCutShort = true;
218
+ }
219
+ }
220
+ return canTell;
221
+ }
222
+ #record(at, expanded) {
223
+ if (expanded.outside !== null) {
224
+ this.#outside ??= { at, field: expanded.outside };
225
+ }
226
+ for (const rule of expanded.broken) {
227
+ if (!this.#firstBroken.has(rule)) {
228
+ this.#firstBroken.set(rule, at);
229
+ }
230
+ }
231
+ for (const rule of expanded.unjudged) {
232
+ this.#unjudged.add(rule);
233
+ }
234
+ if (expanded.trouble !== null) {
235
+ this.#trouble ??= { at, message: `${expanded.trouble} in this state` };
236
+ }
237
+ this.#isCutShort ||= expanded.isCutShort;
238
+ if (expanded.isStuck) {
239
+ this.#stuck ??= at;
240
+ }
241
+ }
242
+ #named(row) {
243
+ return this.#fields.map(([name]) => ({ name, value: toJson(row[name]) }));
244
+ }
245
+ #step(parent, after) {
246
+ const before = parent.from.row;
247
+ return {
248
+ action: this.#name(parent.action.part, parent.action.sentence),
249
+ label: parent.action.sentence,
250
+ input: parent.action.inputs.map(([name]) => ({ name, value: toJson(parent.input[name]) })),
251
+ changes: this.#fields
252
+ .filter(([name]) => keyOf(before[name]) !== keyOf(after[name]))
253
+ .map(([name]) => ({
254
+ field: name,
255
+ before: toJson(before[name]),
256
+ after: toJson(after[name]),
257
+ })),
258
+ state: this.#named(after),
259
+ };
260
+ }
261
+ #pathTo(target) {
262
+ const steps = [];
263
+ let at = target;
264
+ while (at.parent !== null) {
265
+ steps.push(this.#step(at.parent, at.row));
266
+ at = at.parent.from;
267
+ }
268
+ return { start: this.#named(at.row), steps: steps.reverse() };
269
+ }
270
+ #findings() {
271
+ const findings = [];
272
+ if (this.#stuck !== null) {
273
+ findings.push({
274
+ kind: "stuck",
275
+ message: "no action can happen here, and the model does not mark it as an end",
276
+ path: this.#pathTo(this.#stuck),
277
+ });
278
+ }
279
+ if (this.#outside !== null) {
280
+ findings.push({
281
+ kind: "out-of-range",
282
+ message: `the field \`${this.#outside.field}\` leaves the values it declares`,
283
+ path: this.#pathTo(this.#outside.at),
284
+ });
285
+ }
286
+ if (this.#trouble !== null) {
287
+ findings.push({
288
+ kind: "fault",
289
+ message: this.#trouble.message,
290
+ path: this.#pathTo(this.#trouble.at),
291
+ });
292
+ }
293
+ return findings;
294
+ }
295
+ #outcome() {
296
+ const rules = this.#parts.rules.map((rule) => {
297
+ const first = this.#firstBroken.get(rule);
298
+ const isJudged = this.#reason === null && !this.#isCutShort && !this.#unjudged.has(rule);
299
+ const unbroken = isJudged ? "holds" : "unknown";
300
+ return {
301
+ id: this.#name(rule.part, rule.says),
302
+ says: rule.says,
303
+ verdict: first === undefined ? unbroken : "broken",
304
+ path: first === undefined ? null : this.#pathTo(first),
305
+ };
306
+ });
307
+ const findings = this.#findings();
308
+ const hasFailed = findings.length > 0 || rules.some((rule) => rule.verdict === "broken");
309
+ const unfailed = this.#reason === null ? "pass" : "unfinished";
310
+ return {
311
+ status: hasFailed ? "fail" : unfailed,
312
+ states: this.#seen.size,
313
+ rules,
314
+ findings,
315
+ reason: this.#reason,
316
+ };
317
+ }
318
+ }
319
+ /**
320
+ * Searches every state a model can reach, breadth first, and gives the count of distinct
321
+ * states, each rule's verdict with the shortest path to a state that breaks it, and the first
322
+ * stuck, out-of-range and fault findings. An end is asked only in a state where no action can
323
+ * happen. It visits states in the same order as
324
+ * `earnest verify`, so the two results can be compared. Pass the model file's exports as
325
+ * `exports` to get the same ids.
326
+ */
327
+ export const explore = (model, options = {}) => new Search(Object.entries(model.fields), model.runners(), options).run(model.starts());
@@ -0,0 +1,293 @@
1
+ /**
2
+ * A value as `earnest verify --json` prints it: a set, a sequence and a queue are arrays, a map
3
+ * is an array of `[key, value]` pairs, a record is an object, a lock is `true` when held or its
4
+ * holder, a timer and a request are the name of their stage, and a retry is its attempt count.
5
+ */
6
+ export type Json = null | boolean | number | string | readonly Json[] | {
7
+ readonly [name: string]: Json;
8
+ };
9
+ /**
10
+ * Compares two texts by code point, the order of Rust's `str` and of the UTF-8 bytes, which is
11
+ * the order every set, map and record is kept in. JavaScript's own `<` compares UTF-16 code
12
+ * units, and so puts a character past U+FFFF before one from U+E000 to U+FFFF.
13
+ */
14
+ export declare const compareText: (left: string, right: string) => number;
15
+ /** An object read by field name whose values are not yet known to fit any declaration. */
16
+ export type Row = Readonly<Record<string, unknown>>;
17
+ /**
18
+ * The base of every collection, lock, timer, request and retry value. Two values with the same
19
+ * content are the same object, so `===` compares them by content.
20
+ */
21
+ export declare abstract class ModelValue {
22
+ /** Text that is equal for two values exactly when their content is equal. */
23
+ abstract readonly key: string;
24
+ /** The value in the form `earnest verify --json` prints. */
25
+ abstract toJSON(): Json;
26
+ }
27
+ /** Tells a record, which is a plain object, from every other value a model can hold. */
28
+ export declare const isRecord: (value: unknown) => value is Row;
29
+ /**
30
+ * Text that is equal for two model values exactly when their content is equal. A record's key
31
+ * does not depend on the order its fields were written in. Throws for anything a model can't
32
+ * hold, such as `undefined` or a function.
33
+ */
34
+ export declare const keyOf: (value: unknown) => string;
35
+ /** Turns a model value into the form `earnest verify --json` prints. */
36
+ export declare const toJson: (value: unknown) => Json;
37
+ /**
38
+ * Makes a record, and every record inside it, read-only, and gives the same value back. Other
39
+ * values are already immutable.
40
+ */
41
+ export declare const frozen: <T>(value: T) => T;
42
+ /** Tells a JSON array from every other JSON value. */
43
+ export declare const isJsonList: (json: Json) => json is readonly Json[];
44
+ /** Tells a JSON object from every other JSON value. */
45
+ export declare const isJsonObject: (json: Json) => json is {
46
+ readonly [name: string]: Json;
47
+ };
48
+ /**
49
+ * The order set members and map keys are kept and printed in: `null`, then booleans, numbers,
50
+ * text in code point order (see `compareText`), records by their field names in that order, and
51
+ * collections item by item. Returns a negative number, zero or a
52
+ * positive number, as a sort comparator does.
53
+ */
54
+ export declare const compareJson: (left: Json, right: Json) => number;
55
+ /**
56
+ * An immutable set. Members are compared by content and kept in the order of `compareJson`,
57
+ * whatever order they were added in. `has`, `isSubset`, `size`, `isEmpty`, `every` and `some`
58
+ * read the set; `add`, `delete`, `union`, `intersect`, `subtract` and `filter` give back another
59
+ * set and leave this one as it was.
60
+ */
61
+ export declare class SetValue<T> extends ModelValue implements Iterable<T> {
62
+ #private;
63
+ readonly key: string;
64
+ private constructor();
65
+ /** The set of the given items; an item given twice is in the set once. */
66
+ static of<T>(items: Iterable<T>): SetValue<T>;
67
+ get size(): number;
68
+ get isEmpty(): boolean;
69
+ has(item: T): boolean;
70
+ add(item: T): SetValue<T>;
71
+ delete(item: T): SetValue<T>;
72
+ union(other: SetValue<T>): SetValue<T>;
73
+ intersect(other: SetValue<T>): SetValue<T>;
74
+ subtract(other: SetValue<T>): SetValue<T>;
75
+ isSubset(other: SetValue<T>): boolean;
76
+ every(test: (item: T) => boolean): boolean;
77
+ some(test: (item: T) => boolean): boolean;
78
+ filter(test: (item: T) => boolean): SetValue<T>;
79
+ [Symbol.iterator](): Iterator<T>;
80
+ toJSON(): Json;
81
+ }
82
+ /**
83
+ * An immutable map. Keys are compared by content and kept in the order of `compareJson`.
84
+ * `get` gives `null` for a key the map does not have. `set` and `delete` give back another map
85
+ * and leave this one as it was. Iterating gives `[key, value]` pairs.
86
+ */
87
+ export declare class MapValue<K, V> extends ModelValue implements Iterable<readonly [K, V]> {
88
+ #private;
89
+ readonly key: string;
90
+ private constructor();
91
+ /** The map of the given pairs; when a key is given twice, the later pair wins. */
92
+ static of<K, V>(entries: Iterable<readonly [K, V]>): MapValue<K, V>;
93
+ get size(): number;
94
+ get isEmpty(): boolean;
95
+ get keys(): SetValue<K>;
96
+ has(key: K): boolean;
97
+ get(key: K): V | null;
98
+ set(key: K, value: V): MapValue<K, V>;
99
+ delete(key: K): MapValue<K, V>;
100
+ [Symbol.iterator](): Iterator<readonly [K, V]>;
101
+ toJSON(): Json;
102
+ }
103
+ /**
104
+ * An immutable sequence. `get`, `first` and `last` give `null` where there is no item. `push`
105
+ * adds at the end, `shift` drops the first item and `pop` drops the last; each gives back
106
+ * another sequence, and `shift` and `pop` on an empty sequence give the empty sequence. Pushing
107
+ * past the declared `max` is not an error here: the checker reports the state as out of range.
108
+ */
109
+ export declare class SeqValue<T> extends ModelValue implements Iterable<T> {
110
+ #private;
111
+ readonly key: string;
112
+ private constructor();
113
+ /** The sequence of the given items, in the order given. */
114
+ static of<T>(items: Iterable<T>): SeqValue<T>;
115
+ get size(): number;
116
+ get isEmpty(): boolean;
117
+ get first(): T | null;
118
+ get last(): T | null;
119
+ get(position: number): T | null;
120
+ push(item: T): SeqValue<T>;
121
+ shift(): SeqValue<T>;
122
+ pop(): SeqValue<T>;
123
+ includes(item: T): boolean;
124
+ every(test: (item: T) => boolean): boolean;
125
+ some(test: (item: T) => boolean): boolean;
126
+ filter(test: (item: T) => boolean): SeqValue<T>;
127
+ [Symbol.iterator](): Iterator<T>;
128
+ toJSON(): Json;
129
+ }
130
+ /**
131
+ * An immutable first-in, first-out queue with a capacity. `head` is the item `receive` would
132
+ * drop, or `null`. `send`, `receive` and `clear` give back another queue. Sending to a full
133
+ * queue is not an error here: the checker reports the state as out of range.
134
+ */
135
+ export declare class QueueValue<T> extends ModelValue implements Iterable<T> {
136
+ #private;
137
+ readonly key: string;
138
+ private constructor();
139
+ /** The queue of the given capacity holding the given items, the first of them at the head. */
140
+ static of<T>(capacity: number, items: Iterable<T>): QueueValue<T>;
141
+ get size(): number;
142
+ get isEmpty(): boolean;
143
+ get capacity(): number;
144
+ get isFull(): boolean;
145
+ get head(): T | null;
146
+ send(item: T): QueueValue<T>;
147
+ receive(): QueueValue<T>;
148
+ includes(item: T): boolean;
149
+ clear(): QueueValue<T>;
150
+ every(test: (item: T) => boolean): boolean;
151
+ some(test: (item: T) => boolean): boolean;
152
+ [Symbol.iterator](): Iterator<T>;
153
+ toJSON(): Json;
154
+ }
155
+ /**
156
+ * A lock with no owner: free or held. `acquire` and `release` give back another lock; acquiring
157
+ * a held lock gives a held lock, so guard with `isFree` in the action's `when`.
158
+ */
159
+ export declare class LockValue extends ModelValue {
160
+ #private;
161
+ readonly key: string;
162
+ private constructor();
163
+ /** The held lock when `isHeld` is true, the free lock otherwise. */
164
+ static of(isHeld: boolean): LockValue;
165
+ get isFree(): boolean;
166
+ get isHeld(): boolean;
167
+ acquire(): LockValue;
168
+ release(): LockValue;
169
+ toJSON(): Json;
170
+ }
171
+ /**
172
+ * A lock that remembers who holds it. `holder` is `null` when the lock is free. `acquire(owner)`
173
+ * and `release` give back another lock; acquiring a held lock hands it to the new owner, so
174
+ * guard with `isFree` in the action's `when`.
175
+ */
176
+ export declare class OwnedLockValue<Owner> extends ModelValue {
177
+ #private;
178
+ readonly key: string;
179
+ private constructor();
180
+ /** The lock held by `holder`, or the free lock for `null`. */
181
+ static of<Owner>(holder: Owner | null): OwnedLockValue<Owner>;
182
+ get isFree(): boolean;
183
+ get isHeld(): boolean;
184
+ get holder(): Owner | null;
185
+ isHeldBy(owner: Owner): boolean;
186
+ acquire(owner: Owner): OwnedLockValue<Owner>;
187
+ release(): OwnedLockValue<Owner>;
188
+ toJSON(): Json;
189
+ }
190
+ /** The stages of a timer, in the order a declaration lists them. */
191
+ export declare const TIMER_STAGES: readonly TimerStage[];
192
+ /** The name of one stage of a timer. */
193
+ export type TimerStage = "idle" | "running" | "fired";
194
+ /**
195
+ * A timer: idle, running or fired. `start`, `fire`, `cancel` and `reset` give back another timer
196
+ * from any stage; `cancel` and `reset` both give the idle timer.
197
+ */
198
+ export declare class TimerValue extends ModelValue {
199
+ #private;
200
+ readonly key: string;
201
+ private constructor();
202
+ /** The timer at the given stage. */
203
+ static at(stage: TimerStage): TimerValue;
204
+ get isIdle(): boolean;
205
+ get isRunning(): boolean;
206
+ get hasFired(): boolean;
207
+ start(): TimerValue;
208
+ fire(): TimerValue;
209
+ cancel(): TimerValue;
210
+ reset(): TimerValue;
211
+ toJSON(): Json;
212
+ }
213
+ /** The stages of an async request, in the order a declaration lists them. */
214
+ export declare const REQUEST_STAGES: readonly RequestStage[];
215
+ /** The name of one stage of an async request. */
216
+ export type RequestStage = "idle" | "pending" | "succeeded" | "failed";
217
+ /**
218
+ * An async request: idle, pending, succeeded or failed. `isSettled` is true once it succeeded or
219
+ * failed. `send`, `succeed`, `fail` and `reset` give back another request from any stage.
220
+ */
221
+ export declare class RequestValue extends ModelValue {
222
+ #private;
223
+ readonly key: string;
224
+ private constructor();
225
+ /** The request at the given stage. */
226
+ static at(stage: RequestStage): RequestValue;
227
+ get isIdle(): boolean;
228
+ get isPending(): boolean;
229
+ get hasSucceeded(): boolean;
230
+ get hasFailed(): boolean;
231
+ get isSettled(): boolean;
232
+ send(): RequestValue;
233
+ succeed(): RequestValue;
234
+ fail(): RequestValue;
235
+ reset(): RequestValue;
236
+ toJSON(): Json;
237
+ }
238
+ /**
239
+ * A count of attempts with a most. `canRetry` is true below the most and `isExhausted` at it.
240
+ * `again` counts one more attempt and `reset` goes back to zero; both give back another counter.
241
+ * Counting past the most is not an error here: the checker reports the state as out of range.
242
+ */
243
+ export declare class RetryValue extends ModelValue {
244
+ #private;
245
+ readonly key: string;
246
+ private constructor();
247
+ /** The counter that allows `max` attempts and has counted `attempts` of them. */
248
+ static of(max: number, attempts: number): RetryValue;
249
+ get max(): number;
250
+ get attempts(): number;
251
+ get canRetry(): boolean;
252
+ get isExhausted(): boolean;
253
+ again(): RetryValue;
254
+ reset(): RetryValue;
255
+ toJSON(): Json;
256
+ }
257
+ /** Writes a set value: `m.setOf("a", "b")`. Members keep their literal types. */
258
+ export declare const setOf: <const T>(...items: readonly T[]) => SetValue<T>;
259
+ /** Writes a sequence value: `m.seqOf("a", "b")`. It is also how a queue's `.initial` is written. */
260
+ export declare const seqOf: <const T>(...items: readonly T[]) => SeqValue<T>;
261
+ /** Writes a map value from `[key, value]` pairs: `m.mapOf(["a", 1], ["b", 2])`. */
262
+ export declare const mapOf: <const K, const V>(...entries: readonly (readonly [K, V])[]) => MapValue<K, V>;
263
+ /** Thrown by `rem` for a remainder the contract makes a fault; `explore` and `replay` report it. */
264
+ export declare class RemainderFault extends Error {
265
+ constructor();
266
+ }
267
+ /** Thrown by `div` for a division the contract makes a fault; `explore` and `replay` report it. */
268
+ export declare class DivisionFault extends Error {
269
+ constructor();
270
+ }
271
+ /** Thrown by `some` for a `null`; `explore` and `replay` report it as a read of a null value. */
272
+ export declare class NullFault extends Error {
273
+ constructor();
274
+ }
275
+ /**
276
+ * Writes the value inside an optional: `m.some(s.chosen).qty`. A `null` is a fault, as in
277
+ * `earnest verify`, because JavaScript would go on with `null` as a number. Use it where
278
+ * TypeScript would want a postfix `!`, which a model file may not have.
279
+ */
280
+ export declare const some: <T>(value: T | null) => T;
281
+ /**
282
+ * Writes the remainder of `a` divided by `b`: `m.rem(7, 3)` is 1. A negative `a` or a `b`
283
+ * below 1 is a fault, as in `earnest verify`, because languages disagree on the result there.
284
+ * Use it where JavaScript would use `%`, which a model file may not.
285
+ */
286
+ export declare const rem: (a: number, b: number) => number;
287
+ /**
288
+ * Writes the whole number of times `b` goes into `a`: `m.div(7, 2)` is 3. A negative `a` or a
289
+ * `b` below 1 is a fault, as in `earnest verify`, because languages disagree on the result
290
+ * there and Rust panics on a divisor of zero. Use it where JavaScript would use `/`, which a
291
+ * model file may not, since it gives a fraction.
292
+ */
293
+ export declare const div: (a: number, b: number) => number;