pierangelo dell’acqua dept. of science and technology linköping university [email protected]

27
1 Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University [email protected] Constraint programming 2001 November 13th 2001 http://www.ida.liu.se/labs/logpro/ulfni/cp2001/

Upload: inocencia-quesada

Post on 31-Dec-2015

27 views

Category:

Documents


2 download

DESCRIPTION

Constraint Handling Rules. Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University [email protected]. Constraint programming 2001 November 13th 2001 http://www.ida.liu.se/labs/logpro/ulfni/cp2001/. Overview. Motivation Language CHR - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

1

Pierangelo Dell’Acqua

Dept. of Science and Technology

Linköping University

[email protected]

Constraint programming 2001

November 13th 2001

http://www.ida.liu.se/labs/logpro/ulfni/cp2001/

Page 2: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

2

Overview

1 Motivation2 Language CHR3 Declarative and operational semantics4 Properties5 Examples of CHR constraint solvers

Page 3: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

3

Based on

Theory and Practice of Constraint Handling RulesThom Frühwirth, J. Logic Programming 1994:19, 20:1-679

Examples CHR constraint solvers available at: www.informatik.uni-muenchen.de/~fruehwir/chr/

Page 4: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

4

Black-box vs Glass-box solvers

In most systems constraint solving is hard-wired in a built-in constraint solver in a low-level language: black-box approach

efficiency

non-extensible, unpredictable, hard to debug

Some systems facilitate defining new constraints and solvers: glass-box approach

improved control of propagation and search

examples CHR, HAL, ...

Page 5: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

5

Constraint Handling Rules (CHR)

Declarative programming language for the specification and implementation of constraint solvers and programs

Black-box

Host language (Prolog, Lisp, … )

CHR-solver

Application

CHR-constraints

built-in constraints

CHR-constraint solvers are open and flexible, can be maintained, debugged and analysed

Page 6: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

6

CHR by example

The partial order relation X Y as a user-defined constraint:

Computation:A=<B C=<A B=<C

C=<A A=<B propagates C=<B by transitivity

C=<B B=<C simplifies to B=C by antisymmetry

A=<B C=<A simplifies to A=B by antisymmetry since B=C

A=B B=C

X=<Y <=> X=Y | true. reflexivity

X=<Y Y=<X <=> X=Y. antisymmetry

X=<Y Y=<Z ==> X=<Z. transitivity

Page 7: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

7

CHR syntax

A CHR-program is a finite set of CHR-rules. There are three kinds of CHR-rules:

Head H conjunction of CHR-constraintsGuard G conjunction of built-in constraintsBody B conjunction of built-in and CHR-constraints

Simplification H <=> G | B

Propagation H = => G | B

Simpagation H1 \ H2 <=> G | B

Page 8: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

8

Declarative semantics

Simplification rule

H <=> G | B h (g (G) ( H b ( B ) ) )

Propagation rule

H = => G | B h (g (G) ( H b ( B ) ) )

Simpagation rule

H1 \ H2 <=> G | B h1h2 (g (G) (H1H2 b (H1B)))

Page 9: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

9

Declarative semantics (2)

Declarative semantics of a CHR-program P:

Sem(P) = LP, CT

where LP is the logical reading of the CHR-rules in P and CT is a theory for built-in constraints

Page 10: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

10

Operational semantics

A state is a tuple F,E,D where:

F is a conjunction of CHR- and built-in constraints (goal store)

E is a conjunction of CHR-constraints (CHR-store)

D is a conjunction of built-in constraints (built-in constraints store)

Page 11: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

11

CHR transitions

Solve

CF, E, D F, E, D2if C is a built-in constraint and CT |= (CD) D2

Introduce

HF, E, D F, HE, Dif H is a CHR-constraint

Simplify

F, H2E, D BF, E, H=H2Dif (H <=> G | B) in P and CT |= D h (H=H2 G)

Page 12: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

12

CHR transitions (2)

Propagate

F, H2E, D BF, H2E, H=H2Dif (H = => G | B) in P and CT |= D h (H=H2 G)

Page 13: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

13

Initial and final states

An initial state consists of a goal G and empty constraint stores:

G,true,true

A final state is either of the form: (i) F,E,false failed final state

or of the form: (ii) true,E,D successful final state where no transition is applicable and D false

Page 14: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

14

CHR computations

A computation of a goal G is a sequence S0, S1, … of states with

Si Si+1 beginning with the initial state S0 = G,true,true and

ending with a final state or diverging

The logical meaning of a state F,E,D, which occurs in a computation for G, is x (FED), where x are the variables in F,E,D but not in G

