Using monads to fuse recursive programs Claus J¨ urgensen 1 Claus.Juergensen@Inf.TU-Dresden.DE Faculty of Computer Science Dresden University of Technology D-01062 Dresden, Germany – extended abstract — We try to combine the ‘syntactic composition of tree transducers’ [FV98, KV01] on the one hand side and ‘short cut fusion’ [GLP93] on the other hand side. Short cut fusion is based on the ‘cata/build-rule’ [Joh01] or ‘acid rain theo- rem’ [TM95]. Therefore it is necessary to represent the recursive functions as cata- morphisms. A catamorphism is a generalization of the well known list-function foldr for arbitrary algebraic datatypes. In terms of category theory a catamor- phism is the unique mediating morphism from an initial algebra. We invented a new fusion technique using monads: instead of a catamorphism we use the unique mediating morphism from a free monad. Consider the small Haskell program: data Nat = Zero | Succ Nat data Bool = True | False even Zero = True even(Succ n)= odd n odd Zero = False odd(Succ n) = even n The latter four equations define the two mutually recursive functions even and odd. We can view this system of equations as a function which maps the left-hand-side- expressions onto the according right-hand-side-expressions: X : Q(ΣX) T Δ (QX) where X = {n} is the set of variables. The endofunctors Σ, Δ, and Q describe the application of ranked symbols from {Zero (0) , Succ (1) }, {True (0) , False (0) }, and {even (1) , odd (1) }, respectively, to a set (e.g. ΣX = {Zero}∪{Succ x|x X}). The functor T Δ constructs all Δ-trees over a set: T Δ X = X + Δ(T Δ X). It is easy to show that the function X is natural in X and thus we have a natural transformation: : Q · Σ . T Δ · Q which we call the rule of the functional program. It is now possible (using some category theory magic like adjoint functors) to equivalently transform this rule into the form: : Σ . |HT Δ | 1 Supported by the postgraduate program ‘Specification of discrete processes and systems of processes by operational models and logics’ (GRK 334/2) of the German Research Community (DFG) 1