Logic Programming Outline Motivation Examples The Grandmother relation

  • Slides: 22
Download presentation
Logic Programming Outline: Motivation. Examples: The Grandmother relation. Formulation in Prolog. Formulation using Lisp

Logic Programming Outline: Motivation. Examples: The Grandmother relation. Formulation in Prolog. Formulation using Lisp syntax (Mock Prolog). List manipulation using logic. Logic, clauses, and Horn clauses. Unification. The Backtracking Search Resolver. CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 1

Motivation 1. Reduce the programming burden. 2. System should simply accept the necessary information

Motivation 1. Reduce the programming burden. 2. System should simply accept the necessary information and the objective (goal), and then figure out its own solution. 3. Have a program that looks more like its own specification. 4. Take advantage of logical inference to automatically get many of the consequences of the given information. CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 2

Sample Problem For someone (call him or her X) to be the grandmother of

Sample Problem For someone (call him or her X) to be the grandmother of someone else (call him or her Y), X must be the mother of someone (call him or her Z) who is a parent of Y. Someone is a parent of another person, if that someone is either the mother or the father of the other person. Mary is the mother of Stan. Gwen is the mother of Alice. Valery is the mother of Gwen. Stan is the father of Alice. The question: Who is a grandmother of Alice? CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 3

Sample Prolog Program: grandmother. pl grandmother(X, Y) : - mother(X, Z), parent(Z, Y). parent(X,

Sample Prolog Program: grandmother. pl grandmother(X, Y) : - mother(X, Z), parent(Z, Y). parent(X, Y) : - mother(X, Y). parent(X, Y) : - father(X, Y). mother(mary, stan). mother(gwen, alice). mother(valery, gwen). father(stan, alice). CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 4

Sample Prolog Session Welcome to SWI-Prolog (Version 3. 3. 2) Copyright (c) 1990 -2000

Sample Prolog Session Welcome to SWI-Prolog (Version 3. 3. 2) Copyright (c) 1990 -2000 University of Amsterdam. All rights reserved. For help, use ? - help(Topic). or ? - apropos(Word). ? - [grandmother]. % grandmother compiled 0. 00 sec, 1, 312 bytes Yes ? - grandmother(X, alice). X = mary ; X = valery ; No ? CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 5

Example with Lisp Syntax ; courses. lsp Some prerequisite relationships ((prereq cse 142 cse

Example with Lisp Syntax ; courses. lsp Some prerequisite relationships ((prereq cse 142 cse 143)) ((prereq cse 143 cse 373)) ((prereq cse 373 cse 415)) ; To prove X must come before Y, it’s ; enough to show that X is a prereq. for y. ((before X Y) (prereq X Y)) ; Alternatively, one can show X must come ; before Y by showing that for some Z, ; X is a prereq of Z and Z comes before y. ((before X Y) (prereq X Z) (before Z Y)) ; Before(x, y) v ~Prereq(x, z) v ~Before(z, y) ; ( <head> <-- <atomic formula 1> <af 2>. . . <afn>) CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 6

A Session (load ”prolog. lsp” ) (setq *database* ’( ((prereq cse 142 cse 143)).

A Session (load ”prolog. lsp” ) (setq *database* ’( ((prereq cse 142 cse 143)). . . ) ) (query ’((prereq cse 142, X))) X=CSE 143 CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 7

A session (cont) (query a ’(prereq X Y)) X = CSE 142; Y =

A session (cont) (query a ’(prereq X Y)) X = CSE 142; Y = CSE 143; X = CSE 143; Y = CSE 373; X = CSE 373; Y = CSE 415; CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 8

A session (cont) (query ’(before X cse 373)) X = cse 143; X =

A session (cont) (query ’(before X cse 373)) X = cse 143; X = cse 142 ; CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 9

Lists in Prolog: Transformations can be Expressed Declaratively colors([r, g, b]). ? - colors(L).

Lists in Prolog: Transformations can be Expressed Declaratively colors([r, g, b]). ? - colors(L). L = [r, g, b] ? - colors([X|Y]). X = r, Y = [g, b] % X is the head. Y is the tail. ? - mylist([a, [b, c]]). CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 10

Defining Predicates on Lists car([X|L], X). cdr([X|L], L). cons(X, L, [X|L]). ? - car([a,

Defining Predicates on Lists car([X|L], X). cdr([X|L], L). cons(X, L, [X|L]). ? - car([a, X = a ? - cdr([a, X = [b, c] ? - cons(a, X = [a, b, c], X). [b, c], X). c] CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 11

concat a. k. a. append concat([], L, L). concat([X|L 1], L 2, [X|L 3])

concat a. k. a. append concat([], L, L). concat([X|L 1], L 2, [X|L 3]) : - concat(L 1, L 2, L 3). ? - concat([a, b, c], [d, e], X). X = [a, b, c, d, e] ? - concat(X, [d, e], [a, b, c, d, e]). X = [a, b, c] CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 12

concat used backwards ? - concat(X, Y, [x, y, z]). X = [] Y

concat used backwards ? - concat(X, Y, [x, y, z]). X = [] Y = [x, y, z] ; X = [x] Y = [y, z] ; X = [x, y] Y = [z] ; X = [x, y, z] Y = [] ; No CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 13

Logic Prog. vs Functional Prog. Functional programming: evaluation is one-way. Logic programming: evaluation can

Logic Prog. vs Functional Prog. Functional programming: evaluation is one-way. Logic programming: evaluation can be two-way. CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 14

Steps in Writing a Logic Program 1. Formulate assertions in English and translate them

Steps in Writing a Logic Program 1. Formulate assertions in English and translate them into Logic, then into Prolog or Mock Prolog. or 2. Program directly in Prolog or Mock Prolog. CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 15

Predicate Logic “Every Macintosh computer uses electricity. ” (all x) (Macintosh(x) implies Uses. Electricity(x))

Predicate Logic “Every Macintosh computer uses electricity. ” (all x) (Macintosh(x) implies Uses. Electricity(x)) variables: x, y, z, etc. constants: a, b, c, etc. function symbols: f, g, etc. Predicate symbols: P, Q, Macintosh, Uses. Electricity quantifiers: forall, exists Logical connectives: not, implies, and, or. ~, ->, &, v. CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 16

Clause Form (all x) (Macintosh(x) -> Uses. Electricity(x) v Not Macintosh(x). Any boolean formula

Clause Form (all x) (Macintosh(x) -> Uses. Electricity(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. Each clause is a disjunction of literals. CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 17

Horn Clauses Horn clause: at most one unnegated literal (X v Y) is not

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) : mother(X, Y), parent(Y, Z). % head % subgoal 1 % subgoal 2 CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 18

How Does Matching Work in Prolog? Literals are matched using a method called unification.

How Does Matching Work in Prolog? Literals are matched using a method called unification. The scope of a variable in Prolog is a single clause. Unification involves substituting “terms” for variables. CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 19

Unification of Literals A substitution is a set of term/variable pairs. { f(a)/x, b/y,

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 415 -- (c) S. Tanimoto, 2002 Logic Programming 20

Horn-Clause Resolution Example of resolution with the grandmother rules: g(X, Z) : - m(X,

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 : - Q 1, Q 2. Q 1 : - R 1, R 2. we obtain the resolvent: P : - R 1, R 2, Q 2 CSE 415 -- (c) S. Tanimoto, 2002 Logic Programming 21

A Backtracking Horn-Clause Resolver Maintain the rules in a database of rules. Accept a

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 415 -- (c) S. Tanimoto, 2002 Logic Programming 22