book image Knowledge Representation, Reasoning, and the Design of Intelligent Agents

Knowledge Representation, Reasoning, and the Design of Intelligent Agents
The Answer-Set Programming Approach

by Michael Gelfond and Yulia Kahl
Cambridge University Press

The goal of this site is to provide additional resources for those using the book.
Questions can be sent to Yulia Kahl at
Last modified: June 16, 2017. Updates are marked with New!


Course Slides


The following are slides I created for my class on Artificial Intelligence. Since I had other topics to cover, the slides are only for select chapters and are not comprehensive; however, they do cover a fair amount of material and may be of use.
-- Yulia Kahl

Programs from the Book

Download all here

Chapter 4

family.lp -- Section 4.1.1
basic family relationships
orphans.lp -- Section 4.1.2
defining orphans
ancestors.lp -- Section 4.1.3
defining ancestors
ec.lp -- Section 4.2
representing an electrical circuit
hierarchy.lp -- Section 4.3
representing a basic inheritance hierarchy

Chapter 5

uncaring.lp -- Section 5.1
uncaring John -- basic defauts and their exceptions
cowardly.lp -- Section 5.1.2
cowardly students -- more defaults with weak and strong exceptions
course_catalog_1.lp -- Section 5.2
representing null values with value 'staff'
course_catalog_2.lp -- Section 5.2
representing null values with disjunction
orphans2.lp -- Section 5.3
priorities between defaults
blue_deep.lp -- Section 5.4.1
inheritance hierarchy with defaults
darling.lp -- Section 5.4.2
redefining membership using defaults
tweety.lp -- Section 5.4.3
illustration of the specificity principle as applied to an inheritance hierarchy with multiple defaults and exceptions

Chapter 6

hamgraph.lp -- Section 6.1
finds Hamiltonian cycles
sudoku.lp -- Section 6.2.1
solves a Sudoku puzzle
sudokudlv.lp -- Section 6.2.1
sudoku.lp in DLV syntax
mystery.lp -- Section 6.2.2
solves a murder puzzle

Chapter 8

blocks1.lp -- Section 8.1
only for experimentation
blocks2.lp -- Section 8.1
only for experimentation
blocks3.lp -- Section 8.1
final version
bc.lp -- Section 8.5.1
briefcase domain translated from AL system description
bw.lp -- Section 8.5.2
blocks world domain translated from AL system description
twoarms.lp -- Section 8.5.3
blocks world with concurrent actions

Chapter 9 (and Appendices D.1 and D.2)

bwplan_choice.lp -- Section 9.1
bw.lp + simple planning module using choice rule + example initial situation + example goal
where bw.txt is the AL system description of the blocks-world domain translated into ASP (see Ch. 8)
bwplan_disjunction.lp -- Section 9.1
same as bwplan_choice.lp but the simple planning module uses a disjunctive rule instead of a choice rule
ignite.lp -- Section 9.2.2 & Appendix D.1
finds a plan for igniting a burner through a series of pipes and valves
crossing.lp -- Section 9.2.3 & Appendix D.2
finds a plan for solving the missionaries and cannibals problem
heuristics.lp -- Section 9.3
bwplan_choice.lp + new initial state, new goal state + heuristics
twoarmsplan.lp -- Section 9.4
planner for blocks world with concurrent actions
crbwplan.lp -- Section 9.5
CR-Prolog planner for blocks world

Chapter 10 (and Appendix D.3)

circuit.lp -- Section 10.3 & Appendix D.3
finds possible explanations for a broken circuit

Chapter 12 -- Section 12.2.3
Prolog program that computes derivatives of polynomials

SPARC Versions of Book Programs

Download all SPARC programs here

Chapter 4:


Chapter 5:


Chapter 6:


Chapter 8:


Chapter 9:


Chapter 10:


Solvers and Other Useful Software

For clingo, gringo, and clasp visit the Potassco site at

For DLV and ASPIDE, visit

SPARC can be found at

There is a new tool for running SPARC programs online which can be found at
You can simply cut and paste programs to replace the sample code and ask queries or get answer sets.

CRModels is at

MKAtoms is at

Additional resources can be found at the Texas Tech Knowledge Representation Lab site

Additional Exercises with Solutions

ex02.pdf   (Ch. 2) soln_ex02.pdf The answer sets are the same.
This file has the SPARC code for the mini programs: s_ex02.txt
ex_local_cwa.txt   (Ch. 4) local_cwa.lp s_local_cwa.sp
ex_cars.txt   (Section 5.5)   (CR-Prolog) s_cars.sp
ex_loops.txt   (Ch. 6) loops.lp s_loops.sp
ex_clique.txt  (Ch. 6) clique.lp s_clique.sp
ex_max_clique.txt  (Ch. 6)   (CR-Prolog) s_max_clique.sp
ex_morphing_vehicles.txt   (Ch. 8) morphing_vehicles.lp s_morphing_vehicles.sp


Special thanks to Elham Hojati and Yuliya Lierler for finding some of these.
p. 173 — line 3 {occurs(ai): 0 ≤ i ≤ k} {occurs(aj, i): 0 ≤ j ≤ k}
p. 173 In showing the translation of preconditions, we did not make a note about the case where the preconditions are statics. Since in this case we are not concerned about showing their change over time, we should not use holds. Simply write the statics as they are. For example: given a rule "a causes l if s, f" where s is a static and f is a fluent, the translation is
h(l,I+1) :- s,
            I < n.
Blocks world In the original description of the blocks world, we fail to flush out an implicit rule; namely, that a given block must have a location. The effect of this is not felt until we suggest in Section 8.5.3 that we can remove rule 7 from p. 180
impossible put(B1,B) if on(B2,B)
because it is an overspecification. With this rule gone, it becomes possible for a block to be nowhere.
Subsequent references and examples already ignore the removal of the rule, so the text makes sense except for the actual example on overspecification on p. 184.
p. 185, Figure 8.9 States in which f holds do not have loops showing what happens when action a is executed. Add loops labeled by the letter a to the three states in which f holds as there is nothing preventing the exectution of action a in those states.
p. 194 — next to last line (i) For any 0 < i ≤ k, occurs(ai, i-1) in S (i) For any 0 < i ≤ k, occurs(ai, i) in S
p. 199 — line 26 fluent(defined, wrong_config(B)) fluent(defined, wrong_config)
p. 223 — line 24 E2 would be the only possible explanation E1 would be the only possible explanation
p. 241 — next-to-last line New! a(t̅)=y1 or ... or a(t̅)=yn ← body, not intervene(a(t̅)) a(t̅,y1) or ... or a(t̅,yn) ← body, not intervene(a(t̅))
p. 246 — line 29 W1 is 1/24 W is 1/24
p. 250 — last line draw(T):{C:available(C,T)} random(draw(T):{C:available(C,T)})
p. 258 New! In the list of 6 pr-atoms in the top half of the page, the first two only have a vertical bar in them, not a causal stroke. Use a causal stroke instead of a vertical bar.