Commit Graph

16 Commits

Author SHA1 Message Date
3093ac2ca4 refactor(joinir): Phase 92 P1 - 箱化モジュール化・レガシー削除
P1-1: ConditionalStep lowering を1箱に隔離
- 新規作成: src/mir/join_ir/lowering/common/conditional_step_emitter.rs
  - emit_conditional_step_update() を carrier_update_emitter.rs から移動
  - Fail-Fast 不変条件チェック追加(then_delta != else_delta)
  - 副作用を減らしたクリーンなインターフェース
  - 包括的なテストスイート(3テスト)

P1-0: 境界SSOTの固定
- routing.rs: skeleton 設定をrouting層から削除
- pattern2_with_break.rs: skeleton 取得をlower()内部に閉じ込め
  - parity_checker から skeleton を直接取得
  - skeleton の使用を Pattern2 のみに限定

P1-2: escape recognizer をSSOTに戻す
- escape_pattern_recognizer.rs: 未使用フィールド削除
  - quote_char, escape_char 削除(使われていない)
  - 責務を cond/delta 抽出のみに限定
- pattern_recognizer.rs: デフォルト値を使用

P1-3: E2Eテスト作成(実行は後回し)
- apps/tests/test_pattern5b_escape_minimal.hako 作成
  - body-local 変数対応後に検証予定

テスト結果:
- conditional_step_emitter tests: 3 passed
- Pattern2 tests: 18 passed
- Regression: 0 failures

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-16 15:53:40 +09:00
a718af3213 feat(joinir): Phase 92 P0-3 - ConditionalStep → JoinIR Select generation
Complete implementation of P5b escape sequence handling pattern lowering:

Core changes:
- skeleton_types.rs: Add `cond` field to ConditionalStep for escape condition
- escape_pattern_recognizer.rs: Extract and return escape_cond AST
- pattern_recognizer.rs: Pass escape_cond to ConditionalStep
- canonicalizer.rs: Store escape_cond in ConditionalStep variant

JoinIR lowering:
- carrier_update_emitter.rs: Add emit_conditional_step_update() function
  - Lower condition AST via lower_condition_to_joinir
  - Compute both then/else branches (carrier + delta)
  - Emit JoinInst::Select for conditional carrier update
- loop_with_break_minimal.rs: Add skeleton parameter, detect ConditionalStep
- pattern2_with_break.rs: Pass skeleton to lowering function

Backward compatibility:
- skeleton=None preserves existing Pattern1-4 behavior
- fixtures.rs, tests.rs updated for new signature

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-16 15:36:36 +09:00
6169ae03d6 docs(phase-92): Phase 92 P0-1 - ConditionalStep contract (SSOT) documentation
### Changes

#### 1. skeleton_types.rs - ConditionalStep Contract Documentation
Added comprehensive SSOT contract documentation for `UpdateKind::ConditionalStep`:

**Invariants** (Fail-Fast):
1. Single Update: Updates same carrier exactly once per iteration
2. Constant Deltas: Both then_delta and else_delta are compile-time constants
3. Pure Condition: Escape condition has no side effects
4. No Reassignment: Carrier not reassigned elsewhere in loop body
5. Deterministic: Update path determined solely by escape condition

**Supported Use Cases**:
- Escape sequence handling (e.g., `if ch == '\\' { i += 2 } else { i += 1 }`)
- Conditional skip patterns (e.g., `if skip { pos += len } else { pos += 1 }`)

**Fail-Fast Conditions**:
- Multiple updates → Error
- Non-constant deltas → Error
- Side-effect conditions → Error
- Carrier reassignment → Error

**Lowering Strategy** (Phase 92 P0):
Pattern2Break handles ConditionalStep by generating if-else in JoinIR with PHI merge.

### Context

Phase 92 P0-1 complete: Contract (SSOT) established for ConditionalStep.

Next step: P0-2 - Add ConditionalStep support to Pattern2 lowerer.

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-16 15:07:40 +09:00
570c1f6b73 feat(phase-91): Phase 91 Step 2-B/2-D - P5b escape sequence pattern recognition
### Changes

#### 1. UpdateKind Extension (skeleton_types.rs)
- Added `ConditionalStep { then_delta: i64, else_delta: i64 }` variant
- Enables P5b patterns with conditional numeric deltas
- Used for escape sequence handling (i.e., +2 vs +1 based on escape char)

