Skip to content

Instantly share code, notes, and snippets.

@LCamel
Created March 20, 2026 02:01
Show Gist options
  • Select an option

  • Save LCamel/a1294485b1ea3589b00cb76264063ddd to your computer and use it in GitHub Desktop.

Select an option

Save LCamel/a1294485b1ea3589b00cb76264063ddd to your computer and use it in GitHub Desktop.
orphan
● 分析完成。252 個 orphan instances 分佈在 28 個檔案,可以分成三大類:
---
結論:大部分 orphan 是架構上難以避免的
1. 結構上必要的 orphan(~100+ 個)— 不建議動
Pretty / Render instances 是最大宗。原因是經典的循環依賴問題:
Syntax/Abstract/Types.hs 定義 Expr, Stmt, Type ...
Pretty/Abstract.hs 定義 instance Pretty Expr ...
Render/Syntax/Abstract.hs 定義 instance Render Expr ...
- 把 Pretty Expr 搬到 Types.hs → Types.hs 要 import Pretty 相關模組 → 循環依賴
- 把 Pretty Expr 搬到 Pretty/Util.hs(class 定義處)→ 那裡要 import 所有 Syntax types → 變成 God module
這是標準的 表現層分離 設計,orphan 是正確的做法。
涉及的檔案:Pretty/*.hs、Render/**/*.hs、Syntax/Concrete/Instances/ToAbstract.hs
2. 可以搬移的 orphan(~60 個)— 可以改但效益不大
┌────────────────────────────────────────┬───────────────────────┬──────────────────────────┐
│ 檔案 │ Instance 類型 │ 可搬到哪裡 │
├────────────────────────────────────────┼───────────────────────┼──────────────────────────┤
│ Syntax/Common/Instances/Located.hs │ MaybeRanged Name 等 │ Syntax/Common/Types.hs │
├────────────────────────────────────────┼───────────────────────┼──────────────────────────┤
│ Syntax/Common/Instances/Json.hs │ ToJSON Name 等 │ Syntax/Common/Types.hs │
├────────────────────────────────────────┼───────────────────────┼──────────────────────────┤
│ Syntax/Abstract/Instances/Located.hs │ MaybeRanged Expr 等 │ Syntax/Abstract/Types.hs │
├────────────────────────────────────────┼───────────────────────┼──────────────────────────┤
│ Syntax/Abstract/Instances/Json.hs │ ToJSON Expr 等 │ Syntax/Abstract/Types.hs │
├────────────────────────────────────────┼───────────────────────┼──────────────────────────┤
│ Syntax/Typed/Instances/Located.hs │ MaybeRanged for typed │ Syntax/Typed/Types.hs │
├────────────────────────────────────────┼───────────────────────┼──────────────────────────┤
│ Syntax/Typed/Instances/Free.hs │ Free for typed types │ Syntax/Typed/Types.hs │
├────────────────────────────────────────┼───────────────────────┼──────────────────────────┤
│ Syntax/Typed/Instances/Substitution.hs │ Substitutable │ Syntax/Typed/Types.hs │
└────────────────────────────────────────┴───────────────────────┴──────────────────────────┘
這些的 class(MaybeRanged, ToJSON, Free, Substitutable)和 type 都在專案內,理論上可以搬到 Types.hs 裡。但代價是:
- Types.hs 會變臃腫,要多 import aeson, GCL.Range, GCL.Common 等
- 目前刻意用 Instances/ 子目錄做分離,是一種有意識的組織方式
3. 少數零散的 orphan(~5 個)— 最容易修
┌──────────────────────────────────────────┬────────────────────────────────────────────┬───────────────────────────────────────────┐
│ 位置 │ Instance │ 狀況 │
├──────────────────────────────────────────┼────────────────────────────────────────────┼───────────────────────────────────────────┤
│ GCL/Type2/Infer.hs:43 │ Show Env(OVERLAPPING) │ type alias 的 Show,可搬到 Type2/Types.hs │
├──────────────────────────────────────────┼────────────────────────────────────────────┼───────────────────────────────────────────┤
│ GCL/Type2/Subst.hs:16,19 │ Semigroup Subst, Show Subst(OVERLAPPING) │ 同上 │
├──────────────────────────────────────────┼────────────────────────────────────────────┼───────────────────────────────────────────┤
│ GCL/WP/Util.hs:154 │ Fresh WP │ 可搬到 WP monad 定義處 │
├──────────────────────────────────────────┼────────────────────────────────────────────┼───────────────────────────────────────────┤
│ Server/Highlighting.hs, Server/Refine.hs │ 零散 instance │ 可個別評估 │
└──────────────────────────────────────────┴────────────────────────────────────────────┴───────────────────────────────────────────┘
---
建議
這個專案的 orphan 主要來自刻意的架構分離(Pretty/Render vs Types),不是疏忽。維持現狀 + 保留 -fno-warn-orphans 是合理的。如果要改善:
1. 不動 Pretty/Render 相關的 orphan(架構正確)
2. 可選 把 Instances/Located.hs 和 Instances/Json.hs 搬進對應的 Types.hs(減少約 60 個 orphan,但會讓 Types module 變大)
3. 容易修 的是 Type2/ 下那幾個 OVERLAPPING 的 orphan,數量少且風險低
✻ Worked for 5m 6s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment