Church Encoding of Data Types Considered Harmful for Implementations: Functional Pearl

From the λ-calculus it is known how to represent (recursive) data structures by ordinary λ-terms. Based on this idea one can represent algebraic data types in a functional programming language by higher-order functions. Using this encoding we only have to implement functions to achieve an implementation of the functional language with data structures. In this paper we compare the famous Church encoding of data types with the less familiar Scott and Parigot encodings. We show that one can use the encoding of data types by functions in a Hindley-Milner typed language by adding a single constructor for each data type. In an untyped context, like an efficient implementation, this constructor can be omitted. By collecting the basic operations of a data type in a type constructor class and providing instances for the various encodings, these encodings can co-exist in a single program. By changing the instance of this class we can execute the same algorithm in a different encoding. This makes it easier to compare the encodings with each other. We show that in the Church encoding selectors of constructors yielding the recursive type, like the tail of a list, have an undesirable strictness in the spine of the data structure. The Scott and Parigot encodings do not hamper lazy evaluation in any way. The evaluation of the recursive spine by the Church encoding makes the complexity of these destructors linear time. The same destructors in the Scott and the Parigot encoding requires only constant time. Moreover, the Church encoding has problems with sharing reduction results of selectors. The Parigot encoding is a combination of the Scott and Church encoding. Hence we might expect that it combines the best of both worlds, but in practice it does not offer any advantage over the Scott encoding.

[1]  Jan Martin Jansen,et al.  Efficient interpretation by transforming data types and patterns to functions , 2006, Trends in Functional Programming.

[2]  Erik Barendsen An Unsolvable Numeral System in lambda Calculus , 1991, J. Funct. Program..

[3]  Ralf Hinze Church numerals, twice! , 2005, J. Funct. Program..

[4]  Michel Parigot,et al.  Programming with Proofs: A Second Order Type Theory , 1988, ESOP.

[5]  Philip Wadler,et al.  Deforestation: Transforming Programs to Eliminate Trees , 1990, Theor. Comput. Sci..

[6]  Haskell B. Curry,et al.  Combinatory Logic, Volume I , 1959 .

[7]  Herman Geuvers,et al.  The Church-Scott representation of inductive and coinductive data , 2014 .

[8]  P. J. Landin The Mechanical Evaluation of Expressions , 1964, Comput. J..

[9]  Michel Parigot Recursive Programming with Proofs , 1992, Theor. Comput. Sci..

[10]  Corrado Böhm,et al.  A Self-Interpreter of Lambda Calculus Having a Normal Form , 1992, CSL.

[11]  Jonathan P. Seldin Review: Corrado Bohm, Wolf Gross, E. R. Caianiello, Introduction to the CUCH; C. Bohm, T. B. Steel, The CUCH as a Formal and Description Language , 1975 .

[12]  Jan Martin Jansen,et al.  From Interpretation to Compilation , 2008, CEFP.

[13]  Ralf Hinze Generics for the masses , 2004, ICFP '04.

[14]  Corrado Böhm,et al.  Automatic Synthesis of Typed Lambda-Programs on Term Algebras , 1985, Theor. Comput. Sci..

[15]  Marinus J. Plasmeijer,et al.  iTasks: executable specifications of interactive work flow systems for the web , 2007, ICFP '07.

[16]  Richard Statman,et al.  Lambda Calculus with Types: Preface , 2013 .

[17]  Jan Martin Jansen Programming in the λ-Calculus: From Church to Scott and Back , 2013, The Beauty of Functional Code.

[18]  Werner E. Kluge Abstract Computing Machines - A Lambda Calculus Perspective , 2005, Texts in Theoretical Computer Science. An EATCS Series.

[19]  Simon L. Peyton Jones,et al.  A short cut to deforestation , 1993, FPCA '93.

[20]  J. Roger Hindley,et al.  Lambda calculus with types (Perspectives in Logic) , 2014 .

[21]  Michael Rathjen,et al.  Lambda Calculus with Types , 2014 .

[22]  Jon Fairbairn,et al.  Design and implementation of a simple typed language based on the lambda-calculus , 1984 .

[23]  Martín Abadi,et al.  Types for the Scott Numerals , 1993 .

[24]  Torben Æ. Mogensen Efficient self-interpretation in lambda calculus , 1992, Journal of Functional Programming.

[25]  Marko C. J. D. van Eekelen,et al.  Operational machine specification in a functional programming language , 1995, Softw. Pract. Exp..

[26]  Rod M. Burstall,et al.  HOPE: An experimental applicative language , 1980, LISP Conference.

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

[28]  Jørgen Steensgaard-Madsen,et al.  Typed representation of objects by functions , 1989, TOPL.

[29]  Simon L. Peyton Jones,et al.  The spineless tagless G-machine , 1989, FPCA.

[30]  Colin Runciman,et al.  The Reduceron: Widening the von Neumann Bottleneck for Graph Reduction Using an FPGA , 2008, IFL.

[31]  Werner Kluge,et al.  Abstract Computing Machines , 2005 .

[32]  A. Church The calculi of lambda-conversion , 1941 .

[33]  J N Crossley,et al.  Reminiscences of logicians , 1975 .

[34]  T. B. Steel,et al.  Introduction to the CUCH , 1975 .

[35]  Simon L. Peyton Jones,et al.  Report on the programming language Haskell: a non-strict, purely functional language version 1.2 , 1992, SIGP.

[36]  Corrado Böhm,et al.  Lambda-Definition of Function(al)s by Normal Forms , 1994, ESOP.