Documentation
¶
Overview ¶
Package data is ontos/data (L2): the registered canonical embeddings of common data into ontos/core L0 values — ontos-data-v1. The v1 scalars and list (int, utf8-text, bool, list) were frozen 2026-05-31; the structural pair map and set (spec §5.7–§5.8) were frozen 2026-06-01. See docs/spec/ontos-data.md.
An embedding is a recognized L1 labeled compound Tuple(Atom(label), payload…) whose label is registered here and whose payload shape is fixed by the spec. Each datum has exactly ONE canonical form; this layer introduces NO new value and NO new equality — identity is L0 structural identity, always (spec §2).
int Tuple(Atom("int"), Atom(sign ‖ big-endian minimal magnitude))
utf8-text Tuple(Atom("utf8-text"), Atom(valid UTF-8 bytes, verbatim))
bool Tuple(Atom("bool"), Atom(0x00 = false | 0x01 = true))
list Tuple(Atom("list"), elem₀ … elemₙ₋₁)
map Tuple(Atom("map"), Tuple(k₀,v₀) … Tuple(kₙ₋₁,vₙ₋₁))
set Tuple(Atom("set"), e₀ … eₙ₋₁)
decimal Tuple(Atom("decimal"), int(mantissa), int(exponent)) // mantissa × 10^exponent
Two directions per embedding: Encode<Kind> builds the canonical L0 value from a typed datum (the producer normalizes before building, spec §4); Read<Kind> recognizes whether an L0 value is a well-formed canonical embedding of that kind and returns the typed datum, or signals NOT-RECOGNIZED via a *DataError. Recognition is partial and opt-in (spec §1, §7): every non-canonical or malformed form is rejected (never quotiented to equal data, spec §2). Reads never panic on untrusted input — a not-recognized value is an error, not a crash.
map (spec §5.7) and set (spec §5.8) are the embeddings whose canonical form depends on the codec rather than on L0 structure alone: map's entries are sorted strictly ascending by the ontos-codec-v1 byte order on the key, and set's elements strictly ascending by that order on the whole element (so EncodeMap/ ReadMap/EncodeSet/ReadSet call codec.Encode at runtime). set is map's key-discipline with no values — the unordered, unique sibling of list/map. Both are codec-version-relative — frozen against ontos-codec-v1.
decimal (spec §5.9, frozen 2026-06-06) is the fourth scalar: a label "decimal" followed by two int children, the value mantissa × 10^exponent. A non-zero mantissa is not divisible by 10 (no trailing base-10 zero) and zero is decimal(int 0, int 0), so each decimal has one canonical spelling. Unlike the containers list/map/set, decimal RECURSES into its children — they must be canonical ints (the scalar discipline). It is codec-INdependent (its canonical form is purely structural) and mirrors int's recognize/read split (RecognizeDecimal is structural; ReadDecimal materializes to *big.Int, which here is unbounded so it never hits a host limit).
This package is kept beside the model (it imports core, never the reverse). It has no runtime dependency beyond core, ontos/codec (for the map key order, §5.7), and the standard library.
Index ¶
- Variables
- func EncodeBool(b bool) core.Value
- func EncodeDecimal(mantissa, exponent *big.Int) core.Value
- func EncodeInt(n *big.Int) core.Value
- func EncodeList(elems []core.Value) core.Value
- func EncodeMap(entries [][2]core.Value) (core.Value, error)
- func EncodeNull() core.Value
- func EncodeSet(elems []core.Value) (core.Value, error)
- func EncodeText(s string) (core.Value, error)
- func MustEncodeText(s string) core.Value
- func ReadBool(v core.Value) (bool, error)
- func ReadDecimal(v core.Value) (mantissa, exponent *big.Int, err error)
- func ReadInt(v core.Value) (*big.Int, error)
- func ReadList(v core.Value) ([]core.Value, error)
- func ReadMap(v core.Value) ([][2]core.Value, error)
- func ReadNull(v core.Value) error
- func ReadSet(v core.Value) ([]core.Value, error)
- func ReadText(v core.Value) (string, error)
- func RecognizeDecimal(v core.Value) error
- func RecognizeInt(v core.Value) error
- func RecognizeNull(v core.Value) error
- type DataError
Constants ¶
This section is empty.
Variables ¶
var Labels = []string{labelInt, labelText, labelBool, labelList, labelMap, labelSet, labelDecimal, labelNull}
Labels is the canonical ordered list of ontos-data-v1 embedding labels, in the fixed recognition order (spec §5: int, utf8-text, bool, list, map, set, decimal, null). It is the ONE source of the registered label set and its order within this module — built from the named label* constants above so the spelling lives in one place, and pinned against the actual Read<Kind> recognizers by TestLabelsMatchRecognizedOrder. decimal (§5.9) was appended 2026-06-06 and null (§5.10) on 2026-06-13; the order carries no meaning beyond the recognition sequence, so a new label appends rather than reordering.
The CLI keeps its own copy of this order by deliberate design (so it does not depend on this being exported); a cli-side parity test pins that copy against Labels. The order is part of the CLI's `recognized` JSON contract, so drift is a real, CI-caught parity bug — hence the single source here.
Functions ¶
func EncodeBool ¶
func EncodeBool(b bool) core.Value
EncodeBool builds the canonical bool embedding of b (spec §5.3): payload is the single byte 0x01 (true) or 0x00 (false).
func EncodeDecimal ¶
EncodeDecimal builds the canonical decimal embedding of mantissa × 10^exponent (spec §5.9): Tuple(Atom("decimal"), int(mantissa), int(exponent)). The producer normalizes first (normalizeDecimal): a non-zero mantissa is stripped of trailing base-10 zeros into the exponent, and zero is decimal(int 0, int 0). The children are the full canonical int embedding (§5.1), per the value-not-position rule (§2.1).
func EncodeInt ¶
EncodeInt builds the canonical int embedding of n (spec §5.1): a one-byte sign (0x00 non-negative, 0x01 negative) followed by the big-endian minimal-length magnitude of |n|. Zero is the single byte 0x00; there is no negative zero.
func EncodeList ¶
func EncodeList(elems []core.Value) core.Value
EncodeList builds the canonical list embedding of elems (spec §5.5): the "list" label followed by the elements in order (tuple arity len(elems)+1). Elements are arbitrary L0 values, each expected to already be in its own canonical form (the value-not-position rule, spec §2.1); the wrapper adds no further normalization. The empty list is Tuple(Atom("list")) (arity 1). Order and multiplicity are identity — the list is never sorted or deduplicated.
func EncodeMap ¶
func EncodeMap(entries [][2]core.Value) (core.Value, error)
EncodeMap builds the canonical map embedding of entries (spec §5.7): the "map" label followed by the (key, value) entries as arity-2 tuples, SORTED strictly ascending by the ontos-codec-v1 byte order on the key (tuple arity len+1). Keys and values are arbitrary L0 values, each expected to already be in its own canonical form (the value-not-position rule, spec §2.1); the wrapper adds no further normalization and does NOT recurse into a key/value that claims an embedding label.
Unlike the other encoders, EncodeMap is FALLIBLE: duplicate keys (two entries whose key encodings are EQUAL) are forbidden, and EncodeMap does NOT pick a winner — the producer must resolve duplicate intent before calling (spec §5.7, §4). It returns a *DataError (Kind "map") when two keys collide; the order is over codec bytes, never numeric or Unicode collation. The empty map is Tuple(Atom("map")) (arity 1).
func EncodeNull ¶
func EncodeNull() core.Value
EncodeNull builds the canonical null embedding (spec §5.10): the nullary compound Tuple(Atom("null")) — the "null" label with NO payload children (tuple arity exactly 1). null is the single canonical spelling of present-but-no-value; it has exactly one inhabitant (there is no value to vary), so EncodeNull takes no argument and always returns the same value. Its byte form is fixed by structure alone, so — like list and bool — it is codec-independent and not codec-version-relative.
func EncodeSet ¶
func EncodeSet(elems []core.Value) (core.Value, error)
EncodeSet builds the canonical set embedding of elems (spec §5.8): the "set" label followed by the elements, SORTED strictly ascending by the ontos-codec-v1 byte order on the WHOLE element (tuple arity len+1). It is `map` (§5.7) with no values — the same total order, but the elements are single L0 values, not (key, value) entries, so there is no entry-arity shape. Elements are arbitrary L0 values, each expected to already be in its own canonical form (the value-not-position rule, spec §2.1); the wrapper adds no further normalization and does NOT recurse into an element that claims an embedding label.
Like EncodeMap, EncodeSet is FALLIBLE: duplicate elements (two whose encodings are EQUAL) are forbidden, and EncodeSet does NOT pick a winner — the producer must remove duplicates before calling (spec §5.8, §4). It returns a *DataError (Kind "set") when two elements collide; the order is over codec bytes, never numeric or Unicode collation. The empty set is Tuple(Atom("set")) (arity 1).
func EncodeText ¶
EncodeText builds the canonical utf8-text embedding of s (spec §5.2): the payload atom is the string's UTF-8 bytes verbatim, with NO Unicode normalization. The empty string yields the empty-atom payload.
FALLIBLE (spec §4.1). A Go string is an arbitrary immutable byte sequence and is NOT guaranteed to be valid UTF-8 — string([]byte{0x80}) is a perfectly ordinary Go string. Such a string is outside utf8-text's admissible domain and is REJECTED here.
It is rejected rather than encoded because writing the bytes through verbatim mints a value that this package's own ReadText refuses (it requires a valid-UTF-8 payload) — spec §4.1 law 1, producer soundness. The result would be a value only this producer can make and no reader can read, which no caller can detect without re-reading its own output.
This signature was previously total, and returning the bytes unchecked was justified in a comment claiming "a Go string is already a UTF-8 byte sequence". That is false: Go source literals are UTF-8, but a string built from bytes, read off a socket, or sliced mid-rune is not. Correcting it necessarily changes the signature — §4.1 makes fallibility a conformance property, not a style choice.
func MustEncodeText ¶
func MustEncodeText(s string) core.Value
MustEncodeText is EncodeText for callers holding a string already known to be valid UTF-8 — a source literal, or text just returned by ReadText. It panics on an invalid string rather than minting an unreadable value.
Prefer EncodeText anywhere the string's provenance is not locally obvious: this helper trades a silent corruption for a loud crash, which is the right trade only when the caller can genuinely rule the input out.
func ReadBool ¶
ReadBool recognizes a canonical bool embedding and returns its value, or a *DataError. Accepted iff: arity-2 tuple, label "bool", single-byte atom payload equal to 0x00 (false) or 0x01 (true). Any other payload (empty, longer, or a different byte) is not a well-formed bool.
func ReadDecimal ¶
ReadDecimal recognizes a canonical decimal embedding and returns (mantissa, exponent) as *big.Int (the value is mantissa × 10^exponent), or a *DataError — the same canonical-form validation as RecognizeDecimal. This binding is unbounded (*big.Int), so it never reports a host-width resource limit. Never panics on untrusted input.
func ReadInt ¶
ReadInt recognizes a canonical int embedding and returns its integer value, or a *DataError if v is not a well-formed int — the same canonical-form validation as RecognizeInt (see intPayload). This binding is unbounded (*big.Int), so it never reports a host-width resource limit. Accepted iff: arity-2 tuple, label "int", atom payload p with len(p) >= 1, p[0] in {0x00, 0x01}, magnitude m = p[1:] has no leading 0x00, and not negative zero (empty magnitude with sign 0x01).
func ReadList ¶
func ReadList(v core.Value) ([]core.Value, error)
ReadList recognizes a canonical list embedding and returns its element values (possibly empty), or a *DataError. Accepted iff: tuple of arity >= 1 whose first child is the atom "list". The elements are the remaining children, returned in order with NO per-element validation (list constrains only its own shape, never element kind, spec §5.5). The bare unlabeled Tuple(elem…) form is NOT a list, and the arity-0 empty tuple is NOT the empty list (the empty list is the arity-1 Tuple(Atom("list"))).
func ReadMap ¶
func ReadMap(v core.Value) ([][2]core.Value, error)
ReadMap recognizes a canonical map embedding and returns its (key, value) entries in order (possibly empty), or a *DataError. In a single left-to-right pass (spec §5.7): (1) v is a Tuple of arity >= 1 whose child 0 is the atom "map" (else not recognized); (2) arity 1 => the empty map, no entries; (3) every later child is a Tuple of arity EXACTLY 2 (else not recognized); (4) for each entry after the first, encode(prevKey) < encode(key) STRICTLY — this single comparison rejects both mis-sorted (greater) and duplicate (equal, by codec injectivity) entries. There is NO per-entry validation of whether a key or value that claims an embedding label is itself canonical (a non-canonical-int key is an accepted key, spec §5.7), mirroring ReadList. Never panics on untrusted input.
func ReadNull ¶
func ReadNull(v core.Value) error
ReadNull recognizes the canonical null embedding (spec §5.10), returning a *DataError iff v is not the single inhabitant Tuple(Atom("null")). There is no value to return — null is present-but-no-value — so a nil error is the whole result; it never panics. Identical validation to RecognizeNull (null has no payload, so recognize and read coincide, as they do for the empty containers).
func ReadSet ¶
func ReadSet(v core.Value) ([]core.Value, error)
ReadSet recognizes a canonical set embedding and returns its element values in order (possibly empty), or a *DataError. In a single left-to-right pass (spec §5.8): (1) v is a Tuple of arity >= 1 whose child 0 is the atom "set" (else not recognized); (2) arity 1 => the empty set, no elements; (3) for each element after the first, encode(prev) < encode(elem) STRICTLY — this single comparison rejects both mis-sorted (greater) and duplicate (equal, by codec injectivity) elements. Unlike ReadMap there is NO per-element arity check: elements are single values, not arity-2 entry tuples. There is NO recursion into whether an element that claims an embedding label is itself canonical (a non-canonical-int element is accepted, spec §5.8), mirroring ReadList/ReadMap. Never panics on untrusted input.
func ReadText ¶
ReadText recognizes a canonical utf8-text embedding and returns the string, or a *DataError. Accepted iff: arity-2 tuple, label "utf8-text", atom payload whose bytes are valid UTF-8 (the empty atom — the empty string — is valid). The bytes are returned verbatim, with no normalization.
func RecognizeDecimal ¶
func RecognizeDecimal(v core.Value) error
RecognizeDecimal reports whether v is a well-formed canonical decimal embedding WITHOUT materializing its mantissa/exponent: nil for every canonical decimal (including one whose mantissa or exponent magnitude exceeds any fixed host width), or a *DataError. Structural, like RecognizeInt; this Go binding is arbitrary-precision (*big.Int) so it can always materialize what it recognizes, but ontos's tri-core CLIs use this surface for `read --kind decimal` so they agree regardless of host width (spec §5.9). Never panics.
func RecognizeInt ¶
func RecognizeInt(v core.Value) error
RecognizeInt reports whether v is a well-formed canonical int embedding WITHOUT materializing it into a host integer type: nil for every canonical int (including a magnitude beyond any fixed host width), or a *DataError for a non-canonical form.
Recognition is STRUCTURAL — "does v have the frozen canonical int form?" — distinct from materialization ("can a binding's host integer type hold it?"). This Go binding is arbitrary-precision (*big.Int), so it can always materialize what it recognizes; the distinction is load-bearing for a binding with a bounded host integer (e.g. Rust's i128), and ontos's tri-core CLIs use this surface for `read --kind int` so they agree on recognition regardless of host width (ontos-data.md §5.1). Never panics.
func RecognizeNull ¶
func RecognizeNull(v core.Value) error
RecognizeNull reports whether v is the canonical null embedding (spec §5.10): nil iff v is a tuple of arity EXACTLY 1 whose only child is the atom "null", or a *DataError otherwise. Any payload (arity > 1) is NOT a well-formed null — it remains a valid L0 value, merely unrecognized (the typed not-recognized rejection, never a panic and never a quotient). null carries no value to materialize, so this is also the read surface; ReadNull is the alias the tri-core CLIs would use. The bare unlabeled Tuple() (arity 0) and a bare Atom("null") are likewise not a null.
Types ¶
type DataError ¶
DataError reports that a value is not a well-formed canonical embedding of the named kind (NOT-RECOGNIZED). It mirrors codec.DecodeError: a stable, diagnostic Kind plus a human-readable Reason. The Kind is the embedding label ("int", "bool", "utf8-text", "list", "map", "set"); the Reason is diagnostic only.
func (*DataError) Code ¶
Code returns the embedding label this error rejected (one of Labels: "int", "utf8-text", "bool", "list", "map", "set"). It is a LANGUAGE-LOCAL diagnostic, NOT a cross-language contract: the three data cores expose codes on different axes (Go returns the rejected kind; Rust and TS return failure-reason sets that themselves differ), and unlike the codec's DecodeError.Code these are NOT pinned in the conformance vectors — recognition is partial and opt-in (spec §1, §7). Use it for local diagnostics only; do not rely on it across cores or as a stable wire contract.