一句话:提出 Lower Your Guards 算法,将模式匹配归约为守卫树,简化覆盖检查,并在 GHC 中实现,提升准确性和性能。
编译器应警告模式匹配未覆盖输入(缺失或冗余模式),但现代语言特性繁多,准确生成警告困难,尤其在 Haskell 中,GHC 扩展使模式语言极其复杂,现有覆盖检查器仍会报告不准确警告。本文提出 Lower Your Guards 算法,将模式匹配的复杂性归结为守卫树(guard trees),守卫树只有三种构造,极大简化了覆盖检查过程。该算法模块化,易于扩展支持新源语言模式。作者在 GHC 中实现了该算法,并展示其在准确性和性能上均优于 GHC 当前的覆盖检查器。
A compiler should warn if a function defined by pattern matching does not cover its inputs—that is, if there are missing or redundant patterns. Generating such warnings accurately is difficult for modern languages due to the myriad of language features that interact with pattern matching. This is especially true in Haskell, a language with a complicated pattern language that is made even more complex by extensions offered by the Glasgow Haskell Compiler (GHC). Although GHC has spent a significant amount of effort towards improving its pattern-match coverage warnings, there are still several cases where it reports inaccurate warnings. We introduce a coverage checking algorithm called Lower Your Guards, which boils down the complexities of pattern matching into guard trees . While the source language may have many exotic forms of patterns, guard trees only have three different constructs, which vastly simplifies the coverage checking process. Our algorithm is modular, allowing for new forms of source-language patterns to be handled with little changes to the overall structure of the algorithm. We have implemented the algorithm in GHC and demonstrate places where it performs better than GHC’s current coverage checker, both in accuracy and performance.
@article{GrafJS20,
title = {Lower your guards: a compositional pattern-match coverage checker},
author = {Sebastian Graf and Simon Peyton Jones and Ryan G. Scott},
journal = {Proceedings of the ACM on Programming Languages},
volume = {4},
number = {ICFP},
year = {2020},
doi = {10.1145/3408989}
}