Skip to main content

A Sound Strategy to Compile General Recursion into Finite Depth Pattern Matching

  • Conference paper
  • First Online:
Formal Methods: Foundations and Applications (SBMF 2022)

Abstract

Programming languages are popular and diverse, and the convenience of programmatically changing the behavior of complex systems is attractive even for the ones with stringent security requirements, which often impose restrictions on the acceptable programs. A very common restriction is that the program must terminate, which is very hard to check because the Halting Problem is undecidable. In this work, we proposed a technique to unroll recursive programs in functional languages to create terminating versions of them. We prove that our strategy itself is guaranteed to terminate. We also formalize term generation and run property-based tests to build confidence that the semantics is preserved through the transformation. Our strategy can be used to compile general purpose functional languages to restrictive targets such as the eBPF and smart contracts for blockchain networks.

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 44.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 59.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.

    Version 5.3 and higher has support for bounded loops only.

  2. 2.

    The term “fuel” is inspired by Petrol Semantics. It is presented, for instance, in [16].

References

  1. Abel, A.: Foetus—termination checker for simple functional programs. Technical report, Ludwigs-Maximilians-University, Munich (1998)

    Google Scholar 

  2. Abel, A.: Specification and verification of a formal system for structurally recursive functions. In: Coquand, T., Dybjer, P., Nordström, B., Smith, J. (eds.) TYPES 1999. LNCS, vol. 1956, pp. 1–20. Springer, Heidelberg (2000). https://doi.org/10.1007/3-540-44557-9_1

    Chapter  Google Scholar 

  3. Abel, A., Altenkirch, T.: A semantical analysis of structural recursion. In: Fourth International Workshop on Termination WST, pp. 24–25. Darmstadt University of Technology, Dagstuhl, Germany (1999)

    Google Scholar 

  4. Abel, A., Altenkirch, T.: A predicative analysis of structural recursion. J. Funct. Program. 12(1), 1–41 (2002)

    Article  MathSciNet  MATH  Google Scholar 

  5. Barthe, G., et al.: Type-based termination of recursive definitions. Math. Struct. Comput. Sci. 14(1), 97–141 (2004)

    Article  MathSciNet  MATH  Google Scholar 

  6. Appel, A.: Modern Compiler Implementation in ML. Cambridge University Press, New York (2004)

    MATH  Google Scholar 

  7. Authors of eBPF: eBPF - Introduction, Tutorials and Community Resources (2022). https://ebpf.io/

  8. Barthe, G., Grégoire, B., Riba, C.: A tutorial on type-based termination. In: Bove, A., Barbosa, L.S., Pardo, A., Pinto, J.S. (eds.) LerNet 2008. LNCS, vol. 5520, pp. 100–152. Springer, Heidelberg (2009). https://doi.org/10.1007/978-3-642-03153-3_3

    Chapter  Google Scholar 

  9. Barthe, G., Grégoire, B., Riba, C.: Type-based termination with sized products. In: Kaminski, M., Martini, S. (eds.) CSL 2008. LNCS, vol. 5213, pp. 493–507. Springer, Heidelberg (2008). https://doi.org/10.1007/978-3-540-87531-4_35

    Chapter  Google Scholar 

  10. Ringell Developers: Ringell (2022). https://github.com/mayconamaro/ringell

  11. Feitosa, S., Ribeiro, R., Du Bois, A.: A type-directed algorithm to generate random well-typed Java 8 programs. Sci. Comput. Program. 196, 102494 (2020)

    Article  Google Scholar 

  12. GNU: GCC, the GNU Compiler Collection (2022). https://gcc.gnu.org/

  13. Huet, G., Kahn, G., Paulin-Mohring, C.: The CoQ proof assistant a tutorial. Rapport Technique 178 (1997)

    Google Scholar 

  14. Le, T.C., Xu, L., Chen, L., Shi, W.: Proving conditional termination for smart contracts. In: Proceedings of the 2nd ACM Workshop on Blockchains, Cryptocurrencies, and Contracts, BCC 2018, pp. 57–59. Association for Computing Machinery, New York (2018). https://doi.org/10.1145/3205230.3205239

  15. LLVM Project: Clang C Language Family Frontend for LLVM (2022). https://clang.llvm.org/

  16. McBride, C.: Turing-completeness totally free. In: Hinze, R., Voigtländer, J. (eds.) MPC 2015. LNCS, vol. 9129, pp. 257–275. Springer, Cham (2015). https://doi.org/10.1007/978-3-319-19797-5_13

    Chapter  Google Scholar 

  17. Microsoft: Visual Studio Compiler for Windows (2022). https://visualstudio.microsoft.com/cplusplus/

  18. Norell, U.: Towards a practical programming language based on dependent type theory. Ph.D. thesis, Chalmers University of Technology and Göteborg University, Sweden (2007)

    Google Scholar 

  19. O’Sullivan, B., Goerzen, J., Stewart, D.: Real World Haskell. O’Reilly, Sebastopol (2008)

    Google Scholar 

  20. Pierce, B.C.: Types and Programming Languages. MIT Press, Cambridge (2002)

    MATH  Google Scholar 

  21. Rugina, R., Rinard, M.: Recursion unrolling for divide and conquer programs. In: Midkiff, S.P., et al. (eds.) LCPC 2000. LNCS, vol. 2017, pp. 34–48. Springer, Heidelberg (2001). https://doi.org/10.1007/3-540-45574-4_3

    Chapter  MATH  Google Scholar 

  22. Sipser, M.: Introduction to the Theory of Computation, 3rd edn. Cengage Learning, Cambridge (2012)

    MATH  Google Scholar 

  23. Wadler, P., Kokke, W., Siek, J.G.: Programming language foundations in Agda (2020). http://plfa.inf.ed.ac.uk/20.07/

Download references

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Maycon J. J. Amaro .

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

Amaro, M.J.J., Feitosa, S.S., Ribeiro, R.G. (2022). A Sound Strategy to Compile General Recursion into Finite Depth Pattern Matching. In: Lima, L., Molnár, V. (eds) Formal Methods: Foundations and Applications. SBMF 2022. Lecture Notes in Computer Science, vol 13768. Springer, Cham. https://doi.org/10.1007/978-3-031-22476-8_3

Download citation

  • DOI: https://doi.org/10.1007/978-3-031-22476-8_3

  • Published:

  • Publisher Name: Springer, Cham

  • Print ISBN: 978-3-031-22475-1

  • Online ISBN: 978-3-031-22476-8

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics