Safe Haskell | None |
---|---|
Language | Haskell2010 |
Utilities for constructing enumerations over datatypes.
An Enumeration
is a mapping between a particular datatype and a
Path
, which consists of a list of natural numbers. Conceptually,
we can think of all the values of a given datatype as being
organized into a tree, with nodes representing decisions that
narrow down the choices. In such a scheme, a Path
represents a
path through the tree to a single value, and an Enumeration
is a
procedure for converting a Path
to and from a value.
An Enumeration
has two key functions: fromPath
and toPath
,
which translate between paths and instances of a datatype. These
functions are expected to be inverses; or:
fromPath (toPath v) == v
for all values in the domain
Beyond this, there are no additional restrictions. Specifically, two paths may map to the same value.
The numBranches
function indicates the maximum value of the first
path element for an Enumeration
. The minimum value is always 0,
and all values between 0 and numBranches
must be valid. If there
is no upper bound on the value of the first path element,
numBranches
returns Nothing
. The toSizedPath
maps a value to
a path, which also contains the result of numBranches
at each
step in the path.
The withPrefix
function supplies a partial path to an
Enumeration
, yielding a new Enumeration
that maps each value to
the same path(s) as the original Enumeration
, with the prefix
added or removed. More formally, if
subenc = withPrefix enc prepath
Then
toPath enc val == prepath ++ toPath subenc val
fromPath enc (prepath ++ subpath) == fromPath subenc subpath
With multiple uses of withPrefix
, the following must be true:
withPrefix (withPrefix enc path1) path2 == withPrefix enc (path1 ++ path2)
Finally, if a complete path is given to withPrefix
, then the
result is a singleton encoding that gives the value associated with
that path. That is:
fromPath enc fullpath == fromPath (withPrefix enc fullpath) []
This provides "warm-start" functionality for Enumeration
s.
When translating a large number of Path
s with the same prefix, it
will generally be much more efficient to use withPrefix
and use
the resulting Enumeration
than to translate the Path
s directly.
The prefix
function gives the current prefix for an
Enumeration
. The following rule describes the relationship
between prefix
and withPrefix
:
prefix (withPrefix enc prepath) == prepath
Enumeration
s are similar to Encoding
s from the arith-encode
library, except Enumeration
s are generally more flexible, and can
more easily accomodate complex datatypes with invariants. However,
as Path
s are constructed from natural numbers, we can create an
Enumeration
using a series of Encoding
s for intermediate data.
The functions in this module provide the ability to construct
Enumeration
s using Encoding
s
A singleton Enumeration
can be constructed using the singleton
and singletonWithPrefix
functions.
An Encoding
for a datatype can be converted into an Enumeration
(where all paths have a single element) using the fromEncoding
and fromEncodingWithPrefix
functions.
The step
and stepWithPrefix
functions construct an
Enumeration
from an Encoding
for an intermediate value, and a
generator function that produces an Enumeration
from the
intermediate value.
The withPrefix
variants for each of these take a prefix path,
where the non-withPrefix
variants set the prefix to []
.
- data Enumeration ty
- type Path = [Integer]
- data BadPath = BadPath String
- data IllegalArgument :: * = IllegalArgument !String
- fromPath :: Enumeration ty -> Path -> ty
- toPath :: Enumeration ty -> ty -> Path
- toSizedPath :: Enumeration ty -> ty -> [(Integer, Maybe Integer)]
- withPrefix :: Enumeration ty -> Path -> Enumeration ty
- numBranches :: Enumeration ty -> Maybe Integer
- prefix :: Enumeration ty -> Path
- singleton :: Eq ty => ty -> Enumeration ty
- singletonWithPrefix :: Eq ty => Path -> ty -> Enumeration ty
- fromEncoding :: Eq ty => Encoding ty -> Enumeration ty
- fromEncodingWithPrefix :: Eq ty => Path -> Encoding ty -> Enumeration ty
- step :: Encoding ty1 -> (Path -> ty1 -> Enumeration ty2) -> (ty2 -> ty1) -> Enumeration ty2
- stepWithPrefix :: Path -> Encoding ty1 -> (Path -> ty1 -> Enumeration ty2) -> (ty2 -> ty1) -> Enumeration ty2
Definitions
data Enumeration ty Source
A datatype that represents a mapping between Path
s and ty
s.
Note that unlike Encoding
s, not all Path
s are necessarily
valid.
An exception thrown when a Path
is invalid.
data IllegalArgument :: *
Using Enumerations
fromPath :: Enumeration ty -> Path -> ty Source
Generate a ty
from a Path
toPath :: Enumeration ty -> ty -> Path Source
Convert a ty
to a Path
toSizedPath :: Enumeration ty -> ty -> [(Integer, Maybe Integer)] Source
Convert to a list of pairs, where the fst
holds
the path entry, and snd
holds numBranches
. This is used
primarily for encoding values as binary.
withPrefix :: Enumeration ty -> Path -> Enumeration ty Source
Given a prefix path, get an enumeration that generates ty
s
from the rest of the path.
numBranches :: Enumeration ty -> Maybe Integer Source
Get the upper bound on values for the first path component,
or Nothing
if there is no bound.
prefix :: Enumeration ty -> Path Source
The prefix path.
Constructions
:: Eq ty | |
=> ty | The value to map to and from the empty path. |
-> Enumeration ty |
Create an Enumeration
with an empty prefix that maps a single
value to and from the empty path. Equivalent to
singletonWithPrefix []
singletonWithPrefix :: Eq ty => Path -> ty -> Enumeration ty Source
Create an Enumeration
with a given prefix path that maps a
single value to and from the empty path.
:: Eq ty | |
=> Encoding ty | The |
-> Enumeration ty |
Create an Enumeration
with an empty prefix from a single
Encoding
. The Path
will always be of length 1, and contains
the encoded value.
fromEncodingWithPrefix :: Eq ty => Path -> Encoding ty -> Enumeration ty Source
Create an Enumeration
with a given prefix from a single
Encoding
. The Path
will always be of length 1, and contains
the encoded value.
:: Encoding ty1 | The encoding for the first type. |
-> (Path -> ty1 -> Enumeration ty2) | A function that produces an enumeration from the first type. |
-> (ty2 -> ty1) | A function that extracts the first type from the second. |
-> Enumeration ty2 |
Create an Enumeration
with an empty prefix that uses an
Encoding
to convert the first element of the path to an interim
value, then uses that value to construct an Enumeration
to decode
the rest of the path.
:: Path | The prefix path. |
-> Encoding ty1 | The |
-> (Path -> ty1 -> Enumeration ty2) | A function that produces an enumeration from the first type. |
-> (ty2 -> ty1) | A function that extracts the first type from the second. |
-> Enumeration ty2 |
Create an Enumeration
with a prefix that uses an Encoding
to
convert the first element of the path to an interim value, then
uses that value to construct an Enumeration
to decode the rest of
the path.