Monadic Fusion of Functional Programs (original) (raw)

2003, Fixed Points in Computer Science

Abstract

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.

Loading...

Loading Preview

Sorry, preview is currently unavailable. You can download the paper by clicking the button above.

References (90)

  1. Corrado Böhm and Alessandro Berarducci. Automatic synthesis of typed A- programs on term algebras. Theoretical Computer Science, 39:135-154, 1985.
  2. BP991 Richard Bird and Ross Paterson. Generalised folds for nested datatypes. For mat Aspects of Computing, 11(2):200-222, 1999.
  3. Herman Geuvers. Inductive and coinductive types with iteration and re cursion. In Bengt Nordstrom, Kent Pettersson, and Gordon Plotkin, edi tors, Proceedings of the ggg Workshop on 7jpes for Proofs and Programs, Bistad, Sweden, June 199, pages 193-217, 1992. Electronically available via ftp: I/ftp. cc. chaimers . se/pub/cs-reports/baastad.92/proc. dvi. Z. [HinOlJ RaIf Hinze. Manufacturing datatypes. Journal of Functional Programming, 11(5):493-524, 2001.
  4. Nax P. Mendler. Recursive types and type constraints in second-order lambda calculus. In Proceedings of the Second Annual IEEE Symposium on Logic in Computer Science, Ithaca, N. Y., pages 30-36. IEEE Computer Society Press, 1987.
  5. Chris Okasalci. Purely Functional Data Structures. PhD thesis, Carnegie Mellon University, 1996.
  6. Chris Okasaki. From Fast Exponentiation to Square Matrices: An Adventure in Types. In International Conference on Functional Programming, pages 28- 35, September 1999.
  7. Zdzislaw Spiawski and Pawel Urzyczyn. Type fixpoints: Iteration vs. recursion. SICPLAN Notices, 34(9):102-113, 1999. Proceedings of the 1999 International Conference on Functional Programming (ICFP), Paris, France.
  8. A. Arnold. The ji-calculus alternation-depth hierarchy is strict on binary trees. RAIRO-Theoretical Informatics and Applications, 33:329-339, 1999.
  9. A. Arnold and D. Niwifiski. Fixed point characterization of Büchi automata on infinite trees. J. Inf. Process. Cybern. ElK, 26:453-461, 1990.
  10. A. Arnold and D. Niwixlski. Fixed point characterization of weak monadic logic defin able sets of trees. In M. Nivat and A. Podelski, editors, Thee automata and Languages, pages 159-188. Elaevier, 1992.
  11. A. Arnold and D. Niwidaki. Rudiments of z-calculus. Number 146 in Studies in logic and the foundations of mathematics. Elaevier, North-Holland, 2002.
  12. A. Arnold and L. Santocanale. Ambiguous classes in the gaines z-calculua hierarchy. In FOSSA CS 2003 (to appear).
  13. A. Arnold, A. Vincent, and I. Walukiewicz. Games for synthesis of controllers with partial observation. Theoretical Computer Science, to appear, 2002.
  14. D. Janin and I. Walujciewicz. Automata for the modal p-calculus and related results. Lecture Notes in Computer Science, 969:552-562, 1995.
  15. R. Kaivola. On modal mu-calculus and Buchi tree automata. Information Processing Letters, 54:17-22, 1995.
  16. Kupferman and M. Y. Vardi. The weakness of aelI-complementation. Lecture Notes in Computer Science, 1563:455-466, 1999.
  17. D. E. Muller, A. Saoudi, and P. E. Schupp. Alternating automata, the weak monadic theory of trees and its complexity. Theoretical Computer Science, 97:233-244, 1992.
  18. M. 0. Rabin. Weakly definable relations and special automata. In Y. Bar-Hillel, editor, Mathematical Logic and Foundation of Set Theory, pages 1-23. North-Holland, Amsterdam, 1990. References
  19. S. Bloom and Z. Esik. Iteration theories. EATCS Monographs on Theoretical Computer Science, 1993.
  20. Vincent Danos and Josée Desharnais. Labeled Markov processes: stronger and faster approximations. Submitted, 2003.
  21. J. Desharnais, V. Gupta, R. Jagadeesan, and P. Panangaden. Approximating continuous Markov processes. Information and Computation, 2003. To appear. Available from http: //www. if t . ulaval. calj odesharnais.
  22. Alex Simpson and Gordon Plotkin. Complete axioms for categorical fixed- point operators. In Proceedings of the 15th Annual IEEE Symposium on Logic in Computer Science (LICS 2000), pages 30-41. IEEE, June 2000. References
  23. M. Abadi and L. Cardelli. A Theory of Objects. Springer-Verlag, 1996.
  24. A. Gordon. Operational equivalences for untyped and polymorphic ob ject calculi. In A. Pitts and A. Gordon, editors, Higher-Order Opera tional Techniques in Semantics. Cambridge University Press, 1997.
  25. A. M. Pitts. Existential types: Logical relations and operational equiv alence. Number 1443. Springer-Verlag, 1998.
  26. A. M. •Pitts. Parametric polymorphism and operational equivalence. Electronic Notes in Theoretical Computer Science, 10, 1998.
  27. A. M. Pitt.s. Parametric polymorphism and operational equivalence. Mathematical Structures in Computer Science, 10, 2000.
  28. References 1J P. Aczel, J. Adámek, and J. Velebil. A coalgebraic view of infinite trees and iteration. In A. Corradini, M. Lenisa, and U. Montanan, editors, Proc. of CMCS'Ol, volume 44(1) of Electronic Notes in Theoretical Computer Science. Elsevier, 2001.
  29. J. Adáinek, S. Milius, and J. Velebil. Free iterative theories: a coalgebraic view. Accepted for publication in Mathematical Structures in Computer Science, 2002.
  30. J. Adámek and J. Rosick. Locally Presentable and Accessible Categories, volume 189 of London Mathematical Society Lecture Notes. Cambridge University Press, 1994.
  31. M. Barr. Coequalizers and free triples. Math. Z., 116:307-322, 1970.
  32. M. Fiore, G. Plotkin, and D. Turi. Abstract syntax and variable binding. In Proc. of LICS'99, pages 193-202. IEEE CS Press, 1999.
  33. N. Ghani, C. Lüth, F. de Marchi, and J. Power. Algebras, coalgebras, monads and comonads. In A. Corradini, M. Lenisa, and U. Montanan, editors, Proc. of CMCS'OI, volume 44(1) of Electronic Notes in Theoretical Computer Science. 2001.
  34. N. Ghani, C. Lüth, and F. De Marchi. Coalgebraic monads. In L. S. Moss, editor, Proc. of CMCS'02, volume 65(1) of Electronic Notes in Theoretical Computer Science. Elsevier, 2002.
  35. M. Hyland, G. Plotkin, and J. Power. Combining computational effects: Commutativity and sum. In A. Baeza-Yates, U. Montanan, and N. Santoro, editors, Proc. of IFIP 17th World Computer Congress, TC1 Stream / TCS 200E, volume 223 of IFIP Conference Proceedings, pages 474-484. Kluwer Academic Publishers, 2002.
  36. C. M. Kelly. A unified treatment of transfinite constructions for free algebras, free monoids, colimits, associated sheaves and so on. Bull, of Australian Mathematical Society, 22:1-83, 1980.
  37. G. M. Kelly and J. Power. Adjunctions whose counits are equalizers, and presentations of finitary monads. Journal of Pure and Applied Algebra, 89:163-179, 1993.
  38. C. Lüth. Categorical Term Rewriting: Monads and Modularity. PhD thesis, University of Edinburgh, 1998.
  39. C. Lüth and N. Ghani. Monads and modularity. In A. Armando, editor, Proc. of FroCoS'02, number 2309 in Lecture Notes in Computer Science, pages 18-32. Springer Verlag, 2002.
  40. E. Moggi. Computational lambda-calculus and monads. In Proc. of LICS'89, pages 14-23. IEEE CS Press, 1989.
  41. E. Moggi. An abstract view of programming languages. Technical Report ECS-LFCS-90-113, LFCS, 1990.
  42. L. Moss. Parametric corecursion. Theoretical Computer Science, 260(1-2):139-163, 2001. References
  43. J. Adámek, H. Herrlich, and G. E. Strecker. Abstract and Concrete Categories. Pure and Applied Mathematics. John Wiley & Sons, 1990.
  44. ICDPR97aJ L. Correnson, E. Duris, D. Parigot, and G. Roussel. Attribute grammars and func tional programming deforestation. In 4th International StaticAnalysis Symposium- Poster Session, Paris (F), 1997.
  45. ICDPR97bJ L Correnson, E. Duris, D. Parigot, and G. Roussel. Symbolic composition. Technical Report 3348, INRIA, January 1997.
  46. ICF82I B. Courcelle and P Franchi-Zannettacci. Attribute grammars and recursive program schemes. Theoret. Cornput. Sci., 17:163-191, 235-257, 1982.
  47. W. Damm. The JO-and 01-hierarchies. Theoretical Computer Science, 20:95-206, 1982.
  48. J. Engeifriet. Bottom-up and top-down tree transformations-a comparison. Math. Systems Theory, 9(3):198-231, 1975.
  49. FEng8Ol J. Engeifriet. Some open questions and recent results on tree transducers and tree languages. In R.V. Book, editor, Formal language theory: perspectives and open prob lems, pages 24 1-286. New York, Academic Press, 1980.
  50. J. Engeifriet. Tree transducers and syntax-directed semantics. Technical Report Memorandum 363, Technische Hogeschool Twente, March 1981. also in: Pro ceedings of the Colloquium on Trees in Algebra and Programming (CAAP'92), Lille, France 1992.
  51. IEV85J J. Engelfriet and H. Vogler. Macro tree transducers. I. Comput. System Sci., 31:71-146, 1985.
  52. M. M. Fokkinga. Law and Order in Algorithrnics. PhD thesis, University of IWente, Dept INF, Enschede, The Netherlands, 1992.
  53. IFul8lJ Z. Fulöp. On attributed tree transducers. Acta Cybernet., 5:261-279, 1981.
  54. IFV98] Z. FuLop and H. Vogler. Syntax-directed semantics-Formal models based on tree transducers. Monographs in Theoretical Computer Science, An EATCS Series. Springer-Verlag, 1998.
  55. R. Giegerich. Composition and evaluation of attribute coupled grammars. Acta In form., 25:355-423, 1988.
  56. Gi196l A. Gill. Cheap Deforestation for Non-strict Functional Languages. PhD thesis, De partment of Computing Science, Glasgow University January 1996.
  57. IGLP93I A. Gill, J. Launchburry, and S. L. Peyton-Jones. A short cut to deforestation. In Pro ceedings ofFunctional ProgrammingLanguages an CornputerArchitecture (FPCA'93), pages 223-232, Copenhagen, Denmark, June 1993. ACM Press.
  58. HinOOJ R. Hinze. Deriving backtracking monad transformers. In P. Wadler, editor, Pro ceedings of the 2000 International Conference on Functional Programming (ICFP'03), Montreal, Canada, sep 2000.
  59. IJohOll P Johann. Short cut fusion: Proved and improved. In W Taha, editor, Proceedings of the 2nd International Workshop on Semantics, Applications, and Implementation ofProgram Generation (SAIG'Ol), volume 2196 of LNCS, pages 47-71, Florence, Italy, September 2001. Springer.
  60. JurO2J C. Jurgensen. Monadic fusion of functional programs. Technical Report TUD-F102- 12, Technische Universität Dresden, Fakultät Informatik, D-01062 Dresden, Ger many, December 2002.
  61. JV01J C. Jurgensen and H. Vogler. Syntactic composition of top-down tree transducers is short cut fusion. Technical Report TIJD-FI01-10, Technische Universität Dresden, Fakultät Informatik, D-01 062 Dresden, Germany, November 2001. Acceptedfor pub lication in Math. Struct. in Cornp. Science.
  62. G. M. Kelly. A unified treatment of transfinite constructions for free algebras, free monoids, colimits, associated sheaves and so on. Bulletins of the Australian Mathe matical Society, 22: 1-83, 1980.
  63. KUh98j A. Kühnemann. Benefits of tree transducers for optimizing functional programs. In V. Arvind and R. Ramanujam, editors, Proceedings of the 18th INternational Conference on Foundations of Software Technology & Theoretical Computer Science (FST&TCS'98), volume 1530 of LJ"JCS, pages 146-157, Chennai, India, dec 1998. Springer-Verlag.
  64. KVO1I A. ICuhnemann and J. Voigtlander. 'Thee transducer composition as deforestation method for functional programs. Technical Report TUD-FI01-07, Technische Uni versitat Dresden, Fakultät Informatik, D-01062 Dresden, Germany, August 2001.
  65. LGO2aI Ch. Lüth and N. Ghani. Composing monads using coproducts. In International Con ference on Functional Programming (ICFP'02), pages 133-144. ACM Press, Septem ber2002.
  66. Ch. Lüth and N. Ghani. Monads and modularity. In Alessandro Armando, editor, Frontiers of Combining Systems FroCos 2002, 4th International Workshop, number 2309 in Lecture Notes in Artificial Intelligence, pages 18-32. Springer Verlag, 2002.
  67. Mog9Ol E. Moggi. An abstract view of programming languages. Technical Report ECS-LFCS 90-113, LFCS, 1990.
  68. Rou68J W. C. Rounds. T?'ees, transducers and transformations. PhD thesis, Stanford Univer sity, 1968.
  69. TM951 A. Takano and E. Meijer. Shortcut deforestation in calculational form. In Proceedings of the Conference on Functional Programing Languages and ComputerArchitecture, pages 306-313, La Jolla, CA, June 1995. ACM Press.
  70. P. Wadler. Deforestation: Transforming programs to eliminate trees. Theoretical Computer Science, 73(2):231-248, 1990. References
  71. Zena M. Ariola and Matthias Felleisen. The call-by-need lambda calculus. Journal of Functional ProgramrningF 7(3):265 301F May 1997.
  72. AFMZO2} D. AnconaFS. FagorziFE. MoggiFand E. Zucca. Mixin modules and computational effects. SubmittedF2002.
  73. Zena M. AriolaF John MaraistF Martin OderskyF Matthias FelleisenFand Philip Wadler. A call-by-need lambda calculus. In Conference record of POPL '95, nd ACM SIGPLAN-SIGA CT Symposium on Principles of Programming Languages: papers presented at the Symposium: San Francisco, California, Jan uary P2-25, l995Fpages 233-246FNew YorkFNYFUSAF1995. ACM Press.
  74. H[endrikl P[ieter] Barendregt. The Lambda Calculus: Its Syntax and Semantics. North-HollandFrevised editionFl984.
  75. Alan Bawden. Letrec and callcc implement references. Message to comp . lang. schemerl988.
  76. Gerard Boudol. The recursive record semantics of objects revis ited. Lecture Notes in Computer Sciencer2o28:269-283F2001.
  77. Magnus Carlsson. Value recursion in the continuation monad. Unpublished NoteFJanuary 2003.
  78. Levent Erkök and John Launchbury. Recursive monadic bind ings. In Proceedings of the ACM Sigplan International Con ference on Functional Programming (ICFP-OO)rvolume 35.9 of ACM Sigplan NoticesFpages 174-185r N.Y.FSeptember 18-21 2000. ACM Press.
  79. Levent ErkökFJohn LaunchburyFand Andrew Moran. Seman tics of value recursion for monadic input/output. Journal of Theoretical Informatics and ApplicationsF36(2): 155-180r2002.
  80. Levent Erkök. Value Recursion in Monadic Computations. PhD thesisl'OGI School of Science and EngineeringrOHSUrPortlandr Oregonr2002.
  81. Daniel P. Friedman and Amr Sabry. Recursion is a computa tional effect. Technical Report 546FComputer Science Depart mentFlndiana UniversityrDecember 2000. [Jon991 Report on the programming language Haskell 98FFebruary 1999.
  82. Richard KelseyrWilliam ClingerFand Jonathan Rees (Editors). Revised 5 report on the algorithmic language Scheme. ACM SIC- PLAN NoticesF33(9) :26-76FSeptember 1998.
  83. Peter J. Landin. The mechanical evaluation of expressions. The Computer Journalr'6(4):308-320rJanuary 1964.
  84. E. Moggi and S. Fagorzi. A monadic multi-stage metalanguage. In FoSSaCS 2003FLNCS. Springer-VerlagF2003.
  85. John MaraistFMartin OderskyFand Philip Wadler. The call by-need lambda calculus. Journal of Functional ProgrammingP 8(3):275-3l7rMay 1998.
  86. WF94J Andrew K. Wright and Matthias Felleisen. A syntactic approach to type soundness. Information and ComputationFll5(1):38-94F 1994. References
  87. P. Aczel, J. Adámek, and J. Velebil. A coalgebraic view of infinite trees and iteration. In A. Corradini, M. Lenisa, and U. Montanan, eds., Proc. of 4th mt. Wksh. on Coalgebraic Methods in Comput. Sci., CMCS'Ol (Cenova, Apr. 2001), vol. 44(1) of Electr. Notes in Theor. Comput. Sci.. Elsevier, 2001.
  88. P. Aczel, J. Adámek, S. Milius, and J. Velebil. Infinite trees and completely iterative theories: a coalgebraic view. Theor. Comput. Sci., to appear.
  89. S. Milius. On iteratable endofunctors. In Proc. of 9th mt. Conf. on Category The ory and Comput. Sci., CTCS 2002 (Ottawa, Aug. 2002), Electr. Notes in Theor. Comput. Sci., Elsevier, to appear.
  90. L. S. Moss. Parametric corecursion. Theor. Comput. Sci., 260(1-2):139--163, 2001.