Skip to content

Commit f419bc4

Browse files
committed
arch: the great renaming part 1
1 parent cd8f3e1 commit f419bc4

140 files changed

Lines changed: 2132 additions & 2007 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

LENIENT_PARSER.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Let's introduce the concept of "strict mode" for LaTeX parsing.
2+
3+
The default would be `strict = true` and correspond to the current behavior.
4+
5+
When `strict = false`, we would accept a broader syntax, similar to ASCIIMath or
6+
Typst.
7+
8+
Specifically:
9+
10+
(In the expression below `#` is one or more digits)
11+
12+
- `(...)/(...)` -> `\frac{...}{...}`, so `(x+1)/(2)` -> `\frac{x}{2}`
13+
- Also `#/(...)` and `(...)/#`
14+
- Also `?/?` where `?` are a digit or an alphanumeric character
15+
- `x^(...)` -> `x^{...}`
16+
- Also `x^#` so `x^123` -> `x^{123}`
17+
- Also `x^-#` so `x^-1+2` -> `x^{-1}+2`
18+
- `*` -> `\times`
19+
- `oo` -> `\infty`
20+
21+
- The following sequence of characters would be intepreted as indicated:
22+
- `sqrt(...)` -> `\sqrt{...}`
23+
- `cbrt(...)` -> `\sqrt[3]{...}`
24+
- `sqrt#` -> `\sqrt{#}`, so `2+sqrt34-1` -> `2+\sqrt{34}-1`
25+
- `cbrt#` -> `\sqrt[3]{...}`
26+
- `cos`, `sin`, `tan`, `ln`, `log` -> `\cos`, `\sin`, `\tan`, `\ln`, `\log`
27+
- similarly for inverse and hyperbolic trig functions
28+
- `log#(...)` -> `log_{#}(...)`
29+
- `lim_...` -> `\lim_{...}`
30+
- `int_...^...` -> `\int_...^...`
31+
- `>=`, `<=`, `!=`, `` -> `\ge`, `\le`, `\neq`
32+
- `->` -> `\to`
33+
- `=>` -> `\implies`
34+
- `<=>` -> `\equivalent`
35+
- `sum_...^...(...)` -> `\sum_..^... ...`
36+
- `prod_...^...(...)` -> `\prod..^... ...`
37+
- `vec(a, b, c)` -> `\begin{pmatrix}a\\b\\c\end{pmatrix}`
38+
- `mat(a, b, c; d, e, f)` -> `\begin{pmatrix}a& b&c\\d&e&f\end{pmatrix}`
39+
40+
Other examples:
41+
42+
- `sqrt sqrt cbrt x` -> `\sqrt{\sqrt{\sqrt[3]{x}}}`
43+
- `lim_(x->oo) 1/x+1` -> `\lim_{x\to\infty) \frac{1}{x}+1`
44+
- `lim_(x->-oo) 1/(x+1)` -> `\lim_{x\to-\infty) \frac{1}{x+1}`

MIGRATION_GUIDE.md

Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,24 @@ type FormOption =
6666

6767
## 2. Role-Specific Properties Moved to Role Interfaces
6868

69-
Properties that were previously available on all `BoxedExpression` instances
69+
Properties that were previously available on all `Expression` instances
7070
(returning `null` or `undefined` when not applicable) have been removed from
7171
the base interface. They are now only accessible after narrowing with a type
7272
guard.
7373

74-
### Removed from `BoxedExpression`
74+
### Removed from `Expression`
7575

7676
| Property | Access via |
7777
|:-------------------|:-------------------------------------------------|
78-
| `.symbol` | `isBoxedSymbol(expr)` then `expr.symbol` |
79-
| `.string` | `isBoxedString(expr)` then `expr.string` |
80-
| `.ops` | `isBoxedFunction(expr)` then `expr.ops` |
81-
| `.nops` | `isBoxedFunction(expr)` then `expr.nops` |
82-
| `.op1`/`.op2`/`.op3` | `isBoxedFunction(expr)` then `expr.op1` etc. |
83-
| `.isFunctionExpression` | `isBoxedFunction(expr)` then `expr.isFunctionExpression` |
84-
| `.numericValue` | `isBoxedNumber(expr)` then `expr.numericValue` |
85-
| `.isNumberLiteral` | `isBoxedNumber(expr)` then `expr.isNumberLiteral` |
86-
| `.tensor` | `isBoxedTensor(expr)` then `expr.tensor` |
78+
| `.symbol` | `isSymbol(expr)` then `expr.symbol` |
79+
| `.string` | `isString(expr)` then `expr.string` |
80+
| `.ops` | `isFunction(expr)` then `expr.ops` |
81+
| `.nops` | `isFunction(expr)` then `expr.nops` |
82+
| `.op1`/`.op2`/`.op3` | `isFunction(expr)` then `expr.op1` etc. |
83+
| `.isFunctionExpression` | `isFunction(expr)` then `expr.isFunctionExpression` |
84+
| `.numericValue` | `isNumber(expr)` then `expr.numericValue` |
85+
| `.isNumberLiteral` | `isNumber(expr)` then `expr.isNumberLiteral` |
86+
| `.tensor` | `isTensor(expr)` then `expr.tensor` |
8787

