14.11.2012 Views

Curry: An Integrated Functional Logic Language

Curry: An Integrated Functional Logic Language

Curry: An Integrated Functional Logic Language

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

A.5 Constraint Solving and Concurrent Programming<br />

In this example we demonstrate how <strong>Curry</strong>’s concurrent features can be used to solve constraint<br />

problems with finite domains more efficiently than a simple generate-and-test solver. We want to<br />

solve the classical map coloring problem. Consider the following simple map:<br />

Country 1<br />

Country 2<br />

Country 3<br />

Country 4<br />

The problem is to assign to each of the four countries a color (red, green, yellow, or blue) such that<br />

countries with a common border have different colors.<br />

To solve this problem, we have to specify that some countries have different colors. For this<br />

purpose, we define the following constraint which checks to elements for incompatibility:<br />

diff :: a -> a -> Success<br />

diff x y = (x==y)=:=False<br />

For instance, diff t1 t2 is satisfied if t1 and t2 are different constants. If one of the arguments is a<br />

variable, the evaluation of the function is delayed by definition of ==.<br />

Now there is a straightforward solution of the map coloring problem. We define a constraint<br />

coloring specifying the valid colors for each country and a constraint correct specifying which<br />

countries must have different colors:<br />

data Color = Red | Green | Yellow | Blue<br />

isColor :: Color -> Success<br />

isColor Red = success<br />

isColor Yellow = success<br />

isColor Green = success<br />

isColor Blue = success<br />

coloring :: Color -> Color -> Color -> Color -> Success<br />

coloring l1 l2 l3 l4 = isColor l1 & isColor l2 & isColor l3 & isColor l4<br />

correct :: Color -> Color -> Color -> Color -> Success<br />

correct l1 l2 l3 l4 =<br />

diff l1 l2 & diff l1 l3 & diff l2 l3 & diff l2 l4 & diff l3 l4<br />

In classical logic programming, we can compute the solutions to the map coloring problem by<br />

enumerating all potential solutions followed by a check whether a potential solution is a correct<br />

42

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!