prologclpfdprolog-diffailure-slice

SWI Prolog does not terminate


:- use_module(library(clpfd)).

fact(treated=A) :- A in 0..1.
fact(numYears=B) :- B in 0..sup.
fact(numDrugs=C) :- C in 0..sup.
fact(treated2=D) :- D in 0..1.
fact(cParam=E) :- E in 0..4.

is_differentfact(X,X) :- false.
is_differentfact(Element=_,OtherElement=_) :-
    dif(Element,OtherElement).

is_fakt([]).
is_fakt([X|Xs]) :-
    fact(X),
    maplist(is_differentfact(X),Xs),
    is_fakt(Xs).

Why does ?- is_fakt(X) return a list of results answers but after a number of results answers it hangs. I don't know why Prolog cannot return all possible values of X.


Solution

  • Edited version based on the comments of false.

    :- use_module(library(clpfd)).
    :- use_module(library(lists)).
    
    fact(treated-X) :- X in 0..1.
    fact(numYears-X) :- X in 0..sup.
    fact(numDrugs-X) :- X in 0..sup.
    fact(treated2-X) :- X in 0..1.
    fact(cParam-X) :- X in 0..4.
    
    facts(Facts) :-
    findall(X,fact(X),Facts).
    
    is_fact2(_, []).
    is_fact2(Facts, [X|Xs]) :-
        member(X,Facts),
        select(X,Facts,Remaining),
        is_fact2(Remaining,Xs).
    
    is_fakt(X) :-
        facts(Facts),
        is_fact2(Facts,X),
        keysort(X,X).
    

    This terminates now.