#### 2. Canonicalizer Integration (canonicalizer.rs)
- Added P5b pattern detection after existing patterns
- Routes to Pattern2Break (same as skip_whitespace, reflects has_break=true)
- Builds LoopSkeleton with ConditionalStep update
- Position: AFTER skip_whitespace (Pattern2Break refinement, not separate choice)

#### 3. AST Recognizer Enhancement (ast_feature_extractor.rs)
- Updated `find_escape_in_if` to handle both:
  - `if ch == '\\' { i += 2 }` (no else)
  - `if ch == '\\' { i += 2 } else { i += 1 }` (with else)
- Added `extract_delta_pair_from_if` for clean delta extraction
- Extracts counter_name, escape_delta, normal_delta from single if-else statement

#### 4. Comprehensive Unit Test (canonicalizer.rs)
- Test: `test_escape_skip_pattern_recognition`
- Verifies full P5b pattern recognition
- Confirms ConditionalStep with escape_delta=2, normal_delta=1
- Validates skeleton structure and exit contract (has_break=true)

### Results

 Build: 1062/1062 tests PASS (+1 new P5b test)
 No regressions
 P5b pattern now recognized and routed correctly

### Next Steps

- Step 2-E: Parity verification with strict mode
- Step 2-F: Documentation updates

Phase 91 P5b implementation on track!

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

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2025-12-16 14:54:41 +09:00
7db554a763 feat(phase-91): Step 2-A完了 - AST recognizer & re-export chain
## Step 2-A: AST Recognizer (detect_escape_skip_pattern)
- 追加ファイル: src/mir/builder/control_flow/joinir/patterns/ast_feature_extractor.rs
  - EscapeSkipPatternInfo 構造体定義
  - detect_escape_skip_pattern() メイン関数 (MVP実装)
  - Helper関数: find_break_in_if, find_escape_in_if, find_normal_increment等

## Step 2-B: Re-export Chain (SSOT準備)
- 5ファイルで re-export を追加:
  1. src/mir/builder/control_flow/joinir/patterns/mod.rs
  2. src/mir/builder/control_flow/joinir/mod.rs
  3. src/mir/builder/control_flow/mod.rs
  4. src/mir/builder.rs
  5. src/mir/mod.rs
  6. src/mir/loop_canonicalizer/pattern_recognizer.rs

## Pattern Recognizer Wrapper
- try_extract_escape_skip_pattern() を pattern_recognizer.rs に追加
- 既存パターン(skip_whitespace等)に倣う設計

