1. Prepared by: Sharif Omar Salem – ssalemg@gmail.com
Formal Logic:
PROgramming in
LOGic
0
2. Declarative Programming Languages
• A declarative language is based on predicate logic.
• A program written in a declarative language consists only of statements
(clauses) (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.
1
3. Prolog
• Prolog (PROgramming in LOGic) is a declarative programming language.
• Structure of programs
– Programs consist of procedures.
– Procedures consist of clauses.
– Each clause is a fact or a rule.
– Programs are executed by posing queries.
• The set of declarations that constitutes a Prolog program is also known as a
Prolog database.
• Items in a Prolog database are either facts or rules.
2
4. Prolog
• Example of Prolog facts
(a binary predicate called “eat(x,y)”):
– eat (bear, fish)
– eat (bear, fox)
– eat (deer, grass)
– “bear,” “fish,” “fox,” “deer,” and “grass” are constants because they
represent specific elements in the domain.
• Other facts that we could add to the Prolog database:
– animal (bear)
– animal (fish)
– animal (fox)
– animal (deer)
– plant (grass)
3
5. Prolog
• We can now pose some simple queries.
– is (eat (deer, grass))
• yes
– is (eat (bear, rabbit))
• no
• “is” asks if the fact exists in the database.
• Queries may include variables, for example:
– which(x: eat(bear, x))
• produces:
– fish
– Fox
4
8. Prolog
7
The second type of item in a Prolog database is a Prolog
rule.
A rule is a description of a predicate by means of an
implication.
Prolog rule structure can be as
Head if G1 and G2 and G3…..and Gn.
Head :- G1, G2, G3,……………...., Gn.
Where head is the predicate like Prey(x)
G1…..Gn are the body and it is the conditional rule of the
head predicate. Like { eat (y,x) and animal(x) }.
9. Prolog Rules
• For example, we might use a rule to define a predicate of prey:
– prey(x) Prolog Predicate.
– prey(x) if eat(y, x) and animal(x) If eat(y, x) and animal(x) , then
Prey(x) Prolog Rule.
Can be written prey(x) :- eat(y, x), animal(x).
– This 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:
– which(x: prey(x))
• we would get:
• fish
• fox
8
14. Recursion
• Prolog rules are implications.
• Their antecedents may depend on facts or other rules.
• The antecedent of a rule may also depend on that rule itself, in
which case the rule is defined in terms of itself.
• For example, we can then define a binary relation in-food-chain(x,
y), meaning “y is in x’s food chain.” This means one of two things:
1. x eats y directly. food-chain(x,y) . No recursion.
2. x eats something that eats something that eats something .. that
eats y. x eats z and y is in z’s food chain. recursion
food-chain(x,z) ^ food-chain(z,y)
13
15. Recursion
• Case (1) is simple to test from our existing facts, in-food-chain means nothing
different than eat.
• On the other hand, Case (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 in-food-chain incorporates (1) and (2):
– in-food-chain(x, y) if eat(x, y)
– in-food-chain(x, y) if eat(x, z) and in-food-chain(z, y)
• is a recursive rule because it defines the predicate in-food-chain in terms
of in-food-chain.
14
16. ?- path(f, f).
?- path(a, c).
?- path(g, e).
?- path(g, X).
?- path(X, h).
path(X, Y) :- t(X, Y).
path(X, Y) :- t(X, Z), path(Z, Y).
t(g, h).
t(g, d).
t(e, d).
t(h, f).
t(e, f).
t(a, e).
t(a, b).
t(b, f).
t(b, c).
t(f, c).
Example
15
arc a
ed
g
h
f
c
b
22. Horn Clauses and Resolution
Prolog clauses as Predicate logic wffs.
• We can describe the facts in prolog by the wffs in predicate logic.
As example
animal(fox) in Prolog could be written as A(f) in Predicate logic .
partents(john, diana) in Prolog ………. P(j,d) in Predicate logic.
• And describe the rules as a predicate formula.
with the rule: E(y, x) Λ A(x) Pr (x)
– Prolog treats the rule as being universally quantified and uses
universal instantiation to strip off the universal quantifiers:
– ( y)( x)[E(y, x) Λ A(x) Pr(x)]
21
23. Horn Clauses and Resolution
• A Horn clause is a wff composed of predicates and the negations of
predicates joined by disjunctions, where, at most, one predicate is un-
negated.
• Example of Horn clause: [E(y, x)] V [A(x)] V Pr(x)
• This can be rewritten using DeMorgan’s law as
[E(y, x) Λ A(x)]’ V Pr(x)
• Using Implication rule the formula is equivalent to:
E(y, x) Λ A(x) Pr(x)
• The above is a rule in the Prolog programming.
22
24. Horn Clauses and Resolution
• The rule of inference used by Prolog is called resolution ( proof
sequence).
• Two Horn clauses in a Prolog database are resolved into a new Horn
clause if one contains an unnegated predicate that matches a negated
predicate in the other clause.
• For example:
A(a) , [A(a)] V B(b) A(a) Λ [A(a)] V B(b)
A(a), A(a) B(b) B(b)
• which is just an application of modus ponens.
• Therefore, Prolog’s rule of inference includes modus ponens as a special
case.
23
25. 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
this 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.
• This includes both the facts known to the expert and the expert’s
reasoning path in reaching conclusions from those facts.
24