HOME

TheInfoList



OR:

Predicate transformer semantics were introduced by
Edsger Dijkstra Edsger Wybe Dijkstra ( ; ; 11 May 1930 – 6 August 2002) was a Dutch computer scientist, programmer, software engineer, systems scientist, and science essayist. He received the 1972 Turing Award for fundamental contributions to developing progra ...
in his seminal paper " Guarded commands, nondeterminacy and formal derivation of programs". They define the semantics of an
imperative programming In computer science, imperative programming is a programming paradigm of software that uses statements that change a program's state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program ...
paradigm by assigning to each ''statement'' in this language a corresponding ''predicate transformer'': a
total function In mathematics, a partial function from a set to a set is a function from a subset of (possibly itself) to . The subset , that is, the domain of viewed as a function, is called the domain of definition of . If equals , that is, if is ...
between two ''
predicates Predicate or predication may refer to: * Predicate (grammar), in linguistics * Predication (philosophy) * several closely related uses in mathematics and formal logic: **Predicate (mathematical logic) ** Propositional function **Finitary relation, ...
'' on the state space of the statement. In this sense, predicate transformer semantics are a kind of
denotational semantics In computer science, denotational semantics (initially known as mathematical semantics or Scott–Strachey semantics) is an approach of formalizing the meanings of programming languages by constructing mathematical objects (called ''denotations' ...
. Actually, in guarded commands, Dijkstra uses only one kind of predicate transformer: the well-known weakest preconditions (see below). Moreover, predicate transformer semantics are a reformulation of
Floyd–Hoare logic Hoare logic (also known as Floyd–Hoare logic or Hoare rules) is a formal system with a set of logical rules for reasoning rigorously about the correctness of computer programs. It was proposed in 1969 by the British computer scientist and log ...
. Whereas Hoare logic is presented as a
deductive system A formal system is an abstract structure used for inferring theorems from axioms according to a set of rules. These rules, which are used for carrying out the inference of theorems from axioms, are the logical calculus of the formal system. A form ...
, 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 In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing ...
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 In computer science, symbolic execution (also symbolic evaluation or symbex) is a means of analyzing a program to determine what inputs cause each part of a program to execute. An interpreter follows the program, assuming symbolic values for in ...
of statements into predicates: execution runs ''backward'' in the case of weakest-preconditions, or runs ''forward'' in the case of strongest-postconditions.


Weakest preconditions


Definition

For a statement ''S'' and a
postcondition In computer programming, a postcondition is a condition or predicate that must always be true just after the execution of some section of code or after an operation in a formal specification. Postconditions are sometimes tested using assertions ...
''R'', a weakest precondition is a predicate ''Q'' such that for any
precondition In computer programming, a precondition is a condition or predicate that must always be true just prior to the execution of some section of code or before an operation in a formal specification. If a precondition is violated, the effect of th ...
, \ S \ if and only if P \Rightarrow Q . In other words, it is the "loosest" or least restrictive requirement needed to guarantee that ''R'' holds after ''S''. Uniqueness follows easily from the definition: If both ''Q'' and ''Q' '' are weakest preconditions, then by the definition \ S \ so Q' \Rightarrow Q and \ S \ so Q \Rightarrow Q' , and thus Q=Q' . We often use wp(S, R) to denote the weakest precondition for statement ''S'' with repect to a postcondition ''R''.


Conventions

We use '' T '' to denote the predicate that is everywhere true and '' F '' to denote the one that is everywhere false. We shouldn't at least conceptually confuse ourselves with a Boolean expression defined by some language syntax, which might also contain true and false as Boolean scalars. For such scalars we need to do a type coercion such that we have T = predicate(true) and F = predicate(false). Such a promotion is carried out often casually, so people tend to take T as true and F as false.


Skip


Abort


Assignment

We give below two equivalent weakest-preconditions for the assignment statement. In these formulas, R \leftarrow E/math> is a copy of ''R'' where free occurrences of ''x'' are replaced by ''E''. Hence, here, expression ''E'' is implicitly coerced into a ''valid term'' of the underlying logic: it is thus a ''pure'' expression, totally defined, terminating and without side effect. * version 1: * version 2: Provided that '' E '' is well defined, we just apply the so-called ''one-point'' rule on version 1. Then The first version avoids a potential duplication of ''x'' 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: :\begin wp(x := x - 5, x > 10) & = & x - 5 > 10 \\ & \Leftrightarrow & x > 15 \end 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, :\begin wp(x:=x-5;x:=x*2\ ,\ x>20) & = & wp(x:=x-5,wp(x:=x*2, x > 20))\\ & = & wp(x:=x-5,x*2 > 20)\\ & = & (x-5)*2 > 20\\ & = & x > 15 \end


Conditional

As example: :\begin wp(\texttt\ x < y\ \texttt\ x:=y\ \texttt\;\;\texttt\;\;\texttt,\ x \geq y) & = & (x < y \Rightarrow wp(x:=y,x\geq y))\ \wedge\ (\neg (x


While loop


Partial Correctness

Ignoring termination for a moment, we can define the rule for the ''weakest liberal precondition'', denoted ''wlp'', using a predicate ''INV'', called the
loop invariant In computer science, a loop invariant is a property of a program loop that is true before (and after) each iteration. It is a logical assertion, sometimes checked within the code by an assertion call. Knowing its invariant(s) is essential in ...
, typically supplied by the programmer:


Total Correctness

To show total correctness, we also have to show that the loop terminates. For this we define a
well-founded relation In mathematics, a binary relation ''R'' is called well-founded (or wellfounded) on a class ''X'' if every non-empty subset ''S'' ⊆ ''X'' has a minimal element with respect to ''R'', that is, an element ''m'' not related by ''s&nbs ...
on the state space denoted as (wfs, <) and define a variant function '' vf '' , such that we have: Informally, in the above conjunction of three formulas: * the first one means that the variant must be part of the well-founded relation before entering the loop; * the second one means that the body of the loop (i.e. statement ''S'') must preserve the invariant and reduce the variant; * the last one means that the loop postcondition ''R'' must be established when the loop finishes. However, the conjunction of those three is not a necessary condition. Exactly, we have


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 * that there exists a terminating execution (e.g. there exists an implementation), * and, that the final state of all terminating execution satisfies the postcondition. 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 E_i and E_j are simultaneously true, then execution of this statement can run any of the associated statement S_i or S_j.


Repetition

Repetition is a generalization of while statement in a similar way.


Specification statement

Refinement calculus extends GCL with the notion of ''specification statement''. Syntactically, we prefer to write a specification statement as x:l re, post which specifies a computation that starts in a state satisfying ''pre'' and is guaranteed to end in a state satisfying ''post'' by changing only ''x''. We call l a logical constant employed to aid in a specification. For example, we can specify a computation that increment x by 1 as x:l = l, x = l+1 Another example is a computation of a square root of an integer. x:l = l^2, x = l The specification statement appears like a primitive in the sense that it does not contain other statements. However, it is very expressive, as ''pre'' and ''post'' are arbitrary predicates. Its weakest precondition is as follows. It combines Morgan's syntactic idea with the sharpness idea by Bijlsma, Matthews and Wiltink. The very advantage of this is its capability of defining wp of goto L and other jump statements.


Goto statement

Formalization of jump statements like ''goto L'' takes a very long bumpy process. A common belief seems to indicate the goto statement could only be argued operationally. This is probably due to a failure to recognize that ''goto L'' is actually miraculous (i.e. non-strict) and does not follow Dijkstra's coined Law of Miracle Excluded, as stood in itself. But it enjoys an extremely simple operational view from the weakest precondition perspective, which was unexpected. We define For ''goto L'' execution transfers control to label ''L'' at which the weakest precondition has to hold. The way that ''wpL'' is referred to in the rule should not be taken as a big surprise. It is just ''wp(L:S, Q )'' for some ''Q'' computed to that point. This is like any wp rules, using constituent statements to give wp definitions, even though ''goto L'' appears a primitive. The rule does not require the uniqueness for locations where ''wpL'' holds within a program, so theoretically it allows the same label to appear in multiple locations as long as the weakest precondition at each location is the same wpL. The goto statement can jump to any of such locations. This actually justifies that we could place the same labels at the same location multiple times, as ''S(L:L: S1)'', which is the same as ''S(L: S1)''. Also, it does not imply any scoping rule, thus allowing a jump into a loop body, for example. Let us calculate wp of the following program S, which has a jump into the loop body. wp(do x > 0 → L: x := x-1 od; if x < 0 → x := -x; goto L ⫿ x ≥ 0 → skip fi, post) = wp(do x > 0 → L: x := x-1 od, (x<0 ∧ wp(x := -x; goto L, post)) ∨ (x ≥ 0 ∧ post) = wp(do x > 0 → L: x := x-1 od, x<0 ∧ wpL(x ← -x) ∨ x≥0 ∧ post) = the strongest solution of Z: Z ≡ x > 0 ∧ wp(L: x := x-1, Z) ∨ x < 0 ∧ wpL(x ← -x) ∨ x=0 ∧ post = the strongest solution of Z: Z ≡ x > 0 ∧ Z(x ← x-1) ∨ x < 0 ∧ Z(x ← x-1) (x ← -x) ∨ x=0 ∧ post = the strongest solution of Z: Z ≡ x > 0 ∧ Z(x ← x-1) ∨ x < 0 ∧ Z(x ← -x-1) ∨ x=0 ∧ post = post(x ← 0) Therefore, wp(S, post ) = post(x ← 0) .


Other predicate transformers


Weakest liberal precondition

An important variant of the weakest precondition is the weakest liberal precondition wlp(S, R), which yields the weakest condition under which ''S'' either does not terminate or establishes ''R''. It therefore differs from ''wp'' in not guaranteeing termination. Hence it corresponds to
Hoare logic Hoare logic (also known as Floyd–Hoare logic or Hoare rules) is a formal system with a set of logical rules for reasoning rigorously about the correctness of computer programs. It was proposed in 1969 by the British computer scientist and log ...
in partial correctness: for the statement language given above, ''wlp'' differs with ''wp'' only on while-loop, in not requiring a variant (see above).


Strongest postcondition

Given ''S'' a statement and ''R'' a
precondition In computer programming, a precondition is a condition or predicate that must always be true just prior to the execution of some section of code or before an operation in a formal specification. If a precondition is violated, the effect of th ...
(a predicate on the initial state), then sp(S, R) is their strongest-postcondition: it implies any postcondition satisfied by the final state of any execution of S, for any initial state satisfying R. In other words, a Hoare triple \ S \ is provable in Hoare logic if and only if the predicate below hold: :\forall x, sp(S,P) \Rightarrow Q Usually, strongest-postconditions are used in partial correctness. Hence, we have the following relation between weakest-liberal-preconditions and strongest-postconditions: :(\forall x, P \Rightarrow wlp(S,Q)) \ \Leftrightarrow\ (\forall x, sp(S,P) \Rightarrow Q) For example, on assignment we have: Above, the logical variable ''y'' represents the initial value of variable ''x''. Hence, : sp(x := x - 5, x > 15)\ =\ \exists y, x = y - 5 \wedge y > 15 \ \Leftrightarrow \ x > 10 On sequence, it appears that ''sp'' runs forward (whereas ''wp'' runs backward):


Win and sin predicate transformers

Leslie Lamport Leslie B. Lamport (born February 7, 1941 in Brooklyn) is an American computer scientist and mathematician. Lamport is best known for his seminal work in distributed systems, and as the initial developer of the document preparation system LaTeX an ...
has suggested ''win'' and ''sin'' as ''predicate transformers'' for
concurrent programming Concurrent means happening at the same time. Concurrency, concurrent, or concurrence may refer to: Law * Concurrence, in jurisprudence, the need to prove both ''actus reus'' and ''mens rea'' * Concurring opinion (also called a "concurrence"), ...
.


Predicate transformers properties

This section presents some characteristic properties of predicate transformers. Below, ''S'' denotes a predicate transformer (a function between two predicates on the state space) and ''P'' a predicate. For instance, ''S(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 In mathematics, a monotonic function (or monotone function) is a function between ordered sets that preserves or reverses the given order. This concept first arose in calculus, and was later generalized to the more abstract setting of ord ...
. A predicate transformer ''S'' is monotonic if and only if: :(\forall x: P : Q) \Rightarrow (\forall x: S(P): S(Q)) This property is related to the consequence rule of Hoare logic.


Strict

A predicate transformer ''S'' is strict iff: :S(\texttt)\ \Leftrightarrow\ \texttt For instance, ''wp'' is artificially made strict, whereas ''wlp'' is generally not. In particular, if statement ''S'' may not terminate then wlp(S,\texttt) is satisfiable. We have :wlp(\texttt\ \texttt\ \texttt\ \texttt\ \texttt, \texttt) \ \Leftrightarrow \texttt Indeed, T is a valid invariant of that loop. The non-strict but monotonic or conjunctive predicate transformers are called miraculous and can also be used to define a class of programming constructs, in particular, jump statements, which Dijkstra cared less about. Those jump statements include straight goto L, break and continue in a loop and return statements in a procedure body, exception handling, etc. It turns out that all jump statements are executable miracles, i.e. they can be implemented but not strict.


Terminating

A predicate transformer ''S'' is terminating iff: :S(\texttt)\ \Leftrightarrow\ \texttt Actually, this terminology makes sense only for strict predicate transformers: indeed, wp(S,\texttt) is the weakest-precondition ensuring termination of ''S''. 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 ''S'' is conjunctive iff: :S(P \wedge Q)\ \Leftrightarrow\ S(P) \wedge S(Q) This is the case for wp(S,.), even if statement ''S'' is non-deterministic as a selection statement or a specification statement.


Disjunctive

A predicate transformer ''S'' is disjunctive iff: :S(P \vee Q)\ \Leftrightarrow\ S(P) \vee S(Q) This is generally not the case of wp(S,.) when ''S'' is non-deterministic. Indeed, consider a non-deterministic statement ''S'' choosing an arbitrary boolean. This statement is given here as the following ''selection statement'': :S\ =\ \texttt\ \texttt \rightarrow x:=0\ ! \texttt \rightarrow x:=1\ \texttt Then, wp(S,R) reduces to the formula R \leftarrow 0\wedge R \leftarrow 1/math>. Hence, wp(S,\ x=0 \vee x=1) reduces to the ''tautology'' (0=0 \vee 0=1) \wedge (1=0 \vee 1=1) Whereas, the formula wp(S, x=0) \vee wp(S,x=1) reduces to the ''wrong proposition'' (0=0 \wedge 1=0) \vee (1=0 \wedge 1=1).


Applications

* Computations of weakest-preconditions are largely used to statically check assertions in programs using a theorem-prover (like SMT-solvers or
proof assistants In computer science and mathematical logic, a proof assistant or interactive theorem prover is a software tool to assist with the development of formal proofs by human-machine collaboration. This involves some sort of interactive proof editor ...
): see Frama-C or
ESC/Java2 ESC/Java (and more recently ESC/Java2), the "Extended Static Checker for Java," is a programming tool that attempts to find common run-time errors in Java programs at compile time. The underlying approach used in ESC/Java is referred to as exten ...
. * Unlike many other semantic formalisms, predicate transformer semantics was not designed as an investigation into foundations of computation. Rather, it was intended to provide programmers with a methodology to develop their programs as "correct by construction" in a "calculation style". This "top-down" style was advocated by Dijkstra and N. Wirth. It has been formalized further by R.-J. Back and others in the refinement calculus. Some tools like
B-Method The B method is a method of software development based on B, a tool-supported formal method based on an abstract machine notation, used in the development of computer software. Overview B was originally developed in the 1980s by Jean-Raymond Abr ...
now provide
automated reasoning In computer science, in particular in knowledge representation and reasoning and metalogic, the area of automated reasoning is dedicated to understanding different aspects of reasoning. The study of automated reasoning helps produce computer prog ...
in order to promote this methodology. * In the meta-theory of
Hoare logic Hoare logic (also known as Floyd–Hoare logic or Hoare rules) is a formal system with a set of logical rules for reasoning rigorously about the correctness of computer programs. It was proposed in 1969 by the British computer scientist and log ...
, weakest-preconditions appear as a key notion in the proof of relative completeness.


Beyond predicate transformers


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 Hoare logic (also known as Floyd–Hoare logic or Hoare rules) is a formal system with a set of logical rules for reasoning rigorously about the correctness of computer programs. It was proposed in 1969 by the British computer scientist and log ...
for a
Haskell Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lang ...
-like language,
separation logic In computer science, separation logic is an extension of Hoare logic, a way of reasoning about programs. It was developed by John C. Reynolds, Peter O'Hearn, Samin Ishtiaq and Hongseok Yang, drawing upon early work by Rod Burstall. The assertion ...
and
type theory In mathematics, logic, and computer science, a type theory is the formal presentation of a specific type system, and in general type theory is the academic study of type systems. Some type theories serve as alternatives to set theory as a found ...
. This system is currently implemented as a
Coq Coq is an interactive theorem prover first released in 1989. It allows for expressing mathematical assertions, mechanically checks proofs of these assertions, helps find formal proofs, and extracts a certified program from the constructive proof ...
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 Cryptography, or cryptology (from grc, , translit=kryptós "hidden, secret"; and ''graphein'', "to write", or ''-logia'', "study", respectively), is the practice and study of techniques for secure communication in the presence of adve ...
(hiding of information using some randomized noise),
distributed systems A distributed system is a system whose components are located on different networked computers, which communicate and coordinate their actions by passing messages to one another from any system. Distributed computing is a field of computer sci ...
(symmetry breaking).


See also

*
Axiomatic semantics Axiomatic semantics is an approach based on mathematical logic for proving the correctness of computer programs. It is closely related to Hoare logic. Axiomatic semantics define the meaning of a command in a program by describing its effect on as ...
— includes predicate transformer semantics * Dynamic logic — where predicate transformers appear as modalities *
Formal semantics of programming languages In programming language theory, semantics is the rigorous mathematical study of the meaning of programming languages. Semantics assigns computational meaning to valid strings in a programming language syntax. Semantics describes the processes ...
— an overview


Notes


References

* * * * — A systematic introduction to a version of the guarded command language with many worked examples * — A more abstract, formal and definitive treatment * {{DEFAULTSORT:Predicate Transformer Semantics Formal methods Program logic Edsger W. Dijkstra