C API overview

Load Datalog rules, supply facts, and consume answers through a C11 interface. Include <relic.h> and link against librelic.

A complete program

Save this as query.c. It prints the symbols reachable from a, one per line.

#include <stdio.h>
#include <stdlib.h>

#include <relic.h>

static int
print_row(void *ctx, const relic_value_t *values, size_t count)
{
	if (count > 0 && values[0].type == RELIC_SYMBOL)
		puts(relic_string(ctx, values[0].v.id, NULL));

	return (0);
}

int
main(void)
{
	static char name[] = "query";
	relic_query_t *query;
	relic_t *ctx;
	int status;

	ctx = relic_create();
	if (ctx == NULL) {
		perror("relic_create");
		return (EXIT_FAILURE);
	}
	relic_diag_set(ctx, relic_diag_stderr, name);

	query = NULL;
	status = relic_load_string(ctx, "graph",
	    "edge(a, b). edge(b, c).\n"
	    "path(X, Y) :- edge(X, Y).\n"
	    "path(X, Z) :- edge(X, Y), path(Y, Z).\n");
	if (status == 0)
		query = relic_query_prepare(ctx, NULL, "path(a, X)?");
	if (query == NULL)
		status = -1;
	else
		status = relic_query_run(ctx, query, print_row, ctx);

	relic_query_destroy(query);
	relic_destroy(ctx);

	if (fflush(stdout) == EOF || ferror(stdout))
		status = -1;

	return (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}

After installing relic and the development tool pkg-config, compile and run:

cc -o query query.c $(pkg-config --cflags --libs librelic)
./query

The answers are b and c. If you installed under $HOME/.local, set PKG_CONFIG_PATH to include $HOME/.local/lib/pkgconfig. Your platform's dynamic loader must also be able to find the installed shared library. Static-only installations use pkg-config --static --libs librelic.

The lifecycle

relic_create()
Creates an independent context. relic_create_config() accepts limits and worker settings.
relic_load_string()
Loads facts and rules from text. Use relic_fact_add() for typed facts supplied by C, or relic_import_file() for a table.
relic_query_prepare()
Prepares exactly one query ending in ?. Queries check and evaluate what they need; you do not have to call relic_materialize() first.
relic_query_run()
Passes each answer row to a callback. Return zero to continue or nonzero to stop successfully.
relic_query_destroy() / relic_destroy()
Release the query and context. Both accept NULL.

Rows and ownership

A row has one column for each named query variable in first-occurrence order. Values passed to the row callback are borrowed for that call. Symbol and string bytes read with relic_string() are borrowed too; copy the bytes if you retain a row.

Use relic_query_collect() for an owned result. Iterate with relic_result_rows() and relic_result_row(); read its symbols and strings with relic_result_string(). A collected result owns its data and can outlive the context. Release it with relic_result_destroy(). Result identifiers belong to that result and cannot be passed back to the context.

Errors and limits

Check return values: operations returning int use zero for success and โˆ’1 for failure; constructors return NULL on failure. Install relic_diag_stderr for escaped terminal diagnostics, or provide a diagnostic callback. Without one, the library writes nothing.

After a failed call, relic_error() supplies human-readable text and relic_error_class() supplies a machine-readable category. A successful call does not clear an earlier error. Context creation failures set errno instead.

Configure max_tuples, max_memory and max_input when handling untrusted workloads. Zero selects the default, unbounded value for these limits. The memory limit accounts for engine storage; it is not a cap on total process memory or peak allocation. relic_interrupt() requests cancellation of evaluation.

Callbacks and threads

Use a context from one calling thread at a time. Separate contexts are independent. Internal evaluation workers do not make the public context safe for concurrent calls. The documented exception is relic_interrupt() with a build that supports C11 atomics.

During callbacks, restrict calls on the active context to the read operations and interrupt operation permitted by the reference. Mutating or destroying that context is refused. Callbacks must return normally.

Reference and further examples

man 3 relic documents the full API. Browse the public header, library manual source, or embedding example for typed facts, configuration and collected results.