Algorithm Search⌘K

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 XX be a set of inputs and YY a set of outputs. A specification is a pair (P,Q)(P, Q) where P⊆XP \subseteq X is a precondition and Q⊆X×YQ \subseteq X \times Y is a postcondition. Write P(x)P(x) for x∈Px \in P and Q(x,y)Q(x, y) for (x,y)∈Q(x, y) \in Q. A program AA meets (P,Q)(P, Q) when

∀x∈X: P(x)⇒(A stops on x ∧ Q(x,A(x))).\forall x \in X : \ P(x) \Rightarrow \big( A \text{ stops on } x \ \wedge \ Q(x, A(x)) \big).

This is total correctness. The program is partially correct for (P,Q)(P, Q) when ∀x∈X:(P(x)∧A stops on x)⇒Q(x,A(x))\forall x \in X : \big( P(x) \wedge A \text{ stops on } x \big) \Rightarrow Q(x, A(x)). The stop is a hypothesis here and not a goal. See partial correctness. Total correctness is partial correctness plus termination on each xx with P(x)P(x). A specification says what the program computes and does not say how. Many programs can meet the same specification.

The relation QQ may allow several outputs for one input. Then the specification is non-deterministic, and a program may return any of them. If for each xx with P(x)P(x) there is exactly one yy with Q(x,y)Q(x, y), then QQ defines a function on PP. The opposite case is a pitfall. If some xx with P(x)P(x) has no yy with Q(x,y)Q(x, y), then no program meets (P,Q)(P, Q). A good specification is satisfiable: ∀x (P(x)⇒∃y Q(x,y))\forall x \,(P(x) \Rightarrow \exists y \, Q(x, y)).

Examples and non-examples

  • Integer square root. X=Y=ZX = Y = \mathbb{Z}. P(n)P(n) is n≥0n \ge 0. Q(n,r)Q(n, r) is r2≤n<(r+1)2r^2 \le n < (r+1)^2. For each n≥0n \ge 0 there is exactly one such rr. Proof. Each solution is non-negative: from r2≤n<(r+1)2r^2 \le n < (r+1)^2 we get r2<(r+1)2r^2 < (r+1)^2, so 2r+1>02r + 1 > 0 and r≥0r \ge 0. Existence: the set of r≥0r \ge 0 with r2≤nr^2 \le n holds 00, and each element is at most max⁡(n,1)\max(n, 1) because r≤r2r \le r^2 for an integer r≥1r \ge 1, so the set has a largest element rr. Then r+1r + 1 is not in the set, so (r+1)2>n(r+1)^2 > n. Uniqueness: if 0≤r1<r20 \le r_1 < r_2 both satisfied QQ for nn, then r1+1≤r2r_1 + 1 \le r_2 and (r1+1)2≤r22≤n<(r1+1)2(r_1+1)^2 \le r_2^2 \le n < (r_1+1)^2, a contradiction. The program ISQRT of the page on correctness meets it.
  • Sorting. X=YX = Y is the set of finite arrays of numbers. PP is true. Q(A,B)Q(A, B) says that BB is a permutation of AA and B[0]≤B[1]≤⋯B[0] \le B[1] \le \cdots. See the sorting problem.
  • Search in a sorted array (non-deterministic). P(A,key)P(A, key): AA is non-decreasing. Q(A,key,i)Q(A, key, i): either 0≤i<m0 \le i < m and A[i]=keyA[i] = key (with mm the length of AA), or i=−1i = -1 and keykey does not occur in AA. When keykey occurs twice, two outputs are right. The binary search returns one of them.
  • Non-example: too weak. Q(n,r)Q(n, r) is r2≤nr^2 \le n. The constant program r=0r = 0 meets it for each n≥0n \ge 0. The pair (P,Q)(P, Q) is a specification in the sense above, but it does not describe integer square root. Compare n=10n = 10: both r=0r = 0 and r=3r = 3 meet it, and only r=3r = 3 meets the exact one.
  • Non-example: not satisfiable. Take PP = true for integer square root, with the same QQ. For n=−1n = -1 no rr has r2≤−1r^2 \le -1, so no program meets this pair. The precondition n≥0n \ge 0 removes the problem.
  • Non-example: not precise. "Return a number close to n\sqrt n" is not a specification. No test or proof can decide whether r=2r = 2 is close enough for n=10n = 10.
  • Non-example: a method. "Search for rr by halving an interval" describes an algorithm. It does not say what the result must satisfy.

References

  1. Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition, Section 1.1
  2. C. A. R. Hoare, An axiomatic basis for computer programming, Communications of the ACM 12(10) (1969), pp. 576 to 580

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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/154J38/text.yaml