C API
Structured explanations guide
Read document-access proofs and refusal obstacles as data, one step at a time.A refusal is easier to explain when an application can read its causes as data instead of parsing formatted text. This guide uses a document-access policy to show alice's successful proof and mallory's failed rule attempts through the Structured explanations reference.
For the meaning and bounds of this evidence, read Explanations. The Language reference defines statuses, obstacle names and canonical document records. The Querying guide covers caller-owned preparation and optional session-workspace reuse; this guide walks the already prepared evidence.
Policy and inputs
Both users own roadmap.pdf and are declared as users; only mallory is blocked. The policy also contains the static administrator fact for alice, so the refusal search must consider the administrator rule as well as ownership.
admin("alice").
owner_of(User, Doc) :- owns(User, Doc).
allow(User, Doc) :- owner_of(User, Doc), user(User), not(blocked(User)).
allow(User, Doc) :- admin(User), user(User), owns(User, Doc).
The context registers the domain, loads this policy, adds the five runtime facts, solves once, asks both explanation kinds for their storage requirements and allocates the larger workspace. Sizes and alignments are obtained at runtime, never hard-coded or printed. These structured readers use the reference backend in v0.26.0; another backend can return UNSUPPORTED even when it supports text explanations.
1. Prepare the successful decision
Prepare a Why-true explanation for allow(alice, roadmap.pdf) in the caller-owned workspace allocated by the context. The preparation and its _diagnosed variant are documented on Querying; keep the result and workspace alive until the lease is released.
{
// 1. Prepare the successful decision for alice in caller-owned storage.
maelys_datalog_status_t status = maelys_datalog_result_prepare_explanation(
result, MAELYS_DATALOG_EXPLAIN_TRUE, "allow", alice, 2,
storage, storage_bytes, &prepared);
printf("alice preparation: %s\n", maelys_datalog_status_name(status));
check_status(status);
}alice preparation: ok
2. Read the proof summary and derived facts
The information record says whether a proof was found and whether it is truncated. Read each derived fact through its step record: this gives an application the conclusions it can display to its user.
{
// 2. Read the proof summary, then every derived step.
maelys_datalog_explanation_info_t info;
maelys_datalog_status_t status = maelys_datalog_prepared_explanation_info(prepared, &info);
printf("information: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("alice allowed: %s\n", info.found ? "yes" : "no");
printf("proof truncated: %s\n", info.truncated ? "yes" : "no");
for (size_t i = 0; i < info.step_count; ++i) {
maelys_datalog_explanation_step_view_t step;
check_status(maelys_datalog_prepared_explanation_step(prepared, i, &step));
fputs("derived: ", stdout); print_fact(&step.fact); putchar('\n');
}
}information: ok alice allowed: yes proof truncated: no derived: owner_of(alice, roadmap.pdf) derived: allow(alice, roadmap.pdf)
3. Read the premises of each step
Each step gives a range in the complete premise list. Read that range to distinguish input facts, derived support and a checked absence; a negated absence is a successful condition, not a fact that exists in the input.
{
// 3. Each step identifies a contiguous range in the proof's premise list.
maelys_datalog_explanation_info_t info;
check_status(maelys_datalog_prepared_explanation_info(prepared, &info));
for (size_t i = 0; i < info.step_count; ++i) {
maelys_datalog_explanation_step_view_t step;
check_status(maelys_datalog_prepared_explanation_step(prepared, i, &step));
fputs("for: ", stdout); print_fact(&step.fact); putchar('\n');
for (size_t j = 0; j < step.premise_count; ++j) {
maelys_datalog_explanation_premise_view_t premise;
check_status(maelys_datalog_prepared_explanation_premise(prepared, step.premise_begin + j, &premise));
printf(" %s (%s): ", premise_name(premise.kind), origin_name(premise.origin));
print_atom(&premise.atom); putchar('\n');
}
}
}for: owner_of(alice, roadmap.pdf) positive fact (input): owns(alice, roadmap.pdf) for: allow(alice, roadmap.pdf) positive fact (derived): owner_of(alice, roadmap.pdf) positive fact (input): user(alice) negated absence (input): blocked(alice)
4. Prepare the refusal with diagnostics
Release the first lease before reusing its storage for a Why-false request about mallory. A successful _diagnosed call clears the supplied diagnostic. Read false_status to check whether the bounded refusal search was truncated; a completed search still records one obstacle per failed attempt, not every possible failed condition.
{
// 4. End the first lease before preparing mallory's refusal in the same storage.
check_status(maelys_datalog_prepared_explanation_release(prepared));
prepared = NULL;
maelys_datalog_diagnostic_t diagnostic = MAELYS_DATALOG_DIAGNOSTIC_INIT;
maelys_datalog_status_t status = maelys_datalog_result_prepare_explanation_diagnosed(
result, MAELYS_DATALOG_EXPLAIN_FALSE, "allow", mallory, 2,
storage, storage_bytes, &prepared, &diagnostic);
printf("mallory preparation: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("diagnostic.status: %s\n", maelys_datalog_status_name(diagnostic.status));
maelys_datalog_explanation_info_t info;
check_status(maelys_datalog_prepared_explanation_info(prepared, &info));
printf("mallory allowed: %s\n", info.found ? "yes" : "no");
printf("refusal complete: %s\n", info.false_status == MAELYS_DATALOG_WHY_FALSE_STATUS_COMPLETE ? "yes" : "no");
}mallory preparation: ok diagnostic.status: ok mallory allowed: no refusal complete: yes
5. Inspect obstacles and check the refusal
Each obstacle describes one failed rule attempt. Here the ownership rule is defeated by blocked(mallory), while the administrator rule has no admin(mallory) fact; inspect all retained obstacles rather than treating the first one as the only reason. The application checks for the blocking condition in the actual returned records.
{
// 5. Inspect every obstacle and check whether blocked(mallory) defeated a negation.
maelys_datalog_explanation_info_t info;
check_status(maelys_datalog_prepared_explanation_info(prepared, &info));
int blocked_refusal = 0;
for (size_t i = 0; i < info.diagnostic_count; ++i) {
maelys_datalog_explanation_obstacle_view_t obstacle;
check_status(maelys_datalog_prepared_explanation_obstacle(prepared, i, &obstacle));
printf("obstacle: %s (%s): ", obstacle_name(obstacle.obstacle_kind), origin_name(obstacle.obstacle_origin));
print_atom(&obstacle.pattern); putchar('\n');
if (obstacle.obstacle_kind == MAELYS_DATALOG_WHY_FALSE_OBSTACLE_NEGATIVE_CONTRADICTED &&
strcmp(obstacle.pattern.predicate, "blocked") == 0 && obstacle.pattern.arity == 1 &&
obstacle.pattern.terms[0].kind == MAELYS_DATALOG_IR_SYMBOL &&
strcmp(obstacle.pattern.terms[0].as.symbol, "mallory") == 0) blocked_refusal = 1;
}
printf("blocked refusal detected: %s\n", blocked_refusal ? "yes" : "no");
if (!blocked_refusal) return EXIT_FAILURE;
}obstacle: negation contradicted (input): blocked(mallory) obstacle: missing fact (no source): admin(mallory) blocked refusal detected: yes
The readers copy each record into your structure, but the names and values in it still point into the result: keep the result and the prepared explanation alive while you read them, and release the explanation before reusing its storage.
Reference context
Insert the five numbered fragments at API_REFERENCE_EXAMPLE. Each individual step uses the same context with its preceding explanation prepared silently; the complete program runs the displayed fragments in order.
#include <maelys/datalog.h>
#include <maelys/datalog_builders.h>
#include <maelys/datalog_explanations.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.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 maelys_datalog_prepared_explanation_t *prepared = NULL;
static void *storage = NULL;
static size_t storage_bytes = 0;
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 (prepared) { if (maelys_datalog_prepared_explanation_release(prepared)) cleanup_failed = 1; prepared = NULL; }
free(storage); storage = NULL;
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 const char source[] =
"admin(\"alice\").\n"
"owner_of(User, Doc) :- owns(User, Doc).\n"
"allow(User, Doc) :- owner_of(User, Doc), user(User), not(blocked(User)).\n"
"allow(User, Doc) :- admin(User), user(User), owns(User, Doc).\n";
static const maelys_datalog_value_t alice[] = { MAELYS_DATALOG_SYMBOL("alice"), MAELYS_DATALOG_SYMBOL("roadmap.pdf") };
static const maelys_datalog_value_t mallory[] = { MAELYS_DATALOG_SYMBOL("mallory"), MAELYS_DATALOG_SYMBOL("roadmap.pdf") };
static void print_value(const maelys_datalog_value_t *value) {
switch (value->kind) {
case MAELYS_DATALOG_VALUE_SYMBOL: fputs(value->as.symbol, stdout); break;
case MAELYS_DATALOG_VALUE_INTEGER: printf("%" PRId64, value->as.integer); break;
case MAELYS_DATALOG_VALUE_BOOLEAN: fputs(value->as.boolean ? "true" : "false", stdout); break;
default: fputs("Unexpected value kind\n", stderr); exit(EXIT_FAILURE);
}
}
static void print_fact(const maelys_datalog_fact_t *fact) {
printf("%s(", fact->predicate);
for (size_t i = 0; i < fact->arity; ++i) { if (i) fputs(", ", stdout); print_value(&fact->terms[i]); }
putchar(')');
}
static void print_atom(const maelys_datalog_ir_atom_t *atom) {
printf("%s(", atom->predicate);
for (size_t i = 0; i < atom->arity; ++i) {
if (i) fputs(", ", stdout);
const maelys_datalog_ir_term_t *term = &atom->terms[i];
switch (term->kind) {
case MAELYS_DATALOG_IR_SYMBOL: fputs(term->as.symbol, stdout); break;
case MAELYS_DATALOG_IR_INTEGER: printf("%" PRId64, term->as.integer); break;
case MAELYS_DATALOG_IR_BOOLEAN: fputs(term->as.boolean ? "true" : "false", stdout); break;
case MAELYS_DATALOG_IR_VARIABLE: printf("V%" PRIu32, term->as.variable); break;
default: exit(EXIT_FAILURE);
}
}
putchar(')');
}
static const char *origin_name(unsigned origin) {
switch (origin) {
case MAELYS_DATALOG_EXPLANATION_ORIGIN_POLICY_FACT: return "policy fact";
case MAELYS_DATALOG_EXPLANATION_ORIGIN_EDB: return "input";
case MAELYS_DATALOG_EXPLANATION_ORIGIN_IDB: return "derived";
case MAELYS_DATALOG_EXPLANATION_ORIGIN_NOT_APPLICABLE: return "no source";
default: return "unknown";
}
}
static const char *premise_name(unsigned kind) {
switch (kind) {
case MAELYS_DATALOG_EXPLANATION_PREMISE_POSITIVE_FACT: return "positive fact";
case MAELYS_DATALOG_EXPLANATION_PREMISE_NEGATED_ABSENCE: return "negated absence";
default: return "other premise";
}
}
static const char *obstacle_name(unsigned kind) {
switch (kind) {
case MAELYS_DATALOG_WHY_FALSE_OBSTACLE_POSITIVE_NO_MATCH: return "missing fact";
case MAELYS_DATALOG_WHY_FALSE_OBSTACLE_NEGATIVE_CONTRADICTED: return "negation contradicted";
default: return "other obstacle";
}
}
static void prepare_silently(maelys_datalog_explanation_kind_t kind, const maelys_datalog_value_t *terms) {
check_status(maelys_datalog_result_prepare_explanation(result, kind, "allow", terms, 2,
storage, storage_bytes, &prepared));
}
int main(void) {
if (atexit(cleanup_context) != 0) return EXIT_FAILURE;
const maelys_datalog_predicate_t predicates[] = {
MAELYS_DATALOG_EDB("user", 1), MAELYS_DATALOG_EDB("owns", 2),
MAELYS_DATALOG_EDB("blocked", 1), MAELYS_DATALOG_POLICY_FACT("admin", 1),
MAELYS_DATALOG_IDB("owner_of", 2), MAELYS_DATALOG_IDB_QUERY("allow", 2)
};
const char *const atoms[] = { "alice" };
const maelys_datalog_domain_t domain = MAELYS_DATALOG_DOMAIN_WITH_ATOMS("documents", predicates, atoms);
check_status(maelys_datalog_domain_register(&domain));
maelys_datalog_diagnostic_t diagnostic = MAELYS_DATALOG_DIAGNOSTIC_INIT;
check_status(maelys_datalog_policy_load_inline("documents", "document-access", source, strlen(source), &policy, &diagnostic));
check_status(maelys_datalog_session_create(policy, 0, &session));
check_status(maelys_datalog_input_edb_create(&edb));
check_status(MAELYS_DATALOG_ADD_FACT(edb, &diagnostic, "user", "alice"));
check_status(MAELYS_DATALOG_ADD_FACT(edb, &diagnostic, "user", "mallory"));
check_status(MAELYS_DATALOG_ADD_FACT(edb, &diagnostic, "owns", "alice", "roadmap.pdf"));
check_status(MAELYS_DATALOG_ADD_FACT(edb, &diagnostic, "owns", "mallory", "roadmap.pdf"));
check_status(MAELYS_DATALOG_ADD_FACT(edb, &diagnostic, "blocked", "mallory"));
check_status(maelys_datalog_session_solve_edb(session, edb, &result, &diagnostic));
size_t true_bytes = 0, false_bytes = 0, true_alignment = 0, false_alignment = 0;
check_status(maelys_datalog_result_explanation_storage_requirements(result, MAELYS_DATALOG_EXPLAIN_TRUE, &true_bytes, &true_alignment));
check_status(maelys_datalog_result_explanation_storage_requirements(result, MAELYS_DATALOG_EXPLAIN_FALSE, &false_bytes, &false_alignment));
if (true_alignment > _Alignof(max_align_t) || false_alignment > _Alignof(max_align_t)) return EXIT_FAILURE;
storage_bytes = true_bytes > false_bytes ? true_bytes : false_bytes;
storage = malloc(storage_bytes);
if (!storage) return EXIT_FAILURE;
{
/* API_REFERENCE_EXAMPLE */
}
cleanup_context();
return cleanup_failed ? EXIT_FAILURE : EXIT_SUCCESS;
}Full program
The focused part is exactly the five displayed fragments in order, inserted into the reference context. Cleanup releases the final lease before its workspace, result and session.
#include <maelys/datalog.h>
#include <maelys/datalog_builders.h>
#include <maelys/datalog_explanations.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.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 maelys_datalog_prepared_explanation_t *prepared = NULL;
static void *storage = NULL;
static size_t storage_bytes = 0;
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 (prepared) { if (maelys_datalog_prepared_explanation_release(prepared)) cleanup_failed = 1; prepared = NULL; }
free(storage); storage = NULL;
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 const char source[] =
"admin(\"alice\").\n"
"owner_of(User, Doc) :- owns(User, Doc).\n"
"allow(User, Doc) :- owner_of(User, Doc), user(User), not(blocked(User)).\n"
"allow(User, Doc) :- admin(User), user(User), owns(User, Doc).\n";
static const maelys_datalog_value_t alice[] = { MAELYS_DATALOG_SYMBOL("alice"), MAELYS_DATALOG_SYMBOL("roadmap.pdf") };
static const maelys_datalog_value_t mallory[] = { MAELYS_DATALOG_SYMBOL("mallory"), MAELYS_DATALOG_SYMBOL("roadmap.pdf") };
static void print_value(const maelys_datalog_value_t *value) {
switch (value->kind) {
case MAELYS_DATALOG_VALUE_SYMBOL: fputs(value->as.symbol, stdout); break;
case MAELYS_DATALOG_VALUE_INTEGER: printf("%" PRId64, value->as.integer); break;
case MAELYS_DATALOG_VALUE_BOOLEAN: fputs(value->as.boolean ? "true" : "false", stdout); break;
default: fputs("Unexpected value kind\n", stderr); exit(EXIT_FAILURE);
}
}
static void print_fact(const maelys_datalog_fact_t *fact) {
printf("%s(", fact->predicate);
for (size_t i = 0; i < fact->arity; ++i) { if (i) fputs(", ", stdout); print_value(&fact->terms[i]); }
putchar(')');
}
static void print_atom(const maelys_datalog_ir_atom_t *atom) {
printf("%s(", atom->predicate);
for (size_t i = 0; i < atom->arity; ++i) {
if (i) fputs(", ", stdout);
const maelys_datalog_ir_term_t *term = &atom->terms[i];
switch (term->kind) {
case MAELYS_DATALOG_IR_SYMBOL: fputs(term->as.symbol, stdout); break;
case MAELYS_DATALOG_IR_INTEGER: printf("%" PRId64, term->as.integer); break;
case MAELYS_DATALOG_IR_BOOLEAN: fputs(term->as.boolean ? "true" : "false", stdout); break;
case MAELYS_DATALOG_IR_VARIABLE: printf("V%" PRIu32, term->as.variable); break;
default: exit(EXIT_FAILURE);
}
}
putchar(')');
}
static const char *origin_name(unsigned origin) {
switch (origin) {
case MAELYS_DATALOG_EXPLANATION_ORIGIN_POLICY_FACT: return "policy fact";
case MAELYS_DATALOG_EXPLANATION_ORIGIN_EDB: return "input";
case MAELYS_DATALOG_EXPLANATION_ORIGIN_IDB: return "derived";
case MAELYS_DATALOG_EXPLANATION_ORIGIN_NOT_APPLICABLE: return "no source";
default: return "unknown";
}
}
static const char *premise_name(unsigned kind) {
switch (kind) {
case MAELYS_DATALOG_EXPLANATION_PREMISE_POSITIVE_FACT: return "positive fact";
case MAELYS_DATALOG_EXPLANATION_PREMISE_NEGATED_ABSENCE: return "negated absence";
default: return "other premise";
}
}
static const char *obstacle_name(unsigned kind) {
switch (kind) {
case MAELYS_DATALOG_WHY_FALSE_OBSTACLE_POSITIVE_NO_MATCH: return "missing fact";
case MAELYS_DATALOG_WHY_FALSE_OBSTACLE_NEGATIVE_CONTRADICTED: return "negation contradicted";
default: return "other obstacle";
}
}
static void prepare_silently(maelys_datalog_explanation_kind_t kind, const maelys_datalog_value_t *terms) {
check_status(maelys_datalog_result_prepare_explanation(result, kind, "allow", terms, 2,
storage, storage_bytes, &prepared));
}
int main(void) {
if (atexit(cleanup_context) != 0) return EXIT_FAILURE;
const maelys_datalog_predicate_t predicates[] = {
MAELYS_DATALOG_EDB("user", 1), MAELYS_DATALOG_EDB("owns", 2),
MAELYS_DATALOG_EDB("blocked", 1), MAELYS_DATALOG_POLICY_FACT("admin", 1),
MAELYS_DATALOG_IDB("owner_of", 2), MAELYS_DATALOG_IDB_QUERY("allow", 2)
};
const char *const atoms[] = { "alice" };
const maelys_datalog_domain_t domain = MAELYS_DATALOG_DOMAIN_WITH_ATOMS("documents", predicates, atoms);
check_status(maelys_datalog_domain_register(&domain));
maelys_datalog_diagnostic_t diagnostic = MAELYS_DATALOG_DIAGNOSTIC_INIT;
check_status(maelys_datalog_policy_load_inline("documents", "document-access", source, strlen(source), &policy, &diagnostic));
check_status(maelys_datalog_session_create(policy, 0, &session));
check_status(maelys_datalog_input_edb_create(&edb));
check_status(MAELYS_DATALOG_ADD_FACT(edb, &diagnostic, "user", "alice"));
check_status(MAELYS_DATALOG_ADD_FACT(edb, &diagnostic, "user", "mallory"));
check_status(MAELYS_DATALOG_ADD_FACT(edb, &diagnostic, "owns", "alice", "roadmap.pdf"));
check_status(MAELYS_DATALOG_ADD_FACT(edb, &diagnostic, "owns", "mallory", "roadmap.pdf"));
check_status(MAELYS_DATALOG_ADD_FACT(edb, &diagnostic, "blocked", "mallory"));
check_status(maelys_datalog_session_solve_edb(session, edb, &result, &diagnostic));
size_t true_bytes = 0, false_bytes = 0, true_alignment = 0, false_alignment = 0;
check_status(maelys_datalog_result_explanation_storage_requirements(result, MAELYS_DATALOG_EXPLAIN_TRUE, &true_bytes, &true_alignment));
check_status(maelys_datalog_result_explanation_storage_requirements(result, MAELYS_DATALOG_EXPLAIN_FALSE, &false_bytes, &false_alignment));
if (true_alignment > _Alignof(max_align_t) || false_alignment > _Alignof(max_align_t)) return EXIT_FAILURE;
storage_bytes = true_bytes > false_bytes ? true_bytes : false_bytes;
storage = malloc(storage_bytes);
if (!storage) return EXIT_FAILURE;
{
{
// 1. Prepare the successful decision for alice in caller-owned storage.
maelys_datalog_status_t status = maelys_datalog_result_prepare_explanation(
result, MAELYS_DATALOG_EXPLAIN_TRUE, "allow", alice, 2,
storage, storage_bytes, &prepared);
printf("alice preparation: %s\n", maelys_datalog_status_name(status));
check_status(status);
}
{
// 2. Read the proof summary, then every derived step.
maelys_datalog_explanation_info_t info;
maelys_datalog_status_t status = maelys_datalog_prepared_explanation_info(prepared, &info);
printf("information: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("alice allowed: %s\n", info.found ? "yes" : "no");
printf("proof truncated: %s\n", info.truncated ? "yes" : "no");
for (size_t i = 0; i < info.step_count; ++i) {
maelys_datalog_explanation_step_view_t step;
check_status(maelys_datalog_prepared_explanation_step(prepared, i, &step));
fputs("derived: ", stdout); print_fact(&step.fact); putchar('\n');
}
}
{
// 3. Each step identifies a contiguous range in the proof's premise list.
maelys_datalog_explanation_info_t info;
check_status(maelys_datalog_prepared_explanation_info(prepared, &info));
for (size_t i = 0; i < info.step_count; ++i) {
maelys_datalog_explanation_step_view_t step;
check_status(maelys_datalog_prepared_explanation_step(prepared, i, &step));
fputs("for: ", stdout); print_fact(&step.fact); putchar('\n');
for (size_t j = 0; j < step.premise_count; ++j) {
maelys_datalog_explanation_premise_view_t premise;
check_status(maelys_datalog_prepared_explanation_premise(prepared, step.premise_begin + j, &premise));
printf(" %s (%s): ", premise_name(premise.kind), origin_name(premise.origin));
print_atom(&premise.atom); putchar('\n');
}
}
}
{
// 4. End the first lease before preparing mallory's refusal in the same storage.
check_status(maelys_datalog_prepared_explanation_release(prepared));
prepared = NULL;
maelys_datalog_diagnostic_t diagnostic = MAELYS_DATALOG_DIAGNOSTIC_INIT;
maelys_datalog_status_t status = maelys_datalog_result_prepare_explanation_diagnosed(
result, MAELYS_DATALOG_EXPLAIN_FALSE, "allow", mallory, 2,
storage, storage_bytes, &prepared, &diagnostic);
printf("mallory preparation: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("diagnostic.status: %s\n", maelys_datalog_status_name(diagnostic.status));
maelys_datalog_explanation_info_t info;
check_status(maelys_datalog_prepared_explanation_info(prepared, &info));
printf("mallory allowed: %s\n", info.found ? "yes" : "no");
printf("refusal complete: %s\n", info.false_status == MAELYS_DATALOG_WHY_FALSE_STATUS_COMPLETE ? "yes" : "no");
}
{
// 5. Inspect every obstacle and check whether blocked(mallory) defeated a negation.
maelys_datalog_explanation_info_t info;
check_status(maelys_datalog_prepared_explanation_info(prepared, &info));
int blocked_refusal = 0;
for (size_t i = 0; i < info.diagnostic_count; ++i) {
maelys_datalog_explanation_obstacle_view_t obstacle;
check_status(maelys_datalog_prepared_explanation_obstacle(prepared, i, &obstacle));
printf("obstacle: %s (%s): ", obstacle_name(obstacle.obstacle_kind), origin_name(obstacle.obstacle_origin));
print_atom(&obstacle.pattern); putchar('\n');
if (obstacle.obstacle_kind == MAELYS_DATALOG_WHY_FALSE_OBSTACLE_NEGATIVE_CONTRADICTED &&
strcmp(obstacle.pattern.predicate, "blocked") == 0 && obstacle.pattern.arity == 1 &&
obstacle.pattern.terms[0].kind == MAELYS_DATALOG_IR_SYMBOL &&
strcmp(obstacle.pattern.terms[0].as.symbol, "mallory") == 0) blocked_refusal = 1;
}
printf("blocked refusal detected: %s\n", blocked_refusal ? "yes" : "no");
if (!blocked_refusal) return EXIT_FAILURE;
}
}
cleanup_context();
return cleanup_failed ? EXIT_FAILURE : EXIT_SUCCESS;
}Reference
Structured explanations · Preparing explanations · Diagnosed preparation · Program guide.