Sunteți pe pagina 1din 17

Advanced tabling mechanisms

Advanced tabling mechanisms


A call subsumption based tabling engine for Yap Prolog

Flávio Cruz <flaviocruz@gmail.com>


Advisor: Ricardo Rocha <ricroc@dcc.fc.up.pt>

1 Center for Research in Advanced Computing Systems


2 Faculdade de Ciências da Universidade do Porto

February 1, 2010

CRACS Flávio Cruz February 1, 2010 1 / 17


Advanced tabling mechanisms Outline

1 Prolog and SLD


Limitations

2 Tabling
Evaluation
Variant Tabling

3 Subsumptive Tabling
Evaluation

4 Table Space
Variant Table Space
Subsumptive Table Space

5 Objectives

6 Work Plan

CRACS Flávio Cruz February 1, 2010 2 / 17


Advanced tabling mechanisms Prolog and SLD

Prolog and SLD evaluation

In Logic Programming, Selective Linear Definite (SLD) is a top-down


resolution mechanism.
SLD is inherently non-deterministic and does not force any specific
search strategy.
The Prolog language uses depth first search.
Despite being widely used, the current evaluation method has a few
shortcomings.

CRACS Flávio Cruz February 1, 2010 3 / 17


Advanced tabling mechanisms Prolog and SLD

SLD shortcomings

?- path(1, Z) Program
path(X, Z) :- path(X, Y),
1. path(1, Z) edge(Y, Z).
path(X, Z) :- edge(X, Z).

2. path(1, Y), edge(Y, Z) edge(1, 2).


edge(2, 3).

3. path(1, Y)

1 2 3
Infinite Loop

CRACS Flávio Cruz February 1, 2010 4 / 17


Advanced tabling mechanisms Tabling

Tabling

Tabling is a refinement of the SLD resolution.


Consists in storing intermediate answers for subgoals so that they can
be reused when a repeated subgoal appears in the resolution process.
Tabling evaluation reduces the search space, prunes redundant
computations and has better termination properties.
Enables more expressive logic programs.

CRACS Flávio Cruz February 1, 2010 5 / 17


Advanced tabling mechanisms Tabling

Tabled Evaluation

Example

1. path(1, Z)

2. path(1, Y), edge(Y, Z) 3. edge(1, Z) 11. complete

Y = 2 Y = 3

6. edge(2, Z) 9. edge(3, Z) 4. Z = 2 5. fail

7. Z = 3 8. fail 10. fail

Answers: { (4) Z = 2, (7) Z = 3 }.

CRACS Flávio Cruz February 1, 2010 6 / 17


Advanced tabling mechanisms Tabling

Variant Tabling

In the previous example, when a subgoal is called, we verify if a


variant subgoal is already in the table space.
A subgoal G is variant of subgoal G � , when G and G � represent the
same subgoal by variable renaming.
path(X, Y) is variant of path(A, B), because both represent
path(VAR0, VAR1).
While this is a simple approach, more sophisticated techniques are
known.
The YapTab tabling engine from Yap only supports variant checking.

CRACS Flávio Cruz February 1, 2010 7 / 17


Advanced tabling mechanisms Subsumptive Tabling

Subsumptive Tabling

Tabling by call subsumption aims to reuse answer computations by


sharing answers from more general goals.

Theorem
If two subgoals G and G � exist, such that S and S � are the respective
answer sets and G � subsumes G , then S ⊆ S � .

Example: path(X, Z) subsumes path(1, Z).


Subsumptive checking is implemented in SLG-WAM from XSB.
In the past, a technique called Dynamic Threaded Sequential
Automata (DTSA) was used, currently they use Time Stamped Tries
(TST).

CRACS Flávio Cruz February 1, 2010 8 / 17


Advanced tabling mechanisms Subsumptive Tabling

Subsumptive Evaluation

Answers Program
path(X, Z): {(3) X=1 Z=2, (4) X=2 path(X, Z) :- edge(X, Z).
Z=3, (9) X=1 Z=3} path(X, Z) :- edge(X, Y), path(Y, Z).
path(2, Z) {(9) Z=3}
edge(1, 2). edge(2, 3).
path(3, Z): ∅

Example

1. path(X, Z)

2. edge(X, Z) 7. edge(X, Y), path(Y, Z) 12. complete

X=1,Y=2 X=2,Y=3
3. X = 1 4. X = 2
5. fail 8. path(2, Z) 10. path(3, Z) 11. fail
Z = 2 Z = 3

9. Z = 3

CRACS Flávio Cruz February 1, 2010 9 / 17


Advanced tabling mechanisms Table Space

Variant Table Space

Tries are used to index terms.


Tries are a tree-structured automaton where common term prefixes
are represented only once.
Two levels of tries:
� Call Trie: stores subgoals for a specific predicate, like path/2.
� Answer Trie: where answers are stored.
Only the values of variables are stored in the answer trie, in a
mechanism called substitution factoring.
A leaf state in a subgoal trie points to a subgoal frame where state
about the subgoal evaluation is kept.

CRACS Flávio Cruz February 1, 2010 10 / 17


Advanced tabling mechanisms Table Space

Variant Table Space


Example
Table entry for path/2

Subgoal trie for path/2

root

a b VAR0

VAR0 VAR0 VAR1

Subgoal Frame for Subgoal Frame for Subgoal Frame for


path(a, VAR0) path(b, VAR0) path(VAR0, VAR1)

Answer trie for path(a, VAR0)

root

a b

CRACS Flávio Cruz February 1, 2010 11 / 17


Advanced tabling mechanisms Table Space

Time Stamped Tries

A subsumptive approach to tabling.


Instead of using an answer trie, in this technique we use a time
stamped trie.
Whenever a new answer is found, the timestamps along the answer
path are updated to the incremented maximum time stamp.
Each subsumed goal keeps track of the last time stamp used, thus it
can retrieve only the new answers by unification when consuming.
The main objective is to avoid repeated answers.

CRACS Flávio Cruz February 1, 2010 12 / 17


Advanced tabling mechanisms Table Space

Time Stamped Tries

Time stamped trie for subgoal p(X, Y, Z):

Example (before) Example (new answer p(a, b, c))

root node root node

a, 5 b, 2 a, 6 b, 2

a, 5 b, 4 a, 2 a, 5 b, 6 a, 2

d, 5 c, 3 a, 1 b, 4 VAR0, 2 d, 5 c, 3 a, 1 c, 6 b, 4 VAR0, 2

CRACS Flávio Cruz February 1, 2010 13 / 17


Advanced tabling mechanisms Table Space

Finding subsuming goals

Another important component in call by subsumption deals with


locating more generals subgoals in the call trie.
Unification and backtracking are used by SLG-WAM.
The algorithm finds a minimally subsuming call, hence it can be used
to efficiently check wether the found subgoal is a variant or not.

CRACS Flávio Cruz February 1, 2010 14 / 17


Advanced tabling mechanisms Objectives

Objectives

Implement a subsumptive tabling engine in Yap, using the


SLG-WAM’s time stamped tries approach.
Work on new approaches and optimizations using the previous work.
We are trying to reuse as much code from XSB as possible to enable
novel features to be used by both Prolog systems.
Produce original work.

CRACS Flávio Cruz February 1, 2010 15 / 17


Advanced tabling mechanisms Work Plan

Work Plan

Until the end of February: complete a prototype version using time


stamped tries.
March, April, May: implement a few optimizations and try some new
approaches.
May, June: write the dissertation.
Lots of testing!
Produce one or two papers.

CRACS Flávio Cruz February 1, 2010 16 / 17


Advanced tabling mechanisms Work Plan

Some useful links

http://github.com/flavioc/yap/
http://cracs.fc.up.pt/
http://www.dcc.fc.up.pt/~vsc/Yap/
http://flaviocruz.net/

CRACS Flávio Cruz February 1, 2010 17 / 17

S-ar putea să vă placă și