Skip to content

PREVIOUS-specific module handling in LTM compilation cannot be removed yet #370

@bpowers

Description

@bpowers

Description

The prev-init-opcodes implementation plan (Phase 2) assumed that after routing 1-arg PREVIOUS to LoadPrev opcodes, the PREVIOUS-specific code in compile_ltm_equation_fragment() (db_ltm.rs) would be dead code. This is incorrect because LTM link-score equations contain PREVIOUS(TIME) (generated by ltm_augment.rs:543-544), which still requires module expansion.

TIME is transformed to App(UntypedBuiltinFn("time", [])) before the PREVIOUS gate in builtins_visitor.rs, so it routes through module expansion rather than LoadPrev.

Root cause

The PREVIOUS(TIME) pattern is used to detect the first timestep in LTM equations: if (TIME = PREVIOUS(TIME)) then 0 else ....

  • Through module expansion: PREVIOUS(TIME) = TIME at t=0 (stock initial value = input), making the condition true at first timestep.
  • Through LoadPrev: PREVIOUS(TIME) = 0 at t=0 (prev_values initialized to zeros), which would break first-timestep detection and produce incorrect loop scores.

Why it matters

This constraint blocks certain optimizations in the PREVIOUS/INIT single-opcode compilation pathway (#362). The PREVIOUS-specific code cannot be safely removed or fully replaced by LoadPrev opcodes until the first-timestep detection logic in LTM equations is refactored.

Components affected

  • src/simlin-engine/src/db_ltm.rs (compile_ltm_equation_fragment)
  • src/simlin-engine/src/ltm_augment.rs (PREVIOUS(TIME) generation)
  • src/simlin-engine/src/builtins_visitor.rs (PREVIOUS routing decision)

Possible resolution paths

  1. Change LTM equation generation to use TIME = INIT(TIME) instead of TIME = PREVIOUS(TIME) for first-timestep detection, then remove the PREVIOUS module handling code.
  2. Add TIME to the prev_values seeding for LTM contexts specifically, allowing PREVIOUS(TIME) to work correctly through LoadPrev.
  3. Accept the current state as working and correct, and document this constraint.

Context

Identified during design review of issue #362 (Activate PREVIOUS/INIT single-opcode compilation).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions