Skip to content

Add simplified canonical ordering APIs for FlatExpression#549

Closed
Copilot wants to merge 4 commits intomasterfrom
copilot/explore-canonical-representation-pros-cons
Closed

Add simplified canonical ordering APIs for FlatExpression#549
Copilot wants to merge 4 commits intomasterfrom
copilot/explore-canonical-representation-pros-cons

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 5, 2026

This explores the canonical-representation direction for FlatExpression by adding ordering primitives directly on ExprTree. The implementation now keeps that support intentionally narrow and simple: it detects canonical post-order directly and rebuilds non-canonical trees through the existing expression round-trip path.

  • What this adds

    • Introduces ExprTree.IsInOrder() to detect whether a flat tree is already in canonical post-order.
    • Introduces ExprTree.PutInOrder() to normalize a flat tree into that layout.
  • Canonical layout

    • Uses reachable-node post-order:
      • every child appears before its parent
      • the root becomes the last node
      • unreachable nodes are removed
    • This gives a deterministic layout without attempting to reorder semantic child positions for non-commutative expressions.
  • Simplified normalization

    • PutInOrder() no longer performs manual node-index remapping, sibling rewiring, closure-constant compaction, or metadata reconstruction.
    • Instead, it rebuilds the canonical flat tree through the existing ToExpression() / FromExpression() round-trip, reducing complexity and LOC while preserving the supported behavior.
  • Tests added

    • Covers trees produced from expression conversion that are already canonical.
    • Covers direct helper-built flat expressions that are not canonical, then verifies PutInOrder():
      • removes unreachable helper artifacts
      • produces compact constant indexes
      • preserves round-trip semantics
      • yields the expected node order and child links through explicit index assertions
  • Example

    var fe = expr.ToFlatExpression();
    
    if (!fe.IsInOrder())
        fe.PutInOrder();
    
    var roundtrip = fe.ToLightExpression();

Copilot AI changed the title [WIP] Explore canonical representation pros and cons for FlatExpression Add canonical ordering APIs for FlatExpression and compact unreachable nodes May 5, 2026
Copilot AI requested a review from dadhi May 5, 2026 15:41
Copilot finished work on behalf of dadhi May 5, 2026 15:41
Copy link
Copy Markdown
Owner

@dadhi dadhi left a comment

Choose a reason for hiding this comment

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

I do not like so much increased complexity and number of locs. Can we simplify back to support only constant indexes and not signal indexes. Let drop dymamic index stuff and thoutougly test all add helpers in assert js

Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 6, 2026

I do not like so much increased complexity and number of locs. Can we simplify back to support only constant indexes and not signal indexes. Let drop dymamic index stuff and thoutougly test all add he...

Addressed in fdf7b03. PutInOrder() is simplified to rebuild the canonical tree via the existing ToExpression()/FromExpression() path instead of manual index remapping, and the test now asserts explicit helper-produced indexes before and after normalization.

Copilot AI changed the title Add canonical ordering APIs for FlatExpression and compact unreachable nodes Add simplified canonical ordering APIs for FlatExpression May 6, 2026
Copilot AI requested a review from dadhi May 6, 2026 05:16
Copilot finished work on behalf of dadhi May 6, 2026 05:16
@dadhi
Copy link
Copy Markdown
Owner

dadhi commented May 6, 2026

L

@dadhi dadhi closed this May 6, 2026
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.

FlatExpression: explore canonical representation pros and cons

2 participants