% file: pp

% a simple pretty print program to use with a Prolog program which
% returns a parse tree

% test code
% s(s(NP,VP),S0,S) :- np(NP,S0,S1),vp(VP,S1,S).
% np(np(john),[john|S],S).
% vp(vp(eats),[eats|S],S).
% sample query: se([john,eats])?

se(S) :-  s(Tree,S,[]), pp(Tree).

pp(T) :-  pp_function(T,0).

pp_function(F,N) :-  F =.. L, pp_list(L,N), !.
pp_function(F,N) :-  F =.. [].
pp_function(F,N) :-  F =.. [E], write(E).

pp_list([E],N) :-  write(E), K is N+2.
pp_list([F|Args],N) :-  write(F), prin('('), K is N+2, pp_tail(Args,K).

pp_tail([Last],K) :-  pp_function(Last,K), prin(')').
pp_tail([H|T],K) :-  pp_function(H,K), prin(','), nl, tab(K), pp_tail(T,K).
