PKR 2 Stable.1nup
PKR 2 Stable.1nup
PKR 2 Stable.1nup
Thomas Eiter
1. Introduction
4. Stratified Negation
5. Stable Models
6. Extensions
7. Conclusion
Outline
1. Introduction
4. Stratified Negation
5. Stable Models
6. Extensions
7. Conclusion
Example 1
Conclusion: mortal(socrates)
Big Problem
Insight
Kowalski (1979):
ALGORITHM = LOGIC + CONTROL
Prolog
Prolog = “Programming in Logic”
Example 2
man(dilbert).
person(X) ← man(X).
Query ?− person(X)
Answer X = dilbert
[email protected] 626.015 VU 2.0 Principles of KR AAU, SS 2024 6/78
1. Introduction 1.2 Prolog
Example 3 (Recursion)
append([],X,X).
append([X|Y],Z,[X|T]) :- append(Y,Z,T).
reverse([],[]).
reverse([X|Y],Z) :- append(U,[X],Z), reverse(Y,U).
Problem:
Reverse the list [a,b,c]
Example 4
LP Desiderata
It is desirable that
• the order of subgoals in a rule does not matter;
• the order of program rules does not matter;
• in particular, termination is not subject to such order.
Outline
1. Introduction
4. Stratified Negation
5. Stable Models
6. Extensions
7. Conclusion
Example 6
Herbrand Semantics
Example 8 (Program P1 )
h(0, 0).
t(a, b, r).
p(0, 0, b).
p(f (X), Y, Z) ← p(X, Y, Z 0 ), h(X, Y), t(Z, Z 0 , r).
h(f (X), f (Y)) ← p(X, Y, Z 0 ), h(X, Y), t(Z, Z 0 , r).
Grounding
Example 10 (Program P2 )
p(f (X), Y, Z) ← p(X, Y, Z 0 ), h(X, Y), t(Z, Z 0 , r).
h(0, 0).
h(0, 0).
Herbrand Models
Note:
Proposition 1
For every positive logic program P, HB(P) is a model of P.
Theorem 15
Every logic program P has a single minimal model (called the least model),
denoted LM(P).
Example 16
For P2 = { p(f (X), Y, Z) ← p(X, Y, Z 0 ), h(X, Y), t(Z, Z 0 , r). h(0, 0)}, we
have LM(P2 ) = {h(0, 0) }.
LM(P1 ) = {h(0, 0), t(a, b, r), p(0, 0, b), p(f (0), 0, a), h(f (0), f (0))}.
Computation
The minimal model can be computed via fixpoint iteration.
Fundamental result:
Theorem 18
TP has a least fixpoint, lfp(TP ), and lfp(TP ) = LM(P). Furthermore, the
sequence hTPi i, i ≥ 0, converges to lfp(TP ).
Example 19
For P3 = { a ← b. b ← c. c }, we have
TP03 = {}, TP13 = {c}, TP23 = {c, b}, TP33 = {c, b, a}, TP43 = TP33
For P4 = { a ← b. b ← a. c }, we have
Example 20 (cont’d)
TP01 = ∅,
TP11 = {h(0, 0), t(a, b, r), p(0, 0, b)},
TP21 = {h(0, 0), t(a, b, r), p(0, 0, b), p(f (0), 0, b), h(f (0), f (0))},
TP21 = TP31 .
Hence lfp(TP1 ) = {h(0, 0), t(a, b, r), p(0, 0, b), p(f (0), 0, b), h(f (0), f (0))}.
Outline
1. Introduction
4. Stratified Negation
5. Stable Models
6. Extensions
7. Conclusion
Example 22 (Program P5 )
man(dilbert).
single(X) ← man(X), not husband(X).
husband(X) ← fail. % fail = "false" in Prolog
Query:
? − single(X).
Answer:
X = dilbert .
Example 23 (cont’d)
Modifying the last rule of P5 , we get P6 :
man(dilbert).
single(X) ← man(X), not husband(X).
husband(X) ← man(X), not single(X).
ASP Paradigm
Method:
1 encode I as a (non-monotonic) logic program P, such that solutions of
I are represented by models of P
2 compute some model M of P, using an ASP solver
3 extract a solution for I from M .
variant: compute multiple/all models (for multiple/all solutions)
Often: decompose I into problem specification and data
Approach: guess and check (alias generate and test) plus auxiliary
definitions
Outline
1. Introduction
4. Stratified Negation
5. Stable Models
6. Extensions
7. Conclusion
Stratified Negation
Example 25
Dependency Graph
Restriction:
The method works if there is no cyclic negation.
Example 27 (Program P7 )
man(dilbert).
husband(X) ← man(X), married(X).
single(X) ← man(X), not husband(X).
dep(P7 ):
husband
OO L
// married
LLL
⋆
LLL
LLL
L&&
single // man
Stratification
Definition 28 (stratification)
A stratification of a set P of rules is a partitioning
Σ = {S1 . . . , Sn }
of pred(P) into n nonempty, pairwise disjoint sets such that
(a) if p ∈ Si , q ∈ Sj , and p → q is in dep(P), then i ≥ j; and
(b) if p ∈ Si , q ∈ Sj , and p →? q is in dep(P) then i > j.
Semantics
man(dilbert).
husband(X) ← man(X), married(X).
single(X) ← man(X), not husband(X).
Stratification Theorem
Note: stratifications are not unique.
Example 31 (P7 cont’d)
Other stratification: Σ0 = {S10 = {man, married, husband}, S20 = {single}}.
Evaluation with Σ0 yields same result!
Corollary 33
Stratified programs have a canonical model, also called perfect model.
Outline
1. Introduction
4. Stratified Negation
5. Stable Models
6. Extensions
7. Conclusion
man(dilbert). (f1 )
single(dilbert) ← man(dilbert), not husband(dilbert). (r1 )
husband(dilbert) ← man(dilbert), not single(dilbert). (r2 )
Consider M 0 = {man(dilbert)}.
If as in M 0 , man(dilbert) were true and husband(dilbert) false, by r1 also
single(dilbert) should be true. This is not coherent.
Consider M 00 = {man(dilbert), single(dilbert), husband(dilbert)}.
The bodies of r1 and r2 are not true wrt M 00 , hence there is no evidence for
single(dilbert) and husband(dilbert) being true.
Stable Models
Intuition:
M makes an assumption about what is true and what is false.
The reduct PM incorporates this assumptions.
As a “not ”-free program, PM derives positive facts, given by LM(PM ).
If this coincides with M , then the assumption of M is “stable”.
Observe:
PM = P for any “not ”-free program P.
Thus, for any positive program LM(P) (=LM(PM )) is its single stable model.
man(dilbert). (f1 )
single(dilbert) ← man(dilbert), not husband(dilbert). (r1 )
husband(dilbert) ← man(dilbert), not single(dilbert). (r2 )
Candidate interpretations:
M1 = {man(dilbert), single(dilbert)},
M2 = {man(dilbert), husband(dilbert)},
M3 = {man(dilbert), single(dilbert), husband(dilbert)}
M4 = {man(dilbert)},
man(dilbert). (f1 )
single(dilbert) ← man(dilbert), not husband(dilbert). (r1 )
husband(dilbert) ← man(dilbert), not single(dilbert). (r2 )
M1 = {man(dilbert), single(dilbert)}:
M
reduct P6 1 :
man(dilbert).
single(dilbert) ← man(dilbert).
M
The least model of P6 1 is {man(dilbert), single(dilbert)} = M1 .
man(dilbert). (f1 )
single(dilbert) ← man(dilbert), not husband(dilbert). (r1 )
husband(dilbert) ← man(dilbert), not single(dilbert). (r2 )
man(dilbert).
LM(P6M3 ) = {man(dilbert)} 6= M3 .
M4 = {man(dilbert)}: PM4
6 is
man(dilbert).
single(dilbert) ← man(dilbert).
husband(dilbert) ← man(dilbert).
Inconsistent Programs
Example 40 (P⊥ )
p ← not p
Candidate M1 = {}: PM
⊥ = {p}, and LM(P⊥ ) = {p} =
1
6 M1 .
Candidate M2 = {p}: PM
⊥ = {}, and LM(P⊥ ) = {} =
2
6 M2 .
man(dilbert). (r1 )
woman(alice). (r2 )
single(X) ← man(X), not husband(X). (r3 )
husband(X) ← man(X), not single(X). (r4 )
The program grnd(P06 ), and thus P06 , has the following stable models:
M1 = {man(dilbert), woman(alice), single(dilbert)}
M2 = {man(dilbert), woman(alice), husband(dilbert)}
Indeed,
the rule instances of r3 and r4 for dilbert generate two possible scenarios;
the rule instances of r3 and r4 for alice are inapplicable.
Stable model semantics has a strong theoretical basis, many properties are
known.
See e.g.
• [Lifschitz, 2008]
• [Ferraris and Lifschitz, 2005]
• [Gelfond, 2008]
for other insights, alternative definitions and properties of stable models.
a ∨ not b1 ∨ . . . ∨ not bm ∨ c1 ∨ . . . ∨ cn ,
Theorem 46
1 Every stable model M of P is a model of P.
2 A stable model M does not contain any model M 0 of P properly (M 0 6⊂ M ),
i.e., is a minimal model of P (w.r.t. ⊆).
Corollary 47
Stable models are incomparable w.r.t. ⊆, i.e., if M1 and M2 are different stable
models of P, then M1 6⊆ M2 and M2 6⊆ M1 .
Supportedness
Note: each atom a in a stable model M must be derived from some rule of
P.
Extend the immediate consequence operator TP to not .
Theorem 49
Every stable model M of P is a supported model of P.
Supportedness (cont’d)
In fact, by minimality of stable models, every stable model is a minimal
(w.r.t. ⊆) supported model of P.
The converse is not true.
Example 50 (Program Ps )
a ← not b.
b ← c.
c ← b.
Note that M1 = {a} and M2 = {b, c} are both minimal such that
TPs (M1 ) = M1 and TPs (M2 ) = M2 .
The single stable model of Ps is M1 .
Problem with M2 : Self-supportedness of b (via c)
Similar: Ps0 = {a ← not a; a ← a}
Unfounded sets
Stable models amount to supported models with no (cyclic) self-support
Definition 51 (unfounded set, cf. [van Gelder et al., 1991],[Leone et al., 1997])
A set U ⊆ HBP is an unfounded set of P relative to interpretation I , if for every
a ∈ U and r : a ← b1 , . . . , bm , not c1 , . . . , not cn in ∈ grnd(P), either
1 for some i ∈ {1, . . . , m}, either bi 6∈ I or bi ∈ U , or
2 for some j ∈ {1, . . . , n}, cj ∈ I .
Definition 52 (unfounded-freeness)
I is called unfounded-free, if I ∩ U = ∅ for each unfounded set U of P rel. to I .
a ← not b.
b ← c.
c ← b.
Stratified Programs
Stable model semantics gracefully generalizes stratified semantics:
Theorem 55
If a program P is stratified, then P has a single stable model, which coincides
with the perfect (i.e., the iterative least) model of P.
Notes:
A stratified P may have several minimal models; only one is stable
E.g., P = {boring(chess) ← not interesting(chess)}
has two minimal models:
M1 = {boring(chess)} and M2 = {interesting(chess)}.
The perfect model is MP = M1 .
Non-Cumulativity
In classical logic, adding consequences of a theory T to T preserves its
semantics.
This property is known as cumulativity (or lemma support).
For stable model semantics, this property does not hold.
Proposition 3
Suppose P and atom a fulfill M |= a, for each stable model M of P. Then P and
P ∪ { a } need not have the same stable models (even if P is consistent).
Example 56
b ← not c. c ← not b.
a ← b. a ← not a.
P has the stable model M = {a, b}; P ∪ {a} has in addition M 0 = {a, c}.
Computation
How difficult is it to compute some stable model?
Theorem 57
For normal logic programs P, problem CONS is
NP-complete in the propositional and ground case;
NE XP T IME-complete in the datalog (function-free) case;
Σ11 -complete in the general first-order case.
Computation (cont’d)
Evaluation Approaches
Different methods and evaluation approaches:
• resolution-based: [Bonatti et al., 2008]
• forward chaining: [Lefèvre et al., 2015]
• lazy grounding: [Palù et al., 2009], [Dao-Tran et al., 2012], [de Cat et
al., 2012]
• translation-based: e.g. to SAT: assat [Lin and Zhao, 2004], cmodels
[Lierler, 2005], or SMT [Niemelä, 2009]
• meta-interpretation: [E_ et al., 2003]
• portfolio solving: e.g. claspfolio [Gebser et al., 2011], me-asp [Maratea
et al., 2014]
Predominant architecture
Outline
1. Introduction
4. Stratified Negation
5. Stable Models
6. Extensions
7. Conclusion
Extensions
Many extensions exist, partly motivated by applications
Some are syntactic sugar, other strictly add expressiveness
Incomplete list:
• constraints
• strong negation
• disjunction
• aggregates (Smodels, DLV, clasp, wasp)
• cardinality constraints (Smodels)
• optimization: weight constraints, minimize (Smodels);
weak constraints (DLV)
• nested expressions
• templates (for macros), external functions (DLVHEX)
• Frame Logic syntax (for Semantic Web)
• preferences: e.g., PLP
• temporal logic: telingo, ticker
• probabilities: plingo
• KR frontends (diagnosis, inheritance, planning,...) in DLV
Surveys: [Niemelä (ed.), 2005], [Gebser and Schaub, 2016]
[email protected] 626.015 VU 2.0 Principles of KR AAU, SS 2024 64/78
6. Extensions
Application Areas
configuration non-classical reasoning
information integration (abduction, induction)
inconsistency management recommender systems
planning, reasoning about actions systems biology / biomedicine
routing, scheduling knowledge management
diagnosis, repair robotics
security, verification natural language processing
Semantic Web computational linguistics
games, puzzles agents
algorithm design musicology
argumentation context-aware reasoning
classification ...
Constraints
Adding
p ← q1 , . . . , qm , not r1 , . . . , not rn , not p.
Short:
Constraint
← q1 , . . . , qm , not r1 , . . . , not rn .
man(dilbert). (f1 )
single(dilbert) ← man(dilbert), not husband(dilbert). (r1 )
husband(dilbert) ← man(dilbert), not single(dilbert). (r2 )
← husband(X), not has_wedding_ring(X). (c1 )
Strong Negation
Weak negation “not a” means “a can not be proved (derived) using rules,”
and that a is false by default (believed to be false).
This is different from knowing (provably) that a is false; this is expressed by
−a (sometimes ¬a).
This is called strong negation and may make an important difference.
Definition 60
An extended logic program (ELP) is a finite set of rules
a ← b1 , . . . , bm , not c1 , . . . , not cn (n, m ≥ 0) (3)
Strong negation (combined with weak negation) is e.g. helpful to express default
rules.
french(luc). (f1 )
speaks(X, french) ← french(X), not − speaks(X, french). (r1 )
−speaks(X, french) ← thumb(X). (r2 )
Note:
ELPs are closely related to Default Logic [Reiter, 1980]
The answer sets of P correspond 1-1 to the extensions of the default theory
T = (∅, {d(C) | C ∈ P}) (d(C) casts C into a default rule).
Disjunction
Example 62
Example 63
Minimality
man(dilbert).
single(X) ← man(X), not husband(X).
husband(X) ← man(X), not single(X).
man(dilbert).
single(X) ∨ husband(X) ← man(X).
Semantics:
Answer sets of P are defined similarly as for an ELP
Differences:
• I is a model of ground (4), if either {b1 , . . . , bm } * I or
{a1 , . . . , ak , c1 , . . . , cn } ∩ I 6= ∅
• “M is the least model of PM ” ; “M is a minimal model of PM ”
(PM may have multiple minimal models).
man(dilbert).
single(X) ∨ husband(X) ← man(X).
Answer sets:
M1 = {man(dilbert), single(dilbert)}, and
M2 = {man(dilbert), husband(dilbert)}.
Outline
1. Introduction
4. Stratified Negation
5. Stable Models
6. Extensions
7. Conclusion
Conclusion
Answer Set (Stable model) Semantics is a predominant semantics for
nonmonotonic logic programs to date (“pure” declarative programming)
Paradigm shift from computing proofs (Prolog) to computing models
This is massively exploited for Answer Set Programming (ASP)
• encode the solution of a problem in answers sets of a logic program
• run an ASP solver
• extract solution(s) from answer set(s)
ASP is a highly active research field, with growing application range
• brief introduction: e.g., [Brewka et al., 2011]
• a primer: [E_ et al., 2009]
• textbooks: e.g. [Baral, 2003], [Gebser et al., 2012], [Gelfond and Kahl, 2014],
[Lifschitz, 2019]
Special issues on ASP:
Martin Gebser, Roland Kaminski, Benjamin Kaufmann, Torsten Schaub, Marius Thomas Schneider, and
Stefan Ziller.
A portfolio solver for answer set programming: Preliminary report.
In James P. Delgrande and Wolfgang Faber, editors, Logic Programming and Nonmonotonic Reasoning -
11th International Conference, LPNMR 2011, Vancouver, Canada, May 16-19, 2011. Proceedings, volume
6645 of Lecture Notes in Computer Science, pages 352–357. Springer, 2011.
Martin Gebser, Roland Kaminski, Benjamin Kaufmann, and Torsten Schaub.
Answer Set Solving in Practice.
Synthesis Lectures on Artificial Intelligence and Machine Learning. Morgan & Claypool Publishers, 2012.
Michael Gelfond and Yulia Kahl.
Knowledge Representation, Reasoning, and the Design of Intelligent Agents: The Answer-Set Programming
Approach.
Cambridge University Press, New York, NY, USA, 2014.
M. Gelfond and V. Lifschitz.
The Stable Model Semantics for Logic Programming.
In Logic Programming: Proceedings Fifth Intl Conference and Symposium, pages 1070–1080, Cambridge,
Mass., 1988. MIT Press.
M. Gelfond and V. Lifschitz.
Classical Negation in Logic Programs and Disjunctive Databases.
New Generation Computing, 9:365–385, 1991.
M. Gelfond.
Answer sets.
In B. Porter F. van Harmelen, V. Lifschitz, editor, Handbook of Knowledge Representation, chapter 7, pages
285–316. Elsevier, 2008.
References V
Vladimir Lifschitz.
Answer Set Programming and Plan Generation.
Artificial Intelligence, 138:39–54, 2002.
Vladimir Lifschitz.
Twelve definitions of a stable model.
In ICLP, pages 37–51, 2008.
Vladimir Lifschitz.
Answer Set Programming.
Springer, 2019.
Fangzhen Lin and Yuting Zhao.
ASSAT: Computing Answer Sets of a Logic Program by SAT Solvers.
In Proceedings of the Eighteenth National Conference on Artificial Intelligence (AAAI-2002), Edmonton,
Alberta, Canada, 2002. AAAI Press / MIT Press.
Fangzhen Lin and Yuting Zhao.
ASSAT: computing answer sets of a logic program by SAT solvers.
Artificial Intelligence, 157(1-2):115–137, 2004.
Marco Maratea, Luca Pulina, and Francesco Ricca.
A multi-engine approach to answer-set programming.
TPLP, 14(6):841–868, 2014.
References VII
Example 67
Definition 68 (operator)
An operator on a complete lattice (V, ≤) is a mapping T : V −→ V .
Example 69
TP operator for program P on Herbrand interpretations
9. Appendix 9.1 Fixpoint Theorems of Knaster-Tarski/Kleene
Monotone Operators
Definition 70 (monotone operator)
An Operator T : V −→ V on (V, ≤) is monotone, if
Theorem 71 (Knaster-Tarski)
Any monotone operator T on a complete lattice (V, ≤) has a least fixpoint
lfp(T), and
lfp(T) = inf ({x ∈ V | T(x) ≤ x}).
Example 72
TP operator for program P is monotone
9. Appendix 9.1 Fixpoint Theorems of Knaster-Tarski/Kleene
Continuous Operators
A stronger theorem holds for continuous operators.
Example 75
TP operator is also continuous.
9. Appendix 9.1 Fixpoint Theorems of Knaster-Tarski/Kleene
Theorem 76 (Kleene)
Any continuous operator T on a complete lattice (V, ≤) has a least fixpoint, and
bis
semel
quincy
clote ter
mamuk olfe
dalte icsi
quater
Predicates:
Railroad program P:
dep(P):
station linked
circumvent link
*
has_icut_point connected
cutpoint
*
safely_connected
Stratification Σ = {S1 , S2 , S3 }:
Evaluation:
1 PS1 :
M1 = {linked(semel, bis), linked(bis, ter), linked(ter, olfe),
. . . , connected(semel, olfe), . . . ,
circumvent(quincy, semel, bis), . . .}
2 PS2 :
M2 = M1 ∪ {cutpoint(ter, semel, olfe),
has_icut_point(semel, olfe), . . .}
3 PS3 :
MP = M3 = M2 ∪ {safely_connected(semel, bis),
safely_connected(semel, ter)}
/ MP
Note: safely_connected(semel, olfe) ∈
9. Appendix 9.3 The DLV System
http://www.dbai.tuwien.ac.at/proj/dlv/,
http://www.dlvsystem.com
Later version:
• DLV 2.0, combines i-DLV and wasp
https://www.mat.unical.it/DLV2/
9. Appendix 9.3 The DLV System
Features of DLV
Additionally:
• bounded integer arithmetic, and comparison built-ins
• integrity constraints
• weak constraints
• aggregates
• since Release 2010-10-14: functions symbols available! (also in
DLV-Complex)
Support for
• answer set generation
• With release 2010-10-14: well-founded model generation
• brave and cautious reasoning
• many extensions: e.g. DLVHEX, DLVDB , ASPIDE, JDLV ...
9. Appendix 9.3 The DLV System
DLV Syntax
Rules
Integrity Constraints
Queries
b1 , . . . , bk , not bk+1 , . . . , not bm ?
9. Appendix 9.3 The DLV System
Built-in Predicates
Comparison Predicates:
Arithmetic Predicates:
Safety
Safe rules
a(X) :- not b(X), c(X).
a(X) :- X > Y, node(X), node(Y).
Unsafe rules
a(X) v − a(X).
a(X) :- not b(X).
:- X <= Y, node(X).
9. Appendix 9.3 The DLV System
Using DLV
Input is read from files whose names are passed on the command-line.
If the command-line option “--” has been specified, input is also read from
standard input (stdin).
Output is printed to standard output (stdout), one line per model / answer
set.
Detailed documentation is at
http://www.dbai.tuwien.ac.at/proj/dlv/,
http://www.dlvsystem.com
9. Appendix 9.4 Declarative Problem Solving in DLV
Review here briefly some; for more discussion, see [E_ et al., 2009].
Front-ends
Besides the answer set semantics core, DLV offers front-ends for problem
solving.
Many external front ends to DLV exist (e.g., updates, preferences, plan
diagnosis, execution monitoring, etc.)
9. Appendix 9.4 Declarative Problem Solving in DLV
Maximum
Input: Employees and their salaries, represented by empl(_ , _).
Problem: Determine maximum salary of employees.
Further discussion in [E_ et al., 2000], [Leone et al., 2006], [E_ et al., 2009] (+
additional component for computing optimal solutions).
9. Appendix 9.5 The “Guess and Check” Methodology
Example: 3-Coloring
c
• a • • b a • • b
a • • b c c
• •
PD = {node(a), node(b),
a • • b a • • b
node(c), edge(a, b),
edge(b, c), edge(a, c)}
c c
• •
a • • b a • • b
9. Appendix 9.5 The “Guess and Check” Methodology
reached(X):-start(X).
reached(X):-reached(Y), inPath(Y, X). Auxiliary Predicate
start_reached:-start(Y), inPath(X, Y).
9. Appendix 9.5 The “Guess and Check” Methodology
d • • c • c • c
d • d •
a • • b a • • b a • • b
PD = {node(a), node(b),
node(c), node(d),
edge(a, b), edge(a, c) d • • c d • • c
edge(c, b), edge(b, c)
edge(b, d), edge(d, c)
edge(d, a), edge(a, d)
a • • b a • • b
start(a)}
9. Appendix 9.5 The “Guess and Check” Methodology
Given a parent-child relationship parent(P, X) (an acyclic directed graph), find all
pairs of persons belonging to the same generation.
Two persons are of the same generation, if either (i) they are siblings, or (ii) they
are children of two persons of the same generation.
The Ramsey number R(k, m) is the least integer n such that, no matter how
we color the arcs of the complete undirected graph (clique) with n nodes
using two colors, say red and blue, there is a red clique with k nodes (a red
k-clique) or a blue clique with m nodes (a blue m-clique).
R(k, m) exists for all pairs of positive integers k and m.
Consider the following program:
o
blue(X, Y) v red(X, Y):-arc(X, Y). Guess
teach(X, Y):-member(X, cs), course(Y, cs), likes(X, Y), not − teach(X, Y).
−teach(X, Y):-member(X, cs), course(Y, cs), teach(X1 , Y), X1 6= X.
has_course(X):-member(X, cs), teach(X, Y).
:-member(X, cs), not has_course(X).
:-teach(X, Y1 ), teach(X, Y2 ), teach(X, Y3 ),
Y1 6= Y2 , Y1 6= Y3 , Y2 6= Y3 .