**3. The** *CFLP*(D) **programming scheme**

In this section we summarize the essentials of the *CFLP*(D) scheme (López et al., 2006) for lazy Constraint Functional-Logic Programming over a parametrically given constraint domain D, which serves as a logical and semantic framework for the declarative diagnosis method presented in the chapter.

form ♦, � or *<sup>p</sup> tn* →! *<sup>t</sup>* with *tn* ∈ *Pat*⊥(U) are called *primitive constraints*. In the sequel, we use the notation *PCon*⊥(D) for the set of primitive constraints over D and *DCon*⊥(D) for the set

<sup>127</sup> A Semantic Framework for the Declarative Debugging

**Example 3. (Constraint Domain** R**)** *The constraint domain* R *has the carrier set D***<sup>R</sup>** *of ground*

*3.* <sup>+</sup>, <sup>−</sup>, <sup>∗</sup>*, for addition, subtraction and multiplication, such that: x* <sup>+</sup><sup>R</sup> *<sup>y</sup>* <sup>→</sup> *<sup>x</sup>* <sup>+</sup>**<sup>R</sup>** *y for all x*, *<sup>y</sup>* <sup>∈</sup>

*4.* <sup>&</sup>lt;, <sup>≤</sup>, <sup>&</sup>gt;, <sup>≥</sup>*, for numeric comparisons, such that: x* <sup>&</sup>lt;<sup>R</sup> *<sup>y</sup>* <sup>→</sup> *true for all x*, *<sup>y</sup>* <sup>∈</sup> **<sup>R</sup>** *with x* <sup>&</sup>lt;**<sup>R</sup>** *y; <sup>x</sup>* <sup>&</sup>lt;<sup>R</sup> *<sup>y</sup>* <sup>→</sup> *f alse for all x*, *<sup>y</sup>* <sup>∈</sup> **<sup>R</sup>** *with x* <sup>≥</sup>**<sup>R</sup>** *y; t* <sup>&</sup>lt;<sup>R</sup> *<sup>s</sup>* → ⊥ *whenever t* <sup>∈</sup>/ **<sup>R</sup>** *or s* <sup>∈</sup>/ **<sup>R</sup>***; and analogously for* ≤R, >R, ≥R*. In the sequel, e*<sup>1</sup> < *e*<sup>2</sup> *abbreviates e*<sup>1</sup> < *e*<sup>2</sup> →! *true and e*<sup>1</sup> ≥ *e*<sup>2</sup>

The set of *valuations* over a constraint domain D is defined as the set *Val*⊥(D) of ground substitutions (i.e., mappings from variables to ground patterns). The semantics of constraints relies on the idea that a given valuation can satisfy or not a given constraint. Therefore, the set of *solutions* of *<sup>π</sup>* ∈ *PCon*⊥(D) can be defined in a natural way as a subset *Sol*D(*π*) ⊆ *Val*⊥(D); see (López et al., 2006) for details. Moreover, the set of solutions of <sup>Π</sup> ⊆ *PCon*⊥(D) is defined

For any given constraint domain D, a *CFLP*(D)-program P is presented as a set of constrained rewrite rules, called *program rules*, that define the behavior of user-defined functions. More precisely, a *constrained program rule R* for *<sup>f</sup>* <sup>∈</sup> *DF<sup>n</sup>* has the form *<sup>R</sup>* : *<sup>f</sup> tn* <sup>→</sup> *<sup>r</sup>* ⇐ <sup>Δ</sup> (abbreviated

1. The *left-hand side f tn* is a *linear* expression (i.e, there is no variable having more than one occurrence), and for all 1 ≤ *<sup>i</sup>* ≤ *<sup>n</sup>*, *ti* ∈ *Pat*⊥(U) are total patterns. The *right-hand side*

2. <sup>Δ</sup> ⊆ *DCon*⊥(D) is a finite set of total atomic constraints, intended to be interpreted as

Program defined functions can be higher-order and/or non-deterministic. For instance, the T OY program presented in Example 1 can be viewed as an example of *CFLP*(R)-program written in T OY's syntax. The reader is referred to (López et al., 2006) for more explanations

<sup>3</sup> In practice, T OY and similar languages require program rules to be well-typed in a polymorphic type system. However, the *CFLP*(D) scheme can deal also with untyped programs. Well-typedness

as *<sup>f</sup> tn* <sup>→</sup> *<sup>r</sup>* if <sup>Δ</sup> is empty) and is required to satisfy the conditions listed below3:

conjunction, and possibly including occurrences of user defined functions.

is viewed as an additional requirement, not as part of progam semantics.

**<sup>R</sup>** *t s* → ⊥ *otherwise. 2. seq, strict equality primitive for ground patterns over the real numbers, such that: seq*<sup>R</sup> *t t* → *true for all total t* ∈ *D***R***; seq*<sup>R</sup> *t s* → *f alse for all t*, *s* ∈ *D***<sup>R</sup>** *such that t*, *s have no common upper bound with respect to the* information ordering *introduced in (López et al., 2006); seq*<sup>R</sup> *t s* → ⊥

**<sup>R</sup>** *u u* → *true for all u* ∈ **R***; eq*<sup>R</sup>

**<sup>R</sup>** *u v* →

of user defined constraints over D.

*patters over* **R** *and the primitives defined below:*

*<sup>π</sup>*∈<sup>Π</sup> *Sol*D(*π*).

**3.3 Constraint functional-logic programming**

*<sup>r</sup>* ∈ *Exp*⊥(U) is also total.

and examples in other constraint domains.

*f alse for all u*, *v* ∈ **R**, *u* �= *v; eq*<sup>R</sup>

as *Sol*D(Π) =

*1. eq***R***, equality primitive for real numbers, such that: eq*R

of Wrong and Missing Answers in Declarative Constraint Programming

*otherwise. In the sequel, e*<sup>1</sup> == *e*<sup>2</sup> *abbreviates seq e*<sup>1</sup> *e*<sup>2</sup> →! *true.*

**R***; t* +<sup>R</sup> *s* → ⊥ *whenever t* ∈/ **R** *or s* ∈/ **R***; and analogously for* −<sup>R</sup> *and* ∗R*.*

*abbreviates e*<sup>1</sup> < *e*<sup>2</sup> →! *f alse (analogously for other comparison primitives).*

#### **3.1 Preliminary notions**

We consider a *universal signature* <sup>Σ</sup> = �*DC*, *FS*�, where *DC* = *<sup>n</sup>*∈**<sup>N</sup>** *DC<sup>n</sup>* and *FS* <sup>=</sup> *<sup>n</sup>*∈**<sup>N</sup>** *FS<sup>n</sup>* are countably infinite and mutually disjoint sets of *data constructors* resp. *evaluable function symbols*, indexed by arities. Evaluable functions are further classified into domain dependent *primitive functions PF<sup>n</sup>* <sup>⊆</sup> *FS<sup>n</sup>* and user *defined functions DF<sup>n</sup>* <sup>=</sup> *FS<sup>n</sup>* \ *PF<sup>n</sup>* for each *<sup>n</sup>* <sup>∈</sup> **<sup>N</sup>**. We write <sup>Σ</sup><sup>⊥</sup> for the result of extending *DC*<sup>0</sup> with the special symbol <sup>⊥</sup>, intended to denote an undefined data value and we assume that *DC* includes the two constants *true* and *false* and the usual list constructors. We use the notations *c*, *d* ∈ *DC*, *f* , *g* ∈ *FS*, and *h* ∈ *DC* ∪ *FS*. We also assume a countably infinite set V of *variables X*,*Y*, . . . and a set U of *primitive elements u*, *v*,... (as e.g. the set **<sup>R</sup>** of the real numbers) mutually disjoint and disjoint from <sup>Σ</sup>⊥. *Expressions <sup>e</sup>* ∈ *Exp*⊥(U) have the following syntax:

*e* ::= ⊥ | *u* | *X* | *h* | (*e e*<sup>1</sup> ...*em*) % shortly: (*eem*)

where *u* ∈ U, *X* ∈ V, *h* ∈ *DC* ∪ *FS*. An important subclass of expressions is the set of *patterns <sup>s</sup>*, *<sup>t</sup>* ∈ *Pat*⊥(U), whose syntax is defined as follows:

$$t ::= \bot \mid \mu \mid X \mid (c\overline{t}\_m) \mid (f\overline{t}\_m)$$

where *<sup>u</sup>* ∈ U, *<sup>X</sup>* ∈ V, *<sup>c</sup>* <sup>∈</sup> *DC<sup>n</sup>* with *<sup>m</sup>* <sup>≤</sup> *<sup>n</sup>*, and *<sup>f</sup>* <sup>∈</sup> *FS<sup>n</sup>* with *<sup>m</sup>* <sup>&</sup>lt; *<sup>n</sup>*. Patterns are used as representations of possibly functional data values. For instance, the rectangle (*rect* (5, 5) 30 40) we met when discussing Example 1 is a functional data value represented as pattern2.

As usual, we define *substitutions <sup>σ</sup>* ∈ *Sub*⊥(U) as mappings *<sup>σ</sup>* : V → *Pat*⊥(U) extended to *<sup>σ</sup>* : *Exp*⊥(U) → *Exp*⊥(U) in the natural way. By convention, we write *<sup>e</sup><sup>σ</sup>* instead of *<sup>σ</sup>*(*e*) for any *<sup>e</sup>* ∈ *Exp*⊥(U), and *σθ* for the composition of *<sup>σ</sup>* and *<sup>θ</sup>*. A substitution *<sup>σ</sup>* such that *σσ* = *<sup>σ</sup>* is called *idempotent*.

#### **3.2 Constraints over a constraint domain**

Intuitively, a constraint domain provides a set of specific data elements, along with certain primitive functions operating upon them. Primitive predicates can be modelled as primitive functions returning boolean values. Formally, a *constraint domain* with primitive elements U and primitive functions *PF* ⊆ *FS* is any structure D = �*D*<sup>U</sup> , {*p*<sup>D</sup> | *<sup>p</sup>* ∈ *PF*}� with carrier set *<sup>D</sup>*<sup>U</sup> the set of *ground* patterns (i.e., without variables) over <sup>U</sup> and interpretations *<sup>p</sup>*<sup>D</sup> <sup>⊆</sup> *<sup>D</sup><sup>n</sup>* U <sup>×</sup> *<sup>D</sup>*<sup>U</sup> of each *<sup>p</sup>* <sup>∈</sup> *PF<sup>n</sup>* satisfying the technical *monotonicity*, *antimonotonicity*, and *radicality* requirements given in (López et al., 2006). We use the notation *p*<sup>D</sup> *tn* → *t* to indicate that (*tn*, *t*) ∈ *p*D.

*Constraints* over a given constraint domain D are logical statements built from atomic constraints by means of logical conjunction ∧ and existential quantification ∃. *Atomic constraints* can have the form ♦ (standing for *truth*), � (standing for *falsity*), or *p en* →! *t* , meaning that the primitive function *<sup>p</sup>* <sup>∈</sup> *PF<sup>n</sup>* with parameters *en* <sup>∈</sup> *Exp*⊥(U) returns a *total* result *<sup>t</sup>* ∈ *Pat*⊥(U) (i.e, with no occurrences of ⊥). Constraints whose atomic parts have the

<sup>2</sup> Note that (5, 5) can be seen as syntactic sugar for (*pair* 5 5), *pair* being a constructor for ordered pairs.

6 Will-be-set-by-IN-TECH

are countably infinite and mutually disjoint sets of *data constructors* resp. *evaluable function symbols*, indexed by arities. Evaluable functions are further classified into domain dependent *primitive functions PF<sup>n</sup>* <sup>⊆</sup> *FS<sup>n</sup>* and user *defined functions DF<sup>n</sup>* <sup>=</sup> *FS<sup>n</sup>* \ *PF<sup>n</sup>* for each *<sup>n</sup>* <sup>∈</sup> **<sup>N</sup>**. We write <sup>Σ</sup><sup>⊥</sup> for the result of extending *DC*<sup>0</sup> with the special symbol <sup>⊥</sup>, intended to denote an undefined data value and we assume that *DC* includes the two constants *true* and *false* and the usual list constructors. We use the notations *c*, *d* ∈ *DC*, *f* , *g* ∈ *FS*, and *h* ∈ *DC* ∪ *FS*. We also assume a countably infinite set V of *variables X*,*Y*, . . . and a set U of *primitive elements u*, *v*,... (as e.g. the set **<sup>R</sup>** of the real numbers) mutually disjoint and disjoint from <sup>Σ</sup>⊥. *Expressions*

*e* ::= ⊥ | *u* | *X* | *h* | (*e e*<sup>1</sup> ...*em*) % shortly: (*eem*)

where *u* ∈ U, *X* ∈ V, *h* ∈ *DC* ∪ *FS*. An important subclass of expressions is the set of *patterns*

*t* ::= ⊥ | *u* | *X* | (*c tm*) | (*f tm*)

where *<sup>u</sup>* ∈ U, *<sup>X</sup>* ∈ V, *<sup>c</sup>* <sup>∈</sup> *DC<sup>n</sup>* with *<sup>m</sup>* <sup>≤</sup> *<sup>n</sup>*, and *<sup>f</sup>* <sup>∈</sup> *FS<sup>n</sup>* with *<sup>m</sup>* <sup>&</sup>lt; *<sup>n</sup>*. Patterns are used as representations of possibly functional data values. For instance, the rectangle (*rect* (5, 5) 30 40) we met when discussing Example 1 is a functional data value represented as pattern2.

As usual, we define *substitutions <sup>σ</sup>* ∈ *Sub*⊥(U) as mappings *<sup>σ</sup>* : V → *Pat*⊥(U) extended to *<sup>σ</sup>* : *Exp*⊥(U) → *Exp*⊥(U) in the natural way. By convention, we write *<sup>e</sup><sup>σ</sup>* instead of *<sup>σ</sup>*(*e*) for any *<sup>e</sup>* ∈ *Exp*⊥(U), and *σθ* for the composition of *<sup>σ</sup>* and *<sup>θ</sup>*. A substitution *<sup>σ</sup>* such that *σσ* = *<sup>σ</sup>* is

Intuitively, a constraint domain provides a set of specific data elements, along with certain primitive functions operating upon them. Primitive predicates can be modelled as primitive functions returning boolean values. Formally, a *constraint domain* with primitive elements U and primitive functions *PF* ⊆ *FS* is any structure D = �*D*<sup>U</sup> , {*p*<sup>D</sup> | *<sup>p</sup>* ∈ *PF*}� with carrier set *<sup>D</sup>*<sup>U</sup> the set of *ground* patterns (i.e., without variables) over <sup>U</sup> and interpretations *<sup>p</sup>*<sup>D</sup> <sup>⊆</sup> *<sup>D</sup><sup>n</sup>*

<sup>×</sup> *<sup>D</sup>*<sup>U</sup> of each *<sup>p</sup>* <sup>∈</sup> *PF<sup>n</sup>* satisfying the technical *monotonicity*, *antimonotonicity*, and *radicality* requirements given in (López et al., 2006). We use the notation *p*<sup>D</sup> *tn* → *t* to indicate that

