Freezing goal in prolog
Asked Answered
M

1

6

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?

Metic answered 9/6, 2015 at 8:31 Comment(2)
check this code to see if it helps you rosettacode.org/wiki/Sieve_of_Eratosthenes#PrologRennarennane
its does not, its not about sieve its about freezeMetic
M
1

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)
    ).
Metic answered 9/6, 2015 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.