0% found this document useful (0 votes)
49 views

Chapter Four Logic Programming: Logics in Computer Science (Cosc3141)

This document provides an introduction to logic programming and the logic programming language Prolog. It discusses how Prolog differs from procedural languages by being declarative rather than imperative. Prolog programs consist of facts and rules, and allow users to query a knowledge base to derive conclusions from the facts and rules. The document uses an example knowledge base about food chains to illustrate Prolog queries, facts, rules, recursion, and backtracking. Key points are that Prolog applies rules of inference automatically to answers queries, recursive rules are needed when a predicate passes from one object to another, and Prolog uses a depth-first search strategy to explore query solutions.

Uploaded by

Yonathan Berhanu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Chapter Four Logic Programming: Logics in Computer Science (Cosc3141)

This document provides an introduction to logic programming and the logic programming language Prolog. It discusses how Prolog differs from procedural languages by being declarative rather than imperative. Prolog programs consist of facts and rules, and allow users to query a knowledge base to derive conclusions from the facts and rules. The document uses an example knowledge base about food chains to illustrate Prolog queries, facts, rules, recursion, and backtracking. Key points are that Prolog applies rules of inference automatically to answers queries, recursive rules are needed when a predicate passes from one object to another, and Prolog uses a depth-first search strategy to explore query solutions.

Uploaded by

Yonathan Berhanu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Logics in Computer Science (CoSc3141)

CHAPTER FOUR
LOGIC PROGRAMMING
4.1. Introduction
The programming languages with which you are probably familiar, such as C++ or Java, are
known as procedural languages. Much of the content of a program written in a procedural
language consists of instructions to carry out the algorithm the programmer believes will solve
the problem at hand. The programmer, therefore, is telling the computer how to solve the
problem in a step-by-step fashion.

Some programming languages, rather than being procedural, are declarative languages or
descriptive languages. A declarative language is based on predicate logic; such a language comes
equipped with its own rules of inference. A program written in a declarative language consists
only of statements, actually predicate wffs that are declared as hypotheses. Execution of a
declarative program allows the user to pose queries, asking for information about possible
conclusions that can be derived from the hypotheses. After obtaining the user’s query, the
language turns on its “inference engine” and applies its rules of inference to the hypotheses to
see which conclusions fit the user’s query.

The program, remember, contains only the hypotheses, not any explicit instructions as to what
steps to perform in what order. The inference engine of the language acts behind the scenes, so to
speak, to construct a proof sequence. It is the mechanical nature of applying inference rules that
makes this “automated theorem proving” possible.

4.2. Prolog Basics


The programming language Prolog, which stands for PROgramming in LOGic, is a declarative
programming language. The set of declarations that constitutes a Prolog program is also known
as a prolog database. Items in a Prolog database take on one of two forms, known in Prolog as
facts and rules. In order to interact with Prolog database (sometimes called Knowledge base),
we will use a query. Queries are mechanisms used to ask questions from a Prolog database.

Prolog facts: - allow predicates to be defined by stating which items in some domain of
interpretation satisfy the predicates. As an example, suppose we wish to create a Prolog program
that describes food chains in a given ecological region. We might begin with a binary predicate

1 of 9 Kotebe Metropolitan University, Department of Computer Science


Logics in Computer Science (CoSc3141)

eat. We then describe the predicate by giving the pairs of elements in the domain that make eat
true. Thus we might have the facts,

eat(bear, fish).
eat(bear, fox).
eat(deer, grass).

in our database. Here “bear,” “fish,” “fox,” “deer,” and “grass” are constants because they
represent specific elements in the domain. Because the domain itself is never specified except by
describing predicates, at this point we may take the domain to consist of “bear,” “fish,” “fox,”
“deer,” and “grass.” It is up to the user to maintain a consistent understanding and use of the
predicates in a Prolog program. Thus, eat(bear, fish); can be used either to represent the fact that
bears eat fish or the fact that fish eat bears! We impose the convention that eat(X, Y ) means “X
eats Y.”

We could add descriptions of two unary predicates, animal and plant, to the database by adding
the facts:

animal(bear).
animal(fish).
animal(fox).
animal(deer).
plant(grass).

Armed with this Prolog program (database), we can pose some simple queries. For example, the
query

?- animal(bear).

merely asks if the fact animal(bear) is in the database. Because this fact is in the database, Prolog
would respond to the query by answering true. (This is a one-step proof sequence-no rules of
inference are required). Further dialogue with Prolog could include:

?-eat(deer, grass).
true
?-eat(bear, rabbit).
false

2 of 9 Kotebe Metropolitan University, Department of Computer Science


Logics in Computer Science (CoSc3141)

Queries may include variables, as shown in the next example. For example, the query

?-eat(bear, X).

produces X = fish X = fox as a response. Prolog has answered the query by searching the
database for all facts that match the pattern eat(bear, X ), where X is a variable. The answer
“fish” is given first because the rules are searched in order from top to bottom. Queries may
contain the logical connectives and, or, and not.

For example: (Exercise!)

eat(bear, fish).
eat(bear, fox).
eat(deer, grass).
animal(bear).
animal(fish).
animal(fox).
animal(deer).
plant(grass).

What will be Prolog’s response to the query:

?-eat(X, Y) , plant( Y ).

Prolog rule: - A rule is a description of a predicate by means of an implication (the implication


arrow in the rule goes from right to left). For example, we might use a rule to define a predicate
of prey:

prey (X): -eat(Y, X ), animal(X ).

This statement says that X is a prey if it is an animal that is eaten. If we add this rule to our
database, then in response to the query:

?-prey(X). we would get X = fish Y = fox

4.3. Recursion and Backtracking


Prolog rules are implications. Their antecedents (remember that these will appear on the right
side of the rules) may depend on facts, as in

3 of 9 Kotebe Metropolitan University, Department of Computer Science


Logics in Computer Science (CoSc3141)

prey(X ) :- eat(Y, X), animal(X ). or on other rules, as in


hunted(X): - prey(X).

The antecedent of a rule may also depend on that rule itself, in which case the rule is defined in
terms of itself. A definition in which the item being defined is itself part of the definition is
called a recursive definition. As an example, suppose we wish to use the ecology database of the
above example (on page 6) to study food chains. We can then define a binary relation
infoodchain(X, Y), meaning “Y is in X’s food chain.” This, in turn, means one of two things:

1. X eats Y directly or
2. X eats something that eats something that eats something … that eats Y.

Case 2 can be rewritten as follows: 2′. X eats Z and Y is in Z’s food chain. Case 1 is simple to
test from our existing facts, but without (2′), infoodchain means nothing different from eat. On
the other hand, (2′) without (1) sends us down an infinite path of something eating something
eating something and so on, with nothing telling us when to stop. Recursive definitions always
need a stopping point that consists of specific information. The Prolog rule for infoodchain
incorporates (1) and (2′):

infoodchain(X, Y ):- eat(X, Y ).


infoodchain(X, Y ):- eat(X, Z ) , infoodchain(Z, Y ).
It is a recursive rule because it defines the predicate infoodchain in terms of infoodchain. A
recursive rule is necessary when the predicate being described is passed on from one object to
the next. The predicate infoodchain has this property:

infoodchain(X, Y ) ∧infoodchain(Y, Z ) →infoodchain(X, Z ). For example, after the


infoodchain rule is added to the database (ecology) of the above example, the following query is
made:

?-infoodchain(bear, Y ).

The response follows (numbers are added for reference purposes):

1. Y = fish ;
2. Y = raccoon;
3. Y = fox ;
4. Y = deer ;

4 of 9 Kotebe Metropolitan University, Department of Computer Science


Logics in Computer Science (CoSc3141)

5. Y = littlefish ;
6. Y = algae ;
7. Y = fish ;
8. Y = littlefish ;
9. Y = algae ;
10. Y = rabbit ;
11. Y = grass ;
12. Y = grass ;

Prolog applies the simple case of:

infoodchain(bear, Y) :- eat(bear, Y).

First, obtaining answers 1 through 4 directly from the facts eat(bear, fish), eat(bear, raccoon),
and so on.Moving to the recursive case:

infoodchain(bear, Y ):- eat(bear, Z), infoodchain(Z, Y ).

