@altnovatrail/widgets 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/README.md +1 -1
  2. package/kinds/line/line.handles.json +195 -0
  3. package/kinds/line/line.schema.json +321 -0
  4. package/kinds/line/vectors/cases.json +213 -0
  5. package/kinds/line/vectors/gaps-12-d1-1.json +140 -0
  6. package/kinds/line/vectors/gaps-12-d1-2.json +95 -0
  7. package/kinds/line/vectors/gaps-12-d1-3.json +122 -0
  8. package/kinds/line/vectors/gaps-12-d1-4.json +121 -0
  9. package/kinds/line/vectors/gaps-12-d1-5.json +87 -0
  10. package/kinds/line/vectors/gaps-12-d2-1.json +173 -0
  11. package/kinds/line/vectors/gaps-12-d2-6.json +87 -0
  12. package/kinds/line/vectors/gaps-12-d3-2.json +173 -0
  13. package/kinds/line/vectors/gaps-12-d4-4.json +168 -0
  14. package/kinds/line/vectors/gaps-12-d5-3.json +99 -0
  15. package/kinds/line/vectors/gaps-12-d5-4.json +215 -0
  16. package/kinds/line/vectors/gaps-34-d1-1.json +37 -0
  17. package/kinds/line/vectors/gaps-34-d1-2.json +130 -0
  18. package/kinds/line/vectors/gaps-34-d1-4.json +155 -0
  19. package/kinds/line/vectors/gaps-34-d1-5.json +43 -0
  20. package/kinds/line/vectors/gaps-34-d3-2.json +38 -0
  21. package/kinds/line/vectors/gaps-34-d3-5.json +177 -0
  22. package/kinds/line/vectors/gaps-34-d5-3.json +39 -0
  23. package/kinds/line/vectors/invalid-line.json +34 -0
  24. package/kinds/line/vectors/invalid-ring.json +34 -0
  25. package/kinds/line/vectors/invalid.json +43 -0
  26. package/kinds/line/vectors/jump-unreachable.json +104 -0
  27. package/kinds/line/vectors/limit.json +40 -0
  28. package/kinds/line/vectors/op-not-in-scene.json +47 -0
  29. package/kinds/line/vectors/ring-cut.json +104 -0
  30. package/kinds/line/vectors/ring-every.json +107 -0
  31. package/kinds/line/vectors/ring-jump.json +135 -0
  32. package/lib/cores/arith.d.ts +62 -0
  33. package/lib/cores/arith.js +197 -0
  34. package/lib/cores/kind.d.ts +10 -0
  35. package/lib/cores/registry.d.ts +43 -0
  36. package/lib/cores/registry.js +87 -0
  37. package/lib/cores/search.js +36 -8
  38. package/lib/kinds/cells/kind.js +11 -64
  39. package/lib/kinds/cells/operation.d.ts +6 -0
  40. package/lib/kinds/cells/polyomino.d.ts +14 -0
  41. package/lib/kinds/cells/polyomino.js +100 -24
  42. package/lib/kinds/line/cut.d.ts +2 -0
  43. package/lib/kinds/line/cut.js +63 -0
  44. package/lib/kinds/line/goals.d.ts +22 -0
  45. package/lib/kinds/line/goals.js +137 -0
  46. package/lib/kinds/line/index.d.ts +13 -0
  47. package/lib/kinds/line/index.js +17 -0
  48. package/lib/kinds/line/jump.d.ts +10 -0
  49. package/lib/kinds/line/jump.js +95 -0
  50. package/lib/kinds/line/kind.d.ts +25 -0
  51. package/lib/kinds/line/kind.js +125 -0
  52. package/lib/kinds/line/line.d.ts +63 -0
  53. package/lib/kinds/line/line.js +157 -0
  54. package/lib/kinds/line/operation.d.ts +69 -0
  55. package/lib/kinds/line/operation.js +1 -0
  56. package/lib/kinds/line/posts.d.ts +11 -0
  57. package/lib/kinds/line/posts.js +85 -0
  58. package/lib/kinds/line/registry.d.ts +2 -0
  59. package/lib/kinds/line/registry.js +11 -0
  60. package/lib/kinds/line/schema.d.ts +4 -0
  61. package/lib/kinds/line/schema.js +157 -0
  62. package/package.json +9 -2
