Programming in Logic Prologs Declarative Procedural Semantics Readings

  • Slides: 30
Download presentation
Programming in Logic: Prolog’s Declarative & Procedural Semantics Readings: Sections 2. 3 - 2.

Programming in Logic: Prolog’s Declarative & Procedural Semantics Readings: Sections 2. 3 - 2. 7 MB: 2 March 2001 CS 360 Lecture 3 1

Where is the Prolog Program? When one talks about Prolog programs, one is referring

Where is the Prolog Program? When one talks about Prolog programs, one is referring to the Knowledge base. n The definitions of the relations in the Knowledge base is the Prolog code. n MB: 2 March 2001 CS 360 Lecture 3 2

Goals are the terms that Prolog attempts to match against the clause heads in

Goals are the terms that Prolog attempts to match against the clause heads in its Knowledge base. n The top level terms in a query are all goals: n – n ? : a(4, x), b(Y, 7). The top level terms in the body of a clause are all goals: – p(X) : - q(X, a), r(3, X). MB: 2 March 2001 CS 360 Lecture 3 3

Goals and Data Structures Prolog goals look exactly like Prolog data structures. n In

Goals and Data Structures Prolog goals look exactly like Prolog data structures. n In other words, Prolog code looks just like Prolog data. This is intentional! n A Prolog program can modify itself as it is running. n A Prolog program can create new queries & pose them to the Prolog interpreter. n MB: 2 March 2001 CS 360 Lecture 3 4

Prolog’s Declarative Semantics The prolog we have seen so far is called pure Prolog,

Prolog’s Declarative Semantics The prolog we have seen so far is called pure Prolog, i. e. , everything can be expressed in FOPC. n For example: ancestor(A, P) : - parent(A, X), ancestor(X, P). can be translated into the logical expression: A P(ancestor(A, P) n parent(A, X) ancestor(X, P)) MB: 2 March 2001 CS 360 Lecture 3 5

