1 / 28

Logic Programming

Logic Programming Logic programming is based on first order predicate logic. Clauses are very common in logic programming which are special forms of FOL. Program in logic programming is a collection of clauses. Clause in logic programming adopts a special clausal notation.

cameo
Télécharger la présentation

Logic Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Logic Programming • Logic programming is based on first order predicate logic. Clauses are very common in logic programming which are special forms of FOL. • Program in logic programming is a collection of clauses. Clause in logic programming adopts a special clausal notation. • Queries are solved using resolution principle. • A clause in logic programming is represented in a clausal notation as A1, …, Ak B1,…, Bt , where Aj are positive literals and Bk are negative literals.

  2. Conversion of a Clause into Clausal Notation • 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 . • Remove the prefix (x1)… (xn) for the sake of simplicity because all the variables appearing in a clause are universally quantified . • Now a clause is represented as L1V L2 V… V Lm , where Lk , (1  k  n) are literals free from quant.

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

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

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

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

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

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

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

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

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

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

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

  14. 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) }

  15. 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) }

  16. 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 ?"  GRANDFATHER (abraham, mike).

  17. 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) • Resolution tree is given as follows:

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

  19. (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). 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).

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

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

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

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

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

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

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

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

  28. 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 path in the computation of the goal.

More Related