I wrote a purely functional AD library in Mercury [0], which adapts a general approach from [1]. I believe that Owl provides a similar approach [2].
[0] https://github.com/mclements/mercury-ad
[1] https://github.com/qobi/AD-Rosetta-Stone/
[2] https://github.com/owlbarn/owl/tree/main/src/base/algodiff