Search code examples
prolog

How do I tell Prolog that a specific tuple is not in a list


So I tried to tell prolog that in Lst there's a tuple with (a,b,) but the last tuple member is not 2 but there are other elements in Lst that might contain (,_,2).

test(A,B,C) :-
    length(Lst, 2),
    member((A,B,C), Lst),

    member((c,d,_), Lst),
    member((a,b,F), Lst),
    F \= 2,
    member((_,_,2), Lst),
    member((_,_,3), Lst).

When I queried for test(c,d,2) it returned false.

test(A,B,C) :-
    length(Lst, 2),
    member((A,B,C), Lst),

    member((c,d,_), Lst),
    member((a,b,F), Lst),
    F \= 2,
    member((_,_,2), Lst),

Solution

  • From the SWI Prolog documentation on \= availble by querying ?- help(\=). :

    This predicate is logically sound if its arguments are sufficiently instantiated

    Your F isn't instantiated (it isn't bound to any value, so you can't tell that it's not some specific value).

    To make your programs work correctly also in situations where the arguments are not yet sufficiently instantiated, use dif/2 instead.

    Change F \= 2 to dif(F, 2)