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++.
- 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 Scholar
Digital Library
- B. Bloom and M. J. Hirzel. Robust scripting via patterns. In Proc. ACM DLS'12, pp. 29--40, NY, USA. Google Scholar
Digital Library
- 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 Scholar
Digital Library
- R. M. Burstall. Proving properties of programs by structural induction. Computer Journal, 1969.Google Scholar
Cross Ref
- L. Cardelli. Compiling a functional language. In Proc. ACM LFP'84, pp. 208--217. Google Scholar
Digital Library
- 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 Scholar
Digital Library
- S. Don, G. Neverov, and J. Margetson. Extensible pattern matching via a lightweight language extension. In Proc. ACM ICFP'07, pp. 29--40. Google Scholar
Digital Library
- 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 Scholar
- B. Emir. Object-oriented pattern matching. PhD thesis, Lausanne, 2007.Google Scholar
- D. J. Farber, R. E. Griswold, and I. P. Polonsky. SNOBOL, a string manipulation language. J. ACM, 11:21--30, January 1964. Google Scholar
Digital Library
- 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 Scholar
- 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 Scholar
Digital Library
- M. Hirzel, N. Nystrom, B. Bloom, and J. Vitek. Matchete: Paths through the pattern matching jungle. In Proc. PADL'08, pp. 150--166. Google Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
- D. H. H. Ingalls. A simple technique for handling multiple polymorphism. In Proc. ACM OOPSLA'86, pp. 347--349, New York, USA. Google Scholar
Digital Library
- International Organization for Standardization. ISO/IEC 14882:2011: Programming languages: C++. Geneva, Switzerland, 2011.Google Scholar
- 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 Scholar
- B. Jay. Pattern Calculus: Computing with Functions and Structures. Springer Publishing Company, Incorporated, 1st edition, 2009. Google Scholar
Digital Library
- B. Jay and D. Kesner. First-class patterns. J. Funct. Program., 19(2):191--225, Mar. 2009. Google Scholar
Digital Library
- F. Le Fessant and L. Maranget. Optimizing pattern matching. In Proc. ACM ICFP'01, pp. 26--37, New York, USA. Google Scholar
Digital Library
- A. Leung. Prop: A C++ based pattern matching language. Technical report, Courant Institute, New York University, 1996.Google Scholar
- L. Maranget. Compiling lazy pattern matching. In Proc. ACM LFP'92, pp. 21--31, New York, USA. Google Scholar
Digital Library
- L. Maranget. Compiling pattern matching to good decision trees. In Proc. ACM ML'08, pp. 35--46, New York, USA. Google Scholar
Digital Library
- Y. Minsky and S. Weeks. Caml trading – experiences with functional programming on wall street. J. Funct. Program., 18(4):553--564, July 2008. Google Scholar
Digital Library
- G. M. Morton. A computer-oriented geodetic data base and a new technique in file sequencing. Technical report, IBM, Ottawa, Canada, 1966.Google Scholar
- R. Muschevici, A. Potanin, E. Tempero, and J. Noble. Multiple dispatch in practice. In Proc. ACM OOPSLA'08, pp. 563--582. Google Scholar
Digital Library
- R. Nanavati. Experience report: a pure shirt fits. In Proc. ACM ICFP'08, pp. 347--352, New York, USA. Google Scholar
Digital Library
- G. Nelan. An algebraic typing & pattern matching preprocessor for C++, 2000. http://www.primenet.com/georgen/app.html.Google Scholar
- 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 Scholar
- M. Odersky and P. Wadler. Pizza into Java: Translating theory into practice. In In Proc. ACM POPL'97, pp. 146--159. Google Scholar
Digital Library
- C. Okasaki. Purely Functional Data Structures. Cambridge University Press, 1999. Google Scholar
Digital Library
- N. Oosterhof. App.lication patterns in functional languages, 2005.Google Scholar
- E. Pentangelo. Functional C#. http://functionalcsharp.codeplex.com/, 2011.Google Scholar
- P. Pirkelbauer. Programming Language Evolution and Source Code Rejuvenation. PhD thesis, Texas A&M University, December 2010. Google Scholar
Digital Library
- P. Pirkelbauer, Y. Solodkyy, and B. Stroustrup. Open multi-methods for C++. In Proc. ACM GPCE'07, pp. 123--134, New York, USA. Google Scholar
Digital Library
- L. Puel and A. Suarez. Compiling pattern matching by term decomposition. J. Symb. Comput., 15(1):1--26, Jan. 1993. Google Scholar
Digital Library
- M. Rhiger. Type-safe pattern combinators. J. Funct. Program., 19(2):145--156, Mar. 2009. Google Scholar
Digital Library
- A. Richard. OOMatch: pattern matching as dispatch in Java. Master's thesis, University of Waterloo, October 2007.Google Scholar
- Y. Solodkyy. Simplifying the Analysis of C++ Programs. PhD thesis, Texas A&M University, August 2013.Google Scholar
- 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 Scholar
Digital Library
- 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 Scholar
- S. Tobin-Hochstadt. Extensible pattern matching in an extensible language. September 2010.Google Scholar
- M. Tullsen. First class patterns. In Proc. PADL'00, pp. 1--15. Google Scholar
Digital Library
- D. Vandevoorde and N. Josuttis. C++ templates: the complete guide. Addison-Wesley, 2003. Google Scholar
Digital Library
- T. Veldhuizen. Expression templates. C++ Report, 7:26--31, 1995.Google Scholar
- J. Visser. Matching objects without language extension. Journal of Object Technology, 5.Google Scholar
- 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 Scholar
Digital Library
- A. Wright and B. Duba. Pattern matching for Scheme. 1995.Google Scholar
- V. H. Yngve. A programming language for mechanical translation. Mechanical Translation, 5:25--41, July 1958.Google Scholar
Index Terms
Open pattern matching for C++
Recommendations
Open pattern matching for C++
SPLASH '13: Proceedings of the 2013 companion publication for conference on Systems, programming, & applications: software for humanityPattern matching is an abstraction mechanism that can greatly simplify source code. We present functional-style pattern matching for C++ implemented as a library, called Mach7. All the patterns are user-definable, can be stored in variables, passed ...
Open pattern matching for C++
GPCE '13: Proceedings of the 12th international conference on Generative programming: concepts & experiencesPattern 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 ...
Pattern matching in an open world
GPCE 2018: Proceedings of the 17th ACM SIGPLAN International Conference on Generative Programming: Concepts and ExperiencesPattern matching is a pervasive and useful feature in functional programming. There have been many attempts to bring similar notions to Object-Oriented Programming (OOP) in the past. However, a key challenge in OOP is how pattern matching can coexist ...









Comments