skip to main content
research-article
Open Access

Compiling to categories

Published:29 August 2017Publication History
Skip Abstract Section

Abstract

It is well-known that the simply typed lambda-calculus is modeled by any cartesian closed category (CCC). This correspondence suggests giving typed functional programs a variety of interpretations, each corresponding to a different category. A convenient way to realize this idea is as a collection of meaning-preserving transformations added to an existing compiler, such as GHC for Haskell. This paper describes such an implementation and demonstrates its use for a variety of interpretations including hardware circuits, automatic differentiation, incremental computation, and interval analysis. Each such interpretation is a category easily defined in Haskell (outside of the compiler). The general technique appears to provide a compelling alternative to deeply embedded domain-specific languages.

References

  1. Umut Acar. Self-adjusting computation . PhD thesis, School of Computer Science, Carnegie Mellon University, May 2005.Google ScholarGoogle Scholar
  2. Umut A. Acar, Guy E. Blelloch, Matthias Blume, and Robert Harper. Self-adjusting programming . In ML Workshop, 2005.Google ScholarGoogle Scholar
  3. Steve Awodey. Category theory, volume 49 of Oxford Logic Guides. Oxford University Press, 2006.Google ScholarGoogle Scholar
  4. Christiaan Baaij and Jan Kuper. Using rewriting to synthesize functional languages to digital circuits . In Trends in Functional Programming, Lecture Notes in Computer Science , pages 17–33, 2014.Google ScholarGoogle Scholar
  5. John Backus. Can programming be liberated from the von Neumann style? A functional style and its algebra of programs . Communications of the ACM , 21(8):613–641, August 1978. Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. Michael Barr. Fixed points in cartesian closed categories . Theoretical Computer Science, 70(1):65–72, 1990. Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. Per Bjesse, Koen Claessen, Mary Sheeran, and Satnam Singh. Lava: hardware design in Haskell . In ICFP, 1998.Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. Guy E. Blelloch. Prefix Sums and Their Applications . Technical Report CMU-CS-90-190, School of Computer Science, Carnegie Mellon University, November 1990.Google ScholarGoogle Scholar
  9. Max Bolingbroke. Constraint kinds for GHC. http://blog.omega-prime.co.uk/?p=127 , 2011.Google ScholarGoogle Scholar
  10. Richard Boulton, Andrew Gordon, Mike Gordon, John Harrison, John Herbert, and John Van Tassel. Experience with embedding hardware description languages in HOL . In Proceedings of the IFIP TC10/WG 10.2 International Conference on Theorem Provers in Circuit Design: Theory, Practice and Experience , volume A-10, pages 129–156, 1993.Google ScholarGoogle Scholar
  11. Yufei Cai, Paolo G. Giarrusso, Tillmann Rendel, and Klaus Ostermann. A theory of changes for higher-order languages: incrementalizing λ-calculi by static differentiation . In PLDI ’14, pages 145–155, 2014.Google ScholarGoogle Scholar
  12. Magnus Carlsson. Monads for incremental computing . In ICFP, pages 26–35, 2002.Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. Manuel M. T. Chakravarty, Gabriele Keller, and Simon Peyton Jones. Associated type synonyms . In ICFP, 2005a.Google ScholarGoogle ScholarDigital LibraryDigital Library
  14. Manuel M. T. Chakravarty, Gabriele Keller, Simon Peyton Jones, and Simon Marlow. Associated types with class . In Principles of Programming Languages , 2005b.Google ScholarGoogle Scholar
  15. Mark Chu-Carroll. Interpreting lambda calculus using closed cartesian categories. http://goodmath.scientopia.org/2012/03/ 11/interpreting-lambda-calculus-using-closed-cartesian-categories/ , March 2012.Google ScholarGoogle Scholar
  16. Koen Claessen and David Sands. In Asian Computing Science Conference, 1999.Google ScholarGoogle Scholar
  17. Guy Cousineau, Pierre-Louis Curien, and Michel Mauny. The categorical abstract machine . Science of Computer Programming, 8, 1987. Google ScholarGoogle ScholarDigital LibraryDigital Library
  18. Pierre-Louis Curien. Categorical combinators. Information and Control, 69(1-3):188–254, 1986. Google ScholarGoogle ScholarDigital LibraryDigital Library
  19. Leonardo De Moura and Nikolaj Bjørner. Z3: An efficient SMT solver . In Theory and Practice of Software, International Conference on Tools and Algorithms for the Construction and Analysis of Systems , pages 337–340, 2008. Google ScholarGoogle ScholarCross RefCross Ref
  20. Conal Elliott. Programming graphics processors functionally . In Haskell Workshop, 2004. Google ScholarGoogle ScholarDigital LibraryDigital Library
  21. Conal Elliott. Beautiful differentiation . In International Conference on Functional Programming, 2009. Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. Conal Elliott. Generic functional parallel algorithms: Scan and FFT . Proc. ACM Program. Lang., 1(ICFP), September 2017.Google ScholarGoogle ScholarDigital LibraryDigital Library
  23. Conal Elliott, Sigbjørn Finne, and Oege de Moor. Compiling embedded languages . Journal of Functional Prog., 13(2), 2003. Google ScholarGoogle ScholarDigital LibraryDigital Library
  24. Andrew Farmer, Andy Gill, Ed Komp, and Neil Sculthorpe. The HERMIT in the machine: A plugin for the interactive transformation of GHC core language programs . Haskell Symposium, pages 1–12, 2012. Google ScholarGoogle ScholarDigital LibraryDigital Library
  25. GHC Team. The Glorious Glasgow Haskell compilation system user’s guide, version 8.0.1. https://downloads.haskell.org/ ~ghc/latest/docs/html/users_guide , 2016.Google ScholarGoogle Scholar
  26. Jeremy Gibbons. Calculating functional programs . In Algebraic and Coalgebraic Methods in the Mathematics of Program Construction , volume 2297 of Lecture Notes in Computer Science. Springer-Verlag, 2002.Google ScholarGoogle ScholarCross RefCross Ref
  27. Jeremy Gibbons and Nicolas Wu. Folding domain-specific languages: Deep and shallow embeddings . In ICFP, 2014.Google ScholarGoogle Scholar
  28. Andy Gill. Type-safe observable sharing in Haskell . In Haskell Symposium, pages 117–128, September 2009. Google ScholarGoogle ScholarDigital LibraryDigital Library
  29. Andy Gill. Domain-specific languages and code synthesis using Haskell . ACM Queue, 12(4), April 2014. Google ScholarGoogle ScholarDigital LibraryDigital Library
  30. Andy Gill and Graham Hutton. The worker/wrapper transformation . Journal of Functional Prog., pages 227–251, 2009.Google ScholarGoogle ScholarDigital LibraryDigital Library
  31. Ralf Hinze. Memo functions, polytypically! In Workshop on Generic Programming, pages 17–32, 2000.Google ScholarGoogle Scholar
  32. Ralf Hinze. Fun with phantom types . In The fun of programming. Palgrave, 2003. Google ScholarGoogle ScholarCross RefCross Ref
  33. Ralf Hinze. Polytypic values possess polykinded types . In Science of Computer Programming, pages 2–27, June 2004.Google ScholarGoogle Scholar
  34. John Hughes. The design of a pretty-printing library . In Advanced Functional Programming, pages 53–96, 1995.Google ScholarGoogle ScholarCross RefCross Ref
  35. John Hughes. Generalising monads to arrows . Science of Computer Programming, 37:67–111, 1998. Google ScholarGoogle ScholarDigital LibraryDigital Library
  36. John Hughes and Simon Peyton Jones. The pretty package. https://hackage.haskell.org/package/pretty , November 2007. Haskell library.Google ScholarGoogle Scholar
  37. Geraint Jones and Mary Sheeran. Circuit design in Ruby . Formal methods for VLSI design, 1, 1990.Google ScholarGoogle Scholar
  38. Mark P. Jones. Dictionary-free overloading by partial evaluation . In In ACM SIGPLAN Workshop on Partial Evaluation and Semantics-Based Program Manipulation , pages 107–117, 1994.Google ScholarGoogle Scholar
  39. Jerzy Karczmarczuk. Functional differentiation of computer programs . In ICFP, pages 195–203, 1998.Google ScholarGoogle ScholarDigital LibraryDigital Library
  40. Edward Kmett. What constraints entail: Part 1. http://comonad.com/reader/2011/what-constraints-entail-part-1/ , 2011.Google ScholarGoogle Scholar
  41. Joachim Lambek. From λ-calculus to cartesian closed categories. In J.P. Seldin and J.R. Hindley, editors, To H.B. Curry: Essays on Combinatory Logic, Lambda Calculus, and Formalism . Academic Press, 1980.Google ScholarGoogle Scholar
  42. Joachim Lambek. Cartesian closed categories and typed lambda-calculi. In Thirteenth Spring School of the LITP on Combinators and Functional Programming Languages , pages 136–175, 1986.Google ScholarGoogle ScholarCross RefCross Ref
  43. F. William Lawvere and Stephen H. Schanuel. Conceptual Mathematics: A First Introduction to Categories. Cambridge University Press, 2nd edition, 2009. Google ScholarGoogle ScholarCross RefCross Ref
  44. Daan Leijen and Erik Meijer. Domain specific embedded compilers . In Conference on Domain-Specific Languages, pages 109–122, 1999. Google ScholarGoogle ScholarDigital LibraryDigital Library
  45. José Pedro Magalhães, Atze Dijkstra, Johan Jeuring, and Andres Löh. A generic deriving mechanism for Haskell . In Haskell Symposium , pages 37–48, 2010. Google ScholarGoogle ScholarDigital LibraryDigital Library
  46. José Pedro Magalhães et al. GHC.Generics, 2011. URL https://wiki.haskell.org/GHC.Generics . Haskell wiki page.Google ScholarGoogle Scholar
  47. M. Douglas McIlroy. Power series, power serious . Journal of Functional Programming, 9(3):325–337, 1999. Google ScholarGoogle ScholarDigital LibraryDigital Library
  48. R.E. Moore. Interval analysis. Series in automatic computation. Prentice-Hall, 1966.Google ScholarGoogle Scholar
  49. Philip S. Mulry. Categorical fixed point semantics . Theoretical Computer Science, 70(1):85–97, January 1990. Google ScholarGoogle ScholarDigital LibraryDigital Library
  50. Simon Peyton Jones and John Launchbury. Unboxed values as first class citizens in a non-strict functional language . In Functional programming languages and computer architecture , pages 636–666, 1991.Google ScholarGoogle Scholar
  51. Simon Peyton Jones and Simon Marlow. Secrets of the Glasgow Haskell compiler inliner . Journal of Functional Programming, 12(5), July 2002.Google ScholarGoogle Scholar
  52. Simon Peyton Jones, Andrew Tolmach, and Tony Hoare. Playing by the rules: Rewriting as a practical optimisation technique in GHC . In Haskell Workshop, pages 203–233, 2001.Google ScholarGoogle Scholar
  53. Simon L. Peyton Jones. Compiling Haskell by program transformation: A report from the trenches . In European Symposium on Programming , pages 18–44, 1996.Google ScholarGoogle Scholar
  54. Neil Sculthorpe, Jan Bracker, George Giorgidze, and Andy Gill. The constrained-monad problem . In International Conference on Functional Programming , pages 287–298, 2013a. Google ScholarGoogle ScholarDigital LibraryDigital Library
  55. Neil Sculthorpe, Andrew Farmer, and Andy Gill. The HERMIT in the tree: Mechanizing program transformations in the GHC core language . In Symposium on Implementation and Application of Functional Languages, pages 86–103, 2013b. Google ScholarGoogle ScholarCross RefCross Ref
  56. Mary Sheeran. muFP, a language for VLSI design . In Symposium on LISP and Functional Programming, pages 104–112, 1984.Google ScholarGoogle ScholarDigital LibraryDigital Library
  57. Alex Simpson and Gordon Plotkin. Complete axioms for categorical fixed-point operators . In Logic in Computer Science, pages 30–41, 2000.Google ScholarGoogle Scholar
  58. Jeffrey Mark Siskind and Barak A. Pearlmutter. Perturbation confusion and referential transparency: Correct functional implementation of forward-mode AD . In Implementation and Application of Functional Languages, pages 1–9, 2005.Google ScholarGoogle Scholar
  59. Jeffrey Mark Siskind and Barak A. Pearlmutter. Nesting forward-mode AD in a functional framework . Higher Order Symbolic Computation , 21(4):361–376, 2008. Google ScholarGoogle ScholarDigital LibraryDigital Library
  60. Michael Spivak. Calculus on Manifolds: A Modern Approach to Classical Theorems of Advanced Calculus. HarperCollins Publishers, 1971.Google ScholarGoogle Scholar
  61. Martin Sulzmann, Manuel M. T. Chakravarty, Simon L. Peyton Jones, and Kevin Donnelly. System F with type equality coercions . In Types In Languages Design And Implementation, pages 53–66, 2007.Google ScholarGoogle Scholar
  62. Pierre Weis et al. A history of Caml, 2005. URL https://caml.inria.fr/about/history.en.html . Last updated 2005-01-28.Google ScholarGoogle Scholar
  63. R. E. Wengert. A simple automatic derivative evaluation program. Communications of the ACM, 7(8):463–464, 1964. Google ScholarGoogle ScholarDigital LibraryDigital Library
  64. Brent A. Yorgey, Stephanie Weirich, Julien Cretin, Simon L. Peyton Jones, Dimitrios Vytiniotis, and José Pedro Magalhães. Giving Haskell a promotion . In Types In Languages Design And Implementation, pages 53–66. ACM, 2012.Google ScholarGoogle Scholar

Index Terms

  1. Compiling to categories

        Recommendations

        Comments

        Login options

        Check if you have access through your login credentials or your institution to get full access on this article.

        Sign in

        Full Access

        • Published in

          cover image Proceedings of the ACM on Programming Languages
          Proceedings of the ACM on Programming Languages  Volume 1, Issue ICFP
          September 2017
          1173 pages
          EISSN:2475-1421
          DOI:10.1145/3136534
          Issue’s Table of Contents

          Copyright © 2017 Owner/Author

          This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike International 4.0 License.

          Publisher

          Association for Computing Machinery

          New York, NY, United States

          Publication History

          • Published: 29 August 2017
          Published in pacmpl Volume 1, Issue ICFP

          Permissions

          Request permissions about this article.

          Request Permissions

          Check for updates

          Qualifiers

          • research-article

        PDF Format

        View or Download as a PDF file.

        PDF

        eReader

        View online with eReader.

        eReader