Use Style: Paper Title
Total Page:16
File Type:pdf, Size:1020Kb
ICSEA 2014 : The Ninth International Conference on Software Engineering Advances Fundamentals, Prospects and Challenges for Totally Functional Programming Style Paul Bailes, Leighton Brough, Colin Kemp School of Information Technology and Electrical Engineering The University of Queensland, St Lucia, QLD Australia {p.bailes, l.brough, c.kemp}@uq.edu.au Abstract—General recursive definitions contribute to the yield zoetic data; complexity of programming. This complexity could be reduced programming with zoetic data simply involves their by reliance on established, well-understood programming application to appropriate operands (just as with patterns. Catamorphism-based recursion patterns simplify recursion patterns), rather than also having to programming with little practical loss in expressive capability program with explicit recursion the characteristic compared to general recursion, including the capability of behavior of the datatype; defining new recursion patterns. Partial application of creation of zoetic data can be effected by generator catamorphisms, sub-catamorphic recursion patterns and functions which are the derived counterparts of methods to symbolic data allows a comprehensive replacement of symbolic data with functional, or what we describe as symbolic data constructors; “zoetic”, representations that inherently adopt the benefits of this enables a new style of programming (which to catamorphism-based programming. The considerable promise emphasise its distinctiveness from an earlier related of this “Totally Functional” style confronts us with some development of “Total Functional Programming” [1] exciting technical challenges. we call “Totally Functional Programming”, or TFP), in which a comprehensive supercession of symbolic Keywords-component; Catamorphism, Fold, Functional, data by functional representations can be achieved; Recursion. while the comprehensiveness of the foregoing program is unprecedented, important aspects of it are I. INTRODUCTION discernable in (and thus validated by) related fields of computer science. We contend that software is unduly complicated by the The presentation of the argument in this paper follows the pervasive need to program interpreters for the computations above sequence. inherent to symbolic data. By using instead functional The consequent comprehensive replacement of symbolic representations that embody the fusion of characteristic data by functions requires first-class functions, hence we interpretations into data, this pervasive complication can be implicitly adopt functional programming [2] and functional minimized if not avoided, and programming thus languages. We choose Haskell [3] for purposes of significantly simplified. illustration. Our essential argument develops in logical sequence as follows: II. CATAMORPHIC PROGRAMMING recursion patterns such as list “foldr”, which generalize to catamorphisms on regular recursive An approach to programming based entirely on canonical datatypes, suffice to express and simplify a very recursion patterns known as “catamorphisms” [4] is wide range of common recursive definitions; beneficial, viable and self-sufficient. Catamorphisms are more familiar as the list “reduce” or “foldr” functions of other useful and simplifying recursion patterns are functional programming, but apply to all regular recursive also definable in terms of catamorphisms; types. catamorphisms thus embody practically as well as theoretically (in terms of initial algebra semantics) A. General Recursion Too Complex the behaviours characteristic to abstract data types; Recursion patterns simplify and clarify programming, partial application of catamorphisms to typical compared to the use of general iteration/recursion. Consider symbolic representations of data yield functional the case of recursively defining basic arithmetic operations representations that inherently possess these on the simplest recursive datatype, of Natural numbers, in characteristic behaviours, i.e., a kind of liveness Fig. 1. This example exposes some key aspects of Haskell as which we describe as “zoetic” from the Greek follows: “zoion” meaning “animal” (as in “zoology”); declaration of datatypes (e.g., Nat) in terms of partial application of behaviours that are more constructor functions (e.g., Zero and Succ) and their specialized than the generic catamorphism, but are operand types where appropriate (i.e., Nat, thus definable inevitably in terms of catamorphisms, also defining a recursive type); Copyright (c) IARIA, 2014. ISBN: 978-1-61208-367-4 559 ICSEA 2014 : The Ninth International Conference on Software Engineering Advances data Nat = Zero | Succ Nat data List t = Cons t (List t) | Nil add Zero b = b cataL Nil o b = b add (Succ a) b = Succ (add a b) cataL (Cons x xs) o b = o x (cataL xs o b) -- versus mul Zero b = Zero foldr op b [] = b mul (Succ a) b = add b (mul a b) foldr op b (x:xs) = op x (foldr op b xs) exp a (Zero) = Succ Zero Figure 3. Catamorphisms and operations on lists. exp a (Succ b) = mul a (exp a b) Figure 1. General recursive renditions of basic arithmetic operations. sumR Nil = 0 sumR (Cons x xs) = x + sumR xs -- versus cataN Zero f x = x sumC xs = cataL xs (+) 0 cataN (Succ n) f x = f (cataN n f x) appendR Nil ys = ys add a b = cataN a Succ b appendR (Cons x xs) ys = mul a b = cataN a (add b) Zero Cons x (appendR xs ys) exp a b = cataN b (mul a) (Succ Zero) -- versus appendC xs ys = cataL xs Cons ys Figure 2. Catamorphic renditions of basic arithmetic operations. Figure 4. List processing examples. definition of functions by recursion equations; branching by pattern matching on function Observe also that (aside from a reordering of the usual arguments; presentation of operands) cataL is exactly the familiar function application by juxtaposition of operator and “foldr” of functional programming (also known as “reduce”). operand(s). The reader will also observe that just as with Nat above, (Further key novelties of functional languages and Haskell operations on lists may be programmed using the uniform in particular will be explained as introduced in examples interpretation offered by cataL applied to other operations below.) and data pertaining to the specific applications. See Fig. 4 for In this general recursive rendition of arithmetic a comparison of explicit recursive vs. catamorphic operations, the following deficiencies are apparent. definitions of some basic list processing functions. (Note Apart from the suggestive naming of the type and its how in Haskell the form “(θ)” denotes the function constructors, there is nothing in the definition that compels represented by operator ‘θ’, in this case binary addition treatment of members of the type as naturals, or indeed represented by ‘+’.) numbers of any kind; What make catamorphisms attractive as a practical as Instead, the obvious isomorphism between the concrete well as a theoretical basis for programming are their members of Nat and the abstract natural numbers needs to be properties as follows: implemented by an implicit interpreter that converts symbols generality - existence for all regular recursive types, into actions (in this case, iterative applications of other not just Nats or Lists functions); expressiveness - sufficient to define at least any A programmer needs to repeat the implementation of this function provably-terminating in second-order interpreter at each usage of Nat entailing not just extra effort arithmetic [5], i.e., practically-speaking any but the risk of inconsistent implementations leading to reasonable function other than a Universal Turing inconsistent (erroneous) behavior. Machine or other programming language interpreter; Using however the “catamorphism” recursion pattern on essentiality - their embodiment of the initial algebra Nat - cataN - the rendition becomes that of Fig. 2 which semantics [6] of the respective underlying datatypes, significantly remedies the above deficiencies, in that a as the unique homomorphisms that define the uniform interpretation of the symbolic data is provided - i.e., applicable notion of initiality itself; cataN - which moreover derives directly from the type C. Catamorphisms as Pragmatic Basis definition. There are however other recursion patterns that appear to B. Catamorphisms as Practical Basis be necessary for the natural solution of programming The catamorphic pattern defined on Nat above problems. For example, compare the catamorphic renditions generalises for regular recursive types. For example, the in Fig. 5 of the “insert” and “reverse” operations with their catamorphism - cataL - for (polymorphic) lists is as in Fig. 3. definitions in Fig. 6 using respectively the paramorphic [4] Note how in Haskell the type polymorphism on type List is and “left fold” [2] recursion patterns. (N.B. our adoption signified by parameterization on list element type ‘t’. henceforth of customary concrete syntax for the List type.) Copyright (c) IARIA, 2014. ISBN: 978-1-61208-367-4 560 ICSEA 2014 : The Ninth International Conference on Software Engineering Advances -- insert element into ascending list What allows us to continue to treat catamorphisms as a insert e xs = basis in the face of the above is that these other recursion fst $ cataL xs patterns can be synthesized from catamorphisms without (\x (exs,xs) -> recourse to general recursion. The recursion patterns (such as (if e<x then e:x:xs else x:exs, x:xs) paraL, lfold, etc.) can be defined using abstractions (higher- ) order, as needed) from the definitions of the methods