C Extension API

Frontends guide

Compile a grant command and evaluate its document-access rule.

An application can accept a small, product-specific policy format while keeping the ordinary Maelys solver and validation. This example implements a frontend for exactly one command, grant alice roadmap.pdf; it emits a static approval fact and a rule that refuses a blocked user. See the Frontends reference for the descriptor and callback contracts.

1. Write the frontend and load one grant command

Write lower_grant to recognize exactly grant alice roadmap.pdf before adding any records. It emits approved(alice, roadmap.pdf) and the allow rule through the builder, propagates a builder error immediately, and rejects an unsupported command; then load one policy with this frontend.

Example
CODE
static const char source[] = "grant alice roadmap.pdf";
static const maelys_datalog_ir_atom_t approved_fact = {
    .predicate = "approved", .arity = 2,
    .terms = {{.kind = MAELYS_DATALOG_IR_SYMBOL, .as.symbol = "alice"},
              {.kind = MAELYS_DATALOG_IR_SYMBOL, .as.symbol = "roadmap.pdf"}}
};
static const maelys_datalog_ir_rule_t allow_rule = {
    .head = {.predicate = "allow", .arity = 2,
        .terms = {{.kind = MAELYS_DATALOG_IR_VARIABLE, .as.variable = 0},
                  {.kind = MAELYS_DATALOG_IR_VARIABLE, .as.variable = 1}}},
    .body_count = 2,
    .body = {
        {.kind = MAELYS_DATALOG_IR_ATOM, .atom = {.predicate = "approved", .arity = 2,
            .terms = {{.kind = MAELYS_DATALOG_IR_VARIABLE, .as.variable = 0},
                      {.kind = MAELYS_DATALOG_IR_VARIABLE, .as.variable = 1}}}},
        {.kind = MAELYS_DATALOG_IR_NEGATION, .atom = {.predicate = "blocked", .arity = 1,
            .terms = {{.kind = MAELYS_DATALOG_IR_VARIABLE, .as.variable = 0}}}}
    }
};
/* Accept one exact command; reject any other input before emitting records. */
static maelys_datalog_status_t lower_grant(const char *text, size_t length,
    maelys_datalog_program_builder_t *builder, maelys_datalog_diagnostic_t *diagnostic) {
    (void)diagnostic;
    if (length != sizeof(source) - 1 || memcmp(text, source, length) != 0)
        return MAELYS_DATALOG_STATUS_INVALID_ARGUMENT;
    // Builder errors propagate; the core still validates the completed program.
    maelys_datalog_status_t status = maelys_datalog_program_add_fact(builder, &approved_fact);
    if (status != MAELYS_DATALOG_STATUS_OK) return status;
    return maelys_datalog_program_add_rule(builder, &allow_rule);
}
static const maelys_datalog_frontend_t frontend = {
    .abi_version = MAELYS_DATALOG_PROGRAM_ABI_VERSION, .struct_size = sizeof(frontend),
    .name = "grant_command", .semantic_id = "example.grant-command.v1", .lower = lower_grant
};

static void load_grant(void) {
    maelys_datalog_status_t status = maelys_datalog_policy_load_frontend(
        "frontend_documents", "document-access", source, strlen(source), &frontend, &policy, &diagnostic);
    printf("load status: %s\n", maelys_datalog_status_name(status));
    check_status(status);
    printf("frontend: %s\n", frontend.name);
}
load status: ok
frontend: grant_command

2. Inspect the compiled fact and rule

The loader copies the records submitted by the callback and validates them against the host-selected domain. Read the compiled program through a live session to confirm the fact and rule that were accepted.

Example
CODE
static void inspect_grant(void) {
    // 2. Core validation has accepted the frontend's fact and rule before returning a policy.
    check_status(maelys_datalog_session_create(policy, 0, &session));
    const maelys_datalog_program_t *program = NULL;
    check_status(maelys_datalog_session_program(session, &program));
    maelys_datalog_program_info_t info;
    check_status(maelys_datalog_program_info(program, &info));
    printf("compiled facts: %zu\n", info.fact_count);
    printf("compiled rules: %zu\n", info.rule_count);
    maelys_datalog_ir_atom_t fact;
    check_status(maelys_datalog_program_fact(program, 0, &fact));
    printf("static grant: %s(%s, %s)\n", fact.predicate, fact.terms[0].as.symbol, fact.terms[1].as.symbol);
}
compiled facts: 1
compiled rules: 1
static grant: approved(alice, roadmap.pdf)

3. Evaluate the policy with ordinary inputs

No user is blocked in the empty input set, so the rule permits alice to open roadmap.pdf. The result and query are the same application-facing APIs used with the built-in Datalog frontend.

