Maximal sharing in the Lambda calculus with letrec

Increasing sharing in programs is desirable to compactify the code, and to avoid duplication of reduction work at run-time, thereby speeding up execution. We show how a maximal degree of sharing can be obtained for programs expressed as terms in the lambda calculus with letrec. We introduce a notion of 'maximal compactness' for λletrec-terms among all terms with the same infinite unfolding. Instead of defined purely syntactically, this notion is based on a graph semantics. λletrec-terms are interpreted as first-order term graphs so that unfolding equivalence between terms is preserved and reflected through bisimilarity of the term graph interpretations. Compactness of the term graphs can then be compared via functional bisimulation. We describe practical and efficient methods for the following two problems: transforming a λletrec-term into a maximally compact form; and deciding whether two λletrec-terms are unfolding-equivalent. The transformation of a λletrec-terms L into maximally compact form L0 proceeds in three steps: (i) translate L into its term graph G = [[L]] ; (ii) compute the maximally shared form of G as its bisimulation collapse G0 ; (iii) read back a λletrec-term L0 from the term graph G0 with the property [[L0]] = G0. Then L0 represents a maximally shared term graph, and it has the same unfolding as L. The procedure for deciding whether two given λletrec-terms L1 and L2 are unfolding-equivalent computes their term graph interpretations [[L1]] and [[L2]], and checks whether these are bisimilar. For illustration, we also provide a readily usable implementation.

[1]  Thomas Johnsson,et al.  Lambda Lifting: Treansforming Programs to Recursive Equations , 1985, FPCA.

[2]  Jan Willem Klop,et al.  Combinatory reduction systems , 1980 .

[3]  Davide Sangiorgi,et al.  Communicating and Mobile Systems: the π-calculus, , 2000 .

[4]  Daphne Norton,et al.  Algorithms for testing equivalence of finite automata, with a grading tool for JFLAP , 2009 .

[5]  Olivier Danvy,et al.  Lambda-Lifting in Quadratic Time , 2002, J. Funct. Log. Program..

[6]  Stefan Blom,et al.  Term Graph Rewriting. Syntax and semantics , 2001 .

[7]  de Ng Dick Bruijn Lambda calculus notation with nameless dummies, a tool for automatic formula manipulation, with application to the Church-Rosser theorem , 1972 .

[8]  Enno Ohlebusch,et al.  Term Rewriting Systems , 2002 .

[9]  Simon Peyton Jones,et al.  The Implementation of Functional Programming Languages (Prentice-hall International Series in Computer Science) , 1987 .

[10]  André L. M. Santos,et al.  Compilation by transformation in non-strict functional languages , 1995 .

[11]  Simon L. Peyton Jones,et al.  The Implementation of Functional Programming Languages , 1987 .

[12]  Vincent van Oostrom,et al.  Nested Term Graphs , 2014, TERMGRAPH.

[13]  Henk Barendregt,et al.  The Lambda Calculus: Its Syntax and Semantics , 1985 .

[14]  Paul Klint,et al.  Efficient annotated terms , 2000, Softw. Pract. Exp..

[15]  Ron Dinishak The optimal implementation of functional programming languages , 2000, SOEN.

[16]  Benjamin Goldberg,et al.  Detecting sharing of partial applications in functional programs , 1987, FPCA.

[17]  Jakob Grue Simonsen,et al.  Infinitary Combinatory Reduction Systems , 2005, Inf. Comput..

[18]  Jens Palsberg,et al.  Binding-time analysis: abstract interpretation versus type inference , 1994, Proceedings of 1994 IEEE International Conference on Computer Languages (ICCL'94).

[19]  C. Grabmayer,et al.  Expressibility in the Lambda Calculus with μ , 2013 .

[20]  J. Hopcroft,et al.  A Linear Algorithm for Testing Equivalence of Finite Automata. , 1971 .

[21]  Clemens Grabmayer,et al.  Expressibility in the Lambda Calculus with mu , 2012, RTA.

[22]  Clemens Grabmayer,et al.  Term Graph Representations for Cyclic Lambda-Terms , 2013, TERMGRAPH.

[23]  Detlef Plump,et al.  Evaluation of functional expressions by hypergraph rewriting , 1993 .

[24]  de Ng Dick Bruijn,et al.  Lambda calculus notation with nameless dummies, a tool for automatic formula manipulation, with application to the Church-Rosser theorem , 1972 .

[25]  R. J. M. Hughes,et al.  Super-combinators a new implementation method for applicative languages , 1982, LFP '82.

[26]  Thibaut Balabonski,et al.  A unified approach to fully lazy sharing , 2012, POPL '12.

[27]  Richard S. Bird,et al.  de Bruijn notation as a nested datatype , 1999, Journal of Functional Programming.

[28]  Clemens Grabmayer,et al.  Expressibility in the Lambda Calculus with mu , 2012, RTA.

[29]  Ulrik Pagh Schultz,et al.  Optimal Lambda Lifting in Quadratic Time , 2008, IFL.

[30]  Olaf Chitil,et al.  Common Subexpressions Are Uncommon in Lazy Functional Languages , 1997, Implementation of Functional Languages.

[31]  Paul Klint,et al.  ATerms for manipulation and exchange of structured data: It's all about sharing , 2007, Inf. Softw. Technol..

[32]  Robin Milner,et al.  Communicating and mobile systems - the Pi-calculus , 1999 .

[33]  John E. Hopcroft,et al.  An n log n algorithm for minimizing states in a finite automaton , 1971 .