Logic programming in Rust
Ascent is a logic programming language (similar to Datalog) embedded in Rust via macros.
For more information, check out the CC paper on Ascent.
In addition, this OOPSLA paper describes the "Bring Your Own Data Structures to Datalog" aspect of Ascent.
Examples
Computing all the connected nodes in a graph
ascent!
Using Ascent
- Install Rust.
- Make a new Rust project:
- Add
ascentas a dependency inCargo.toml:[] = "*" - Write some Ascent code in
main.rs. Here is a complete example:use ascent; ascent! - Run the program:
Features
Lattices
Ascent supports computing fixed points of user-defined lattices. The lattice keyword defines a lattice in Ascent. The type of the final column of a lattice must implement the Lattice trait. A lattice is like a relation, except that when a new lattice fact (v1, v2, ..., v(n-1), vn) is discovered, and a fact (v1, v2, ..., v(n-1), v'n) is already present in the database, vn and v'n are joined together to produce a single fact.
This feature enables writing programs not expressible in Datalog. For example we can use this feature to compute the lengths of shortest paths between nodes in a graph.
ascent!
In this example, Dual<T> is the dual of the lattice T. We use Dual<T> because we are interested in shortest paths, given two path lengths l1 and l2 for any given pair of nodes, we only store min(l1, l2).
Parallel Ascent
Ascent is capable of producing parallel code. The macros ascent_par! and ascent_run_par! produce parallelized code.
Naturally, column types must be Send + Sync to work in parallel Ascent.
Parallel Ascent utilizes rayon, so the parallelism level can be controlled either via rayon's ThreadPoolBuilder or using the RAYON_NUM_THREADS environment variable (see here for more info).
ascent_run!
In addition to ascent!, we provide the ascent_run! macro. Unlike ascent!, this macro evaluates the ascent program when invoked. The main advantage of ascent_run! is that local variables are in scope inside the Ascent program. For example, we can define a function for discovering the (optionally reflexive) transitive closure of a relation like so:
In the above example, we initialize the relation r directly to shorten the program.
BYODS
BYODS (short for Bring Your Own Data Structures to Datalog) is an extension of Ascent that enables relations to be backed by custom data structures. This feature allows improving the algorithmic complexity of Ascent programs by optimizing the data structures used to back relations. For example, a program that requires transitive closure computation of a large graph could improve its performance by choosing a union-find based data structure for the transitive closure relation:
in Cargo.toml:
[]
= "*"
= "*"
use trrel_uf;
ascent!
The #[ds(trrel_uf)] attribute directs the Ascent compiler to use the data structure provider defined in the module trrel_uf for the path relation.
You can find a complete example of BYODS dramatically speeding up Ascent computations here.
See BYODS.MD for more information on BYODS.
Conditions and Generative Clauses
The syntax is designed to be familiar to Rust users. In this example, edge is populated with non-reflexive edges from node. Note that any type that implements Clone + Eq + Hash can be used as a relation column.
ascent!
Negation and Aggregation
Ascent supports stratified negation and aggregation. Aggregators are defined in ascent::aggregators. You can find sum, min, max, count, and mean there.
In the following example, the average grade of students is stored in avg_grade:
use mean;
type Student = u32;
type Course = u32;
type Grade = u16;
ascent!
You can define your own aggregators if the provided aggregators are not sufficient. For example, an aggregator for getting the 2nd highest value of a column can have the following signature:
Aggregators can even be parameterized! For an example of a parameterized aggregator, lookup the definition of percentile in ascent::aggregators.
Macro definitions
It may be useful to define macros that expand to either body items or head items. Ascent allows you to do this.
You can find more about macros in Ascent macros here.
Misc
-
#![measure_rule_times]causes execution times of individual rules to be measured. Example:ascent! let mut prog = default; prog.run; println!;Note that
scc_times_summary()is generated for all Ascent programs. With#![measure_rule_times]it reports execution times of individual rules too. -
With
#![generate_run_timeout], arun_timeoutfunction is generated that stops after the given amount of time. -
The feature
wasm-bindgenallows Ascent programs to run in WASM environments. -
structdeclarations can be added to the top ofascent!definitions. This allows changing the name and visibility of the generated type and introduction of type/lifetime parameters and constraints.ascent!Hint: If you get a "private type ... in public interface (error E0446)" warning, you can fix it by making the generated Ascent type private, as done in the above example.