% Bird Example with GCLP (Generalized Corteous Logic Programs)

% Definition von equals
n_equals(X,Y) :- equals(X,Y),!,fail().
n_equals(X,Y).

equals(X,Y) :- X=Y.

% not(nice_bird_s(...)).
not(nice_bird_s(X)) :- nice_bird_s(X),!,fail().
not(nice_bird_s(X)).

% not(nice_bird_r_1(...)).
not(nice_bird_r_1(X)) :- nice_bird_r_1(X),!,fail().
not(nice_bird_r_1(X)).

% not(nice_bird_r_2(...)).
not(nice_bird_r_2(X)) :- nice_bird_r_2(X),!,fail().
not(nice_bird_r_2(X)).

% Facts
eagle("Horst").
falcon("Ebbi").
overrides("eagle","falcon").

% Rules
% q :- q_u, not(q_s).
nice_bird(X) :- nice_bird_u(X), not(nice_bird_s(X)).
nice_bird(Y) :- nice_bird_u(Y), not(nice_bird_s(Y)).

% q_c_j
nice_bird_c_1(X) :- eagle(X).
nice_bird_c_2(Y) :- falcon(Y).

% q_u :- q_c_j, not(q_r_j)
nice_bird_u(X) :- nice_bird_c_1(X), not(nice_bird_r_1(X)).
nice_bird_u(Y) :- nice_bird_c_2(Y), not(nice_bird_r_2(Y)).

% q_r_j
nice_bird_r_1(X) :- nice_bird_c_1(X), nice_bird_c_2(Y), overrides("falcon","eagle"), n_equals(X,Y).
nice_bird_r_2(Y) :- nice_bird_c_2(Y), nice_bird_c_1(X), overrides("eagle","falcon"), n_equals(X,Y).

% q_s
nice_bird_s(X) :- nice_bird_u(X), nice_bird_u(Y), n_equals(X,Y).
					
% Goal
:- solve(nice_bird_u(X)).