1 / 11

Logic Programming

Logic Programming. Motivation. Predicate Logic Clause Form. Horn Clauses. A backtracking Horn-clause resolver. Sample logic programs. Motivation.

najila
Download Presentation

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 Motivation. Predicate Logic Clause Form. Horn Clauses. A backtracking Horn-clause resolver. Sample logic programs. CSE 341, S. Tanimoto Logic Programming -

  2. Motivation 1. Reduce the programming burden: it would be nice to have a system that could simply accept the necessary information and the objective (goal), and then figure out its own solution. 2. Have a program that looks more like its own specification. 3. Take advantage of logical inference to automatically get many of the consequences of the given information. CSE 341, S. Tanimoto Logic Programming -

  3. Predicate Logic Every Macintosh computer uses electricity. (all x) (Macintosh(x) implies UsesElectricity(x)) variables: x, y, z, etc. constants: a, b, c, etc. function symbols: f, g, etc. Predicate symbols: P, Q, Macintosh, UsesElectricity quantifiers: all, exists Logical connectives: not, implies, and, or. ~, ->, &, V. CSE 341, S. Tanimoto Logic Programming -

  4. Clause Form (all x) (Macintosh(x) -> UsesElectricity(x) UsesElectricy(x) V Not Macintosh(x). Any boolean formula can be put into conjunctive normal form (CNF). If not Y then X and not Z. Y or (X & not Z) (Y or X & (Y or not Z) (X V Y) & (Y V ~Z) clauses: (X V Y), (Y V ~Z) X, Y, and ~Z are called literals. CSE 341, S. Tanimoto Logic Programming -

  5. Horn Clauses Horn clause: at most one unnegated literal (X V Y) is not a Horn clause. (Y V ~Z) is a Horn clause. “If X is the mother of Y and Y is a parent of Z, then X is a grandmother of Z.” (m(X,Y) & p(Y, Z)) -> g(X,Z). grandmother(X, Z) provided mother(X, Y) and parent (Y, Z) g(X,Z) :- m(X,Y), p(Y,Z). ; Edinburgh Prolog syntax ((grandmother x z) ; head (not (mother x y)) ; subgoal 1 (not (parent y z))) ; subgoal 2 CSE 341, S. Tanimoto Logic Programming -

  6. Unification of Literals A substitution is a set of term/variable pairs. { f(a)/x, b/y, z/w } A unifier for a pair of literals is a substitution that when applied to both literals, makes them identical. P(x, a), P(f(a), y) have the unifier { f(a)/x, a/y } P(x), P(y) have the unifier { a/x, a/y }, but they also have the unifier { x/y }. The latter is more general because after unifying with { x/y} we get P(x) whereas with the other it is P(a), yet we can obtain the latter from the former with an additional substitution { a/x }. CSE 341, S. Tanimoto Logic Programming -

  7. Horn-Clause Resolution Example of resolution with the grandmother rules: g(X, Z) :- m(X, Y), p(Y, Z). m(X, Y) :- p(X, Y), f(X). g(X, Z) :- p(X, Y), f(X), p(Y, Z). more generally, from: P :- Q1, Q2. Q1 :- R1, R2. we obtain the resolvent: P :- R1, R2, Q2 CSE 341, S. Tanimoto Logic Programming -

  8. A Backtracking Horn-Clause Resolver Maintain the rules in a database of rules. Accept a query (goal) as a positive unit clause, e.g., P(a, b). Put this goal on the subgoal list. Repeatedly try to satisfy the next clause on the subgoal list as follows: Find a rule in the database whose head unifies with the subgoal. Apply the same unifier to the literals in the body of that rule and replace the subgoal by these new literals. If the subgoal list is empty, stop. The combined set of unifiers includes the solution. CSE 341, S. Tanimoto Logic Programming -

  9. Example Logic Program (SETF DATABASE3 '( ; Rules: ((GRANDMOTHER X Z) (MOTHER X Y) (PARENT Y Z)) ((PARENT X Y) (MOTHER X Y)) ((PARENT X Y) (FATHER X Y)) ; Facts: ((MOTHER MARY JOHN)) ((FATHER JOHN BILL)) ((FATHER PETER JAMES)) )) (QUERY'((GRANDMOTHER X BILL))) CSE 341, S. Tanimoto Logic Programming -

  10. Invertible List Manipulation (SETF DATABASE4 '( ;;; (APPEND L1 L2 L3) is true provided L3 is the ;;; result of appending L1 and L2. ((APPEND NILL L L)) ((APPEND L NILL L)) ((APPEND (CONS X L1) L2 (CONS X L3)) (APPEND L1 L2 L3)) )) (QUERY '((APPEND (CONS X (CONS Y NILL)) (CONS Z (CONS W NILL)) L)) ) SOLUTION: L=(CONS X (CONS Y (CONS Z (CONS W NILL)))) CSE 341, S. Tanimoto Logic Programming -

  11. Invertible List Manipulation (Cont.) (QUERY '((APPEND L (CONS Z (CONS W NILL)) (CONS X (CONS Y (CONS Z (CONS W NILL)))))) ) SOLUTION: L=(CONS X (CONS Y NILL)) Functional programming: evaluation is one-way. Logic programming: evaluation can be two-way. CSE 341, S. Tanimoto Logic Programming -

More Related