Search code examples
prologclpfd

Excluding a tuples_in list in prolog


This is the problem in short: 16 children are to be seated in a 4 x 4 array of chairs. The children are 8 girls (numbered 1..8) and 8 boys (numbered 9..16). 1,3,5,8 think boys are yucky 9,10,11,14 think girls are gross these pairs are enemies: [[1,2], [4,6], [4,7], [4, 9],[9,11], [12, 14], [14,16]]

The predicate to find two children are not enemies is defined as:

not_enemy(A, B) :-
    NotA #\= A #\/ NotB #\= B,
    tuples_in([[NotA, NotB]],
              [[1,2], [4,6], [4,7], [4, 9],[9,11], [12, 14], [14,16]]).

The above code was found here

but when I query ?- not_enemy(1,2) the output is true.

I have to use this long code instead:

not_enemy(A, B) :-
          A #=1 #==> B #\= 2,
          A #=4 #==> B #\= 6,
          A #=4 #==> B #\= 7,
          A #=4 #==> B #\= 9,
          A #=9 #==> B #\= 11,
          A #=12 #==> B #\= 14,
          A #=14 #==> B #\= 16.

Could anyone please help to correct the first piece of code? Thanks in Advance.


Solution

  • I've found another answer, instead of using this

    not_enemy(A, B) :-
        NotA #\= A #\/ NotB #\= B,
        tuples_in([[NotA, NotB]],
                  [[1,2], [4,6], [4,7], [4, 9],[9,11], [12, 14], [14,16]]).
    

    Just negate tuples_in with #\

    not_enemy(A, B) :-
        #\ tuples_in([[A,B]],
                     [[1,2], [4,6], [4,7], [4, 9],[9,11], [12, 14], [14,16]]).