Schemas¶
A schema fixes the exact form of a construct that recurs across the libraries, in every place it appears: its definition, its view and its binding. Code either has that form or not, so a schema is decidable, and a new instance is written by filling the schema in, not by copying a neighbor. They apply to every library of the chain and are maintained only here.
Records¶
A record is a type the libraries keep as flat byte buffers in a monotonically growing urd Store: a tag T, its stored
form urd::data<T>, its builder urd::builder<T, C>, its views and their Python classes.
The examples use one library, lib, with a component formalism and three records:
Term, an object or a variable;Predicate<F>, a name and an arity, of a fact kindF(static or fluent);Atom<T, F>, a predicate applied to terms, lifted (T = LiftedTag: its terms may be variables) or ground (T = GroundTag).
<Name> is a record’s name in CamelCase (Atom).
Kept flat, a record of any size costs no allocation of its own: a reused builder holds its fields, vectors included, while it is built, and interning writes them into the Store’s buffers, which only grow. Creating dynamically sized objects therefore allocates nothing once those buffers have grown; an index handed out stays valid, as no record is ever removed.
The record schema solves a problem that schema-driven data libraries such as Protocol Buffers
and Cap’n Proto also address: declare a structured type once, and get from that one
declaration how it is built, how it is read and how it reaches another language. Cap’n Proto comes closest. Its
generated code splits each struct into a Builder, which writes into arena segments, and a Reader, which reads the same
bytes in place, without a decoding step. urd’s builder<T, C> and index_view<T, C> make the same split, and its
records are flat bytes as well. Protocol Buffers generates one message class that owns its fields and is encoded to and
decoded from a wire format.
The goal differs, and with it the design:
Deduplication instead of messages. Both libraries build self-contained messages to exchange between programs: a message is a value, compared field by field, and neither keeps a store of distinct messages (Protocol Buffers’ deterministic serialization is not canonical, so its bytes cannot serve as a key). A record lives in a Store that keeps each distinct record once and identifies it by a dense index: a builder interns its children first and brings its record into its domain’s canonical form (sorted literals, say) before it is looked up, so that equal structures are one record; inserting an existing record returns its index, equality is comparing indices, and an index keys a table.
Typed references instead of embedded children. A child of a message is embedded in it (Protocol Buffers copies submessages; a Cap’n Proto pointer stays within its message, and its builder cannot point twice at one object). Sharing a child would mean replacing it by an integer field, which the generated code neither types nor resolves. A child of a record is an
index<T>of its own type, which its view resolves to the child’s view, so records form one shared graph across the Store.Reading through a context. A Cap’n Proto Reader resolves pointers inside its own message. A view resolves its children through a context, the repository (S3), which can be layered over a frozen parent’s (a Datalog program’s rules refer to its planning task’s atoms without copying them) or can nest the repositories its children live in.
Atom in UML. The Store keeps each record once and hands out its index; a view
holds an index and the Store (its context) and inherits the field getters of its mixin. The builder has the record's
fields, each in its built form (S2), and only mutates; builders are pooled in a Store of slots, so building allocates
nothing once the slots have grown. A borrowed view reads a builder, or a record not in the Store, with the same
mixin.A repository reads a record by its index (operator[], unchecked, and at, checked), and inserts and finds a
record from its builder or its urd::data<T>.
Note
Keys of several parts. A record numbered within another (a row within its relation) is identified by a key of
several parts, urd::key<Ts...>, resolved in order, each part selecting the container the next is looked up in:
urd::key<F, Row<F>> is a row’s relation, then the row there. A specialization urd::key_of<T> beside the tag
names it (urd::key_t<T>; urd::key<T> by default). urd::index<T> is the key of one part, so every lookup,
insertion and count takes a key, whatever its number of parts. The record holds its own part as index and the
parts before it as fields (a row’s relation); its key() joins them (S1).
Note
A key that is a handle. A record whose Store names it by a handle rather than a number (a state’s storage, by
its pool’s tree handle) says so by a specialization urd::key_value_of<T> beside the tag: type, the handle,
and none, the handle that names nothing. Its key is urd::key<T> as any other’s; urd::index<T> names a
number and does not exist for it.
Note
The stored form. A lookup hands a record out as urd::stored_ref<T>, by value: by default a reference to the
stored urd::data<T>. A record kept in a representation of its own specializes the two together:
urd::stored_ref_of<T> names what a lookup hands out, and urd::data<T> stays the owning form it is inserted
and found from. A row is stored packed at its columns’ widths: stored_ref_of<Row<F>> is PackedRow<F>, a
reader of its bits, and data<Row<F>> holds its relation and its values. How each field is read from the stored
form is a specialization urd::stored_field<Member, Stored> beside it (a row’s values: its bits; its relation: its
slot’s). A state’s storage is stored in its pool’s tree, its lookup a reader of the pool and its key, its values
unpacked on reading. The views read every form through the same mixin (S3). This is the one customization point of
how a record is stored.
S1 decidable A record is
urd::data<T>, with an index, its fields and nothing else.- Name:
schema-record-data- Check:
python -m pybrokkr.rules S1(with the compile database of a configured build)
The tag
Tis an empty struct (struct Term {};), or a template of tags (template<FactKind F> struct Predicate {};). Each tag has one specializationdata<T>; a family of tags has one partial specialization per shape of its members whose fields differ (the lifted and the ground atoms), or one for the whole family.Each specialization has, in this order:
urd::key<T> index;, where the record has an index: its own part of its key (a number, or a handle its Store hands out), which its Store writes at insertion, empty until then. A value held in other records has none (Values);the fields, data members of any type that urd can store as bytes;
data() = default;and the constructor from the fields in order, which leavesindexempty;either the copy deleted and the move and the destructor defaulted, as in the example, so that a copy that would cost the record’s size is a compile error; or, for a trivially copyable record (indices and numbers only), its declaration that it is copied, which the compiler checks:
static constexpr bool copyable() noexcept { static_assert(std::is_trivially_copyable_v<data>, "a copyable record is trivially copyable"); return true; }
fields(), which names each field once, under its member’s name:urd::Fields<urd::field<"<member>", &data::<member>>...>;identifying_members(), the members that make the record what it is:urd::members_of(fields())for all fields, or some of them where the record stores more than identifies it;cista_members():urd::Members<&data::index, &data::<member>...>, the index and the fields in their order;key(), where it has an index: its whole key,urd::key_t<T>, the one way urd reads it: the fields that select the container it lies in, if any, then its index (an atom’surd::key<Atom>(index), a row’surd::key<F, Row<F>>(relation, index)).
A record declares nothing else: no other function, alias, variable or
static_assert. A value derived from the fields is a function of the view (S3), not a member. The lifted atoms’ record:// include/lib/formalism/atom_data.hpp namespace urd { template<lib::formalism::FactKind F> struct data<lib::formalism::Atom<lib::LiftedTag, F>> { urd::key<lib::formalism::Atom<lib::LiftedTag, F>> index; urd::index<lib::formalism::Predicate<F>> predicate; urd::flat_vector<urd::index<lib::formalism::Term>> terms; data() = default; data(urd::index<lib::formalism::Predicate<F>> predicate_, urd::flat_vector<urd::index<lib::formalism::Term>> terms_) : index(), predicate(predicate_), terms(std::move(terms_)) {} data(const data& other) = delete; data& operator=(const data& other) = delete; data(data&& other) = default; data& operator=(data&& other) = default; ~data() = default; static constexpr auto fields() noexcept { return urd::Fields<urd::field<"predicate", &data::predicate>, urd::field<"terms", &data::terms>> {}; } static constexpr auto identifying_members() noexcept { return urd::members_of(fields()); } static constexpr auto cista_members() noexcept { return urd::Members<&data::index, &data::predicate, &data::terms> {}; } urd::key<lib::formalism::Atom<lib::LiftedTag, F>> key() const noexcept { return urd::key<lib::formalism::Atom<lib::LiftedTag, F>>(index); } }; }
S2 decidable A record’s builder is
urd::builder<T, C>, with the record’s index and fields, each in its built form, and functions that change it.- Name:
schema-record-builder- Check:
python -m pybrokkr.rules S2(with the compile database of a configured build)
A record that is built before it is interned has a builder: a specialization
urd::builder<T, C>with the template header of its record and the typeCof the context it is read through (a type only: no member holds a context). Each specialization has, in this order:urd::key<T> index;, where its record has one;for each field of
data<T>, in its order and under its name,urd::built_t<M, Context> <field>;, whereMis the field’s type in the record andContextthe repository that holdsM’s records (Cfor the record’s own).built_tgives the built form: an index becomes a child (its index, or a handle that owns a builder or a record not interned yet), aurd::data<T>its builder, urd’s flat containers std’s (aflat_vectorastd::vector), anything else stays as it is;fields()andidentifying_members(), as its record’s, by member pointers of the builder;public member functions that change it, none
constand nonestatic: a builder is read through its view (S3), so a public read on it would be a second place for it;functions(), where it has public member functions, each listed under its Python name (urd::function<"clear", &builder::clear>), so that what a builder offers in Python is decided where it is (S4).
A builder declares nothing else: no base, constructor, alias, variable, friend, nor a member that refers to its context. A builder whose child owns a record is move-only;
urd::clonecopies it deeply. The lifted atoms’ builder:// include/lib/formalism/atom_builder.hpp namespace urd { template<lib::formalism::FactKind F, typename C> struct builder<lib::formalism::Atom<lib::LiftedTag, F>, C> { urd::key<lib::formalism::Atom<lib::LiftedTag, F>> index; urd::built_t<urd::index<lib::formalism::Predicate<F>>, C> predicate; urd::built_t<urd::flat_vector<urd::index<lib::formalism::Term>>, C> terms; static constexpr auto fields() noexcept { return urd::Fields<urd::field<"predicate", &builder::predicate>, urd::field<"terms", &builder::terms>> {}; } static constexpr auto identifying_members() noexcept { return urd::members_of(fields()); } void clear() noexcept { terms.clear(); } static constexpr auto functions() noexcept { return urd::Functions<urd::function<"clear", &builder::clear>> {}; } }; }
Note
A built form of the library’s own. A field whose form in a builder is not its own type (a packed form, a list a builder keeps sorted) gets that form by a specialization
urd::built_form<M, C>beside the type, so that the builder’s field is still writtenbuilt_t. It givestype, the form, andassign(value, into), where the form can be made from the record’s member alone: written into the builder’s own value, in place, so that nothing is allocated once its buffers have grown. A form whose order needs the context takes a statefulurd::order_by, given to its Store asstd::settakes its comparison: a relation’s rows, kept sorted and unique by their values, which only the repository can read.Note
Values that are children by their position. A list field whose elements are children or plain values by their position (a row’s values: an object where its relation’s column is one, a number otherwise) says which by a specialization
urd::element_children<&data<T>::<field>>beside the record:is_child(record, context, position), andchildandelement, which convert one to the other. urd builds, copies and interns such a field element by element, a child as any other child: a row copied into another repository has its objects copied with it.S3 decidable A record’s view has one
get_<field>()per field and may derive more from them, never readingrecord().- Name:
schema-record-view- Check:
python -m pybrokkr.rules S3(with the compile database of a configured build)
A record’s views (
urd::index_view, and the borrowed, owning and shared views of itsdata<T>orbuilder<T, C>) take their accessors from the specializationurd::view_mixin<T, V>of its tag. Each mixin has, in this order:a private
self(), the view:static_cast<const V&>(*this);for each field of
fields(), in order,get_<field>(), which returnsself().template member<&urd::record_of<V>::<field>>(): the field resolved through the view’s context;any further member derived from the fields (whether it is ground, a filtered range), built on the getters above;
functions(), which lists every member beyondself()and the field getters, and the free functions offered with the view, each under its Python name with anurd::argper further parameter (urd::function<"value", &view_mixin::value, urd::arg<"column">>). A member template whose template arguments its callers write (get<FluentTag>()) is C++’s alone and not listed.
A mixin declares member functions only (no data member, alias, nested type or using-declaration), has no base but its tag’s mixin (a held form’s mixin may inherit it), and never calls
record(), which would bypass the resolutionmember<>does and tie the view to how the record is laid out. A function derives something new: none only forwards a path of getters its callers could read themselves (get_name()asget_symbol().get_name()).A family of tags has a mixin per record specialization, or one generic mixin over its own parameters that serves several (
view_mixin<Atom<T, F>, V>serves the lifted and the ground atoms). The lifted atoms’ mixin, with a derived function:// include/lib/formalism/atom_view.hpp namespace urd { template<lib::formalism::FactKind F, typename V> struct view_mixin<lib::formalism::Atom<lib::LiftedTag, F>, V> { private: const V& self() const noexcept { return static_cast<const V&>(*this); } public: auto get_predicate() const noexcept { return self().template member<&urd::record_of<V>::predicate>(); } auto get_terms() const noexcept { return self().template member<&urd::record_of<V>::terms>(); } // Derived: whether none of its terms is a variable. bool is_ground() const noexcept { return std::ranges::none_of(get_terms(), [](auto term) { return term.is_variable(); }); } static constexpr auto functions() noexcept { return urd::Functions<urd::function<"is_ground", &view_mixin::is_ground>> {}; } }; }
is_groundmay as well be a free function over the view, declared before the mixin and listed the same way (&lib::formalism::is_ground<V>); Python calls it as the same method,atom.is_ground().Note
A key of the library’s own. A field whose type no context resolves as an
urd::index(a label, a packed handle) tells urd how a view reads it by a specializationurd::resolution<M>beside the type:of, the field as its getter returns it, andstored, the field written from what reads as it. A label that names nothing reads as itself.S4 decidable A record is bound in Python by
urd::python::bind_index<T>(m)where it holds its index,urd::python::bind_data<T>(m),urd::python::bind_builder<T, C>(m)for its builder andurd::python::bind_view<View>(m)for each of its views, one class each, and by nothing else.- Name:
schema-record-binding- Check:
python -m pybrokkr.rules S4(with the compile database of a configured build)
urd binds a record’s fields, its index, comparison, hashing,
strandrepr, and its views’ field getters andfunctions()(S3). The classes are named fromurd::name<T>(), the tag’sstatic constexpr std::string_view name, so that no binding names a record or a field by hand:Class
Members
<Name>Indexbind_index,urd::index<T>:value,is_none(),int(), comparison and hashing; bound once, beforebind_data, by whichever module binds it first<Name>Databind_data,urd::data<T>: an__init__from the fields in order, each field read and written under its name, comparison by the identifying members,strandrepr<Name>bind_view, the view: eachget_<field>(), comparison and hashing by its identity,strandrepr,context(),index(), and the entries offunctions(). A view that borrows, owns or shares its record is<Name>Borrowed,<Name>Owned,<Name>Shared; a builder’s view hasBuilderbefore these (AtomBuilderBorrowed)<Name>Builderbind_builder,urd::builder<T, C>: an__init__that takes each field by its name, every one optional,set_<field>(value)for each field, the entries of itsfunctions()(S2), andview(context). A repository interns a builder with its children (Repository.insert(builder))What a view offers in Python is decided where the view is, by
fields()andfunctions(), never by a.defin a binding. A function that takes more than the view (options, a context) is an algorithm over it, bound withm.def.// python/src/pylib/formalism/atom.cpp urd::python::bind_index<lib::formalism::Atom>(m); urd::python::bind_data<lib::formalism::Atom>(m); urd::python::bind_builder<lib::formalism::Atom, lib::Repository>(m); // set_predicate, set_terms, clear, view urd::python::bind_view<lib::formalism::AtomView>(m); // get_predicate, get_terms and functions(): is_ground
Note
Names of a family. A tag of a family is named after its arguments where the records of one module need names of their own (
urd::joined_name<"Atom", Tag::name>), and not where each is bound in a module of its own.
Values¶
A value is a record no Store holds: urd::data<T> with no index, compared, hashed, written and bound as a record is,
by S1 to S4. A value that holds a child (an index of a record) is built by its builder and read through its views, as a
record. A value with neither an index nor a child (two numbers, a label: urd::PlainValue) is held, built and read as
it is, with no builder or view of its own.
S5 decidable A type that declares
fields()andidentifying_members()is aurd::data<T>specialization, or a key.- Name:
schema-value- Check:
python -m pybrokkr.rules S5(with the compile database of a configured build)
Every type with a static
fields()andidentifying_members()is a record (S1 to S4) or a key of the library’s own (oneurd::resolutionreads, S3). A type withfields()alone is printable, not a value. A class with behaviour (private state, member functions) declares nofields()and is written by afmt::formatterof its own. A plain value, the bounds of an interval:// include/lib/formalism/declarations.hpp namespace lib::formalism { struct Bounds { static constexpr std::string_view name = "Bounds"; }; } template<> struct urd::data<lib::formalism::Bounds> { std::int64_t lower; std::int64_t upper; data() = default; data(std::int64_t lower_, std::int64_t upper_) : lower(lower_), upper(upper_) {} static constexpr bool copyable() noexcept { static_assert(std::is_trivially_copyable_v<data>, "a copyable record is trivially copyable"); return true; } static constexpr auto fields() noexcept { return urd::Fields<urd::field<"lower", &data::lower>, urd::field<"upper", &data::upper>> {}; } static constexpr auto identifying_members() noexcept { return urd::members_of(fields()); } static constexpr auto cista_members() noexcept { return urd::Members<&data::lower, &data::upper> {}; } }; // python/src/pylib/formalism/bounds.cpp urd::python::bind_data<lib::formalism::Bounds>(m); // BoundsData
Status¶
python -m pybrokkr.rules S checks the five rules on every tracked file, tests included; what it reports is where a
library does not follow them yet. A file it cannot parse is a finding, not a skip.