![Page 1: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/1.jpg)
Tabled Logic Programming and Its Applications
Manuel Carro1,2 Pablo Chico de Guzman1
1School of Computer Science, Technical University of Madrid, Spain2IMDEA Software Institute, Spain
Prometidos-CM Summer SchoolFacultad de Informatica, UCM – September 19-21, 2011
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 1 / 45
![Page 2: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/2.jpg)
Roadmap
Introduction: the Logic Programming paradigm.
Pitfalls.
Tabling Basics:I Memoing.I Breaking loops.
Analysis of some applications.
Advanced Tabling Capabilities:I Subsumptive tabling.I Negation with tabling.
Implementation issues.
Open research topics.
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 2 / 45
![Page 3: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/3.jpg)
The Logic Programming Paradigm
![Page 4: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/4.jpg)
The Logic Programming Paradigm
Intro to LP
Logic programming: declarative, relational style.
Origins linked to theorem proving and linguistics.I Try to solve problems / execute programs with a theorem prover.I Can you do that? Yes, but you (initially) end up with a sort of limited
theorem prover.I Tabling also tries to overcome some of these initial limitations.
Aim: clean, expressive, obviously correct programs.
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 4 / 45
![Page 5: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/5.jpg)
The Logic Programming Paradigm
Facts, Queries, Rules
person(mike). person(alice ).
person(bob). person(mirna ).
food(vegetable , salad ). likes(mike , vegetable ).
food(vegetable , beans ). likes(mike , fish).
food(meat , chicken ). likes(alice , vegetable ).
food(meat , rabbit ). likes(bob , fish).
food(meat , pork). likes(bob , meat).
food(fish , soul). likes(bob , vegetable ).
food(fish , octopus ). likes(mirna , fish).
likes(mirna , vegetable ).
meal(Person , Food):-
likes(Person , Type),
food(Type , Food).
same_food(P1, P2, Food):-
meal(P1, Food),
meal(P2, Food).
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 5 / 45
![Page 6: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/6.jpg)
The Logic Programming Paradigm
Execution of a Query
?- meal(alice, What).
likes(alice, T), food(T, What)
food(vegetables, What)
T = vegetables
√ √What = salad What = beans
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 6 / 45
![Page 7: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/7.jpg)
The Logic Programming Paradigm
Execution of a Query
?- meal(Who, beans).
likes(Who, T), food(T, beans)
food(vegetable, beans)
food(fish, beans)
food(vegetable, beans)
food(fish, beans)
. . .
Who = mike
T = vegetable
Who = mike
T = fish
Who = alice
T = vegetableWho = bob
T = fish
√fail
√fail
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 7 / 45
![Page 8: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/8.jpg)
The Logic Programming Paradigm
Logical Variables
?- likes(mirna , X), X = meat.
no
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 8 / 45
![Page 9: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/9.jpg)
The Logic Programming Paradigm
Arithmetic
Prolog arithmetic is a compromise.
X is ExpI Aritmetically evaluates Exp
I Unifies result with X
?- X is 3 * sqrt (2).
X = 4.242640687119286 ?
?- X = 3 * sqrt (2).
X = 3*sqrt (2) ?
?- 2 is 1 * X.
{ERROR: arithmetic :*/2, arg 1 - Instantiation Error}
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 9 / 45
![Page 10: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/10.jpg)
The Logic Programming Paradigm
Interpretation of Formulas
Rules and facts are stylized formulas:
meal(Person , Food):-
likes(Person , Type),
food(Type , Food).
is a way of writing the formula
∀P ∀F ∃T likes(P,T ) ∧ food(T ,F )→ meal(P,F )
and a query
?- meal(mirna , X).
is an attempt to mechanically and constructively determine whether
∃X .meal(mirna,X )
holds. If so, it is demonstrated by finding an X for which meal(mirna,X )can be inferred from the program. This is what a Prolog system does byapplying an automated deduction procedure.
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 10 / 45
![Page 11: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/11.jpg)
The Logic Programming Paradigm
Structured Data
couple(married(mirna , bob )).
?- couple(C).
C = married(mirna , bob)
?- couple(married(mirna , X)).
X = bob
Lists
What Prolog notationEmpty list []
Singleton list [a]
Two-element list [a, b]
List with a head a and a tail T [a|T]
List starting with a, b, c and continuing with tail T [a, b, c|T]
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 11 / 45
![Page 12: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/12.jpg)
The Logic Programming Paradigm
Pitfalls
Redundant computations
hanoi(N, L):-
hanoi(N, A, B, C, L),
A = a, B = b, C = c.
hanoi(0, _, _, _, []).
hanoi(N, A, B, C, M):-
N > 0,
N1 is N - 1,
hanoi(N1, A, C, B, M1),
hanoi(N1, C, B, A, M2),
append(M1, [move(A, B)|M2], M).
fib (0) := 0.
fib (1) := 1.
fib(N) := fib(N-1) + fib(N-2)
:- N > 1.
Non termination of otherwise correct programs
path(A, B):-
edge(A, C),
path(C, B).
path(A, B):- edge(A, B).
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 12 / 45
![Page 13: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/13.jpg)
Tabling Basics
![Page 14: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/14.jpg)
Tabling Basics
Towards Tabling: Memoing
Remember calls and their answers.
Can improve speed.
We will see two examples.
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 14 / 45
![Page 15: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/15.jpg)
Tabling Basics
Towards Tabling: Memoing (Cont.)
hanoi(0, _A, _B, _C , []).
hanoi(N, A, B, C, M):-
N > 0, N1 is N - 1,
hanoi(N1, A, C, B, M1),
hanoi(N1, C, B, A, M2),
append(M1, [move(A, B)|M2], M).
| ?- hanoi(2, A, B, C, L).
L = [move(A,C),move(A,B),move(C,B)]
Solutions for a given size differ only on name of the pegs.There are two (recursive) calls of the same size.
I Calling with free variables gives the most general answer.
Memoing answer and recovering it adjusting variables in the answerto match those in the call avoids recomputing answer.
Length of answer in order of computation steps, so constant speedupexpected.
Experimentally, 3-fold speedup by adding :- table hanoi/5.
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 15 / 45
![Page 16: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/16.jpg)
Tabling Basics
Towards Tabling: Memoing (Cont.)
fib (0) := 0.
fib (1) := 1.
fib(N) := fib(N-1) + fib(N-2) :- N > 1.
Computing fib(N-1) includes the computation of fib(N-2).
Memoing answers greatly speeds up computation.
:- table fib/2. reduces complexity from exponential (O(( 1+√
52 )n))
to linear!
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 16 / 45
![Page 17: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/17.jpg)
Tabling Basics
Towards Tabling: Termination Issues
path(A, B):- edge(A, C), path(C, B).
path(A, B):- edge(A, B).
edge(a, b). edge(b, a). edge(b, c).
a b c
Logical reading:
There is a path from A to B if they are directly connected by edges.
There is a path from A to B if there is an edge from A to C and aconnection from A to B.
Question:
Which nodes are reachable from a??- path(a, X).
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 17 / 45
![Page 18: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/18.jpg)
Tabling Basics
Towards Tabling: Termination Issues
path(A, B):- edge(A, C), path(C, B).
path(A, B):- edge(A, B).
edge(a, b). edge(b, a). edge(b, c).
a b c
Standard Prolog (SLD) strategy leads to loops:
?- path(a, X).
edge(a, C1), path(C1, X)
edge(a, b), path(b, X)
edge(a, b), edge(b, C2), path(C2, X)
edge(a, b), edge(b, a), path(a, X)
Initial reaction: carry around list of visited nodes.
But: is it necessary? What is wrong with the program? Does not itprecisely specify the meaning of being reachable?
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 18 / 45
![Page 19: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/19.jpg)
Tabling Basics
Towards Tabling: Bottom-Up Evaluation
Read the program as logic:
path(A, B) ← edge(A, C) ∧ path(C, B).
path(A, B) ← edge(A, B).
edge(a, b). edge(b, a). edge(b, c).
a b c
From {edge(a, b), edge(b, a), edge(b, c)}and path(A, B) ← edge(A, B)
infer {path(a, b), path(b, a), path(b, c)}using path(A, B) ← edge(A, C) ∧ path(C, B)
infer {path(a, a), path(b, b), path(a, c)}Nothing more can be inferred. The meaning of the program is:
{edge(a, b), edge(b, a), edge(b, c), path(a, b), path(b,
a), path(b, c), path(a, a), path(b, b), path(a, c)}
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 19 / 45
![Page 20: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/20.jpg)
Tabling Basics
Towards Tabling: Bottom-Up Evaluation
path(A, B) ← edge(A, C) ∧ path(C, B).
path(A, B) ← edge(A, B).
edge(a, b). edge(b, a). edge(b, c).
a b c
Note that:
We have not changed the program (no visited node list).
But: we wanted to find out which nodes are reachable from node aand we computed much more.
I We actually computed the least fixpoint of the program (its standardsemantics).
I Other applications could require the greatest fixpoint.
More on bottom-up evaluation surely in the Deductive Databases talk.
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 20 / 45
![Page 21: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/21.jpg)
Tabling Basics
Towards Tabling: Suspending and Resuming
path(A, B) :- edge(A, C), path(C, B).
path(A, B) :- edge(A, B).
edge(a, b). edge(b, a). edge(b, c).
a b c
Challenge: a goal-directed (top-down) strategy which
Detects loops.
Derives only the consequences necessary for top-down execution.
Idea: when we havep:- q, p, r.
p.
and the call to p is repeated (enters loop):
1 Suspend the computation before calling p.
2 Use the fact p to generate a solution to the original query.
3 Use this solution to resume the suspended computation.
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 21 / 45
![Page 22: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/22.jpg)
Tabling Basics
Tabled Evaluation
Example
:- table p/1.
p(A) :-
q,
p(B),
A = 2.
p(A) :-
A = 1.
Subgoal Answers
4. A = 11. p(A) 8. A = 2
13. Complete
1. p(A).
?− p(A).
5. A = 1.
9. A = 2.
14. no.
3. A = 1.
2. p(B), A = 2.
Suspension
1. q, p(B), A = 2.
10. p(2), A = 2.
11. A = 2.
6. p(1), A = 2.
7. A = 2.
12. fail
Linear tabling uses recomputation instead of suspension.
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 22 / 45
![Page 23: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/23.jpg)
Some Applications
![Page 24: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/24.jpg)
Some Applications
Parsing
Let us assume input has been tokenized
Arithmetic terms
expr −→ expr + term
expr −→ term
term −→ term * fact
term −→ fact
fact −→ ( expr )
fact −→ int(Int)
Examples of strings we want to parse:
String Evaluates to3 33 + 5 83 * 4 122 + 3 * 4 14(2 + 3) * 4 20
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 24 / 45
![Page 25: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/25.jpg)
Some Applications
Parsing (Cont.)
Attempt of a Prolog parserfor the previous grammar1
expr −→ expr + term | term
term −→ term * fact | fact
fact −→ ( expr ) | int(Int)
expr(S0,S) :- expr(S0,S1), S1 = [+|S2], term(S2,S).
expr(S0,S) :- term(S0,S).
term(S0,S) :- term(S0,S1), S1 = [*|S2], fact(S2,S).
term(S0,S) :- fact(S0,S).
fact(S0,S) :- S0 = [’(’|S1], expr(S1,S2), S2 = [’)’|S].
fact(S0,S) :- S0 = [N|S], integer(N).
expr(I, R) means: “R is a suffix ofI, and the prefix of I w.r.t. R is avalid expression.”
?- expr([3, +, 4, *], R).
R = [*]
R = [+,4,*]
Complete parsing: ?- expr(Input, []).
1Prolog provides a specific syntax to write parsers, closer to the “−→” notation.Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 25 / 45
![Page 26: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/26.jpg)
Some Applications
Parsing (Cont.)Parsing in action
expr(S0,S) :- expr(S0,S1), S1 = [+|S2], term(S2,S).
expr(S0,S) :- term(S0,S).
term(S0,S) :- term(S0,S1), S1 = [*|S2], fact(S2,S).
term(S0,S) :- fact(S0,S).
fact(S0,S) :- S0 = [’(’|S1], expr(S1,S2), S2 = [’)’|S].
fact(S0,S) :- S0 = [N|S], integer(N).
?- expr([3], []). (wait forever...)
What is happening?
?- expr ([3], []).
expr([3], S11), S11 = [+|S21], term(S21, [])
expr([3], S12), S12 = [+|S22], term(S22, S11), S11 = [+|S21], ...
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 26 / 45
![Page 27: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/27.jpg)
Some Applications
Parsing (Cont.)
A left-recursive rule
expr(S0 ,S) :- expr(S0 ,S1), S1 = [+|S2], term(S2 ,S).
generates a repeated computation and a loop.
Tabling left-recursive rules amends this problem:
:- table expr/2, term /2.
Then:
?- expr([3, +, 4, *], []).
no
?- expr([3, +, 4, *, 7], []).
yes
?- expr([’(’, 3, +, 4, ’)’, *, 7], []).
yes
?- E = [_,_,_,_,_,_,_,_], expr(E, []).
no
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 27 / 45
![Page 28: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/28.jpg)
Some Applications
Tabling and Parsing
Tabling + a recursive descendant parser (like the one we havepresented) implements an Earley parsing:
The Earley parser is a type of chart parser mainly used forparsing in computational linguistics, named after itsinventor, Jay Earley. The algorithm uses dynamicprogramming. — Wikipedia
Earley parsers parse all context-free grammars:I Complexity O(n3) for ambiguous grammars.I Complexity O(n2) for unambiguous grammarsI Linear for a large class of grammars.
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 28 / 45
![Page 29: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/29.jpg)
Some Applications
For Completeness: Earley Parser
f u n c t i o n EARLEY−PARSE( words , grammar )ENQUEUE( (γ → •S , [ 0 , 0 ] ) , c h a r t [ 0 ] )
f o r i ← from 0 to LENGTH( words ) dof o r each s t a t e i n c h a r t [ i ] do
i f INCOMPLETE?( s t a t e ) theni f NEXT−CAT( s t a t e ) i s a n o n t e r m i n a l then
PREDICTOR( s t a t e ) // non−t e r m i n a le l s e do
SCANNER( s t a t e ) // t e r m i n a le l s e do
COMPLETER( s t a t e )end
endr e t u r n c h a r t
p r o c e d u r e PREDICTOR ( (A → α•B, [ i , j ] ) ) ,f o r each (B → γ ) i n GRAMMAR−RULES−FOR(B, grammar ) do
ENQUEUE( (B → •γ , [ j , j ] ) , c h a r t [ j ] )end
p r o c e d u r e SCANNER( (A → α•B, [ i , j ] ) ) ,i f B ⊂ PARTS−OF−SPEECH( word [ j ] ) then
ENQUEUE( (B → word [ j ] , [ j , j + 1 ] ) , c h a r t [ j + 1 ] )end
p r o c e d u r e COMPLETER( (B → γ• , [ j , k ] ) ) ,f o r each (A → α•Bβ , [ i , j ] ) i n c h a r t ( [ j ] ) do
ENQUEUE( (A → αB•β , [ i , k ] ) , c h a r t [ k ] )end
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 29 / 45
![Page 30: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/30.jpg)
Some Applications
Automatic Dynamic Programming
Knap-sack problem: given n items of integer size ki (1 ≤ i ≤ n), anda knap-sack size K :
I Determine whether there is a subset of the items that sums to K.I Find such a subset — we are not maximizing / minimizing.
Program: try all items (n, n − 1, . . . , 1) and decide, for each item,whether to include it or not.
ks(0,0).
ks(I,K) :- I>0, %% Skip item Ith
I1 is I-1, ks(I1,K).
ks(I,K) :- I>0, %% Include item Ith
item_size(I,Ki),
K1 is K-Ki, K1 >= 0,
I1 is I-1, ks(I1,K1).
item_size (1,2). item_size (2,3).
item_size (3,5). item_size (4,6).
Worst-case complexity is 2n.
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 30 / 45
![Page 31: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/31.jpg)
Some Applications
Automatic Dynamic Programming
A tabling solution to the knapsack 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_size(I,Ki), K1 is K-Ki ,
K1 >= 0, I1 is I-1,
ks(I1 ,K1).
item_size (1,2). item_size (2,3).
item_size (3,5). item_size (4,6).
Worst-case complexity is O(nK ).
Intuitition: for every n and every number smaller than or equal to K ,we check only once whether it succeeds or fails.
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 31 / 45
![Page 32: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/32.jpg)
Some Applications
Knapsack and (again) Graph Traversal
I0 I1 I2 In−1 In
add I1
¬add
add I2
¬add
add In
¬add
Decision at every node vs. incrementally building a transitive closure.
In fact, Knapsack solution very similar to graph traversal.
Many problems very similar to graph traversals — or directly graphtraversal.
I E.g., model checking.
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 32 / 45
![Page 33: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/33.jpg)
Some Applications
For Completeness: Udi Manber Procedural Version
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 33 / 45
![Page 34: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/34.jpg)
Some Applications
Tabling: Recap and Big Picture
Tabling terminates and is complete for all calls with thebounded-term-depth property (those for which there is a bound onthe size of the terms which can be generated).
Tabling computes a fix point (the least fix point) for tabled predicatesin a goal-driven way.
Improves efficiency (automatic dynamic programming).
Improves termination.
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 34 / 45
![Page 35: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/35.jpg)
Some Applications
Tabling: Recap and Big Picture
It is not complete (naturally) for programs with an infinite leastfixpoint:
n(0).
n(s(X)):- n(X).
?- n(X), X = a.
It terminates for programs which loop due to repeated calls.
n(s(X)):- n(X).
?- n(X).
It may not terminate for programs without bound term depthproperty:
p(X):- p(s(X)).
?- p(X).
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 35 / 45
![Page 36: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/36.jpg)
Advanced Tabling Capabilities
![Page 37: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/37.jpg)
Advanced Tabling Capabilities
Variant vs. Subsumption Tabling
p(f(Y),X,1) and p(f(Z),U,1) are variants as one can be made tolook like the other by a renaming of the variables.
Let us have t1: p(f(Y),X,1) and t2: p(f(Z),Z,1).I We can rename variables in t1 to become t2, but not the other way
around.I They are not variants.I t1 subsumes (is more general than) t2.
Call Variance vs Call Subsumption:I Either Variance or Subsumption can be used to check if we are
executing a new tabled call.
Answer Variance vs Answer Subsumption:I Either Variance or Subsumption can be used to check if we have
computed a new answer for a tabled predicate.
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 37 / 45
![Page 38: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/38.jpg)
Advanced Tabling Capabilities
Variant vs. Subsumption Tabling (Cont.)
Variant Tabling:I More efficient table look-up.I Better for side-effects.I Useful for goal-directed queries and meta-interpreters.
Subsumption Tabling:I Can avoid more recomputation (catches more similarities).I Better termination:
:- table ps/1 as subsumptive.
p(X) :- p(s(X)).
I Can save space in internal tables.
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 38 / 45
![Page 39: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/39.jpg)
Advanced Tabling Capabilities
Variant vs. Subsumption Tabling (Cont.)
Subsumption can be parametric
It is natural to use syntactical subsumption.
However, from a more semantic / knowledge reasoning point of view,terms may have an interpretation from which subsumption can beinferred.
E.g., the term X > 3 subsumes X = 4 in the arithmetic domain.
is(some animal, dog) is subsumed by belongs(some animal,
mammal).
Lattice for subsumption check.
Also in abstract interpretation: relations in lattice of abstract domainencoded in subsumption lattice.
I Remember tabling computes a fixpoint.I Basically, what an abstract interpreter does!
(and the mechanism is not very different)
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 39 / 45
![Page 40: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/40.jpg)
Advanced Tabling Capabilities
Negation and Well-Founded Semantics
Gives semantics for all normal logic programs. It allows logic programs toadequately handle inconsistencies and paraconsistencies, for example:“The village barber shaves everyone in the village who does not shavehimself.”
shaves(barber ,Person):-
villager(Person),
not shaves(Person ,Person ).
shaves(doctor ,doctor ). % Play with this
villager(barber ). villager(doctor ). villager(mayor ).
?- shaves(X, Y).
Applications: verification, Flora-2, medical informatics. . .
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 40 / 45
![Page 41: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/41.jpg)
Implementation Break
![Page 42: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/42.jpg)
Implementation Break
Suspension Based Implementation
Sharing local stack and heap.Copying those bindings which are different for each consumer.
POINTS
CHOICE
G
TRAIL
HEAP
...
[
1
X
]
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 42 / 45
![Page 43: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/43.jpg)
Implementation Break
Suspension Based Implementation
Sharing local stack and heap.Copying those bindings which are different for each consumer.
POINTS
CHOICE
G
TRAIL
C1
HEAP
...
[
1
2
]
p
q
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 42 / 45
![Page 44: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/44.jpg)
Implementation Break
Suspension Based Implementation
Sharing local stack and heap.Copying those bindings which are different for each consumer.
POINTS
CHOICE
G
TRAIL
C1
HEAP
...
[
1
]
p
q
2
C1
@X
2
CHAT AREA
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 42 / 45
![Page 45: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/45.jpg)
Implementation Break
Suspension Based Implementation
Sharing local stack and heap.Copying those bindings which are different for each consumer.
POINTS
CHOICE
G
TRAIL
HEAP
...
[
1
X
]
p
q
C1
@X
2
CHAT AREA
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 42 / 45
![Page 46: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/46.jpg)
Implementation Break
Suspension Based Implementation
Sharing local stack and heap.Copying those bindings which are different for each consumer.
POINTS
CHOICE
G
TRAIL
C2
HEAP
...
[
1
3
]
p
q
r
s
C1
@X
2
CHAT AREA
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 42 / 45
![Page 47: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/47.jpg)
Implementation Break
Suspension Based Implementation
Sharing local stack and heap.Copying those bindings which are different for each consumer.
POINTS
CHOICE
G
TRAIL
C2
HEAP
...
[
1
3
]
p
q
r
s
C1
@X
2
CHAT AREA
@X
3C2
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 42 / 45
![Page 48: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/48.jpg)
Implementation Break
Suspension Based Implementation
Sharing local stack and heap.Copying those bindings which are different for each consumer.
POINTS
CHOICE
G
TRAIL
HEAP
...
[
1
X
]
p
q
r
s
C1
@X
2
CHAT AREA
@X
3C2
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 42 / 45
![Page 49: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/49.jpg)
Implementation Break
Suspension Based Implementation
Sharing local stack and heap.Copying those bindings which are different for each consumer.
POINTS
CHOICE
G
TRAIL
HEAP
...
[
1
2
]
p
q
r
s
C1
C1
@X
2
CHAT AREA
@X
3C2
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 42 / 45
![Page 50: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/50.jpg)
Implementation Break
Suspension Based Implementation
Sharing local stack and heap.Copying those bindings which are different for each consumer.
POINTS
CHOICE
G
TRAIL
HEAP
...
[
1
3
]
p
q
r
s
C2
C1
@X
2
CHAT AREA
@X
3C2
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 42 / 45
![Page 51: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/51.jpg)
Open Research Topics
![Page 52: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/52.jpg)
Open Research Topics
Open Research Topics
Pruning of Answer-On-Demand Tabling.
Subsumptive tabling with constraints.
Pruning of previous subsumed calls.
Call abstraction.
Parallelism.
Table compression.
Side-effects.
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 44 / 45
![Page 53: Tabled Logic Programming and Its Applications [2ex]software.imdea.org/~mcarro/Material/Tabling/intro_to_tabling.pdf · Tabled Logic Programming and Its Applications Manuel Carro1;2](https://reader030.vdocuments.site/reader030/viewer/2022040800/5e3553ed1684e15c9765e65e/html5/thumbnails/53.jpg)
Open Research Topics
Thanks
Parts of this presentation draw from material from:
Terrance Swift (CENTRIA, Universidade Novade Lisboa)
David S. Warren (NYU at Stony Brook)
to whom we thank.
Carro, Chico de Guzman (IMDEA,UPM) Tabling and applications Prometidos SS 45 / 45