Predicate transformer semantics have been introduced by Edsger Dijkstra in his seminal paper "Guarded commands, nondeterminacy and formal derivation of programs". They define the semantics of an imperative programming paradigm by assigning to each statement in this language a corresponding predicate transformer: a total function between two predicates on the state space of the statement. In this sense, predicate transformer semantics are a kind of denotational semantics. Actually, in guarded commands, Dijkstra uses only one kind of predicate transformer: the well-known weakest preconditions (see below).
Contents
- Definition
- Assignment
- Sequence
- Conditional
- Partial Correctness
- Total Correctness
- Non deterministic guarded commands
- Selection
- Repetition
- Specification statement or weakest precondition of procedure call
- Weakest liberal precondition
- Strongest postcondition
- Win and sin predicate transformers
- Predicate transformers properties
- Monotonic
- Strict
- Terminating
- Conjunctive
- Disjunctive
- Applications
- Weakest preconditions and strongest postconditions of imperative expressions
- Probabilistic Predicate Transformers
- References
Moreover, predicate transformer semantics are a reformulation of Floyd–Hoare logic. Whereas Hoare logic is presented as a deductive system, predicate transformer semantics (either by weakest-preconditions or by strongest-postconditions see below) are complete strategies to build valid deductions of Hoare logic. In other words, they provide an effective algorithm to reduce the problem of verifying a Hoare triple to the problem of proving a first-order formula. Technically, predicate transformer semantics perform a kind of symbolic execution of statements into predicates: execution runs backward in the case of weakest-preconditions, or runs forward in the case of strongest-postconditions.
Definition
Given a statement S, the weakest-precondition of S is a function mapping any postcondition R to a precondition P. Actually, the result of this function, denoted
More formally, let us use variable x to denote abusively the tuple of variables involved in statement S. Then, a given Hoare triple
Formally, weakest-preconditions are defined recursively over the abstract syntax of statements. Actually, weakest-precondition semantics is a continuation-passing style semantics of state transformers where the predicate in parameter is a continuation.
Assignment
We give below two equivalent weakest-preconditions for the assignment statement. In these formulas,
The first version avoids a potential duplication of E in R, whereas the second version is simpler when there is at most a single occurrence of x in R. The first version also reveals a deep duality between weakest-precondition and strongest-postcondition (see below).
An example of a valid calculation of wp (using version 2) for assignments with integer valued variable x is:
This means that in order for the postcondition x > 10 to be true after the assignment, the precondition x > 15 must be true before the assignment. This is also the "weakest precondition", in that it is the "weakest" restriction on the value of x which makes x > 10 true after the assignment.
Sequence
For example,
Conditional
As example:
Partial Correctness
Ignoring termination for a moment, we can define the rule for the weakest liberal precondition, denoted wlp, using a predicate I, called the loop invariant, typically supplied by the programmer:
This simply states that (1) the invariant must hold at the start of the loop; (2) additionally, for any initial state y, the invariant and guard taken together be strong enough to establish the weakest precondition necessary for the loop body to be able to re-establish the invariant; (3) finally, if and when the loop terminates in a given state y, the fact that the loop guard is false along with the invariant should be able to establish the required postcondition.
Total Correctness
To show total correctness, we also have to show that the loop terminates. For this we define a well-founded relation on the state space denoted "<" and called loop variant. Hence, we have:
Informally, in the above conjunction of three formulas:
In predicate transformers semantics, invariant and variant are built by mimicking the Kleene fixed-point theorem. Below, this construction is sketched in set theory. We assume that U is a set denoting the state space. First, we define a family of subsets of U denoted
Then, we define:
With these definitions,
However, in practice, such an abstract construction can not be handled efficiently by theorem provers. Hence, loop invariants and variants are provided by human users, or are inferred by some abstract interpretation procedure.
Non-deterministic guarded commands
Actually, Dijkstra's Guarded Command Language (GCL) is an extension of the simple imperative language given until here with non-deterministic statements. Indeed, GCL aims to be a formal notation to define algorithms. Non-deterministic statements represent choices left to the actual implementation (in an effective programming language): properties proved on non-deterministic statements are ensured for all possible choices of implementation. In other words, weakest-preconditions of non-deterministic statements ensure
Notice that the definitions of weakest-precondition given above (in particular for while-loop) preserve this property.
Selection
Selection is a generalization of if statement:
Here, when two guards
Repetition
Repetition is a generalization of while statement in a similar way.
Specification statement (or weakest-precondition of procedure call)
Refinement calculus extends non-deterministic statements with the notion of specification statement. Informally, this statement represents a procedure call in black box, where the body of the procedure is not known. Typically, using a syntax close to B-Method, a specification statement is written
where
The weakest-precondition of specification statement is given by:
Moreover, a statement S implements such a specification statement if and only if the following predicate is a tautology:
Indeed, in such a case, the following property is ensured for all postcondition R (this is a direct consequence of wp monotonicity, see below):
Informally, this last property ensures that any proof about some statement involving a specification remains valid when replacing this specification by any of its implementations.
Weakest liberal precondition
An important variant of the weakest precondition is the weakest liberal precondition
Strongest postcondition
Given S a statement and R a precondition (a predicate on the initial state), then
Usually, strongest-postconditions are used in partial correctness. Hence, we have the following relation between weakest-liberal-preconditions and strongest-postconditions:
For example, on assignment we have:
Above, the logical variable y represents the initial value of variable x. Hence,
On sequence, it appears that sp runs forward (whereas wp runs backward):
Win and sin predicate transformers
Leslie Lamport has suggested win and sin as predicate transformers for concurrent programming.
Predicate transformers properties
This section presents some characteristic properties of predicate transformers. Below, T denotes a predicate transformer (a function between two predicates on the state space) and P a predicate. For instance, T(P) may denote wp(S,P) or sp(S,P). We keep x as the variable of the state space.
Monotonic
Predicate transformers of interest (wp, wlp, and sp) are monotonic. A predicate transformer T is monotonic if and only if:
This property is related to the consequence rule of Hoare logic.
Strict
A predicate transformer T is strict iff:
For instance, wp is strict, whereas wlp is generally not. In particular, if statement S may not terminate then
Indeed, true is a valid invariant of that loop.
Terminating
A predicate transformer T is terminating iff:
Actually, this terminology makes sense only for strict predicate transformers: indeed,
It seems that naming this property non-aborting would be more appropriate: in total correctness, non-termination is abortion, whereas in partial correctness, it is not.
Conjunctive
A predicate transformer T is conjunctive iff:
This is the case for
Disjunctive
A predicate transformer T is disjunctive iff:
This is generally not the case of
Then,
Hence,
Whereas, the formula
The same counter-example can be reproduced using a specification statement (see above) instead:
Applications
Weakest-preconditions and strongest-postconditions of imperative expressions
In predicate transformers semantics, expressions are restricted to terms of the logic (see above). However, this restriction seems too strong for most existing programming languages, where expressions may have side effects (call to a function having a side effect), may not terminate or abort (like division by zero). There are many proposals to extend weakest-preconditions or strongest-postconditions for imperative expression languages and in particular for monads.
Among them, Hoare Type Theory combines Hoare logic for a Haskell-like language, separation logic and type theory . This system is currently implemented as a Coq library called Ynot. In this language, evaluation of expressions corresponds to computations of strongest-postconditions.
Probabilistic Predicate Transformers
Probabilistic Predicate Transformers are an extension of predicate transformers for probabilistic programs. Indeed, such programs have many applications in cryptography (hiding of information using some randomized noise), distributed systems (symmetry breaking).