C API

Result

Query results, enumerate relations, and render explanations through the stable C facade.

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

The examples use result, the solved document-access result; terms describe Alice’s access query and absent_terms describe Mallory’s denied access query. The context supplies fact and count for enumeration and caller-owned storage for explanations. The symbol-text example obtains its symbol ID from an enumerated fact; each prepared explanation is created in the displayed fragment. The page reference context provides these values and the helpers prepare, check_status and print_text_lines.

Handle: result_t

C · handleSince ≤ v0.1.0-alpha.2
result_t

#include <maelys/datalog.h>

typedef struct maelys_datalog_result maelys_datalog_result_t;
Answer to one successful solve. Query and explain it while live, then release it before reusing its session.

Functions

result_query()

Query a ground fact.

C · functionSince ≤ v0.1.0-alpha.2
result_query()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_result_query(
const maelys_datalog_result_t *result,
const char *predicate,
const maelys_datalog_value_t *terms,
size_t arity,
int *out_present
);
Checks whether one fully specified fact is present in an existing result. It does not solve the policy again.
Arguments
resultconst maelys_datalog_result_t *handleread
Result to inspect.
predicateconst char *in
Name of a queryable predicate.
termsconst maelys_datalog_value_t *in
Fully specified values for the query.
aritysize_tin
Number of values in terms; must match the registered predicate.
Outputs

Written on success.

out_presentint *out
Receives 1 if the fact is derived or 0 if absent.
Return status

maelys_datalog_status_t — OK on success.

An absent fact is a successful negative answer; an API error is separate.

Example
const maelys_datalog_value_t terms[] = {
    MAELYS_DATALOG_SYMBOL("alice"),
    MAELYS_DATALOG_SYMBOL("roadmap.pdf"),
};
int present = 0;
maelys_datalog_status_t status = maelys_datalog_result_query(
    result, "allow", terms, 2u, &present);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("present: %d\n", present);

result_derived_fact_count()

Count derived facts.

C · functionSince v0.4.0
result_derived_fact_count()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_result_derived_fact_count(
const maelys_datalog_result_t *result,
size_t *out_count
);
Reads the number of facts derived during this solve, without enumerating them or deriving additional facts.
Arguments
resultconst maelys_datalog_result_t *handleread
Result to inspect.
Outputs

Written on success.

out_countsize_t *out
Receives the number of distinct derived IDB facts across all predicates, excluding runtime EDB and policy facts.
Return status

maelys_datalog_status_t — OK on success.

Returns OK and a total across IDB predicates, including those not queryable.

Example
size_t count = 0;
maelys_datalog_status_t status = maelys_datalog_result_derived_fact_count(result, &count);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("derived facts: %zu\n", count);
status: ok
derived facts: 1

result_enumerate()

Enumerate a query relation.

C · functionSince ≤ v0.1.0-alpha.2
result_enumerate()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_result_enumerate(
const maelys_datalog_result_t *result,
const char *predicate,
size_t arity,
maelys_datalog_fact_view_t *out_facts,
size_t capacity,
size_t *out_count
);
Lists the derived facts of a query-enabled predicate in an existing result, using the supplied output array.
Arguments
resultconst maelys_datalog_result_t *handleread
Result to inspect.
predicateconst char *in
Queryable predicate name.
aritysize_tin
Declared predicate arity.
capacitysize_tin
Number of fact-view entries available in the caller-owned out_facts array.
Outputs

Written on success.

Caller-owned array of fact views, bounded by capacity; NULL to measure the matching count.
out_countsize_t *out
Receives the number of matching facts for predicate at the declared arity.
Return status

maelys_datalog_status_t — OK on success.

Returns OK and the matching count. Supply caller storage to fetch views after measuring.

Example
maelys_datalog_fact_view_t facts[1];
size_t count = 0;
maelys_datalog_status_t status = maelys_datalog_result_enumerate(
    result, "allow", 2u, facts, 1u, &count);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("facts: %zu\n", count);
if (count != 0u) printf("arity: %zu\n", facts[0].arity);
status: ok
facts: 1
arity: 2

result_symbol_text()

Resolve a result symbol.

C · functionSince ≤ v0.1.0-alpha.2
result_symbol_text()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_result_symbol_text(
const maelys_datalog_result_t *result,
uint32_t symbol_id,
const char **out_text,
size_t *out_length
);
Looks up the string represented by a symbol ID belonging to this result. That ID is not a reusable symbol identifier for another result.
Arguments
resultconst maelys_datalog_result_t *handleread
Result to inspect.
symbol_iduint32_tin
Result-scoped identifier from an enumerated fact.
Outputs

Written on success.

out_textconst char **out
Receives result-owned symbol text for symbol_id, borrowed until the result is freed; do not free it.
out_lengthsize_t *out
Receives the byte length of the symbol text returned in out_text.
Return status

maelys_datalog_status_t — OK on success.

Returns borrowed text tied to this result; never free it independently.

Example
check_status(maelys_datalog_result_enumerate(
    result, "allow", 2u, &fact, 1u, &count));
uint32_t symbol_id = fact.terms[0].as.symbol_id;
const char *text = NULL;
size_t length = 0;
maelys_datalog_status_t status = maelys_datalog_result_symbol_text(
    result, symbol_id, &text, &length);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("symbol: %.*s\n", (int)length, text);
printf("bytes: %zu\n", length);
status: ok
symbol: alice
bytes: 5

result_filter_statistics()

C · functionSince v0.10.0
result_filter_statistics()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_result_filter_statistics(
const maelys_datalog_result_t *result,
maelys_datalog_filter_statistics_t *out_statistics
);
Reads the reference solver’s filter statistics for this result. These describe solving work, not a later Why-false explanation search.
Arguments
resultconst maelys_datalog_result_t *handleread
Live result from a successful solve.
Outputs

Written on success.

Receives the solve counters.
Return status

maelys_datalog_status_t — OK on success.

OK for a live reference-backend result; UNSUPPORTED for another backend.

Example
maelys_datalog_filter_statistics_t statistics = {0};
maelys_datalog_status_t status = maelys_datalog_result_filter_statistics(result, &statistics);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("evaluations: %zu\n", statistics.evaluations);
printf("matches: %zu\n", statistics.matches);
printf("non-matches: %zu\n", statistics.non_matches);
printf("cost units: %zu\n", statistics.cost_units);
status: ok
evaluations: 0
matches: 0
non-matches: 0
cost units: 0

result_explain_true_text()

Render Why-true text.

C · functionSince ≤ v0.1.0-alpha.2
result_explain_true_text()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_result_explain_true_text(
const maelys_datalog_result_t *result,
const char *predicate,
const maelys_datalog_value_t *terms,
size_t arity,
char *out_text,
size_t capacity,
size_t *out_required
);
Produces a bounded Why-true document describing a derivation of the requested fact in this result. It explains the existing answer rather than solving the policy again.
Arguments
resultconst maelys_datalog_result_t *handleread
Result to inspect.
predicateconst char *in
Queryable predicate name.
termsconst maelys_datalog_value_t *in
Fully specified typed query terms.
aritysize_tin
Declared predicate arity.
capacitysize_tin
Byte capacity of the caller-owned out_text buffer, including the terminating NUL; zero when measuring.
Outputs

Written on success.

out_textchar *out
Caller-owned buffer for Why-true text, bounded by capacity bytes. A short buffer returns PAYLOAD_TOO_LARGE without partial text.
out_requiredsize_t *out
Receives the required Why-true text length in bytes, excluding the terminating NUL, including when the buffer is too small.
Return status

maelys_datalog_status_t — OK on success.

Returns OK with the required byte count; an insufficient text buffer is separate from a truncated explanation.

