Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
skip to main content
research-article
Open access

Closure-Free Functional Programming in a Two-Level Type Theory

Published: 15 August 2024 Publication History

Abstract

Many abstraction tools in functional programming rely heavily on general-purpose compiler optimization to achieve adequate performance. For example, monadic binding is a higher-order function which yields runtime closures in the absence of sufficient compile-time inlining and beta-reductions, thereby significantly degrading performance. In current systems such as the Glasgow Haskell Compiler, there is no strong guarantee that general-purpose optimization can eliminate abstraction overheads, and users only have indirect and fragile control over code generation through inlining directives and compiler options. We propose a two-stage language to simultaneously get strong guarantees about code generation and strong abstraction features. The object language is a simply-typed first-order language which can be compiled without runtime closures. The compile-time language is a dependent type theory. The two are integrated in a two-level type theory. We demonstrate two applications of the system. First, we develop monads and monad transformers. Here, abstraction overheads are eliminated by staging and we can reuse almost all definitions from the existing Haskell ecosystem. Second, we develop pull-based stream fusion. Here we make essential use of dependent types to give a concise definition of a concatMap operation with guaranteed fusion. We provide an Agda implementation and a typed Template Haskell implementation of these developments.

References

[1]
Agda developers. 2024. Agda documentation. https://agda.readthedocs.io/en/v2.6.4.2/
[2]
Danil Annenkov, Paolo Capriotti, Nicolai Kraus, and Christian Sattler. 2019. Two-Level Type Theory and Applications. ArXiv e-prints, may, arxiv:1705.03307
[3]
Anders Bondorf. 1992. Improving Binding Times Without Explicit CPS-Conversion. In Proceedings of the Conference on Lisp and Functional Programming, LFP 1992, San Francisco, California, USA, 22-24 June 1992, Jon L. White (Ed.). ACM, 1–10. https://doi.org/10.1145/141471.141483
[4]
Simon Boulier, Pierre-Marie Pédrot, and Nicolas Tabareau. 2017. The Next 700 Syntactical Models of Type Theory. In Proceedings of the 6th ACM SIGPLAN Conference on Certified Programs and Proofs (CPP 2017). ACM, New York, NY, USA. 182–194. isbn:978-1-4503-4705-1 https://doi.org/10.1145/3018610.3018620
[5]
Joachim Breitner. 2014. Call Arity. In Trends in Functional Programming - 15th International Symposium, TFP 2014, Soesterberg, The Netherlands, May 26-28, 2014. Revised Selected Papers, Jurriaan Hage and Jay McCarthy (Eds.) (Lecture Notes in Computer Science, Vol. 8843). Springer, 34–50. https://doi.org/10.1007/978-3-319-14675-1_3
[6]
Jacques Carette and Oleg Kiselyov. 2011. Multi-stage programming with functors and monads: Eliminating abstraction overhead from generic code. Sci. Comput. Program., 76, 5 (2011), 349–375. https://doi.org/10.1016/J.SCICO.2008.09.008
[7]
John Cartmell. 1978. Generalised algebraic theories and contextual categories. Ph. D. Dissertation. Oxford University.
[8]
Wontae Choi, Baris Aktemur, Kwangkeun Yi, and Makoto Tatsuta. 2011. Static analysis of multi-staged programs via unstaging translation. In Proceedings of the 38th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2011, Austin, TX, USA, January 26-28, 2011, Thomas Ball and Mooly Sagiv (Eds.). ACM, 81–92. https://doi.org/10.1145/1926385.1926397
[9]
Duncan Coutts. 2011. Stream fusion : practical shortcut fusion for coinductive sequence types. Ph. D. Dissertation. University of Oxford, UK. http://ora.ox.ac.uk/objects/uuid:b4971f57-2b94-4fdf-a5c0-98d6935a44da
[10]
Olivier Danvy, Karoline Malmkjæ r, and Jens Palsberg. 1996. Eta-Expansion Does The Trick. ACM Trans. Program. Lang. Syst., 18, 6 (1996), 730–751. https://doi.org/10.1145/236114.236119
[11]
Olivier Danvy and Lasse R. Nielsen. 2001. Defunctionalization at Work. In Proceedings of the 3rd international ACM SIGPLAN conference on Principles and practice of declarative programming, September 5-7, 2001, Florence, Italy. ACM, 162–174. https://doi.org/10.1145/773184.773202
[12]
Edsko de Vries and Andres Löh. 2014. True sums of products. In Proceedings of the 10th ACM SIGPLAN workshop on Generic programming, WGP 2014, Gothenburg, Sweden, August 31, 2014, José Pedro Magalhães and Tiark Rompf (Eds.). ACM, 83–94. https://doi.org/10.1145/2633628.2633634
[13]
Edsko de Vries and Andres Löh. 2014. True sums of products. In Proceedings of the 10th ACM SIGPLAN workshop on Generic programming, WGP 2014, Gothenburg, Sweden, August 31, 2014, José Pedro Magalhães and Tiark Rompf (Eds.). ACM, 83–94. https://doi.org/10.1145/2633628.2633634
[14]
Paul Downen, Zena M. Ariola, Simon Peyton Jones, and Richard A. Eisenberg. 2020. Kinds are calling conventions. Proc. ACM Program. Lang., 4, ICFP (2020), 104:1–104:29. https://doi.org/10.1145/3408986
[15]
Peter Dybjer. 1994. Inductive Families. Formal Aspects Comput., 6, 4 (1994), 440–465. https://doi.org/10.1007/BF01211308
[16]
Peter Dybjer. 1995. Internal Type Theory. In Types for Proofs and Programs, International Workshop TYPES’95, Torino, Italy, June 5-8, 1995, Selected Papers, Stefano Berardi and Mario Coppo (Eds.) (Lecture Notes in Computer Science, Vol. 1158). Springer, 120–134. https://doi.org/10.1007/3-540-61780-9_66
[17]
Richard A. Eisenberg and Stephanie Weirich. 2012. Dependently typed programming with singletons. In Proceedings of the 5th ACM SIGPLAN Symposium on Haskell, Haskell 2012, Copenhagen, Denmark, 13 September 2012, Janis Voigtländer (Ed.). ACM, 117–130. https://doi.org/10.1145/2364506.2364522
[18]
Cormac Flanagan, Amr Sabry, Bruce F. Duba, and Matthias Felleisen. 1993. The Essence of Compiling with Continuations. In Proceedings of the ACM SIGPLAN’93 Conference on Programming Language Design and Implementation (PLDI), Albuquerque, New Mexico, USA, June 23-25, 1993, Robert Cartwright (Ed.). ACM, 237–247. https://doi.org/10.1145/155090.155113
[19]
GHC developers. 2024. GHC documentation. https://downloads.haskell.org/ghc/9.8.2/docs/users_guide/
[20]
GHC developers. 2024. GHC.Base source. https://hackage.haskell.org/package/base-4.19.1.0/docs/src/GHC.Base.html##.
[21]
Robert Harper, Furio Honsell, and Gordon Plotkin. 1993. A framework for defining logics. J. ACM, 40, 1 (1993), Jan., 143–184. issn:0004-5411 https://doi.org/10.1145/138027.138060
[22]
Ralf Hinze, Thomas Harper, and Daniel W. H. James. 2010. Theory and Practice of Fusion. In Implementation and Application of Functional Languages - 22nd International Symposium, IFL 2010, Alphen aan den Rijn, The Netherlands, September 1-3, 2010, Revised Selected Papers, Jurriaan Hage and Marco T. Morazán (Eds.) (Lecture Notes in Computer Science, Vol. 6647). Springer, 19–37. https://doi.org/10.1007/978-3-642-24276-2_2
[23]
Martin Hofmann. 1995. Conservativity of Equality Reflection over Intensional Type Theory. In TYPES 95. 153–164.
[24]
Martin Hofmann. 1997. Syntax and Semantics of Dependent Types. In Semantics and Logics of Computation. Cambridge University Press, 79–130.
[25]
Martin Hofmann. 1999. Semantical Analysis of Higher-Order Abstract Syntax. In Proceedings of the 14th Annual IEEE Symposium on Logic in Computer Science (LICS ’99). IEEE Computer Society, Washington, DC, USA. 204–. isbn:0-7695-0158-3 http://dl.acm.org/citation.cfm?id=788021.788940
[26]
Simon Huber. 2016. Cubical Interpretations of Type Theory. Ph. D. Dissertation. University of Gothenburg.
[27]
Jasper Hugunin. 2020. Why Not W? In 26th International Conference on Types for Proofs and Programs, TYPES 2020, March 2-5, 2020, University of Turin, Italy, Ugo de’Liguoro, Stefano Berardi, and Thorsten Altenkirch (Eds.) (LIPIcs, Vol. 188). Schloss Dagstuhl - Leibniz-Zentrum für Informatik, 8:1–8:9. https://doi.org/10.4230/LIPICS.TYPES.2020.8
[28]
Neil D. Jones, Carsten K. Gomard, and Peter Sestoft. 1993. Partial evaluation and automatic program generation. Prentice Hall. isbn:978-0-13-020249-9
[29]
Simon Peyton Jones. 2001. Tackling the awkward squad: monadic input/output, concurrency, exceptions, and foreign-language calls in Haskell. NATO SCIENCE SERIES SUB SERIES III COMPUTER AND SYSTEMS SCIENCES, 180 (2001), 47–96.
[30]
Oleg Kiselyov, Aggelos Biboudis, Nick Palladinos, and Yannis Smaragdakis. 2017. Stream fusion, to completeness. In Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages, POPL 2017, Paris, France, January 18-20, 2017, Giuseppe Castagna and Andrew D. Gordon (Eds.). ACM, 285–299. https://doi.org/10.1145/3009837.3009880
[31]
Oleg Kiselyov, Kedar N. Swadi, and Walid Taha. 2004. A methodology for generating verified combinatorial circuits. In EMSOFT 2004, September 27-29, 2004, Pisa, Italy, Fourth ACM International Conference On Embedded Software, Proceedings, Giorgio C. Buttazzo (Ed.). ACM, 249–258. https://doi.org/10.1145/1017753.1017794
[32]
Tomoaki Kobayashi and Oleg Kiselyov. 2024. Complete Stream Fusion for Software-Defined Radio. In Proceedings of the 2024 ACM SIGPLAN International Workshop on Partial Evaluation and Program Manipulation, PEPM 2024, London, UK, 16 January 2024, Gabriele Keller and Meng Wang (Eds.). ACM, 57–69. https://doi.org/10.1145/3635800.3636962
[33]
András Kovács. 2022. Staged Compilation with Two-Level Type Theory. Proc. ACM Program. Lang., 6, ICFP (2022), 540–569. https://doi.org/10.1145/3547641
[34]
András Kovács. 2023. Type-Theoretic Signatures for Algebraic Theories and Inductive Types. CoRR, abs/2302.08837 (2023), https://doi.org/10.48550/ARXIV.2302.08837 arXiv:2302.08837.
[35]
András Kovács. 2022. Demo implementation for the paper "Staged Compilation With Two-Level Type Theory". https://doi.org/10.5281/zenodo.6757373
[36]
András Kovács. 2024. Artifacts for the paper "Closure-Free Functional Programming in a Two-Level Type Theory". https://doi.org/10.5281/zenodo.12656335
[37]
Xavier Leroy, Damien Doligez, Alain Frisch, Jacques Garrigue, Didier Rémy, KC Sivaramakrishnan, and Jérôme Vouillon. 2023. The OCaml system release 5.1: Documentation and user’s manual. https://v2.ocaml.org/manual/
[38]
Paul Blain Levy. 1999. Call-by-Push-Value: A Subsuming Paradigm. In Typed Lambda Calculi and Applications, 4th International Conference, TLCA’99, L’Aquila, Italy, April 7-9, 1999, Proceedings, Jean-Yves Girard (Ed.) (Lecture Notes in Computer Science, Vol. 1581). Springer, 228–242. https://doi.org/10.1007/3-540-48959-2_17
[39]
Sheng Liang, Paul Hudak, and Mark P. Jones. 1995. Monad Transformers and Modular Interpreters. In Conference Record of POPL’95: 22nd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, San Francisco, California, USA, January 23-25, 1995, Ron K. Cytron and Peter Lee (Eds.). ACM Press, 333–343. https://doi.org/10.1145/199448.199528
[40]
Luke Maurer, Paul Downen, Zena M. Ariola, and Simon L. Peyton Jones. 2017. Compiling without continuations. In Proceedings of the 38th ACM SIGPLAN Conference on Programming Language Design and Implementation, PLDI 2017, Barcelona, Spain, June 18-23, 2017, Albert Cohen and Martin T. Vechev (Eds.). ACM, 482–494. https://doi.org/10.1145/3062341.3062380
[41]
mtl developers. 2024. mtl documentation. https://hackage.haskell.org/package/mtl
[42]
Aleksandar Nanevski, Frank Pfenning, and Brigitte Pientka. 2008. Contextual modal type theory. ACM Trans. Comput. Log., 9, 3 (2008), 23:1–23:49. https://doi.org/10.1145/1352582.1352591
[43]
Flemming Nielson. 1984. Abstract interpretation using domain theory. Ph. D. Dissertation. University of Edinburgh, UK. https://ethos.bl.uk/OrderDetails.do?uin=uk.bl.ethos.350060
[44]
Flemming Nielson and Hanne Riis Nielson. 1992. Two-level functional languages (Cambridge tracts in theoretical computer science, Vol. 34). Cambridge University Press. isbn:978-0-521-40384-9
[45]
Matthew Pickering, Andres Löh, and Nicolas Wu. 2020. Staged sums of products. In Proceedings of the 13th ACM SIGPLAN International Symposium on Haskell, Haskell@ICFP 2020, Virtual Event, USA, August 7, 2020, Tom Schrijvers (Ed.). ACM, 122–135. https://doi.org/10.1145/3406088.3409021
[46]
Brigitte Pientka, Andreas Abel, Francisco Ferreira, David Thibodeau, and Rébecca Zucchini. 2019. Cocon: Computation in Contextual Type Theory. CoRR, abs/1901.03378 (2019), arXiv:1901.03378. arxiv:1901.03378
[47]
John C. Reynolds. 1998. Definitional Interpreters for Higher-Order Programming Languages. High. Order Symb. Comput., 11, 4 (1998), 363–397. https://doi.org/10.1023/A:1010027404223
[48]
Amos Robinson and Ben Lippmeier. 2017. Machine fusion: merging merges, more or less. In Proceedings of the 19th International Symposium on Principles and Practice of Declarative Programming, Namur, Belgium, October 09 - 11, 2017, Wim Vanhoof and Brigitte Pientka (Eds.). ACM, 139–150. https://doi.org/10.1145/3131851.3131865
[49]
Tiark Rompf and Martin Odersky. 2012. Lightweight modular staging: a pragmatic approach to runtime code generation and compiled DSLs. Commun. ACM, 55, 6 (2012), 121–130. https://doi.org/10.1145/2184319.2184345
[50]
Kedar N. Swadi, Walid Taha, Oleg Kiselyov, and Emir Pasalic. 2006. A monadic approach for avoiding code duplication when staging memoized functions. In Proceedings of the 2006 ACM SIGPLAN Workshop on Partial Evaluation and Semantics-based Program Manipulation, 2006, Charleston, South Carolina, USA, January 9-10, 2006, John Hatcliff and Frank Tip (Eds.). ACM, 160–169. https://doi.org/10.1145/1111542.1111570
[51]
Walid Taha and Tim Sheard. 2000. MetaML and multi-stage programming with explicit annotations. Theor. Comput. Sci., 248, 1-2 (2000), 211–242. https://doi.org/10.1016/S0304-3975(00)00053-0
[52]
Vladimir Voevodsky. 2013. A simple type system with two identity types. Unpublished note
[53]
Philip Wadler. 1989. Theorems for free!. In Functional Programming Languages and Computer Architecture. ACM Press, 347–359.
[54]
Philip Wadler and Stephen Blott. 1989. How to Make ad-hoc Polymorphism Less ad-hoc. In Conference Record of the Sixteenth Annual ACM Symposium on Principles of Programming Languages, Austin, Texas, USA, January 11-13, 1989. ACM Press, 60–76. https://doi.org/10.1145/75277.75283
[55]
Ningning Xie, Matthew Pickering, Andres Löh, Nicolas Wu, Jeremy Yallop, and Meng Wang. 2022. Staging with class: a specification for typed template Haskell. Proc. ACM Program. Lang., 6, POPL (2022), 1–30. https://doi.org/10.1145/3498723
[56]
Jeremy Yallop and Oleg Kiselyov. 2019. Generating mutually recursive definitions. In Proceedings of the 2019 ACM SIGPLAN Workshop on Partial Evaluation and Program Manipulation, PEPM@POPL 2019, Cascais, Portugal, January 14-15, 2019, Manuel V. Hermenegildo and Atsushi Igarashi (Eds.). ACM, 75–81. https://doi.org/10.1145/3294032.3294078

