# CS345 Solutions #2

1. (30 pts.)
(a)
After step 2: p(1) :- NOT r(1) & s(1) p(2) :- NOT r(2) & s(2) r(1) :- NOT p(1) & t(1) After step 3: p(1) :- NOT r(1) & s(1) p(2) :- NOT r(2) & s(2) Afer step 4: p(1) :- s(1) p(2) :- s(2) After step 5: p(1) :- p(2) :- We infer p(1) and p(2).

GL(M) = EDB + {p(1), p(2)} = M

M is a stable model.

(b)
After step 2: p(1) :- NOT r(1) & s(1) p(2) :- NOT r(2) & s(2) r(1) :- NOT p(1) & t(1) After step 3: p(2) :- NOT r(2) & s(2) After step 4: p(2) :- s(2) After step 5: p(2) :- We infer p(2).

GL(M) = EDB + {p(2)} <> M

M is not a stable model (in fact, M is not a model).

(c)
After step 2: p :- r p :- NOT t q :- t r :- s s :- NOT q t :- NOT p u :- v After step 3: p :- r q :- t r :- s t :- NOT p u :- v After step 4: p :- r q :- t r :- s u :- v t :- We infer t, and then from q :- t, we can infer q. Note that we can't use positive IDB's from M (such as v) to infer anything.

GL(M) = {t, q} <> M

M is not a stable model.

(d)
After step 2: p :- r p :- NOT t q :- t r :- s s :- NOT q t :- NOT p u :- v After step 3: p :- r p :- NOT t q :- t r :- s s :- NOT q u :- v After step 4: p :- r p :- q :- t r :- s s :- u :- v We infer p and s, and then from r:- s we infer r.

GL(M) = {p, s, r} = M

M is a stable model.

2. (40 pts.)
(a)
Round 0:
None of the IDB predicates are true.

Round 1:
greenPath(1,2), monopoly(1,2), and monopoly(2,3) are inferred.

Round 2:

The inflationary model is EDB + {greenPath(1,2), monopoly(1,2), monopoly(2,3)}.

(b)
Round 0:
None of the IDB predicates are true.

Round 1:
win(Y) is false for any Y, so subgoals of the form NOT win(Y) are true and can be eliminated. We are left with rules of the form: win(X) :- move(X,Y). We can infer win(X) if there exists some Y such that move(X,Y) is in the EDB.

Round 2:
No more inferences can be made.

The inflationary model is EDB + {win(X) | move(X,Y) is in the EDB}.

(c)
One solution is to change the monopoly rule, so that the red/green bus line rules are: greenPath(X,Y) :- green(X,Y) greenPath(X,Y) :- greenPath(X,Z) & greenPath(Z,Y) monopoly(X,Y) :- red(X,Y) & NOT greenPath(X,Y) & NOT green(X,Y) For the given EDB, the inflationary model of these revised rules is the same as the stratified model.

A solution that works for the general case is:

round(0) :- round(s(X)) :- round(X) greenPath(X,Y) :- green(X,Y) greenPath(X,Y) :- greenPath(X,Z) & greenPath(Z,Y) greenPath2(X,Y,0) :- green(X,Y) greenPath2(X,Y,s(C)) :- greenPath2(X,Z,C) & greenPath(Z,Y) greenPath2(X,Y,s(C)) :- greenPath(X,Z) & greenPath2(Z,Y,C) new(C) :- greenPath(X,Y,C) monopoly(X,Y) :- red(X,Y) & NOT greenPath(X,Y) & round(s(C)) & NOT new(C)

3. (30 pts.)
(a)
Round 1:
After instantiating the win rule in all possible ways, deleting true EDB subgoals, and deleting rules with false EDB subgoals, we get: win(1) :- NOT win(2) win(2) :- NOT win(3) win(3) :- NOT win(4) win(3) :- NOT win(1) Round 1:
No positive inferences. The largest unfounded set is {win(4)}. Infer NOT win(4). We can delete the body of the third rule, since NOT win(4) is true.

Round 2:
We can infer win(3) from the third rule. Delete the last two rules, since the head is known to be true. The largest unfounded set is {win(3)}. Infer NOT win(3). Delete the body of the second rule, since NOT win(3) is true.

Round 3:
We can infer win(1).

The WF model is {win(1), NOT win(2), win(3), NOT win(4)}

(b)
After instantiating the win rule in all possible ways, deleting rules with false subgoals and deleting all true subgoals, we get: win(1) :- NOT win(2) win(2) :- NOT win(3) win(3) :- NOT win(4) win(3) :- NOT win(1) win(4) :- NOT win(5) Round 0 1 2 3 ------------------------- win(1) 0 1 0 1 win(2) 0 1 0 1 win(3) 0 1 0 1 win(4) 0 1 1 1 win(5) 0 0 0 0 The WF model is {win(4), NOT win(5)}. win(1), win(2), and win(3) are unknown.

(c)
There is ALWAYS a 2-valued well-founded model if the move graph for the win program is acyclic. If the move graph is acyclic, then the program is locally stratified since there can be no loops (and so no negative loops) in the dependency graph. Since the program is locally stratified, it must have a 2-valued well-founded model.

There is SOMETIMES a 2-valued well-founded model if the move graph is cyclic. Move graphs is parts a and b both has cycles. Part a has a 2-valued well-founded model, but part b doesn't have a 2-valued well-founded model.