Linear Tabling1 Tabled Prolog and Linear Tabling Neng-Fa Zhou (City Univ. of New York) Yi-Dong Shen (Chinese Academy of Sciences) Taisuke Sato (Tokyo Inst. of Technology)
Linear Tabling2 Tabling is Useful 4 Eliminate infinite loops path(X,Y):-edge(X,Y). path(X,Y):-edge(X,Z),path(Z,Y). 4 Reduce redundant computations fib(0,1). fib(1,1). fib(N,F):-N>1, N1 is N-1,fib(N1,F1), N2 is N-2,fib(N2,F2), F is F1+F2.
Linear Tabling3 Tabling in OLDT (SLG-WAM) A... A’ producer consumer 4 suspend/resume –Complicate implementation freeze stacks overhead on standard programs garbage collection table A’ is suspended after the existing answers are exhausted
Linear Tabling4 A... A’ pioneer follower table A’ fails or becomes a producer after consuming existing answers A needs to be re-evaluated in some cases 4 Advantages –Easy to implement –Space efficient –Overhead-free 4 Disadvantage –Re-computation 4 Optimizations –Subgoal optimization –Semi-naïve evaluation
Linear Tabling5 The Linear Tabling Framework 4 Augmented programs p(X,Y):-p(X,Z),e(Z,Y),memo(p(X,Y)). p(X,Y):-e(X,Y),memo(p(X,Y)). p(X,Y):-check_completion(p(X,Y)). p(X,Y):-p(X,Z),e(Z,Y). p(X,Y):-e(X,Y).
Linear Tabling6 The Linear Tabling Framework (cont.) 4 table_start(A) –Executed when a tabled subgoal A is encountered 4 memo(A) –Executed when a clause succeeds 4 check_completion(A) –Executed after all clauses have been tried.
Linear Tabling7 Definitions Loops, pioneers and followers A derivation G i … G j forms a loop if 1.G i =(A,…) and G j =(A’,…) 2.A and A’ are variants 3.A is an ancestor of A’ Subgoal A is called a pioneer and A’ is called a follower of A.
Linear Tabling8 Definitions (cont.) 4 Top-most looping nodes and subgoals A node in an SLD-tree is called a top-most looping node if the selected subgoal of the node is the pioneer of a loop that is not contained in any other loops.
Linear Tabling9 A Linear Tabling Method 4 table_start(A) –If A is complete, resolve A by using answers. –If A is a pioneer, register A and resolve A by using program clauses. –If A is a follower, resolve A by using answers and fail A after all existing answers are exhausted.
Linear Tabling10 A Linear Tabling Method (cont.) 4 memo(A) –Add A into the table and fail.
Linear Tabling11 A Linear Tabling Method (cont.) 4 check_completion(A) –If A has never occurred in a loop, complete A and resolve A by using the answers. –If A is a top-most looping subgoal If no new answer was produced in the last round, then complete A and resolve A by using the answers Otherwise, start a new round of evaluation of A. –If A is a looping subgoal but not a top-most one Set A ’s state to temporary complete and resolve A by using the answers
Linear Tabling12 Example p(X,Y):-p(X,Z),e(Z,Y),memo(p(X,Y)).(p1) p(X,Y):-e(X,Y),memo(p(X,Y)).(p2) p(X,Y):-check_completion(p(X,Y)). (p3) e(a,b).(e1) e(b,c).(e2) 1. p(a,Y0). 2. p(a,Z1), e(Z1,Y0), memo(p(a,Y0)). 3. e(a,Y0), memo(p(a,Y0)). 4. memo(p(a,b)). 5. check_comp(p(a,Y0)). First round p1 p2 e1 p3 program
Linear Tabling13 1. p(a,Y0). 6. p(a,Z1), e(Z1,Y0), memo(p(a,Y0)). 10. check_comp(p(a,Y0)). Second round p1p3 7. e(b,Y0), memo(p(a,Y0)). 8. memo(p(a,c)). use p(a,b) e2 9. e(c,Y0), memo(p(a,Y0)). use p(a,c) p(X,Y):-p(X,Z),e(Z,Y),memo(p(X,Y)).(p1) p(X,Y):-e(X,Y),memo(p(X,Y)).(p2) p(X,Y):-check_completion(p(X,Y)). (p3) e(a,b). (e1) e(b,c).(e2) p(a,b). program table p2 … p(a,b). p(a,c).
Linear Tabling14 Characteristics of the Method 4 Fixpoints are computed by iterating the evaluation of top-most looping subgoals 4 Followers consume answers only 4 Pioneers consume answers lazily –Top-most looping subgoals consume answers after they are complete –Other looping subgoals consume answers after all clauses have been tried
Linear Tabling15 Adopted and Related Tabling Strategies 4 Lazy answer consumption –Local scheduling strategy in SLG-WAM [Freire96] 4 What to do after a follower consumes all available answers? –Steals the pioneer’s choice pointer [Zhou00] –Fails the follower [Guo & Gupta 01] 4 Where to start re-computation? –At the top-most looping subgoal [Shen98] –At every looping subgoal [Guo01]
Linear Tabling16 Strengths and Weaknesses 4 Lazy answer consumption is suitable for all- solution search –A basic operation used in PRISM 4 Not suitable for single-solution search or programs with cuts –For the query, once(p(X)), a ll solutions are computed even though only one is needed.
Linear Tabling17 Optimization Techniques 4 Subgoal Optimization In each round of evaluation of a top-most looping subgoal, each subgoal needs to be evaluated only once. 4 Semi-naïve Optimization Mimic the semi-naïve technique in bottom-up evaluation: at least one new answer is involved in the join of answers for each rule.
Linear Tabling18 Semi-naïve Evaluation in Linear Tabling Let H:-A 1,…,A k,…,A n be a rule where A k is the last dependent subgoal of H. For a subgoal C of H, it is safe for A k to consume only new answers if: –1. C has occurred in an early round –2. No subgoal A i (i<k) has consumed a new answer.
Linear Tabling19 Performance Evaluation 4 BP vs. XSB (CPU time) 4 BP vs. XSB (Stack space)
Linear Tabling20 Papers 1.N.F. Zhou, Y.D. Shen, L. Yuan, and J. You: A Linear Tabling Mechanism, The Journal of Functional and Logic Programming, N.F. Zhou and T. Sato: Efficient Fixpoint Computation in Linear Tabling, ACM SIGPLAN International Conference on Principles and Practice of Declarative Programming (PPDP), pp , N.F. Zhou, Y. Shen, and T. Sato: Semi-naive Evaluation in Linear Tabling, ACM PPDP, pp.90-97, 2004.