%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                  Predicate Library  
%
%    [library].    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

:-  use_module(library(lists)). 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% math formula

less_than(A, B) :- A < B.
equal(A, B) :- A = B. 
greater_than(A, B) :- A > B.
greater_equal(A, B) :- A >= B. 
less_equal(A, B) :- A =< B. 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Predicate: write_list(+List).
% Writes out each element of List on the screen, throwing a newline
% between elements.
% Control mechanism: simple recursion
% I/O builtins: write/1, nl/0
% e.g. | ?- write_list([foo(a), 'Hello', baz]).
%      foo(a)
%      Hello
%      baz

write_list([Head|Tail]):-
	write(Head), nl,
	write_list(Tail).
write_list([]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A modification of write list

report_error([]) :- nl. 
report_error([X|L]) :-
 write(X), write(' '),
 report_error(L). 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ancestor( (Ent, ID), (Ent1, ID1) )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

ancestor(Child, Parent)  :-
  Child = (Entity,  ID),
  dyn(ent_occ(Entity, ID, Parents)), % child exists

  Parent = (Entity1, ID1), 
  member((Entity1, ID1), Parents).   % Parent is the parent
  
ancestor(Child, Parent) :-
  Child = (Entity,  ID),
  dyn(ent_occ(Entity, ID, Parents)),   % child exists

  Parent = (Entity1, ID1), 
  \+ member((Entity1, ID1), Parents),  % Parent is NOT the parent
  member((Entity2, ID2), Parents),     % Parent2 is the parent
  ancestor((Entity2, ID2), Parent ). 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
set([], []).
set([X|L], [X|Res] ) :-
  \+ member(X, L),
  set(L, Res).

set([X|L], Res ) :-
  member(X, L),
  set(L, Res).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% already defined in the list library
% 
% member(X, [X|_]).
% member(X, [_|L]) :-
%   member(X, L).
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% delete(+Ele, +List, -Rest_list)
% only delete the first X element from the list

deleteX(_, [], []).
deleteX(X, [X|L], L).
deleteX(X, [O|L], [O|Res]) :-
  \+ X = O, 
  deleteX(X, L, Res).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/********* already defined in the list library
append(L, [], L).
append([], L, L).

append([X|L1], L, [X|Result]) :-
   append(L1, L, Result).
********/ 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Small is a subset of Large

subset(Small, Large) :-
    findall(_,( member(X, Small), member(X, Large)), _). 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pretty_print(Term) :-
   Term =.. List,
   pretty(List). 

pretty([]).
pretty([X|R]) :-
   write(X),nl, 
   pretty(R). 
%%%%%%%%%%%%%%% end of document %%%%%%%%%%%%%%%%%%%%%%


