Skip to main content

Kan Extensions for Program Optimisation—Or: Art and Dan Explain an Old Trick

Ralf Hinze

Abstract

Many program optimisations involve transforming a program in direct style to an equivalent program in continuation-passing style. This paper investigates the theoretical underpinnings of this transformation in the categorical setting of monads. We argue that so-called absolute Kan Extensions underlie this program optimisation. It is known that every Kan extension gives rise to a monad, the codensity monad, and furthermore that every monad is isomorphic to a codensity monad. The end formula for Kan extensions then induces an implementation of the monad, which can be seen as the categorical counterpart of continuation-passing style. We show that several optimisations are instances of this scheme: Church representations and implementation of backtracking using success and failure continuations, among others. Furthermore, we develop the calculational properties of Kan extensions, powers and ends. In particular, we propose a two-dimensional notation based on string diagrams that aims to support effective reasoning with Kan extensions.

Affiliation
University of Oxford‚ Computing Laboratory‚ Wolfson Building‚ Parks Road‚ Oxford OX1 3QD‚ England
Book Title
11th International Conference on Mathematics of Program Construction (MPC '12)
Editor
Gibbons‚ Jeremy and Nogueira‚ Pablo
Location
Madrid‚ Spain
Pages
324–362
Publisher
Springer Berlin / Heidelberg
Series
Lecture Notes in Computer Science
Volume
7342
Year
2012