:- auto_table.

:- export
	value/3,
	parent/3,
	father/2,
	mother/2,
	protagonist/2,
	full_name/2,
%	found_h1/2,
	found_it/2.

:- import
%	found_it/2 from found_it,
	append/3 from lists.

/* extract(File, Frame) :-
	protagonist
5	assertz(
*/


%Frames from Bratko
value(Frame, Slot, Value) :-
	Query =.. [Frame, Slot, Value],
	call(Query), !.

value(Frame, Slot, Value) :-
	parent(Frame, ParentFrame),
	value(ParentFrame, Slot, Value).

parent(Frame, ParentFrame) :-
	(Query =.. [Frame, a_kind_of, ParentFrame];
	Query =.. [Frame, instance_of, ParentFrame]),
	call(Query).

%Found It!
found_it(element(_,_,L), It) :-
	found_it(L, It), !.

found_it([], []) :- !.

found_it([X|L], S3) :-
%	write(X), nl,
	found_it(X, S1), !,
	found_it(L, S2), !,
	append(S1, S2, S3), !.

found_it(It, [It]) :- !.

%Father
father(element('Father',A,B), Father) :-
	found_it(B, Father).

father(element(_,_,L), Father) :-
	father(L, Father).

father([X|L], Father) :-
	father(X, Father), !;
	father(L, Father).

%Mother
mother(element('Mother',A,B), Mother) :-
	found_it(B, Mother).

mother(element(_,_,L), Mother) :-
	mother(L, Mother).

mother([X|L], Mother) :-
	mother(X, Mother), !;
	mother(L, Mother).

%Protagonist (Last Name)
protagonist(element(Mathematician,A,B), Protag) :-
	found_it(B, Protag).

protagonist(element(_,_,L), Protag) :-
	protagonist(L, Protag).

protagonist([X|L], Protag) :-
	protagonist(X, Protag), !;
	protagonist(L, Protag).

%Full Name
full_name(element(h1,A,B), Name) :-
	found_it(B, Name).

full_name(element(_,_,L), Name) :-
	full_name(L, Name).

full_name([X|L], Name) :-
	full_name(X, Name), !;
	full_name(L, Name).
/*
found_h1(element(_,_,L), Protag) :-
	found_h1(L, Protag).

found_h1([], []).

found_h1([X|L], S3) :-
	write(X), nl,
	found_h1(X, S1),
	found_h1(L, S2),
	append(S1, S2, S3).

found_h1(Protag, Protag).*/

