@nakafa/aksara-contracts 8.4.1 → 8.8.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.
Files changed (47) hide show
  1. package/dist/hash/sha256.d.ts +16 -0
  2. package/dist/hash/sha256.js +164 -0
  3. package/dist/hash/stream.d.ts +7 -5
  4. package/dist/hash/stream.js +13 -18
  5. package/dist/math/boundary.js +3 -22
  6. package/dist/math/edge.d.ts +11 -0
  7. package/dist/math/edge.js +74 -0
  8. package/dist/math/graph.d.ts +9 -0
  9. package/dist/math/graph.js +66 -0
  10. package/dist/math/plane.js +6 -0
  11. package/dist/math/shape.d.ts +392 -0
  12. package/dist/math/shape.js +59 -0
  13. package/dist/math/straight.d.ts +13 -0
  14. package/dist/math/straight.js +26 -0
  15. package/dist/visual/biology/blood.d.ts +91 -0
  16. package/dist/visual/biology/blood.js +55 -0
  17. package/dist/visual/biology/heart.d.ts +92 -0
  18. package/dist/visual/biology/heart.js +54 -0
  19. package/dist/visual/biology/mitochondrion.d.ts +91 -0
  20. package/dist/visual/biology/mitochondrion.js +45 -0
  21. package/dist/visual/biology/muscle.d.ts +103 -0
  22. package/dist/visual/biology/muscle.js +62 -0
  23. package/dist/visual/biology/neuron.d.ts +97 -0
  24. package/dist/visual/biology/neuron.js +48 -0
  25. package/dist/visual/biology/paddy.d.ts +100 -0
  26. package/dist/visual/biology/paddy.js +54 -0
  27. package/dist/visual/biology/synapse.d.ts +101 -0
  28. package/dist/visual/biology/synapse.js +46 -0
  29. package/dist/visual/biology/vessel.d.ts +104 -0
  30. package/dist/visual/biology/vessel.js +60 -0
  31. package/dist/visual/biology/yeast.d.ts +108 -0
  32. package/dist/visual/biology/yeast.js +61 -0
  33. package/dist/visual/catalog.d.ts +2177 -778
  34. package/dist/visual/catalog.js +28 -0
  35. package/dist/visual/mathematics/journey.d.ts +118 -0
  36. package/dist/visual/mathematics/journey.js +125 -0
  37. package/dist/visual/mathematics/logic.d.ts +90 -0
  38. package/dist/visual/mathematics/logic.js +49 -0
  39. package/dist/visual/mathematics/network.d.ts +101 -0
  40. package/dist/visual/mathematics/network.js +97 -0
  41. package/dist/visual/mathematics/numeral.d.ts +81 -0
  42. package/dist/visual/mathematics/numeral.js +48 -0
  43. package/dist/visual/mathematics/sets.d.ts +92 -0
  44. package/dist/visual/mathematics/sets.js +95 -0
  45. package/dist/visual/physics/thermodynamics.d.ts +117 -0
  46. package/dist/visual/physics/thermodynamics.js +95 -0
  47. package/package.json +1 -1
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Incremental SHA-256 (FIPS 180-4) over bytes added in pieces. It keeps one
3
+ * 64-byte block and the eight state words, so hashing a stream of any length
4
+ * needs constant memory, and its digest equals Web Crypto's digest of the
5
+ * same bytes joined.
6
+ */
7
+ export declare class Sha256 {
8
+ #private;
9
+ /** Adds the next bytes of the message. */
10
+ update(bytes: Uint8Array): void;
11
+ /**
12
+ * Pads the message, finishes the hash, and returns its 32 digest bytes. It is
13
+ * read once: the padding becomes part of the state.
14
+ */
15
+ digest(): Uint8Array;
16
+ }
@@ -0,0 +1,164 @@
1
+ // biome-ignore-all lint/suspicious/noBitwiseOperators: SHA-256 is defined on 32-bit words (FIPS 180-4), so its rotations, shifts, and logical functions are bitwise by definition.
2
+ /**
3
+ * Packs 32-bit words into a big-endian view, so every read of a word index is a
4
+ * number and the state view's bytes are the digest bytes.
5
+ */
6
+ function wordView(words) {
7
+ const view = new DataView(new ArrayBuffer(words.length * 4));
8
+ let index = 0;
9
+ for (const word of words) {
10
+ view.setUint32(index * 4, word);
11
+ index += 1;
12
+ }
13
+ return view;
14
+ }
15
+ /**
16
+ * The 64 round constants of SHA-256: the first 32 bits of the fractional parts
17
+ * of the cube roots of the first 64 primes (FIPS 180-4, section 4.2.2).
18
+ */
19
+ const ROUND_CONSTANTS = wordView([
20
+ 0x42_8a_2f_98, 0x71_37_44_91, 0xb5_c0_fb_cf, 0xe9_b5_db_a5, 0x39_56_c2_5b,
21
+ 0x59_f1_11_f1, 0x92_3f_82_a4, 0xab_1c_5e_d5, 0xd8_07_aa_98, 0x12_83_5b_01,
22
+ 0x24_31_85_be, 0x55_0c_7d_c3, 0x72_be_5d_74, 0x80_de_b1_fe, 0x9b_dc_06_a7,
23
+ 0xc1_9b_f1_74, 0xe4_9b_69_c1, 0xef_be_47_86, 0x0f_c1_9d_c6, 0x24_0c_a1_cc,
24
+ 0x2d_e9_2c_6f, 0x4a_74_84_aa, 0x5c_b0_a9_dc, 0x76_f9_88_da, 0x98_3e_51_52,
25
+ 0xa8_31_c6_6d, 0xb0_03_27_c8, 0xbf_59_7f_c7, 0xc6_e0_0b_f3, 0xd5_a7_91_47,
26
+ 0x06_ca_63_51, 0x14_29_29_67, 0x27_b7_0a_85, 0x2e_1b_21_38, 0x4d_2c_6d_fc,
27
+ 0x53_38_0d_13, 0x65_0a_73_54, 0x76_6a_0a_bb, 0x81_c2_c9_2e, 0x92_72_2c_85,
28
+ 0xa2_bf_e8_a1, 0xa8_1a_66_4b, 0xc2_4b_8b_70, 0xc7_6c_51_a3, 0xd1_92_e8_19,
29
+ 0xd6_99_06_24, 0xf4_0e_35_85, 0x10_6a_a0_70, 0x19_a4_c1_16, 0x1e_37_6c_08,
30
+ 0x27_48_77_4c, 0x34_b0_bc_b5, 0x39_1c_0c_b3, 0x4e_d8_aa_4a, 0x5b_9c_ca_4f,
31
+ 0x68_2e_6f_f3, 0x74_8f_82_ee, 0x78_a5_63_6f, 0x84_c8_78_14, 0x8c_c7_02_08,
32
+ 0x90_be_ff_fa, 0xa4_50_6c_eb, 0xbe_f9_a3_f7, 0xc6_71_78_f2,
33
+ ]);
34
+ /**
35
+ * The initial hash value: the first 32 bits of the fractional parts of the
36
+ * square roots of the first 8 primes (FIPS 180-4, section 5.3.3).
37
+ */
38
+ const INITIAL_STATE = [
39
+ 0x6a_09_e6_67, 0xbb_67_ae_85, 0x3c_6e_f3_72, 0xa5_4f_f5_3a, 0x51_0e_52_7f,
40
+ 0x9b_05_68_8c, 0x1f_83_d9_ab, 0x5b_e0_cd_19,
41
+ ];
42
+ const BLOCK_BYTES = 64;
43
+ const LENGTH_BYTES = 8;
44
+ /** Rotates a 32-bit word right. */
45
+ function rotateRight(word, bits) {
46
+ return (word >>> bits) | (word << (32 - bits));
47
+ }
48
+ /** Reads the 32-bit big-endian word at a word index of a view. */
49
+ function wordAt(view, index) {
50
+ return view.getUint32(index * 4);
51
+ }
52
+ /** Writes the 32-bit big-endian word at a word index of a view. */
53
+ function setWord(view, index, word) {
54
+ view.setUint32(index * 4, word);
55
+ }
56
+ /** Expands one block into the 64-word message schedule (FIPS 180-4, 6.2.2 step 1). */
57
+ function expandSchedule(block, schedule) {
58
+ for (let index = 0; index < 16; index += 1) {
59
+ setWord(schedule, index, wordAt(block, index));
60
+ }
61
+ for (let index = 16; index < 64; index += 1) {
62
+ const early = wordAt(schedule, index - 15);
63
+ const late = wordAt(schedule, index - 2);
64
+ const sigma0 = rotateRight(early, 7) ^ rotateRight(early, 18) ^ (early >>> 3);
65
+ const sigma1 = rotateRight(late, 17) ^ rotateRight(late, 19) ^ (late >>> 10);
66
+ setWord(schedule, index, (wordAt(schedule, index - 16) +
67
+ sigma0 +
68
+ wordAt(schedule, index - 7) +
69
+ sigma1) >>>
70
+ 0);
71
+ }
72
+ }
73
+ /** Runs the 64 rounds over the schedule and adds the result to the state (6.2.2 steps 2 to 4). */
74
+ function mixRounds(state, schedule) {
75
+ let a = wordAt(state, 0);
76
+ let b = wordAt(state, 1);
77
+ let c = wordAt(state, 2);
78
+ let d = wordAt(state, 3);
79
+ let e = wordAt(state, 4);
80
+ let f = wordAt(state, 5);
81
+ let g = wordAt(state, 6);
82
+ let h = wordAt(state, 7);
83
+ for (let index = 0; index < 64; index += 1) {
84
+ const sum1 = rotateRight(e, 6) ^ rotateRight(e, 11) ^ rotateRight(e, 25);
85
+ const choice = (e & f) ^ (~e & g);
86
+ const first = (h +
87
+ sum1 +
88
+ choice +
89
+ wordAt(ROUND_CONSTANTS, index) +
90
+ wordAt(schedule, index)) >>>
91
+ 0;
92
+ const sum0 = rotateRight(a, 2) ^ rotateRight(a, 13) ^ rotateRight(a, 22);
93
+ const majority = (a & b) ^ (a & c) ^ (b & c);
94
+ h = g;
95
+ g = f;
96
+ f = e;
97
+ e = (d + first) >>> 0;
98
+ d = c;
99
+ c = b;
100
+ b = a;
101
+ a = (first + sum0 + majority) >>> 0;
102
+ }
103
+ setWord(state, 0, (wordAt(state, 0) + a) >>> 0);
104
+ setWord(state, 1, (wordAt(state, 1) + b) >>> 0);
105
+ setWord(state, 2, (wordAt(state, 2) + c) >>> 0);
106
+ setWord(state, 3, (wordAt(state, 3) + d) >>> 0);
107
+ setWord(state, 4, (wordAt(state, 4) + e) >>> 0);
108
+ setWord(state, 5, (wordAt(state, 5) + f) >>> 0);
109
+ setWord(state, 6, (wordAt(state, 6) + g) >>> 0);
110
+ setWord(state, 7, (wordAt(state, 7) + h) >>> 0);
111
+ }
112
+ /**
113
+ * Incremental SHA-256 (FIPS 180-4) over bytes added in pieces. It keeps one
114
+ * 64-byte block and the eight state words, so hashing a stream of any length
115
+ * needs constant memory, and its digest equals Web Crypto's digest of the
116
+ * same bytes joined.
117
+ */
118
+ export class Sha256 {
119
+ #state = wordView(INITIAL_STATE);
120
+ #block = new Uint8Array(BLOCK_BYTES);
121
+ #blockView = new DataView(this.#block.buffer);
122
+ #schedule = new DataView(new ArrayBuffer(64 * 4));
123
+ #blockLength = 0;
124
+ #totalBytes = 0;
125
+ /** Adds the next bytes of the message. */
126
+ update(bytes) {
127
+ this.#totalBytes += bytes.byteLength;
128
+ let offset = 0;
129
+ while (offset < bytes.byteLength) {
130
+ const taken = Math.min(BLOCK_BYTES - this.#blockLength, bytes.byteLength - offset);
131
+ this.#block.set(bytes.subarray(offset, offset + taken), this.#blockLength);
132
+ this.#blockLength += taken;
133
+ offset += taken;
134
+ if (this.#blockLength === BLOCK_BYTES) {
135
+ this.#compress();
136
+ this.#blockLength = 0;
137
+ }
138
+ }
139
+ }
140
+ /**
141
+ * Pads the message, finishes the hash, and returns its 32 digest bytes. It is
142
+ * read once: the padding becomes part of the state.
143
+ */
144
+ digest() {
145
+ const bitLength = this.#totalBytes * 8;
146
+ this.#block[this.#blockLength] = 0x80;
147
+ this.#blockLength += 1;
148
+ if (this.#blockLength > BLOCK_BYTES - LENGTH_BYTES) {
149
+ this.#block.fill(0, this.#blockLength);
150
+ this.#compress();
151
+ this.#blockLength = 0;
152
+ }
153
+ this.#block.fill(0, this.#blockLength);
154
+ this.#blockView.setUint32(BLOCK_BYTES - 8, Math.floor(bitLength / 2 ** 32));
155
+ this.#blockView.setUint32(BLOCK_BYTES - 4, bitLength >>> 0);
156
+ this.#compress();
157
+ return new Uint8Array(this.#state.buffer.slice(0));
158
+ }
159
+ /** Mixes the full 64-byte block into the state words. */
160
+ #compress() {
161
+ expandSchedule(this.#blockView, this.#schedule);
162
+ mixRounds(this.#state, this.#schedule);
163
+ }
164
+ }
@@ -1,12 +1,14 @@
1
+ import { Effect } from "effect";
1
2
  /**
2
- * Collects the UTF-8 bytes of one digest stream so that Web Crypto can digest
3
- * it in one call. Each piece is encoded on its own, exactly as an incremental
4
- * hash encoded it, so a lone surrogate keeps the bytes it always had.
3
+ * Hashes one digest stream piece by piece. Each piece is encoded to UTF-8 on its
4
+ * own, so a lone surrogate keeps the bytes it always had, and the running
5
+ * SHA-256 keeps memory constant however long the stream grows: a snapshot proof
6
+ * over a hundred megabytes of rows never holds those bytes at once.
5
7
  */
6
8
  export declare class DigestStream {
7
9
  #private;
8
10
  /** Appends one piece of text in the order the stream was built. */
9
11
  add(text: string): void;
10
- /** Digests every piece in order and returns the branded sha256 identity. */
11
- digest(): import("effect/Effect").Effect<string & import("effect/Brand").Brand<"@NakafaAI/AksaraSha256Hash">, never, never>;
12
+ /** Finishes the stream once and returns its branded sha256 identity. */
13
+ digest(): Effect.Effect<string & import("effect/Brand").Brand<"@NakafaAI/AksaraSha256Hash">, never, never>;
12
14
  }
@@ -1,27 +1,22 @@
1
- import { MutableList } from "effect";
2
- import { hashBytes } from "#contracts/hash/digest";
1
+ import { Effect } from "effect";
2
+ import { Hex } from "effect/encoding";
3
+ import { Sha256 } from "#contracts/hash/sha256";
4
+ import { Sha256HashSchema } from "#contracts/ids";
3
5
  /**
4
- * Collects the UTF-8 bytes of one digest stream so that Web Crypto can digest
5
- * it in one call. Each piece is encoded on its own, exactly as an incremental
6
- * hash encoded it, so a lone surrogate keeps the bytes it always had.
6
+ * Hashes one digest stream piece by piece. Each piece is encoded to UTF-8 on its
7
+ * own, so a lone surrogate keeps the bytes it always had, and the running
8
+ * SHA-256 keeps memory constant however long the stream grows: a snapshot proof
9
+ * over a hundred megabytes of rows never holds those bytes at once.
7
10
  */
8
11
  export class DigestStream {
9
- #pieces = MutableList.make();
10
- #length = 0;
12
+ #hash = new Sha256();
13
+ #encoder = new TextEncoder();
11
14
  /** Appends one piece of text in the order the stream was built. */
12
15
  add(text) {
13
- const bytes = new TextEncoder().encode(text);
14
- MutableList.append(this.#pieces, bytes);
15
- this.#length += bytes.byteLength;
16
+ this.#hash.update(this.#encoder.encode(text));
16
17
  }
17
- /** Digests every piece in order and returns the branded sha256 identity. */
18
+ /** Finishes the stream once and returns its branded sha256 identity. */
18
19
  digest() {
19
- const bytes = new Uint8Array(this.#length);
20
- let offset = 0;
21
- for (const piece of MutableList.toArray(this.#pieces)) {
22
- bytes.set(piece, offset);
23
- offset += piece.byteLength;
24
- }
25
- return hashBytes(bytes);
20
+ return Effect.sync(() => Sha256HashSchema.make(`sha256:${Hex.encode(this.#hash.digest())}`));
26
21
  }
27
22
  }
@@ -1,6 +1,7 @@
1
- import { Array as Arr, Match, Option, Result, Schema } from "effect";
1
+ import { Array as Arr, Option, Result, Schema } from "effect";
2
2
  import { readAffine } from "#contracts/math/formula/affine";
3
- import { readFormula, readRelation } from "#contracts/math/formula/parse";
3
+ import { readRelation } from "#contracts/math/formula/parse";
4
+ import { straightPoints } from "#contracts/math/straight";
4
5
  /**
5
6
  * How far from a boundary line a point may lie and still stand on it, relative
6
7
  * to the size of the line's terms there: room for the rounding of decimal
@@ -33,26 +34,6 @@ function onLine({ a, b, c }, { x, y }) {
33
34
  const size = Math.abs(a * x) + Math.abs(b * y) + Math.abs(c);
34
35
  return Math.abs(value) <= ON_LINE_TOLERANCE * Math.max(1, size);
35
36
  }
36
- /** Two points of a graph that is a straight line: an affine formula in its input axis. */
37
- function straightGraph(formula, input) {
38
- return Result.getSuccess(readFormula(formula)).pipe(Option.flatMap((node) => readAffine(node, [input])), Option.map(({ constant, terms }) => {
39
- const slope = terms[input] ?? 0;
40
- /** The graph's point at one value of its input. */
41
- const at = (value) => input === "x"
42
- ? { x: value, y: slope * value + constant }
43
- : { x: slope * value + constant, y: value };
44
- return [at(0), at(1)];
45
- }));
46
- }
47
- /** Two points of a straight object: a line, ray, segment or straight graph. */
48
- function straightPoints(object) {
49
- return Match.value(object).pipe(Match.discriminators("kind")({
50
- function: ({ formula, inputAxis = "x" }) => straightGraph(formula, inputAxis),
51
- line: ({ through }) => Option.some(through),
52
- ray: ({ from, through }) => Option.some([from, through]),
53
- segment: ({ from, to }) => Option.some([from, to]),
54
- }), Match.orElse(() => Option.none()));
55
- }
56
37
  /** Two points of a straight object drawn solid, which a strict boundary may not hold. */
57
38
  function solidStraight(object) {
58
39
  return "stroke" in object && object.stroke !== undefined
@@ -0,0 +1,11 @@
1
+ import type { Schema } from "effect";
2
+ import type { PlaneMathFrame, PlaneMathObject } from "#contracts/math/plane";
3
+ /**
4
+ * Guardrail G2: a polygon cut out by the scene's lines, with an edge along a
5
+ * line or a straight graph, may not close along the frame. The frame only says
6
+ * where the reader looks first and the plane continues past it, so an edge
7
+ * near a side of the frame that lies on no line, ray, segment, graph or axis
8
+ * is invented: it turns an open region into a closed one. Such a part of the
9
+ * plane is a `region` of inequalities, which continues past the frame.
10
+ */
11
+ export declare function inventedEdgeIssues(frame: PlaneMathFrame, objects: readonly PlaneMathObject[]): readonly Schema.FilterIssue[];
@@ -0,0 +1,74 @@
1
+ import { Array as Arr, BigDecimal, Option } from "effect";
2
+ import { decimal } from "#contracts/math/rational";
3
+ import { straightPoints } from "#contracts/math/straight";
4
+ /** The x axis and the y axis, which every plane scene draws. */
5
+ const AXES = [
6
+ [
7
+ { x: 0, y: 0 },
8
+ { x: 1, y: 0 },
9
+ ],
10
+ [
11
+ { x: 0, y: 0 },
12
+ { x: 0, y: 1 },
13
+ ],
14
+ ];
15
+ /**
16
+ * Whether a point stands exactly on a line, by the cross product of decimal
17
+ * coordinates: a corner at an authored approximation such as 3.33 is not on
18
+ * the line it approximates, so the rule never guesses.
19
+ */
20
+ function onCarrier([from, through], point) {
21
+ const cross = BigDecimal.subtract(BigDecimal.multiply(BigDecimal.subtract(decimal(through.x), decimal(from.x)), BigDecimal.subtract(decimal(point.y), decimal(from.y))), BigDecimal.multiply(BigDecimal.subtract(decimal(through.y), decimal(from.y)), BigDecimal.subtract(decimal(point.x), decimal(from.x))));
22
+ return BigDecimal.isZero(cross);
23
+ }
24
+ /** The edges of a polygon, the last one closing it back to its first corner. */
25
+ function edges(vertices) {
26
+ return Arr.zip(vertices, [
27
+ ...Arr.drop(vertices, 1),
28
+ ...Arr.take(vertices, 1),
29
+ ]);
30
+ }
31
+ /**
32
+ * How close to a side of the frame, as a share of the frame's width or
33
+ * height, an edge stands when it closes a shape where the frame happens to
34
+ * end: authors cut an open region at the frame or just inside its padding.
35
+ */
36
+ const SIDE_BAND = 0.1;
37
+ /** Whether an edge runs along a side of the frame, within its band. */
38
+ function alongSide({ x, y }, [from, to]) {
39
+ const width = (x.max - x.min) * SIDE_BAND;
40
+ const height = (y.max - y.min) * SIDE_BAND;
41
+ return ((from.x <= x.min + width && to.x <= x.min + width) ||
42
+ (from.x >= x.max - width && to.x >= x.max - width) ||
43
+ (from.y <= y.min + height && to.y <= y.min + height) ||
44
+ (from.y >= y.max - height && to.y >= y.max - height));
45
+ }
46
+ /**
47
+ * Guardrail G2: a polygon cut out by the scene's lines, with an edge along a
48
+ * line or a straight graph, may not close along the frame. The frame only says
49
+ * where the reader looks first and the plane continues past it, so an edge
50
+ * near a side of the frame that lies on no line, ray, segment, graph or axis
51
+ * is invented: it turns an open region into a closed one. Such a part of the
52
+ * plane is a `region` of inequalities, which continues past the frame.
53
+ */
54
+ export function inventedEdgeIssues(frame, objects) {
55
+ const carriers = [...Arr.getSomes(Arr.map(objects, straightPoints)), ...AXES];
56
+ // The lines that cut the plane without end: lines and straight graphs.
57
+ const cuts = Arr.getSomes(Arr.map(objects, (object) => object.kind === "line" || object.kind === "function"
58
+ ? straightPoints(object)
59
+ : Option.none()));
60
+ /** Whether an edge lies along one of some lines. */
61
+ const along = (lines, [from, to]) => Arr.some(lines, (line) => onCarrier(line, from) && onCarrier(line, to));
62
+ return Arr.flatMap(objects, (object, index) => object.kind === "polygon" &&
63
+ Arr.some(edges(object.vertices), (edge) => along(cuts, edge))
64
+ ? Arr.findFirst(edges(object.vertices), (edge) => alongSide(frame, edge) && !along(carriers, edge)).pipe(Option.match({
65
+ onNone: () => [],
66
+ onSome: ([from, to]) => [
67
+ {
68
+ issue: `Expected the polygon "${object.id}" to close only along lines of the scene; its edge from (${from.x}, ${from.y}) to (${to.x}, ${to.y}) runs along the frame's edge and lies on no line, ray, segment, graph or axis, so it closes an open region with an invented edge. Draw it as { kind: "region", inequalities: [...] }, which continues past the frame, and bound it only where the mathematics does, such as x <= 7 for an area up to 7.`,
69
+ path: ["objects", index],
70
+ },
71
+ ],
72
+ }))
73
+ : []);
74
+ }
@@ -0,0 +1,9 @@
1
+ import { Schema } from "effect";
2
+ import type { PlaneMathObject } from "#contracts/math/plane";
3
+ /**
4
+ * Guardrail G4: a parametric curve that traces a graph y = f(x) is written as
5
+ * a function. A function reads as the graph it is, takes its domain in x, and
6
+ * lets a scene scale each axis on its own, while a parametric curve keeps one
7
+ * scale for both, as a circle needs.
8
+ */
9
+ export declare function graphAsParametricIssues(objects: readonly PlaneMathObject[]): readonly Schema.FilterIssue[];
@@ -0,0 +1,66 @@
1
+ import { Array as Arr, Match, Option, Result, Schema } from "effect";
2
+ import { readAffine } from "#contracts/math/formula/affine";
3
+ import { readFormula } from "#contracts/math/formula/parse";
4
+ import { FormulaNodeSchema } from "#contracts/math/formula/syntax";
5
+ /** Whether two formulas are the same tree. */
6
+ const sameFormula = Schema.toEquivalence(FormulaNodeSchema);
7
+ /** The formulas directly below a node of a formula. */
8
+ function children(node) {
9
+ return Match.value(node).pipe(Match.discriminatorsExhaustive("kind")({
10
+ binary: ({ left, right }) => [left, right],
11
+ call: ({ args }) => args,
12
+ logarithm: ({ base, value }) => [base, value],
13
+ name: () => [],
14
+ negate: ({ operand }) => [operand],
15
+ number: () => [],
16
+ }));
17
+ }
18
+ /** Whether a formula reads t anywhere outside the copies of `inside` it holds. */
19
+ function readsTOutside(node, inside) {
20
+ return (!sameFormula(node, inside) &&
21
+ ((node.kind === "name" && node.name === "t") ||
22
+ Arr.some(children(node), (child) => readsTOutside(child, inside))));
23
+ }
24
+ /** Whether a formula reads t at all. */
25
+ function readsT(node) {
26
+ return ((node.kind === "name" && node.name === "t") ||
27
+ Arr.some(children(node), readsT));
28
+ }
29
+ /**
30
+ * How a parametric curve traces a graph y = f(x), if it does: its x changes at
31
+ * one constant rate in t (`x = t - 6`), or its y reads t only through the
32
+ * formula of its x (`x = exp(t)`, `y = log_2(exp(t))`). Either way each x
33
+ * meets the curve at one height.
34
+ */
35
+ function graphTrace(x, y) {
36
+ return Option.all([
37
+ Result.getSuccess(readFormula(x)),
38
+ Result.getSuccess(readFormula(y)),
39
+ ]).pipe(Option.flatMap(([across, up]) => {
40
+ if (Option.exists(readAffine(across, ["t"]), ({ terms }) => (terms.t ?? 0) !== 0)) {
41
+ return Option.some("whose x changes at one constant rate in t");
42
+ }
43
+ return readsT(across) && !readsTOutside(up, across)
44
+ ? Option.some("whose y reads t only through its x")
45
+ : Option.none();
46
+ }));
47
+ }
48
+ /**
49
+ * Guardrail G4: a parametric curve that traces a graph y = f(x) is written as
50
+ * a function. A function reads as the graph it is, takes its domain in x, and
51
+ * lets a scene scale each axis on its own, while a parametric curve keeps one
52
+ * scale for both, as a circle needs.
53
+ */
54
+ export function graphAsParametricIssues(objects) {
55
+ return Arr.flatMap(objects, (object, index) => object.kind === "parametric"
56
+ ? Option.match(graphTrace(object.x, object.y), {
57
+ onNone: () => [],
58
+ onSome: (trace) => [
59
+ {
60
+ issue: `Expected the parametric curve "${object.id}", ${trace} (x = ${object.x}), to be written as { kind: "function", formula, domain }: it is the graph y = f(x), and a graph lets each axis scale on its own.`,
61
+ path: ["objects", index],
62
+ },
63
+ ],
64
+ })
65
+ : []);
66
+ }
@@ -3,11 +3,14 @@ import { ArcStartDegreesSchema, ArcSweepDegreesSchema, GEOMETRY_TOLERANCE, hasUn
3
3
  import { strictBoundaryIssues } from "#contracts/math/boundary";
4
4
  import { planeBoundsIssues } from "#contracts/math/bounds";
5
5
  import { PlaneCurveObjectSchemas } from "#contracts/math/curve";
6
+ import { inventedEdgeIssues } from "#contracts/math/edge";
6
7
  import { FormulaParameterSchema, FormulaReadoutSchema, PlaneFormulaPointSchema, } from "#contracts/math/formula/field";
7
8
  import { formulaScopeIssues, parameterScope, planeFormulaFields, } from "#contracts/math/formula/scope";
8
9
  import { sliderFormulaIssues } from "#contracts/math/formula/slider";
10
+ import { graphAsParametricIssues } from "#contracts/math/graph";
9
11
  import { MATH_LINE_MESSAGE, MATH_POLYLINE_MESSAGE, MATH_RAY_MESSAGE, MATH_SEGMENT_MESSAGE, MATH_VECTOR_MESSAGE, MathObjectFields, MathStrokeFields, mathLabelAnchorFields, } from "#contracts/math/object";
10
12
  import { planeResolutionIssues } from "#contracts/math/resolution";
13
+ import { figureAspectIssues } from "#contracts/math/shape";
11
14
  /** Checks a plane polygon has non-zero signed area. */
12
15
  function hasPlaneArea(vertices) {
13
16
  const offsets = normalizedPlaneOffsets(vertices);
@@ -176,4 +179,7 @@ export const PlaneMathVisualSchema = Schema.Struct({
176
179
  ...planeBoundsIssues(frame, objects, labels, parameterScope(parameters)),
177
180
  ...planeResolutionIssues(frame, objects, labels, view),
178
181
  ...strictBoundaryIssues(objects),
182
+ ...graphAsParametricIssues(objects),
183
+ ...inventedEdgeIssues(frame, objects),
184
+ ...figureAspectIssues(frame, objects, view),
179
185
  ])));