Translating Prolog clauses into FOPC n The clauses : ancestor(A, P) : - parent(A,

Translating Prolog clauses into FOPC n The clauses : ancestor(A, P) : - parent(A, P). ancestor (A, P): - parent(A, X), ancestor(X, P). Translates into FOPC as: A P( ancestor(A, P) parent(A, P) ( parent(A, X) ancestor(X, P))) MB: 2 March 2001 CS 360 Lecture 3 6

Prolog Declarative Semantics In the body of the clause, the commas separating the top-level

Prolog Declarative Semantics In the body of the clause, the commas separating the top-level terms are understood to be conjunctions (and’s). n There are two ways to specify disjunctions (or’s): n – – Write separate clauses (as done for ancestor) Use a semicolon (“; ”) MB: 2 March 2001 CS 360 Lecture 3 7

Disjunctions in Prolog n We could rewrite the ancestor example in Prolog as follows:

Disjunctions in Prolog n We could rewrite the ancestor example in Prolog as follows: ancestor(A, P) : - parent(A, P) ; parent(A, X), ancestor(X, P). MB: 2 March 2001 CS 360 Lecture 3 8

Negation in Pure Prolog n We cannot express negation in the clause body in

Negation in Pure Prolog n We cannot express negation in the clause body in pure prolog: – For example, we can’t express in pure Prolog the following FOPC expression H W( husband(H, W) married(H, W) male(H) divorced(H, W) ) MB: 2 March 2001 CS 360 Lecture 3 9

Negation in Pure Prolog cont’d Likewise, Prolog can’t tell us something is false rather

Negation in Pure Prolog cont’d Likewise, Prolog can’t tell us something is false rather it tells us that it can’t prove something is true. n For example, given the Knowledge base: n – – n man(socrates). mortal(X) : - man(X). The answer to mortal(mike) would be no, not because it isn’t true, but because Prolog can’t prove it’s true using that KB. MB: 2 March 2001 CS 360 Lecture 3 10

Prolog’s Procedural Semantics I Prolog’s control flow different from languages like Java. n In

Prolog’s Procedural Semantics I Prolog’s control flow different from languages like Java. n In Prolog no distinction between statements & method calls - all goals recursively matched against KB (system defined relations (write/1) have own definitions). n Goal parameters can be unbound variables. n Goals succeed or they fail. n MB: 2 March 2001 CS 360 Lecture 3 11

Prolog Control Flow Example KB 1. a(X) : - b(X). n n n b(2).

Prolog Control Flow Example KB 1. a(X) : - b(X). n n n b(2). Given the query a(2), Prolog 2. Execution scans thetrace KB of forquery the first claus If it finds a match then it recurses on the goals in the clause b [a(2)] (goal stack) Query succeeded here. 1. X = 2 (which clause [b(2)] matched & bindings) 2. [] MB: 2 March 2001 CS 360 Lecture 3 12

Prolog Control Flow Example KB 1. a(X) : - b(X). n n 2. b(2).

Prolog Control Flow Example KB 1. a(X) : - b(X). n n 2. b(2). Execution trace of query Query: a(2) [a(2)] (goal stack) Notice that after the query goal matched the clause head, the 1. X = 2 (which clause [b(2)] matched & bindings) 2. [] MB: 2 March 2001 CS 360 Lecture 3 13

Prolog Control Flow Example KB 1. a(X) : - b(X). n n Query: a(3),

Prolog Control Flow Example KB 1. a(X) : - b(X). n n Query: a(3), Prolog scans the KB for first clause head match 2. b(2). of query If it fails to find a match then. Execution it tries totrace backtrack its matches [a(3)] There are no other possible matches. 1. X = 3 Query fails. [b(3)] MB: 2 March 2001 CS 360 Lecture 3 14

Backtracking KB : 1. a(X) : - b(X). 2. a(3). 3. b(2). n n

Backtracking KB : 1. a(X) : - b(X). 2. a(3). 3. b(2). n n Execution tracebacktracks of query to last Query: a(3), from point of failure, Prolog Matches clause 2 and succeeds. [a(3)] 1. X = 3 [b(3)] 2. [] MB: 2 March 2001 CS 360 Lecture 3 15

Flow of Control with Conjunctions [a(X)] Query: a(X). KB: 1. a(X) : - b(X),

Flow of Control with Conjunctions [a(X)] Query: a(X). KB: 1. a(X) : - b(X), c(X). 2. b(2). 3. c(2). Response: X=2? 1. X = X’ Note renaming of variables in matched clause, X replaced by X’. I[b(X’), c(X’)]Goal query is replaced by first clause body goal 2. X’ = 2 and rest of clause body goals are pushed onto [c(2)] goal stack. 3. [] MB: 2 March 2001 CS 360 Lecture 3 16

Flow of Control with Conjunctions [a(X)] Query: a(X). KB: 1. a(X) : - b(X),

Flow of Control with Conjunctions [a(X)] Query: a(X). KB: 1. a(X) : - b(X), c(X). 2. b(2). 3. b(3) 4. c(3). Notice unbinding 1. X = X’ of variable (X’) when backtrack [b(X’), c(X’)] over binding point. 2. X’ = 2 3. X’ = 3 [c(2)] Response: X=3? [c(3)] 4. [] MB: 2 March 2001 CS 360 Lecture 3 17

Prolog Procedural Semantics II When goal fails, control undoes any bindings done at that

Prolog Procedural Semantics II When goal fails, control undoes any bindings done at that step & backs up to previous step. n It continues scan of KB for clause heads matching current goal. n If none found then continues to backtrack. n MB: 2 March 2001 CS 360 Lecture 3 18

Flow of Control with Conjunctions [a(X)] Query: a(X). KB: 1. a(X) : - b(X),

Flow of Control with Conjunctions [a(X)] Query: a(X). KB: 1. a(X) : - b(X), c(X). 2. b(Y) : - d(Z), e(Y, Z). 3. b(3) 4. c(3). 5. d(5). 6. e(3, 5). Response: X=3? MB: 2 March 2001 1. X = X’ [b(X’), c(X’)] 2. X’ = Y [d(Z), e(Y, Z), c(Y)] 5. Z = 5 [e(Y, 5), c(Y)] 6. Y = 3 [c(3)] 4. [] CS 360 Lecture 3 19

Flow of Control with Conjunctions Query: a(X). KB: 1. a(X) : - b(X), c(X).

Flow of Control with Conjunctions Query: a(X). KB: 1. a(X) : - b(X), c(X). 2. b(Y) : - d(Z), e(Y, Z). 3. b(3) 4. c(4). 5. d(5). 6. e(3, 5). 7. e(4, 5). Response: X=4? MB: 2 March 2001 [a(X)] 1. X = X’ [b(X’), c(X’)] 2. X’ = Y [d(Z), e(Y, Z), c(Y)] 5. Z = 5 [e(Y, 5), c(Y)] 6. Y = 3 [c(3)] 7. Y = 4 [c(4)] 4. [] CS 360 Lecture 3 20

Additional Answers What happens when Prolog returns an answer and prompts you with “?

Additional Answers What happens when Prolog returns an answer and prompts you with “? ”? n Since an answer was returned, the trace ended with a true and an empty goal stack. n If you type in “; ” then Prolog will, in effect, change that true to fails and backtrack. n MB: 2 March 2001 CS 360 Lecture 3 21

Effects of Clause/Goal Ordering A Prolog program can be declaratively correct, but procedurally inadequate

Effects of Clause/Goal Ordering A Prolog program can be declaratively correct, but procedurally inadequate (may recurse forever (p : - p. ), or simply be inefficient). n The program’s behavior is determined by the order of the clauses in the KB and the order of the goals in the clauses. n Changing either can dramatically affect the behavior of the program. n MB: 2 March 2001 CS 360 Lecture 3 22

Example: Parental Fact KB parent(pam, bob). n parent(tom, liz). n parent(bob, ann). n parent(bob,

Example: Parental Fact KB parent(pam, bob). n parent(tom, liz). n parent(bob, ann). n parent(bob, pat). n parent(pat, jim). n MB: 2 March 2001 CS 360 Lecture 3 23

Predecessor Relation Variations pred 1(X, Z) : - parent(X, Z). pred 1(X, Z) :

Predecessor Relation Variations pred 1(X, Z) : - parent(X, Z). pred 1(X, Z) : - parent(X, Y), pred 1(Y, Z). pred 2(X, Z) : - parent(X, Y), pred 2(Y, Z). pred 2(X, Z) : - parent(X, Z). pred 3(X, Z) : - pred 3(Y, Z), parent(X, Y). pred 4(X, Z) : - pred 4(Y, Z), parent(X, Y). pred 4(X, Z) : - parent(X, Z). MB: 2 March 2001 CS 360 Lecture 3 24

Computing predn(tom, pat) For n from 1 to 3, Prolog returns yes. n For

Computing predn(tom, pat) For n from 1 to 3, Prolog returns yes. n For pred 4(tom, pat), Prolog loops forever. n While the first 3 preds find an answer they do so with different amounts of computation. n MB: 2 March 2001 CS 360 Lecture 3 25

Trace of pred 1(tom, pat) [pred 1(tom, pat)] 7. X=tom, Z=pat 8. X=tom, Z=pat

Trace of pred 1(tom, pat) [pred 1(tom, pat)] 7. X=tom, Z=pat 8. X=tom, Z=pat KB: 1. parent(pam, bob). [parent(tom, pat)] [parent(tom, Y), pred 1(Y, pat)] 2. Y=bob 2. parent(tom, bob). 3. parent(tom, liz). [pred 1(bob, pat)] 4. parent(bob, ann). 7. X’=bob, Z’=pat 5. parent(bob, pat). [parent(bob, pat)] 6. parent(pat, jim). 7. pred 1(X, Z) : - parent(X, Z). 5. 8. pred 1(X, Z) : - parent(X, Y), pred 1(Y, Z). [] MB: 2 March 2001 CS 360 Lecture 3 26

Exercises n Try doing the execution traces of pred 2(tom, pat), pred 3(tom, pat),

Exercises n Try doing the execution traces of pred 2(tom, pat), pred 3(tom, pat), and pred 4(tom, pat) MB: 2 March 2001 CS 360 Lecture 3 27

Quick Quiz What is the difference between the structure of Prolog code and Prolog

Quick Quiz What is the difference between the structure of Prolog code and Prolog data? n What is a goal and how is it different from structured data? n How do you express a negated goal? n What does it mean when Prolog answers no n MB: 2 March 2001 CS 360 Lecture 3 28

Quick Quiz cont’d In pure Prolog, does changing the order of the clauses or

Quick Quiz cont’d In pure Prolog, does changing the order of the clauses or of the goals in the clauses change the declarative meaning? n What about the procedural meaning? n Is there anything declaratively wrong with: n – – n ancestor(A, P) : - ancestor(X, P), parent(A, X). ancestor(A, P) : - parent(A, P). Is there anything procedurally wrong? MB: 2 March 2001 CS 360 Lecture 3 29

Summary Declarative semantics of pure Prolog program defines when a goal is true and

Summary Declarative semantics of pure Prolog program defines when a goal is true and for what instantiation of variables. n Commas between goals mean and, semicolons mean or. n Procedural semantics are determined by clause and goal ordering. n Goal failure causes backtracking and unbinding of affected variables. n MB: 2 March 2001 CS 360 Lecture 3 30