1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu,...

50
1 COSC3306: COSC3306: Programming Programming Paradigms Paradigms Lecture 8: Declarative Lecture 8: Declarative Programming Programming Specifications Specifications Haibin Zhu, Ph.D. Haibin Zhu, Ph.D. Computer Science Computer Science Nipissing University Nipissing University (C) 2003 (C) 2003

Transcript of 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu,...

Page 1: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

11

COSC3306:COSC3306:Programming ParadigmsProgramming Paradigms

Lecture 8: DeclarativeLecture 8: DeclarativeProgramming SpecificationsProgramming Specifications

Haibin Zhu, Ph.D.Haibin Zhu, Ph.D.Computer ScienceComputer ScienceNipissing University Nipissing University

(C) 2003(C) 2003

Page 2: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

22

ContentsContents

Logic ProgrammingLogic Programming

First order predicate calculus and Horn First order predicate calculus and Horn clause clause

ResolutionResolution

UnificationUnification

Search structureSearch structure

ApplicationApplication

Page 3: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

33

Logic ProgrammingLogic Programming

Logic programming refers to a paradigm that Logic programming refers to a paradigm that uses a form of symbolic logic as a programming uses a form of symbolic logic as a programming language.language.

The property of logic programming model isThe property of logic programming model is– {Logic Program} = {Logic Statements} + {Control {Logic Program} = {Logic Statements} + {Control

Strategy}Strategy}

where logic statements consists of a set of where logic statements consists of a set of rulesrules and and factsfacts expressing the relationships between expressing the relationships between objects.objects.

Page 4: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

44

Logic ProgrammingLogic Programming

Control strategy is known as Control strategy is known as resolutionresolution or the or the sequence of stepssequence of steps the deduction system the deduction system chooses to answer a question from the logic chooses to answer a question from the logic system. system.

The form of logic used in logic programming The form of logic used in logic programming languages is the languages is the first-order predicate calculusfirst-order predicate calculus, , which is a paradigm of formally expressing which is a paradigm of formally expressing logical statements. A logical statement is a logical statements. A logical statement is a statement that is either true or false.statement that is either true or false.

Page 5: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

55

First-order predicate calculusFirst-order predicate calculus

Constant:Constant: A symbol that represents an object A symbol that represents an object such as names and numbers also known as such as names and numbers also known as atomsatoms, since it can not be broken down into , since it can not be broken down into subsubobjects.objects.Logical Statement:Logical Statement: An unconditional term, An unconditional term, sometimes called a sometimes called a factfact, that represents relations , that represents relations between objects and asserts that something is a between objects and asserts that something is a quality, attribute or property of something else. quality, attribute or property of something else.

Page 6: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

66

First-order predicate calculusFirst-order predicate calculus

An exampleAn example– Suppose that we want to express a logical statement Suppose that we want to express a logical statement

(a fact) that Fred likes Elizabeth. This can be (a fact) that Fred likes Elizabeth. This can be converted as:converted as:

– Likes(Fred, Elizabeth).Likes(Fred, Elizabeth).– In general, a relation that we would say in English as: In general, a relation that we would say in English as:

SubjectSubjectVerbVerbObjectObject, can be converted into a logical , can be converted into a logical statement as: statement as: Verb(Subject, Object)Verb(Subject, Object), or more , or more generally as:generally as:

– Relationship(Object1, Object2).Relationship(Object1, Object2).– The relationship is called a The relationship is called a predicatepredicate and the objects and the objects

that are related are called that are related are called argumentsarguments or or parametersparameters. .

Page 7: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

77

First-order predicate calculusFirst-order predicate calculus

Variable:Variable: A symbol that represents an A symbol that represents an unspecified object, meaning that it can represent unspecified object, meaning that it can represent different objects at different times of the different objects at different times of the computation logic. For example, computation logic. For example,

integer(X)integer(X)

Function:Function: A term that represents a predefined A term that represents a predefined computation. It can be used to express the computation. It can be used to express the derivation of another predicate. For example,derivation of another predicate. For example,

integer(X) integer(X) integer(successor(X)). integer(successor(X)).

Page 8: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

88

First-order predicate calculusFirst-order predicate calculus

ConnectorConnector : : A symbol that connects two or more terms A symbol that connects two or more terms (literals). In the following table, the names, symbols, (literals). In the following table, the names, symbols, examples and the meanings associated with the logical examples and the meanings associated with the logical connectives of the predicate calculus are outlined.connectives of the predicate calculus are outlined.

namename symbolsymbol exampleexample meaningmeaningconjunctionconjunction x x y y x and yx and ydisjunctiondisjunction x x y y x or yx or yequivalenceequivalence x x y y x is equivalent to x is equivalent to yyright implicationright implication x x y y x implies yx implies yleft implicationleft implication x x y y y implies xy implies xright-left implicationright-left implication x x y y x implies y and y x implies y and y

implies ximplies xnegationnegation xx negation of xnegation of x

Page 9: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

99

First-order predicate calculusFirst-order predicate calculus

Quantifier:Quantifier: A symbol that introduces variables such as A symbol that introduces variables such as called called for allfor all known as known as universal quantifieruniversal quantifier and and called called there existsthere exists known as known as existential quantifierexistential quantifier. For example,. For example,

XX (man(X) (man(X) human(X)) human(X))means that, means that, for allfor all x such that x is a man, then x is a x such that x is a man, then x is a human. And human. And

XX (human(X) (human(X) man(X)) man(X))means that, means that, therethere existsexists an X such that if X is a human an X such that if X is a human then x is a man. Because the scope of the quantifiers is then x is a man. Because the scope of the quantifiers is the variables in which they are attached, one can use the the variables in which they are attached, one can use the parentheses in order to extend the scope of the universal parentheses in order to extend the scope of the universal and existential quantifiers.and existential quantifiers.

Page 10: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

1010

First-order predicate calculusFirst-order predicate calculus

Punctuation:Punctuation: A symbol that is used in the A symbol that is used in the structure of predicates and logic structure of predicates and logic statements such as left and right statements such as left and right parentheses, comma and the period.parentheses, comma and the period.

Page 11: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

1111

First-order predicate calculusFirst-order predicate calculus

Precedence: Precedence: Evaluation of operators in a precise Evaluation of operators in a precise sequence determined by the sequence determined by the rules of operator rules of operator precedenceprecedence as outlined below from high to low level of as outlined below from high to low level of priority.priority.

operator operation order of evaluation (precedence), universal quantifier from left to right

existential quantifier negation from left to right, , conjunction from left to right

disjunctionequivalence

, , right implication from left to rightleft implicationright-left implication

Page 12: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

1212

EquivalenceEquivalence

P P Q is equivalent to Q is equivalent to P P Q Q

P P Q is equivalent to Q is equivalent to Q Q P P

P P Q is equivalent to Q is equivalent to

(( P P Q ) Q ) (( Q Q V) V)

(P (P Q) means Q) means P P Q Q

(P (P Q) means Q) means P P Q Q

Page 13: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

1313

Logical StatementsLogical Statements

We define a logical statement to be of the formWe define a logical statement to be of the form(Q1 or Q2 or … or Qk) (Q1 or Q2 or … or Qk) (P1 and P2 and (P1 and P2 and

… and Pn)… and Pn)where the P’s and Q’s are predicates also where the P’s and Q’s are predicates also known as known as literalsliterals or or termsterms. . The arrow pointing to the right is to be The arrow pointing to the right is to be understood as meaning "implies or drives", and understood as meaning "implies or drives", and the logic statement is to be understood as the logic statement is to be understood as meaning that Q1, meaning that Q1, oror Q2, and so on, imply P1, Q2, and so on, imply P1, andand P2, and so on. P2, and so on.

Page 14: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

1414

