Just to explain even more, don’t do that unless you have to. Start by reading the docs to forall/2 as those explain how to use the predicate and how it is implemented. There is also some discussion about side effects in particular. It is not clear @mgondan1 if you must have a side effect or if you just happen to show an example with a side effect in it, for demonstration.
If you want the side effect, this seems easy to read and write: