C API

Input EDB

Build complete request fact sets with the stable opaque input buffer.

The examples use edb, an empty input fact buffer; diagnostic, a diagnostic pointer; and facts, the Alice user and document-ownership facts. The page reference context defines these values and the helpers check_status, add_user, fact_count and allocate_storage.

Declarations from <maelys/datalog.h> and its optional C11 builders <maelys/datalog_builders.h>. For explanations and complete examples, see the Runtime EDB guide.

Handle: input_edb_t

C · handleSince v0.4.0
input_edb_t

#include <maelys/datalog.h>

typedef struct maelys_datalog_input_edb maelys_datalog_input_edb_t;
Opaque, reusable buffer for the complete input facts of one request. Create or initialize it, add facts, solve, then clear it before another request.

Creation

input_edb_create()

Create a default input EDB.

C · functionSince v0.4.0
input_edb_create()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_input_edb_create(
maelys_datalog_input_edb_t **out_edb
);
Creates an empty input buffer using the installed profile’s default capacities. The application fills it with facts before solving a request.
Outputs

Written on success.

Receives the new opaque input handle.
Return status

maelys_datalog_status_t — OK on success.

Release the new buffer with input_edb_free.

Example
maelys_datalog_input_edb_t *request = NULL;
maelys_datalog_status_t status = maelys_datalog_input_edb_create(&request);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("facts: %zu\n", fact_count(request));
check_status(maelys_datalog_input_edb_free(request));
request = NULL;

input_edb_create_with_capacity()

Create a bounded input EDB.

C · functionSince v0.4.0
input_edb_create_with_capacity()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_input_edb_create_with_capacity(
size_t fact_capacity,
size_t text_capacity,
maelys_datalog_input_edb_t **out_edb
);
Creates an empty input buffer with explicit fact and text capacities, fixed for its lifetime.
Arguments
fact_capacitysize_tin
Maximum stored raw fact entries.
text_capacitysize_tin
Maximum interned predicate and symbol bytes.
Outputs

Written on success.

Receives the new opaque input handle.
Return status

maelys_datalog_status_t — OK on success.

The requested capacities stay fixed; the buffer never grows.

Example
const size_t requested_facts = 2u, requested_text = 128u;
maelys_datalog_input_edb_t *request = NULL;
maelys_datalog_status_t status = maelys_datalog_input_edb_create_with_capacity(
    requested_facts, requested_text, &request);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("requested fact capacity: %zu\n", requested_facts);
printf("requested text capacity: %zu\n", requested_text);
check_status(maelys_datalog_input_edb_free(request));
request = NULL;
status: ok
requested fact capacity: 2
requested text capacity: 128

input_edb_storage_requirements()

Measure caller storage.

C · functionSince v0.4.0
input_edb_storage_requirements()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_input_edb_storage_requirements(
size_t fact_capacity,
size_t text_capacity,
size_t *out_bytes,
size_t *out_alignment
);
Measures the size and alignment needed to initialize an input buffer in application-provided memory. It does not allocate or initialize that memory.
Arguments
fact_capacitysize_tin
Maximum stored raw fact entries.
text_capacitysize_tin
Maximum interned predicate and symbol bytes.
Outputs

Written on success.

out_bytessize_t *out
Receives required caller-storage bytes.
out_alignmentsize_t *out
Receives required storage alignment.
Return status

maelys_datalog_status_t — OK on success.

The output bytes and alignment describe the requested capacities; no EDB is created.

Example
size_t bytes = 0, alignment = 0;
maelys_datalog_status_t status = maelys_datalog_input_edb_storage_requirements(
    2u, 128u, &bytes, &alignment);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("storage required: %s\n", bytes > 0 ? "yes" : "no");
printf("alignment is a power of two: %s\n",
    alignment != 0 && (alignment & (alignment - 1u)) == 0 ? "yes" : "no");
status: ok
storage required: yes
alignment is a power of two: yes

input_edb_init()

Initialize in caller storage.

C · functionSince v0.4.0
input_edb_init()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_input_edb_init(
void *storage,
size_t storage_bytes,
size_t fact_capacity,
size_t text_capacity,
maelys_datalog_input_edb_t **out_edb
);
Initializes an empty input buffer inside memory supplied by the application, using the requested fact and text capacities.
Arguments
storagevoid *in/out
Caller-owned aligned memory for the handle.
storage_bytessize_tin
Available bytes in caller storage.
fact_capacitysize_tin
Maximum stored raw fact entries.
text_capacitysize_tin
Maximum interned predicate and symbol bytes.
Outputs

