Skip to main content

Fantastic Morphisms and Where to Find Them

A Guide to Recursion Schemes

  • Conference paper
  • First Online:
Mathematics of Program Construction (MPC 2022)

Abstract

Structured recursion schemes have been widely used in constructing, optimizing, and reasoning about programs over inductive and coinductive datatypes. Their plain forms, catamorphisms and anamorphisms, are restricted in expressivity. Thus many generalizations have been proposed, which further led to several unifying frameworks of structured recursion schemes. However, the existing work on unifying frameworks typically focuses on the categorical foundation, and thus is perhaps inaccessible to practitioners who are willing to apply recursion schemes in practice but are not versed in category theory. To fill this gap, this expository paper introduces structured recursion schemes from a practical point of view: a variety of recursion schemes are motivated and explained in contexts of concrete programming examples. The categorical duals of these recursion schemes are also explained.

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

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 49.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 64.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    It requires the DeriveFunctor extension of ghc to derive functors automatically.

  2. 2.

    The recursion scheme requires the ghc extension RankNTypes since the first argument involves a polymorphic function.

  3. 3.

    The name mutumorphism is a bit special in the zoo of recursion schemes: the prefix mutu- is from Latin rather than Greek.

  4. 4.

    Support of gadts is turned on by the extension gadt s in ghc.

  5. 5.

    It requires the RankNTypes extension of ghc.