*Constraints* over a given constraint domain D are logical statements built from atomic constraints by means of logical conjunction ∧ and existential quantification ∃. *Atomic constraints* can have the form ♦ (standing for *truth*), � (standing for *falsity*), or *p en* →! *t* , meaning that the primitive function *<sup>p</sup>* <sup>∈</sup> *PF<sup>n</sup>* with parameters *en* <sup>∈</sup> *Exp*⊥(U) returns a *total* result *<sup>t</sup>* ∈ *Pat*⊥(U) (i.e, with no occurrences of ⊥). Constraints whose atomic parts have the

<sup>2</sup> Note that (5, 5) can be seen as syntactic sugar for (*pair* 5 5), *pair* being a constructor for ordered pairs.

*<sup>n</sup>*∈**<sup>N</sup>** *DC<sup>n</sup>* and *FS* <sup>=</sup>

*<sup>n</sup>*∈**<sup>N</sup>** *FS<sup>n</sup>*

U

We consider a *universal signature* <sup>Σ</sup> = �*DC*, *FS*�, where *DC* =

**3.1 Preliminary notions**

called *idempotent*.

(*tn*, *t*) ∈ *p*D.

*<sup>e</sup>* ∈ *Exp*⊥(U) have the following syntax:

**3.2 Constraints over a constraint domain**

*<sup>s</sup>*, *<sup>t</sup>* ∈ *Pat*⊥(U), whose syntax is defined as follows:

form ♦, � or *<sup>p</sup> tn* →! *<sup>t</sup>* with *tn* ∈ *Pat*⊥(U) are called *primitive constraints*. In the sequel, we use the notation *PCon*⊥(D) for the set of primitive constraints over D and *DCon*⊥(D) for the set of user defined constraints over D.

**Example 3. (Constraint Domain** R**)** *The constraint domain* R *has the carrier set D***<sup>R</sup>** *of ground patters over* **R** *and the primitives defined below:*


The set of *valuations* over a constraint domain D is defined as the set *Val*⊥(D) of ground substitutions (i.e., mappings from variables to ground patterns). The semantics of constraints relies on the idea that a given valuation can satisfy or not a given constraint. Therefore, the set of *solutions* of *<sup>π</sup>* ∈ *PCon*⊥(D) can be defined in a natural way as a subset *Sol*D(*π*) ⊆ *Val*⊥(D); see (López et al., 2006) for details. Moreover, the set of solutions of <sup>Π</sup> ⊆ *PCon*⊥(D) is defined as *Sol*D(Π) = *<sup>π</sup>*∈<sup>Π</sup> *Sol*D(*π*).

#### **3.3 Constraint functional-logic programming**

For any given constraint domain D, a *CFLP*(D)-program P is presented as a set of constrained rewrite rules, called *program rules*, that define the behavior of user-defined functions. More precisely, a *constrained program rule R* for *<sup>f</sup>* <sup>∈</sup> *DF<sup>n</sup>* has the form *<sup>R</sup>* : *<sup>f</sup> tn* <sup>→</sup> *<sup>r</sup>* ⇐ <sup>Δ</sup> (abbreviated as *<sup>f</sup> tn* <sup>→</sup> *<sup>r</sup>* if <sup>Δ</sup> is empty) and is required to satisfy the conditions listed below3:


Program defined functions can be higher-order and/or non-deterministic. For instance, the T OY program presented in Example 1 can be viewed as an example of *CFLP*(R)-program written in T OY's syntax. The reader is referred to (López et al., 2006) for more explanations and examples in other constraint domains.

<sup>3</sup> In practice, T OY and similar languages require program rules to be well-typed in a polymorphic type system. However, the *CFLP*(D) scheme can deal also with untyped programs. Well-typedness is viewed as an additional requirement, not as part of progam semantics.

**3.5 Answer collection assertions**

form *<sup>G</sup>* ⇒

computed answers

**3.6 Declarative semantics**

*<sup>t</sup>* = ⊥ or *Sol*D(Π) = <sup>∅</sup>.

rely on a given c-interpretation I over D.

**Definition 1. (Interpretation-Dependent Semantic Notions)**

*(a) Sol*<sup>I</sup> (*π*) = *Sol*D(*π*)*, for any <sup>π</sup>* ∈ *PCon*⊥(D)*.*

In this work we propose to use computation trees for missing answers whose nodes have attached so-called *answer collection assertions*, briefly *aca*s. The *aca* at the root node has the

<sup>129</sup> A Semantic Framework for the Declarative Debugging

*I* = ∅) is the disjunction of computed answers observed by the user. This root *aca* asserts that the computed answers cover all the solutions of the initial goal, and will be regarded as a false statement in case that the user misses computed answers. For example, the root *aca* corresponding to the initial goal *G*fD for program PfD is even (fDiff (gen 1 2)) == true ⇒ stating that this goal has (unexpectedly) failed. The *aca*s at internal nodes in our

Note that *G*� asks for the solutions of the production *ftn* → *t* which satisfy the constraint store *S*. The *aca*s of this form correspond to the intermediate calls to program defined functions *f* needed for collecting all the answers computed for the initial goal *G*. Due to *lazy evaluation*, the parameters *tn* and the result *t* will appear in the most evaluated form demanded by the topmost computation. When these values are functions, they are represented in terms of partial applications of top-level function names. This is satisfactory under the assumption

In this subsection we recall some notions and results on the declarative semantics of *CFLP*(D)-programs which were developed in (López et al., 2006) and are needed for the rest of this work. Given a constraint domain D we consider two different kinds of constrained statements (briefly, *c-statements*) involving partial patterns *<sup>t</sup>*, *ti* ∈ *Pat*⊥(U), partial expressions

1. *c-productions e* → *<sup>t</sup>* ⇐ <sup>Π</sup>, with *<sup>e</sup>* ∈ *Exp*⊥(U) and *<sup>t</sup>* ∈ *Pat*⊥(U), intended to mean that *e* can be evaluated to *t* if Π holds (if Π is empty they boil down to unconstrained productions written as *e* → *t*). As a particular kind of c-productions useful for debugging we distinguish *c-facts f tn* <sup>→</sup> *<sup>t</sup>* ⇐ <sup>Π</sup> with *<sup>f</sup>* <sup>∈</sup> *DFn*. A c-production is called *trivial* iff

2. *c-atoms p en* <sup>→</sup>! *<sup>t</sup>* ⇐ <sup>Π</sup>, with *<sup>p</sup>* <sup>∈</sup> *PF<sup>n</sup>* and *<sup>t</sup>* total (if <sup>Π</sup> is empty they boil down to unconstrained atoms written as *<sup>p</sup> en* →! *<sup>t</sup>* ). A c-atom is called *trivial* iff *Sol*D(Π) = <sup>∅</sup>. In the sequel, we use *ϕ* to denote any c-statement. A *c-interpretation* over D is defined as any set I of c-facts including all the trivial c-facts and closed under D*-entailment*, a generalization of the entailment notion introduced in (Caballero & Rodríguez, 2004) to arbitrary constraint domains. We write I ��D *<sup>ϕ</sup>* to indicate that the c-statement *<sup>ϕ</sup>* (not necessarily a c-fact) is semantically valid in the c-interpretation I. This notation relies on a formal definition given in (López et al., 2006). Now we are in a position to define various semantics notions which

*1. The set of* **solutions** *of <sup>δ</sup>* ∈ *DCon*⊥(D) *is a subset Sol*<sup>I</sup> (*δ*) ⊆ *Val*⊥(D) *defined as follows:*

that no local function definitions are allowed in programs, as it happens in T OY.

*<sup>e</sup>*, *ei* ∈ *Exp*⊥(U), and a finite set <sup>Π</sup> ⊆ *PCon*⊥(D) of primitive constraints:

*<sup>i</sup>*∈*<sup>I</sup> Si* covers all the solutions for the intermediate goal *<sup>G</sup>*� : *ftn* → *<sup>t</sup>* ✷ *<sup>S</sup>*.

