In the ideal, a software engineer builds software the way an
engineer builds a bridge: from a specification whose properties
can be ANALYSED before construction. Testing samples the input
space; proof covers it entirely.
THE MOTIVATION, stated as a comparison:
TESTING demonstrates the presence of defects on the
inputs you tried. Coverage is a sample.
INSPECTION human reading; catches much, but humans
reason poorly about concurrency and about
very large case-splits.
PROOF establishes a property for ALL inputs. If
the specification and the proof are right,
that class of defect cannot exist.
A FORMAL SPECIFICATION is one written in a language with a
MATHEMATICALLY DEFINED SYNTAX AND SEMANTICS, so that:
Β· it is UNAMBIGUOUS β there is exactly one reading, which is
the property natural language cannot provide (recall the
requirements topic)
Β· it can be ANALYSED MECHANICALLY β inconsistencies and
incompletenesses can be detected by tool
Β· properties can be PROVEN to follow from it
Β· it can be REFINED to code with the refinement itself
proven correct
WHERE THE PAYOFF IS LARGEST: writing a formal specification
forces the questions nobody asked. Industrial experience
reports repeatedly find that most of the value comes not from
the proof but from the ACT OF SPECIFYING β the ambiguities
surfaced while formalising are defects removed at requirements
cost.
TWO STYLES OF FORMAL SPECIFICATION:
ALGEBRAIC APPROACH β the system is described in terms of
OPERATIONS and their RELATIONSHIPS, given as axioms. Suited to
specifying interfaces and abstract data types.
Example, a stack:
pop(push(s, x)) = s
top(push(s, x)) = x
isEmpty(new()) = true
isEmpty(push(s, x)) = false
pop(new()) = ERROR
Note: no state is described, only the RELATIONS between
operations. Languages: Larch, OBJ, Lotos.
MODEL-BASED APPROACH β the system is modelled using
mathematical constructs such as SETS, SEQUENCES, RELATIONS and
FUNCTIONS, and operations are defined by how they change the
state. Languages: Z, VDM, B, Alloy.
Example, an air-traffic sector as a set:
Sector : β Aircraft
capacity : β
INVARIANT #Sector β€ capacity
Enter(a) β pre a β Sector β§ #Sector < capacity
post Sector' = Sector βͺ {a}
Leave(a) β pre a β Sector
post Sector' = Sector \ {a}
The INVARIANT is the point: it must hold before and after
every operation, and proving that it does is what rules out a
whole class of failure.
THE HONEST COSTS:
Β· REQUIRES SPECIALISED TRAINING. Few engineers, and almost no
customers, can read Z or B β so a formal specification
CANNOT SERVE as the agreed document with the customer. You
end up maintaining two specifications.
Β· EXPENSIVE. Specification and proof effort is substantial,
and proof effort grows quickly with system size.
Β· POORLY SUITED to specifying user interfaces and user
interaction, which is a large part of many systems.
Β· TOOL SUPPORT is far behind mainstream development tooling.
Β· IT PROVES THE CODE MATCHES THE SPECIFICATION β not that
the specification is what the customer wanted. Formal
methods address VERIFICATION, and validation remains a
human problem. A proven-correct implementation of the wrong
requirement is still worthless.
Β· THE ENVIRONMENT IS NOT PROVEN. The compiler, the OS, the
hardware and the libraries are outside the proof. seL4
addresses this for a microkernel by proving down to the
binary, which shows both that it is possible and how much
work it is.
WHERE FORMAL METHODS PAY FOR THEMSELVES β the pattern is
consistent: high cost of failure, plus a small, sharply defined
core.
railway signalling and interlocking (the B method, Paris
Line 14 and other metros)
avionics and flight control
medical device control
security kernels and separation kernels (seL4, proven
functionally correct)
cryptographic protocol design and verification
hardware and processor design β where formal equivalence
checking is now completely routine and unremarkable
distributed protocol design (TLA+, model-checking
consensus and replication protocols)
THE MODERN PRAGMATIC POSITION β "LIGHTWEIGHT FORMAL METHODS":
do not attempt to prove the whole system. Apply formality
where the state space is large and human intuition fails:
Β· MODEL CHECKING a concurrency or distributed protocol,
where the interleavings are too numerous to test but few
enough for a tool to exhaust
Β· TYPE SYSTEMS as machine-checked lightweight proofs β a
strong type system is a formal method that ordinary
engineers use daily without noticing
Β· STATIC ANALYSIS and abstract interpretation to prove the
absence of specific defect classes (null dereference,
buffer overrun, integer overflow)
Β· DESIGN-BY-CONTRACT β preconditions, postconditions and
invariants as executable assertions, checked at run time
rather than proven. Most of the specification discipline,
at a small fraction of the cost, and it is the form of
formality most worth adopting in ordinary work.
THAT LAST BULLET IS THE PRACTICAL TAKEAWAY: the habit of
writing down a precondition, a postcondition and an invariant
for each operation captures most of the benefit β the
ambiguities surface β without requiring anyone to construct a
proof.
The frame condition is the most instructive detail here. "Nothing else changes" is never written in prose and is a genuine defect class β an operation that modifies something it should not. The formal proof failed without it, which is a concrete demonstration of formality finding a specification gap rather than merely restating one.
π Go further: the single most accessible entry point is model checking with TLA+. You specify a concurrent or distributed algorithm as a state machine and the checker exhaustively explores every reachable interleaving β including the one-in-a-billion ordering that your tests will never produce but production will. Amazon's published experience is the standard reference: engineers found serious bugs in already-designed systems, and reported that the specification process itself changed how they thought about the designs. It is formality applied exactly where human reasoning is weakest. Search "how Amazon web services uses formal methods".
π‘ Exam angle: explain what a formal specification is and its benefits (unambiguous, mechanically analysable, provable, refinable). Distinguish the algebraic approach (operations and their relationships, suited to interfaces and abstract data types) from the model-based approach (sets, sequences, relations, with state and invariants) and name languages of each β Larch/OBJ/Lotos versus Z/VDM/B. Describe Cleanroom software engineering: defect avoidance, formal specification, correctness verification instead of developer unit testing, statistical usage testing. State the limitations, especially that proof establishes conformance to the specification and not that the specification is correct, and that customers cannot read formal notation.
Syllabus points
Formal verification approaches
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Related topics in Testing, Cost Estimation, Quality & Configuration Management