Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fmt #54

Merged
merged 2 commits into from
Nov 11, 2024
Merged

Fmt #54

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions ascent/examples/ascent_agg_clause.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//! Aggregate `agg` clause

use ascent::{aggregators::{count, max, mean, min, sum}, ascent};
use ascent::aggregators::{count, max, mean, min, sum};
use ascent::ascent;

ascent! {
// Facts:

relation number(i32);

// Rules:

relation lowest(i32);

lowest(y) <-- agg y = min(x) in number(x);
Expand All @@ -31,17 +32,17 @@ ascent! {
}

fn main() {
let mut prog = AscentProgram::default();
prog.number = (1..=5).map(|n| (n,)).collect();
let mut prog = AscentProgram::default();

prog.number = (1..=5).map(|n| (n,)).collect();

prog.run();
prog.run();

let AscentProgram { lowest, greatest, average, total, cardinality, ..} = prog;
let AscentProgram { lowest, greatest, average, total, cardinality, .. } = prog;

assert_eq!(lowest, vec![(1,)]);
assert_eq!(greatest, vec![(5,)]);
assert_eq!(average, vec![(3,)]);
assert_eq!(total, vec![(15,)]);
assert_eq!(cardinality, vec![(5,)]);
assert_eq!(lowest, vec![(1,)]);
assert_eq!(greatest, vec![(5,)]);
assert_eq!(average, vec![(3,)]);
assert_eq!(total, vec![(15,)]);
assert_eq!(cardinality, vec![(5,)]);
}
26 changes: 9 additions & 17 deletions ascent/examples/ascent_disjunction_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ ascent! {
// Facts:

relation number(i32);

// Rules:

relation square(i32);

square(y * y) <-- number(y), number(y * y);
Expand All @@ -23,23 +23,15 @@ ascent! {
}

fn main() {
let mut prog = AscentProgram::default();
prog.number = (1..=10).map(|n| (n,)).collect();
let mut prog = AscentProgram::default();

prog.number = (1..=10).map(|n| (n,)).collect();

prog.run();
prog.run();

let AscentProgram { mut even_or_square, ..} = prog;
let AscentProgram { mut even_or_square, .. } = prog;

even_or_square.sort_by_key(|(key,)| *key);
even_or_square.sort_by_key(|(key,)| *key);

assert_eq!(even_or_square, vec![
(1,),
(2,),
(4,),
(6,),
(8,),
(9,),
(10,),
]);
assert_eq!(even_or_square, vec![(1,), (2,), (4,), (6,), (8,), (9,), (10,),]);
}
36 changes: 13 additions & 23 deletions ascent/examples/ascent_for_in_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,22 @@ ascent! {
relation seed(i32);

// Rules:

relation number(i32);

number(x + y) <-- seed(x), for y in 0..3;
}

fn main() {
let mut prog = AscentProgram::default();

prog.seed = vec![
(0,),
(10,),
];

prog.run();

let AscentProgram { mut number, ..} = prog;

number.sort_by_key(|(key,)| *key);

assert_eq!(number, vec![
(0,),
(1,),
(2,),
(10,),
(11,),
(12,),
]);
let mut prog = AscentProgram::default();

prog.seed = vec![(0,), (10,)];

prog.run();

let AscentProgram { mut number, .. } = prog;

number.sort_by_key(|(key,)| *key);

assert_eq!(number, vec![(0,), (1,), (2,), (10,), (11,), (12,),]);
}
41 changes: 15 additions & 26 deletions ascent/examples/ascent_generic_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Node(&'static str);

ascent! {
struct AscentProgram<N> where N: Clone + Eq + Hash;

// Facts:

relation node(N);
Expand All @@ -26,29 +26,18 @@ ascent! {
}

fn main() {
let mut prog: AscentProgram<Node> = AscentProgram::default();

prog.node = vec![
(Node("A"),),
(Node("B"),),
(Node("C"),),
];

prog.edge = vec![
(Node("A"), Node("B")),
(Node("B"), Node("C")),
];

prog.run();

let AscentProgram { mut reachable, ..} = prog;

reachable.sort_by_key(|(_, key)| key.0);
reachable.sort_by_key(|(key, _)| key.0);

assert_eq!(reachable, vec![
(Node("A"), Node("B")),
(Node("A"), Node("C")),
(Node("B"), Node("C")),
]);
let mut prog: AscentProgram<Node> = AscentProgram::default();

prog.node = vec![(Node("A"),), (Node("B"),), (Node("C"),)];

prog.edge = vec![(Node("A"), Node("B")), (Node("B"), Node("C"))];

prog.run();

let AscentProgram { mut reachable, .. } = prog;

reachable.sort_by_key(|(_, key)| key.0);
reachable.sort_by_key(|(key, _)| key.0);

assert_eq!(reachable, vec![(Node("A"), Node("B")), (Node("A"), Node("C")), (Node("B"), Node("C")),]);
}
31 changes: 12 additions & 19 deletions ascent/examples/ascent_if_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,32 @@ ascent! {
// Facts:

relation number(isize);

// Rules:

relation even(isize);

even(x) <-- number(x), if x % 2 == 0;

relation odd(isize);

odd(x) <-- number(x), if x % 2 != 0;
}

fn main() {
let mut prog = AscentProgram::default();
prog.number = (1..=5).map(|n| (n,)).collect();
let mut prog = AscentProgram::default();

prog.number = (1..=5).map(|n| (n,)).collect();

prog.run();
prog.run();

let AscentProgram { mut even, mut odd, ..} = prog;
let AscentProgram { mut even, mut odd, .. } = prog;

even.sort_by_key(|(key,)| *key);
even.sort_by_key(|(key,)| *key);

assert_eq!(even, vec![
(2,),
(4,),
]);
assert_eq!(even, vec![(2,), (4,),]);

odd.sort_by_key(|(key,)| *key);
odd.sort_by_key(|(key,)| *key);

assert_eq!(odd, vec![
(1,),
(3,),
(5,),
]);
assert_eq!(odd, vec![(1,), (3,), (5,),]);
}
35 changes: 13 additions & 22 deletions ascent/examples/ascent_if_let_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ ascent! {
// Facts:

relation option(Option<isize>);

// Rules:

relation some(isize);

some(y) <-- option(x), if let Some(y) = x;
Expand All @@ -19,24 +19,15 @@ ascent! {
}

fn main() {
let mut prog = AscentProgram::default();

prog.option = vec![
(None,),
(Some(1),),
(Some(2),),
(Some(3),),
];

prog.run();

let AscentProgram { mut some, ..} = prog;

some.sort_by_key(|(key,)| *key);

assert_eq!(some, vec![
(1,),
(2,),
(3,),
]);
let mut prog = AscentProgram::default();

prog.option = vec![(None,), (Some(1),), (Some(2),), (Some(3),)];

prog.run();

let AscentProgram { mut some, .. } = prog;

some.sort_by_key(|(key,)| *key);

assert_eq!(some, vec![(1,), (2,), (3,),]);
}
52 changes: 23 additions & 29 deletions ascent/examples/ascent_lattice.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! Aggregate `agg` clause

use ascent::{ascent, Dual};
use ascent::{Dual, ascent};

pub type Node = &'static str;

ascent! {
// Facts:

relation edge(Node, Node, u32);

// Rules:
Expand All @@ -15,35 +15,29 @@ ascent! {

shortest_path(x, y, Dual(*w)) <-- edge(x, y, w);

shortest_path(x, z, Dual(w + l)) <--
edge(x, y, w),
shortest_path(x, z, Dual(w + l)) <--
edge(x, y, w),
shortest_path(y, z, ?Dual(l));
}

fn main() {
let mut prog = AscentProgram::default();

prog.edge = vec![
("A", "B", 1),
("A", "D", 4),
("B", "C", 1),
("B", "D", 1),
("C", "D", 2),
];

prog.run();

let AscentProgram { mut shortest_path, ..} = prog;

shortest_path.sort_by_key(|(_, key, _)| *key);
shortest_path.sort_by_key(|(key, _, _)| *key);

assert_eq!(shortest_path, vec![
("A", "B", Dual(1)),
("A", "C", Dual(2)),
("A", "D", Dual(2)),
("B", "C", Dual(1)),
("B", "D", Dual(1)),
("C", "D", Dual(2)),
]);
let mut prog = AscentProgram::default();

prog.edge = vec![("A", "B", 1), ("A", "D", 4), ("B", "C", 1), ("B", "D", 1), ("C", "D", 2)];

prog.run();

let AscentProgram { mut shortest_path, .. } = prog;

shortest_path.sort_by_key(|(_, key, _)| *key);
shortest_path.sort_by_key(|(key, _, _)| *key);

assert_eq!(shortest_path, vec![
("A", "B", Dual(1)),
("A", "C", Dual(2)),
("A", "D", Dual(2)),
("B", "C", Dual(1)),
("B", "D", Dual(1)),
("C", "D", Dual(2)),
]);
}
Loading
Loading