The stack is one of the simplest linear data structures whose access protocol is described as last-in-first-out (or, equivalently as first-in-last-out) Stacks are used extensively in many computer programs and typical applications include expression evaluation, parsing, and parameter passing.
|
Type: |
STACK |
|||||||||||||||
|
Sets: |
STACK, Object, Boolean |
|||||||||||||||
|
Syntax: |
|
|||||||||||||||
|
|
|
|||||||||||||||
|
Axioms: |
|
|||||||||||||||
|
|
∀ x Î (Object - {error}); s ∈ (STACK - {error}) |
|||||||||||||||
|
|
the following expressions yield true |
|||||||||||||||
|
|
|
We will refer to a particular stack axiom by prefixing its number with the letter S. Axiom S4 describes the characteristic behaviour of a stack. The functions which return stacks are constructor functions. The functions which return true or false are predicates. A function is strict if it returns error whenever any of its arguments is error.
If we assume that integers are contained in the type Object then the following expressions represent valid stacks.
It is possible to simplify the last three examples by applying the stack axioms to reduce the stack expressions. We show the steps involved for each case below.
pop (push (2, push (1, nilStack)))
|
|
pop (push (2, push (1, nilStack))) |
|
|
≡ |
push (1, nilStack) |
axiom S4 |
push (3, pop (push (2, push (1, nilStack))))
|
|
push (3, pop (push (2, push (1, nilStack)))) |
|
|
≡ |
push (3, push (1, nilStack)) |
axiom S4 |
push (top (push (2, push (1, nilStack))), pop (push (1, nilStack)))
|
|
push (top (push (2, push (1, nilStack))), pop (push (1, nilStack))) |
|
|
≡ |
push (top (push (2, push (1, nilStack))), nilStack) |
axiom S4 |
|
≡ |
push (2, nilStack) |
axiom S3 |
The following expressions do not represent valid stacks:
We will demonstrate that they are invalid by showing that each one reduces to error.
pop (nilStack)
|
|
pop (nilStack) |
|
|
≡ |
error |
axiom S6 |
push (2, pop (nilStack))
|
|
push (2, pop (nilStack)) |
|
|
≡ |
push (2, error) |
axiom S6 |
|
≡ |
error |
axiom S7 |
push (top (nilStack), nilStack)
|
|
push (top (nilStack), nilStack) |
|
|
≡ |
push (error, nilStack) |
axiom S5 |
|
≡ |
error |
axiom S7 |
push (2, pop (pop (push (1, nilStack))))
|
|
push (2, pop (pop (push (1, nilStack)))) |
|
|
≡ |
push (2, pop (nilStack)) |
axiom S4 |
|
≡ |
push (2, error) |
axiom S5 |
|
≡ |
error |
axiom S7 |
The examples in the previous section indicate that every valid stack may be reduced to a normal form. The stack axioms provide the rules by which stack expressions can be reduced.
Definition – Reduced Stack Expression
|
A stack expression is in reduced form (i.e. it is a reduced stack expression) if it is either nilStack or an expression of the form push (x, s) where x denotes some valid object, and s is a reduced stack expression. |
By using the axioms, all stack expressions can either be reduced or have the value error.
Exercises
Reduce each of the following stack expressions. You
should indicate which axiom you are using at each stage of the reduction.