A Duality of Sorts

  • Ralf Hinze
  • José Pedro Magalhães
  • Nicolas Wu
Part of the Lecture Notes in Computer Science book series (LNCS, volume 8106)


Sorting algorithms are one of the key pedagogical foundations of computer science, and their properties have been studied heavily. Perhaps less well known, however, is the fact that many of the basic sorting algorithms exist as a pair, and that these pairs arise naturally out of the duality between folds and unfolds. In this paper, we make this duality explicit, by showing how to define common sorting algorithms as folds of unfolds, or, dually, as unfolds of folds. This duality is preserved even when considering optimised sorting algorithms that require more exotic variations of folds and unfolds, and intermediary data structures. While all this material arises naturally from a categorical modelling of these recursion schemes, we endeavour to keep this presentation accessible to those not versed in abstract nonsense.


Sorting Algorithm Functional Programming Recursion Scheme Input List Empty List 
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.


Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.


  1. Braun, W., Rem, M.: A logarithmic implementation of flexible arrays. Memorandum MR83/4, Eindhoven University of Technology (1983)Google Scholar
  2. Gibbons, J., Jones, G.: The Under-Appreciated Unfold. In: Proceedings of the International Conference on Functional Programming, ICFP 1998, pp. 273–279. ACM (1998), doi:10.1145/289423.289455Google Scholar
  3. van Groningen, J., van Noort, T., Achten, P., Koopman, P., Plasmeijer, R.: Exchanging sources between Clean and Haskell: A double-edged front end for the Clean compiler. In: Proceedings of the Third ACM Haskell Symposium on Haskell, Haskell 2010, pp. 49–60. ACM (2010), doi:10.1145/1863523.1863530Google Scholar
  4. Hinze, R.: Generic programming with adjunctions. In: Gibbons, J. (ed.) Generic and Indexed Programming. LNCS, vol. 7470, pp. 47–129. Springer, Heidelberg (2012)CrossRefGoogle Scholar
  5. Hinze, R., James, D.W.H., Harper, T., Wu, N., Magalhães, J.P.: Sorting with bialgebras and distributive laws. In: Proceedings of the 8th ACM SIGPLAN Workshop on Generic Programming, WGP 2012, pp. 69–80. ACM (2012), doi:10.1145/2364394.2364405Google Scholar
  6. Knuth, D.E.: The Art of Computer Programming, 2nd edn. Sorting and Searching, vol. 3. Addison-Wesley (1998)Google Scholar
  7. Meertens, L.: Paramorphisms. Formal Aspects of Computing 4(5), 413–424 (1992), doi:10.1007/BF01211391CrossRefzbMATHGoogle Scholar
  8. Meijer, E., Fokkinga, M., Paterson, R.: Functional programming with bananas, lenses, envelopes and barbed wire. In: Hughes, J. (ed.) FPCA 1991. LNCS, vol. 523, pp. 124–144. Springer, Heidelberg (1991)CrossRefGoogle Scholar
  9. Peyton Jones, S., et al.: Haskell 98, Language and Libraries. The Revised Report. Cambridge University Press (2003), A special issue of JFPGoogle Scholar
  10. Sheard, T., Pasalic, T.: Two-level types and parameterized modules. Journal of Functional Programming 14(5), 547–587 (2004)CrossRefzbMATHGoogle Scholar
  11. Vene, V., Uustalu, T.: Functional programming with apomorphisms (corecursion). Proceedings of the Estonian Academy of Sciences: Physics, Mathematics 47(3), 147–161 (1998)MathSciNetzbMATHGoogle Scholar
  12. Wadler, P.: Deforestation: Transforming programs to eliminate trees. In: Ganzinger, H. (ed.) ESOP 1988. LNCS, vol. 300, pp. 344–358. Springer, Heidelberg (1988)CrossRefGoogle Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2013

Authors and Affiliations

  • Ralf Hinze
    • 1
  • José Pedro Magalhães
    • 1
  • Nicolas Wu
    • 1
  1. 1.Department of Computer ScienceUniversity of OxfordUK

Personalised recommendations