C Extension API

Native modules guide

Load a document policy with a native filter, planner and deferred domain.

At application startup, register a native file-suffix filter and a join planner before loading the document-access policy. The domain installer supplies the input and query declarations when the loader needs them; the application then checks which documents each user may open. See the Native modules reference for the descriptor and callback contracts.

Policy and inputs

DATALOG
allow(User, Doc) :- user(User), owns(User, Doc), not(blocked(User)), document_suffix(Doc, ".pdf").

1. Register the document filter

The document_suffix filter compares the final bytes of a document name with the literal pattern ".pdf". Its callbacks validate the pattern, calculate a conservative work charge, then evaluate without allocation, I/O, retained arguments or mutable external state. These are the callbacks an author supplies; the descriptor binds them to the filter name and semantic identity.

Example
CODE
/* The filter is pure and bounded. No callback keeps pointers or calls the engine. */
static maelys_datalog_status_t validate_suffix(const unsigned char *pattern, size_t length) {
    if (!pattern || !length || length > 63) return MAELYS_DATALOG_STATUS_INVALID_FIELD;
    return MAELYS_DATALOG_STATUS_OK;
}
static maelys_datalog_status_t suffix_cost(size_t value_length, size_t pattern_length, size_t *out_cost) {
    (void)value_length;
    if (!out_cost) return MAELYS_DATALOG_STATUS_INVALID_ARGUMENT;
    if (pattern_length == SIZE_MAX) return MAELYS_DATALOG_STATUS_PAYLOAD_TOO_LARGE;
    *out_cost = pattern_length + 1;
    return MAELYS_DATALOG_STATUS_OK;
}
static maelys_datalog_status_t evaluate_suffix(const unsigned char *value, size_t value_length,
    const unsigned char *pattern, size_t pattern_length, int *out_matched) {
    if (!value || !pattern || !out_matched) return MAELYS_DATALOG_STATUS_INVALID_ARGUMENT;
    *out_matched = value_length >= pattern_length &&
        memcmp(value + value_length - pattern_length, pattern, pattern_length) == 0;
    return MAELYS_DATALOG_STATUS_OK;
}
static const maelys_datalog_filter_module_t suffix_module = {
    MAELYS_DATALOG_MODULE_ABI_VERSION, sizeof(maelys_datalog_filter_module_t),
    "document_suffix", "example.document-suffix.bytes-v1.cost-pattern-plus-one",
    validate_suffix, suffix_cost, evaluate_suffix
};

static void register_document_filter(void) {
    // 1. Register a byte-wise suffix filter before loading any policy.
    maelys_datalog_status_t status = maelys_datalog_register_filter_module(&suffix_module);
    printf("filter registration: %s\n", maelys_datalog_status_name(status));
    check_status(status);
}

2. Register a safe join planner

The planner chooses the highest default_score among the safe candidates supplied by the engine, breaking ties by body_index. It returns an index in that candidates array, not a rule-body index; the core still checks bindings, anchors deltas, runs the rule and owns all data.

Example
CODE
/* The core supplies only safe choices; this callback never reorders the array. */
static maelys_datalog_status_t choose_join(const maelys_datalog_join_candidate_t *candidates,
    size_t candidate_count, size_t *out_candidate_index) {
    if (!candidates || !candidate_count || !out_candidate_index)
        return MAELYS_DATALOG_STATUS_INVALID_ARGUMENT;
    size_t best = 0;
    for (size_t i = 1; i < candidate_count; ++i)
        if (candidates[i].default_score > candidates[best].default_score ||
            (candidates[i].default_score == candidates[best].default_score &&
             candidates[i].body_index < candidates[best].body_index)) best = i;
    *out_candidate_index = best;
    return MAELYS_DATALOG_STATUS_OK;
}
static const maelys_datalog_planner_module_t planner_module = {
    MAELYS_DATALOG_MODULE_ABI_VERSION, sizeof(maelys_datalog_planner_module_t),
    "document_join", "example.document-join.score-then-body-v1", choose_join
};

