Techniques for preventing the recomputation in logic programs

In this thesis we investigate techniques for preventing recomputation in the execution of logic programs. One can reduce recomputation by eliminating subquery redundancy. This can be achieved by storing intermediate results, which are shared between identical subqueries. This is known as the tabulation technique. We consider top-down evaluation procedures incorporating a tabulation or `lemmatization' mechanism and also bottom-up evaluation with goal orientation, as exempli ed by the `magic sets' transformation. By assessing these two forms of evaluation and by pointing out some relationships between them we proceed towards a recomputation-free evaluation. Several top-down evaluation strategies that use tabulation have been proposed in the literature. However, all of these mechanisms su er from recomputation whenever new queries require sets of solutions that are partially computed and stored in the tables. This type of recomputation emerges simply because the top-down evaluator fails to distinguish between visited and unvisited search space. We refer to this problem as the Lemma Recomputation Problem. We investigate a tabulation mechanism for top-down evaluation that overcomes the problem by recording together with the lemmas the branches of the search space which have been examined. Later computations of new answers make use of this information to divert the search from earlier visited branches. The original rules of the database are rewritten to yield, jointly with the solutions, the explored branches. The unvisited paths (branches) of the search space are inferred from the explored ones. This approach suggests a family of algorithms for the inference of paths. We implement this technique by introducing a set of program transformations that embed this diversion mechanism into the transformed programs. Several cases are dealt with including one application to the implementation of tuple-oriented tabulation where a speci c transformation is proposed. We present an illustration of this speci c transformation by applying our technique to an example of Event Calculus, a formalism to reason about events and time within a logic programming framework. Finally, the thesis is concerned with identifying relations and di erences between the tabulation technique applied to top-down evaluation and the goal oriented bottom-up computation of logic programs. We describe several sources of redundant computation and discuss a method for removing this redundancy from bottom-up evaluation with the magic sets rewriting. It is pointed out that the full bene ts of tabulation, namely the previously described sharing of answers between identical calls, are not obtained in bottom-up evaluation. A method is proposed to recover tabulation by introducing a new subsumption checking over adorned magic atoms. This feature avoids some of the identi ed recomputation in the bottom-up evaluation. By considering the relations between these two forms of evaluation, we demonstrate that an analogue of the lemma recomputation problem arises in the bottom-up evaluation of magic rewritten programs. A new transformation is proposed on top of this rewriting to overcome the recomputation problem. Acknowledgements To my supervisor, Marek Sergot. Yet again, this huge wonderful gentleman managed to make a successful `product' out of me. To the people who read and commented on drafts of this thesis, namely Marcelo Finger, Kostas Stathis, Rob Miller, John Browning, Nihan Kesim, Mario Florido, Alessandro Provetti, Danilo Montesi and Priscila Lima. A special mention to Aspassia Daskalopulu since she read and commented on most of the thesis. Thanks very much for all the patience and support. To my o cemates Aspassia, Sonia, John, Priscila, Michel and David for providing me with such a nice and supportive environment. To Luis and Maria Helena, for all those nice evening meals at their house in Docklands, and for their support and encouragement during the most di cult times. My friends around the world for providing me with all those nice discussions about work, fun and life in general through email. Among them I include Mario Florido, Alipio Jorge, Juergen Dingel, Alessandro Provetti, Hermann Puhlmann, Fernando Ribeiro and Ruy de Queiroz. To my family, specially my parents, for all the support, kindness and above all for being there whenever I need them. To Junta Nacional de Investiga c~ ao Cient ca e Tecnol ogica (JNICT), my sponsor, which supported me through the CIENCIA program, grant number BD/1297/91-IA , and PRAXIS XXI program, grant number BD/2832/94. Anivers ario No tempo em que festejavam o dia dos meus anos, Eu era feliz e ningu em estava morto. Na casa antiga, at e eu fazer anos era uma tradi c~ ao de h a s eculos, E a alegria de todos, e a minha, estava certa como uma religi~ ao qualquer. No tempo em que festejavam o dia dos meus anos, Eu tinha a grande sa ude de n~ ao perceber coisa nenhuma, De ser inteligente para entre a famil ia, E de n~ ao ter as esperan cas que os outros tinham por mim. Quando vim a ter esperan cas, j a n~ao sabia ter esperan cas. Quando vim a olhar para a vida, perdera o sentido da vida. Sim, o que fui de suposto a mim-mesmo, O que fui de cora c~ ao e parentesco, O que fui de ser~ oes de meia-prov incia, O que fui de amarem-me e eu ser menino, O que fui ai, meu Deus! , o que s o hoje sei que fui. A que distância! (Nem o acho.) O tempo em que festejavam o dia dos meus anos! O que sou hoje e como a humidade no correr do m da casa, Pondo grelado nas paredes. O que eu sou hoje (e a casa dos que me amaram treme atrav es das minhas l agrimas), O que eu sou hoje e terem vendido a casa, E terem morrido todos, E estar eu sobrevivente a mim-mesmo como um f osforo frio. No tempo em que festejavam o dia dos meus anos. Que meu amor, como uma pessoa, esse tempo! Desejo f isico da alma de se encontrar ali outra vez, Por uma viagem metaf isica e carnal, Com uma dualidade de eu para mim. Comer o passado como p~ao de fome, sem tempo de manteiga nos dentes! Vejo tudo outra vez com uma nitidez que me cega para o que h a aqui. A mesa posta com mais lugares, com melhores desenhos na loi ca, com mais copos, O aparador com muitas coisas doces, frutas, o resto na sombra debaixo do al cado -, As tias velhas, os primos diferentes, e tudo era por minha causa, No tempo em que festejavam o dia dos meus anos. P ara, meu cora c~ ao! N~ ao penses! Deixo o pensar na cabe ca! O meu Deus, meu Deus, meu Deus! Hoje j a n~ao fa co anos. Duro. Somam-se-me dias. Serei velho quando for. Mais nada. Raiva de n~ ao ter trazido o passado roubado na algibeira! O tempo em que festejavam o dia dos meus anos! Alvaro de Campos, "Poesias" In memory of Candido Moreira de Sousa (21/7/1914 23/12/1992) King Lear is dead, . the telephone is still ringing, but the dream is on, the dream is on . 4