Constraint Handling Rules - A Tutorial for (Prolog) - DTAI - KU Leuven

3 downloads 144 Views 6MB Size Report
Paint-Mixing Engine: ▻ Prolog (SWI-Prolog, . . . ) ▻ Leuven CHR system. Everything in a file: paint.pl. :- use_modul
Constraint Handling Rules A Tutorial for (Prolog) Programmers

Tom Schrijvers Katholieke Universiteit Leuven, Belgium [email protected]

ICLP 2008 – December 9-13, 2008

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

1 / 112

Overview

2/112 1 2 3 4 5 6 7 8 9 10 11 12 13

Tom Schrijvers (K.U.Leuven)

Introduction The Art of CHR Simpagation For the Sake of Arguments En Guarde A Perfect Match Bigger Programs Propagation Order in the Rules Reactivation CHR vs. Prolog Tutorial Summary Facts about CHR

Constraint Handling Rules

2 / 112

Overview

3/112 1 2 3 4 5 6 7 8 9 10 11 12 13

Tom Schrijvers (K.U.Leuven)

Introduction The Art of CHR Simpagation For the Sake of Arguments En Guarde A Perfect Match Bigger Programs Propagation Order in the Rules Reactivation CHR vs. Prolog Tutorial Summary Facts about CHR

Constraint Handling Rules

3 / 112

About This Tutorial

Expected background: I know & like programming

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

4 / 112

About This Tutorial

Expected background: I know & like programming I know Prolog, . . .

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

4 / 112

About This Tutorial

Expected background: I know & like programming I know Prolog, . . . I but have an itch that Prolog doesn’t scratch

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

4 / 112

About This Tutorial

Expected background: I know & like programming I know Prolog, . . . I but have an itch that Prolog doesn’t scratch I . . . or you will have at the end of this tutorial

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

4 / 112

About This Tutorial

Expected mindset: I forget about Prolog for now

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

5 / 112

About This Tutorial

Expected mindset: I forget about Prolog for now I do not assume things to work as in Prolog

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

5 / 112

About This Tutorial

Expected mindset: I forget about Prolog for now I do not assume things to work as in Prolog I really, they won’t be the same!!!

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

5 / 112

About This Tutorial

Expected mindset: I forget about Prolog for now I do not assume things to work as in Prolog I really, they won’t be the same!!! You’ve been warned!

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

5 / 112

About This Tutorial

Expected mindset: I forget about Prolog for now I do not assume things to work as in Prolog I really, they won’t be the same!!! You’ve been warned! Also: I do not expect anything from the word “constraint”

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

5 / 112

About This Tutorial

Expected mindset: I forget about Prolog for now I do not assume things to work as in Prolog I really, they won’t be the same!!! You’ve been warned! Also: I do not expect anything from the word “constraint” I CHR constraints aren’t

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

5 / 112

About this tutorial

What you’ll learn:

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

6 / 112

About this tutorial

What you’ll learn: 1 how CHR works (operationally)

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

6 / 112

About this tutorial

What you’ll learn: 1 how CHR works (operationally) 2 how to program in CHR

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

6 / 112

About this tutorial

What you’ll learn: 1 how CHR works (operationally) 2 how to program in CHR 3 just maybe, what CHR is good for

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

6 / 112

Overview

7/112 1 2 3 4 5 6 7 8 9 10 11 12 13

Tom Schrijvers (K.U.Leuven)

Introduction The Art of CHR Simpagation For the Sake of Arguments En Guarde A Perfect Match Bigger Programs Propagation Order in the Rules Reactivation CHR vs. Prolog Tutorial Summary Facts about CHR

Constraint Handling Rules

7 / 112

Art 101: Mixing Paint

As novice, you have to start at the bottom of the ladder:

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

8 / 112

Art 101: Mixing Paint

As novice, you have to start at the bottom of the ladder: I Learning how to obtain different colors

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

8 / 112

Art 101: Mixing Paint

As novice, you have to start at the bottom of the ladder: I Learning how to obtain different colors I by mixing paint

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

8 / 112

Art 101: Mixing Paint

As novice, you have to start at the bottom of the ladder: I Learning how to obtain different colors I by mixing paint I Made easy by the Paint Mixing Rules (PMR) language

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

8 / 112

The Paint

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

9 / 112

Fewer Paint

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

10 / 112

The Rules

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

11 / 112

The Rules

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

11 / 112

The Rules

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

11 / 112

PMR as CHR Under the hood, PMR is of course CHR: PMR

CHR

paints

constraints

rules

simplification rules (textual)

mixing bucket

constraint store

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

12 / 112

Paint as Constraints

Declaring our paint colors, in textual form: :- chr_constraint red.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

%

13 / 112

Paint as Constraints

Declaring our paint colors, in textual form: :- chr_constraint red. :- chr_constraint blue.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

% %

13 / 112

Paint as Constraints

Declaring our paint colors, in textual form: :- chr_constraint :- chr_constraint :- chr_constraint :- chr_constraint ...

Tom Schrijvers (K.U.Leuven)

red. blue. yellow. orange.

Constraint Handling Rules

% % % %

13 / 112

Paint as Constraints?

Are paint colors really “constraints”?

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

14 / 112

Paint as Constraints?

Are paint colors really “constraints”? No: not in the Constraint Programming sense

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

14 / 112

Paint as Constraints?

Are paint colors really “constraints”? No: not in the Constraint Programming sense Yes: in the CHR sense I 1st-class entities in CHR I rewritten by rules

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

14 / 112

Paint as Constraints?

Are paint colors really “constraints”? No: not in the Constraint Programming sense Yes: in the CHR sense I 1st-class entities in CHR I rewritten by rules (Are Prolog predicates really “predicates”?)

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

14 / 112

The Paint Rules in CHR

Simplification Rule: head body .

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

15 / 112

The Paint Rules in CHR

Simplification Rule: head body . “head may be replaced with body ”

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

15 / 112

The Paint Rules in CHR

Simplification Rule: head body . “head may be replaced with body ” % red, blue

Tom Schrijvers (K.U.Leuven)

purple.

Constraint Handling Rules

15 / 112

The Paint Rules in CHR

Simplification Rule: head body . “head may be replaced with body ” % red, blue

purple.

% red, yellow

orange.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

15 / 112

The Paint Rules in CHR

Simplification Rule: head body . “head may be replaced with body ” % red, blue

purple.

% red, yellow

orange.

% blue, yellow green. Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

15 / 112

Time For. . .

. . . a cocktail break! Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

16 / 112

Cocktail Break

You can have as many heads as you like: vodka, martini vodka_martini.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

17 / 112

Cocktail Break

You can have as many heads as you like: vodka, martini vodka_martini. tequila, cointreau, lime margarita.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

17 / 112

Cocktail Break

You can have as many heads as you like: vodka, martini vodka_martini. tequila, cointreau, lime margarita. gin, cherry_brandy, cointreau, grenadine, pineapple, lemon, angostura_bitters singapore_sling. but at least one! Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

17 / 112

Cocktail Break

Warning: too many heads may cause a headache!

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

18 / 112

Cocktail Break

Warning: too many heads may cause a headache! vodka_martini, margarita, singapore_sling hangover, blackout.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

18 / 112

Cocktail Break

Warning: too many heads may cause a headache! vodka_martini, margarita, singapore_sling hangover, blackout. You can have many constraints in the body too.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

18 / 112

Running the Rules Paint-Mixing Engine: I Prolog (SWI-Prolog, . . . ) I Leuven CHR system

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

19 / 112

Running the Rules Paint-Mixing Engine: I Prolog (SWI-Prolog, . . . ) I Leuven CHR system Everything in a file: paint.pl :- use_module(library(chr)). :- chr_constraint red. ... red, blue purple. ...

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

19 / 112

Running the Rules Paint-Mixing Engine: I Prolog (SWI-Prolog, . . . ) I Leuven CHR system Everything in a file: paint.pl :- use_module(library(chr)). :- chr_constraint red. ... ?- [paint]. red, blue purple. ...

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

19 / 112

Running the Rules

red, blue purple. red, yellow orange. blue, yellow green. ?- red, blue.

I I

queries answers

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

20 / 112

Running the Rules

red, blue purple. red, yellow orange. blue, yellow green. ?- red, blue. purple I I

queries answers

Tom Schrijvers (K.U.Leuven)

?- red, yellow.

Constraint Handling Rules

20 / 112

Running the Rules

red, blue purple. red, yellow orange. blue, yellow green. ?- red, blue. purple I I

queries answers

?- red, yellow. orange ?- blue, yellow.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

20 / 112

Running the Rules

red, blue purple. red, yellow orange. blue, yellow green. ?- red, blue. purple I I

