tion solver inside, but the system seeing A#=B
equality, without knowing the values of the vari-
ables, stores this relation, which will be used when
needed. The program: X in 0 .. 12, X
in -2 .. 4, X#<3. produces a “fuzzy” an-
swer: X in 0..2. A relatively complex Sudoku
solver can be coded in about 10 lines, the puzzle
“SEND+MORE=MONEY”, even less. The students
learned how to deal with such concepts as redundancy
of information, contradictions, and conventions, not
always explicit, but current (here: the first digit of a
natural integer is non-zero). The specials operators,
such as #=<, enable the coexistence between standard
mathematical relations in Prolog (=<), and the set-
valued constraints. The students could invent them-
selves the CLP variant of the permutation program,
with arbitrary number N of integer constants:
length(L,N),L ins 1 .. N,
all_different(L),label(L).
where label assigns concrete values to members of
a constrained data set. The constraint systems are
used for scheduling, optimization and fault diagno-
sis, etc. Some graphical constraint applications as
the drawing package Asymptote permit to state such
commands as “put the box A in the middle of the line
B, and orient it orthogonally to it”, without specifing
the coordinates, and not even knowing where lies the
line B, given only as a “vertical line of length 10, end-
ing on the intersection ef curves C and D”. Constraint
(visual) tools are used to teach geometry in school.
But geometry is not just for mathematical problems,
historians of visual arts, and archaeologists need it ev-
ery day.
5.1 Just Logic. . .
There are many categories of constraints. Temporal
intervals are different from binary logic. While teach-
ing elements of Artificial Intelligence, we proposed
some logic puzzles published by R. Smullyan, e.g., in
(Smullyan, 1978). We didn’t ask to solve such prob-
lems using Prolog, but the constraint package CLP(B)
turned out to be excellent for formalizing problems,
and for testing the solution. Here are two easy prob-
lems, belonging to the world of Knights, who told
only the truth, and Knaves, constant liars. The first
problem is: two individuals approach, the first says,
“Neither of us is a knight.” What are they? The
boolean constraint solution is a one-liner:
sat(A =:= ~A * ~B),labeling([A,B]).
The form sat(...) takes an expression, and finds
its satisfiability, with * being the conjuction, +: dis-
junction, =:=: equality, and ~: negation. The form
labeling constructs a model of the represented uni-
verse, with the variables being assigned some values
from the allowed set. The argument of sat is a literal
translation of the verbal statement. The expression is
manifestly false, nobody can say that he is a Knave,
and the solution is here: A=Knave, B=Knight.
The second problem is more complicated. Three
people, Black, White and Red approach, Black says:
“all of us are Knaves”, and White: “exactly one of us
is a Knight.” Which is which? The solution [Knave,
Knight, Knave] is given by another one-liner:
L=[Black,White,Red],sat((Black # +L) *
(White=:=card([1],L))),labeling(L).
where # is the inequality, “+” computes the n-ary dis-
junction, and card finds the number of truths in a
list. This is not a domain specific strategy, but a pro-
gramming methodology. Already school pupils learn
how to transform equations and inequalities with un-
knowns, yet later some teachers and textbooks of-
fer them a restricted meaning of the concept of algo-
rithm, and convince them that computers may operate
only with concrete, known data, through sequences
of statements. Kuhn in “The Structure of Scientific
Revolutions” (Kuhn, 1962) observes that “a student
in the humanities has constantly before him a num-
ber of competing and incommensurable solutions to
his problems, solutions that he must ultimately exam-
ine for himself”. It is more natural for him to operate
with unknowns, while for an engineer, an unknown
often reduces to a placeholder awaiting the derivation
of its value from the data, and untouched before.
5.2 Constraint Handling Rules
The CHR formalism (Fr
¨
uhwirth, 2009), is an ad-
vanced topic, designed to assemble specific, domain-
oriented algorithms efficiently, and easy to embed
into existing languages. The basic implementation
platform remains Prolog. CHR is able to propagate
and simplify constraints, and eliminate the redundan-
cies. Its teaching helps to understand how the con-
straint systems work.
The example below is an arithmetic exercise. The
standard recursive formulation of maxl(L,X), which
computes X, the maximum of numbers in L, say,
maxl([2,9,7,1],X) is shown first:
maxl([X],X). % What else?...
maxl([Y|Q],X):-maxl(Q,Z),(Y>Z,X=Y;X=Z).
This reduces the set of eligible values by
rejecting those known to be smaller than
some other. The equivalent CHR program:
max(X) \ max(Y) <=> X>=Y | true., which
may be called: max(2), max(9), max(7),
CSEDU 2016 - 8th International Conference on Computer Supported Education
304