Denotational Correctness of Forward-Mode Automatic Differentiation for Iteration and Recursion
Matthijs Vákár
TL;DR
The paper provides a rigorous semantic foundation for forward- mode automatic differentiation in expressive languages that feature partial constructs, iteration, and recursion. It defines a forward- mode AD macro $\overrightarrow{\mathcal{D}}$ and proves its correctness using a denotational semantics in $\mathbf{Diff}$ augmented with a partiality monad, supplemented by a semantic logical-relations argument via subsconing. To handle recursion, the authors introduce $\omega$-diffeological spaces ($\mathbf{\omega Diff}$) and bilimit-compact expansions, enabling a coherent treatment of both differentiation and recursion; forward AD correctness extends from first- order types to recursive types through $\omega$-ds- based logical relations. The approach yields a canonical, adequacy- grounded semantics aligning with operational semantics and offers a principled path toward robust AD integration in real-world functional languages with recursion and higher- order features. Overall, the work advances the mathematical understanding of AD in partial and recursive language features and lays groundwork for future reverse- mode extensions and practical differentiable programming foundations.
Abstract
We present semantic correctness proofs of forward-mode Automatic Differentiation (AD) for languages with sources of partiality such as partial operations, lazy conditionals on real parameters, iteration, and term and type recursion. We first define an AD macro on a standard call-by-value language with some primitive operations for smooth partial functions and constructs for real conditionals and iteration, as a unique structure preserving macro determined by its action on the primitive operations. We define a semantics for the language in terms of diffeological spaces, where the key idea is to make use of a suitable partiality monad. A semantic logical relations argument, constructed through a subsconing construction over diffeological spaces, yields a correctness proof of the defined AD macro. A key insight is that, to reason about differentiation at sum types, we work with relations which form sheaves. Next, we extend our language with term and type recursion. To model this in our semantics, we introduce a new notion of space, suitable for modeling both recursion and differentiation, by equipping a diffeological space with a compatible $ω$cpo-structure. We demonstrate that our whole development extends to this setting. By making use of a semantic, rather than syntactic, logical relations argument, we circumvent the usual technicalities of logical relations techniques for type recursion.