Logical StatementsLogical StatementsHorn clause: Horn clause: A restricted kind of logical statement. A restricted kind of logical statement. – Headed Horn clause:Headed Horn clause: if the left-hand side is a single term such as: if the left-hand side is a single term such as:– mammals(human) mammals(human) legs(human, 2). legs(human, 2).– Headless Horn clause:Headless Horn clause: if the left-hand side is empty such as: if the left-hand side is empty such as:– arms(human, 2).arms(human, 2).

Therefore, for the general form of a Horn clause such asTherefore, for the general form of a Horn clause such asQ Q (P1 and P2 and … and Pn) (P1 and P2 and … and Pn)

Or Or (P1 and P2 and … and Pn) (P1 and P2 and … and Pn) Q Q means that, Q is true if all the P’s are true. Q is called the means that, Q is true if all the P’s are true. Q is called the head of the clause (only one literal as the head of the head of the clause (only one literal as the head of the clause), and the (P1 and P2 and … and Pn) is the body of clause), and the (P1 and P2 and … and Pn) is the body of the clause. When logical statements are used for resolution, the clause. When logical statements are used for resolution, only Horn clauses, which simplify the resolution process are only Horn clauses, which simplify the resolution process are used.used.

Page 15: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

1515

ResolutionResolutionIn a logic programming system there is no indication In a logic programming system there is no indication about how a particular goal might be proved from a about how a particular goal might be proved from a given set of predicates.given set of predicates.A typical A typical inference ruleinference rule can be stated as: can be stated as:from the logical statements x from the logical statements x y and y y and y z, one can z, one can derive the logical statement x derive the logical statement x z. z.In other words, an inference rule allows you to construct In other words, an inference rule allows you to construct a new set of logical statements that are proved true from a new set of logical statements that are proved true from a given set of original logical statements that are already a given set of original logical statements that are already true. true. The essence of a The essence of a logic programlogic program is that from a collection is that from a collection of logical statements, known as facts and rules, a of logical statements, known as facts and rules, a desired fact, known as desired fact, known as queryquery or or goalgoal, might be proved to , might be proved to be true by application of the inference rule. be true by application of the inference rule.

Page 16: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

1616

ResolutionResolution

Resolution is a sequence of steps that deduction Resolution is a sequence of steps that deduction system chooses to derive a new logical system chooses to derive a new logical statement. statement. The resolution can be written symbolically in The resolution can be written symbolically in three equivalent ways as shown below.three equivalent ways as shown below.

P or QP or Q QQPP (P or Q) and (P or Q) and PPPP PPQQ QQ QQ

We say that Q is a logical consequences of We say that Q is a logical consequences of logical statements if whenever all of the logical logical statements if whenever all of the logical statements are interpreted as TRUE, so is Q. statements are interpreted as TRUE, so is Q.

Page 17: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

1717

ResolutionResolution

P P P1, P2, …, Pn-1, PnP1, P2, …, Pn-1, Pn

Q Q Q1, Q2, …, Qm-1, Qm Q1, Q2, …, Qm-1, Qm

If p matches Qm,If p matches Qm,

Q Q Q1, Q2, …, Qm-1, P1, P2, …, Pn-1, Q1, Q2, …, Qm-1, P1, P2, …, Pn-1, PnPn

That is That is

Q,P Q,P Q1, Q2, …, Qm-1, Qm, P1, P2, …, Q1, Q2, …, Qm-1, Qm, P1, P2, …, Pn-1, PnPn-1, Pn

Page 18: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

1818

Example 1Example 1

Suppose we have a set of logical Suppose we have a set of logical statements as the following:statements as the following:

1.1. not commited_crime or not got_caught or not commited_crime or not got_caught or in_jailin_jail

2.2. got_caught or not in_jailgot_caught or not in_jail

3.3. commited_crimecommited_crime

4.4. got_caughtgot_caught

5.5. not in_jailnot in_jail

Page 19: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

1919

Example 1Example 1

Using the resolution principles as the following, Using the resolution principles as the following, new logical statements are deduced.new logical statements are deduced.(1)(1) not commited_crime or not got_caught or not commited_crime or not got_caught or in_jailin_jail(3)(3) commited_crimecommited_crime(6)(6) not got_caught or in_jailnot got_caught or in_jail (new clause)(new clause)(4)(4) got_caughtgot_caught(7)(7) in_jailin_jail (new clause)(new clause)(5)(5) not in_jailnot in_jailFALSEFALSE

Page 20: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

2020

Example 1Example 1

By adding new generated logical statements the By adding new generated logical statements the set of logical statements are:set of logical statements are:(1)(1) not commited_crime or not got_caught or not commited_crime or not got_caught or in_jailin_jail(2)(2) got_caught or not in_jailgot_caught or not in_jail(6)(6) not got_caught or in_jailnot got_caught or in_jail(3)(3) commited_crimecommited_crime(4)(4) got_caughtgot_caught(5)(5) not in_jailnot in_jail(7)(7) in_jailin_jail

Page 21: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

2121

Example 1Example 1

This listing of logical statements shows an inconsistency This listing of logical statements shows an inconsistency by including the two logical statements, (5) and (7), by including the two logical statements, (5) and (7), because in which no one can be both in jail and not in jail because in which no one can be both in jail and not in jail at the same time. This list of logical statements shows its at the same time. This list of logical statements shows its resolution to FALSE.resolution to FALSE.In general, the resolution strategy is to search through a In general, the resolution strategy is to search through a set of logical statements, looking for two logical set of logical statements, looking for two logical statements, one of which contains a term X and the statements, one of which contains a term X and the other contains other contains X. Thus X. Thus

XXXX resolves toresolves toFALSEFALSE

Page 22: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

2222

Example 2Example 2

We need to show that happy(tom) is a logical We need to show that happy(tom) is a logical consequences of the set of logical statements, (1) consequences of the set of logical statements, (1) through (7). through (7). (1)(1) happy(tom) or happy(tom) or watching(tom, football) or watching(tom, football) or has(tom, supplies)has(tom, supplies)(2)(2) has(tom, supplies) or has(tom, supplies) or has(tom, beer) or has(tom, beer) or has(tom, pretzels)has(tom, pretzels)(3)(3) watching(tom, football) or watching(tom, football) or is_on(tv) or is_on(tv) or playing(cowboys)playing(cowboys)(4)(4) is_on(tv)is_on(tv)(5)(5) playing(cowboys)playing(cowboys)(6)(6) has(tom, beer)has(tom, beer)(7)(7) has(tom, pretzels)has(tom, pretzels)

Page 23: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

2323

Example 2Example 2

We know from the resolution strategy that We know from the resolution strategy that if:if:

(8)(8) happy(tom)happy(tom)

which is the negation of the query, and which is the negation of the query, and that (1)&(2)& … &(8) resolves to FALSE, that (1)&(2)& … &(8) resolves to FALSE, then the query happy(tom) is TRUE. Now, then the query happy(tom) is TRUE. Now, let’s look at the resolution steps.let’s look at the resolution steps.

Page 24: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

2424

Example 2Example 2

(8)(8)happy(tom)happy(tom)(1)happy(tom) or (1)happy(tom) or watching(tom, football) or watching(tom, football) or has(tom, supplies)has(tom, supplies)(9)(9)watching(tom, football) or watching(tom, football) or has(tom, supplies)has(tom, supplies)

(9)(9)watching(tom, football) or watching(tom, football) or has(tom, supplies)has(tom, supplies)(3)watching(tom, football) or (3)watching(tom, football) or is_on(tv) or is_on(tv) or playing(cowboys)playing(cowboys)(10)(10)has(tom, supplies) or has(tom, supplies) or is_on(tv) or is_on(tv) or playing(cowboys)playing(cowboys)

(10)(10)has(tom, supplies) or has(tom, supplies) or is_on(tv) or is_on(tv) or playing(cowboys)playing(cowboys)(2)has(tom, supplies) or (2)has(tom, supplies) or has(tom, beer) or has(tom, beer) or has(tom, pretzels)has(tom, pretzels)(11)(11)is_on(tv) or is_on(tv) or playing(cowboys) or playing(cowboys) or has(tom, beer) or has(tom, beer) or

has(tom, pretzels)has(tom, pretzels)

Page 25: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

2525

Example 2Example 2

(11)(11) is_on(tv) or is_on(tv) or playing(cowboys) or playing(cowboys) or has(tom, has(tom, beer) or beer) or has(tom, pretzels)has(tom, pretzels)

(4)(4) is_on(tv)is_on(tv)(12)(12)playing(cowboys) or playing(cowboys) or has(tom, beer) or has(tom, beer) or has(tom, has(tom,

pretzels)pretzels)

(12)(12) playing(cowboys) or playing(cowboys) or has(tom, beer) or has(tom, beer) or has(tom, pretzels)has(tom, pretzels)

(5)(5) playing(cowboys)playing(cowboys)(13)(13) has(tom, beer) or has(tom, beer) or has(tom, pretzels)has(tom, pretzels)

Page 26: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

2626

Example 2Example 2

(13)(13)has(tom, beer) or has(tom, beer) or has(tom, pretzels)has(tom, pretzels)

(6)has(tom, beer)(6)has(tom, beer)

(14)(14)has(tom, pretzels)has(tom, pretzels)

(14)(14)has(tom, pretzels)has(tom, pretzels)

(7)has(tom, pretzels)(7)has(tom, pretzels)

FALSEFALSE

Since we have derived FALSE from Since we have derived FALSE from happy(tom) happy(tom) and (1) through (7), we may conclude that Tom and (1) through (7), we may conclude that Tom is indeed happy. is indeed happy.

Page 27: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

2727

To be continuedTo be continued

UnificationUnification

Search StructuresSearch Structures

ApplicationsApplications

Prolog Prolog

Page 28: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

2828

UnificationUnification

Unification is a process of pattern matching to make Unification is a process of pattern matching to make terms identical in order to be discarded from both sides. terms identical in order to be discarded from both sides. Example:Example: Suppose there is a set of facts as the Suppose there is a set of facts as the following:following:– likes(Mary, food).likes(Mary, food).– likes(Mary, milk).likes(Mary, milk).– likes(Mahsa, milk).likes(Mahsa, milk).– likes(Mahsa, Mary).likes(Mahsa, Mary).

Conjunctions and the use of variables can be combined Conjunctions and the use of variables can be combined in order to form the queries.in order to form the queries.

Page 29: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

2929

ExampleExample

Assume we ask the following question from the Assume we ask the following question from the logic system: logic system: Is there anything that Mahsa and Is there anything that Mahsa and Mary both like? Mary both like? This question contains two This question contains two goals:goals:First, find out if there is some X (a variable) that First, find out if there is some X (a variable) that Mary likes.Mary likes.Second, find out if Mahsa likes whatever X is.Second, find out if Mahsa likes whatever X is.Two goals might be written as a conjunction of Two goals might be written as a conjunction of two subgoals like this:two subgoals like this:?-?- likes(Mary, X), likes(Mahsa, X).likes(Mary, X), likes(Mahsa, X).

Page 30: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

3030

ExampleExample

?-?- likes(Mary, X)likes(Mary, X) , likes(Mahsa, X). , likes(Mahsa, X).

likes(Mary, food).likes(Mary, food). likes(Mary, milk).likes(Mary, milk).

likes(Mahsa, milk).likes(Mahsa, milk). likes(Mahsa, Mary).likes(Mahsa, Mary).

Resolution: first goal is succeeded.Resolution: first goal is succeeded.

Unification: variable X is instantiated to Unification: variable X is instantiated to foodfood..