Written on success.

Receives the new opaque input handle.
Return status

maelys_datalog_status_t — OK on success.

The handle in out_edb borrows the supplied aligned memory.

Example
const size_t requested_facts = 2u, requested_text = 128u;
size_t bytes = 0, alignment = 0;
check_status(maelys_datalog_input_edb_storage_requirements(
    requested_facts, requested_text, &bytes, &alignment));
void *storage = allocate_storage(bytes, alignment);
maelys_datalog_input_edb_t *request = NULL;
maelys_datalog_status_t status = maelys_datalog_input_edb_init(
    storage, bytes, requested_facts, requested_text, &request);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
check_status(maelys_datalog_input_edb_free(request)); request = NULL;
free(storage);

Functions

input_edb_add_fact()

Add one input fact.

C · functionSince v0.4.0
input_edb_add_fact()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_input_edb_add_fact(
maelys_datalog_input_edb_t *edb,
const char *predicate,
const maelys_datalog_value_t *terms,
size_t arity,
maelys_datalog_diagnostic_t *out_diagnostic
);
Appends one typed fact to the input buffer and copies its text values into the buffer’s storage. This call does not evaluate the policy.
Arguments
edbmaelys_datalog_input_edb_t *handlemodified
Input buffer receiving the fact.
predicateconst char *in
Predicate name of the input fact.
termsconst maelys_datalog_value_t *in
Array of typed ground values.
aritysize_tin
Number of terms in the fact.

out_diagnostic: optional diagnostic details on failure; pass NULL when not needed.

Return status

maelys_datalog_status_t — OK on success.

On failure that fact is not inserted.

Example
const maelys_datalog_value_t terms[] = { MAELYS_DATALOG_SYMBOL("alice") };
maelys_datalog_status_t status = maelys_datalog_input_edb_add_fact(
    edb, "user", terms, 1u, diagnostic);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("facts: %zu\n", fact_count(edb));

input_edb_add_facts()

Add a batch of input facts.

C · functionSince v0.4.0
input_edb_add_facts()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_input_edb_add_facts(
maelys_datalog_input_edb_t *edb,
const maelys_datalog_fact_t *facts,
size_t fact_count,
maelys_datalog_diagnostic_t *out_diagnostic
);
Appends a complete batch of typed facts in one operation. The batch is atomic: if an entry cannot be stored, none of this batch is added.
Arguments
edbmaelys_datalog_input_edb_t *handlemodified
Input buffer receiving the batch.
factsconst maelys_datalog_fact_t *in
Array of typed facts to append together; predicate names, arities and term types may differ.
fact_countsize_tin
Number of facts in the array.

out_diagnostic: optional diagnostic details on failure; pass NULL when not needed.

Return status

maelys_datalog_status_t — OK on success.

On rejection, the previous facts, interned text and string index remain unchanged.

Example
maelys_datalog_status_t status = maelys_datalog_input_edb_add_facts(
    edb, facts, sizeof(facts) / sizeof(facts[0]), diagnostic);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("facts: %zu\n", fact_count(edb));

input_edb_count()

Count input entries.

C · functionSince v0.4.0
input_edb_count()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_input_edb_count(
const maelys_datalog_input_edb_t *edb,
size_t *out_count
);
Reads the number of entries currently stored in the input buffer, before any solving or result enumeration.
Arguments
edbconst maelys_datalog_input_edb_t *handleread
Input buffer whose entries are counted.
Outputs

Written on success.

out_countsize_t *out
Receives the number of stored raw entries.
Return status

maelys_datalog_status_t — OK on success.

The output count includes duplicate raw entries.

Example
add_user(edb, diagnostic);
size_t count = 0;
maelys_datalog_status_t status = maelys_datalog_input_edb_count(edb, &count);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("facts: %zu\n", count);

input_edb_text_usage()

Observe interned text occupancy.

C · functionSince v0.8.0
input_edb_text_usage()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_input_edb_text_usage(
const maelys_datalog_input_edb_t *edb,
size_t *out_used,
size_t *out_capacity
);
Reads how much of the input buffer’s text storage is occupied by interned strings.
Arguments
edbconst maelys_datalog_input_edb_t *handleread
Input buffer whose text storage is measured.
Outputs

Written on success.

out_usedsize_t *out
Interned predicate and symbol bytes used, including terminating NULs.
out_capacitysize_t *out
Configured text capacity of this buffer.
Return status

maelys_datalog_status_t — OK on success.

Both outputs stay unchanged on error. This measures committed input text only, not engine or candidate scratch memory.

Example
add_user(edb, diagnostic);
size_t used = 0, capacity = 0;
maelys_datalog_status_t status = maelys_datalog_input_edb_text_usage(edb, &used, &capacity);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("text bytes used: %zu\n", used);
printf("text capacity: %zu\n", capacity);
status: ok
text bytes used: 11
text capacity: 40960

input_edb_view()

Borrow ordered raw input facts.

C · functionSince v0.8.0
input_edb_view()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_input_edb_view(
const maelys_datalog_input_edb_t *edb,
const maelys_datalog_fact_t **out_facts,
size_t *out_count
);
Borrows a view of the stored input facts in insertion order. The view refers to the input buffer rather than copying its contents.
Arguments
edbconst maelys_datalog_input_edb_t *handleread
Input buffer lending the fact view.
Outputs

Written on success.

out_factsconst maelys_datalog_fact_t **out
Borrowed array of raw entries, before native deduplication.
out_countsize_t *out
Number of entries in the borrowed view.
Return status

maelys_datalog_status_t — OK on success.

Outputs stay unchanged on error. The view lasts until the next successful mutation or free of this EDB.

Example
add_user(edb, diagnostic);
const maelys_datalog_fact_t *view = NULL;
size_t count = 0;
maelys_datalog_status_t status = maelys_datalog_input_edb_view(edb, &view, &count);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("facts: %zu\n", count);
if (count != 0u) printf("predicate: %s\n", view[0].predicate);
status: ok
facts: 1
predicate: user

input_edb_clear()

Clear input entries.

C · functionSince v0.4.0
input_edb_clear()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_input_edb_clear(
maelys_datalog_input_edb_t *edb
);
Removes the input entries so the same buffer can hold another request. Clearing input does not release a live result or reset its session.
Arguments
edbmaelys_datalog_input_edb_t *handlemodified
Input buffer to empty.
Return status

maelys_datalog_status_t — OK on success.

Clearing resets the buffer for another complete request; this is not secure erasure.

Example
add_user(edb, diagnostic);
printf("facts before: %zu\n", fact_count(edb));
maelys_datalog_status_t status = maelys_datalog_input_edb_clear(edb);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("facts after: %zu\n", fact_count(edb));
facts before: 1
status: ok
facts after: 0

Release

input_edb_free()

Release an input EDB.

C · functionSince v0.4.0
input_edb_free()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_input_edb_free(
maelys_datalog_input_edb_t *edb
);
Releases the input buffer. Results already produced from it remain independent of that input storage.
Arguments
edbmaelys_datalog_input_edb_t *handlereleased
Input buffer to release.
Return status

maelys_datalog_status_t — OK on success.

Caller-owned storage supplied to init remains the caller's responsibility.

Example
maelys_datalog_status_t status = maelys_datalog_input_edb_free(edb);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
edb = NULL;

Macros

ADD_FACT()

C · macroSince v0.4.0
ADD_FACT()

#include <maelys/datalog_builders.h>

MAELYS_DATALOG_ADD_FACT(
edb,
diagnostic,
...
)

Adds one typed fact through the input EDB API.

Arguments
edbmaelys_datalog_input_edb_t *
Initialized input EDB handle.
diagnosticmaelys_datalog_diagnostic_t *
Initialized diagnostic pointer, or NULL when diagnostics are not needed.
...const char * predicate, then 0–4 supported C term expressions
Predicate name followed by supported typed terms.
Effect

Converts and appends one fact. Separate successful calls remain inserted if a later call fails. Returns a public status code.

Example
maelys_datalog_status_t status = MAELYS_DATALOG_ADD_FACT(
    edb, diagnostic, "owns", "alice", "roadmap.pdf");
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);

ADD_FACTS()

C · macroSince v0.4.0
ADD_FACTS()

#include <maelys/datalog_builders.h>

MAELYS_DATALOG_ADD_FACTS(
edb,
diagnostic,
...
)

Adds one batch through the input EDB API.

Arguments
edbmaelys_datalog_input_edb_t *
Initialized input EDB handle.
diagnosticmaelys_datalog_diagnostic_t *
Initialized diagnostic pointer, or NULL when diagnostics are not needed.
...one or more MAELYS_DATALOG_FACT(...) descriptors
A nonempty comma-separated batch of fact descriptors.
Effect

Converts and submits the descriptors in one atomic batch: failure inserts none of its facts. Returns a public status code.

