一键导入
closure-converter
Transforms closures to explicit environments. Use when: (1) Implementing functional languages, (2) Building compilers, (3) Understanding closures.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Transforms closures to explicit environments. Use when: (1) Implementing functional languages, (2) Building compilers, (3) Understanding closures.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Implement abstract machines for defining and executing operational semantics of programming languages.
Implements alias and points-to analysis for pointer programs. Use for: (1) Optimizing compilers, (2) Verifying memory safety, (3) Program understanding, (4) Parallelization.
Define program meaning through logical assertions and proof rules (Hoare logic).
Implements common subexpression elimination (CSE). Use when: (1) Building compilers, (2) Optimizing code, (3) Program analysis.
Implements general dataflow analysis framework. Use when: (1) Building compilers, (2) Static analysis tools, (3) Program verification.
Builds denotational semantic models. Use when: (1) Formalizing language semantics, (2) Proving program properties, (3) Semantic analysis.
| name | closure-converter |
| description | Transforms closures to explicit environments. Use when: (1) Implementing functional languages, (2) Building compilers, (3) Understanding closures. |
| version | 1.0.0 |
| tags | ["compiler","closures","code-generation","functional"] |
| difficulty | intermediate |
| languages | ["python","ocaml","rust"] |
| dependencies | ["lambda-calculus-interpreter"] |
Transforms closures to explicit environment passing.
| Concept | Description |
|---|---|
| Free variables | Variables used but not defined |
| Closure | Function + captured environment |
| Environment | Mapping from variables to values |
| Lambda lifting | Move functions to top level |
lambda-calculus-interpreter - Lambda calculusjit-compiler-builder - VM designlambda-calculus-interpreter - Bytecode| Reference | Why It Matters |
|---|---|
| Reynolds, "Definitional Interpreters for Higher-Order Functions" (1972) | Closure conversion theory |
| Plotkin, "Call-by-Name, Call-by-Value and the λ-Calculus" (1975) | Lambda calculus foundations |
| Fisher & Springer, "Lambda-Liftable Modules" (2000) | Lambda lifting and closure conversion |
Real-world closure conversion implementations to study:
| Artifact | Why It Matters |
|---|---|
| GHC's closure analysis | Sophisticated info table and closure representation |
| OCaml's closure conversion | Production-quality, optimized for performance |
| Scala 2 lambda encoding | Anonymous function representation |
| LuaJIT's closure IR | Trace-based closure optimization |
| PyPy's closure conversion | Tracing JIT with efficient closures |
| V8's closure representation | Hidden class and context optimization |
Current active research in closure conversion:
| Direction | Key Papers | Challenge |
|---|---|---|
| Zero-cost closures | "Zero-cost Closures" (2018) | No heap allocation for non-escaping |
| In-place updates | "Thunks revisited" (2008) | Update-in-place vs copying |
| Unboxed representations | "Unboxed tuples" (GHC) | Avoiding heap allocation |
| Escape analysis | "Escape Analysis" (1994) | Static lifetime detection |
| Lambda lifting | "Lambda dropping" (2001) | When to lift, what to capture |
Common bugs in production closure conversion:
| Pitfall | Real Example | Prevention |
|---|---|---|
| Wrong free variable set | Early Scala lost captured vars | Compute FV with bound variable hygiene |
| Space leaks | GHC's CAFs holding closures | Analyze thunk vs value carefully |
| Lambda lift too early | Lost sharing opportunities | Lift after inlining |
| Wrong environment order | Variable capture bugs | Verify env layout matches usage |
| Closure escape | JavaScript hidden class changes | Track escape precisely |
| Mutable capture | Closure capturing mutref unsoundness | Track mutability separately |
In languages with mutability:
// Unsound if closure captures mutable ref
val x = Ref(0)
val f = () => { x := 1; x() }
Solution: Mark closures that capture mutable references, restrict their usage.
Lambda lifting too early loses optimization opportunities: