# Checking for membership in list without instantiating anonymous variables

**URL:** <https://swi-prolog.discourse.group/t/checking-for-membership-in-list-without-instantiating-anonymous-variables/1542>\
**Category:** Help!\
**Created:** [November 26, 2019, 9:39pm UTC](https://swi-prolog.discourse.group/t/checking-for-membership-in-list-without-instantiating-anonymous-variables/1542 "2019-11-26T21:39:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![JCR](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@JCR](https://swi-prolog.discourse.group/u/JCR)\
**Post date:** [November 26, 2019, 9:39pm UTC](https://swi-prolog.discourse.group/t/checking-for-membership-in-list-without-instantiating-anonymous-variables/1542/1 "2019-11-26T21:39:53Z")

</div>

Hi!  
Below i want to check if `p(a)` and `p(b)` are members of the list. But after the fist call to `member/2`,` L` is instantiated to `p(a)` so the second call to `member/2` fails. Is there a way to avoid this? Ie checking for membership without changing the list that involves anonymous variables?

```
myList([p(_)]).

main:-
    myList(L),
    member(p(a), L),
    member(p(b), L).

```

Cheers/JCR

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [November 26, 2019, 9:46pm UTC](https://swi-prolog.discourse.group/t/checking-for-membership-in-list-without-instantiating-anonymous-variables/1542/2 "2019-11-26T21:46:39Z")

</div>

This works, but I know Jan will most likely show something that is better that I am not aware. It uses [copy\_term/2](https://www.swi-prolog.org/pldoc/man?predicate=copy_term/2)

```prolog
other :-
    myList(L),
    copy_term(L,L1),
    member(p(a), L),
    member(p(b), L1).

```

```prolog
?- main.
false.

?- other.
true.

```

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [November 26, 2019, 10:04pm UTC](https://swi-prolog.discourse.group/t/checking-for-membership-in-list-without-instantiating-anonymous-variables/1542/3 "2019-11-26T22:04:24Z")

</div>

Double negation?

```
main:-
    myList(L),
    \+ \+ member(p(a), L),
    \+ \+ member(p(b), L).
```

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [November 26, 2019, 10:05pm UTC](https://swi-prolog.discourse.group/t/checking-for-membership-in-list-without-instantiating-anonymous-variables/1542/4 "2019-11-26T22:05:39Z")

</div>

> [@pmoura](#):
>
> Double negation?

To this day I know that is important to know but have no clue as how or the reasoning behind it. Care to explain.

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [November 26, 2019, 10:09pm UTC](https://swi-prolog.discourse.group/t/checking-for-membership-in-list-without-instantiating-anonymous-variables/1542/5 "2019-11-26T22:09:10Z")

</div>

Negation never binds variables in the negated goal. Thus, by using double negation you can verify that a goal succeeds without binding any of its variables.

---

<div class="post-metadata">

**Author:** ![JCR](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@JCR](https://swi-prolog.discourse.group/u/JCR)\
**Post date:** [November 26, 2019, 10:10pm UTC](https://swi-prolog.discourse.group/t/checking-for-membership-in-list-without-instantiating-anonymous-variables/1542/6 "2019-11-26T22:10:29Z")

</div>

Thanks @EricGT.

I should have explained more clearly in my original post that i don’t know how many times `member/2` will be called and what the first argument of the compound `p` will be. So what i have is something like

```
myList([p(_)]).

main:-
   myList(L),
   member(p(a), L),
   member(p(b), L),
   ...,
   member(p(n), L).

```

Where there is an unknown number of such `member/2` predicates each with `p(unknownAtom)`.

Cheers/JC

---

<div class="post-metadata">

**Author:** ![JCR](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@JCR](https://swi-prolog.discourse.group/u/JCR)\
**Post date:** [November 26, 2019, 10:15pm UTC](https://swi-prolog.discourse.group/t/checking-for-membership-in-list-without-instantiating-anonymous-variables/1542/7 "2019-11-26T22:15:00Z")

</div>

Thanks! This solved my problem.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [November 26, 2019, 10:31pm UTC](https://swi-prolog.discourse.group/t/checking-for-membership-in-list-without-instantiating-anonymous-variables/1542/8 "2019-11-26T22:31:12Z")

</div>

In looking for more info on double negation found

[forall/2](https://www.swi-prolog.org/pldoc/man?predicate=forall/2)

> The predicate [forall/2](https://www.swi-prolog.org/pldoc/man?predicate=forall/2) is implemented as `\+ ( Cond, \+ Action)` , i.e., _There is no instantiation of Cond for which Action is false._ . The use of double negation implies that [forall/2](https://www.swi-prolog.org/pldoc/man?predicate=forall/2)_does not change any variable bindings_ . It proves a relation. The [forall/2](https://www.swi-prolog.org/pldoc/man?predicate=forall/2) control structure can be used for its side-effects.
