# IDP4 playground

IngmarDasseville

2,391 views

### Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

## Code Examples

Simple Equation

1

2

3

4

a :: element of {1,2,3}.

b :: element of {1,2,3}.

a + b = 5.

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Propositions

1

2

3

4

p :: proposition.

q :: proposition.

p | q.

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Graph Coloring

1

2

3

4

5

6

7

8

//Declaration of given sets

borders := {("a","b"), ("b","c"), ("c","a")}.

colours := {1..3}.

//Declaration of the interpretation we are looking for

colorof/1 :: function to colours.

! borders (\(x,y) -> colorof x ~= colorof y).

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

NQueens

1

2

3

4

5

6

7

8

9

10

11

dom := {1..8}.

column/1 :: function to dom.

alldiff f := ! dom (\x -> ! dom (\y -> x ~= y => f x ~= f y)).

! {

column,

\x -> x - column x,

\x -> x + column x

} alldiff.

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Peano

1

2

3

4

5

6

7

8

9

10

11

12

13

14

//declare s and nil as constructors

s/1 :: constructor.

nil/0 :: constructor.

//converts explains how to build a peano number from an integer

conv x := case x of 0 -> nil; > 0 -> s (conv (x-1));.

//c is a number which should have a certain peano representation

c :: element of {1..10}.

conv c = s ( s( s( s (nil)))).

//d is the peano representation of 3

d := conv 3.

relevant d.

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Transitive Closure

1

2

3

4

5

6

7

closure s := union s { (a,d) || (a,b) <- (closure s), (c,d) <- (closure s), b = c}.

s := {(1,2) , (2,3) , (3,4)}.

sClosed := closure s.

relevant sClosed.

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

External Functions

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

#script(lua)

clingo = require("clingo")

N = clingo.Number

function gcd(a, b)

if a.number == 0 then

return b

else

na = a.number

nb = b.number

nc = nb % na

return gcd(N(nc), a)

end

end

#end.

gcd/2 :: external function.

gcd 15 20 = 5.

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Suggested playgrounds

Open Source Your Knowledge: become a Contributor and help others learn.
Create New Content