This in-depth introduction for students and researchers shows how to use ASP for intelligent tasks, including answering queries, planning, and diagnostics. This in-depth introduction to knowledge representation and reasoning and their use in designing agents for answering logical and probabilistic queries, planning, diagnostics, and other intelligent tasks is based on answer set programming, a powerful knowledge-representation paradigm. It is intended as a textbook for courses in artificial intelligence and will also interest researchers wanting to learn more about recent developments. This in-depth introduction to knowledge representation and reasoning and their use in designing agents for answering logical and probabilistic queries, planning, diagnostics, and other intelligent tasks is based on answer set programming, a powerful knowledge-representation paradigm. It is intended as a textbook for courses in artificial intelligence and will also interest researchers wanting to learn more about recent developments. Knowledge representation and reasoning is the foundation of artificial intelligence, declarative programming, and the design of knowledge-intensive software systems capable of performing intelligent tasks. Using logical and probabilistic formalisms based on answer set programming (ASP) and action languages, this book shows how knowledge-intensive systems can be given knowledge about the world and how it can be used to solve non-trivial computational problems. The authors maintain a balance between mathematical analysis and practical design of intelligent agents. All the concepts, such as answering queries, planning, diagnostics, and probabilistic reasoning, are illustrated by programs of ASP. The text can be used for AI-related undergraduate and graduate classes and by researchers who would like to learn more about ASP and knowledge representation. 1. Logic-based approach to agent design 2. Answer set Prolog (ASP) 3. Roots of ASP 4. Creating a knowledge base 5. Representing defaults 6. The answer set programming paradigm 7. Algorithms for computing answer sets 8. Modeling dynamic domains 9. Planning agents 10. Diagnostic agents 11. Probabilistic reasoning 12. The Prolog programming language.