Search code examples
prologprolog-dif

How do i create a brother predicate for prolog?


I have been told to create a brother predicate which finds out if a brother has a sibling. Brother(B, S) :-. I know that you would need to find out if they have the same parents but i am not sure how to do this.

father(dan,phil).
father(dan,jack).
father(dan,christine).
father(phil,kenton).
father(phil,shula).
father(phil,david).
father(phil,elizabeth).
father(jack,lillian).
father(jack,tony).
father(jack,jenny).
father(tony,tommy).
father(tony,helen).
father(tony,john).
father(roger,adam).
father(roger,debbie).
father(brian,kate).
father(david,pip).
father(david,josh).
father(mark,daniel).

mother(doris,phil).
mother(doris,jack).
mother(doris,christine).
mother(jill,kenton).
mother(jill,shula).
mother(jill,david).
mother(jill,elizabeth).
mother(peggy,lillian).
mother(peggy,tony).
mother(peggy,jenny).
mother(pat,tommy).
mother(pat,helen).
mother(pat,john).
mother(jenny,adam).
mother(jenny,debbie).
mother(jenny,kate).
mother(ruth,pip).
mother(ruth,josh).
mother(shula,daniel).

male(dan).
male(phil).
male(jack).
male(kenton).
male(david).
male(tony).
male(brian).
male(roger).
male(mark).
male(john).
male(tommy).
male(adam).
male(daniel).
male(josh).

female(doris).
female(jill).
female(peggy).
female(christine).
female(shula).
female(ruth).
female(elizabeth).
female(lillian).
female(pat).
female(jenny).
female(helen).
female(debbie).
female(kate).
female(pip).

dead(dan).
dead(doris).
dead(jack).
dead(mark).
dead(john).
dead(fred).


parent(X,Y) :-
        father(X,Y); mother(X,Y).

grandfather(X,Y) :-
        father(X,Z), mother(Z,Y).
grandfather(X,Y) :-
        father(X,Z), father(Z,Y).

ancestor(X,Y) :- parent(X,Y).
ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y).

archer(dan).
archer(X) :- father(P,X), archer(P).

This is the document i have which defines parents, grandfather etc. I need to create a brother predicate which sits as brother(B being brother, S being sibling). For example brother(liam, georgia). Liam is the brother of Georgia should be true


Solution

  • You can solve your problem by first providing a formal definition of what a brother(B,S) is:

    A brother B of a slibing S is a male/1 which has the same father/2 F and mother/2 M.

    Now that we know this, we write for each condition one or more lines.


    The first one is simply the head of the clause:

    brother(B,S) :-
    

    Now since a brother is a male/1, the first condition we check is:

        male(B),
    

    Next we have to check whether they have the same father/2. Therefore we define a father F, and F has to be the father of B, so we write:

        father(F,B),
    

    since that father has to be the father of S as well, we write:

        father(F,S),
    

    The same holds for the mother: we define a mother M and check that this mother is the mother of both B and S:

        mother(M,B),
        mother(M,S).
    

    Now putting it all together we obtain:

    brother(B,S) :-
        male(B),
        father(F,B),
        father(F,S),
        mother(M,B),
        mother(M,S).
    

    Now a problem is that with this predicate, a male person will be a brother of himself. If you do not want this behavior, we add a constraint:

    And a person is not a brother/2 of himself.

    You thus have to use the not equal predicate:

        B \= S.
    

    Or the full predicate:

    brother(B,S) :-
        male(B),
        father(F,B),
        father(F,S),
        mother(M,B),
        mother(M,S),
        B \= S.
    

    This predicate generates the following answers:

    ?- brother(B,S).
    B = phil,
    S = jack ;
    B = phil,
    S = christine ;
    B = jack,
    S = phil ;
    B = jack,
    S = christine ;
    B = kenton,
    S = shula ;
    B = kenton,
    S = david ;
    B = kenton,
    S = elizabeth ;
    B = david,
    S = kenton
    ...