CPSC 125 Ch 1 Sec 5

Preview:

DESCRIPTION

Logic Programming

Citation preview

Formal Logic

Mathematical Structures for Computer Science

Chapter 1

Copyright © 2006 W.H. Freeman & Co. MSCS Slides Formal Logic

Section 1.5 Logic Programming 1

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 fit the user’s query.

Section 1.5 Logic Programming 2

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, fish) ■  eat (bear, fox) ■  eat (deer, grass)

●  “bear,” “fish,” “fox,” “deer,” and “grass” are constants because they represent specific elements in the domain.

Section 1.5 Logic Programming 3

Prolog

●  Other facts that we could add to the Prolog database: ■  animal (bear) ■  animal (fish) ■  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 4

Prolog

●  Queries may include variables, for example: ■  which(x: eat(bear, x))

●  produces: ■  fish ■  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 5

Prolog Rules

●  For example, we might use a rule to define 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: ■  fish ■  fox

Section 1.5 Logic Programming 6

Horn Clauses and Resolution

●  We can describe the facts in our database by the wffs ■  E(b, fi) ■  E(b, fo) ■  E(d, g) ■  A(b) ■  A( fi) ■  A( fo) ■  A(d) ■  P(g)

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

Section 1.5 Logic Programming 7

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 8

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 9

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. 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 10

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

Section 1.5 Logic Programming 11

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.

Recommended