*<sup>i</sup>*∈*<sup>I</sup> Si* (written as the *failure symbol* if

*<sup>i</sup>*∈*<sup>I</sup> Si*, asserting that the disjunction of

*<sup>i</sup>*∈*<sup>I</sup> Si*, where *<sup>G</sup>* is the initial goal and

of Wrong and Missing Answers in Declarative Constraint Programming

computation trees have the form *ftn* → *<sup>t</sup>* ✷ *<sup>S</sup>* ⇒

The intended use of programs is to perform computations by solving goals proposed by the user. An *admissible goal* for a given *CFLP*(D)-program must have the form *G* : ∃*U*. (*P* ✷ Δ), where *U* is a finite set of so-called *existential variables* of the goal *G* (the rest of variables in *G* are called *free variables* and denoted by *f var*(*G*)), *P* is a finite conjunction of so-called *productions* of the form *e* → *s* fulfilling the *admissibility conditions* given in (López et al., 2006), with *e* ∈ *Exp*⊥(U) and *<sup>s</sup>* ∈ *Pat*⊥(U) intended to mean that *<sup>e</sup>* can be evaluated to *<sup>s</sup>*, and <sup>Δ</sup> ⊆ *DCon*⊥(D) is a finite conjunction of total user defined constraints. Two special kinds of admissible goals are interesting. *Initial goals*, where *U* and *P* are both empty (i.e., *G* has only a constrained part Δ without occurrences of existential variables), and *solved goals* (also called *solved forms*) of the form *S* : ∃*U*. (*σ* ✷ Π), where *σ* is a finite set of productions *X* → *t* or *s* → *Y* interpreted as the variable bindings of an idempotent substitution and <sup>Π</sup> ⊆ *PCon*⊥(D) is a finite conjunction of total primitive constraints. Finally, a *goal solving system* for *CFLP*(D) is expected to accept a program P and an initial goal *G* from the user, and to obtain one or more solved forms *Si* as *computed answers*. As explained in Section 2, an initial goal *G* for the *CFLP*(R)-program shown in Example 1 can be *intersect* (*head* (*ladder* (20, 20) 50 20)) (*head* (*ladder* (5, 5) 30 40)) (*X*,*Y*) == *<sup>R</sup>* and a computed answer *S* for *G* is *<sup>R</sup>* → *true* ✷ *<sup>X</sup>* ≤ <sup>35</sup> ∧ *<sup>X</sup>* ≥ <sup>20</sup> ∧ *<sup>Y</sup>* ≤ 5.

Goal solving systems can be implementations of *CFLP* languages such as *Curry* (Hanus, 2003) or T OY (López & Sánchez, 1999), or formal *goal solving calculi* including recent proposals such as the *CDNC*(D) calculus (López et al., 2004), which is sound and complete with respect to the declarative semantics discussed in the next subsection, and behaves as a faithful formal model for actual computations in the T OY system.

#### **3.4 Standardized programs and negative theories**

Let P be a *CFLP*(D)-program. Its associated *Negative Theory* P<sup>−</sup> is obtained in two steps. First, each program rule *f tn* → *r* ⇐ Δ is replaced by a *standardized* form *f Xn* → *Y* ⇐ *R*, where *Xn*,*Y* are new variables, *R* = ∃*U*. *R* with *U* = *var*(*R*) \ {*Xn*,*Y*}, and the condition *R* is *X*<sup>1</sup> → *t*1, ..., *Xn* → *tn*, Δ, *r* → *Y*. Next, P<sup>−</sup> is built by taking one *axiom* (*f*)<sup>−</sup> <sup>P</sup> of the form ∀*Xn*,*Y*. (*f Xn* → *Y* ⇒ ( *<sup>i</sup>*∈*<sup>I</sup> Ri*) ∨ (⊥ → *<sup>Y</sup>*)) for each function symbol *<sup>f</sup>* whose standardized program rules are { *<sup>f</sup> Xn* → *<sup>Y</sup>* ⇐ *Ri*}*i*∈*I*. By convention, we may use the notation *Df* for the disjunction ( *<sup>i</sup>*∈*<sup>I</sup> Ri*) ∨ (⊥ → *<sup>Y</sup>*), and we may leave the universal quantification of the variables *Xn*,*Y* implicit. Intuitively, the axiom (*f*)− <sup>P</sup> says that any result computed for *f* must be obtained by means of some of the rules for *f* in the program. The last alternative (⊥ → *Y*) within *Df* says that *Y* is bound to the undefined result ⊥ in case that no program rule for *f* succeeds to compute a more defined result. For example, let PfD be the *CFLP*(H)-program given in Section 2, with the third program rule for fDiff omitted. Then P<sup>−</sup> fD includes (among others) the following axiom for the function symbol *fDiff*:

$$(\mathsf{Diff}\vec{\mathcal{H}})\bar{\mathcal{P}}\_{\mathsf{FD}} : \forall \, \mathsf{L}, \mathsf{F}. \mathsf{(}\mathsf{Diff}\, \mathsf{L} \to \mathsf{F} \Rightarrow$$

$$\exists \mathsf{X}. \left(\mathsf{L} \to [\mathsf{X}] \land \mathsf{X} \to \mathsf{F}\right) \lor$$

$$\exists \mathsf{X}. Y. \mathsf{Zs}. \left(\mathsf{L} \to \left(\mathsf{X} : \mathsf{Y} : \mathsf{Zs}\right) \land \mathsf{X} \mathrel{\mathrel{\scalebox{1.0pt}{ $\mathsf{L}$ }}} \land \mathsf{X} \mathrel{\mathrel{\scalebox{1.0pt}{ $\mathsf{L}$ }}} \to \mathsf{F}\right) \lor$$

$$(\mathsf{L} \to \mathsf{F})\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!$$

8 Will-be-set-by-IN-TECH

The intended use of programs is to perform computations by solving goals proposed by the user. An *admissible goal* for a given *CFLP*(D)-program must have the form *G* : ∃*U*. (*P* ✷ Δ), where *U* is a finite set of so-called *existential variables* of the goal *G* (the rest of variables in *G* are called *free variables* and denoted by *f var*(*G*)), *P* is a finite conjunction of so-called *productions* of the form *e* → *s* fulfilling the *admissibility conditions* given in (López et al., 2006), with *e* ∈ *Exp*⊥(U) and *<sup>s</sup>* ∈ *Pat*⊥(U) intended to mean that *<sup>e</sup>* can be evaluated to *<sup>s</sup>*, and <sup>Δ</sup> ⊆ *DCon*⊥(D) is a finite conjunction of total user defined constraints. Two special kinds of admissible goals are interesting. *Initial goals*, where *U* and *P* are both empty (i.e., *G* has only a constrained part Δ without occurrences of existential variables), and *solved goals* (also called *solved forms*) of the form *S* : ∃*U*. (*σ* ✷ Π), where *σ* is a finite set of productions *X* → *t* or *s* → *Y* interpreted as the variable bindings of an idempotent substitution and <sup>Π</sup> ⊆ *PCon*⊥(D) is a finite conjunction of total primitive constraints. Finally, a *goal solving system* for *CFLP*(D) is expected to accept a program P and an initial goal *G* from the user, and to obtain one or more solved forms *Si* as *computed answers*. As explained in Section 2, an initial goal *G* for the *CFLP*(R)-program shown in Example 1 can be *intersect* (*head* (*ladder* (20, 20) 50 20)) (*head* (*ladder* (5, 5) 30 40)) (*X*,*Y*)

== *<sup>R</sup>* and a computed answer *S* for *G* is *<sup>R</sup>* → *true* ✷ *<sup>X</sup>* ≤ <sup>35</sup> ∧ *<sup>X</sup>* ≥ <sup>20</sup> ∧ *<sup>Y</sup>* ≤ 5.

quantification of the variables *Xn*,*Y* implicit. Intuitively, the axiom (*f*)−

∃*X*. (*L* → [*X*] ∧ *X* → *F*) ∨

(⊥ → *F*))

model for actual computations in the T OY system.

**3.4 Standardized programs and negative theories**

form ∀*Xn*,*Y*. (*f Xn* → *Y* ⇒ (

notation *Df* for the disjunction (

<sup>P</sup>fD : <sup>∀</sup> *<sup>L</sup>*, *<sup>F</sup>*. (*fDiff L* <sup>→</sup> *<sup>F</sup>* <sup>⇒</sup>

omitted. Then P<sup>−</sup>

(*fDiff*) −

Goal solving systems can be implementations of *CFLP* languages such as *Curry* (Hanus, 2003) or T OY (López & Sánchez, 1999), or formal *goal solving calculi* including recent proposals such as the *CDNC*(D) calculus (López et al., 2004), which is sound and complete with respect to the declarative semantics discussed in the next subsection, and behaves as a faithful formal

Let P be a *CFLP*(D)-program. Its associated *Negative Theory* P<sup>−</sup> is obtained in two steps. First, each program rule *f tn* → *r* ⇐ Δ is replaced by a *standardized* form *f Xn* → *Y* ⇐ *R*, where *Xn*,*Y* are new variables, *R* = ∃*U*. *R* with *U* = *var*(*R*) \ {*Xn*,*Y*}, and the condition *R* is *X*<sup>1</sup> → *t*1, ..., *Xn* → *tn*, Δ, *r* → *Y*. Next, P<sup>−</sup> is built by taking one *axiom* (*f*)<sup>−</sup>

standardized program rules are { *<sup>f</sup> Xn* → *<sup>Y</sup>* ⇐ *Ri*}*i*∈*I*. By convention, we may use the

computed for *f* must be obtained by means of some of the rules for *f* in the program. The last alternative (⊥ → *Y*) within *Df* says that *Y* is bound to the undefined result ⊥ in case that no program rule for *f* succeeds to compute a more defined result. For example, let PfD be the *CFLP*(H)-program given in Section 2, with the third program rule for fDiff

fD includes (among others) the following axiom for the function symbol *fDiff*:

∃*X*,*Y*, *Zs*. (*L* → (*X* : *Y* : *Zs*) ∧ *X* /= *Y* ∧ *X* // *fDiff* (*Y* : *Zs*) → *F*) ∨

*<sup>i</sup>*∈*<sup>I</sup> Ri*) ∨ (⊥ → *<sup>Y</sup>*)) for each function symbol *<sup>f</sup>* whose

*<sup>i</sup>*∈*<sup>I</sup> Ri*) ∨ (⊥ → *<sup>Y</sup>*), and we may leave the universal

<sup>P</sup> of the

<sup>P</sup> says that any result

In this work we propose to use computation trees for missing answers whose nodes have attached so-called *answer collection assertions*, briefly *aca*s. The *aca* at the root node has the form *<sup>G</sup>* ⇒ *<sup>i</sup>*∈*<sup>I</sup> Si*, where *<sup>G</sup>* is the initial goal and *<sup>i</sup>*∈*<sup>I</sup> Si* (written as the *failure symbol* if *I* = ∅) is the disjunction of computed answers observed by the user. This root *aca* asserts that the computed answers cover all the solutions of the initial goal, and will be regarded as a false statement in case that the user misses computed answers. For example, the root *aca* corresponding to the initial goal *G*fD for program PfD is even (fDiff (gen 1 2)) == true ⇒ stating that this goal has (unexpectedly) failed. The *aca*s at internal nodes in our computation trees have the form *ftn* → *<sup>t</sup>* ✷ *<sup>S</sup>* ⇒ *<sup>i</sup>*∈*<sup>I</sup> Si*, asserting that the disjunction of computed answers *<sup>i</sup>*∈*<sup>I</sup> Si* covers all the solutions for the intermediate goal *<sup>G</sup>*� : *ftn* → *<sup>t</sup>* ✷ *<sup>S</sup>*. Note that *G*� asks for the solutions of the production *ftn* → *t* which satisfy the constraint store *S*. The *aca*s of this form correspond to the intermediate calls to program defined functions *f* needed for collecting all the answers computed for the initial goal *G*. Due to *lazy evaluation*, the parameters *tn* and the result *t* will appear in the most evaluated form demanded by the topmost computation. When these values are functions, they are represented in terms of partial applications of top-level function names. This is satisfactory under the assumption that no local function definitions are allowed in programs, as it happens in T OY.

#### **3.6 Declarative semantics**

In this subsection we recall some notions and results on the declarative semantics of *CFLP*(D)-programs which were developed in (López et al., 2006) and are needed for the rest of this work. Given a constraint domain D we consider two different kinds of constrained statements (briefly, *c-statements*) involving partial patterns *<sup>t</sup>*, *ti* ∈ *Pat*⊥(U), partial expressions *<sup>e</sup>*, *ei* ∈ *Exp*⊥(U), and a finite set <sup>Π</sup> ⊆ *PCon*⊥(D) of primitive constraints:


In the sequel, we use *ϕ* to denote any c-statement. A *c-interpretation* over D is defined as any set I of c-facts including all the trivial c-facts and closed under D*-entailment*, a generalization of the entailment notion introduced in (Caballero & Rodríguez, 2004) to arbitrary constraint domains. We write I ��D *<sup>ϕ</sup>* to indicate that the c-statement *<sup>ϕ</sup>* (not necessarily a c-fact) is semantically valid in the c-interpretation I. This notation relies on a formal definition given in (López et al., 2006). Now we are in a position to define various semantics notions which rely on a given c-interpretation I over D.

#### **Definition 1. (Interpretation-Dependent Semantic Notions)**

*1. The set of* **solutions** *of <sup>δ</sup>* ∈ *DCon*⊥(D) *is a subset Sol*<sup>I</sup> (*δ*) ⊆ *Val*⊥(D) *defined as follows: (a) Sol*<sup>I</sup> (*π*) = *Sol*D(*π*)*, for any <sup>π</sup>* ∈ *PCon*⊥(D)*.*

intended model will be equivalent for the purposes of this work, as long as the ground c-facts

<sup>131</sup> A Semantic Framework for the Declarative Debugging

The aim of declarative diagnosis of wrong answers is to start with an observed *symptom* of erroneous program behavior, and detect some *error* in the program. The proper notions of

**Definition 3. (Symptoms and Errors)** *Assume* I *is the intended interpretation for program* P*, and consider a solved form S produced as computed answer for the initial goal G by some goal solving*

*2.* P *is* **incorrect** *with respect to* I *iff there exists some program rule* (*ftn* → *r* ⇐ Δ) ∈ P

For instance, the computed answer shown in Example 1 is wrong with respect to the intended model of the program assumed in that example, for the reasons already discussed in Section 2. As illustrated by that example, computed answers typically include constraints on the variables occurring in the initial goal. However, goal solving systems for *CFLP*(D) programs also maintain internal information on constraints related to variables used in intermediate computation steps, but not occurring in the initial goal. Such information is relevant for declarative debugging purposes. Therefore, in the rest of this section we will assume that

Assuming that *S* is a computed answer for an initial goal *G* using a program P, the declarative diagnosis of wrong answers needs a suitable *Computation Tree* (shortly, *CT*) representing the computation. In our setting we will obtain the *CT* from a logical proof P �*CPPC*(D) *<sup>G</sup>* ⇐ *<sup>S</sup>* which derives the statement *G* ⇐ *S* from the program P in the *Constraint Positive Proof Calculus* (shortly *CPPC*(D)) given by the inference rules in Fig. 2. We will say that the

Most of these inference rules have been borrowed from the proof theory of *CRWL*(D), a *Constraint ReWriting Logic* which characterizes the semantics of *CFLP*(D) programs (López et al., 2006). The main novelties in *CPPC*(D) are the addition of rule **EX** (to deal with existential quantifiers in computed answers) and a reformulation of rule **DF**P, which is presented as the consecutive application of two inference steps named **AR***<sup>f</sup>* and **FA***<sup>f</sup>* , which cannot be applied separately. The purpose of this composite inference is to introduce the c-facts *f tn* → *t* ⇐ Π at the conclusion of inference **FA***<sup>f</sup>* , called *boxed c-facts* in the sequel. As we will see, only boxed c-facts will appear at the nodes of *CTs* obtained from *CPPC*(D)-proofs. Therefore, all the queries asked to the user during a declarative debugging session will be about the validity of c-facts in the intended model of the program, which is itself represented as a set of c-facts. We also agree that the premises *<sup>G</sup><sup>σ</sup>* ⇐ <sup>Π</sup> in rule **EX** (resp. <sup>Δ</sup> ⇐ <sup>Π</sup> in rule **DF**P) must be understood as a shorthand for several premises *α* ⇐ Π, one for each atomic *ϕ* in *Gσ* (resp. <sup>Δ</sup>). Moreover, rule **PF** depends on the side condition *Sol*D(Π) ⊆ *Sol*D(*ptn* → *<sup>t</sup>*) which is true iff *<sup>p</sup>*<sup>D</sup> *tn<sup>η</sup>* → *<sup>t</sup><sup>η</sup>* holds for all *<sup>η</sup>* ∈ *Sol*D(Π). Some other inference rules in Fig. 2 have similar

*1. S is a* **wrong answer** *w.r.t* I *(serving as* **symptom***) iff Sol*D(*S*) �⊆ *Sol*<sup>I</sup> (*G*)*.*

*(manifesting an* **error***) that is not valid in* I *(in the sense of Definition 2).*

computed answers *S* include also constraints related to intermediate variables.

**4.2 A logical calculus for witnessing computed answers**

*CPPC*(D)-proof *witnesses* the computed answer.

valid in them are the same.

*system. We define:*

conditions.

symptom and error in our setting are as follows:

of Wrong and Missing Answers in Declarative Constraint Programming

*(b) Sol*<sup>I</sup> (*δ*) = {*<sup>η</sup>* ∈ *Val*⊥(D) |I��D *δη*}*, for any <sup>δ</sup>* ∈ *DCon*⊥(D) \ *PCon*⊥(D)*.*


For primitive constraints one can easily check that *Sol*<sup>I</sup> (Π) = *Sol*D(Π). Moreover, we note that *Sol*<sup>I</sup> (*S*) = *Sol*D(*S*) for every solved form *<sup>S</sup>*.
