#8 formal methods – pro logic

27
Prepared by: Sharif Omar Salem – [email protected] Formal Logic: PROgramming in LOGic 1

Transcript of #8 formal methods – pro logic

Page 1: #8 formal methods – pro logic

1

Prepared by: Sharif Omar Salem – [email protected]

Formal Logic: PROgramming in

LOGic

Page 2: #8 formal methods – pro logic

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.

Page 3: #8 formal methods – pro logic

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.

Page 4: #8 formal methods – pro logic

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)

Page 5: #8 formal methods – pro logic

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

Page 6: #8 formal methods – pro logic

6

Example

elephant(george).elephant(mary).elephant(X) :- grey(X), mammal(X), hasTrunk(X).

Procedure for elephant

Predicate

Clauses

Rule

Facts

Page 7: #8 formal methods – pro logic

7

Example

?- elephant(george).

yes

?- elephant(jane).

no

Queries

Replies

Page 8: #8 formal methods – pro logic

8

Prolog

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.

------------- Body --------------

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) }.

Page 9: #8 formal methods – pro logic

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

Page 10: #8 formal methods – pro logic

10

Exercise

male(bertram).male(percival).

female(lucinda).female(camilla).

pair(X, Y) :- male(X), female(Y).

?- pair(percival, Y).

?- pair(apollo, daphne).

?- pair(camilla, Y).

?- pair(X, lucinda).

?- pair(X, X).

?- pair(bertram, lucinda).

?- pair(X, daphne).

?- pair(X, Y).

Page 11: #8 formal methods – pro logic

11

Exercise

drinks(john, martini).drinks(mary, gin).drinks(susan, vodka).drinks(john, gin).drinks(fred, gin).

pair(X, Y, Z) :-drinks(X, Z),drinks(Y, Z).

?- pair(X, john, martini).

?- pair(mary, susan, gin).

?- pair(john, mary, gin).

?- pair(john, john, gin).

?- pair(X, Y, gin).

?- pair(bertram, lucinda).

?- pair(bertram, lucinda,

vodka).

?- pair(X, Y, Z).

This definition forces X and Y to be distinct:pair(X, Y, Z) :- drinks(X, Z), drinks(Y, Z), X \== Y.

Page 12: #8 formal methods – pro logic

12

Exercise

berkshire

wiltshire

surrey

hampshire sussex

kent

How to represent this relation?

Note that borders are symmetric (two directions).

(a) Representing a symmetric relation.

(b) Implementing a strange ticket condition.

Page 13: #8 formal methods – pro logic

13

SEE TUTORIAL 1 FOR MORE DETAILS

Page 14: #8 formal methods – pro logic

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)

Page 15: #8 formal methods – pro logic

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.

Page 16: #8 formal methods – pro logic

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

arc a

ed

gh

f

cb

Page 17: #8 formal methods – pro logic

17

Examples

Rules

Facts

Goals/Queries

Page 18: #8 formal methods – pro logic

18

Examples

Rules

Facts

Goals/Queries

Page 19: #8 formal methods – pro logic

19

Examples

Page 20: #8 formal methods – pro logic

20

Prolog Standards

Page 21: #8 formal methods – pro logic

21

Prolog Standards

Page 22: #8 formal methods – pro logic

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)]

Page 23: #8 formal methods – pro logic

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.

Page 24: #8 formal methods – pro logic

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.

Page 25: #8 formal methods – pro logic

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.

Page 26: #8 formal methods – pro logic

26

Prepared by: Sharif Omar Salem – [email protected]

End Of Lecture

Page 27: #8 formal methods – pro logic

27

Prepared by: Sharif Omar Salem – [email protected]

Next Lecture:ProLogic_Tutorial