🤖 AI Summary
Traditional linear type systems equate resource consumption with syntactic occurrence, making them ill-suited for lazy evaluation—where syntactic presence does not guarantee semantic usage. This work introduces Linear Core, the first statically typed extension of GHC Core supporting linear resource usage guarantees under lazy semantics. Its core innovation is the decoupling of syntactic linearity (governed by typing rules) from semantic linearity (enforced via operational semantics), enabling compiler optimizations—including sharing, inlining, and rewrite rules—to preserve linear constraints safely. Linear Core integrates linear type theory with a lazy operational semantics, is implemented as a GHC plugin, and is formally proven sound with respect to both type safety and linear usage preservation. Empirical evaluation on real-world linear libraries (e.g., linear-base) confirms its practicality and effectiveness.
📝 Abstract
Traditionally, in linearly typed languages, consuming a linear resource is synonymous with its syntactic occurrence in the program. However, under the lens of non-strict evaluation, linearity can be further understood semantically, where a syntactic occurrence of a resource does not necessarily entail using that resource when the program is executed. While this distinction has been largely unexplored, it turns out to be inescapable in Haskell's optimising compiler, which heavily rewrites the source program in ways that break syntactic linearity but preserve the program's semantics. We introduce Linear Core, a novel system which accepts the lazy semantics of linearity statically and is suitable for lazy languages such as the Core intermediate language of the Glasgow Haskell Compiler. We prove that Linear Core is sound, guaranteeing linear resource usage, and that multiple optimising transformations preserve linearity in Linear Core while failing to do so in Core. We have implemented Linear Core as a compiler plugin to validate the system against linearity-heavy libraries, including linear-base.