(
ce: ComputeEngine,
e: Entry,
sides: { lhs: MathJSON; rhs: MathJSON },
lhsW: MathJSON,
rhsW: MathJSON,
forcedDirection?: 'lhs-rhs' | 'rhs-lhs'
)
| 766 | * |
| 767 | * The machine policy never emits `'transform'` (override-only). |
| 768 | */ |
| 769 | export function orientEntry( |
| 770 | ce: ComputeEngine, |
| 771 | e: Entry, |
| 772 | sides: { lhs: MathJSON; rhs: MathJSON }, |
| 773 | lhsW: MathJSON, |
| 774 | rhsW: MathJSON, |
| 775 | forcedDirection?: 'lhs-rhs' | 'rhs-lhs' |
| 776 | ): Orientation | { error: 'box-error' | 'unorientable'; detail?: string } { |
| 777 | // -- cost of the variable-named canonical sides |
| 778 | let cLhs: number; |
| 779 | let cRhs: number; |
| 780 | ce.pushScope(); |
| 781 | try { |
| 782 | const types = variableTypes(e); |
| 783 | for (const v of e.variables) { |
| 784 | try { |
| 785 | ce.declare(v, types[v] ?? 'complex'); |
| 786 | } catch { |
| 787 | /* name collides with a CE built-in — tolerate (same as load.ts) */ |
| 788 | } |
| 789 | } |
| 790 | const lhs = ce.expr(sides.lhs as never); |
| 791 | const rhs = ce.expr(sides.rhs as never); |
| 792 | if (!lhs.isValid) |
| 793 | return { error: 'box-error', detail: `invalid lhs: ${lhs.toString()}` }; |
| 794 | if (!rhs.isValid) |
| 795 | return { error: 'box-error', detail: `invalid rhs: ${rhs.toString()}` }; |
| 796 | cLhs = ce.costFunction(lhs); |
| 797 | cRhs = ce.costFunction(rhs); |
| 798 | } catch (err) { |
| 799 | return { error: 'box-error', detail: String((err as Error)?.message).slice(0, 200) }; |
| 800 | } finally { |
| 801 | ce.popScope(); |
| 802 | } |
| 803 | |
| 804 | const wLhs = collectWildcards(lhsW); |
| 805 | const wRhs = collectWildcards(rhsW); |
| 806 | // A direction is viable if the match side is a pattern-viable function |
| 807 | // expression and the replace side introduces no new wildcards. |
| 808 | const viableLR = Array.isArray(lhsW) && isSubset(wRhs, wLhs); |
| 809 | const viableRL = Array.isArray(rhsW) && isSubset(wLhs, wRhs); |
| 810 | |
| 811 | const costs = { lhs: cLhs, rhs: cRhs }; |
| 812 | const LR: Orientation = { |
| 813 | match: lhsW, |
| 814 | replace: rhsW, |
| 815 | direction: 'lhs-rhs', |
| 816 | purpose: 'expand', |
| 817 | costs, |
| 818 | }; |
| 819 | const RL: Orientation = { |
| 820 | match: rhsW, |
| 821 | replace: lhsW, |
| 822 | direction: 'rhs-lhs', |
| 823 | purpose: 'expand', |
| 824 | costs, |
| 825 | }; |
no test coverage detected