Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tabling To guarantee termination in non-ground programs, instead of ancestors and pruning rules, tabulation mechanisms are required –when there is a possible.

Similar presentations


Presentation on theme: "Tabling To guarantee termination in non-ground programs, instead of ancestors and pruning rules, tabulation mechanisms are required –when there is a possible."— Presentation transcript:

1 Tabling To guarantee termination in non-ground programs, instead of ancestors and pruning rules, tabulation mechanisms are required –when there is a possible loop, suspend the literal and try alternative solutions –when a solution is found, store it in a table –resume suspended nodes with new solutions in the table –apply an algorithm to determine completion of the process, i.e. when no more solutions exist, and fail the corresponding suspended nodes

2 Tabling example SLX is also implemented with tabulation mechanisms It uses XSB-Prolog tabling implementation SLX with tabling is available with XSB-Prolog from Version 2.0 onwards Try it at: p(X)  p(Y) p(a) p(X) p(Y) 1) suspend X = a 2) resume Y = a X = _ Table for p(X) http://xsb.sourceforge.net/

3 Tabling (cont.) If a solution is already stored in a table, and the predicate is called again, then: –there is no need to compute the solution again –simply pick it from the table! This increases efficiency. Sometimes by one order of magnitude.

4 Fibonacci example fib(1,1). fib(2,1). fib(X,F)  fib(X-1,F1), fib(X-2,F2), F is F1 + F2. fib(4,A)fib(3,B)fib(2,C) C=1D=1 fib(1,D) B=3 fib(2,E) E=1 A=4 fib(3,F) F=3 Y=7 Table for fib Q F 2 1 1 3 4 5 7 fib(6,X) fib(5,Y) fib(4,H) H=4 X=11 6 11 Linear rather than exponential

5 XSB-Prolog Can be used to compute under WFS Prolog + tabling –To using tabling on, eg, predicate p with 3 arguments: :- table p/3. Table are used from call to call until: abolish_all_table abolish_table_pred(P/A)

6 XSB Prolog (cont.) WF negation can be used via tnot(Pred) Explicit negation via -Pred The answer to query Q is yes if Q is either true or undefined in the WFM The answer is no if Q is false in the WFM of the program

7 Distinguishing T from U After providing all answers, tables store suspended literals due to recursion through negation Residual Program If the residual is empty then True If it is not empty then Undefined The residual can be inspected with: get_residual(Pred,Residual)

8 Residual program example :- table a/0. :- table b/0. :- table c/0. :- table d/0. a :- b, tnot(c). c :- tnot(a). b :- tnot(d). d :- d. | ?- a,b,c,d,fail. no | ?- get_residual(a,RA). RA = [tnot(c)] ; no | ?- get_residual(b,RB). RB = [] ; no | ?- get_residual(c,RC). RC = [tnot(a)] ; no | ?- get_residual(d,RD). no | ?-

9 Transitive closure Due to circularity completion cannot conclude not reach(c) SLDNF (and Prolog) loops on that query XSB-Prolog works fine :- auto_table. edge(a,b). edge(c,d). edge(d,c). reach(a). reach(A) :- edge(A,B),reach(B). |?- reach(X). X = a; no. |?- reach(c). no. |?-tnot(reach(c)). yes.

10 Transitive closure (cont) :- auto_table. edge(a,b). edge(c,d). edge(d,c). reach(a). reach(A) :- edge(A,B),reach(B). Declarative semantics closer to operational Left recursion is handled properly The version on the right is usually more efficient :- auto_table. edge(a,b). edge(c,d). edge(d,c). reach(a). reach(A) :- reach(B), edge(A,B). Instead one could have written

11 Grammars Prolog provides “for free” a right-recursive descent parser With tabling left-recursion can be handled It also eliminates redundancy (gaining on efficiency), and handle grammars that loop under Prolog.

12 Grammars example :- table expr/2, term/2. expr --> expr, [+], term. expr --> term. term --> term, [*], prim. term --> prim. prim --> [‘(‘], expr, [‘)’]. prim --> [Int], {integer(Int)}. This grammar loops in Prolog XSB handles it correctly, properly associating * and + to the left

