Abstract
Selecting operations based on the run-time type of an object is key to many object-oriented and functional programming techniques. We present a technique for implementing open and efficient type switching on hierarchical extensible data types. The technique is general and copes well with C++ multiple inheritance. To simplify experimentation and gain realistic performance using production-quality compilers and tool chains, we implement a type switch construct as an ISO C++11 library, called Mach7. This library-only implementation provides concise notation and outperforms the visitor design pattern, commonly used for case analysis on types in object-oriented programming. For closed sets of types, its performance roughly equals equivalent code in functional languages, such as OCaml and Haskell. The type-switching code is easier to use and is more expressive than hand-coded visitors are. The library is non-intrusive and circumvents most of the extensibility restrictions typical of the visitor design pattern. It was motivated by applications involving large, typed, abstract syntax trees.
Supplemental Material
Available for Download
This submission is an update to they yesterday's submission. Few things were corrected in text plus yesterday's PS file had problem. The current PS file was obtained with ps2pdf from the attached PDF and seems to be correct.
- Clang: a C language family frontend for LLVM. http://clang.llvm.org/, 2007.Google Scholar
- Liz: A System for Axiomatic Programming. http://liz.axiomatics.org/, 2012.Google Scholar
- A. Appel, L. Cardelli, K. Fisher, C. Gunter, R. Harper, X. Leroy, M. Lillibridge, D. B. MacQueen, J. Mitchell, G. Morrisett, J. H. Reppy, J. G. Riecke, Z. Shao, and C. A. Stone. Principles and a preliminary design for ML2000. March 1999.Google Scholar
- L. Augustsson. Compiling pattern matching. In Proc. of a conference on Functional programming languages and computer architecture, pages 368--381, New York, NY, USA, 1985. Springer-Verlag New York, Inc. Google Scholar
Digital Library
- L. Cardelli. Compiling a functional language. In Proc. of the 1984 ACM Symposium on LISP and functional programming, LFP '84, pages 208--217, New York, NY, USA, 1984. ACM. Google Scholar
Digital Library
- L. Cardelli, J. Donahue, M. Jordan, B. Kalsow, and G. Nelson. The Modula-3 type system. In Proc. of the 16th ACM SIGPLAN-SIGACT symposium on Principles of programming languages, POPL '89, pages 202--212, New York, NY, USA, 1989. ACM. Google Scholar
Digital Library
- Y. Caseau. Efficient handling of multiple inheritance hierarchies. In Proc. of the 8th conference on Object-oriented programming systems, languages, and applications, OOPSLA '93, pages 271--287, New York, NY, USA, 1993. ACM. Google Scholar
Digital Library
- CodeSourcery, Compaq, EDG, HP, IBM, Intel, Red Hat, and SGI. Itanium C++ ABI, March 2001. http://www.codesourcery.com/public/cxx-abi/.Google Scholar
- N. H. Cohen. Type-extension type test can be performed in constant time. ACM Trans. Program. Lang. Syst., 13(4):626--629, Oct. 1991. Google Scholar
Digital Library
- W. R. Cook. Object-oriented programming versus abstract data types. In Proc. of the REX School/Workshop on Foundations of Object-Oriented Languages, pages 151--178, London, UK, 1991. Springer-Verlag. Google Scholar
Digital Library
- O.-J. Dahl. SIMULA 67 common base language, (Norwegian Computing Center. Publication). 1968. ISBN B0007JZ9J6. Google Scholar
Digital Library
- J. Dean, G. DeFouw, D. Grove, V. Litvinov, and C. Chambers. Vortex: an optimizing compiler for object-oriented languages. In Proc. of the 11th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications, OOPSLA '96, pages 83--100, New York, NY, USA, 1996. Google Scholar
Digital Library
- L. P. Deutsch and A. M. Schiffman. Efficient implementation of the smalltalk-80 system. In Proc. of the 11th ACM SIGACT-SIGPLAN symposium on Principles of programming languages, POPL '84, pages 297--302, New York, NY, USA, 1984. ACM. Google Scholar
Digital Library
- E. W. Dijkstra. Guarded commands, non-determinacy and formal derivation of programs. Jan. 1975.Google Scholar
- G. Dos Reis and B. Stroustrup. A principled, complete, and efficient representation of C++. In Proc. Joint Conference of ASCM 2009 and MACIS 2009, volume 22 of COE Lecture Notes, pages 407--421, December 2009.Google Scholar
- K. Driesen and U. Holzle. The direct cost of virtual function calls in C++. In Proc. of the 11th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications, OOPSLA '96, pages 306--323, New York, NY, USA, 1996. ACM. Google Scholar
Digital Library
- R. Ducournau. Perfect hashing as an almost perfect subtype test. ACM Trans. Program. Lang. Syst., 30(6):33:1--33:56, Oct. 2008. ISSN 0164-0925. Google Scholar
Digital Library
- M. A. Ellis and B. Stroustrup. The annotated C++ reference manual. Addison-Wesley Longman Publishing Co., Inc., Boston, MA, USA, 1990. ISBN 0-201-51459-1. Google Scholar
Digital Library
- B. Emir. Object-oriented pattern matching. PhD thesis, Lausanne, 2007.Google Scholar
- M. D. Ernst, C. S. Kaplan, and C. Chambers. Predicate dispatching: A unified theory of dispatch. In ECOOP '98, the 12th European Conference on Object-Oriented Programming, pages 186--211, Brussels, Belgium, July 20-24, 1998. Google Scholar
Digital Library
- E. Gamma, R. Helm, R. E. Johnson, and J. M. Vlissides. Design patterns: Abstraction and reuse of object-oriented design. In Proc. of the 7th European Conference on Object-Oriented Programming, ECOOP '93, pages 406-431, London, UK, UK, 1993. Springer-Verlag. Google Scholar
Digital Library
- J. Garrigue. Programmingwith polymorphic variants. In ACM Workshop on ML, Sept. 1998.Google Scholar
- M. Gibbs and B. Stroustrup. Fast dynamic casting. Softw. Pract. Exper., 36:139--156, February 2006. Google Scholar
Digital Library
- N. Glew. Type dispatch for named hierarchical types. In Proc. of the 4th ACM SIGPLAN international conference on Func-tional programming, ICFP '99, pages 172--182, New York, NY, USA, 1999. Google Scholar
Digital Library
- C. Grothoff. Walkabout revisited: The runabout. In ECOOP 2003 - Object-Oriented Programming, pages 103--125. Springer-Verlag, 2003.Google Scholar
Cross Ref
- R. Harper and G. Morrisett. Compiling polymorphism using intensional type analysis. In Proc. of the 22nd ACM SIGPLAN-SIGACT symposium on Principles of programming languages, POPL '95, pages 130--141, New York, NY, USA, 1995. ACM. Google Scholar
Digital Library
- U. Holzle, C. Chambers, and D. Ungar. Optimizing dynamically-typed object-oriented languages with polymorphic inline caches. In Proc. of the European Conference on Object-Oriented Programming, volume 512 of Lecture Notes in Computer Science, pages 21-38, Berlin, Germany, 1991. Springer-Verlag. Google Scholar
Digital Library
- International Organization for Standardization. ISO/IEC 14882:2011: Programming languages: C++. Geneva, Switzerland, 3rd edition, 2011.Google Scholar
- S. P. Jones, editor. Haskell 98 Language and Libraries - The Revised Report. Cambridge University Press, Cambridge, England, 2003.Google Scholar
- A. Kennedy and C. V. Russo. Generalized algebraic data types and object-oriented programming. In Proc. of the 20th annual ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications, OOPSLA '05, pages 21--40, New York, NY, USA, 2005. ACM. Google Scholar
Digital Library
- A. Krall, J. Vitek, and R. N. Horspool. Near optimal hierarchical encoding of types. In In Proc. European Conference on Object-Oriented Programming, ECOOP'97, Lecture Notes in Computer Science, pages 128--145. Springer-Verlag, 1997.Google Scholar
Cross Ref
- S. Krishnamurthi, M. Felleisen, and D. Friedman. Synthesizing object-oriented and functional design to promote reuse. In E. Jul, editor, ECOOP'98 - Object-Oriented Programming, volume 1445 of Lecture Notes in Computer Science, pages 91--113. Springer Berlin / Heidelberg, 1998. Google Scholar
Digital Library
- B. Liskov. Keynote address - data abstraction and hierarchy. In OOPSLA '87: Addendum to the proc. on Object-oriented programming systems, languages and applications, pages 17--34, New York, NY, USA, 1987. ACM Press. Google Scholar
Digital Library
- B. Liskov, R. R. Atkinson, T. Bloom, E. B. Moss, R. Schaffert, and A. Snyder. Clu reference manual. Technical report, Cambridge, MA, USA, 1979. Google Scholar
Digital Library
- A. Loh and R. Hinze. Open data types and open functions. In Proc. of the 8th ACM SIGPLAN international conference on Principles and practice of declarative programming, PPDP '06, pages 133--144, New York, NY, USA, 2006. ACM. Google Scholar
Digital Library
- Microsoft Research. Phoenix compiler and shared source common language infrastructure. http://research.microsoft.com/phoenix/, 2005.Google Scholar
- R. Milner, M. Tofte, and R. Harper. The Definition of Standard ML. MIT Press, Cambridge, MA, USA, 1990. Google Scholar
Digital Library
- P.-E. Moreau, C. Ringeissen, and M. Vittek. A pattern matching compiler for multiple target languages. In Proc. of the 12th international conference on Compiler construction, CC'03, pages 61--76, Berlin, Heidelberg, 2003. Springer-Verlag. Google Scholar
Digital Library
- M. Odersky, V. Cremet, I. Dragos, G. Dubochet, B. Emir, S. Mcdirmid, S. Micheloud, N. Mihaylov, M. Schinz, E. Stenman, L. Spoon, andM. Zenger. An overview of the Scala programming language (2nd edition). Technical Report LAMP-REPORT-2006-001, Ecole Polytechnique Federale de Lausanne, 2006.Google Scholar
- B. C. Oliveira, M. Wang, and J. Gibbons. The visitor pattern as a reusable, generic, type-safe component. In Proc. of the 23rd ACM SIGPLAN conference on Object-oriented programming systems languages and applications, OOPSLA '08, pages 439--456, New York, NY, USA, 2008. ACM. Google Scholar
Digital Library
- J. Palsberg and C. B. Jay. The essence of the visitor pattern. In Proc. of the 22nd International Computer Software and Applications Conference, COMPSAC '98, pages 9-15,Washing-ton, DC, USA, 1998. IEEE Computer Society. Google Scholar
Digital Library
- S. Peyton Jones, D. Vytiniotis, S. Weirich, and G. Washburn. Simple unification-based type inference for GADTs. In Proc. of the 11th ACM SIGPLAN international conference on Functional programming, ICFP '06, pages 50-61, New York, NY, USA, 2006. ACM. Google Scholar
Digital Library
- T. Ramananandro, G. Dos Reis, and X. Leroy. Formal verification of object layout for C++ multiple inheritance. In Proc. of the 38th annual ACM SIGPLAN-SIGACT symposium on Principles of programming languages, POPL '11, pages 67--80, New York, NY, USA, 2011. ACM. Google Scholar
Digital Library
- J. G. Rossie, Jr. and D. P. Friedman. An algebraic semantics of subobjects. In Proc. of the 10th conference on Object-oriented programming systems, languages, and applications, OOPSLA '95, pages 187--199, New York, NY, USA, 1995. ACM. Google Scholar
Digital Library
- L. Schubert, M. Papalaskaris, and J. Taugher. Determining type, part, color, and time relationships. Computer, 16:53--60, 1983. ISSN 0018-9162. Google Scholar
Digital Library
- D. A. Spuler. Compiler CodeGeneration for Multiway Branch Statements as a Static Search Problem. Technical Report Technical Report 94/03, James Cook University, Jan. 1994.Google Scholar
- B. Stroustrup. Multiple inheritance for C++. In Proc. of the Spring'87 EUUG Conference, EUUG '87, May 1987. Revised version in AT&T C++ Translator Release Notes, June 1989. Also, USENIX Computing Systems, V2 no 4, Fall 1989, pp 367--396.Google Scholar
- Tom Duff. Duff's Device, Aug 1988. http://www.lysator.liu.se/c/duffs-device.html.Google Scholar
- D. Ungar and D. Patterson. Berkeley smalltalk: Who knows where the time goes? In G. Krasner, editor, Smalltalk-80: Bits of History andWords of Advice, pages 189--206. Addison-Wesley, 1983. ISBN 0-201-11669-3.Google Scholar
- J. Vitek, R. N. Horspool, and A. Krall. Efficient type inclu-sion tests. In Proc. of the 12th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and appli-cations, OOPSLA '97, pages 142--157, New York, NY, USA, 1997. ACM. Google Scholar
Digital Library
- D. Vytiniotis, G. Washburn, and S. Weirich. An open and shut typecase. In Proc. of the 2005 ACM SIGPLAN international workshop on Types in languages design and implementation, TLDI '05, pages 13--24, New York, NY, USA, 2005. ACM. Google Scholar
Digital Library
- P.Wadler. The expression problem. Mail to the java-genericity mailing list, November 1998.Google Scholar
- D. Wasserrab, T. Nipkow, G. Snelting, and F. Tip. An operational semantics and type safety proof for multiple inheritance in C++. In Proc. of the 21st annual ACM SIGPLAN conference on Object-oriented programming systems, languages, and applications, OOPSLA '06, pages 345-362, New York, NY, USA, 2006. ACM. Google Scholar
Digital Library
- N. Wirth. Type extensions. ACM Trans. Program. Lang. Syst., 10(2):204--214, Apr. 1988. Google Scholar
Digital Library
- M. Zenger and M. Odersky. Extensible algebraic datatypes with defaults. In Proc. of the 6th ACM SIGPLAN international conference on Functional programming, ICFP '01, pages 241--252, New York, NY, USA, 2001. ACM. Google Scholar
Digital Library
- M. Zenger and M. Odersky. Independently extensible solutions to the expression problem. In Proc. FOOL 12, Jan. 2005.Google Scholar
- Y. Zibin and J. Y. Gil. Efficient subtyping tests with PQ-encoding. In Proc. of the 16th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications, OOPSLA '01, pages 96--107, New York, NY, USA, 2001. ACM. Google Scholar
Digital Library
Index Terms
Open and efficient type switch for C++
Recommendations
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 ...
Open and efficient type switch for C++
OOPSLA '12: Proceedings of the ACM international conference on Object oriented programming systems languages and applicationsSelecting operations based on the run-time type of an object is key to many object-oriented and functional programming techniques. We present a technique for implementing open and efficient type switching on hierarchical extensible data types. The ...
The paradigm of open C++
Open languages are a new class of formal languages initially defined in [Sunik]. A language of this class combines the grammar of an object-oriented programming language with the universality of a natural language. This work details Open C++ based on ...







Comments