Sub topic 4
Specifying behavior: preconditions, results, and axioms
This chapter assumes you can read Python classes, know what exceptions are, and have seen the idea of an ADT as a set of operations. Here you learn to state what each operation promises, without saying how it works.
Why a signature is not enough
A signature lists names, parameters, and return values. It cannot tell a client what pop() does on an empty stack, or whether pop() returns the item pushed last or first. Two classes can share the same signatures and behave in opposite ways. A behavioral specification closes that gap.
Preconditions, results, and errors
A specification for one operation has three parts:
- Precondition: what must be true before the call. The client is responsible for it.
- Result (postcondition): what is true after the call, including the return value and any change to the object.
- Error case: what happens when the precondition fails.
For pop() on a stack:
| Part | Statement |
|---|---|
| Precondition | The stack is not empty. |
| Result | Returns the top item and removes it. |
| Error case | On an empty stack, raises `IndexError`. |
The Stack interface from this tutorial records this in its docstring.
from abc import ABC, abstractmethod
class Stack(ABC):
"""A last-in, first-out collection.
push(x) then pop() returns x.
pop() and peek() raise IndexError on an empty stack.
"""
@abstractmethod
def push(self, item):
"""Add item to the top of the stack."""
@abstractmethod
def pop(self):
"""Remove and return the top item."""
@abstractmethod
def peek(self):
"""Return the top item without removing it."""
@abstractmethod
def is_empty(self):
"""Return True if the stack has no items."""The docstring states the error case. Every implementation must honor it.
Axioms
An axiom is a rule that holds for every valid use of the operations. Axioms describe behavior by relating operations to each other, so they say nothing about storage. For a stack s and item x:
push(x)followed bypop()returnsx.push(x)followed bypeek()returnsx.peek()does not change the stack.- A new stack is empty.
- After
push(x), the stack is not empty. push(x)followed bypop()leaves the stack as it was before.
Together these pin down last-in, first-out order. A queue breaks axiom 1 when it holds more than one item, because a queue's removal returns the oldest item.
Who is at fault
The precondition divides responsibility. If a client calls pop() on an empty stack, the client broke the contract. The implementation then has a defined response, here IndexError. When a spec states an error case, clients may rely on it. When a spec leaves it out, implementations may differ, and clients must not depend on any one behavior.
Common errors
- Describing storage: "pop removes the last list element" is an implementation detail. Write "removes the most recently pushed item."
- Omitting the empty case: this leaves
ListStackandLinkedStackfree to disagree. - Axioms that need internals: if an axiom mentions a list or a node, it belongs to the representation, not the ADT.
Exercise
A client calls peek() on an empty Stack. Which part of the specification covers this, and what must the implementation do?
Show answer
The error case. The precondition (stack not empty) is violated by the client, and the spec says peek() raises IndexError on an empty stack, so every implementation must raise it.
Exercise
Write one axiom, using only stack operations, that says peek() has no side effects.
Show answer
For any non-empty stack, calling peek() and then pop() returns the same item that peek() returned, and the stack's contents are unchanged by the peek() call. Equivalently, two consecutive peek() calls return the same item.