We introduce Combinatory Homomorphic Automatic Differentiation (CHAD), a principled, pure, provably correct define-then-run method for performing forward- and reverse-mode automatic differentiation (AD) on programming languages with expressive features. It implements AD as a compositional, type-respecting source-code transformation that generates purely functional code. This code transformation is principled in the sense that it is the unique homomorphic (structure preserving) extension to expressive languages of the well-known and unambiguous definitions of AD for a first-order functional language. Correctness of the method follows by a (compositional) logical relations argument that shows that the semantics of the syntactic derivative is the usual calculus derivative of the semantics of the original program. In their most elegant formulation, the transformations generate code with linear types. However, the code transformations can be implemented in a standard functional language lacking linear types: while the correctness proof requires tracking of linearity, the actual transformations do not. In fact, even in a standard functional language, we can get all of the type-safety that linear types give us: we can implement all linear types used to type the transformations as abstract types, by using a basic module system. In this paper, we detail the method when applied to a simple higher-order language for manipulating statically sized arrays. However, we explain how the methodology applies, more generally, to functional languages with other expressive features. Finally, we discuss how the scope of CHAD extends beyond applications in AD to other dynamic program analyses that accumulate data in a commutative monoid.
翻译:我们引入了混合自定义自动差异( CHAD), 这是一种原则性、 纯度、 正确、 定义和运行正确的方法, 用于在有表达功能的编程语言上进行前向和反向模式自动区别( AD) 。 它将AD 作为一种组成性、 尊重类型源代码的转换, 生成纯功能代码。 这种代码转换具有原则性, 因为它是一种独特的同义( 结构保存) 扩展, 以表达为一阶功能语言的已知和明确的 AD 定义。 方法的正确性遵循一种( 组合性) 逻辑关系参数, 表明超正义性组合衍生物的语义分析是原始程序语义的通常缩放。 在最优的配方中, 转换产生代码的代码类型是线性代码。 然而, 代码转换可以在缺乏线性类型的标准功能语言中实施: 虽然正确性证据需要跟踪直线性, 实际变不。 事实上, 即使在一种标准的功能性语言中, 我们可以得到直线性类型的所有类型安全性分析, 直线性类型分析, 最终地向我们解释: 我们运用了一种直线语言, 将这一系统应用了一种更高级的方法, 。 然而, 我们使用所有直线性格式, 我们使用了一种直线性格式, 将使用了一种直线性格式, 我们使用了一种直线性格式, 我们用的方法可以应用了一种方法, 。