Lune

OOPSLA2026顶会

Class-Dictionary Specialization with Rank-2 Polymorphic Functions

Yong Qi Foo, Michael D. Adams

摘要

Rank-2 polymorphism, when combined with type-class-constrained arguments, enables powerful abstractions and code reuse by allowing functions to accept arguments that are themselves ad-hoc polymorphic. Optimizing compilers like the Glasgow Haskell Compiler (GHC) use techniques like class-dictionary specialization and inlining for optimization. However, these techniques can falter when the rank-2 polymorphic functions are recursive. Specializing the polymorphic arguments of recursive rank-2 polymorphic function applications requires inlining the applied function to expose type and dictionary applications to the arguments, but the compiler’s heuristic-driven inliner is reluctant to inline recursive functions, risking non-termination during compilation. This stalemate causes recursive rank-2 polymorphic functions to remain un-optimized and be left with a runtime penalty. Within the Haskell ecosystem, we identify this stalemate within three widely used libraries: the Scrap Your Boilerplate (SYB) and SYB With Class (SYB3) generic-programming libraries, and a fragment of the lens optics library. In these libraries, the combination of rank-2 polymorphism, type-class constraints and recursion is central to their implementation. In this paper, we present a new optimization technique that breaks this stalemate and enables class-dictionary specialization with recursive rank-2 polymorphic functions. We introduce a partial evaluator that strategically applies the standard transformations of inlining, β -reduction and memoization to applications of rank-2 polymorphic functions that are partially static , i.e., whose arguments have some information known statically. This process exposes applications of its polymorphic arguments onto concrete types and dictionaries, which can be specialized by the standard compiler optimization pipeline. Additionally, we introduce type-constant folding to evaluate run-time type-equality tests statically, further leveraging static type information gained from partial evaluation. We implement our technique as a GHC plugin and demonstrate its effectiveness by resolving the performance bottlenecks in the aforementioned Haskell libraries. On SYB and SYB3 traversals, our technique achieves speedups of 43× on average (up to 155×) and 6.1× on average (up to 9.5×), respectively, matching the performance of their hand-written counterparts. On the fragment of the lens library containing the identified slowdowns, our technique achieves speedups of 1.6× on average (up to 2.1×).

问 Lune

几分钟看清这篇 Paper 的位置,不用再花一下午。

Lune 能读引用它和与它最相关的顶会 Paper,讲清它说了什么、后来怎样、哪里站不住,每一点都注明出自哪篇 Paper。不用硬啃,也不靠猜。

可以从这些问题问起

Lune 可以查阅

摘要 · 相关的 Paper

获取答案

免费开始,无需绑卡

相关 Paper

  1. Lazy Linearity for a Core Functional LanguageRodrigo Mesquita, Bernardo ToninhoPOPL 2026
  2. Grisette: Symbolic Compilation as a Functional Programming LibrarySirui Lu, Rastislav BodíkPOPL 2023 · 10 次引用
  3. Better Defunctionalization through Lambda Set SpecializationWilliam Brandon, Benjamin Driscoll, Frank Dai, Wilson Berkow 等PLDI 2023 · 7 次引用
  4. Recursive Program Synthesis using ParamorphismsQiantan Hong, Alex AikenPLDI 2024 · 7 次引用
  5. Digging for fold: synthesis-aided API discovery for HaskellMichael B. James, Zheng Guo, Ziteng Wang, Shivani Doshi 等OOPSLA 2020 · 19 次引用
黄昏的海面,两侧是细线勾勒的悬崖