Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freezing goal in prolog

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?

like image 216
whd Avatar asked Nov 10 '22 11:11

whd


1 Answers

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)
    ).
like image 83
whd Avatar answered Nov 15 '22 12:11

whd