Data type algebra
Fundamental types
Fundamental types are types built-in in strict encoding and not derived from any other types. These types include:
Unit type
()
Byte type
Byte
Integer numbers (signed
I_
, unsignedU_
and naturalN_
)Floating-point numbers
F_
UTF-8 character
Utf8
Byte type is introduced due to the fact that it semantically different from a 8-bit signed or unsigned integer: it does not contain information about sign and may not be representative with an integer at all.
While Unicode character type can be expressed expressed as a composite type, it will be very verbose expression (union with 256 variants), so for the practical purposes (to reduce the complexity of types which use Unicode strings) it was decided to built it in.
Strict encoding has reserved place for 55 more types, which may be introduced in a future to represent more floating-point integer encodings, Unicode variants etc. At the present moment use of that type identifiers would result in encoding/decoding failure.
Integers
Integer types are named using a single upper case latter specifying set of integers used in type (U
for unsigned, I
for signed and N
for natural non-zero integers) followed by a decimal number of bits in the type encoding (like U8
or I1024
).
Strict encoding covers integer types of different size in two ranges:
Bit size | Step | |
---|---|---|
8 to 256 | 8 bits (i.e. 1 byte) | from U8 up to U256 |
272 to 4352 | 128 bits (i.e. 16 bytes) | from U272 to U4352 |
In total, there are 64 different types for unsigned integers, 64 types for signed and 64 types for non-zero integers, giving 194 possible integer types in total. Not all of these types have a representation in all of the supported languages, so below we give a list of integer types which can be represented in Rust:
Sten type | Bytes | Encoding | Rust type |
---|---|---|---|
| 1 | N/A |
|
| 2 | LE |
|
| 3 | LE |
|
| 4 | LE |
|
| 6 | LE |
|
| 8 | LE |
|
| 16 | LE |
|
| 32 | LE |
|
| 64 | LE |
|
| 128 | LE |
|
Floating-point numbers
Strict encoding supports the following floating number encodings:
Sten type | Bytes | Encoding | Rust type |
---|---|---|---|
| 2 | bfloat16 |
|
| 2 | IEEE Half |
|
| 4 | IEEE Single |
|
| 8 | IEEE Double |
|
| 10 | IEEE X87 Extended |
|
| 16 | IEEE Quad |
|
| 32 | IEEE Oct |
|
Strict encoding has 54 more type identifiers reserved for possible use by future floating-point number encodings (like Tappered float etc); at the present moment use of that type identifiers would result in encoding/decoding failure.
Type composition
Strict encoding uses generalized algebraic data types (GADT). This means that new types can be composed out of primitive types via following fundamental morphisms:
Name | Syntax form | Max no of elements / fields / variants |
---|---|---|
Product types (structure, tuple) |
| 255 |
Sum types (union, enum) |
| 255 |
Mapping (function) |
| From |
Fixed array |
|
|
Dynamic array |
| From |
Dynamic set |
| From |
Enums
Enums are a special case of unit type in which each variant is represented by a Byte
value.
Dynamic collections
Fundamental morphisms can be used to build more advanced types, like dynamic maps and dynamic arrays of tuples
Syntax form | No of elements | |
---|---|---|
Dynamic map |
| From |
Dynamic array of tuples |
| From |
Construction ^U..D
used in type expression specifying minimum and maximum size of a dynamic collection is called confinement bounds. It can be seen as an upper and lower indexes on the possible number of elements, i.e. type definition [Byte ^ 1..20]
can be read as and means product type with dynamic number of fields, from 1 to 20 max, where each field is a byte - or, in more common terms, an byte array of dynamic size which can't have less than one byte - and can't grow larger than 20 bytes.
For simplifying syntax strict encoding provides comprehensions and defaults for specifying the confinement bounds:
Comprehension | Expands to | Comment |
---|---|---|
|
| Default number of elements in confined collections is from zero to 2^16 |
|
| Collection which must contain at least one item |
|
| Collection with minimum of |
|
| Collection having maximum of |
Please note that type expression of [•^N..N]
is not allowed, since it means "dynamic" collection with a fixed number of items, which is nonsense, so please use [•^N]
instead.
Optionals
A special case of union type of frequent use is an optional monad, which may contain some type or be None
. In strict encoding there is a special comprehension for writing an optional: T?
, which is an equivalend ot writing (()|T)
.
Types provided by the standard library
Most frequently used types are provided by a strict encoding standard library StdLib
:
Sten type | Type definition | Rust type from amplify library |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
| type definition too long |
|
Last updated