Hostname: page-component-77f85d65b8-pztms Total loading time: 0 Render date: 2026-03-28T17:20:38.319Z Has data issue: false hasContentIssue false

CHAD for expressive total languages

Published online by Cambridge University Press:  14 July 2023

Fernando Lucatelli Nunes
Affiliation:
Department of Information and Computing Sciences, Utrecht University, Utrecht, Netherlands
Matthijs Vákár*
Affiliation:
Department of Information and Computing Sciences, Utrecht University, Utrecht, Netherlands
*
Corresponding author: Matthijs Vákár; Email: matthijsvakar@gmail.com
Rights & Permissions [Opens in a new window]

Abstract

We show how to apply forward and reverse mode Combinatory Homomorphic Automatic Differentiation (CHAD) (Vákár (2021). ESOP, 607–634; Vákár and Smeding (2022). ACM Transactions on Programming Languages and Systems 44 (3) 20:1–20:49.) to total functional programming languages with expressive type systems featuring the combination of

  • tuple types;

  • sum types;

  • inductive types;

  • coinductive types;

  • function types.

We achieve this by analyzing the categorical semantics of such types in $\Sigma$-types (Grothendieck constructions) of suitable categories. Using a novel categorical logical relations technique for such expressive type systems, we give a correctness proof of CHAD in this setting by showing that it computes the usual mathematical derivative of the function that the original program implements. The result is a principled, purely functional and provably correct method for performing forward- and reverse-mode automatic differentiation (AD) on total functional programming languages with expressive type systems.

Information

Type
Special Issue: Differences and Metrics in Programs Semantics: Advances in Quantitative Relational Reasoning
Creative Commons
Creative Common License - CCCreative Common License - BY
This is an Open Access article, distributed under the terms of the Creative Commons Attribution licence (http://creativecommons.org/licenses/by/4.0/), which permits unrestricted re-use, distribution and reproduction, provided the original article is properly cited.
Copyright
© The Author(s), 2023. Published by Cambridge University Press
Figure 0

Figure 1: Grammar for the kinds, types, and terms of the source language for our AD transformations.

Figure 1

Figure 2: Kinding rules for the AD source language. Note that we only consider the formation of function types of nonparameterized types (shaded in gray).

Figure 2

Figure 3: Typing rules for the AD source language.

Figure 3

Figure 4: We consider the standard $\beta\eta$-laws above for our language. We write $\stackrel{\# {x_1,\ldots,x_n}}{=}$ to indicate that the variables $x_1,\ldots,x_n$ need to be fresh in the left-hand side. Equations hold on pairs of terms of the same type. As usual, we only distinguish terms up to $\alpha$-renaming of bound variables.

Figure 4

Figure 5: Functorial action $\Delta,\Delta'\mid\Gamma,x:\tau{}[^{\sigma}\!/\!_{{\alpha}}]\vdash \tau{}[^{x\vdash t}\!/\!_{{\alpha}}] :\tau{}[^{\rho}\!/\!_{{\alpha}}]$ in argument ${\alpha}$ of parameterized types $\Delta,{\alpha}:\mathrm{type}\vdash \tau:\mathrm{type}$ on terms $\Delta'\mid\Gamma,x:\sigma\vdash t:\rho$ of the source language.

Figure 5

Figure 6: A grammar for the kinds, types, and terms of the target language, extending that of Fig. 1.

Figure 6

Figure 7: Kinding rules for the AD target language that we consider on top of those of Fig. 2, where our first rule specifies how kinding judgments of the source language imply kinding of types in the target language. Observe that, according to the second rule, type variables ${\alpha}$ from the kinding context $\Delta $ can be used as a linear type $\underline{\alpha}$. Note that we only consider the formation of $\Sigma$- and $\Pi$-types and linear function types of nonparameterized types (shaded in gray).

Figure 7

Figure 8: Typing rules for the AD target language that we consider on top of the rules of Figs. 3 and 9.

Figure 8

Figure 9: Typing rules for the AD target language that we consider on top of the rules of Figs. 3 and 8.

Figure 9

Figure 10: Equational rules for the idealized, linear AD language, which we use on top of the rules of Fig. 4. In addition to standard $\beta\eta$-rules for ${!(-)\otimes_{(-)}}$- and $\multimap$-types, we add rules making $(\underline{0},+)$ into a commutative monoid on the terms of each linear type as well as rules which say that terms of linear types are homomorphisms in their linear variable. Equations hold on pairs of terms of the same type/types of the same kind. As usual, we only distinguish terms up to $\alpha$-renaming of bound variables.

Figure 10

Figure 11: Functorial action $\Delta,\Delta'\mid\Gamma;\mathsf{v}:\underline{\alpha}{}[^{\underline{\sigma}}\!/\!_{\underline{\alpha}}]\vdash \underline{\alpha}{}[^{\mathsf{v}\vdash t}\!/\!_{\underline{\alpha}}] :\underline{\alpha}{}[^{\underline{\gamma}}\!/\!_{\underline{\alpha}}]$ in argument $\underline{\alpha}$ of parameterized types $\Delta,{\alpha}:\mathrm{type}\mid \Gamma\vdash \underline{\alpha}:\mathrm{ltype}$ on terms $\Delta'\mid\Gamma;\mathsf{v}:\underline{\sigma}\vdash t:\underline{\gamma}$ of the target language.