skip to main content
article

Pattern matching in an open world

Published:07 April 2020Publication History
Skip Abstract Section

Abstract

Pattern 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 with the open nature of OOP data structures, while at the same time guaranteeing other desirable properties for pattern matching.

This paper discusses several desirable properties for pattern matching in an OOP context and shows how existing approaches are lacking some of these properties. We argue that the traditional semantics of pattern matching, which is based on the order of patterns and adopted by many approaches, is in conflict with the openness of data structures. Therefore we suggest that a more restricted, top-level pattern matching model, where the order of patterns is irrelevant, is worthwhile considering in an OOP context. To compensate for the absence of ordered patterns we propose a complementary mechanism for case analysis with defaults, which can be used when nested and/or multiple case analysis is needed. To illustrate our points we develop Castor: a meta-programming library inScala that adopts both ideas. Castor generates code that uses type-safe extensible visitors, and largely removes boilerplate code typically associated with visitors. We illustrate the applicability of our approach with a case study modularizing the interpreters in the famous book ”Types and Programming Languages”.

References

  1. Patrick Bahr and Tom Hvitved. 2011. Compositional data types. In Proceedings of the seventh ACM SIGPLAN workshop on Generic programming. ACM, 83–94. Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. Eugene Burmako. 2017. Unification of Compile-Time and Runtime Metaprogramming in Scala. Ph.D. Dissertation. EPFL.Google ScholarGoogle Scholar
  3. Jacques Carette, Oleg Kiselyov, and Chung-chieh Shan. 2009. Finally tagless, partially evaluated: Tagless staged interpreters for simpler typed languages. Journal of Functional Programming 19, 5 (2009), 509–543. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Craig Chambers. 1992. Object-oriented multi-methods in Cecil. In European Conference on Object-Oriented Programming. Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. Curtis Clifton, Gary T Leavens, Craig Chambers, and Todd Millstein. 2000. MultiJava: Modular open classes and symmetric multiple dispatch for Java. In ACM Sigplan Notices, Vol. 35. ACM, 130–145. Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. Burak Emir, Martin Odersky, and John Williams. 2007. Matching objects with patterns. In European Conference on Object-Oriented Programming. Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. 1994. Design Patterns : Elements of Reusable Object-Oriented Software. Addisson-Wesley. Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. Jacques Garrigue. 1998. Programming with polymorphic variants. In ML Workshop.Google ScholarGoogle Scholar
  9. Jacques Garrigue. 2000. Code reuse through polymorphic variants. In Workshop on Foundations of Software Engineering.Google ScholarGoogle Scholar
  10. Felix Geller, Robert Hirschfeld, and Gilad Bracha. 2010. Pattern Matching for an object-oriented and dynamically typed programming language. Number 36. Universitätsverlag Potsdam.Google ScholarGoogle Scholar
  11. Martin Hirzel, Nathaniel Nystrom, Bard Bloom, and Jan Vitek. 2008. Matchete: Paths through the pattern matching jungle. In International Symposium on Practical Aspects of Declarative Languages. Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. Christian Hofer and Klaus Ostermann. 2010. Modular Domain-specific Language Components in Scala. In Proceedings of the Ninth International Conference on Generative Programming and Component Engineering (GPCE ’10). Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. Christian Hofer, Klaus Ostermann, Tillmann Rendel, and Adriaan Moors. 2008. Polymorphic Embedding of Dsls. In Proceedings of the 7th International Conference on Generative Programming and Component Engineering (GPCE ’08). Google ScholarGoogle ScholarDigital LibraryDigital Library
  14. Michael Homer, James Noble, Kim B. Bruce, Andrew P. Black, and David J. Pearce. 2012. Patterns As Objects in Grace. In Proceedings of the 8th Symposium on Dynamic Languages (DLS ’12). New York, NY, USA, 17–28. Google ScholarGoogle ScholarDigital LibraryDigital Library
  15. Pablo Inostroza and Tijs van der Storm. 2015. Modular Interpreters for the Masses: Implicit Context Propagation Using Object Algebras. In Proceedings of the 2015 ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences. Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. Chinawat Isradisaikul and Andrew C. Myers. 2013. Reconciling Exhaustive Pattern Matching with Objects. In Proceedings of the 34th ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI ’13). Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. Simon Peyton Jones. 2003. Haskell 98 language and libraries: the revised report. Cambridge University Press.Google ScholarGoogle Scholar
  18. Jed Liu and Andrew C Myers. 2003. JMatch: Iterable abstract pattern matching for Java. In PADL. Google ScholarGoogle ScholarDigital LibraryDigital Library
  19. Andres Löh and Ralf Hinze. 2006. Open data types and open functions. In Proceedings of the 8th ACM SIGPLAN international conference on Principles and practice of declarative programming. Google ScholarGoogle ScholarDigital LibraryDigital Library
  20. Todd Millstein, Colin Bleckner, and Craig Chambers. 2004. Modular Typechecking for Hierarchically Extensible Datatypes and Functions. ACM Trans. Program. Lang. Syst. 26, 5 (Sept. 2004). Google ScholarGoogle ScholarDigital LibraryDigital Library
  21. Robin Milner, Mads Tofte, Robert Harper, and David Macqueen. 1997. The Definition of Standard ML-Revised. (1997). Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. Martin Odersky, Philippe Altherr, Vincent Cremet, Burak Emir, Sebastian Maneth, Stéphane Micheloud, Nikolay Mihaylov, Michel Schinz, Erik Stenman, and Matthias Zenger. 2004. An overview of the Scala programming language. Technical Report.Google ScholarGoogle Scholar
  23. Martin Odersky and Matthias Zenger. 2005. Independently extensible solutions to the expression problem. In The 12th International Workshop on Foundations of Object-Oriented Languages.Google ScholarGoogle Scholar
  24. Bruno C. d. S. Oliveira. 2009. Modular Visitor Components. In Proceedings of the 23rd European Conference on Object-Oriented Programming. Google ScholarGoogle ScholarDigital LibraryDigital Library
  25. Bruno C. d. S. Oliveira and William R. Cook. 2012. Extensibility for the Masses: Practical Extensibility with Object Algebras. In Proceedings of the 26th European Conference on Object-Oriented Programming. Google ScholarGoogle ScholarDigital LibraryDigital Library
  26. Bruno C. d. S. Oliveira, Shin-Cheng Mu, and Shu-Hung You. 2015. Modular Reifiable Matching: A List-of-functors Approach to Twolevel Types. In Proceedings of the 2015 ACM SIGPLAN Symposium on Haskell (Haskell ’15). Google ScholarGoogle ScholarDigital LibraryDigital Library
  27. Bruno C. d. S. Oliveira, Tijs van der Storm, Alex Loh, and William R. Cook. 2013. Feature-Oriented Programming with Object Algebras. In Proceedings of the 27th European Conference on Object-Oriented Programming. Google ScholarGoogle ScholarDigital LibraryDigital Library
  28. Benjamin C Pierce. 2002. Types and programming languages. MIT press. Google ScholarGoogle ScholarDigital LibraryDigital Library
  29. Tillmann Rendel, Jonathan Immanuel Brachthäuser, and Klaus Ostermann. 2014. From Object Algebras to Attribute Grammars. In Proceedings of the 2014 ACM International Conference on Object-Oriented Programming Systems Languages and Applications. Google ScholarGoogle ScholarDigital LibraryDigital Library
  30. Tiark Rompf and Martin Odersky. 2010. Lightweight Modular Staging: A Pragmatic Approach to Runtime Code Generation and Compiled DSLs. In In GPCE. Google ScholarGoogle ScholarDigital LibraryDigital Library
  31. Sukyoung Ryu, Changhee Park, and Guy L Steele Jr. 2010. Adding pattern matching to existing object-oriented languages. In ACM SIGPLAN Foundations of Object-Oriented Languages Workshop.Google ScholarGoogle Scholar
  32. Tim Sheard and Simon Peyton Jones. 2002. Template metaprogramming for Haskell. In Proceedings of the 2002 ACM SIGPLAN workshop on Haskell. Google ScholarGoogle ScholarDigital LibraryDigital Library
  33. Yuriy Solodkyy, Gabriel Dos Reis, and Bjarne Stroustrup. 2013. Open Pattern Matching for C++. In Proceedings of the 12th International Conference on Generative Programming: Concepts and Experiences (GPCE ’13). Google ScholarGoogle ScholarDigital LibraryDigital Library
  34. Wouter Swierstra. 2008. Data types à la carte. Journal of functional programming 18, 4 (2008), 423–436. Google ScholarGoogle ScholarDigital LibraryDigital Library
  35. Philip Wadler. 1998. The Expression Problem. Email. (Nov. 1998). Discussion on the Java Genericity mailing list.Google ScholarGoogle Scholar
  36. Hongwei Xi, Chiyan Chen, and Gang Chen. 2003. Guarded Recursive Datatype Constructors. In Proceedings of the 30th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’03). Google ScholarGoogle ScholarDigital LibraryDigital Library
  37. Matthias Zenger and Martin Odersky. 2001. Extensible Algebraic Datatypes with Defaults. In Proceedings of the Sixth ACM SIGPLAN International Conference on Functional Programming. Google ScholarGoogle ScholarDigital LibraryDigital Library
  38. Weixin Zhang. 2017. Extensible domain-specific languages in objectoriented programming. HKU Theses Online (HKUTO) (2017).Google ScholarGoogle Scholar
  39. Weixin Zhang and Bruno C. d. S. Oliveira. 2017. EVF: An Extensible and Expressive Visitor Framework for Programming Language Reuse. In European Conference on Object-Oriented Programming.Google ScholarGoogle Scholar

Index Terms

  1. Pattern matching in an open world

    Recommendations

    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 53, Issue 9
      GPCE '18
      September 2018
      214 pages
      ISSN:0362-1340
      EISSN:1558-1160
      DOI:10.1145/3393934
      Issue’s Table of Contents
      • cover image ACM Conferences
        GPCE 2018: Proceedings of the 17th ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences
        November 2018
        214 pages
        ISBN:9781450360456
        DOI:10.1145/3278122

      Copyright © 2018 ACM

      Publisher

      Association for Computing Machinery

      New York, NY, United States

      Publication History

      • Published: 7 April 2020

      Check for updates

      Qualifiers

      • 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!