queries answers

?- red, yellow. orange ?- blue, yellow. green

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

20 / 112

In Slow Motion red, blue purple. red, yellow orange. blue, yellow green.

?-

,

. I

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

query: left-to-right

21 / 112

In Slow Motion red, blue purple. red, yellow orange. blue, yellow green.

?-

Tom Schrijvers (K.U.Leuven)

,

.

Constraint Handling Rules

I

query: left-to-right

I

active constraint: blue

22 / 112

In Slow Motion red, blue purple. red, yellow orange. blue, yellow green.

?-

Tom Schrijvers (K.U.Leuven)

,

.

Constraint Handling Rules

I

query: left-to-right

I

active constraint: blue

I

put in constraint store

23 / 112

In Slow Motion red, blue purple. red, yellow orange. blue, yellow green.

?-

Tom Schrijvers (K.U.Leuven)

,

I

active constraint: blue

I

fire first rule?

I

no partner red in constraint store

.

Constraint Handling Rules

24 / 112

In Slow Motion red, blue purple. red, yellow orange. blue, yellow green.

?-

Tom Schrijvers (K.U.Leuven)

,

.

Constraint Handling Rules

I

active constraint: blue

I

fire second rule?

I

no blue in head

25 / 112

In Slow Motion red, blue purple. red, yellow orange. blue, yellow green.

?-

Tom Schrijvers (K.U.Leuven)

,

I

active constraint: blue

I

fire third rule?

I

no partner yellow in constraint store

.

Constraint Handling Rules

26 / 112

In Slow Motion red, blue purple. red, yellow orange. blue, yellow green.

?-

,

. I

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

deactivate constraint blue

27 / 112

In Slow Motion red, blue purple. red, yellow orange. blue, yellow green.

?-

Tom Schrijvers (K.U.Leuven)

,

.

Constraint Handling Rules

I

active constraint: yellow

28 / 112

In Slow Motion red, blue purple. red, yellow orange. blue, yellow green.

?-

Tom Schrijvers (K.U.Leuven)

,

.

Constraint Handling Rules

I

active constraint: yellow

I

put in constraint store

29 / 112

In Slow Motion red, blue purple. red, yellow orange. blue, yellow green.

?-

Tom Schrijvers (K.U.Leuven)

,

.

Constraint Handling Rules

I

active constraint: yellow

I

fire first rule?

I

no yellow in head

30 / 112

In Slow Motion red, blue purple. red, yellow orange. blue, yellow green.

?-

Tom Schrijvers (K.U.Leuven)

,

I

active constraint: yellow

I

fire second rule?

I

no partner red in constraint store

.

Constraint Handling Rules

31 / 112

In Slow Motion red, blue purple. red, yellow orange. blue, yellow green.

?-

Tom Schrijvers (K.U.Leuven)

,

.

Constraint Handling Rules

I

active constraint: yellow

I

fire third rule?

32 / 112

In Slow Motion red, blue purple. red, yellow orange. blue, yellow green.

?-

Tom Schrijvers (K.U.Leuven)

,

I

active constraint: yellow

I

fire third rule!

I

with partner blue in constraint store

.

Constraint Handling Rules

33 / 112

In Slow Motion red, blue purple. red, yellow orange. blue, yellow green.

?-

Tom Schrijvers (K.U.Leuven)

,

.

Constraint Handling Rules

I

fire third rule!

I

delete the matched head constraints

34 / 112

In Slow Motion red, blue purple. red, yellow orange. blue, yellow green.

?-

Tom Schrijvers (K.U.Leuven)

,

.

Constraint Handling Rules

I

fire third rule!

I

add the body to (front of) query

35 / 112

In Slow Motion red, blue purple. red, yellow orange. blue, yellow green.

?-

Tom Schrijvers (K.U.Leuven)

,

.

Constraint Handling Rules

I

active constraint: green

36 / 112

In Slow Motion red, blue purple. red, yellow orange. blue, yellow green.

?-

Tom Schrijvers (K.U.Leuven)

,

.

Constraint Handling Rules

to make a long story short: I

...

I

final constraint store

37 / 112

What Happens?

red, blue purple. red, yellow orange. blue, yellow green. ?- red, yellow. orange

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

38 / 112

What Happens?

red, blue purple. red, yellow orange. blue, yellow green. ?- red, yellow. orange ?- yellow, red. orange

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

38 / 112

What Happens?

red, blue purple. red, yellow orange. blue, yellow green. ?- red.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

39 / 112

What Happens?

red, blue purple. red, yellow orange. blue, yellow green. ?- red. red

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

39 / 112

What Happens?

red, blue purple. red, yellow orange. blue, yellow green. ?- red, yellow, blue.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

40 / 112

What Happens?

red, blue purple. red, yellow orange. blue, yellow green. ?- red, yellow, blue. orange blue

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

40 / 112

What Happens?

red, blue purple. red, yellow orange. blue, yellow green. ?- red, blue, yellow.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

41 / 112

What Happens?

red, blue purple. red, yellow orange. blue, yellow green. ?- red, blue, yellow. purple yellow

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

41 / 112

Summary

Simplification Rule: I head body . I replace head with body

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

42 / 112

Summary

Simplification Rule: I head body . I replace head with body Queries: I processed left-to-right I incrementally I active constraint looks for partners

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

42 / 112

Overview

43/112 1 2 3 4 5 6 7 8 9 10 11 12 13

Tom Schrijvers (K.U.Leuven)

Introduction The Art of CHR Simpagation For the Sake of Arguments En Guarde A Perfect Match Bigger Programs Propagation Order in the Rules Reactivation CHR vs. Prolog Tutorial Summary Facts about CHR

Constraint Handling Rules

43 / 112

Brown

Brown stays brown. brown, orange brown. brown, green brown. brown, purple brown. ...

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

44 / 112

Short Hand Simpagation rule: headk \head r body . brown \ orange true. brown \ green true. brown \ purple true. ...

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

45 / 112

Short Hand Simpagation rule: headk \head r body . brown \ orange true. brown \ green true. brown \ purple true. ... I

headk : kept head

Tom Schrijvers (K.U.Leuven)

(1 or more)

Constraint Handling Rules

45 / 112

Short Hand Simpagation rule: headk \head r body . brown \ orange true. brown \ green true. brown \ purple true. ... I I

headk : kept head headr : removed head

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

(1 or more) (1 or more)

45 / 112

Short Hand Simpagation rule: headk \head r body . brown \ orange true. brown \ green true. brown \ purple true. ... I I I

headk : kept head headr : removed head true : Prolog’s no-op

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

(1 or more) (1 or more)

45 / 112

Lead to Gold philosophers_stone \ lead gold. ?- philosophers_stone, lead.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

46 / 112

Lead to Gold philosophers_stone \ lead gold. ?- philosophers_stone, lead. philosophers_stone gold

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

46 / 112

Lead to Gold philosophers_stone \ lead gold. ?- philosophers_stone, lead. philosophers_stone gold ?- philosophers_stone, lead, lead.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

46 / 112

Lead to Gold philosophers_stone \ lead gold. ?- philosophers_stone, lead. philosophers_stone gold ?- philosophers_stone, lead, lead. philosophers_stone gold gold Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

46 / 112

All Combinations philosophers_stone \ lead gold. ?- lead, lead.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

47 / 112

All Combinations philosophers_stone \ lead gold. ?- lead, lead. lead lead

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

47 / 112

All Combinations philosophers_stone \ lead gold. ?- lead, lead. lead lead ?- lead, lead, philosophers_stone.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

47 / 112

All Combinations philosophers_stone \ lead gold. ?- lead, lead. lead lead ?- lead, lead, philosophers_stone. philosophers_stone gold gold Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

47 / 112

All Combinations philosophers_stone \ lead gold. ?- lead, lead. lead lead ?- lead, lead, philosophers_stone. philosophers_stone gold gold Rule fires with all combinations! Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

47 / 112

Summary

Simplification Rule: I head body . I replace head with body

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

48 / 112

Summary

Simplification Rule: I head body . I replace head with body Simpagation Rule: I headk \headr body . I replace headr with body I in the presence of headk I fires all possible combinations

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

48 / 112

Overview

49/112 1 2 3 4 5 6 7 8 9 10 11 12 13

Tom Schrijvers (K.U.Leuven)

Introduction The Art of CHR Simpagation For the Sake of Arguments En Guarde A Perfect Match Bigger Programs Propagation Order in the Rules Reactivation CHR vs. Prolog Tutorial Summary Facts about CHR

Constraint Handling Rules

49 / 112

Piggy Bank Merger

:- chr_constraint piggy/1. piggy(I), piggy(J) K is I + J, piggy(K). I

constraints can have arguments

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

50 / 112

Piggy Bank Merger

:- chr_constraint piggy/1. piggy(I), piggy(J) K is I + J, piggy(K). I I

constraints can have arguments arity must be declared

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

50 / 112

Piggy Bank Merger

:- chr_constraint piggy/1. piggy(I), piggy(J) K is I + J, piggy(K). I I I

constraints can have arguments arity must be declared Prolog terms as arguments

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

50 / 112

Piggy Bank Merger :- chr_constraint piggy/1. piggy(I), piggy(J) K is I + J, piggy(K).

?- piggy(5),piggy(1),piggy(4),piggy(2).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

51 / 112

Piggy Bank Merger :- chr_constraint piggy/1. piggy(I), piggy(J) K is I + J, piggy(K).

?-

Tom Schrijvers (K.U.Leuven)

5€

,

1€

,

4€

Constraint Handling Rules

,

2€

.

51 / 112

Piggy Bank Merger :- chr_constraint piggy/1. piggy(I), piggy(J) K is I + J, piggy(K).

?-

,

1€

,

4€

,

2€

.

5€

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

51 / 112

Piggy Bank Merger :- chr_constraint piggy/1. piggy(I), piggy(J) K is I + J, piggy(K).

?-

, 5€

Tom Schrijvers (K.U.Leuven)

,

4€

,

2€

.

1€

Constraint Handling Rules

51 / 112

Piggy Bank Merger :- chr_constraint piggy/1. piggy(I), piggy(J) K is I + J, piggy(K).

?-

,

,

4€

,

2€

.

6€

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

51 / 112

Piggy Bank Merger :- chr_constraint piggy/1. piggy(I), piggy(J) K is I + J, piggy(K).

?-

, 4€

Tom Schrijvers (K.U.Leuven)

,

,

2€

.

6€

Constraint Handling Rules

51 / 112

Piggy Bank Merger :- chr_constraint piggy/1. piggy(I), piggy(J) K is I + J, piggy(K).

?-

,

,

,

2€

.

10€

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

51 / 112

Piggy Bank Merger :- chr_constraint piggy/1. piggy(I), piggy(J) K is I + J, piggy(K).

?-

,

, 10€

Tom Schrijvers (K.U.Leuven)

,

6€

.

2€

Constraint Handling Rules

51 / 112

Piggy Bank Merger :- chr_constraint piggy/1. piggy(I), piggy(J) K is I + J, piggy(K).

?-

,

,

,

6€

.

12€

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

51 / 112

CHR(X )

CHR is an embedded language I embedded in host language X

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

52 / 112

CHR(X )

CHR is an embedded language I embedded in host language X I here X = Prolog

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

52 / 112

CHR(X )

CHR is an embedded language I embedded in host language X I here X = Prolog 2-way communication:

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

52 / 112

CHR(X )

CHR is an embedded language I embedded in host language X I here X = Prolog 2-way communication: I Prolog calls CHR constraints e.g. all along from toplevel

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

52 / 112

CHR(X )

CHR is an embedded language I embedded in host language X I here X = Prolog 2-way communication: I Prolog calls CHR constraints e.g. all along from toplevel I CHR calls Prolog e.g. K is I + J

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

52 / 112

Not Just Numbers

:- chr_constraint value/1. value(I), value(J) append(I,J,K), value(K).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

53 / 112

Not Just Numbers

:- chr_constraint value/1. value(I), value(J) append(I,J,K), value(K).

?- value([a]), value([b]).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

53 / 112

Not Just Numbers

:- chr_constraint value/1. value(I), value(J) append(I,J,K), value(K).

?- value([a]), value([b]). value([b,a]).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

53 / 112

Not Just Numbers

:- chr_constraint value/1. value(I), value(J) append(I,J,K), value(K).

?- value([a]), value([b]). value([b,a]). ?- value([f(a),g(b)]), value([h(i,j)]).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

53 / 112

Not Just Numbers

:- chr_constraint value/1. value(I), value(J) append(I,J,K), value(K).

?- value([a]), value([b]). value([b,a]). ?- value([f(a),g(b)]), value([h(i,j)]). value([h(i,j),f(a),g(b)]).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

53 / 112

Not Just Numbers

:- chr_constraint value/1. value(I), value(J) append(I,J,K), value(K).

?- value([a]), value([b]). value([b,a]). ?- value([f(a),g(b)]), value([h(i,j)]). value([h(i,j),f(a),g(b)]). Any Prolog terms!

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

53 / 112

Summary

CHR constraints I zero or more arguments I Prolog terms

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

54 / 112

Summary

CHR constraints I zero or more arguments I Prolog terms Bodies I CHR constraints and I Prolog predicate calls

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

54 / 112

Overview

55/112 1 2 3 4 5 6 7 8 9 10 11 12 13

Tom Schrijvers (K.U.Leuven)

Introduction The Art of CHR Simpagation For the Sake of Arguments En Guarde A Perfect Match Bigger Programs Propagation Order in the Rules Reactivation CHR vs. Prolog Tutorial Summary Facts about CHR

Constraint Handling Rules

55 / 112

Count Down

Problem: write a program to enumerate values: ?- generate(5).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

56 / 112

Count Down

Problem: write a program to enumerate values: ?- generate(5). value(5) value(4) value(3) value(2) value(1)

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

56 / 112

Count Down

Solution: :- chr_constraint generate/1.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

57 / 112

Count Down

Solution: :- chr_constraint generate/1. :- chr_constraint value/1.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

57 / 112

Count Down

Solution: :- chr_constraint generate/1. :- chr_constraint value/1. generate(N) value(N), M is N - 1, generate(M).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

57 / 112

Running the Program

:- chr_constraint generate/1. :- chr_constraint value/1. generate(N) value(N), M is N - 1, generate(M).

?- generate(5).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

58 / 112

Running the Program

:- chr_constraint generate/1. :- chr_constraint value/1. generate(N) value(N), M is N - 1, generate(M).

?- generate(5). % waiting

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

58 / 112

Running the Program

:- chr_constraint generate/1. :- chr_constraint value/1. generate(N) value(N), M is N - 1, generate(M).

?- generate(5). % waiting % still waiting

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

58 / 112

Running the Program

:- chr_constraint generate/1. :- chr_constraint value/1. generate(N) value(N), M is N - 1, generate(M).

?- generate(5). % waiting % still waiting ERROR: Out of local stack

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

58 / 112

Running the Program

:- chr_constraint generate/1. :- chr_constraint value/1. generate(N) value(N), M is N - 1, generate(M).

?- generate(5). % waiting % still waiting ERROR: Out of local stack Recursion without a base case!

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

58 / 112

Guarded Rules :- chr_constraint generate/1. :- chr_constraint value/1. generate(N) N == 0 | true. generate(N) N > 0 | value(N), M is N - 1, generate(M).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

59 / 112

Guarded Rules :- chr_constraint generate/1. :- chr_constraint value/1. generate(N) N == 0 | true. generate(N) N > 0 | value(N), M is N - 1, generate(M).

Guarded rule: I apply rule if guard succeeds

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

59 / 112

Guarded Rules :- chr_constraint generate/1. :- chr_constraint value/1. generate(N) N == 0 | true. generate(N) N > 0 | value(N), M is N - 1, generate(M).

Guarded rule: I apply rule if guard succeeds I guard is optional

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

59 / 112

Guarded Rules :- chr_constraint generate/1. :- chr_constraint value/1. generate(N) N == 0 | true. generate(N) N > 0 | value(N), M is N - 1, generate(M).

Guarded rule: I apply rule if guard succeeds I guard is optional I guard may contain any Prolog, but may not bind any variables from the head (pure check) Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

59 / 112

Even Shorter

generate(N) N > 0 | value(N), M is N - 1, generate(M).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

60 / 112

Even Shorter

generate(N) N > 0 | value(N), M is N - 1, generate(M).

?- generate(5).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

60 / 112

Even Shorter

generate(N) N > 0 | value(N), M is N - 1, generate(M).

?- generate(5). value(5) value(4) value(3) value(2) value(1) generate(0)

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

60 / 112

Summary Simplification Rule: I head guard | body . I replace head with body I if guard succeeds

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

61 / 112

Summary Simplification Rule: I head guard | body . I replace head with body I if guard succeeds Simpagation Rule: I headk \headr guard | body . I replace headr with body I in the presence of headk I if guard succeeds Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

61 / 112

Overview

62/112 1 2 3 4 5 6 7 8 9 10 11 12 13

Tom Schrijvers (K.U.Leuven)

Introduction The Art of CHR Simpagation For the Sake of Arguments En Guarde A Perfect Match Bigger Programs Propagation Order in the Rules Reactivation CHR vs. Prolog Tutorial Summary Facts about CHR

Constraint Handling Rules

62 / 112

CHR Style: Matching

:- chr_constraint generate/1. :- chr_constraint value/1. generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M).

Matching: I “inline” notation for guard I same meaning as explicit guard

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

63 / 112

Meaning of Matching Maching

Guard

c(X) true.

c(X) true | true.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

64 / 112

Meaning of Matching Maching

Guard

c(X) true.

c(X) true | true.

c(a) true.

c(X) X == a | true.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

64 / 112

Meaning of Matching Maching

Guard

c(X) true.

c(X) true | true.

c(a) true.

c(X) X == a | true.

c(f(A)) true.

c(X) nonvar(X), X = f(A) | true.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

64 / 112

Meaning of Matching Maching

Guard

c(X) true.

c(X) true | true.

c(a) true.

c(X) X == a | true.

c(f(A)) true.

c(X) nonvar(X), X = f(A) | true.

c(X,X) true.

c(X,Y) X == Y | true.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

64 / 112

Meaning of Matching Maching

Guard

c(X) true.

c(X) true | true.

c(a) true.

c(X) X == a | true.

c(f(A)) true.

c(X) nonvar(X), X = f(A) | true.

c(X,X) true.

c(X,Y) X == Y | true.

c(X), d(X) true.

c(X), d(Y) X == Y | true.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

64 / 112

Matching: CHR vs. Prolog p(world) :- writeln(’Hello, World!’). :- chr_constraint c/1. c(world) writeln(’Hello, World!’).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

65 / 112

Matching: CHR vs. Prolog p(world) :- writeln(’Hello, World!’). :- chr_constraint c/1. c(world) writeln(’Hello, World!’). ?- p(world).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

65 / 112

Matching: CHR vs. Prolog p(world) :- writeln(’Hello, World!’). :- chr_constraint c/1. c(world) writeln(’Hello, World!’). ?- p(world). Hello, World!

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

65 / 112

Matching: CHR vs. Prolog p(world) :- writeln(’Hello, World!’). :- chr_constraint c/1. c(world) writeln(’Hello, World!’). ?- p(world). Hello, World! ?- p(Free).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

65 / 112

Matching: CHR vs. Prolog p(world) :- writeln(’Hello, World!’). :- chr_constraint c/1. c(world) writeln(’Hello, World!’). ?- p(world). Hello, World! ?- p(Free). Hello, World! Free = world

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

65 / 112

Matching: CHR vs. Prolog p(world) :- writeln(’Hello, World!’). :- chr_constraint c/1. c(world) writeln(’Hello, World!’). ?- p(world). Hello, World! ?- p(Free). Hello, World! Free = world ?- p(hello).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

65 / 112

Matching: CHR vs. Prolog p(world) :- writeln(’Hello, World!’). :- chr_constraint c/1. c(world) writeln(’Hello, World!’). ?- p(world). Hello, World! ?- p(Free). Hello, World! Free = world ?- p(hello). No Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

65 / 112

Matching: CHR vs. Prolog p(world) :- writeln(’Hello, World!’). :- chr_constraint c/1. c(world) writeln(’Hello, World!’). ?- p(world). Hello, World!

?- c(world).

?- p(Free). Hello, World! Free = world ?- p(hello). No Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

65 / 112

Matching: CHR vs. Prolog p(world) :- writeln(’Hello, World!’). :- chr_constraint c/1. c(world) writeln(’Hello, World!’). ?- p(world). Hello, World!

?- c(world). Hello, World!

?- p(Free). Hello, World! Free = world ?- p(hello). No Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

65 / 112

Matching: CHR vs. Prolog p(world) :- writeln(’Hello, World!’). :- chr_constraint c/1. c(world) writeln(’Hello, World!’). ?- p(world). Hello, World!

?- c(world). Hello, World!

?- p(Free). Hello, World! Free = world

?- c(Free).

?- p(hello). No Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

65 / 112

Matching: CHR vs. Prolog p(world) :- writeln(’Hello, World!’). :- chr_constraint c/1. c(world) writeln(’Hello, World!’). ?- p(world). Hello, World!

?- c(world). Hello, World!

?- p(Free). Hello, World! Free = world

?- c(Free). c(Free)

?- p(hello). No Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

65 / 112

Matching: CHR vs. Prolog p(world) :- writeln(’Hello, World!’). :- chr_constraint c/1. c(world) writeln(’Hello, World!’). ?- p(world). Hello, World!

?- c(world). Hello, World!

?- p(Free). Hello, World! Free = world

?- c(Free). c(Free)

?- p(hello). No

?- c(hello).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

65 / 112

Matching: CHR vs. Prolog p(world) :- writeln(’Hello, World!’). :- chr_constraint c/1. c(world) writeln(’Hello, World!’). ?- p(world). Hello, World!

?- c(world). Hello, World!

?- p(Free). Hello, World! Free = world

?- c(Free). c(Free)

?- p(hello). No

?- c(hello). c(hello)

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

65 / 112

Summary

Matching I short-hand for equality-based guards I one-way unification I only instantiates head I does not instantiate constraints

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

66 / 112

Overview

67/112 1 2 3 4 5 6 7 8 9 10 11 12 13

Tom Schrijvers (K.U.Leuven)

Introduction The Art of CHR Simpagation For the Sake of Arguments En Guarde A Perfect Match Bigger Programs Propagation Order in the Rules Reactivation CHR vs. Prolog Tutorial Summary Facts about CHR

Constraint Handling Rules

67 / 112

Combining Rules

generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). value(I), value(J) K is I + J, value(K). What’s the outcome? ?- generate(4).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

68 / 112

Combining Rules

generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). value(I), value(J) K is I + J, value(K). What’s the outcome? ?- generate(4). value(10)

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

68 / 112

Combining Rules

generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). value(I), value(J) K is I + J, value(K). What’s the outcome? ?- generate(4). value(10) ?- generate(5).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

68 / 112

Combining Rules

generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). value(I), value(J) K is I + J, value(K). What’s the outcome? ?- generate(4). value(10) ?- generate(5). value(15)

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

68 / 112

In Slow Motion generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). value(I), value(J) K is I + J, value(K).

?-

Tom Schrijvers (K.U.Leuven)

2

5

Constraint Handling Rules

69 / 112

In Slow Motion generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). value(I), value(J) K is I + J, value(K).

?-

5

2 Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

69 / 112

In Slow Motion generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). value(I), value(J) K is I + J, value(K).

?-

Tom Schrijvers (K.U.Leuven)

2

M is 2 - 1

Constraint Handling Rules

M 5

69 / 112

In Slow Motion generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). value(I), value(J) K is I + J, value(K).

?-

M is 2 - 1

M

5

2 Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

69 / 112

In Slow Motion generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). value(I), value(J) K is I + J, value(K).

?-

1

5

2 Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

69 / 112

In Slow Motion generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). value(I), value(J) K is I + J, value(K).

?-

5

2 Tom Schrijvers (K.U.Leuven)

1 Constraint Handling Rules

69 / 112

In Slow Motion generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). value(I), value(J) K is I + J, value(K).

?-

1

M is 1 - 1

M 5

2 Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

69 / 112

In Slow Motion generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). value(I), value(J) K is I + J, value(K).

?-

M is 1 - 1

2 Tom Schrijvers (K.U.Leuven)

M 5

1 Constraint Handling Rules

69 / 112

In Slow Motion generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). value(I), value(J) K is I + J, value(K).

?-

M is 1 - 1

M 5

3 Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

69 / 112

In Slow Motion generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). value(I), value(J) K is I + J, value(K).

?-

0

5

3 Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

69 / 112

In Slow Motion generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). value(I), value(J) K is I + J, value(K).

?-

5

0 Tom Schrijvers (K.U.Leuven)

3 Constraint Handling Rules

69 / 112

In Slow Motion generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). value(I), value(J) K is I + J, value(K).

?-

5

3 Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

69 / 112

Combining Rules generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true. What’s the outcome? ?- generate(4).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

70 / 112

Combining Rules generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true. What’s the outcome? ?- generate(4). value(2) value(3)

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

70 / 112

Combining Rules generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true. What’s the outcome? ?- generate(4). value(2) value(3) ?- generate(10).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

70 / 112

Combining Rules generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true. What’s the outcome? ?- generate(4). value(2) value(3) ?- generate(10). value(2) value(3) value(5) value(7) Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

70 / 112

How Does It Work? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

1 0

Constraint Handling Rules

71 / 112

How Does It Work? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

71 / 112

How Does It Work? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

