Generalized algebraic datatypes (GADTs) extend Haskell's type system by allowing the return types of data constructors to be more specific than they would be otherwise. For instance, we could make a data type `data Equal a b where Refl :: Equal a a` which provides evidence that two types are equal. With GADTs, Haskell's type system can verify important static properties about functions and data. For instance, we can statically check array sizes or the types of objects in an embedded language.



But how can we use GADTs when the data are not statically known? For example, we may read an array of unknown length from a file, or parse a language where the types of subexpressions are unknown. Initially, it may seem difficult to pass these dynamic data from the dangerous outside world to our GADTs, and we might be inclined to give up the internal type safety that GADTs provide to permit handling the unknown data. But by passing around type evidence, we can handle these situations, too.



In this talk, I will introduce GADTs. We will build an example embedded language, and then use GADTs to make it type-safe. Finally, we will write a parser that reads the language dynamically and parses it into the GADT by passing around type evidence.



For a preview, you can check out some preliminary materials I've prepared on Github (https://github.com/bmsherman/gadts-talk).