% [version 1.2]

% rp( +Expr, ?List ) - List  is expression  Expr
%                      in postfix (reverse Polish) notation

rp( A, [A] ) :- constant( A ).
rp( +(A,B), AABBP ) :- rp( A, AA ), rp( B, BB ),
	append( BB, [+], BBP ),
	append( AA, BBP, AABBP ).
rp( *(A,B), AABBP ) :- rp( A, AA ), rp( B, BB ),
	append( BB, [*], BBP ),
	append( AA, BBP, AABBP ).

% constant( +C ) - C is a constant (i.e. has no arguments)
constant( C ) :- functor( C, _, 0 ).

% rp( A,...) loops for A uninstantiated
% We change the control without changing the logic:

%  rp1( ?Expr, +List )

rp1( A, [A] ) :- constant( A ).
rp1( +(A,B), AABBP ) :- 
	append( AA, BBP, AABBP ),
	append( BB, [+], BBP ),
	rp1( A, AA ), rp1( B, BB ).
rp1( *(A,B), AABBP ) :- 
	append( AA, BBP, AABBP ),
	append( BB, [*], BBP ),
	rp1( A, AA ), rp1( B, BB ).

% Notation, used in the documentation, for specifying requirements
% on predicate arguments (rather obscure)

%   +   - input
%   ?   - output or input
%   -   - unbound
%   @   - will not be further instantiated

