Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Predicate transformer semantics

From Wikipedia, the free encyclopedia
Reformulation of Floyd-Hoare logic
Semantics
Subfields
Topics
Analysis
Applications
Semantics of
programming languages
Types
Theory

Predicate transformer semantics were introduced byEdsger Dijkstra in his seminal paper "Guarded commands, nondeterminacy and formal derivation of programs". They define the semantics of animperative programming paradigm by assigning to eachstatement in this language a correspondingpredicate transformer: atotal function between twopredicates on the state space of the statement. In this sense, predicate transformer semantics are a kind ofdenotational semantics. Actually, inguarded commands, Dijkstra uses only one kind of predicate transformer: the well-knownweakest preconditions (see below).

Moreover, predicate transformer semantics are a reformulation ofFloyd–Hoare logic. Whereas Hoare logic is presented as adeductive system, predicate transformer semantics (either byweakest-preconditions or bystrongest-postconditions see below) arecomplete strategies to buildvalid deductions of Hoare logic. In other words, they provide an effectivealgorithm to reduce the problem of verifying a Hoare triple to the problem of proving afirst-order formula. Technically, predicate transformer semantics perform a kind ofsymbolic execution of statements into predicates: execution runsbackward in the case of weakest-preconditions, or runsforward in the case of strongest-postconditions.

Weakest preconditions

[edit]

Definition

[edit]