Example
char text[16u * 1024u];
size_t required = 0;
maelys_datalog_status_t status = maelys_datalog_result_explain_true_text(
    result, "allow", terms, 2u, text, sizeof(text), &required);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("text bytes: %zu\n", required);
print_text_lines("explanation", text);
Show output (10 lines)
status: ok
text bytes: 304
explanation: MAELYS-DATALOG-v2
explanation: document=why-true
explanation: status=complete
explanation: steps=1 premises=2
explanation: step=0 rule=1 fact="allow"("alice","roadmap.pdf")
explanation: premise=0 body=0 kind=positive origin=edb fact="owns"("alice","roadmap.pdf") parent=-
explanation: premise=1 body=1 kind=negative-absence origin=edb fact="blocked"("alice") parent=-
explanation: result-step=0

result_explain_false_text()

Render Why-false text.

C · functionSince v0.1.0-alpha.4
result_explain_false_text()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_result_explain_false_text(
const maelys_datalog_result_t *result,
const char *predicate,
const maelys_datalog_value_t *terms,
size_t arity,
char *out_text,
size_t capacity,
size_t *out_required
);
Produces a bounded Why-false document describing obstacles encountered when investigating an absent fact. This is a diagnostic exploration, not an exhaustive proof of every possible obstruction.
Arguments
resultconst maelys_datalog_result_t *handleread
Result to inspect.
predicateconst char *in
Queryable predicate name.
termsconst maelys_datalog_value_t *in
Fully specified typed query terms.
aritysize_tin
Declared predicate arity.
capacitysize_tin
Byte capacity of the caller-owned out_text buffer, including the terminating NUL; zero when measuring.
Outputs

Written on success.

out_textchar *out
Caller-owned buffer for Why-false text, bounded by capacity bytes. A short buffer returns PAYLOAD_TOO_LARGE without partial text.
out_requiredsize_t *out
Receives the required Why-false text length in bytes, excluding the terminating NUL, including when the buffer is too small.
Return status

maelys_datalog_status_t — OK on success.

Returns OK with the required byte count; the document may describe a bounded, truncated exploration. Variables printed as ?N keep their rule-local IR identifier; use program_rule_variable_name() to read the source spelling. The identifiers and explanation formats are unchanged.

Example
char text[16u * 1024u];
size_t required = 0;
maelys_datalog_status_t status = maelys_datalog_result_explain_false_text(
    result, "allow", absent_terms, 2u, text, sizeof(text), &required);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("text bytes: %zu\n", required);
print_text_lines("explanation", text);
Show output (12 lines)
status: ok
text bytes: 405
explanation: MAELYS-DATALOG-v2
explanation: document=why-false
explanation: status=complete
explanation: query="allow"("mallory","roadmap.pdf") origin=none
explanation: summary=none
explanation: limit-hits=none candidate-rules=1 substitutions=0 diagnostics=1 filter-cost=0
explanation: diagnostic=0 rule=1 depth=0 target="allow"("mallory","roadmap.pdf")
explanation: binding=0 value="mallory"
explanation: binding=1 value="roadmap.pdf"
explanation: obstacle=positive-no-match body=0 origin=none pattern="owns"("mallory","roadmap.pdf")

result_explain_text_diagnosed()

C · functionSince v0.26.0
result_explain_text_diagnosed()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_result_explain_text_diagnosed(
const maelys_datalog_result_t *result,
maelys_datalog_explanation_kind_t kind,
const char *predicate,
const maelys_datalog_value_t *terms,
size_t arity,
char *out_text,
size_t capacity,
size_t *out_required,
maelys_datalog_diagnostic_t *diagnostic
);
Renders a Why-true or Why-false explanation into caller-owned text, with optional caller-owned failure diagnostics.
Arguments
resultconst maelys_datalog_result_t *handleread
Result to inspect.
kindmaelys_datalog_explanation_kind_tin
Why-true or Why-false explanation kind.
predicateconst char *in
Queryable predicate name.
termsconst maelys_datalog_value_t *in
Fully specified typed query terms.
aritysize_tin
Declared predicate arity.
capacitysize_tin
Byte capacity of the caller-owned out_text buffer, including the terminating NUL; zero when measuring.
diagnosticmaelys_datalog_diagnostic_t *in/out
Initialized caller-owned diagnostic record for failure details; NULL omits those details. The record must not overlap argument, engine, workspace and output storage.
Outputs

