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
Tax calculation will be finalised at checkout
Purchases are for personal use only
Learn about institutional subscriptionsNotes
- 1.
The code can be found under: https://github.com/HOL-Theorem-Prover/HOL/examples/pattern_matches.
- 2.
Compare function CASEWISE in define.ml.
- 3.
See patternMatchesSyntax.
- 4.
See e. g. PMATCH_SIMP_ss or PMATCH_REMOVE_GUARDS_ss.
- 5.
See PMATCH_LIFT_BOOL_ss.
- 6.
See e. g. PMATCH_CASE_SPLIT_ss.
- 7.
See COMPUTE_REDUNDANT_ROWS_INFO_OF_PMATCH and
PMATCH_IS_EXHAUSTIVE_CONSEQ_CONV.
- 8.
See PMATCH_REMOVE_REDUNDANT_ss.
- 9.
See /src/1/PmatchHeuristics.sig in the HOL4 sources.
- 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.
See PMATCH_IS_EXHAUSTIVE_CONSEQ_CONV.
- 12.
See PMATCH_REMOVE_REDUNDANT_ss.
- 13.
Our code generator renames variables. Here x has become v5, for example.
- 14.
The exceptions are non-constructor patterns that are not part of a constructor family.
References
Augustsson, L.: Compiling pattern matching. In: FPCA, pp. 368–381 (1985). http://dx.doi.org/10.1007/3-540-15975-4_48
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
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)
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
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)
Maranget, L.: Compiling pattern matching to good decision trees, September 2008
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)
Okasaki, C.: Purely Functional Data Structures. Cambridge University Press, Cambridge (1998)
Peyton Jones, S.L.: The Implementation of Functional Programming Languages (Prentice-Hall International Series in Computer Science). Prentice-Hall Inc., Upper Saddle River (1987)
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)
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)
Wadler, P.: Views: A way for pattern matching to cohabit with data abstraction. In: POPL, pp. 307–313. ACM (1987)
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
Acknowledgements
The second author was partially supported by the Royal Society UK and the Swedish Research Council.
Author information
Authors and Affiliations
Corresponding author
Editor information
Editors and Affiliations
Rights 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)