slides tabled clp.pdf

Preview:

Citation preview

A General Implementation Frameworkfor Tabled CLP

Pablo Chico de Guzman1 Manuel Carro1,2

Manuel V. Hermenegildo1,2 Peter Stuckey3

1IMDEA Software Institute, Spain2School of Computer Science, Technical University of Madrid, Spain

3University of Melbourne, Australia

FLOPS’12 — Kobe, Japan — May 25, 2012

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 1 / 1

Logic Programming

Contents

Introduction to Tabling.

Constraint Logic Programming (CLP).

Main goal: the combination of tabling and CLP.

Applications of Tabled CLP.

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 2 / 1

Logic Programming

Prolog basics (SLD resolution)Non-determinism and logical variables

Example

reach(X,Y) :-

edge(X,Z),

reach(Z,Y).

reach(X,Y) :-

edge(X,Y).

edge (1,2).

edge (1,3).

?- reach(1,Y).

Y = 2;

Y = 3;

no

1. reach(1,Y).

2. edge(1,Z), reach(Z,Y). 5. edge(1,Y).

3. reach(2,Y). 4. reach(3,Y). 6. edge(1,2).

7. Y = 2.

9. Y = 3.

10. no

8. edge(1,3).

fail fail

Z=2 Z=3 Y=2Y=3

1

2 3

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 3 / 1

Logic Programming

SLD pitfallsInfinite loops

Example

reach(X,Y) :-

edge(X,Z),

reach(Z,Y).

reach(X,Y) :-

edge(X,Y).

edge (1,2).

edge (2,1).

?- reach(1,Y).

...

2

1

1. reach(1,Y).

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

3. reach(2,Y).

4. edge(2,Z), reach(Z,Y).

5. reach(1,Y).

Z=2

Z=1

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 4 / 1

Tabling

The tabling algorithm through an example (OLDT resolution)

Example

:- table reach /2.

reach(X,Y) :-

edge(X,Z),

reach(Z,Y).

reach(X,Y) :-

edge(X,Y).

edge (1 ,2).

edge (2 ,1).

?- reach(1,Y).

Y = 1;

Y = 2;

no

Subgoal Answers

10. Y = 1

2. reach(1,Y) 15. Y = 2

18. Complete

9. Y = 1

5. reach(2,Y) 17. Y = 2

18. Complete

1. reach(1,Y).

4. reach(2,Y).

7. reach(1,Y).

3. edge(1,Z), reach(Z,Y). 13. edge(1,Y).

6. edge(2,Z), reach(Z,Y). 8. edge(2,1).

14. edge(1,2).

12. fail

11. reach(1,1). 16. reach(1,2).

Z=2

Z=1

Y=1 Y=2

Y=2

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 5 / 1

Tabling

Why is tabling interesting?

Properties:I Conservative extension of Prolog.I Avoid recomputations.I Better termination properties; easier to reason about termination.

F Ensures termination for “bounded term size” programs.F In other cases, less dependent on clause / subgoal order.

Applications:I Deductive databases.I Natural language (left recursive grammars).I Fixpoint: program analysis, reachibility analysis. . .I Well Founded Semantics:

F A predicate can be defined based on its negation.F Semantic web reasoning.

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 6 / 1

CLP

Constraint Logic Programming

Natural extension of LP: very general relations between variablesallowed (beyond Herbrand term equality).

Prolog execution inserts new constraints in the constraint store (CS).

Constraint solver checks consistency of CS.

Examplep(X,Y) :-

X #> 5,

X #< 2.

p(X,Y) :-

X #>= 2,

Y #=< 2,

X #= Y.

?- p(X,Y).

X = 2,

Y = 2;

no

1. p(X,Y) CS=�

3. X #<2.

2. X #>5, X #<2. CS=�

CS={X>5}

CS={X>5, X<2}4. fail.

5. X #≥2, Y #≤2, X#=Y.

6. Y #≤2, X#=Y.

7. X#=Y.

8. true.

CS=�

CS={X≥2}

CS={X≥2, Y≤2}

CS={X=2, Y=2}

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 7 / 1

Tabled CLP Issues

Interaction between CLP and Tabling

When a call is considered a consumer of a previous generator?

Approach Generator Consumervariant t(1,X,Z) t(1,Y,Z)

subsumption t(1,X,Z) t(1,2,Z)

constraints t(1,X){X>2} t(1,X){X>3}

Previous approaches for tabling with constraints:I Call variant with constraints (syntactic checking):

Generator Consumert(1,X){X>2} t(1,X){X>3}

I Call abstraction (execution without constraints):

Generator Consumert(1,X){X>2} ; t(1,X) t(1,X){X>3} ; t(1,X)

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 8 / 1

