Safe Haskell | Safe-Inferred |
---|

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

# Documentation

mkProduct :: Eq i => [(i, a)] -> Product i aSource

Form a `Product`

with all fields filled from an association list.
Error if the keys are not distinct.

getProd :: Eq i => Product i a -> i -> aSource

Look up a field of a finite product. Error if the field does not exist.

setProd :: Eq i => Product i a -> i -> a -> Product i aSource

Modify a field of a finite product. Error if the field does not exist.

hasProd :: Eq i => Product i a -> i -> BoolSource

Check for existence of a field in a finite product.

mkSum :: Eq i => SumTemplate i -> i -> a -> Sum i aSource

Form a `Sum`

filling the passed index with the passed value.

setSum :: Eq i => Sum i a -> i -> a -> Sum i aSource

Modify a field of a finite sum. Error if the field does not exist.

data SumTemplate i Source

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

template with `SumTemplate`

MyIxSet`mkSumTemplate`

, and use
that template with `mkSum`

to create actual

values in your
interpreter.
`Sum`

MyIxSet MyValue

mkSumTemplate :: Product i a -> SumTemplate iSource

Create a `SumTemplate`

with index set identical to the input.