8888
### Before
8989

@@ -100,14 +100,14 @@ if (expr.numericValue !== null) {
100100
### After
101101

102102
```ts
103-
import { isBoxedSymbol, isBoxedNumber, sym } from '@cortex-js/compute-engine';
103+
import { isSymbol, isNumber, sym } from '@cortex-js/compute-engine';
104104

105-
if (isBoxedSymbol(expr)) {
105+
if (isSymbol(expr)) {
106106
// expr.symbol is `string` — guaranteed non-undefined
107107
console.log(expr.symbol);
108108
}
109109

110-
if (isBoxedNumber(expr)) {
110+
if (isNumber(expr)) {
111111
// expr.numericValue is `number | NumericValue` — guaranteed non-undefined
112112
console.log(expr.numericValue);
113113
}
@@ -120,10 +120,10 @@ if (sym(expr) === 'Pi') {
120120

121121
See Section 6 for the full list of type guards and role interfaces.
122122

123-
**Note:** The `sym()` helper combines `isBoxedSymbol()` check with symbol name access,
123+
**Note:** The `sym()` helper combines `isSymbol()` check with symbol name access,
124124
making simple symbol comparisons more concise.
125125

126-
### Still on `BoxedExpression`
126+
### Still on `Expression`
127127

128128
- `.re` / `.im` — typed `number`, return `NaN` when not applicable
129129
- `.shape` — typed `number[]`, returns `[]` for scalars
@@ -274,12 +274,12 @@ Nine type guard functions are available for runtime type checking. They narrow
274274
to role interfaces that provide typed access to properties specific to that
275275
expression kind. These guards are now **required** to access role-specific
276276
properties (`.symbol`, `.ops`, `.numericValue`, etc.) that have been removed
277-
from the base `BoxedExpression` interface.
277+
from the base `Expression` interface.
278278

279279
### Before
280280

281281
```ts
282-
// Properties were on BoxedExpression, returned null when not applicable
282+
// Properties were on Expression, returned null when not applicable
283283
if (expr.symbol !== null) {
284284
console.log(expr.symbol);
285285
}
@@ -292,41 +292,41 @@ if (expr.numericValue !== null) {
292292

293293
```ts
294294
import {
295-
isBoxedNumber,
296-
isBoxedSymbol,
297-
isBoxedFunction,
298-
isBoxedString,
299-
isBoxedTensor,
295+
isNumber,
296+
isSymbol,
297+
isFunction,
298+
isString,
299+
isTensor,
300300
isDictionary,
301301
isCollection,
302302
isIndexedCollection,
303-
isBoxedExpression,
303+
isExpression,
304304
} from '@cortex-js/compute-engine';
305305

306306
// Type guards narrow the type — no undefined checks needed
307-
if (isBoxedNumber(expr)) {
307+
if (isNumber(expr)) {
308308
// expr.numericValue is `number | NumericValue` (not undefined)
309309
// expr.isNumberLiteral is `true` (not boolean)
310310
console.log(expr.numericValue);
311311
}
312312

313-
if (isBoxedSymbol(expr)) {
313+
if (isSymbol(expr)) {
314314
// expr.symbol is `string` (not undefined)
315315
console.log(expr.symbol);
316316
}
317317

318-
if (isBoxedFunction(expr)) {
319-
// expr.ops is `ReadonlyArray<BoxedExpression>` (not undefined)
318+
if (isFunction(expr)) {
319+
// expr.ops is `ReadonlyArray<Expression>` (not undefined)
320320
// expr.isFunctionExpression is `true`
321321
console.log(expr.ops, expr.nops, expr.op1);
322322
}
323323

324-
if (isBoxedString(expr)) {
324+
if (isString(expr)) {
325325
// expr.string is `string` (not undefined)
326326
console.log(expr.string);
327327
}
328328

329-
if (isBoxedTensor(expr)) {
329+
if (isTensor(expr)) {
330330
// expr.tensor is `Tensor<any>` (not undefined)
331331
// expr.shape is `number[]`, expr.rank is `number`
332332
console.log(expr.shape, expr.rank);
@@ -351,7 +351,7 @@ For quick symbol name checks, use the `sym()` helper:
351351
import { sym } from '@cortex-js/compute-engine';
352352

353353
// Instead of:
354-
if (isBoxedSymbol(expr) && expr.symbol === 'Pi') { /* ... */ }
354+
if (isSymbol(expr) && expr.symbol === 'Pi') { /* ... */ }
355355

356356
// You can write:
357357
if (sym(expr) === 'Pi') { /* ... */ }
@@ -364,15 +364,15 @@ const name = sym(expr); // string | undefined
364364

365365
| Guard | Narrows to |
366366
|:-------------------|:-------------------------------------------|
367-
| `isBoxedNumber` | `BoxedExpression & NumberLiteralInterface` |
368-
| `isBoxedSymbol` | `BoxedExpression & SymbolInterface` |
369-
| `isBoxedFunction` | `BoxedExpression & FunctionInterface` |
370-
| `isBoxedString` | `BoxedExpression & StringInterface` |
371-
| `isBoxedTensor` | `BoxedExpression & TensorInterface` |
372-
| `isDictionary` | `BoxedExpression & DictionaryInterface` |
373-
| `isCollection` | `BoxedExpression & CollectionInterface` |
374-
| `isIndexedCollection` | `BoxedExpression & IndexedCollectionInterface` |
375-
| `isBoxedExpression` | `BoxedExpression` (from `unknown`) |
367+
| `isNumber` | `Expression & NumberLiteralInterface` |
368+
| `isSymbol` | `Expression & SymbolInterface` |
369+
| `isFunction` | `Expression & FunctionInterface` |
370+
| `isString` | `Expression & StringInterface` |
371+
| `isTensor` | `Expression & TensorInterface` |
372+
| `isDictionary` | `Expression & DictionaryInterface` |
373+
| `isCollection` | `Expression & CollectionInterface` |
374+
| `isIndexedCollection` | `Expression & IndexedCollectionInterface` |
375+
| `isExpression` | `Expression` (from `unknown`) |
376376

377377
---
378378

@@ -422,7 +422,7 @@ class MyTarget implements LanguageTarget {
422422
getOperators(): CompiledOperators { /* ... */ }
423423
getFunctions(): CompiledFunctions { /* ... */ }
424424
createTarget(options?: Partial<CompileTarget>): CompileTarget { /* ... */ }
425-
compile(expr: BoxedExpression, options?: CompilationOptions): CompilationResult {
425+
compile(expr: Expression, options?: CompilationOptions): CompilationResult {
426426
/* ... */
427427
}
428428
}
@@ -470,12 +470,13 @@ expr.simplify({ rules: otherRules });
470470

471471
## 9. Subpath Exports
472472

473-
MathJSON types can now be imported without pulling in the full engine.
473+
`Expression` now refers to the compute-engine runtime expression type.
474+
The MathJSON type has been renamed to `MathJsonExpression`.
474475

475476
### Before
476477

477478
```ts
478-
// Only one import path — pulls in the entire engine
479+
// MathJSON type (old name)
479480
import { Expression } from '@cortex-js/compute-engine';
480481
```
481482

@@ -484,40 +485,41 @@ import { Expression } from '@cortex-js/compute-engine';
484485
```ts
485486
// Full engine
486487
import { ComputeEngine } from '@cortex-js/compute-engine';
488+
import type { Expression } from '@cortex-js/compute-engine';
487489

488490
// MathJSON types only (lightweight, no engine code)
489-
import { Expression } from '@cortex-js/compute-engine/math-json';
491+
import { MathJsonExpression } from '@cortex-js/compute-engine/math-json';
490492
```
491493

492494
---
493495

494496
## 10. Removed Properties
495497

496-
The following properties have been removed from the `BoxedExpression` base
498+
The following properties have been removed from the `Expression` base
497499
interface. They are now only available on the corresponding role interfaces,
498500
accessed via type guards.
499501

500502
| Removed Property | Type Guard → Interface |
501503
|:----------------------------|:--------------------------------------------------|
502-
| `expr.numericValue` | `isBoxedNumber()``NumberLiteralInterface` |
503-
| `expr.isNumberLiteral` | `isBoxedNumber()``NumberLiteralInterface` |
504-
| `expr.symbol` | `isBoxedSymbol()``SymbolInterface` |
505-
| `expr.string` | `isBoxedString()``StringInterface` |
506-
| `expr.isFunctionExpression` | `isBoxedFunction()``FunctionInterface` |
507-
| `expr.ops` | `isBoxedFunction()``FunctionInterface` |
508-
| `expr.nops` | `isBoxedFunction()``FunctionInterface` |
509-
| `expr.op1` / `op2` / `op3` | `isBoxedFunction()``FunctionInterface` |
510-
| `expr.tensor` | `isBoxedTensor()``TensorInterface` |
504+
| `expr.numericValue` | `isNumber()``NumberLiteralInterface` |
505+
| `expr.isNumberLiteral` | `isNumber()``NumberLiteralInterface` |
506+
| `expr.symbol` | `isSymbol()``SymbolInterface` |
507+
| `expr.string` | `isString()``StringInterface` |
508+
| `expr.isFunctionExpression` | `isFunction()``FunctionInterface` |
509+
| `expr.ops` | `isFunction()``FunctionInterface` |
510+
| `expr.nops` | `isFunction()``FunctionInterface` |
511+
| `expr.op1` / `op2` / `op3` | `isFunction()``FunctionInterface` |
512+
| `expr.tensor` | `isTensor()``TensorInterface` |
511513

512514
Accessing these properties without first narrowing with a type guard is now a
513515
TypeScript compile error.
514516

515517
```ts
516-
// Compile error — .symbol does not exist on BoxedExpression
518+
// Compile error — .symbol does not exist on Expression
517519
console.log(expr.symbol);
518520

519521
// Correct — narrow first, then access
520-
if (isBoxedSymbol(expr)) {
522+
if (isSymbol(expr)) {
521523
console.log(expr.symbol); // string, guaranteed
522524
}
523525
```
@@ -541,13 +543,13 @@ if (expr.symbol !== null) {
541543

542544
**After:**
543545
```ts
544-
import { isBoxedSymbol, isBoxedNumber, isBoxedFunction } from '@cortex-js/compute-engine';
546+
import { isSymbol, isNumber, isFunction } from '@cortex-js/compute-engine';
545547

546-
if (isBoxedSymbol(expr)) {
548+
if (isSymbol(expr)) {
547549
return expr.symbol;
548-
} else if (isBoxedNumber(expr)) {
550+
} else if (isNumber(expr)) {
549551
return expr.numericValue.toString();
550-
} else if (isBoxedFunction(expr)) {
552+
} else if (isFunction(expr)) {
551553
return expr.operator;
552554
}
553555
```
@@ -565,9 +567,9 @@ if (expr.ops) {
565567

566568
**After:**
567569
```ts
568-
import { isBoxedFunction } from '@cortex-js/compute-engine';
570+
import { isFunction } from '@cortex-js/compute-engine';
569571

570-
if (isBoxedFunction(expr)) {
572+
if (isFunction(expr)) {
571573
for (const arg of expr.ops) {
572574
process(arg);
573575
}
@@ -583,9 +585,9 @@ const value = expr.numericValue ?? 0; // Default to 0 if not a number
583585

584586
**After:**
585587
```ts
586-
import { isBoxedNumber } from '@cortex-js/compute-engine';
588+
import { isNumber } from '@cortex-js/compute-engine';
587589

588-
const value = isBoxedNumber(expr) ? expr.numericValue : 0;
590+
const value = isNumber(expr) ? expr.numericValue : 0;
589591
```
590592

591593
### Pattern: Symbol Name Extraction
@@ -611,10 +613,10 @@ const [P, L, U] = luDecomposition.ops; // Unsafe - ops might be null
611613

612614
**After:**
613615
```ts
614-
import { isBoxedFunction } from '@cortex-js/compute-engine';
616+
import { isFunction } from '@cortex-js/compute-engine';
615617

616618
const lu = luDecomposition.evaluate();
617-
if (isBoxedFunction(lu)) {
619+
if (isFunction(lu)) {
618620
const [P, L, U] = lu.ops;
619621
// Safe to use P, L, U here
620622
}
@@ -640,7 +642,7 @@ import {
640642
ComputeEngine,
641643
compile,
642644
expand,
643-
isBoxedFunction,
645+
isFunction,
644646
} from '@cortex-js/compute-engine';
645647

646648
const ce = new ComputeEngine();

0 commit comments

Comments
 (0)