Generalized algebraic data type explained
In functional programming, a generalized algebraic data type (GADT, also first-class phantom type, guarded recursive datatype, or equality-qualified type) is a generalization of a parametric algebraic data type (ADT).
Overview
In a GADT, the product constructors (called data constructors in Haskell) can provide an explicit instantiation of the ADT as the type instantiation of their return value. This allows defining functions with a more advanced type behaviour. For a data constructor of Haskell 2010, the return value has the type instantiation implied by the instantiation of the ADT parameters at the constructor's application.
-- A parametric ADT that is not a GADTdata List a = Nil | Cons a (List a)
integers :: List Intintegers = Cons 12 (Cons 107 Nil)
strings :: List Stringstrings = Cons "boat" (Cons "dock" Nil)
-- A GADTdata Expr a where EBool :: Bool -> Expr Bool EInt :: Int -> Expr Int EEqual :: Expr Int -> Expr Int -> Expr Bool
eval :: Expr a -> aeval e = case e of EBool a -> a EInt a -> a EEqual a b -> (eval a)
(eval b)
expr1 :: Expr Boolexpr1 = EEqual (EInt 2) (EInt 3)
ret = eval expr1 -- False
They are currently implemented in the Glasgow Haskell Compiler (GHC) as a non-standard extension, used by, among others, Pugs and Darcs. OCaml supports GADT natively since version 4.00.[1]
The GHC implementation provides support for existentially quantified type parameters and for local constraints.
History
An early version of generalized algebraic data types were described by and based on pattern matching in ALF.
Generalized algebraic data types were introduced independently by and prior by as extensions to the algebraic data types of ML and Haskell. Both are essentially equivalent to each other. They are similar to the inductive families of data types (or inductive datatypes) found in Coq's Calculus of Inductive Constructions and other dependently typed languages, modulo the dependent types and except that the latter have an additional positivity restriction which is not enforced in GADTs.
introduced extended algebraic data types which combine GADTs together with the existential data types and type class constraints.
Type inference in the absence of any programmer supplied type annotation, is undecidable and functions defined over GADTs do not admit principal types in general. Type reconstruction requires several design trade-offs and is an area of active research (; .
In spring 2021, Scala 3.0 is released.[2] This major update of Scala introduce the possibility to write GADTs[3] with the same syntax as algebraic data types, which is not the case in other programming languages according to Martin Odersky.[4]
Applications
Applications of GADTs include generic programming, modelling programming languages (higher-order abstract syntax), maintaining invariants in data structures, expressing constraints in embedded domain-specific languages, and modelling objects.
Higher-order abstract syntax
An important application of GADTs is to embed higher-order abstract syntax in a type safe fashion. Here is an embedding of the simply typed lambda calculus with an arbitrary collection of base types, product types (tuples) and a fixed point combinator:
data Lam :: * -> * where Lift :: a -> Lam a -- ^ lifted value Pair :: Lam a -> Lam b -> Lam (a, b) -- ^ product Lam :: (Lam a -> Lam b) -> Lam (a -> b) -- ^ lambda abstraction App :: Lam (a -> b) -> Lam a -> Lam b -- ^ function application Fix :: Lam (a -> a) -> Lam a -- ^ fixed pointAnd a type safe evaluation function:eval :: Lam t -> teval (Lift v) = veval (Pair l r) = (eval l, eval r)eval (Lam f) = \x -> eval (f (Lift x))eval (App f x) = (eval f) (eval x)eval (Fix f) = (eval f) (eval (Fix f))The factorial function can now be written as:fact = Fix (Lam (\f -> Lam (\y -> Lift (if eval y
See also
Further reading
- Applications
- Web site: Lennart. Augustsson. Lennart Augustsson. Kent. Petersson. Silly type families. September 1994.
- James. Cheney. James Cheney (computer scientist). Ralf. Hinze. Ralf Hinze. 2003. First-Class Phantom Types. Technical Report CUCIS TR2003-1901. Cornell University. 1813/5614.
- Book: Hongwei. Xi. Hongwei Xi. Chiyan. Chen. Chiyan Chen. Gang. Chen. Proceedings of the 30th ACM SIGPLAN-SIGACT symposium on Principles of programming languages . Guarded recursive datatype constructors . 2003. 224–235. ACM Press. 10.1145/604131.604150. 978-1581136289. 10.1.1.59.4622. 15095297.
- Tim. Sheard. Tim Sheard. Emir. Pasalic. Emir Pasalic. 2004. Meta-programming with built-in type equality. Proceedings of the Fourth International Workshop on Logical Frameworks and Meta-languages (LFM'04), Cork. 199. 49–65. 10.1016/j.entcs.2007.11.012. free.
- Semantics
- Type reconstruction
- Simon. Peyton Jones. Simon Peyton Jones. Geoffrey. Washburn. Geoffrey Washburn. Stephanie. Weirich. Stephanie Weirich. 2004. Wobbly types: type inference for generalised algebraic data types. Technical Report MS-CIS-05-25. University of Pennsylvania.
- Simon. Peyton Jones. Simon Peyton Jones. Dimitrios. Vytiniotis. Dimitrios Vytiniotis. Stephanie. Weirich. Stephanie Weirich. Geoffrey. Washburn. Geoffrey Washburn. 2006. Simple Unification-based Type Inference for GADTs. Proceedings of the ACM International Conference on Functional Programming (ICFP'06), Portland.
- Martin. Sulzmann. Martin Sulzmann. Jeremy. Wazny. Jeremy Wazny. Peter J.. Stuckey. Peter J. Stuckey. A Framework for Extended Algebraic Data Types . 8th International Symposium on Functional and Logic Programming (FLOPS 2006). M.. Hagiya. P.. Wadler. Phil Wadler. Lecture Notes in Computer Science. 3945. 46–64. 2006.
- Martin. Sulzmann. Martin Sulzmann. Tom. Schrijvers. Tom Schrijvers. Peter J.. Stuckey. Peter J. Stuckey. Principal Type Inference for GHC-Style Multi-Parameter Type Classes . Programming Languages and Systems: 4th Asian Symposium (APLAS 2006) . Naoki . Kobayashi . Lecture Notes in Computer Science. 4279. 26–43. 2006.
- Book: Tom. Schrijvers. Tom Schrijvers. Simon. Peyton Jones. Simon Peyton Jones. Martin. Sulzmann. Martin Sulzmann. Dimitrios. Vytiniotis. Proceedings of the 14th ACM SIGPLAN international conference on Functional programming. Complete and decidable type inference for GADTs. Dimitrios Vytiniotis. 2009. 341–352. 10.1145/1596550.1596599. 9781605583327. 11272015. http://research.microsoft.com/en-us/um/people/simonpj/papers/gadt/implication_constraints.pdf.
- Chuan-kai. Lin. Practical Type Inference for the GADT Type System. Doctoral Dissertation. Portland State University. 2010. 2011-08-08. 2016-06-11. https://web.archive.org/web/20160611160430/https://sites.google.com/site/chklin/research/dissertation.pdf?attredirects=0. dead.
- Other
External links
Notes and References
- Web site: OCaml 4.00.1. ocaml.org.
- Web site: Kmetiuk . Anatolii . Scala 3 Is Here! . scala-lang.org . École Polytechnique Fédérale Lausanne (EPFL) Lausanne, Switzerland . 19 May 2021.
- Web site: Scala 3 – Book Algebraic Data Types . scala-lang.org . École Polytechnique Fédérale Lausanne (EPFL) Lausanne, Switzerland . 19 May 2021 . gadt-scala3.
- Web site: Odersky . Martin . A Tour of Scala 3 – Martin Odersky . https://ghostarchive.org/varchive/youtube/20211219/_Rnrx2lo9cw . 2021-12-19 . live . youtube.com . Scala Days Conferences . 19 May 2021 . scala3-tour.