Definitionintro
Specification
Also called Formal specification, Problem specification.
A precise statement of what an algorithm must do, relating inputs to required outputs.
A specification is a precise statement of what a program or algorithm must do. It lists the legal inputs with a precondition and it states the right outputs for each of them with a postcondition. A program is correct when it meets its specification.
Definition
Let be a set of inputs and a set of outputs. A specification is a pair where is a precondition and is a postcondition. Write for and for . A program meets when
This is total correctness. The program is partially correct for when . The stop is a hypothesis here and not a goal. See partial correctness. Total correctness is partial correctness plus termination on each with . A specification says what the program computes and does not say how. Many programs can meet the same specification.
The relation may allow several outputs for one input. Then the specification is non-deterministic, and a program may return any of them. If for each with there is exactly one with , then defines a function on . The opposite case is a pitfall. If some with has no with , then no program meets . A good specification is satisfiable: .
Examples and non-examples
- Integer square root. . is . is . For each there is exactly one such . Proof. Each solution is non-negative: from we get , so and . Existence: the set of with holds , and each element is at most because for an integer , so the set has a largest element . Then is not in the set, so . Uniqueness: if both satisfied for , then and , a contradiction. The program ISQRT of the page on correctness meets it.
- Sorting. is the set of finite arrays of numbers. is true. says that is a permutation of and . See the sorting problem.
- Search in a sorted array (non-deterministic). : is non-decreasing. : either and (with the length of ), or and does not occur in . When occurs twice, two outputs are right. The binary search returns one of them.
- Non-example: too weak. is . The constant program meets it for each . The pair is a specification in the sense above, but it does not describe integer square root. Compare : both and meet it, and only meets the exact one.
- Non-example: not satisfiable. Take = true for integer square root, with the same . For no has , so no program meets this pair. The precondition removes the problem.
- Non-example: not precise. "Return a number close to " is not a specification. No test or proof can decide whether is close enough for .
- Non-example: a method. "Search for by halving an interval" describes an algorithm. It does not say what the result must satisfy.
Related definitions
- Precondition: the first half of a specification, the assumption on the input.
- Postcondition: the second half, the guarantee on the output.
- Hoare triple: a specification written as for a program text .
- Partial correctness and total correctness: the two ways in which a program can meet a specification.
- Design by contract: a method where the specification of each routine is part of its code.
- Weakest precondition: the largest precondition under which a program meets a given postcondition.
References
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag 154J38 Edit this page History Report an error Markdown JSON