1 / 38

Declarative Programming

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, ...

nishan
Download Presentation

Declarative 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. 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

  2. Given • { All men are mortal. • Socrates is a man. } • infer (conclude, reason that, ...) • Socrates is mortal. What is Logic? • Logic is formal system for reasoning • Reasoning is inferring new facts from old • 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?)

  3. Logic Programming • Program  Logic FormulaExecution of program  Proving a theorem • User: 1. Specifies WHAT is true 2. Asks if something else followsProlog: 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)

  4. Given • { All men are mortal. • Socrates is a man. } • infer (conclude, reason that, ...) • Socrates is mortal. 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 • Proof Process: Given Believed statements, Determine other Believed statements. { s1, ..., sn } ⊢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

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

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

  7. 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

  8. Substitution • A Substitution is a set { t1/v1 t2/v2 ... tn/vn }where vi are distinct variables ti are terms that do not use any vj • 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 }

  9. t -- a term  -- a substitution 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 )

  10. 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.

  11. 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}

  12. Unifiers • t1 and t2 are unified by  iff t1  = t2 . Then  is called a unifier t1 and t2 are unifiable • Examples: t1 t2 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) • NB t1 and t2 are symmetric -- either can have variables

  13.  t1  = t2  = { 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) Multiple Unifiers • Unifier for t1 = f(X) and t2 = 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??

  14. 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

  15. Most General Unifier •  is MGU for t1 and t2 iff • unifies t1 and t2 , and • : unifier of t1 and t2 , 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.  ○’=  )

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

  17. MGU (con't) • Notes: • If t1 and t2 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 t1 and t2, if one exists; or reports failure. • Slow part is verifying legal substitution: none of vi appear in any tj. Avoid by resetting Prolog's occurscheck parameter.

  18. 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

  19. 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)

  20. Relation to Predicate Calculus • In general:t. x1, …, xm. t[called “atomic formula”]t :- t1, …, tn. x1, …, xm. t1, …, tn  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

  21. 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(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 | ?- 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 | ?-

  22. 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… | ?-

  23. Prolog's Proof Process • User provides • KB: Knowledge Base (List of Horn Clauses --- axioms) • : Query (aka Goal, Theorem) -- Literal1. 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 { σ } KB1 ⊢Pmortal(X){ socrates/X } • Failure (otherwise)

  24. 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 } ...

  25. (1) success empty substitution Examples of Proofs: I (DB retrieval) • Using KB1 = • Query 1 :on(aa, bb) on(aa, bb) • Hence KB1 ├Pon(aa, bb){} ( Like Data Base retrieval )

  26. (1) Y=bb success –{bb/Y} Examples of Proofs: IIa (Variables) • Using KB1 = on(aa, Y) • Query 2 : on(aa, Y) • KB1 ├Pon(aa, Y) { bb/Y }

  27. (1) X=aa, Y=bb (2) X=bb, Y=cc success {aa/X,bb/Y} success {bb/X,cc/Y} Examples of Proofs: IIb (Variables) • Using KB1 = • Query 3 : on( X, Y ) Either is sufficient! • KB1 ├Pon(X, Y){ aa/X, bb/Y } ... on(aa,bb) • KB1 ├Pon(X, Y){ bb/X, cc/Y } ... on(bb,cc)

  28. Attempts Ie, KB1 ├Pon(aa,bb) Ie, KB1 ├Pon(X,b10) for any value of X. Examples of Proofs: III (Failure) • Using KB1 = • Query 4 : on(aa, b10) • Query 5 : on(X, b10)

  29. (3) X=bb, Y=cc on(bb,cc) (2) success Examples of Proofs: IVa (Rules) • Using KB1 = • Query 6 : above(bb, cc) Ie, KB1 ├Pabove(bb,cc)

  30. (3) X=bb, Y=W on(bb, W) (2) success –{cc/W} Examples of Proofs: IVb (Rules) • Using KB1 = • Query 7 : above(bb, W) Ie, KB1 ├Pabove(bb,W){cc/W}

  31. (3) X=bb, Y=W (4) X=aa, Y=cc on(aa,cc) on(aa,Z)&above(Z,cc) (1) Z=bb above(bb,cc) Examples of Proofs: V (big) • Using KB2 = • Query 7 : above(aa, cc)

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

  33. Examples of Proofs: VI (many answers) • Using KB3 = • 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)

  34. 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  G1, G2, ..., Gn is a top-down proof of G1 (from KB) iff • Gn = success, and • Gi is a SUBGOAL (in KB) of Gi-1, i=2,3,... n

  35. Subgoals Subgoals of G = { g1, ..., gr} in KB: Rule 1: If  atomic axiom ``t '' in KB where t and gi have mgu , then { g1 , ..., gi-1, gi+1, ..., gr } is a subgoal of G . (If r=1, then ``success'' is subgoal of G .) Rule 2: If  rule ``t :- t1, ..., t_k '' in KB where t and gi have mgu , then { t1, ..., tk, g1, ..., gi-1, gi+1, ..., gr } is a subgoal of G .

  36. 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) • KB3 = above(A,B) on(A,B) on(A,Z), above(Z, B) success

  37. 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] above(A,Z1), above(Z1, B) above(bb,B) above(cc, B) on(A,c1) on(Z1,B), on(A, Z1) on(Z1,Z), above(Z,B), on(A, Z1)

  38. Variable bindings Unifier 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”

More Related