COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications

COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications

325 Views

Download Presentation
## COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**COSC3306:Programming ParadigmsLecture 8:**DeclarativeProgramming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003**Contents**• Logic Programming • First order predicate calculus and Horn clause • Resolution • Unification • Search structure • Application**Logic Programming**• Logic programming refers to a paradigm that uses a form of symbolic logic as a programming language. • The property of logic programming model is • {Logic Program} = {Logic Statements} + {Control Strategy} • where logic statements consists of a set of rules and facts expressing the relationships between objects.**Logic Programming**• Control strategy is known as resolution or the sequence of steps the deduction system chooses to answer a question from the logic system. • The form of logic used in logic programming languages is the first-order predicate calculus, which is a paradigm of formally expressing logical statements. A logical statement is a statement that is either true or false.**First-order predicate calculus**• Constant: A symbol that represents an object such as names and numbers also known as atoms, since it can not be broken down into subobjects. • Logical Statement: An unconditional term, sometimes called a fact, that represents relations between objects and asserts that something is a quality, attribute or property of something else.**First-order predicate calculus**• An example • Suppose that we want to express a logical statement (a fact) that Fred likes Elizabeth. This can be converted as: • Likes(Fred, Elizabeth). • In general, a relation that we would say in English as: SubjectVerbObject, can be converted into a logical statement as: Verb(Subject, Object), or more generally as: • Relationship(Object1, Object2). • The relationship is called a predicate and the objects that are related are called arguments or parameters.**First-order predicate calculus**• Variable: A symbol that represents an unspecified object, meaning that it can represent different objects at different times of the computation logic. For example, • integer(X) • Function: A term that represents a predefined computation. It can be used to express the derivation of another predicate. For example, • integer(X) integer(successor(X)).**First-order predicate calculus**• Connector : A symbol that connects two or more terms (literals). In the following table, the names, symbols, examples and the meanings associated with the logical connectives of the predicate calculus are outlined. • name symbol example meaning • conjunction x y x and y • disjunction x y x or y • equivalence x y x is equivalent to y • right implication x y x implies y • left implication x y y implies x • right-left implication x y x implies y and y implies x • negation x negation of x**First-order predicate calculus**• Quantifier: A symbol that introduces variables such as called for all known as universal quantifier and called there exists known as existential quantifier. For example, • X (man(X) human(X)) • means that, for all x such that x is a man, then x is a human. And • X (human(X) man(X)) • means that, thereexists an X such that if X is a human then x is a man. Because the scope of the quantifiers is the variables in which they are attached, one can use the parentheses in order to extend the scope of the universal and existential quantifiers.**First-order predicate calculus**• Punctuation: A symbol that is used in the structure of predicates and logic statements such as left and right parentheses, comma and the period.**First-order predicate calculus**• Precedence: Evaluation of operators in a precise sequence determined by the rules of operator precedence as outlined below from high to low level of priority. operatoroperationorder of evaluation (precedence) , universal quantifier from left to right existential quantifier negation from left to right , , conjunction from left to right disjunction equivalence , , right implication from left to right left implication right-left implication**Equivalence**• P Q is equivalent to P Q • P Q is equivalent to Q P • P Q is equivalent to • ( P Q ) ( Q V) • (P Q) means P Q • (P Q) means P Q**Logical Statements**• We define a logical statement to be of the form • (Q1 or Q2 or … or Qk) (P1 and P2 and … and Pn) • where the P’s and Q’s are predicates also known as literals or terms. • The arrow pointing to the right is to be understood as meaning "implies or drives", and the logic statement is to be understood as meaning that Q1, or Q2, and so on, imply P1, and P2, and so on.**Logical Statements**• Horn clause: A restricted kind of logical statement. • Headed Horn clause: if the left-hand side is a single term such as: • mammals(human) legs(human, 2). • Headless Horn clause: if the left-hand side is empty such as: • arms(human, 2). • Therefore, for the general form of a Horn clause such as • Q (P1 and P2 and … and Pn) • Or (P1 and P2 and … and Pn) Q • 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 clause), and the (P1 and P2 and … and Pn) is the body of the clause. When logical statements are used for resolution, only Horn clauses, which simplify the resolution process are used.**Resolution**• In a logic programming system there is no indication about how a particular goal might be proved from a given set of predicates. • A typical inference rule can be stated as: • from the logical statements x y and y z, one can derive the logical statement x z. • In other words, an inference rule allows you to construct a new set of logical statements that are proved true from a given set of original logical statements that are already true. • The essence of a logic program is that from a collection of logical statements, known as facts and rules, a desired fact, known as query or goal, might be proved to be true by application of the inference rule.**Resolution**• Resolution is a sequence of steps that deduction system chooses to derive a new logical statement. • The resolution can be written symbolically in three equivalent ways as shown below. • P or Q QP (P or Q) and P • P P • Q Q Q • We say that Q is a logical consequences of logical statements if whenever all of the logical statements are interpreted as TRUE, so is Q.**Resolution**• P P1, P2, …, Pn-1, Pn • Q Q1, Q2, …, Qm-1, Qm • If p matches Qm, • Q Q1, Q2, …, Qm-1, P1, P2, …, Pn-1, Pn • That is • Q,P Q1, Q2, …, Qm-1, Qm, P1, P2, …, Pn-1, Pn**Example 1**• Suppose we have a set of logical statements as the following: • not commited_crime or not got_caught or in_jail • got_caught or not in_jail • commited_crime • got_caught • not in_jail**Example 1**• Using the resolution principles as the following, new logical statements are deduced. • (1) not commited_crime or not got_caught or in_jail • (3) commited_crime • (6) not got_caught or in_jail (new clause) • (4) got_caught • (7) in_jail (new clause) • (5) not in_jail • FALSE**Example 1**• By adding new generated logical statements the set of logical statements are: • (1) not commited_crime or not got_caught or in_jail • (2) got_caught or not in_jail • (6) not got_caught or in_jail • (3) commited_crime • (4) got_caught • (5) not in_jail • (7) in_jail**Example 1**• This listing of logical statements shows an inconsistency by including the two logical statements, (5) and (7), 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 resolution to FALSE. • In general, the resolution strategy is to search through a set of logical statements, looking for two logical statements, one of which contains a term X and the other contains X. Thus • X • X resolves to • FALSE**Example 2**• We need to show that happy(tom) is a logical consequences of the set of logical statements, (1) through (7). • (1) happy(tom) or watching(tom, football) or has(tom, supplies) • (2) has(tom, supplies) or has(tom, beer) or has(tom, pretzels) • (3) watching(tom, football) or is_on(tv) or playing(cowboys) • (4) is_on(tv) • (5) playing(cowboys) • (6) has(tom, beer) • (7) has(tom, pretzels)**Example 2**• We know from the resolution strategy that if: • (8) happy(tom) • which is the negation of the query, and that (1)&(2)& … &(8) resolves to FALSE, then the query happy(tom) is TRUE. Now, let’s look at the resolution steps.**Example 2**(8)happy(tom) (1)happy(tom) or watching(tom, football) or has(tom, supplies) (9)watching(tom, football) or has(tom, supplies) (9)watching(tom, football) or has(tom, supplies) (3)watching(tom, football) or is_on(tv) or playing(cowboys) (10)has(tom, supplies) or is_on(tv) or playing(cowboys) (10)has(tom, supplies) or is_on(tv) or playing(cowboys) (2)has(tom, supplies) or has(tom, beer) or has(tom, pretzels) (11)is_on(tv) or playing(cowboys) or has(tom, beer) or has(tom, pretzels)**Example 2**(11) is_on(tv) or playing(cowboys) or has(tom, beer) or has(tom, pretzels) (4) is_on(tv) • playing(cowboys) or has(tom, beer) or has(tom, pretzels) (12) playing(cowboys) or has(tom, beer) or has(tom, pretzels) (5) playing(cowboys) (13) has(tom, beer) or has(tom, pretzels)**Example 2**(13)has(tom, beer) or has(tom, pretzels) (6)has(tom, beer) (14)has(tom, pretzels) (14)has(tom, pretzels) (7)has(tom, pretzels) FALSE Since we have derived FALSE from happy(tom) and (1) through (7), we may conclude that Tom is indeed happy.**To be continued**• Unification • Search Structures • Applications • Prolog**Unification**• Unification is a process of pattern matching to make terms identical in order to be discarded from both sides. • Example: Suppose there is a set of facts as the following: • likes(Mary, food). • likes(Mary, milk). • likes(Mahsa, milk). • likes(Mahsa, Mary). • Conjunctions and the use of variables can be combined in order to form the queries.**Example**• Assume we ask the following question from the logic system: Is there anything that Mahsa and Mary both like? This question contains two goals: • First, find out if there is some X (a variable) that Mary likes. • Second, find out if Mahsa likes whatever X is. • Two goals might be written as a conjunction of two subgoals like this: • ?- likes(Mary, X), likes(Mahsa, X).**Example**• ?- likes(Mary, X) , likes(Mahsa, X). • likes(Mary, food). likes(Mary, milk). • likes(Mahsa, milk). likes(Mahsa, Mary). Resolution: first goal is succeeded. Unification: variable X is instantiated to food. Next: attempt to satisfy the second goal.**Example**• ?- likes(Mary, food) , likes(Mahsa, food). • likes(Mary, food). likes(Mary, milk). • likes(Mahsa, milk). likes(Mahsa, Mary). Resolution: second goal is failed, because there is no term to be matched. Next: attempt to re-satisfy the first goal.**Example**• ?- likes(Mary, X) , likes(Mahsa, X). • likes(Mary, food). likes(Mary, milk). • likes(Mahsa, milk). likes(Mahsa, Mary). Resolution: first goal is succeeded. Unification: variable X is instantiated to milk. Next: attempt to satisfy the second goal.**Example**• likes(Mary, milk), likes(Mahsa, milk). • likes(Mary, food). likes(Mary, milk). • likes(Mahsa, milk). 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.**© 2003 Brooks/Cole Publishing / Thomson Learning™**Figure 10.1 The resolution and verification process for the query likes (Mary, X), likes (Mahsa, X)**Logical program development**• Logical Analysis: Correctness that the correct logical statements are introduced and produced • Control Analysis: the efficiency when it is performed entirely by the logic programming control system • The difference of Resolution and Unification • The result of resolution is FALSE or True • The result of unification is Fail or some facts**Search Structure**• Depth-first (DFS) • Of Breadth- first (BFS)**Example**• married(John)<= wife(Mary, John), husband(John, Mary) • married(John)<= husband(John, Mary) • wife(Mary, John) • husband(John, Mary)**Backward and Forward Search**• Backward • Phase 1: A given goal is reduced to simple sub-goals corresponding to basic facts or procedures. The process is repeated until the goal is represented as a tree having only simple sub-goals corresponding to basic facts or procedures as leaves. Such tree is called a tree on inference or resolution tree (see example below). • Phase 2: Searching for values for (Verifying) the simple sub-goals and putting them together to form the response. This process is actually called a backtracking. • Forward • Counterpart for the Backward Chaining is called Forward Chaining Procedure. The Forward Chaining Procedure deals with putting basic facts together to infer all possible new facts.**Example of backward search**• Basic Facts: • B1(2). B1(3). B1(4). • B21(1). B21(2). B21(3). • B22(1). B22(3). B22(9). • B3(1). B3(2). B3(5). B3(36). • Rules of Inference: • P(x) <= B1(x) • P(x) <= P2 (x), B3(x) • P2(x) <= B21(x), B22(x) • Verifier Goal: • ? P(1) • true**2**1 4 3**Example of forward search**• Basic Facts: • B11(1) . B11(2) . B11(3) . B11(4) . • B12(3). B12(4). B12(5). B12(6). • B21(1) . B21(2) . B21(3) . B21(4) . • B22(1). B22(2). B22(3). B22(5). • Rules of Inference: • P(x) <= P1(x). • P(x) <= P2(x) • P1(x) <= B11(x) , B12(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)**Application of Logic Programming**• Production systems • System State • Production Rules • Control system**© 2003 Brooks/Cole Publishing / Thomson Learning™**Figure 10.3 Computation model of a production system**Reasoning systems**• A reasoning system consists of • Agents • Communication paths • Reasoning: a process by which new information is extracted from a knowledge base. • Knowledge base: only a small portion of the knowledge is stated explicitly and more knowledge could be made explicit via an inference mechanism.**Features**• Modularity • The system maintains a collection of modules rather than one huge one. • Reliability • The problem solving can continue even if one of the agents fails. • Heterogeneous unification • Problem-solving techniques and knowledge representation may vary for different parts of a reasoning problem.**Prolog Platform Installation**• http://www.swi-prolog.org/**Summary**• First order predicate calculus and Horn clause • Resolution • Unification • Search structure • Application