hexpr-0.0.0.0: A framework for symbolic, homoiconic languages.

Data.FiniteType

Description

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.

Synopsis

# Documentation

data Product i a Source

Types where every field in the index set is filled with a value.

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.

data Sum i a Source

Types where exactly one feild in the index set is filled with a value.

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

Form a `Sum` filling the passed index with the passed value.

getSum :: Sum i a -> aSource

Extract the value of the unique filled field in a finite sum.

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.

hasSum :: Sum i a -> iSource

Determine which field is filled in a finite sum.

data SumTemplate i Source

Template from which a finite sum may be created.

Generally, you would define a `Product MyIxSet MyType` in your language's statics, then transform this into a `SumTemplate MyIxSet` template with `mkSumTemplate`, and use that template with `mkSum` to create actual `Sum MyIxSet MyValue` values in your interpreter.

Create a `SumTemplate` with index set identical to the input.