C API

Input EDB guide

Use the C runtime edb API: ownership, explanations and complete examples.

The generated Runtime EDB reference contains the complete declarations and individual API cards. This guide keeps the usage explanations and examples.

The stable facade accepts runtime facts either as one borrowed array passed to session_solve(), or in an opaque, reusable input_edb_t buffer passed to session_solve_edb(). These are two input paths for a complete request snapshot, not incremental updates to the previous solve. See the EDB concept for the distinction between request facts and policy facts.

Types

Defined in the reference

TypePurpose
input_edb_tOpaque, reusable request-fact buffer.
fact_tOne predicate and its typed terms, used in a batch.

Used from other pages

These types are defined on the linked pages; this page uses them in the roles below.

TypeUse on this pageDefined in
diagnostic_tOptional output argument receiving insertion details.Errors

Functions

Start with the common operations. Capacity and memory management are useful when the application needs explicit limits or supplies its own storage; inspection functions observe an existing buffer. All are declared in <maelys/datalog.h>.

Common operations

FunctionPurpose
input_edb_create()Allocate a default-capacity EDB.
input_edb_add_fact()Append one typed fact.
input_edb_add_facts()Append one atomic batch.
input_edb_clear()Start a fresh request in the same buffer.
input_edb_free()End the handle lifetime.

Capacity and memory management

FunctionPurpose
input_edb_create_with_capacity()Allocate an EDB with explicit, fixed fact and text capacities.
input_edb_storage_requirements() / input_edb_init()Place the EDB handle in caller-owned aligned memory.

Inspection functions

FunctionPurpose
input_edb_count()Count stored raw entries.
input_edb_text_usage()Inspect committed string-storage occupancy.
input_edb_view()Borrow committed raw facts until mutation.

Function-like macro builders

These C11 conveniences are distinct from the public EDB functions below. A macro with () accepts arguments; it is not an exported function.

Function-like macroPurpose
MAELYS_DATALOG_SYMBOL(value)Marks a borrowed C string as a symbol-valued term.
MAELYS_DATALOG_BOOL(value)Requests boolean rather than integer conversion for a C expression.
MAELYS_DATALOG_FACT(...)Builds one borrowed checked descriptor for an atomic batch.
MAELYS_DATALOG_ADD_FACT(edb, diagnostic, ...)Adds one typed fact through the input EDB API.
MAELYS_DATALOG_ADD_FACTS(edb, diagnostic, ...)Adds one batch through the input EDB API.

Input EDB handle

See the input_edb_t reference.

Input fact type

See the fact_t reference.

Common use

The quickstart follows this lifecycle: create an input buffer, add request facts, solve, then release the buffer. For later requests, clear and refill the same buffer. The default buffer has fixed capacities and never grows automatically.

Create an input buffer

See the input_edb_create() reference.

Add input facts

Add one fact through the typed call below, or submit a whole request batch when partial insertion would be unsafe. Successful insertion copies predicate and symbol strings, so the caller can release its input strings afterward. Domain membership is checked at solve time.

See the input_edb_add_fact() reference.

See the input_edb_add_facts() reference.

CODE
maelys_datalog_status_t status = maelys_datalog_input_edb_add_facts(
    edb, facts, fact_count, &diagnostic);

Without the macros: typed arrays

Use the typed API for C++, FFI, large batches or an array populated dynamically. Assume edb is initialized, diagnostic is initialized and rc is a status. The examples below are two alternative ways to add input:

CODE
const maelys_datalog_value_t terms[] = {
    {.kind = MAELYS_DATALOG_VALUE_SYMBOL, .as.symbol = "alice"},
    {.kind = MAELYS_DATALOG_VALUE_SYMBOL, .as.symbol = "roadmap.pdf"},
};
rc = maelys_datalog_input_edb_add_fact(
    edb, "owns", terms, 2u, &diagnostic);