static void register_document_planner(void) {
    // 2. Choose only among the safe join candidates supplied by the engine.
    maelys_datalog_status_t status = maelys_datalog_register_planner_module(&planner_module);
    printf("planner registration: %s\n", maelys_datalog_status_name(status));
    check_status(status);
}
planner registration: ok

3. Register the deferred domain

Write install_document_predicates to add user, owns, blocked and allow through domain_builder_add() when policy loading invokes it; the domain identity stays in the context. A callback error, including an ignored builder error, rejects the candidate domain; nothing is partially committed.

Example
CODE
static const maelys_datalog_predicate_t predicates[] = {
    MAELYS_DATALOG_EDB("user", 1), MAELYS_DATALOG_EDB("owns", 2),
    MAELYS_DATALOG_EDB("blocked", 1), MAELYS_DATALOG_IDB_QUERY("allow", 2)
};
static maelys_datalog_status_t install_document_predicates(maelys_datalog_domain_builder_t *builder) {
    for (size_t i = 0; i < sizeof(predicates) / sizeof(predicates[0]); ++i) {
        maelys_datalog_status_t status = maelys_datalog_domain_builder_add(builder, &predicates[i]);
        if (status != MAELYS_DATALOG_STATUS_OK) return status;
    }
    return MAELYS_DATALOG_STATUS_OK;
}

static void register_document_domain(void) {
    // 3. Defer predicate declarations until a policy loads this named domain.
    maelys_datalog_status_t status = maelys_datalog_domain_register_advanced(&domain,
        "Document access with a native file-suffix filter", install_document_predicates);
    printf("domain registration: %s\n", maelys_datalog_status_name(status));
    check_status(status);
}

4. Load the policy and verify its declarations

Loading the rule uses both registered modules and invokes the deferred installer. Reading the compiled program confirms that four predicates were installed; this program view is borrowed from the session and has no separate release call.

Example
CODE
static void load_and_inspect_policy(void) {
    // 4. Loading invokes the installer and compiles the custom filter in the rule.
    maelys_datalog_status_t status = maelys_datalog_policy_load_inline(domain.name,
        "document-access", source, strlen(source), &policy, NULL);
    printf("policy loading: %s\n", maelys_datalog_status_name(status));
    check_status(status);
    check_status(maelys_datalog_session_create(policy, 0, &session));
    const maelys_datalog_program_t *program = NULL;
    maelys_datalog_program_info_t info;
    check_status(maelys_datalog_session_program(session, &program));
    check_status(maelys_datalog_program_info(program, &info));
    printf("installed predicates: %zu\n", info.predicate_count);
}
policy loading: ok
installed predicates: 4

5. Use the native modules in access decisions

The policy allows an owned .pdf only for a declared user who is not blocked. Read the returned decisions in application code: alice may open roadmap.pdf, the .txt document fails the custom filter, and mallory is refused by the blocked input fact.

Example
CODE
static void query_document_access(void) {
    // 5. Read application decisions; callbacks themselves do not print or retain state.
    maelys_datalog_status_t status = maelys_datalog_session_solve(session, facts,
        sizeof(facts) / sizeof(facts[0]), &result, NULL);
    printf("solving: %s\n", maelys_datalog_status_name(status));
    check_status(status);
    const char *const users[] = { "alice", "alice", "mallory" };
    const char *const documents[] = { "roadmap.pdf", "draft.txt", "roadmap.pdf" };
    for (size_t i = 0; i < sizeof(users) / sizeof(users[0]); ++i) {
        const maelys_datalog_value_t terms[] = {
            MAELYS_DATALOG_SYMBOL(users[i]), MAELYS_DATALOG_SYMBOL(documents[i])
        };
        int present = 0;
        check_status(maelys_datalog_result_query(result, "allow", terms, 2, &present));
        printf("allow(%s, %s): %s\n", users[i], documents[i], present ? "yes" : "no");
    }
}
solving: ok
allow(alice, roadmap.pdf): yes
allow(alice, draft.txt): no
allow(mallory, roadmap.pdf): no