References

  1. Abel, A., Matthes, R., Uustalu, T.: Iteration and coiteration schemes for higher-order and nested datatypes. Theoret. Comput. Sci. 333(1–2), 3–66 (2005). https://doi.org/10.1016/j.tcs.2004.10.017

    Article  MathSciNet  MATH  Google Scholar 

  2. Bergroth, L., Hakonen, H., Raita, T.: A survey of longest common subsequence algorithms. In: Proceedings Seventh International Symposium on String Processing and Information Retrieval. SPIRE 2000, pp. 39–48, September 2000. https://doi.org/10.1109/SPIRE.2000.878178

  3. Bird, R.: Pearls of Functional Algorithm Design. Cambridge University Press, Cambridge (2010)

    Google Scholar 

  4. Bird, R., de Moor, O.: Algebra of Programming, London (1997)

    Google Scholar 

  5. Bird, R., Gibbons, J.: Algorithm Design with Haskell. Cambridge University Press, Cambridge (2020). http://www.cs.ox.ac.uk/publications/books/adwh/

  6. Bird, R., Paterson, R.: Generalised folds for nested datatypes. Formal Aspects Comput. 11(2), 200–222 (1999). https://doi.org/10.1007/s001650050047

    Article  MATH  Google Scholar 

  7. Bird, R., Meertens, L.: Nested datatypes. In: Jeuring, J. (ed.) MPC 1998. LNCS, vol. 1422, pp. 52–67. Springer, Heidelberg (1998). https://doi.org/10.1007/BFb0054285

    Chapter  Google Scholar 

  8. Bird, R.S., Paterson, R.: de Bruijn notation as a nested datatype. J. Funct. Program. 9(1), 77–91 (1999). https://doi.org/10.1017/S0956796899003366

    Article  MathSciNet  MATH  Google Scholar 

  9. Capretta, V., Uustalu, T., Vene, V.: Recursive coalgebras from comonads. Inf. Comput. 204(4), 437–468 (2006). https://doi.org/10.1016/j.ic.2005.08.005

    Article  MathSciNet  MATH  Google Scholar 

  10. Coutts, D., Leshchinskiy, R., Stewart, D.: Stream fusion: from lists to streams to nothing at all. In: Proceedings of the 12th ACM SIGPLAN International Conference on Functional Programming. ICFP 2007, pp. 315–326. Association for Computing Machinery, New York (2007). https://doi.org/10.1145/1291151.1291199

  11. Felleisen, M., Findler, R.B., Flatt, M., Krishnamurthi, S.: How to Design Programs: An Introduction to Programming and Computing. The MIT Press, Cambridge (2018)

    Google Scholar 

  12. Fokkinga, M.: Law and order in algorithmics. Ph.D. thesis, University of Twente, 7500 AE Enschede, Netherlands, February 1992

    Google Scholar 

  13. Fokkinga, M.: Monadic maps and folds for arbitrary datatypes. Memoranda Informatica 94–28, Department of Computer Science, University of Twente, June 1994. http://doc.utwente.nl/66622/

  14. Fokkinga, M.M.: Tupling and mutumorphisms. Squiggolist 1(4), 81–82 (1990)

    Google Scholar 

  15. Gibbons, J.: Generic downwards accumulations. Sci. Comput. Program. 37(1–3), 37–65 (2000). https://doi.org/10.1016/S0167-6423(99)00022-2

    Article  MathSciNet  MATH  Google Scholar 

  16. Gibbons, J.: Metamorphisms: streaming representation-changers. Sci. Comput. Program. 65(2), 108–139 (2007). https://doi.org/10.1016/j.scico.2006.01.006

    Article  MathSciNet  MATH  Google Scholar 

  17. Gibbons, J.: Coding with asymmetric numeral systems. In: Hutton, G. (ed.) MPC 2019. LNCS, vol. 11825, pp. 444–465. Springer, Cham (2019). https://doi.org/10.1007/978-3-030-33636-3_16

    Chapter  Google Scholar 

  18. Gibbons, J.: How to design co-programs. J. Funct. Program. 31, e15 (2021). https://doi.org/10.1017/S0956796821000113

    Article  MATH  Google Scholar 

  19. Gibbons, J., Wu, N.: Folding domain-specific languages: deep and shallow embeddings (functional pearl). In: Proceedings of the 19th ACM SIGPLAN International Conference on Functional Programming, Gothenburg, Sweden, 1–3 September 2014, pp. 339–347. ACM (2014). https://doi.org/10.1145/2628136.2628138

  20. Hagino, T.: Category theoretic approach to data types. Ph.D. thesis, University of Edinburgh (1987)

    Google Scholar 

  21. Hinze, R.: Fun with phantom types. The fun of programming, pp. 245–262 (2003)

    Google Scholar 

  22. Hinze, R.: Generic programming with adjunctions. In: Gibbons, J. (ed.) Generic and Indexed Programming. LNCS, vol. 7470, pp. 47–129. Springer, Heidelberg (2012). https://doi.org/10.1007/978-3-642-32202-0_2

    Chapter  MATH  Google Scholar 

  23. Hinze, R.: Adjoint folds and unfolds–an extended study. Sci. Comput. Program. 78(11), 2108–2159 (2013). https://doi.org/10.1016/j.scico.2012.07.011

    Article  Google Scholar 

  24. Hinze, R., James, D.W.H., Harper, T., Wu, N., Magalhães, J.P.: Sorting with bialgebras and distributive laws. In: Löh, A., Garcia, R. (eds.) Proceedings of the 8th ACM SIGPLAN workshop on Generic programming, WGP@ICFP 2012, Copenhagen, Denmark, 9–15 September 2012, pp. 69–80. ACM (2012). https://doi.org/10.1145/2364394.2364405

  25. Hinze, R., Magalhães, J.P., Wu, N.: A duality of sorts. In: Achten, P., Koopman, P. (eds.) The Beauty of Functional Code. LNCS, vol. 8106, pp. 151–167. Springer, Heidelberg (2013). https://doi.org/10.1007/978-3-642-40355-2_11

    Chapter  MATH  Google Scholar 

  26. Hinze, R., Wu, N.: Histo- and dynamorphisms revisited. In: Proceedings of the 9th ACM SIGPLAN Workshop on Generic Programming. WGP 2013, New York, NY, USA, pp. 1–12 (2013). https://doi.org/10.1145/2502488.2502496

  27. Hinze, R., Wu, N.: Unifying structured recursion schemes: an extended study. J. Funct. Program. 26, e1 (2016). https://doi.org/10.1017/S0956796815000258

    Article  MathSciNet  MATH  Google Scholar 

  28. Hinze, R., Wu, N., Gibbons, J.: Unifying structured recursion schemes. In: Proceedings of the 18th ACM SIGPLAN International Conference on Functional Programming, ICFP 2013. New York, NY, USA, pp. 209–220 (2013). https://doi.org/10.1145/2500365.2500578

  29. Hinze, R., Wu, N., Gibbons, J.: Conjugate hylomorphisms - or: the mother of all structured recursion schemes. In: Proceedings of the 42nd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2015, pp. 527–538. ACM, New York (2015). https://doi.org/10.1145/2676726.2676989

  30. Hoare, C.A.R.: Chapter II: Notes on Data Structuring, pp. 83–174. Academic Press Ltd., GBR (1972)

    Google Scholar 

  31. Hu, Z., Iwasaki, H., Takeichi, M.: Calculating accumulations. N. Gener. Comput. 17(2), 153–173 (1999)

    Article  Google Scholar 

  32. Hu, Z., Iwasaki, H., Takeichi, M.: Deriving structural hylomorphisms from recursive definitions. ACM SIGPLAN Not. 31 (1999). https://doi.org/10.1145/232629.232637

  33. Hutton, G.: Fold and unfold for program semantics. In: Proceedings of the Third ACM SIGPLAN International Conference on Functional Programming. ICFP 1998, pp. 280–288. Association for Computing Machinery, New York (1998). https://doi.org/10.1145/289423.289457

  34. Johann, P., Ghani, N.: Initial algebra semantics is enough! In: Della Rocca, S.R. (ed.) TLCA 2007. LNCS, vol. 4583, pp. 207–222. Springer, Heidelberg (2007). https://doi.org/10.1007/978-3-540-73228-0_16

    Chapter  Google Scholar 

  35. Johann, P., Ghani, N.: Foundations for structured programming with GADTs. SIGPLAN Not. 43(1), 297–308 (2008). https://doi.org/10.1145/1328897.1328475

    Article  MATH  Google Scholar 

  36. Johann, P., Ghani, N.: A principled approach to programming with nested types in Haskell. High.-Order Symb. Comput. 22(2), 155–189 (2009). https://doi.org/10.1007/s10990-009-9047-7

    Article  MATH  Google Scholar 

  37. Kabanov, J., Vene, V.: Recursion schemes for dynamic programming. In: Uustalu, T. (ed.) MPC 2006. LNCS, vol. 4014, pp. 235–252. Springer, Heidelberg (2006). https://doi.org/10.1007/11783596_15

    Chapter  Google Scholar 

  38. Kmett, E.: Time for chronomorphisms. http://comonad.com/reader/2008/time-for-chronomorphisms/ (2008), accessed: 2020-06-15

  39. Kmett, E.: Recursion-schemes: representing common recursion patterns as higher-order functions (2011). https://hackage.haskell.org/package/recursion-schemes

  40. Lambek, J.: A fixpoint theorem for complete categories. Math. Z. 103, 151–161 (1968)

    Article  MathSciNet  Google Scholar 

  41. Malcolm, G.: Algebraic data types and program transformation. Ph.D. thesis, University of Groningen (1990)

    Google Scholar 

  42. Malcolm, G.: Data structures and program transformation. Sci. Comput. Program. 14(2–3), 255–280 (1990). https://doi.org/10.1016/0167-6423(90)90023-7

    Article  MathSciNet  MATH  Google Scholar 

  43. Meertens, L.: First Steps Towards the Theory of Rose Trees. CWI, Amsterdam (1988)

    Google Scholar 

  44. Meertens, L.: Paramorphisms. Formal Aspects Comput. 4(5), 413–424 (1992)

    Article  Google Scholar 

  45. Meijer, E., Fokkinga, M., Paterson, R.: Functional programming with bananas, lenses, envelopes and barbed wire. In: Hughes, J. (ed.) 5th ACM Conference on Functional Programming Languages and Computer Architecture. FPCA 1991, vol. 523, pp. 124–144 (1991). https://doi.org/10.1007/3540543961_7

  46. Okasaki, C.: Purely functional random-access lists. In: Proceedings of the Seventh International Conference on Functional Programming Languages and Computer Architecture. FPCA 1995, pp. 86–95. Association for Computing Machinery, New York (1995). https://doi.org/10.1145/224164.224187

  47. Pardo, A.: Monadic corecursion -definition, fusion laws, and applications-. Electron. Notes Theor. Comput. Sci. 11(C), 105–139 (1998). https://doi.org/10.1016/S1571-0661(04)00055-6

  48. Pardo, A.: Generic accumulations. In: Gibbons, J., Jeuring, J. (eds.) Generic Programming: IFIP TC2/WG2.1 Working Conference on Generic Programming. International Federation for Information Processing, vol. 115, pp. 49–78. Kluwer Academic Publishers (2002). https://doi.org/10.1007/978-0-387-35672-3_3

  49. Pardo, A.: Combining datatypes and effects. In: Vene, V., Uustalu, T. (eds.) AFP 2004. LNCS, vol. 3622, pp. 171–209. Springer, Heidelberg (2005). https://doi.org/10.1007/11546382_4

    Chapter  Google Scholar 

  50. Uustalu, T., Vene, V.: Primitive (co)recursion and course-of-value (co)iteration, categorically. Informatica 10(1), 5–26 (1999). https://doi.org/10.3233/INF-1999-10102

    Article  MathSciNet  MATH  Google Scholar 

  51. Uustalu, T., Vene, V.: Comonadic notions of computation, 203(5), 263–284 (2008). https://doi.org/10.1016/j.entcs.2008.05.029

  52. Uustalu, T., Vene, V., Pardo, A.: Recursion schemes from comonads. Nordic J. Comput. 8(3), 366–390 (2001)

    Google Scholar 

  53. Vene, V., Uustalu, T.: Functional programming with apomorphisms (corecursion). Proc. Estonian Acad. Sci. Phys. Math. 47(3), 147–161 (1998)

    MathSciNet  MATH  Google Scholar 

Download references

Acknowledgements

Particular thanks are due to Jeremy Gibbons for his numerous suggestions and comments. We would also like to thank the reviewers for the efforts in helping us to improve this paper.

We dedicate this paper to the memory of Richard Bird.

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Zhixuan Yang .

Editor information

Editors and Affiliations

Rights and permissions

Reprints and permissions

Copyright information

© 2022 The Author(s), under exclusive license to Springer Nature Switzerland AG

About this paper

Check for updates. Verify currency and authenticity via CrossMark

Cite this paper

Yang, Z., Wu, N. (2022). Fantastic Morphisms and Where to Find Them. In: Komendantskaya, E. (eds) Mathematics of Program Construction. MPC 2022. Lecture Notes in Computer Science, vol 13544. Springer, Cham. https://doi.org/10.1007/978-3-031-16912-0_9

Download citation

  • DOI: https://doi.org/10.1007/978-3-031-16912-0_9

  • Published:

  • Publisher Name: Springer, Cham

  • Print ISBN: 978-3-031-16911-3

  • Online ISBN: 978-3-031-16912-0

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics