Presentation is loading. Please wait.

Presentation is loading. Please wait.

3.1 3. Rules Simple rules. Processing rules. Multiple sub-goals. PROLOG syntax. Recursive rules.

Similar presentations


Presentation on theme: "3.1 3. Rules Simple rules. Processing rules. Multiple sub-goals. PROLOG syntax. Recursive rules."— Presentation transcript:

1 3.1 3. Rules Simple rules. Processing rules. Multiple sub-goals. PROLOG syntax. Recursive rules.

2 3.2 Simple Rules So far PROLOG programs have consisted of facts. –Things which are true.  So far as the program is concerned that is. PROLOG programs can also contain rules. –Things which are true provided that something else is true. Rules and facts are called clauses or predicates. preceded(X,Y) :- succeeded(Y,X). Head or Condition Head or Condition Body or Conclusion Body or Conclusion if

3 3.3 Processing Rules The program already contains the succeeded relation (see practical 1, question 5). succeeded(tiberius, octavius). succeeded(gaius, tiberius). succeeded(claudius, gaius). Match 1 is with the only clause for preceded. –No choice point generated as there are no choices available. The consequence of match 1 is that PROLOG must now attempt to prove succeeded(tiberius,octavius). Choice point generated as there are three clauses for the succeeded relation. Match 2 succeeds. There are no more answers in the database.

4 3.4 Processing Rules II Another query : |? - preceded(gaius, tiberius). no |? - A query containing a logical variable : |? - preceded(gaius, K). K = claudius yes |? - Obviously, preceded is just succeeded “backwards”. –Not very useful. Queries with more than one sub-goal are much more interesting.

5 3.5 Multiple Sub-Goals A parricide is someone who kills one (or more) of their parents. parricide(X) :- murdered(X,Y), parent(Y,X). Note the local logical variable, Y. The database already contains the murdered relation (see practical 1, question 6). murdered(octavius, postumus). murdered(livia, octavius). murdered(livia, drusus). murdered(tiberius, germanicus). murdered(gaius, germanicus). murdered(gaius, tiberius).

6 3.6 Multiple Sub-Goals II |? - parricide(gaius). yes |? - Based on parent we can define grandparent. grandparent(X,Y) :- parent(X,Z), parent(Z,Y). Note the local logical variable, Z. |? - grandparent(G,C). C = agrippina G = octavius ; C = postumus G = octavius yes |? - GNU PROLOG does not print whatever values it used for Z to find values for G and C.

7 3.7 A Bit Of PROLOG Syntax A PROLOG program is a set of facts and rules. –A set of clauses. –The order does not matter. –Can have any number of clauses for a given relation. –It is good practice to keep together all the facts and rules for a given relation.  Some implementations insist on this. –The facts and rules for a given relation are sometimes called the procedure for that relation. In general, a clause has a head and a body. –A fact is a clause with an empty body. –A query is a clause with an empty head. PROLOG comments start with % and continue to the end of the line.

8 3.8 Recursive Rules To find great-grandparents : |? - parent(X,Y),grandparent(Y,Z). Remember, the, means logical and (c.f. && in C++ or Java). – ; actually means logical or (c.f. || in C++ or Java). What about great-great-grandparents and so on? Better to have a general relation for finding ancestors. ancestor(X,Y) :- parent(X,Y). % Ancestor 1 ancestor(X,Y) :- parent(X,Z), % Ancestor 2 ancestor(Z,Y). Ancestor 1 is simple : parents are also ancestors. Ancestor 2 says that if X is a parent of some person Z and that Z is an ancestor of Y then X is also an ancestor of Y.

9 3.9 Recursive Rules II ancestor is the classic example of a simple transitive relation. |? - ancestor(livia,tiberius). true ? yes |? - ancestor(octavius,postumus). true ? yes |? - ancestor(octavius,Who). Who = julia ? ; Who = agrippina ? ; Who = postumus ? ; Who = gaius ? ; no |? -

10 3.10 Summary PROLOG programs consist of a set of clauses. –A clause is a fact or a rule. A set of clauses for the same relation are called the procedure for that relation. A clause has a head and a body. –Empty body : fact. –Empty head : query. –Otherwise : rule. A body is made up of zero or more sub-goals. The body of a rule can refer to the rule recursively.


Download ppt "3.1 3. Rules Simple rules. Processing rules. Multiple sub-goals. PROLOG syntax. Recursive rules."

Similar presentations


Ads by Google