Skip to content

Unify ConditionalFormula evaluation and add algebraic fusion for chained NaturalArray transforms#88

Merged
DanexCodr merged 9 commits into
mainfrom
copilot/add-array-literal-methods-and-conditional-plan
Apr 12, 2026
Merged

Unify ConditionalFormula evaluation and add algebraic fusion for chained NaturalArray transforms#88
DanexCodr merged 9 commits into
mainfrom
copilot/add-array-literal-methods-and-conditional-plan

Conversation

Copilot AI commented Apr 12, 2026

Copy link
Copy Markdown
Contributor

Implements the ConditionalFormula O(1) step by compiling conditional branches into a single arithmetic expression where purity allows, while preserving fallback semantics for non-encodable cases. It also closes key optimization gaps in simplification and introduces lazy fusion behavior for chained map/filter/zip over NaturalArray views.

  • ConditionalFormula expression unification

    • Converts branch chains into one indicator-weighted expression (c*A + (1-c)*B folded from tail to head).
    • Keeps runtime on the expression path to a single evaluate(...) per index for encodable formulas.
    • Preserves branch-statement fallback when purity/pattern checks fail.
  • Numeric condition layer + constant folding

    • Adds boolean→numeric indicator lowering (and/or/!, comparison forms) with pure-only gating.
    • Adds compile-time boolean folding for constant conditions (including literal comparisons), eliminating dead branches early.
  • Simplification pass expansion

    • Adds structural and algebraic reductions:
      • a-a -> 0
      • a+a -> 2*a
      • (a*b)+(a*c) -> a*(b+c) (common-factor extraction)
      • c*A + (1-c)*A -> A
    • Adds numeric literal folding for +, -, *, and exact integer /.
  • Formula lifecycle in NaturalArray

    • Keeps one active conditional formula by composing new formulas with existing formula state, preserving precedence while avoiding growth of independent conditional entries.
  • Lazy map/filter/zip fusion in LiteralRegistry

    • Composes chained lazy map views (map(...).map(...)) into a single mapper pipeline.
    • Composes chained lazy filter views (filter(...).filter(...)) into a single predicate pipeline.
    • Extends zip source handling to consume lazy map/filter views directly.
  • Coverage updates

    • Extends ConditionalFormulaOptimization.cod with:
      • constant-true / constant-false condition cases
      • chained map/filter fusion cases
      • zip over mapped arrays case
// Unified conditional expression fold (tail -> head)
Expr unified = elseExpr;
for (int i = indicators.size() - 1; i >= 0; i--) {
    Expr c = indicators.get(i);
    Expr a = branches.get(i);
    unified = (c * a) + ((1 - c) * unified);
}

Copilot AI and others added 9 commits April 12, 2026 06:31
Agent-Logs-Url: https://github.com/DanexCodr/Coderive/sessions/d041c95d-62d9-47f6-b0ee-a71fe3d10a62

Co-authored-by: DanexCodr <216312766+DanexCodr@users.noreply.github.com>
Agent-Logs-Url: https://github.com/DanexCodr/Coderive/sessions/d041c95d-62d9-47f6-b0ee-a71fe3d10a62

Co-authored-by: DanexCodr <216312766+DanexCodr@users.noreply.github.com>
Agent-Logs-Url: https://github.com/DanexCodr/Coderive/sessions/d041c95d-62d9-47f6-b0ee-a71fe3d10a62

Co-authored-by: DanexCodr <216312766+DanexCodr@users.noreply.github.com>
Agent-Logs-Url: https://github.com/DanexCodr/Coderive/sessions/40be63ba-f796-4997-9cd7-22680da225c2

Co-authored-by: DanexCodr <216312766+DanexCodr@users.noreply.github.com>
@DanexCodr DanexCodr marked this pull request as ready for review April 12, 2026 10:04
@DanexCodr DanexCodr merged commit fb2bc66 into main Apr 12, 2026
@DanexCodr DanexCodr deleted the copilot/add-array-literal-methods-and-conditional-plan branch April 12, 2026 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants