Answer Set Programming Answer Set Programming Vladimir Lifschitz
Total Page:16
File Type:pdf, Size:1020Kb
Vladimir Lifschitz Answer Set Programming Answer Set Programming Vladimir Lifschitz Answer Set Programming 123 Vladimir Lifschitz Department of Computer Science University of Texas at Austin Austin, TX, USA ISBN 978-3-030-24657-0 ISBN 978-3-030-24658-7 (eBook) https://doi.org/10.1007/978-3-030-24658-7 © Springer Nature Switzerland AG 2019 This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. The publisher, the authors, and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, express or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations. This Springer imprint is published by the registered company Springer Nature Switzerland AG. The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland To the community of computer scientists, mathematicians, and philosophers who invented answer set programming and are working hard to make it better “What constitutes the dignity of a craft is that it creates a fellowship, that it binds men together and fashions for them a common language.” – Antoine de Saint-Exupéry Preface Answer set programming is a programming methodology rooted in research on artificial intelligence and computational logic. It was created at the turn of the century, and it is now used in many areas of science and technology. This book is about the art of programming for CLINGO—one of the most efficient and widely used answer set programming systems available today—and about the mathematics of answer set programming. It is based on an undergraduate class taught at the University of Texas at Austin. The book is self-contained; the only prerequisite is some familiarity with programming and discrete mathematics. Chapter 1 describes the main ideas of answer set programming and its place in the world of programming languages. Chapter 2 explains, in an informal way, several constructs available in the input language of CLINGO. Chapter 3 shows how they can be used to solve a number of computational problems. Chapters 4– 6 put the discussion of answer set programming on a firm mathematical foundation. (Reading Chap. 6 is not necessary for understanding the rest of the book.) Chapter 7 describes a few more programming constructs and gives examples of their use. In Chap. 8, answer set programming is applied to the problem of representing actions and generating plans. Many examples in this book are framed as exercises, and their solutions are presented in the appendix. This organization gives the reader an opportunity to look for a solution on his own and then compare his ideas with the solution proposed in the book. This book, as well as everything else I have written, owes much to my wife, Elena, who heroically accompanied me in long travels around the world in search for an ideal academic environment. Eventually, I found it in the Computer Science Department of the University of Texas at Austin. In my younger years, I was fortunate to meet a few outstanding scientists and to have an opportunity to take their classes and talk to them. I learned logic from Nikolai Shanin (1919–2011), Sergei Maslov (1939–1982), and Grigori Mints (1939–2014) and artificial intelligence from John McCarthy (1927–2011) and Raymond Reiter (1939–2002). The most important influence on my professional work, next to that of my teachers, came from Michael Gelfond, an old friend and vii viii Preface one of the founding fathers of answer set programming. He has read a draft of this book and suggested many ways to improve it. Useful comments have been provided also by Amelia Harrison, Roland Kaminski, Joohyung Lee, and Alfred Zhong and by students who took my classes on answer set programming. Finally, thanks to Susan Evans, senior editor, with the Springer Nature’s Com- puter Science team, who gave me the idea of converting lecture notes into a textbook. Austin, TX, USA Vladimir Lifschitz May 2019 Contents 1 Introduction .................................................................. 1 1.1 Declarative Programming ............................................. 1 1.2 Logic Programming ................................................... 2 1.3 Answer Set Solvers .................................................... 4 1.4 Bibliographical and Historical Remarks.............................. 4 2 Input Language of CLINGO ................................................. 7 2.1 Rules ................................................................... 8 2.2 Directives and Comments ............................................. 10 2.3 Arithmetic .............................................................. 13 2.4 Definitions ............................................................. 15 2.5 Choice Rules ........................................................... 19 2.6 Variables in a Choice Rule ............................................ 22 2.7 Constraints ............................................................. 23 2.8 Anonymous Variables ................................................. 26 2.9 Bibliographical and Historical Remarks.............................. 27 3 Combinatorial Search ....................................................... 29 3.1 Seating Arrangements ................................................. 29 3.2 Who Owns the Jackal? ................................................ 32 3.3 Schur Numbers ........................................................ 32 3.4 Digression on Grounding and Solving................................ 35 3.5 Set Packing............................................................. 37 3.6 Search in Graphs....................................................... 37 3.7 Search in Two Dimensions ............................................ 42 3.8 Partner Units Problem ................................................. 44 3.9 Additional Programming Exercises................................... 45 3.10 Bibliographical and Historical Remarks.............................. 48 ix x Contents 4 Propositional Programs and Minimal Models ............................ 51 4.1 Propositional Formulas ................................................ 52 4.2 Equivalence ............................................................ 54 4.3 Minimal Models ....................................................... 56 4.4 Stable Models of Positive Propositional Programs................... 58 4.5 Propositional Image of a CLINGO Program .......................... 60 4.6 Values of a Ground Term .............................................. 63 4.7 More on Propositional Images ........................................ 65 4.8 Bibliographical and Historical Remarks.............................. 68 5 Programs with Negation .................................................... 71 5.1 Examples ............................................................... 71 5.2 Stable Models of a Propositional Program ........................... 73 5.3 Stable Models as Fixpoints............................................ 76 5.4 Simplifying Propositional Programs .................................. 78 5.5 CLINGO Programs with Negation ..................................... 81 5.6 The Law of Excluded Middle ......................................... 83 5.7 CLINGO Programs with Choice ....................................... 86 5.8 Theorem on Constraints ............................................... 88 5.9 Bibliographical and Historical Remarks.............................. 89 6 Mathematics of Stable Models.............................................. 93 6.1 Strong Equivalence .................................................... 93 6.2 Theorem on Strong Equivalence ...................................... 98 6.3 Program Completion .................................................. 99 6.4 Theorem on Completion............................................... 103 6.5 Bibliographical and Historical Remarks.............................. 106 7 More About the Language of CLINGO..................................... 109 7.1 Counting ............................................................... 109 7.2 Summation ............................................................. 112 7.3 Maximum and Minimum .............................................. 116 7.4 Optimization ........................................................... 117 7.5 Symbolic Functions ................................................... 122 7.6 Classical Negation ..................................................... 124 7.7 Bibliographical and Historical Remarks.............................. 126 8 Dynamic Systems............................................................. 129 8.1 Example: The Blocks World .........................................