Written on success.

out_textchar *out
Caller-owned buffer for the Why-true or Why-false text, bounded by capacity bytes. A short buffer returns PAYLOAD_TOO_LARGE without partial text.
out_requiredsize_t *out
Receives the required explanation text length in bytes, excluding the terminating NUL, including when the buffer is too small.
Return status

maelys_datalog_status_t — OK on success.

Uses the buffer, allocation and cache contracts of result_explain_true_text and result_explain_false_text. An initialized diagnostic receives failure details and is cleared on success; NULL preserves the original behavior. PAYLOAD_TOO_LARGE is separate from a successfully returned truncated document. Variables printed as ?N keep their rule-local IR identifier; use program_rule_variable_name() to read the source spelling. The identifiers and explanation formats are unchanged.

Example
char text[16u * 1024u];
size_t required = 0;
maelys_datalog_diagnostic_t diagnostic = MAELYS_DATALOG_DIAGNOSTIC_INIT;
for (int kind = MAELYS_DATALOG_EXPLAIN_TRUE; kind <= MAELYS_DATALOG_EXPLAIN_FALSE; ++kind) {
    maelys_datalog_status_t status = maelys_datalog_result_explain_text_diagnosed(
        result, (maelys_datalog_explanation_kind_t)kind, "allow", terms, 2,
        text, sizeof(text), &required, &diagnostic);
    printf("status: %s\n", maelys_datalog_status_name(status));
    check_status(status);
    print_text_lines(kind == MAELYS_DATALOG_EXPLAIN_TRUE ? "why-true" : "why-false", text);
}
Show output (16 lines)
status: ok
why-true: MAELYS-DATALOG-v2
why-true: document=why-true
why-true: status=complete
why-true: steps=1 premises=2
why-true: step=0 rule=1 fact="allow"("alice","roadmap.pdf")
why-true: premise=0 body=0 kind=positive origin=edb fact="owns"("alice","roadmap.pdf") parent=-
why-true: premise=1 body=1 kind=negative-absence origin=edb fact="blocked"("alice") parent=-
why-true: result-step=0
status: ok
why-false: MAELYS-DATALOG-v2
why-false: document=why-false
why-false: status=not-applicable
why-false: query="allow"("alice","roadmap.pdf") origin=idb
why-false: summary=none
why-false: limit-hits=none candidate-rules=0 substitutions=0 diagnostics=0 filter-cost=0

result_prepare_explanation()

Prepare a retained explanation.

C · functionSince v0.4.0
result_prepare_explanation()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_result_prepare_explanation(
maelys_datalog_result_t *result,
maelys_datalog_explanation_kind_t kind,
const char *predicate,
const maelys_datalog_value_t *terms,
size_t arity,
void *storage,
size_t storage_bytes,
maelys_datalog_prepared_explanation_t **out_prepared
);
Prepares one Why-true or Why-false document in caller-owned storage and leases its result.
Arguments
resultmaelys_datalog_result_t *handlemodified
Result used to prepare the explanation.
Why-true or Why-false explanation kind.
predicateconst char *in
Queryable predicate name.
termsconst maelys_datalog_value_t *in
Fully specified typed query terms.
aritysize_tin
Number of query terms.
storagevoid *in/out
Exclusive, aligned caller-owned explanation workspace.
storage_bytessize_tin
Available bytes in caller-owned preparation storage.
Outputs

Written on success.