An answer of a goal G is the final state of a computation for G

Page 15: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

15

Example CHR calculus

X=<Y <=> X=Y | true. reflexivity

X=<Y Y=<X <=> X=Y. antisymmetry

X=<Y Y=<Z ==> X=<Z. transitivity

A=<B C=<A B=<C, true, trueIntroduce 3 true, A=<B C=<A B=<C, truePropagate C=<B, A=<B C=<A B=<C, trueIntroduce true, A=<B C=<A B=<C C=<B, trueSimplify B=C, A=<B C=<A, trueSolve true, A=<B C=<A, B=C Simplify A=B, true, B=C Solve true, true, A=B B=C

Page 16: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

16

Logical equivalence of states

CHR transitions preserve the logical meaning of states:

Lemma Let P be a CHR program and G a goal. If C is the logical meaning of a state in a computation of G, then

LP , CT |= ( G C)

There is no distinction between successful and failed computations

Page 17: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

17

Correspondence between semantics

Theorem (Soundness) Let P be a CHR program and G a goal. If G has a computation with answer C, then

LP, CT |= ( C G)

Theorem (Completeness) Let P be a CHR program and G a goal with at least one finite computation. Let C be a conjunction of constraints. If LP, CT |= (GC), then G has a computation with answer C2 such that

LP, CT |= ( C C2)

Page 18: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

18

Example: completeness

The completeness theorem does not hold if G has no finite computations:

Let P be { p <=> p } and G the goal p.

Since LP is {pp}, it holds that LP,CT |= pp, but G has only an infinite computation

Page 19: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

19

Example: failed computations

The completeness theorem is weak for failed computations:

Let P be:

We have that LP, CT |= q, but q has no failed computation. It has a successful derivation with answer q.

{ p <=> q,

p <=> false }

Page 20: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

20

Confluence

Confluence: The answer of a goal G is always the same, no matter which of the applicable rules are applied

{ p <=> q,

p <=> false }

is not confluent

{ p <=> q,

p <=> false,

q <=> false}

is confluent

Page 21: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

21

Soundness and Completeness revisited

Theorem (Strong Soundness and Completeness)

Let P be a terminating and confluent CHR program, G a goal and C a conjunction of constraints.

Then the following are equivalent:

• LP, CT |= (CG)

• G has a computation with answer C2 such that:

LP, CT |= (CC2)

• Every computation of G has an answer C2 such that:

LP, CT |= (CC2)

Page 22: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

22

CLP + CHR

Any CLP language can be extended with CHR

- Allow clauses for CHR constraints: introduce choices

- Regard a predicate as a constraint and add CHR rules for it

Idea:

Don’t know and don’t care nondeterminism combined in a declarative way

Page 23: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

23

CLP+CHR language

A CLP+CHR program is a finite set of :

(i) CLP clauses for predicates and CHR constraints, and

(ii) CHR rules for CHR constraints.

A CLP clause is of the form:

H :- B1,…,Bk (k 0)

an atom or a CHR constraintnot a built-in constraint

conjunction of atoms, CHR constraints and built-in constraints

Page 24: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

24

CLP+CHR language (2)

The logical meaning of a CLP clause is given by Clark’s completion

Backward compatibility

Labelling declarations (see def. 6.1 of JLP paper) are dropped,easily simulated

H :- B

label-with H if G

lw, H <=> G | H2, lw

H2 :- B

CHR constraint new predicatenew CHR constraint

Page 25: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

25

CLP+CHR transitions

Unfold (revisited)

H2F, E, D BF, E, H=H2Dif (H:-B) in P, H2 is a predicate and CT |= Dh (H=H2)

Label (revisited)

F, H2E, D BF, E, H=H2Dif (H:-B) in P, H2 is a CHR constraint and CT |= Dh (H=H2)

Page 26: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

26

Examples of CHR solvers

Several constraint solvers have been written in CHR, including new constraint domains such as terminological and temporal reasoning

bool.pl boolean constraints

arc.pl arc-consistency over finite domains

interval.pl interval domains over integers and reals

list.pl equality constraints over concatenation of lists

Page 27: Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University pier@itn.liu.se

27

Sicstus Prolog + CHR

A CHR rule in SicstusProlog+CHR is of the form:

H <=> G | B H = => G | B H1 \ H2 <=> G | B

where:

H is a conjunction of CHR-constraintsG is a conjunction of atoms and built-in constraintsB is a conjunction of atoms, built-in and CHR-constraints

A CHR rule can be fired if its guard G is true

Note that during the proof of the guard G no new binding can be generated for variables that occur also in H