Singleton variable in branch WARNING PROLOG

I have to do a genealogy tree in Prolog and my code works but I keep getting the warning Singleton variable in branch: X, please help me correct my code, I know I have to change probably the name of a variable X but still get the error, how can I do it correctly? Thanks.

hombre('nicanor_ulloa').
hombre('jose_arcadio_buendia').
hombre('jose_arcadio').
hombre('aureliano1').
hombre('arcadio').
hombre('aureliano_jose').
hombre('aurelianos17').
hombre('fernando_del_carpio').
hombre('jose_arcadio_segundo').
hombre('aureliano_segundo').
hombre('gaston').
hombre('jose_arcadio_nieto').
hombre('mauricio_babilonia').
hombre('aureliano_babilonia').
hombre('aureliano2').
mujer('rebeca_montiel').
mujer('ursula_iguaran').
mujer('rebeca')
...
...
...
padre(A,B):-
   hombre(A),desciende(B,A).

madre(A,B):-
   mujer(A),desciende(B,A).

diferente(A,B):-
   A\==B.

hijo(A,B):-
   hombre(A),madre(B,A);padre(B,A).

hija(A,B):-
   mujer(A),madre(B,A);padre(B,A).

hermano(A,B):-
   hombre(A),diferente(A,B), padre(X,A); madre(X,B).

hermana(A,B):-
   mujer(A),diferente(A,B), padre(X,A); madre(X,B).

tio(A,B):-
   hombre(A), hermano(X,A);hermana(X,A), padre(X,A);madre(X,B).

tia(A,B):-
   mujer(A), hermano(X,A);hermana(X,A), padre(X,A);madre(X,B).

primos(A,B):-
   hombre(A);mujer(A),tio(X,B);tia(X,B),desciende(A,X).

abuelo(A,B):-
   hombre(A), desciende(X,A), desciende(B,X).      

abuela(A,B):-
   mujer(A), desciende(X,A), desciende(B,X).  

cuñados(A,B):-
   mujer(A);hombre(A), hermano(X,A);hermana(X,A), esposos(B,X).

Upvotes: 1

Views: 8054

Answers (1)

false
false

Reputation: 10122

The warning gives you a clue that something is wrong with:

hermano(A,B):-
   hombre(A),diferente(A,B), padre(X,A); madre(X,B).

In fact, with common Prolog indentation, this becomes even more evident:

hermano(A,B):-
   (  hombre(A),
      diferente(A,B),
      padre(X,A)
   ;  madre(X,B)
   ).

So A is a brother, if

  • A is male and different to B, and A has a father X, or
  • X is mother of B.

In other words: Anything could be a brother A of B, provided B has a mother.

What you meant instead was probably:

hermano(A,B):-
   hombre(A),
   diferente(A,B),
   desciente(A,X), % ( padre(X,A) ; madre(X,A) )
   desciente(B,X). % ( padre(X,B) ; madre(X,B) )

So you want to include half-brothers.

Also, better use the following definition:

diferente(A, B) :-
   dif(A, B).

Upvotes: 3

Related Questions