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
#include <maelys/datalog.h>
typedef struct maelys_datalog_result maelys_datalog_result_t;Functions
result_query()
Query a ground fact.
#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
);Written on success.
maelys_datalog_status_t — OK on success.
An absent fact is a successful negative answer; an API error is separate.
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);status: ok present: 1
result_derived_fact_count()
Count derived facts.
#include <maelys/datalog.h>
maelys_datalog_status_t
maelys_datalog_result_derived_fact_count(
const maelys_datalog_result_t *result,
size_t *out_count
);Written on success.
maelys_datalog_status_t — OK on success.
Returns OK and a total across IDB predicates, including those not queryable.
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.
#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
);Written on success.
maelys_datalog_status_t — OK on success.
Returns OK and the matching count. Supply caller storage to fetch views after measuring.
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.
#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
);Written on success.
maelys_datalog_status_t — OK on success.
Returns borrowed text tied to this result; never free it independently.
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()
#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
);Written on success.
maelys_datalog_status_t — OK on success.
OK for a live reference-backend result; UNSUPPORTED for another backend.
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.
#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
);Written on success.
maelys_datalog_status_t — OK on success.
Returns OK with the required byte count; an insufficient text buffer is separate from a truncated explanation.
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.
#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
);Written on success.
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.
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()
#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
);Written on success.
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.
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.
#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
);Written on success.
maelys_datalog_status_t — OK on success.
Returns OK with a retained handle. Invalid alignment or insufficient storage fails without acquiring a result lease.
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()
#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
);Written on success.
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.
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.
#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
);Written on success.
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.
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.
#include <maelys/datalog.h>
maelys_datalog_status_t
maelys_datalog_result_free(
maelys_datalog_result_t *result
);maelys_datalog_status_t — OK on success.
Returns OK after ending the result lease; another solve may then reuse its session.
maelys_datalog_status_t status = maelys_datalog_result_free(result);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
result = NULL;status: ok
Macros
QUERY()
#include <maelys/datalog_builders.h>
MAELYS_DATALOG_QUERY(
result,
present,
...
)Queries a fully specified fact using C11 term conversion.
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().
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);status: ok present: 1
Handle: prepared_explanation_t
#include <maelys/datalog.h>
typedef struct maelys_datalog_prepared_explanation maelys_datalog_prepared_explanation_t;Creation
result_explanation_storage_requirements()
Measure result explanation storage.
#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
);Written on success.
maelys_datalog_status_t — OK on success.
Returns OK with both requirements. Invalid kind or outputs fail without changing the outputs.
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.
#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
);Written on success.
maelys_datalog_status_t — OK on success.
Returns OK with the byte length excluding the terminating NUL.
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.
#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
);Written on success.
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.
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.
#include <maelys/datalog.h>
maelys_datalog_status_t
maelys_datalog_prepared_explanation_release(
maelys_datalog_prepared_explanation_t *prepared
);maelys_datalog_status_t — OK on success.
Returns OK after releasing the prepared handle.
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;status: ok
Macros
EXPLANATION_STORAGE()
#include <maelys/datalog_builders.h>
MAELYS_DATALOG_EXPLANATION_STORAGE(
name,
bytes
)Declares suitably aligned caller-owned bytes for prepared explanations.
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.
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;prepared: yes
Struct: fact_view_t
#include <maelys/datalog.h>
Typedef
typedef struct {
size_t arity;
maelys_datalog_term_view_t terms[MAELYS_DATALOG_PUBLIC_MAX_TERMS];
} maelys_datalog_fact_view_t;Constants
| Name | Value | Meaning |
|---|---|---|
PUBLIC_Since ≤ v0.1.0-alpha.2 | 4 | Maximum number of terms in a public fact or query. |
Struct: term_view_t
#include <maelys/datalog.h>
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;| Used by | fact_view_t — terms |
|---|
Enum: explanation_kind_t
#include <maelys/datalog.h>
Typedef
typedef enum {
MAELYS_DATALOG_EXPLAIN_TRUE = 1,
MAELYS_DATALOG_EXPLAIN_FALSE = 2
} maelys_datalog_explanation_kind_t;Struct: filter_statistics_t
#include <maelys/datalog.h>
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 ↗.