<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta>
      <journal-title-group>
        <journal-title>PEG</journal-title>
      </journal-title-group>
    </journal-meta>
    <article-meta>
      <title-group>
        <article-title>On Teaching Constraint-based Modeling and Algorithms for Decision Support in Prolog</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>François Fages</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Inria Saclay and Ecole Polytechnique</institution>
          ,
          <addr-line>Palaiseau</addr-line>
          ,
          <country country="FR">France</country>
        </aff>
      </contrib-group>
      <pub-date>
        <year>2024</year>
      </pub-date>
      <volume>2</volume>
      <abstract>
        <p>Constraint programming techniques are particularly successful at solving discrete optimization problems such as resource allocation, scheduling or transport problems which are ubiquitous in the industry. Although historically introduced in the mid 80's with the generalization of Prolog to the class of Constraint Logic Programming languages, those techniques are mainly used today through constraint solving libraries in standard programming languages such as C++, Java, Python, and mainly taught with constraint-based modeling languages such as MiniZinc or Essence, in the tradition of algebraic modeling languages developed for mixed integer linear programming. Nonetheless, the foundations of both constraint solvers and constraint-based models in first-order logic should make of Prolog with its constraint-solving libraries a unique language to teach all aspects of constraint programming, provided missing higher-level MiniZinc-like mathematical modeling language constructs are added to Prolog libraries. This is what I have developed for teaching purposes in SWI-Prolog through a package named modeling. This package contains libraries for defining shorthand functional notations, subscripted variables (arrays in addition to lists), set comprehension (bounded iteration and quantifiers compatible with constraints, in addition to recursion), front-end to constraint solving libraries for shorthand expansions on arrays and reification, and search tree tracing and visualization. This approach makes it possible to teach constraint-based modeling, search programming, and constraint solving with a unique high-level modeling/programming language, Prolog.</p>
      </abstract>
      <kwd-group>
        <kwd>eol&gt;constraint programming</kwd>
        <kwd>combinatorial optimization</kwd>
        <kwd>algebraic modeling languages</kwd>
        <kwd>MiniZinc</kwd>
        <kwd>metapredicates</kwd>
        <kwd>set comprehension</kwd>
        <kwd>answer constraint semantics</kwd>
        <kwd>constraint solving</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1. Introduction</title>
      <p>
        The problem of placing  queens on a chessboard of size  ×  such that they do not attack
each other (i.e. not placed on the same column, row or diagonal) is classically modeled and
solved in Prolog with a finite domain constraint solver in the framework of Constraint Logic
Programming (CLP) [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ], by
• creating a list of  finite domain decision variables, representing say the queens in each
column, with domain 1.. , representing the rows where they are placed,
• and posting disequality constraints between each pair of queens, by double recursion on
the list of variables and their tail list.
      </p>
      <p>
        However, for a course on constraint-based modeling and solving of combinatorial problems in
an engineering school, it is more natural to use mathematical notations on subscripted variables,
and set comprehension for posting constraints by iteration on indices rather than by double
recursion on lists. This is more in the spirit of mathematical modeling languages, such as
algebraic modeling languages developed for mixed integer linear programming, or modeling
languages like MiniZinc [
        <xref ref-type="bibr" rid="ref2 ref3">2, 3</xref>
        ] or Essence [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ] for constraint programming.
      </p>
      <p>
        For example, one can compare the following MiniZinc-like model using our SWI-Prolog