@@ -0,0 +1,69 @@
1
+ import type { Ask, Json, Played, SceneError } from "../../cores/kind.js";
2
+ import type { Line, State } from "./line.js";
3
+ /**
4
+ * A part of the kind's JSON Schema, written by the conventions of the module format's schemas:
5
+ * an object closed by unevaluatedProperties, a description for each member.
6
+ */
7
+ export type Schema = {
8
+ readonly [keyword: string]: Json;
9
+ };
10
+ /** A number of a state, a safe integer, by the line and an operation's parameters. */
11
+ export interface Counter<Options = unknown> {
12
+ /** What the number counts, for the schema. */
13
+ readonly description: string;
14
+ value(line: Line, options: Options, state: State): number;
15
+ }
16
+ /** A counter bound to a scene: the number of a state. */
17
+ export type Count = (state: State) => number;
18
+ export interface Operation<Options = unknown> {
19
+ /** The operation's name: its member in a scene's "operations" and in a move. */
20
+ readonly name: string;
21
+ readonly schema: {
22
+ /** The operation's parameters in a scene, under "operations". */
23
+ readonly options: Schema;
24
+ /** What a move of the operation holds, under the operation's name. */
25
+ readonly move: Schema;
26
+ /** The member of the state the operation keeps besides the posts, if it keeps one. */
27
+ readonly state?: {
28
+ readonly member: string;
29
+ readonly schema: Schema;
30
+ };
31
+ };
32
+ /** The parameters, read by the line; undefined where they cannot be read, with their errors added. */
33
+ read(value: unknown, line: Line, path: string, errors: SceneError[]): Options | undefined;
34
+ /** The value of the operation's own member of the state at the scene's start. */
35
+ start?(line: Line, options: Options): Json;
36
+ /** A move, given what it holds under the operation's name, or the rules' short reason to refuse it. */
37
+ play(line: Line, options: Options, state: State, move: Json): Played<State>;
38
+ /** What every move of the operation to try from the state holds, in a fixed order. */
39
+ moves(line: Line, options: Options, state: State): readonly Json[];
40
+ /** The counters the operation adds, by their names. */
41
+ readonly counters?: Readonly<Record<string, Counter<Options>>>;
42
+ }
43
+ export interface Goal<Options = unknown> {
44
+ /** The goal's name: its member in a scene's "goal". */
45
+ readonly name: string;
46
+ /** The goal's parameters in a scene; a counter is named by a $ref to #/$defs/counter. */
47
+ readonly schema: Schema;
48
+ /**
49
+ * The parameters, or undefined with their errors added, by the line and the counters the scene
50
+ * has: the line's own, and each of the scene's operations' that the kind has, by its name, the
51
+ * count undefined where the operation's parameters do not read, or, where the scene's operations
52
+ * are no map, for every operation of the kind, the scene refused either way.
53
+ */
54
+ read(value: unknown, line: Line, path: string, errors: SceneError[], counters: ReadonlyMap<string, Count | undefined>): Options | undefined;
55
+ meets(line: Line, options: Options, state: State): boolean;
56
+ /** What the goal asks of the scene's state graph where it is not the fewest moves. */
57
+ asks?(line: Line, options: Options): Ask<State>;
58
+ }
59
+ /** The operations and goals of the kind, and the version of its schema they make. */
60
+ export interface Registry {
61
+ /**
62
+ * The version of the kind's schema, "major.minor": a new operation, goal or counter is the next
63
+ * minor version, and a change of one a scene, a move or a state already uses the next major one.
64
+ */
65
+ readonly version: string;
66
+ /** In the order a state's moves are listed in, and a proof's path with them. */
67
+ readonly operations: readonly Operation[];
68
+ readonly goals: readonly Goal[];
69
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ import type { SceneError } from "../../cores/kind.js";
2
+ import type { Counter, Operation } from "./operation.js";
3
+ /** The line's own counters, which every scene has. */
4
+ export declare const lineCounters: Readonly<Record<string, Counter>>;
5
+ /** The schema of a move that names a place, {"at": <number>}. */
6
+ export declare function placeMove(description: string): Operation["schema"]["move"];
7
+ /** The schema of parameters that are none, {}. */
8
+ export declare function noOptions(description: string): Operation["schema"]["options"];
9
+ /** Parameters that are none, {}: true, or undefined with their errors added. */
10
+ export declare function readNone(value: unknown, path: string, errors: SceneError[]): true | undefined;
11
+ export declare const post: Operation<true>;
@@ -0,0 +1,85 @@
1
+ // The posts of the line — trees in a row, stops of a bus, pages read, flowers, lamps — and the
2
+ // operation that puts one on a free place. The line keeps its posts under "posts" whether or not a
3
+ // scene allows the operation, so a line only to look at has them too, and its counters: the posts,
4
+ // the gaps between neighbouring posts and their lengths together — on a ring, as many gaps as
5
+ // posts, which is what a closed line teaches, and the whole way round. A post is
6
+ // never taken away by a move: the widget's undo is the shell's, and a post that could go would make
7
+ // every graph one of every set of places both ways, with no key shorter.
8
+ import { object } from "../../cores/json.js";
9
+ import { moveAt, placesOf, withPlace } from "./line.js";
10
+ const posts = (line, state) => placesOf(line, state, "posts");
11
+ /** The line's own counters, which every scene has. */
12
+ export const lineCounters = {
13
+ posts: {
14
+ description: "The posts on the line.",
15
+ value: (line, _options, state) => posts(line, state).length,
16
+ },
17
+ gaps: {
18
+ description: "The gaps between neighbouring posts: on an open line one fewer than the posts, on a ring as many as the posts, a lone post's gap the way round from it back to it; none where there is no post.",
19
+ value(line, _options, state) {
20
+ const on = posts(line, state).length;
21
+ return line.closed ? on : Math.max(on - 1, 0);
22
+ },
23
+ },
24
+ length: {
25
+ description: "The gaps' lengths together, each its count of places times the spacing: on an open line from the first post to the last, 0 where there are fewer than two posts; on a ring the whole way round where there is a post, 0 where there is none.",
26
+ value(line, _options, state) {
27
+ const on = posts(line, state);
28
+ if (line.closed) {
29
+ return on.length === 0 ? 0 : line.places * line.spacing;
30
+ }
31
+ return on.length < 2 ? 0 : (on.at(-1) - on[0]) * line.spacing;
32
+ },
33
+ },
34
+ };
35
+ function refuse(reason) {
36
+ return { ok: false, reason };
37
+ }
38
+ /** The schema of a move that names a place, {"at": <number>}. */
39
+ export function placeMove(description) {
40
+ return {
41
+ description,
42
+ type: "object",
43
+ properties: { at: { description: "The place, by its number.", $ref: "#/$defs/place" } },
44
+ required: ["at"],
45
+ unevaluatedProperties: false,
46
+ };
47
+ }
48
+ /** The schema of parameters that are none, {}. */
49
+ export function noOptions(description) {
50
+ return { description, type: "object", unevaluatedProperties: false };
51
+ }
52
+ /** Parameters that are none, {}: true, or undefined with their errors added. */
53
+ export function readNone(value, path, errors) {
54
+ const before = errors.length;
55
+ return object(value, path, errors, []) === undefined || errors.length > before ? undefined : true;
56
+ }
57
+ export const post = {
58
+ name: "post",
59
+ schema: {
60
+ options: noOptions("Put a post on any free place of the line; no parameters."),
61
+ move: placeMove("Put a post on a place."),
62
+ },
63
+ read: (value, _line, path, errors) => readNone(value, path, errors),
64
+ play(line, _options, state, move) {
65
+ const at = moveAt(line, move);
66
+ if (at === undefined) {
67
+ return refuse("no-place");
68
+ }
69
+ const on = posts(line, state);
70
+ if (on.includes(at)) {
71
+ return refuse("taken");
72
+ }
73
+ return { ok: true, state: { ...state, posts: withPlace(on, at) } };
74
+ },
75
+ moves(line, _options, state) {
76
+ const on = new Set(posts(line, state));
77
+ const moves = [];
78
+ for (let place = line.first; place < line.first + line.places; place++) {
79
+ if (!on.has(place)) {
80
+ moves.push({ at: place });
81
+ }
82
+ }
83
+ return moves;
84
+ },
85
+ };
@@ -0,0 +1,2 @@
1
+ import type { Registry } from "./operation.js";
2
+ export declare const registry: Registry;
@@ -0,0 +1,11 @@
1
+ // The operations and goals of the kind line. A new operation or goal is a module of its own,
2
+ // listed here, with the next minor version: nothing else of the kind changes for it.
3
+ import { cut } from "./cut.js";
4
+ import { counters, every } from "./goals.js";
5
+ import { jump } from "./jump.js";
6
+ import { post } from "./posts.js";
7
+ export const registry = {
8
+ version: "1.2",
9
+ operations: [post, cut, jump],
10
+ goals: [counters, every],
11
+ };
@@ -0,0 +1,4 @@
1
+ import type { Registry, Schema } from "./operation.js";
2
+ /** The $id of the kind's schema at a version: a name, which need not resolve. */
3
+ export declare function schemaId(version: string): string;
4
+ export declare function lineSchema(registry: Registry): Schema;
@@ -0,0 +1,157 @@
1
+ import { maxNumber, maxPlaces, maxSpacing } from "./line.js";
2
+ import { lineCounters } from "./posts.js";
3
+ /** The $id of the kind's schema at a version: a name, which need not resolve. */
4
+ export function schemaId(version) {
5
+ return `https://altnovatrail.github.io/widgets/kinds/line/${version}/line.schema.json`;
6
+ }
7
+ const places = (description) => ({
8
+ description,
9
+ type: "array",
10
+ items: { description: "A place, by its number.", $ref: "#/$defs/place" },
11
+ uniqueItems: true,
12
+ });
13
+ export function lineSchema(registry) {
14
+ const options = {};
15
+ const moves = {};
16
+ const state = {
17
+ posts: places("The places a post stands on now, by their numbers, in ascending order."),
18
+ };
19
+ const counters = Object.entries(lineCounters).map(([name, c]) => `${name}: ${c.description}`);
20
+ for (const op of registry.operations) {
21
+ options[op.name] = op.schema.options;
22
+ moves[op.name] = op.schema.move;
23
+ if (op.schema.state !== undefined) {
24
+ state[op.schema.state.member] = op.schema.state.schema;
25
+ }
26
+ for (const [name, c] of Object.entries(op.counters ?? {})) {
27
+ counters.push(`${name}, where the scene allows ${op.name}: ${c.description}`);
28
+ }
29
+ }
30
+ const names = counters.map((counter) => counter.slice(0, counter.search(/[,:]/)));
31
+ const goals = {};
32
+ for (const goal of registry.goals) {
33
+ goals[goal.name] = goal.schema;
34
+ }
35
+ return {
36
+ $schema: "https://json-schema.org/draft/2020-12/schema",
37
+ $id: schemaId(registry.version),
38
+ title: "A scene of the widget kind line",
39
+ description: "A line of places, the operations a learner may play on it with their parameters, and at most one goal a state may meet. Under $defs, a move and a state of the kind.",
40
+ type: "object",
41
+ properties: {
42
+ line: {
43
+ description: "The line as the scene gives it.",
44
+ $ref: "#/$defs/line",
45
+ unevaluatedProperties: false,
46
+ },
47
+ operations: {
48
+ description: "The operations a learner may play on the scene, each by its name with its parameters; none, for a line only to look at.",
49
+ type: "object",
50
+ properties: options,
51
+ unevaluatedProperties: false,
52
+ },
53
+ goal: {
54
+ description: "What a state is to meet, one goal by its name with its parameters.",
55
+ type: "object",
56
+ properties: goals,
57
+ minProperties: 1,
58
+ maxProperties: 1,
59
+ unevaluatedProperties: false,
60
+ },
61
+ },
62
+ required: ["line", "operations"],
63
+ unevaluatedProperties: false,
64
+ $defs: {
65
+ place: {
66
+ description: "A place of the line by its number: the line's first number for its first place, and each next place the next number.",
67
+ type: "integer",
68
+ minimum: 0,
69
+ maximum: maxNumber,
70
+ },
71
+ counter: {
72
+ description: `A counter, a number of a state the widget shows, by its name: ${counters.join(" ")}`,
73
+ enum: names,
74
+ },
75
+ line: {
76
+ description: "Places in a row, numbered from the first number on, each a spacing from the next, open or closed into a ring, and the posts that stand on them at the start.",
77
+ type: "object",
78
+ properties: {
79
+ places: {
80
+ description: "The count of places.",
81
+ type: "integer",
82
+ minimum: 2,
83
+ maximum: maxPlaces,
84
+ },
85
+ first: {
86
+ description: "The number of the first place, as the statement numbers it — a floor, a page, a post —, so that the last place's number is at most the greatest a place has; 0 where left out.",
87
+ type: "integer",
88
+ minimum: 0,
89
+ maximum: maxNumber,
90
+ },
91
+ spacing: {
92
+ description: "The length from a place to the next, in the statement's unit — metres, steps, minutes; 1 where left out.",
93
+ type: "integer",
94
+ minimum: 1,
95
+ maximum: maxSpacing,
96
+ },
97
+ vertical: {
98
+ description: "Whether the line is drawn upright, its first place at the bottom, as floors, stairs or a well; it changes no rule, and a ring is never upright. False where left out.",
99
+ type: "boolean",
100
+ },
101
+ closed: {
102
+ description: "Whether the line is closed into a ring, its last place next to its first, as bushes round a flowerbed or a fence round a garden: as many gaps as posts, no end to cut at, and a jumper going on round it. False where left out.",
103
+ type: "boolean",
104
+ },
105
+ sides: {
106
+ description: "A ring's sides, where it is drawn as a figure with corners, as a square flowerbed or a garden's fence: each side by its count of gaps, the first place a corner and each next corner a side on, the sides' gaps together the ring's places. A round ring where left out; it changes no rule.",
107
+ type: "array",
108
+ items: {
109
+ description: "A side's count of gaps.",
110
+ type: "integer",
111
+ minimum: 1,
112
+ maximum: maxPlaces - 1,
113
+ },
114
+ minItems: 3,
115
+ maxItems: maxPlaces,
116
+ },
117
+ posts: places("The places a post stands on at the scene's start, each once."),
118
+ },
119
+ required: ["places"],
120
+ dependentRequired: { sides: ["closed"] },
121
+ allOf: [
122
+ {
123
+ not: {
124
+ properties: {
125
+ closed: { description: "A ring.", const: true },
126
+ vertical: { description: "Upright.", const: true },
127
+ },
128
+ required: ["closed", "vertical"],
129
+ },
130
+ },
131
+ {
132
+ not: {
133
+ properties: {
134
+ closed: { description: "An open line.", const: false },
135
+ sides: { description: "Sides given." },
136
+ },
137
+ required: ["closed", "sides"],
138
+ },
139
+ },
140
+ ],
141
+ },
142
+ move: {
143
+ description: "A move: one member, named by the operation it plays, holding what a move of the operation holds.",
144
+ type: "object",
145
+ properties: moves,
146
+ minProperties: 1,
147
+ maxProperties: 1,
148
+ },
149
+ state: {
150
+ description: "A state: the posts, and a member for each operation of the scene that keeps one.",
151
+ type: "object",
152
+ properties: state,
153
+ required: ["posts"],
154
+ },
155
+ },
156
+ };
157
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@altnovatrail/widgets",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "NovaTrail's widget kinds: each kind's rules and solver over its states, and the shell they share.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -46,7 +46,14 @@
46
46
  },
47
47
  "./kinds/cells/cells.schema.json": "./kinds/cells/cells.schema.json",
48
48
  "./kinds/cells/cells.handles.json": "./kinds/cells/cells.handles.json",
49
- "./kinds/cells/vectors/*": "./kinds/cells/vectors/*"
49
+ "./kinds/cells/vectors/*": "./kinds/cells/vectors/*",
50
+ "./kinds/line": {
51
+ "types": "./lib/kinds/line/index.d.ts",
52
+ "default": "./lib/kinds/line/index.js"
53
+ },
54
+ "./kinds/line/line.schema.json": "./kinds/line/line.schema.json",
55
+ "./kinds/line/line.handles.json": "./kinds/line/line.handles.json",
56
+ "./kinds/line/vectors/*": "./kinds/line/vectors/*"
50
57
  },
51
58
  "devDependencies": {
52
59
  "@biomejs/biome": "2.6.9",