Files
hakorune/src/mir/control_tree/normalized_shadow/mod.rs
tomoaki 6a3b6deb20 feat(anf): Phase 145 P0/P1/P2 - ANF (A-Normal Form) transformation
Implement ANF transformation for impure expressions to fix evaluation order:

Phase 145 P0 (Skeleton):
- Add anf/ module with contract/plan/execute 3-layer separation
- AnfDiagnosticTag, AnfOutOfScopeReason, AnfPlan enums
- Stub execute_box (always returns Ok(None))
- 11 unit tests pass

Phase 145 P1 (Minimal success):
- String.length() whitelist implementation
- BinaryOp + MethodCall pattern: x + s.length() → t = s.length(); result = x + t
- Exit code 12 verification (VM + LLVM EXE)
- 17 unit tests pass

Phase 145 P2 (Generalization):
- Recursive ANF for compound expressions
- Left-to-right, depth-first evaluation order
- Patterns: x + s.length() + z, s1.length() + s2.length()
- ANF strict mode (HAKO_ANF_STRICT=1)
- Diagnostic tags (joinir/anf/*)
- 21 unit tests pass, 0 regression

Also includes Phase 143 P2 (else symmetry) completion.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-19 16:19:49 +09:00

56 lines
2.1 KiB
Rust

//! Phase 121: StepTree → Normalized shadow lowering (dev-only)
//!
//! ## Purpose
//!
//! Establish minimal route from StepTree (structure SSOT) to Normalized form,
//! verifying parity with existing router for if-only patterns.
//!
//! ## Scope
//!
//! - **if-only**: Support if statements without loops
//! - **loop rejection**: Reject loops via capability guard
//!
//! ## Design Rules
//!
//! **Input SSOT**:
//! - `StepTree` + `StepTreeContract` (no facts re-analysis)
//! - Lowering decisions based only on contract information
//!
//! **Output**:
//! - `JoinModule` (Normalized dialect)
//! - Or "Normalized-equivalent intermediate" expressed in existing JoinIR types
//!
//! **Execution conditions**:
//! - dev-only: Only runs when `joinir_dev_enabled()` returns true
//! - strict: Only fail-fast on mismatch when `joinir_strict_enabled()` returns true
//!
//! **Prohibitions**:
//! - No fallback: Shadow conversion failure logs reason in dev-only, fail-fast in strict
//! - No direct env reads (must go through `src/config/env/*`)
//! - No hardcoding (no branching on fixture names or variable names)
pub mod builder;
pub mod contracts;
pub mod normalized_verifier;
pub mod env_layout;
pub mod if_as_last_join_k;
pub mod loop_true_break_once; // Phase 131: loop(true) break-once
pub mod loop_true_if_break_continue; // Phase 143 P0: loop(true) + if + break
pub mod post_if_post_k; // Phase 129-C: post-if with post_k continuation
pub mod legacy;
pub mod dev_pipeline;
pub mod parity_contract;
pub mod available_inputs_collector; // Phase 126: available_inputs SSOT
pub mod exit_reconnector; // Phase 131 P1.5: Direct variable_map reconnection (Option B)
pub mod common; // Phase 138: Common utilities (ReturnValueLowererBox)
pub mod anf; // Phase 145 P0: ANF (A-Normal Form) transformation
pub use builder::StepTreeNormalizedShadowLowererBox;
pub use contracts::{CapabilityCheckResult, UnsupportedCapability};
pub use parity_contract::{MismatchKind, ShadowParityResult};
pub use env_layout::EnvLayout;
pub use exit_reconnector::ExitReconnectorBox; // Phase 131 P1.5
#[cfg(test)]
mod tests; // Phase 143 R0: Separated unit tests