feat(phase-91): JoinIR Selfhost depth-2 advancement - Pattern P5b design & planning

## Overview
Analyzed 34 loops across selfhost codebase to identify JoinIR coverage gaps.
Current readiness: 47% (16/30 loops). Next frontier: Pattern P5b (Escape Handling).

## Current Status
- Phase 91 planning document: Complete
  - Loop inventory across 6 key files
  - Priority ranking: P5b (escape) > P5 (guard) > P6 (nested)
  - Effort estimates and ROI analysis

- Pattern P5b Design: Complete
  - Problem statement (variable-step carriers)
  - Pattern definition with Skeleton layout
  - Recognition algorithm (8-step detection)
  - Capability taxonomy (P5b-specific guards)
  - Lowering strategy (Phase 92 preview)

- Test fixture: Created
  - Minimal escape sequence parser
  - JSON string with backslash escape

- Loop Canonicalizer extended
  - Capability table updated with P5b entries
  - Fail-Fast criteria documented
  - Implementation checklist added

## Key Findings

### Loop Readiness Matrix
| Category | Count | JoinIR Status |
|----------|-------|--------------|
| Pattern 1 (simple bounded) | 16 |  Ready |
| Pattern 2 (with break) | 1 | ⚠️ Partial |
| **Pattern P5b (escape seq)** | ~3 |  NEW |
| Pattern P5 (guard-bounded) | ~2 |  Deferred |
| Pattern P6 (nested loops) | ~8 |  Deferred |

### Top Candidates
1. **P5b**: json_loader.hako:30 (8 lines, high reuse)
   - Effort: 2-3 days (recognition)
   - Impact: Unlocks all escape parsers

2. **P5**: mini_vm_core.hako:541 (204 lines, monolithic)
   - Effort: 1-2 weeks
   - Impact: Major JSON optimization

3. **P6**: seam_inspector.hako:76 (7+ nesting)
   - Effort: 2-3 weeks
   - Impact: Demonstrates nested composition

## Phase 91 Strategy
**Recognition-only phase** (no lowering in P1):
- Step 1: Design & planning 
- Step 2: Canonicalizer implementation (detect_escape_pattern)
- Step 3: Unit tests + parity verification
- Step 4: Lowering deferred to Phase 92

## Files Added
- docs/development/current/main/phases/phase-91/README.md - Full analysis & planning
- docs/development/current/main/design/pattern-p5b-escape-design.md - Technical design
- tools/selfhost/test_pattern5b_escape_minimal.hako - Test fixture

## Files Modified
- docs/development/current/main/design/loop-canonicalizer.md
  - Capability table extended with P5b entries
  - Pattern P5b full section added
  - Implementation checklist updated

## Acceptance Criteria (Phase 91 Step 1)
-  Loop inventory complete (34 loops across 6 files)
-  Pattern P5b design document ready
-  Test fixture created
-  Capability taxonomy extended
-  Implementation deferred (Step 2+)

## References
- JoinIR Architecture: joinir-architecture-overview.md
- Phase 91 Plan: phases/phase-91/README.md
- P5b Design: design/pattern-p5b-escape-design.md

Next: Implement detect_escape_pattern() recognition in Phase 91 Step 2

🤖 Generated with Claude Code

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-12-16 14:22:36 +09:00
parent 26077186aa
commit 9e3b258046
4 changed files with 1157 additions and 11 deletions

View File

@ -295,16 +295,26 @@ pub enum CarrierRole {
Skeleton を生成できても lower/merge できるとは限らない。以下の Capability で判定する:
| Capability | 説明 | 未達時の理由タグ |
|--------------------------|------------------------------------------|-------------------------------------|
| `ConstStepIncrement` | キャリア更新が定数ステップi=i+const | `CAP_MISSING_CONST_STEP` |
| `SingleBreakPoint` | break が単一箇所のみ | `CAP_MISSING_SINGLE_BREAK` |
| `SingleContinuePoint` | continue が単一箇所のみ | `CAP_MISSING_SINGLE_CONTINUE` |
| `NoSideEffectInHeader` | ループ条件に副作用がない | `CAP_MISSING_PURE_HEADER` |
| `OuterLocalCondition` | 条件変数が外側スコープで定義済み | `CAP_MISSING_OUTER_LOCAL_COND` |
| `ExitBindingsComplete` | 境界へ渡す値が過不足ない | `CAP_MISSING_EXIT_BINDINGS` |
| `CarrierPromotion` | LoopBodyLocal を昇格可能 | `CAP_MISSING_CARRIER_PROMOTION` |
| `BreakValueConsistent` | break 値の型が一貫 | `CAP_MISSING_BREAK_VALUE_TYPE` |
| Capability | 説明 | 未達時の理由タグ | Pattern対応 |
|--------------------------|------------------------------------------|-------------------------------------|------------|
| `ConstStepIncrement` | キャリア更新が定数ステップi=i+const | `CAP_MISSING_CONST_STEP` | P1-P5 |
| `SingleBreakPoint` | break が単一箇所のみ | `CAP_MISSING_SINGLE_BREAK` | P1-P5 |
| `SingleContinuePoint` | continue が単一箇所のみ | `CAP_MISSING_SINGLE_CONTINUE` | P4 |
| `NoSideEffectInHeader` | ループ条件に副作用がない | `CAP_MISSING_PURE_HEADER` | P1-P5 |
| `OuterLocalCondition` | 条件変数が外側スコープで定義済み | `CAP_MISSING_OUTER_LOCAL_COND` | P1-P5 |
| `ExitBindingsComplete` | 境界へ渡す値が過不足ない | `CAP_MISSING_EXIT_BINDINGS` | P1-P5 |
| `CarrierPromotion` | LoopBodyLocal を昇格可能 | `CAP_MISSING_CARRIER_PROMOTION` | P2-P3 |
| `BreakValueConsistent` | break 値の型が一貫 | `CAP_MISSING_BREAK_VALUE_TYPE` | P2-P5 |
| `EscapeSequencePattern` | エスケープシーケンス対応P5b専用 | `CAP_MISSING_ESCAPE_PATTERN` | **P5b** |
**新規 P5b 関連 Capability**:
| Capability | 説明 | 必須条件 |
|--------------------------|------------------------------------------|---------------------------------------|
| `ConstEscapeDelta` | escape_delta が定数 | `if ch == "\\" { i = i + const }` |
| `ConstNormalDelta` | normal_delta が定数 | `i = i + const` (after escape block) |
| `SingleEscapeCheck` | escape check が単一箇所のみ | 複数の escape 処理がない |
| `ClearBoundaryCondition` | 文字列終端検出が明確 | `if ch == boundary { break }` |
### 語彙の安定性
@ -365,7 +375,7 @@ pub struct RoutingDecision {
---
## 最初の対象ループ: skip_whitespace受け入れ基準
## 対象ループ 1: skip_whitespace受け入れ基準
### 対象ファイル
@ -410,6 +420,120 @@ loop(p < len) {
---
## 対象ループ 2: Pattern P5b - Escape Sequence HandlingPhase 91 新規)
### 目的
エスケープシーケンス対応ループを JoinIR 対象に拡大する。JSON/CSV パーサーの文字列処理で共通パターン。
### 対象ファイル
`tools/selfhost/test_pattern5b_escape_minimal.hako`
```hako
loop(i < n) {
local ch = s.substring(i, i+1)
if ch == "\"" { break } // String boundary
if ch == "\\" {
i = i + 1 // Skip escape character (conditional +2 total)
ch = s.substring(i, i+1) // Read escaped character
}
out = out + ch // Process character
i = i + 1 // Standard increment
}
```
### Pattern P5b の特徴
| 特性 | 説明 |
|-----|------|
| **Header** | `loop(i < n)` - Bounded loop on string length |
| **Escape Check** | `if ch == escape_char { i = i + escape_delta }` |
| **Normal Increment** | `i = i + 1` (always +1) |
| **Accumulator** | `out = out + char` - String append pattern |
| **Boundary** | `if ch == boundary { break }` - String terminator |
| **Carriers** | Position (`i`), Accumulator (`out`) |
| **Deltas** | normal_delta=1, escape_delta=2 (or variable) |
### 必要 Capability (P5b 拡張)
-`ConstStepIncrement` (normal: i = i + 1)
-`SingleBreakPoint` (boundary check only)
-`NoSideEffectInHeader` (i < n pure)
- `OuterLocalCondition` (i, n は外側定義)
- **`ConstEscapeDelta`** (escape: i = i + 2, etc.) - **P5b 専用**
- **`SingleEscapeCheck`** (one escape pattern only) - **P5b 専用**
- **`ClearBoundaryCondition`** (explicit boundary detection) - **P5b 専用**
### Fail-Fast 基準 (P5b 非対応のケース)
以下のいずれかに該当する場合Fail-Fast:
1. **複数エスケープチェック**: `if ch == "\\" ... if ch2 == "'" ...`
- 理由: `CAP_MISSING_SINGLE_ESCAPE_CHECK`
2. **可変ステップ**: `i = i + var` (定数でない)
- 理由: `CAP_MISSING_CONST_ESCAPE_DELTA`
3. **無条件に近いループ**: `loop(true)` without clear boundary
- 理由: `CAP_MISSING_CLEAR_BOUNDARY_CONDITION`
4. **複数 break 点**: String boundary + escape processing exit
- 理由: `CAP_MISSING_SINGLE_BREAK`
### 認識アルゴリズム (高レベル)
```
1. Header carrier 抽出: loop(i < n) から i を取得
2. Escape check block 発見: if ch == "\" { ... }
3. Escape delta 抽出: i = i + const
4. Accumulator パターン発見: out = out + ch
5. Normal increment 抽出: i = i + 1 (escape if block 外)
6. Boundary check 発見: if ch == "\"" { break }
7. LoopSkeleton 構築
- carriers: [i (dual deltas), out (append)]
- exits: has_break=true
8. RoutingDecision: Pattern5bEscape
```
### 実装予定 (Phase 91)
**Step 1** (このドキュメント):
- [ ] Pattern P5b 設計書完成
- [ ] テストフィクスチャ作成
- [ ] Capability 定義追加
**Step 2** (Phase 91 本実装):
- [ ] `detect_escape_pattern()` in Canonicalizer
- [ ] Unit tests (P5b recognition)
- [ ] Parity verification (strict mode)
- [ ] Documentation update
**Step 3** (Phase 92 lowering):
- [ ] Pattern5bEscape lowerer 実装
- [ ] E2E test with escape fixture
- [ ] VM/LLVM parity verification
### 受け入れ基準 (Phase 91)
1. Canonicalizer escape pattern を認識
2. RoutingDecision.chosen == Pattern5bEscape
3. missing_caps == [] (すべての capability 満たす)
4. Strict parity green (`HAKO_JOINIR_STRICT=1`)
5. 既存テスト退行なし
6. Lowering Step 3 (Phase 91 では recognition のみ)
### References
- **P5b 詳細設計**: `docs/development/current/main/design/pattern-p5b-escape-design.md`
- **テストフィクスチャ**: `tools/selfhost/test_pattern5b_escape_minimal.hako`
- **Phase 91 計画**: `docs/development/current/main/phases/phase-91/README.md`
---
## 追加・変更チェックリスト
- [ ] 追加するループ形を最小 fixture に落とす再現固定