Receives the retained prepared-explanation handle.
Return status

maelys_datalog_status_t — OK on success.

Returns OK with a retained handle. Invalid alignment or insufficient storage fails without acquiring a result lease.

Example
maelys_datalog_prepared_explanation_t *explanation = NULL;
maelys_datalog_status_t status = maelys_datalog_result_prepare_explanation(
    result, MAELYS_DATALOG_EXPLAIN_TRUE, "allow", terms, 2u,
    storage, sizeof(storage), &explanation);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("prepared: %s\n", explanation != NULL ? "yes" : "no");
check_status(maelys_datalog_prepared_explanation_release(explanation));
explanation = NULL;
status: ok
prepared: yes

result_prepare_explanation_diagnosed()

C · functionSince v0.26.0
result_prepare_explanation_diagnosed()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_result_prepare_explanation_diagnosed(
maelys_datalog_result_t *result,
maelys_datalog_explanation_kind_t kind,
const char *predicate,
const maelys_datalog_value_t *terms,
size_t arity,
void *storage,
size_t storage_bytes,
maelys_datalog_prepared_explanation_t **out_prepared,
maelys_datalog_diagnostic_t *diagnostic
);
Prepares one Why-true or Why-false explanation in caller-owned storage, with optional caller-owned failure diagnostics.
Arguments
resultmaelys_datalog_result_t *handlemodified
Result used to prepare the explanation.
Why-true or Why-false explanation kind.
predicateconst char *in
Queryable predicate name.
termsconst maelys_datalog_value_t *in
Fully specified typed query terms.
aritysize_tin
Number of query terms.
storagevoid *in/out
Exclusive, aligned caller-owned explanation workspace.
storage_bytessize_tin
Available bytes in caller-owned preparation storage.
diagnosticmaelys_datalog_diagnostic_t *in/out
Initialized caller-owned diagnostic record for failure details; NULL omits those details. The record must not overlap argument, engine, workspace and output storage.
Outputs

Written on success.

Receives the retained prepared-explanation handle.
Return status

maelys_datalog_status_t — OK on success.

Uses the same status, storage and result-lease contract as result_prepare_explanation. An initialized diagnostic receives code, status, message, hint and predicate on failure and is cleared on success. A NULL diagnostic preserves the behavior of the original function; invalid diagnostic storage or version is rejected before use.

Example
maelys_datalog_diagnostic_t diagnostic = MAELYS_DATALOG_DIAGNOSTIC_INIT;
maelys_datalog_prepared_explanation_t *explanation = NULL;
maelys_datalog_status_t status = maelys_datalog_result_prepare_explanation_diagnosed(
    result, MAELYS_DATALOG_EXPLAIN_TRUE, "allow", terms, 2,
    storage, sizeof(storage), &explanation, &diagnostic);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("prepared: %s\n", explanation != NULL ? "yes" : "no");
printf("diagnostic.status: %s\n", maelys_datalog_status_name(diagnostic.status));
check_status(maelys_datalog_prepared_explanation_release(explanation));
explanation = NULL;
status: ok
prepared: yes
diagnostic.status: ok

result_explain_text_in()

Render once in caller storage.

C · functionSince v0.4.1
result_explain_text_in()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_result_explain_text_in(
maelys_datalog_result_t *result,
maelys_datalog_explanation_kind_t kind,
const char *predicate,
const maelys_datalog_value_t *terms,
size_t arity,
void *storage,
size_t storage_bytes,
char *out_text,
size_t capacity,
size_t *out_required
);
Prepares, sizes, writes and releases one explanation in caller-owned storage in a single call.
Arguments
resultmaelys_datalog_result_t *handlemodified
Result used to prepare the explanation.
Why-true or Why-false explanation kind.
predicateconst char *in
Queryable predicate name.
termsconst maelys_datalog_value_t *in
Fully specified typed query terms.
aritysize_tin
Number of query terms.
storagevoid *in/out
Exclusive, aligned caller-owned explanation workspace.
storage_bytessize_tin
Available bytes in caller-owned preparation storage.
capacitysize_tin
Available output-text bytes, including the terminating NUL.
Outputs

