Files
hakorune/docs/archive/phases/phase-16/implementation-notes.txt

139 lines
4.1 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

================================================================================
Phase 16: 折りたたみ言語 実装メモ
================================================================================
【実装上の重要な判断】
1. セルフホスティング後に実装する理由
- 混ぜると危険(複雑性の爆発)
- 目標のブレを防ぐ
- テストの組み合わせ爆発を避ける
2. Nyashで実装することの意義
- ドッグフーディングの極致
- 15,000行のコードベースなら理解しやすい
- コミュニティが貢献しやすい
================================================================================
実装の技術的詳細
================================================================================
■ SimpleFoldPass最小実装案
```rust
// もしRustで書くなら参考実装
pub struct SimpleFoldPass;
impl SimpleFoldPass {
pub fn run(&mut self, mir: &mut MIRModule) -> bool {
// Array.map().filter().map()だけを検出
// 3連鎖まで
// ArrayBoxのみ対象
// 純関数判定は決め打ち
}
}
```
■ Nyashでの実装イメージ
```nyash
box FoldPass {
init { patterns, purity_db }
detectChains(mir) {
local chains
chains = new ArrayBox()
# BoxCall列を走査して連鎖を検出
for inst in mir.instructions {
if me.isChainStart(inst) {
local chain = me.buildChain(inst)
if chain.length() <= 3 and me.allPure(chain) {
chains.push(chain)
}
}
}
return chains
}
foldChain(chain) {
# 連鎖を1つのfused callに変換
local ops = chain.map(inst => me.extractOp(inst))
return new FusedCall(chain.first().array, ops)
}
}
```
================================================================================
段階的実装計画
================================================================================
Step 1: 基盤整備1週間
- 純度属性の構文設計
- MIRへの属性伝播
- テストフレームワーク
Step 2: Array融合2週間
- map/filter連鎖の検出
- fused call生成
- VMサポート追加
Step 3: 効果測定1週間
- ベンチマーク作成
- メモリプロファイル
- GC圧力測定
Step 4: 拡張1ヶ月
- String操作
- I/Oバッチ化
- ループ最適化
================================================================================
リスクと対策
================================================================================
リスク1: 最適化バグでプログラムが壊れる
対策: trace_hash/heap_hashで常に検証
リスク2: デバッグが困難になる
対策: --unfoldオプションで即座に無効化
リスク3: 性能が逆に悪化する
対策: Fold Budgetで自動OFF
================================================================================
成功の測定基準
================================================================================
1. 定量的指標
- Array操作: 30%以上高速化
- メモリ使用量: 50%削減
- GC頻度: 1/3に削減
2. 定性的指標
- コードの可読性維持
- デバッグ容易性維持
- ユーザーコード変更不要
================================================================================
将来の拡張可能性
================================================================================
- 非同期処理の融合
- GUI構築の最適化
- カスタム融合ルール
- プラグインBox対応
- JIT/AOTとの協調
================================================================================
参考資料
================================================================================
- Haskell: Stream Fusion
- Rust: Iterator Fusion
- Julia: Loop Fusion
- Clojure: Transducers
ただし、Nyashの「Everything is Box」哲学に合わせて
独自のアプローチを取る。
================================================================================