Declarative Programming

Post on 14-Jan-2016

28 views 2 download

description

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, ... - PowerPoint PPT Presentation

Transcript of Declarative Programming

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

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

Logic Programming Program Logic Formula

Execution 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 forfather(X, tom) vs father(tom, Y)

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 democracyX(at), xY=, Boy(1X,( ) :-

AcceptReject

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.

{ 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

6

Concept of PROLOG PROgramming in LOGic

Sound Reasoning Engine1. 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

Tying Prolog to Logic Syntax: Horn Clauses

(aka Rules + Facts) Terms

Proof Process: Resolution Substitution Unification

Semantics (From Predicate Calculus … + not )

8

Terms BNF:

term ::= constant | variable | functor constant ::= atom starting w/lower casevariable ::= atom starting w/upper casefunctor ::= constant (tlist ) tlist ::= “” | term {, tlist }

Examples of term :a1 b fred constantX Yc3 Fred _4 variable married(fred) g(a, f(Yc3), b) functor

Ground Term term with no variablesf(q) g(f(w), w1(b,c)) are groundf(Q) g(f(w), w1(B,c)) are not

9

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 }

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

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

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 substitutionEg:

{ a/X} ○ { b/Y} = { a/X, b/Y} { Z/X} ○ { a/Z} = { a/X, a/Z}

13

Examples:t1 t2 unifier termf(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 t1 and t2 are unified by iff

t1 = t2 .Then is called a unifier

t1 and t2 are unifiable

NB t1 and t2 are symmetric -- either can have variables

14

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

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)

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 variable1 = { 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

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. ○ ’= )

17

MGU --- Example#2

A mgu for f( W, Z, g(Z) ) & f( X, h(X), Y )

{W/X }

W/X

h(X)/Zh(W)/Z

g(Z)/Y

h(W)/Z g(h(W))/Y

g(h(W))/Y

18

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.

19

MGU ProcedureRecursive 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

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

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

22

What User Really Types > sicstusSICStus 3.11.2 (x86-linux-glibc2.3): Wed Jun 2 11:44:50 CEST 2004Licensed 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 bytesyes % Prolog's answer to most operations| ?-

on(X,Y).X = aa,Y = bb ? ; % User types “;” to ask for ANOTHER answerX = 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 questionY = bb ? % Prolog's answer: a binding listyes % Prolog's statement that there was answer| ?-

23

What User Really Types -- II

> sicstusSICStus 3.11.2 (x86-linux-glibc2.3): Wed Jun 2 11:44:50

CEST 2004Licensed 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 anotherno % but this is only one…| ?-

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 { σ }

KB1 ⊢P mortal(X){ socrates/X } Failure (otherwise)

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

Examples of Proofs: I (DB retrieval)

Using KB1 =

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 KB1 ├P on(aa, bb){}

empty substitution( Like Data Base retrieval )

27

Examples of Proofs: IIa (Variables)

Query 2 : on(aa, Y) on(aa, Y)

success –{bb/Y}

(1) Y=bb

KB1 ├P on(aa, Y) { bb/Y }

Using KB1 =

on(aa, bb). (1)

on(bb, cc). (2)

above(X, Y) :- on(X,Y). (3)

28

Examples of Proofs: IIb (Variables)

Query 3 : on( X, Y )

KB1 ├P on(X, Y){ aa/X, bb/Y } ... on(aa,bb)

KB1 ├P on(X, Y){ bb/X, cc/Y } ... on(bb,cc)

Using KB1 =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

Ie, KB1 ├P on(aa,bb)

Examples of Proofs: III (Failure)

Query 4 : on(aa, b10)

Using KB1 =on(aa, bb). (1)

on(bb, cc). (2)

above(X, Y) :- on(X,Y).

(3)

Query 5 : on(X, b10)

Ie, KB1 ├P on(X,b10) for any value of X.

Attempts

30

Examples of Proofs: IVa (Rules)

Query 6 : above(bb, cc)

Ie, KB1 ├P above(bb,cc)

on(bb,cc)

(3) X=bb, Y=cc

success

(2)

Using KB1 =on(aa, bb). (1)

on(bb, cc). (2)

above(X, Y) :- on(X,Y). (3)

31

Examples of Proofs: IVb (Rules)

Query 7 : above(bb, W)

Ie, KB1 ├P above(bb,W){cc/W}

on(bb, W)

(3) X=bb, Y=W

success –{cc/W}

(2)

Using KB1 =on(aa, bb). (1

)

on(bb, cc). (2)

above(X, Y) :- on(X,Y). (3)

32

Examples of Proofs: V (big)

Query 7 : above(aa, cc)

on(aa,cc)

(3) X=bb, Y=W

Using KB2 =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

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)

success

on(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

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 KB3 =

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

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

1. Gn = success, and2. Gi is a SUBGOAL (in KB) of Gi-1, i=2,3,...

n

36

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 .

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)

KB3 =

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

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

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 bindingsUnifier