Pattern Matches in HOL:

A New Representation and Improved Code Generation
  • Thomas TuerkEmail author
  • Magnus O. Myreen
  • Ramana Kumar
Conference paper
Part of the Lecture Notes in Computer Science book series (LNCS, volume 9236)


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.



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


  1. 1.
    Augustsson, L.: Compiling pattern matching. In: FPCA, pp. 368–381 (1985).
  2. 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). CrossRefGoogle Scholar
  3. 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) CrossRefGoogle Scholar
  4. 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). Google Scholar
  5. 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. 6.
    Maranget, L.: Compiling pattern matching to good decision trees, September 2008Google Scholar
  7. 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)zbMATHMathSciNetCrossRefGoogle Scholar
  8. 8.
    Okasaki, C.: Purely Functional Data Structures. Cambridge University Press, Cambridge (1998) CrossRefGoogle Scholar
  9. 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. 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) CrossRefGoogle Scholar
  11. 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) CrossRefGoogle Scholar
  12. 12.
    Wadler, P.: Views: A way for pattern matching to cohabit with data abstraction. In: POPL, pp. 307–313. ACM (1987)Google Scholar
  13. 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). CrossRefGoogle Scholar

Copyright information

© Springer International Publishing Switzerland 2015

Authors and Affiliations

  • Thomas Tuerk
    • 1
    Email author
  • Magnus O. Myreen
    • 2
    • 3
  • Ramana Kumar
    • 3
  1. 1.Independent ScholarBrechenGermany
  2. 2.CSE DepartmentChalmers University of TechnologyGothenburgSweden
  3. 3.Computer LaboratoryUniversity of CambridgeCambridgeUK

Personalised recommendations