190 likes | 281 Views
Logic Programming Part 2: Semantics. James Cheney CS 411. 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. Unification.
E N D
Logic Programming Part 2: Semantics James Cheney CS 411
Resolution • Resolution principle: • If A1 ... An B and B,C1,...,Cm D • Then A1 ... An,C1,...,Cm D • Proof search: • To prove B, try to prove B False • Apply resolution until LHS is empty.
Unification • In Prolog, logic variables X,Y,Z can be substituted by any other term • Unification: Algorithm for finding substitution making two terms equal
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
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?)
Example f f X Y Y g X
Example f f X Y Y g X
Example X Y Y g X
Example Answer: X = Y Y g X
Example Answer: X = Y Y g Y
Example Answer: X = Y Y = g(Y)
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.
Robinson’s Algorithm • Consider a general unification problem P = t1 == u1, t2 == u2, ... • Reduce the problem by replacing one equation with a set of equations • Succeed if all equations reducible, else fail.
Robinson’s Algorithm • Two constants unify if they are equal. c == c, P P Else fail.
Robinson’s Algorithm • Two function applications unify if the symbols are equal, and the corresponding arguments unify. f(t1,...tn) == f(u1,...,un), P t1 == u1,... tn == un, P • Must have equal number of arguments
Robinson’s Algorithm • Two variables unify if they are the same. X == X, P P
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.
Operational Semantics • In general, we have • a program P = {clauses}, • a goal G = {atomic goals}, • and a substitution q : VarTerm • Base case: If there are no goals then succeed (P,{},q) Yes q
Operational Semantics • Inductive case: Suppose state is (P,G,q) with G nonempty. • Pick a p(t1,...,tn) in G. • Assume clause p(u1,...,un) :- G’ in P. • Assume q’ unifies p(t1,...,tn) and p(u1,...,un) • Then: (P,G,q) (P,q’G’, q’q).