Macros are a common part of Lisp languages, and one of their most lauded features. Much research has gone into making macros both safer and more powerful resulting in developments in multiple areas, including maintaining hygiene, and typed program staging. However, macros do suffer from various downsides, including being second-class. Particularly egregious for eager functional programming, they are unable to be passed to higher-order functions or freely composed. Fexprs, as reformulated by John Shutt, provide a first-class and more powerful alternative to macros that meshes well with pure functional programming. Unfortunately, naive execution of fexprs is much slower than macros due to re-executing unoptimized operative combiner code at runtime that, in a macro-based language, would have been expanded and then optimized at compile time. To show that fexprs can be practical replacements for macros, we formulate a small purely functional fexpr based Lisp, Kraken, with an online partial evaluation and compilation framework that supports first-class, partially-static-data environments and can completely optimize away fexprs that are used and written in the style of macros. We show our partial evaluation and compilation framework produces code that is more than 70,000 times faster than naive interpretation due to the elimination of repeated work and exposure of static information enabling additional optimization. In addition, our Kraken compiler performs better compared to existing interpreted languages that support fexprs, including improving on NewLisp's fexpr performance by 233x on one benchmark.
翻译:宏是Lisp语言的常见组成部分,也是最受赞誉的特性之一。大量研究致力于使宏更安全、更强大,催生了多个领域的进展,包括保持卫生性以及类型化程序阶段划分。然而,宏存在各种缺点,包括作为二等公民。对于急切函数式编程而言尤为严重的是,宏无法传递给高阶函数或自由组合。John Shutt重新定义的fexpr提供了一等化且更强大的宏替代方案,与纯函数式编程完美契合。不幸的是,fexpr的朴素执行速度远慢于宏,因为在基于宏的语言中,原本可在编译时展开并优化的操作符组合子代码,在运行时需重新执行未经优化的代码。为证明fexpr可实用替代宏,我们基于fexpr构建了小型纯函数式Lisp语言Kraken,其在线部分求值与编译框架支持一等化的部分静态数据环境,并能完全优化掉以宏风格使用和编写的fexpr。实验表明,由于消除了重复计算并暴露静态信息以实现额外优化,我们的部分求值与编译框架生成的代码比朴素解释执行快超过70,000倍。此外,与支持fexpr的现有解释型语言相比,Kraken编译器的性能更优,包括在某一基准测试中将NewLisp的fexpr性能提升233倍。