71 / 112

How Does It Work? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

71 / 112

How Does It Work? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

71 / 112

How Does It Work? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

71 / 112

How Does It Work? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

71 / 112

How Does It Work? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

71 / 112

How Does It Work? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

71 / 112

How Does It Work? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

71 / 112

How Does It Work? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

71 / 112

How Does It Work? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

71 / 112

How Does It Work? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

71 / 112

How Does It Work? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

71 / 112

How Does It Work? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

71 / 112

Alternatives? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

2

3

4

5

6

7

8

9

10

Tom Schrijvers (K.U.Leuven)

Just added value(3). What now?

Constraint Handling Rules

72 / 112

Alternatives? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

2

3

4

5

6

7

8

9

10

Tom Schrijvers (K.U.Leuven)

Just added value(3). What now? I remove value(6) ?

Constraint Handling Rules

72 / 112

Alternatives? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

2

3

4

5

6

7

8

9

10

Tom Schrijvers (K.U.Leuven)

Just added value(3). What now? I remove value(6) ? I remove value(9) ?

Constraint Handling Rules

72 / 112

Alternatives? generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

2

3

4

5

6

7

8

9

10

Tom Schrijvers (K.U.Leuven)

Just added value(3). What now? I remove value(6) ? I remove value(9) ? I unspecified in CHR!

Constraint Handling Rules

72 / 112

Alternate Ending generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

73 / 112

Alternate Ending generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

73 / 112

Alternate Ending generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

73 / 112

Alternate Ending generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

73 / 112

Alternate Ending generate(1) true. generate(N) N > 1 | value(N), M is N - 1, generate(M). value(I) \ value(J) J mod I =:= 0 | true.

Tom Schrijvers (K.U.Leuven)

2

3

4

5

6

7

8

9

10

Constraint Handling Rules

73 / 112

Summary

CHR Programs I

Programs consist of a sequence of rules

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

74 / 112

Summary

CHR Programs I I

Programs consist of a sequence of rules Active constraint traverses the rules top-to-bottom to find any that fire

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

74 / 112

Summary

CHR Programs I I

I

Programs consist of a sequence of rules Active constraint traverses the rules top-to-bottom to find any that fire Rest of query/body waits

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

74 / 112

Summary

CHR Programs I I

I I

Programs consist of a sequence of rules Active constraint traverses the rules top-to-bottom to find any that fire Rest of query/body waits Unspecified which partner is found

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

74 / 112

Overview

75/112 1 2 3 4 5 6 7 8 9 10 11 12 13

Tom Schrijvers (K.U.Leuven)

Introduction The Art of CHR Simpagation For the Sake of Arguments En Guarde A Perfect Match Bigger Programs Propagation Order in the Rules Reactivation CHR vs. Prolog Tutorial Summary Facts about CHR

Constraint Handling Rules

75 / 112

Simpagation with Hk = ∅

Simpagation rule

headk \headr guard | body .

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

76 / 112

Simpagation with Hk = ∅

Simpagation rule

headk \headr guard | body . What if head k = ∅?

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

76 / 112

Simpagation with Hk = ∅

Simpagation rule

headk \headr guard | body . What if head k = ∅? Simplification rule

head r guard | body .

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

76 / 112

Simpagation with Hr = ∅ Simpagation rule

headk \headr guard | body .

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

77 / 112

Simpagation with Hr = ∅ Simpagation rule

headk \headr guard | body . What if head r = ∅?

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

77 / 112

Simpagation with Hr = ∅ Simpagation rule

headk \headr guard | body . What if head r = ∅? Propagation rule

head k ==> guard | body . I add body I in the presence of head k I if guard holds Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

77 / 112

Propagation

a, b ==> c. ?- a, b.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

78 / 112

Propagation

a, b ==> c. ?- a, b. a b c

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

78 / 112

Propagation

a, b ==> c. ?- a, b. a b c ?- a, b, b.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

78 / 112

Propagation

a, b ==> c. ?- a, b. a b c ?- a, b, b. a b b c c

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

78 / 112

Cellular Automaton Writing Wolfram’s celluar automaton Rule 110 in CHR.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

79 / 112

Cellular Automaton Writing Wolfram’s celluar automaton Rule 110 in CHR.

d c b a 1 Tom Schrijvers (K.U.Leuven)

2 Constraint Handling Rules

5 80 / 112

Cellular Automaton Writing Wolfram’s celluar automaton Rule 110 in CHR.

d c b a 1 Tom Schrijvers (K.U.Leuven)

2 Constraint Handling Rules

5 80 / 112

Cellular Automaton Writing Wolfram’s celluar automaton Rule 110 in CHR.

d c b a 1 Tom Schrijvers (K.U.Leuven)

2 Constraint Handling Rules

5 80 / 112

Cellular Automaton Writing Wolfram’s celluar automaton Rule 110 in CHR.

d c b a 1 Tom Schrijvers (K.U.Leuven)

2 Constraint Handling Rules

5 80 / 112

Cellular Automaton Writing Wolfram’s celluar automaton Rule 110 in CHR.

d c b a 1 Tom Schrijvers (K.U.Leuven)

2

3

Constraint Handling Rules

5 80 / 112

Cellular Automaton Writing Wolfram’s celluar automaton Rule 110 in CHR.

d c b a 1 Tom Schrijvers (K.U.Leuven)

2

3

Constraint Handling Rules

4

5 80 / 112

Cellular Automaton Writing Wolfram’s celluar automaton Rule 110 in CHR.

d c b a 1 Tom Schrijvers (K.U.Leuven)

2

3

Constraint Handling Rules

4

5 80 / 112

Cellular Automaton Writing Wolfram’s celluar automaton Rule 110 in CHR.

d c b a 1 Tom Schrijvers (K.U.Leuven)

2

3

Constraint Handling Rules

4

5 80 / 112

Cellular Automaton CHR cells % black(Name,Below,Above,Generation). :- chr_constraint black/4. % white(Name,Below,Above,Generation). :- chr_constraint white/4.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

81 / 112

Cellular Automaton CHR cells % black(Name,Below,Above,Generation). :- chr_constraint black/4. % white(Name,Below,Above,Generation). :- chr_constraint white/4. d

?- white(d,c,a,1), black(c,b,d,1), white(b,a,c,1), white(a,d,b,1).

c b a 1

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

81 / 112

Automaton Rule white(C,_,_.G) , black(B,A,C,G) , white(A,_,_,G) ==> G < 10 | NG is G + 1, black(B,A,C,NG).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

82 / 112

Automaton Rule white(C,_,_.G) , black(B,A,C,G) , white(A,_,_,G) ==> G < 10 | NG is G + 1, black(B,A,C,NG). ?- white(d,c,a,1), black(c,b,d,1), white(b,a,c,1), white(a,d,b,1).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

82 / 112

Automaton Rule white(C,_,_.G) , black(B,A,C,G) , white(A,_,_,G) ==> G < 10 | NG is G + 1, black(B,A,C,NG). ?- white(d,c,a,1), black(c,b,d,1), white(b,a,c,1), white(a,d,b,1).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

82 / 112

Another Example of Propagation generate(N) ==> value(2).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

83 / 112

Another Example of Propagation generate(N) ==> value(2). generate(N), value(I) ==> I < N | J is I + 1, value(J).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

83 / 112

Another Example of Propagation generate(N) ==> value(2). generate(N), value(I) ==> I < N | J is I + 1, value(J). value(I) \ value(J) J mod I =:= 0 | true. ?- generate(10).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

83 / 112

Another Example of Propagation generate(N) ==> value(2). generate(N), value(I) ==> I < N | J is I + 1, value(J). value(I) \ value(J) J mod I =:= 0 | true. ?- generate(10). value(2) value(3) value(5) value(7) generate(10)

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

83 / 112

Summary Simplification Rule: head guard | body . I

replace head with body

Simpagation Rule: headk \headr guard | body . I

replace headr with body

I

in the presence of headk

Propagation Rule: head ==> guard | body . I add body I in the presence of head I fires once for each combination (propagation history) Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

84 / 112

Overview

85/112 1 2 3 4 5 6 7 8 9 10 11 12 13

Tom Schrijvers (K.U.Leuven)

Introduction The Art of CHR Simpagation For the Sake of Arguments En Guarde A Perfect Match Bigger Programs Propagation Order in the Rules Reactivation CHR vs. Prolog Tutorial Summary Facts about CHR

Constraint Handling Rules

85 / 112

Biased Coin?

coin heads. coin tails. What’s the outcome? ?- coin.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

86 / 112

Biased Coin?

coin heads. coin tails. What’s the outcome? ?- coin. heads

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

86 / 112

Biased Coin?

coin heads. coin tails. What’s the outcome? ?- coin. heads Rules are tried in order!

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

86 / 112

Coin Flipping in Prolog CHR coin heads. coin tails.

Prolog p_coin :- heads. p_coin :- tails.

What’s the outcome?

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

87 / 112

Coin Flipping in Prolog CHR coin heads. coin tails.

Prolog p_coin :- heads. p_coin :- tails.

What’s the outcome? ?- coin. heads

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

87 / 112

Coin Flipping in Prolog CHR coin heads. coin tails.

Prolog p_coin :- heads. p_coin :- tails.

What’s the outcome? ?- coin. heads

Tom Schrijvers (K.U.Leuven)

?- p_coin. heads

Constraint Handling Rules

87 / 112

Coin Flipping in Prolog CHR coin heads. coin tails.

Prolog p_coin :- heads. p_coin :- tails.

What’s the outcome? ?- coin. heads

Tom Schrijvers (K.U.Leuven)

?- p_coin. heads ; y

Constraint Handling Rules

87 / 112

Coin Flipping in Prolog CHR coin heads. coin tails.

Prolog p_coin :- heads. p_coin :- tails.

What’s the outcome? ?- coin. heads

?- p_coin. heads ; y tails

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

87 / 112

Coin Flipping in Prolog CHR coin heads. coin tails.

Prolog p_coin :- heads. p_coin :- tails.

What’s the outcome? ?- coin. heads

?- p_coin. heads ; y tails

I

Prolog: backtracking

I

CHR: committed choice for simplification rules

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

87 / 112

Propagation Coin Flipping

coin ==> heads. coin ==> tails. What’s the outcome? ?- coin. heads tails coin

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

88 / 112

Propagation Coin Flipping

coin ==> heads. coin ==> tails. What’s the outcome? ?- coin. heads tails coin Rules are applied in sequence.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

88 / 112

Propagation Coin Flipping

coin ==> heads. coin tails. coin ==> side. What’s the outcome? ?- coin. heads tails

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

89 / 112

Propagation Coin Flipping

coin ==> heads. coin tails. coin ==> side. What’s the outcome? ?- coin. heads tails Rules are applied in sequence, until the active constraint is removed.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

89 / 112

Exploiting Rule Order generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

90 / 112

Exploiting Rule Order generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). vs. generate(0) true. generate(N) value(N), M is N - 1, generate(M).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

90 / 112

Exploiting Rule Order generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). vs. generate(0) true. generate(N) value(N), M is N - 1, generate(M). for queries ?- generate(N). with N ≥ 0

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

90 / 112

Exploiting Rule Order generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). vs. generate(0) true. generate(N) value(N), M is N - 1, generate(M). for queries ?- generate(N). with N ≥ 0 Better Style?

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

90 / 112

Exploiting Rule Order generate(0) true. generate(N) N > 0 | value(N), M is N - 1, generate(M). vs. generate(0) true. generate(N) value(N), M is N - 1, generate(M). for queries ?- generate(N). with N ≥ 0 Better Style? Compare to: generate(0) :- !. generate(N) :- value(N), M is N - 1, generate(M). Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

90 / 112

Summary

Rule Order I rules are tried from top to bottom I applied in sequence I until the active constraint is removed I committed choice: no alternatives explored for simplification rules

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

91 / 112

Overview

92/112 1 2 3 4 5 6 7 8 9 10 11 12 13

Tom Schrijvers (K.U.Leuven)

Introduction The Art of CHR Simpagation For the Sake of Arguments En Guarde A Perfect Match Bigger Programs Propagation Order in the Rules Reactivation CHR vs. Prolog Tutorial Summary Facts about CHR

Constraint Handling Rules

92 / 112

Hello, World! c(hello) writeln(hello). c(world) writeln(world). ?- c(hello), c(world).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

93 / 112

Hello, World! c(hello) writeln(hello). c(world) writeln(world). ?- c(hello), c(world). hello world

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

93 / 112

Hello, World! c(hello) writeln(hello). c(world) writeln(world). ?- c(hello), c(world). hello world ?- c(X).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

93 / 112

Hello, World! c(hello) writeln(hello). c(world) writeln(world). ?- c(hello), c(world). hello world ?- c(X). c(X)

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

93 / 112

Hello, World! c(hello) writeln(hello). c(world) writeln(world). ?- c(hello), c(world). hello world ?- c(X). c(X) ?- c(X), X = hello.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

93 / 112

Hello, World! c(hello) writeln(hello). c(world) writeln(world). ?- c(hello), c(world). hello world ?- c(X). c(X) ?- c(X), X = hello. hello

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

93 / 112

Hello, World! c(hello) writeln(hello). c(world) writeln(world). ?- c(hello), c(world). hello world ?- c(X). c(X) ?- c(X), X = hello. hello ?- c(X), c(world), X = hello.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

93 / 112

Hello, World! c(hello) writeln(hello). c(world) writeln(world). ?- c(hello), c(world). hello world ?- c(X). c(X) ?- c(X), X = hello. hello ?- c(X), c(world), X = hello. world hello X = hello Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

Reactivation I

constraints suspend in the constraint store

I

unification reactivates suspended constraints

93 / 112

A Constraint: Inequality neq(X,X) fail. neq(X,Y) X \= Y | true.

?- neq(a,a).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

94 / 112

A Constraint: Inequality neq(X,X) fail. neq(X,Y) X \= Y | true.

?- neq(a,a). No

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

94 / 112

A Constraint: Inequality neq(X,X) fail. neq(X,Y) X \= Y | true.

?- neq(a,a). No ?- neq(a,b).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

94 / 112

A Constraint: Inequality neq(X,X) fail. neq(X,Y) X \= Y | true.

?- neq(a,a). No ?- neq(a,b). Yes

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

94 / 112

A Constraint: Inequality neq(X,X) fail. neq(X,Y) X \= Y | true.

?- neq(a,a). No ?- neq(a,b). Yes ?- neq(A,B).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

94 / 112

A Constraint: Inequality neq(X,X) fail. neq(X,Y) X \= Y | true. ?- neq(A,B), A = B. ?- neq(a,a). No ?- neq(a,b). Yes ?- neq(A,B). neq(A,B)

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

94 / 112

A Constraint: Inequality neq(X,X) fail. neq(X,Y) X \= Y | true.

?- neq(a,a). No

?- neq(A,B), A = B. No

?- neq(a,b). Yes ?- neq(A,B). neq(A,B)

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

94 / 112

A Constraint: Inequality neq(X,X) fail. neq(X,Y) X \= Y | true.

?- neq(a,a). No

?- neq(A,B), A = B. No ?- neq(A,B), A = a, B = a.

?- neq(a,b). Yes ?- neq(A,B). neq(A,B)

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

94 / 112

A Constraint: Inequality neq(X,X) fail. neq(X,Y) X \= Y | true.

?- neq(a,a). No ?- neq(a,b). Yes

?- neq(A,B), A = B. No ?- neq(A,B), A = a, B = a. No

?- neq(A,B). neq(A,B)

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

94 / 112

A Constraint: Inequality neq(X,X) fail. neq(X,Y) X \= Y | true.

?- neq(a,a). No ?- neq(a,b). Yes

?- neq(A,B), A = B. No ?- neq(A,B), A = a, B = a. No ?- neq(A,B), A = a, B = b.

?- neq(A,B). neq(A,B)

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

94 / 112

A Constraint: Inequality neq(X,X) fail. neq(X,Y) X \= Y | true.

?- neq(a,a). No ?- neq(a,b). Yes ?- neq(A,B). neq(A,B)

Tom Schrijvers (K.U.Leuven)

?- neq(A,B), A = B. No ?- neq(A,B), A = a, B = a. No ?- neq(A,B), A = a, B = b. Yes

Constraint Handling Rules

94 / 112

A Constraint: Inequality neq(X,X) fail. neq(X,Y) X \= Y | true.

?- neq(a,a). No ?- neq(a,b). Yes ?- neq(A,B). neq(A,B)

Tom Schrijvers (K.U.Leuven)

?- neq(A,B), A = B. No ?- neq(A,B), A = a, B = a. No ?- neq(A,B), A = a, B = b. Yes ?- neq(A,B), A = f(C), B = f(D).

Constraint Handling Rules

94 / 112

A Constraint: Inequality neq(X,X) fail. neq(X,Y) X \= Y | true.

?- neq(a,a). No ?- neq(a,b). Yes ?- neq(A,B). neq(A,B)

Tom Schrijvers (K.U.Leuven)

?- neq(A,B), A = B. No ?- neq(A,B), A = a, B = a. No ?- neq(A,B), A = a, B = b. Yes ?- neq(A,B), A = f(C), B = f(D). neq(f(C),f(D)) Constraint Handling Rules

94 / 112

Using Reactivation for Finite Domains domain(X,L) ground(X) | memberchk(X,L). domain(X,[]) fail. domain(X,[V]) X = V. domain(X,L1), domain(X,L2) intersection(L1,L2,L), domain(X,L). intersection(L1,L2,L3) :- ...

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

95 / 112

Using Reactivation for Finite Domains domain(X,L) ground(X) | memberchk(X,L). domain(X,[]) fail. domain(X,[V]) X = V. domain(X,L1), domain(X,L2) intersection(L1,L2,L), domain(X,L). intersection(L1,L2,L3) :- ... ?- domain(X,[dog,fox,horse,snails,zebra]).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

95 / 112

Using Reactivation for Finite Domains domain(X,L) ground(X) | memberchk(X,L). domain(X,[]) fail. domain(X,[V]) X = V. domain(X,L1), domain(X,L2) intersection(L1,L2,L), domain(X,L). intersection(L1,L2,L3) :- ... ?- domain(X,[dog,fox,horse,snails,zebra]). domain(X,[dog,fox,horse,snail,zebra])

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

95 / 112

Using Reactivation for Finite Domains domain(X,L) ground(X) | memberchk(X,L). domain(X,[]) fail. domain(X,[V]) X = V. domain(X,L1), domain(X,L2) intersection(L1,L2,L), domain(X,L). intersection(L1,L2,L3) :- ... ?- domain(X,[dog,fox,horse,snails,zebra]). domain(X,[dog,fox,horse,snail,zebra]) ?- domain(X,[d,f,h,s,z]), X = z.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

95 / 112

Using Reactivation for Finite Domains domain(X,L) ground(X) | memberchk(X,L). domain(X,[]) fail. domain(X,[V]) X = V. domain(X,L1), domain(X,L2) intersection(L1,L2,L), domain(X,L). intersection(L1,L2,L3) :- ... ?- domain(X,[dog,fox,horse,snails,zebra]). domain(X,[dog,fox,horse,snail,zebra]) ?- domain(X,[d,f,h,s,z]), X = z. X = z

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

95 / 112

Using Reactivation for Finite Domains domain(X,L) ground(X) | memberchk(X,L). domain(X,[]) fail. domain(X,[V]) X = V. domain(X,L1), domain(X,L2) intersection(L1,L2,L), domain(X,L). intersection(L1,L2,L3) :- ... ?- domain(X,[dog,fox,horse,snails,zebra]). domain(X,[dog,fox,horse,snail,zebra]) ?- domain(X,[d,f,h,s,z]), X = z. X = z ?- domain(X,[d,f,h,s,z]), domain(X,[c,s,z]).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

95 / 112

Using Reactivation for Finite Domains domain(X,L) ground(X) | memberchk(X,L). domain(X,[]) fail. domain(X,[V]) X = V. domain(X,L1), domain(X,L2) intersection(L1,L2,L), domain(X,L). intersection(L1,L2,L3) :- ... ?- domain(X,[dog,fox,horse,snails,zebra]). domain(X,[dog,fox,horse,snail,zebra]) ?- domain(X,[d,f,h,s,z]), X = z. X = z ?- domain(X,[d,f,h,s,z]), domain(X,[c,s,z]). domain(X,[s,z])

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

95 / 112

Using Reactivation for Finite Domains domain(X,L) ground(X) | memberchk(X,L). domain(X,[]) fail. domain(X,[V]) X = V. domain(X,L1), domain(X,L2) intersection(L1,L2,L), domain(X,L). intersection(L1,L2,L3) :- ... ?- domain(X,[dog,fox,horse,snails,zebra]). domain(X,[dog,fox,horse,snail,zebra]) ?- domain(X,[d,f,h,s,z]), X = z. X = z ?- domain(X,[d,f,h,s,z]), domain(X,[c,s,z]). domain(X,[s,z]) ?- domain(X,[d,f,h,s,z]), domain(X,[c,z]).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

95 / 112

Using Reactivation for Finite Domains domain(X,L) ground(X) | memberchk(X,L). domain(X,[]) fail. domain(X,[V]) X = V. domain(X,L1), domain(X,L2) intersection(L1,L2,L), domain(X,L). intersection(L1,L2,L3) :- ... ?- domain(X,[dog,fox,horse,snails,zebra]). domain(X,[dog,fox,horse,snail,zebra]) ?- domain(X,[d,f,h,s,z]), X = z. X = z ?- domain(X,[d,f,h,s,z]), domain(X,[c,s,z]). domain(X,[s,z]) ?- domain(X,[d,f,h,s,z]), domain(X,[c,z]). X = z Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

95 / 112

Summary

Reactivation I

rules may not fire because of lack of instantiation

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

96 / 112

Summary

Reactivation I I

rules may not fire because of lack of instantiation upon instantiation, the rule may now fire

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

96 / 112

Summary

Reactivation I I I

rules may not fire because of lack of instantiation upon instantiation, the rule may now fire useful for implementing constraint solvers

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

96 / 112

Overview

97/112 1 2 3 4 5 6 7 8 9 10 11 12 13

Tom Schrijvers (K.U.Leuven)

Introduction The Art of CHR Simpagation For the Sake of Arguments En Guarde A Perfect Match Bigger Programs Propagation Order in the Rules Reactivation CHR vs. Prolog Tutorial Summary Facts about CHR

Constraint Handling Rules

97 / 112

Summary: CHR vs Prolog

Prolog

CHR

1

≥1

rule selection

unification

matching & guard

different rules

alternatives/backtracking

try all in sequence

failure

delay

heads

no rule

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

98 / 112

CHR and Backtracking

p :- a. p :- b.

a c1. a c2.

b d1. b d2.

?- p. c1

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

99 / 112

CHR and Backtracking

p :- a. p :- b.

a c1. a c2.

b d1. b d2.

?- p. c1 ; y d1

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

99 / 112

CHR and Backtracking

p :- a. p :- b.

a c1. a c2.

b d1. b d2. I

?- p. c1 ; y d1

Tom Schrijvers (K.U.Leuven)

I I

Constraint Handling Rules

Prolog creates choicepoints CHR does not Prolog backtracking undoes CHR changes 99 / 112

Backtracking for Labeling domain(X,L) ground(X) | memberchk(X,L). domain(X,[]) fail. domain(X,[V]) X = V. domain(X,L1), domain(X,L2) intersection(L1,L2,L), domain(X,L). split(X) ground(X) | true. split(X), domain(X,[V|Vs]) X = V ; domain(X,Vs). ?- domain(X,[d,z]), split(X).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

100 / 112

Backtracking for Labeling domain(X,L) ground(X) | memberchk(X,L). domain(X,[]) fail. domain(X,[V]) X = V. domain(X,L1), domain(X,L2) intersection(L1,L2,L), domain(X,L). split(X) ground(X) | true. split(X), domain(X,[V|Vs]) X = V ; domain(X,Vs). ?- domain(X,[d,z]), split(X). X = d ; y X = z

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

100 / 112

Backtracking for Labeling domain(X,L) ground(X) | memberchk(X,L). domain(X,[]) fail. domain(X,[V]) X = V. domain(X,L1), domain(X,L2) intersection(L1,L2,L), domain(X,L). split(X) ground(X) | true. split(X), domain(X,[V|Vs]) X = V ; domain(X,Vs). ?- domain(X,[d,z]), split(X). X = d ; y X = z ?- domain(X,[d,f,z]), split(X).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

100 / 112

Backtracking for Labeling domain(X,L) ground(X) | memberchk(X,L). domain(X,[]) fail. domain(X,[V]) X = V. domain(X,L1), domain(X,L2) intersection(L1,L2,L), domain(X,L). split(X) ground(X) | true. split(X), domain(X,[V|Vs]) X = V ; domain(X,Vs). ?- domain(X,[d,z]), split(X). X = d ; y X = z ?- domain(X,[d,f,z]), split(X). X = d ; y domain(X,[f,z]) Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

100 / 112

Backtracking for Labeling domain(X,L) ground(X) | memberchk(X,L). domain(X,[]) fail. domain(X,[V]) X = V. domain(X,L1), domain(X,L2) intersection(L1,L2,L), domain(X,L). indomain(X) ground(X) | true. % indomain(X), domain(X,[V1,...,Vn]) X = V1;...;X = Vn.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

101 / 112

