skip to main content
research-article

Open pattern matching for C++

Published:27 October 2013Publication History
Skip Abstract Section

Abstract

Pattern matching is an abstraction mechanism that can greatly simplify source code. We present functional-style pattern matching for C++ implemented as a library, called Mach71. All the patterns are user-definable, can be stored in variables, passed among functions, and allow the use of class hierarchies. As an example, we implement common patterns used in functional languages.

Our approach to pattern matching is based on compile-time composition of pattern objects through concepts. This is superior (in terms of performance and expressiveness) to approaches based on run-time composition of polymorphic pattern objects. In particular, our solution allows mapping functional code based on pattern matching directly into C++ and produces code that is only a few percent slower than hand-optimized C++ code.

The library uses an efficient type switch construct, further extending it to multiple scrutinees and general patterns. We compare the performance of pattern matching to that of double dispatch and open multi-methods in C++.

References

  1. L. Augustsson. Compiling pattern matching. In Proc. of a conference on Functional programming languages and computer architecture, pp. 368--381, New York, USA, 1985. Springer-Verlag Inc. Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. B. Bloom and M. J. Hirzel. Robust scripting via patterns. In Proc. ACM DLS'12, pp. 29--40, NY, USA. Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. K. Bruce, L. Cardelli, G. Castagna, G. T. Leavens, and B. Pierce. On binary methods. Theor. Pract. Object Syst., 1(3):221--242, 1995. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. R. M. Burstall. Proving properties of programs by structural induction. Computer Journal, 1969.Google ScholarGoogle ScholarCross RefCross Ref
  5. L. Cardelli. Compiling a functional language. In Proc. ACM LFP'84, pp. 208--217. Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. P. Cuoq, J. Signoles, P. Baudin, R. Bonichon, G. Canet, L. Correnson, B. Monate, V. Prevosto, and A. Puccetti. Experience report: Ocaml for an industrial-strength static analysis framework. In Proc. ACM ICFP'09, pp. 281--286, New York, USA. Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. S. Don, G. Neverov, and J. Margetson. Extensible pattern matching via a lightweight language extension. In Proc. ACM ICFP'07, pp. 29--40. Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. G. Dos Reis and B. Stroustrup. A principled, complete, and efficient representation of C++. In Joint ASCM'09 and MACIS'09, pp. 407--421.Google ScholarGoogle Scholar
  9. B. Emir. Object-oriented pattern matching. PhD thesis, Lausanne, 2007.Google ScholarGoogle Scholar
  10. D. J. Farber, R. E. Griswold, and I. P. Polonsky. SNOBOL, a string manipulation language. J. ACM, 11:21--30, January 1964. Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. F. Geller, R. Hirschfeld, and G. Bracha. Pattern Matching for an object-oriented and dynamically typed programming language. Technische Berichte, Universität Potsdam. Univ.-Verlag, 2010.Google ScholarGoogle Scholar
  12. M. Gordon, R. Milner, L. Morris, M. Newey, and C. Wadsworth. A metalanguage for interactive proof in LCF. In Proc. ACM POPL'78, pp. 119--130, New York, USA. Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. M. Hirzel, N. Nystrom, B. Bloom, and J. Vitek. Matchete: Paths through the pattern matching jungle. In Proc. PADL'08, pp. 150--166. Google ScholarGoogle ScholarDigital LibraryDigital Library
  14. M. Homer, J. Noble, K. B. Bruce, A. P. Black, and D. J. Pearce. Patterns as objects in Grace. In Proc. ACM DLS'12, pp. 17--28. Google ScholarGoogle ScholarDigital LibraryDigital Library
  15. P. Hudak, H. Committee, P. Wadler, and S. Jones. Report on the Programming Language Haskell: A Non-strict, Purely Functional Language: Version 1.0. ML Library. Haskell Committee, 1990.Google ScholarGoogle Scholar
  16. D. H. H. Ingalls. A simple technique for handling multiple polymorphism. In Proc. ACM OOPSLA'86, pp. 347--349, New York, USA. Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. International Organization for Standardization. ISO/IEC 14882:2011: Programming languages: C++. Geneva, Switzerland, 2011.Google ScholarGoogle Scholar
  18. J. Järvi, J. Willcock, H. Hinnant, and A. Lumsdaine. Function overloading based on arbitrary properties of types. C/C++ Users Journal, 21(6):25--32, June 2003.Google ScholarGoogle Scholar
  19. B. Jay. Pattern Calculus: Computing with Functions and Structures. Springer Publishing Company, Incorporated, 1st edition, 2009. Google ScholarGoogle ScholarDigital LibraryDigital Library
  20. B. Jay and D. Kesner. First-class patterns. J. Funct. Program., 19(2):191--225, Mar. 2009. Google ScholarGoogle ScholarDigital LibraryDigital Library
  21. F. Le Fessant and L. Maranget. Optimizing pattern matching. In Proc. ACM ICFP'01, pp. 26--37, New York, USA. Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. A. Leung. Prop: A C++ based pattern matching language. Technical report, Courant Institute, New York University, 1996.Google ScholarGoogle Scholar
  23. L. Maranget. Compiling lazy pattern matching. In Proc. ACM LFP'92, pp. 21--31, New York, USA. Google ScholarGoogle ScholarDigital LibraryDigital Library
  24. L. Maranget. Compiling pattern matching to good decision trees. In Proc. ACM ML'08, pp. 35--46, New York, USA. Google ScholarGoogle ScholarDigital LibraryDigital Library
  25. Y. Minsky and S. Weeks. Caml trading – experiences with functional programming on wall street. J. Funct. Program., 18(4):553--564, July 2008. Google ScholarGoogle ScholarDigital LibraryDigital Library
  26. G. M. Morton. A computer-oriented geodetic data base and a new technique in file sequencing. Technical report, IBM, Ottawa, Canada, 1966.Google ScholarGoogle Scholar
  27. R. Muschevici, A. Potanin, E. Tempero, and J. Noble. Multiple dispatch in practice. In Proc. ACM OOPSLA'08, pp. 563--582. Google ScholarGoogle ScholarDigital LibraryDigital Library
  28. R. Nanavati. Experience report: a pure shirt fits. In Proc. ACM ICFP'08, pp. 347--352, New York, USA. Google ScholarGoogle ScholarDigital LibraryDigital Library
  29. G. Nelan. An algebraic typing & pattern matching preprocessor for C++, 2000. http://www.primenet.com/georgen/app.html.Google ScholarGoogle Scholar
  30. M. Odersky, V. Cremet, I. Dragos, G. Dubochet, B. Emir, S. Mcdirmid, S. Micheloud, N. Mihaylov, M. Schinz, E. Stenman, L. Spoon, and M. Zenger. An overview of the Scala programming language (2nd edition). Technical report, EPTF, 2006.Google ScholarGoogle Scholar
  31. M. Odersky and P. Wadler. Pizza into Java: Translating theory into practice. In In Proc. ACM POPL'97, pp. 146--159. Google ScholarGoogle ScholarDigital LibraryDigital Library
  32. C. Okasaki. Purely Functional Data Structures. Cambridge University Press, 1999. Google ScholarGoogle ScholarDigital LibraryDigital Library
  33. N. Oosterhof. App.lication patterns in functional languages, 2005.Google ScholarGoogle Scholar
  34. E. Pentangelo. Functional C#. http://functionalcsharp.codeplex.com/, 2011.Google ScholarGoogle Scholar
  35. P. Pirkelbauer. Programming Language Evolution and Source Code Rejuvenation. PhD thesis, Texas A&M University, December 2010. Google ScholarGoogle ScholarDigital LibraryDigital Library
  36. P. Pirkelbauer, Y. Solodkyy, and B. Stroustrup. Open multi-methods for C++. In Proc. ACM GPCE'07, pp. 123--134, New York, USA. Google ScholarGoogle ScholarDigital LibraryDigital Library
  37. L. Puel and A. Suarez. Compiling pattern matching by term decomposition. J. Symb. Comput., 15(1):1--26, Jan. 1993. Google ScholarGoogle ScholarDigital LibraryDigital Library
  38. M. Rhiger. Type-safe pattern combinators. J. Funct. Program., 19(2):145--156, Mar. 2009. Google ScholarGoogle ScholarDigital LibraryDigital Library
  39. A. Richard. OOMatch: pattern matching as dispatch in Java. Master's thesis, University of Waterloo, October 2007.Google ScholarGoogle Scholar
  40. Y. Solodkyy. Simplifying the Analysis of C++ Programs. PhD thesis, Texas A&M University, August 2013.Google ScholarGoogle Scholar
  41. Y. Solodkyy, G. Dos Reis, and B. Stroustrup. Open and efficient type switch for C++. In Proc. ACM OOPSLA'12, pp. 963--982. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  42. A. Sutton, B. Stroustrup, and G. Dos Reis. Concepts lite: Constraining templates with predicates. Technical Report WG21/N3580, JTC1/SC22/WG21 C++ Standards Committee, 2013.Google ScholarGoogle Scholar
  43. S. Tobin-Hochstadt. Extensible pattern matching in an extensible language. September 2010.Google ScholarGoogle Scholar
  44. M. Tullsen. First class patterns. In Proc. PADL'00, pp. 1--15. Google ScholarGoogle ScholarDigital LibraryDigital Library
  45. D. Vandevoorde and N. Josuttis. C++ templates: the complete guide. Addison-Wesley, 2003. Google ScholarGoogle ScholarDigital LibraryDigital Library
  46. T. Veldhuizen. Expression templates. C++ Report, 7:26--31, 1995.Google ScholarGoogle Scholar
  47. J. Visser. Matching objects without language extension. Journal of Object Technology, 5.Google ScholarGoogle Scholar
  48. P. Wadler. Views: a way for pattern matching to cohabit with data abstraction. In Proc. ACM POPL'87, pp. 307--313, New York, USA. Google ScholarGoogle ScholarDigital LibraryDigital Library
  49. A. Wright and B. Duba. Pattern matching for Scheme. 1995.Google ScholarGoogle Scholar
  50. V. H. Yngve. A programming language for mechanical translation. Mechanical Translation, 5:25--41, July 1958.Google ScholarGoogle Scholar

