Monadic Fusion of Functional Programs (original) (raw)
2003, Fixed Points in Computer Science
Recently, higher-rank datatypes have drawn interest in the functional pro grainming community [0ka99,0ka96,HinOl]. Rank-2 non-regular types, so-called nested datatypes, have been investigated in the context of Haskell. To define total functions which traverse nested datastructures, Bird et al. [B P99] have developed generalized folds which implement an iteration scheme and are strong enough to encode most of the known algorithms for nested datatypes. In this note, we in vestigate a scheme to overcome some limitations of iteration which we expound in the following. Since the work of Böhm et al. [BB85] it is well-known that iteration for rank-1 datatypes can be simulated in typed lambda-calculi. The easiest examples are iterative definitions of addition and multiplication for Church numerals. The iterative definition of the predecessor, however, is inefficient: It traverses the whole numeral in order to remove one constructor. Surely, taking the predecessor should run in constant time. Primitive recursion is the combination of iteration and efficient predecessor. A typical example for a prim. rec. algorithm is the natural definition of the factorial function. It is common belief that prim. rec. cannot be reduced to it eration in a computationally faithful manner. This is because no encoding of natural numbers in the polymorphic lambda-calculus (System F) seems possible which supports a constant-time predecessor operation (see Splawski and Urzy czyn [SU99]). Mendler extended System F by a scheme of prim. rec. for rank-i datatypes and proved strong normalization [Men87]. Mendler's formulation does not follow the usual category-theoretic approach with initial recursive algebras (see Geuvers [Geu92]). For rank-2 datatypes there are also examples of functions which can most naturally be implemented with prim. rec. One is redecoration for triangular mati-ices which is presented below. These examples are not instances of generalized folds a Ia Bird et al., which remain within the realm of iteration but hardwire Kan extensions into the recursion scheme. Rank-2 prim. rec., which we propose in this work, seeks to extend rank-2 iteration in the same way that prim. rec. extends rank-i iteration. We achieve this by lifting Mendler's scheme of prim. rec. to rank 2. The decision for Mendler-style and against the traditional way roots in the following observation: Experiments with formulations according to the traditional style showed unnecessary but unavoidable traversals of the whole data structures in our examples. Mendler's style, however, yielded precisely the The first author is supported by the GraduiertenkoUeg "Logik in der Informatik" of the Deutsche Forschungsgemeinschaft.
Sign up for access to the world's latest research.
checkGet notified about relevant papers
checkSave papers to use in your research
checkJoin the discussion with peers
checkTrack your impact