Skip to main content

Pattern Matches in HOL:

A New Representation and Improved Code Generation

  • Conference paper
  • First Online:

Part of the book series: Lecture Notes in Computer Science ((LNTCS,volume 9236))

Abstract

Pattern matching is ubiquitous in functional programming and also very useful for definitions in higher-order logic. However, it is not directly supported by higher-order logic. Therefore, the parsers of theorem provers like HOL4 and Isabelle/HOL contain a pattern-compilation algorithm. Internally, decision trees based on case constants are used. For non-trivial case expressions, there is a big discrepancy between the user’s view and the internal representation.

This paper presents a new general-purpose representation for case expressions that mirrors the input syntax in the internal representation closely. Because of this close connection, the new representation is more intuitive and often much more compact. Complicated parsers and pretty printers are no longer required. Proofs can more closely follow the user’s intentions, and code generators can produce better code. Moreover, the new representation is more general than the currently used representation, supporting guards, patterns with multiple occurrences of the same bound variable, unbound variables, arithmetic expressions in patterns, and more. This work has been implemented in the HOL4 theorem prover and integrated into CakeML’s proof-producing code generator.

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

Buying options

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

Learn about institutional subscriptions

Notes

  1. 1.

    The code can be found under: https://github.com/HOL-Theorem-Prover/HOL/examples/pattern_matches.

  2. 2.

    Compare function CASEWISE in define.ml.

  3. 3.

    See patternMatchesSyntax.

  4. 4.

    See e. g. PMATCH_SIMP_ss or PMATCH_REMOVE_GUARDS_ss.

  5. 5.

    See PMATCH_LIFT_BOOL_ss.

  6. 6.

    See e. g. PMATCH_CASE_SPLIT_ss.

  7. 7.

    See COMPUTE_REDUNDANT_ROWS_INFO_OF_PMATCH and

    PMATCH_IS_EXHAUSTIVE_CONSEQ_CONV.

  8. 8.

    See PMATCH_REMOVE_REDUNDANT_ss.

  9. 9.

    See /src/1/PmatchHeuristics.sig in the HOL4 sources.

  10. 10.

    See e. g. HOL4’s Description Manual Sect. 4.5.2 or Isabelle/HOL’s manual Defining Recursive Functions in Isabelle/HOL Sect. 10.1.

  11. 11.

    See PMATCH_IS_EXHAUSTIVE_CONSEQ_CONV.

  12. 12.

    See PMATCH_REMOVE_REDUNDANT_ss.

  13. 13.

    Our code generator renames variables. Here x has become v5, for example.

  14. 14.

    The exceptions are non-constructor patterns that are not part of a constructor family.

References

  1. Augustsson, L.: Compiling pattern matching. In: FPCA, pp. 368–381 (1985). http://dx.doi.org/10.1007/3-540-15975-4_48

  2. Harrison, J.: HOL light: an overview. In: Berghofer, S., Nipkow, T., Urban, C., Wenzel, M. (eds.) TPHOLs 2009. LNCS, vol. 5674, pp. 60–66. Springer, Heidelberg (2009). http://dx.doi.org/10.1007/978-3-642-03359-9_4

    Chapter  Google Scholar 

  3. Krauss, A.: Partial recursive functions in higher-order logic. In: Furbach, U., Shankar, N. (eds.) IJCAR 2006. LNCS (LNAI), vol. 4130, pp. 589–603. Springer, Heidelberg (2006)

    Chapter  Google Scholar 

  4. Kumar, R., Arthan, R., Myreen, M.O., Owens, S.: HOL with definitions: semantics, soundness, and a verified implementation. In: Klein, G., Gamboa, R. (eds.) ITP 2014. LNCS, vol. 8558, pp. 308–324. Springer, Heidelberg (2014). http://dx.doi.org/10.1007/978-3-319-08970-6_20

    Google Scholar 

  5. Kumar, R., Myreen, M.O., Norrish, M., Owens, S.: CakeML: a verified implementation of ML. In: Jagannathan, S., Sewell, P. (eds.) POPL, pp. 179–192. ACM (2014)

    Google Scholar 

  6. Maranget, L.: Compiling pattern matching to good decision trees, September 2008

    Google Scholar 

  7. Myreen, M.O., Owens, S.: Proof-producing translation of higher-order logic into pure and stateful ML. J. Funct. Program. 24(2–3), 284–315 (2014)

    Article  MATH  MathSciNet  Google Scholar 

  8. Okasaki, C.: Purely Functional Data Structures. Cambridge University Press, Cambridge (1998)

    Book  Google Scholar 

  9. Peyton Jones, S.L.: The Implementation of Functional Programming Languages (Prentice-Hall International Series in Computer Science). Prentice-Hall Inc., Upper Saddle River (1987)

    Google Scholar 

  10. Slind, K.: Function definition in higher-order logic. In: von Wright, J., Harrison, J., Grundy, J. (eds.) TPHOLs 1996. LNCS, vol. 1125, pp. 381–397. Springer, Heidelberg (1996)

    Chapter  Google Scholar 

  11. Slind, K., Norrish, M.: A brief overview of HOL4. In: Mohamed, O.A., Muñoz, C., Tahar, S. (eds.) TPHOLs 2008. LNCS, vol. 5170, pp. 28–32. Springer, Heidelberg (2008)

    Chapter  Google Scholar 

  12. Wadler, P.: Views: A way for pattern matching to cohabit with data abstraction. In: POPL, pp. 307–313. ACM (1987)

    Google Scholar 

  13. Wenzel, M., Paulson, L.C., Nipkow, T.: The isabelle framework. In: Mohamed, O.A., Muñoz, C., Tahar, S. (eds.) TPHOLs 2008. LNCS, vol. 5170, pp. 33–38. Springer, Heidelberg (2008). http://dx.doi.org/10.1007/978-3-540-71067-7_7

    Chapter  Google Scholar 

Download references

Acknowledgements

The second author was partially supported by the Royal Society UK and the Swedish Research Council.

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Thomas Tuerk .

Editor information

Editors and Affiliations

Rights and permissions

Reprints and permissions

Copyright information

© 2015 Springer International Publishing Switzerland

About this paper

Cite this paper

Tuerk, T., Myreen, M.O., Kumar, R. (2015). Pattern Matches in HOL:. In: Urban, C., Zhang, X. (eds) Interactive Theorem Proving. ITP 2015. Lecture Notes in Computer Science(), vol 9236. Springer, Cham. https://doi.org/10.1007/978-3-319-22102-1_30

Download citation

  • DOI: https://doi.org/10.1007/978-3-319-22102-1_30

  • Published:

  • Publisher Name: Springer, Cham

  • Print ISBN: 978-3-319-22101-4

  • Online ISBN: 978-3-319-22102-1

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics