PrettyCLP: a Light Java Implementation for Teaching CLP Alessio - - PowerPoint PPT Presentation

prettyclp a light java implementation for teaching clp
SMART_READER_LITE
LIVE PREVIEW

PrettyCLP: a Light Java Implementation for Teaching CLP Alessio - - PowerPoint PPT Presentation

PrettyCLP: a Light Java Implementation for Teaching CLP Alessio Stalla 1 Davide Zanucco 2 Agostino Dovier 2 Viviana Mascardi 1 DISI - Univ. of Genova, alessiostalla@gmail.com,mascardi@disi.unige.it DIMI - Univ. of Udine,


slide-1
SLIDE 1

PrettyCLP: a Light Java Implementation for Teaching CLP

Alessio Stalla1 Davide Zanucco2 Agostino Dovier2 Viviana Mascardi1

DISI - Univ. of Genova, alessiostalla@gmail.com,mascardi@disi.unige.it DIMI - Univ. of Udine, zanucco.davide@spes.uniud.it,agostino.dovier@uniud.it

Pescara, September 2nd, 2011

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 1 / 20

slide-2
SLIDE 2

Summary

1

Introduction

2

Pretty Prolog

3

CLP(FD) basics

4

Demo

5

Conclusions

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 2 / 20

slide-3
SLIDE 3

Introduction

Introduction

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 3 / 20

slide-4
SLIDE 4

Introduction

Introduction

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 3 / 20

slide-5
SLIDE 5

Introduction

Introduction

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 3 / 20

slide-6
SLIDE 6

Introduction

Introduction

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 3 / 20

slide-7
SLIDE 7

Introduction

Introduction

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 3 / 20

slide-8
SLIDE 8

Introduction

Introduction

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 3 / 20

slide-9
SLIDE 9

Introduction

Introduction

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 3 / 20

slide-10
SLIDE 10

Pretty Prolog

Pretty Prolog

PrettyProlog was developed by a team of the University of Genova, for providing concrete answers to demands raised by Prolog novices [Stalla, Mascardi, Martelli: CILC09]. Main goal: help to understand the construction and the visit strategy of the SLD tree. It was developed from scratch, without reusing existing Prolog implementations, and designed to be simple, modular, and easily expandable.

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 4 / 20

slide-11
SLIDE 11

Pretty Prolog

Pretty Prolog

PrettyProlog was developed by a team of the University of Genova, for providing concrete answers to demands raised by Prolog novices [Stalla, Mascardi, Martelli: CILC09]. Main goal: help to understand the construction and the visit strategy of the SLD tree. It was developed from scratch, without reusing existing Prolog implementations, and designed to be simple, modular, and easily expandable. In this work we have in fact expanded Pretty Prolog to deal with Constraint Logic Programming on finite domains Propagation algorithms have been implemented. Differences between arc/bounds consistency are made (as much as possible) explicit

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 5 / 20

slide-12
SLIDE 12

CLP(FD) basics

CLP basics

Syntax

A first-order language Π, F, V is partitioned in the two sets ΠC and ΠP (Π = ΠC ∪ ΠP and ΠC ∩ ΠP = ∅) ΠP is the set of program defined predicate symbols ΠC is the set of constraints predicate symbols (assumed to contain “=”). Similarly, F is partitioned into FC and FP.

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 6 / 20

slide-13
SLIDE 13

CLP(FD) basics

CLP(FD) basics

Syntax

We focus on CLP on finite domains CLP(FD): FC contains the binary arithmetic function symbols +, −, ∗, /, mod

  • etc. as well as a constant symbol for any integer number

ΠC contains ≤, <, etc. false is assumed to be a special predicate in ΠP which has no rules defining it. domain and labeling are assumed to be in ΠC

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 7 / 20

slide-14
SLIDE 14

CLP(FD) basics

CLP(FD) basics

Syntax

Usual notions for program/constraint atom/literals. A primitive constraint is a constraint atom or its negation and a constraint is a conjunction of primitive constraints (true denotes the empty conjunction) A goal CLP is of the form ← ¯ B, where ¯ B is a conjunction of program atoms and primitive constraints. A CLP rule is of the form A ← ¯ B where A is a program atom and ← ¯ B is a CLP goal. A CLP program is a set of CLP rules.

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 8 / 20

slide-15
SLIDE 15

CLP(FD) basics

CLP(FD) basics

Syntax

Usual notions for program/constraint atom/literals. A primitive constraint is a constraint atom or its negation and a constraint is a conjunction of primitive constraints (true denotes the empty conjunction) A goal CLP is of the form ← ¯ B, where ¯ B is a conjunction of program atoms and primitive constraints. A CLP rule is of the form A ← ¯ B where A is a program atom and ← ¯ B is a CLP goal. A CLP program is a set of CLP rules. Operationally, conjunctions and sets are just lists

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 8 / 20

slide-16
SLIDE 16

CLP(FD) basics

CLP(FD) basics

Semantics of a constraint

Semantically, a constraint C on n variables X1, . . . , Xn with domains D1, . . . , Dn, respectively, is a relation on D1 × · · · × Dn. In CLP(FD) Di’s are finite subsets of Z A solution for C is a mapping [X1/d1, . . . , Xn/dn] such that d1, . . . , dn ∈ C. If there are no solutions, then C is inconsistent.

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 9 / 20

slide-17
SLIDE 17

CLP(FD) basics

CLP(FD) basics

Semantics of a constraint

Semantically, a constraint C on n variables X1, . . . , Xn with domains D1, . . . , Dn, respectively, is a relation on D1 × · · · × Dn. In CLP(FD) Di’s are finite subsets of Z A solution for C is a mapping [X1/d1, . . . , Xn/dn] such that d1, . . . , dn ∈ C. If there are no solutions, then C is inconsistent. The intended semantics of FC symbols (in this case, the arithmetical functions on integer numbers) is fulfilled. This allows to evaluate, e.g., X + 1 ≤ Y ∗ Y − 5 ∗ Z

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 9 / 20

slide-18
SLIDE 18

CLP(FD) basics

CLP(FD) basics

Operational semantics

It is parametric on the function solve that given a constraint C should detect whether C is satisfiable (consistent) in the constraint domain. During its computation, solve(C) might rewrite C to an equivalent simplified constraint. In practice, for complexity reasons, solve is an incomplete procedure, in the sense that instead of verifying consistency of the (entire) constraints, acts locally in each primitive constraint, removing some values in domains that cannot belong to any solution until a local property is satisfied. This phase is also called Constraint propagation

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 10 / 20

slide-19
SLIDE 19

CLP(FD) basics

CLP(FD) basics

Why solve is incomplete?

Let us consider the constraint X = Y, X = W, X = Z, Y = W, Y = Z, W = Z, where DX = DY = DZ = DW = {0, 1, 2}. Although it is inconsistent, default options in Prolog implementations are such that it is left unaltered by solve and, therefore, inconsistency is not detected.

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 11 / 20

slide-20
SLIDE 20

CLP(FD) basics

CLP(FD) basics

Why solve is incomplete?

Let us consider the constraint X = Y, X = W, X = Z, Y = W, Y = Z, W = Z, where DX = DY = DZ = DW = {0, 1, 2}. Although it is inconsistent, default options in Prolog implementations are such that it is left unaltered by solve and, therefore, inconsistency is not detected. It is the encoding of the 3-coloring problem

  • f a graph

Checking consistency of this class of con- straints is therefore NP-hard and a fast propagation algorithm can not check it (un- less P=NP).

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 11 / 20

slide-21
SLIDE 21

CLP(FD) basics

CLP(FD) basics

State

A state is a pair G | C where G is a CLP goal and C is a constraint (also known as the constraint store). A state G | C is said to be:

successful if G = true and solve(C) = false. failing if either solve(C) = false or there are no clauses in P with the same predicate of the head of the selected atom in G. unsolved if G = true and it is not failing.

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 12 / 20

slide-22
SLIDE 22

CLP(FD) basics

CLP(FD) basics

Derivation Step

Let G1 | C1 be an unsolved state, where G1 =← L1, . . . , Lm, and P a

  • program. A CLP-derivation step G1 | C1 ⇒ G2 | C2 is defined as

follows: Let Li be the selected literal in G1 (let us assume it is L1—as it happens in the implementation). Then G2 | C2 is obtained from S and P in one of the following ways:

L1 is a primitive constraint, C2 = L1 ∧ C1. If solve(C2) = false, then G2 =← false, otherwise G2 =← L2, . . . , Ln. If L1 = p(t1, . . . , tn) is a program atom, and p(s1, . . . , sn) ← ¯ B is a renaming of a clause of P then G2 =← t1 = s1, . . . , tn = sn, ¯ B, L2, . . . , Ln and C2 = C1.

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 13 / 20

slide-23
SLIDE 23

CLP(FD) basics

CLP(FD) basics

Derivation Step

Let G1 | C1 be an unsolved state, where G1 =← L1, . . . , Lm, and P a

  • program. A CLP-derivation step G1 | C1 ⇒ G2 | C2 is defined as

follows: Let Li be the selected literal in G1 (let us assume it is L1—as it happens in the implementation). Then G2 | C2 is obtained from S and P in one of the following ways:

L1 is a primitive constraint, C2 = L1 ∧ C1. If solve(C2) = false, then G2 =← false, otherwise G2 =← L2, . . . , Ln. If L1 = p(t1, . . . , tn) is a program atom, and p(s1, . . . , sn) ← ¯ B is a renaming of a clause of P then G2 =← t1 = s1, . . . , tn = sn, ¯ B, L2, . . . , Ln and C2 = C1.

This is not what it is implemented by the various Prolog systems!!!

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 13 / 20

slide-24
SLIDE 24

CLP(FD) basics

CLP(FD) basics

State

A derivation for a state S0 in P is a maximal sequence of derivations such that S0 ⇒ S1 ⇒ · · · . A derivation for a goal G is a derivation for the state G | true. A finite derivation S0 ⇒ · · · ⇒ Sn is said successful (resp. failing) if Sn is a successful (resp., failing) state. In the case of a successful derivation the computed answer is the projection of the constraint store of Sn on the variables in S0. A simplification, based on solve, is usually employed to make the

  • utput readable.

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 14 / 20

slide-25
SLIDE 25

CLP(FD) basics

CLP(FD) basics

Domain

The semantics of domain([V1, . . . , Vn], a, b) is that of assigning the domain DVi = {a, a + 1, . . . , b} to Vi’s or to update DVi with DVi ∩ {a, a + 1, . . . , b} If DVi = ∅ then false

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 15 / 20

slide-26
SLIDE 26

CLP(FD) basics

CLP(FD) basics

Labeling

The semantics of labeling([V1, . . . , Vn]) is to look for an instantiation for V1, . . . , Vn that “satisfy” the current state. If there are none of them then false

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 16 / 20

slide-27
SLIDE 27

CLP(FD) basics

CLP(FD) basics

Arc Consistency

A primitive constraint c on the variables X1, . . . , Xn is arc consistent (hyper arc consistent if n > 2) if: for all i ∈ {1, . . . , n} for all di ∈ Di exist d1 ∈ D1 . . . exist di−1 ∈ Di−1 exist di+1 ∈ Di+1 . . . exist dn ∈ Dn s.t. [X1/d1, . . . , Xn/dn] is a solution of c.

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 17 / 20

slide-28
SLIDE 28

CLP(FD) basics

CLP(FD) basics

Bounds Consistency

We use the same notion used by major Prolog implementations, a.k.a. interval consistency. A primitive constraint c on the variables X1, . . . , Xn is bounds consistent (hyper bounds consistent if n > 2) if: for all i ∈ {1, . . . , n} for all di ∈ {min Di, max Di} (the two interval bounds) exist d1 ∈ min D1.. max D1 (not necessarily in D1 !!!) . . . exist di−1 ∈ min Di−1.. max Di−1 exist di+1 ∈ min Di+1.. max Di+1 . . . exist dn ∈ min Dn.. max Dn s.t. [X1/d1, . . . , Xn/dn] is a solution of c.

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 18 / 20

slide-29
SLIDE 29

CLP(FD) basics

CLP(FD) basics

Bounds Consistency

We use the same notion used by major Prolog implementations, a.k.a. interval consistency. A primitive constraint c on the variables X1, . . . , Xn is bounds consistent (hyper bounds consistent if n > 2) if: for all i ∈ {1, . . . , n} for all di ∈ {min Di, max Di} (the two interval bounds) exist d1 ∈ min D1.. max D1 (not necessarily in D1 !!!) . . . exist di−1 ∈ min Di−1.. max Di−1 exist di+1 ∈ min Di+1.. max Di+1 . . . exist dn ∈ min Dn.. max Dn s.t. [X1/d1, . . . , Xn/dn] is a solution of c. The constraint 2X = Y where: DX = {0, 1}, DY = {0, 1, 2} is bounds consistent but not arc consistent.

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 18 / 20

slide-30
SLIDE 30

Demo

System Demo

. . . Speriamo funzioni . . .

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 19 / 20

slide-31
SLIDE 31

Conclusions

Conclusions

We have proved that it is possible to produce Tesi di laurea triennali with some usefulness (Alessio Stalla, 2009; Davide Zanucco, 2010)

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 20 / 20

slide-32
SLIDE 32

Conclusions

Conclusions

We have proved that it is possible to produce Tesi di laurea triennali with some usefulness (Alessio Stalla, 2009; Davide Zanucco, 2010) We have extended Pretty Prolog so as to deal with constraints on finite domains It is a wip. Anyway, the .jar is available on the web. Java sources are available on demand We hope someone of you will find it interesting and use it in your courses (we’ll be glad to add features on demand—e.g. the choice

  • f the granularity of viewpoints)

Stalla, Zanucco, Dovier, Mascardi () PrettyCLP 20 / 20