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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use super::Goal;
use crate::core::State;
#[derive(Debug)]
pub struct All {
goals: Vec<Box<dyn Goal>>,
}
impl From<Vec<Box<dyn Goal>>> for All {
fn from(goals: Vec<Box<dyn Goal>>) -> Self {
All { goals }
}
}
impl FromIterator<Box<dyn Goal>> for All {
fn from_iter<T: IntoIterator<Item = Box<dyn Goal>>>(iter: T) -> Self {
All {
goals: iter.into_iter().collect(),
}
}
}
impl Goal for All {
fn apply(&self, state: State) -> Option<State> {
self.goals.iter().try_fold(state, |s, g| g.apply(s))
}
}
#[macro_export]
macro_rules! all {
($($item:expr),* $(,)?) => {
{
let goals: Vec<Box<dyn $crate::goals::Goal>> = vec![$(Box::new($item)),*];
$crate::goals::All::from(goals)
}
};
}
pub use all;
#[cfg(test)]
mod tests {
use crate::{core::LVar, core::Query, goals::unify};
use super::all;
#[test]
fn succeeds() {
let x = LVar::new();
let y = LVar::new();
let goal = all![unify(y, x), unify(y, 1)];
let result = goal.query((x, y)).collect::<Vec<_>>();
assert_eq!(result, vec![(1, 1)]);
}
#[test]
fn fails() {
let x = LVar::new();
let goal = all![unify(x, 5), unify(x, 7)];
let result = goal.query(x).collect::<Vec<_>>();
assert_eq!(result, vec![]);
}
}