Index Terms

  1. Open pattern matching for C++

      Recommendations

      Reviews

      Andrea F Paramithiotti

      Pattern matching is a powerful technique for mining large code sources to extract complex data patterns. Although the technique itself is well known and implemented in many programming languages and applications, the authors of this paper present a novel approach, implementing pattern matching with an efficient switch function embedded in a C++ library. This approach creates patterns at compile time rather than at run time, so it is more functional than procedural in nature. The authors claim a generalized increase in efficiency of the code using this approach. The paper first describes the conceptual model behind the function, and then explains how the function is implemented in practice. The core of the technique involves creating patterns as expression templates on which structural and algebraic decomposition are performed. The most interesting part of the paper presents the performance evaluations. These are reported in terms of both structural issues, such as pattern matching and compilation time overhead, and practical ones, such as applying the technique to multi-argument hashing and rewriting Haskell code in C++. Of course, the findings are slightly in favor of this technique, but the conclusions are balanced, also pointing out cases for which this technique is not well suited. Online Computing Reviews Service

      Access critical reviews of Computing literature here

      Become a reviewer for Computing Reviews.

      Comments

      Login options

      Check if you have access through your login credentials or your institution to get full access on this article.

      Sign in

      Full Access

      • Published in

        cover image ACM SIGPLAN Notices
        ACM SIGPLAN Notices  Volume 49, Issue 3
        GPCE '13
        March 2014
        181 pages
        ISSN:0362-1340
        EISSN:1558-1160
        DOI:10.1145/2637365
        Issue’s Table of Contents
        • cover image ACM Conferences
          GPCE '13: Proceedings of the 12th international conference on Generative programming: concepts & experiences
          October 2013
          198 pages
          ISBN:9781450323734
          DOI:10.1145/2517208

        Copyright © 2013 ACM

        Publisher

        Association for Computing Machinery

        New York, NY, United States

        Publication History

        • Published: 27 October 2013

        Check for updates

        Qualifiers

        • research-article

      PDF Format

      View or Download as a PDF file.

      PDF

      eReader

      View online with eReader.

      eReader
      About Cookies On This Site

      We use cookies to ensure that we give you the best experience on our website.

      Learn more

      Got it!