cs7120 (prasad)l16-meaning1 procedural and declarative meaning of prolog [email protected]

17
cs7120 (Prasad) L16-Meaning 1 Procedural and Declarative Meaning of Prolog [email protected] http://www.knoesis.org/ tkprasad/

Upload: junior-george-jennings

Post on 02-Jan-2016

223 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog t.k.prasad@wright.edu

cs7120 (Prasad) L16-Meaning 1

Procedural and Declarative Meaning of Prolog

[email protected]

http://www.knoesis.org/tkprasad/

Page 2: Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog t.k.prasad@wright.edu

p :- q, r.q.

• Declarative – Machine independent. Formalized through

propositional and predicate logic.p is true if q is true and r is true.

q is true.

• Procedural To solve p: first solve q, and then solve r.

q is solved.

cs7120 (Prasad) L16-Meaning 2

Page 3: Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog t.k.prasad@wright.edu

Consequences : EquivalenceDeclarative Reading

p :- q, r.

q.

equivalent to

p :- r, q.

q.

since “OR”and “AND” are commutative.

p :- p.

p.

equivalent to

p.

p :- p.

since “AND” is commutative.cs7120 (Prasad) L16-Meaning 3

Page 4: Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog t.k.prasad@wright.edu

(cont’d)Procedural Reading

p :- p.

p.

is not equivalent to

p.

p :- p.

• The former program loops on the query ?-p, while the latter program succeeds.

• Prolog strategy is computationally efficient but incomplete.

cs7120 (Prasad) L16-Meaning 4

Page 5: Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog t.k.prasad@wright.edu

Declarative Meaning

• Specifies whether or not a given goal G is true, and if so, for what values of the variables is it true.

• Instance of a clause C is obtained by substituting terms for each of its variables.

• Variant of a clause C is obtained by renam- ing the variables of C.

cs7120 (Prasad) L16-Meaning 5

Page 6: Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog t.k.prasad@wright.edu

Example

• Clause: p(X) :- q(X,Y), r(Y).

• Instance:p(a) :- q(a,Y), r(Y).

p(b) :- q(b,b), r(b).

• Variant:p(A) :- q(A,B), r(B).

cs7120 (Prasad) L16-Meaning 6

Page 7: Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog t.k.prasad@wright.edu

Declarative Meaning

• Given a program P and a goal G,

G is true ( i.e., logically follows from P)

if and only if:

(1) There is a clause C in P such that

(2) there is a clause instance I of C such that

(a) the head of I is identical to G, and

(b) all the goals in the body of I are true.

(Recursive definition of satisfaction)

cs7120 (Prasad) L16-Meaning 7

Page 8: Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog t.k.prasad@wright.edu

• An instance of a clause C is obtained by renaming each variable in C and possibly substituting the variable by some term.

• E.g. an instance of p(X,Y) :- q(Y,Z).

is

p(U,a) :- q(a,V).• Recall that scope of a variable is the clause (locality

of reference) and same variable name implies equality constraint.

cs7120 (Prasad) L16-Meaning 8

Page 9: Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog t.k.prasad@wright.edu

• Specifies how Prolog answers questions.

• Procedural meaning is an algorithm to execute a list of goals given a Prolog program:

program

success/failure indication

goal list execute

instantiation of variables

Procedural Meaning

9

Page 10: Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog t.k.prasad@wright.edu

procedure execute( Program, GoalList, Success)

• execute = declarative meaning + procedural elements

G is true ( i.e. logically follows from P) if and only if:

(1) there is a clause C in P such that

(2) there is a clause instance I of C such that

(a) the head of I is identical to G, and

(b) all the goals in the body of I are true

Search program from top to bottom to find such clause

Match G and head of C Execute goals in order as they

appear in program

Page 11: Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog t.k.prasad@wright.edu

procedure execute( Program, GoalList, Success)

begin

if empty(GoalList) then Success := true

else begin Goal := head(GoalList);

OtherGoals := tail(GoalList);

Satisfied := false;

while not Satisfied and

“more clauses in Program” do

begin (* NEXT GoalList GENERATION *)

end;

Success := Satisfied

end

end;

cs7120 (Prasad) L16-Meaning 11

Page 12: Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog t.k.prasad@wright.edu

Let next clause in Program be

H :- B1, B2, …, Bn.

Construct a variant of this clause (variables disjoint from GoalList)

H’ :- B1’, B2’, …, Bn’.

match(Goal, H’, MatchOK, substitution);

if MatchOK then

begin

NewGoals := apply(substitution,

append( [B1’,B2’,…,Bn’], OtherGoals) );

execute(Program, NewGoals, Satisfied);

end

cs7120 (Prasad) L16-Meaning 12

Page 13: Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog t.k.prasad@wright.edu

Debugging PrologWelcome to SWI-Prolog (Multi-threaded, 32 bits, Version 5.6.62)

Copyright (c) 1990-2008 University of Amsterdam.…

For help, use ?- help(Topic). or ?- apropos(Word).

1 ?- consult(user).

| likes(mary,food).

likes(mary,wine).

likes(john,wine).

likes(john,mary).

| % user://2 compiled 0.00 sec, 632 bytes

cs7120 (Prasad) L16-Meaning 13

Page 14: Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog t.k.prasad@wright.edu

2 ?- trace, likes(X,Y).

Call: (9) likes(_G597, _G598) ? creep

Exit: (9) likes(mary, food) ? creep

X = mary,

Y = food ;

Redo: (9) likes(_G597, _G598) ? creep

Exit: (9) likes(mary, wine) ? creep

X = mary,

Y = wine ;

Redo: (9) likes(_G597, _G598) ? creep

Exit: (9) likes(john, wine) ? creep

[debug] 4 ?- likes(tom,john).

false.

cs7120 (Prasad) L16-Meaning 14

Debugging----------------

trace.spy(…).

;

…?creep…?leap…?abort

Page 15: Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog t.k.prasad@wright.edu

Propositional Prologp. p :- true.

p :- q.

p if q

cs7120 (Prasad) L16-Meaning 15

pq

p => q true false

true true false

false true true

pq

Facts

Rules

Boolean Logic

Page 16: Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog t.k.prasad@wright.edu

De Morgan’s Laws

cs7120 (Prasad) L16-Meaning 16

)()()( qpqp

)()()( qpqp

prq

prq

)()(

)(

p :- q,r.

Page 17: Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog t.k.prasad@wright.edu

cs7120 (Prasad) L16-Meaning 17

)()(

)(

prpq

prq

p :- q.p :- r. p :- q ; r.

cf. Normal Form / Clausal form

Sum of product form disjunctive normal form product of sum forms conjunctive normal form