@nakafa/aksara-contracts 6.0.0 → 6.2.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/assessment/figure.d.ts +567 -6
- package/dist/math/bounds.d.ts +3 -2
- package/dist/math/bounds.js +55 -9
- package/dist/math/curve.d.ts +64 -0
- package/dist/math/curve.js +83 -0
- package/dist/math/formula/box.d.ts +61 -0
- package/dist/math/formula/box.js +68 -0
- package/dist/math/formula/compile.d.ts +11 -0
- package/dist/math/formula/compile.js +100 -0
- package/dist/math/formula/contour.d.ts +17 -0
- package/dist/math/formula/contour.js +162 -0
- package/dist/math/formula/field.d.ts +42 -0
- package/dist/math/formula/field.js +54 -0
- package/dist/math/formula/mesh.d.ts +25 -0
- package/dist/math/formula/mesh.js +28 -0
- package/dist/math/formula/names.d.ts +4 -0
- package/dist/math/formula/names.js +23 -0
- package/dist/math/formula/parse.d.ts +10 -0
- package/dist/math/formula/parse.js +240 -0
- package/dist/math/formula/plane.d.ts +75 -0
- package/dist/math/formula/plane.js +121 -0
- package/dist/math/formula/reader.d.ts +102 -0
- package/dist/math/formula/reader.js +58 -0
- package/dist/math/formula/region.d.ts +16 -0
- package/dist/math/formula/region.js +94 -0
- package/dist/math/formula/sample.d.ts +14 -0
- package/dist/math/formula/sample.js +182 -0
- package/dist/math/formula/scope.d.ts +41 -0
- package/dist/math/formula/scope.js +151 -0
- package/dist/math/formula/slider.d.ts +9 -0
- package/dist/math/formula/slider.js +32 -0
- package/dist/math/formula/space.d.ts +10 -0
- package/dist/math/formula/space.js +94 -0
- package/dist/math/formula/syntax.d.ts +77 -0
- package/dist/math/formula/syntax.js +111 -0
- package/dist/math/formula/token.d.ts +43 -0
- package/dist/math/formula/token.js +150 -0
- package/dist/math/plane.d.ts +148 -5
- package/dist/math/plane.js +16 -4
- package/dist/math/scene.js +17 -4
- package/dist/math/solid.d.ts +124 -0
- package/dist/math/solid.js +86 -0
- package/dist/math/space.d.ts +214 -0
- package/dist/math/space.js +8 -2
- package/dist/math/visual.d.ts +545 -3
- package/dist/math/visual.js +12 -2
- package/package.json +57 -1
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { Array as Arr, MutableHashMap, MutableHashSet, MutableList, Option, Result, Schema, } from "effect";
|
|
2
|
+
import { SamplePointSchema, } from "#contracts/math/formula/box";
|
|
3
|
+
/** Cells per frame side, enough for a smooth outline at the largest card size. */
|
|
4
|
+
export const CONTOUR_CELLS = 240;
|
|
5
|
+
/** One grid corner: its key, its point, and the field's value there when defined. */
|
|
6
|
+
const CornerSchema = Schema.Struct({
|
|
7
|
+
key: Schema.String,
|
|
8
|
+
point: SamplePointSchema,
|
|
9
|
+
value: Schema.Option(Schema.Finite),
|
|
10
|
+
});
|
|
11
|
+
/** One grid corner whose value is defined. */
|
|
12
|
+
const LocatedSchema = Schema.Struct({
|
|
13
|
+
key: Schema.String,
|
|
14
|
+
point: SamplePointSchema,
|
|
15
|
+
value: Schema.Finite,
|
|
16
|
+
});
|
|
17
|
+
/** One crossing on a cell edge: the shared key of that edge and its point. */
|
|
18
|
+
const CrossingSchema = Schema.Struct({
|
|
19
|
+
key: Schema.String,
|
|
20
|
+
point: SamplePointSchema,
|
|
21
|
+
});
|
|
22
|
+
/** One segment of an outline, with the order it was found in. */
|
|
23
|
+
const SegmentSchema = Schema.Struct({
|
|
24
|
+
from: CrossingSchema,
|
|
25
|
+
index: Schema.Int,
|
|
26
|
+
to: CrossingSchema,
|
|
27
|
+
});
|
|
28
|
+
/** Reads the field once at every corner of a square grid over the frame. */
|
|
29
|
+
function grid(field, box, cells) {
|
|
30
|
+
const dx = (box.x.max - box.x.min) / cells;
|
|
31
|
+
const dy = (box.y.max - box.y.min) / cells;
|
|
32
|
+
const columns = Arr.makeBy(cells + 1, (column) => Arr.makeBy(cells + 1, (row) => {
|
|
33
|
+
const x = box.x.min + column * dx;
|
|
34
|
+
const y = box.y.min + row * dy;
|
|
35
|
+
return {
|
|
36
|
+
key: `${column}:${row}`,
|
|
37
|
+
point: { x, y, z: 0 },
|
|
38
|
+
value: Option.liftPredicate(field(x, y), Number.isFinite),
|
|
39
|
+
};
|
|
40
|
+
}));
|
|
41
|
+
return Arr.flatMap(Arr.makeBy(cells, (column) => column), (column) => Arr.flatMap(Arr.makeBy(cells, (row) => row), (row) => Option.toArray(Option.all([
|
|
42
|
+
Arr.get(columns, column).pipe(Option.flatMap((each) => Arr.get(each, row))),
|
|
43
|
+
Arr.get(columns, column + 1).pipe(Option.flatMap((each) => Arr.get(each, row))),
|
|
44
|
+
Arr.get(columns, column + 1).pipe(Option.flatMap((each) => Arr.get(each, row + 1))),
|
|
45
|
+
Arr.get(columns, column).pipe(Option.flatMap((each) => Arr.get(each, row + 1))),
|
|
46
|
+
]))));
|
|
47
|
+
}
|
|
48
|
+
/** One corner with its defined value, or nothing where the field is undefined. */
|
|
49
|
+
function locate(corner) {
|
|
50
|
+
return Option.map(corner.value, (value) => ({ key: corner.key, point: corner.point, value }));
|
|
51
|
+
}
|
|
52
|
+
/** The corners of a cell with their values, or nothing when any is undefined. */
|
|
53
|
+
function square([a, b, c, d]) {
|
|
54
|
+
return Option.all([locate(a), locate(b), locate(c), locate(d)]);
|
|
55
|
+
}
|
|
56
|
+
/** Whether a corner lies where the field is at least zero. */
|
|
57
|
+
function inside(corner) {
|
|
58
|
+
return corner.value >= 0;
|
|
59
|
+
}
|
|
60
|
+
/** Where the field crosses zero between two corners, if it does. */
|
|
61
|
+
function crossing(from, to) {
|
|
62
|
+
if (inside(from) === inside(to)) {
|
|
63
|
+
return Option.none();
|
|
64
|
+
}
|
|
65
|
+
const fraction = from.value / (from.value - to.value);
|
|
66
|
+
return Option.some({
|
|
67
|
+
key: from.key < to.key ? `${from.key}|${to.key}` : `${to.key}|${from.key}`,
|
|
68
|
+
point: {
|
|
69
|
+
x: from.point.x + (to.point.x - from.point.x) * fraction,
|
|
70
|
+
y: from.point.y + (to.point.y - from.point.y) * fraction,
|
|
71
|
+
z: 0,
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The zero-crossing segments inside one cell. A saddle, where opposite
|
|
77
|
+
* corners agree, is resolved by the field's value at the cell center.
|
|
78
|
+
*/
|
|
79
|
+
function cellSegments(field, cell) {
|
|
80
|
+
const [a, b, c, d] = cell;
|
|
81
|
+
const bottom = crossing(a, b);
|
|
82
|
+
const right = crossing(b, c);
|
|
83
|
+
const top = crossing(c, d);
|
|
84
|
+
const left = crossing(d, a);
|
|
85
|
+
return Option.match(Option.all([bottom, right, top, left]), {
|
|
86
|
+
onNone: () => Arr.getSomes(Arr.map([
|
|
87
|
+
[bottom, right],
|
|
88
|
+
[right, top],
|
|
89
|
+
[top, left],
|
|
90
|
+
[left, bottom],
|
|
91
|
+
[bottom, top],
|
|
92
|
+
[left, right],
|
|
93
|
+
], ([from, to]) => Option.zipWith(from, to, (start, end) => [start, end]))),
|
|
94
|
+
onSome: ([down, east, up, west]) => {
|
|
95
|
+
const center = field((a.point.x + c.point.x) / 2, (a.point.y + c.point.y) / 2);
|
|
96
|
+
return center >= 0 === inside(a)
|
|
97
|
+
? [[down, east], [up, west]]
|
|
98
|
+
: [[west, down], [east, up]];
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
/** Joins segments that share an edge crossing into continuous polylines. */
|
|
103
|
+
function join(segments) {
|
|
104
|
+
const byKey = MutableHashMap.empty();
|
|
105
|
+
Arr.forEach(segments, (segment) => {
|
|
106
|
+
for (const key of [segment.from.key, segment.to.key]) {
|
|
107
|
+
const known = Option.getOrElse(MutableHashMap.get(byKey, key), () => []);
|
|
108
|
+
MutableHashMap.set(byKey, key, [...known, segment]);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
const used = MutableHashSet.empty();
|
|
112
|
+
return Arr.filterMap(segments, (start) => {
|
|
113
|
+
if (MutableHashSet.has(used, start.index)) {
|
|
114
|
+
return Result.failVoid;
|
|
115
|
+
}
|
|
116
|
+
MutableHashSet.add(used, start.index);
|
|
117
|
+
const line = MutableList.make();
|
|
118
|
+
MutableList.append(line, start.from.point);
|
|
119
|
+
MutableList.append(line, start.to.point);
|
|
120
|
+
let { key } = start.to;
|
|
121
|
+
let next = Option.none();
|
|
122
|
+
do {
|
|
123
|
+
next = Arr.findFirst(Arr.flatten(Option.toArray(MutableHashMap.get(byKey, key))), (segment) => !MutableHashSet.has(used, segment.index));
|
|
124
|
+
if (Option.isSome(next)) {
|
|
125
|
+
const segment = next.value;
|
|
126
|
+
MutableHashSet.add(used, segment.index);
|
|
127
|
+
const forward = segment.from.key === key;
|
|
128
|
+
MutableList.append(line, forward ? segment.to.point : segment.from.point);
|
|
129
|
+
key = forward ? segment.to.key : segment.from.key;
|
|
130
|
+
}
|
|
131
|
+
} while (Option.isSome(next));
|
|
132
|
+
return Result.succeed(MutableList.toArray(line));
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Traces every curve where a field is zero inside the frame, the marching
|
|
137
|
+
* squares method, so an equation such as `x^2 + y^2 = 4` draws its circle.
|
|
138
|
+
*/
|
|
139
|
+
export function contourLines(field, box, cells = CONTOUR_CELLS) {
|
|
140
|
+
const squares = Arr.getSomes(Arr.map(grid(field, box, cells), square));
|
|
141
|
+
const found = Arr.flatMap(squares, (cell) => cellSegments(field, cell));
|
|
142
|
+
return join(Arr.map(found, ([from, to], index) => ({ from, index, to })));
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Shades the part of the frame where a field is at least zero, one shape per
|
|
146
|
+
* cell: the corners inside and the crossings between them, so a cell wholly
|
|
147
|
+
* inside is its square and a cell the outline cuts keeps only its inner part.
|
|
148
|
+
*/
|
|
149
|
+
export function contourFill(field, box, cells = CONTOUR_CELLS) {
|
|
150
|
+
return Arr.filterMap(Arr.getSomes(Arr.map(grid(field, box, cells), square)), ([a, b, c, d]) => {
|
|
151
|
+
const outline = Arr.flatMap([
|
|
152
|
+
[a, b],
|
|
153
|
+
[b, c],
|
|
154
|
+
[c, d],
|
|
155
|
+
[d, a],
|
|
156
|
+
], ([from, to]) => [
|
|
157
|
+
...(inside(from) ? [from.point] : []),
|
|
158
|
+
...Option.toArray(Option.map(crossing(from, to), ({ point }) => point)),
|
|
159
|
+
]);
|
|
160
|
+
return outline.length >= 3 ? Result.succeed(outline) : Result.failVoid;
|
|
161
|
+
});
|
|
162
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
/** An authored expression, such as `2x^2 - 3x + 1` or `a sin(t)`. */
|
|
3
|
+
export declare const FormulaTextSchema: Schema.String;
|
|
4
|
+
/** An authored equation, such as `x^2 + y^2 = 4`. */
|
|
5
|
+
export declare const EquationTextSchema: Schema.String;
|
|
6
|
+
/** An authored inequality, such as `y < 2x + 1` or `x + y <= 4`. */
|
|
7
|
+
export declare const InequalityTextSchema: Schema.String;
|
|
8
|
+
/**
|
|
9
|
+
* One slider a learner moves to change every formula that reads its name. Its
|
|
10
|
+
* value starts inside its range, and a positive step, when given, snaps it.
|
|
11
|
+
* A `label` names the rich label the slider shows instead of its letter, such
|
|
12
|
+
* as theta for an angle slider named `a`.
|
|
13
|
+
*/
|
|
14
|
+
export declare const FormulaParameterSchema: Schema.Struct<{
|
|
15
|
+
readonly label: Schema.optionalKey<Schema.String>;
|
|
16
|
+
readonly max: Schema.Finite;
|
|
17
|
+
readonly min: Schema.Finite;
|
|
18
|
+
readonly name: Schema.String;
|
|
19
|
+
readonly step: Schema.optionalKey<Schema.Finite>;
|
|
20
|
+
readonly value: Schema.Finite;
|
|
21
|
+
}>;
|
|
22
|
+
export type FormulaParameter = typeof FormulaParameterSchema.Type;
|
|
23
|
+
/** A plane point placed by formulas of the sliders, such as (cos(a), sin(a)). */
|
|
24
|
+
export declare const PlaneFormulaPointSchema: Schema.Struct<{
|
|
25
|
+
readonly x: Schema.String;
|
|
26
|
+
readonly y: Schema.String;
|
|
27
|
+
}>;
|
|
28
|
+
export type PlaneFormulaPoint = typeof PlaneFormulaPointSchema.Type;
|
|
29
|
+
/** Whether an anchor is placed by formulas rather than written exactly. */
|
|
30
|
+
export declare const isPlaneFormulaPoint: <I>(input: I) => input is I & {
|
|
31
|
+
readonly x: string;
|
|
32
|
+
readonly y: string;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* One value a scene shows under its picture, such as cos(a), next to the rich
|
|
36
|
+
* label its key names. It reads only the sliders and follows them.
|
|
37
|
+
*/
|
|
38
|
+
export declare const FormulaReadoutSchema: Schema.Struct<{
|
|
39
|
+
readonly formula: Schema.String;
|
|
40
|
+
readonly key: Schema.String;
|
|
41
|
+
}>;
|
|
42
|
+
export type FormulaReadout = typeof FormulaReadoutSchema.Type;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Array as Arr, Result, Schema } from "effect";
|
|
2
|
+
import { MathVisualKeySchema } from "#contracts/math/base";
|
|
3
|
+
import { readFormula, readRelation } from "#contracts/math/formula/parse";
|
|
4
|
+
import { FormulaParameterNameSchema, } from "#contracts/math/formula/syntax";
|
|
5
|
+
/** Passes a formula that reads, or names the problem of one that does not. */
|
|
6
|
+
function readable(read) {
|
|
7
|
+
return (text) => Result.match(read(text), {
|
|
8
|
+
onFailure: (problem) => problem.message,
|
|
9
|
+
onSuccess: () => true,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
/** Passes a relation that reads with one of the allowed comparisons. */
|
|
13
|
+
function comparedWith(allowed, message) {
|
|
14
|
+
return (text) => Result.match(readRelation(text), {
|
|
15
|
+
onFailure: (problem) => problem.message,
|
|
16
|
+
onSuccess: ({ comparison }) => Arr.contains(allowed, comparison) || message,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
/** An authored expression, such as `2x^2 - 3x + 1` or `a sin(t)`. */
|
|
20
|
+
export const FormulaTextSchema = Schema.String.check(Schema.makeFilter(readable(readFormula)));
|
|
21
|
+
/** An authored equation, such as `x^2 + y^2 = 4`. */
|
|
22
|
+
export const EquationTextSchema = Schema.String.check(Schema.makeFilter(comparedWith(["="], "Expected an equation with =.")));
|
|
23
|
+
/** An authored inequality, such as `y < 2x + 1` or `x + y <= 4`. */
|
|
24
|
+
export const InequalityTextSchema = Schema.String.check(Schema.makeFilter(comparedWith(["<", "<=", ">", ">="], "Expected <, <=, >, or >=.")));
|
|
25
|
+
/**
|
|
26
|
+
* One slider a learner moves to change every formula that reads its name. Its
|
|
27
|
+
* value starts inside its range, and a positive step, when given, snaps it.
|
|
28
|
+
* A `label` names the rich label the slider shows instead of its letter, such
|
|
29
|
+
* as theta for an angle slider named `a`.
|
|
30
|
+
*/
|
|
31
|
+
export const FormulaParameterSchema = Schema.Struct({
|
|
32
|
+
label: Schema.optionalKey(MathVisualKeySchema),
|
|
33
|
+
max: Schema.Finite,
|
|
34
|
+
min: Schema.Finite,
|
|
35
|
+
name: FormulaParameterNameSchema,
|
|
36
|
+
step: Schema.optionalKey(Schema.Finite.check(Schema.isGreaterThan(0))),
|
|
37
|
+
value: Schema.Finite,
|
|
38
|
+
}).check(Schema.makeFilter(({ max, min, value }) => (min < max && min <= value && value <= max) ||
|
|
39
|
+
"Expected min < max and a value between them."));
|
|
40
|
+
/** A plane point placed by formulas of the sliders, such as (cos(a), sin(a)). */
|
|
41
|
+
export const PlaneFormulaPointSchema = Schema.Struct({
|
|
42
|
+
x: FormulaTextSchema,
|
|
43
|
+
y: FormulaTextSchema,
|
|
44
|
+
});
|
|
45
|
+
/** Whether an anchor is placed by formulas rather than written exactly. */
|
|
46
|
+
export const isPlaneFormulaPoint = Schema.is(PlaneFormulaPointSchema);
|
|
47
|
+
/**
|
|
48
|
+
* One value a scene shows under its picture, such as cos(a), next to the rich
|
|
49
|
+
* label its key names. It reads only the sliders and follows them.
|
|
50
|
+
*/
|
|
51
|
+
export const FormulaReadoutSchema = Schema.Struct({
|
|
52
|
+
formula: FormulaTextSchema,
|
|
53
|
+
key: MathVisualKeySchema,
|
|
54
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { type SamplePoint, type SampleRange } from "#contracts/math/formula/box";
|
|
3
|
+
/** Cells along each side of a sampled surface. */
|
|
4
|
+
export declare const MESH_CELLS = 96;
|
|
5
|
+
/**
|
|
6
|
+
* A surface sampled on a grid: (rows + 1) by (columns + 1) points, row by row,
|
|
7
|
+
* with nothing where the surface is undefined so a renderer leaves a hole.
|
|
8
|
+
*/
|
|
9
|
+
export declare const SampleGridSchema: Schema.Struct<{
|
|
10
|
+
readonly columns: Schema.Int;
|
|
11
|
+
readonly points: Schema.$Array<Schema.Option<Schema.Struct<{
|
|
12
|
+
readonly x: Schema.Finite;
|
|
13
|
+
readonly y: Schema.Finite;
|
|
14
|
+
readonly z: Schema.Finite;
|
|
15
|
+
}>>>;
|
|
16
|
+
readonly rows: Schema.Int;
|
|
17
|
+
}>;
|
|
18
|
+
export type SampleGrid = typeof SampleGridSchema.Type;
|
|
19
|
+
/** Where a surface stands for one pair of parameter values. */
|
|
20
|
+
export type SampleSurface = (u: number, v: number) => SamplePoint;
|
|
21
|
+
/** Samples a surface over two parameter ranges on an even grid. */
|
|
22
|
+
export declare function sampleGrid(surface: SampleSurface, u: SampleRange, v: SampleRange, cells?: {
|
|
23
|
+
readonly columns: number;
|
|
24
|
+
readonly rows: number;
|
|
25
|
+
}): SampleGrid;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Array as Arr, Option, Schema } from "effect";
|
|
2
|
+
import { SamplePointSchema, } from "#contracts/math/formula/box";
|
|
3
|
+
/** Cells along each side of a sampled surface. */
|
|
4
|
+
export const MESH_CELLS = 96;
|
|
5
|
+
/**
|
|
6
|
+
* A surface sampled on a grid: (rows + 1) by (columns + 1) points, row by row,
|
|
7
|
+
* with nothing where the surface is undefined so a renderer leaves a hole.
|
|
8
|
+
*/
|
|
9
|
+
export const SampleGridSchema = Schema.Struct({
|
|
10
|
+
columns: Schema.Int,
|
|
11
|
+
points: Schema.Array(Schema.Option(SamplePointSchema)),
|
|
12
|
+
rows: Schema.Int,
|
|
13
|
+
});
|
|
14
|
+
/** Samples a surface over two parameter ranges on an even grid. */
|
|
15
|
+
export function sampleGrid(surface, u, v, cells = {
|
|
16
|
+
columns: MESH_CELLS,
|
|
17
|
+
rows: MESH_CELLS,
|
|
18
|
+
}) {
|
|
19
|
+
const points = Arr.flatMap(Arr.makeBy(cells.rows + 1, (row) => row), (row) => Arr.makeBy(cells.columns + 1, (column) => {
|
|
20
|
+
const point = surface(u.min + ((u.max - u.min) * column) / cells.columns, v.min + ((v.max - v.min) * row) / cells.rows);
|
|
21
|
+
return Number.isFinite(point.x) &&
|
|
22
|
+
Number.isFinite(point.y) &&
|
|
23
|
+
Number.isFinite(point.z)
|
|
24
|
+
? Option.some(point)
|
|
25
|
+
: Option.none();
|
|
26
|
+
}));
|
|
27
|
+
return { columns: cells.columns, points, rows: cells.rows };
|
|
28
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Array as Arr, HashSet } from "effect";
|
|
2
|
+
import { FORMULA_CONSTANTS, } from "#contracts/math/formula/syntax";
|
|
3
|
+
/** Every variable and parameter a node reads; constants are left out. */
|
|
4
|
+
export function formulaNames(node) {
|
|
5
|
+
if (node.kind === "number") {
|
|
6
|
+
return HashSet.empty();
|
|
7
|
+
}
|
|
8
|
+
if (node.kind === "name") {
|
|
9
|
+
return node.name in FORMULA_CONSTANTS
|
|
10
|
+
? HashSet.empty()
|
|
11
|
+
: HashSet.make(node.name);
|
|
12
|
+
}
|
|
13
|
+
if (node.kind === "negate") {
|
|
14
|
+
return formulaNames(node.operand);
|
|
15
|
+
}
|
|
16
|
+
if (node.kind === "binary") {
|
|
17
|
+
return HashSet.union(formulaNames(node.left), formulaNames(node.right));
|
|
18
|
+
}
|
|
19
|
+
if (node.kind === "logarithm") {
|
|
20
|
+
return HashSet.union(formulaNames(node.base), formulaNames(node.value));
|
|
21
|
+
}
|
|
22
|
+
return Arr.reduce(node.args, HashSet.empty(), (names, each) => HashSet.union(names, formulaNames(each)));
|
|
23
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Result } from "effect";
|
|
2
|
+
import { type FormulaError, type FormulaNode } from "#contracts/math/formula/syntax";
|
|
3
|
+
/** Reads a formula that is one expression, such as `2x^2 - 3x + 1`. */
|
|
4
|
+
export declare function readFormula(text: string): Result.Result<FormulaNode, FormulaError>;
|
|
5
|
+
/** Reads one equation or inequality, such as `x^2 + y^2 = 4` or `y < 2x + 1`. */
|
|
6
|
+
export declare function readRelation(text: string): Result.Result<{
|
|
7
|
+
comparison: "<" | "<=" | "=" | ">" | ">=";
|
|
8
|
+
left: FormulaNode;
|
|
9
|
+
right: FormulaNode;
|
|
10
|
+
}, FormulaError>;
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { Array as Arr, MutableList, Option, Result, Schema } from "effect";
|
|
2
|
+
import { at, expect, finished, here, peek, problemAt, startReader, } from "#contracts/math/formula/reader";
|
|
3
|
+
import { FormulaFunctionSchema, } from "#contracts/math/formula/syntax";
|
|
4
|
+
/** How tightly each operator holds its operands; implicit products bind like `*`. */
|
|
5
|
+
const SUM = 10;
|
|
6
|
+
const PRODUCT = 20;
|
|
7
|
+
const SIGN = 25;
|
|
8
|
+
const POWER = 30;
|
|
9
|
+
const DEGREE = 40;
|
|
10
|
+
const isFunction = Schema.is(FormulaFunctionSchema);
|
|
11
|
+
/** Whether the current token can begin a factor of an implicit product. */
|
|
12
|
+
function startsFactor(reader) {
|
|
13
|
+
return Option.exists(peek(reader), (token) => {
|
|
14
|
+
if (token.kind === "number" || token.kind === "name") {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
return (token.kind === "symbol" &&
|
|
18
|
+
(token.symbol === "(" ||
|
|
19
|
+
token.symbol === "√" ||
|
|
20
|
+
(token.symbol === "|" && reader.bars === 0)));
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/** Reads the comma-separated arguments of one call, checking their count. */
|
|
24
|
+
function readArguments(reader, callee, start) {
|
|
25
|
+
return Result.gen(function* () {
|
|
26
|
+
if (!at(reader, "(")) {
|
|
27
|
+
return yield* problemAt("function-arguments", start, `Expected "(" after ${callee}.`);
|
|
28
|
+
}
|
|
29
|
+
reader.index += 1;
|
|
30
|
+
const outerBars = reader.bars;
|
|
31
|
+
reader.bars = 0;
|
|
32
|
+
const first = yield* readExpression(reader, 0);
|
|
33
|
+
const rest = MutableList.make();
|
|
34
|
+
while (at(reader, ",")) {
|
|
35
|
+
reader.index += 1;
|
|
36
|
+
MutableList.append(rest, yield* readExpression(reader, 0));
|
|
37
|
+
}
|
|
38
|
+
yield* expect(reader, ")");
|
|
39
|
+
reader.bars = outerBars;
|
|
40
|
+
const args = [
|
|
41
|
+
first,
|
|
42
|
+
...MutableList.toArray(rest),
|
|
43
|
+
];
|
|
44
|
+
const many = callee === "max" || callee === "min";
|
|
45
|
+
if (many ? args.length < 2 : args.length !== 1) {
|
|
46
|
+
return yield* problemAt("function-arguments", start, many
|
|
47
|
+
? `${callee} takes two or more arguments.`
|
|
48
|
+
: `${callee} takes one argument.`);
|
|
49
|
+
}
|
|
50
|
+
return args;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/** Reads a function call, with `sin^2(x)` powers and `log_b(x)` bases. */
|
|
54
|
+
function readCall(reader, callee, start) {
|
|
55
|
+
return Result.gen(function* () {
|
|
56
|
+
const base = callee === "log" && at(reader, "_")
|
|
57
|
+
? Option.some(yield* readSubscript(reader))
|
|
58
|
+
: Option.none();
|
|
59
|
+
const power = at(reader, "^")
|
|
60
|
+
? Option.some(yield* readExponent(reader))
|
|
61
|
+
: Option.none();
|
|
62
|
+
const args = yield* readArguments(reader, callee, start);
|
|
63
|
+
const [value] = args;
|
|
64
|
+
const call = Option.match(base, {
|
|
65
|
+
onNone: () => ({ args, callee, kind: "call" }),
|
|
66
|
+
onSome: (each) => ({ base: each, kind: "logarithm", value }),
|
|
67
|
+
});
|
|
68
|
+
return Option.match(power, {
|
|
69
|
+
onNone: () => call,
|
|
70
|
+
onSome: (right) => ({
|
|
71
|
+
kind: "binary",
|
|
72
|
+
left: call,
|
|
73
|
+
operator: "^",
|
|
74
|
+
right,
|
|
75
|
+
}),
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/** Reads the `_b` base of a logarithm: one number, name, or group. */
|
|
80
|
+
function readSubscript(reader) {
|
|
81
|
+
reader.index += 1;
|
|
82
|
+
return readPrefix(reader);
|
|
83
|
+
}
|
|
84
|
+
/** Reads the `^n` power written between a function's name and its arguments. */
|
|
85
|
+
function readExponent(reader) {
|
|
86
|
+
reader.index += 1;
|
|
87
|
+
return readPrefix(reader);
|
|
88
|
+
}
|
|
89
|
+
/** Reads what one symbol opens: a group, absolute bars, a root, or a sign. */
|
|
90
|
+
function readSymbol(reader, symbol, position) {
|
|
91
|
+
return Result.gen(function* () {
|
|
92
|
+
if (symbol === "(") {
|
|
93
|
+
const outerBars = reader.bars;
|
|
94
|
+
reader.bars = 0;
|
|
95
|
+
const inner = yield* readExpression(reader, 0);
|
|
96
|
+
yield* expect(reader, ")");
|
|
97
|
+
reader.bars = outerBars;
|
|
98
|
+
return inner;
|
|
99
|
+
}
|
|
100
|
+
if (symbol === "|") {
|
|
101
|
+
reader.bars += 1;
|
|
102
|
+
const inner = yield* readExpression(reader, 0);
|
|
103
|
+
yield* expect(reader, "|");
|
|
104
|
+
reader.bars -= 1;
|
|
105
|
+
return {
|
|
106
|
+
args: [inner],
|
|
107
|
+
callee: "abs",
|
|
108
|
+
kind: "call",
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
if (symbol === "√") {
|
|
112
|
+
const inner = yield* readExpression(reader, POWER);
|
|
113
|
+
return {
|
|
114
|
+
args: [inner],
|
|
115
|
+
callee: "sqrt",
|
|
116
|
+
kind: "call",
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
if (symbol === "-" || symbol === "+") {
|
|
120
|
+
const operand = yield* readExpression(reader, SIGN);
|
|
121
|
+
return symbol === "-"
|
|
122
|
+
? { kind: "negate", operand }
|
|
123
|
+
: operand;
|
|
124
|
+
}
|
|
125
|
+
return yield* problemAt("value-expected", position, `Expected a value, not "${symbol}".`);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/** Reads one factor: a number, a name, a call, or what a symbol opens. */
|
|
129
|
+
function readPrefix(reader) {
|
|
130
|
+
return Result.gen(function* () {
|
|
131
|
+
const token = peek(reader);
|
|
132
|
+
if (Option.isNone(token)) {
|
|
133
|
+
return yield* problemAt("value-expected", reader.length, "Expected a value.");
|
|
134
|
+
}
|
|
135
|
+
const current = token.value;
|
|
136
|
+
reader.index += 1;
|
|
137
|
+
if (current.kind === "number") {
|
|
138
|
+
return { kind: "number", value: current.value };
|
|
139
|
+
}
|
|
140
|
+
if (current.kind === "name") {
|
|
141
|
+
return isFunction(current.name)
|
|
142
|
+
? yield* readCall(reader, current.name, current.position)
|
|
143
|
+
: { kind: "name", name: current.name };
|
|
144
|
+
}
|
|
145
|
+
if (current.kind === "comparison") {
|
|
146
|
+
return yield* problemAt("comparison-unexpected", current.position, "Expected a value before the comparison.");
|
|
147
|
+
}
|
|
148
|
+
return yield* readSymbol(reader, current.symbol, current.position);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
/** How tightly each written operator binds, and how its right side reads. */
|
|
152
|
+
const INFIX = [
|
|
153
|
+
{ operator: "+", power: SUM, right: SUM + 1 },
|
|
154
|
+
{ operator: "-", power: SUM, right: SUM + 1 },
|
|
155
|
+
{ operator: "*", power: PRODUCT, right: PRODUCT + 1 },
|
|
156
|
+
{ operator: "/", power: PRODUCT, right: PRODUCT + 1 },
|
|
157
|
+
{ operator: "^", power: POWER, right: POWER - 1 },
|
|
158
|
+
];
|
|
159
|
+
/** The written operator ahead and its binding, if one stands there. */
|
|
160
|
+
function infixAhead(reader) {
|
|
161
|
+
return Arr.findFirst(INFIX, ({ operator }) => at(reader, operator));
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Extends what has been read with every operator ahead that binds tighter
|
|
165
|
+
* than `minimum`: degrees, written operators, and implicit products.
|
|
166
|
+
*/
|
|
167
|
+
function extend(reader, minimum, left) {
|
|
168
|
+
return Result.gen(function* () {
|
|
169
|
+
if (at(reader, "°") && DEGREE > minimum) {
|
|
170
|
+
reader.index += 1;
|
|
171
|
+
const right = { kind: "number", value: Math.PI / 180 };
|
|
172
|
+
return yield* extend(reader, minimum, {
|
|
173
|
+
kind: "binary",
|
|
174
|
+
left,
|
|
175
|
+
operator: "*",
|
|
176
|
+
right,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
const infix = infixAhead(reader);
|
|
180
|
+
if (Option.isSome(infix)) {
|
|
181
|
+
if (infix.value.power <= minimum) {
|
|
182
|
+
return left;
|
|
183
|
+
}
|
|
184
|
+
reader.index += 1;
|
|
185
|
+
const right = yield* readExpression(reader, infix.value.right);
|
|
186
|
+
const { operator } = infix.value;
|
|
187
|
+
return yield* extend(reader, minimum, {
|
|
188
|
+
kind: "binary",
|
|
189
|
+
left,
|
|
190
|
+
operator,
|
|
191
|
+
right,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
if (startsFactor(reader) && PRODUCT > minimum) {
|
|
195
|
+
const right = yield* readExpression(reader, PRODUCT + 1);
|
|
196
|
+
return yield* extend(reader, minimum, {
|
|
197
|
+
kind: "binary",
|
|
198
|
+
left,
|
|
199
|
+
operator: "*",
|
|
200
|
+
right,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
return left;
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
/** Reads operators that bind tighter than `minimum`, the core of a Pratt parser. */
|
|
207
|
+
function readExpression(reader, minimum) {
|
|
208
|
+
return Result.flatMap(readPrefix(reader), (left) => extend(reader, minimum, left));
|
|
209
|
+
}
|
|
210
|
+
/** Reads a formula that is one expression, such as `2x^2 - 3x + 1`. */
|
|
211
|
+
export function readFormula(text) {
|
|
212
|
+
return Result.gen(function* () {
|
|
213
|
+
const reader = yield* startReader(text);
|
|
214
|
+
if (reader.tokens.length === 0) {
|
|
215
|
+
return yield* problemAt("empty", 0, "Expected a formula.");
|
|
216
|
+
}
|
|
217
|
+
const node = yield* readExpression(reader, 0);
|
|
218
|
+
yield* finished(reader);
|
|
219
|
+
return node;
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
/** Reads one equation or inequality, such as `x^2 + y^2 = 4` or `y < 2x + 1`. */
|
|
223
|
+
export function readRelation(text) {
|
|
224
|
+
return Result.gen(function* () {
|
|
225
|
+
const reader = yield* startReader(text);
|
|
226
|
+
if (reader.tokens.length === 0) {
|
|
227
|
+
return yield* problemAt("empty", 0, "Expected a formula.");
|
|
228
|
+
}
|
|
229
|
+
const left = yield* readExpression(reader, 0);
|
|
230
|
+
const token = peek(reader);
|
|
231
|
+
if (Option.isNone(token) || token.value.kind !== "comparison") {
|
|
232
|
+
return yield* problemAt("comparison-expected", here(reader), "Expected =, <, <=, >, or >=.");
|
|
233
|
+
}
|
|
234
|
+
const comparison = token.value.comparison;
|
|
235
|
+
reader.index += 1;
|
|
236
|
+
const right = yield* readExpression(reader, 0);
|
|
237
|
+
yield* finished(reader);
|
|
238
|
+
return { comparison, left, right };
|
|
239
|
+
});
|
|
240
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Option, Result, Schema } from "effect";
|
|
2
|
+
import type { PlaneCurveObjectSchemas } from "#contracts/math/curve";
|
|
3
|
+
import { type SampleBox, type SamplePoint } from "#contracts/math/formula/box";
|
|
4
|
+
import { type FormulaEvaluator, type FormulaScope } from "#contracts/math/formula/compile";
|
|
5
|
+
import type { PlaneFormulaPoint } from "#contracts/math/formula/field";
|
|
6
|
+
import type { FormulaError } from "#contracts/math/formula/syntax";
|
|
7
|
+
/**
|
|
8
|
+
* What a formula object draws once sampled: curves, marked points, a shaded
|
|
9
|
+
* region with solid or dashed edges, a surface grid, or the faces of a solid.
|
|
10
|
+
*/
|
|
11
|
+
export declare const FormulaGeometrySchema: Schema.Union<readonly [Schema.Struct<{
|
|
12
|
+
readonly branches: Schema.$Array<Schema.$Array<Schema.Struct<{
|
|
13
|
+
readonly x: Schema.Finite;
|
|
14
|
+
readonly y: Schema.Finite;
|
|
15
|
+
readonly z: Schema.Finite;
|
|
16
|
+
}>>>;
|
|
17
|
+
readonly kind: Schema.Literal<"curves">;
|
|
18
|
+
}>, Schema.Struct<{
|
|
19
|
+
readonly kind: Schema.Literal<"points">;
|
|
20
|
+
readonly points: Schema.$Array<Schema.Struct<{
|
|
21
|
+
readonly x: Schema.Finite;
|
|
22
|
+
readonly y: Schema.Finite;
|
|
23
|
+
readonly z: Schema.Finite;
|
|
24
|
+
}>>;
|
|
25
|
+
}>, Schema.Struct<{
|
|
26
|
+
readonly boundary: Schema.$Array<Schema.Struct<{
|
|
27
|
+
readonly points: Schema.$Array<Schema.Struct<{
|
|
28
|
+
readonly x: Schema.Finite;
|
|
29
|
+
readonly y: Schema.Finite;
|
|
30
|
+
readonly z: Schema.Finite;
|
|
31
|
+
}>>;
|
|
32
|
+
readonly strict: Schema.Boolean;
|
|
33
|
+
}>>;
|
|
34
|
+
readonly fill: Schema.$Array<Schema.$Array<Schema.Struct<{
|
|
35
|
+
readonly x: Schema.Finite;
|
|
36
|
+
readonly y: Schema.Finite;
|
|
37
|
+
readonly z: Schema.Finite;
|
|
38
|
+
}>>>;
|
|
39
|
+
readonly kind: Schema.Literal<"region">;
|
|
40
|
+
}>, Schema.Struct<{
|
|
41
|
+
readonly grid: Schema.Struct<{
|
|
42
|
+
readonly columns: Schema.Int;
|
|
43
|
+
readonly points: Schema.$Array<Schema.Option<Schema.Struct<{
|
|
44
|
+
readonly x: Schema.Finite;
|
|
45
|
+
readonly y: Schema.Finite;
|
|
46
|
+
readonly z: Schema.Finite;
|
|
47
|
+
}>>>;
|
|
48
|
+
readonly rows: Schema.Int;
|
|
49
|
+
}>;
|
|
50
|
+
readonly kind: Schema.Literal<"grid">;
|
|
51
|
+
}>, Schema.Struct<{
|
|
52
|
+
readonly faces: Schema.$Array<Schema.$Array<Schema.Struct<{
|
|
53
|
+
readonly x: Schema.Finite;
|
|
54
|
+
readonly y: Schema.Finite;
|
|
55
|
+
readonly z: Schema.Finite;
|
|
56
|
+
}>>>;
|
|
57
|
+
readonly kind: Schema.Literal<"faces">;
|
|
58
|
+
}>]>;
|
|
59
|
+
export type FormulaGeometry = typeof FormulaGeometrySchema.Type;
|
|
60
|
+
/** Compiles one authored expression into its evaluator. */
|
|
61
|
+
export declare function compileText(text: string): Result.Result<FormulaEvaluator, FormulaError>;
|
|
62
|
+
/** Evaluates one compiled formula with one more variable bound. */
|
|
63
|
+
export declare function evaluateWith(evaluate: FormulaEvaluator, scope: FormulaScope, name: string, value: number): number;
|
|
64
|
+
/**
|
|
65
|
+
* Places one formula point with the sliders' current values. A coordinate
|
|
66
|
+
* that is not finite there, such as ln(0), places nothing.
|
|
67
|
+
*/
|
|
68
|
+
export declare function planeFormulaPoint(point: PlaneFormulaPoint, scope: FormulaScope): Result.Result<Option.Option<SamplePoint>, FormulaError>;
|
|
69
|
+
/** The plane objects a formula defines. */
|
|
70
|
+
export type PlaneFormulaObject = (typeof PlaneCurveObjectSchemas)[number]["Type"];
|
|
71
|
+
/**
|
|
72
|
+
* Samples one plane formula object inside its frame. `named` holds the input
|
|
73
|
+
* values a function must hit exactly, such as the x of each of its labels.
|
|
74
|
+
*/
|
|
75
|
+
export declare function planeFormulaGeometry(object: PlaneFormulaObject, box: SampleBox, scope: FormulaScope, named?: readonly number[]): Result.Result<FormulaGeometry, FormulaError>;
|