Download presentation

Presentation is loading. Please wait.

Published byKoby Pollman Modified over 2 years ago

1
1 Logic Programming Technical Foundation Second part of Cmput325 Fall 2004 R Greiner + B Price

2
2 Declarative Programming Motivation Warm Fuzzies What is Logic?... Logic Programming? Foundations: Terms, Substitution, Unification, Horn Clauses Proof (Resolution) Examples: List Processing; Integer Issues Logic / Theorem Proving Search Strategies Declarative/Procedural,... Other parts of Prolog “Impure'' Operators” --- NOT, ! Utilities ? Constraint Programming ? Bayesian Belief Nets

3
3 What is Logic? Logic is formal system for reasoning Reasoning is inferring new facts from old Given { All men are mortal. Socrates is a man. } infer (conclude, reason that,...) Socrates is mortal. What is role of Logic within CS? Foundation of discrete mathematics Automatic theorem proving Hardware design/debugging Artificial intelligence (Cmput366) Components: Syntax (What does it look like?) Reasoning/ProofTheory (New facts from old) Semantics (What should it do?)

4
4 Logic Programming Program Logic Formula Execution of program Proving a theorem User: 1. Specifies WHAT is true 2. Asks if something else follows Prolog: Answers question. By comparison, in Procedural Programming (C, JAVA, Fortran,...) user must … decide on data-structure explicitly write procedures: search, match, substitute write diff programs for father( X, tom) vs father(tom, Y )

5
5 What is Logic? Syntax: Set of Expressions ? Well-formed or not? SEQUENCE of Symbols Accept: The boys are at home. at(X, home) :- boy(X). Reject: boys.home the angrily democracy X(at), xY=, Boy(1X,( ) :- Accept Reject Given { All men are mortal. Socrates is a man. } infer (conclude, reason that,...) Socrates is mortal. Proof Process: Given Believed statements, Determine other Believed statements. { s 1,..., s n } ⊢ P s Semantics: Which expressions are Believed ? John's mother is Mary. T John's mother is Fred. F Colorless green ideas sleep furiously. F

6
6 Concept of PROLOG PROgramming in LOGic Sound Reasoning Engine 1. User asserts true statements. User asserts { All men are mortal. Socrates is a man. } 2. User poses query. “Is Socrates mortal?” “Who/what is mortal?” 3. Prolog provides answer (Y/N, binding). “Yes” “Socrates ''

7
7 Tying Prolog to Logic Syntax: Horn Clauses (aka Rules + Facts) Terms Proof Process: Resolution Substitution Unification Semantics (From Predicate Calculus … + not )

8
8 Terms BNF: term ::= constant | variable | functor constant ::= atom starting w/lower case variable ::= atom starting w/upper case functor ::= constant ( tlist ) tlist ::= “” | term {, tlist } Examples of term : a1 b fred constant X Yc3 Fred _4 variable married(fred) g(a, f(Yc3), b) functor Ground Term term with no variables f(q) g(f(w), w1(b,c)) are ground f(Q) g(f(w), w1(B,c)) are not

9
9 Substitution A Substitution is a set { t 1 /v 1 t 2 /v 2... t n /v n } where v i are distinct variables t i are terms that do not use any v j Examples: + { a/X } { a/X b/Y f(a,W)/Z } { W/X f(W)/Y W/Z } - { a/f(X) } { a/X b/X } { f(X)/X } { f(Y)/X g(q)/Y } - { f(g(Y))/X g(q)/Y }

10
10 Applying a Substitution Given “t ” is term, result of applying to t Small Examples: X {a/X} a f(X) {a/X} f(a) Example, using t = f( a, h(Y,b), X ) t -- a term -- a substitution { b/X } f( a, h(Y,b), b ) { b/X f(Z)/Y } f( a, h(f(Z),b), b ) { Z/X f(Z,a)/Y } f( a, h(f(Z,a),b), Z ) { Z/W } f( a, h(Y,b), X )

11
11 Applying a Substitution f( a, h(Y,b), X ) { b/X } = f( a, h(Y,b), b ) f( a, h(Y,b), X ) { b/X f(Z)/Y } = f( a, h(f(Z),b), b ) f( a, h(Y,b), X ) { Z/X f(Z,a)/Y } = f( a, h(f(Z,a),b), Z ) f( a, h(Y,b), X ) { Z/W } = f( a, h(Y,b), X ) need not include all variables in t can include variables not in t.

12
12 Composition of Substitutions ○ is composition of substitutions , For any term t, t [ ○ ] = (t ) . Example: f(X) [{ Z/X} ○ { a/Z}] = (f(X) { Z/X}) {a/Z} = f(Z) { a/Z} =f(a) ○ is typically a substitution Eg: { a/X} ○ { b/Y} = { a/X, b/Y} { Z/X} ○ { a/Z} = { a/X, a/Z}

13
13 Examples: t 1 t 2 unifier term f(b,c) f(b,c) {} f(b,c) f(X,b) f(a,Y){ a/X b/Y } f(a,b) f(a,b) f(c,d) * f(a,b) f(X,X) * f(X,a) f(Y,Y){ a/X a/Y } f( a, a ) f(g(U),d) f(X,U){ d/U g(d)/X } f( g(d),d ) f(X) g(X) * f(X,g(X)) f(Y,Y) * f(X) f(Y) { Y/X } f(Y) Unifiers t 1 and t 2 are unified by iff t 1 = t 2 . Then is called a unifier t 1 and t 2 are unifiable NB t 1 and t 2 are symmetric -- either can have variables

14
14 Multiple Unifiers Unifier for t 1 = f(X) and t 2 = f(Y) … { X/Y } and { Y/X } make sense, but { a/Y a/X } has irrelevant constant { X/Y g/W } has irrelevant variable ( W ) Adding irrelevant constants/bindings unifiers best one?? t 1 = t 2 = { X/Y } f(X) { Y/X } f(Y) { a/Y a/X } f(a) { g(b,Z))/Y g(b,Z)/X) } f(g(b,Z) ) { X/Y W/g) } f(X)

15
15 Quest for Best Unifier Wish list: No irrelevant constants So {X/Y} preferred over { a/Y, a/X } No irrelevant bindings So {X/Y} preferred over { X/Y, f(4,Z)/W } Spse 1 has constant where 2 has variable 1 = { a/X, a/Y }, 2 = {Y/X} Then substitution s.t. 2 ○ = 1 = {a/Y} : {Y/X} ○ {a/Y} = { a/X, a/Y } Spse 1 has extra binding over 2 1 = {a/X, b/Y}, 2 = {a/X} Then substitution s.t. 2 ○ = 1 = {b/Y} : {a/X} ○ {b/Y} = {a/X, b/Y} INFERIOR unifier = composition of Good Unifier + another substitution

16
16 Most General Unifier is MGU for t 1 and t 2 iff unifies t 1 and t 2, and : unifier of t 1 and t 2, substitution s.t. ○ = Ie, for all terms t, t = (t ) Eg, = {X/Y} is mgu for f(X) and f(Y) Consider unifier = {a/X a/Y} Use = { Y/a }: f(X) = f(X) {a/X a/Y} = f(a) f(X)[ ○ ] = [f(X) ] = [f(X) {Y/X}] = f(Y) {a/X a/Y} = f(a) Similarly, f(Y) = f(a) = f(Y) [ ○ ] ( is not a mgu, as ¬ ’s.t. ○ ’= )

17
17 MGU --- Example#2 A mgu for f( W, Z, g(Z) ) & f( X, h(X), Y ) { W/X } W/X h(X)/Z h(W)/Z g(Z)/Y h(W)/Z g(h(W))/Y

