Upload
katherine-williams
View
215
Download
1
Embed Size (px)
Citation preview
3.13.1
3. Rules3. 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
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.
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.
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).
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.
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.
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.
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|? -
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.