Example
CODE
static void query_grant(void) {
    // 3. The ordinary solver can evaluate a policy produced by the custom frontend.
    check_status(maelys_datalog_input_edb_create(&edb));
    check_status(maelys_datalog_session_solve_edb(session, edb, &result, &diagnostic));
    const maelys_datalog_value_t query[] = {MAELYS_DATALOG_SYMBOL("alice"), MAELYS_DATALOG_SYMBOL("roadmap.pdf")};
    int allowed = 0;
    check_status(maelys_datalog_result_query(result, "allow", query, sizeof(query) / sizeof(query[0]), &allowed));
    printf("alice allowed: %s\n", allowed ? "yes" : "no");
}

4. Reject an unsupported command

This frontend accepts only its declared command. A different command returns an error instead of selecting another grammar or returning a partially built policy.

Example
CODE
static void reject_other_command(void) {
    // 4. An unsupported command fails; the loader does not fall back to another frontend.
    maelys_datalog_policy_t *rejected = NULL;
    const char invalid_source[] = "grant anyone everything";
    maelys_datalog_status_t status = maelys_datalog_policy_load_frontend(
        "frontend_documents", "invalid-command", invalid_source, strlen(invalid_source),
        &frontend, &rejected, &diagnostic);
    printf("invalid command: %s\n", maelys_datalog_status_name(status));
    printf("policy returned: %s\n", rejected ? "yes" : "no");
    if (status == MAELYS_DATALOG_STATUS_OK || rejected != NULL) exit(EXIT_FAILURE);
}
invalid command: invalid_argument
policy returned: no

The loader copies the frontend identity and the records emitted by lower. The builder and source bytes are borrowed only during lower; do not retain them or reenter policy loading. A builder error is sticky even if a callback ignores its status.

Reference context

Insert the numbered fragments at API_REFERENCE_EXAMPLE in order. The context contains the host domain, status helper, cleanup and main, which calls the visible functions in order. The command, emitted records and lower_grant are visible above; each displayed step is also executed independently after its earlier setup.

CODE
#include <maelys/datalog.h>
#include <maelys/datalog_builders.h>
#include <maelys/datalog_frontend.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_input_edb_t *edb = 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 (edb) { if (maelys_datalog_input_edb_free(edb)) cleanup_failed = 1; edb = 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 maelys_datalog_diagnostic_t diagnostic;
static void register_domain(void) {
    const maelys_datalog_predicate_t predicates[] = {
        MAELYS_DATALOG_POLICY_FACT("approved", 2),
        MAELYS_DATALOG_EDB("blocked", 1), MAELYS_DATALOG_IDB_QUERY("allow", 2)
    };
    const char *const atoms[] = {"alice", "roadmap.pdf"};
    const maelys_datalog_domain_t domain = MAELYS_DATALOG_DOMAIN_WITH_ATOMS("frontend_documents", predicates, atoms);
    check_status(maelys_datalog_domain_register(&domain));
}
/* API_REFERENCE_EXAMPLE */
int main(void) {
    if (atexit(cleanup_context) != 0) return EXIT_FAILURE;
    diagnostic = MAELYS_DATALOG_DIAGNOSTIC_INIT;
    register_domain();
    load_grant();
    inspect_grant();
    query_grant();
    reject_other_command();
    cleanup_context();
    return cleanup_failed ? EXIT_FAILURE : EXIT_SUCCESS;
}
Full program
CODE
#include <maelys/datalog.h>
#include <maelys/datalog_builders.h>
#include <maelys/datalog_frontend.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_input_edb_t *edb = 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 (edb) { if (maelys_datalog_input_edb_free(edb)) cleanup_failed = 1; edb = 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 maelys_datalog_diagnostic_t diagnostic;
static void register_domain(void) {
    const maelys_datalog_predicate_t predicates[] = {
        MAELYS_DATALOG_POLICY_FACT("approved", 2),
        MAELYS_DATALOG_EDB("blocked", 1), MAELYS_DATALOG_IDB_QUERY("allow", 2)
    };
    const char *const atoms[] = {"alice", "roadmap.pdf"};
    const maelys_datalog_domain_t domain = MAELYS_DATALOG_DOMAIN_WITH_ATOMS("frontend_documents", predicates, atoms);
    check_status(maelys_datalog_domain_register(&domain));
}
static const char source[] = "grant alice roadmap.pdf";
static const maelys_datalog_ir_atom_t approved_fact = {
    .predicate = "approved", .arity = 2,
    .terms = {{.kind = MAELYS_DATALOG_IR_SYMBOL, .as.symbol = "alice"},
              {.kind = MAELYS_DATALOG_IR_SYMBOL, .as.symbol = "roadmap.pdf"}}
};
static const maelys_datalog_ir_rule_t allow_rule = {
    .head = {.predicate = "allow", .arity = 2,
        .terms = {{.kind = MAELYS_DATALOG_IR_VARIABLE, .as.variable = 0},
                  {.kind = MAELYS_DATALOG_IR_VARIABLE, .as.variable = 1}}},
    .body_count = 2,
    .body = {
        {.kind = MAELYS_DATALOG_IR_ATOM, .atom = {.predicate = "approved", .arity = 2,
            .terms = {{.kind = MAELYS_DATALOG_IR_VARIABLE, .as.variable = 0},
                      {.kind = MAELYS_DATALOG_IR_VARIABLE, .as.variable = 1}}}},
        {.kind = MAELYS_DATALOG_IR_NEGATION, .atom = {.predicate = "blocked", .arity = 1,
            .terms = {{.kind = MAELYS_DATALOG_IR_VARIABLE, .as.variable = 0}}}}
    }
};
/* Accept one exact command; reject any other input before emitting records. */
static maelys_datalog_status_t lower_grant(const char *text, size_t length,
    maelys_datalog_program_builder_t *builder, maelys_datalog_diagnostic_t *diagnostic) {
    (void)diagnostic;
    if (length != sizeof(source) - 1 || memcmp(text, source, length) != 0)
        return MAELYS_DATALOG_STATUS_INVALID_ARGUMENT;
    // Builder errors propagate; the core still validates the completed program.
    maelys_datalog_status_t status = maelys_datalog_program_add_fact(builder, &approved_fact);
    if (status != MAELYS_DATALOG_STATUS_OK) return status;
    return maelys_datalog_program_add_rule(builder, &allow_rule);
}
static const maelys_datalog_frontend_t frontend = {
    .abi_version = MAELYS_DATALOG_PROGRAM_ABI_VERSION, .struct_size = sizeof(frontend),
    .name = "grant_command", .semantic_id = "example.grant-command.v1", .lower = lower_grant
};

