Abstract
Initial algebra semantics is one of the cornerstones of the theory of modern functional programming languages. For each inductive data type, it provides a Church encoding for that type, a build combinator which constructs data of that type, a fold combinator which encapsulates structured recursion over data of that type, and a fold/build rule which optimises modular programs by eliminating from them data constructed using the buildcombinator, and immediately consumed using the foldcombinator, for that type. It has long been thought that initial algebra semantics is not expressive enough to provide a similar foundation for programming with nested types in Haskell. Specifically, the standard folds derived from initial algebra semantics have been considered too weak to capture commonly occurring patterns of recursion over data of nested types in Haskell, and no build combinators or fold/build rules have until now been defined for nested types. This paper shows that standard folds are, in fact, sufficiently expressive for programming with nested types in Haskell. It also defines buildcombinators and fold/build fusion rules for nested types. It thus shows how initial algebra semantics provides a principled, expressive, and elegant foundation for programming with nested types in Haskell.
Similar content being viewed by others
References
Abel, A., Matthes, R., Uustalu, T.: Iteration and coiteration schemes for higher-order and nested datatypes. Theor. Comput. Sci. 333(1–2), 3–66 (2005)
Altenkirch, T., Reus, B.: Monadic presentations of lambda terms using generalized inductive types. In: Proc., Computer Science Logic, pp. 453–468 (1999)
Bainbridge, E.S., Freyd, P.J., Scedrov, A., Scott, P.J.: Functorial polymorphism. Theor. Comput. Sci. 70(1), 35–64 (1990). Corrigendum in 71(3), 431 (1990)
Bayley, I.: Generic operations on nested datatypes. Ph.D. Dissertation, University of Oxford (2001)
Bird, R., Meertens, L.: Nested datatypes. In: Proc., Mathematics of Program Construction, pp. 52–67 (1998)
Bird, R., Paterson, R.: de Bruijn notation as a nested datatype. J. Funct. Program. 9(1), 77–91 (1999)
Bird, R., Paterson, R.: Generalised folds for nested datatypes. Form. Asp. Comput. 11(2), 200–222 (1999)
Blampied, P.: Structured recursion for non-uniform data-types. Ph.D. Dissertation, University of Nottingham (2000)
Dybjer, P.: Inductive families. Form. Asp. Comput. 6(4), 440–465 (1994)
Fiore, M., Plotkin, G.D., Turi, D.: Abstract syntax and variable binding. In: Proc., Logic in Computer Science, pp. 193–202 (1999)
Ghani, N., Uustalu, T., Vene, V.: Build, augment and destroy. Universally. In: Proc., Asian Symposium on Programming Languages, pp. 327–347 (2003)
Ghani, N., Johann, P., Uustalu, T., Vene, V.: Monadic augment and generalised short cut fusion. In: Proc., International Conference on Functional Programming, pp. 294–305 (2005)
Ghani, N., Haman, M., Uustalu, T., Vene, V.: Representing cyclic structures as nested types. Presented at Trends in Functional Programming (2006)
Gill., A.: Cheap deforestation for non-strict functional languages. Ph.D. Dissertation, Glasgow University (1996)
Gill, A., Launchbury, J., Peyton Jones, S.L.: A short cut to deforestation. In: Proc., Functional Programming Languages and Computer Architecture, pp. 223–232 (1993)
Hinze, R.: Polytypic functions over nested datatypes. Discrete Math. Theor. Comput. Sci. 3(4), 193–214 (1999)
Hinze, R.: Efficient generalized folds. In: Proc., Workshop on Generic Programming, pp. 1–16 (2000)
Hinze, R.: Functional Pearl: Perfect trees and bit-reversal permutations. J. Funct. Program. 10(3), 305–317 (2000)
Hinze, R.: A new approach to generic functional programming. In: Proc., Principles of Programming Languages, pp. 119–132 (2000)
Hinze, R.: Manufacturing datatypes. J. Funct. Program. 11(5), 493–524 (2001)
Hinze, R., Juering, J.: Generic Haskell: applications. In: Generic Programming: Advanced Lectures, pp. 57–97 (2003)
Hughes, R.J.M., Swierstra, S.D.: Polish parsers, step by step. In: Proc., International Conference on Functional Programming, pp. 239–248 (2003)
Jansson, P., Juering, J.: PolyP—a polytypic programming language extension. In: Proc., Principles of Programming Languages, pp. 470–482 (1997)
Johann, P.: A generalization of short-cut fusion and its correctness proof. Higher-Order Symb. Comput. 15, 273–300 (2002)
Johann, P., Ghani, N.: Initial algebra semantics is enough! In: Proc., Typed Lambda Calculi and Applications, pp. 207–222 (2007)
Johann, P., Ghani, N.: Foundations for structured programming with GADTs. In: Proc., Principles of Programming Languages, pp. 297–308 (2008)
MacLane, S.: Categories for the Working Mathematician. Springer, Berlin (1971)
Martin, C., Gibbons, J., Bayley, I.: Disciplined efficient generalised folds for nested datatypes. Form. Asp. Comput. 16(1), 19–35 (2004)
McBride, C., McKinna, J.: View from the left. J. Funct. Program. 14(1), 69–111 (2004)
Mycroft, A.: Polymorphic type schemes and recursive definitions. In: Proc., International Symposium on Programming, pp. 217–228 (1984)
Okasaki, C.: Purely Functional Data Structures. Cambridge University Press, Cambridge (1998)
Peyton Jones, S.L.: Haskell 98 Language and Libraries: The Revised Report. Cambridge University Press, Cambridge (2003)
Pitts, A.: Parametric polymorphism and operational equivalence. Math. Struct. Comput. Sci. 10, 1–39 (2000)
Pitts, A.: Parametric polymorphism, recursive types, and operational equivalence. Unpublished Manuscript
Plotkin, G., Power, J.: Notions of computation determine monads. In: Proc., Foundations of Software Science and Computation Structure, pp. 342–356 (2002)
Smyth, M.B., Plotkin, G.: The category-theoretic solution of recursive domain equations. SIAM J. Comput. 11(4), 761–783 (1982)
Takano, A., Meijer, E.: Shortcut deforestation in calculational form. In: Proc., Functional Programming Languages and Computer Architecture, pp. 306–313 (1995)
Uustalu, T., Vene, V.: Mendler-style inductive types. Nord. J. Comput. 6(3), 343–361 (1999)
Wehr, M.: Non-uniform recursion: The solution (minimal sorting for fold). Available at www.citeseer.ist.psu.edu/wehr00nonuniform.html
Author information
Authors and Affiliations
Corresponding author
Additional information
This is a revised and extended version of the conference paper [25].
Supported in part by EPSRC grant EP/C511964/2.
Rights and permissions
About this article
Cite this article
Johann, P., Ghani, N. A principled approach to programming with nested types in Haskell. Higher-Order Symb Comput 22, 155–189 (2009). https://doi.org/10.1007/s10990-009-9047-7
Published:
Issue Date:
DOI: https://doi.org/10.1007/s10990-009-9047-7