Freezing goal in prolog

516 views Asked by At

I want to freeze my goal until some variable, for example list, is unbounded, right now I have

sieve(N,L) :-
   freeze(Aux,sieve(N,L,[],Aux)),
   numlist(2,N,Aux).

sieve(N,L,R,[H|T]) :-
   freeze(X, X mod H =\= 0 ; X == H),
   findall(X,select(X,T,_),P),
   sieve(N,L,[H|R],P).
sieve(_,L,L,[]).

But it stop after some operations and waits forever. Could someone tell me how to correct this?

1

There are 1 answers

0
whd On BEST ANSWER

Ok i found out solution i had to change recursive call in sieve so now i call it in freeze predicate.

as requested i found clue here Lazy lists in Prolog?

sieve(N,L) :-
    sieve(L,Strumien,[]),
    numlist(2,N,X),
    X = Strumien.

sieve(L,Strumien,X) :-
    freeze(Strumien,
        (   Strumien =[H|T],
            filter(H,T,Z),
            sieve(L,Z,[H|X])
        )).
sieve(L,[],L).

filter(H,S,X) :-
    filter(H,S,X,[]).

filter(_,[],X,X).
filter(H,S,X,Y) :-
    freeze(S,S =[H1|T]),
    ( H1 mod H =\= 0 ->
        append(Y,[H1],Y2),
        filter(H,T,X,Y2)
    ;
        filter(H,T,X,Y)
    ).