C API

Structured explanations

Inspect prepared proofs, supporting premises and refusal obstacles as records.

Read a prepared explanation as records to show why a document was allowed or why an attempted rule failed. The Structured explanations guide shows how to use result_prepare_explanation() and its diagnosed variant, then inspect the records for alice and mallory.

Handle: prepared_explanation_t ↗

Functions

prepared_explanation_info()

C · functionSince v0.10.0
prepared_explanation_info()

#include <maelys/datalog_explanations.h>

maelys_datalog_status_t
maelys_datalog_prepared_explanation_info(
const maelys_datalog_prepared_explanation_t *prepared,
maelys_datalog_explanation_info_t *out_info
);
Read the query outcome, completeness and record counts of a prepared explanation.
Arguments
preparedconst maelys_datalog_prepared_explanation_t *handleread
Live prepared explanation whose result and caller-owned workspace remain alive for the entire read.
Outputs

Written on success.

out_infomaelys_datalog_explanation_info_t *out
Receives the explanation outcome and record counts; must not overlap the explanation workspace.
Return status

maelys_datalog_status_t — OK on success.

Returns OK with the requested record. An invalid or expired lease fails; NULL output or output overlapping the workspace returns INVALID_ARGUMENT. A backend without structured views returns UNSUPPORTED.

Example
maelys_datalog_explanation_info_t info;
maelys_datalog_status_t status = maelys_datalog_prepared_explanation_info(prepared, &info);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("proof found: %s\n", info.found ? "yes" : "no");
status: ok
proof found: yes

prepared_explanation_obstacle()

C · functionSince v0.10.0
prepared_explanation_obstacle()

#include <maelys/datalog_explanations.h>

maelys_datalog_status_t
maelys_datalog_prepared_explanation_obstacle(
const maelys_datalog_prepared_explanation_t *prepared,
size_t index,
maelys_datalog_explanation_obstacle_view_t *out_obstacle
);
Read one failed rule attempt and its supporting facts and obstacle in a Why-false explanation.
Arguments
preparedconst maelys_datalog_prepared_explanation_t *handleread
Live prepared explanation whose result and caller-owned workspace remain alive for the entire read.
indexsize_tin
Zero-based obstacle index, strictly less than the Why-false diagnostic_count.
Outputs

Written on success.

out_obstaclemaelys_datalog_explanation_obstacle_view_t *out
Receives the failed rule attempt and obstacle; must not overlap the explanation workspace.
Return status

maelys_datalog_status_t — OK on success.

Returns OK with the requested record. An invalid or expired lease fails; NULL output or output overlapping the workspace returns INVALID_ARGUMENT. A backend without structured views returns UNSUPPORTED. A record index outside its count returns NOT_FOUND. An explanation of the wrong kind returns INVALID_ARGUMENT.

Example
check_status(maelys_datalog_prepared_explanation_release(prepared));
prepared = NULL;
prepare_silently(MAELYS_DATALOG_EXPLAIN_FALSE, mallory);
maelys_datalog_explanation_obstacle_view_t obstacle;
maelys_datalog_status_t status = maelys_datalog_prepared_explanation_obstacle(prepared, 0, &obstacle);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("obstacle: %s\n", obstacle_name(obstacle.obstacle_kind));
status: ok
obstacle: negation contradicted

prepared_explanation_premise()

C · functionSince v0.10.0
prepared_explanation_premise()

#include <maelys/datalog_explanations.h>

maelys_datalog_status_t
maelys_datalog_prepared_explanation_premise(
const maelys_datalog_prepared_explanation_t *prepared,
size_t index,
maelys_datalog_explanation_premise_view_t *out_premise
);
Read one supporting fact, checked absence, comparison, filter or aggregate in a Why-true proof.
Arguments
preparedconst maelys_datalog_prepared_explanation_t *handleread
Live prepared explanation whose result and caller-owned workspace remain alive for the entire read.
indexsize_tin
Zero-based premise index in the complete Why-true premise list, strictly less than premise_count.
Outputs

Written on success.

out_premisemaelys_datalog_explanation_premise_view_t *out
Receives the selected premise and active operands; must not overlap the explanation workspace.
Return status

maelys_datalog_status_t — OK on success.

Returns OK with the requested record. An invalid or expired lease fails; NULL output or output overlapping the workspace returns INVALID_ARGUMENT. A backend without structured views returns UNSUPPORTED. A record index outside its count returns NOT_FOUND. An explanation of the wrong kind returns INVALID_ARGUMENT.

