Finite types are well-known in theory. For those who aren't theorists, there are two kinds of finite type: finite products and finite sums (also called finite coproducts).
Finite products are a generalization of tuples and records. Where tuples are indexed by integer and records ar eindexed by name, finite products can use any index set. Finite sums are a generalization of discriminated unions (also called variants) so that, again, they are indexed by any set.
Finite products and sums are useful generally for organizing data, but can be particularly
useful where functinos are curried. A finite product using
Either Int String (or similar)
as an index set, we can easily simulate a mix of positional and keyword arguments to such
functions. With finite sums, we can specify the type of a function which can take one of
multiple valid sets of arguments.
- data Product i a
- mkProduct :: Eq i => [(i, a)] -> Product i a
- getProd :: Eq i => Product i a -> i -> a
- setProd :: Eq i => Product i a -> i -> a -> Product i a
- hasProd :: Eq i => Product i a -> i -> Bool
- data Sum i a
- mkSum :: Eq i => SumTemplate i -> i -> a -> Sum i a
- getSum :: Sum i a -> a
- setSum :: Eq i => Sum i a -> i -> a -> Sum i a
- hasSum :: Sum i a -> i
- data SumTemplate i
- mkSumTemplate :: Product i a -> SumTemplate i
Product with all fields filled from an association list.
Error if the keys are not distinct.
Look up a field of a finite product. Error if the field does not exist.
Modify a field of a finite product. Error if the field does not exist.
Check for existence of a field in a finite product.
Sum filling the passed index with the passed value.
Modify a field of a finite sum. Error if the field does not exist.
Template from which a finite sum may be created.
Generally, you would define a
in your language's statics,
then transform this into a
Product MyIxSet MyType
mkSumTemplate, and use
that template with
mkSum to create actual
values in your
Sum MyIxSet MyValue