Programs for Cheap!

Write down the definition of a recursion operator on a piece of paper. Tell me its type, but be careful not to let me see the operator's definition. I will tell you an optimization theorem that the operator satisfies. As an added bonus, I will also give you a proof of correctness for the optimisation, along with a formal guarantee about its effect on performance. The purpose of this paper is to explain these tricks.


I. INTRODUCTION
At the risk of seeming off-message for a logic conference, modern programmers are too busy to bother proving theorems about their programs.Free theorems [1] provide a handy shortcut, as they allow us to conclude useful properties of functional programs without any proof whatsoever, provided the programs are suitably polymorphic.In this paper we apply this technique to the area of program optimisation, developing a generic optimisation pattern for recursive programs that can be instantiated for a wide variety of operators.
The focus of this paper is on a particular technique for optimising recursive programs: the worker/wrapper transformation [2].This is a simple but general transformation based around a change of data representation.The transformation replaces a program that uses some type A with the combination of a worker program that uses a new type B and a wrapper program that converts between the two types as necessary.The idea behind the transformation is that the change of type allows the use of more efficient operations.
The worker/wrapper transformation was originally formulated and proved correct for recursive programs defined in terms of least fixed points [2].It has subsequently also been developed for programs written as folds [3] and unfolds [4], and some progress has been made towards unifying the various different approaches [5].Most recently, in the context of least fixed points it has been shown how rigorous arguments can be made about the effect of the transformation on a program's efficiency, based on improvement theory [6].
Thus far, all instances of the worker/wrapper transformation have centered on an application of a rolling or fusion rule, properties that allow functions to be moved into and out of a recursive context.Variants of these rules exist for a wide class of recursion operators, so this seems a natural starting point for developing a generic theory.As it turns out, the appropriate generalisations of rolling and fusion rules are the categorical notions of weak and strong dinaturality.
Dinaturality arises in category theory as a generalisation of the notion of natural transformations, families of morphisms with a commutativity property.For example, the natural transformation reverse : [A] → [A] that reverses the order of a list satisfies the property reverse • map f = map f • reverse, where map applies a function to each element of a list.In a simple categorical semantics where objects correspond to types and arrows correspond to functions, natural transformations correspond to the familiar notion of parametric polymorphism, and the commutativity properties arise as free theorems for the types of polymorphic functions.
However, as a model of polymorphism, natural transformations have a significant limitation: their source and target must be functors.This means that polymorphic functions where the type variable appears negatively in either the source or target, for example fix : (A → A) → A, cannot be defined as natural transformations.For this reason, the concept of naturality is sometimes generalised to dinaturality and strong dinaturality.To put it in categorical terms, dinatural transformations generalise natural transformations to the case where the source and target of the transformation may have mixed variance.
It is widely known that there is a relationship between (strong) dinaturality and parametricity, the property from which free theorems follow [7], [8].The exact details of this relationship are unclear, and the situation is not helped by the wide variety of models of parametricity that have been developed.However, it is known that parametricity for certain types entails strong dinaturality [9].For the purposes of this paper, we assume that all recursion operators of interest are strongly dinatural; in practice, we are not aware of any such operators in common use where this assumption fails.
Our paper has two main contributions.First of all, we develop a generic version of the worker/wrapper transformation, applicable to a wide class of recursion operators, with a correctness theorem based around the categorical notion of strong dinaturality.Secondly, we provide an equally general improvement theorem for the transformation, based around a modified notion of dinaturality we call "lax strong dinaturality".This theorem allows us to make formal guarantees about the performance effects of the worker/wrapper transformation when applied to the same wide class of operators.This guarantee can be phrased informally as "the worker/wrapper transformation never makes programs worse".
In this way, we establish strong dinaturality as the essence of the worker/wrapper transformation, and obtain a general theory that is applicable to a wide class of recursion operators.Furthermore, the efficiency side of the theory suggests a general categorical viewpoint on theories of program improvement in terms of preorder-enriched categories.This echoes earlier work by Hoare and others on a similar approach to program refinement [10], [11].Finally, we observe that not only do all existing worker/wrapper theories arise as instances of our new general theory, but the theory can also be used to derive new instances, including a theory for monadic fixed-points and an interesting degenerate case for arrow fixed-points.

