canrun_basic/cmp/
lt.rs

1use canrun::assert_2;
2use canrun::goal::Goal;
3use canrun::value::IntoVal;
4use canrun::DomainType;
5use std::fmt::Debug;
6
7/// Ensure that one value is less than another.
8///
9/// # Example:
10/// ```
11/// use canrun::{unify, util, var, all, Goal};
12/// use canrun::domains::example::I32;
13/// use canrun_basic::lt;
14///
15/// let (x, y) = (var(), var());
16/// let goal: Goal<I32> = all![
17///     unify(x, 1),
18///     unify(y, 2),
19///     lt(x, y)
20/// ];
21/// let results: Vec<_> = goal.query((x, y)).collect();
22/// assert_eq!(results, vec![(1, 2)]);
23/// ```
24pub fn lt<'a, A, AV, B, BV, D>(a: AV, b: BV) -> Goal<'a, D>
25where
26    A: PartialOrd<B> + Debug + 'a,
27    B: Debug + 'a,
28    AV: IntoVal<A>,
29    BV: IntoVal<B>,
30    D: DomainType<'a, A> + DomainType<'a, B>,
31{
32    assert_2(a, b, |a, b| a < b)
33}
34
35#[cfg(test)]
36mod tests {
37    use super::lt;
38    use canrun::domains::example::I32;
39    use canrun::{unify, util, var, Goal};
40
41    #[test]
42    fn succeeds() {
43        let (x, y) = (var(), var());
44        let goals: Vec<Goal<I32>> = vec![unify(x, 1), unify(y, 2), lt(x, y)];
45        util::assert_permutations_resolve_to(goals, (x, y), vec![(1, 2)]);
46    }
47
48    #[test]
49    fn fails() {
50        let (x, y) = (var(), var());
51        let goals: Vec<Goal<I32>> = vec![unify(x, 2), unify(y, 1), lt(x, y)];
52        util::assert_permutations_resolve_to(goals, (x, y), vec![]);
53    }
54}