
rule( (partOf(X,Y) :- component(X,Y)) ).
rule( (partOf(X,Y) :- component(X,Z), partOf(Z,Y)) ).


component(1,2). component(2,3). component(1,3).


% partOfM(2,Y).
/*
rule( (magic_partOf(2):-true) ).
rule( (magic_partOf(X) :-  magic_partOf(Z),component(Z,X)) ).

rule( (partOfM(X,Y) :- magic_partOf(X),component(X,Y)) ).
rule( (partOfM(X,Y) :- magic_partOf(X),component(X,Z), partOfM(Z,Y)) ).
*/

%Forward chaining

forward(K1,K3) :- step(K1,K2), forward(K2,K3).
forward(K1,K1).

step(K1,K2) :-
	rule((A:-B)),		% A :- B
	true_in(B,K1),
	not true_in(A,K1),
	append(K1,[A],K2).

true_in( (A,B), K) :- 
	true_in(A,K), 
	true_in(B,K).
true_in( A, K) :- write(A),nl,member(A,K).

/*
member(A,[A|_]).
member(A,[_|B]) :- member(A,B).

append([],A,A).
append([A|B],C,[A|D]) :- append(B,C,D).
*/

%rule( ( c=3 :- a=1, b=2 )).
%rule( ( d=4 :- c=3 )).

/*
rule( ( a :- b, c )).
rule( ( a :- f, c )).
rule( ( f :- d )).
rule( ( b :- d, e )).
*/

%r:-forward([a=1,b=2],K), writelist(K).
 r:-forward([d,e,c],K), write(K).
