kleene-datalog
Datalog rules written in Rust as typed values: relations carry their arity in the type (Rel<N>), rules are built from atoms, conditions (lt, ne, within, ...), negation and aggregates, and there is no parser at run time (rules written as text with datalog! are turned into the same calls at build time). The program lowers onto kleene-plan, which plans and runs it (recursive two-column rules as graph kernels, the rest as column joins) and gives a proof for every fact.
Inputs are facts in hand, Arrow columns, DataFusion tables (an X.gar table from kleene-gar), a two-column relation shared as a graph with no copy, or external relations such as index lookups. Part of kleene.
use kleene_datalog::{Engine, Facts, Inputs, Program};
use kleene_sparse::{CpuBackend, CpuDevice};
// reach(F) :- entry(F). reach(G) :- reach(F), calls(F, G).
let mut p = Program::new();
let entry = p.input::<1>("entry");
let calls = p.input::<2>("calls");
let reach = p.derived::<1>("reach");
let (f, g) = (p.var("F"), p.var("G"));
p.rule(reach.at([f]), [entry.at([f])])?;
p.rule(reach.at([g]), [reach.at([f]), calls.at([f, g])])?;
let mut inputs = Inputs::new();
inputs.facts(&entry, Facts::from_rows(1, [[0u32]])?)?;
inputs.facts(&calls, Facts::from_rows(2, [[0u32, 1], [1, 2], [3, 4]])?)?;
// Threads: the rules' joins and the graph kernels run on the device's own workers
// (`CpuDevice::default()` takes every core), not on tokio. Only table reads through
// DataFusion run on the runtime you bring: give it more than one thread.
let engine = Engine::<CpuBackend>::new(CpuDevice::default()).explain(true);
let rt = tokio::runtime::Builder::new_multi_thread().build()?;
let out = rt.block_on(engine.run(&p, &inputs))?;
let mut reached: Vec<u64> = out.get(&reach).unwrap().column(0).iter().collect();
reached.sort_unstable();
assert_eq!(reached, [0, 1, 2]);
// Why does reach(2) hold? A proof down to the input facts.
let why = out.why(&p, &reach, [2u32]).expect("an explained run keeps proofs");
println!("{}", why.render(&p));
# Ok::<(), Box<dyn std::error::Error>>(())
Rules as text: datalog!
The same rules can be written as Datalog text; the datalog! macro turns them into the builder's calls at build time (no parser at run time), so the result is the same Program with the same typed handles. It makes a new program with its relations, or adds rules to a program in hand beside builder rules. A bare name in uppercase (F, K1) is a variable, named as written in the proofs; _ is a column nothing reads; anything else (kind::CALL, code(0), a lowercase local, {FIRST_MAX}) is a Rust value.
use kleene_datalog::{Program, datalog};
let (p, r) = datalog! {
input entry/1, calls/3.
derived reach/1, fan/2.
reach(F) :- entry(F).
reach(G) :- reach(F), calls(F, G, _).
fan(F, N) :- N = count(G) by (F): calls(F, G, W), W > 0, !entry(G).
}?;
assert_eq!(p.rules().len(), 3);
// more rules on a program in hand, on its handles
let mut q = Program::new();
let calls = q.input::<2>("calls");
let reach = q.derived::<1>("reach");
let start = 7u32;
datalog!(&mut q =>
reach(G) :- calls(start, G).
reach(G) :- reach(F), calls(F, G).
)?;
assert_eq!(q.rules().len(), 2);
# let _ = (r.reach, reach);
# Ok::<(), Box<dyn std::error::Error>>(())
Licence
MIT or Apache-2.0, at your option.