一句话:提出结合 lens 与 traversal 的泛型派生库,可自动生成高效的数据结构查询与修改遍历。
函数式编程中,traversal 与 lens 是处理递归数据结构的常用模式。已有许多库能根据数据类型的泛型结构自动派生 traversal,但如何在保持表达力的同时生成高效代码仍具挑战。本文结合 lens 与 traversal,提出一种泛型派生方法:利用数据类型的泛型形状生成通用抽象,再通过编译器内联与特化将其消除。由此得到的库在查询和修改复杂数据结构方面具有前所未有的表达力与灵活性,且生成的代码性能可与手写优化版本相当。
Functional programmers have an established tradition of using traversals as a design pattern to work with recursive data structures. The technique is so prolific that a whole host of libraries have been designed to help in the task of automatically providing traversals by analysing the generic structure of data types. More recently, lenses have entered the functional scene and have proved themselves to be a simple and versatile mechanism for working with product types. They make it easy to focus on the salient parts of a data structure in a composable and reusable manner. This paper uses the combination of lenses and traversals to give rise to a library with unprecedented expressivity and flexibility for querying and modifying complex data structures. Furthermore, since lenses and traversals are based on the generic shape of data, this information is used to generate code that is as efficient as hand-optimised versions. The technique leverages the structure of data to produce generic abstractions that are then eliminated by the standard workhorses of modern functional compilers: inlining and specialisation.
@article{paperbot271,
title = {Generic deriving of generic traversals},
author = {Csongor Kiss and Matthew Pickering and Nicolas Wu},
journal = {Proceedings of the ACM on Programming Languages},
volume = {2},
number = {ICFP},
year = {2018},
doi = {10.1145/3236780}
}