230 likes | 393 Views
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
E N D
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
Table for p(X) Tabling example p(X) ¬ p(Y) p(a) • 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) 1) suspend X = a p(Y) 2) resume Y = a http://xsb.sourceforge.net/ X = a X = _
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.
fib(4,H) fib(5,Y) fib(4,A) fib(3,F) Table for fib fib(3,B) fib(2,E) Q F fib(2,C) fib(1,D) C=1 D=1 Fibonacci example fib(6,X) fib(1,1). fib(2,1). fib(X,F) ¬ fib(X-1,F1), fib(X-2,F2), F is F1 + F2. X=11 Y=7 H=4 A=4 F=3 B=3 2 1 E=1 1 1 3 3 4 4 • Linear rather than exponential 5 7 6 11
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)
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
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)
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 | ?-
Due to circularity completion cannot conclude not reach(c) SLDNF (and Prolog) loops on that query XSB-Prolog works fine Transitive closure :- 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.
:- 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 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
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.
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
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!
a a q0 q1 q3 a b q2 Finite State Machines • Tabling is well suited for Automata Theory implementations initial(q0). d(q0,a,q1). d(q1,a,q2). d(q2,b,q1). d(q1,a,q3). is_final(q3). :- 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).
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 Ki (1 £ i £ n), determine whether there is a subset of the items that sums to K
The Knap-Sack Problem Given n items, each with a weight Ki (1 £ i £ n), determine whether there is a subset of the items that sums to K. :- 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). • There is an exponential number of subsets. Computing this with Prolog is exponential. • There are only I2 possible distinct calls. Computing this with tabling is polynomial.
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
the residuals of the query SMs of the residual SMs of residual where query is true XNMR mode c. DELAY LIST = [a] DELAY LIST = [b]? nmr| ?- • Extends the level of the Prolog shell with querying stable models of the residual: s {c;a} :- table a/0, b/0, c/0. a :- tnot(b). b :- tnot(a). c :- b. c :- a. ; {c;b} ; no nmr| ?- a. DELAY LIST = [tnot(b)] s {a}; {b}; no nmr| ?- C:\> xsb xnmr. […] nmr| ?- a. DELAY LIST = [tnot(b)] [example]. yes t {a}; no
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…
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
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
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
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