Sub topic 13
The abstraction function
This chapter assumes you know what a representation invariant is (the previous chapter) and can read basic Python classes.
Two worlds, one mapping
An abstract data type (ADT) has two views. Clients see the abstract value, such as the set {1, 2, 3}. The implementation holds a concrete value, such as the list [3, 1, 2]. The abstraction function (AF) maps each valid concrete value to the abstract value it stands for.
The representation invariant tells you which concrete values are legal. The AF tells you what each legal value means. Together they define correctness. An operation is correct if it takes a legal concrete value to a legal one, and the abstract value changes exactly as the specification says.
The AF for IntSet
IntSet stores its elements in a list. Its AF is: the list [a, b, c] represents the set {a, b, c}. This is a comment, not code. Python can't run it, but you can write it down and use it to reason about the class.
The AF is usually many-to-one. The lists [1, 2] and [2, 1] are different concrete values, but both map to {1, 2}. The code below makes that visible by defining equality on the abstract value.
class IntSet:
"""A set of integers.
Abstraction function: the list [a, b, c] represents the set {a, b, c}.
Representation invariant: _items contains no duplicates.
"""
def __init__(self):
self._items = []
self._check_rep()
def _check_rep(self):
assert len(self._items) == len(set(self._items))
def add(self, x):
if x not in self._items:
self._items.append(x)
self._check_rep()
def contains(self, x):
return x in self._items
def size(self):
return len(self._items)
def __eq__(self, other):
# Equality compares abstract values, so it applies the AF to both sides.
return isinstance(other, IntSet) and set(self._items) == set(other._items)
__hash__ = None # a mutable set should not be hashable
a = IntSet()
a.add(1)
a.add(2)
b = IntSet()
b.add(2)
b.add(1)
print(a._items, b._items) # [1, 2] [2, 1]
print(a == b) # TrueThe two lists differ, yet a == b is True. Comparing _items directly would have given False and exposed an implementation detail.
What the AF is used for
- Writing specifications of operations.
add(x)must change the abstract value from S to S ∪ {x}. You check this by applying the AF before and after the call. - Defining equality. As shown above,
__eq__should follow the AF, not compare raw fields. - Changing representations safely. If you switch to a sorted list, the AF stays the same. Only the invariant gains a rule ("sorted"), and clients notice nothing.
Common errors
- Leaving the AF unwritten. Without it, nobody can say whether
removeis right. State the AF in the class docstring. - Equality on the representation. Comparing
_itemsmakes equal sets look different. It also breaks if the representation later changes. - Treating the AF as total. It is defined only for values that satisfy the invariant. A list with duplicates still maps somewhere, but not to anything the specification promises.
- Confusing it with the invariant. The invariant is a yes/no test on concrete values. The AF is a mapping to abstract values.
Exercise
A Bag ADT (a set that counts repeats) is stored as a list of pairs [(item, count), ...]. Write an abstraction function in one sentence, and give two different concrete values that map to the same abstract value.
Show answer
AF: the list of pairs [(x1, n1), (x2, n2), ...] represents the bag that contains x1 n1 times, x2 n2 times, and so on. The concrete values [("a", 2), ("b", 1)] and [("b", 1), ("a", 2)] are different lists but both represent the same bag. A reasonable invariant is that each item appears in at most one pair and every count is positive.