%------------------------------------------------------------------------
% Program that might be gining wrong results.
%
% Model T = {}, F = {p,q,r,s}.
%------------------------------------------------------------------------

:- dynamic rule/2.

:- table interp_g/1.

interp([]).
interp([G|Gs]) :- interp_g(G),interp(Gs).

interp_g(tnot(G)) :- tnot(interp_g(G)).
interp_g(G) :- rule(G,B),interp(B).

%-------------------------------------------------------------------------------

test :- new_program, query(Goal), interp_g(Goal), fail.
test :- ( interp_g(p) ->
		( tnot(interp_g(p)) -> writeln('i(p) is undefined')
		; writeln('i(p) is true') )
	; writeln('i(p) is false (OK)') ),
        ( interp_g(q) ->
		( tnot(interp_g(q)) -> writeln('i(q) is undefined')
		; writeln('i(q) is true') )
	; writeln('i(q) is false (OK)') ),
	( interp_g(r) ->
		( tnot(interp_g(r)) -> writeln('i(r) is undefined')
		; writeln('i(r) is true') )
	; writeln('i(r) is false (OK)') ),
	( interp_g(s) ->
		( tnot(interp_g(s)) -> writeln('i(r) is undefined')
		; writeln('i(s) is true') )
	; writeln('i(s) is false (OK)') ), fail.
test :- writeln('Program interp5 finished execution...').

%-------------------------------------------------------------------------------

new_program :- cleanup, assert(query(s)),
	assert(rule(p,[tnot(q),r])),
	assert(rule(p,[q])),
	assert(rule(s,[tnot(r),q])),
	assert(rule(q,[tnot(s),p])).

cleanup :- retractall(query(_)), retractall(rule(_,_)), abolish_all_tables.