package named modeling1 [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ], with the classical CLP program to solve the N-queens problem:
:- use_module(library(modeling)).
:- use_module(library(clpfd)).
queens(N,
Queens):int_array(Queens, [N], 1..N),
for_all([I in 1..N-1, D in 1..N-I],
(Queens[I] #\= Queens[I+D],
      </p>
      <p>Queens[I] #\= Queens[I+D]+D,</p>
      <p>Queens[I] #\= Queens[I+D]-D)),
satisfy(Queens).
?- queens(N, Queens).</p>
      <p>N = 1,
Queens = array(1) ;
N = 4,
Queens = array(2, 4, 1, 3) ;
N = 4,
Queens = array(3, 1, 4, 2) ;
N = 5,
Queens = array(1, 3, 5, 2, 4) .
queens(N,
Queens):length(Queens, N),
Queens ins 1..N,
safe(Queens),
label(Queens).
safe([]).
safe([QI | Tail])
:noattack(Tail, QI, 1),
safe(Tail).
noattack([], _, _).
noattack([QJ | Tail], QI,
D):</p>
      <p>QI #\= QJ,
QI #\= QJ + D,
QI #\= QJ - D,
D1 #= D + 1,
noattack(Tail, QI, D1).</p>
      <p>
        The writing of the model using subscripted variables and set comprehension is arguably
higher-level, since it does not involve recursion and termination proof, but simply bounded
quantification on pairs of indices defined by set comprehension. This is even more striking when
it comes to breaking all symetries of the chessboard square, i.e. of the dihedral group of order 8
including reflection symetries around the vertical, horizontal, diagonal, antidiagonal axes and
rotation symetries, using lexicographic ordering on the primal and dual models expressed by
playing on the indices, rather than by tricky list recursions [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ].
      </p>
      <p>
        MiniZinc was designed with a principle of independence of constraint solvers, in order to
compare their performances on common benchmarks of models. A MiniZinc model is usually
transformed in a FlatZinc model in which the high-level constructs have been eliminated and
replaced by a flat constraint satisfaction problem that can be executed by a variety of constraint
solvers parsing FlatZinc syntax. This is the way for instance SICStus-Prolog is interfaced with
FlatZinc as a general purpose constraint solver to solve problems modeled in MiniZinc [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ].
      </p>
      <p>
        For teaching purposes however, we are more interested in having a MiniZinc interpreter in
Prolog in order to combine pure constraint-based modeling aspects with programming aspects
for programming search or data interface. This is possible in Eclipse system [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ] but, to the best of
our knowledge, did not exist in the form of Prolog libraries. This was thus our main motivation
1https://eu.swi-prolog.org/pack/list?p=modeling
      </p>
      <p>satisfy([Q1,Q2,Q3,Q4])
Q1=1</p>
      <p>Q1̸=1
Q2=3</p>
      <p>Q2̸=3</p>
      <p>Q1=2</p>
      <p>
        Q1̸=2
[
        <xref ref-type="bibr" rid="ref1 ref2 ref3 ref4">2,4,1,3</xref>
        ]
      </p>
      <p>Q1=3</p>
      <p>
        Q1̸=3
[
        <xref ref-type="bibr" rid="ref1 ref2 ref3 ref4">3,1,4,2</xref>
        ] Q2=1
      </p>
      <p>Q2̸=1
for developing a modeling pack to introduce functional notations and set comprehension
predicates, and write constraint-based mathematical models à la MiniZinc in Prolog.</p>
      <p>Making these high-level metapredicates available in Prolog for the pure modeling part, makes
it possible to combine them with full programming features of Prolog for data interfaces,
programming search, representing the search tree by a Prolog term, and visualizing it, a very
important feature for teaching constraint programming. Fig. 1 displays the search tree term
generated by satisfy/1 predicate with trace option of our modeling library for computing all
solutions to queens(4, Q) query.</p>
      <p>The possibility of combining constraint domains and reifying constraints with Boolean
variables, also makes of CLP a very expressive modeling language, compared to more traditional
algebraic modeling languages developed for linear programming. For example, the computation
of magic series (1, . . . , ) satisfying the equation</p>
      <p>∀  ∈ {1, . . . , }  = card { ∈ {1, . . . } |  =  − 1}
i.e. series where  is the number of occurrences of the value  − 1 in the series, can be solved
by a quite direct transcription of the mathematical definition of the problem as follows:
magic_series(N,
X):array(X, [N]),
for_all(I in 1..N,</p>
      <p>X[I] #= int_sum(J in 1..N,</p>
      <p>truth_value(X[J] #= I-1))),
satisfy(X).
?- magic_series(N, X).</p>
      <p>X = array(1, 2, 1, 0),
N = 4 ;
X = array(2, 0, 2, 0),
N = 4 ;
X = array(2, 1, 2, 0, 0),
N = 5 ;
X = array(3, 2, 1, 1, 0, 0, 0),
N = 7 ;
X = array(4, 2, 1, 0, 1, 0, 0, 0),
N = 8 .</p>
      <p>X = array(5, 2, 1, 0, 0, 1, 0, 0, 0),
N = 9 .</p>
      <p>In this definition, the Boolean truth values in {0, 1} of all constraints  =  − 1 for 1 ≤  ≤ 
are summed as integers, and the sum is constrained to be equal to . That sum is expressed
using general-purpose shorthand functional notations introduced for predicates where one
argument can be interpreted as a result, here the last argument for constraint int_sum/3 and
reification constraint truth_value/2.</p>
      <p>These features added to Prolog with libraries allow us to teach constraint programming
techniques in a unique programming environment, by focusing successively
• on constraint-based mathematical modeling techniques for a variety of constraint
satisfaction problems at a high-level of abstraction,
• on general purpose constraint solvers programmed in Prolog for diferent domains,
• and on search procedures including heuristic search and constraint-based local search
procedures written as well in Prolog.</p>
    </sec>
    <sec id="sec-2">
      <title>2. Shorthand functional notations</title>
      <p>Mathematical notations necessitate allowing shorthand functional notations in Prolog
expressions and goals. We have defined in library(shorthand) some general purpose
metapredicates shorthand/3, expand/2, expand/1, to respectively define new shorthand notations,
and expanding them in a term or in a goal before executing it.</p>
      <p>For example, this is the way the Array[Indices] functional notation for subscripted
variables is defined in SWI-Prolog in library(array), as a shorthand for V satisfying the
predicate cell(Array, Indices, V), by:
:- op(100, yf, []).
user:shorthand([](Indices, Array), V, cell(Array, Indices, V)) :- !.</p>
      <p>Independently of the particular syntax used in this example, shorthand functional notations
can be defined for any predicate in which one argument can be interpreted as a result, e.g. for
cell/2 functional notation without using a particular syntax, with respect to the last argument
of cell/3 predicate, by:
user:shorthand(cell(Array, Indices), V, cell(Array, Indices, V)) :- !.</p>
    </sec>
    <sec id="sec-3">
      <title>3. Comprehension metapredicates with answer constraint semantics</title>
      <p>
        Most aggregation metapredicates in Prolog are based on a special mechanism introduced by
David Warren in [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ] to collect information across backtracking, and illustrated in his seminal
paper with the setof(X, P, S) metapredicate, for collecting in  the set of instances of 
such that  is provable. In this approach, a non deterministic goal  is thus used as generator
comprehension by backtracking. This mechanism used for higher-order programming in
Prolog [
        <xref ref-type="bibr" rid="ref9">9</xref>
        ], and quantifiers like forall/3, cannot be used to constrain context variables, since
they refer to the success semantics of Prolog, not the answer constraint semantics [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ].
      </p>
      <p>
        We had thus to introduce in library(comprehension) a few metapredicates for
generator comprehension by iteration, rather than by backtracking, in order to satisfy the logical
semantics of constraints. For example, we introduced a universal quantifier for_all/3 capable
of constraing context variables to satisfy the goal for all instances satisfying the condition,
similarly to lower-level maplist/2 which requires specifying context and lambda variables
explicitly (operational view), but in sharp contrast to ISO Prolog forall/3 metapredicate
which merely tests goal satisfiability on all elements:
?- L=[A, B, C], forall(member(X, L), 1=X). % satisfiability test for all elements
L = [A, B, C].
?- L=[A, B, C], for_all(X in L, 1=X). % constraint posted for all elements
L = [
        <xref ref-type="bibr" rid="ref1 ref1 ref1">1, 1, 1</xref>
        ],
A = B, B = C, C = 1.
?- L=[A,B,C], maplist([X]&gt;&gt;(1=X), L). % lambda constraint posted for all elements
L = [
        <xref ref-type="bibr" rid="ref1 ref1 ref1">1, 1, 1</xref>
        ],
A = B, B = C, C = 1.
?- forall(member(X, [
        <xref ref-type="bibr" rid="ref1 ref2 ref3">1, 2, 3</xref>
        ]), X #&lt; Y).
true.
?- for_all(X in [
        <xref ref-type="bibr" rid="ref1 ref2 ref3">1, 2, 3</xref>
        ], X #&lt; Y).
clpfd:(Y in 4..sup).
?- maplist({Y}/[X]&gt;&gt;(X #&lt; Y), [
        <xref ref-type="bibr" rid="ref1 ref2 ref3">1, 2, 3</xref>
        ]).
clpfd:(Y in 4..sup).
      </p>
      <p>We similarly found it useful to introduce a comprehension metapredicate aggregate_for/6
of hopefuly easier use for the students than foldl/n.</p>
    </sec>
    <sec id="sec-4">
      <title>4. Front-end to constraint solving libraries</title>
      <p>We have defined library(clp) as a front-end to clpfd and clpr constraint solving libraries
on, respectively, (Z, +, * , ..., #=, #&gt;, ...) and (R, +, * , ..., {=}, {&gt;}, ...). This allows
us also to expand shorthand notations in constraints, accept indiferently arrays or lists in the
arguments of global constraints, reify clpr constraints with clpfd Boolean variables, and
define hybrid constraints.</p>
      <p>Furthermore, the labeling/2 predicate is enriched in this front-end library with a new
option for tracing the search tree and visualizing it in diferent forms (e.g. generating LaTeX
tikz used for Fig. 1).</p>
    </sec>
    <sec id="sec-5">
      <title>5. Modeling library</title>
      <p>Finally, library(modeling) defines predicates for creating arrays of Boolean, integer or
lfoating point decision variables, specify their domains using shorthand notations, and solve
constraint satisfaction and optimization problems with default search strategies.</p>
    </sec>
    <sec id="sec-6">
      <title>6. Plan of the course</title>
      <p>With this modeling package in Prolog, it is possible to give a course on “Constraint-based
modeling and algorithms for decision support in Prolog” at Master level, and in part at Bachelor
3rd year level,
• by focusing first on constraint-based mathematical modeling techniques for solving
various combinatorial problems, through a simple use of the modeling library,
• before getting into the foundations in first-order logic of constraint languages and logic
programming,
• the programming of general purpose constraint solvers,
• and the study of search procedures.</p>
      <p>One possible plan for the course in this approach is as follows:</p>
      <p>In such a course, Prolog with modeling pack provides an end-to-end solution to cover the
diferent aspects of constraint programming techniques for knowledge representation and
combinatorial optimisation, going from constraint-based modeling to relation-based programming,
with their common foundations in first-order logic.</p>
    </sec>
    <sec id="sec-7">
      <title>Acknowledgments</title>
      <p>I acknowledge fruitful discussions with especially Mathieu Hemery, Sylvain Soliman and of
course my students over the years.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <given-names>J.</given-names>
            <surname>Jafar</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J.-L.</given-names>
            <surname>Lassez</surname>
          </string-name>
          ,
          <article-title>Constraint logic programming</article-title>
          ,
          <source>in: Proceedings of the 14th ACM Symposium on Principles of Programming Languages</source>
          , Munich, Germany, ACM,
          <year>1987</year>
          , pp.
          <fpage>111</fpage>
          -
          <lpage>119</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <string-name>
            <given-names>N.</given-names>
            <surname>Nethercote</surname>
          </string-name>
          ,
          <string-name>
            <given-names>P. J.</given-names>
            <surname>Stuckey</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R.</given-names>
            <surname>Becket</surname>
          </string-name>
          ,
          <string-name>
            <given-names>S.</given-names>
            <surname>Brand</surname>
          </string-name>
          ,
          <string-name>
            <given-names>G. J.</given-names>
            <surname>Duck</surname>
          </string-name>
          , G. Tack,
          <article-title>MiniZinc: Towards a standard CP modelling language</article-title>
          ,
          <source>in: Proc. 13th Int. Conf. on Principles and Practice of Constraint Programming</source>
          ,
          <source>CP'07</source>
          , volume
          <volume>4741</volume>
          <source>of LNCS</source>
          , Springer-Verlag,
          <year>2007</year>
          , pp.
          <fpage>529</fpage>
          -
          <lpage>543</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          <article-title>[3] The Zinc team</article-title>
          ,
          <source>MiniZinc web page</source>
          ,
          <year>2023</year>
          . http://www.minizinc.org/.
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          [4]
          <string-name>
            <given-names>A. M.</given-names>
            <surname>Frisch</surname>
          </string-name>
          ,
          <string-name>
            <given-names>W.</given-names>
            <surname>Harvey</surname>
          </string-name>
          ,
          <string-name>
            <given-names>C.</given-names>
            <surname>Jeferson</surname>
          </string-name>
          ,
          <string-name>
            <given-names>B.</given-names>
            <surname>Martinez-Hernandez</surname>
          </string-name>
          ,
          <string-name>
            <surname>I. Miguel</surname>
          </string-name>
          ,
          <article-title>Essence: A constraint language for specifying combinatorial problems</article-title>
          ,
          <source>Constraints</source>
          <volume>13</volume>
          (
          <year>2008</year>
          )
          <fpage>268</fpage>
          -
          <lpage>306</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [5]
          <string-name>
            <given-names>F.</given-names>
            <surname>Fages</surname>
          </string-name>
          ,
          <article-title>A constraint-based mathematical modeling library in prolog with answer constraint semantics</article-title>
          ,
          <source>in: 17th International Symposium on Functional and Logic Programming</source>
          ,
          <source>FLOPS</source>
          <year>2024</year>
          , volume
          <volume>14659</volume>
          <source>of LNCS</source>
          , Springer-Verlag, Kumamoto, Japan,
          <year>2024</year>
          , pp.
          <fpage>135</fpage>
          -
          <lpage>150</lpage>
          . URL: https://inria.hal.science/hal-04478132. doi:
          <volume>10</volume>
          .1007/
          <fpage>978</fpage>
          -981-97-2300-
          <issue>3</issue>
          _
          <fpage>8</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          [6]
          <string-name>
            <given-names>Mats</given-names>
            <surname>Carlsson</surname>
          </string-name>
          et al.,
          <source>Sicstus 4.2.3</source>
          ,
          <year>2012</year>
          . https://sicstus.sics.se/.
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          [7]
          <string-name>
            <given-names>K.</given-names>
            <surname>Apt</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            <surname>Wallace</surname>
          </string-name>
          ,
          <source>Constraint Logic Programming using Eclipse</source>
          , Cambridge University Press,
          <year>2006</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          [8]
          <string-name>
            <given-names>D. H. D.</given-names>
            <surname>Warren</surname>
          </string-name>
          ,
          <article-title>Higher-order extensions to Prolog: Are they needed?</article-title>
          ,
          <source>in: Machine Intelligence</source>
          , volume
          <volume>10</volume>
          ,
          <year>1982</year>
          , pp.
          <fpage>441</fpage>
          -
          <lpage>454</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          [9]
          <string-name>
            <given-names>K.</given-names>
            <surname>Sagonas</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D. S.</given-names>
            <surname>Warren</surname>
          </string-name>
          ,
          <article-title>Efcient execution of HiLog in WAM-based prolog implementations</article-title>
          , in: L.
          <string-name>
            <surname>Sterling</surname>
          </string-name>
          (Ed.),
          <source>Proceedings of the 12th International Conference on Logic Programming</source>
          , MIT Press,
          <year>1995</year>
          , pp.
          <fpage>349</fpage>
          -
          <lpage>363</lpage>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>