Written on success.

out_textchar *out
Caller-owned buffer for formatted explanation text, bounded by capacity bytes; an insufficient buffer returns PAYLOAD_TOO_LARGE.
out_requiredsize_t *out
Receives the formatted explanation length in bytes, excluding the terminating NUL.
Return status

maelys_datalog_status_t — OK on success.

Returns OK with a document or a public error; no prepared handle survives a failed write. A truncated document is distinct from PAYLOAD_TOO_LARGE. Variables printed as ?N keep their rule-local IR identifier; use program_rule_variable_name() to read the source spelling. The identifiers and explanation formats are unchanged.

Example
char text[16u * 1024u];
size_t required = 0;
maelys_datalog_status_t status = maelys_datalog_result_explain_text_in(
    result, MAELYS_DATALOG_EXPLAIN_TRUE, "allow", terms, 2u,
    storage, sizeof(storage), text, sizeof(text), &required);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
print_text_lines("explanation", text);
Show output (9 lines)
status: ok
explanation: MAELYS-DATALOG-v2
explanation: document=why-true
explanation: status=complete
explanation: steps=1 premises=2
explanation: step=0 rule=1 fact="allow"("alice","roadmap.pdf")
explanation: premise=0 body=0 kind=positive origin=edb fact="owns"("alice","roadmap.pdf") parent=-
explanation: premise=1 body=1 kind=negative-absence origin=edb fact="blocked"("alice") parent=-
explanation: result-step=0

Release

result_free()

Release a result.

C · functionSince ≤ v0.1.0-alpha.2
result_free()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_result_free(
maelys_datalog_result_t *result
);
Releases a result and its result-local data, allowing the session to solve another request once explanation leases have also been released.
Arguments
resultmaelys_datalog_result_t *handlereleased
Result to release.
Return status

maelys_datalog_status_t — OK on success.

Returns OK after ending the result lease; another solve may then reuse its session.

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

Macros

QUERY()

C · macroSince v0.4.0
QUERY()

#include <maelys/datalog_builders.h>

MAELYS_DATALOG_QUERY(
result,
present,
...
)

Queries a fully specified fact using C11 term conversion.

Arguments
resultconst maelys_datalog_result_t *
One live result returned by a successful solve.
presentint *
Receives 0 or 1 only after a successful query.
...const char * predicate, then 0–4 supported C term expressions
The ground fact to query; strings, integers and explicit booleans use C11 conversion.
Effect

Converts the supplied terms and calls the public ground-query function. It does not solve the policy again. The same public status and presence output as maelys_datalog_result_query().

Example
int present = 0;
maelys_datalog_status_t status = MAELYS_DATALOG_QUERY(
    result, &present, "allow", "alice", "roadmap.pdf");
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("present: %d\n", present);

Handle: prepared_explanation_t

C · handleSince v0.4.0
prepared_explanation_t

#include <maelys/datalog.h>

typedef struct maelys_datalog_prepared_explanation maelys_datalog_prepared_explanation_t;
An opaque explanation retained in caller-owned aligned storage. It leases its result until prepared_explanation_release() ends the lease; the workspace and result must remain alive throughout.

Creation

result_explanation_storage_requirements()

Measure result explanation storage.

C · functionSince v0.4.0
result_explanation_storage_requirements()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_result_explanation_storage_requirements(
const maelys_datalog_result_t *result,
maelys_datalog_explanation_kind_t kind,
size_t *out_bytes,
size_t *out_alignment
);
Reports the workspace bytes and alignment needed to prepare an explanation from this result.
Arguments
resultconst maelys_datalog_result_t *in
Result whose explanation workspace is measured.
Why-true or Why-false explanation kind.
Outputs

Written on success.

out_bytessize_t *out
Receives the required workspace size in bytes.
out_alignmentsize_t *out
Receives the required workspace alignment.
Return status

maelys_datalog_status_t — OK on success.

Returns OK with both requirements. Invalid kind or outputs fail without changing the outputs.

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

Functions

prepared_explanation_text_size()

Read prepared text length.

C · functionSince v0.4.0
prepared_explanation_text_size()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_prepared_explanation_text_size(
const maelys_datalog_prepared_explanation_t *prepared,
size_t *out_required
);
Reads the cached formatted length of a prepared explanation without traversing its proof again.
Arguments
preparedconst maelys_datalog_prepared_explanation_t *handleread
Prepared explanation to inspect.
Outputs

Written on success.

out_requiredsize_t *out
Receives the cached prepared-text length in bytes, excluding the terminating NUL.
Return status

maelys_datalog_status_t — OK on success.

Returns OK with the byte length excluding the terminating NUL.

Example
maelys_datalog_prepared_explanation_t *explanation = NULL;
check_status(prepare(result, &explanation));
maelys_datalog_status_t status;
size_t required = 0;
status = maelys_datalog_prepared_explanation_text_size(explanation, &required);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("text bytes: %zu\n", required);
check_status(maelys_datalog_prepared_explanation_release(explanation));
explanation = NULL;
status: ok
text bytes: 304

prepared_explanation_write_text()

Write prepared explanation text.

C · functionSince v0.4.0
prepared_explanation_write_text()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_prepared_explanation_write_text(
const maelys_datalog_prepared_explanation_t *prepared,
char *out_text,
size_t capacity
);
Formats a retained explanation into caller-owned output text without preparing it again.
Arguments
preparedconst maelys_datalog_prepared_explanation_t *handleread
Prepared explanation to inspect.
capacitysize_tin
Byte capacity of out_text, including the terminating NUL; reserve text_size plus one byte for the complete text.
Outputs

Written on success.

out_textchar *out
Caller-owned output buffer bounded by capacity bytes. Must be non-NULL; PAYLOAD_TOO_LARGE writes no partial text.
Return status

maelys_datalog_status_t — OK on success.

Returns OK when the complete text fits. PAYLOAD_TOO_LARGE writes no partial text; reserve text_size plus one byte.

Example
maelys_datalog_prepared_explanation_t *explanation = NULL;
check_status(prepare(result, &explanation));
maelys_datalog_status_t status;
char text[16u * 1024u];
status = maelys_datalog_prepared_explanation_write_text(explanation, text, sizeof(text));
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
print_text_lines("explanation", text);
check_status(maelys_datalog_prepared_explanation_release(explanation));
explanation = NULL;
Show output (9 lines)
status: ok
explanation: MAELYS-DATALOG-v2
explanation: document=why-true
explanation: status=complete
explanation: steps=1 premises=2
explanation: step=0 rule=1 fact="allow"("alice","roadmap.pdf")
explanation: premise=0 body=0 kind=positive origin=edb fact="owns"("alice","roadmap.pdf") parent=-
explanation: premise=1 body=1 kind=negative-absence origin=edb fact="blocked"("alice") parent=-
explanation: result-step=0

Release

prepared_explanation_release()

Release a prepared explanation.

C · functionSince v0.4.0
prepared_explanation_release()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_prepared_explanation_release(
maelys_datalog_prepared_explanation_t *prepared
);
Ends the prepared explanation lease so its result and workspace may be reused or released.
Arguments
Prepared explanation to release.
Return status

maelys_datalog_status_t — OK on success.

Returns OK after releasing the prepared handle.

Example
maelys_datalog_prepared_explanation_t *explanation = NULL;
check_status(prepare(result, &explanation));
maelys_datalog_status_t status;
status = maelys_datalog_prepared_explanation_release(explanation);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
explanation = NULL;

Macros

EXPLANATION_STORAGE()

C · macroSince v0.4.1
EXPLANATION_STORAGE()

#include <maelys/datalog_builders.h>

MAELYS_DATALOG_EXPLANATION_STORAGE(
name,
bytes
)

Declares suitably aligned caller-owned bytes for prepared explanations.

Arguments
nameC identifier token
Name of the byte array being declared, not a string expression.
bytespositive integer constant expression
Compile-time array size in bytes; check it against the runtime storage bound.
Effect

Declares aligned caller-owned bytes. It does not size, prepare or release an explanation; the example size is illustrative, not a guaranteed bound. An aligned fixed-size byte array; it neither prepares nor releases an explanation.

Example
MAELYS_DATALOG_EXPLANATION_STORAGE(
    explanation_storage, 256u * 1024u);
maelys_datalog_prepared_explanation_t *explanation = NULL;
maelys_datalog_status_t status = maelys_datalog_result_prepare_explanation(
    result, MAELYS_DATALOG_EXPLAIN_TRUE, "allow", terms, 2u,
    explanation_storage, sizeof(explanation_storage), &explanation);
check_status(status);
printf("prepared: %s\n", explanation != NULL ? "yes" : "no");
check_status(maelys_datalog_prepared_explanation_release(explanation));
explanation = NULL;

Struct: fact_view_t

C · structSince v0.10.0
fact_view_t

#include <maelys/datalog.h>

Borrowed view of one result fact returned by enumeration. Its symbol IDs belong to the same live result.
Fields
aritysize_t
Number of populated result terms.
Typed result terms; resolve symbol IDs with result_symbol_text() on this result.
Typedef
typedef struct {
    size_t arity;
    maelys_datalog_term_view_t terms[MAELYS_DATALOG_PUBLIC_MAX_TERMS];
} maelys_datalog_fact_view_t;

Constants

Array bound
NameValueMeaning
PUBLIC_MAX_TERMS
Since ≤ v0.1.0-alpha.2
4Maximum number of terms in a public fact or query.

Struct: term_view_t

C · structSince v0.10.0
term_view_t

#include <maelys/datalog.h>

A typed term borrowed from an enumerated result fact. Symbol IDs are scoped to that result and must be resolved with result_symbol_text() while it remains alive.
Fields
Selects the active result-term payload member.
asunion { uint32_t symbol_id; int64_t integer; int boolean; }
Tagged result payload: symbol_id, integer or boolean. Resolve symbol_id with the result that produced this term.
Typedef
typedef struct {
    maelys_datalog_value_kind_t kind;
    union {
        uint32_t symbol_id;
        int64_t integer;
        int boolean;
    } as;
} maelys_datalog_term_view_t;

Enum: explanation_kind_t

C · enumSince v0.4.0
explanation_kind_t

#include <maelys/datalog.h>

Selects Why-true or Why-false when preparing or rendering an explanation.
Members
EXPLAIN_TRUE1
Why-true explanation.
EXPLAIN_FALSE2
Why-false explanation.
Typedef
typedef enum {
    MAELYS_DATALOG_EXPLAIN_TRUE = 1,
    MAELYS_DATALOG_EXPLAIN_FALSE = 2
} maelys_datalog_explanation_kind_t;

Struct: filter_statistics_t

C · structSince v0.10.0
filter_statistics_t

#include <maelys/datalog.h>

Filter activity accumulated by a reference-backend solve.
Fields
evaluationssize_t
Total filter evaluations.
matchessize_t
Filter evaluations that matched.
non_matchessize_t
Filter evaluations that did not match.
cost_unitssize_t
Accumulated filter cost units.
Typedef
typedef struct {
    size_t evaluations;
    size_t matches;
    size_t non_matches;
    size_t cost_units;
} maelys_datalog_filter_statistics_t;

status_t ↗ · result_t ↗ · value_t ↗ · value_kind_t ↗.

Usage guides

Querying guide · Full program.