A match of eat(bear, Z) occurs with Z equal to “fish.” Prolog then looks for all solutions to the
relation infoodchain(fish, Y). Using first the simple case of infoodchain, a match occurs with the
fact eat(fish, littlefish). This results in response 5, littlefish. There are no other facts of the form
eat(fish, Y ), so the next thing to try is the recursive case of:

infoodchain(fish, Y ).
infoodchain(fish, Y ) :- eat(fish, Z), infoodchain(Z, Y).

A match of eat(fish, Z) occurs with Z equal to “littlefish.” Prolog then looks for all solutions to
the relation infoodchain (littlefish, Y). Using the simple case of infoodchain, a match occurs with
the fact eat(littlefish, algae). This results in response 6, algae. There are no other facts of the
form eat(littlefish, Y), so the next thing to try is the recursive case of:

infoodchain(littlefish, Y).
infoodchain(littlefish, Y) :- eat(littlefish, Z) , infoodchain(Z,Y).

A match of eat(littlefish, Z) occurs with Z equal to “algae.” Prolog then looks for all solutions to
the relation infoodchain(algae, Y ). A search of the entire database reveals no facts of the form
eat(algae, Y) (or eat(algae, Z)), so neither the simple case nor the recursive case of
infoodchain(algae, Y ) can be pursued further.

Prolog has reached a dead-end with infoodchain(algae, Y) and will backtrack up the path.
Because there are no other facts of the form eat(littlefish, Z), the search for solutions to

5 of 9 Kotebe Metropolitan University, Department of Computer Science


Logics in Computer Science (CoSc3141)

infoodchain(littlefish, Y) terminates. Then, because there are no other facts of the form eat(fish,
Z), the search for solutions to infoodchain(fish, Y) terminates. Backing up still further, there is
another match of eat(bear, Z) with Z equal to “raccoon” that will generate another search path.

In this example, once Prolog began to investigate infoodchain(fish, Y), all query answers that
could be obtained from exploring this path (responses 5 and 6) were generated before other
answers (responses 7–12). Exploring as far as possible down a given path and then backtracking
up that path before exploring other paths is called a depth-first search strategy.

Exercise!
Trace the execution of the Prolog program of the above example and explain why responses
7–12 occur.

4.4. Horn Clauses and Matching


How do Prolog facts and rules relate to more formal predicate logic? We can describe the facts in
our database as wffs:

E(b, fi)
E(b, fo)
E(d, g)
A(b)
A(fi)
A(fo)
A(d)
P(g)

and the rule by the wff E(y, x) ∧ A(x) → Pr(x) Universal quantifiers are not explicitly part of the
rule as it appears in a Prolog program, but Prolog treats the rule as being universally quantified
(∀y)( ∀x)[ E( y, x) ∧ A(x) → Pr(x)] and repeatedly uses universal instantiation to strip off the
universal quantifiers and allow the variables to assume in turn each value of the domain. Both
facts and rules are examples of Horn clauses. A Horn Clause is a wff composed of predicates or
the negations of predicates (with either variables or constants as arguments) joined by
disjunctions, where at most one predicate is unnegated. Thus, the fact E(d, g) is an example of a
Horn clause because it consists of a single unnegated predicate.

The wff [ E( y, x) ]′ ∨ [ A(x) ]′ ∨Pr(x) is an example of a Horn clause because it consists of three
predicates joined by disjunction where only Pr(x) is unnegated. By De Morgan’s law, it is

6 of 9 Kotebe Metropolitan University, Department of Computer Science


Logics in Computer Science (CoSc3141)

equivalent to [ E( y, x) ∧ A(x) ]′ ∨Pr(x) which in turn is equivalent to E( y, x) ∧ A(x) → Pr(x)and


therefore represents the rule in our Prolog program.

The single rule of inference used by Prolog is called resolution. Two Horn clauses in a Prolog
database are resolved into a single new Horn clause if one contains an unnegated predicate that
matches a negated predicate in the other clause. The new clause eliminates the matching term
and is then available to use in answering the query. For example, A(a) [A(a)]′∨ B(b) resolves to
B(b). This says that from A(a), [A(a)]′∨ B(b) which is equivalent to A(a), A(a) → B(b) Prolog
infers B(b) which is just an application of modus ponens. Therefore, Prolog’s rule of inference
includes modus ponens as a special case.

