background preloader

Types

Facebook Twitter

Heterogeneous-collections

Gadt. Binomial queues as nested types. Warning: this is probably only of interest to functional programmers. Maciej Kotowicz recently asked on the Haskell Cafe mailing list about implementing binomial queues (also known as binomial heaps) using fancy type machinery so that the type-checker can enforce the shape invariants of the data structure. This reminded me of a discussion I had with some colleagues in the summer of 1998 about using nested types to enforce these kinds of invariants. A little digging around in mail archives yielded this email, presented here with some light formatting and a few fixed typos. I've been playing with binomial queues as a nested type, and I think you'll find the end result interesting. Let me first quickly review the usual implementation of binomial queues.

Recall that a binomial tree has the shape data Tree a = Node a [Tree a] A binomial tree of rank k has k children, of ranks k-1 .. 0 (stored in the list in decreasing order of rank). Type Binom a = [Maybe (Tree a)] or somewhat more efficiently as. Types and functional dependencies. What to Know Before Debating about Types. GHC/Type families. The GHC Users Guide has a Type Family section. Indexed type families, or type families for short, are a Haskell extension supporting ad-hoc overloading of data types. Type families are parametric types that can be assigned specialized representations based on the type parameters they are instantiated with. They are the data type analogue of type classes: families are used to define overloaded data in the same way that classes are used to define overloaded functions. Type families are useful for generic programming, for creating highly parameterised library interfaces, and for creating interfaces with enhanced static information, much like dependent types.

Type families come in two flavors: data families and type synonym families. Data families are the indexed form of data and newtype definitions. NB: see also Simon's blog entry on let generalisation for a significant change in the policy for let generalisation, driven by the type family extension. What are type families? An Int instance to.