II. WORKER/WRAPPER FOR LEAST FIXED POINTS
In this section, we present a version of the worker/wrapper transformation for the particular case of recursive programs defined as least fixed points, i.e. using general recursion, together with proofs of correctness.We then discuss the limitations of this presentation of the technique, and the problem of generalising to a wider range of recursion operators.

A. Review Of The Theory
The general form of the worker/wrapper transformation is based on a simple change of type.Given two types, A and B , along with functions abs :B → A and rep :A → B , we attempt to factorise an original program that uses type A into a worker that uses the new type B and a wrapper that performs the necessary change of data representation.The wrapper allows the new worker program to be used in the same contexts as the original program.In order for this to be possible, we require the additional assumption that abs • rep = id A , essentially capturing the idea that rep provides a faithful representation of elements of A in B .The worker/wrapper transformation can be presented with weaker forms of this assumption, but for now we only consider this strong formulation.
In the particular case for programs defined as least fixed points, we have the original program written as a fixed point of a function f : A → A, and wish to derive a new function g : B → B , such that the following equation holds: In this case, fix f is the original program of type A, while abs is the wrapper and fix g is the worker of type B .
Our use of least fixed points means that we must choose an appropriate semantic basis where this notion makes sense; the traditional choice is the category Cpo of complete pointed partial orders and continuous functions.Our theory will also make use of the following two rules.Firstly, the rolling rule, allowing functions inside a fixed point to be "rolled" out: Secondly, the fusion rule, which assuming the function h is strict (i.e.h ⊥ = ⊥) can be stated as follows: The original paper [2] provided the following proof of correctness for the worker/wrapper transformation: This proof gives us a direct definition for the new function g, to which standard techniques can then be used to 'fuse together' the functions in the definition for g to give a more efficient implementation for the worker program fix g.
A later paper based on folds [3] gave a proof of correctness for the worker/wrapper transformation based on fusion.Adapting this proof to the fix case, we obtain: This proof gives a specification for the new function g in terms of the given functions rep and f , from which the aim is then to calculate an implementation.It also appears as a subproof of the complete proof for the worker/wrapper transformation for fixed points that is presented in [5].Both of the above proofs essentially have only one nontrivial step.In the first proof, this is the application of the rolling rule.In the second proof, it is the use of fusion.
We illustrate this theory with a simple example.Consider the naïve polymorphic function reverse : [T ] → [T ] that reverses a list of elements of type T , defined by: This can be written as a least fixed point as follows: To obtain a more efficient version of reverse using the worker/wrapper transformation, we shall apply the idea of difference lists, lists represented by functions on lists.In particular, we take Secondly, for a non-empty list: Note that the calculation for g did not require the use of induction.Expanding out the resulting new definition reverse = abs (fix g), we obtain the familiar "fast reverse", where fix g is replaced by the equivalent recursive definition rev .Thus we see that the transformation from naïve to fast reverse is an instance of the worker/wrapper transformation.

B. Generalising From The Fix Case
There are several reasons why we would like to generalise the least fixed point presentation to a wider range of settings.Firstly, the full power of the fixed-point operator fix is not always available to the programmer.This is becoming increasingly the case as the popularity of dependently typed languages such as Agda and Coq increases, as these languages tend to have totality requirements that preclude the use of general recursion.Secondly, the general recursion that is provided by the use of fixed-points is unstructured, and other recursion operators such as folds and unfolds can be significantly easier to reason with in practice.Finally, the least fixed points presentation is tied to the framework of complete pointed partial orders, preventing us from applying the theory to languages where this semantic model does not apply.
Sculthorpe and Hutton [5] made some way toward generalising the worker/wrapper transformation, giving a uniform presentation of various worker/wrapper theories.However, this is somewhat unsatisfactory, as it does little to explain why such a uniform presentation is possible, only demonstrating that it is.Nevertheless, this work provided a vital stepping-stone toward the general theory we present in this paper.
In the previous subsection, we noted that both proofs center on an application of either the rolling rule or fusion.For this reason, we believe it is appropriate to view these rules as the "essence" of the worker/wrapper transformation.Thus, to generalise the worker/wrapper transformation, the first step is to generalise these rules.In this case, the appropriate generalisation of the rolling rule is the category-theoretic notion of dinaturality.The fusion rule can be similarly generalised to the notion of strong dinaturality.

III. DINATURALITY AND STRONG DINATURALITY
Now we shall explain the concepts of dinaturality and strong dinaturality, including their relationship with the rolling rule and fusion.For this section, we assume a small amount of knowledge of category theory, up to functors.
Firstly, we present the notion of a natural transformation.