13 Grammars example :- table expr/3, term/3. expr(V) --> expr(E), [+], term(T), {V is E + T}. expr(V) --> term(V). term(V) --> term(T), [*], prim(P), {V is T * P}. term(V) --> prim(V). prim(V) --> [‘(‘], expr(V), [‘)’]. prim(Int) --> [Int], {integer(Int)}. With XSB one gets “for free” a parser based on a variant of Earley’s algorithm, or an active chart recognition algorithm Its time complexity is better!

14 Finite State Machines :- table rec/2. rec(St) :- initial(I), rec(St,I). rec([],S) :- is_final(S). rec([C|R],S) :- d(S,C,S2), rec(R,S2). Tabling is well suited for Automata Theory implementations q0q1 q2 q3 a a b a initial(q0). d(q0,a,q1). d(q1,a,q2). d(q2,b,q1). d(q1,a,q3). is_final(q3).

15 Dynamic Programming Strategy for evaluating subproblems only once. –Problems amenable for DP, might also be for XSB. The Knap-Sack Problem: –Given n items, each with a weight K i (1  i  n), determine whether there is a subset of the items that sums to K

16 The Knap-Sack Problem :- table ks/2. ks(0,0). ks(I,K) :- I > 0, I1 is I-1, ks(I1,K). ks(I,K) :- I > 0, item(I,Ki), K1 is K-Ki, I1 is I-1, ks(I1,K1). Given n items, each with a weight K i (1  i  n), determine whether there is a subset of the items that sums to K. There is an exponential number of subsets. Computing this with Prolog is exponential. There are only I 2 possible distinct calls. Computing this with tabling is polynomial.

17 Combined WFM and ASP at work XSB-Prolog XASP package combines XSB with Smodels –Makes it possible to combine WFM computation with Answer-sets –Use (top-down) WFM computation to determine the relevant part of the program –Compute the stable models of the residual –Possibly manipulate the results back in Prolog

18 XNMR mode Extends the level of the Prolog shell with querying stable models of the residual: :- table a/0, b/0, c/0. a :- tnot(b). b :- tnot(a). c :- b. c :- a. C:\> xsb xnmr. […] nmr| ?- [example]. yes nmr| ?- c. DELAY LIST = [a] DELAY LIST = [b]? s {c;a} ; {c;b} ; no nmr| ?- the residuals of the query SMs of the residual s {a}; {b}; no nmr| ?- a. DELAY LIST = [tnot(b)] t {a}; no a. DELAY LIST = [tnot(b)] SMs of residual where query is true

19 XNMR mode and relevance Stable models given a query First computes the relevant part of the program given the query This step already allows for: –Processing away literal in the WFM –Grounding of the program, given the query. This is a different grounding mechanism, in contrast to lparse or to that of DLV It is query dependant and doesn’t require that much domain predicates in rule bodies…

20 XASP libraries Allow for calling smodels from within XSB- Programs Detailed control and processing of Stable Models Two libraries are provided –sm_int which includes a quite low level (external) control of smodels –xnmr_int which allows for a combination of SMs and prolog in the same program

21 sm_int library Assumes a store with (smodels) rules Provides predicates for –Initializing the store ( smcInit/0 and smcReInit/0 ) –Adding and retracting rules ( smcAddRule/2 and smcRetractRule/2 ) –Calling smodels on the rules of the store ( smcCommitProgram/0 and smcComputeModel/0 ) –Examine the computed SMs ( smcExamineModel/2 ) –smcEnd/0 for reclaiming resources in the end

22 xnmr_int library Allows for control, within Prolog of the interface provided by xnmr. –Predicates that call goals, compute residual, and compute SMs of the residual pstable_model(+Query,-Model,0) –Computes one SM of the residual of the Query –Upon backtracking, computes other SMs pstable_model(+Query,-Model,1) –As above but only SMs where Query is true Allow for pre and pos-processing of the models –E.g. for finding models that are minimal or prefered in some sense –For pretty input and output, etc You must: :- import pstable_model/3 from xnmr_int

23 Exercise Write a XBS-XASP program that –Reads from the input the dimension N of the board –Computes the solution for the N-queens problem of that dimension –Shows the solution “nicely” in the screen –Shows what is common to all solution E.g. (1,1) has never a queen, in no solution Write a XSB-XASP program that computes minimal diagnosis of digital circuits


Download ppt "Tabling To guarantee termination in non-ground programs, instead of ancestors and pruning rules, tabulation mechanisms are required –when there is a possible."

Similar presentations


Ads by Google