C API

Domain

Declare and register a closed domain through the stable opaque C API.

Declarations from <maelys/datalog.h> and its optional C11 builders <maelys/datalog_builders.h>. For explanations and complete examples, see the Registries guide.

Struct: domain_t

C · structSince v0.10.0
domain_t

#include <maelys/datalog.h>

The domain declaration supplied when registering a policy vocabulary.
Fields
nameconst char *
Name used to select this domain when a policy is loaded.
Array of predicate declarations: names, arities and flags.
predicate_countsize_t
Number of entries in predicates.
atomsconst char *const *
Optional array of literal strings allowed in policy source.
atom_countsize_t
Number of entries in atoms.
Typedef
typedef struct {
    const char *name;
    const maelys_datalog_predicate_t *predicates;
    size_t predicate_count;
    const char *const *atoms;
    size_t atom_count;
} maelys_datalog_domain_t;
Example
/* predicates is the document-access declaration array in the Registries guide. */
const maelys_datalog_domain_t domain = {
    "documents", predicates,
    sizeof(predicates) / sizeof(predicates[0]), NULL, 0u,
};
printf("domain: %s\n", domain.name);
printf("predicates: %zu, atoms: %zu\n", domain.predicate_count, domain.atom_count);
domain: documents
predicates: 7, atoms: 0

Creation

DOMAIN_NO_ATOMS()

C · macroSince v0.10.0
DOMAIN_NO_ATOMS()

#include <maelys/datalog_builders.h>

MAELYS_DATALOG_DOMAIN_NO_ATOMS(
name,
predicates
)

Declares a domain when policy source contains no quoted symbolic constants.

Arguments
nameconst char *
Name identifying the registered domain.
predicatesconst maelys_datalog_predicate_t[N] array
A nonempty fixed-size array; a pointer cannot supply its length via sizeof.
Effect

Uses sizeof to count the predicate array and declares zero policy-source atoms. Registration remains a separate call.

Example
const maelys_datalog_domain_t domain =
    MAELYS_DATALOG_DOMAIN_NO_ATOMS("documents", predicates);
printf("domain: %s\n", domain.name);
printf("predicates: %zu, atoms: %zu\n", domain.predicate_count, domain.atom_count);
domain: documents
predicates: 7, atoms: 0

DOMAIN_WITH_ATOMS()

C · macroSince v0.10.0
DOMAIN_WITH_ATOMS()

#include <maelys/datalog_builders.h>

MAELYS_DATALOG_DOMAIN_WITH_ATOMS(
name,
predicates,
atoms
)

Declares a domain with an explicit array of allowed policy-source atoms.

Arguments
nameconst char *
Name identifying the registered domain.
predicatesconst maelys_datalog_predicate_t[N] array
A nonempty fixed-size predicate array, not a pointer.
atomsconst char *const [N] array
A nonempty fixed-size array of policy-source strings, not a pointer.
Effect

Uses sizeof to count both arrays. It declares allowed policy-source atoms but does not add facts or register the domain.

Example
const char *const atoms[] = {"mallory"};
const maelys_datalog_domain_t domain =
    MAELYS_DATALOG_DOMAIN_WITH_ATOMS("documents", predicates, atoms);
printf("domain: %s\n", domain.name);
printf("predicates: %zu, atoms: %zu\n", domain.predicate_count, domain.atom_count);
domain: documents
predicates: 7, atoms: 1

Functions

domain_register()

Register a domain.

C · functionSince ≤ v0.1.0-alpha.2
domain_register()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_domain_register(
const maelys_datalog_domain_t *domain
);
Registers a domain: its predicate names, arities, permissions and allowed policy-source atoms. Policies can then be loaded against this vocabulary; registration does not load or evaluate a policy.
Arguments
domainconst maelys_datalog_domain_t *in
Domain declaration to register; its name links later policy loads to this vocabulary.
Return status

maelys_datalog_status_t — OK on success.

A public error status on failure. Do not load a policy for a domain whose registration failed.

Example
/* domain contains the document-access declarations in the Registries guide. */
maelys_datalog_status_t status = maelys_datalog_domain_register(&domain);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
/* Load a policy only after registration succeeds. */

Struct: predicate_t

C · structSince v0.10.0
predicate_t

#include <maelys/datalog.h>

One predicate in the domain vocabulary, with a fixed name, arity and set of permissions.
Fields
nameconst char *
Predicate name used by policy rules, facts and queries, such as owns.
aritysize_t
Number of terms in each fact or call; owns(User, Document) has arity 2.
flagsunsigned int
Bitmask declaring the permitted fact origin and optional query permission; the combinations are explained in the Registries guide.
Typedef
typedef struct {
    const char *name;
    size_t arity;
    unsigned flags;
} maelys_datalog_predicate_t;
Example
const maelys_datalog_predicate_t owns = {
    "owns", 2u, MAELYS_DATALOG_PREDICATE_EDB,
};
printf("predicate: %s\n", owns.name);
printf("arity: %zu, flags: 0x%x\n", owns.arity, owns.flags);
predicate: owns
arity: 2, flags: 0x1

Creation

EDB()

C · macroSince v0.4.0
EDB()

#include <maelys/datalog_builders.h>

MAELYS_DATALOG_EDB(
name,
arity
)

Declares a runtime-input predicate.

Arguments
nameconst char *
Predicate name used in policy source and request facts.
aritysize_t
Number of terms (0–4) in each fact of this predicate.
Effect

Constructs a predicate declaration with EDB origin. It does not insert a fact or register the domain.