These are trusted native callbacks, not sandboxed plugins. Register filter and planner modules on the startup thread before initializing or loading any policy; descriptors and strings are copied, but callback code must remain loaded for the process lifetime. There is no replacement or unload operation. Keep filter and planner callbacks deterministic, bounded, allocation-free and reentrant: no retained arguments, mutated inputs, engine reentry, I/O, time, locale or mutable external state. semantic_id versions both behavior and cost accounting and contributes to policy fingerprints. Domains are process-global even when explicit contexts are used.

Reference context

Insert the numbered fragments at API_REFERENCE_EXAMPLE in order; main calls their visible functions. Callback implementations and descriptors are visible in the steps, not hidden in this context. The context contains the domain, callback implementations and cleanup; each displayed step has its preceding setup applied silently.

CODE
#include <maelys/datalog.h>
#include <maelys/datalog_builders.h>
#include <maelys/datalog_module.h>
#include <maelys/datalog_program.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static maelys_datalog_policy_t *policy = NULL;
static maelys_datalog_session_t *session = NULL;
static maelys_datalog_result_t *result = NULL;
static int cleanup_failed = 0;
static void check_status(maelys_datalog_status_t status) {
    if (status != MAELYS_DATALOG_STATUS_OK) {
        fprintf(stderr, "API call failed: %s\n", maelys_datalog_status_name(status));
        exit(EXIT_FAILURE);
    }
}
static void cleanup_context(void) {
    if (result) { if (maelys_datalog_result_free(result)) cleanup_failed = 1; result = NULL; }
    if (session) { if (maelys_datalog_session_free(session)) cleanup_failed = 1; session = NULL; }
    if (policy) { if (maelys_datalog_policy_free(policy)) cleanup_failed = 1; policy = NULL; }
}

static const char *const atoms[] = { ".pdf" };
static const maelys_datalog_domain_t domain = {
    "document_modules", NULL, 0, atoms, sizeof(atoms) / sizeof(atoms[0])
};
static const char source[] =
    "allow(User, Doc) :- user(User), owns(User, Doc), not(blocked(User)), "
    "document_suffix(Doc, \".pdf\").\n";
static const maelys_datalog_fact_t facts[] = {
    { "user", 1, { MAELYS_DATALOG_SYMBOL("alice") } },
    { "user", 1, { MAELYS_DATALOG_SYMBOL("mallory") } },
    { "owns", 2, { MAELYS_DATALOG_SYMBOL("alice"), MAELYS_DATALOG_SYMBOL("roadmap.pdf") } },
    { "owns", 2, { MAELYS_DATALOG_SYMBOL("alice"), MAELYS_DATALOG_SYMBOL("draft.txt") } },
    { "owns", 2, { MAELYS_DATALOG_SYMBOL("mallory"), MAELYS_DATALOG_SYMBOL("roadmap.pdf") } },
    { "blocked", 1, { MAELYS_DATALOG_SYMBOL("mallory") } }
};
/* API_REFERENCE_EXAMPLE */
int main(void) {
    if (atexit(cleanup_context) != 0) return EXIT_FAILURE;
    register_document_filter();
    register_document_planner();
    register_document_domain();
    load_and_inspect_policy();
    query_document_access();
    cleanup_context();
    return cleanup_failed ? EXIT_FAILURE : EXIT_SUCCESS;
}
Full program
CODE
#include <maelys/datalog.h>
#include <maelys/datalog_builders.h>
#include <maelys/datalog_module.h>
#include <maelys/datalog_program.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static maelys_datalog_policy_t *policy = NULL;
static maelys_datalog_session_t *session = NULL;
static maelys_datalog_result_t *result = NULL;
static int cleanup_failed = 0;
static void check_status(maelys_datalog_status_t status) {
    if (status != MAELYS_DATALOG_STATUS_OK) {
        fprintf(stderr, "API call failed: %s\n", maelys_datalog_status_name(status));
        exit(EXIT_FAILURE);
    }
}
static void cleanup_context(void) {
    if (result) { if (maelys_datalog_result_free(result)) cleanup_failed = 1; result = NULL; }
    if (session) { if (maelys_datalog_session_free(session)) cleanup_failed = 1; session = NULL; }
    if (policy) { if (maelys_datalog_policy_free(policy)) cleanup_failed = 1; policy = NULL; }
}