Recommendations

Comments

Information & Contributors

Information

Published In

cover image Proceedings of the ACM on Programming Languages
Proceedings of the ACM on Programming Languages  Volume 8, Issue ICFP
August 2024
1031 pages
EISSN:2475-1421
DOI:10.1145/3554318
Issue’s Table of Contents
This work is licensed under a Creative Commons Attribution International 4.0 License.

Publisher

Association for Computing Machinery

New York, NY, United States

Publication History

Published: 15 August 2024
Published in PACMPL Volume 8, Issue ICFP

Permissions

Request permissions for this article.

Check for updates

Badges

Author Tags

  1. staged compilation
  2. two-level type theory

Qualifiers

  • Research-article

Funding Sources

  • Knut and Alice Wallenberg Foundation

Contributors

Other Metrics

Bibliometrics & Citations

Bibliometrics

Article Metrics

  • 0
    Total Citations
  • 460
    Total Downloads
  • Downloads (Last 12 months)460
  • Downloads (Last 6 weeks)67
Reflects downloads up to 20 Feb 2025

Other Metrics

Citations

View Options

View options

PDF

View or Download as a PDF file.

PDF

eReader

View online with eReader.

eReader

Login options

Full Access

Figures

Tables

Media

Share

Share

Share this Publication link

Share on social media