For two functors F, G : C → D between categories C and D, a family of arrows α
This diagram is a coherence property, essentially requiring that each of the α A "do the same thing", independent of the choice of the particular A. In this way, natural transformations provide a categorical notion of parametric polymorphism.
However, some polymorphic operators, such as fix : (A → A) → A cannot be expressed as natural transformations.This is because natural transformations require both their source and target to be functors, whereas in the case of fix the source type A → A is not functorial because A appears in a negative position.It is natural to ask whether there is a categorical notion that captures these operators as well, where the source and target may not be functors.The notion of dinaturality was developed for precisely such cases.
For two functors F, G : For fix , this property exactly captures the rolling rule.To see this, take C = Cpo and D = Set (the category of sets and total functions), let F (X , Y ) = Hom (X , Y ) and G (X , Y ) = Y , and assume we are given a (continuous) function f ∈ F (B , A) = B → A. Then chasing the function f around the above diagram, we obtain the rolling rule: Note that G (h, id B ) expands simply to id B because G ignores its contravariant argument.We can use this diagram-chasing technique to obtain rolling rules for other recursion operators such as fold and unfold .Thus we see that dinaturality can be considered a generalisation of the rolling rule.
For some purposes, however, the notion of dinaturality is too weak.For example, the composition of two dinatural transformations is not necessarily dinatural.For this reason, the stronger property of strong dinaturality is sometimes used.This property is captured by the following diagram, which should be read as "if the diamond on the left commutes, then the outer hexagon commutes".

