Jake Owen
Jake Owen

Reputation: 13

Prolog - Issue using math operators

I have a set of rules that consist of;

family(Mother,Father,Children), where children is a list (e.g. [ag,bg]) and person/6. The date is person in set out as Day,Month,Year.

I want to say that if two children who are not born in the same or consecutive days must be a year apart. I've been at this for hours and just cannot seem to get anywhere, below is my code, which I personally cannot see why it would not work. Any advice will be greatly appreciated.

bad_children() :-
      family(_,_,Q),
      member(J,Q),
      member(T,Q),
      person(J,_,_,_,date(E,M,Y),place(_,_)),
      person(T,_,_,_,date(F,M,K),place(_,_)),
      ( E \= F ; E \= (F+1) ; E \= (F-1) ),
      (Y-K) < 1
   ;  (K-Y) < 1,
      write(J),
      write(' and '),
      write(T),
      write(' are born to close together.').

Upvotes: 0

Views: 36

Answers (1)

CapelliC
CapelliC

Reputation: 60014

the main mistake is about operator (\=)/2. It does mean don't unify, and it doesn't perform arithmetic evaluation.

Also, you should bracket the disjunction (Y-K) < 1 ; (K-Y) < 1.

So, the (untested) code could be

bad_children :-
 family(_,_,Q),
 member(person(J,_,_,_,date(E,M,Y),_),Q), % You're not interested in place/2
 member(person(T,_,_,_,date(F,M,K),_),Q),
 J \= T, % Regardless the date, you should check the identity. Here \= is fine
 (E =\= F; E =\= F+1; E =\= F-1),
 (Y-K < 1 ; K-Y < 1),
 format('~w and ~w are born to close together.', [J, T]).

Upvotes: 1

Related Questions