18
18 MGU (con't) Notes: If t 1 and t 2 are unifiable, then mgu. Can be more than 1 mgu but they differ only in variable names. Not every unifier is a mgu. A mgu uses constants only as necessary. Implementation: fast algorithm that computes a mgu of t 1 and t 2, if one exists; or reports failure. Slow part is verifying legal substitution: none of v i appear in any t j. Avoid by resetting Prolog's occurscheck parameter.

19
19 MGU Procedure Recursive Procedure MGU( x, y ) If x=y then Return() If Variable(x) then Return( MguVar(x,y) ) If Variable(y) then Return( MguVar(y,x) ) If Constant(x) or Constant(y) then Return( False ) If Not(Length(x) = Length(y)) then Return( False ) g [] For i = 0.. Length(x) s MGU( Part(x,i), Part(y,i) ) g Compose( g, s ) x Substitute( x, g ) y Substitute( y, g ) Return( g ) End Procedure MguVar (v,e) If Includes(v,e) then Return( False ) Return( [v/e] ) End

20
20 Prolog's Syntax BNF: Horn ::= literal . | literal :- llist . llist ::= literal {, llist } literal ::= term Examples: father(john, sue). father(odin, X). parent(X, Y) :- father(X, Y). gparent(X, Z) :- parent(X, Y), parent(Y, Z). How to read as predicate calculus? father(john, sue) X. father(odin, X). X,Y. father(X,Y) parent(X,Y) X,Y,Z.parent(X,Y)} & parent(Y,Z) gparent(X,Z)

21
21 Relation to Predicate Calculus In general: t. x 1, …, x m. t [called “atomic formula”] t :- t 1, …, t n. x 1, …, x m. t 1, …, t n t. [called “(production) rule”] Can write any Predicate Calculus Expression in Conjunctive Normal Form Horn clauses are CNF, with ONE Positive Literal Horn CNF Predicate Calculus expressions that canNOT be written as Horn Clauses. Axioms A v B

22
22 What User Really Types > sicstus SICStus 3.11.2 (x86-linux-glibc2.3): Wed Jun 2 11:44:50 CEST 2004 Licensed to cs.ualberta.ca | ?- [user]. % For user to enter ``Assert-fact'' mode. % consulting user... | on(aa,bb). | on(bb,cc). | above(X,Y) :- on(X,Y). | % Typing ^D exits ``assert'' mode. % consulted user in module user, 0 msec 608 bytes yes % Prolog's answer to most operations | ?- on(X,Y). X = aa, Y = bb ? ; % User types “;” to ask for ANOTHER answer X = bb, Y = cc ? ; no % Prolog's no means there are no other answers | ?- on(aa,bb). % User asks a question. yes % Prolog's answer | ?- on(aa,Y). % User's second question Y = bb ? % Prolog's answer: a binding list yes % Prolog's statement that there was answer | ?-

23
23 What User Really Types -- II > sicstus SICStus 3.11.2 (x86-linux-glibc2.3): Wed Jun 2 11:44:50 CEST 2004 Licensed to cs.ualberta.ca | ?- [file1]. % file ”file1” contains facts, rules % file1 consulted 120 byes 0.3333 sec. yes | ?- on(aa,b10). no % Prolog's answer: not derivable | ?- on(X,b10). no % Again: NO entailments | ?- above(bb,cc). yes % More than simple look-up! | ?- above(bb,W). W = cc ; % Prolog finds answer; user wants another no % but this is only one… | ?-

24
24 Prolog's Proof Process User provides KB: Knowledge Base (List of Horn Clauses --- axioms) : Query (aka Goal, Theorem) -- Literal 1. Who is mortal? mortal(X). 2. Is Socrates mortal? mortal(socrates). Prolog finds a Proof of , from KB, if one exists & substitution for γ's variables: σ KB ⊢ P { σ } KB 1 ⊢ P mortal(X){ socrates/X } Failure (otherwise)

25
25 Proof Process (con’t) Prolog finds ``Top-Down'' (refutation) Proof … returns bindings Actually returns LIST of σ i s [one for each proof] { socrates/X } { plato/X } { freddy/X }...

26
26 Examples of Proofs: I (DB retrieval) Using KB 1 = on(aa, bb). (1) on(bb, cc). (2) above(X, Y) :- on(X,Y). (3) Query 1 : on(aa, bb) on(aa, bb ) success (1) Hence KB 1 ├ P on(aa, bb){} empty substitution ( Like Data Base retrieval )

27
27 Examples of Proofs: IIa (Variables) Query 2 : on(aa, Y) on(aa, Y) success –{bb/Y} (1) Y=bb KB 1 ├ P on(aa, Y) { bb/Y } Using KB 1 = on(aa, bb). (1) on(bb, cc). (2) above(X, Y) :- on(X,Y). (3)

28
28 Examples of Proofs: IIb (Variables) Query 3 : on( X, Y ) KB 1 ├ P on(X, Y){ aa/X, bb/Y }... on(aa,bb) KB 1 ├ P on(X, Y){ bb/X, cc/Y }... on(bb,cc) Using KB 1 = on(aa, bb). (1) on(bb, cc). (2) above(X, Y) :- on(X,Y). (3) (2) X =bb, Y=cc success {bb/X,cc/Y} success {aa/X,bb/Y} (1) X =aa, Y=bb Either is sufficient!

29
29 Ie, KB 1 ├ P on(aa,bb) Examples of Proofs: III (Failure) Query 4 : on(aa, b10) Using KB 1 = on(aa, bb). (1) on(bb, cc). (2) above(X, Y) :- on(X,Y). (3) Query 5 : on(X, b10) Ie, KB 1 ├ P on(X,b10) for any value of X. Attempts

30
30 Examples of Proofs: IVa (Rules) Query 6 : above(bb, cc) Ie, KB 1 ├ P above(bb,cc) on(bb,cc) (3) X=bb, Y=cc success (2) Using KB 1 = on(aa, bb). (1) on(bb, cc). (2) above(X, Y) :- on(X,Y). (3)

31
31 Examples of Proofs: IVb (Rules) Query 7 : above(bb, W) Ie, KB 1 ├ P above(bb,W){cc/W} on(bb, W) (3) X=bb, Y=W success –{cc/W} (2) Using KB 1 = on(aa, bb). (1) on(bb, cc). (2) above(X, Y) :- on(X,Y). (3)

32
32 Examples of Proofs: V (big) Query 7 : above(aa, cc) on(aa,cc) (3) X=bb, Y=W Using KB 2 = on(aa, bb). (1) on(bb, cc). (2) above(X, Y) :- on(X,Y). (3) above(X,Y) :- on(X,Z), above(Z,Y). (4) on(aa,Z)&above(Z,cc) (4) X=aa, Y=cc (1) Z=bb above(bb,cc)

33
33 Examples of Proofs: V (big) above(aa,cc) on(aa, bb). (1) on(bb, cc). (2) above(X, Y) :- on(X,Y). (3) above(X,Y):- on(X,Z),above(Z,Y). (4) on(aa,Z),above(Z,cc) (4) X=aa, Y=cc (1) Z=bb above(bb, cc) on(bb,cc) (3) X=bb, Y=cc (2) Z=cc above(cc, cc) (2) successon(cc,cc) (3) on(aa,cc) (3) X=bb, Y=cc on(bb,Z), above(Z, cc) (4) X=bb, Y=cc on(cc,Z),above(Z,cc) (4) X=cc, Y=cc aa bb cc

34
34 Examples of Proofs: VI (many answers) Query 9 : above( X, Y) Answers { X=aa, Y=bb } above( aa, bb) (3), (1) { X=bb, Y=cc } above( bb, cc) (3), (2) { X=aa, Y=cc } above( aa, cc) (4), (1), (3), (2) { X=c1, Y=c2 } above( c1, c2) (5) Using KB 3 = on(aa, bb). (1) on(bb, cc). (2) above(X, Y) :- on(X,Y). (3) above(X,Y) :- on(X,Z), above(Z,Y). (4) above(d1, d2) (5)

35
35 Prolog's Proof Process A goal is either a sequence of literals (conjunction), the special goal `` success '' (eg, ) on(X,Y)p(X,5), q(X)success Sequence of (sub)goals G 1, G 2,..., G n is a top-down proof of G 1 (from KB) iff 1. G n = success, and 2. G i is a SUBGOAL (in KB) of G i-1, i=2,3,... n

36
36 Subgoals Subgoals of G = { g 1,..., g r } in KB: Rule 1: If atomic axiom ``t '' in KB where t and g i have mgu , then { g 1 ,..., g i-1 , g i+1 ,..., g r } is a subgoal of G. (If r=1, then `` success '' is subgoal of G.) Rule 2: If rule ``t :- t 1,..., t_k '' in KB where t and g i have mgu , then { t 1 ,..., t k , g 1 ,..., g i-1 , g i+1 ,..., g r } is a subgoal of G.

37
37 Example of Subgoals -- I Subgoals of are = {X/A, Y/B} using Rule 2, (3) = {X/A, Y/B} using Rule 2, (4) = {c1/A, c2/B} using Rule 1, (5) KB 3 = on(aa, bb). (1) on(bb, cc). (2) above(X, Y) :- on(X,Y). (3) above(X,Y) :- on(X,Z), above(Z,Y). (4) above(c1, c2) (5) above(A,B) on(A,B) on(A,Z), above(Z, B) success

38
38 Example of Subgoals -- II Subgoals of are = {aa/A, bb/Z1} using Rule 3, (1) [first literal] = {bb/A, bb/Z1} using Rule 3, (2) [first literal] = {c1/Z1, c2/B} using Rule 3, (5) [second literal] = {X/Z1, B/Y} using Rule 4, (3) [second literal] = {X/Z1, Y/B} using Rule 4, (4) [second literal] on(aa, bb). (1) on(bb, cc). (2) above(X, Y) :- on(X,Y). (3) above(X,Y):-on(X,Z),above(Z,Y). (4) above(c1, c2) (5) above(A,Z 1 ), above(Z 1, B) above(bb,B) above(cc, B) on(A,c1) on (Z 1,B), on(A, Z 1 ) on (Z 1,Z), above(Z,B), on(A, Z 1 )

39
39 Comments wrt Prolog's Proof Procedure found during proof Prolog returns these overall mgu's 1-by-1. Prolog uses specific strategy called “SLD Resolution” Variable bindings Unifier

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google