C Advanced API

Structured explanations

Inspect Why-true steps and Why-false obstacles from one prepared explanation lease.

Use text explanations from the Stable Query API when a human-readable document is enough. The Advanced API exposes structured views of the same prepared explanation for applications that need to inspect a derivation step or a failed candidate rule programmatically. It does not run a second explanation search when reading these records.

Types

Defined on this page

TypePurpose
maelys_datalog_explanation_info_tKind, completeness and record counts.
maelys_datalog_explanation_step_view_tOne Why-true derived fact and its premise range.
maelys_datalog_explanation_premise_view_tWhy-true evidence for one rule-body position.
maelys_datalog_explanation_obstacle_view_tWhy-false candidate, bindings, support and obstacle.
maelys_datalog_explanation_support_view_tOne supporting fact inside a Why-false candidate.

Used from other pages

These types are defined on the linked pages; this page uses them in the roles below.

TypeUse on this pageDefined in
maelys_datalog_prepared_explanation_tInput lease whose records are inspected while live.Stable Querying

Functions

FunctionPurpose
maelys_datalog_prepared_explanation_info()Read counts and completeness before indexing.
maelys_datalog_prepared_explanation_step()Read one Why-true step.
maelys_datalog_prepared_explanation_premise()Read one Why-true premise.
maelys_datalog_prepared_explanation_obstacle()Read one Why-false diagnostic candidate.

Prepare once, inspect while the result is live

First call the stable prepare function with caller-owned explanation storage. Then read info, followed by indices below its reported counts. A Why-true step contains a premise_begin and premise_count identifying its entries in the shared premise array. Why-false supplies diagnostic obstacle records instead. IDs and indices are local to this one explanation, not durable policy identifiers.

C · struct
maelys_datalog_explanation_info_t
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;
Metadata for one prepared Why-true or Why-false document.
Fields
kindmaelys_datalog_explanation_kind_t
Requested Why-true or Why-false kind.
foundint
Whether the queried fact is present.
truncatedint
Whether bounded exploration omitted further detail.
step_countsize_t
Why-true record counts.
premise_countsize_t
Why-true record counts.
false_statusunsigned int
Why-false result classification.
false_summaryunsigned int
Why-false result classification.
query_originunsigned int
Origin classification and reached search limits.
limit_hitsunsigned int
Origin classification and reached search limits.
candidate_rule_countsize_t
Why-false exploration counters.
substitution_countsize_t
Why-false exploration counters.
diagnostic_countsize_t
Why-false exploration counters.
filter_cost_unitssize_t
Why-false exploration counters.
querymaelys_datalog_fact_t
Ground fact that was explained.
C · struct
maelys_datalog_explanation_step_view_t
typedef struct {
    size_t rule_id, premise_begin, premise_count;
    maelys_datalog_fact_t fact;
} maelys_datalog_explanation_step_view_t;
One Why-true rule application and its premise span.
Fields
rule_idsize_t
Rule identifier local to this explanation.
premise_beginsize_t
Range in the explanation premise array.
premise_countsize_t
Range in the explanation premise array.
factmaelys_datalog_fact_t
Derived ground fact for this step.
C · struct
maelys_datalog_explanation_premise_view_t
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;
One positive fact, absence, comparison, filter or aggregate in a Why-true step.
Fields
kindunsigned int
Premise classification, fact origin, body position and optional parent step.
originunsigned int
Premise classification, fact origin, body position and optional parent step.
body_indexunsigned int
Premise classification, fact origin, body position and optional parent step.
parent_stepunsigned int
Premise classification, fact origin, body position and optional parent step.
atommaelys_datalog_ir_atom_t
Ground atom or comparison according to kind.
opmaelys_datalog_ir_comparison_t
Ground atom or comparison according to kind.
lhsmaelys_datalog_value_t
Values meaningful for comparison, aggregate or filter kinds.
rhsmaelys_datalog_value_t
Values meaningful for comparison, aggregate or filter kinds.
filter_valuemaelys_datalog_value_t
Values meaningful for comparison, aggregate or filter kinds.
filter_program_indexsize_t
Filter identity when this is a filter premise.
filter_kindunsigned int
Filter identity when this is a filter premise.
projected_variableunsigned int
Aggregate projection and value when this is an aggregate premise.
aggregate_valueuint32_t
Aggregate projection and value when this is an aggregate premise.
C · struct
maelys_datalog_explanation_support_view_t
typedef struct {
    unsigned body_index, origin;
    maelys_datalog_fact_t fact;
} maelys_datalog_explanation_support_view_t;
Supporting fact already matched by a Why-false candidate.
Fields
body_indexunsigned int
Body position and fact origin.
originunsigned int
Body position and fact origin.
factmaelys_datalog_fact_t
Matched supporting ground fact.
C · struct
maelys_datalog_explanation_obstacle_view_t
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;
One bounded Why-false candidate with bindings, support and the obstacle reached.
Fields
rule_idsize_t
Candidate rule, search depth and target fact.
depthsize_t
Candidate rule, search depth and target fact.
targetmaelys_datalog_fact_t
Candidate rule, search depth and target fact.
bound_variable_maskuint32_t
Bound variables and their candidate values.
substitutionmaelys_datalog_value_t[32]
Bound variables and their candidate values.
support_countsize_t
Positive support found before the obstacle.
supportsmaelys_datalog_explanation_support_view_t[8]
Positive support found before the obstacle.
obstacle_kindunsigned int
Obstacle classification and body position.
obstacle_originunsigned int
Obstacle classification and body position.
body_indexunsigned int
Obstacle classification and body position.
unbound_term_maskunsigned int
Obstacle classification and body position.
patternmaelys_datalog_ir_atom_t
Read only the members relevant to the obstacle kind.
opmaelys_datalog_ir_comparison_t
Read only the members relevant to the obstacle kind.
lhsmaelys_datalog_value_t
Read only the members relevant to the obstacle kind.
rhsmaelys_datalog_value_t
Read only the members relevant to the obstacle kind.
filter_valuemaelys_datalog_value_t
Read only the members relevant to the obstacle kind.
filter_program_indexsize_t
Filter identity for a filter obstacle.
filter_kindunsigned int
Filter identity for a filter obstacle.

