Slide 14.24: Rules (cont.) Slide 14.26: Rules (cont.) Home |
fun( X ) :- red( X ), car( X ). fun( X ) :- blue( X ), bike( X ). car( vw_beatle ). car( ford_escort ). bike( harley_davidson ). red( vw_beatle ). red( ford_escort ). blue( harley_davidson ).Examples of Rule I (Cont.)
harley_davidson
is fun.
To do this we can ask Prolog the following question.
?- fun(harley_davidson). /* to which Prolog will reply */ yes /* to show the program succeeded */Prolog first sees if
harley_davidson
is red, however only vw_beatles
and ford_escorts
are red.
Hence the query red(harley_davidson)
will fail.
This means that the first clause of fun
will fail.
As a result, we now try the second clause of fun.
This will mean that we will attempt the subgoals blue(harley_davidson)
and bike(harley_davidson)
.
Both these goals match facts in the database.
As a result fun
succeeds as we saw above.
?- fun( What ).
To which Prolog will reply
What = vw_beatle yesFirstly Prolog will try the first clause of
fun
.
This results in trying the goal red(What)
.
This succeeds matching the first clause of red
with the binding What=vw_beatle
.
Now attempt the goal car(vw_beatle)
.
This matches the first clause of car
, and, as a result, the fun
goal succeeds.