Example
maelys_datalog_status_t status = MAELYS_DATALOG_ADD_FACTS(
    edb, diagnostic,
    MAELYS_DATALOG_FACT("user", "alice"),
    MAELYS_DATALOG_FACT("owns", "alice", "roadmap.pdf"));
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);

Struct: fact_t

C · structSince v0.10.0
fact_t

#include <maelys/datalog.h>

One input fact: a predicate name, its arity and up to four typed terms.
Fields
predicateconst char *
Registered predicate that receives the fact.
aritysize_t
Number of populated terms; must match the predicate declaration.
Typed values copied into the input buffer when the fact is added.
Typedef
typedef struct {
    const char *predicate;
    size_t arity;
    maelys_datalog_value_t terms[MAELYS_DATALOG_PUBLIC_MAX_TERMS];
} maelys_datalog_fact_t;

Creation

FACT()

C · macroSince v0.4.0
FACT()

#include <maelys/datalog_builders.h>

MAELYS_DATALOG_FACT(
...
)

Builds one borrowed checked descriptor for an atomic batch.

Arguments
...const char * predicate, then 0–4 supported C term expressions
Predicate name followed by strings, supported integer expressions, or explicit boolean expressions.
Effect

Builds one borrowed, checked fact descriptor for a later atomic batch. It inserts no fact. Produces a temporary fact descriptor; it inserts nothing until ADD_FACTS is called.

Example
maelys_datalog_fact_t fact = MAELYS_DATALOG_FACT("owns", "alice", "roadmap.pdf").fact;
printf("predicate: %s\n", fact.predicate);
printf("arity: %zu\n", fact.arity);
predicate: owns
arity: 2

Enum: value_kind_t

C · enumSince ≤ v0.1.0-alpha.2
value_kind_t

#include <maelys/datalog.h>

Tag selecting the symbol, integer or boolean member of a typed input or result term.
Members
VALUE_SYMBOL1
Selects the symbol member: borrowed NUL-terminated text for input values.
VALUE_INTEGER2
Selects the integer member: a signed 64-bit integer.
VALUE_BOOLEAN3
Selects the boolean member: 0 for false, 1 for true.
Typedef
typedef enum {
    MAELYS_DATALOG_VALUE_SYMBOL = 1,
    MAELYS_DATALOG_VALUE_INTEGER = 2,
    MAELYS_DATALOG_VALUE_BOOLEAN = 3
} maelys_datalog_value_kind_t;

Struct: value_t

C · structSince v0.10.0
value_t

#include <maelys/datalog.h>

A typed ground value supplied in an input fact, query or explanation request. Symbol inputs carry text rather than a result-owned symbol identifier.
Fields
Selects the active member of as: SYMBOL, INTEGER or BOOLEAN.
asunion { const char *symbol; int64_t integer; int boolean; }
Tagged value payload. Use symbol for text, integer for a signed integer, or boolean for a boolean value, according to kind.
Typedef
typedef struct {
    maelys_datalog_value_kind_t kind;
    union {
        const char *symbol;
        int64_t integer;
        int boolean;
    } as;
} maelys_datalog_value_t;

Creation

BOOL()

C · macroSince v0.4.0
BOOL()

#include <maelys/datalog_builders.h>

MAELYS_DATALOG_BOOL(
value
)

Requests boolean rather than integer conversion for a C expression.

Arguments
valuescalar C expression convertible to _Bool
A value or expression to interpret as a boolean, rather than as an integer.
Effect

Casts the expression to _Bool so the C11 typed builders select BOOLEAN. Produces a _Bool expression. It inserts no fact.

Example
maelys_datalog_value_t value = {
    .kind = MAELYS_DATALOG_VALUE_BOOLEAN, .as.boolean = MAELYS_DATALOG_BOOL(1)
};
printf("boolean: %s\n", value.as.boolean ? "yes" : "no");

SYMBOL()

C · macroSince v0.4.0
SYMBOL()

#include <maelys/datalog_builders.h>

MAELYS_DATALOG_SYMBOL(
value
)

Marks a borrowed C string as a symbol-valued term.

Arguments
valueconst char *
Borrowed NUL-terminated symbol string, valid until the consuming call returns.
Effect

Builds a symbol-value initializer. It neither copies the string nor inserts a fact. Produces a maelys_datalog_value_t initializer; the consuming call validates and copies when appropriate.

Example
maelys_datalog_value_t user = MAELYS_DATALOG_SYMBOL("alice");
printf("symbol: %s\n", user.as.symbol);

Usage guides

Runtime EDB guide · Full program.