Read indexed records

C · function
Read explanation metadata
maelys_datalog_status_t
maelys_datalog_prepared_explanation_info(
const maelys_datalog_prepared_explanation_t *explanation,
maelys_datalog_explanation_info_t *out_info
);
Reads the kind, completeness and record counts of a prepared explanation before the application inspects individual records.
Arguments
explanationconst maelys_datalog_prepared_explanation_t *
Live prepared explanation lease.
out_infomaelys_datalog_explanation_info_t *
Receives metadata and record counts.
Return value
maelys_datalog_status_t

OK for a live reference-backend prepared explanation; UNSUPPORTED for another backend.

C · function
Read a Why-true step
maelys_datalog_status_t
maelys_datalog_prepared_explanation_step(
const maelys_datalog_prepared_explanation_t *explanation,
size_t index,
maelys_datalog_explanation_step_view_t *out_step
);
Reads one Why-true derivation step and the range of premises that support it, without repeating the explanation search.
Arguments
explanationconst maelys_datalog_prepared_explanation_t *
Live prepared Why-true explanation.
indexsize_t
Step index below info.step_count.
out_stepmaelys_datalog_explanation_step_view_t *
Receives this step.
Return value
maelys_datalog_status_t

OK for an existing step; NOT_FOUND for an out-of-range index.

C · function
Read a Why-true premise
maelys_datalog_status_t
maelys_datalog_prepared_explanation_premise(
const maelys_datalog_prepared_explanation_t *explanation,
size_t index,
maelys_datalog_explanation_premise_view_t *out_premise
);
Reads the evidence for one rule-body position in a prepared Why-true explanation.
Arguments
explanationconst maelys_datalog_prepared_explanation_t *
Live prepared Why-true explanation.
indexsize_t
Premise index below info.premise_count.
out_premisemaelys_datalog_explanation_premise_view_t *
Receives this premise.
Return value
maelys_datalog_status_t

OK for an existing premise; NOT_FOUND for an out-of-range index.

C · function
Read a Why-false obstacle
maelys_datalog_status_t
maelys_datalog_prepared_explanation_obstacle(
const maelys_datalog_prepared_explanation_t *explanation,
size_t index,
maelys_datalog_explanation_obstacle_view_t *out_obstacle
);
Reads one diagnostic obstacle record from a prepared Why-false explanation.
Arguments
explanationconst maelys_datalog_prepared_explanation_t *
Live prepared Why-false explanation.
indexsize_t
Diagnostic index below info.diagnostic_count.
out_obstaclemaelys_datalog_explanation_obstacle_view_t *
Receives one candidate and its obstacle.
Return value
maelys_datalog_status_t

OK for an existing obstacle; NOT_FOUND for an out-of-range index.

CODE
static maelys_datalog_status_t inspect_true_steps(
    const maelys_datalog_prepared_explanation_t *explanation) {
    maelys_datalog_explanation_info_t info = {0};
    maelys_datalog_status_t rc = maelys_datalog_prepared_explanation_info(
        explanation, &info);
    if (rc != MAELYS_DATALOG_STATUS_OK) return rc;
    if (info.kind != MAELYS_DATALOG_EXPLAIN_TRUE) return MAELYS_DATALOG_STATUS_INVALID_ARGUMENT;
    for (size_t i = 0; i < info.step_count; ++i) {
        maelys_datalog_explanation_step_view_t step = {0};
        rc = maelys_datalog_prepared_explanation_step(explanation, i, &step);
        if (rc != MAELYS_DATALOG_STATUS_OK) return rc;
        /* step.premise_begin and step.premise_count delimit its evidence. */
    }
    return MAELYS_DATALOG_STATUS_OK;
}

Release the prepared explanation before releasing its result; text pointers in views borrow that live result. Do not place output records inside the explanation arena. Structured access is reference-backend-only; another backend returns UNSUPPORTED rather than a fabricated view. A truncated result is an incomplete bounded exploration, not proof that no other explanation exists.