Skip to content

ltm: replace string-backed synthetic equations with typed IR #965

Closed

Description

@bpowers

Problem

LTM has a typed causal-access IR on the original model's Expr2 AST, but it still represents generated equations as source-facing datamodel::Equation strings and transforms them by re-parsing Expr0.

The seam is visible in three places:

  • LtmSyntheticVar::equation is a datamodel::Equation, even though these equations are internal compiler artifacts rather than persisted/user-authored equations.
  • ltm_augment.rs has ten Expr0::new call sites for ceteris-paribus wrapping, reducer substitution, row pinning, and related transforms. The resulting AST is generally printed back to text; db/ltm/parse.rs::parse_ltm_equation later wraps that text in a transient datamodel aux and parses it again for compilation. ltm: C-LEARN LTM compile-time dominated by per-fragment fixed overhead (DimensionsContext clone, O(D^2) intern_name, repeated parse) #655 separately measures the repeated parse cost.
  • Access shape is decided twice. db/ltm_ir.rs::{resolve_literal_index,classify_subscript_shape,classify_iterated_dim_shape} classify the model's Expr2 reference sites for causal edges/routing. ltm_augment.rs::{resolve_literal_element_index,classify_expr0_subscript_shape,...} re-derive the same facts from re-parsed Expr0 so the partial builder can identify the occurrence to keep live.

The code documents the invariant directly: the Expr0 and Expr2 siblings "must stay in sync"; disagreement can cause the edge emitter and partial builder to choose different shapes, wrap the intended live reference in PREVIOUS(), and silently zero or corrupt a link score.

Why simplify this

#520 made the element graph, edge-shape query, and link-score routing consume one ClassifiedSite IR. That solved duplication among Expr2 consumers, but the actual equation transformer remains outside that IR and has no stable identity for the classified reference occurrence. It therefore parses printed text and reconstructs shape semantics.

This boundary has repeatedly amplified bugs:

Each local fix can be correct, but the architecture keeps creating the same synchronization burden.

Proposed direction

Introduce one typed LTM transformation/causal-access representation that connects reference classification to equation generation.

A possible shape is:

struct LtmEquation {
    dimensions: LtmDimensions,
    expr: LtmExpr,
}

struct ClassifiedSite {
    id: SiteId,                 // stable occurrence/path in the expression
    access: AccessPattern,      // per-axis iterated/pinned/reduced/dynamic
    routing: SiteRouting,       // direct or through a specific agg
    // existing target-element/reducer metadata as needed
}

The important property is not these exact types. It is that the ceteris-paribus/reducer transforms select occurrences by the same site identity and access metadata that causal-edge emission used, instead of reclassifying a printed Expr0.

Then:

  1. Build the reference-site/access IR once from the parsed/lowered model equation.
  2. Express PREVIOUS wrapping, reducer substitution, pinning, guard forms, and generated link/loop equations as typed AST transforms/builders.
  3. Carry typed synthetic equations through LTM compilation. Serialize only for diagnostics/debugging or at an unavoidable source-format boundary; do not round-trip through datamodel::Variable::Aux merely to compile an internal equation.
  4. Remove the parallel Expr0 literal/subscript classifiers and their manual synchronization contract.

This can be staged: first retain Expr0 as the generated-equation AST but parse the source equation once and attach stable site paths; then teach the fragment compiler to accept typed synthetic equations; finally collapse or remove the Expr0/Expr2 bridge. A dedicated LtmExpr may be cleaner if neither public AST level safely represents generated-only constructs.

Acceptance criteria

  • One representation is authoritative for both causal access/routing and selecting reference occurrences during LTM equation transformation.
  • resolve_literal_element_index / classify_expr0_subscript_shape no longer duplicate the Expr2 classifier, and no correctness comment requires the two implementations to stay synchronized.
  • Internal LtmSyntheticVar equations are stored/compiled as typed expressions; normal operation does not print and re-parse them through datamodel::Equation.
  • Existing scalar, arrayed, mapped-dimension, fixed-index, dynamic-index, and aggregate-node LTM golden/numeric behavior remains unchanged.
  • Tests exercise the shared access/transform semantics directly; parser/printer round-trip tests remain useful defense for genuine text boundaries, not an LTM correctness dependency.

Relationship to existing work

Part of the LTM epic #488.

Activity

  1. added
    engine
    ltm
    on Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    engineltmtech debt

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions