Skip to main content
Log in

A principled approach to programming with nested types in Haskell

  • Published:
Higher-Order and Symbolic Computation

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.

This is a preview of subscription content, log in via an institution to check access.

Access this article

Price excludes VAT (USA)
Tax calculation will be finalised during checkout.

Instant access to the full article PDF.

Similar content being viewed by others

References

  1. 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)

    Article  MATH  MathSciNet  Google Scholar 

  2. Altenkirch, T., Reus, B.: Monadic presentations of lambda terms using generalized inductive types. In: Proc., Computer Science Logic, pp. 453–468 (1999)

  3. 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)

    Article  MATH  MathSciNet  Google Scholar 

  4. Bayley, I.: Generic operations on nested datatypes. Ph.D. Dissertation, University of Oxford (2001)

  5. Bird, R., Meertens, L.: Nested datatypes. In: Proc., Mathematics of Program Construction, pp. 52–67 (1998)

  6. Bird, R., Paterson, R.: de Bruijn notation as a nested datatype. J. Funct. Program. 9(1), 77–91 (1999)

    Article  MATH  MathSciNet  Google Scholar 

  7. Bird, R., Paterson, R.: Generalised folds for nested datatypes. Form. Asp. Comput. 11(2), 200–222 (1999)

    Article  MATH  Google Scholar 

  8. Blampied, P.: Structured recursion for non-uniform data-types. Ph.D. Dissertation, University of Nottingham (2000)

  9. Dybjer, P.: Inductive families. Form. Asp. Comput. 6(4), 440–465 (1994)

    Article  MATH  Google Scholar 

  10. Fiore, M., Plotkin, G.D., Turi, D.: Abstract syntax and variable binding. In: Proc., Logic in Computer Science, pp. 193–202 (1999)

  11. Ghani, N., Uustalu, T., Vene, V.: Build, augment and destroy. Universally. In: Proc., Asian Symposium on Programming Languages, pp. 327–347 (2003)

  12. 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)

  13. Ghani, N., Haman, M., Uustalu, T., Vene, V.: Representing cyclic structures as nested types. Presented at Trends in Functional Programming (2006)

  14. Gill., A.: Cheap deforestation for non-strict functional languages. Ph.D. Dissertation, Glasgow University (1996)

  15. 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)

  16. Hinze, R.: Polytypic functions over nested datatypes. Discrete Math. Theor. Comput. Sci. 3(4), 193–214 (1999)

    MATH  MathSciNet  Google Scholar 

  17. Hinze, R.: Efficient generalized folds. In: Proc., Workshop on Generic Programming, pp. 1–16 (2000)

  18. Hinze, R.: Functional Pearl: Perfect trees and bit-reversal permutations. J. Funct. Program. 10(3), 305–317 (2000)

    Article  MATH  MathSciNet  Google Scholar 

  19. Hinze, R.: A new approach to generic functional programming. In: Proc., Principles of Programming Languages, pp. 119–132 (2000)

  20. Hinze, R.: Manufacturing datatypes. J. Funct. Program. 11(5), 493–524 (2001)

    Article  MATH  MathSciNet  Google Scholar 

  21. Hinze, R., Juering, J.: Generic Haskell: applications. In: Generic Programming: Advanced Lectures, pp. 57–97 (2003)

  22. Hughes, R.J.M., Swierstra, S.D.: Polish parsers, step by step. In: Proc., International Conference on Functional Programming, pp. 239–248 (2003)

  23. Jansson, P., Juering, J.: PolyP—a polytypic programming language extension. In: Proc., Principles of Programming Languages, pp. 470–482 (1997)

  24. Johann, P.: A generalization of short-cut fusion and its correctness proof. Higher-Order Symb. Comput. 15, 273–300 (2002)

    Article  MATH  Google Scholar 

  25. Johann, P., Ghani, N.: Initial algebra semantics is enough! In: Proc., Typed Lambda Calculi and Applications, pp. 207–222 (2007)

  26. Johann, P., Ghani, N.: Foundations for structured programming with GADTs. In: Proc., Principles of Programming Languages, pp. 297–308 (2008)

  27. MacLane, S.: Categories for the Working Mathematician. Springer, Berlin (1971)

    Google Scholar 

  28. Martin, C., Gibbons, J., Bayley, I.: Disciplined efficient generalised folds for nested datatypes. Form. Asp. Comput. 16(1), 19–35 (2004)

    Article  MATH  Google Scholar 

  29. McBride, C., McKinna, J.: View from the left. J. Funct. Program. 14(1), 69–111 (2004)

    Article  MATH  MathSciNet  Google Scholar 

  30. Mycroft, A.: Polymorphic type schemes and recursive definitions. In: Proc., International Symposium on Programming, pp. 217–228 (1984)

  31. Okasaki, C.: Purely Functional Data Structures. Cambridge University Press, Cambridge (1998)

    Book  Google Scholar 

  32. Peyton Jones, S.L.: Haskell 98 Language and Libraries: The Revised Report. Cambridge University Press, Cambridge (2003)

    Google Scholar 

  33. Pitts, A.: Parametric polymorphism and operational equivalence. Math. Struct. Comput. Sci. 10, 1–39 (2000)

    Article  MathSciNet  Google Scholar 

  34. Pitts, A.: Parametric polymorphism, recursive types, and operational equivalence. Unpublished Manuscript

  35. Plotkin, G., Power, J.: Notions of computation determine monads. In: Proc., Foundations of Software Science and Computation Structure, pp. 342–356 (2002)

  36. Smyth, M.B., Plotkin, G.: The category-theoretic solution of recursive domain equations. SIAM J. Comput. 11(4), 761–783 (1982)

    Article  MATH  MathSciNet  Google Scholar 

  37. Takano, A., Meijer, E.: Shortcut deforestation in calculational form. In: Proc., Functional Programming Languages and Computer Architecture, pp. 306–313 (1995)

  38. Uustalu, T., Vene, V.: Mendler-style inductive types. Nord. J. Comput. 6(3), 343–361 (1999)

    MATH  MathSciNet  Google Scholar 

  39. Wehr, M.: Non-uniform recursion: The solution (minimal sorting for fold). Available at www.citeseer.ist.psu.edu/wehr00nonuniform.html

Download references

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Patricia Johann.

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

Reprints 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

Download citation

  • Published:

  • Issue Date:

  • DOI: https://doi.org/10.1007/s10990-009-9047-7

Navigation