static const char *const atoms[] = { ".pdf" };
static const maelys_datalog_domain_t domain = {
    "document_modules", NULL, 0, atoms, sizeof(atoms) / sizeof(atoms[0])
};
static const char source[] =
    "allow(User, Doc) :- user(User), owns(User, Doc), not(blocked(User)), "
    "document_suffix(Doc, \".pdf\").\n";
static const maelys_datalog_fact_t facts[] = {
    { "user", 1, { MAELYS_DATALOG_SYMBOL("alice") } },
    { "user", 1, { MAELYS_DATALOG_SYMBOL("mallory") } },
    { "owns", 2, { MAELYS_DATALOG_SYMBOL("alice"), MAELYS_DATALOG_SYMBOL("roadmap.pdf") } },
    { "owns", 2, { MAELYS_DATALOG_SYMBOL("alice"), MAELYS_DATALOG_SYMBOL("draft.txt") } },
    { "owns", 2, { MAELYS_DATALOG_SYMBOL("mallory"), MAELYS_DATALOG_SYMBOL("roadmap.pdf") } },
    { "blocked", 1, { MAELYS_DATALOG_SYMBOL("mallory") } }
};
/* The filter is pure and bounded. No callback keeps pointers or calls the engine. */
static maelys_datalog_status_t validate_suffix(const unsigned char *pattern, size_t length) {
    if (!pattern || !length || length > 63) return MAELYS_DATALOG_STATUS_INVALID_FIELD;
    return MAELYS_DATALOG_STATUS_OK;
}
static maelys_datalog_status_t suffix_cost(size_t value_length, size_t pattern_length, size_t *out_cost) {
    (void)value_length;
    if (!out_cost) return MAELYS_DATALOG_STATUS_INVALID_ARGUMENT;
    if (pattern_length == SIZE_MAX) return MAELYS_DATALOG_STATUS_PAYLOAD_TOO_LARGE;
    *out_cost = pattern_length + 1;
    return MAELYS_DATALOG_STATUS_OK;
}
static maelys_datalog_status_t evaluate_suffix(const unsigned char *value, size_t value_length,
    const unsigned char *pattern, size_t pattern_length, int *out_matched) {
    if (!value || !pattern || !out_matched) return MAELYS_DATALOG_STATUS_INVALID_ARGUMENT;
    *out_matched = value_length >= pattern_length &&
        memcmp(value + value_length - pattern_length, pattern, pattern_length) == 0;
    return MAELYS_DATALOG_STATUS_OK;
}
static const maelys_datalog_filter_module_t suffix_module = {
    MAELYS_DATALOG_MODULE_ABI_VERSION, sizeof(maelys_datalog_filter_module_t),
    "document_suffix", "example.document-suffix.bytes-v1.cost-pattern-plus-one",
    validate_suffix, suffix_cost, evaluate_suffix
};

static void register_document_filter(void) {
    // 1. Register a byte-wise suffix filter before loading any policy.
    maelys_datalog_status_t status = maelys_datalog_register_filter_module(&suffix_module);
    printf("filter registration: %s\n", maelys_datalog_status_name(status));
    check_status(status);
}
/* The core supplies only safe choices; this callback never reorders the array. */
static maelys_datalog_status_t choose_join(const maelys_datalog_join_candidate_t *candidates,
    size_t candidate_count, size_t *out_candidate_index) {
    if (!candidates || !candidate_count || !out_candidate_index)
        return MAELYS_DATALOG_STATUS_INVALID_ARGUMENT;
    size_t best = 0;
    for (size_t i = 1; i < candidate_count; ++i)
        if (candidates[i].default_score > candidates[best].default_score ||
            (candidates[i].default_score == candidates[best].default_score &&
             candidates[i].body_index < candidates[best].body_index)) best = i;
    *out_candidate_index = best;
    return MAELYS_DATALOG_STATUS_OK;
}
static const maelys_datalog_planner_module_t planner_module = {
    MAELYS_DATALOG_MODULE_ABI_VERSION, sizeof(maelys_datalog_planner_module_t),
    "document_join", "example.document-join.score-then-body-v1", choose_join
};

