Predicate Transformers

Overview

Define {Q}S{R} as the predicate:

If execution of S is begun in a state satisfying Q, then it is guaranteed to terminate in a finite amount of time in a state satisfying R.

Weakest Precondition

For any command S and predicate R, we define the weakest precondition of S with respect to R, denoted wp(S,R), as

the set of all states such that execution of S begun in any one of them is guaranteed to terminate in a finite amount of time in a state satisfying R.

Expression {Q}S{R} is equivalent to Qwp(S,R).

Law of the Excluded Miracle

Given any command S, wp(S,F)=F.

Distributivity of Conjunction

Given command S and predicates Q and R,

wp(S,QR)=wp(S,Q)wp(S,R)

Law of Monotonicity

Given command S and predicates Q and R, if QR, then wp(S,Q)wp(S,R).

Distributivity of Disjunction

Given command S and predicates Q and R,

wp(S,Q)wp(S,R)wp(S,QR)

Strongest Postcondition

For any command S and predicate Q, we define the strongest postcondition of S with respect to Q, denoted sp(S,Q), as

the state such that execution of S begun in a state satisfying Q is guaranteed to terminate in after a finite amount of time.

Expression {Q}S{R} is equivalent to sp(S,Q)R.

Commands

Skip

For any predicate R, wp(skip,R)=R.

Abort

For any predicate R, wp(abort,R)=F.

Sequential Composition

Sequential composition is one way of composing larger program segments from smaller segments. Let S1 and S2 be two commands. Then S1;S2 is defined as

wp(S1;S2,R)=wp(S1,wp(S2,R))

Assignment

Simple

The assignment command has form x:=e, provided the types of x and e are the same. This command is read as "x becomes e" and is defined as

wp(x:=e,R)=domain(e) cand Rex

where domain(e) is a predicate that describes the set of all states in which e may be evaluated.

General

The multiple assignment command has form

x1s1,,xnsn:=e1,,en

where each xi is an identifier, each si is a selector, and each expression ei has the same type as xisi. We denote this assignment more compactly as x¯:=e¯. We define multiple assignment as

wp(x¯:=e¯,R)=domain(e¯) cand Re¯x¯

Alternative

The general form of the alternative command is:

if B1S1 | B2S2 | BnSnfi 

Each BiSi is called a guarded command. To execute the alternative command, find one true guard and execute the corresponding command. Notice this is nondeterministic. We denote the alternative command as IF and define IF in terms of wp as:

wp(IF,R)=(i,1indomain(Bi))(i,1inBi)(i,1in(Biwp(Si,R)))

Iterative

The general form of the iterative command is:

do B1S1 | B2S2 | BnSnod 

We denote the iterative command as DO and define DO in terms of wp as:

wp(DO,R)=k0,Hk(R)

where Hk is given recursive definition:

H0(R)=¬(B1Bn)RHk+1(R)=H0(R)wp(IF,Hk(R))

Bound Functions

A bound function t returns an upper bound on the number of iterations an iterative command may perform. On every iteration, it must evaluate to a smaller number than in the previous iteration. That is, t(k+)<t(k) for all kN. In this way, we guarantee the iterative command eventually terminates.

Understanding Loops

Consider loop DO. Suppose a predicate P satisfies

  1. PBiwp(Si,P) for all 1in.

Suppose further that an integer function t satisfies the following, where t1 is a fresh identifier:

  1. PBB(t>0),
  2. PBiwp(t1:=t;Si,t<t1), for 1in.

Then Pwp(DO,P¬BB).

Powered by Forestry.md