Example
maelys_datalog_explanation_premise_view_t premise;
maelys_datalog_status_t status = maelys_datalog_prepared_explanation_premise(prepared, 0, &premise);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("premise: %s (%s)\n", premise_name(premise.kind), origin_name(premise.origin));
status: ok
premise: positive fact (input)

prepared_explanation_step()

C · functionSince v0.10.0
prepared_explanation_step()

#include <maelys/datalog_explanations.h>

maelys_datalog_status_t
maelys_datalog_prepared_explanation_step(
const maelys_datalog_prepared_explanation_t *prepared,
size_t index,
maelys_datalog_explanation_step_view_t *out_step
);
Read one derived fact and the range of premises supporting it in a Why-true proof.
Arguments
preparedconst maelys_datalog_prepared_explanation_t *handleread
Live prepared explanation whose result and caller-owned workspace remain alive for the entire read.
indexsize_tin
Zero-based step index, strictly less than the Why-true step_count.
Outputs

Written on success.

out_stepmaelys_datalog_explanation_step_view_t *out
Receives the derived fact and its premise range; must not overlap the explanation workspace.
Return status

maelys_datalog_status_t — OK on success.

Returns OK with the requested record. An invalid or expired lease fails; NULL output or output overlapping the workspace returns INVALID_ARGUMENT. A backend without structured views returns UNSUPPORTED. A record index outside its count returns NOT_FOUND. An explanation of the wrong kind returns INVALID_ARGUMENT.

Example
maelys_datalog_explanation_step_view_t step;
maelys_datalog_status_t status = maelys_datalog_prepared_explanation_step(prepared, 0, &step);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
fputs("derived: ", stdout); print_fact(&step.fact); putchar('\n');
status: ok
derived: owner_of(alice, roadmap.pdf)

Struct: explanation_info_t

C · structSince v0.10.0
explanation_info_t

#include <maelys/datalog_explanations.h>

Outcome, completeness and record counts of a prepared Why-true or Why-false explanation.
Fields
foundint
For Why-true, nonzero when a proof was found; for Why-false, nonzero when the query is already true and refusal analysis is not applicable.
truncatedint
Nonzero when the retained explanation is incomplete.
step_countsize_t
Number of Why-true steps available to prepared_explanation_step().
premise_countsize_t
Number of Why-true premises available to prepared_explanation_premise().
candidate_rule_countsize_t
Number of candidate rules examined during Why-false analysis.
substitution_countsize_t
Number of substitutions examined during Why-false analysis.
diagnostic_countsize_t
Number of retained Why-false obstacles available to prepared_explanation_obstacle().
filter_cost_unitssize_t
Filter evaluation cost units consumed by Why-false analysis.
querymaelys_datalog_fact_t
Resolved Why-false query fact; the Why-true information record does not populate this field.
Typedef
typedef struct {
    maelys_datalog_explanation_kind_t kind;
    int found, truncated;
    size_t step_count, premise_count;
    unsigned false_status, false_summary, query_origin, limit_hits;
    size_t candidate_rule_count, substitution_count, diagnostic_count, filter_cost_units;
    maelys_datalog_fact_t query;
} maelys_datalog_explanation_info_t;

Struct: explanation_step_view_t

C · structSince v0.10.0
explanation_step_view_t

#include <maelys/datalog_explanations.h>

A derived fact and the contiguous range of premises supporting its rule application.
Fields
rule_idsize_t
One-based normalized rule number; subtract one to read that rule with program_rule().
premise_beginsize_t
Zero-based first premise index for this step in the complete Why-true premise list.
premise_countsize_t
Number of consecutive premises belonging to this step, beginning at premise_begin.
factmaelys_datalog_fact_t
Derived fact established by this proof step; its text borrows the live result.
Typedef
typedef struct {
    size_t rule_id, premise_begin, premise_count;
    maelys_datalog_fact_t fact;
} maelys_datalog_explanation_step_view_t;

Struct: explanation_premise_view_t

C · structSince v0.10.0
explanation_premise_view_t

#include <maelys/datalog_explanations.h>