if (rc != MAELYS_DATALOG_STATUS_OK) goto cleanup;
CODE
const maelys_datalog_fact_t facts[] = {
    {.predicate = "user", .arity = 1u, .terms = {
        {.kind = MAELYS_DATALOG_VALUE_SYMBOL, .as.symbol = "alice"},
    }},
    {.predicate = "owns", .arity = 2u, .terms = {
        {.kind = MAELYS_DATALOG_VALUE_SYMBOL, .as.symbol = "alice"},
        {.kind = MAELYS_DATALOG_VALUE_SYMBOL, .as.symbol = "roadmap.pdf"},
    }},
};
rc = maelys_datalog_input_edb_add_facts(
    edb, facts, sizeof(facts) / sizeof(facts[0]), &diagnostic);
if (rc != MAELYS_DATALOG_STATUS_OK) goto cleanup;

Both paths use the same native validation and copying rules. The typed batch function accepts a zero-length batch; the variadic macro requires at least one FACT. A FACT descriptor is not a fact_t initializer. These helpers only build input: call session_solve_edb() after successful insertion to evaluate the request.

Reuse and release

input_edb_clear() starts another request in the same buffer; it is not a secure erase. At the end of the handle's lifetime, call input_edb_free().

See the input_edb_clear() reference.

See the input_edb_free() reference.

Example — document access

For the policy from the quickstart, build the same seven-fact request: three users, two ownership facts, one delegation and one blocked user. The domain declares blocked/1 as EDB, so the last fact is request data, not policy source. Both tabs submit one atomic batch to an already initialized EDB.

CODEDocument-access request facts

request-facts.c

#include <maelys/datalog.h>

maelys_datalog_status_t add_document_request(
    maelys_datalog_input_edb_t *edb, maelys_datalog_diagnostic_t *diagnostic) {
    const maelys_datalog_fact_t facts[] = {
        {.predicate = "user", .arity = 1u, .terms = {{.kind = MAELYS_DATALOG_VALUE_SYMBOL, .as.symbol = "alice"}}},
        {.predicate = "user", .arity = 1u, .terms = {{.kind = MAELYS_DATALOG_VALUE_SYMBOL, .as.symbol = "bob"}}},
        {.predicate = "user", .arity = 1u, .terms = {{.kind = MAELYS_DATALOG_VALUE_SYMBOL, .as.symbol = "mallory"}}},
        {.predicate = "owns", .arity = 2u, .terms = {
            {.kind = MAELYS_DATALOG_VALUE_SYMBOL, .as.symbol = "alice"},
            {.kind = MAELYS_DATALOG_VALUE_SYMBOL, .as.symbol = "roadmap.pdf"}}},
        {.predicate = "delegated", .arity = 2u, .terms = {
            {.kind = MAELYS_DATALOG_VALUE_SYMBOL, .as.symbol = "bob"},
            {.kind = MAELYS_DATALOG_VALUE_SYMBOL, .as.symbol = "roadmap.pdf"}}},
        {.predicate = "owns", .arity = 2u, .terms = {
            {.kind = MAELYS_DATALOG_VALUE_SYMBOL, .as.symbol = "mallory"},
            {.kind = MAELYS_DATALOG_VALUE_SYMBOL, .as.symbol = "roadmap.pdf"}}},
        {.predicate = "blocked", .arity = 1u, .terms = {{.kind = MAELYS_DATALOG_VALUE_SYMBOL, .as.symbol = "mallory"}}},
    };
    return maelys_datalog_input_edb_add_facts(
        edb, facts, sizeof(facts) / sizeof(facts[0]), diagnostic);
}

The batch is atomic: none of its seven facts is inserted if validation fails. A later solve reads exactly this input snapshot; it does not inherit facts from a previous request.

Capacity and memory

Use these alternatives to default creation when the application needs explicit fact and text limits or must provide the buffer's memory itself.

Explicit capacities

create_with_capacity() allocates a buffer with the requested fixed limits. Like the default buffer, it never grows automatically.

See the input_edb_create_with_capacity() reference.

Caller-owned storage

Call storage_requirements() to obtain the required size and alignment, then pass suitably aligned memory to init().

See the input_edb_storage_requirements() reference.

See the input_edb_init() reference.

The native insertion calls do not allocate or grow this buffer. Text lookup and rollback storage are reserved at initialization; small configured buffers may use linear lookup, while larger ones use a preallocated index. The index is an implementation detail, not a worst-case constant-time guarantee or a public symbol-ID input API.

