一句话:利用依赖类型将指针地址操作封装为纯接口,使函数式程序保持引用透明并获得图级遍历性能。
纯函数式语言适合构建自动推理系统,但项遍历耗时按语法树大小而非共享图大小增长;在 Lean、Coq 等交互式定理证明器的自动化中,这种指数级膨胀尤为严重。要恢复最优复杂度,只需对项的内存地址做简单操作,但直接暴露这些操作会破坏引用透明。本文提出用依赖类型将指针地址操作封装在纯函数接口之后,仅在信任上付出可忽略的代价。作者在 Lean v4 中实现了该方案,并指出其他基于依赖类型理论的语言也可采用。
Functional programming languages are particularly well-suited for building automated reasoning systems, since (among other reasons) a logical term is well modeled by an inductive type, traversing a term can be implemented generically as a higher-order combinator, and backtracking search is dramatically simplified by persistent datastructures. However, existing pure functional programming languages all suffer a major limitation in these domains: traversing a term requires time proportional to the tree size of the term as opposed to its graph size. This limitation would be particularly devastating when building automation for interactive theorem provers such as Lean and Coq, for which the exponential blowup of term-tree sizes has proved to be both common and difficult to prevent. All that is needed to recover the optimal scaling is the ability to perform simple operations on the memory addresses of terms, and yet allowing these operations to be used freely would clearly violate the basic premise of referential transparency. We show how to use dependent types to seal the necessary pointer-address manipulations behind pure functional interfaces while requiring only a negligible amount of additional trust. We have implemented our approach for the upcoming version (v4) of Lean, and our approach could be adopted by other languages based on dependent type theory as well.
DOI 原文 · arXiv · PDF(开放获取) · DBLP
@article{abs-2003-01685,
title = {Sealing pointer-based optimizations behind pure functions},
author = {Daniel Selsam and Simon Hudon and Leonardo de Moura},
journal = {Proceedings of the ACM on Programming Languages},
volume = {4},
number = {ICFP},
year = {2020},
doi = {10.1145/3408997}
}