A first remark: predicates and functors start with a lowercase letter, not an uppercase: uppercases are used for variables.
You seem to miss the point that predicates in Prolog do not return a value. They can only succeed, or fail (in that sense they "return" a boolean). The way to provide (non-boolean) output is by using unification.
Here you can do this by putting the literal in the head like:
if_then_else(X,Y,male) :- father(X,Y).
if_then_else(X,Y,female).
But now there is another problem: Prolog backtracks. So it means that even if the first clause succeeds, it will try the second clause. So pedro-i
will both be male
and female
. You can solve this problem by putting a guard on the second clause that says that the clause succeeds if Prolog cannot prove that there is a father(X,Y)
relation. Something like:
if_then_else(X,Y,male) :- father(X,Y).
if_then_else(X,Y,female) :- \+ father(X,Y).
But this can result in a computationally expensive problem: it is possible that it takes a long time to prove that there is a father(X,Y).
relation, and if there is no such relation, failing to prove so will take even more time (because Prolog needs to check all branches). It might even result in an infinite loop to do so. In this case you can use a cut (!
). If you reach a cut, Prolog will not attempt to find results in the following clauses of the predicate. So you can write:
if_then_else(X,Y,male) :- father(X,Y), !.
if_then_else(X,Y,female).
Alternatively you can use Prolog's if-then-else structure and use explicit unification:
if_then_else(X,Y,Z) :-
( father(X,Y)
-> Z = male
; Z = female
).