Download Presentation
## Logic Programming

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

**Logic Programming**• Logic programming had its beginning in early 1970s. It was primarily introduced by Kowalski (1974) and Colmerauer et al. (1973) following the earlier work in automated theorem proving and Artificial Intelligence. • Logic programming is based on first order predicate logic. Clauses are very common in logic programming which have special forms of first order predicate logic (FOL) formula. • Program in logic programming is a collection of clauses. Clause in logic programming adopts a special clausal notation.**Queries are solved using resolution principle.**• Several logic programming languages have been developed since then reflecting different choices of clause selection. • In the family of logic programming there are two categories viz., Prolog and its extensions (Prolog-II, IC-Prolog, MU-Prolog, LPA Prolog, Edinburgh Prolog etc.) based on sequential execution and other languages, such as PARALOG, concurrent Prolog, GHC etc are based on parallel execution. • Prolog is an acronym for Programming in Logic and is based on logic programming concepts • The treatment of non determinism distinguishes between sequential and parallel languages.**A clause in logic programming is represented in a clausal**notation given by A1, …, Ak B1,…, Bt , where Aj are positive literals and Bk are negative literals. Conversion of a Clause into Clausal Representation • A clause in FOL is a closed formula of the form, (x1)… (xn) (L1V… V Lm), where each Lk , (1 k n) is a literal and xk , (1 k n) are all the variables occurring in L1, …, Lm .**Let us remove the prefix (x1)… (xn) from the above**representation for the sake of simplicity because all the variables appearing in a clause are universally quantified . • Therefore, a clause is represented as L1V L2 V… V Lm , where Lk , (1 k n) are literals free from quantifiers. Now separate positive and negative literals in the clause as follows.: (L1V L2 V… V Lm) (A1 V … VAk V ~ B1 V …V~ Bt), where m = k + t, Aj, (1 j k)are positive literals and Bj , (1 j t) are negative literals.** (A1 V … VAk ) V ~ (B1 … Bt)** (B1 … Bt) (A1 V … VAk ) {since P Q ~ P V Q} • Clausal notation is written in the form : (A1 V … VAk ) (B1 … Bt) Or A1, …, Ak B1,…, Bt . • Here Aj , (1 j k) are positive literals and Bi , (1 i t) are negative literals. • It must be noted that interpretation of A B is same as B A .**In clausal notation, all variables are assumed to be**universally quantified. • Bi , (1 i t)(negative literals) are called antecedents and Aj , (1 j k) (positive literals) are called consequents. • Commas in antecedent and consequent denote conjunction and disjunction respectively.**Logic Programming Concepts**• A program clause is a clause of the form A B1, …, Bn that contains precisely one positive literal. It is also called conditional rule. • A in a program clause is called headand (B1, …, Bn) is called body of the rule. • The informal semantics of A B1, …, Bn in logic is that "for all interpretations, if B1... Bn is true, then A is true". • Formal semantics of the rule A B1, …, Bn in logic programming is that "A is proved if B1, ….Bn are proved together".**Here A, B1, B2, ….Bn are predicates along with the**arguments and the symbol denotes "implied by”. • A unit clause is a clause of the form A . This is unconditional clause. Alternatively, a program clause with only one positive literal and with no negative literals is called unit clause. It has an empty body always • Its informal semantics in logic is that "for all interpretations, A is true”. • Formal interpretation of unit clause in logic programming is that "A is valid".**A logic program is a finite sequence of program clauses.**• A goal clause is a clause of the form B1, …, Bn with no positive literal or an empty consequent. Each Bk (1 k n) is called a sub goal of the goal clause. • The meaning of a logic program P is the set of ground goals deducible from P. • An empty clause is the clause with empty consequent and empty antecedents. This clause is to be understood as a contradiction. It is graphically denoted by square / rectangle.**Applying the results of FOL to the logic programs, a goal G**with respect to a program P (finite set of clauses) is solved by showing that the set of clauses P { ~ G} is unsatisfiable or there is a resolution refutation of P { ~ G}. • If so, then G is logical consequence of a program P. The basic constructs of logic programming are inherited from FOL. • There are three basic statements: facts, rules and queries. These are special forms of clauses as already mentioned earlier.**Fact (Unit clause): The simplest kind of statement is**called fact which states a relation between objects such as BROTHER ( john, mike) . • This fact says that john is a brother of mike. BROTHER is a predicate symbol that shows relationship between two individuals named john and mike. Rule(Program Clause): If we have to define a rule “x is a grandfather of y, if x is a father of z and z is a parent of y” using logic programming convention, then we write GRANDFATHER (x, y) FATHER (x, z) , PARENT (z, y)**Query(Goal): Query is used to retrieve information from**logic program. • Answering a query with respect to a logic program is to determine whether the query is a logical consequence of a program or not. • Logical consequences are obtained by applying resolution rule. • Query can be simpleor conjunctive. • A simple query ( B) is one which has only one sub goal and is a logical consequence of a program if B is deducible from it.**A conjunctive query is conjunction of sub goals ( B1, B2,**….Bn ) which is a logical consequence of a program if all the sub goals in the query are logical consequences of it. • Within each type of query, a goal(s) can be ground or non ground. • In ground query, the goal(s) contains constants. Answer to ground query is either yes or no depending upon whether it is a logical consequence of a logic program or not.**In non ground query, the goal(s) should have atleast one**variable as an argument. • Comma “,” in the query denotes logical conjunction ( ) to separate sub goals and is different from the comma used to separate the arguments of the predicate in the goals of the query. • Selection of the sub goal to be reduced in a conjunctive query is arbitrary in logic programming. In any given resolvent, all the sub goals must be reduced.**Constants:Constants are numbers (e.g., 23, 5 etc), string**(e.g., 'mary', 'robert' etc) and symbols (e.g., mary, robert etc) a descriptive names in lower case letters Variables: Variable stands for an unspecified but single entity rather than a store location in memory. • The scope of the variable is a clause in which it appears. Variables can appear in facts, rules or goals. • Variables in a fact or rule are universally quantified whereas in goals are existentially quantified.**Example: Consider the following logic program.**GRANDFATHER (x, y) FATHER (x, z) , PARENT (z, y) PARENT (x, y) FATHER (x, y) PARENT (x, y) MOTHER (x, y) FATHER ( abraham, robert) FATHER ( robert, mike) • In FOL, above program is represented by a set of clauses as: S = { GRANDFATHER (x, y) V ~ FATHER (x, z) V ~ PARENT (z, y), PARENT(x, y) V ~ FATHER (x, y), PARENT(x, y) V ~ MOTHER (x, y), FATHER ( abraham, robert), FATHER ( robert, mike) }**Let us number the clauses of S as follows:**i. GRANDFATHER (x, y) V ~ FATHER (x, z) V ~ PARENT(z, y) ii. PARENT(x, y) V ~ FATHER (x, y) iii. PARENT(x, y) V ~ MOTHER (x, y), iv. FATHER ( abraham, robert) v. FATHER ( robert, mike) Simple queries : (a) Ground Query Query: “Is abraham a grandfather of mike ?” Goal: GRANDFATHER (abraham, mike).**In FOL, ~ GRANDFATHER (abraham, mike) is negation of Goal**= GRANDFATHER (abraham, mike). • Include {~Goal} in the set S and show using resolution refutation that S {~ Goal} is unsatisfiable in order to conclude the Goal. • Let ~ Goal is numbered as ( vi) in continuation of first five clauses of S listed above. vi. ~ GRANDFATHER (abraham, mike)**( i ) ( vi )**{x / abraham, y / mike} ( iv) ~ FATHER (abraham, z) V ~ PARENT (z, mike) {z / robert} ~ PARENT (robert, mike) ( ii) ~ FATHER (robert, mike) (v) Answer: Yes**(b) Non ground queries**Query: “Does there exist x such that x is a father of robert ?” {Who is father of robert?} FATHER (x, robert). • Include ~ FATHER (x, robert) in the given set S and show that we get a refutation with some most general unifier. • Hence it is proved that FATHER (x, robert) logical consequence of S with binding of { x / abraham}.**Similarly the following simple non ground query can be**solved Query: “Does there exist x such that abraham is a father of x?" {abraham is father of whom?} FATHER (abraham, x). Query : “Do there exist x and y such that x is a father of y?" { who is father of whom?} FATHER (x, y).**Conjunctive queries:**(a) Ground Query Query:“Is abraham a father of robert and robert is a father of mike?” FATHER(abraham, robert), FATHER ( robert, mike). Answer: yes (b) Non ground queries: FATHER (abraham, x), FATHER ( x, mike). Answer: x = robert**Resolution: Resolution is a powerful inference technique.**There are three important cases of resolution. • In backward chaining, one of the clauses resolved is a goal clause. • In forward chaining, one of the clauses is always a given fact. • In rule collapsing, two rules are resolved with first rule’s left side matching some thing with the right side of second rule and combine into one rule. The resolution in all three cases is illustrated in the following ways.**Backward Chaining: Goal clause is resolved with one of the**given clause. In clausal form: i. a b, c, d. ii. a. {goal clause} Result of resolution: b, c, d. {conjunctive sub goals} In logic form i. a V ~ b V ~ c V ~ d ii. ~ a Result of resolution: ~ b V ~ c V ~ d**Forward Chaining: One of the clauses is always a given fact.**In clausal form: i. a b, c, d. ii. c. Result of resolution: a b, d. In logic form i. a V ~ b V ~ c V ~ d ii. c Result of resolution: a V ~ b V ~ d**Rule Collapsing:Two given rules are resolved with one**rule’s left side matching with the right side of other rule and combine into one rule. In clausal form: i. a b, c, d. ii. c e, f. Result of resolution: a b, d, e, f. In logic form i. a V ~ b V ~ c V ~ d ii. c V ~ e V ~ f Result of resolution: a V ~ b V ~ d V ~ e V ~ f**Abstract Interpreter for Logic Program**• An abstract interpreter for logic program takes program P and a goal /query as an input. • It performs computations and shows that the goal is a logical consequence of P or not. • The heart of computational model of logic programs is unification. • The following interpreter performs computations for a goal (ground or non ground).**Input: A program P and a goal G**Output: If G is a logical consequence of P then for ground query, output Yes and for non ground query, output the most general unifier obtained while satisfying goal G else output No. Method: • Initialize Resolvent with G; Progress = true; • While ((Resolvent empty) and (Progress = true) ) do { • Choose a goal from Resolvent and store it in S;**if ( a clause A B1,….,Bn where, n 0 from**P such that S and A unify with most general unifier (mgu) ) then replace S by B1,….,Bn where, n 0 in the Resolvent after applying to the Resolvent else Progress = false } {end of while} • If (Resolvent = empty and Progress = true) then ( if = then output Yeselse output )else output No. • Stop.**For some computations, there is only one clause from the**program that can reduce each subgoal. • Such a computation is called deterministic. • If there are alternative choices for the sub goals, then computation becomes non deterministic. • To handle non deterministic computation, develop all possible reduction in parallel or sequentially using some clause selection strategy. • Generate all possible computation paths and correct solutions are reported.**Each iteration of the while loop of an interpreter**corresponds to a single application of modus ponen rule which means that: Given a rule of the form A B1, ….Bn , where, n 1 and the facts B1’ , ...., Bn’, using modus ponen law A’ can be deduced if A’ B1’,…,Bn’ is an instance of A B1, …,Bn . • Proof tree of a ground goal consists of nodes and edges which represent the goals reduced during the computation. • Search tree of a non ground goal with respect to a program is a trace of all possible paths in the computation of the goal.**Consider the following logic.**SON(x, y) FATHER(y, x) , MALE(x) FATHER( abraham, robert) FATHER( robert, mike) MALE(abraham) MALE(robert) MALE(mike) Generate Proof tree for a goal : SON( mike, robert)**Proof Tree:** SON( mike, robert) FATHER(robert, mike), MALE(mike)**Search tree for SON(x, y)** SON(x, y) FATHER(y, x), MALE(x) {y / abraham, x / robert} {y / robert, x / mike} MALE(robert) MALE(mike)**Prolog Language**• Prolog is unique in its ability to infer facts from the given facts and rules. • In Prolog, an order of clauses in the program and goals in the body of a clause is fixed. • There are two major issues in logic programming which have been resolved in Prolog. • First issue is the arbitrary choice of goal in the resolvent to be reduced. • Second one is the non deterministic choice of the clause from the program to effect the reduction.**In Prolog, first issue is resolved by choosing the first sub**goal from the resolvent and the second issue is resolved by choosing the first clause matched searching sequentially the clauses from top to bottom in a program. • A stack scheduling policy is adopted. It maintains the resolvent as a stack. • It pops the top sub goal for reduction, and pushes the derived goals on the resolvent stack. • Prolog is an approximate realization of logic programming computation model on a sequential machine.**General syntax of Prolog**• A Prolog program is a collection of facts and rules. Brief syntax of Prolog is given below: • Constants are numerals and symbols such as, 4, mary, etc. • String is a string of characters and is enclosed within single quotes e.g., 'This is a string'. • Functionand predicate names must start with alphabet and are formed by using lower case letters, numerals and underscore ( _ ).**Variable names are formed similar to function and predicate**names but it must start with upper case letter. Normally, we use X, Y, Z, ... for variables. • Clause (representing fact or rule) in Prolog is terminated by full stop (.). • Goal to a Prolog program is given after symbols ?-. Goal might be simple or conjunction of sub goal(s) terminated by full stop.**Arithmetic assignment is achieved by a predicate called**‘is’ which is an infix operator. Evaluation of goal ‘X is Expression’ is done by first evaluating • Expression according to the evaluation rules of arithmetic and is unified with left hand side variable X in order to succeed the goal. • All variables on right hand side of ‘is’ must be known at the time of its execution otherwise the goal will fail.**Examples:**1. X is 2 + 5 succeeds by unifying X with 7. 2. Y is X -3 succeeds by unifying Y with 4. (Here X = 7 is available prior to executing this goal). 3. 7 is X fails as left side of 'is' has to be variable. Equality operator denoted by ‘=‘ is also an infix operator. When the goal T1 = T2 (pronounced as ‘T1 equals T2’) is attempted to be satisfied by Prolog, it succeeds if T1 and T2match. • Here T1and T2are any terms.It is to be noted that T1 = T2 or T2 = T1 are same.**Examples:**1.3 = X, succeeds by unifying a variable X with 3. 2. mary = mery , fails. 3. X = likes(ram, sita), succeeds by unifying X with likes(ram, sita). 4. X = 4, succeeds by unifying a variable X with 4. 5. love(X, sita) = love(ram, Y), succeeds by unifying a variable X with ram and Y with sita.**Non equality is denoted \= which is opposite of equality.**The goal T1 \= T2succeeds if the goal T1 = T2 fails otherwise the goal T1 \= T2 fails. • It is pronounced as ‘T1 is not equals to T2’. Examples: 1. likes(ram, X) \= likes(sita, ram) succeeds. 2. father(X, Y) \= father(john, mery) fails. 3. 4 \= 3 succeeds. 4. mary = mery succeeds.**Prolog Program**• Prolog program is a finite sequence of program clauses. • Informally, we can define Prolog program to be a set of facts and rules. It is based on Horn clauses. • Representation of a Horn clause in Prolog: A :- B1 , B2 , …, Bn . (1) • The symbol of logic programming is represented by :- in Prolog . • ‘A’ is positive atom and B1, …and Bn are negative atoms.**Clause given in (1) is a conditional rule which is**interpreted as "A is true if B1 … Bn are simultaneously true". • A clause with no antecedent is a fact that is always true and is denoted by A. . • :- B1, …, Bn. means a conjunctive query interpreted as "if B1, … and Bn are true, then what is the consequent?". The goal in Prolog is represented as ?- B1, …, Bn. Here Bi , (1 i n) are called sub goals.**The symbols A and Bi , (1 i n) represent predicate**names with arguments, if any. The scope of variables is only a clause in which it is appearing. • Comment in Prolog are preceded by percentage symbol ‘%’ if single line and enclosed within /* comments */ for multiple lines. Examples: • Prolog rule for computing square of a number. % square(X, Y) - succeeds if Y is a square of X. square(X, Y) :- Y is X * X. Query: ?- square(2, Y). Answer: Y = 4**The goals ?- square(X, 9) and ?- square(3, 9) fail because**left side of 'is' has to be a variable and the variables appearing in the right side of 'is' should be bound. • If we define above rule using equality operator ( = ) such as square(X, Y) :- Y = X * X. • Then the goal ?- square(X, 3 * 3) succeeds with X = 3 whereas the goal ?- square(X, 9) fails because equality is for comparing the terms and not for evaluating an expression on right side of equal operator.**Prolog rule to compare two terms for equality.**/* equal(X, Y) - succeeds if terms X and Y are equal */ equal(X, Y) :- X = Y. Goals: ?- equal(5, 5). Answers: Yes ?- equal(5, X). Answers: X = 5 ?- equal (5, 6). Answers: No**Prolog Control Strategy**• While executing Prolog goal, leftmost goal is chosen instead of an arbitrary one as is done in logic programming. • Prolog is non deterministic because a goal can be satisfied using more than one rules. • Non determinism is resolved by choosing a program clause using sequential search in a program from top to bottom for a unifiable clause.**Prolog contains three basic control strategies.**• Forward movement • Matching (Unification) • Backward movement (Backtracking) Forward Movement: • Choose a rule by searching sequentially in the program from top to bottom whose head matches with the goal with possible unifier. • Remember the position of the matched rule.Join the rule body in front of the sequence of sub goals to be solved. • Repeat until either goal is satisfied or is failed.**Unification:**• Unification is a process of matching or finding the most general unifier (mgu ). • Constant matches with the same constant. • A variable matches with any constant and becomes instantiated. • A variable matches with another variable. Examples : • love(X, sita) and love(X, Y) unify with love(ram, sita) with mgu as {X / ram} and {X / ram, Y / sita} respectively. • love(ram, sita) does not unify with love(sita, ram), love(X, mary), love(john, X) etc.