1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// This test is a Datalog program that generates Fibonacci numbers F_{0..25}.
//
// It tests that the main loop is run at least once even if no input relations
// are provided, as well as arithmetic operators, Rust expression conditions,
// and interpolation.
mod datalog {
use crepe::crepe;
crepe! {
@output
struct Fib(u32, u32);
Fib(0, 0);
Fib(1, 1);
Fib(n + 2, x + y) <- Fib(n, x), Fib(n + 1, y), (n <= 23);
}
pub fn run() -> Vec<(u32, u32)> {
let (fib,) = Crepe::new().run();
let mut output: Vec<_> = fib.into_iter().map(|Fib(x, y)| (x, y)).collect();
output.sort_unstable();
output
}
}
#[test]
fn test_fibonacci() {
let results = datalog::run();
assert_eq!(
results,
[
(0, 0),
(1, 1),
(2, 1),
(3, 2),
(4, 3),
(5, 5),
(6, 8),
(7, 13),
(8, 21),
(9, 34),
(10, 55),
(11, 89),
(12, 144),
(13, 233),
(14, 377),
(15, 610),
(16, 987),
(17, 1597),
(18, 2584),
(19, 4181),
(20, 6765),
(21, 10946),
(22, 17711),
(23, 28657),
(24, 46368),
(25, 75025)
]
);
}