ABSTRACT
Generalized algebraic data types (GADTs) have received much attention recently in the functional programming community. They generalize the (type) parameterized algebraic datatypes (PADTs) of ML and Haskell by permitting value constructors to return specific, rather than parametric, type-instantiations of their own datatype. GADTs have a number of applications, including strongly-typed evaluators, generic pretty-printing, generic traversals and queries, and typed LR parsing. We show that existing object-oriented programming languages such as Java and C# can express GADT definitions, and a large class of GADT-manipulating programs, through the use of generics, subclassing, and virtual dispatch. However, some programs can be written only through the use of redundant runtime casts. Moreover, instantiation-specific, yet safe, operations on ordinary PADTs only admit indirect cast-free implementations, via higher-order encodings. We propose a generalization of the type constraint mechanisms of C# and Java to both avoid the need for casts in GADT programs and higher-order contortions in PADT programs; we present a Visitor pattern for GADTs, and describe a refined switch construct as an alternative to virtual dispatch on datatypes. We formalize both extensions and prove type soundness.
- F. Baader and T. Nipkow. Term Rewriting and All That. Cambridge University Press, 1998. Google ScholarDigital Library
- K. B. Bruce, L. Cardelli, G. Castagna, J. Eifrig, S. F. Smith, V. Trifonov, G. T. Leavens, and B. C. Pierce. On binary methods. Theory and Practice of Object Systems, 1(3):221--242, 1995. Google ScholarDigital Library
- J. Cheney and R. Hinze. First-class phantom types. Technical Report 1901, Cornell University, 2003.Google Scholar
- T. Coquand. Pattern matching with dependent types. In Proceedings of the 1992 Workshop on Types for Proofs and Programs, 1992.Google Scholar
- K. Crary, S. Weirich, and G. Morrisett. Intensional polymorphism in type erasure semantics. In Journal of Functional Programming, November 2002. Google ScholarDigital Library
- E. Gamma, R. Helm, R. Johnson, and J. Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley Professional Computing Series. Addison-Wesley Publishing Company, New York, NY, 1995. Google ScholarDigital Library
- A. Hejlsberg, S. Wiltamuth, and P. Golde. C# version 2.0 specification, 2005. Available from http://msdn.microsoft.com/vcsharp/team/language/default.aspx.Google Scholar
- R. Hinze. Fun with phantom types. In J. Gibbons and O. de Moor, editors, The Fun of Programming, pages 245--262. Palgrave Macmillan, Mar. 2003.Google ScholarCross Ref
- A. Igarashi, B. Pierce, and P. Wadler. Featherweight Java: A minimal core calculus for Java and GJ. In Conference on Object-Oriented Programming, Systems, Languages and Applications (OOPSLA), 1999. Google ScholarDigital Library
- A. Kennedy and D. Syme. Transposing F to C#: Expressivity of parametric polymorphism in an object-oriented language. Concurrency and Computation: Practice and Experience, 16:707--733, 2004. Google ScholarDigital Library
- A. J. Kennedy and D. Syme. Design and implementation of generics for the .NET Common Language Runtime. In Programming Language Design and Implementation. ACM, 2001. Google ScholarDigital Library
- E. Meijer, November 2004. Presentation to IFIP WG 2.8 (private communication).Google Scholar
- M. Odersky, P. Altherr, V. Cremet, B. Emir, S. Micheloud, N. Mihaylov, M. Schinz, E. Stenman, and M. Zenger. The Scala language specification, 2005. Available from http://scala.epfl.ch/.Google Scholar
- M. Odersky and P. Wadler. Pizza into Java: Translating Theory into Practice. In 24th ACM Symposium on Principles of Programming Languages (POPL), pages 146--159, 1997. Google ScholarDigital Library
- C. Paulin-Mohring. Inductive definitions in the system Coq: Rules and properties. Technical Report 92-49, Laboratoire de l'Informatique du Parallélisme, Ecole Normale Supérieure de Lyon, December 1992.Google Scholar
- S. Peyton Jones et al. The ghc compiler version 6.4, March 2005. Download at http://haskell.org/ghc.Google Scholar
- S. Peyton Jones, G. Washburn, and S. Weirich. Wobbly types: type inference for generalised algebraic data types. Draft, July 2004.Google Scholar
- F. Pottier and N. Gauthier. Polymorphic typed defunctionalization. In Proceedings of the 31st ACM Symposium on Principles of Programming Languages (POPL'04), pages 89--98, Venice, Italy, Jan. 2004. Google ScholarDigital Library
- P. Sestoft. Representing typed expressions, December 2001. Code sample in http://www.dina.kvl.dk/~sestoft/gcsharp/#expr.Google Scholar
- P. Sestoft and H. I. Hansen. C# Precisely. MIT Press, October 2004. Google ScholarDigital Library
- T. Sheard. Languages of the future. In OOPSLA '04: Companion to the 19th annual ACM SIGPLAN conference on Object-oriented programming systems, languages, and applications, pages 116--119. ACM Press, 2004. Google ScholarDigital Library
- T. Sheard and E. Pasalic. Meta-programming with built-in type equality. In Fourth International Workshop on Logical Frameworks and Meta-languages (LFM'04), July 2004.Google Scholar
- V. Simonet and F. Pottier. Constraint-based type inference for guarded algebraic data types. Research Report 5462, INRIA, Jan. 2005.Google Scholar
- S. Weirich. Type-checker to generate typed term from untyped source, September 2004. Response to challenge at Dagstuhl'04 set by Lennart Augustsson. In ghc regression suite (tc.hs).Google Scholar
- H. Xi, C. Chen, and G. Chen. Guarded recursive datatype constructors. In POPL '03: Proceedings of the 30th ACM SIGPLAN-SIGACT symposium on Principles of Programming Languages, pages 224--235. ACM Press, 2003. Google ScholarDigital Library
- M. Zenger and M. Odersky. Extensible algebraic datatypes with defaults. In ICFP '01: Proceedings of the 6th ACM International Conference on Functional Programming, pages 241--252. ACM Press, 2001. Google ScholarDigital Library
Index Terms
- Generalized algebraic data types and object-oriented programming
Recommendations
A constraint-based approach to guarded algebraic data types
We study HMG(X), an extension of the constraint-based type system HM(X) with deep pattern matching, polymorphic recursion, and guarded algebraic data types. Guarded algebraic data types subsume the concepts known in the literature as indexed types, ...
Generalized algebraic data types and object-oriented programming
Proceedings of the 20th annual ACM SIGPLAN conference on Object oriented programming systems languages and applicationsGeneralized algebraic data types (GADTs) have received much attention recently in the functional programming community. They generalize the (type) parameterized algebraic datatypes (PADTs) of ML and Haskell by permitting value constructors to return ...
Pointwise generalized algebraic data types
TLDI '10: Proceedings of the 5th ACM SIGPLAN workshop on Types in language design and implementationIn the GADT (Generalized Algebraic Data Types) type system, a pattern-matching branch can draw type information from both the scrutinee type and the data constructor type. Even though the type system can handle complex interactions between the two types,...
Comments