
% File: stack.pl
% Author: Dave Wessels
%
% Purpose: provide a simple stack example using lists,
%    treats front of list as top of stack
% 
% Supported operations:
%    stack(S):         succeeds if S is a valid stack
%    pop(S,NewS):      succeeds if NewS is result of popping top element off S
%    top(S, E):        succeeds if E is top element of S
%    push(S, E, NewS): succeeds if NewS is result of pushing E onto S
%    isempty(S):       succeeds if S is an empty stack


% stack(S)
% --------
% succeeds if S is a valid stack, simply a list of anything
stack(S) :- list(S).


% pop(S,NewS)
% -----------
% succeeds if NewS is result of popping top element off S
pop([_|T], T).   % can only succeed on non-empty stacks


% top(S, E)
% ---------
% succeeds if E is top element of S
top([E|_], E).   % can only succeed on non-empty stacks


% push(S, E, NewS)
% ----------------
% succeeds if NewS is result of pushing E onto S
push(S, E, [E|S]) :- stack(S).


% isempty(S)
% ----------
% succeeds if S is an empty stack
isempty([]).