## Phase 91 MVP Design
- Quote/escape char は期待値("と\)にハードコード(Phase 91 MVP)
- Normal delta は常に1を期待
- Escape delta は AST から抽出

## Test Results
 cargo build --release: 成功
 cargo test --release --lib: 1061/1061 PASS
- 退行なし

## 次: Step 2-B本体 (Canonicalizer統合)
- canonicalizer.rs に detect_escape_skip_pattern() 統合
- LoopSkeleton & RoutingDecision を構築
- Pattern2Break に寄せる

🤖 Generated with Claude Code

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2025-12-16 14:36:32 +09:00
2674e074b6 feat(joinir): Phase 142 P2 Step 3-A - Pattern4 early return fail-fast 2025-12-16 13:48:30 +09:00
42339ca77f feat(mir): Phase 143 P1 - Add parse_string pattern to canonicalizer
Expand loop canonicalizer to recognize parse_string patterns with both
continue (escape handling) and return (quote found) statements.

## Implementation

### New Pattern Detection (ast_feature_extractor.rs)
- Add `detect_parse_string_pattern()` function
- Support nested continue detection using `has_continue_node()` helper
- Recognize both return and continue in same loop body
- Return ParseStringInfo { carrier_name, delta, body_stmts }
- ~120 lines added

### Canonicalizer Integration (canonicalizer.rs)
- Try parse_string pattern first (most specific)
- Build LoopSkeleton with HeaderCond, Body, Update steps
- Set ExitContract: has_continue=true, has_return=true
- Route to Pattern4Continue (both exits present)
- ~45 lines modified

### Export Chain
- Add re-exports through 7 module levels:
  ast_feature_extractor → patterns → joinir → control_flow → builder → mir
- 10 lines total across 7 files

### Unit Test
- Add `test_parse_string_pattern_recognized()` in canonicalizer.rs
- Verify skeleton structure (3+ steps)
- Verify carrier (name="p", delta=1, role=Counter)
- Verify exit contract (continue=true, return=true, break=false)
- Verify routing decision (Pattern4Continue, no missing_caps)
- ~180 lines added

## Target Pattern
`tools/selfhost/test_pattern4_parse_string.hako`

Pattern structure:
- Check for closing quote → return
- Check for escape sequence → continue (nested inside another if)
- Regular character processing → p++

## Results
-  Strict parity green: Pattern4Continue
-  All 19 unit tests pass
-  Nested continue detection working
-  ExitContract correctly set (first pattern with both continue+return)
-  Default behavior unchanged

## Technical Challenges
1. Nested continue detection required recursive search
2. First pattern with both has_continue=true AND has_return=true
3. Variable step updates (p++ vs p+=2) handled with base delta

## Statistics
- New patterns: 1 (parse_string)
- Total patterns: 4 (skip_whitespace, parse_number, continue, parse_string)
- New capabilities: 0 (uses existing ConstStep)
- Lines added: ~300
- Files modified: 9
- Parity status: Green 

Phase 143 P1: Complete

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-16 12:37:47 +09:00
d605611a16 feat(canonicalizer): Phase 143-P0 - parse_number pattern support
Add parse_number pattern recognition to canonicalizer, expanding adaptation
range for digit collection loops with break in THEN clause.

## Changes

### New Recognizer (ast_feature_extractor.rs)
- `detect_parse_number_pattern()`: Detects `if invalid { break }` pattern
- `ParseNumberInfo`: Struct for extracted pattern info
- ~150 lines added

### Canonicalizer Integration (canonicalizer.rs)
- Parse_number pattern detection before skip_whitespace
- LoopSkeleton construction with 4 steps (Header + Body x2 + Update)
- Routes to Pattern2Break (has_break=true)
- ~60 lines modified

### Export Chain (6 files)
- patterns/mod.rs → joinir/mod.rs → control_flow/mod.rs
- builder.rs → mir/mod.rs
- 8 lines total

### Tests
- `test_parse_number_pattern_recognized()`: Unit test for recognition
- Strict parity verification: GREEN (canonical and router agree)
- ~130 lines added

## Pattern Comparison

| Aspect | Skip Whitespace | Parse Number |
|--------|----------------|--------------|
| Break location | ELSE clause | THEN clause |
| Pattern | `if cond { update } else { break }` | `if invalid { break } rest... update` |
| Body after if | None | Required (result append) |

## Results

-  Skeleton creation successful
-  RoutingDecision matches router (Pattern2Break)
-  Strict parity OK (canonicalizer ↔ router agreement)
-  Unit test PASS
-  Manual test: test_pattern2_parse_number.hako executes correctly

## Statistics

- New patterns: 1 (parse_number)
- Total patterns: 3 (skip_whitespace, parse_number, continue)
- Lines added: ~280
- Files modified: 8
- Parity status: Green 

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-16 09:08:37 +09:00
d0d3512be4 refactor(mir): Phase 140-P4-B - pattern_recognizer を SSOT 化(71 行削減)
- try_extract_skip_whitespace_pattern を ast_feature_extractor 呼び出しに変更
- 重複コード 100+ 行削減(250 → 179 行)
- crate-wide re-export chain 確立(ast_feature_extractor → patterns → joinir → control_flow → builder → mir)
- 全テスト PASS(14 tests)
- フォーマット適用

Phase 140-P4-A/P4-B 完了:SSOT 化成功
2025-12-16 07:09:22 +09:00
e404746612 refactor(mir): Phase 139-P3-B - RoutingDecision を enum 対応 + レガシー削除
- RoutingDecision の missing_caps を Vec<CapabilityTag> に変更(型安全化)
- error_tags は to_tag() メソッドで自動生成
- 全 callsite を enum variant に修正
- capability_tags モジュール(文字列定数群)を完全削除
- 全テスト PASS(型安全性向上を確認)
- フォーマット適用
2025-12-16 07:02:14 +09:00
146f14a019 refactor(mir): Phase 139-P3-A - CapabilityTag enum 定義
- CapabilityTag enum を定義(8 variants)
- to_tag() メソッドでエラータグ文字列に変換
- description() メソッドで人間可読な説明を提供
- legacy capability_tags モジュールを deprecated マーク(P3-B で削除予定)
- 型安全性向上:コンパイル時エラー検出、IDE 支援
2025-12-16 06:58:13 +09:00
5edd81e373 refactor(mir): Phase 138-P1-A - loop_canonicalizer を4モジュール分割
## 概要
- 931行の mod.rs を 4モジュール + 161行 mod.rs に分割
- 全14テスト PASS、退行なし

## モジュール構成
- skeleton_types.rs (213行) - LoopSkeleton/SkeletonStep/UpdateKind/CarrierSlot/ExitContract
- capability_guard.rs (104行) - RoutingDecision/capability_tags
- pattern_recognizer.rs (249行) - try_extract_skip_whitespace_pattern
- canonicalizer.rs (414行) - canonicalize_loop_expr + 統合テスト
- mod.rs (161行) - 型定義と re-export

## ファイルサイズ達成
- 最大ファイル: canonicalizer.rs 414行(目標250行を一部超過するが許容範囲)
- mod.rs: 931行 → 161行 (83%削減)
- 合計: 1141行(元の931行 + tests分離で増加)

## テスト結果
- 14 tests passed
- loop_canonicalizer::* 全テスト green
2025-12-16 06:41:46 +09:00
58f66e3fa2 feat(mir): Phase 137-5 - Decision Policy SSOT化完了
## 目的
Canonicalizer の RoutingDecision.chosen を「lowerer 選択の最終結果」にする
(構造クラス名ではなく ExitContract ベースの決定)

## 実装内容

### 1. Canonicalizer の決定ロジック修正
- `src/mir/loop_canonicalizer/mod.rs`
  - `skip_whitespace` パターン認識で ExitContract (has_break=true) を考慮
  - Pattern3IfPhi → Pattern2Break に修正(構造は似ているが break あり)
  - 単体テスト更新(Pattern2Break 期待に変更)

### 2. Parity 検証テスト修正
- `src/mir/builder/control_flow/joinir/routing.rs`
  - `test_parity_check_mismatch_detected` → `test_parity_check_skip_whitespace_match`
  - Canonicalizer と Router の一致を検証(ミスマッチ検出からマッチ検証へ)
  - Phase 137-5 の SSOT 原則を反映

### 3. ドキュメント更新
- `docs/development/current/main/design/loop-canonicalizer.md`
  - Phase 137-5: Decision Policy SSOT セクション追加
  - ExitContract 優先の原則を明記
  - skip_whitespace の例を追加

- `docs/development/current/main/phases/phase-137/README.md`
  - Phase 4 完了マーク追加
  - Phase 5 完了セクション追加(実装・検証・効果)

## 検証結果
-  単体テスト: `cargo test --release --lib loop_canonicalizer::tests` (11/11 passed)
-  Parity テスト: `cargo test --release --lib 'routing::tests::test_parity'` (2/2 passed)
-  Strict モード: `HAKO_JOINIR_STRICT=1` で skip_whitespace parity OK

## 効果
- Router と Canonicalizer の pattern 選択が一致
- ExitContract が pattern 決定の SSOT として明確化
- 構造的特徴(if-else 等)は notes に記録(将来拡張に備える)

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-16 06:17:03 +09:00
a0009d474d feat(mir): Loop Canonicalizer Phase 3 - skip_whitespace pattern recognition
## Summary
skip_whitespace パターンを Skeleton→Decision で認識可能に。
dev-only 観測で chosen=Pattern3IfPhi / missing_caps=[] を固定。

## Changes
- src/mir/loop_canonicalizer/mod.rs:
  - try_extract_skip_whitespace_pattern() 追加
    - loop(cond) { ... if check { p = p + 1 } else { break } } パターン認識
    - carrier name, delta, body statements を抽出
  - canonicalize_loop_expr() 拡張(skip_whitespace 対応)
    - Pattern3IfPhi 成功時は RoutingDecision::success 返却
    - Skeleton に HeaderCond, Body, Update ステップ追加
    - CarrierSlot に Counter role 設定
    - ExitContract に has_break=true 設定
  - Phase 3 unit tests 追加
    - test_skip_whitespace_pattern_recognition: 基本パターン
    - test_skip_whitespace_with_body_statements: body 付きパターン
    - test_skip_whitespace_fails_without_else: else なし失敗
    - test_skip_whitespace_fails_with_wrong_delta: 減算パターン失敗
  - Phase 2 obsolete tests 削除
- src/mir/builder/control_flow/joinir/routing.rs:
  - Debug 出力拡張(chosen pattern 表示)

## Tests
- cargo test --release --lib loop_canonicalizer::tests: PASS(11 tests)
- cargo test --release --lib: PASS(1044 tests, 退行なし)
- HAKO_JOINIR_DEBUG=1 test_pattern3_skip_whitespace.hako:
  - chosen=Pattern3IfPhi 
  - missing_caps=[] 

## Validation
-  dev-only 観測(HAKO_JOINIR_DEBUG=1)のときだけログ出力
-  フラグ OFF 時は完全不変
-  skip_whitespace パターンで SUCCESS 固定
-  unit tests で全パターン固定

Phase 137-3 complete
2025-12-16 05:38:18 +09:00
e8d93f107c feat(mir): Loop Canonicalizer Phase 2 - dev-only observation
## Summary
ループ入口で LoopSkeleton/RoutingDecision を観測できるようにした。
既定挙動は完全不変(dev-only の並行観測のみ)。

## Changes
- src/mir/loop_canonicalizer/mod.rs: canonicalize_loop_expr() 追加
  - Phase 2 最小実装: loop 構造検証のみ
  - パターン検出は未実装(全て Fail-Fast)
  - LoopSkeleton の基本構造(HeaderCond step)を生成
  - 詳細な Fail-Fast 理由を返す
- src/mir/builder/control_flow/joinir/routing.rs: dev-only 観測ポイント
  - joinir_dev_enabled() ON 時のみ観測ログ出力
  - LoopSkeleton/RoutingDecision の内容を可視化
  - 既存の routing/lowering は完全無変更
- 最小実装: skip_whitespace 相当の構造検証のみ対応
- 追加テスト:
  - test_canonicalize_rejects_non_loop
  - test_canonicalize_minimal_loop_structure
  - test_canonicalize_rejects_multi_statement_loop
  - test_canonicalize_rejects_if_without_else

## Tests
- cargo test --release --lib: 1043 passed (退行なし)
- HAKO_JOINIR_DEBUG=1: 観測ログ出力確認
- デフォルト: 完全無変更(ログも挙動も)

## Acceptance Criteria
 joinir_dev_enabled() ON 時のみ観測ログが出る
 joinir_dev_enabled() OFF 時は完全に無変更(ログも挙動も)
 既存の smoke / cargo test --release --lib が退行しない
 最小パターン(if-else with break)で LoopSkeleton が生成できる
 未対応パターンは Fail-Fast で詳細理由を返す

Phase 137-2 complete
2025-12-16 05:17:56 +09:00
2bdf1abe78 feat(mir): Phase 1 - Loop Canonicalizer type definitions
Implements the foundation for loop canonicalization - a preprocessing
layer that decomposes AST loops into normalized skeleton representation
to prevent combinatorial explosion in pattern detection.

## Implementation (Phase 1)

Created `src/mir/loop_canonicalizer/mod.rs` with:

1. **Core Types**:
   - `LoopSkeleton`: Canonical loop representation
   - `SkeletonStep`: Step kinds (HeaderCond, BreakCheck, ContinueCheck, Update, Body)
   - `UpdateKind`: Carrier update classification (ConstStep, Conditional, Arbitrary)
   - `ExitContract`: Exit presence tracking (break/continue/return)
   - `CarrierSlot`: Loop variables with roles and update rules
   - `CarrierRole`: Semantic roles (Counter, Accumulator, ConditionVar, Derived)
   - `CapturedSlot`: Outer scope variable capture

2. **Capability Guard**:
   - `RoutingDecision`: Pattern selection with diagnostics
   - `capability_tags` module: Standardized Fail-Fast vocabulary
     (CAP_MISSING_CONST_STEP, CAP_MISSING_SINGLE_BREAK, etc.)

3. **Helper Methods**:
   - Skeleton counting (break_checks, continue_checks)
   - Carrier name extraction
   - Exit contract queries
   - Display implementations for debugging

4. **Unit Tests**: 6 tests covering all basic functionality

## Design Principles

- **Input**: AST only (no JoinIR dependencies)
- **Output**: LoopSkeleton only (no BlockId/ValueId)
- **Boundary**: Clear separation from lowering concerns
- **Fail-Fast**: Capability-based rejection with clear reasons

## Next Steps (Not in this commit)

- Phase 2: `LoopCanonicalizer::canonicalize(ast) -> Result<LoopSkeleton>`
- Phase 3: Test with skip_whitespace fixture
- Phase 4: Integration with existing JoinIR lowering

## Acceptance Criteria

 `cargo build --release` succeeds
 `cargo test --release --lib` passes (1039 tests)
 6 new unit tests for loop_canonicalizer pass
 No regressions in existing tests

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

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