Next: attempt to satisfy the second goal. Next: attempt to satisfy the second goal.

Page 31: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

3131

ExampleExample

?-?- likes(Mary, food) likes(Mary, food) , likes(Mahsa, , likes(Mahsa, food).food).

likes(Mary, food).likes(Mary, food). likes(Mary, milk).likes(Mary, milk).

likes(Mahsa, milk).likes(Mahsa, milk). likes(Mahsa, Mary).likes(Mahsa, Mary).

Resolution: second goal is failed, becauseResolution: second goal is failed, becausethere is no term to be matched.there is no term to be matched.Next: attempt to re-satisfy the first goal.Next: attempt to re-satisfy the first goal.

Page 32: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

3232

ExampleExample

?-?- likes(Mary, X)likes(Mary, X) , likes(Mahsa, X). , likes(Mahsa, X).

likes(Mary, food).likes(Mary, food). likes(Mary, milk).likes(Mary, milk).

likes(Mahsa, milk).likes(Mahsa, milk). likes(Mahsa, Mary).likes(Mahsa, Mary).

Resolution: first goal is succeeded. Unification: variable X is instantiated to milk.Next: attempt to satisfy the second goal.

Page 33: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

3333

ExampleExample

likes(Mary, milklikes(Mary, milk)) , likes(Mahsa, milk), likes(Mahsa, milk). .

likes(Mary, food).likes(Mary, food). likes(Mary, milk).likes(Mary, milk).

likes(Mahsa, milk).likes(Mahsa, milk). likes(Mahsa, Mary).likes(Mahsa, Mary).

Resolution: second goal is succeeded.Unification: system notifies you of success with X = milk.Next: waits for a reply.The answer of this query indicates that, Mahsa and Mary both like milk.

Page 34: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

3434

Figure 10.1 The resolution and verification process for the query likes (Mary, X), likes (Mahsa, X)

© 2003 Brooks/Cole Publishing / Thomson Learning™

Page 35: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

3535

Logical program developmentLogical program development

Logical Analysis: Correctness that the correct Logical Analysis: Correctness that the correct logical statements are introduced and producedlogical statements are introduced and produced

Control Analysis: the efficiency when it is Control Analysis: the efficiency when it is performed entirely by the logic programming performed entirely by the logic programming control systemcontrol system

The difference of Resolution and Unification The difference of Resolution and Unification – The result of resolution is FALSE or TrueThe result of resolution is FALSE or True– The result of unification is Fail or some factsThe result of unification is Fail or some facts

Page 36: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

3636

Search StructureSearch Structure

Depth-first (DFS) Depth-first (DFS)

Of Breadth- first (BFS)Of Breadth- first (BFS)

Page 37: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

3737

ExampleExample

married(John)<= wife(Mary, John), married(John)<= wife(Mary, John), husband(John, Mary)husband(John, Mary)

married(John)<= husband(John, Mary)married(John)<= husband(John, Mary)

wife(Mary, John)wife(Mary, John)

husband(John, Mary)husband(John, Mary)

Page 38: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

3838

Figure 10.2 The search space for the query married(John)

Page 39: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

3939

Backward and Forward SearchBackward and Forward SearchBackwardBackward– Phase 1: A given goal is reduced to simple sub-goals Phase 1: A given goal is reduced to simple sub-goals

corresponding to basic facts or procedures. The process corresponding to basic facts or procedures. The process is repeated until the goal is represented as a tree having is repeated until the goal is represented as a tree having only simple sub-goals corresponding to basic facts or only simple sub-goals corresponding to basic facts or procedures as leaves. Such tree is called a tree on procedures as leaves. Such tree is called a tree on inference or resolution tree (see example below).inference or resolution tree (see example below).

– Phase 2: Searching for values for (Verifying) the simple Phase 2: Searching for values for (Verifying) the simple sub-goals and putting them together to form the sub-goals and putting them together to form the response. This process is actually called a backtracking.response. This process is actually called a backtracking.

ForwardForward– Counterpart for the Backward Chaining is called Counterpart for the Backward Chaining is called Forward Chaining Forward Chaining

ProcedureProcedure. The Forward Chaining Procedure deals with putting . The Forward Chaining Procedure deals with putting basic facts together basic facts together to infer all possible new factsto infer all possible new facts..

Page 40: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

4040

Example of backward searchExample of backward search

Basic Facts:Basic Facts:– B1(2). B1(3). B1(4). B1(2). B1(3). B1(4). – B21(1). B21(2). B21(3).B21(1). B21(2). B21(3).– B22(1). B22(3). B22(9). B22(1). B22(3). B22(9). – B3(1). B3(2). B3(5). B3(36). B3(1). B3(2). B3(5). B3(36).

Rules of Inference:Rules of Inference:– P(x) <= B1(x) P(x) <= B1(x) – P(x) <= P2 (x), B3(x)P(x) <= P2 (x), B3(x)– P2(x) <= B21(x), B22(x)P2(x) <= B21(x), B22(x)

Verifier Goal:Verifier Goal:?? P(1) P(1)truetrue

Page 41: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

4141

1 2

34

Page 42: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

4242

Example of forward searchExample of forward search

Basic Facts:Basic Facts:– B11(1) . B11(2) . B11(3) . B11(4) . B11(1) . B11(2) . B11(3) . B11(4) . – B12(3). B12(4). B12(5). B12(6). B12(3). B12(4). B12(5). B12(6). – B21(1) . B21(2) . B21(3) . B21(4) . B21(1) . B21(2) . B21(3) . B21(4) . – B22(1). B22(2). B22(3). B22(5). B22(1). B22(2). B22(3). B22(5).

Rules of Inference:Rules of Inference:– P(x) <= P1(x).P(x) <= P1(x).– P(x) <= P2(x)P(x) <= P2(x)– P1(x) <= B11(x) , B12(x)P1(x) <= B11(x) , B12(x)– P2(x) <= B21(x) , B22(x)P2(x) <= B21(x) , B22(x)

P(1), P(2), P(3), P(4)P1(3), P1(4) P2(1),p2(2),P2(3)

Page 43: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

4343

Page 44: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

4444

Page 45: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

4545

Application of Logic ProgrammingApplication of Logic Programming

Production systemsProduction systems– System StateSystem State– Production RulesProduction Rules– Control systemControl system

Page 46: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

4646

Figure 10.3 Computation model of a production system

© 2003 Brooks/Cole Publishing / Thomson Learning™

Page 47: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

4747

Reasoning systemsReasoning systems

A reasoning system consists of A reasoning system consists of – AgentsAgents– Communication paths Communication paths

Reasoning: a process by which new information Reasoning: a process by which new information is extracted from a knowledge base.is extracted from a knowledge base.Knowledge base: only a small portion of the Knowledge base: only a small portion of the knowledge is stated explicitly and more knowledge is stated explicitly and more knowledge could be made explicit via an knowledge could be made explicit via an inference mechanism.inference mechanism.

Page 48: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

4848

FeaturesFeatures

ModularityModularity– The system maintains a collection of modules rather The system maintains a collection of modules rather

than one huge one.than one huge one.

ReliabilityReliability– The problem solving can continue even if one of the The problem solving can continue even if one of the

agents fails.agents fails.

Heterogeneous unificationHeterogeneous unification– Problem-solving techniques and knowledge Problem-solving techniques and knowledge

representation may vary for different parts of a representation may vary for different parts of a reasoning problem.reasoning problem.

Page 49: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

4949

Prolog Platform InstallationProlog Platform Installation

http://www.swi-prolog.orghttp://www.swi-prolog.org//

Page 50: 1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.

5050

SummarySummary

First order predicate calculus and Horn First order predicate calculus and Horn clause clause

ResolutionResolution

UnificationUnification

Search structureSearch structure

ApplicationApplication