Skip to content

Commit 05a112c

Browse files
authored
Adjust the BSATN doc to fit reality better (#216)
adjust BSATN doc to fit reality better
1 parent 5cd7182 commit 05a112c

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

docs/bsatn.md

+44-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SATN Binary Format (BSATN)
1+
# Binary SATN Format (BSATN)
22

33
The Spacetime Algebraic Type Notation binary (BSATN) format defines
44
how Spacetime `AlgebraicValue`s and friends are encoded as byte strings.
@@ -29,16 +29,26 @@ To do this, we use inductive definitions, and define the following notation:
2929
| [`AlgebraicValue`](#algebraicvalue) | A value of any type. |
3030
| [`SumValue`](#sumvalue) | A value of a sum type, i.e. an enum or tagged union. |
3131
| [`ProductValue`](#productvalue) | A value of a product type, i.e. a struct or tuple. |
32-
| [`BuiltinValue`](#builtinvalue) | A value of a builtin type, including numbers, booleans and sequences. |
3332

3433
### `AlgebraicValue`
3534

3635
The BSATN encoding of an `AlgebraicValue` defers to the encoding of each variant:
3736

3837
```fsharp
39-
bsatn(AlgebraicValue) = bsatn(SumValue) | bsatn(ProductValue) | bsatn(BuiltinValue)
38+
bsatn(AlgebraicValue)
39+
= bsatn(SumValue)
40+
| bsatn(ProductValue)
41+
| bsatn(ArrayValue)
42+
| bsatn(String)
43+
| bsatn(Bool)
44+
| bsatn(U8) | bsatn(U16) | bsatn(U32) | bsatn(U64) | bsatn(U128) | bsatn(U256)
45+
| bsatn(I8) | bsatn(I16) | bsatn(I32) | bsatn(I64) | bsatn(I128) | bsatn(I256)
46+
| bsatn(F32) | bsatn(F64)
4047
```
4148

49+
Algebraic values include sums, products, arrays, strings, and primitives types.
50+
The primitive types include booleans, unsigned and signed integers up to 256-bits, and floats, both single and double precision.
51+
4252
### `SumValue`
4353

4454
An instance of a sum type, i.e. an enum or tagged union.
@@ -60,44 +70,58 @@ bsatn(elems) = bsatn(elem_0) ++ .. ++ bsatn(elem_n)
6070

6171
Field names are not encoded.
6272

63-
### `BuiltinValue`
73+
### `ArrayValue`
74+
75+
The encoding of an `ArrayValue` is:
76+
77+
```
78+
bsatn(ArrayValue(a))
79+
= bsatn(len(a) as u32)
80+
++ bsatn(normalize(a)_0)
81+
++ ..
82+
++ bsatn(normalize(a)_n)
83+
```
84+
85+
where `normalize(a)` for `a: ArrayValue` converts `a` to a list of `AlgebraicValue`s.
6486

65-
An instance of a buil-in type.
66-
Built-in types include booleans, integers, floats, strings and arrays.
67-
The BSATN encoding of `BuiltinValue`s defers to the encoding of each variant:
87+
### Strings
6888

89+
For strings, the encoding is defined as:
6990
```fsharp
70-
bsatn(BuiltinValue)
71-
= bsatn(Bool)
72-
| bsatn(U8) | bsatn(U16) | bsatn(U32) | bsatn(U64) | bsatn(U128)
73-
| bsatn(I8) | bsatn(I16) | bsatn(I32) | bsatn(I64) | bsatn(I128)
74-
| bsatn(F32) | bsatn(F64)
75-
| bsatn(String)
76-
| bsatn(Array)
91+
bsatn(String(s)) = bsatn(len(s) as u32) ++ bsatn(utf8_to_bytes(s))
92+
```
93+
That is, the BSATN encoding is the concatenation of
94+
- the bsatn of the string's length as a `u32` integer byte
95+
- the utf8 representation of the string as a byte array
7796

78-
bsatn(Bool(b)) = bsatn(b as u8)
97+
### Primitives
98+
99+
For the primitive variants of `AlgebraicValue`, the BSATN encodings are:s
100+
101+
```fsharp
102+
bsatn(Bool(false)) = [0]
103+
bsatn(Bool(true)) = [1]
79104
bsatn(U8(x)) = [x]
80105
bsatn(U16(x: u16)) = to_little_endian_bytes(x)
81106
bsatn(U32(x: u32)) = to_little_endian_bytes(x)
82107
bsatn(U64(x: u64)) = to_little_endian_bytes(x)
83108
bsatn(U128(x: u128)) = to_little_endian_bytes(x)
109+
bsatn(U256(x: u256)) = to_little_endian_bytes(x)
84110
bsatn(I8(x: i8)) = to_little_endian_bytes(x)
85111
bsatn(I16(x: i16)) = to_little_endian_bytes(x)
86112
bsatn(I32(x: i32)) = to_little_endian_bytes(x)
87113
bsatn(I64(x: i64)) = to_little_endian_bytes(x)
88114
bsatn(I128(x: i128)) = to_little_endian_bytes(x)
115+
bsatn(I256(x: i256)) = to_little_endian_bytes(x)
89116
bsatn(F32(x: f32)) = bsatn(f32_to_raw_bits(x)) // lossless conversion
90117
bsatn(F64(x: f64)) = bsatn(f64_to_raw_bits(x)) // lossless conversion
91118
bsatn(String(s)) = bsatn(len(s) as u32) ++ bsatn(bytes(s))
92-
bsatn(Array(a)) = bsatn(len(a) as u32)
93-
++ bsatn(normalize(a)_0) ++ .. ++ bsatn(normalize(a)_n)
94119
```
95120

96121
Where
97122

98-
- `f32_to_raw_bits(x)` is the raw transmute of `x: f32` to `u32`
99-
- `f64_to_raw_bits(x)` is the raw transmute of `x: f64` to `u64`
100-
- `normalize(a)` for `a: ArrayValue` converts `a` to a list of `AlgebraicValue`s
123+
- `f32_to_raw_bits(x)` extracts the raw bits of `x: f32` to `u32`
124+
- `f64_to_raw_bits(x)` extracts the raw bits of `x: f64` to `u64`
101125

102126
## Types
103127

0 commit comments

Comments
 (0)