One Why-true premise, with the operands selected by its kind.
Fields
body_indexunsigned int
Zero-based position of the premise or obstacle in its normalized rule body.
atommaelys_datalog_ir_atom_t
Resolved fact, checked absence or aggregate source atom, selected by the premise kind.
opmaelys_datalog_ir_comparison_t
Comparison operator when the premise or obstacle compares values.
lhsmaelys_datalog_value_t
Resolved left operand for comparison or aggregate conditions.
rhsmaelys_datalog_value_t
Resolved right operand for comparison or aggregate conditions.
filter_valuemaelys_datalog_value_t
Resolved value passed to the filter for a filter premise or obstacle.
filter_program_indexsize_t
Index of the filter program when the active kind is a filter.
filter_kindunsigned int
Filter kind recorded for a filter premise or obstacle.
projected_variableunsigned int
Variable number projected by an aggregate premise; local to this explanation.
aggregate_valueuint32_t
Computed value of the aggregate premise.
Typedef
typedef struct {
    unsigned kind, origin, body_index, parent_step;
    maelys_datalog_ir_atom_t atom; /* ground fact, absence, or aggregate source */
    maelys_datalog_ir_comparison_t op;
    maelys_datalog_value_t lhs, rhs, filter_value;
    size_t filter_program_index;
    unsigned filter_kind, projected_variable;
    uint32_t aggregate_value;
} maelys_datalog_explanation_premise_view_t;

Constants

Parent-step sentinel

EXPLANATION_NO_STEP

C · constantSince v0.10.0
EXPLANATION_NO_STEP
#define MAELYS_DATALOG_EXPLANATION_NO_STEP UINT16_MAX

Sentinel indicating that a premise has no supporting derived step.

Value

65535

Struct: explanation_support_view_t

C · structSince v0.10.0
explanation_support_view_t

#include <maelys/datalog_explanations.h>

A fact that supported a body literal before a Why-false rule attempt failed.
Fields
body_indexunsigned int
Zero-based position of the premise or obstacle in its normalized rule body.
factmaelys_datalog_fact_t
Fact supporting this body condition before the candidate rule failed; its text borrows the live result.
Typedef
typedef struct {
    unsigned body_index, origin;
    maelys_datalog_fact_t fact;
} maelys_datalog_explanation_support_view_t;

Struct: explanation_obstacle_view_t

C · structSince v0.10.0
explanation_obstacle_view_t

#include <maelys/datalog_explanations.h>

A failed rule attempt, its bound variables and supports, and the obstacle that stopped it.
Fields
rule_idsize_t
One-based normalized rule number; subtract one to read that rule with program_rule().
depthsize_t
Depth of the failed rule attempt in the Why-false search.
targetmaelys_datalog_fact_t
Resolved fact the failed rule attempt tried to establish.
bound_variable_maskuint32_t
Bit i indicates whether substitution[i] holds a bound variable value.
substitutionmaelys_datalog_value_t[32]
Resolved values for variables whose bits are set in bound_variable_mask; other entries are inactive.
support_countsize_t
Number of active records in supports.
supportsmaelys_datalog_explanation_support_view_t[8]
Body facts supporting the failed rule attempt, up to support_count.
body_indexunsigned int
Zero-based position of the premise or obstacle in its normalized rule body.
unbound_term_maskunsigned int
Bit i marks an unbound term at position i in the obstacle pattern.
patternmaelys_datalog_ir_atom_t
Obstacle fact pattern; terms marked in unbound_term_mask are not ground values.
opmaelys_datalog_ir_comparison_t
Comparison operator when the premise or obstacle compares values.
lhsmaelys_datalog_value_t
Resolved left operand for comparison or aggregate conditions.
rhsmaelys_datalog_value_t
Resolved right operand for comparison or aggregate conditions.
filter_valuemaelys_datalog_value_t
Resolved value passed to the filter for a filter premise or obstacle.
filter_program_indexsize_t
Index of the filter program when the active kind is a filter.
filter_kindunsigned int
Filter kind recorded for a filter premise or obstacle.
Typedef
typedef struct {
    size_t rule_id, depth;
    maelys_datalog_fact_t target;
    uint32_t bound_variable_mask;
    maelys_datalog_value_t substitution[MAELYS_DATALOG_IR_MAX_VARIABLES];
    size_t support_count;
    maelys_datalog_explanation_support_view_t supports[MAELYS_DATALOG_IR_MAX_BODY];
    unsigned obstacle_kind, obstacle_origin, body_index, unbound_term_mask;
    maelys_datalog_ir_atom_t pattern;
    maelys_datalog_ir_comparison_t op;
    maelys_datalog_value_t lhs, rhs, filter_value;
    size_t filter_program_index;
    unsigned filter_kind;
} maelys_datalog_explanation_obstacle_view_t;

