User:Paolo Liberatore~enwiki/Clp

Constraint logic programming

 * Prolog chooses the first clause that unifies, a form of logic programming makes a non-deterministic choice; in other words, for logic programming any matching clause can be chosen, and the goal is provable iff there is a computation (among the possible ones) that leads to the empty clause
 * constraints may be added to Prolog or to a form of logic programming, leading to different way of choosing the clause
 * a constraint can be expressed in logic programming (via a predicate and a number of ground literals telling when it is satisfied); the main problem with this approach is that the various forms of local consistency cannot be used;
 * in constraint logic programming, a particular form of constraints is decided and used; in particular, these may be constraints over integers or over finite domains (the ones used as the general form of constraint programming in the other articles)
 * the constraint store does not only contain the constraints from the body of the clauses, but also the equalities that derive from adding literal=head to it when the literal is matched with the head of the clause; this includes the regular unification of terms as done in classical logic programming, but may also include a constraint such as X=2+Y derived from matching A(X) with the head of A(2+Y):-B(Y);
 * labeling statements are used for constraints over finite domains, which are actually intended arithmetic constraints;
 * without the labeling literals, the output is the current domains as reduced via the local consistency conditions on the constraint store: does this depend on the interpreter?
 * labeling statement: what does it find exactly? an assignment to its variables that satisfies all constraints or a complete satisfying assignment restricted to these variables?

Constraint satisfaction and unification
(MS1.4)

An expression of the form A=B, where A and B are literals with the same predicate, can be considered a constraint, as it is an expression over some variables, and it can be true or false (satisfied or unsatisfied) depending on the value of the variables. Constraint logic programming includes equalities of literals as constraints. In other words, a constraint logic program is based on a specific domain with its own set of constraint, but implicitly consider equality of literals as additional constraints. Constraints that are equality of literals have some specific properties. In particular, if A=P(ta1,...,tan) and B=P(tb1,...,tbn), then A=B is equivalent to ta1=tb1,...,tan=tbn. Each such equality tai=tbi can also be seen a constraint: it is an expression over variables (the ones in the terms tai and tbi) that can be satisfied or not. Constraint logic programming implicitly include these constraints.

If no function symbol is allowed, every such equality ta=tb is actually either an equality between two variables, or between a variable and a constant, or between two constants. In the latter case, it can be checked directly. In the first and second case, this is a binary and unary constraint, respectively. As a result, constraint logic programming automatically includes equality constraints between variables and between a variable and a constant. In other words, a logic program over a specific constraint language implicitly refer to equality constraints.

When function symbols are allowed, a variable can take a term as a value (including a term containing other variables). As a result, the inclusion of function symbols effectively extends the domain to include all possible terms over the domain. For example, if the original domain of constraints is that of reals and a given function symbol f is defined, then a variable can assume as a value either a real or an arbitrary term built over reals and variables using the function symbol f. In other words, the presence of function symbols extends the domains of variables from their original domain to include terms. Equality of terms tai=tbi are handled using unification in (classical) logic programming. The various cases are:


 * if both terms are the same variable or the same constant, this equality is trivially satisfied;
 * if the two terms are functions, the equality is false if the two functions are different or have different number of arguments, and is replaced with the equality of their arguments otherwise;
 * if one term is a variable and the other one is a function term containing the variable, the equality is violated;
 * if one term is a variable and the other one is a term not containing the variable, the value of the variable is the term.

These rules allow reducing tai=tbi to either truth, falsity, or substituion of a variable with a term. In constraint logic programming, this process may also end up with new constraints (e.g., X+2=Y*2).

Terms
The terms of constraint logic programming always include the terms of logic programming, which are obtained from variables and constants using functors (or tree constructors MS). The current goal can contain, in this case:


 * regular logic programming literals, that is, predicate(term,...,term)
 * equality of terms: term1=term2

When evaluating a goal, a regular literal p(t1,...,tn) is treated similarly to regular logic programming: a clause whose head is p(t1',...,tn') is chosen. Its variables are all replaced with new ones (including the ones in the head), leading to a rule p(t1,...,tn):-B. The goal is added t1=t1, ..., tn=tn, B. In other words, instead of unification, equalities of terms are added to the goal.

If the goal contains X=t or t=X, then X can be replaced with t everywhere. This is done by all interpreter, but can be considered as a post-processing step when defining the semantics of a logic program.

When evaluating t=t' in the goal, what happens depend on the form of t and t'. If they are terms with different top-level functors, this literal is unsatisfiable and the algorithm backtracks. Otherwise, if t=f(t1,...,tn) and t'=f(t1',...,tn'), this literal is replaced with t1=t1',...tn=tn'.

When considering reals and finite domains (integers), the goal can contain:


 * regular logic programming literals, that is, predicate(term,...,term)
 * equality of terms: term1=term2
 * constraints

There are two differences: first, since constraints can be in the body of clauses, they can show up in the goal when the body of a clause is added; second, terms are defined differently, which implies that a literal term1=term2 can be different from above. In particular, clp over reals includes expressions as terms: numerical constants and expressions over variables and constants are terms as well. As a result, X+2 or X/Y are terms. Other definitions of terms are used for clp over finite domains.

Since the goal may contain an equality of terms, it may contain a term of the form f(X+Y,Z)=f(Z+2,0). When evaluating this equality, the interpreter proceeds like in regular logic programming: since both terms have the same functor, this equality is replaced with X+Y=X+2 and Z=0 in the goal. When evaluating the first one, the interpreter moves it to the constraint store because this is a constraint. The same happens for Z=0; however, since this constraint allows a single value for Z only, Z is replaced with 0 in the goal and in the constraint store.

MS

 * determined variables and local propagation (1.7)
 * implication among constraints and redundancy (2.1)
 * projection (2.2); related to composition and extended composition
 * constraints that cannot be projected (only for some infinite domains, of course) (2.3)
 * equivalence on a subset of variables (2.3)

FA

 * Equality is a constraint. A constraint = is always assumed to be present in the language of constraints. Equality of two terms is always a constraint. When the two terms are functional, f(t1..tn)=g(q1..qn) is equivalent to t1=q1 ... tn=qn
 * Don't know nondeterminism=try all possibilities
 * Don't care nondeterminism=if there are more than one possibility, try an arbitrary one