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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate::core::{
    constraints::{resolve_1, Constraint, ResolveFn},
    LVarList, State, Unify, Value,
};
use crate::goals::unify;
use crate::goals::Any;
use crate::goals::Goal;
use std::fmt::Debug;
use std::iter::repeat;
use std::rc::Rc;

use super::LVec;

/** Create a [`Goal`] that attempts to unify a `Value<T>` with
any of the items in a `LVec<T>`.

This goal will fork the state for each match found.

# Examples:
```
use canrun::{LVar, all, unify, lvec, Query};

let x = LVar::new();
let xs = LVar::new();
let goal = all![
    unify(&x, 1),
    unify(&xs, lvec![1, 2, 3]),
    lvec::member(x, xs),
];
let results: Vec<_> = goal.query(x).collect();
assert_eq!(results, vec![1]);
```

```
# use canrun::{LVar, all, unify, lvec, Query};
let x = LVar::new();
let goal = all![
    lvec::member(x, lvec![1, 2, 3]),
];
let results: Vec<_> = goal.query(x).collect();
assert_eq!(results, vec![1, 2, 3]);
```
*/
pub fn member<T, IntoT, IntoLVecT>(item: IntoT, collection: IntoLVecT) -> Member<T>
where
    T: Unify,
    IntoT: Into<Value<T>>,
    IntoLVecT: Into<Value<LVec<T>>>,
{
    Member {
        item: item.into(),
        collection: collection.into(),
    }
}

/** A [`Goal`] that attempts to unify a `Value<T>` with
any of the items in a `LVec<T>`. Create with [`member`].
*/
#[derive(Debug)]
pub struct Member<T: Unify> {
    item: Value<T>,
    collection: Value<LVec<T>>,
}

impl<T: Unify> Goal for Member<T> {
    fn apply(&self, state: State) -> Option<State> {
        state.constrain(Rc::new(self.clone()))
    }
}

impl<T: Unify> Clone for Member<T> {
    fn clone(&self) -> Self {
        Self {
            item: self.item.clone(),
            collection: self.collection.clone(),
        }
    }
}

impl<T: Unify> Constraint for Member<T> {
    fn attempt(&self, state: &State) -> Result<ResolveFn, LVarList> {
        let collection = resolve_1(&self.collection, state)?;
        let any = collection
            .vec
            .iter()
            .zip(repeat(self.item.clone()))
            .map(|(a, b)| Rc::new(unify(a, b)) as Rc<dyn Goal>)
            .collect::<Any>();
        Ok(Box::new(move |state| any.apply(state)))
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        core::LVar,
        core::Query,
        goal_vec,
        goals::{either, unify},
        lvec,
    };

    use super::member;

    #[test]
    fn basic_member() {
        let x = LVar::new();
        let goal = member(x, lvec![1, 2, 3]);
        let results = goal.query(x).collect::<Vec<_>>();
        assert_eq!(results, vec![1, 2, 3]);
    }

    #[test]
    fn member_with_conditions() {
        let x = LVar::new();
        let goals = goal_vec![unify(x, 2), member(x, lvec![1, 2, 3])];
        goals.assert_permutations_resolve_to(&x, vec![2]);
    }

    #[test]
    fn unify_two_contains_1() {
        let x = LVar::new();
        let list = lvec![1, 2, 3];
        let goals = goal_vec![member(1, &x), member(1, &x), unify(&x, list)];
        goals.assert_permutations_resolve_to(&x, vec![vec![1, 2, 3]]);
    }

    #[test]
    fn unify_two_contains_2() {
        let x = LVar::new();
        let list = lvec![1, 2, 3];
        let goals = goal_vec![member(1, &x), member(2, &x), unify(&x, list)];
        goals.assert_permutations_resolve_to(&x, vec![vec![1, 2, 3]]);
    }

    #[test]
    fn unify_two_contains_3() {
        let x = LVar::new();
        let list = lvec![1, 2, 3];
        let goals = goal_vec![
            either(member(1, &x), member(4, &x)),
            member(2, &x),
            unify(&x, list),
        ];
        goals.assert_permutations_resolve_to(&x, vec![vec![1, 2, 3]]);
    }

    #[test]
    fn unify_two_contains_4() {
        let x = LVar::new();
        let list = lvec![1, 2, 3];
        let goals = goal_vec![member(1, &x), member(4, &x), unify(&x, list)];

        goals.assert_permutations_resolve_to(&x, vec![]);
    }
}