In applying the resolution rule, variables are considered to “match” any constant symbol. (This is
the repeated application of universal instantiation.) In any resulting new clause, the variables are
replaced with their associated constants in a consistent manner. Thus, in response to the query

?-prey(X ).

Prolog searches the database for a rule with the desired predicate Pr(x) as the consequent. It finds
[ E( y, x) ]′ ∨ [ A(x) ]′ ∨Pr(x) It then proceeds through the database looking for other clauses that
can be resolved with this clause. The first such clause is the fact E(b, fi). These two clauses
resolve into [ A( fi) ]′ ∨Pr( fi) (Note that the constant fi has replaced x everywhere.) Using this
new clause, it can be resolved with the fact A(fi) to conclude Pr( fi). Having reached all
conclusions possible from resolution with the fact E(b, fi), Prolog backtracks to search for
another clause to resolve with the rule clause; this time around it would find E(b, fo).

As a more complex example of resolution, suppose we add the rule hunted(X ) :- prey(X) to the
database. This rule in symbolic form is [Pr(x)]→H(x) or, as a Horn clause, [Pr(x)]′ ∨ H(x) It
resolves with the rule defining prey [E( y, x)]′ ∨ [A(x)]′ ∨Pr(x) to give the new rule [E( y, x)]′ ∨
[A(x)]′ ∨ H(x). The query:

?- hunted(X). will use this new rule to conclude X = fish X = fox

For example, suppose that a Prolog database (ecology database) contains the following entries:

eat(bear, fish).
eat(fish, littlefish).
eat(littlefish, algae).

7 of 9 Kotebe Metropolitan University, Department of Computer Science


Logics in Computer Science (CoSc3141)

eat(raccoon, fish).
eat(bear, raccoon).
eat(bear, fox).
eat(fox, rabbit).
eat(rabbit, grass).
eat(bear, deer).
eat(deer, grass).
eat(wildcat, deer).
animal(bear).
animal(fish).
animal(littlefish).
animal(raccoon).
animal(fox).
animal(rabbit).
animal(deer).
animal(wildcat).
plant(grass).
plant(algae).
prey(X):- eat(Y,X), animal(X).

Then the following dialog with Prolog could take place:


?-animal(rabbit).
true
?-eat(wildcat, grass).
false
?-eat(X, fish)
X = bear X = raccoon
?-eat(X, Y ), plant(Y).
X = littlefish, Y = algae ;
X = rabbit, Y = grass ;
X = deer, Y = grass ;
?-prey(X ).
X = fish ;
X = littlefish ;
X = fish ;
X = raccoon ;
X = fox ;
X = rabbit ;
X = deer ;
X = deer.

Note that fish is listed twice as satisfying the last query because fish are eaten by bear (fact 1)
and by raccoon (fact 3). Similarly, deer are eaten by both bear and wildcat.

8 of 9 Kotebe Metropolitan University, Department of Computer Science


Logics in Computer Science (CoSc3141)

4.5. Expert Systems


Many interesting applications programs have been developed, in Prolog and similar logic
programming languages that gather a database of facts and rules about some domain and then
use the database to draw conclusions. Such programs are known as expert systems, knowledge-
based systems, or rule-based systems.

The database in an expert system attempts to capture the knowledge (“elicit the expertise”) of a
human expert in a particular field, including both the facts known to the expert and the expert’s
reasoning path in reaching conclusions from those facts. The completed expert system not only
simulates the human expert’s actions but can be questioned to reveal why it made certain choices
and not others.

Expert systems have been built that simulate a medical specialist’s diagnosis from a patient’s
symptoms, a factory manager’s decisions regarding valve control in a chemical plant based on
sensor readings, the decisions of a fashion buyer for a retail store based on market research, the
choices made by a consultant specifying a computer system configuration based on customer
needs, and many more. The challenging part of building an expert system lies in extracting all
pertinent facts and rules from the human expert.

9 of 9 Kotebe Metropolitan University, Department of Computer Science

You might also like