Example
static const maelys_datalog_predicate_t predicates[] = {
    MAELYS_DATALOG_EDB("owns", 2),
};
printf("predicate: %s\n", predicates[0].name);
printf("arity: %zu, flags: 0x%x\n", predicates[0].arity, predicates[0].flags);
predicate: owns
arity: 2, flags: 0x1

EDB_QUERY()

C · macroSince v0.4.0
EDB_QUERY()

#include <maelys/datalog_builders.h>

MAELYS_DATALOG_EDB_QUERY(
name,
arity
)

Declares a runtime-input predicate that may also be queried.

Arguments
nameconst char *
Queryable runtime-input predicate name.
aritysize_t
Number of terms (0–4) in each fact of this predicate.
Effect

Constructs an EDB-origin declaration with query permission. It does not insert or query a fact.

Example
static const maelys_datalog_predicate_t predicates[] = {
    MAELYS_DATALOG_EDB_QUERY("owns", 2),
};
printf("predicate: %s\n", predicates[0].name);
printf("arity: %zu, flags: 0x%x\n", predicates[0].arity, predicates[0].flags);
predicate: owns
arity: 2, flags: 0x5

IDB()

C · macroSince v0.4.0
IDB()

#include <maelys/datalog_builders.h>

MAELYS_DATALOG_IDB(
name,
arity
)

Declares a relation derived by policy rules.

Arguments
nameconst char *
Name of a relation derived by rules.
aritysize_t
Number of terms (0–4) in each fact of this predicate.
Effect

Constructs an IDB-origin declaration for rule-derived facts. It does not evaluate rules.

Example
static const maelys_datalog_predicate_t predicates[] = {
    MAELYS_DATALOG_IDB("can_read", 2),
};
printf("predicate: %s\n", predicates[0].name);
printf("arity: %zu, flags: 0x%x\n", predicates[0].arity, predicates[0].flags);
predicate: can_read
arity: 2, flags: 0x2

IDB_QUERY()

C · macroSince v0.4.0
IDB_QUERY()

#include <maelys/datalog_builders.h>

MAELYS_DATALOG_IDB_QUERY(
name,
arity
)

Declares a derived relation that may also be queried.

Arguments
nameconst char *
Name of a queryable relation derived by rules.
aritysize_t
Number of terms (0–4) in each fact of this predicate.
Effect

Constructs an IDB-origin declaration with query permission. It does not run a query.

Example
static const maelys_datalog_predicate_t predicates[] = {
    MAELYS_DATALOG_IDB_QUERY("allow", 2),
};
printf("predicate: %s\n", predicates[0].name);
printf("arity: %zu, flags: 0x%x\n", predicates[0].arity, predicates[0].flags);
predicate: allow
arity: 2, flags: 0x6

POLICY_FACT()

C · macroSince v0.4.0
POLICY_FACT()

#include <maelys/datalog_builders.h>

MAELYS_DATALOG_POLICY_FACT(
name,
arity
)

Declares a predicate whose ground facts come from policy source.

Arguments
nameconst char *
Name of a predicate populated by policy-source facts.
aritysize_t
Number of terms (0–4) in each fact of this predicate.
Effect

Constructs a POLICY_FACT-origin declaration. The policy source supplies its facts when loaded.

Example
static const maelys_datalog_predicate_t predicates[] = {
    MAELYS_DATALOG_POLICY_FACT("trusted", 1),
};
printf("predicate: %s\n", predicates[0].name);
printf("arity: %zu, flags: 0x%x\n", predicates[0].arity, predicates[0].flags);
predicate: trusted
arity: 1, flags: 0x8

POLICY_FACT_QUERY()

C · macroSince v0.4.0
POLICY_FACT_QUERY()

#include <maelys/datalog_builders.h>

MAELYS_DATALOG_POLICY_FACT_QUERY(
name,
arity
)

Declares policy-source facts that may also be queried.

Arguments
nameconst char *
Name of a queryable policy-source-fact predicate.
aritysize_t
Number of terms (0–4) in each fact of this predicate.
Effect

Constructs a POLICY_FACT-origin declaration with query permission. It does not add a source fact.

Example
static const maelys_datalog_predicate_t predicates[] = {
    MAELYS_DATALOG_POLICY_FACT_QUERY("trusted", 1),
};
printf("predicate: %s\n", predicates[0].name);
printf("arity: %zu, flags: 0x%x\n", predicates[0].arity, predicates[0].flags);
predicate: trusted
arity: 1, flags: 0xc

Enum: predicate_flags_t

C · enumSince ≤ v0.1.0-alpha.2
predicate_flags_t

#include <maelys/datalog.h>

Predicate-origin and query-permission bits used when declaring a domain. Combine one origin with QUERY when membership queries are required.
Members
PREDICATE_EDB1
Runtime facts may be supplied for this predicate.
PREDICATE_IDB2
Rules may derive this predicate.
PREDICATE_QUERY4
The predicate may be queried; combine it with an origin, never use it alone.
PREDICATE_POLICY_FACT8
Direct facts may be written in policy source.
Typedef
typedef enum {
    MAELYS_DATALOG_PREDICATE_EDB = 1u << 0,
    MAELYS_DATALOG_PREDICATE_IDB = 1u << 1,
    MAELYS_DATALOG_PREDICATE_QUERY = 1u << 2,
    MAELYS_DATALOG_PREDICATE_POLICY_FACT = 1u << 3
} maelys_datalog_predicate_flags_t;

Usage guides

Registries guide · Full program.