The following is an example program using Copris.

1: object FirstStep { 2: import jp.kobe_u.copris._ 3: import jp.kobe_u.copris.dsl._ 4: 5: def main ( args : Array[String] ) = { 6: val x = int('x, 0, 7) 7: val y = int('y, 0, 7) 8: add(x + y === 7) 9: add(x * 2 + y * 4 === 20) 10: if (find) { 11: println(solution) 12: } 13: } 14: }

The sixth line declares a CSP variable x ∈ {0,1,2,…,7}, and assigns it to the Scala variable x.

The seventh line declares a CSP variable y ∈ {0,1,2,…,7}, and assigns it to the Scala variable y.

The eighth line adds a CSP constraint x + y = 7.

The ninth line adds a CSP constraint 2x + 4y = 20.

The tenth line finds a first solution, and the next line displays the solution as follows. Solution(Map(x -> 4, y -> 3),Map())

The following is an example program of solving 8-Queens. Don't you think it is very simple?

1: object Queens { 2: import jp.kobe_u.copris._; import jp.kobe_u.copris.dsl._ 3: def queens ( n : Int ) = { 4: for (i <- 1 to n) int('q(i), 1, n) 5: add(Alldifferent((1 to n).map(i => 'q(i)))) 6: add(Alldifferent((1 to n).map(i => 'q(i) + i))) 7: add(Alldifferent((1 to n).map(i => 'q(i) - i))) 8: if (find) { 9: do { 10: println(solution) 11: } while (findNext) 12: } 13: } 14: def main ( args : Array[String] ) = 15: queens(8) 16: }

The following is an example program of solving a Sudoku puzzle.

1: object Sudoku { 2: import jp.kobe_u.copris._; import jp.kobe_u.copris.dsl._ 3: def solve ( m : Int , n : Int , puzzle : Seq[Seq[Int]] ) = { 4: for (i <- 0 until n; j <- 0 until n) 5: int('x(i,j), 1, n) 6: for (i <- 0 until n) 7: add(Alldifferent((0 until n).map('x(i,_)))) 8: for (j <- 0 until n) 9: add(Alldifferent((0 until n).map('x(_,j)))) 10: for (i <- 0 until n by m; j <- 0 until n by m) { 11: val xs = for (di <- 0 until m; dj <- 0 until m) yield 'x(i+di,j+dj) 12: add(Alldifferent(xs)) 13: } 14: for (i <- 0 until n; j <- 0 until n; if puzzle(i)(j) > 0) 15: add('x(i,j) === puzzle(i)(j)) 16: if (find) { 17: for (i <- 0 until n) 18: println((0 until n).map(j => solution('x(i,j))).mkString( " " )) 19: } 20: } 21: def main ( args : Array[String] ) = { 22: 23: val puzzle = Seq( 24: Seq(0, 0, 0, 0, 0, 0, 0, 0, 0), 25: Seq(0, 4, 3, 0, 0, 0, 6, 7, 0), 26: Seq(5, 0, 0, 4, 0, 2, 0, 0, 8), 27: Seq(8, 0, 0, 0, 6, 0, 0, 0, 1), 28: Seq(2, 0, 0, 0, 0, 0, 0, 0, 5), 29: Seq(0, 5, 0, 0, 0, 0, 0, 4, 0), 30: Seq(0, 0, 6, 0, 0, 0, 7, 0, 0), 31: Seq(0, 0, 0, 5, 0, 1, 0, 0, 0), 32: Seq(0, 0, 0, 0, 8, 0, 0, 0, 0)) 33: solve(3, 9, puzzle) 34: } 35: }