static void load_grant(void) {
    maelys_datalog_status_t status = maelys_datalog_policy_load_frontend(
        "frontend_documents", "document-access", source, strlen(source), &frontend, &policy, &diagnostic);
    printf("load status: %s\n", maelys_datalog_status_name(status));
    check_status(status);
    printf("frontend: %s\n", frontend.name);
}
static void inspect_grant(void) {
    // 2. Core validation has accepted the frontend's fact and rule before returning a policy.
    check_status(maelys_datalog_session_create(policy, 0, &session));
    const maelys_datalog_program_t *program = NULL;
    check_status(maelys_datalog_session_program(session, &program));
    maelys_datalog_program_info_t info;
    check_status(maelys_datalog_program_info(program, &info));
    printf("compiled facts: %zu\n", info.fact_count);
    printf("compiled rules: %zu\n", info.rule_count);
    maelys_datalog_ir_atom_t fact;
    check_status(maelys_datalog_program_fact(program, 0, &fact));
    printf("static grant: %s(%s, %s)\n", fact.predicate, fact.terms[0].as.symbol, fact.terms[1].as.symbol);
}
static void query_grant(void) {
    // 3. The ordinary solver can evaluate a policy produced by the custom frontend.
    check_status(maelys_datalog_input_edb_create(&edb));
    check_status(maelys_datalog_session_solve_edb(session, edb, &result, &diagnostic));
    const maelys_datalog_value_t query[] = {MAELYS_DATALOG_SYMBOL("alice"), MAELYS_DATALOG_SYMBOL("roadmap.pdf")};
    int allowed = 0;
    check_status(maelys_datalog_result_query(result, "allow", query, sizeof(query) / sizeof(query[0]), &allowed));
    printf("alice allowed: %s\n", allowed ? "yes" : "no");
}
static void reject_other_command(void) {
    // 4. An unsupported command fails; the loader does not fall back to another frontend.
    maelys_datalog_policy_t *rejected = NULL;
    const char invalid_source[] = "grant anyone everything";
    maelys_datalog_status_t status = maelys_datalog_policy_load_frontend(
        "frontend_documents", "invalid-command", invalid_source, strlen(invalid_source),
        &frontend, &rejected, &diagnostic);
    printf("invalid command: %s\n", maelys_datalog_status_name(status));
    printf("policy returned: %s\n", rejected ? "yes" : "no");
    if (status == MAELYS_DATALOG_STATUS_OK || rejected != NULL) exit(EXIT_FAILURE);
}
int main(void) {
    if (atexit(cleanup_context) != 0) return EXIT_FAILURE;
    diagnostic = MAELYS_DATALOG_DIAGNOSTIC_INIT;
    register_domain();
    load_grant();
    inspect_grant();
    query_grant();
    reject_other_command();
    cleanup_context();
    return cleanup_failed ? EXIT_FAILURE : EXIT_SUCCESS;
}