03.06.2013 Views

Turbo Prolog

Turbo Prolog

Turbo Prolog

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Use of Fail<br />

<strong>Turbo</strong> <strong>Prolog</strong> contains a standard predicate that forces backtracking-fail. The effect of<br />

fail corresponds to the effect of 2=3. We'll use Program 15 to illustrate the use of this<br />

predicate.<br />

1* Program 15 *1<br />

domains<br />

name = symbol<br />

predicates<br />

father(name,name)<br />

everybody<br />

clauses<br />

father(leonard,katherine).<br />

father(carl,jason).<br />

father(carl,marilyn).<br />

everybody if<br />

father(X,y) and<br />

write(X,1I is II,Y,II's father\n ll ) and<br />

fail.<br />

The goal father(X, Y) could be used in two different situations:<br />

• As an inquiry to the <strong>Turbo</strong> <strong>Prolog</strong> system (an external goaO<br />

• On the right side of a rule (an internal goaO, as in:<br />

grandfather(X,B) if father(X,y) and father(Y,B).<br />

With father(X, Y) as an external goal, <strong>Turbo</strong> <strong>Prolog</strong> will write out all possible solutions in<br />

the usual way:<br />

X= .... , Y= ... .<br />

X= .... , Y= ... .<br />

.... solutions<br />

With father(X, Y) as an internal goal, <strong>Turbo</strong> <strong>Prolog</strong> will continue with the next subgoal<br />

once it has been satisfied and will display only one solution. However, the predicate<br />

everybody in Program 15 uses the fail predicate to disturb the usual mechanism.<br />

The object of the predicate everybody is to produce neater responses from program<br />

runs. Compare the answers to the two goals<br />

Goal: father_to(X,y)<br />

X=leonard, Y = katherine<br />

X=carl, Y=jason<br />

X=carl, Y=marilyn<br />

3 solutions<br />

Goal: everybody<br />

leonard is katherine's father<br />

carl is jason's father<br />

carl is marilyn's father<br />

No solution<br />

The predicate everybody makes use of backtracking to generate more solutions for<br />

father(X, Y) by trying to satisfy the right side of everybody:<br />

father(X,Y) and write(X,1I is lI,y,llIs father\n ll ) and fail.<br />

Tutorial III: <strong>Turbo</strong> <strong>Prolog</strong>'s Relentless Search for Solutions 57

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!