Tabled CLP Issues

Interaction between CLP and Tabling II

Constraint solver should provide some common constraint operations:I Entailment checking: a set of constraints C1 is entailed by another set

of constraints C2 in the domain D if D |= C2 → C1.

Generator Consumert(1,X){X>2} t(1,X){X>3}

I Projection: the projection of constraint C onto variables V is aconstraint C ′ over variables V such that D |= ∃x .C ↔ C ′ wherex = vars(C )− V .

Generator Consumert(1,X){X>2,Y>3} ; t(1,X){X>2} t(1,X){X>2,Y<1} ; t(1,X){X>2}

Soundness and termination for “constraint-compact” domains:I Similar to “bounded term size” property.I Every infinite set of constraints contains a finite cover.

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 9 / 1

Implementation Break

Our Tabled CLP frameworkSeparation of concerns

t(1,X) {X>3}

Tabling ConstraintSolverengine

Global Table Constraint Store

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 10 / 1

Implementation Break

Our Tabled CLP frameworkSeparation of concerns

t(1,X) {X>3}

Tabling ConstraintSolverengine

Global Table Constraint Storet(1,X)

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 10 / 1

Implementation Break

Our Tabled CLP frameworkSeparation of concerns

t(1,X) {X>3}

Tabling ConstraintSolverengine

Global Table Constraint Storet(1,X) {X>3}

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 10 / 1

Implementation Break

Our Tabled CLP frameworkSeparation of concerns

t(2,X) {X<2}

Tabling ConstraintSolverengine

Global Table Constraint Storet(1,X) {X>3}

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 10 / 1

Implementation Break

Our Tabled CLP frameworkSeparation of concerns

t(2,X) {X<2}

Tabling ConstraintSolverengine

Global Table Constraint Storet(1,X) {X>3}t(2,X)

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 10 / 1

Implementation Break

Our Tabled CLP frameworkSeparation of concerns

t(2,X) {X<2}

Tabling ConstraintSolverengine

Global Table Constraint Storet(1,X) {X>3}t(2,X) {X<2}

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 10 / 1

Implementation Break

Our Tabled CLP frameworkSeparation of concerns

t(1,X) {X<1,Y<1}

Tabling ConstraintSolverengine

Global Table Constraint Storet(1,X) {X>3}t(2,X) {X<2}

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 10 / 1

Implementation Break

Our Tabled CLP frameworkSeparation of concerns

t(1,X) {X<1,Y<1} {X<1}Projection

Tabling ConstraintSolverengine

Global Table Constraint Storet(1,X) {X>3}t(2,X) {X<2}

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 10 / 1

Implementation Break

Our Tabled CLP frameworkSeparation of concerns

t(1,X) {X<1,Y<1} {X<1}Projection

NOEntailment

Tabling ConstraintSolverengine

Global Table Constraint Storet(1,X) {X>3}t(2,X) {X<2}

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 10 / 1

Implementation Break

Our Tabled CLP frameworkSeparation of concerns

t(1,X) {X<1,Y<1} {X<1}Projection

NOEntailment

Tabling ConstraintSolverengine

Global Table Constraint Storet(1,X) {X>3 ∨ X<1}t(2,X) {X<2}

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 10 / 1

Implementation Break

Our Tabled CLP frameworkSeparation of concerns

t(1,X) {X>5,Y<7}

Tabling ConstraintSolverengine

Global Table Constraint Storet(1,X) {X>3 ∨ X<1}t(2,X) {X<2}

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 10 / 1

Implementation Break

Our Tabled CLP frameworkSeparation of concerns

t(1,X) {X>5,Y<7} {X>5}Projection

Tabling ConstraintSolverengine

Global Table Constraint Storet(1,X) {X>3 ∨ X<1}t(2,X) {X<2}

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 10 / 1

Implementation Break

Our Tabled CLP frameworkSeparation of concerns

t(1,X) {X>5,Y<7} {X>5}Projection

YESEntailment

Tabling ConstraintSolverengine

Global Table Constraint Storet(1,X) {X>3 ∨ X<1}t(2,X) {X<2}

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 10 / 1

Applications of Tabled CLP

Tabled CLP applicationsDifference Constraints

X − Y ≤ d .

Can simulate: X ≤ Max and X ≥ Min, X ≤ Y .

Scheduling problems and temporal reasoning.

X Yd

Constraint solver based on shortest-path algorithms.I Efficient projection and entailment operations.

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 11 / 1

Applications of Tabled CLP

Tabled CLP applicationsBasic examples

Entailment vs Constraint call variant:

Example

:- table p/1.

p(X) :-

Y #=< X-1,

p(Y).

p(1).

?- X #=< 10, p(X).

X = 1;

2 #=< X,

