The worker/wrapper transformation is a general-purpose technique for refactoring recursive programs to improve their performance. The two previous approaches to formalising the technique were based upon different recursion operators and different correctness conditions. In this paper we show how these two approaches can be generalised in a uniform manner by combining their correctness conditions, extend the theory with new conditions that are both necessary and sufficient to ensure the correctness of the worker/wrapper technique, and explore the benefits that result. All the proofs have been mechanically verified using the Agda system.
IntroductionA fundamental objective in computer science is the development of programs that are clear, efficient, and correct. However, these aims are often in conflict. In particular, programs that are written for clarity may not be efficient, while programs that are written for efficiency may be difficult to comprehend and contain subtle bugs. One approach to resolving these tensions is to use program transformation techniques to systematically rewrite programs to improve their efficiency, without compromising their correctness.The focus of this paper is the worker/wrapper transformation, a transformation technique for improving the performance of recursive programs by using more efficient intermediate data structures. The basic idea is simple and general: Given a recursive program of some type A, we aim to factorise it into a more efficient worker program of some other type B, together with a wrapper function of type B → A that allows the new worker to be used in the same context as the original recursive program.Special cases of the worker/wrapper transformation have been used for many years. For example, the technique has played a key role in the Glasgow Haskell Compiler since its inception more than 20 years ago, to replace the use of boxed data structures by more efficient unboxed data structures (Peyton Jones & Launchbury 1991). However, it is only recently -in two articles that lay the foundations for the present work (Gill & Hutton 2009;Hutton et al. 2010) -that the worker/wrapper transformation has been formalised, and considered as a general approach to program optimisation.The original formalisation (Gill & Hutton 2009) was based upon a least-fixed-point semantics of recursive programs. Within this setting the worker/wrapper transformation 114 N. Sculthorpe and G. Hutton was explained and formalised, proved correct, and a range of programming applications presented. Using fixed points allowed the worker/wrapper transformation to be formalised, but did not take advantage of the additional structure that is present in many recursive programs. To this end, a more structured approach (Hutton et al. 2010) was then developed based upon initial-algebra semantics, a categorical approach to recursion that is widely used in program optimisation (Bird & de Moor 1997). More specifically, a worker/wrapper theory was developed for programs defined using fold operators, whic...