May be transformed into (for systems using when):
:- module(produce,[p/1,q/1],ciao(h)). :- qload(library(cc_lib)). p(X) :- ask((N^T^(X=[N|T]),N>0),_3) & , ( ask(nonvar(_3)) -> T=[_1|_2], p(T) ) & . p(X) :- ask((N^T^(X=[N|T]),N=0),_1) & , ( ask(nonvar(_1)) -> T=[] ) & . q(X) :- ask(N^N1^T^(X=[N,N1|T]),_1) & , ( ask(nonvar(_1)) -> N1 is N-1, q([N1|T]) ) & . q(X) :- ask(X=[0],_1) & .