Presentation is loading. Please wait.

Presentation is loading. Please wait.

Logic Programming Part 2: Semantics James Cheney CS 411.

Similar presentations


Presentation on theme: "Logic Programming Part 2: Semantics James Cheney CS 411."— Presentation transcript:

1 Logic Programming Part 2: Semantics James Cheney CS 411

2 Resolution Resolution principle: –If A 1... A n  B and B,C 1,...,C m  D –Then A 1... A n,C 1,...,C m  D Proof search: –To prove B, try to prove B  False –Apply resolution until LHS is empty.

3 Unification In Prolog, logic variables X,Y,Z can be substituted by any other term Unification: Algorithm for finding substitution making two terms equal

4 Examples f(X,[Y,Z]) == f(g(Z),W) –unifies with X = g(Z), W = [Y,Z] f(X,g(X)) == f(2,g(Y)) –Doesn’t iunify: X = 2 and X = g(Y) impossible

5 More Examples f(X) == X –Doesn’t unify! –X = f(X) = f(f(X)) =... f(X,Y) == f(Y,g(X)) –Doesn’t unify! (Why?)

6 Example f XY f X Y g

7 f XY f X Y g

8 XY X Y g

9 Y X g Answer: X = Y

10 Example Y Y g Answer: X = Y

11 Example Answer: X = Y Y = g(Y)

12 Example Answer: X = Y Y = g(Y) Problem: In equation Y = f(Y), Y occurs circularly. There is no finite term satisfying this equation. Checking that this is not the case is called the occurs check. For efficiency reasons, PROLOG skips this check.

13 Robinson’s Algorithm Consider a general unification problem P = t 1 == u 1, t 2 == u 2,... Reduce the problem by replacing one equation with a set of equations Succeed if all equations reducible, else fail.

14 Robinson’s Algorithm Two constants unify if they are equal. c == c, P  P Else fail.

15 Robinson’s Algorithm Two function applications unify if the symbols are equal, and the corresponding arguments unify. f(t 1,...t n ) == f(u 1,...,u n ), P  t 1 == u 1,... t n == u n, P Must have equal number of arguments

16 Robinson’s Algorithm Two variables unify if they are the same. X == X, P  P

17 Robinson’s Algorithm Otherwise, a variable X unifies with a term t provided X does not occur in t. X = t, P  P[t/X] (if X not in FV(t)) Proceed by substituting t for X in P.

18 Operational Semantics In general, we have –a program P = {clauses}, –a goal G = {atomic goals}, –and a substitution  : Var  Term Base case: If there are no goals then succeed (P,{},  )  Yes 

19 Operational Semantics Inductive case: Suppose state is (P,G,  ) with G nonempty. Pick a p(t1,...,tn) in G. Assume clause p(u1,...,un) :- G’ in P. Assume  ’ unifies p(t1,...,tn) and p(u1,...,un) Then: (P,G,q)  (P,  ’G’,  ’  ).


Download ppt "Logic Programming Part 2: Semantics James Cheney CS 411."

Similar presentations


Ads by Google