static void register_document_planner(void) {
    // 2. Choose only among the safe join candidates supplied by the engine.
    maelys_datalog_status_t status = maelys_datalog_register_planner_module(&planner_module);
    printf("planner registration: %s\n", maelys_datalog_status_name(status));
    check_status(status);
}
static const maelys_datalog_predicate_t predicates[] = {
    MAELYS_DATALOG_EDB("user", 1), MAELYS_DATALOG_EDB("owns", 2),
    MAELYS_DATALOG_EDB("blocked", 1), MAELYS_DATALOG_IDB_QUERY("allow", 2)
};
static maelys_datalog_status_t install_document_predicates(maelys_datalog_domain_builder_t *builder) {
    for (size_t i = 0; i < sizeof(predicates) / sizeof(predicates[0]); ++i) {
        maelys_datalog_status_t status = maelys_datalog_domain_builder_add(builder, &predicates[i]);
        if (status != MAELYS_DATALOG_STATUS_OK) return status;
    }
    return MAELYS_DATALOG_STATUS_OK;
}

static void register_document_domain(void) {
    // 3. Defer predicate declarations until a policy loads this named domain.
    maelys_datalog_status_t status = maelys_datalog_domain_register_advanced(&domain,
        "Document access with a native file-suffix filter", install_document_predicates);
    printf("domain registration: %s\n", maelys_datalog_status_name(status));
    check_status(status);
}
static void load_and_inspect_policy(void) {
    // 4. Loading invokes the installer and compiles the custom filter in the rule.
    maelys_datalog_status_t status = maelys_datalog_policy_load_inline(domain.name,
        "document-access", source, strlen(source), &policy, NULL);
    printf("policy loading: %s\n", maelys_datalog_status_name(status));
    check_status(status);
    check_status(maelys_datalog_session_create(policy, 0, &session));
    const maelys_datalog_program_t *program = NULL;
    maelys_datalog_program_info_t info;
    check_status(maelys_datalog_session_program(session, &program));
    check_status(maelys_datalog_program_info(program, &info));
    printf("installed predicates: %zu\n", info.predicate_count);
}
static void query_document_access(void) {
    // 5. Read application decisions; callbacks themselves do not print or retain state.
    maelys_datalog_status_t status = maelys_datalog_session_solve(session, facts,
        sizeof(facts) / sizeof(facts[0]), &result, NULL);
    printf("solving: %s\n", maelys_datalog_status_name(status));
    check_status(status);
    const char *const users[] = { "alice", "alice", "mallory" };
    const char *const documents[] = { "roadmap.pdf", "draft.txt", "roadmap.pdf" };
    for (size_t i = 0; i < sizeof(users) / sizeof(users[0]); ++i) {
        const maelys_datalog_value_t terms[] = {
            MAELYS_DATALOG_SYMBOL(users[i]), MAELYS_DATALOG_SYMBOL(documents[i])
        };
        int present = 0;
        check_status(maelys_datalog_result_query(result, "allow", terms, 2, &present));
        printf("allow(%s, %s): %s\n", users[i], documents[i], present ? "yes" : "no");
    }
}
int main(void) {
    if (atexit(cleanup_context) != 0) return EXIT_FAILURE;
    register_document_filter();
    register_document_planner();
    register_document_domain();
    load_and_inspect_policy();
    query_document_access();
    cleanup_context();
    return cleanup_failed ? EXIT_FAILURE : EXIT_SUCCESS;
}