@altnovatrail/widgets 0.2.0 → 0.3.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/README.md +1 -1
- package/cores/vector.schema.json +22 -1
- package/kinds/cards/cards.handles.json +109 -0
- package/kinds/cards/cards.schema.json +483 -0
- package/kinds/cards/vectors/any.json +255 -0
- package/kinds/cards/vectors/box.json +325 -0
- package/kinds/cards/vectors/fork-taps.json +305 -0
- package/kinds/cards/vectors/fork.json +254 -0
- package/kinds/cards/vectors/invalid-goal.json +70 -0
- package/kinds/cards/vectors/invalid-ids.json +113 -0
- package/kinds/cards/vectors/invalid-schema.json +88 -0
- package/kinds/cards/vectors/invalid-taps.json +214 -0
- package/kinds/cards/vectors/key.json +264 -0
- package/kinds/cards/vectors/limit.json +274 -0
- package/kinds/cards/vectors/no-goal.json +205 -0
- package/kinds/cards/vectors/none.json +239 -0
- package/kinds/cards/vectors/recog.json +113 -0
- package/kinds/cards/vectors/riddle.json +397 -0
- package/kinds/cards/vectors/solo.json +218 -0
- package/kinds/cells/cells.schema.json +17 -2
- package/kinds/line/line.handles.json +195 -0
- package/kinds/line/line.schema.json +336 -0
- package/kinds/line/vectors/cases.json +213 -0
- package/kinds/line/vectors/gaps-12-d1-1.json +140 -0
- package/kinds/line/vectors/gaps-12-d1-2.json +95 -0
- package/kinds/line/vectors/gaps-12-d1-3.json +122 -0
- package/kinds/line/vectors/gaps-12-d1-4.json +121 -0
- package/kinds/line/vectors/gaps-12-d1-5.json +87 -0
- package/kinds/line/vectors/gaps-12-d2-1.json +173 -0
- package/kinds/line/vectors/gaps-12-d2-6.json +87 -0
- package/kinds/line/vectors/gaps-12-d3-2.json +173 -0
- package/kinds/line/vectors/gaps-12-d4-4.json +168 -0
- package/kinds/line/vectors/gaps-12-d5-3.json +99 -0
- package/kinds/line/vectors/gaps-12-d5-4.json +215 -0
- package/kinds/line/vectors/gaps-34-d1-1.json +37 -0
- package/kinds/line/vectors/gaps-34-d1-2.json +130 -0
- package/kinds/line/vectors/gaps-34-d1-4.json +155 -0
- package/kinds/line/vectors/gaps-34-d1-5.json +43 -0
- package/kinds/line/vectors/gaps-34-d3-2.json +38 -0
- package/kinds/line/vectors/gaps-34-d3-5.json +177 -0
- package/kinds/line/vectors/gaps-34-d5-3.json +39 -0
- package/kinds/line/vectors/invalid-line.json +34 -0
- package/kinds/line/vectors/invalid-ring.json +34 -0
- package/kinds/line/vectors/invalid.json +43 -0
- package/kinds/line/vectors/jump-unreachable.json +104 -0
- package/kinds/line/vectors/limit.json +40 -0
- package/kinds/line/vectors/op-not-in-scene.json +47 -0
- package/kinds/line/vectors/ring-cut.json +104 -0
- package/kinds/line/vectors/ring-every.json +107 -0
- package/kinds/line/vectors/ring-jump.json +135 -0
- package/lib/cores/arith.d.ts +62 -0
- package/lib/cores/arith.js +197 -0
- package/lib/cores/kind.d.ts +31 -3
- package/lib/cores/registry.d.ts +43 -0
- package/lib/cores/registry.js +87 -0
- package/lib/cores/search.d.ts +22 -6
- package/lib/cores/search.js +70 -14
- package/lib/kinds/cards/goal.d.ts +25 -0
- package/lib/kinds/cards/goal.js +72 -0
- package/lib/kinds/cards/index.d.ts +14 -0
- package/lib/kinds/cards/index.js +17 -0
- package/lib/kinds/cards/kind.d.ts +23 -0
- package/lib/kinds/cards/kind.js +101 -0
- package/lib/kinds/cards/parts.d.ts +36 -0
- package/lib/kinds/cards/parts.js +34 -0
- package/lib/kinds/cards/scene.d.ts +102 -0
- package/lib/kinds/cards/scene.js +356 -0
- package/lib/kinds/cards/schema.d.ts +10 -0
- package/lib/kinds/cards/schema.js +285 -0
- package/lib/kinds/cells/kind.js +11 -64
- package/lib/kinds/cells/operation.d.ts +6 -0
- package/lib/kinds/cells/polyomino.d.ts +14 -0
- package/lib/kinds/cells/polyomino.js +100 -24
- package/lib/kinds/cells/registry.js +1 -1
- package/lib/kinds/cells/schema.js +3 -1
- package/lib/kinds/line/cut.d.ts +2 -0
- package/lib/kinds/line/cut.js +63 -0
- package/lib/kinds/line/goals.d.ts +22 -0
- package/lib/kinds/line/goals.js +137 -0
- package/lib/kinds/line/index.d.ts +13 -0
- package/lib/kinds/line/index.js +17 -0
- package/lib/kinds/line/jump.d.ts +10 -0
- package/lib/kinds/line/jump.js +95 -0
- package/lib/kinds/line/kind.d.ts +25 -0
- package/lib/kinds/line/kind.js +125 -0
- package/lib/kinds/line/line.d.ts +63 -0
- package/lib/kinds/line/line.js +157 -0
- package/lib/kinds/line/operation.d.ts +69 -0
- package/lib/kinds/line/operation.js +1 -0
- package/lib/kinds/line/posts.d.ts +11 -0
- package/lib/kinds/line/posts.js +85 -0
- package/lib/kinds/line/registry.d.ts +2 -0
- package/lib/kinds/line/registry.js +11 -0
- package/lib/kinds/line/schema.d.ts +4 -0
- package/lib/kinds/line/schema.js +159 -0
- package/package.json +16 -4
- package/cores/handles.schema.json +0 -63
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pattern of a fraction as JSON writes it in a scene and a state: the one form module-kit's
|
|
3
|
+
* solver answers in, an integer or `p/q` in lowest terms with the sign before p, as "0", "-3" or
|
|
4
|
+
* "7/2". A module's key may write a fraction in any form of its value, but a scene and a state
|
|
5
|
+
* write it in this one alone, and "2/4" or "3/1" there is an error. The pattern cannot see lowest
|
|
6
|
+
* terms or the bounds of a safe integer; parseFraction does.
|
|
7
|
+
*/
|
|
8
|
+
export const fractionPattern = "^(0|-?[1-9][0-9]{0,15}(/([2-9]|[1-9][0-9]{1,15}))?)$";
|
|
9
|
+
/** The part of a kind's schema for a fraction in a scene or a state. */
|
|
10
|
+
export const fractionSchema = {
|
|
11
|
+
type: "string",
|
|
12
|
+
pattern: fractionPattern,
|
|
13
|
+
description: 'A number, exact: an integer or a fraction in lowest terms with its sign before the numerator, as "0", "-3" or "7/2".',
|
|
14
|
+
};
|
|
15
|
+
const NOTATION = /^(-?[1-9][0-9]{0,15})(?:\/([1-9][0-9]{0,15}))?$/;
|
|
16
|
+
/** The fraction num/den in lowest terms; a denominator of 0 or a part not a safe integer throws. */
|
|
17
|
+
export function fraction(num, den = 1) {
|
|
18
|
+
safe(num);
|
|
19
|
+
safe(den);
|
|
20
|
+
if (den === 0) {
|
|
21
|
+
throw new RangeError("a fraction with the denominator 0");
|
|
22
|
+
}
|
|
23
|
+
const divisor = gcd(num, den);
|
|
24
|
+
const sign = den < 0 ? -1 : 1;
|
|
25
|
+
return made((sign * num) / divisor, (sign * den) / divisor);
|
|
26
|
+
}
|
|
27
|
+
/** a + b. */
|
|
28
|
+
export function add(a, b) {
|
|
29
|
+
// Over the least common denominator, then reduced by what the numerator still shares with it.
|
|
30
|
+
const shared = gcd(a.den, b.den);
|
|
31
|
+
const left = product(a.num, b.den / shared);
|
|
32
|
+
const right = product(b.num, a.den / shared);
|
|
33
|
+
const den = product(a.den, b.den / shared);
|
|
34
|
+
const num = left === undefined || right === undefined ? undefined : left + right;
|
|
35
|
+
if (den === undefined || num === undefined || !Number.isSafeInteger(num)) {
|
|
36
|
+
return exact(BigInt(a.num) * BigInt(b.den) + BigInt(b.num) * BigInt(a.den), BigInt(a.den) * BigInt(b.den));
|
|
37
|
+
}
|
|
38
|
+
const divisor = gcd(num, shared);
|
|
39
|
+
return made(num / divisor, den / divisor);
|
|
40
|
+
}
|
|
41
|
+
/** a − b. */
|
|
42
|
+
export function subtract(a, b) {
|
|
43
|
+
return add(a, made(-b.num, b.den));
|
|
44
|
+
}
|
|
45
|
+
/** a · b. */
|
|
46
|
+
export function multiply(a, b) {
|
|
47
|
+
return times(a, b, "·", b);
|
|
48
|
+
}
|
|
49
|
+
/** a ÷ b; b of 0 throws. */
|
|
50
|
+
export function divide(a, b) {
|
|
51
|
+
if (b.num === 0) {
|
|
52
|
+
throw new RangeError("a division by 0");
|
|
53
|
+
}
|
|
54
|
+
const reciprocal = b.num < 0 ? made(-b.den, -b.num) : made(b.den, b.num);
|
|
55
|
+
return times(a, reciprocal, "÷", b);
|
|
56
|
+
}
|
|
57
|
+
// The operation and its right operand as asked only name them in the error, written only if thrown,
|
|
58
|
+
// since the product runs on every edge of a state graph.
|
|
59
|
+
function times(a, b, sign, asked) {
|
|
60
|
+
// Each numerator reduced by the other's denominator first, so the product is in lowest terms
|
|
61
|
+
// and no part of it is larger than the result's own.
|
|
62
|
+
const first = gcd(a.num, b.den);
|
|
63
|
+
const second = gcd(b.num, a.den);
|
|
64
|
+
const num = product(a.num / first, b.num / second);
|
|
65
|
+
const den = product(a.den / second, b.den / first);
|
|
66
|
+
if (num === undefined || den === undefined) {
|
|
67
|
+
throw new RangeError(`not a safe integer in the result of ${fractionText(a)} ${sign} ${fractionText(asked)}`);
|
|
68
|
+
}
|
|
69
|
+
return made(num, den);
|
|
70
|
+
}
|
|
71
|
+
/** −1, 0 or 1 as a is less than, equal to or greater than b. */
|
|
72
|
+
export function compare(a, b) {
|
|
73
|
+
const left = product(a.num, b.den);
|
|
74
|
+
const right = product(b.num, a.den);
|
|
75
|
+
if (left !== undefined && right !== undefined) {
|
|
76
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
77
|
+
}
|
|
78
|
+
const difference = BigInt(a.num) * BigInt(b.den) - BigInt(b.num) * BigInt(a.den);
|
|
79
|
+
return difference < 0n ? -1 : difference > 0n ? 1 : 0;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The whole quotient of a by b and its remainder, from 0 to |b| − 1 whatever the signs: a place on
|
|
83
|
+
* a ring of b, counted on from any whole number. b of 0 throws.
|
|
84
|
+
*/
|
|
85
|
+
export function divideWithRemainder(a, b) {
|
|
86
|
+
safe(a);
|
|
87
|
+
safe(b);
|
|
88
|
+
if (b === 0) {
|
|
89
|
+
throw new RangeError("a division by 0");
|
|
90
|
+
}
|
|
91
|
+
// % keeps a's sign; a less its remainder is a multiple of b no larger than a, so it divides exactly.
|
|
92
|
+
const remainder = a % b;
|
|
93
|
+
const quotient = (a - remainder) / b;
|
|
94
|
+
if (remainder >= 0) {
|
|
95
|
+
return { quotient: quotient + 0, remainder: remainder + 0 };
|
|
96
|
+
}
|
|
97
|
+
return b > 0
|
|
98
|
+
? { quotient: quotient - 1, remainder: remainder + b }
|
|
99
|
+
: { quotient: quotient + 1, remainder: remainder - b };
|
|
100
|
+
}
|
|
101
|
+
/** The greatest common divisor of a and b, never negative; of 0 and 0, 0. */
|
|
102
|
+
export function gcd(a, b) {
|
|
103
|
+
safe(a);
|
|
104
|
+
safe(b);
|
|
105
|
+
let x = Math.abs(a);
|
|
106
|
+
let y = Math.abs(b);
|
|
107
|
+
while (y !== 0) {
|
|
108
|
+
const rest = x % y;
|
|
109
|
+
x = y;
|
|
110
|
+
y = rest;
|
|
111
|
+
}
|
|
112
|
+
return x;
|
|
113
|
+
}
|
|
114
|
+
/** The least common multiple of a and b, never negative; 0 where either is 0. */
|
|
115
|
+
export function lcm(a, b) {
|
|
116
|
+
if (a === 0 || b === 0) {
|
|
117
|
+
safe(a);
|
|
118
|
+
safe(b);
|
|
119
|
+
return 0;
|
|
120
|
+
}
|
|
121
|
+
const multiple = product(Math.abs(a) / gcd(a, b), Math.abs(b));
|
|
122
|
+
if (multiple === undefined) {
|
|
123
|
+
throw new RangeError(`not a safe integer: the least common multiple of ${a} and ${b}`);
|
|
124
|
+
}
|
|
125
|
+
return multiple;
|
|
126
|
+
}
|
|
127
|
+
/** A fraction in the notation of fractionPattern, for a scene or a state. */
|
|
128
|
+
export function fractionText(value) {
|
|
129
|
+
return value.den === 1 ? String(value.num) : `${value.num}/${value.den}`;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* The fraction a text writes in the notation of fractionPattern, or undefined where it writes
|
|
133
|
+
* none: a fraction not in lowest terms, with the denominator 1, or a part past a safe integer, the
|
|
134
|
+
* pattern's matches among them. One value has one text, so a state that holds it keys as one.
|
|
135
|
+
*/
|
|
136
|
+
export function parseFraction(value) {
|
|
137
|
+
if (value === "0") {
|
|
138
|
+
return made(0, 1);
|
|
139
|
+
}
|
|
140
|
+
const match = NOTATION.exec(value);
|
|
141
|
+
if (match === null) {
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
const num = Number(match[1]);
|
|
145
|
+
const den = match[2] === undefined ? 1 : Number(match[2]);
|
|
146
|
+
if (!Number.isSafeInteger(num) || !Number.isSafeInteger(den)) {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
if (match[2] !== undefined && (den === 1 || gcd(num, den) !== 1)) {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
return made(num, den);
|
|
153
|
+
}
|
|
154
|
+
/** A fraction of a scene by fractionSchema, read as the other readers of a scene's JSON are. */
|
|
155
|
+
export function readFraction(value, path, errors) {
|
|
156
|
+
const read = typeof value === "string" ? parseFraction(value) : undefined;
|
|
157
|
+
if (read === undefined) {
|
|
158
|
+
errors.push({
|
|
159
|
+
path,
|
|
160
|
+
message: 'a number as "0", "-3" or "7/2": an integer, or a fraction in lowest terms',
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
return read;
|
|
164
|
+
}
|
|
165
|
+
function safe(value) {
|
|
166
|
+
if (!Number.isSafeInteger(value)) {
|
|
167
|
+
throw new RangeError(`not a safe integer: ${String(value)}`);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
// The product of two safe integers, or undefined where it is not one. Past 2^53 the floating-point
|
|
171
|
+
// product rounds to 2^53 or more, never back below it, so the check of the result is exact.
|
|
172
|
+
function product(a, b) {
|
|
173
|
+
const result = a * b;
|
|
174
|
+
return Number.isSafeInteger(result) ? result : undefined;
|
|
175
|
+
}
|
|
176
|
+
// The same fraction, reduced in bigint where its parts on the way are past a safe integer.
|
|
177
|
+
function exact(num, den) {
|
|
178
|
+
let x = num < 0n ? -num : num;
|
|
179
|
+
let y = den;
|
|
180
|
+
while (y !== 0n) {
|
|
181
|
+
const rest = x % y;
|
|
182
|
+
x = y;
|
|
183
|
+
y = rest;
|
|
184
|
+
}
|
|
185
|
+
const reducedNum = num / x;
|
|
186
|
+
const reducedDen = den / x;
|
|
187
|
+
const top = BigInt(Number.MAX_SAFE_INTEGER);
|
|
188
|
+
if (reducedNum > top || reducedNum < -top || reducedDen > top) {
|
|
189
|
+
throw new RangeError(`not a safe integer in a fraction: ${reducedNum}/${reducedDen}`);
|
|
190
|
+
}
|
|
191
|
+
return made(Number(reducedNum), Number(reducedDen));
|
|
192
|
+
}
|
|
193
|
+
// -0, which a product or a quotient of 0 by a negative number gives, is 0: JSON writes the two
|
|
194
|
+
// alike, and Object.is would part them.
|
|
195
|
+
function made(num, den) {
|
|
196
|
+
return { num: num + 0, den };
|
|
197
|
+
}
|
package/lib/cores/kind.d.ts
CHANGED
|
@@ -55,6 +55,16 @@ export interface Kind<Scene, State extends Json, Move extends Json> {
|
|
|
55
55
|
* path follows. A move listed here that play refuses is skipped.
|
|
56
56
|
*/
|
|
57
57
|
moves(scene: Scene, state: State): readonly Move[];
|
|
58
|
+
/**
|
|
59
|
+
* At least how many moves `moves` lists from the state, counted without listing them, so that a
|
|
60
|
+
* state whose moves alone pass the limit of work stops the search before they are made: a kind
|
|
61
|
+
* one of whose states may have tens of thousands of moves gives it. It is a whole number and never
|
|
62
|
+
* more than moves lists: the search throws on a count that is no whole number, and on one above
|
|
63
|
+
* the list where the count alone keeps within the limit, while a count above the list that passes
|
|
64
|
+
* it would stop a scene the list does not. A kind that leaves it out, or gives 0, has its moves
|
|
65
|
+
* counted once listed, and the work and the limit are the same either way.
|
|
66
|
+
*/
|
|
67
|
+
leastMoves?(scene: Scene, state: State): number;
|
|
58
68
|
/**
|
|
59
69
|
* What a move from the state reads and writes that its canonical key leaves out, in characters as
|
|
60
70
|
* the key would write it: a place the state keeps by leaving it out, which every move still
|
|
@@ -79,8 +89,13 @@ export interface Kind<Scene, State extends Json, Move extends Json> {
|
|
|
79
89
|
* - paths — how many ways of the fewest moves lead to a state that meets it, two moves to one state
|
|
80
90
|
* two ways; where every move leads one layer on, as moves right and down do, that is every way;
|
|
81
91
|
* - max, min — the greatest or least measure of a state that meets it, a safe integer, since the
|
|
82
|
-
* rules are exact
|
|
83
|
-
*
|
|
92
|
+
* rules are exact;
|
|
93
|
+
* - parts — which of the scene's parts meet the goal, by their IDs, none where none does, read off
|
|
94
|
+
* the scene with no search, and the witness that shows it, of the kind's own form: a kind whose
|
|
95
|
+
* answer is no number gives them, and its states meet the goal where they hold that answer. The
|
|
96
|
+
* proof's key gives each ID once, sorted by UTF-16 code units, whatever order the kind gives.
|
|
97
|
+
* Where no state meets the goal, the key is "unreachable", whatever the goal asks but parts, whose
|
|
98
|
+
* key is the parts whatever states the moves reach.
|
|
84
99
|
*/
|
|
85
100
|
export type Ask<State> = {
|
|
86
101
|
readonly key: "fewest";
|
|
@@ -92,6 +107,12 @@ export type Ask<State> = {
|
|
|
92
107
|
} | {
|
|
93
108
|
readonly key: "max" | "min";
|
|
94
109
|
measure(state: State): number;
|
|
110
|
+
} | {
|
|
111
|
+
readonly key: "parts";
|
|
112
|
+
readonly parts: readonly string[];
|
|
113
|
+
readonly witness: {
|
|
114
|
+
readonly [member: string]: Json;
|
|
115
|
+
};
|
|
95
116
|
};
|
|
96
117
|
/**
|
|
97
118
|
* Why a scene gives no proof: limit, its state graph passes the kind's limit of states or of
|
|
@@ -112,7 +133,8 @@ export type Undecided = {
|
|
|
112
133
|
* of its layers. A number with no member asks is the fewest moves to the goal, a path of them its
|
|
113
134
|
* witness; asks names any other key and its witness: count, a path to each state counted — one of
|
|
114
135
|
* each class where the scene gives symmetries — in the graph's order; paths, the first of the
|
|
115
|
-
* ways counted; max or min, the path to the first state of that measure
|
|
136
|
+
* ways counted; max or min, the path to the first state of that measure; parts, the sorted IDs of
|
|
137
|
+
* the parts that meet the goal, with the kind's own witness and no ends. The ends are the states
|
|
116
138
|
* the witness ends in, each meeting the goal. The member witness names its form, so that a core
|
|
117
139
|
* whose witness is no path adds its own.
|
|
118
140
|
*/
|
|
@@ -136,6 +158,12 @@ export type ProofJson = {
|
|
|
136
158
|
readonly path: readonly Json[];
|
|
137
159
|
};
|
|
138
160
|
readonly ends: readonly Json[];
|
|
161
|
+
} | {
|
|
162
|
+
readonly key: readonly string[];
|
|
163
|
+
readonly asks: "parts";
|
|
164
|
+
readonly witness: {
|
|
165
|
+
readonly [member: string]: Json;
|
|
166
|
+
};
|
|
139
167
|
} | {
|
|
140
168
|
readonly key: "unreachable";
|
|
141
169
|
readonly witness: {
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Json, Played, SceneError } from "./kind.js";
|
|
2
|
+
/** A part of a registry: an operation or a goal, by the name a scene and a move give it. */
|
|
3
|
+
export interface Named {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Throws where the registry's version is not "major.minor" or a name of an operation or a goal is
|
|
8
|
+
* given twice or is not in snake_case.
|
|
9
|
+
*/
|
|
10
|
+
export declare function checkRegistry(kind: string, registryVersion: string, parts: readonly Named[]): void;
|
|
11
|
+
/** Adds a name to those taken; throws, naming what it is, where it is taken or not in snake_case. */
|
|
12
|
+
export declare function takeName(taken: Set<string>, named: string, what: string): void;
|
|
13
|
+
/**
|
|
14
|
+
* One part's parameters read at the path, or undefined where they do not read: then an error is
|
|
15
|
+
* there, the part's own or, where it named none, one that the kind cannot read them.
|
|
16
|
+
*/
|
|
17
|
+
export declare function readPart<T>(read: (value: unknown, path: string, errors: SceneError[]) => T | undefined, value: unknown, path: string, errors: SceneError[]): {
|
|
18
|
+
readonly options: T;
|
|
19
|
+
} | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* The scene's goal, a map of one member named by the goal, with its parameters read; null where
|
|
22
|
+
* the scene has none, where it does not read, or where `read` is undefined because what the goal
|
|
23
|
+
* reads against — the kind's sheet or line — did not read itself.
|
|
24
|
+
*/
|
|
25
|
+
export declare function readGoal<G>(input: unknown, goals: ReadonlyMap<string, G>, errors: SceneError[], read: ((goal: G, value: unknown, path: string, errors: SceneError[]) => unknown) | undefined): {
|
|
26
|
+
readonly goal: G;
|
|
27
|
+
readonly options: unknown;
|
|
28
|
+
} | null;
|
|
29
|
+
/** The chosen operations with their parameters, in the registry's order. */
|
|
30
|
+
export declare function inRegistryOrder<O extends Named>(operations: readonly O[], chosen: ReadonlyMap<string, unknown>): {
|
|
31
|
+
readonly op: O;
|
|
32
|
+
readonly options: unknown;
|
|
33
|
+
}[];
|
|
34
|
+
/**
|
|
35
|
+
* A move dispatched to the scene's operation it names, by its one member: refused as not a move,
|
|
36
|
+
* as an operation the kind has but the scene does not allow, or as one the kind does not have.
|
|
37
|
+
*/
|
|
38
|
+
export declare function dispatch<O extends Named, S>(allowed: readonly {
|
|
39
|
+
readonly op: O;
|
|
40
|
+
readonly options: unknown;
|
|
41
|
+
}[], known: ReadonlyMap<string, O>, move: {
|
|
42
|
+
readonly [operation: string]: Json;
|
|
43
|
+
}, play: (op: O, options: unknown, held: Json) => Played<S>): Played<S>;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// What every kind assembled from a registry of operations and goals shares: the check of the
|
|
2
|
+
// registry's names, the reading of one part's parameters and of the scene's goal, and the dispatch
|
|
3
|
+
// of a move to the operation it names. Each kind keeps what its own scene and state add.
|
|
4
|
+
import { isObject, map, pointer } from "./json.js";
|
|
5
|
+
const version = /^[1-9][0-9]*\.(0|[1-9][0-9]*)$/;
|
|
6
|
+
const snakeCase = /^[a-z][a-z0-9_]*$/;
|
|
7
|
+
/**
|
|
8
|
+
* Throws where the registry's version is not "major.minor" or a name of an operation or a goal is
|
|
9
|
+
* given twice or is not in snake_case.
|
|
10
|
+
*/
|
|
11
|
+
export function checkRegistry(kind, registryVersion, parts) {
|
|
12
|
+
if (!version.test(registryVersion)) {
|
|
13
|
+
throw new Error(`the version of ${kind} is "major.minor": ${registryVersion}`);
|
|
14
|
+
}
|
|
15
|
+
const names = new Set();
|
|
16
|
+
for (const part of parts) {
|
|
17
|
+
takeName(names, part.name, "an operation's or a goal's name");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Adds a name to those taken; throws, naming what it is, where it is taken or not in snake_case. */
|
|
21
|
+
export function takeName(taken, named, what) {
|
|
22
|
+
if (!snakeCase.test(named) || taken.has(named)) {
|
|
23
|
+
throw new Error(`${what} is new and in snake_case: ${named}`);
|
|
24
|
+
}
|
|
25
|
+
taken.add(named);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* One part's parameters read at the path, or undefined where they do not read: then an error is
|
|
29
|
+
* there, the part's own or, where it named none, one that the kind cannot read them.
|
|
30
|
+
*/
|
|
31
|
+
export function readPart(read, value, path, errors) {
|
|
32
|
+
const before = errors.length;
|
|
33
|
+
const options = read(value, path, errors);
|
|
34
|
+
if (options === undefined && errors.length === before) {
|
|
35
|
+
errors.push({ path, message: "parameters the kind cannot read" });
|
|
36
|
+
}
|
|
37
|
+
return options === undefined ? undefined : { options };
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The scene's goal, a map of one member named by the goal, with its parameters read; null where
|
|
41
|
+
* the scene has none, where it does not read, or where `read` is undefined because what the goal
|
|
42
|
+
* reads against — the kind's sheet or line — did not read itself.
|
|
43
|
+
*/
|
|
44
|
+
export function readGoal(input, goals, errors, read) {
|
|
45
|
+
const goalMap = input === undefined ? undefined : map(input, "/goal", errors);
|
|
46
|
+
if (goalMap === undefined) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
const entries = Object.entries(goalMap);
|
|
50
|
+
const [named, value] = entries[0] ?? ["", undefined];
|
|
51
|
+
const found = goals.get(named);
|
|
52
|
+
if (entries.length !== 1) {
|
|
53
|
+
errors.push({ path: "/goal", message: "one goal, by its name" });
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
if (found === undefined) {
|
|
57
|
+
errors.push({ path: pointer("/goal", named), message: "no goal of the kind has this name" });
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
if (read === undefined) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
const part = readPart((v, path, errs) => read(found, v, path, errs), value, pointer("/goal", named), errors);
|
|
64
|
+
return part === undefined ? null : { goal: found, options: part.options };
|
|
65
|
+
}
|
|
66
|
+
/** The chosen operations with their parameters, in the registry's order. */
|
|
67
|
+
export function inRegistryOrder(operations, chosen) {
|
|
68
|
+
return operations
|
|
69
|
+
.filter((op) => chosen.has(op.name))
|
|
70
|
+
.map((op) => ({ op, options: chosen.get(op.name) }));
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* A move dispatched to the scene's operation it names, by its one member: refused as not a move,
|
|
74
|
+
* as an operation the kind has but the scene does not allow, or as one the kind does not have.
|
|
75
|
+
*/
|
|
76
|
+
export function dispatch(allowed, known, move, play) {
|
|
77
|
+
const named = isObject(move) ? Object.keys(move) : [];
|
|
78
|
+
if (named.length !== 1) {
|
|
79
|
+
return { ok: false, reason: "not-a-move" };
|
|
80
|
+
}
|
|
81
|
+
const opName = named[0];
|
|
82
|
+
const chosen = allowed.find(({ op }) => op.name === opName);
|
|
83
|
+
if (chosen === undefined) {
|
|
84
|
+
return { ok: false, reason: known.has(opName) ? "op-not-in-scene" : "unknown-op" };
|
|
85
|
+
}
|
|
86
|
+
return play(chosen.op, chosen.options, move[opName]);
|
|
87
|
+
}
|
package/lib/cores/search.d.ts
CHANGED
|
@@ -62,7 +62,8 @@ export type GraphResult<State, Move> = {
|
|
|
62
62
|
}));
|
|
63
63
|
/**
|
|
64
64
|
* A scene's proof with its whole state graph: the key the goal asks with its witness — the path to
|
|
65
|
-
* the state it ends in, or for a count a path to each state counted — and those states
|
|
65
|
+
* the state it ends in, or for a count a path to each state counted — and those states; or the parts
|
|
66
|
+
* a kind reads off its scene, with its own witness.
|
|
66
67
|
*/
|
|
67
68
|
export type Proof<State, Move> = {
|
|
68
69
|
readonly key: number;
|
|
@@ -76,6 +77,13 @@ export type Proof<State, Move> = {
|
|
|
76
77
|
readonly paths: readonly (readonly Move[])[];
|
|
77
78
|
readonly ends: readonly State[];
|
|
78
79
|
readonly graph: StateGraph<State, Move>;
|
|
80
|
+
} | {
|
|
81
|
+
readonly key: readonly string[];
|
|
82
|
+
readonly asks: "parts";
|
|
83
|
+
readonly witness: {
|
|
84
|
+
readonly [member: string]: Json;
|
|
85
|
+
};
|
|
86
|
+
readonly graph: StateGraph<State, Move>;
|
|
79
87
|
} | {
|
|
80
88
|
readonly key: "unreachable";
|
|
81
89
|
readonly ends: readonly [];
|
|
@@ -96,12 +104,20 @@ export declare function stateGraph<Scene, State extends Json, Move extends Json>
|
|
|
96
104
|
* it asks: the fewest moves to the goal, with the path of the first state in the graph's order that
|
|
97
105
|
* meets it; how many states meet it, with a path to each, the first of each class where the scene
|
|
98
106
|
* gives symmetries; how many ways of the fewest moves reach it, with the first; or the greatest or
|
|
99
|
-
* least measure of a state that meets it, with the path to the first state of it
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
107
|
+
* least measure of a state that meets it, with the path to the first state of it; or the parts the
|
|
108
|
+
* kind reads off its scene, with its own witness, whatever states the graph reaches. Where no state
|
|
109
|
+
* of the graph meets any other goal, the key is "unreachable", the whole graph its witness. The
|
|
110
|
+
* order of the graph follows the kind's order of moves, so the same scene gives the same proof on
|
|
111
|
+
* every run, and a key of parts gives each ID once, sorted by UTF-16 code units. A scene with no
|
|
112
|
+
* goal has nothing to prove once its graph is within the limit; past the limit it does not
|
|
113
|
+
* decide, as any scene, nor where a count of ways passes what a key holds exactly.
|
|
104
114
|
*/
|
|
105
115
|
export declare function prove<Scene, State extends Json, Move extends Json>(kind: Kind<Scene, State, Move>, input: unknown, seed: string): Proof<State, Move>;
|
|
116
|
+
/**
|
|
117
|
+
* The form of a key this core proves, as the scene schema of a kind on it declares it under
|
|
118
|
+
* $defs/key: a module's build compares the key of the proof with a scene's key as they are, so the
|
|
119
|
+
* form is the proof's own, whatever the goal asks.
|
|
120
|
+
*/
|
|
121
|
+
export declare const searchKeySchema: Json;
|
|
106
122
|
/** A proof as JSON, as the scene vectors and a module's build take it. */
|
|
107
123
|
export declare function proofJson<State extends Json, Move extends Json>(proof: Proof<State, Move>): ProofJson;
|
package/lib/cores/search.js
CHANGED
|
@@ -35,7 +35,9 @@ function search(kind, scene, seed) {
|
|
|
35
35
|
}
|
|
36
36
|
// The work is a sum over the states, whatever order they are found in, so a graph passes the
|
|
37
37
|
// limit at a build exactly where it does on a device. A state's work is counted before its moves
|
|
38
|
-
// are played, so one with too many moves to try stops the search before they cost anything more
|
|
38
|
+
// are played, so one with too many moves to try stops the search before they cost anything more;
|
|
39
|
+
// where the kind counts them before it lists them, before they are listed too, and since that
|
|
40
|
+
// count is never more than the list, the search stops at the same state either way.
|
|
39
41
|
let work = 0;
|
|
40
42
|
const nodes = [];
|
|
41
43
|
const byKey = new Map();
|
|
@@ -65,15 +67,12 @@ function search(kind, scene, seed) {
|
|
|
65
67
|
// The version a state is played at is its layer, though a widget may reach the state at a
|
|
66
68
|
// later version, by an undo or a longer way: a kind on this core reads nothing of `at`.
|
|
67
69
|
const at = { version: node.layer, seed };
|
|
68
|
-
const
|
|
69
|
-
if (
|
|
70
|
-
const implied = kind.implied?.(scene, node.state) ?? 0;
|
|
71
|
-
work += moves.length * (node.key.length + implied + moveWork);
|
|
72
|
-
}
|
|
73
|
-
if (work > kind.workLimit) {
|
|
70
|
+
const listed = listMoves(kind, scene, node.state, node.key, work);
|
|
71
|
+
if (listed === null) {
|
|
74
72
|
return { ok: false, undecided: "limit", passes: "work" };
|
|
75
73
|
}
|
|
76
|
-
|
|
74
|
+
work = listed.work;
|
|
75
|
+
for (const move of listed.moves) {
|
|
77
76
|
const played = kind.play(scene, node.state, move, at);
|
|
78
77
|
if (!played.ok) {
|
|
79
78
|
continue;
|
|
@@ -130,6 +129,35 @@ function search(kind, scene, seed) {
|
|
|
130
129
|
};
|
|
131
130
|
return { ok: true, graph };
|
|
132
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* The moves the kind lists from a state and the work so far with theirs, or null where the work
|
|
134
|
+
* passes the kind's limit: before the moves are listed, where the kind counts them first. It stays
|
|
135
|
+
* out of the search's loop: a closure over the loop's state there slowed every graph by half on the
|
|
136
|
+
* stand-in for a phone (measurements 2.5).
|
|
137
|
+
*/
|
|
138
|
+
function listMoves(kind, scene, state, key, before) {
|
|
139
|
+
const least = kind.leastMoves?.(scene, state) ?? 0;
|
|
140
|
+
if (!Number.isSafeInteger(least) || least < 0) {
|
|
141
|
+
throw new RangeError(`a kind's count of a state's moves is a whole number: ${least}`);
|
|
142
|
+
}
|
|
143
|
+
let weight = 0;
|
|
144
|
+
if (least > 0) {
|
|
145
|
+
weight = key.length + (kind.implied?.(scene, state) ?? 0) + moveWork;
|
|
146
|
+
if (before + least * weight > kind.workLimit) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const moves = kind.moves(scene, state);
|
|
151
|
+
if (moves.length < least) {
|
|
152
|
+
throw new RangeError(`the kind listed ${moves.length} moves from a state it counted at least ${least} from`);
|
|
153
|
+
}
|
|
154
|
+
let work = before;
|
|
155
|
+
if (moves.length > 0) {
|
|
156
|
+
weight ||= key.length + (kind.implied?.(scene, state) ?? 0) + moveWork;
|
|
157
|
+
work += moves.length * weight;
|
|
158
|
+
}
|
|
159
|
+
return work > kind.workLimit ? null : { moves, work };
|
|
160
|
+
}
|
|
133
161
|
/** What the scene's goal asks: the fewest moves where the kind does not say, or the scene has no goal. */
|
|
134
162
|
function asked(kind, scene) {
|
|
135
163
|
return (kind.hasGoal(scene) ? kind.asks?.(scene) : undefined) ?? { key: "fewest" };
|
|
@@ -180,11 +208,13 @@ function pathTo(graph, end) {
|
|
|
180
208
|
* it asks: the fewest moves to the goal, with the path of the first state in the graph's order that
|
|
181
209
|
* meets it; how many states meet it, with a path to each, the first of each class where the scene
|
|
182
210
|
* gives symmetries; how many ways of the fewest moves reach it, with the first; or the greatest or
|
|
183
|
-
* least measure of a state that meets it, with the path to the first state of it
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
211
|
+
* least measure of a state that meets it, with the path to the first state of it; or the parts the
|
|
212
|
+
* kind reads off its scene, with its own witness, whatever states the graph reaches. Where no state
|
|
213
|
+
* of the graph meets any other goal, the key is "unreachable", the whole graph its witness. The
|
|
214
|
+
* order of the graph follows the kind's order of moves, so the same scene gives the same proof on
|
|
215
|
+
* every run, and a key of parts gives each ID once, sorted by UTF-16 code units. A scene with no
|
|
216
|
+
* goal has nothing to prove once its graph is within the limit; past the limit it does not
|
|
217
|
+
* decide, as any scene, nor where a count of ways passes what a key holds exactly.
|
|
188
218
|
*/
|
|
189
219
|
export function prove(kind, input, seed) {
|
|
190
220
|
const read = kind.read(input);
|
|
@@ -199,11 +229,17 @@ export function prove(kind, input, seed) {
|
|
|
199
229
|
return { undecided: "no-goal" };
|
|
200
230
|
}
|
|
201
231
|
const graph = result.graph;
|
|
232
|
+
const ask = asked(kind, read.scene);
|
|
233
|
+
if (ask.key === "parts") {
|
|
234
|
+
// The parts are read off the scene, whatever states its moves reach; a module's build compares
|
|
235
|
+
// keys as written, so the order is the proof's, not the kind's.
|
|
236
|
+
const key = [...new Set(ask.parts)].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
237
|
+
return { key, asks: "parts", witness: ask.witness, graph };
|
|
238
|
+
}
|
|
202
239
|
const first = graph.nodes.find((node) => node.goal);
|
|
203
240
|
if (first === undefined) {
|
|
204
241
|
return { key: "unreachable", ends: [], graph };
|
|
205
242
|
}
|
|
206
|
-
const ask = asked(kind, read.scene);
|
|
207
243
|
switch (ask.key) {
|
|
208
244
|
case "count": {
|
|
209
245
|
const ends = counted(graph, ask.images);
|
|
@@ -240,6 +276,23 @@ export function prove(kind, input, seed) {
|
|
|
240
276
|
};
|
|
241
277
|
}
|
|
242
278
|
}
|
|
279
|
+
/**
|
|
280
|
+
* The form of a key this core proves, as the scene schema of a kind on it declares it under
|
|
281
|
+
* $defs/key: a module's build compares the key of the proof with a scene's key as they are, so the
|
|
282
|
+
* form is the proof's own, whatever the goal asks.
|
|
283
|
+
*/
|
|
284
|
+
export const searchKeySchema = {
|
|
285
|
+
description: "The key of a scene, the form of the kind solver's proof: a whole number, whatever the goal asks — the fewest moves, a count, a maximum or a minimum —, or unreachable, where no state meets the goal.",
|
|
286
|
+
oneOf: [
|
|
287
|
+
{
|
|
288
|
+
description: "What the goal asks, a whole number.",
|
|
289
|
+
type: "integer",
|
|
290
|
+
minimum: 0,
|
|
291
|
+
maximum: Number.MAX_SAFE_INTEGER,
|
|
292
|
+
},
|
|
293
|
+
{ description: "No state of the scene's state graph meets the goal.", const: "unreachable" },
|
|
294
|
+
],
|
|
295
|
+
};
|
|
243
296
|
/** A proof as JSON, as the scene vectors and a module's build take it. */
|
|
244
297
|
export function proofJson(proof) {
|
|
245
298
|
if ("undecided" in proof) {
|
|
@@ -252,6 +305,9 @@ export function proofJson(proof) {
|
|
|
252
305
|
ends: [],
|
|
253
306
|
};
|
|
254
307
|
}
|
|
308
|
+
if (proof.asks === "parts") {
|
|
309
|
+
return { key: proof.key, asks: "parts", witness: proof.witness };
|
|
310
|
+
}
|
|
255
311
|
if (proof.asks === "count") {
|
|
256
312
|
return { key: proof.key, asks: "count", witness: { paths: proof.paths }, ends: proof.ends };
|
|
257
313
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Json } from "../../cores/kind.js";
|
|
2
|
+
import { type Card, type Condition, type Literal, type Scene } from "./scene.js";
|
|
3
|
+
/** The cards a goal ranges over, in the scene's order. */
|
|
4
|
+
export declare function range(scene: Scene, among: readonly string[]): Card[];
|
|
5
|
+
/** Whether a card meets a literal: has the value it names, or any other. */
|
|
6
|
+
export declare function fits(card: Card, literal: Literal): boolean;
|
|
7
|
+
/** A condition's literals, depth first, in the order the witness gives a card's fits. */
|
|
8
|
+
export declare function literals(condition: Condition): Literal[];
|
|
9
|
+
/** The answer of a scene with a goal: the sorted IDs of the cards that meet it, and its witness. */
|
|
10
|
+
export interface Answer {
|
|
11
|
+
readonly parts: readonly string[];
|
|
12
|
+
readonly witness: {
|
|
13
|
+
readonly [member: string]: Json;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The cards of the goal's range that meet it, by their IDs sorted by UTF-16 code units, and the
|
|
18
|
+
* witness: the rule — the condition the goal names, the values the group shares as an all of them,
|
|
19
|
+
* or the signs a card must differ by — and a row for each card of the range in the scene's order,
|
|
20
|
+
* with whether it fits each literal of the rule, depth first, or the first other card it does not
|
|
21
|
+
* differ from, none for a card that differs from every other.
|
|
22
|
+
*/
|
|
23
|
+
export declare function answer(scene: Scene): Answer | null;
|
|
24
|
+
/** A condition as the scene's JSON gives it. */
|
|
25
|
+
export declare function conditionJson(condition: Condition): Json;
|