5. Delarative Diagnosis in the CLP sheme Alexandre Tessier and Gerard Ferrand LIFO, BP 6759, F-45067 Orleans Cedex 2, Frane : fAlexandre.Tessier, Gerard.Ferrandginria.fr email When a result is omputed but it is onsidered as inorret beause it is not expeted, we onsider that we have a symptom (of error). The symptom may be a wrong answer or a missing answer. The role of diagnosis is to loate an error, that is a limited program fragment responsible for the symptom. The notions of symptom and error have a meaning only w.r.t. some notion of expeted semantis. We onsider only delarative semantis. The user does not need to understand the operational behaviour of the CLP system. Symptom and error are onneted via some kind of tree and the diagnosis amounts to searh for a kind of minimal symptom in this tree. Several searh strategies are possible. The priniples of an implementation are desribed, with a diagnosis session. 5.1 Introdution To intuitively introdue the basi notions of symptom and error let us onsider a toy program in the paradigm lp(FD) : fa(N,F) :- B#=1, aux(N,B,F). aux(N,B,P) :- N#=0, B#=P. aux(N,B,P) :- N#=M+1, C#=N, aux(M,C,P). with an expeted semantis suh as: { f a is the fatorial funtion, that is f a(N; F ) , F = N ! { and aux(N; B; P ) , P = N ! B . So the last lause should be: aux(N,B,P) :- N#=M+1, C#=N*B, aux(M,C,P). For the goal: N#=<2, fa(N,F) we have three omputed answer onstraints whih are F = 1, N = 0. F = 1, N = 1. F = 1, N = 2. A goal is the desription of a relation. Here the expeted relation is: = 1; N = 0 or F = 1; N = 1 or F = 2; N = 2. So this omputed result is onsidered as inorret beause it is not expeted. Let us onsider that we have a symptom (of error). But there are two ways to understand that there is a symptom: F 132 A. Tessier and G. Ferrand Firstly we have a wrong answer, F = 1, N = 2. To be more formal, a way to express that it is a wrong answer is to say that, with respet to the expeted semantis of the program, the following logi formula (of the form: omputed answer onstraint ) goal ) is false : F =1^N =2)N 62^ ( f a N; F ) It is a rst kind of symptom. Seondly we have a missing answer, F = 2, N = 2. Unlike the rst kind of symptom, to be able to say that it is a missing answer we have to take all the omputed answer onstraints into aount: It is missing beause among these omputed answer onstraints we do not see F = 2, N = 2. To be more formal, a way to express that it is a missing answer is to say that, with respet to the expeted semantis of the program, the following logi formula is false : N 62^ ( f a N; F ) ) (F = 1 ^ N = 0) _ (F = 1 ^ N = 1) _ (F = 1 ^ N = 2) (sine with respet to the expeted semantis, for F = 2; N = 2, the left hand side is true but the right hand side is false). It is a seond kind of symptom. So we have two kinds of symptoms: for a question (that is a goal) it is possible to have several omputed answers C1 ; ; Ci ; so we may be interested either in a single answer or in the sequene of all the answers. We onsider that there are two levels of omputation and two kinds of results. At eah level the omputation is nite : At a rst level a result is a single Ci . If Ci is a wrong answer we have a symptom of the rst kind. At a seond level of omputation it is the sequene C1 ; ; Ci ; whih is the result. To be more preise, sine it is a nite omputation, it is a sequene: C1 ; ; Cn (terminated by \no more" answer). Finite failure is the partiular ase where n = 0. If an expeted answer C is missing among C1 ; ; Ci ; we have a symptom of the seond kind. With these intuitive motivations we all positive the rst level, where there is the rst kind of symptom, and we all negative the seond level, where there is the seond kind of symptom ([5.8℄). It is beause we are in a relational paradigm that these two levels are shown to be so dierent. From an intuitive viewpoint symptoms are \aused" by errors. Roughly speaking an error is a limited program fragment responsible for the symptom and the role of diagnosis is to loate the error. For the positive level, in our example, the lause aux(N,B,P) :- N#=M+1, C#=N, aux(M,C,P) is erroneous and is responsible for the wrong answer F = 1, N = 2, that is to say for the positive symptom whih is formalised by the (unexpeted) formula 5. Delarative Diagnosis F =1^N =2)N 62^ ( f a N; F 133 ) but it is possible to give more information about the \ause" of a symptom: In this small example it is easy to understand that the symptom, that is f a(2; 1) whih is false, omes from aux(2; 1; 1), whih omes from aux(1; 2; 1), whih omes from aux(0; 1; 1). But aux(0; 1; 1) is true whereas aux(1; 2; 1) is false. This transition between true and false is through the erroneous lause and the onstraint N = 1; B = 2; P = 1; M = 0; C = 1, sine it is for these values that the body of the erroneous lause is true and its head is false. So the onstraint gives more information about the \ause" of the symptom. We onsider that it is the pair made of this erroneous lause and this onstraint whih is an error (inorretness), alled positive error (positive inorretness) beause it is responsible for the positive symptom. For the negative level the program fragment responsible for the symptom is a \paket of lauses" (all the lauses beginning with a same prediate symbol): the intuitive reason is that some answers are missing beause some lause instanes are missing. In our example, the \paket" aux(N,B,P) :- N#=0, B#=P. aux(N,B,P) :- N#=M+1, C#=N, aux(M,C,P). is erroneous and is responsible for the missing answer F = 2, N = 2., that is to say for the negative symptom whih is formalised by the (unexpeted) formula N 62^ ( f a N; F ) ) (F = 1 ^ N = 0) _ (F = 1 ^ N = 1) _ (F = 1 ^ N = 2) but it is possible to give more information about the \ause" of a negative symptom, like for the positive level. In this small example it is easy to understand that there is again a transition between true and false through the erroneous \paket" and a onstraint: With respet to the expeted semantis, aux(N; B; P ) is true for N = 1; B = 2; P = 2 but it is not possible to have one of the two bodies: either N = 0; B = P or N = M + 1; C = N; aux(M; C; P ) true for some values of M; C . A more formal way to express this is to say that for N = 1; B = 2; P = 2 the formula 9 9 M C (N = 0 ^ B = P ) _ (N = M + 1 ^ C = N ^ aux(M; C; P )) is false w.r.t. the expeted semantis. We onsider again that it is the pair made of the erroneous \paket" and the onstraint N = 1; B = 2; P = 2 whih is an error (inorretness), alled negative error (negative inorretness) beause it is responsible for the negative symptom. The notions of symptom and error have a meaning only w.r.t. some notion of expeted semantis. We onsider only delarative semantis and we presuppose that, during a diagnosis session, the user is able to deide, for some omputed answers, if they are wrong, or if some expeted answer is 134 A. Tessier and G. Ferrand missing. In pratie a omputed answer may be intriate, it is the origin of the presentation problem (stressed by Lloyd [5.5℄) whih may be a diÆulty of delarative diagnosis. However this presupposition is neessary to give a meaning to delarative debugging questions. From a oneptual viewpoint the user behaves like an orale whih is able to deide if something is wrong or missing (it is the same abstrat notion of orale and the same theoretial framework if we an partially replae the user by a system using some form of speiation for the expeted semantis of the program). The notion of positive symptom and positive error omes from Shapiro's seminal work (wrong answer and inorretness, [5.7℄). For the negative side, the notion of negative symptom and negative error does not orrespond to Shapiro's notions (missing answer and insuÆieny) whih need more omplex interation with the orale. Theses notions ome from W. Drabent, S. Nadjm-Tehrani, J. Maluszynski (non ompletely overed atom, [5.2℄). The rest of the hapter is organised as follows. Setion 5.2 denes the theoretial notions of symptom and error. Setion 5.3 denes the proof-trees and the diagnosis sheme. Setion 5.4 desribes the diagnosis algorithms. Setion 5.5 links the omputation of a result whih is a symptom with the prooftree used in the diagnosis sheme. Setion 5.6 desribes an implementation. Setion 5.7 shows a diagnosis session. Setion 5.8 presents a onlusion and future work. 5.2 Basi Notions of Symptom and Error We use the basi theoretial notions of the CLP Sheme ([5.4℄). D is a onstraint domain. We onsider only denite programs, that is to say without negation (thanks to disequations and global onstraints, negation as failure is less useful than in lassial Prolog). A program P is supposed to be normalised in suh a way that only distint variables are allowed as prediate arguments (all the links between variables are expressed by the onstraints). It is a faility to simplify the explanation but it is not a loss of generality of the diagnosis method. An interpretation I for the language of the program is supposed to be given. It is a formalisation of the expeted semantis of the program. I must be an expansion of D, that is to say that it adds to D an interpretation for the new prediate symbols. The following denitions are intuitively motivated by the previous introdution. Denition 5.2.1. symptom (of false in I . P C ! G w.r.t. I ) if (C onstraint, G goal) is a omputed positive is a omputed answer for G, but C ! G is C 5. Delarative Diagnosis 135 Denition 5.2.2. A positive error (positive inorretness) (of P w.r.t. I ) is a pair made up of a lause p(X ) B in P and a onstraint C suh that, for some solution of C , B is true in I but p(X ) is false in I . In some logi formulas we use the following notation : 9 A F means quantiation over the variables of F whih have no ourrene in the expression A. In the program P a \paket" of lauses ( ) p X B1 ::: ( ) p X B m is the set of all the lauses of P beginning with a same p. The \paket" is also alled the denition of the prediate p. The ompleted denition of the prediate p is the formula ( ) $ 9 p(X ) (B1 _ _ Bm ) It an be rewritten as p X [p(X ) 9 X (B1 _ _ Bm )℄ ^ [p(X ) ! 9 X (B1 _ _ Bm )℄ ( ) 9 X (B1 _ _ Bm ) is merely equivalent to p(X ) (B1 _ _ Bm ), whih is merely equivalent to the \paket" of p. The other diretion: p(X ) ! 9 X (B1 _ _ Bm ) annot be simplied so muh but it is going to be a useful notation. F I (P ) (only-if(P)) is the set of the formulas p X ( ) ! 9 X (B1 _ _ Bm ) Remark that we ould also mention I F (P ), the set of the formulas p X ( ) p X 9 X (B1 _ _ Bm ) but it is merely equivalent to P . Usually the set of the ompleted denitions is denoted by P , it is equivalent to I F (P ) ^ F I (P ). Denition 5.2.3. G ! 9 G (C1 _ _ Cn ) (Ci onstraints, G goal) is a omputed negative symptom (of P w.r.t. I ) if for the goal G there exists a nite SLD-tree whose omputed answer onstraints are C1 ; ; Cn , but G ! 9 G (C1 _ _ Cn ) is false in I . Note that in the partiular ase n = 0 there is a nite failure and in suh a ase (C1 _ _ Cn ) is merely false so G ! 9 G (C1 _ _ Cn ) is :G. Denition 5.2.4. A negative error (negative inorretness) (of P w.r.t. I ) is a pair made up of p(X ) ! 9 X (B1 _ _ Bm ) in F I (P ) and a onstraint C suh that, for some solution of C , p(X ) is true in I but 9 X (B1 _ _ Bm ) is false in I . 136 A. Tessier and G. Ferrand In the rest of this setion we explain why, if there is a omputed positive (resp. negative) symptom then there is a positive (resp. negative) error. It is a short and easy veriation but this result is purely logial and non onstrutive. In the next setion we set out a renement of this result giving more information about the onnetion between symptom and error. Positive level: At rst we reall the basi soundness result ([5.4℄): If C is a omputed answer onstraint for the goal G then P j=D C ! G, that is to say C ! G is true in all the expansions of D whih are models of P . With regard to the question of the appliability to CLP systems with inomplete solvers, it is interesting to remark that, for this kind of soundness, neither orretness nor ompleteness is required for the solver (intuitively: if an inomplete solver does not rejet C , if C is unsatisable then C ! G is true. If an inorret solver rejets C , C is not a omputed answer, even if it is satisable). Let C ! G be a omputed positive symptom. It is false in I so, thanks to the previous soundness result, I is not a model of P so there is in P some lause p(X ) B whih is false in I , so for some assignment a in D, (namely for some values in D), B is true in I but p(X ) is false in I . Suh a ould give some information about the \ause" of symptoms. To have a positive error it is suÆient to take a C suh as a is solution of C . There is always suh a C , e.g. true. However the more preise C is, the more informative it is. Negative level: Similar explanation, using another basi soundness result: If for the goal G there is a nite SLD-tree whose omputed answer onstraints are C1 ; ; Cn , then F I (P ) j=D G ! 9 G (C1 _ _ Cn ). It is interesting to remark that now, for this kind of soundness, orretness (but not ompleteness) is required for the solver (intuitively: a satisable Ci annot be removed from the onlusion of the impliation, but an unsatisable Ci an be added). 5.3 Connetion between Symptom and Error via Proof-Trees Now we onsider a notion of symptom whih is more general than a omputed symptom. Intuitively in these symptoms the onstraint may be more informative than in a omputed symptom. Denition 5.3.1. C 0 symptom (of is false in I . P ^ C w.r.t. I ) if (C; C 0 onstraints, G goal) is a positive is a omputed answer for G, but C 0 ^ C ! G ! C G So if C 0 = true the symptom is a omputed symptom. Likewise: Denition 5.3.2. C 0 ^ G ! 9 G(C1 _ _ Cn ) (C 0 ; Ci onstraints, G goal) is a negative symptom (of P w.r.t. I ) if for the goal G there exists a nite 5. Delarative Diagnosis SLD-tree whose omputed answer onstraints are C1 ; 9 G (C1 _ _ Cn ) is false in I . ; C 137 n , but C 0 ^ G ! At eah level of omputation, we an get a logial representation of the omputation as a tree. This tree is a proof-tree aording to some rules as usual in logial formalisms. The rules and the proof-trees are not the same for the two levels of omputation but the diagnosis sheme is the same and it is easy to explain it beause at eah level the diagnosis amounts to searh for a kind of minimal symptom in this tree. To learly explain what are the rules and the proof-trees we use a new toy theoretial example: The program is ( ) ( ) q (X ) r (X ) ( ) ( ) 0: X < 0: X < 1: p X q X ;r X : q X X > and the goal is p(X ). At the positive level a omputation is a SLD-derivation. In our example for the goal p(X ) there is a SLD-derivation giving the omputed answer X > 0 ^ X < 1. We an onsider this omputation as a proof of the formula X > 0 ^ X < 1 ! p(X ) and to be more preise we an onsider the omputation as the onstrution of the following tree: 1!X <1 1 ! r(X ) X X > 0 ^ X < 1 ! X > 0 ^ r (X ) X > 0 ^ X < 1 ! q (X ) ^ r (X ) X > 0 ^ X < 1 ! p(X ) Suh a tree is made of rules and is alled a proof-tree aording to these rules. There are two kinds of rules: 0!X >0 > 0 ! q (X ) X > X < X < { For eah lause p(X ) B in P , a \program" rule 0 C ! B 0 C ^ C ! C ^ p(X ) { and the \logial" rules. In our framework it is onvenient to onsider all the rules (C1 ! G1 ); ; (Cn ! Gn ) C ! G where C ! G is a logial onsequene of (C1 ! G1 ); ; (Cn ! Gn ). These rules are alled logial rules. For example we an get the previous proof-tree with the two following rules: 138 A. Tessier and G. Ferrand C C 0 0 C ! C ^ G 0 0 C ! G ^ G ! G ! C But from the same omputation giving the same answer we an also extrat another proof-tree: X > 0^ X < 1 ! X > 0 X > 0^ X < 1 ! X < 1 X > 0 ^ X < 1 ! q (X ) X > 0 ^ X < 1 ! r (X ) X > 0 ^ X < 1 ! q (X ) ^ r (X ) X > 0 ^ X < 1 ! p(X ) We an get this seond proof-tree with the \program rule" and the following \logial" rules: C ! ! ! ^ ^ G1 C C ^ C G1 C1 ^ ^ C n G G n n!C So for eah omputed answer onstraint C for a goal G, the formula C ! G is the root of various proof-trees, alled (positive) proof-trees, aording to these various rules, and eah of these proof-trees an be easily obtained from the omputation namely from the orresponding SLD-derivation. In partiular if C ! G is a omputed (positive) symptom then it is the root of various (positive) proof-trees. Let us onsider suh a proof-tree. Eah node of this proof-tree is labelled by a C 0 ! G0 . It may be a symptom node (the root is a symptom node). Let us onsider the notion of minimal symptom node where \minimal" is dened w.r.t the binary relation: x hild of y . So a node is a minimal symptom node if it is a symptom node but no hild of it is a symptom node. To eah node of the proof-tree there is a rule whih is assoiated (the label C ! G of the node is the onlusion of the rule). Clearly in a \logial" rule, if the hypotheses are not symptoms then the onlusion is not a symptom. So the rule whih is assoiated to a minimal symptom node annot be a \logial" rule so neessarily it is a \program" rule C ^ C C 0!B 0 ! C ^ p(X ) Moreover, in this program rule, for some solution of C ^ C 0 , p(X ) is false in 0 0 I (sine C ^ C ! C ^ p(X ) is a symptom), but B is true in I (sine C ! B 0 is not a symptom), so the lause p(X ) B and the onstraint C ^ C of this program rule give a positive error. 5. Delarative Diagnosis 139 Clearly there are always minimal symptoms (sine proof-trees are nite) and any way to nd a minimal symptom in a positive proof-tree gives the loalisation of a positive error. Moreover, to nd a minimal symptom the following method is suÆient: To onsider only onlusions of program rules (nodes of the form C ^ C 0 ! C ^ p(X )), and to searh for minimal symptoms with respet to these nodes. So we have to test formulas C ^ C 0 ! C ^ p(X ) for symptoms. But suh a test amounts to querying (the orale) whether 0 C ^ C ! p(X ) is expeted, namely is true in I . At the negative level a omputation is a nite SLD-tree. In our example for the goal p(X ) there is a nite SLD-tree giving the omputed answers (X > 0 ^ X < 1) and (X < 0 ^ X < 1). We an onsider this omputation as a proof of the formula p(X ) ! (X > 0 ^ X < 1) _ (X < 0 ^ X < 1) and to be more preise we an onsider the omputation as the onstrution of the following tree: X > 0 !X > 0 X < 0 !X < 0 X <1!X <1 X<1!X<1 q(X ) ! X > 0 _ X < 0 X > 0 ^r(X ) !X> 0 ^X < 1 X < 0 ^r(X ) !X< 0 ^X < 1 q(X ) ^ r(X ) ! (X > 0 ^ X < 1) _ (X < 0 ^ X < 1) p(X ) ! (X > 0 ^ X < 1) _ (X < 0 ^ X < 1) Suh a tree is made of rules and is alled a proof-tree aording to these rules. There are two kinds of rules (with, as usual, appropriate onditions on the free variables, whih are not detailed here): { For eah p(X ) ! 9 X (B1 _ _ Bm ) in F I (P ), a \program" rule W for i : Bi ! 9 Bi j Cji W W i C ^ p(X ) ! 9 Cp(X ) i j C ^ Cj { and \logial" rules, for example G !9 W G i Ci G C ! ^ 0 ! 9 Ci G for i : CiW^ G W 0 ! 9 GG G 0 i i j Cj 0 W i j Cj C So if for a goal G there is a nite SLD-tree whose omputed answer onstraints are C1 ; ; Cn , then the formula G ! 9 G (C1 _ _ Cn ) is the root of various proof-trees, alled (negative) proof-trees, aording to these rules, and eah of these proof-trees an be easily obtained from the omputation namely from the orresponding SLD-tree ([5.3℄, [5.6℄). In partiular if G ! 9 G (C1 _ _ Cn ) is a omputed (negative) symptom then it is the root of various (negative) proof-trees. Let us onsider suh a proof-tree, in whih eah node is labelled by a G0 ! 9 G (C10 _ _ Cn0 ). A node may be a symptom node (the root is a symptom node). Let us onsider the notion of minimal symptom node where \minimal" is dened w.r.t the 0 140 A. Tessier and G. Ferrand binary relation: x hild of y . So a node is a minimal symptom node if it is a symptom node but no hild of it is a symptom node. To eah node of the proof-tree there is a rule whih is assoiated (the label G ! 9 G (C1 _ _ Cn ) of the node is the onlusion of the rule). Clearly in a \logial" rule, if the hypotheses are not symptoms then the onlusion is not a symptom. So the rule whih is assoiated to a minimal symptom node annot be a \logial" rule so neessarily it is a \program" rule for i : Bi ! 9 Bi j Cji W W i C ^ p(X ) ! 9 Cp(X ) i j C ^ Cj W MoreoverWinWthis program rule, for some solution of C , sine C ^Wp(WX ) ! i Cp(X ) i j C ^ Cj is a symptom,Wp(X ) is true in I but 9 Cp(X ) i j C ^ i i Cj is false in I . But the Bi ! 9 Bi j Cj are not symptoms. Let us suppose W 9 X (B1 _ _ Bm )Wis true in I , then, for some i, 9 X Bi is true, so 9 Bi j Cji W is true, so 9 Cp(X ) i j C ^ Cji is true whih is a ontradition. So 9 X (B1 _ _ Bm ) is false in I . So p(X ) ! 9 X (B1 _ _ Bm ) and the onstraint C of this program rule give a negative error. Clearly there are always minimal symptoms (sine proof-trees are nite) and any way to nd a minimal symptom in a negative proof-tree gives the loalisation of a negative error. Moreover, to nd a minimal symptom the following method is suÆient: To onsider only onlusions of program rules (nodes W W of the form C ^ p(X ) ! 9 Cp(X ) i j C ^ Cji ), and to searh for minimal symptoms with respet to theses nodes. Suh a minimal symptom an be found W W by testing if some C ^ p(X ) ! 9 Cp(X ) i j C ^ Cji are symptoms. But this W W test amounts to querying (the orale) whether C ^ p(X ) ! 9 Cp(X ) i j Cji is expeted, namely is true in I . 9 5.4 Diagnosis Algorithm So for eah level of omputation we have a notion of proof-tree. And any way to nd a minimal symptom in a proof-tree gives the loalisation of an error. Let us onsider a proof-tree rooted by a (omputed) symptom. The diagnoser queries the orale about the labels of the nodes of the proof-tree, it is a test: \is this a symptom?". The orale does not need to understand the operational behaviour of the system. The objetive of the diagnoser is to loate a minimal symptom (a minimal symptom node). With this end in view the diagnoser uses a strategy in order to hoose the node whih orresponds to the next query to the orale. Let us assume that eah node of the proof-tree an be either expeted (it is not a symptom) or unexpeted (it is a symptom) or unknown (it is not yet determined). The diagnosis algorithm is the following: 5. Delarative Diagnosis 141 while no minimal symptom appears in the proof tree hoose an unknown node aording to some strategy query the orale about the hosen node in order to determine if it is expeted or unexpeted show the error assoiated with a minimal symptom It is easy to show that this algorithm is orret and that it is omplete in the sense that it always founds an error (the proof-tree is nite). In general the orale is the user and we have to take into aount the fat that the user annot answer some queries of the diagnoser. Thus we have a fourth ategory of nodes: the dontask nodes (the orale annot determine if it is a symptom). It is trivial to see that this possibility involves that the diagnoser is not always able to loate a minimal symptom. Moreover it involves that the strategies used to hoose a node are more intriate and sometimes annot hoose a node. As an example, we desribe here the strategies Top-Down and Divide&Query whih are the most useful in pratie. The Top-Down strategy follows a path of unexpeted nodes from the root until it reahes an unknown node. This unknown node is the hosen node. Beause of the dontask nodes, this strategy does not always nd a node to query (see Fig 5.7). It looks like a prex traversal of the proof-tree where subproof-trees rooted by an expeted or dontask node are removed (see Fig. 5.1). The relation topdown(N ode; C hosenN ode) provides the node hosen (C hosenN ode) by the Top-Down strategy in the tree rooted by N ode. If the strategy annot onlude C hosenN ode = notf ound. ( ) if N ode is unknown then N ode = C hosenN ode else if N ode is unexpeted then topdown0 (hildren(N ode); C hosenN ode) else C hosenN ode = notf ound topdown N ode; C hosenN ode 0 ([℄; notf ound) 0 topdown ([N odejC hildren℄; C hosenN ode) topdown(N ode; C hosenN ode0 ) if C hosenN ode0 = notf ound then topdown0 (C hildren; C hosenN ode) else C hosenN ode = C hosenN ode0 topdown Fig. 5.1 shows an example of proof-tree and the node hosen by the TopDown strategy. The proof-tree is drawn as usual: the root of the proof-tree is at the top of the drawing, the parent relation of the proof-tree links a node whih is the onlusion of a rule with its hypotheses whih are its hildren on the drawing. In the drawing the following notation are used: unexpeted 142 A. Tessier and G. Ferrand nodes are labelled by \N", expeted nodes by \Y", dontask nodes by \X" and unknown nodes by \?"). N N Y X N X ? ? Y N N ? choosen node ? ? ? ? N ? ? ? ? ? Y Y ? X Y ? ? Fig. 5.1. Top-Down Strategy The basi priniple of the Divide&Query strategy is to hoose a node in order to divide the searh spae in two parts: { if the node is expeted, the subtree rooted by this node an be removed from the searh spae, { if the node is unexpeted, eah node whih is not a desendant of this node an be removed from the searh spae. The point is to hoose a node suh that there is as muh unknown nodes in its subtree as out of its subtree (onsidering that all subtrees rooted by an expeted node are removed from the searh spae). Note that it is not always possible to have as muh unknown nodes in the subtree as out of it, but the strategy hooses the nodes whih is the most near this ondition. The Divide&Query strategy here is an improvement of the Divide-andquery strategy of [5.7℄. It really hooses the best node (that is the node whih better divides the searh spae in two parts) and it takes into aount dontask nodes and the fat that the strategy may be hanged during a diagnosis session. The tree onsidered is the tree rooted by an unexpeted node with the least number of unknown and dontask nodes, but where a minimal symptom is always possible (remember the dontask nodes). In Fig. 5.2 we use the same notations as in the previous example. In addition, the integer at the left of the nodes is the number of unknown or dontask nodes in their subtrees. The 1st subtree and the 4th subtree are not onsidered beause of the dontask nodes: if the unknown nodes are expeted no minimal symptom an be deteted. The 3rd subtree is onsidered beause the number of unknown nodes is lower than in the 2nd subtree. The 3rd subtree has 5 unknown nodes so the Divide&Query strategy hooses the root of the 5. Delarative Diagnosis 143 subtree with 3 unknown nodes: when the user answer the query the subtree will have 2 unknown nodes inside and 2 unknown nodes outside ((5 1)=2). 15 N 11 N 2 N 2 N 0 Y 2 X 6 N choosen node 1 X 1 ? 6 ? 3 ? 0 Y 1 ? 3 ? 1 ? 1 ? 1 ? 5 N 1 X 2 ? 1 ? 1 ? 0 Y 0 Y 1 ? 1 ? 0 Y 1 ? 1 ? Fig. 5.2. Strategy Divide&Query It is possible to build pathologial ases (where whatever node you hoose there is few nodes in one side and a lot of nodes in the other side) but in pratie this strategy queries about log2 (N ) nodes (where N is the number of unknown nodes). For instane, in a proof-tree with 1,000,000 nodes there is less than 20 queries (try to nd an error without delarative diagnosis!). 5.5 Abstrat Proof-Trees As said previously, only the nodes whih orresponds to onlusion of program rules an be minimal symptom nodes. So, in order to derease the size (number of nodes) of proof-trees, we dene abstrat proof-trees. Let us assume that a proof tree is given, the abstrat proof tree whih orresponds to the proof tree is dened as follow: 1. the root of the abstrat proof-tree is the root of the proof-tree, 2. y is a hild of a node x in the abstrat proof-tree if { y is the onlusion of a program rule in the proof tree, { y is a desendant of x in the proof-tree, { and there is no other onlusion of program rules between x and the proof-tree. y in The abstrat proof trees A whih orresponds to a proof tree B an be also dened as an abstration of the proof tree B in the sense of Chapter 8: all the onlusion of program rules are seleted (in addition of the root). In the following we say proof-tree instead of abstrat proof-tree. 144 A. Tessier and G. Ferrand The user invokes the diagnoser when a (positive or negative) symptom appears at the end of a (positive or negative) omputation. The omputation is either a SLD-derivation (a branh of a SLD-tree) or a SLD-tree. But the diagnoser uses a proof-tree! The point is that it is possible to ompute diretly a (positive or negative) proof-tree rooted by the (positive or negative) omputed symptom from a (positive or negative) omputation, that is from (a branh of) a searh-tree. In order to simplify, the omputation rule is assumed to be without oroutining (for instane the standard omputation rule of Prolog), then prooftrees an be dedued from searh-tree using the notion of erasing dened below. [5.6℄ shows an extension to any omputation rule. The main useful notion is: let x and y be two nodes of the searh-tree, y is a node where x is erased if { Ai is the atom seleted at the node x in its goal (the goal on x is A1 ; : : : ; Ai ; : : : ; An and the store is C ), { y is a desendants of x where Ai is fully solved (the goal on y is 0 0 A1 ; : : : ; Ai 1 ; Ai+1 ; : : : ; An and the store is C ^ C where C is a omputed answer to Ai ). Let x be a node of the searh-tree, we denote by { { { { ( ) the set of onstraints aumulated from the root until the node x, ( ) the atom seleted at the node x in its goal, erased(x) the set of nodes where x is erased, sthildren(x) the set of hildren of x in the searh-tree. store x selet x Let C be a omputed answer onstraint for the goal G suh that C ! G is a positive symptom. C is made of the onstraints aumulated along a suess branh of the searh-tree. The set of nodes of this branh is denoted by branh. In order to dene the positive proof tree whih orresponds to the positive symptom omputed, we have to determine: 1. the root of the proof-tree; 2. the binary relation: x hild of y in the proof-tree; 3. the formula (query) whih labels a node of the proof-tree. We onsider, in order to simplify, that the nodes of the proof-tree are a subset of the nodes of the searh-tree, but the labels of a node is dierent depending on whether it is onsidered as a node of the proof-tree or a node of the searh-tree. First, the root of the positive proof-tree is the root of the searh-tree. Seondly, the list L of hildren of a node x of the positive proof-tree is given by the relation +hildren: 5. Delarative Diagnosis 145 +hildren (x; L) fy g = sthildren(x) \ branh if y 2 erased(x) then L = [℄ else + hildren0 (x; y; L1); L = [y jL1℄ 0(x; y; L) +hildren fz g = erased(y ) \ branh if z 2 erased(x) then L = [℄ else + hildren0 (x; z; L1); L = [z jL1℄ Finally, the formula assoiated with a node x of the positive proof-tree is store(y ) ! selet(x) where y is the suess leaf of branh. We use the onstraint store(y ) in the formula beause it is the most preise we an know, but as said in Setion 5.3 there exists various proof-trees. For example, let us onsider the small program (without onstraints and variables to be more onise, in other words the onstraints are always true): p q; r: p q; a: r a: q w: w: a z: a: The positive proof-tree whih orresponds to the rst answer to the goal p is given by Fig. 5.3. The nodes of the searh-tree used by the proof-tree have been dupliated for the legibility of the drawing. The seleted atom in the goals is underlined in the searh-tree. For example, p has two hildren in the positive proof-tree: its hild q in the searh-tree, the node r where q is erased. Note that the node where r is erased is also the node where p is erased. On Fig. 5.3 you an reognise a more lassial notion of proof-tree in logi programming (a node orresponds to the head of a lause of the program and its hildren orrespond to the body of the lause). Let C1 ; : : : ; Cn be the omputed answer onstraints for the goal G suh that G ! 9 G (C1 _ _ Cn ) is a negative symptom. In order to dene the negative proof tree whih orresponds to the negative symptom omputed, we have to determine: 1. the root of the proof-tree; 2. the binary relation: x hild of y in the proof-tree; 3. the formula (query) whih labels a node of the proof-tree. 146 A. Tessier and G. Ferrand Search Tree Positive Proof Tree p p q,r q,a q r w,r w,a w a r a a z First Positive Answer z Fig. 5.3. From a branh of the searh-tree to the positive proof-tree. First, the root of the negative proof-tree is the root of the searh-tree. Seondly, the list L of hildren of a node x of the negative proof-tree is given by the relation hildren: hildren(x; L) let S be the list of nodes of (sthildren(x) n erased(x)) hildren0 (x; S; L) 0(x; [℄; [℄) 0 hildren (x; [y jS ℄; [y jL℄) hildren00 (x; y; L1) hildren0 (x; S; L2) hildren append L ; L ; L ( 1 2 ) hildren 00 (x; y; L) let S be the list of nodes of (erased(y ) n erased(x)) 0 hildren (x; S; L) Finally, the formula assoiated with a node x of the negative proof-tree W is store(x) ^ selet(x) ! y2erased(x) store(y ). For example, let us onsider again the previous program: p q; r: p q; a: r a: q w: w: a a: z: 5. Delarative Diagnosis 147 The negative proof-tree whih orresponds to the searh-tree for the goal p is given by Fig. 5.4. The nodes of the searh-tree used by the proof-tree have been dupliated for the legibility of the drawing. The seleted atom in the goals is underlined in the searh-tree. For example, p has four hildren in the negative proof-tree: its hildren q and q in the searh-tree, the node r where the rst q is erased, the node a where the seond q is erased. Note that the nodes where r and a are erased are also the nodes where p is erased. Search Tree Negative Proof Tree p p q,r q,a q r q a w,r w,a w a w z r a a z z z Fig. 5.4. From the searh-tree to the negative proof-tree. 5.6 Implementation The positive part of the delarative diagnoser has been implemented and tested on the INRIA platform: TkCalypso, developed in the DiSCiPl projet. At the time of writing this book, implementation of the negative part is in progress. TkCalypso is an extension of GNU-Prolog [5.1℄. It inludes a graphial interfae (Fig. 5.5) and some debugger modules. Eah module an be plugged or unplugged. Fig. 5.6 shows the struture of TkCalypso with the three modules that are atually implemented: searh-tree visualisation, stati debugger and delarative diagnoser. Communiations between GNU-Prolog and the modules and the graphial interfae are handled by the \Core/Gestionnary" pakages. This setion desribes the main features of the module alled \Delarative Diagnoser". When a goal is given to TkCalypso, it stores informations on the searhtree in order to reompute it eÆiently and make post-mortem analysis of the searh-tree. 148 A. Tessier and G. Ferrand Fig. 5.5. Graphial interfae: a positive symptom GNU-Prolog GUI (Tcl/Tk) Core Search Tree Gestionnary Static Debugger Declarative Diagnoser Fig. 5.6. Arhiteture of the TkCalypso platform. 5. Delarative Diagnosis 149 If the user noties a omputed symptom, then the delarative diagnoser is alled and the parent relation of the (positive or negative) proof-tree is omputed dynamially from the searh-tree. One we have a (positive or negative) proof-tree, the diagnosis priniple is always the same: hoose a node of the proof-tree (aording to some strategy), hek if it is expeted or not expeted, until a minimal symptom is founded. Several strategies have been implemented in order to hoose the node to query: Top-Down, Bottom-Up, Divide&Query, Nearby-Error and UserGuided (Top-Down and Divide&Query are desribed in Setion 5.4). Built-in prediates are known as orret prediates, but also user prediates ould be known as orret. For example, when the stati analysis (see Chapter 2) has proved their orretness. Another example is when the user is onvined that some prediates are orret. So the user an set a list of orret prediates, whih will be used by the diagnoser. It is possible that the user does not want to be questioned on some prediates. For example, the semantis of the prediate is very intriate and the user want to suspend the queries on that prediate as long as possible. So the user an set a list of prediates whih must not be questioned. The status of a node of the proof-tree is more preise that the ones desribe in Setion 5.4, beause we want to know the origin of this status, it an be: { { { { { { { when the prediate assoiated to the node is not a built-in and the node has not been queried, expeted(user ) when the user said that the node is expeted, expeted(list) when the prediates assoiated to the node is in the list of orret prediates (when the user is onvined that the prediate is orret), expeted(system) when the prediate assoiated to the node is a built-in prediate (the are not suspeted!), unexpeted(user ) when the user said that the node is not expeted, dontask (user ) when the user does not want to answer the query assoiated to the node (for example the query is very intriate), note that the user an ome bak later to the query assoiated with this node, dontask (list) when the prediates assoiated to the node is in the list of prediates that the user does not want to answer (for example the user does not know the semantis of the prediates or the user wants to delay the queries about the prediate). unknown The user has the possibility to dynamially add some prediates to the list of orret prediates or remove some prediates from it. If the user adds a prediate, eah unknown or dontask ( ) node onerning the prediate is labelled by expeted(list); if a node is unexpeted(user) then the problem is given to the user: either remove the prediate from the orret prediate list or label the node as expeted(user). When the user removes a prediate of the list, eah expeted(list) node onerning the prediate are labelled by unknown. 150 A. Tessier and G. Ferrand The user an also hange dynamially the list of prediates whih must not be questioned. If the user adds a prediate to the list, eah unknown node onerning the prediate beomes a dontask (list) node. If the user removes a prediate of the list, eah dontask (list) node onerning the prediate beomes an unknown node. Fig. 5.7. Delarative Diagnoser Interfae The graphial interfae (see Fig.5.7) of the diagnoser works like an hypertext navigator: the user an navigate between queries, beause its answers are seen as hyper-link between queries (it is possible to go bak, go forward, see an history...). The proof-tree is displayed with informations about the nodes. For example, the user an see the status of a node { with olours: red = unexpeted, grey = unknown, blue = dontask, green = expeted, { and with shades: light = system, medium = user, dark = list. The user an also hoose the query with the mouse on the proof tree: it is the \User Guided" strategy. 5. Delarative Diagnosis 151 The query C ! A is displayed as A :- 9 A C . The diagnoser uses a trivial store simpliation in order to simplify the onstraint 9 A C in the query. But this point has to be improved (this is disussed later in Setion 5.8). 5.7 A Diagnosis Session Let us onsider the following QuikSort program whih is intended to sort a list of nite domain terms: qs([℄, [℄). qs([Pivot | List℄, SortList) :partition(Pivot, List, MinList, MaxList), qs(MinList, SortMinList), qs(MaxList, SortMaxList), append([Pivot|SortMinList℄, SortMaxList, SortList). partition(_, [℄, [℄, [℄). partition(Pivot, [X | List℄, [X | MinList℄, MaxList) :X #< Pivot, partition(Pivot, List, MinList, MaxList). partition(Pivot, [X | List℄, MinList, [X | MaxList℄) :X #> Pivot, partition(Pivot, List, MinList, MaxList). In the expeted model of qs(A,B) A is a list of distint nite domain terms, B is a permutation of A and B is an inreasing list (for example X < 5 ! qs([5; X; 7℄; [X; 5; 7℄) is expeted). In the expeted model of partition(A, B, C, D) A is a nite domain term, B is a list of nite domain terms, C is the list of members of B whih are lower than A, D is the list of members of B whih are greater than A, B is obtained by a fusion of C and D. Fig. 5.5 shows that for the goal qs([12,X,Y,6,Z℄,L) the rst answer is: L X Y Z = = = = [12, #2(8..11), #23(7..10), 6, #44(0..5)℄ #2(8..11) #23(7..10) #44(0..5) (X = #2(8..11) means that X is a nite domain variable whose domain is 8::11). It is a positive symptom: L is not an inreasing list. So we all the positive delarative diagnoser module of TkCalypso and a new window appears on the sreen, with a drawing of the positive proof tree. The diagnoser queries the user on some nodes of the proof-tree. For example in Fig. 5.8, the user will answer (lik on) \Not Expeted" beause the last onstraint in the store is #44(0..5) #< 6, so [6, #44(0..5)℄ is not an inreasing list. The store simpliation has been disabled on the gure and we see that without simpliation the store quikly beomes unreadable. 152 A. Tessier and G. Ferrand Fig. 5.8. A query 5. Delarative Diagnosis 153 After several queries the diagnoser nds a minimal symptom and shows the orresponding error. Fig 5.9 shows an error, rst it displays the inorret lause and next the error: a lause instane and a onstraint store. The problem is that in append([A|F℄, G, C) the pivot A should be between the list F and the list G: append(F, [A|G℄, C). Fig. 5.9. An error Sometimes it is not easy to x the error provided by the delarative diagnoser, but the user is sure that the lause is inorret, so the user does not need to searh elsewhere in the program. Thus delarative diagnosis is very eÆient espeially for large programs (with a lot of lauses) or for large omputations (searh-tree with a lot of nodes). It is also a good tool for eduational purpose. 5.8 Conlusion The main remaining task onerns the interation with the orale. Of ourse, answers to previous queries may be used by the delarative diagnoser in order to automatially answer to some other queries: Let us onsider a query \C ! A expeted?" If it is stored that C 0 ! A is expeted and if C ! C 0 is true then C ! A is expeted. If it is stored that C 0 ! A is unexpeted and if C 0 ! C is true then C ! A is unexpeted. Likewise for the negative side. 154 Referenes The diÆulty is to deide if C ! C 0 is true (or in general C1 _ _ Cm ! 0 0 C1 _ _ Cn ), that is the entailment problem. It is interesting to study how assertions dened in Chapter 1 ould be used to answer to queries of the delarative diagnoser. Then some assertions are viewed as a partial speiation of the expeted semantis of the program. Despite these tehniques it is not possible to ompletely avoid interation with the user. So, works in progress onerns the presentation problem, that is to show queries in an understandable form. Variable elimination, redundant onstraint elimination, onstraint simpliation and approximation may be useful methods to present queries to the user. Referenes 5.1 D. Diaz. A Native Prolog Compiler with Constraint Solving over Finite Domains Edition 1.0, for GNU Prolog version 1.0.0, 1999. http://www.gnu.org/software/prolog/ 5.2 W. Drabent, S. Nadjm-Tehrani, and J. Maluszynski. Algorithmi Debugging with Assertions. In Harvey Abramson and M. H. Rogers, editors, MetaProgramming in Logi Programming, pages 501{522. The MIT Press, 1989. 5.3 G. Ferrand and A. Tessier. Clariation of the bases of Delarative Diagnosers for CLP. Deliverable D.WP2.1.M1.1-1. Debugging Systems for Constraint Programming (ESPRIT 22532), 1997. http://disipl.inria.fr/ 5.4 J. Jaar, M. J. Maher, K. Marriott, and P. J. Stukey. Semantis of Constraint Logi Programs. Journal of Logi Programming, 37(1-3):1{46, 1998. 5.5 J. W. Lloyd. Delarative Programming in Esher. Tehnial Report CSTR-95013, Department of Computer Siene, University of Bristol, 1995. 5.6 B. Malfon and A. Tessier. An Adaptation of Negative Delarative Error Diagnosis to any Computation Rule. Deliverable D.WP2.1.M2.1-1. Debugging Systems for Constraint Programming (ESPRIT 22532), 1998. http://disipl.inria.fr/ 5.7 E. Y. Shapiro. Algorithmi Program Debugging. ACM Distinguished Dissertation. The MIT Press, 1982. 5.8 A. Tessier. Corretness and Completeness of CLP Semantis revisited with (Co)Indution. Deliverable D.WP2.1.M2.1-2. Debugging Systems for Constraint Programming (ESPRIT 22532), 1998. http://disipl.inria.fr/