Enum: explanation_premise_kind_t

C · enumSince v0.10.0
explanation_premise_kind_t

#include <maelys/datalog.h>

Selects the meaning and active operands of a Why-true premise.
Members
EXPLANATION_PREMISE_POSITIVE_FACT1
A positive body fact was found.
EXPLANATION_PREMISE_NEGATED_ABSENCE2
A negated body fact was absent.
EXPLANATION_PREMISE_COMPARISON_TRUE3
The comparison succeeded.
EXPLANATION_PREMISE_FILTER_TRUE4
The filter matched.
EXPLANATION_PREMISE_COUNT5
A grouped count premise.
EXPLANATION_PREMISE_MIN6
A grouped minimum premise.
EXPLANATION_PREMISE_MAX7
A grouped maximum premise.
EXPLANATION_PREMISE_SUM8
A grouped sum premise.
Typedef
typedef enum {
    MAELYS_DATALOG_EXPLANATION_PREMISE_POSITIVE_FACT = 1,
    MAELYS_DATALOG_EXPLANATION_PREMISE_NEGATED_ABSENCE = 2,
    MAELYS_DATALOG_EXPLANATION_PREMISE_COMPARISON_TRUE = 3,
    MAELYS_DATALOG_EXPLANATION_PREMISE_FILTER_TRUE = 4,
    MAELYS_DATALOG_EXPLANATION_PREMISE_COUNT = 5,
    MAELYS_DATALOG_EXPLANATION_PREMISE_MIN = 6,
    MAELYS_DATALOG_EXPLANATION_PREMISE_MAX = 7,
    MAELYS_DATALOG_EXPLANATION_PREMISE_SUM = 8
} maelys_datalog_explanation_premise_kind_t;

Enum: explanation_origin_t

C · enumSince v0.10.0
explanation_origin_t

#include <maelys/datalog.h>

Identifies whether a fact comes from policy data, runtime inputs or derived results.
Members
EXPLANATION_ORIGIN_POLICY_FACT1
A static fact loaded with the policy.
EXPLANATION_ORIGIN_EDB2
A runtime input fact.
EXPLANATION_ORIGIN_IDB3
A derived fact.
EXPLANATION_ORIGIN_NOT_APPLICABLE4
No applicable fact origin.
Typedef
typedef enum {
    MAELYS_DATALOG_EXPLANATION_ORIGIN_POLICY_FACT = 1,
    MAELYS_DATALOG_EXPLANATION_ORIGIN_EDB = 2,
    MAELYS_DATALOG_EXPLANATION_ORIGIN_IDB = 3,
    MAELYS_DATALOG_EXPLANATION_ORIGIN_NOT_APPLICABLE = 4
} maelys_datalog_explanation_origin_t;

Enum: why_false_status_t

C · enumSince v0.10.0
why_false_status_t

#include <maelys/datalog.h>

Distinguishes a complete refusal, a truncated search and an inapplicable Why-false request.
Members
WHY_FALSE_STATUS_NOT_APPLICABLE1
The queried fact is already true, so Why-false analysis does not apply.
WHY_FALSE_STATUS_COMPLETE2
The Why-false search completed within its limits.
WHY_FALSE_STATUS_TRUNCATED3
The Why-false search reached a limit and is incomplete.
Typedef
typedef enum {
    MAELYS_DATALOG_WHY_FALSE_STATUS_NOT_APPLICABLE = 1,
    MAELYS_DATALOG_WHY_FALSE_STATUS_COMPLETE = 2,
    MAELYS_DATALOG_WHY_FALSE_STATUS_TRUNCATED = 3
} maelys_datalog_why_false_status_t;

Enum: why_false_summary_t

C · enumSince v0.10.0
why_false_summary_t

#include <maelys/datalog.h>

Indicates whether the refusal search found any candidate rule.
Members
WHY_FALSE_SUMMARY_NONE0
No special refusal summary.
WHY_FALSE_SUMMARY_NO_CANDIDATE_RULE1
No rule could produce this query.
Typedef
typedef enum {
    MAELYS_DATALOG_WHY_FALSE_SUMMARY_NONE = 0,
    MAELYS_DATALOG_WHY_FALSE_SUMMARY_NO_CANDIDATE_RULE = 1
} maelys_datalog_why_false_summary_t;

