@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,151 @@
|
|
|
1
|
+
import { Array as Arr, HashSet, Option, Order, Record as Rec, Result, Schema, } from "effect";
|
|
2
|
+
import { formulaNames } from "#contracts/math/formula/names";
|
|
3
|
+
import { readFormula, readRelation } from "#contracts/math/formula/parse";
|
|
4
|
+
/** One authored formula of an object: where it stands and what it may read. */
|
|
5
|
+
const FormulaFieldSchema = Schema.Struct({
|
|
6
|
+
path: Schema.Array(Schema.Union([Schema.String, Schema.Int])),
|
|
7
|
+
relation: Schema.Boolean,
|
|
8
|
+
text: Schema.String,
|
|
9
|
+
variables: Schema.Array(Schema.String),
|
|
10
|
+
});
|
|
11
|
+
/** Builds one expression field. */
|
|
12
|
+
export function expression(path, text, variables) {
|
|
13
|
+
return { path, relation: false, text, variables };
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Lists every formula a plane object holds and the variables each may read:
|
|
17
|
+
* its input axis for a function, t for a parametric curve, theta for a polar
|
|
18
|
+
* curve, and x and y for an equation or a region.
|
|
19
|
+
*/
|
|
20
|
+
export function planeFormulaFields(object) {
|
|
21
|
+
if (object.kind === "function") {
|
|
22
|
+
return [expression(["formula"], object.formula, [object.inputAxis ?? "x"])];
|
|
23
|
+
}
|
|
24
|
+
if (object.kind === "parametric") {
|
|
25
|
+
return [
|
|
26
|
+
expression(["x"], object.x, ["t"]),
|
|
27
|
+
expression(["y"], object.y, ["t"]),
|
|
28
|
+
];
|
|
29
|
+
}
|
|
30
|
+
if (object.kind === "polar") {
|
|
31
|
+
return [expression(["radius"], object.radius, ["theta"])];
|
|
32
|
+
}
|
|
33
|
+
if (object.kind === "marker") {
|
|
34
|
+
return [expression(["x"], object.x, []), expression(["y"], object.y, [])];
|
|
35
|
+
}
|
|
36
|
+
if (object.kind === "path" || object.kind === "loop") {
|
|
37
|
+
return Arr.flatMap(object.points, (point, index) => [
|
|
38
|
+
expression(["points", index, "x"], point.x, []),
|
|
39
|
+
expression(["points", index, "y"], point.y, []),
|
|
40
|
+
]);
|
|
41
|
+
}
|
|
42
|
+
if (object.kind === "implicit") {
|
|
43
|
+
return [
|
|
44
|
+
{
|
|
45
|
+
path: ["equation"],
|
|
46
|
+
relation: true,
|
|
47
|
+
text: object.equation,
|
|
48
|
+
variables: ["x", "y"],
|
|
49
|
+
},
|
|
50
|
+
];
|
|
51
|
+
}
|
|
52
|
+
if (object.kind === "region") {
|
|
53
|
+
return Arr.map(object.inequalities, (text, index) => ({
|
|
54
|
+
path: ["inequalities", index],
|
|
55
|
+
relation: true,
|
|
56
|
+
text,
|
|
57
|
+
variables: ["x", "y"],
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Lists every formula a space object holds and the variables each may read:
|
|
64
|
+
* t for a curve, x and y for a surface z = f(x, y), u and v for a parametric
|
|
65
|
+
* surface, and the axis variable for a solid of revolution.
|
|
66
|
+
*/
|
|
67
|
+
export function spaceFormulaFields(object) {
|
|
68
|
+
if (object.kind === "parametric" || object.kind === "surface") {
|
|
69
|
+
const variables = object.kind === "parametric" ? ["t"] : ["u", "v"];
|
|
70
|
+
return Arr.map(["x", "y", "z"], (axis) => expression([axis], object[axis], variables));
|
|
71
|
+
}
|
|
72
|
+
if (object.kind === "function") {
|
|
73
|
+
return [expression(["formula"], object.formula, ["x", "y"])];
|
|
74
|
+
}
|
|
75
|
+
if (object.kind === "revolution") {
|
|
76
|
+
return [expression(["formula"], object.formula, [object.axis])];
|
|
77
|
+
}
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
/** Every name one formula reads, or none when it does not parse at all. */
|
|
81
|
+
function namesOf(field) {
|
|
82
|
+
if (field.relation) {
|
|
83
|
+
return Result.match(readRelation(field.text), {
|
|
84
|
+
onFailure: () => HashSet.empty(),
|
|
85
|
+
onSuccess: ({ left, right }) => HashSet.union(formulaNames(left), formulaNames(right)),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return Result.match(readFormula(field.text), {
|
|
89
|
+
onFailure: () => HashSet.empty(),
|
|
90
|
+
onSuccess: formulaNames,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/** Reports one formula that reads a name it may not, at its root path. */
|
|
94
|
+
export function strayIssues(field, declared, root) {
|
|
95
|
+
const allowed = HashSet.fromIterable([...field.variables, ...declared]);
|
|
96
|
+
const stray = Arr.sort(Arr.fromIterable(HashSet.difference(namesOf(field), allowed)), Order.String);
|
|
97
|
+
return Arr.isReadonlyArrayEmpty(stray)
|
|
98
|
+
? []
|
|
99
|
+
: [
|
|
100
|
+
{
|
|
101
|
+
issue: `Expected only ${Arr.join([...field.variables, ...declared], ", ")} in this formula, not ${Arr.join(stray, ", ")}.`,
|
|
102
|
+
path: [...root, ...field.path],
|
|
103
|
+
},
|
|
104
|
+
];
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Reports every formula that reads a name its object does not offer, and every
|
|
108
|
+
* slider whose name repeats an earlier one.
|
|
109
|
+
*/
|
|
110
|
+
export function formulaScopeIssues(objects, fields, parameters) {
|
|
111
|
+
const names = Arr.map(parameters, ({ name }) => name);
|
|
112
|
+
const declared = Arr.dedupe(names);
|
|
113
|
+
const repeated = Arr.flatMap(parameters, ({ name }, index) => Arr.contains(Arr.take(names, index), name)
|
|
114
|
+
? [
|
|
115
|
+
{
|
|
116
|
+
issue: "Expected a unique slider name.",
|
|
117
|
+
path: ["parameters", index, "name"],
|
|
118
|
+
},
|
|
119
|
+
]
|
|
120
|
+
: []);
|
|
121
|
+
const unknown = Arr.flatMap(objects, (object, index) => Arr.flatMap(fields(object), (field) => strayIssues(field, declared, ["objects", index])));
|
|
122
|
+
return [...repeated, ...unknown];
|
|
123
|
+
}
|
|
124
|
+
/** Each slider's starting value by its name, the scope a scene first draws with. */
|
|
125
|
+
export function parameterScope(parameters) {
|
|
126
|
+
return Rec.fromEntries(Arr.map(parameters, ({ name, value }) => [name, value]));
|
|
127
|
+
}
|
|
128
|
+
/** Whether a point lies inside a box; a plane box ignores z. */
|
|
129
|
+
function inside(box, point) {
|
|
130
|
+
/** Whether a value lies inside one range, its ends included. */
|
|
131
|
+
const within = (value, range) => value >= range.min && value <= range.max;
|
|
132
|
+
return (within(point.x, box.x) &&
|
|
133
|
+
within(point.y, box.y) &&
|
|
134
|
+
(box.z === undefined || within(point.z, box.z)));
|
|
135
|
+
}
|
|
136
|
+
/** Whether sampled geometry shows anything inside its frame. */
|
|
137
|
+
export function geometryVisible(geometry, box) {
|
|
138
|
+
if (geometry.kind === "curves") {
|
|
139
|
+
return Arr.some(geometry.branches, Arr.isReadonlyArrayNonEmpty);
|
|
140
|
+
}
|
|
141
|
+
if (geometry.kind === "region") {
|
|
142
|
+
return Arr.isReadonlyArrayNonEmpty(geometry.fill);
|
|
143
|
+
}
|
|
144
|
+
if (geometry.kind === "points") {
|
|
145
|
+
return Arr.some(geometry.points, (point) => inside(box, point));
|
|
146
|
+
}
|
|
147
|
+
if (geometry.kind === "grid") {
|
|
148
|
+
return Arr.some(geometry.grid.points, (point) => Option.exists(point, (each) => inside(box, each)));
|
|
149
|
+
}
|
|
150
|
+
return Arr.every(geometry.faces, (face) => Arr.every(face, (corner) => inside(box, corner)));
|
|
151
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Schema } from "effect";
|
|
2
|
+
import { type FormulaParameter, type FormulaReadout } from "#contracts/math/formula/field";
|
|
3
|
+
import type { PlaneLabelAnchor } from "#contracts/math/plane";
|
|
4
|
+
/**
|
|
5
|
+
* Reports every label position or readout formula that reads a name other
|
|
6
|
+
* than a slider's, since only the sliders move them, and every readout key
|
|
7
|
+
* that repeats an earlier one.
|
|
8
|
+
*/
|
|
9
|
+
export declare function sliderFormulaIssues(labels: readonly PlaneLabelAnchor[], readouts: readonly FormulaReadout[], parameters: readonly FormulaParameter[]): readonly Schema.FilterIssue[];
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Array as Arr } from "effect";
|
|
2
|
+
import { isPlaneFormulaPoint, } from "#contracts/math/formula/field";
|
|
3
|
+
import { expression, strayIssues } from "#contracts/math/formula/scope";
|
|
4
|
+
/**
|
|
5
|
+
* Reports every label position or readout formula that reads a name other
|
|
6
|
+
* than a slider's, since only the sliders move them, and every readout key
|
|
7
|
+
* that repeats an earlier one.
|
|
8
|
+
*/
|
|
9
|
+
export function sliderFormulaIssues(labels, readouts, parameters) {
|
|
10
|
+
const declared = Arr.dedupe(Arr.map(parameters, ({ name }) => name));
|
|
11
|
+
const anchors = Arr.flatMap(labels, ({ at }, index) => isPlaneFormulaPoint(at)
|
|
12
|
+
? Arr.flatMap(["x", "y"], (axis) => strayIssues(expression([axis], at[axis], []), declared, [
|
|
13
|
+
"labels",
|
|
14
|
+
index,
|
|
15
|
+
"at",
|
|
16
|
+
]))
|
|
17
|
+
: []);
|
|
18
|
+
const values = Arr.flatMap(readouts, ({ formula }, index) => strayIssues(expression(["formula"], formula, []), declared, [
|
|
19
|
+
"readouts",
|
|
20
|
+
index,
|
|
21
|
+
]));
|
|
22
|
+
const keys = Arr.map(readouts, ({ key }) => key);
|
|
23
|
+
const repeated = Arr.flatMap(keys, (key, index) => Arr.contains(Arr.take(keys, index), key)
|
|
24
|
+
? [
|
|
25
|
+
{
|
|
26
|
+
issue: "Expected a unique readout key.",
|
|
27
|
+
path: ["readouts", index, "key"],
|
|
28
|
+
},
|
|
29
|
+
]
|
|
30
|
+
: []);
|
|
31
|
+
return [...anchors, ...values, ...repeated];
|
|
32
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Result } from "effect";
|
|
2
|
+
import type { SampleBox } from "#contracts/math/formula/box";
|
|
3
|
+
import type { FormulaScope } from "#contracts/math/formula/compile";
|
|
4
|
+
import { type FormulaGeometry } from "#contracts/math/formula/plane";
|
|
5
|
+
import type { FormulaError } from "#contracts/math/formula/syntax";
|
|
6
|
+
import type { SpaceSolidObjectSchemas } from "#contracts/math/solid";
|
|
7
|
+
/** The space objects a formula or a face defines. */
|
|
8
|
+
export type SpaceFormulaObject = (typeof SpaceSolidObjectSchemas)[number]["Type"];
|
|
9
|
+
/** Samples one space formula object, or builds the faces of a solid. */
|
|
10
|
+
export declare function spaceFormulaGeometry(object: SpaceFormulaObject, box: SampleBox, scope: FormulaScope): Result.Result<FormulaGeometry, FormulaError>;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Array as Arr, Result } from "effect";
|
|
2
|
+
import { sampleGrid } from "#contracts/math/formula/mesh";
|
|
3
|
+
import { compileText, evaluateWith, } from "#contracts/math/formula/plane";
|
|
4
|
+
import { sampleCurve } from "#contracts/math/formula/sample";
|
|
5
|
+
const FULL_TURN = { max: 2 * Math.PI, min: 0 };
|
|
6
|
+
/** Moves one point by an offset. */
|
|
7
|
+
function shifted(point, offset) {
|
|
8
|
+
return {
|
|
9
|
+
x: point.x + offset.x,
|
|
10
|
+
y: point.y + offset.y,
|
|
11
|
+
z: point.z + offset.z,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
/** Pairs each corner of a face with the next one, wrapping to the first. */
|
|
15
|
+
function edges(face) {
|
|
16
|
+
return Arr.zip(face, Arr.append(Arr.drop(face, 1), Arr.headNonEmpty(face)));
|
|
17
|
+
}
|
|
18
|
+
/** The faces of a prism: both bases and one quadrilateral per base edge. */
|
|
19
|
+
function prismFaces(base, offset) {
|
|
20
|
+
const top = Arr.map(base, (corner) => shifted(corner, offset));
|
|
21
|
+
return [
|
|
22
|
+
[...base],
|
|
23
|
+
[...top],
|
|
24
|
+
...Arr.map(edges(base), ([from, to]) => [
|
|
25
|
+
from,
|
|
26
|
+
to,
|
|
27
|
+
shifted(to, offset),
|
|
28
|
+
shifted(from, offset),
|
|
29
|
+
]),
|
|
30
|
+
];
|
|
31
|
+
}
|
|
32
|
+
/** The faces of a pyramid: its base and one triangle per base edge. */
|
|
33
|
+
function pyramidFaces(base, apex) {
|
|
34
|
+
return [[...base], ...Arr.map(edges(base), ([from, to]) => [from, to, apex])];
|
|
35
|
+
}
|
|
36
|
+
/** A point at a radius from an axis, at a coordinate along it and an angle around it. */
|
|
37
|
+
function revolve(axis, along, radius, angle) {
|
|
38
|
+
const across = radius * Math.cos(angle);
|
|
39
|
+
const depth = radius * Math.sin(angle);
|
|
40
|
+
if (axis === "x") {
|
|
41
|
+
return { x: along, y: across, z: depth };
|
|
42
|
+
}
|
|
43
|
+
return axis === "y"
|
|
44
|
+
? { x: depth, y: along, z: across }
|
|
45
|
+
: { x: across, y: depth, z: along };
|
|
46
|
+
}
|
|
47
|
+
/** Samples one space formula object, or builds the faces of a solid. */
|
|
48
|
+
export function spaceFormulaGeometry(object, box, scope) {
|
|
49
|
+
return Result.gen(function* () {
|
|
50
|
+
if (object.kind === "prism") {
|
|
51
|
+
return {
|
|
52
|
+
faces: prismFaces(object.base, object.offset),
|
|
53
|
+
kind: "faces",
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (object.kind === "pyramid") {
|
|
57
|
+
return {
|
|
58
|
+
faces: pyramidFaces(object.base, object.apex),
|
|
59
|
+
kind: "faces",
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
if (object.kind === "parametric") {
|
|
63
|
+
const fx = yield* compileText(object.x);
|
|
64
|
+
const fy = yield* compileText(object.y);
|
|
65
|
+
const fz = yield* compileText(object.z);
|
|
66
|
+
const branches = sampleCurve((t) => ({
|
|
67
|
+
x: evaluateWith(fx, scope, "t", t),
|
|
68
|
+
y: evaluateWith(fy, scope, "t", t),
|
|
69
|
+
z: evaluateWith(fz, scope, "t", t),
|
|
70
|
+
}), box, object.parameter);
|
|
71
|
+
return { branches, kind: "curves" };
|
|
72
|
+
}
|
|
73
|
+
if (object.kind === "function") {
|
|
74
|
+
const f = yield* compileText(object.formula);
|
|
75
|
+
const domain = object.domain ?? { x: box.x, y: box.y };
|
|
76
|
+
const grid = sampleGrid((x, y) => ({ x, y, z: f({ ...scope, x, y }) }), domain.x, domain.y);
|
|
77
|
+
return { grid, kind: "grid" };
|
|
78
|
+
}
|
|
79
|
+
if (object.kind === "surface") {
|
|
80
|
+
const fx = yield* compileText(object.x);
|
|
81
|
+
const fy = yield* compileText(object.y);
|
|
82
|
+
const fz = yield* compileText(object.z);
|
|
83
|
+
const grid = sampleGrid((u, v) => {
|
|
84
|
+
const point = { ...scope, u, v };
|
|
85
|
+
return { x: fx(point), y: fy(point), z: fz(point) };
|
|
86
|
+
}, object.u, object.v);
|
|
87
|
+
return { grid, kind: "grid" };
|
|
88
|
+
}
|
|
89
|
+
const radius = yield* compileText(object.formula);
|
|
90
|
+
const { axis } = object;
|
|
91
|
+
const grid = sampleGrid((along, angle) => revolve(axis, along, evaluateWith(radius, scope, axis, along), angle), object.domain, FULL_TURN);
|
|
92
|
+
return { grid, kind: "grid" };
|
|
93
|
+
});
|
|
94
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
/** Arithmetic operators of a formula, from the loosest binding to the tightest. */
|
|
3
|
+
export declare const FormulaOperatorSchema: Schema.Literals<readonly ["+", "-", "*", "/", "^"]>;
|
|
4
|
+
export type FormulaOperator = typeof FormulaOperatorSchema.Type;
|
|
5
|
+
/** Comparisons that turn two expressions into an equation or an inequality. */
|
|
6
|
+
export declare const FormulaComparisonSchema: Schema.Literals<readonly ["=", "<", "<=", ">", ">="]>;
|
|
7
|
+
export type FormulaComparison = typeof FormulaComparisonSchema.Type;
|
|
8
|
+
/** Functions of one argument, in radians where an angle is meant. */
|
|
9
|
+
export declare const FormulaUnarySchema: Schema.Literals<readonly ["abs", "acos", "asin", "atan", "cbrt", "ceil", "cos", "cosh", "cot", "csc", "exp", "floor", "ln", "log", "sec", "sign", "sin", "sinh", "sqrt", "tan", "tanh"]>;
|
|
10
|
+
export type FormulaUnary = typeof FormulaUnarySchema.Type;
|
|
11
|
+
/**
|
|
12
|
+
* Every function a formula may call: the functions of one argument, and `min`
|
|
13
|
+
* and `max`, which take two or more. A logarithm in another base is written
|
|
14
|
+
* `log_b(x)`.
|
|
15
|
+
*/
|
|
16
|
+
export declare const FormulaFunctionSchema: Schema.Literals<readonly ["abs", "acos", "asin", "atan", "cbrt", "ceil", "cos", "cosh", "cot", "csc", "exp", "floor", "ln", "log", "sec", "sign", "sin", "sinh", "sqrt", "tan", "tanh", "max", "min"]>;
|
|
17
|
+
export type FormulaFunction = typeof FormulaFunctionSchema.Type;
|
|
18
|
+
/** Named constants, each with its exact numeric value. */
|
|
19
|
+
export declare const FORMULA_CONSTANTS: {
|
|
20
|
+
readonly e: number;
|
|
21
|
+
readonly pi: number;
|
|
22
|
+
};
|
|
23
|
+
/** The variables an object's formula is written in, chosen by its kind. */
|
|
24
|
+
export declare const FormulaVariableSchema: Schema.Literals<readonly ["theta", "t", "u", "v", "x", "y", "z"]>;
|
|
25
|
+
export type FormulaVariable = typeof FormulaVariableSchema.Type;
|
|
26
|
+
/**
|
|
27
|
+
* A slider parameter's name: one lowercase Latin letter that is neither a
|
|
28
|
+
* variable nor the constant e, so `a`, `b`, `h`, `k`, and `m` all qualify.
|
|
29
|
+
*/
|
|
30
|
+
export declare const FormulaParameterNameSchema: Schema.String;
|
|
31
|
+
/** The named type of one formula node, which a node's children repeat. */
|
|
32
|
+
export type FormulaNode = {
|
|
33
|
+
readonly kind: "number";
|
|
34
|
+
readonly value: number;
|
|
35
|
+
} | {
|
|
36
|
+
readonly kind: "name";
|
|
37
|
+
readonly name: string;
|
|
38
|
+
} | {
|
|
39
|
+
readonly kind: "negate";
|
|
40
|
+
readonly operand: FormulaNode;
|
|
41
|
+
} | {
|
|
42
|
+
readonly kind: "binary";
|
|
43
|
+
readonly left: FormulaNode;
|
|
44
|
+
readonly operator: FormulaOperator;
|
|
45
|
+
readonly right: FormulaNode;
|
|
46
|
+
} | {
|
|
47
|
+
readonly args: readonly [FormulaNode, ...FormulaNode[]];
|
|
48
|
+
readonly callee: FormulaFunction;
|
|
49
|
+
readonly kind: "call";
|
|
50
|
+
} | {
|
|
51
|
+
readonly base: FormulaNode;
|
|
52
|
+
readonly kind: "logarithm";
|
|
53
|
+
readonly value: FormulaNode;
|
|
54
|
+
};
|
|
55
|
+
/** One parsed formula expression as a tree of numbers, names, and operations. */
|
|
56
|
+
export declare const FormulaNodeSchema: Schema.Codec<FormulaNode>;
|
|
57
|
+
/** One parsed equation or inequality: two expressions and their comparison. */
|
|
58
|
+
export declare const FormulaRelationSchema: Schema.Struct<{
|
|
59
|
+
readonly comparison: Schema.Literals<readonly ["=", "<", "<=", ">", ">="]>;
|
|
60
|
+
readonly left: Schema.Codec<FormulaNode, FormulaNode, never, never>;
|
|
61
|
+
readonly right: Schema.Codec<FormulaNode, FormulaNode, never, never>;
|
|
62
|
+
}>;
|
|
63
|
+
export type FormulaRelation = typeof FormulaRelationSchema.Type;
|
|
64
|
+
/** Why a formula could not be read, so an editor can say it in any language. */
|
|
65
|
+
export declare const FormulaProblemSchema: Schema.Literals<readonly ["comparison-expected", "comparison-unexpected", "empty", "function-arguments", "group-unclosed", "operator-expected", "symbol-unexpected", "value-expected"]>;
|
|
66
|
+
export type FormulaProblem = typeof FormulaProblemSchema.Type;
|
|
67
|
+
declare const FormulaError_base: Schema.Class<FormulaError, Schema.TaggedStruct<"FormulaError", {
|
|
68
|
+
readonly message: Schema.String;
|
|
69
|
+
readonly position: Schema.Int;
|
|
70
|
+
readonly problem: Schema.Literals<readonly ["comparison-expected", "comparison-unexpected", "empty", "function-arguments", "group-unclosed", "operator-expected", "symbol-unexpected", "value-expected"]>;
|
|
71
|
+
}>, import("effect/Cause").YieldableError & {
|
|
72
|
+
readonly brand: unique symbol;
|
|
73
|
+
}>;
|
|
74
|
+
/** A formula that does not parse, with the problem and where it starts. */
|
|
75
|
+
export declare class FormulaError extends FormulaError_base {
|
|
76
|
+
}
|
|
77
|
+
export {};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
/** Arithmetic operators of a formula, from the loosest binding to the tightest. */
|
|
3
|
+
export const FormulaOperatorSchema = Schema.Literals(["+", "-", "*", "/", "^"]);
|
|
4
|
+
/** Comparisons that turn two expressions into an equation or an inequality. */
|
|
5
|
+
export const FormulaComparisonSchema = Schema.Literals([
|
|
6
|
+
"=",
|
|
7
|
+
"<",
|
|
8
|
+
"<=",
|
|
9
|
+
">",
|
|
10
|
+
">=",
|
|
11
|
+
]);
|
|
12
|
+
/** Functions of one argument, in radians where an angle is meant. */
|
|
13
|
+
export const FormulaUnarySchema = Schema.Literals([
|
|
14
|
+
"abs",
|
|
15
|
+
"acos",
|
|
16
|
+
"asin",
|
|
17
|
+
"atan",
|
|
18
|
+
"cbrt",
|
|
19
|
+
"ceil",
|
|
20
|
+
"cos",
|
|
21
|
+
"cosh",
|
|
22
|
+
"cot",
|
|
23
|
+
"csc",
|
|
24
|
+
"exp",
|
|
25
|
+
"floor",
|
|
26
|
+
"ln",
|
|
27
|
+
"log",
|
|
28
|
+
"sec",
|
|
29
|
+
"sign",
|
|
30
|
+
"sin",
|
|
31
|
+
"sinh",
|
|
32
|
+
"sqrt",
|
|
33
|
+
"tan",
|
|
34
|
+
"tanh",
|
|
35
|
+
]);
|
|
36
|
+
/**
|
|
37
|
+
* Every function a formula may call: the functions of one argument, and `min`
|
|
38
|
+
* and `max`, which take two or more. A logarithm in another base is written
|
|
39
|
+
* `log_b(x)`.
|
|
40
|
+
*/
|
|
41
|
+
export const FormulaFunctionSchema = Schema.Literals([
|
|
42
|
+
...FormulaUnarySchema.literals,
|
|
43
|
+
"max",
|
|
44
|
+
"min",
|
|
45
|
+
]);
|
|
46
|
+
/** Named constants, each with its exact numeric value. */
|
|
47
|
+
export const FORMULA_CONSTANTS = { e: Math.E, pi: Math.PI };
|
|
48
|
+
/** The variables an object's formula is written in, chosen by its kind. */
|
|
49
|
+
export const FormulaVariableSchema = Schema.Literals([
|
|
50
|
+
"theta",
|
|
51
|
+
"t",
|
|
52
|
+
"u",
|
|
53
|
+
"v",
|
|
54
|
+
"x",
|
|
55
|
+
"y",
|
|
56
|
+
"z",
|
|
57
|
+
]);
|
|
58
|
+
/**
|
|
59
|
+
* A slider parameter's name: one lowercase Latin letter that is neither a
|
|
60
|
+
* variable nor the constant e, so `a`, `b`, `h`, `k`, and `m` all qualify.
|
|
61
|
+
*/
|
|
62
|
+
export const FormulaParameterNameSchema = Schema.String.check(Schema.isPattern(/^[a-df-sw]$/u));
|
|
63
|
+
/** One parsed formula expression as a tree of numbers, names, and operations. */
|
|
64
|
+
export const FormulaNodeSchema = Schema.Union([
|
|
65
|
+
Schema.Struct({ kind: Schema.Literal("number"), value: Schema.Finite }),
|
|
66
|
+
Schema.Struct({ kind: Schema.Literal("name"), name: Schema.String }),
|
|
67
|
+
Schema.Struct({
|
|
68
|
+
kind: Schema.Literal("negate"),
|
|
69
|
+
operand: Schema.suspend(() => FormulaNodeSchema),
|
|
70
|
+
}),
|
|
71
|
+
Schema.Struct({
|
|
72
|
+
kind: Schema.Literal("binary"),
|
|
73
|
+
left: Schema.suspend(() => FormulaNodeSchema),
|
|
74
|
+
operator: FormulaOperatorSchema,
|
|
75
|
+
right: Schema.suspend(() => FormulaNodeSchema),
|
|
76
|
+
}),
|
|
77
|
+
Schema.Struct({
|
|
78
|
+
args: Schema.NonEmptyArray(Schema.suspend(() => FormulaNodeSchema)),
|
|
79
|
+
callee: FormulaFunctionSchema,
|
|
80
|
+
kind: Schema.Literal("call"),
|
|
81
|
+
}),
|
|
82
|
+
Schema.Struct({
|
|
83
|
+
base: Schema.suspend(() => FormulaNodeSchema),
|
|
84
|
+
kind: Schema.Literal("logarithm"),
|
|
85
|
+
value: Schema.suspend(() => FormulaNodeSchema),
|
|
86
|
+
}),
|
|
87
|
+
]);
|
|
88
|
+
/** One parsed equation or inequality: two expressions and their comparison. */
|
|
89
|
+
export const FormulaRelationSchema = Schema.Struct({
|
|
90
|
+
comparison: FormulaComparisonSchema,
|
|
91
|
+
left: FormulaNodeSchema,
|
|
92
|
+
right: FormulaNodeSchema,
|
|
93
|
+
});
|
|
94
|
+
/** Why a formula could not be read, so an editor can say it in any language. */
|
|
95
|
+
export const FormulaProblemSchema = Schema.Literals([
|
|
96
|
+
"comparison-expected",
|
|
97
|
+
"comparison-unexpected",
|
|
98
|
+
"empty",
|
|
99
|
+
"function-arguments",
|
|
100
|
+
"group-unclosed",
|
|
101
|
+
"operator-expected",
|
|
102
|
+
"symbol-unexpected",
|
|
103
|
+
"value-expected",
|
|
104
|
+
]);
|
|
105
|
+
/** A formula that does not parse, with the problem and where it starts. */
|
|
106
|
+
export class FormulaError extends Schema.TaggedError()("FormulaError", {
|
|
107
|
+
message: Schema.String,
|
|
108
|
+
position: Schema.Int,
|
|
109
|
+
problem: FormulaProblemSchema,
|
|
110
|
+
}) {
|
|
111
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Result, Schema } from "effect";
|
|
2
|
+
import { FormulaError } from "#contracts/math/formula/syntax";
|
|
3
|
+
/** One token of a formula and the index of its first character. */
|
|
4
|
+
export declare const FormulaTokenSchema: Schema.Union<readonly [Schema.Struct<{
|
|
5
|
+
readonly kind: Schema.Literal<"number">;
|
|
6
|
+
readonly position: Schema.Int;
|
|
7
|
+
readonly value: Schema.Finite;
|
|
8
|
+
}>, Schema.Struct<{
|
|
9
|
+
readonly kind: Schema.Literal<"name">;
|
|
10
|
+
readonly name: Schema.String;
|
|
11
|
+
readonly position: Schema.Int;
|
|
12
|
+
}>, Schema.Struct<{
|
|
13
|
+
readonly kind: Schema.Literal<"symbol">;
|
|
14
|
+
readonly position: Schema.Int;
|
|
15
|
+
readonly symbol: Schema.Literals<readonly ["(", ")", "*", "+", ",", "-", "/", "^", "_", "|", "°", "√"]>;
|
|
16
|
+
}>, Schema.Struct<{
|
|
17
|
+
readonly comparison: Schema.Literals<readonly ["=", "<", "<=", ">", ">="]>;
|
|
18
|
+
readonly kind: Schema.Literal<"comparison">;
|
|
19
|
+
readonly position: Schema.Int;
|
|
20
|
+
}>]>;
|
|
21
|
+
export type FormulaToken = typeof FormulaTokenSchema.Type;
|
|
22
|
+
/**
|
|
23
|
+
* Reads a formula into tokens. Letters split into the longest known words, so
|
|
24
|
+
* `2xy` reads as two, x, y and `sinh` as one function; π and θ read as `pi`
|
|
25
|
+
* and `theta`, and ² and ³ as powers.
|
|
26
|
+
*/
|
|
27
|
+
export declare function tokenizeFormula(text: string): Result.Result<never, FormulaError> | Result.Result<({
|
|
28
|
+
readonly kind: "number";
|
|
29
|
+
readonly position: number;
|
|
30
|
+
readonly value: number;
|
|
31
|
+
} | {
|
|
32
|
+
readonly kind: "name";
|
|
33
|
+
readonly name: string;
|
|
34
|
+
readonly position: number;
|
|
35
|
+
} | {
|
|
36
|
+
readonly kind: "symbol";
|
|
37
|
+
readonly position: number;
|
|
38
|
+
readonly symbol: "(" | ")" | "*" | "+" | "," | "-" | "/" | "^" | "_" | "|" | "°" | "√";
|
|
39
|
+
} | {
|
|
40
|
+
readonly comparison: "<" | "<=" | "=" | ">" | ">=";
|
|
41
|
+
readonly kind: "comparison";
|
|
42
|
+
readonly position: number;
|
|
43
|
+
})[], never>;
|