X #=< 10;

no

1. p(X){X=<10}

2. Y #=< X−1, p(Y). CS={X=<10}

3. p(Y).

Projection

{Y=<9}

4. X = 1.

Y=1

CS={2=<X=<10}

CS={X=<10,Y=<X−1}

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 12 / 1

Applications of Tabled CLP

Entailment vs. Call Abstraction

TCHR: implementation of CHR on top of XSB Prolog with tabling.I It uses call abstraction.

Constraints reduce the search space.

Ciao TCLP TCHR

Reach 30 7 140 129 978

Reach 25 6 680 129 876

Reach 20 5 964 128 955

Reach 15 4 316 129 313

Reach 10 2 296 128 994

Reach 5 427 129 616

Reach 0 1 129 472

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 13 / 1

Applications of Tabled CLP

Tabled CLP applicationsTimed Automata I

Definition

Difference constraint to model time relationships:I Invariants and transitions.

Reset initializes a clock variable X to be X = 0.

States apply invariants and arbitrary delays to clock variables.

P processes.

Each process has a clockvariable Xi .

K indicates the nextprocess to enter Qme .

State representation:s([Q1, ...,Qn],K , [X1, ...,Xn]).

Q0 Q1 Q2

Qme

Xi <= 2

K = i, Reset XiReset Xi

K == 0 Xi <= 2

K == i

Xi >= 4K = 0

Xi >= 4, K != i

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 14 / 1

Applications of Tabled CLP

Tabled CLP applicationsTimed Automata II

Reachability code

reach(s(Si ,K,X),s(Si ,K,Xd)) :-

delay(X,Xd),

inv(Si ).

reach(s(Si ,Ki,X),s(Sj ,Kf,Yd)) :-

reach(s(Si ,Ki,X),s(Sk ,Kt,Z)),

processes(P),

each_trans(P,s(Sk ,Kt,Z),s(Sj ,Kf,Y)),

delay(Y,Yd),

inv(Sj ).

Q0 Q1 Q2

Qme

Xi <= 2

K = i, Reset XiReset Xi

K == 0 Xi <= 2

K == i

Xi >= 4K = 0

Xi >= 4, K != i

Automata definition

inv(s(q1 ,_,X)) :- X #=< 2.

trans(_,s(q0 ,0,_), s(q1 ,0,X)) :- X #= 0.

trans(P,s(q1 ,_,Xin), s(q2 ,P,Xout)) :- Xin #=< 2, Xout #= 0.

trans(_,s(q2 ,K,X), s(q0 ,K,X)) :- K =\= P, X #>= 4.

trans(P,s(q2 ,P,X), s(qme ,P,X)) :- X #>= 4.

trans(P,s(qme ,_,X), s(q0 ,P,X)).

processes (1)

...

processes(N).

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 15 / 1

Applications of Tabled CLP

Tabled CLP applicationsTimed Automata III

UPPAAL is a fast tool built specifically for TA verification:I Developed since 1999.

Ciao is a general-purpose, multi-paradigm language.

Ciao TCLP UPPAAL

Fisher 2 0 0

Fisher 3 12 1

Fisher 4 270 44

Fisher 5 10 576 4 514

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 16 / 1

Conclusions

Conclusions

Tabling and constraints, separately, makes LP richer.I Together, they are even more expressive.

Standard tabling uses syntactic checking ⇒ useless with constraints!

Tabled CLP framework independent from constraint solver:I Constraint solver provides:

F Projection.F Entailment.

I Validated with different constraint solvers.

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 17 / 1

Thanks!

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 18 / 1

Backup

Disequality Constraints

X = a, X = Y , X 6= a and X 6= Y (infinite constants).

X and Y MUST be different when they are bounded.

Constraint solver using attribute variables:I Checks disequalities after unification.I Equalities are managed by Prolog.I Projection:

{d | d ∈ C , vars(d) ⊆ V }I Entailment:

D 6= |= C1 → C2 iff C1 ⊇ C2

Example: reachibility analysis without visiting a node.

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 19 / 1

Backup

TCLP for Branch & Bound optimization

Optimization Code

:- table path /3.

p(X,Y,Cost) :-

edge(X,Y,Cost).

p(X,Y,C) :-

edge(X,Z,C1),

C2 #>= 0,

C #= C1 + C2 ,

path(Z,Y,C2).

minimize_path(X,Y,Best ,Min) :-

once(path(X,Y,Best)),

NewBest #< Best ,

(

minimize_path(X,Y,NewBest ,Min)

;

Min = Best

),

!.

?- minimize_path(X,Y,_,Min).

Pablo Chico de Guzman (IMDEA Institute) Tabled CLP Framework FLOPS’12 Kobe 20 / 1

Recommended