Enum: why_false_obstacle_kind_t

C · enumSince v0.10.0
why_false_obstacle_kind_t

#include <maelys/datalog.h>

Identifies the failed condition recorded for a candidate rule.
Members
WHY_FALSE_OBSTACLE_POSITIVE_NO_MATCH1
A positive body atom has no matching fact.
WHY_FALSE_OBSTACLE_NEGATIVE_CONTRADICTED2
A negated body atom has a contradicting fact.
WHY_FALSE_OBSTACLE_COMPARISON_FALSE3
A comparison failed.
WHY_FALSE_OBSTACLE_RECURSIVE_NO_BASE_SUPPORT4
Recursion has no base support for the target.
WHY_FALSE_OBSTACLE_FILTER_FALSE5
A filter did not match.
WHY_FALSE_OBSTACLE_COUNT_MISMATCH6
The count does not satisfy the condition.
WHY_FALSE_OBSTACLE_MIN_MISMATCH7
The minimum does not satisfy the condition.
WHY_FALSE_OBSTACLE_MAX_MISMATCH8
The maximum does not satisfy the condition.
WHY_FALSE_OBSTACLE_SUM_MISMATCH9
The sum does not satisfy the condition.
WHY_FALSE_OBSTACLE_MIN_EMPTY10
The minimum has no source fact.
WHY_FALSE_OBSTACLE_MAX_EMPTY11
The maximum has no source fact.
Typedef
typedef enum {
    MAELYS_DATALOG_WHY_FALSE_OBSTACLE_POSITIVE_NO_MATCH = 1,
    MAELYS_DATALOG_WHY_FALSE_OBSTACLE_NEGATIVE_CONTRADICTED = 2,
    MAELYS_DATALOG_WHY_FALSE_OBSTACLE_COMPARISON_FALSE = 3,
    MAELYS_DATALOG_WHY_FALSE_OBSTACLE_RECURSIVE_NO_BASE_SUPPORT = 4,
    MAELYS_DATALOG_WHY_FALSE_OBSTACLE_FILTER_FALSE = 5,
    MAELYS_DATALOG_WHY_FALSE_OBSTACLE_COUNT_MISMATCH = 6,
    MAELYS_DATALOG_WHY_FALSE_OBSTACLE_MIN_MISMATCH = 7,
    MAELYS_DATALOG_WHY_FALSE_OBSTACLE_MAX_MISMATCH = 8,
    MAELYS_DATALOG_WHY_FALSE_OBSTACLE_SUM_MISMATCH = 9,
    MAELYS_DATALOG_WHY_FALSE_OBSTACLE_MIN_EMPTY = 10,
    MAELYS_DATALOG_WHY_FALSE_OBSTACLE_MAX_EMPTY = 11
} maelys_datalog_why_false_obstacle_kind_t;

Enum: why_false_limit_t

C · enumSince v0.10.0
why_false_limit_t

#include <maelys/datalog.h>

Bit flags identifying which bounded Why-false searches reached a limit.
Members
WHY_FALSE_LIMIT_CANDIDATE_RULES1
Candidate-rule search limit reached.
WHY_FALSE_LIMIT_SUBSTITUTIONS2
Substitution search limit reached.
WHY_FALSE_LIMIT_DEPTH4
Search depth limit reached.
WHY_FALSE_LIMIT_DIAGNOSTICS8
Retained obstacle limit reached.
WHY_FALSE_LIMIT_FILTER_COST16
Filter evaluation cost limit reached.
Typedef
typedef enum {
    MAELYS_DATALOG_WHY_FALSE_LIMIT_CANDIDATE_RULES = 1u << 0,
    MAELYS_DATALOG_WHY_FALSE_LIMIT_SUBSTITUTIONS = 1u << 1,
    MAELYS_DATALOG_WHY_FALSE_LIMIT_DEPTH = 1u << 2,
    MAELYS_DATALOG_WHY_FALSE_LIMIT_DIAGNOSTICS = 1u << 3,
    MAELYS_DATALOG_WHY_FALSE_LIMIT_FILTER_COST = 1u << 4
} maelys_datalog_why_false_limit_t;

Usage guides