Both creation paths use the common insertion and solve operations. Finish every initialized handle with input_edb_free(); this call does not free caller-owned storage, which remains the application's responsibility.

Inspection

These accessors inspect one already populated input buffer without solving. They are useful for admission diagnostics and for native adapters such as event windows. They do not change the EDB or allocate memory.

Count the stored entries before inspecting their text occupancy or content. input_edb_count() includes duplicates; it does not count deduplicated facts.

See the input_edb_count() reference.

When an input approaches its fixed text budget, inspect only the bytes already committed to this EDB; this is not a prediction of solve memory.

See the input_edb_text_usage() reference.

Use the view to audit the order and content accepted into the buffer before solve. It is borrowed, not a copy.

See the input_edb_view() reference.

Do not modify the borrowed array or its strings. Do not pass the view back into a mutating operation on the same buffer; copying to a different buffer is allowed. clear() resets text usage to zero, while a rejected append preserves the reported usage. Free text alone does not guarantee that the next solve fits every other engine limit.

Macro builder reference

See the SYMBOL() reference.

See the BOOL() reference.

See the FACT() reference.

See the ADD_FACT() reference.

See the ADD_FACTS() reference.

Using the C11 builders

The optional macros are declared in <maelys/datalog_builders.h>, also included by <maelys/datalog.h>. They are source conveniences, not a new ABI.

Assume edb is a successfully created or caller-initialized input buffer, diagnostic is a diagnostic_t, and rc is a status. One fact can be added directly:

CODE
rc = MAELYS_DATALOG_ADD_FACT(
    edb, &diagnostic, "owns", "alice", "roadmap.pdf");
if (rc != MAELYS_DATALOG_STATUS_OK) goto cleanup;

For a small batch written in the program, use one call:

CODE
rc = MAELYS_DATALOG_ADD_FACTS(
    edb, &diagnostic,
    MAELYS_DATALOG_FACT("user", "alice"),
    MAELYS_DATALOG_FACT("owns", "alice", "roadmap.pdf"),
    MAELYS_DATALOG_FACT("blocked", "mallory")
);
if (rc != MAELYS_DATALOG_STATUS_OK) goto cleanup;
OperationWhat it doesOn failure
FACT(predicate, ...)Constructs a checked descriptor; inserts nothing.Range errors are retained for the enclosing batch call. Unsupported types fail compilation.
ADD_FACT(edb, diagnostic, predicate, ...)Appends one fact.That fact is not added; earlier successful calls remain.
ADD_FACTS(edb, diagnostic, FACT(...), ...)Counts the facts and calls the native batch function once.No fact from this batch is added; existing facts and storage remain unchanged.

The table abbreviates the common MAELYS_DATALOG_ prefix. These macros support C11, zero to four terms per fact, and at least one fact per batch. They accept the same values:

C valueDatalog term
char * or const char *Symbol, copied into the EDB on successful insertion.
Standard signed or unsigned integerInteger, checked against INT64_MIN..INT64_MAX.
_Bool or MAELYS_DATALOG_BOOL(expression)Boolean. Plain C11 true and comparisons are int: wrap them to request boolean semantics.

For example, MAELYS_DATALOG_FACT("enabled", "alice", MAELYS_DATALOG_BOOL(1)) builds a symbol and a boolean; MAELYS_DATALOG_FACT("quota", "alice", 1024) builds a symbol and an integer. Their predicates still need to be declared in the domain before solving. Floats, arbitrary pointers and structs are rejected at compilation.

Each argument is evaluated once, but evaluation order is unspecified: do not put mutually dependent side effects in different arguments. FACT borrows strings until submission; ADD_FACTS() copies them into native storage before returning successfully. It uses automatic temporary arrays proportional to the written batch, no extra malloc, and no heap fallback. This says nothing about an allocating EDB constructor or Python allocations.

Range failures and native shape/storage failures are atomic. Diagnostic fact and term indices are zero-based within the submitted batch. Domain membership, declared arities and policy-specific limits are checked at solve time, not by these macros.