G (B , B )
G (h,idB ) ; ; If we set X = F (B , A), p = F (h, A) and q = F (B , h), then the diamond on the left commutes trivially and the outer hexagon reduces to ordinary dinaturality.Thus we confirm that strong dinaturality is indeed a stronger property.
Applying strong dinaturality to fix in a similar manner to above, we see that it corresponds to a fusion rule.Choosing some x :X and letting p x = f :A → A and q x = g :B → B , we chase values around the diagram as before: Thus we see that while dinaturality is a generalisation of the rolling rule, strong dinaturality is a generalisation of fusion.Because rolling and fusion rules are the essence of the worker/wrapper transformation, it makes sense to use (strong) dinaturality as the basis for developing a generalised theory.We develop such a theory in the next section.
IV. WORKER/WRAPPER FOR STRONG DINATURALS Suppose we have chosen a particular programming language to work with, and let C be the category where the objects are types in that language and the arrows are functions from one type to another.Then a polymorphic type ∀x .T where x appears in both positive and negative positions in T can be represented by a functor F : C op × C → Set, where F (A, A) is the set of terms in the language of type T [A/x].In turn, a recursion operator that takes terms of type F (A, A) and produces terms of type G (A, A) can be represented by a strong dinatural transformation from F to G. It is known that for certain types, strong dinaturality will follow from a free theorem [9].For example, the free theorem for the typing fix : (A → A) → A is fusion, which we showed in the previous section to be equivalent to strong dinaturality.Now we present the first of the two central results of this paper, in the form of a general categorical worker/wrapper theorem, which is summarised in Fig. 1.The data in the theorem can be interpreted as follows: • The category C is a programming language.
• The objects A and B are types in the language.
• The functors F, G are families of types, i.e. types with a free variable.• The arrows abs and rep are functions in the language.
• The elements f and g are terms in the language.
• The strong dinatural α is a recursion operator.Under these interpretations, we can see that the theorem allows us to factorise an original program written as α A f into a worker program α B g and wrapper function G (rep, abs).
The wealth of conditions in Fig. 1 requires some explanation.Previous worker/wrapper theorems in the literature had varying numbers of possible correctness conditions, ranging from just one in the original theory [2] to a total of five in [5].This variation is a result of the way previous theories were first developed separately and then unified, and all previous conditions are included in some generalised form in our presentation.The nine conditions given in this paper were chosen to best expose the symmetries in the theory.In practical applications, one selects the condition that results in the simplest calculation for the worker program.
The conditions are related in various ways.Firstly, the (2) and (3) groups of conditions are categorically dual.This can be seen by exchanging C for the opposite category C op , and then swapping the roles of abs and rep.Note that the dinatural transformation is still in the same direction.
Secondly, each numeric condition (n) implies the corresponding condition (nβ), which in turn implies (nγ).Thus the γ conditions are the weakest conditions for the theorem.These relationships can be proved as follows: Given: If any one of the following conditions holds: then we have the factorisation: The conditions of the theorem are related as shown in the following diagram: (1) (3)  3) implies (3β) by strong dinaturality.Note that because the target of the functors is Set, strong dinaturality can be written pointwise as 2β) and (3β) can be weakened to their corresponding γ conditions by applying G (rep, abs), G (id , abs) and G (rep, id ) to each side respectively.Thirdly, using the assumption that abs • rep = id , condition (1) implies conditions (2) and (3).The same can be said of the corresponding β conditions.In the first case this can be shown by simply applying F (rep, id ) or F (id , abs) to both sides of condition (1).In the second case, one applies either G (rep, id ) or G (id , abs) to both sides of (1β), and the result then follows from applying dinaturality.
Finally, using abs • rep = id all three γ conditions are equivalent.In fact, the right hand sides are all equal.The proof that (1γ) is equivalent to (2γ) is as follows: The proof for (1γ) and (3γ) is dual.Thus we see that all three are equivalent.The basic relationships between the conditions are summarised in the right-hand side of Fig. 1.
Given these relationships, it suffices to prove the theorem for one of the γ conditions.For example, it can be proved for (2γ) simply by applying the assumption abs • rep = id : We include these trivial conditions as it allows us to break up the proof as a whole into individually trivial steps.Conditions (1), ( 2) and (3) are precisely the three correctness conditions given in the original worker/wrapper theory for fold [3], while the corresponding β and γ conditions are weakenings of those conditions.The β conditions are simply the weakenings obtained by adding a recursive context, while the γ conditions are weakened further so that they are all equivalent, much like the two weakened conditions of Sculthorpe and Hutton [5].However, those two conditions correspond here to the conditions (1β) and (2β), which in this generalised setting are not in general equivalent.
It is also worth noting that only conditions (2) and (3) rely on strong dinaturality, which is necessary for them to imply the β conditions.With all other conditions, the theorem follows from the weaker dinaturality property.
In earlier work, weaker versions of the assumption abs • rep = id were also considered [2], [5].For example, the proof of correctness for the original presentation of the worker/wrapper transformation in terms of least fixed points still holds if the assumption is weakened to abs The lack of weaker alternative assumptions means that our new theory is not a full generalisation of the earlier work.While this is not a significant issue, it is a little unsatisfactory.In our theorem, the assumption abs • rep = id is used four times.For each of those four uses, a different weakening can be made.The four weakened versions are as follows: (C1) α A (F (abs We call these assumptions (Cn), as they are related to the (C) assumptions from the literature.The first two assumptions, (C1) and (C2), are used to prove that (1γ) is equivalent to (2γ) and (3γ) respectively, while (C3) and (C4) are used to prove the result from those two same conditions.As expected from this, (C1) is dual to (C2), and (C3) is dual to (C4).
There is also some duality between (C1) and (C4), and between (C2) and (C3).Strengthening the assumptions by removing the application to f gives us (C1) which case the duality holds exactly.
Despite these relationships, however, we have yet to devise a single equality weaker than abs • rep = id that implies the correctness of the generalised worker/wrapper theorem.We suspect that doing so would require additional assumptions to be made about the strong dinatural transformation α.

V. EXAMPLES
In this section, we demonstrate the generality of our new theory by specialising to four particular dinatural transformations.The first two such specialisations give rise to the worker/wrapper theories for fix and fold as presented in previous papers.The last two specialisations are new.

A. Least Fixed Points
Firstly, we shall consider the least fixed point operator, fix : (A → A) → A. This can be considered a dinatural transformation of type F → G if we take the following definitions for the underlying functors F and G: Recalling the discussion from section III, we note that the functors must be typed F, G : Cpo op ⊥ × Cpo ⊥ → Set in order to obtain the correct strong dinaturality property.
By instantiating the theorem from Fig. 1 for the fix operator, we obtain the following set of preconditions: (1) Note that the functions abs and rep must be strict, because they are arrows in Cpo ⊥ .However, the hom-sets Cpo (A, A) and Cpo (B , B ) are the full function spaces, so their respective elements f and g need not be strict.By instantiating the conclusion of the theorem we obtain the worker/wrapper factorisation fix f = abs (fix g) from [2], [5].
As one would expect from the previous section, the first five of the preconditions correspond to the five conditions given for the general fix theory of Sculthorpe and Hutton [5].However that theory had only one strictness requirement: for condition (2), rep must be strict to imply the conclusion.Here, we require both abs and rep to be strict for all conditions.
We can eliminate most of these strictness conditions using two observations.Firstly, we note that the strictness of abs is guaranteed by the assumption abs • rep = id : Secondly, by examining the proof we can see that the full power of strong dinaturality is only needed for conditions (2) and ( 3), and in all other cases dinaturality suffices.As there are no strictness side conditions for the rolling rule, we can also elide strictness conditions for the normal dinaturality property.As condition (3) relies on strong dinaturality being applied with abs, for which we already have strictness guaranteed, the only strictness condition remaining is the requirement that rep be strict in (2) as in the earlier paper.

B. Folds
Next, we consider the fold operator.For a functor H with an initial algebra μH, the fold operator for the type A takes an arrow of type H A → A and extends it to an arrow of type μH → A. That is, we have the following typing: The type μH can be thought of as a least fixed point of H, and is a canonical solution to the equation μH ∼ = H (μH). Informally, the fold operator reduces a structure of type μH to a single value of type A by recursing on all the subterms, and then assembling the subresults according to f .One of the key properties of the fold operator is the fusion law.Given arrows f : H A → A, g : H B → B , h : A → B , fusion is captured by the following implication: This can be recast into the language of strong dinatural transformations in a straightforward manner.In particular, if we define the functors F, G : then the operator fold is a strong dinatural transformation from F to G. The strong dinaturality property corresponds precisely to the fusion law given above.
Instantiating the worker/wrapper theorem from Fig. 1 in this context gives the following set of preconditions: (1) Instantiating the conclusion gives us fold f = abs • fold g.In this case, the first five preconditions and the conclusion are precisely those given for the fold theory in [5].
We note that it is unnecessary to assume anything about the object μH in this presentation: strong dinaturality is sufficient to get all the necessary properties of the fold operator.We speculate that this may be related to the link between strong dinaturality and initial algebras as observed by Uustalu [8].

C. Monadic Fixed Points
Monads are a mathematical construct commonly used in programming language theory to deal with effects such as state and exceptions [12].Languages like Haskell use monads to embed effectful computations into a pure language.In this context, a value of type M A for some monad M is an effectful computation that produces a result of type A, where the nature of the underlying effect is captured by the monad M.
Formally, a monad is a type constructor M equipped with two operations of the following types: The bind operation is often written infix as > > =.The monad operations must obey the following three monad laws: Given these operations and properties, the type constructor M can be made into a functor by the following definition: Sometimes it is useful to perform recursive computations within a monad.For many effects, the appropriate interpretation of recursion is unclear, as there are multiple plausible implementations with different semantics.Furthermore, while one could define a uniform monadic recursion operator using fix , for most monads this results in nontermination.For these reasons, some monads come equipped with a monadic fix operation mfix : (A → M A) → M A. Monadic fix operations are required to follow a number of laws, but here we concern ourselves only with one such law, which follows from parametricity [13].For any strict function s : A → B and functions f : A → M A, g : B → M B , we have: This property is similar to the fusion property of the ordinary fix operator.In fact, if we define functors F (A, B ) = C (A, M B ) and G (A, B ) = M B , we can see that this property precisely states that mfix is a strong dinatural transformation from F to G. Thus we can instantiate our worker/wrapper theorem for the case of monadic fixed points.The preconditions are listed below.Note that once again we have a strictness side condition on rep, though in this case we cannot eliminate it from conditions as we could before as we lack the necessary non-strict rolling rule property.However, once again we can ignore strictness conditions on abs. (1) Instantiating the conclusion gives the worker/wrapper factorisation mfix f = M abs (mfix g).This theorem is more-orless what one might expect given the similarity between mfix and the normal fix operation, but monadic recursion has not previously been studied in the context of the worker/wrapper transformation and the theorem is entirely new.It is our general theory that allows us to quickly and easily generate a theorem that can now be used to apply the worker/wrapper transformation to programs written using monadic recursion.Note that we used none of the monad operations and rules, relying entirely on the strong dinaturality property of mfix , so our theory requires only that M be a functor to ensure that F and G are truly functorial in both A and B .

D. Arrow Loops
Unfortunately, monads cannot capture all notions of effectful computation we may wish to use.For this reason, we may sometimes choose to use a more general framework such as arrows [14].An arrow is a binary type constructor Arr together with three operations of the following types: The seq operator is typically written infix as ≫.Arrows are required to obey a number of laws, which we shall not list here.However, we do note the associativity law: In general, arrows are a particular form of category, where the objects are the same as the underlying category of the programming language, and Arr A B represents the set of arrows from A to B .The operation arr is thus a functor from the underlying category of the language to the category represented by the arrow structure.
Thus far, arrows have no notion of recursion.However, some arrows provide an extra loop combinator [15]: Intuitively, loop connects one of the outputs of an arrow back into one of its inputs, as in the following picture: Once again, loops are expected to satisfy a number of laws that we shall not list here.It follows from the laws of arrows and loops that if f ≫ second (arr h) = second (arr h) ≫ g then loop f = loop g, implying that loop is a dinatural transformation F → G between the following functors: Therefore, by instantiating our worker/wrapper theorem we can conclude that, given abs and rep such that abs • rep = id and one of the following preconditions: (1) then we can conclude loop f = loop g.
We have again instantiated our general theory to produce a novel worker/wrapper theory with very little effort, allowing the worker/wrapper transformation to be applied to programs written using the arrow loop combinator.Just as was the case for monadic recursion, our theorem is based entirely on the property of strong dinaturality, and thus does not require any of the arrow laws to hold beyond the assumption that the loop operator is strongly dinatural.Note that in this case we have a degenerate form of the conclusion where the wrapper is just the identity, because the functor G ignores its inputs.

VI. WORKER/WRAPPER FOR IMPROVEMENT
Thus far, we have only addressed the problem of proving the generalised worker/wrapper transformation correct.However, any useful optimisation must do more than simply preserve the meaning of a program: the transformed program ought to be in some way better than the original with respect to resource usage.At the very least, it should not be worse.We refer to this as the problem of improvement.
In much work on program optimisation in the context of functional languages, discussion of improvement is limited to empirical measures such as benchmarks [16] and profiling [17].This is because the operational behaviour of functional programs can be hard to predict, especially in call-byneed languages such as Haskell.In essence, empirical methods are used to circumvent the limitations of theory, which in the case of improvement is underdeveloped.
In this section, we develop a more rigorous approach to improvement for the worker/wrapper transformation.Firstly, we discuss improvement theory à la Sands, which formed the basis of the approach we used in our previous paper [6].Secondly, we review the concept of preorder-enriched categories, the theoretical machinery we use to model improvement in a general categorical setting.Thirdly, we discuss the appropriate generalisation of strong dinaturality in this setting.Finally, we present a refined version of the worker/wrapper correctness theorem from Fig. 1 that uses these ideas to formulate an improvement theorem, which can be used to verify efficiency properties of the transformation.

A. Improvement Theory à la Sands
Improvement theory is an approach to reasoning about efficiency based on operational semantics.The general idea is that two terms can be compared by counting the resources each term uses in all possible contexts.Given two terms S and T , if for every context C we know that C[S ] requires no more resources to evaluate than C [T ], we say that S is an improvement of T .This idea can be applied to a wide range of resources, including both time and space usage.
This theory was developed initially by Sands for the particular case of the call-by-name lambda calculus [18].Subsequently, Moran and Sands developed a theory for call-by-need time costs [19], while Gustavsson and Sands developed the corresponding theory for space usage [20], [21].
While improvement theory provides the machinery needed to reason about the behaviour of call-by-need languages that are traditionally considered unpredictable, it is unfortunately limited by being tied to specific operational semantics.While subsequent work by Sands [22] goes some way toward rectifying this, we would like a general, categorical theory compatible with the approach we used earlier in this paper.For this reason, we shall use an alternate approach to reasoning about improvement based on preorder-enriched categories.

B. Improvement Theory Via Preorder-Enriched Categories
Category theory offers us one fundamental way to compare arrows: by asking if they are equal or not.This makes the theory ideal for reasoning about equivalence of programs.However, if we wish to reason about other properties, we require additional structure.For this purpose, we use the machinery of enriched category theory [23].In general, categories can be enriched over a wide variety of structures, but in this case we shall use preorders to enrich our categories.
A preorder-enriched category is a category where each hom-set Hom (A, B ) is equipped with a preorder , and composition is monotonic with respect to this ordering: Functors between preorder-enriched categories are also required to respect the ordering of arrows: As arrows are used to model programs, the use of a preorder structure allows us to make ordering comparisons between programs.Any notion of improvement will lead to an ordering on programs, so this is precisely the machinery we need to make general arguments about improvement; while appeals to a particular semantics are needed to establish an ordering on programs, once such an ordering is in place we can continue reasoning with categorical techniques.Where before we used equational reasoning in our proofs, the preordering allows us to use the technique of inequational reasoning.Any ordinary (locally small) category can be treated as a preorder-enriched category simply by equipping its homsets with the discrete ordering (i.e.f f for all arrows f ).
Thus, any statement true of preorder-enriched categories can be specialised to a statement that is true of any category.The use of preorder-enriched categories to compare programs has previously been considered in the area of program refinement [10], [11].While improvement is the problem of making a program more efficient, refinement is the related problem of making a program more executable, in the sense of transforming a specification into an implementation.Our focus is on improvement, but it is worth noting that all of the theory in this section can be applied equally to refinement.

C. Generalising Strong Dinaturality
To generalise categorical properties to the setting of orderenriched categories, we can use the technique of laxification.Put simply, laxification is the process of replacing equalities with inequalities (or in the case of 2-categories, with 2-cells).By applying laxification to the earlier diagram for strong dinaturality from section III, and drawing the inequalities as a new style of arrow + 3 , we obtain the following diagram for lax strong dinaturality: ; ; Note that F and G are now functors between order-enriched categories that respect the arrow ordering.The diagram expresses the following implication in pictorial form: We also use the term oplax when the ordering is reversed (i.e. using rather than ), and bilax when both lax and oplax properties hold.To rephrase, a bilax strong dinatural transformation must satisfy the above property for both the normal ordering on arrows and the inverse ordering.The choice of which direction is lax and which is oplax is arbitrary.For the purposes of this paper, we choose bilax strong dinaturality as our generalisation of strong dinaturality.
We specifically choose to use bilax strong dinaturality for two reasons.First of all, in our previous paper on improvement [6] we used a fusion theorem for fixed-points that bears a great deal of similarity to bilax strong dinaturality, and its bidirectionality was useful in proving the central theorem of that paper.Secondly, Johann and Voigtländer's technique to generate inequational free theorems from polymorphic types [24] results in precisely this same bidirectionality.

D. Worker/Wrapper Theorem For Improvement
By using bilax strong dinaturality as our generalisation of strong dinaturality, we can adapt the theorem we presented in Fig. 1 to an inequational version.By exchanging Set in the strictness requirements.In turn, instantiating the conclusion of our new theorem gives fix f abs (fix g).The resulting improvement theorem for fix is similar to the version from our previous paper [6], with two differences.Firstly, our new theorem is for arbitrary resource usage in the Cpo setting, whereas the earlier theorem was specific to time performance.Secondly, the earlier theorem had no strictness conditions, whereas the above theorem does.In both cases, these differences are inherited from the fusion theorem or strong dinaturality property of the underlying theory.

F. Remarks
The process of generalising from the correctness theorem of Fig. 1 to the improvement theorem of Fig. 2 was entirely straightforward, our treatment of correctness leading immediately to a related treatment of improvement.This is encouraging, as it helps to justify our choice of machinery.Furthermore, the similarities between the two theorems mean that it should be straightforward to adapt a proof of correctness into a proof of improvement, a benefit this work shares with our previous paper [6].However, our work improves on that previous paper by showing that the correctness theorem can be considered a specialisation of the improvement theorem, which serves as progress toward uniting the two separate branches of correctness and efficiency.We hope that more work will go into unifying these aspects of program optimisation.

VII. CONCLUSION
We began this paper by stating that programmers are too busy to prove theorems, and the goal of this paper has been to reduce the number of theorems a programmer need prove.Our chosen mechanism for doing this was by developing the worker/wrapper transformation, a general-purpose program optimisation technique for recursive programs.
Using the categorical concept of strong dinaturality, we have developed a highly re-usable version of the worker/wrapper transformation whose correctness theorem can be instantiated for a wide class of recursion operators without the need for any proofs.Furthermore, with little extra work, we have expanded this correctness theorem into a theorem that can deal with the other side of optimisation, that of improvement.We also demonstrated the utility of these theorems by instantiating them for a number of recursion patterns, including some for which no worker/wrapper theorem had been developed before.
In keeping with our goal of avoiding proofs, what proofs there were in this paper have all been short and straightforward.This reflects the often-stated property of categorical thinking: "with the right definitions, the proofs are trivial".
There is much precedence for free theorems being used to prove the correctness of optimisations.For example, the correctness of shortcut fusion (also known as foldr /build fusion) relies on a free theorem that comes from the rank-2 type of build [25].Furthermore, Seidel and Voigtländer [26] show that an approach based on free theorems can be used to derive properties relating to efficiency as well.Our paper builds on these approaches by developing a theory that treats both of these aspects of optimisation in a uniform way.We hope that this approach will be built upon in the future.
The inspiration for using strong dinaturality as a generalisation of fusion came from work by Uustalu [8].This idea expands on earlier work where dinaturality was used in its non-strengthened form in axiomatisations of fixed-point operators [27], [28].In that work it served a similar role as in this paper, as an analogue of the rolling rule.
Beyond the technical results of this paper, there are two key ideas we hope readers will take away with them.The first idea is that making the right observations about the deep mathematical structure of a problem or theory can lead to straightforward generalisations.If we strip away all the categorical work, this paper boils down to the observation that the fusion and rolling rules are the essence of the worker/wrapper transformation.All the other work comes from simply trying to generalise these rules as far as they will go.
The second idea that we hope readers will take away is that higher category theory doesn't need to be complicated to be useful.While we have used enriched category theory in this paper to reason about program improvement, we have tried to make these ideas as accessible as possible.Despite this drive toward accessibility and simplicity, the results we have proved with this framework are decidedly non-trivial.In short, a little enriched category theory can go a long way.
There are a number of potential avenues for further work on these ideas.Probably the most straightforward next step would be to instantiate the new theory we have developed for various operators, and investigate the particular properties of each instantiation.This would have two benefits: firstly, it would make it easier for programmers to use this theory for their own applications, and secondly, the particular properties of each instantiation may suggest ways to develop the general theory.In particular, it would be interesting to see what other assumptions, if any, are needed to include weakened versions of the abs • rep = id assumption as seen in earlier papers on the worker/wrapper transformation [2], [5], [6].
Another way to develop this work would be to investigate particular models of program equivalence and efficiency.By developing plausible models where our underlying assumptions hold, we can argue that our assumptions are justified in the general case.In particular, it would be useful to further investigate the relationship between parametricity and bilax strong dinaturality, hopefully developing a notion of parametricity which implies bilax strong dinaturality for all relevant types.If this should turn out to be impossible, we would also like to know why this is the case.
At the moment, in order to verify preconditions for our improvement theorem, one would need to fall back on a pre-existing theory of improvement based on the operational semantics of a programming language.This is undesirable, as it increases the amount of theoretical knowledge and proof skills that a programmer would need to use our theory.We would like to develop a richer theory of bilax strong dinaturality as applied to program improvement, to see if this assumption can be used elsewhere to prove improvement relations.Ultimately, we would like to see a purely categorical theory of improvement, allowing improvement relations to be proved in a purely abstract way without the need to reason at the level of an underlying concrete semantics.
Finally, we would like to investigate the potential for automating the worker/wrapper transformation.By far the biggest hurdle for this would be automating the verification of the preconditions.We believe that the best approach to doing this would be to adapt algorithms designed for higher-order unification [29], the problem of solving equations on lambda terms.It may even be possible to adapt these algorithms to deal with the inequational conditions of our improvement theorem.The HERMIT system [30], [31] has already been used in work on automating worker/wrapper; it may be possible to integrate higher-order unification into HERMIT.
and define abs : B → A and rep : A → B by: abs afunc = λxs → afunc xs [ ] rep rfunc = λxs ys → rfunc xs + + ys It is straightforward to verify that abs • rep = id .Using the fusion-based formulation we take rep • f = g • rep as our specification for the new function g, for which we calculate a definition by case analysis.Firstly, for the empty list: reverse xs = rev xs [ ] rev [ ] ys = ys rev (x : xs) ys = rev xs (x : ys) The fusion rule is precisely this property, with an extra strictness condition on h.This strictness condition can be recovered by treating F and G as functors from the strict subcategory Cpo ⊥ in which all arrows are strict.The functor F (X , Y ) is still defined as the full function space Cpo (X , Y ), including non-strict arrows.