Upcoming SlideShare
×

# CPSC 125 Ch 1 Sec 5

972
-1

Published on

Logic Programming

Published in: Technology
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
972
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
32
0
Likes
1
Embeds 0
No embeds

No notes for slide

### CPSC 125 Ch 1 Sec 5

1. 1. Formal Logic Mathematical Structures for Computer Science Chapter 1 Copyright © 2006 W.H. Freeman & Co. MSCS Slides Formal Logic
2. 2. Declarative Programming Languages ●  A declarative language is based on predicate logic. ●  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 ﬁt the user’s query. Section 1.5 Logic Programming 1
3. 3. Prolog ●  Prolog (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 are either facts or rules. ●  Example of Prolog facts (a binary predicate called “eat”): ■  eat (bear, ﬁsh) ■  eat (bear, fox) ■  eat (deer, grass) ●  “bear,” “ﬁsh,” “fox,” “deer,” and “grass” are constants because they represent speciﬁc elements in the domain. Section 1.5 Logic Programming 2
4. 4. Prolog ●  Other facts that we could add to the Prolog database: ■  animal (bear) ■  animal (ﬁsh) ■  animal (fox) ■  animal (deer) ■  plant (grass) ●  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. Section 1.5 Logic Programming 3
5. 5. Prolog ●  Queries may include variables, for example: ■  which(x: eat(bear, x)) ●  produces: ■  ﬁsh ■  fox ●  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. Section 1.5 Logic Programming 4
6. 6. Prolog Rules ●  For example, we might use a rule to deﬁne a predicate of prey: ■  prey(x) if eat(y, x) and 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: ■  ﬁsh ■  fox Section 1.5 Logic Programming 5
7. 7. Horn Clauses and Resolution ●  We can describe the facts in our database by the wffs ■  E(b, ﬁ) ■  E(b, fo) ■  E(d, g) ■  A(b) ■  A( ﬁ) ■  A( fo) ■  A(d) ■  P(g) ■  with the rule: E(y, x) Λ A(x) → Pr (x) ●  Prolog treats the rule as being universally quantiﬁed and uses universal instantiation to strip off the universal quantiﬁers: ■  (∀y)(∀x)[E(y, x) Λ A(x) → Pr(x)] Section 1.5 Logic Programming 6
8. 8. Horn Clauses and Resolution ●  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. ●  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) ●  This is equivalent to: ■  E(y, x) Λ A(x) → Pr(x) ●  The above is a rule in the Prolog program. Section 1.5 Logic Programming 7
9. 9. Horn Clauses and Resolution ●  The rule of inference used by Prolog is called resolution. ●  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) ■  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. Section 1.5 Logic Programming 8
10. 10. 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 deﬁned in terms of itself. ●  For example, we can then deﬁne 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. 2.  x eats something that eats something that eats something ... that eats y. This can also be stated as: 2.  x eats z and y is in z’s food chain. Section 1.5 Logic Programming 9
11. 11. Recursion ●  Case (1) is simple to test from our existing facts, but without (2), in- food-chain means nothing different than eat. ●  On the other hand, (2) without (1) sends us down an inﬁnite path of something eating something eating something and so on, with nothing telling us when to stop. ●  Recursive deﬁnitions always need a stopping point that consists of speciﬁc 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 deﬁnes the predicate in-food-chain in terms of in-food-chain. Section 1.5 Logic Programming 10
12. 12. 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 ﬁeld. ●  This includes both the facts known to the expert and the expert’s reasoning path in reaching conclusions from those facts. Section 1.5 Logic Programming 11
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.