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

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

Embed Size (px)

Citation preview

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

3.13.1

3. Rules3. Rules

• Simple rules.

• Processing rules.

• Multiple sub-goals.

• PROLOG syntax.

• Recursive rules.

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

3.23.2

Simple RulesSimple 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

ifif

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

3.33.3

Processing RulesProcessing 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.

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

3.43.4

Processing Rules IIProcessing Rules II

• Another query :

|? - preceded(gaius, tiberius).no|? -

• A query containing a logical variable :

|? - preceded(gaius, K).K = claudiusyes|? -

• Obviously, preceded is just succeeded “backwards”.

– Not very useful.

• Queries with more than one sub-goal are much more interesting.

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

3.53.5

Multiple Sub-GoalsMultiple 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).

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

3.63.6

Multiple Sub-Goals IIMultiple 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 = octaviusyes|? -

• GNU PROLOG does not print whatever values it used for Z to find values for G and C.

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

3.73.7

A Bit Of PROLOG SyntaxA 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.

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

3.83.8

Recursive RulesRecursive 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 1ancestor(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.

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

3.93.9

Recursive Rules IIRecursive 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|? -

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

3.103.10

SummarySummary

• 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.