If you're going to use membero
there is no general solution to this problem. Calling membero
with fresh vars will cause it to generate all (read, infinite) possible lists for which q
is a member. Of course lists larger than 3 don't apply - but since you've used run*
it will continue blindly trying lists larger than count 3 even though each one will fail.
It's possible to write a better version of membero
in newer versions of core.logic using the constraint infrastructure, but the details of how one might do this are likely to change over the coming months. Until there's a solid public api for defining constraints you're stuck with the kind of subtle ordering and non-termination issues that trouble Prolog.