For a statementS and apostconditionR, aweakest precondition is a predicateQ such that for anypreconditionP,{P}S{R}{\displaystyle \{P\}S\{R\}} if and only ifPQ{\displaystyle P\Rightarrow Q}. In other words, it is the "loosest" or least restrictive requirement needed to guarantee thatR holds afterS. Uniqueness follows easily from the definition: If bothQ andQ' are weakest preconditions, then by the definition{Q}S{R}{\displaystyle \{Q'\}S\{R\}} soQQ{\displaystyle Q'\Rightarrow Q} and{Q}S{R}{\displaystyle \{Q\}S\{R\}} soQQ{\displaystyle Q\Rightarrow Q'}, and thusQ=Q{\displaystyle Q=Q'}. We often usewp(S,R){\displaystyle wp(S,R)} to denote the weakest precondition for statementS with respect to a postconditionR.

Conventions

[edit]

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

[edit]
wp(skip,R) = R{\displaystyle wp({\texttt {skip}},R)\ =\ R}

Abort

[edit]
wp(abort,R) = F{\displaystyle wp({\texttt {abort}},R)\ =\ {\texttt {F}}}

Assignment

[edit]

We give below two equivalent weakest-preconditions for the assignment statement. In these formulas,R[xE]{\displaystyle R[x\leftarrow E]} is a copy ofR wherefree occurrences ofx are replaced byE. Hence, here, expressionE is implicitly coerced into avalid term of the underlying logic: it is thus apure expression, totally defined, terminating and without side effect.

  • version 1:
wp(x:=E,R) = (y.y=ER[xy]){\displaystyle wp(x:=E,R)\ =\ (\forall y.y=E\Rightarrow R[x\leftarrow y])}

wherey is a fresh variable and not free in E and R (representing the final value of variablex)

  • version 2:

Provided that E is well defined, we just apply the so-calledone-point rule on version 1. Then

wp(x:=E,R) = R[xE]{\displaystyle wp(x:=E,R)\ =\ R[x\leftarrow E]}

The first version avoids a potential duplication ofx inR, whereas the second version is simpler when there is at most a single occurrence ofx inR. The first version also reveals a deep duality between weakest-precondition and strongest-postcondition (see below).

An example of a valid calculation ofwp (using version 2) for assignments with integer valued variablex is:

wp(x:=x5,x>10)=x5>10x>15{\displaystyle {\begin{array}{rcl}wp(x:=x-5,x>10)&=&x-5>10\\&\Leftrightarrow &x>15\end{array}}}

This means that in order for the postconditionx > 10 to be true after the assignment, the preconditionx > 15 must be true before the assignment. This is also the "weakest precondition", in that it is the "weakest" restriction on the value ofx which makesx > 10 true after the assignment.

Sequence

[edit]
wp(S1;S2,R) = wp(S1,wp(S2,R)){\displaystyle wp(S_{1};S_{2},R)\ =\ wp(S_{1},wp(S_{2},R))}

For example,

wp(x:=x5;x:=x2 , x>20)=wp(x:=x5,wp(x:=x2,x>20))=wp(x:=x5,x2>20)=(x5)2>20=x>15{\displaystyle {\begin{array}{rcl}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{array}}}

Conditional

[edit]
wp(if E then S1 else S2 end,R) = (Ewp(S1,R))(¬Ewp(S2,R)){\displaystyle wp({\texttt {if}}\ E\ {\texttt {then}}\ S_{1}\ {\texttt {else}}\ S_{2}\ {\texttt {end}},R)\ =\ (E\Rightarrow wp(S_{1},R))\wedge (\neg E\Rightarrow wp(S_{2},R))}

As example:

wp(if x<y then x:=y elseskipend, xy)=(x<ywp(x:=y,xy))  (¬(x<y)wp(skip,xy))=(x<yyy)  (¬(x<y)xy)true{\displaystyle {\begin{array}{rcl}wp({\texttt {if}}\ x<y\ {\texttt {then}}\ x:=y\ {\texttt {else}}\;\;{\texttt {skip}}\;\;{\texttt {end}},\ x\geq y)&=&(x<y\Rightarrow wp(x:=y,x\geq y))\ \wedge \ (\neg (x<y)\Rightarrow wp({\texttt {skip}},x\geq y))\\&=&(x<y\Rightarrow y\geq y)\ \wedge \ (\neg (x<y)\Rightarrow x\geq y)\\&\Leftrightarrow &{\texttt {true}}\end{array}}}

While loop

[edit]

Partial correctness

[edit]

Ignoring termination for a moment, we can define the rule for theweakest liberal precondition, denotedwlp, using a predicateINV, called theLoopINVariant, typically supplied by the programmer:

wlp(while E do S done,R) INV  if  (EINVwlp(S,INV)) (¬EINVR){\displaystyle wlp({\texttt {while}}\ E\ {\texttt {do}}\ S\ {\texttt {done}},R)\Leftarrow \ {\textit {INV}}\ \ {\text{if}}\ \ {\begin{array}{l}\\(E\wedge {\textit {INV}}\Rightarrow wlp(S,{\textit {INV}}))\\\wedge \ (\neg E\wedge {\textit {INV}}\Rightarrow R)\end{array}}}

Total correctness

[edit]

To show total correctness, we also have to show that the loop terminates. For this we define awell-founded relation on the state space denoted as (wfs, <) and define a variant functionvf , such that we have:

wp(while E do S done,R)  INV  if    (EINVvfwfs) (EINVv=vfwp(S,INVv<vf)) (¬EINVR){\displaystyle wp({\texttt {while}}\ E\ {\texttt {do}}\ S\ {\texttt {done}},R)\ \Leftarrow \ {\textit {INV}}\ \ {\text{if}}\ \ \ \ {\begin{array}{l}\\(E\wedge {\textit {INV}}\Rightarrow {\textit {vf}}\in {\textit {wfs}})\\\wedge \ (E\wedge {\textit {INV}}\wedge v={\textit {vf}}\Rightarrow wp(S,{\textit {INV}}\wedge v<{\textit {vf}}))\\\wedge \ (\neg E\wedge {\textit {INV}}\Rightarrow R)\end{array}}}

wherev is a fresh tuple of variables

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. statementS) must preserve the invariant and reduce the variant;
  • the last one means that the loop postconditionR must be established when the loop finishes.

However, the conjunction of those three is not a necessary condition. Exactly, we have

wp(while E do S done,R)  =  the strongest solution of the recursive equation Z:[Z(Ewp(S,Z))(¬ER)]{\displaystyle wp({\texttt {while}}\ E\ {\texttt {do}}\ S\ {\texttt {done}},R)\ \ =\ \ {\text{the strongest solution of the recursive equation}}\ {\begin{array}{l}Z:[Z\equiv (E\wedge wp(S,Z))\vee (\neg E\wedge R)]\end{array}}}

Non-deterministic guarded commands

[edit]

Actually, Dijkstra'sGuarded 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 forwhile-loop) preserve this property.

Selection

[edit]

Selection is a generalization ofif statement:

wp(if E1S1 []  [] EnSn fi,R) =(E1En) (E1wp(S1,R)) (Enwp(Sn,R)){\displaystyle wp({\texttt {if}}\ E_{1}\rightarrow S_{1}\ [\!]\ \ldots \ [\!]\ E_{n}\rightarrow S_{n}\ {\texttt {fi}},R)\ ={\begin{array}{l}(E_{1}\vee \ldots \vee E_{n})\\\wedge \ (E_{1}\Rightarrow wp(S_{1},R))\\\ldots \\\wedge \ (E_{n}\Rightarrow wp(S_{n},R))\\\end{array}}}

[citation needed]

Here, when two guardsEi{\displaystyle E_{i}} andEj{\displaystyle E_{j}} are simultaneously true, then execution of this statement can run any of the associated statementSi{\displaystyle S_{i}} orSj{\displaystyle S_{j}}.

Repetition

[edit]

Repetition is a generalization ofwhile statement in a similar way.

Specification statement

[edit]

Refinement calculus extends GCL with the notion ofspecification statement. Syntactically, we prefer to write a specification statement as

x:l[pre,post]{\displaystyle x:l[pre,post]}

which specifies a computation that starts in a state satisfyingpre and is guaranteed to end in a state satisfyingpostby changing onlyx. We calll{\displaystyle 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[x=l,x=l+1]{\displaystyle x:l[x=l,x=l+1]}

Another example is a computation of a square root of an integer.

x:l[x=l2,x=l]{\displaystyle x:l[x=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, aspre andpost are arbitrary predicates. Its weakest precondition is as follows.

wp(x:l[pre,post],R)=(l::pre)(s:(l:pre:post(xs)):R(xs)){\displaystyle wp(x:l[pre,post],R)=(\exists l::pre)\wedge (\forall s:(\forall l:pre:post(x\leftarrow s)):R(x\leftarrow s))}

wheres is fresh.

It combines Morgan's syntactic idea with the sharpness idea by Bijlsma, Matthews and Wiltink.[1] The very advantage of this is its capability of defining wp of goto L and other jump statements.[2]

Goto statement

[edit]

Formalization of jump statements likegoto 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 thatgoto 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

wp(goto L,R)=wpL{\displaystyle wp({\texttt {goto}}\ L,R)=wpL}

wherewpL is the weakest precondition at labelL.

Forgoto L execution transfers control to labelL at which the weakest precondition has to hold. The way thatwpL is referred to in the rule should not be taken as a big surprise. It is justwp(L:S,Q){\displaystyle wp(L:S,Q)} for someQ computed to that point. This is like any wp rules, using constituent statements to give wp definitions, even thoughgoto L appears a primitive. The rule does not require the uniqueness for locations wherewpL 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, asS(L:L:S1){\displaystyle S(L:L:S1)}, which is the same asS(L:S1){\displaystyle 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)   =   { sequential composition and alternation rules }     wp(do x > 0 → L: x := x-1 od, (x<0 ∧ wp(x := -x; goto L, post)) ∨ (x ≥  0 ∧ post)   =   { sequential composition, goto, assignment rules }     wp(do x > 0 → L: x := x-1 od, x<0 ∧ wpL(x ← -x) ∨ x≥0 ∧ post)   =   { repetition rule }     the strongest solution of               Z: [ Z ≡ x > 0 ∧ wp(L: x := x-1, Z) ∨ x < 0 ∧ wpL(x ← -x) ∨ x=0 ∧ post ]       =  { assignment rule, found wpL = Z(x ← x-1) }     the strongest solution of               Z: [ Z ≡ x > 0 ∧ Z(x ← x-1) ∨ x < 0 ∧ Z(x ← x-1) (x ← -x) ∨ x=0 ∧ post]   =  { substitution }     the strongest solution of               Z:[ Z ≡ x > 0 ∧ Z(x ← x-1) ∨ x < 0 ∧ Z(x ← -x-1) ∨ x=0 ∧ post ]   =  { solve the equation by approximation }     post(x ← 0)

Therefore,

wp(S, post) = post(x ← 0).

Other predicate transformers

[edit]

Weakest liberal precondition

[edit]

An important variant of the weakest precondition is theweakest liberal preconditionwlp(S,R){\displaystyle wlp(S,R)}, which yields the weakest condition under whichS either does not terminate or establishesR. It therefore differs fromwp in not guaranteeing termination. Hence it corresponds toHoare logic in partial correctness: for the statement language given above,wlp differs withwp only onwhile-loop, in not requiring a variant (see above).

Strongest postcondition

[edit]

GivenS a statement andR aprecondition (a predicate on the initial state), thensp(S,R){\displaystyle sp(S,R)} is theirstrongest-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{P}S{Q}{\displaystyle \{P\}S\{Q\}} is provable in Hoare logic if and only if the predicate below hold:

x,sp(S,P)Q{\displaystyle \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:

(x,Pwlp(S,Q))  (x,sp(S,P)Q){\displaystyle (\forall x,P\Rightarrow wlp(S,Q))\ \Leftrightarrow \ (\forall x,sp(S,P)\Rightarrow Q)}

For example, on assignment we have:

sp(x:=E,R) = y,x=E[xy]R[xy]{\displaystyle sp(x:=E,R)\ =\ \exists y,x=E[x\leftarrow y]\wedge R[x\leftarrow y]}

wherey is fresh

Above, the logical variabley represents the initial value of variablex.Hence,

sp(x:=x5,x>15) = y,x=y5y>15  x>10{\displaystyle sp(x:=x-5,x>15)\ =\ \exists y,x=y-5\wedge y>15\ \Leftrightarrow \ x>10}

On sequence, it appears thatsp runs forward (whereaswp runs backward):

sp(S1;S2 , R) = sp(S2,sp(S1,R)){\displaystyle sp(S_{1};S_{2}\ ,\ R)\ =\ sp(S_{2},sp(S_{1},R))}

Win and sin predicate transformers

[edit]

Leslie Lamport has suggestedwin andsin aspredicate transformers forconcurrent programming.[3]

Predicate transformers properties

[edit]

This section presents some characteristic properties of predicate transformers.[4] Below,S denotes a predicate transformer (a function between two predicates on the state space) andP a predicate. For instance,S(P) may denotewp(S,P) orsp(S,P). We keepx as the variable of the state space.

Monotonic

[edit]

Predicate transformers of interest (wp,wlp, andsp) aremonotonic. A predicate transformerS ismonotonic if and only if:

(x:P:Q)(x:S(P):S(Q)){\displaystyle (\forall x:P:Q)\Rightarrow (\forall x:S(P):S(Q))}

This property is related to theconsequence rule of Hoare logic.

Strict

[edit]

A predicate transformerS isstrict iff:

S(F)  F{\displaystyle S({\texttt {F}})\ \Leftrightarrow \ {\texttt {F}}}

For instance,wp is artificially made strict, whereaswlp is generally not. In particular, if statementS may not terminate thenwlp(S,F){\displaystyle wlp(S,{\texttt {F}})} is satisfiable. We have

wlp(while true do skip done,F) T{\displaystyle wlp({\texttt {while}}\ {\texttt {true}}\ {\texttt {do}}\ {\texttt {skip}}\ {\texttt {done}},{\texttt {F}})\ \Leftrightarrow {\texttt {T}}}

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,[5] i.e. they can be implemented but not strict.

Terminating

[edit]

A predicate transformerS isterminating if:

S(T)  T{\displaystyle S({\texttt {T}})\ \Leftrightarrow \ {\texttt {T}}}

Actually, this terminology makes sense only for strict predicate transformers: indeed,wp(S,T){\displaystyle wp(S,{\texttt {T}})} is the weakest-precondition ensuring termination ofS.

It seems that naming this propertynon-aborting would be more appropriate: in total correctness, non-termination is abortion, whereas in partial correctness, it is not.

Conjunctive

[edit]

A predicate transformerS isconjunctive iff:

S(PQ)  S(P)S(Q){\displaystyle S(P\wedge Q)\ \Leftrightarrow \ S(P)\wedge S(Q)}

This is the case forwp(S,.){\displaystyle wp(S,.)}, even if statementS is non-deterministic as a selection statement or a specification statement.

Disjunctive

[edit]

A predicate transformerS isdisjunctive iff:

S(PQ)  S(P)S(Q){\displaystyle S(P\vee Q)\ \Leftrightarrow \ S(P)\vee S(Q)}

This is generally not the case ofwp(S,.){\displaystyle wp(S,.)} whenS is non-deterministic. Indeed, consider a non-deterministic statementS choosing an arbitrary Boolean. This statement is given here as the followingselection statement:

S = if truex:=0 [] truex:=1 fi{\displaystyle S\ =\ {\texttt {if}}\ {\texttt {true}}\rightarrow x:=0\ [\!]\ {\texttt {true}}\rightarrow x:=1\ {\texttt {fi}}}

Then,wp(S,R){\displaystyle wp(S,R)} reduces to the formulaR[x0]R[x1]{\displaystyle R[x\leftarrow 0]\wedge R[x\leftarrow 1]}.

Hence,wp(S, x=0x=1){\displaystyle wp(S,\ x=0\vee x=1)} reduces to thetautology(0=00=1)(1=01=1){\displaystyle (0=0\vee 0=1)\wedge (1=0\vee 1=1)}

Whereas, the formulawp(S,x=0)wp(S,x=1){\displaystyle wp(S,x=0)\vee wp(S,x=1)}reduces to thewrong proposition(0=01=0)(1=01=1){\displaystyle (0=0\wedge 1=0)\vee (1=0\wedge 1=1)}.

Applications

[edit]

Beyond predicate transformers

[edit]

Weakest-preconditions and strongest-postconditions of imperative expressions

[edit]

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 (likedivision by zero). There are many proposals to extend weakest-preconditions or strongest-postconditions for imperative expression languages and in particular formonads.

Among them,Hoare Type Theory combinesHoare logic for aHaskell-like language,separation logic andtype theory.[9]This system is currently implemented as aRocq library calledYnot.[10] In this language,evaluation of expressions corresponds to computations ofstrongest-postconditions.

Probabilistic Predicate Transformers

[edit]

Probabilistic Predicate Transformers are an extension of predicate transformers forprobabilistic programs.Indeed, such programs have many applications incryptography (hiding of information using some randomized noise),distributed systems (symmetry breaking).[11]

See also

[edit]

Notes

[edit]
  1. ^Chen, Wei and Udding, Jan Tijmen, "The Specification Statement Refined" WUCS-89-37 (1989).https://openscholarship.wustl.edu/cse_research/749
  2. ^Chen, Wei, "A wp Characterization of Jump Statements," 2021 International Symposium on Theoretical Aspects of Software Engineering (TASE), 2021, pp. 15-22. doi: 10.1109/TASE52547.2021.00019.
  3. ^Lamport, Leslie (July 1990)."win andsin: Predicate Transformers for Concurrency".ACM Trans. Program. Lang. Syst.12 (3):396–428.CiteSeerX 10.1.1.33.90.doi:10.1145/78969.78970.S2CID 209901.
  4. ^Back, Ralph-Johan; Wright, Joakim (2012) [1978].Refinement Calculus: A Systematic Introduction. Texts in Computer Science. Springer.ISBN 978-1-4612-1674-2.
  5. ^Chen, Wei, "Exit Statements are Executable Miracles" WUCS-91-53 (1991).https://openscholarship.wustl.edu/cse_research/671
  6. ^Dijkstra, Edsger W. (1968). "A Constructive Approach to the Problem of Program Correctness".BIT Numerical Mathematics.8 (3):174–186.doi:10.1007/bf01933419.S2CID 62224342.
  7. ^Wirth, N. (April 1971)."Program development by stepwise refinement"(PDF).Comm. ACM.14 (4):221–7.doi:10.1145/362575.362577.hdl:20.500.11850/80846.S2CID 13214445.
  8. ^Tutorial on Hoare Logic: aCoq library, giving a simple but formal proof thatHoare logic is sound and complete with respect to anoperational semantics.
  9. ^Nanevski, Aleksandar; Morrisett, Greg; Birkedal, Lars (September 2008)."Hoare Type Theory, Polymorphism and Separation"(PDF).Journal of Functional Programming.18 (5–6):865–911.doi:10.1017/S0956796808006953.S2CID 6956622.
  10. ^Ynot aCoq library implementing Hoare Type Theory.
  11. ^Morgan, Carroll;McIver, Annabelle; Seidel, Karen (May 1996)."Probabilistic Predicate Transformers"(PDF).ACM Trans. Program. Lang. Syst.18 (3):325–353.CiteSeerX 10.1.1.41.9219.doi:10.1145/229542.229547.S2CID 5812195.

References

[edit]
Works
Main research
areas
Related
people
Retrieved from "https://en.wikipedia.org/w/index.php?title=Predicate_transformer_semantics&oldid=1311882792"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp