C Extension API
Backends
Implement snapshot, resource-aware and retained-input native solvers with explicit commit and abort.Implement a native solver with an explicit lifecycle: validate a supported program, stage a complete result, then publish state only when the core commits it. The Backends guide applies the independent ABI 5, ABI 6 and ABI 7 contracts to document-access decisions.
Struct: backend_t
#include <maelys/datalog_backend.h>
Typedef
typedef struct maelys_datalog_backend_t {
uint32_t abi_version;
size_t struct_size;
const char *name;
const char *semantic_id;
uint64_t capabilities;
/* Read-only, deterministic for the program, without allocation or reentry.
* Called once per session creation before prepare (explicit public queries
* are separate calls). Zero bytes is valid; alignment must be a nonzero
* power of two <= alignof(max_align_t), even for zero bytes. */
maelys_datalog_status_t (*storage_requirements)(const maelys_datalog_program_t *program,
size_t *out_bytes, size_t *out_alignment);
/* storage is non-NULL and valid for this call only; storage->bytes is the
* caller's exact pointer. Retain the buffer, not the descriptor pointer.
* Absent storage is represented by {sizeof(...), NULL, 0, 1}. */
maelys_datalog_status_t (*prepare)(const maelys_datalog_program_t *program,
const maelys_datalog_backend_storage_t *storage, void **out_state);
maelys_datalog_status_t (*solve)(void *state,
const maelys_datalog_fact_t *canonical_inputs,
size_t input_count, maelys_datalog_backend_output_t *output,
void **out_result_state, maelys_datalog_diagnostic_t *out_diagnostic);
/* Caller-owned explanation storage introduced in ABI 3 is retained in ABI 5.
* The versioned common diagnostic protocol introduced in ABI 4 also remains.
* All three are required if either EXPLAIN capability is advertised; the
* host calls only supported kinds. No allocator calls, acquired resources,
* engine reentry or retained query-string pointers. Result state stays alive
* until all explanations are released. Scratch belongs in the same storage;
* bounded automatic locals are allowed. No destroy hook is needed: this
* storage contains only values and references borrowed from the live result.
*
* storage_requirements is read-only and deterministic for (result, kind).
* Alignment must be a nonzero power of two <= alignof(max_align_t), size > 0.
* prepare extracts once and returns the EXACT text length excluding NUL;
* SIZE_MAX is invalid. On failure storage may change, retained state may not.
* write_text only formats the prepared storage; never extracts/searches again.
* The host checks capacity first. Successful output is NUL-terminated and
* exactly the prepared length. All callbacks are trusted and bounded. */
maelys_datalog_status_t (*explanation_storage_requirements)(
void *state, void *result_state, maelys_datalog_explanation_kind_t kind,
size_t *out_bytes, size_t *out_alignment);
maelys_datalog_status_t (*explanation_prepare)(
void *state, void *result_state, maelys_datalog_explanation_kind_t kind,
const char *predicate, const maelys_datalog_value_t *terms, size_t arity,
void *storage, size_t storage_bytes, size_t *out_text_size);
maelys_datalog_status_t (*explanation_write_text)(
void *state, void *result_state, maelys_datalog_explanation_kind_t kind,
const void *storage, char *out_text, size_t capacity);
/* Exactly once per accepted result, after host validation/copy/deduplication
* and installation; for windows, only after publication is irrevocable.
* Never called for an abandoned candidate (including window init probes).
* Infallible, no allocation or reentry; precedes every explanation callback.
* solve must keep previously committed backend state intact until commit.
* destroy_result without commit means abort; after commit it means release.
* A NULL result_state is valid and does not suppress either callback. */
void (*commit)(void *state, void *result_state);
void (*destroy_result)(void *state, void *result_state);
void (*destroy)(void *state);
} maelys_datalog_backend_t;| Used by | backend_storage_requirements() · session_config_set_backend() · extension_t — backends · extension_v2_t — backends_v5 · session_options_t — backend |
|---|
Constants
Snapshot ABI
BACKEND_ABI_VERSION
#define MAELYS_DATALOG_BACKEND_ABI_VERSION 5uABI 5 snapshot descriptor version; fixed resource policy and complete snapshot solving.
5
Struct: backend_v6_t
#include <maelys/datalog_backend.h>
Typedef
typedef struct maelys_datalog_backend_v6_t {
uint32_t abi_version;
size_t struct_size;
const char *name;
const char *semantic_id;
uint64_t capabilities;
uint64_t resource_features;
maelys_datalog_status_t (*storage_requirements)(
const maelys_datalog_program_t *program,
const maelys_datalog_session_resources_t *resources,
size_t *out_bytes, size_t *out_alignment);
maelys_datalog_status_t (*prepare)(
const maelys_datalog_program_t *program,
const maelys_datalog_session_resources_t *resources,
const maelys_datalog_backend_storage_t *storage, void **out_state);
/* Snapshot solve, complete emission, explanation and commit/abort contracts
* retain their ABI 5 semantics and callback signatures. No delta callback. */
maelys_datalog_status_t (*solve)(
void *state, const maelys_datalog_fact_t *canonical_inputs,
size_t input_count, maelys_datalog_backend_output_t *output,
void **out_result_state, maelys_datalog_diagnostic_t *out_diagnostic);
maelys_datalog_status_t (*explanation_storage_requirements)(
void *state, void *result_state, maelys_datalog_explanation_kind_t kind,
size_t *out_bytes, size_t *out_alignment);
maelys_datalog_status_t (*explanation_prepare)(
void *state, void *result_state, maelys_datalog_explanation_kind_t kind,
const char *predicate, const maelys_datalog_value_t *terms, size_t arity,
void *storage, size_t storage_bytes, size_t *out_text_size);
maelys_datalog_status_t (*explanation_write_text)(
void *state, void *result_state, maelys_datalog_explanation_kind_t kind,
const void *storage, char *out_text, size_t capacity);
void (*commit)(void *state, void *result_state);
void (*destroy_result)(void *state, void *result_state);
void (*destroy)(void *state);
} maelys_datalog_backend_v6_t;Constants
Resource-aware ABI
BACKEND_V6_ABI_VERSION
#define MAELYS_DATALOG_BACKEND_V6_ABI_VERSION 6uABI 6 resource-aware snapshot descriptor version; independent of ABI 5 and ABI 7.
6
BACKEND_V6_SIZE
#define MAELYS_DATALOG_BACKEND_V6_SIZE sizeof(maelys_datalog_backend_v6_t)Complete required ABI 6 descriptor size; optional compatible tails may follow without inserting callbacks.
sizeof(maelys_datalog_backend_v6_t)platform-dependent
Struct: backend_v7_t
#include <maelys/datalog_backend.h>
Typedef
typedef struct maelys_datalog_backend_v7_t {
uint32_t abi_version;
size_t struct_size;
const char *name, *semantic_id;
uint64_t capabilities, resource_features;
maelys_datalog_status_t (*storage_requirements)(const maelys_datalog_program_t *program,
const maelys_datalog_session_resources_t *resources, size_t *out_bytes, size_t *out_alignment);
maelys_datalog_status_t (*prepare)(const maelys_datalog_program_t *program,
const maelys_datalog_session_resources_t *resources, const maelys_datalog_backend_storage_t *storage, void **out_state);
maelys_datalog_backend_transaction_solve_t solve;
maelys_datalog_status_t (*explanation_storage_requirements)(void *state, void *result_state,
maelys_datalog_explanation_kind_t kind, size_t *out_bytes, size_t *out_alignment);
maelys_datalog_status_t (*explanation_prepare)(void *state, void *result_state, maelys_datalog_explanation_kind_t kind,
const char *predicate, const maelys_datalog_value_t *terms, size_t arity, void *storage, size_t storage_bytes, size_t *out_text_size);
maelys_datalog_status_t (*explanation_write_text)(void *state, void *result_state, maelys_datalog_explanation_kind_t kind,
const void *storage, char *out_text, size_t capacity);
void (*commit)(void *state, void *result_state);
void (*destroy_result)(void *state, void *result_state);
void (*destroy)(void *state);
} maelys_datalog_backend_v7_t;Constants
Retained-input ABI
BACKEND_V7_ABI_VERSION
#define MAELYS_DATALOG_BACKEND_V7_ABI_VERSION 7uABI 7 retained-input descriptor version; replacement/delta solving with commit-only base adoption.
7
BACKEND_V7_SIZE
#define MAELYS_DATALOG_BACKEND_V7_SIZE sizeof(maelys_datalog_backend_v7_t)Complete retained-input ABI 7 descriptor size; this type must never be cast as ABI 5 or ABI 6.
sizeof(maelys_datalog_backend_v7_t)platform-dependent
| Used by | backend_t — solve · backend_v6_t — solve |
|---|
Handle: backend_output_t
#include <maelys/datalog_backend.h>
typedef struct maelys_datalog_backend_output maelys_datalog_backend_output_t;Functions
backend_charge()
#include <maelys/datalog_backend.h>
maelys_datalog_status_t
maelys_datalog_backend_charge(
maelys_datalog_backend_output_t *output,
uint64_t units
);maelys_datalog_status_t — OK on success.
Returns OK if the charge is accepted. An exhausted work limit fails the candidate and remains sticky; propagate the returned status without continuing unbounded work.
maelys_datalog_status_t status = maelys_datalog_backend_charge(output, 1);
printf("bounded work charged: %s\n", maelys_datalog_status_name(status));
return status;bounded work charged: ok
backend_filter()
#include <maelys/datalog_backend.h>
maelys_datalog_status_t
maelys_datalog_backend_filter(
maelys_datalog_backend_output_t *output,
const char *name,
const char *semantic_id,
const unsigned char *value,
size_t value_length,
const unsigned char *pattern,
size_t pattern_length,
int *out_matched
);Written on success.
maelys_datalog_status_t — OK on success.
Returns OK with a normalized match after the registered filter’s cost check and evaluation. An unknown name/semantic identity returns UNSUPPORTED; a pattern not declared by the compiled program returns INVALID_FIELD. Invalid arguments, exhausted filter quotas, evaluation errors and prior sticky failures reject the candidate. Propagate every failure.
maelys_datalog_status_t status = maelys_datalog_backend_filter(
output, "ends_with", "string.ends-with.utf8-bytes-v1",
(const unsigned char *)document, strlen(document),
(const unsigned char *)".pdf", 4, out_matched);
if (status) return status;
printf("PDF suffix matched: %s\n", *out_matched ? "yes" : "no");
return MAELYS_DATALOG_STATUS_OK;PDF suffix matched: yes
backend_emit()
#include <maelys/datalog_backend.h>
maelys_datalog_status_t
maelys_datalog_backend_emit(
maelys_datalog_backend_output_t *output,
const maelys_datalog_fact_t *fact
);maelys_datalog_status_t — OK on success.
Returns OK after accepting the copied fact. Invalid facts, unknown vocabulary, exhausted capacity or a prior sticky output failure return an error; no partial result is published when solve fails.
maelys_datalog_status_t status = maelys_datalog_backend_emit(output, answer);
printf("derived fact accepted: %s\n", maelys_datalog_status_name(status));
return status;derived fact accepted: ok
Struct: backend_input_t
#include <maelys/datalog_backend.h>
Typedef
typedef struct {
size_t struct_size;
uint32_t contract_version, kind;
maelys_datalog_input_base_t base, next;
const maelys_datalog_backend_input_view_t *additions, *removals;
size_t addition_count, removal_count;
} maelys_datalog_backend_input_t;Constants
Transaction packets
BACKEND_INPUT_VERSION
#define MAELYS_DATALOG_BACKEND_INPUT_VERSION 1uVersion of the borrowed retained-input transaction packet.
1
BACKEND_INPUT_REPLACE
#define MAELYS_DATALOG_BACKEND_INPUT_REPLACE 1uRetained-input packet whose additions contain the complete replacement dynamic EDB.
1
BACKEND_INPUT_DELTA
#define MAELYS_DATALOG_BACKEND_INPUT_DELTA 2uRetained-input packet with removals applied before additions; additions win and absent removals do nothing.
2
Handle: backend_input_view_t
#include <maelys/datalog_backend.h>
typedef struct maelys_datalog_backend_input_view maelys_datalog_backend_input_view_t;Functions
backend_input_at()
#include <maelys/datalog_backend.h>
maelys_datalog_status_t
maelys_datalog_backend_input_at(
const maelys_datalog_backend_input_view_t *view,
size_t index,
maelys_datalog_fact_t *out_fact
);Written on success.
maelys_datalog_status_t — OK on success.
Returns OK with the exported fact. An out-of-range index returns NOT_FOUND; any failure leaves out_fact unchanged. The call allocates nothing and exposes no internal symbol IDs.
maelys_datalog_status_t status = maelys_datalog_backend_input_at(view, index, out_fact);
printf("borrowed input read: %s\n", maelys_datalog_status_name(status));
return status;borrowed input read: ok borrowed input read: ok borrowed input read: ok
Type: backend_transaction_solve_t
#include <maelys/datalog_backend.h>
Typedef
typedef maelys_datalog_status_t (*maelys_datalog_backend_transaction_solve_t)(
void *state, const maelys_datalog_backend_input_t *input, maelys_datalog_backend_output_t *output,
void **out_result_state, maelys_datalog_diagnostic_t *out_diagnostic);| Used by | backend_v7_t — solve |
|---|
Struct: session_allocation_resources_t
#include <maelys/datalog_backend.h>
Typedef
typedef struct {
maelys_datalog_session_resources_t base;
size_t execution_byte_cap;
const maelys_datalog_allocation_service_t *allocation;
} maelys_datalog_session_allocation_resources_t;Constants
Elastic resource contract
ALLOCATION_RESOURCES_V1_SIZE
#define MAELYS_DATALOG_ALLOCATION_RESOURCES_V1_SIZE \
sizeof(maelys_datalog_session_allocation_resources_t)Complete elastic resource-tail size, including normalized base resources, execution cap and the session-bound service pointer.
sizeof(maelys_datalog_session_allocation_resources_t)platform-dependent
Struct: allocation_service_t
#include <maelys/datalog_backend.h>
Typedef
typedef struct {
size_t struct_size;
uint32_t contract_version;
uint32_t reserved; /* zero */
uint64_t required_features; /* zero in V1 */
size_t tracking_bytes;
size_t maximum_alignment;
void *context;
maelys_datalog_status_t (*inspect)(
void *context, size_t bytes, size_t alignment,
maelys_datalog_allocation_budget_t *out_budget);
maelys_datalog_status_t (*acquire)(
void *context, size_t bytes, size_t alignment,
void **out_block, maelys_datalog_diagnostic_t *out_diagnostic);
void (*release)(void *context, void *block);
} maelys_datalog_allocation_service_t;Constants
Service contract
ALLOCATION_SERVICE_VERSION
#define MAELYS_DATALOG_ALLOCATION_SERVICE_VERSION 1uVersion of the host’s session-bound allocation-service contract.
1
ALLOCATION_SERVICE_PREFIX_SIZE
#define MAELYS_DATALOG_ALLOCATION_SERVICE_PREFIX_SIZE \
(offsetof(maelys_datalog_allocation_service_t, required_features) + sizeof(uint64_t))Minimum readable allocation-service prefix, expressed from the installed public structure layout.
(offsetof(maelys_datalog_allocation_service_t, required_features) + sizeof(uint64_t))platform-dependent
ALLOCATION_SERVICE_V1_SIZE
#define MAELYS_DATALOG_ALLOCATION_SERVICE_V1_SIZE \
sizeof(maelys_datalog_allocation_service_t)Complete V1 allocation-service descriptor size; use its source expression rather than a fixed byte count.
sizeof(maelys_datalog_allocation_service_t)platform-dependent
Struct: allocation_budget_t
#include <maelys/datalog_backend.h>
Typedef
typedef struct {
size_t struct_size;
uint32_t contract_version;
uint32_t reserved; /* zero */
uint64_t required_features; /* zero in V1 */
size_t cap_bytes;
size_t current_bytes;
size_t remaining_bytes;
size_t operation_peak_bytes;
size_t required_charge;
uint32_t fits;
uint32_t reserved_tail; /* zero */
} maelys_datalog_allocation_budget_t;Creation
ALLOCATION_BUDGET_INIT
#include <maelys/datalog_backend.h>
#define MAELYS_DATALOG_ALLOCATION_BUDGET_INITStarting value for inspecting a session allocation request: initializes the size and contract version, then clears every output field. Pass the record to allocation_service_t.inspect.
Sets the installed size and contract version automatically and clears the remaining fields before the service fills them in. An initialized allocation_budget_t output record for allocation_service_t.inspect.
| Field | Initial value | Meaning |
|---|---|---|
struct_size | MAELYS_DATALOG_ALLOCATION_BUDGET_V1_SIZE | Readable output size; the ALLOCATION_BUDGET_INIT macro sets the installed V1 size automatically.set automatically |
contract_version | MAELYS_DATALOG_ALLOCATION_BUDGET_VERSION | Allocation-budget contract version, set automatically by ALLOCATION_BUDGET_INIT.set automatically |
reserved | UINT32_C(0) | Reserved input bits; keep zero. |
required_features | UINT64_C(0) | Required budget-contract features; zero in V1. |
cap_bytes | 0 | Maximum charge available to this session’s execution allocation service. |
current_bytes | 0 | Current execution charge, including the session's fixed execution reservation and live service allocations. |
remaining_bytes | 0 | Unused charge remaining below cap_bytes. |
operation_peak_bytes | 0 | Largest charge reached in the current operation; inspection does not update this value. |
required_charge | 0 | Exact charge for the inspected request, including service tracking and alignment overhead. |
fits | UINT32_C(0) | Exactly 1 when the inspected request fits the remaining budget, otherwise 0; inspection acquires nothing. |
reserved_tail | UINT32_C(0) | Reserved output tail; zero in V1. |
Expansion
{ MAELYS_DATALOG_ALLOCATION_BUDGET_V1_SIZE, MAELYS_DATALOG_ALLOCATION_BUDGET_VERSION, UINT32_C(0), UINT64_C(0), 0, 0, 0, 0, 0, UINT32_C(0), UINT32_C(0) }maelys_datalog_allocation_budget_t budget = MAELYS_DATALOG_ALLOCATION_BUDGET_INIT;
printf("budget version initialized: %s\n",
budget.contract_version == MAELYS_DATALOG_ALLOCATION_BUDGET_VERSION ? "yes" : "no");
printf("ready for allocation service inspect: %s\n",
budget.struct_size == sizeof(budget) && budget.reserved == 0 && budget.reserved_tail == 0 ? "yes" : "no");
return MAELYS_DATALOG_STATUS_OK;budget version initialized: yes ready for allocation service inspect: yes
Versioning
Initialize with the ALLOCATION_BUDGET_INIT macro; these values identify the structure version and are not set by hand.
Budget contract
ALLOCATION_BUDGET_VERSION
#define MAELYS_DATALOG_ALLOCATION_BUDGET_VERSION 1uVersion of the allocation-budget record initialized by ALLOCATION_BUDGET_INIT.
1
ALLOCATION_BUDGET_PREFIX_SIZE
#define MAELYS_DATALOG_ALLOCATION_BUDGET_PREFIX_SIZE \
(offsetof(maelys_datalog_allocation_budget_t, required_features) + sizeof(uint64_t))Minimum readable allocation-budget prefix, expressed from the installed public structure layout.
(offsetof(maelys_datalog_allocation_budget_t, required_features) + sizeof(uint64_t))platform-dependent
ALLOCATION_BUDGET_V1_SIZE
#define MAELYS_DATALOG_ALLOCATION_BUDGET_V1_SIZE \
sizeof(maelys_datalog_allocation_budget_t)Complete V1 allocation-budget size; retain the sizeof expression rather than a platform-specific number.
sizeof(maelys_datalog_allocation_budget_t)platform-dependent