Backtracking for Labeling domain(X,L) ground(X) | memberchk(X,L). domain(X,[]) fail. domain(X,[V]) X = V. domain(X,L1), domain(X,L2) intersection(L1,L2,L), domain(X,L). indomain(X) ground(X) | true. % indomain(X), domain(X,[V1,...,Vn]) X = V1;...;X = Vn. indomain(X), domain(X,Vs) member(X,Vs).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

101 / 112

Backtracking for Labeling domain(X,L) ground(X) | memberchk(X,L). domain(X,[]) fail. domain(X,[V]) X = V. domain(X,L1), domain(X,L2) intersection(L1,L2,L), domain(X,L). indomain(X) ground(X) | true. % indomain(X), domain(X,[V1,...,Vn]) X = V1;...;X = Vn. indomain(X), domain(X,Vs) member(X,Vs). ?- domain(X,[d,f,z]), indomain(X).

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

101 / 112

Backtracking for Labeling domain(X,L) ground(X) | memberchk(X,L). domain(X,[]) fail. domain(X,[V]) X = V. domain(X,L1), domain(X,L2) intersection(L1,L2,L), domain(X,L). indomain(X) ground(X) | true. % indomain(X), domain(X,[V1,...,Vn]) X = V1;...;X = Vn. indomain(X), domain(X,Vs) member(X,Vs). ?- domain(X,[d,f,z]), indomain(X). X = d ; y X = f ; y X = z

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

101 / 112

Overview

102/112 1 2 3 4 5 6 7 8 9 10 11 12 13

Tom Schrijvers (K.U.Leuven)

Introduction The Art of CHR Simpagation For the Sake of Arguments En Guarde A Perfect Match Bigger Programs Propagation Order in the Rules Reactivation CHR vs. Prolog Tutorial Summary Facts about CHR

Constraint Handling Rules

102 / 112

Summary

You should now have an understanding of:

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

103 / 112

Summary

You should now have an understanding of: I how CHR works.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

103 / 112

Summary

You should now have an understanding of: I how CHR works. I how CHR differs from Prolog.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

103 / 112

Summary

You should now have an understanding of: I how CHR works. I how CHR differs from Prolog. I that CHR is not exclusively about constraints .

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

103 / 112

Overview

104/112 1 2 3 4 5 6 7 8 9 10 11 12 13

Tom Schrijvers (K.U.Leuven)

Introduction The Art of CHR Simpagation For the Sake of Arguments En Guarde A Perfect Match Bigger Programs Propagation Order in the Rules Reactivation CHR vs. Prolog Tutorial Summary Facts about CHR

Constraint Handling Rules

104 / 112

History: Programming Highlights

1991 CHR is born, Thom Fr¨ uhwirth

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

105 / 112

History: Programming Highlights

1991 CHR is born, Thom Fr¨ uhwirth 1995 Christian Holzbaur implements CHR(SICStus)

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

105 / 112

History: Programming Highlights

1991 CHR is born, Thom Fr¨ uhwirth 1995 Christian Holzbaur implements CHR(SICStus) 2002 Leuven CHR is born

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

105 / 112

History: Programming Highlights

1991 CHR is born, Thom Fr¨ uhwirth 1995 Christian Holzbaur implements CHR(SICStus) 2002 Leuven CHR is born 2002-2005 optimized compilation & program analysis (abstract interpretation), PhDs of Gregory Duck and Tom Schrijvers

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

105 / 112

History: Programming Highlights

1991 CHR is born, Thom Fr¨ uhwirth 1995 Christian Holzbaur implements CHR(SICStus) 2002 Leuven CHR is born 2002-2005 optimized compilation & program analysis (abstract interpretation), PhDs of Gregory Duck and Tom Schrijvers 2004 refined semantics, Gregory Duck et al.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

105 / 112

History: Programming Highlights

1991 CHR is born, Thom Fr¨ uhwirth 1995 Christian Holzbaur implements CHR(SICStus) 2002 Leuven CHR is born 2002-2005 optimized compilation & program analysis (abstract interpretation), PhDs of Gregory Duck and Tom Schrijvers 2004 refined semantics, Gregory Duck et al. 2004 1st CHR workshop

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

105 / 112

History: Programming Highlights

1991 CHR is born, Thom Fr¨ uhwirth 1995 Christian Holzbaur implements CHR(SICStus) 2002 Leuven CHR is born 2002-2005 optimized compilation & program analysis (abstract interpretation), PhDs of Gregory Duck and Tom Schrijvers 2004 refined semantics, Gregory Duck et al. 2004 1st CHR workshop 2005-2008 computational complexity, PhD Jon Sneyers

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

105 / 112

History: Programming Highlights

1991 CHR is born, Thom Fr¨ uhwirth 1995 Christian Holzbaur implements CHR(SICStus) 2002 Leuven CHR is born 2002-2005 optimized compilation & program analysis (abstract interpretation), PhDs of Gregory Duck and Tom Schrijvers 2004 refined semantics, Gregory Duck et al. 2004 1st CHR workshop 2005-2008 computational complexity, PhD Jon Sneyers 2005- Leuven JCHR (Java), Peter Van Weert

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

105 / 112

History: Programming Highlights

1991 CHR is born, Thom Fr¨ uhwirth 1995 Christian Holzbaur implements CHR(SICStus) 2002 Leuven CHR is born 2002-2005 optimized compilation & program analysis (abstract interpretation), PhDs of Gregory Duck and Tom Schrijvers 2004 refined semantics, Gregory Duck et al. 2004 1st CHR workshop 2005-2008 computational complexity, PhD Jon Sneyers 2005- Leuven JCHR (Java), Peter Van Weert 2007 first concurrent system, Sulzmann & Lam

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

105 / 112

CHR Systems

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

106 / 112

CHR Programs Thom Fr¨uhwirth, with his students: I

lots of example programs, constraint solvers and others

Thom Fr¨uhwirth & Tom Schrijvers, I

union-find

Jon Sneyers, I

Fibonacci heaps and Dijkstra’s shortest path

I

Hopcroft’s DFA minimization

I

Turing and RAM machine simulators

Henning Christiansen, I

meta-programming in CHR

... Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

107 / 112

CHR Theory & Applications Declarative Semantics I

classical first-order logic

I

linear logic

Tom Schrijvers (K.U.Leuven)

(Fr¨ uhwirth) (Betz)

Constraint Handling Rules

108 / 112

CHR Theory & Applications Declarative Semantics I

classical first-order logic

I

linear logic

(Fr¨ uhwirth) (Betz)

Rewriting Properties I

confluence

(Fr¨ uhwirth;Duck;Haemmerle)

I

completion

(Fr¨ uhwirth&Abdennadher)

I

termination

(Fr¨ uhwirth;Voets&Pilozzi)

I

complexity

Tom Schrijvers (K.U.Leuven)

(Fr¨ uhwirth;De Koninck)

Constraint Handling Rules

108 / 112

CHR Theory & Applications Declarative Semantics I

classical first-order logic

I

linear logic

(Fr¨ uhwirth) (Betz)

Rewriting Properties I

confluence

(Fr¨ uhwirth;Duck;Haemmerle)

I

completion

(Fr¨ uhwirth&Abdennadher)

I

termination

(Fr¨ uhwirth;Voets&Pilozzi)

I

complexity

(Fr¨ uhwirth;De Koninck)

Applications I

type systems

I

test case generation

(Schrijvers)

I

multi-agent systems

(Alberti)

I

...

Tom Schrijvers (K.U.Leuven)

(Sulzmann & Stuckey)

Constraint Handling Rules

108 / 112

CHR Researchers around the World

Fig. 1. CHR research groups all over the world: 1. Ulm, Germany (Fr¨uhwirth, Meister, Betz, 2. Leuven, Belgium (Schrijvers, Demoen, Sneyers, De Koninck, Van Weert, . . . ); 3. Melbourne, Australia (Duck, Stuckey, Garc´ıa de la Banda, Wazny, Brand, . . . ); 4. Vi-

Djelloul, Raiser, . . . );

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

109 / 112

CHR in Industry multi-headed business rules custom constraint solvers

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

110 / 112

CHR in Industry multi-headed business rules custom constraint solvers I

Scientific Software & Systems Ltd. I

I

Cornerstone Technology Inc I

I

injection mould design tool

BSSE System and Software Engineering I

I

stock brokering software

test generation

MITRE Corporation I

optical network design

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

110 / 112

Further Reading

I

CHR website (Google for) I I

I

programs papers

CHR Survey I

As Time Goes By: Constraint Handling Rules – A Survey of CHR Research from 1998 to 2007, J. Sneyers, P. Van Weert, T. Schrijvers, L. De Koninck.

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

111 / 112

Thank You!

My Questions: Get the Quiz! Your Questions?

Tom Schrijvers (K.U.Leuven)

Constraint Handling Rules

112 / 112