Generalized algebraic data types and object-oriented programming
Authors:
Open Access
- 12 October 2005
- journal article
- conference paper
- Published by Association for Computing Machinery (ACM) in ACM SIGPLAN Notices
- Vol. 40 (10) , 21-40
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.Keywords
This publication has 9 references indexed in Scilit:
- Languages of the future2004
- Polymorphic typed defunctionalization2004
- Guarded recursive datatype constructors2003
- Fun with phantom types2003
- Intensional polymorphism in type-erasure semanticsJournal of Functional Programming, 2002
- Extensible algebraic datatypes with defaults2001
- Design and implementation of generics for the .NET Common language runtime2001
- Featherweight Java1999
- Pizza into Java1997