Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog

Similar presentations


Presentation on theme: "Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog"— Presentation transcript:

1 cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog t.k.prasad@wright.edu http://www.knoesis.org/tkprasad/

2 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-Meaning2

3 Consequences : Equivalence Declarative 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-Meaning3

4 (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-Meaning4

5 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-Meaning5

6 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-Meaning6

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

8 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-Meaning8

9 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 

10 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

11 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 *)(* NEXT GoalList GENERATION *) end; Success := Satisfied end end; cs7120 (Prasad)L16-Meaning11

12 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-Meaning12

13 Debugging Prolog Welcome 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-Meaning13

14 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-Meaning14 Debugging ---------------- trace. spy(…). ; …?creep …?leap …?abort

15 Propositional Prolog p.  p :- true. p :- q. p if q cs7120 (Prasad)L16-Meaning15 p => qtruefalse true false true Facts Rules Boolean Logic

16 De Morgan’s Laws cs7120 (Prasad)L16-Meaning16 p :- q,r.

17 cs7120 (Prasad)L16-Meaning17 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


Download ppt "Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog"

Similar presentations


Ads by Google