[go: up one dir, main page]
More Web Proxy on the site http://driver.im/ skip to main content
10.1145/2517208.2517222acmconferencesArticle/Chapter ViewAbstractPublication PagesgpceConference Proceedingsconference-collections
research-article

Open pattern matching for C++

Published: 27 October 2013 Publication History

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.
[2]
B. Bloom and M. J. Hirzel. Robust scripting via patterns. In Proc. ACM DLS'12, pp. 29--40, NY, USA.
[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.
[4]
R. M. Burstall. Proving properties of programs by structural induction. Computer Journal, 1969.
[5]
L. Cardelli. Compiling a functional language. In Proc. ACM LFP'84, pp. 208--217.
[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.
[7]
S. Don, G. Neverov, and J. Margetson. Extensible pattern matching via a lightweight language extension. In Proc. ACM ICFP'07, pp. 29--40.
[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.
[9]
B. Emir. Object-oriented pattern matching. PhD thesis, Lausanne, 2007.
[10]
D. J. Farber, R. E. Griswold, and I. P. Polonsky. SNOBOL, a string manipulation language. J. ACM, 11:21--30, January 1964.
[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.
[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.
[13]
M. Hirzel, N. Nystrom, B. Bloom, and J. Vitek. Matchete: Paths through the pattern matching jungle. In Proc. PADL'08, pp. 150--166.
[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.
[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.
[16]
D. H. H. Ingalls. A simple technique for handling multiple polymorphism. In Proc. ACM OOPSLA'86, pp. 347--349, New York, USA.
[17]
International Organization for Standardization. ISO/IEC 14882:2011: Programming languages: C++. Geneva, Switzerland, 2011.
[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.
[19]
B. Jay. Pattern Calculus: Computing with Functions and Structures. Springer Publishing Company, Incorporated, 1st edition, 2009.
[20]
B. Jay and D. Kesner. First-class patterns. J. Funct. Program., 19(2):191--225, Mar. 2009.
[21]
F. Le Fessant and L. Maranget. Optimizing pattern matching. In Proc. ACM ICFP'01, pp. 26--37, New York, USA.
[22]
A. Leung. Prop: A C++ based pattern matching language. Technical report, Courant Institute, New York University, 1996.
[23]
L. Maranget. Compiling lazy pattern matching. In Proc. ACM LFP'92, pp. 21--31, New York, USA.
[24]
L. Maranget. Compiling pattern matching to good decision trees. In Proc. ACM ML'08, pp. 35--46, New York, USA.
[25]
Y. Minsky and S. Weeks. Caml trading – experiences with functional programming on wall street. J. Funct. Program., 18(4):553--564, July 2008.
[26]
G. M. Morton. A computer-oriented geodetic data base and a new technique in file sequencing. Technical report, IBM, Ottawa, Canada, 1966.
[27]
R. Muschevici, A. Potanin, E. Tempero, and J. Noble. Multiple dispatch in practice. In Proc. ACM OOPSLA'08, pp. 563--582.
[28]
R. Nanavati. Experience report: a pure shirt fits. In Proc. ACM ICFP'08, pp. 347--352, New York, USA.
[29]
G. Nelan. An algebraic typing & pattern matching preprocessor for C++, 2000. http://www.primenet.com/georgen/app.html.
[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.
[31]
M. Odersky and P. Wadler. Pizza into Java: Translating theory into practice. In In Proc. ACM POPL'97, pp. 146--159.
[32]
C. Okasaki. Purely Functional Data Structures. Cambridge University Press, 1999.
[33]
N. Oosterhof. App.lication patterns in functional languages, 2005.
[34]
E. Pentangelo. Functional C#. http://functionalcsharp.codeplex.com/, 2011.
[35]
P. Pirkelbauer. Programming Language Evolution and Source Code Rejuvenation. PhD thesis, Texas A&M University, December 2010.
[36]
P. Pirkelbauer, Y. Solodkyy, and B. Stroustrup. Open multi-methods for C++. In Proc. ACM GPCE'07, pp. 123--134, New York, USA.
[37]
L. Puel and A. Suarez. Compiling pattern matching by term decomposition. J. Symb. Comput., 15(1):1--26, Jan. 1993.
[38]
M. Rhiger. Type-safe pattern combinators. J. Funct. Program., 19(2):145--156, Mar. 2009.
[39]
A. Richard. OOMatch: pattern matching as dispatch in Java. Master's thesis, University of Waterloo, October 2007.
[40]
Y. Solodkyy. Simplifying the Analysis of C++ Programs. PhD thesis, Texas A&M University, August 2013.
[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.
[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.
[43]
S. Tobin-Hochstadt. Extensible pattern matching in an extensible language. September 2010.
[44]
M. Tullsen. First class patterns. In Proc. PADL'00, pp. 1--15.
[45]
D. Vandevoorde and N. Josuttis. C++ templates: the complete guide. Addison-Wesley, 2003.
[46]
T. Veldhuizen. Expression templates. C++ Report, 7:26--31, 1995.
[47]
J. Visser. Matching objects without language extension. Journal of Object Technology, 5.
[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.
[49]
A. Wright and B. Duba. Pattern matching for Scheme. 1995.
[50]
V. H. Yngve. A programming language for mechanical translation. Mechanical Translation, 5:25--41, July 1958.

Cited By

View all
  • (2024)The Ultimate Conditional SyntaxProceedings of the ACM on Programming Languages10.1145/36897468:OOPSLA2(988-1017)Online publication date: 8-Oct-2024
  • (2024)A Comprehensive Review of Static Memory AnalysisIEEE Access10.1109/ACCESS.2024.348225312(170204-170226)Online publication date: 2024
  • (2023)Bit-Stealing Made Legal: Compilation for Custom Memory Representations of Algebraic Data TypesProceedings of the ACM on Programming Languages10.1145/36078587:ICFP(813-846)Online publication date: 31-Aug-2023
  • Show More Cited By

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

Please enable JavaScript to view thecomments powered by Disqus.

Information & Contributors

Information

Published In

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
Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected]

Sponsors

Publisher

Association for Computing Machinery

New York, NY, United States

Publication History

Published: 27 October 2013

Permissions

Request permissions for this article.

Check for updates

Author Tags

  1. c++
  2. pattern matching

Qualifiers

  • Research-article

Conference

GPCE'13
Sponsor:
GPCE'13: Generative Programming: Concepts and Experiences
October 27 - 28, 2013
Indiana, Indianapolis, USA

Acceptance Rates

GPCE '13 Paper Acceptance Rate 20 of 59 submissions, 34%;
Overall Acceptance Rate 56 of 180 submissions, 31%

Contributors

Other Metrics

Bibliometrics & Citations

Bibliometrics

Article Metrics

  • Downloads (Last 12 months)39
  • Downloads (Last 6 weeks)3
Reflects downloads up to 21 Dec 2024

Other Metrics

Citations

Cited By

View all
  • (2024)The Ultimate Conditional SyntaxProceedings of the ACM on Programming Languages10.1145/36897468:OOPSLA2(988-1017)Online publication date: 8-Oct-2024
  • (2024)A Comprehensive Review of Static Memory AnalysisIEEE Access10.1109/ACCESS.2024.348225312(170204-170226)Online publication date: 2024
  • (2023)Bit-Stealing Made Legal: Compilation for Custom Memory Representations of Algebraic Data TypesProceedings of the ACM on Programming Languages10.1145/36078587:ICFP(813-846)Online publication date: 31-Aug-2023
  • (2022)Don't DIY: Automatically transform legacy Python code to support structural pattern matching2022 IEEE 22nd International Working Conference on Source Code Analysis and Manipulation (SCAM)10.1109/SCAM55253.2022.00024(164-169)Online publication date: Oct-2022
  • (2020)Pattern matching in an open worldACM SIGPLAN Notices10.1145/3393934.327812453:9(134-146)Online publication date: 7-Apr-2020
  • (2018)Pattern matching in an open worldProceedings of the 17th ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences10.1145/3278122.3278124(134-146)Online publication date: 5-Nov-2018
  • (2014)An Evaluation of Domain-Specific Language Technologies for Code GenerationProceedings of the 2014 14th International Conference on Computational Science and Its Applications10.1109/ICCSA.2014.16(18-26)Online publication date: 30-Jun-2014
  • (2013)Open pattern matching for C++Proceedings of the 2013 companion publication for conference on Systems, programming, & applications: software for humanity10.1145/2508075.2508098(97-98)Online publication date: 26-Oct-2013

View Options

Login options

View options

PDF

View or Download as a PDF file.

PDF

eReader

View online with eReader.

eReader

Media

Figures

Other

Tables

Share

Share

Share this Publication link

Share on social media