Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions src/compute-engine/boxed-expression/rules.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Expression } from '../../math-json/types';

import { _BoxedExpression } from './abstract-boxed-expression';
import type {
BoxedRule,
BoxedRuleSet,
Expand Down Expand Up @@ -353,7 +354,10 @@ function parseRulePart(
);
return expr;
}
return ce.box(rule, { canonical: options?.canonical ?? false });
const canonical =
options?.canonical ??
(rule instanceof _BoxedExpression ? rule.isCanonical : false);
return ce.box(rule, { canonical });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think that this slightly refined check is a bit more correct ?

}

/** A rule can be expressed as a string of the form
Expand Down Expand Up @@ -689,8 +693,7 @@ export function applyRule(
substitution: BoxedSubstitution,
options?: Readonly<Partial<ReplaceOptions>>
): RuleStep | null {
const canonical =
options?.canonical ?? (expr.isCanonical || expr.isStructural);
let canonical = options?.canonical ?? (expr.isCanonical || expr.isStructural);

let operandsMatched = false;

Expand All @@ -705,8 +708,18 @@ export function applyRule(

// At least one operand (directly or recursively) matched: but continue onwards to match against
// the top-level expr., test against any 'condition', et cetera.
if (operandsMatched)
if (operandsMatched) {
// If new/replaced operands are all canonical, and options do not explicitly specify canonical
// status, then should be safe to mark as fully-canonical
if (
!canonical &&
options?.canonical === undefined &&
newOps.every((x) => x.isCanonical)
)
canonical = true;

expr = expr.engine.function(expr.operator, newOps, { canonical });
}
}

// eslint-disable-next-line prefer-const
Expand All @@ -715,12 +728,11 @@ export function applyRule(

if (canonical && match) {
const awc = getWildcards(match);
const originalMatch = match;
match = match.canonical;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (line) appeared to be non-effective, its removal not affecting any tests, and so removed it.

const bwc = getWildcards(match);
const canonicalMatch = match.canonical;
const bwc = getWildcards(canonicalMatch);
if (!awc.every((x) => bwc.includes(x)))
throw new Error(
`\n| Invalid rule "${rule.id}"\n| The canonical form of ${dewildcard(originalMatch).toString()} is "${dewildcard(match).toString()}" and it does not contain all the wildcards of the original match.\n| This could indicate that the match expression in canonical form is already simplified and this rule may not be necessary`
`\n| Invalid rule "${rule.id}"\n| The canonical form of ${dewildcard(canonicalMatch).toString()} is "${dewildcard(match).toString()}" and it does not contain all the wildcards of the original match.\n| This could indicate that the match expression in canonical form is already simplified and this rule may not be necessary`
);
}

Expand Down Expand Up @@ -764,6 +776,15 @@ export function applyRule(
}
}

// Have a (direct) match: in this case, consider the canonical-status of the replacement, too.
if (
!canonical &&
options?.canonical === undefined &&
replace instanceof _BoxedExpression &&
replace.isCanonical
)
canonical = true;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(note: this in reference to the added doc. for method replace)


//@note: '.subs()' acts like an expr. 'clone' here (in case of an empty substitution)
const result =
typeof replace === 'function'
Expand All @@ -778,6 +799,8 @@ export function applyRule(
if (isRuleStep(result))
return canonical ? { ...result, value: result.value.canonical } : result;

// (Need to request the canonical variant to account for case of a custom replace: which may not
// have returned canonical.)
return { value: canonical ? result.canonical : result, because };
}

Expand All @@ -786,7 +809,7 @@ export function applyRule(
* and the set of rules that were applied.
*
* The `replace` function can be used to apply a rule to a non-canonical
* expression. @fixme: account for options.canonical
* expression.
*
*/
export function replace(
Expand Down
16 changes: 13 additions & 3 deletions src/compute-engine/global-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ export interface BoxedExpression {
*
* :::info[Note]
* Applicable to canonical and non-canonical expressions.
*
*
* If this is a function, an empty substitution is given, and the computed value of `canonical`
* does not differ from that of this expr.: then a call this method is analagous to requesting a
* *clone*.
Expand Down Expand Up @@ -1107,8 +1107,18 @@ export interface BoxedExpression {
*
* See also `expr.subs()` for a simple substitution of symbols.
*
* If `options.canonical` is not set, the result is canonical if `this`
* is canonical.
* Procedure for the determining the canonical-status of the input expression and replacements:
*
* - If `options.canonical` is set, the *entire expr.* is canonicalized to this degree: whether
* the replacement occurs at the top-level, or within/recursively.
*
* - If otherwise, the *direct replacement will be canonical* if either the 'replaced' expression
* is canonical, or the given replacement (- is a BoxedExpression and -) is canonical.
* Notably also, if this replacement takes place recursively (not at the top-level), then exprs.
* containing the replaced expr. will still however have their (previous) canonical-status
* *preserved*... unless this expr. was previously non-canonical, and *replacements have resulted
* in canonical operands*. In this case, an expr. meeting this criteria will be updated to
* canonical status. (Canonicalization is opportunistic here, in other words).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs your verification and amendment if necessary.
After considering the possibilities here, particularly with recursion, thought that this strategy makes sense.

*
* :::info[Note]
* Applicable to canonical and non-canonical expressions.
Expand Down