This paper was converted on www.awesomepapers.org from LaTeX by an anonymous user.
Want to know more? Visit the Converter page.

Gradual Exact Logic
Unifying Hoare Logic and Incorrectness Logic via Gradual Verification

Conrad Zimmerman [email protected] Northeastern UniversityBostonMAUSA  and  Jenna DiVincenzo [email protected] Purdue UniversityWest LafayetteINUSA
Abstract.

Previously, gradual verification has been developed using overapproximating logics such as Hoare logic. We show that the static verification component of gradual verification is also connected to underapproximating logics like incorrectness logic. To do this, we use a novel definition of gradual verification and a novel gradualization of exact logic (Maksimovic et al., 2023) which we call gradual exact logic. Further, we show that Hoare logic, incorrectness logic, and gradual verification can be defined in terms of gradual exact logic.

We hope that this connection can be used to develop tools and techniques that apply to both gradual verification and bug-finding. For example, we envision that techniques defined in terms of exact logic can be directly applied to verification, bug-finding, and gradual verification, using the principles of gradual typing (Garcia et al., 2016).

1. Overview

Incorrectness logic (O’Hearn, 2020) has been recently developed as a formal basis for “true bug-finding” and has been applied in industrial-strength tools (Le et al., 2022). Deductions in this logic prove reachability, which enables bug-finding tools to prove the existence of an invalid state while selectively exploring the possible paths.

At the same time, gradual verification (GV) (Bader et al., 2018) addresses the complexity of traditional static verification. Gradually verified programs may contain imprecise specifications—logical formulas annotated to indicate that they contain only a partial specification of behavior. A gradual verifier checks the imprecise specifications using static verification where it can and run-time checks (i.e. dynamic verification) elsewhere. These run-time checks can be exercised, e.g. with testing, giving the programmer confidence that their code will not enter a state that violates their partial specifications. Using gradual verification, programmers can incrementally verify a program, incrementally learn verification constructs, and safely guard unverified components.

More recent work (Zilberstein et al., 2023; Maksimovic et al., 2023; Lööw et al., 2024) has produced logics and tools that unify over-approximating (OX) logic, often used in verification, and under-approximating (UX) logics including IL. In this paper, we propose another unification of OX and UX logics that we derive using the principles of gradual verification. The resulting characterization of IL demonstrates a previously unexplored connection between GV and IL.

1.1. Hoare logic

A triple in Hoare logic (HL) (Hoare, 1969) is denoted {P}C{Q}\{P\}~{}C~{}\{Q\}. (P,QP,Q refer to logical specifications; CC refers to a program statement.) Semantically, the triple is valid if, for every state σP\sigma\in P (i.e., PP is true of σ\sigma), when σCσ\sigma\stackrel{{\scriptstyle C}}{{\to}}\sigma^{\prime} (i.e., executing CC results in σ\sigma^{\prime}), then σQ\sigma^{\prime}\in Q:

PPQQ𝐶\overset{C}{\longrightarrow}

Thus HL is an overapproximating (OX) logic—the postcondition QQ overapproximates the states that are reachable from PP; precisely, Q{σσP:σ𝐶σ}Q\supseteq\{\sigma^{\prime}\mid\exists\sigma\in P:\sigma\overset{C}{\to}\sigma^{\prime}\}. HL is a formal foundation for program verification precisely because the postcondition is true in all ending states.

1.2. Incorrectness logic

A triple in incorrectness logic (IL) (O’Hearn, 2020) is denoted [P]C[Q][P]~{}C~{}[Q]111IL triples often include a specification for error states, but we omit error handling to simplify the comparison with other logics.. Semantically, the triple is valid if, for every σQ\sigma^{\prime}\in Q, there is some σP\sigma\in P such that σ𝐶σ\sigma\overset{C}{\to}\sigma^{\prime}:

PPQQC\stackrel{{\scriptstyle C}}{{\longrightarrow}}

Thus IL is an underapproximating (UX) logic—the postcondition QQ underapproximates the states that are reachable from PP; precisely, Q{σσP:σ𝐶σ}Q\subseteq\{\sigma^{\prime}\mid\exists\sigma\in P:\sigma\overset{C}{\to}\sigma^{\prime}\}. Interpreting QQ as a specification of a bug, IL is a logic for finding bugs since a valid triple indicates that the bug is reachable.

1.3. Gradual verification

Gradual verification (GV) (Bader et al., 2018) reduces the burden of static verification by allowing incomplete (imprecise) specifications. A gradual verifier may make optimistic assumptions when verifying imprecisely-specified code. Typically, the final program is elaborated to check these assumptions at run-time. However, in this work we focus solely on the static verification component of GV so that we can compare its logic with HL and IL.

We denote imprecise triples by {?P}C{Q}\{\textnormal{{?}}\wedge P\}~{}C~{}\{Q\}. Intuitively, this triple is valid if there is some PPP^{\prime}\Rightarrow P such that {P}C{Q}\{P^{\prime}\}~{}C~{}\{Q\} is valid in HL. One can think of ? as representing the additional assumptions introduced by PP^{\prime}.

For example, the following imprecise triple is valid:

{?}x:-x+1{x>0}\{{\color[rgb]{.75,0,.25}\definecolor[named]{pgfstrokecolor}{rgb}{.75,0,.25}\textnormal{{?}}}\wedge\top\}~{}x\coloneq x+1~{}\{x>0\}

This follows from the validity of

{x0}x:-x+1{x>0}.\{{\color[rgb]{.75,0,.25}\definecolor[named]{pgfstrokecolor}{rgb}{.75,0,.25}x\geq 0}\wedge\top\}~{}x\coloneq x+1~{}\{x>0\}.

That is, the postcondition is ensured when assuming x0x\geq 0.

However, the assumptions must be plausible; formally, PP^{\prime} (and thus PP) must be satisfiable (i.e., PP^{\prime}\not\equiv\bot). Otherwise, all imprecise triples would be vacuously valid by taking PP^{\prime}\equiv\bot. With this in mind, GV can be stated as a reachability problem—semantically, {?P}C{Q}\{\textnormal{{?}}\wedge P\}~{}C~{}\{Q\} is valid if there exists some states σ\sigma and σ\sigma^{\prime} such that σP\sigma\in P, σ𝐶σ\sigma\overset{C}{\to}\sigma^{\prime}, and σQ\sigma^{\prime}\in Q:

PPQQC\stackrel{{\scriptstyle C}}{{\longrightarrow}}

By contrapositive, the triple is invalid (i.e., static verification will error) if it is never possible for CC to ensure QQ, given PP.

By comparing this diagram with those in §1.1 and §1.2, we can deduce that HL and IL triples are valid imprecise triples, except for vacuous cases where PP\equiv\bot in HL or QQ\equiv\bot in IL.

Our semantic definition of validity is equivalent to the previous definition which uses Hoare triples: Let PP^{\prime} be a formula that represents σ\sigma as specifically as possible, then (intuitively) {P}C{Q}\{P^{\prime}\}~{}C~{}\{Q\} is valid since σ𝐶σ\sigma\overset{C}{\to}\sigma^{\prime} and σQ\sigma^{\prime}\in Q.

But, we can also define valid imprecise triples in terms of IL—{?P}C{Q}\{\textnormal{{?}}\wedge P\}~{}C~{}\{Q\} is valid if [P]C[Q][P]~{}C~{}[Q^{\prime}] is valid for some QQQ^{\prime}\Rightarrow Q. In §2.5 we will prove these definitions equivalent.

2. Formal foundations

We will now sketch our formal definitions and results. See the appendices for the full statements and proofs.

  1. (1)

    We define gradual exact logic—a consistent lifting (Garcia et al., 2016) of exact logic (Maksimovic et al., 2023).

  2. (2)

    We show that HL, IL, and GV can be characterized by gradual exact logic.

  3. (3)

    We show that GV contains OX and UX deductions.

  4. (4)

    We show that, for imprecise specifications, GV can be equivalently defined using OX or UX logics.

2.1. Exact logic

Exact logic (EL) (Maksimovic et al., 2023) is the intersection of HL and IL: an EL triple (P)C(Q)(P)~{}C~{}(Q) is valid if {P}C{Q}\{P\}~{}C~{}\{Q\} and [P]C[Q][P]~{}C~{}[Q] are both valid. Deductions are thus exact—they can neither under- or overapproximate behavior (see Appendix E for rules).

2.2. Gradual exact logic

We further define gradual exact logic (EL~\widetilde{\text{EL}}) as a consistent lifting (Garcia et al., 2016) of EL.

First, some definitions: Formula denotes all formulas in FOL with arithmetic; we call these precise. SatFormula denotes all satisfiable formulas. An imprecise formula is of the form ?P\textnormal{{?}}\wedge P where PFormulaP\in\textsc{Formula}. A gradual formula P~F~ormula\widetilde{P}\in\widetilde{\textsc{F}}\textsc{ormula} can be either precise or imprecise.

The concretization γ:F~ormula𝒫(Formula)\gamma:\widetilde{\textsc{F}}\textsc{ormula}\to\mathcal{P}(\textsc{Formula}) interprets a gradual formulas as sets of precise formulas:

γ(?P)={PSatFormulaPP},γ(P)={P}\gamma(\textnormal{{?}}\wedge P)=\{P^{\prime}\in\textsc{SatFormula}\mid P^{\prime}\Rightarrow P\},\quad\gamma(P)=\{P\}

Let (P)C(Q)\vdash(P)~{}C~{}(Q) denote a valid EL triple. Deductions in EL~\widetilde{\text{EL}}, denoted ~(P~)C(Q~)\mathrel{\widetilde{\vdash}}(\widetilde{P})~{}C~{}(\widetilde{Q}), are defined as a consistent lifting of EL deductions (Section E.2):

~(P~)C(Q~)def(P)C(Q)for some Pγ(P~)Qγ(Q~)\mathrel{\widetilde{\vdash}}(\widetilde{P})~{}C~{}(\widetilde{Q})\mathrel{\stackrel{{\scriptstyle\text{def}}}{{\iff}}}\vdash(P)~{}C~{}(Q)~{}\text{for some $P\in\gamma(\widetilde{P})$, $Q\in\gamma(\widetilde{Q})$}

2.3. Strongest postconditions

sp(P,C)\operatorname{sp}(P,C) denotes the strongest (WRT \Rightarrow) QQ for which {P}C{Q}\{P\}~{}C~{}\{Q\} is valid (calculated as usual; see Section B.2). Strongest postconditions are related to HL, IL, and EL as follows:

{P}C{Q}\displaystyle\vdash\{P\}~{}C~{}\{Q\} sp(P,C)Q\displaystyle\iff\operatorname{sp}(P,C)\Rightarrow Q
[P]C[Q]\displaystyle\vdash[P]~{}C~{}[Q] sp(P,C)Q\displaystyle\iff\operatorname{sp}(P,C)\Leftarrow Q
(P)C(Q)\displaystyle\vdash(P)~{}C~{}(Q) sp(P,C)Q\displaystyle\iff\operatorname{sp}(P,C)\equiv Q

2.4. HL and IL via EL~\widetilde{\text{EL}}

We can characterize valid HL triples as EL~\widetilde{\text{EL}} triples where the postcondition is made imprecise. Assuming that PP\not\equiv\bot and CC terminates (in either of these cases {P}C{Q}\{P\}~{}C~{}\{Q\} is vacuous), we have P,Q,sp(P,C)SatFormulaP,Q,\operatorname{sp}(P,C)\in\textsc{SatFormula} and thus (Theorem 6)

{P}C{Q}\displaystyle\vdash\{P\}~{}C~{}\{Q\} sp(P,C)Q\displaystyle\iff\operatorname{sp}(P,C)\Rightarrow Q
sp(P,C)γ(?Q)\displaystyle\iff\operatorname{sp}(P,C)\in\gamma(\textnormal{{?}}\wedge Q)
~(P)C(?Q).\displaystyle\iff\mathrel{\widetilde{\vdash}}(P)~{}C~{}(\textnormal{{?}}\wedge Q).

Likewise, we can characterize valid IL triples as EL~\widetilde{\text{EL}} triples where the precondition is made imprecise. Weakest preconditions are not always defined for IL (O’Hearn, 2020), however, we can reuse weakest preconditions for HL to witness the necessary formula (see Section B.3). Assuming QQ\not\equiv\bot (otherwise [P]C[Q][P]~{}C~{}[Q] is vacuous), we have (Theorem 7)

[P]C[Q]\displaystyle\vdash[P]~{}C~{}[Q] Qsp(P,C)\displaystyle\iff Q\Rightarrow\operatorname{sp}(P,C)
Qsp(wp(Q,C)P,C)\displaystyle\iff Q\equiv\operatorname{sp}(\operatorname{wp}(Q,C)\wedge P,C)
(Pwp(Q,C))C(Q)\displaystyle\iff\vdash(P\wedge\operatorname{wp}(Q,C))~{}C~{}(Q)
~(?P)C(Q).\displaystyle\iff\mathrel{\widetilde{\vdash}}(\textnormal{{?}}\wedge P)~{}C~{}(Q).

2.5. GV via HL, EL~\widetilde{\text{EL}}, and IL

We can now give a precise definition of GV in terms of HL, as sketched in §1.3222Here we define GV more generally for all gradual preconditions, whereas in §1.3 we defined it for imprecise preconditions.. We denote valid GV triples as ~{P~}C{Q}\mathrel{\widetilde{\vdash}}\{\widetilde{P}\}~{}C~{}\{Q\}.

~{P~}C{Q}def{P}C{Q}for some Pγ(P~)\mathrel{\widetilde{\vdash}}\{\widetilde{P}\}~{}C~{}\{Q\}\mathrel{\stackrel{{\scriptstyle\text{def}}}{{\iff}}}\vdash\{P\}~{}C~{}\{Q\}~{}\text{for some $P\in\gamma(\widetilde{P})$}

Using this definition and applying the characterization of HL from §2.4 to characterize GV in terms of EL:

~{P~}C{Q}~(P~)C(?Q)\mathrel{\widetilde{\vdash}}\{\widetilde{P}\}~{}C~{}\{Q\}\iff\mathrel{\widetilde{\vdash}}(\widetilde{P})~{}C~{}(\textnormal{{?}}\wedge Q)

For sake of comparison, we can define IL~\widetilde{\text{IL}} as a lifting of IL, the same way we have lifted HL to GV.

~[P]C[Q~]def[P]C[Q]for some Qγ(Q~)\mathrel{\widetilde{\vdash}}[P]~{}C~{}[\widetilde{Q}]\mathrel{\stackrel{{\scriptstyle\text{def}}}{{\iff}}}\vdash[P]~{}C~{}[Q]~{}\text{for some $Q\in\gamma(\widetilde{Q})$}

But, in the case of imprecision this is equivalent to GV. We can see this using the characterization of IL given in §2.4:

~[P]C[?Q]\displaystyle\mathrel{\widetilde{\vdash}}[P]~{}C~{}[\textnormal{{?}}\wedge Q] ~(?P)C(?Q)\displaystyle\iff\mathrel{\widetilde{\vdash}}(\textnormal{{?}}\wedge P)~{}C~{}(\textnormal{{?}}\wedge Q)
~{?P}C{Q}.\displaystyle\iff\mathrel{\widetilde{\vdash}}\{\textnormal{{?}}\wedge P\}~{}C~{}\{Q\}.

Note: GV and IL~\widetilde{\text{IL}} differ on precise formulas—~[P]C[Q]\mathrel{\widetilde{\vdash}}[P]~{}C~{}[Q] is not equivalent to ~{P}C{Q}\mathrel{\widetilde{\vdash}}\{P\}~{}C~{}\{Q\}. Also, we do not gradualize postconditions in HL or preconditions in IL because we can arbitrarily weaken these specifications already.

While verification of precise formulas is a key aspect of GV, this demonstrates that verification of imprecise formulas can be accomplished using IL, and moreover that GV, when verifying imprecisely-specified code, is proving an IL deduction. Finally, we can make precise our claim from §1.3 that both OX and UX deductions are valid in (the imprecise fragment of) GV. Assuming PP\not\equiv\bot, we have (immediate from definition of GV)

{P}C{Q}~{?P}C{Q}.\vdash\{P\}~{}C~{}\{Q\}\implies\mathrel{\widetilde{\vdash}}\{\textnormal{{?}}\wedge P\}~{}C~{}\{Q\}.

Also, assuming QQ\not\equiv\bot, we have

[P]C[Q]\displaystyle\vdash[P]~{}C~{}[Q] ~(?P)C(Q)\displaystyle\implies\mathrel{\widetilde{\vdash}}(\textnormal{{?}}\wedge P)~{}C~{}(Q)
~(?P)C(?Q)\displaystyle\implies\mathrel{\widetilde{\vdash}}(\textnormal{{?}}\wedge P)~{}C~{}(\textnormal{{?}}\wedge Q)
~{?P}C{Q}.\displaystyle\implies\mathrel{\widetilde{\vdash}}\{\textnormal{{?}}\wedge P\}~{}C~{}\{Q\}.

Thus GV (and EL~\widetilde{\text{EL}}) represents the union of HL and IL, while EL represents the intersection.

3. Applications

GV and IL verifiers in practice operate quite similarly; for example, compare the core consume operation of Zimmerman et al. (2024) for a gradual verifier and of Lööw et al. (2024) for an IL verifier. Both types of verifiers make assumptions, including pruning paths, when establishing a postcondition. This work formalizes the connection between these methods of verification; in particular, gradual verification of imprecisely-specified code is equivalent to IL deductions. We hope that this will allow verifiers that already incorporate both OX and UX logics, for example Lööw et al. (2024), to be easily extended to GV.

Similarly, we hope our approach can be used as a framework for unifying techniques across static verification, GV, and bug-finding. For example, bi-abduction has been developed in OX logics (Calcagno et al., 2011), applied to UX verification (Lööw et al., 2024), and is related to GV (DiVincenzo, 2023). We expect that techniques like this could be developed in the context of an exact logic, and then their applications to OX, UX, and GV logics could be derived using AGT-style techniques (Garcia et al., 2016).

4. Caveats and future work

While we have proven the results described, we have done so only for a very restrictive language, and thus our results should be considered preliminary. In particular, we do not consider heaps, method calls, or loops. We expect our results extend to these constructs, but showing this will require significant work.

Our definition of GV differs from previous definitions (Bader et al., 2018; Wise et al., 2020; Zimmerman et al., 2024). Our novel definition more clearly demonstrates the connection with IL, and we believe it captures the essence of GV. However, this definition only considers static verification, and thus does not consider run-time assertions. We expect this could be added, and we have hopes that we may be able to model run-time assertions using an evidence-based calculus similar to that of gradual typing Garcia et al. (2016). In addition, further work is necessary to elucidate how the “gradual guarantees” (Garcia et al., 2016) affect the relation of GV to IL. In particular, the static gradual guarantee seems to prohibit arbitrarily dropping paths, which IL can do.

Finally, our work is (to our knowledge) the first to explore a gradualization of exact logic. It remains to be seen whether this is useful its own right. For example, if library developers write exact specifications, a gradual exact logic could be used to aid development of these specifications, similar to how GV aids OX verification. But significantly more work would be necessary for this.

5. Conclusion

We have demonstrated the similarities and differences of GV and IL. Specifically, the relation between IL deductions and EL~\widetilde{\text{EL}} deductions with imprecise preconditions shows that the notion of assumptions used in GV is equivalent to the consequence rule (and path pruning) in IL. We also have shown that GV with imprecise specifications is equivalent to IL. Furthermore, we have defined gradual exact logic and used this to formally compare IL, HL, and GV. While much work remains before it is widely applicable, we hope that this framework can be used to develop techniques that uniformly target all three methods of verification.

Acknowledgements.
We thank Devin Singh for his contributions to this work.

References

  • (1)
  • Bader et al. (2018) Johannes Bader, Jonathan Aldrich, and Éric Tanter. 2018. Gradual Program Verification. In Verification, Model Checking, and Abstract Interpretation - 19th International Conference, VMCAI 2018, Los Angeles, CA, USA, January 7-9, 2018, Proceedings (Lecture Notes in Computer Science, Vol. 10747), Isil Dillig and Jens Palsberg (Eds.). Springer, 25–46. https://doi.org/10.1007/978-3-319-73721-8_2
  • Calcagno et al. (2011) Cristiano Calcagno, Dino Distefano, Peter W. O’Hearn, and Hongseok Yang. 2011. Compositional Shape Analysis by Means of Bi-Abduction. J. ACM 58, 6, Article 26 (Dec. 2011), 66 pages. https://doi.org/10.1145/2049697.2049700
  • DiVincenzo (2023) Jenna Wise DiVincenzo. 2023. Gradual Verification of Recursive Heap Data Structures. Ph. D. Dissertation. Carnegie Mellon University.
  • Garcia et al. (2016) Ronald Garcia, Alison M. Clark, and Éric Tanter. 2016. Abstracting gradual typing. In Proceedings of the 43rd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2016, St. Petersburg, FL, USA, January 20 - 22, 2016, Rastislav Bodík and Rupak Majumdar (Eds.). ACM, 429–442. https://doi.org/10.1145/2837614.2837670
  • Hoare (1969) C. A. R. Hoare. 1969. An Axiomatic Basis for Computer Programming. Commun. ACM 12, 10 (1969), 576–580. https://doi.org/10.1145/363235.363259
  • Le et al. (2022) Quang Loc Le, Azalea Raad, Jules Villard, Josh Berdine, Derek Dreyer, and Peter W. O’Hearn. 2022. Finding real bugs in big programs with incorrectness logic. Proc. ACM Program. Lang. 6, OOPSLA1 (2022), 1–27. https://doi.org/10.1145/3527325
  • Lööw et al. (2024) Andreas Lööw, Daniele Nantes-Sobrinho, Sacha-Élie Ayoun, Caroline Cronjäger, Petar Maksimovic, and Philippa Gardner. 2024. Compositional Symbolic Execution for Correctness and Incorrectness Reasoning. In 38th European Conference on Object-Oriented Programming, ECOOP 2024, September 16-20, 2024, Vienna, Austria (LIPIcs, Vol. 313), Jonathan Aldrich and Guido Salvaneschi (Eds.). Schloss Dagstuhl - Leibniz-Zentrum für Informatik, 25:1–25:28. https://doi.org/10.4230/LIPICS.ECOOP.2024.25
  • Maksimovic et al. (2023) Petar Maksimovic, Caroline Cronjäger, Andreas Lööw, Julian Sutherland, and Philippa Gardner. 2023. Exact Separation Logic: Towards Bridging the Gap Between Verification and Bug-Finding. In 37th European Conference on Object-Oriented Programming, ECOOP 2023, July 17-21, 2023, Seattle, Washington, United States (LIPIcs, Vol. 263), Karim Ali and Guido Salvaneschi (Eds.). Schloss Dagstuhl - Leibniz-Zentrum für Informatik, 19:1–19:27. https://doi.org/10.4230/LIPICS.ECOOP.2023.19
  • O’Hearn (2020) Peter W. O’Hearn. 2020. Incorrectness logic. Proc. ACM Program. Lang. 4, POPL (2020), 10:1–10:32. https://doi.org/10.1145/3371078
  • Wise et al. (2020) Jenna Wise, Johannes Bader, Cameron Wong, Jonathan Aldrich, Éric Tanter, and Joshua Sunshine. 2020. Gradual verification of recursive heap data structures. Proc. ACM Program. Lang. 4, OOPSLA (2020), 228:1–228:28. https://doi.org/10.1145/3428296
  • Zilberstein et al. (2023) Noam Zilberstein, Derek Dreyer, and Alexandra Silva. 2023. Outcome Logic: A Unifying Foundation for Correctness and Incorrectness Reasoning. Proc. ACM Program. Lang. 7, OOPSLA1 (2023), 522–550. https://doi.org/10.1145/3586045
  • Zimmerman et al. (2024) Conrad Zimmerman, Jenna DiVincenzo, and Jonathan Aldrich. 2024. Sound Gradual Verification with Symbolic Execution. Proc. ACM Program. Lang. 8, POPL (2024), 2547–2576. https://doi.org/10.1145/3632927

Appendix A Grammar

We define a basic language that includes mutable variables and conditionals. Note that we do not include while loops or functions; these constructs are left for future work.

ExprE\displaystyle\mathrm{Expr}\ni E~{}{\Coloneqq}~{} nxEEEE\displaystyle n\mid\bot\mid\top\mid x\mid E\vee E\mid E\wedge E\mid
E=EE<E¬E\displaystyle E=E\mid E<E\mid\neg E
CmdC\displaystyle\mathrm{Cmd}\ni C~{}{\Coloneqq}~{} skipx:-EifEthenCelseCC;C\displaystyle\textnormal{{skip}}\mid x\coloneq E\mid\textnormal{{if}}\ E\ \textnormal{{then}}\ C\ \textnormal{{else}}\ C\mid C;C
AsrtP,Q,R\displaystyle\mathrm{Asrt}\ni P,Q,R~{}{\Coloneqq}~{} E¬PPPPPxP\displaystyle E\mid\neg P\mid P\wedge P\mid P\vee P\mid\exists x\,P

where nn\in\mathbb{Z} and xx a variable name.

We assume that programs are typed correctly; for example, expressions EE in ifEthenC1elseC2\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2} will always evaluate to a boolean value.

Definition 0 (Implication).

PQP\Rightarrow Q if all states that satisfy PP also satisfy QQ.

Note: we do not formalize states or the semantics of assertions; we assume that our logic is close enough to first-order logic and thus use FOL deductions to reason about assertions.

Note: We use {\cdot}\Rightarrow{\cdot} to denote implication between formulas in first-order logic with booleans and arithmetic. {\cdot}\implies{\cdot} denotes “if-then” in our metatheory.

Definition 0 (Equivalence of propositions).

PQP\equiv Q denotes that PP and QQ are logically equivalent; that is,

PQdefPQandQP.P\equiv Q\mathrel{\stackrel{{\scriptstyle\text{def}}}{{\iff}}}P\Rightarrow Q~{}\text{and}~{}Q\Rightarrow P.

Throughout this paper, we assume that identity of propositions coincides with equivalence; that is, when we write an individual proposition, technically we are denoting the equivalence class that contains that proposition. For example, {,}={1=1,1=2}\{\top,\bot\}=\{1=1,1=2\}.

Definition 0 (Replacement).

P[x/E]P[x/E] denotes PP with all free occurrences of xx replaced by EE.

Definition 0.

fv(P)\operatorname{fv}(P) denotes the free variables in the assertion PP.

Definition 0.

fv(C)\operatorname{fv}(C) denotes all variables referenced or assigned in CC.

Definition 0.

mod(C)\operatorname{mod}(C) denotes all variables assigned in CC. Explicitly,

mod(skip)\displaystyle\operatorname{mod}(\textnormal{{skip}}) :-\displaystyle\coloneq\emptyset
mod(x:-E)\displaystyle\operatorname{mod}(x\coloneq E) :-x\displaystyle\coloneq x
mod(C1;C2)\displaystyle\operatorname{mod}(C_{1};C_{2}) :-mod(C1)mod(C2)\displaystyle\coloneq\operatorname{mod}(C_{1})\cup\operatorname{mod}(C_{2})
mod(ifEthenC1elseC2)\displaystyle\operatorname{mod}(\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}) :-mod(C1)mod(C2)\displaystyle\coloneq\operatorname{mod}(C_{1})\cup\operatorname{mod}(C_{2})

Appendix B Predicate transformers

B.1. Weakest preconditions

Definition 0.

PP is the weakest precondition for a statement CC and postcondition QQ, denoted wp(C,Q)\operatorname{wp}(C,Q), if it is the weakest predicate (WRT \Rightarrow) that ensures that if a state satisfies PP, then after executing CC, QQ holds.

The following explicit calculations correspond to the previous definition (proved in Theorem 4):

wp(skip,Q)=Q\displaystyle\operatorname{wp}(\textnormal{{skip}},Q)=Q
wp(x:-E,Q)=Q[x/E]\displaystyle\operatorname{wp}(x\coloneq E,Q)=Q[x/E]
wp(C1;C2,Q)=wp(C1,wp(C2,Q))\displaystyle\operatorname{wp}(C_{1};C_{2},Q)=\operatorname{wp}(C_{1},\operatorname{wp}(C_{2},Q))
wp(ifEthenC1elseC2,Q)=\displaystyle\operatorname{wp}(\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2},Q)=
(wp(C1,Q)E)(wp(C2,Q)¬E)\displaystyle\quad(\operatorname{wp}(C_{1},Q)\wedge E)\vee(\operatorname{wp}(C_{2},Q)\wedge\neg E)
Lemma 0 (Stronger postcondition).

If QQQ\Rightarrow Q^{\prime} then
wp(C,Q)wp(C,Q)\operatorname{wp}(C,Q)\Rightarrow\operatorname{wp}(C,Q^{\prime}).

Proof.

By induction on CC:

skip::
wp(skip,Q)QQwp(skip,Q)\operatorname{wp}(\textnormal{{skip}},Q)\equiv Q\Rightarrow Q^{\prime}\equiv\operatorname{wp}(\textnormal{{skip}},Q^{\prime})
x:-Ex\coloneq E::
wp(x:-E,Q)Q[E/x]Q[E/x]wp(x:-E,Q)\operatorname{wp}(x\coloneq E,Q)\equiv Q[E/x]\Rightarrow Q^{\prime}[E/x]\equiv\operatorname{wp}(x\coloneq E,Q^{\prime})
C1;C2C_{1};C_{2}::
(1):

wp(C2,Q)wp(C2,Q)\operatorname{wp}(C_{2},Q)\Rightarrow\operatorname{wp}(C_{2},Q^{\prime}) by induction

(2):

wp(C1,wp(C2,Q))wp(C1,wp(C2,Q))\operatorname{wp}(C_{1},\operatorname{wp}(C_{2},Q))\Rightarrow\operatorname{wp}(C_{1},\operatorname{wp}(C_{2},Q^{\prime})) by induction using (1)

(3):

wp(C1;C2,Q)wp(C1;C2,Q)\operatorname{wp}(C_{1};C_{2},Q)\Rightarrow\operatorname{wp}(C_{1};C_{2},Q^{\prime}) by (2) and definition of wp\operatorname{wp}

ifEthenC1elseC2\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}::
(1):

wp(C1,Q)wp(C1,Q)\operatorname{wp}(C_{1},Q)\Rightarrow\operatorname{wp}(C_{1},Q^{\prime}) by induction

(2):

wp(C2,Q)wp(C2,Q)\operatorname{wp}(C_{2},Q)\Rightarrow\operatorname{wp}(C_{2},Q^{\prime}) by induction

(3):

By (1) and (2)

(wp(C1,Q)E)(wp(C2,Q)¬E)\displaystyle(\operatorname{wp}(C_{1},Q)\wedge E)\vee(\operatorname{wp}(C_{2},Q)\wedge\neg E)
(wp(C1,Q)E)(wp(C2,Q)¬E)\displaystyle\quad\Rightarrow(\operatorname{wp}(C_{1},Q^{\prime})\wedge E)\vee(\operatorname{wp}(C_{2},Q^{\prime})\wedge\neg E)
(4):

By (3) and definition of wp\operatorname{wp}

wp(ifEthenC1elseC2,Q)\displaystyle\operatorname{wp}(\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2},Q)
wp(ifEthenC1elseC2,Q)\displaystyle\quad\Rightarrow\operatorname{wp}(\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2},Q^{\prime})\qed

B.2. Strongest postconditions

Definition 0.

QQ is the strongest postcondition for precondition PP and statement CC, denoted sp(P,C)\operatorname{sp}(P,C), if it is the strongest predicate (WRT \Rightarrow) that ensures that if a state satisfies PP, then after executing CC, QQ holds.

The following explicit calculations correspond to the previous definition (proved in Theorem 8):

sp(P,skip)=P\displaystyle\operatorname{sp}(P,\textnormal{{skip}})=P
sp(P,x:-E)=v(x=E[x/v]P[x/v])\displaystyle\operatorname{sp}(P,x\coloneq E)=\exists v(x=E[x/v]\wedge P[x/v])
where vfv(P)v\notin\operatorname{fv}(P)
sp(P,C1;C2)=sp(sp(P,C1),C2)\displaystyle\operatorname{sp}(P,C_{1};C_{2})=\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2})
sp(P,ifEthenC1elseC2)=\displaystyle\operatorname{sp}(P,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})=
sp(PE,C1)sp(P¬E,C2)\displaystyle\quad\operatorname{sp}(P\wedge E,C_{1})\vee\operatorname{sp}(P\wedge\neg E,C_{2})
Lemma 0 (Stronger precondition).

If PPP\Rightarrow P^{\prime} then
sp(P,C)sp(P,C)\operatorname{sp}(P,C)\Rightarrow\operatorname{sp}(P^{\prime},C).

Proof.

By induction on CC:

skip::

sp(P,skip)PPsp(P,skip)\operatorname{sp}(P,\textnormal{{skip}})\equiv P\Rightarrow P^{\prime}\equiv\operatorname{sp}(P^{\prime},\textnormal{{skip}}).

x:-Ex\coloneq E::

Note that P[x/v]P[x/v]P[x/v]\Rightarrow P^{\prime}[x/v] by logic. Then:

sp(P,x:-E)\displaystyle\operatorname{sp}(P,x\coloneq E) v(x=E[x/v]P[x/v])\displaystyle\equiv\exists v(x=E[x/v]\wedge P[x/v]) defn sp\operatorname{sp}
v(x=E[x/v]P[x/v])\displaystyle\Rightarrow\exists v(x=E[x/v]\wedge P^{\prime}[x/v]) logic
sp(P,x:-E)\displaystyle\equiv\operatorname{sp}(P^{\prime},x\coloneq E) defn sp\operatorname{sp}
C1;C2C_{1};C_{2}::
(1):

sp(P,C1)sp(P,C1)\operatorname{sp}(P,C_{1})\Rightarrow\operatorname{sp}(P^{\prime},C_{1}) by induction

(2):

sp(sp(P,C1),C2)sp(sp(P,C1),C2)\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2})\Rightarrow\operatorname{sp}(\operatorname{sp}(P^{\prime},C_{1}),C_{2}) by induction using (1)

(3):

By (2) and definition of sp\operatorname{sp},

sp(P,C1;C2)\displaystyle\operatorname{sp}(P,C_{1};C_{2}) sp(sp(P,C1),C2)\displaystyle\equiv\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2})
sp(sp(P,C1),C2)\displaystyle\Rightarrow\operatorname{sp}(\operatorname{sp}(P^{\prime},C_{1}),C_{2})
sp(P,C1;C2)\displaystyle\equiv\operatorname{sp}(P^{\prime},C_{1};C_{2})
ifEthenC1elseC2\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}::
(1):

PEPEP\wedge E\Rightarrow P^{\prime}\wedge E by logic

(2):

sp(PE,C1)sp(PE,C1)\operatorname{sp}(P\wedge E,C_{1})\Rightarrow\operatorname{sp}(P^{\prime}\wedge E,C_{1}) by induction using (1)

(3):

P¬EP¬EP\wedge\neg E\Rightarrow P^{\prime}\wedge\neg E by logic

(4):

sp(P¬E,C2)sp(P¬E,C2)\operatorname{sp}(P\wedge\neg E,C_{2})\Rightarrow\operatorname{sp}(P^{\prime}\wedge\neg E,C_{2}) by induction using (3)

(5):
sp(P,ifEthenC1elseC2)\displaystyle\operatorname{sp}(P,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})
sp(PE,C1)sp(P¬E,C2)\displaystyle\quad\equiv\operatorname{sp}(P\wedge E,C_{1})\vee\operatorname{sp}(P\wedge\neg E,C_{2}) defn sp\operatorname{sp}
sp(PE,C1)sp(P¬E,C2)\displaystyle\quad\Rightarrow\operatorname{sp}(P^{\prime}\wedge E,C_{1})\vee\operatorname{sp}(P^{\prime}\wedge\neg E,C_{2}) (2), (4), logic
sp(P,ifEthenC1elseC2)\displaystyle\quad\equiv\operatorname{sp}(P^{\prime},\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}) defn sp\operatorname{sp}
Lemma 0.

sp(,C)\operatorname{sp}(\bot,C)\equiv\bot

Proof.

By induction on CC:

skip::

sp(,skip)\operatorname{sp}(\bot,\textnormal{{skip}})\equiv\bot by definition.

x:-Ex\coloneq E::

sp(,x:-E)v(x=E[x/v])\operatorname{sp}(\bot,x\coloneq E)\equiv\exists v(x=E[x/v]\wedge\bot)\equiv\bot by logic.

ifEthenC1elseC2\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}::
sp(,ifEthenC1elseC2)\displaystyle\operatorname{sp}(\bot,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})
sp(E,C1)sp(¬E,C2)\displaystyle\quad\equiv\operatorname{sp}(\bot\wedge E,C_{1})\vee\operatorname{sp}(\bot\wedge\neg E,C_{2}) defn sp\operatorname{sp}
sp(,C1)sp(,C2)\displaystyle\quad\equiv\operatorname{sp}(\bot,C_{1})\vee\operatorname{sp}(\bot,C_{2}) logic
\displaystyle\quad\equiv\bot\vee\bot induction
\displaystyle\quad\equiv\bot logic
C1;C2C_{1};C_{2}::
sp(,C1;C2)\displaystyle\operatorname{sp}(\bot,C_{1};C_{2}) sp(sp(,C1),C2)\displaystyle\equiv\operatorname{sp}(\operatorname{sp}(\bot,C_{1}),C_{2}) defn sp\operatorname{sp}
sp(,C2)\displaystyle\equiv\operatorname{sp}(\bot,C_{2}) induction
\displaystyle\equiv\bot induction
Lemma 0.

sp(P,C)P\operatorname{sp}(P,C)\equiv\bot\implies P\equiv\bot

Note: for a more expressive language, we would also need termination for this to hold.

Proof.

By induction on CC:

skip::

sp(P,skip)P\bot\equiv\operatorname{sp}(P,\textnormal{{skip}})\equiv P by definition.

x:-Ex\coloneq E::
(1):

sp(P,x:-E)\bot\equiv\operatorname{sp}(P,x\coloneq E) by assumption

(2):

sp(P,x:-E)v(x=E[x/v]P[x/v])\operatorname{sp}(P,x\coloneq E)\equiv\exists v(x=E[x/v]\wedge P[x/v])

(3):

v(xE[x/v]¬P[x/v])\top\equiv\forall v(x\neq E[x/v]\vee\neg P[x/v]) by logic using (1) and (2)

(4):

v¬P[x/v]\top\equiv\forall v\neg P[x/v] by logic using (3)

We prove this using a semantic argument (assuming standard Kripke semantics for FOL):

Let \mathcal{M} be a model and 𝗒||\mathsf{y}\in\lvert\mathcal{M}\rvert. By (3) we have v(xE[x/v]¬P[x/v])\mathcal{M}\vDash\forall v(x\neq E[x/v]\vee\neg P[x/v]), and thus [v𝗒]xE[x/v]¬P[x/v]\mathcal{M}[v\mapsto\mathsf{y}]\vDash x\neq E[x/v]\vee\neg P[x/v].

Let 𝗓=E[x/v][v𝗒]\mathsf{z}=\llbracket E[x/v]\rrbracket_{\mathcal{M}[v\mapsto\mathsf{y}]}. Then we have [v𝗒,x𝗓]xE[x/v]\mathcal{M}[v\mapsto\mathsf{y},x\mapsto\mathsf{z}]\nvDash x\neq E[x/v].

Thus we have [v𝗒,x𝗓]¬P[x/v]\mathcal{M}[v\mapsto\mathsf{y},x\mapsto\mathsf{z}]\vDash\neg P[x/v]. Since xfv(P[x/v])x\notin\operatorname{fv}(P[x/v]), we have [v𝗒]¬P[x/v]\mathcal{M}[v\mapsto\mathsf{y}]\vDash\neg P[x/v]. Thus we can conclude that v¬P[x/v]\mathcal{M}\vDash\forall v\neg P[x/v].

(5):

¬P[x/v]¬P\top\equiv\neg P[x/v]\equiv\neg P by logic using (4) and since vfv(P)v\notin\operatorname{fv}(P)

(6):

P\bot\equiv P by logic using (5)

C1;C2C_{1};C_{2}:
(1):

sp(P,C1;C2)\bot\equiv\operatorname{sp}(P,C_{1};C_{2}) by assumption

(2):

sp(P,C1;C2)sp(sp(P,C1),C2)\operatorname{sp}(P,C_{1};C_{2})\equiv\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2}) by definition

(3):

sp(sp(P,C1),C2)\bot\equiv\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2}) by logic using (1) and (2)

(4):

sp(P,C1)\bot\equiv\operatorname{sp}(P,C_{1}) by induction using (3)

(5):

P\bot\equiv P by induction using (4)

[ifEthenC1elseC2\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}]

(1):

sp(P,ifEthenC1elseC2)\bot\equiv\operatorname{sp}(P,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}) by assumption

(2):

sp(P,ifEthenC1elseC2)sp(PE,C1)sp(P¬E,C2)\operatorname{sp}(P,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})\equiv\operatorname{sp}(P\wedge E,C_{1})\vee\operatorname{sp}(P\wedge\neg E,C_{2}) by definition

(3):

sp(PE,C1)sp(P¬E,C2)\bot\equiv\operatorname{sp}(P\wedge E,C_{1})\vee\operatorname{sp}(P\wedge\neg E,C_{2}) by (1) and (2)

(4):

sp(PE,C1)\bot\equiv\operatorname{sp}(P\wedge E,C_{1}) by logic using (3)

(5):

sp(P¬E,C2)\bot\equiv\operatorname{sp}(P\wedge\neg E,C_{2}) by logic using (3)

(6):

PE\bot\equiv P\wedge E by induction using (4)

(7):

P¬E\bot\equiv P\wedge\neg E by induction using (5)

(8):

(PE)(P¬E)P(E¬E)P\bot\equiv(P\wedge E)\vee(P\wedge\neg E)\equiv P\wedge(E\vee\neg E)\equiv P by logic using (6) and (7) ∎

Lemma 0.

If PSatFormulaP\in\textsc{SatFormula} then sp(P,C)SatFormula\operatorname{sp}(P,C)\in\textsc{SatFormula}.

Proof.

We prove the contrapositive: assume sp(P,C)SatFormula\operatorname{sp}(P,C)\notin\\ \textsc{SatFormula}, then sp(P,C)\operatorname{sp}(P,C)\equiv\bot, and thus by Lemma 6 PP\equiv\bot. Therefore PSatFormulaP\notin\textsc{SatFormula}. ∎

Lemma 0.

If yfv(C)y\notin\operatorname{fv}(C) then sp(yP,C)ysp(P,C)\operatorname{sp}(\exists y\,P,C)\equiv\exists y\operatorname{sp}(P,C).

Proof.

By induction on CC:

skip::

sp(yP,skip)yPysp(P,skip)\operatorname{sp}(\exists y\,P,\textnormal{{skip}})\equiv\exists y\,P\equiv\exists y\,\operatorname{sp}(P,\textnormal{{skip}})

x:-Ex\coloneq E::

Assuming yvy\not\equiv v,

sp(yP,x:-E)\displaystyle\operatorname{sp}(\exists y\,P,x\coloneq E)
v(x=E[x/v](yP)[x/v])\displaystyle\quad\equiv\exists v(x=E[x/v]\wedge(\exists y\,P)[x/v]) defn sp\operatorname{sp}
v(x=E[x/v](yP[x/v]))\displaystyle\quad\equiv\exists v(x=E[x/v]\wedge(\exists y\,P[x/v])) yxfv(x:-E)\displaystyle y\not\equiv x\in\operatorname{fv}(x\coloneq E)
yv(x=E[x/v]P[x/v])\displaystyle\quad\equiv\exists y\exists v(x=E[x/v]\wedge P[x/v]) yfv(E[x/v])\displaystyle y\notin\operatorname{fv}(E[x/v])
ysp(P,x:-E)\displaystyle\quad\equiv\exists y\,\operatorname{sp}(P,x\coloneq E) defn sp\operatorname{sp}
C1;C2C_{1};C_{2}::
sp(yP,C1;C2)\displaystyle\operatorname{sp}(\exists y\,P,C_{1};C_{2}) sp(sp(yP,C1),C2)\displaystyle\equiv\operatorname{sp}(\operatorname{sp}(\exists y\,P,C_{1}),C_{2}) defn sp\operatorname{sp}
sp(ysp(P,C1),C2)\displaystyle\equiv\operatorname{sp}(\exists y\,\operatorname{sp}(P,C_{1}),C_{2}) induction
ysp(sp(P,C1),C2)\displaystyle\equiv\exists y\,\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2}) induction
ysp(P,C1;C2)\displaystyle\equiv\exists y\,\operatorname{sp}(P,C_{1};C_{2}) defn sp\operatorname{sp}
ifEthenC1elseC2\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}::
sp(yP,ifEthenC1elseC2)\displaystyle\operatorname{sp}(\exists y\,P,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})
sp((yP)E,C1)sp((yP)¬E,C2)\displaystyle\quad\equiv\operatorname{sp}((\exists y\,P)\wedge E,C_{1})\vee\operatorname{sp}((\exists y\,P)\wedge\neg E,C_{2}) defn sp\operatorname{sp}
sp(y(PE),C1)sp(y(P¬E),C2)\displaystyle\quad\equiv\operatorname{sp}(\exists y(P\wedge E),C_{1})\vee\operatorname{sp}(\exists y(P\wedge\neg E),C_{2}) yfv(E)\displaystyle y\notin\operatorname{fv}(E)
(ysp(PE,C1))(ysp(P¬E,C2))\displaystyle\quad\equiv(\exists y\,\operatorname{sp}(P\wedge E,C_{1}))\vee(\exists y\,\operatorname{sp}(P\wedge\neg E,C_{2})) induction
y(sp(PE,C1)sp(P¬E,C2))\displaystyle\quad\equiv\exists y(\operatorname{sp}(P\wedge E,C_{1})\vee\operatorname{sp}(P\wedge\neg E,C_{2})) logic
ysp(P,ifEthenC1elseC2)\displaystyle\quad\equiv\exists y\,\operatorname{sp}(P,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}) defn sp\operatorname{sp}
Lemma 0.

sp(P1P2,C)sp(P1,C)sp(P2,C)\operatorname{sp}(P_{1}\vee P_{2},C)\equiv\operatorname{sp}(P_{1},C)\vee\operatorname{sp}(P_{2},C)

Proof.

By induction on CC:

skip::

sp(P1P2,skip)P1P2sp(P1,skip)sp(P2,skip)\operatorname{sp}(P_{1}\vee P_{2},\textnormal{{skip}})\equiv P_{1}\vee P_{2}\equiv\\ \operatorname{sp}(P_{1},\textnormal{{skip}})\vee\operatorname{sp}(P_{2},\textnormal{{skip}}).

x:-Ex\coloneq E::
sp(P1P2,x:-E)\displaystyle\operatorname{sp}(P_{1}\vee P_{2},x\coloneq E)
v(x=E[x/v](P1P2)[x/v])\displaystyle\quad\equiv\exists v(x=E[x/v]\wedge(P_{1}\vee P_{2})[x/v]) defn sp\operatorname{sp}
v(x=E[x/v](P1[x/v]P2[x/v]))\displaystyle\quad\equiv\exists v(x=E[x/v]\wedge(P_{1}[x/v]\vee P_{2}[x/v])) subst.
v((x=E[x/v]P1[x/v])\displaystyle\quad\equiv\exists v((x=E[x/v]\wedge P_{1}[x/v])~{}\vee
(x=E[x/v]P2[x/v]))\displaystyle\hskip 38.99998pt(x=E[x/v]\wedge P_{2}[x/v])) logic
(v(x=E[x/v]P1[x/v]))\displaystyle\quad\equiv(\exists v(x=E[x/v]\wedge P_{1}[x/v]))~{}\vee
(v(x=E[x/v]P2[x/v]))\displaystyle\hskip 23.99997pt(\exists v(x=E[x/v]\wedge P_{2}[x/v])) logic
sp(P1,x:-E)sp(P2,x:-E)\displaystyle\quad\equiv\operatorname{sp}(P_{1},x\coloneq E)\vee\operatorname{sp}(P_{2},x\coloneq E) defn sp\operatorname{sp}
C1;C2C_{1};C_{2}::
sp(P1P2,C1;C2)\displaystyle\operatorname{sp}(P_{1}\vee P_{2},C_{1};C_{2})
sp(sp(P1P2,C1),C2)\displaystyle\quad\equiv\operatorname{sp}(\operatorname{sp}(P_{1}\vee P_{2},C_{1}),C_{2}) defn sp\operatorname{sp}
sp(sp(P1,C1)sp(P2,C1),C2)\displaystyle\quad\equiv\operatorname{sp}(\operatorname{sp}(P_{1},C_{1})\vee\operatorname{sp}(P_{2},C_{1}),C_{2}) induction
sp(sp(P1,C1),C2)sp(sp(P2,C1),C2)\displaystyle\quad\equiv\operatorname{sp}(\operatorname{sp}(P_{1},C_{1}),C_{2})\vee\operatorname{sp}(\operatorname{sp}(P_{2},C_{1}),C_{2}) induction
sp(P1,C1;C2)sp(P2,C1;C2)\displaystyle\quad\equiv\operatorname{sp}(P_{1},C_{1};C_{2})\vee\operatorname{sp}(P_{2},C_{1};C_{2}) defn sp\operatorname{sp}
ifEthenC1elseC2\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}::
sp(P1P2,ifEthenC1elseC2)sp(E(P1P2),C1)sp(¬E(P1P2),C2)defn spsp((EP1)(EP2),C1)sp((¬EP1)(¬EP2),C2)logicsp(EP1,C1)sp(EP2,C1)sp(¬EP1,C2)sp(¬EP2,C2)Lemma 9(sp(EP1,C1)sp(¬EP1,C2))(sp(EP2,C1)sp(¬EP2,C2))logicsp(P1,ifEthenC1elseC2)sp(P2,ifEthenC1elseC2)defn sp\begin{array}[]{p{1em}rlp{1em}r}\lx@intercol\operatorname{sp}(P_{1}\vee P_{2},\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})\hfil\lx@intercol\\ &\equiv&\operatorname{sp}(E\wedge(P_{1}\vee P_{2}),C_{1})~{}\vee\\ &&\operatorname{sp}(\neg E\wedge(P_{1}\vee P_{2}),C_{2})&&\text{defn $\operatorname{sp}$}\\ &\equiv&\operatorname{sp}((E\wedge P_{1})\vee(E\wedge P_{2}),C_{1})~{}\vee\\ &&\operatorname{sp}((\neg E\wedge P_{1})\vee(\neg E\wedge P_{2}),C_{2})&&\text{logic}\\ &\equiv&\operatorname{sp}(E\wedge P_{1},C_{1})\vee\operatorname{sp}(E\wedge P_{2},C_{1})~{}\vee\\ &&\operatorname{sp}(\neg E\wedge P_{1},C_{2})\vee\operatorname{sp}(\neg E\wedge P_{2},C_{2})&&\text{\lx@cref{creftypecap~refnum}{lem:sp-disj}}\\ &\equiv&(\operatorname{sp}(E\wedge P_{1},C_{1})\vee\operatorname{sp}(\neg E\wedge P_{1},C_{2}))~{}\vee\\ &&(\operatorname{sp}(E\wedge P_{2},C_{1})\vee\operatorname{sp}(\neg E\wedge P_{2},C_{2}))&&\text{logic}\\ &\equiv&\operatorname{sp}(P_{1},\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})~{}\vee\\ &&\operatorname{sp}(P_{2},\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})&&\text{defn $\operatorname{sp}$}\end{array}\qed
Lemma 0 (Frame rule).

If mod(C)fv(P)=\operatorname{mod}(C)\cap\operatorname{fv}(P)=\emptyset then
sp(PR,C)Psp(R,C)\operatorname{sp}(P\wedge R,C)\equiv P\wedge\operatorname{sp}(R,C).

Proof.

By induction on CC:

skip::

sp(PR,skip)PRPsp(R,skip)\operatorname{sp}(P\wedge R,\textnormal{{skip}})\equiv P\wedge R\equiv P\wedge\operatorname{sp}(R,\textnormal{{skip}})

x:-Ex\coloneq E::

Let vfv(P)v\notin\operatorname{fv}(P). Note that xfv(x:-E)x\in\operatorname{fv}(x\coloneq E), thus from our assumptions xfv(P)x\notin\operatorname{fv}(P) Then,

sp(PR,x:-E)\displaystyle\operatorname{sp}(P\wedge R,x\coloneq E)
v(x=E[x/v]P[x/v]R[x/v])\displaystyle\quad\equiv\exists v(x=E[x/v]\wedge P[x/v]\wedge R[x/v]) defn sp\operatorname{sp}
v(x=E[x/v]PR[x/v])\displaystyle\quad\equiv\exists v(x=E[x/v]\wedge P\wedge R[x/v]) xfv(P)x\notin\operatorname{fv}(P)
Pv(x=E[x/v]R[x/v])\displaystyle\quad\equiv P\wedge\exists v(x=E[x/v]\wedge R[x/v]) vfv(P)v\notin\operatorname{fv}(P)
Psp(R,x:-E)\displaystyle\quad\equiv P\wedge\operatorname{sp}(R,x\coloneq E) defn sp\operatorname{sp}
C1;C2C_{1};C_{2}::

Note that xmod(C1;C2)x\notin\operatorname{mod}(C_{1};C_{2}) thus xmod(C1)x\notin\operatorname{mod}(C_{1}) and x\ClassNoteNoLineclassnamenotetextmod(C2)x\ClassNoteNoLine{classname}{notetext}\operatorname{mod}(C_{2}). Then,

sp(PR,C1;C2)\displaystyle\operatorname{sp}(P\wedge R,C_{1};C_{2}) sp(sp(PR,C1),C2)\displaystyle\equiv\operatorname{sp}(\operatorname{sp}(P\wedge R,C_{1}),C_{2}) defn sp\operatorname{sp}
sp(Psp(R,C1),C2)\displaystyle\equiv\operatorname{sp}(P\wedge\operatorname{sp}(R,C_{1}),C_{2}) induction
Psp(sp(R,C1),C2)\displaystyle\equiv P\wedge\operatorname{sp}(\operatorname{sp}(R,C_{1}),C_{2}) induction
Psp(R,C1;C2)\displaystyle\equiv P\wedge\operatorname{sp}(R,C_{1};C_{2}) defn sp\operatorname{sp}
ifEthenC1elseC2\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}::

Note that by assumption
xmod(ifEthenC1elseC2)x\notin\operatorname{mod}(\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}) thus xmod(C1)x\notin\operatorname{mod}(C_{1}) and xmod(C2)x\notin\operatorname{mod}(C_{2}). Then,

sp(PR,ifEthenC1elseC2)\displaystyle\operatorname{sp}(P\wedge R,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})
sp(PRE,C1)sp(PR¬E,C2)\displaystyle\quad\equiv\operatorname{sp}(P\wedge R\wedge E,C_{1})\vee\operatorname{sp}(P\wedge R\wedge\neg E,C_{2}) defn sp\operatorname{sp}
(Psp(RE,C1))(Psp(R¬E,C2))\displaystyle\quad\equiv(P\wedge\operatorname{sp}(R\wedge E,C_{1}))\vee(P\wedge\operatorname{sp}(R\wedge\neg E,C_{2})) ind.
P(sp(RE,C1)sp(R¬E,C2))\displaystyle\quad\equiv P\wedge(\operatorname{sp}(R\wedge E,C_{1})\vee\operatorname{sp}(R\wedge\neg E,C_{2})) logic
Psp(R,ifEthenC1elseC2)\displaystyle\quad\equiv P\wedge\operatorname{sp}(R,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}) defn sp\operatorname{sp}
Lemma 0.
sp(PE,ifEthenC1elseC2)\displaystyle\operatorname{sp}(P\wedge E,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}) sp(PE,C1)\displaystyle\equiv\operatorname{sp}(P\wedge E,C_{1})
sp(P¬E,ifEthenC1elseC2)\displaystyle\operatorname{sp}(P\wedge\neg E,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}) sp(P¬E,C2)\displaystyle\equiv\operatorname{sp}(P\wedge\neg E,C_{2})
Proof.
sp(PE,ifEthenC1elseC2)\displaystyle\operatorname{sp}(P\wedge E,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})
sp(PEE,C1)sp(PE¬E,C2)\displaystyle\quad\equiv\operatorname{sp}(P\wedge E\wedge E,C_{1})\vee\operatorname{sp}(P\wedge E\wedge\neg E,C_{2}) defn sp\operatorname{sp}
sp(PE,C1)sp(,C2)\displaystyle\quad\equiv\operatorname{sp}(P\wedge E,C_{1})\vee\operatorname{sp}(\bot,C_{2}) logic
sp(PE,C1)\displaystyle\quad\equiv\operatorname{sp}(P\wedge E,C_{1})\vee\bot
sp(PE,C1)\displaystyle\quad\equiv\operatorname{sp}(P\wedge E,C_{1}) logic
sp(P¬E,ifEthenC1elseC2)\displaystyle\operatorname{sp}(P\wedge\neg E,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})
sp(PE¬E,C1)sp(P¬E¬E,C2)\displaystyle\quad\equiv\operatorname{sp}(P\wedge E\wedge\neg E,C_{1})\vee\operatorname{sp}(P\wedge\neg E\wedge\neg E,C_{2}) defn sp
sp(,C1)sp(P¬E,C2)\displaystyle\quad\equiv\operatorname{sp}(\bot,C_{1})\vee\operatorname{sp}(P\wedge\neg E,C_{2}) logic
sp(P¬E,C2)\displaystyle\quad\equiv\bot\vee\operatorname{sp}(P\wedge\neg E,C_{2})
sp(P¬E,C2)\displaystyle\quad\equiv\operatorname{sp}(P\wedge\neg E,C_{2}) logic ∎

B.3. Fixpoint

We can define a function Qsp(wp(C,Q),C)Q\mapsto\operatorname{sp}(\operatorname{wp}(C,Q),C). We demonstrate that this function reaches a fixpoint.

Lemma 0.

sp(wp(C,Q)P,C)Qsp(P,C)\operatorname{sp}(\operatorname{wp}(C,Q)\wedge P,C)\equiv Q\wedge\operatorname{sp}(P,C)

Proof.

By induction on CC:

skip::

sp(wp(skip,Q)P,skip)QPQsp(P,skip)\operatorname{sp}(\operatorname{wp}(\textnormal{{skip}},Q)\wedge P,\textnormal{{skip}})\equiv Q\wedge P\equiv\\ Q\wedge\operatorname{sp}(P,\textnormal{{skip}})

x:-Ex\coloneq E::

Let vfv(Q)v\notin\operatorname{fv}(Q). Then,

sp(wp(x:-E,Q)P,x:-E)\displaystyle\operatorname{sp}(\operatorname{wp}(x\coloneq E,Q)\wedge P,x\coloneq E)
v(x=E[x/v]\displaystyle\quad\equiv\exists v(x=E[x/v]~{}\wedge
(wp(x:-E,Q)P)[x/v])\displaystyle\hskip 37.00002pt(\operatorname{wp}(x\coloneq E,Q)\wedge P)[x/v]) defn sp\operatorname{sp}
v(x=E[x/v](Q[x/E]P)[x/v])\displaystyle\quad\equiv\exists v(x=E[x/v]\wedge(Q[x/E]\wedge P)[x/v]) defn wp\operatorname{wp}
v(x=E[x/v]Q[x/E[x/v]]P[x/v])\displaystyle\quad\equiv\exists v(x=E[x/v]\wedge Q[x/E[x/v]]\wedge P[x/v]) subst
v(x=E[x/v]Q[x/x]P[x/v])\displaystyle\quad\equiv\exists v(x=E[x/v]\wedge Q[x/x]\wedge P[x/v]) subst =
v(x=E[x/v]QP[x/v])\displaystyle\quad\equiv\exists v(x=E[x/v]\wedge Q\wedge P[x/v]) redundant
Q(vx=E[x/v]P[x/v])\displaystyle\quad\equiv Q\wedge(\exists v\,x=E[x/v]\wedge P[x/v]) vfv(Q)\displaystyle v\notin\operatorname{fv}(Q)
Qsp(P,x:-E)\displaystyle\quad\equiv Q\wedge\operatorname{sp}(P,x\coloneq E) defn sp\operatorname{sp}
ifEthenC1elseC2\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}::
(1):

Qsp(PE,C1)sp(wp(C1,Q)PE,C1)Q\wedge\operatorname{sp}(P\wedge E,C_{1})\equiv\operatorname{sp}(\operatorname{wp}(C_{1},Q)\wedge P\wedge E,C_{1}) by induction using C1C_{1}

(2):

Qsp(P¬E,C2)sp(wp(C2,Q)P¬E,C2)Q\wedge\operatorname{sp}(P\wedge\neg E,C_{2})\equiv\operatorname{sp}(\operatorname{wp}(C_{2},Q)\wedge P\wedge\neg E,C_{2}) by induction using C2C_{2}

(3):
Qsp(P,ifEthenC1elseC2)\displaystyle Q\wedge\operatorname{sp}(P,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})
Q(sp(PE,C1)sp(P¬E,C2))\displaystyle\quad\equiv Q\wedge(\operatorname{sp}(P\wedge E,C_{1})\vee\operatorname{sp}(P\wedge\neg E,C_{2})) defn sp\operatorname{sp}
(Qsp(PE,C1))\displaystyle\quad\equiv(Q\wedge\operatorname{sp}(P\wedge E,C_{1}))~{}\vee
(Qsp(P¬E,C2))\displaystyle\hskip 23.50009pt(Q\wedge\operatorname{sp}(P\wedge\neg E,C_{2})) logic
sp(wp(C1,Q)PE,C1)\displaystyle\quad\equiv\operatorname{sp}(\operatorname{wp}(C_{1},Q)\wedge P\wedge E,C_{1})~{}\vee
sp(wp(C2,Q)P¬E,C2)\displaystyle\hskip 21.00009pt\operatorname{sp}(\operatorname{wp}(C_{2},Q)\wedge P\wedge\neg E,C_{2}) (1), (2)
sp(wp(C1,Q)PE,\displaystyle\quad\equiv\operatorname{sp}(\operatorname{wp}(C_{1},Q)\wedge P\wedge E,
ifEthenC1elseC2)\displaystyle\hskip 36.0001pt\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})~{}\vee
sp(wp(C2,Q)P¬E,\displaystyle\hskip 22.50003pt\operatorname{sp}(\operatorname{wp}(C_{2},Q)\wedge P\wedge\neg E,
ifEthenC1elseC2)\displaystyle\hskip 36.0001pt\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})
sp(((wp(C1,Q)E)\displaystyle\quad\equiv\operatorname{sp}(((\operatorname{wp}(C_{1},Q)\wedge E)~{}\vee
(wp(C2,Q)¬E))P,\displaystyle\hskip 40.00006pt(\operatorname{wp}(C_{2},Q)\wedge\neg E))\wedge P,
ifEthenC1elseC2)\displaystyle\hskip 38.00008pt\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})
sp(wp(ifEthenC1elseC2,Q)P,\displaystyle\quad\equiv\operatorname{sp}(\operatorname{wp}(\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2},Q)\wedge P,
ifEthenC1elseC2)\displaystyle\hskip 35.00005pt\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}) defn wp\operatorname{wp}
C1;C2C_{1};C_{2}::
(1):

wp(C2,Q)sp(P,C1)sp(wp(C1,wp(C2,Q))P,C1)\operatorname{wp}(C_{2},Q)\wedge\operatorname{sp}(P,C_{1})\equiv\operatorname{sp}(\operatorname{wp}(C_{1},\\ \operatorname{wp}(C_{2},Q))\wedge P,C_{1}) by induction using C1C_{1}

(2):

Qsp(sp(P,C1),C2)sp(wp(C2,Q)sp(P,C1),C2)Q\wedge\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2})\equiv\operatorname{sp}(\operatorname{wp}(C_{2},Q)\wedge\\ \operatorname{sp}(P,C_{1}),C_{2}) by induction using C2C_{2}. Then,

Qsp(P,C1;C2)\displaystyle Q\wedge\operatorname{sp}(P,C_{1};C_{2})
Qsp(sp(P,C1),C2)\displaystyle\quad\equiv Q\wedge\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2}) defn sp\operatorname{sp}
sp(wp(C2,Q)sp(P,C1),C2)\displaystyle\quad\equiv\operatorname{sp}(\operatorname{wp}(C_{2},Q)\wedge\operatorname{sp}(P,C_{1}),C_{2}) (2)
sp(sp(wp(C1,wp(C2,Q))P,C1),C2)\displaystyle\quad\equiv\operatorname{sp}(\operatorname{sp}(\operatorname{wp}(C_{1},\operatorname{wp}(C_{2},Q))\wedge P,C_{1}),C_{2}) (1)
sp(sp(wp(C1;C2,Q)P,C1),C2)\displaystyle\quad\equiv\operatorname{sp}(\operatorname{sp}(\operatorname{wp}(C_{1};C_{2},Q)\wedge P,C_{1}),C_{2}) defn wp\operatorname{wp}
sp(wp(C1;C2,Q)P,C1;C2)\displaystyle\quad\equiv\operatorname{sp}(\operatorname{wp}(C_{1};C_{2},Q)\wedge P,C_{1};C_{2}) defn sp\operatorname{sp}
Lemma 0 (Fixpoint property).

If Qsp(,C)Q\Rightarrow\operatorname{sp}(\top,C) then
Qsp(wp(C,Q),C)Q\equiv\operatorname{sp}(\operatorname{wp}(C,Q),C).

Proof.

Immediate from Lemma 12, noting that QQsp(,C)Q\equiv Q\wedge\operatorname{sp}(\top,C) in this case. ∎

Lemma 0.

If Qsp(P,C)Q\Rightarrow\operatorname{sp}(P,C) then Qsp(Pwp(C,Q),C)Q\equiv\operatorname{sp}(P\wedge\operatorname{wp}(C,Q),C).

Proof.
Q\displaystyle Q Qsp(P,C)\displaystyle\equiv Q\wedge\operatorname{sp}(P,C) Qsp(P,C)\displaystyle Q\Rightarrow\operatorname{sp}(P,C)
sp(Pwp(C,Q),C)\displaystyle\equiv\operatorname{sp}(P\wedge\operatorname{wp}(C,Q),C)

Appendix C Hoare logic

Hoare triples are denoted by {P}C{Q}\vdash\{P\}~{}C~{}\{Q\}. Deductions in Hoare logic are characterized by the following rules:

{mathpar}\inferrule

[OX-Skip] ⊢{P}   skip   {P} \inferrule[OX-Assign] ⊢{P[x/E]}   x :-E   {P} \inferrule[OX-Seq] ⊢{P}   C_1   {R}

⊢{R}   C_2   {Q} ⊢{P}   C_1; C_2   {Q} \inferrule[OX-If] ⊢{E ∧P}   C_1   {Q}

⊢{¬E ∧P}   C_2   {Q} ⊢{P}   if  E  then  C_1  else  C_2   {Q} \inferrule[OX-Cons] P ⇒P’
⊢{P’}   C   {Q’}
Q’ ⇒Q ⊢{P}   C   {Q}

C.1. Weakest preconditions

Lemma 0.

If {P}C{Q}\vdash\{P\}~{}C~{}\{Q\} then Pwp(C,Q)P\Rightarrow\operatorname{wp}(C,Q).

Proof.

By induction on the derivation of {P}C{Q}\vdash\{P\}~{}C~{}\{Q\}:

OX-Skip::
(1):

PQP\equiv Q by inversion

(2):

PQwp(skip,Q)P\equiv Q\equiv\operatorname{wp}(\textnormal{{skip}},Q) by definition

OX-Assign::
(1):

PQ[E/x]P\equiv Q[E/x] by inversion

(2):

PQ[E/x]wp(x:-E,Q)P\equiv Q[E/x]\equiv\operatorname{wp}(x\coloneq E,Q) by definition

OX-Seq::
(1):

{P}C1{R}\vdash\{P\}~{}C_{1}~{}\{R\} for some RR by inversion

(2):

Psp(C1,R)P\Rightarrow\operatorname{sp}(C_{1},R) by induction using (1)

(3):

{R}C2{Q}\vdash\{R\}~{}C_{2}~{}\{Q\} by inversion

(4):

Rwp(C2,Q)R\Rightarrow\operatorname{wp}(C_{2},Q) by induction using (3)

(5):

wp(C1,R)wp(C1,wp(C2,Q))\operatorname{wp}(C_{1},R)\Rightarrow\operatorname{wp}(C_{1},\operatorname{wp}(C_{2},Q)) by Lemma 2 using (4)

(6):

By (4), (5), and definition of wp\operatorname{wp},

P\displaystyle P wp(C1,R)\displaystyle\Rightarrow\operatorname{wp}(C_{1},R)
wp(C1,wp(C2,Q))\displaystyle\Rightarrow\operatorname{wp}(C_{1},\operatorname{wp}(C_{2},Q))
wp(C1;C2,Q)\displaystyle\equiv\operatorname{wp}(C_{1};C_{2},Q)
OX-If::
(1):

{EP}C1{Q}\vdash\{E\wedge P\}~{}C_{1}~{}\{Q\} by inversion

(2):

EPwp(C1,Q)E\wedge P\Rightarrow\operatorname{wp}(C_{1},Q) by induction using (1)

(3):

{¬EP}C2{Q}\vdash\{\neg E\wedge P\}~{}C_{2}~{}\{Q\} by inversion

(4):

¬EPwp(C2,Q)\neg E\wedge P\Rightarrow\operatorname{wp}(C_{2},Q) by induction using (3)

(5):
P\displaystyle P (PE)(P¬E)\displaystyle\equiv(P\wedge E)\vee(P\wedge\neg E) logic
(wp(C1,Q)E)(wp(C2,Q)¬E)\displaystyle\Rightarrow(\operatorname{wp}(C_{1},Q)\wedge E)\vee(\operatorname{wp}(C_{2},Q)\wedge\neg E) (2) and (4)
wp(ifEthenC1elseC2,Q)\displaystyle\Rightarrow\operatorname{wp}(\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2},Q) defn wp\operatorname{wp}
OX-Cons::
(1):

PPP\Rightarrow P^{\prime} for some PP^{\prime} by inversion

(2):

QQQ^{\prime}\Rightarrow Q for some QQ^{\prime} by inversion

(3):

{P}C{Q}\vdash\{P^{\prime}\}~{}C~{}\{Q^{\prime}\} by inversion

(4):

Pwp(C,Q)P^{\prime}\Rightarrow\operatorname{wp}(C,Q^{\prime}) by induction using (3)

(5):

wp(C,Q)wp(C,Q)\operatorname{wp}(C,Q^{\prime})\Rightarrow\operatorname{wp}(C,Q) by Lemma 2 using (4)

(6):

PPwp(C,Q)wp(C,Q)P\Rightarrow P^{\prime}\Rightarrow\operatorname{wp}(C,Q^{\prime})\Rightarrow\operatorname{wp}(C,Q) by (1), (4), and (5) ∎

Lemma 0.

{wp(C,Q)}C{Q}\vdash\{\operatorname{wp}(C,Q)\}~{}C~{}\{Q\}

Proof.

By induction on CC:

skip::

wp(skip,Q)Q\operatorname{wp}(\textnormal{{skip}},Q)\equiv Q by definition; (Q)skip(Q)\vdash(Q)~{}\textnormal{{skip}}~{}(Q) by OX-Skip.

x:-Ex\coloneq E::
(1):

wp(x:-E,Q)Q[x/E]\operatorname{wp}(x\coloneq E,Q)\equiv Q[x/E] by definition

(2):

{Q[x/E]}x:-E{Q}\vdash\{Q[x/E]\}~{}x\coloneq E~{}\{Q\} by OX-Assign using (1)

C1;C2C_{1};C_{2}::
(1):

{wp(C1,wp(C2,Q))}C1{wp(C2,Q)}\vdash\{\operatorname{wp}(C_{1},\operatorname{wp}(C_{2},Q))\}~{}C_{1}~{}\{\operatorname{wp}(C_{2},Q)\} by induction

(2):

{wp(C2,Q)}C2{Q}\vdash\{\operatorname{wp}(C_{2},Q)\}~{}C_{2}~{}\{Q\} by induction

(3):

{wp(C1,wp(C2,Q))}C1;C2{Q}\vdash\{\operatorname{wp}(C_{1},\operatorname{wp}(C_{2},Q))\}~{}C_{1};C_{2}~{}\{Q\} by OX-Seq using (1) and (2)

(4):

wp(C1;C2,Q)wp(C1,wp(C2,Q))\operatorname{wp}(C_{1};C_{2},Q)\equiv\operatorname{wp}(C_{1},\operatorname{wp}(C_{2},Q)) by definition

(5):

{wp(C1;C2,Q)}C1;C2{Q}\vdash\{\operatorname{wp}(C_{1};C_{2},Q)\}~{}C_{1};C_{2}~{}\{Q\} by (3) and (4)

ifEthenC1elseC2\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}::
(1):

Let P(wp(C1,Q)E)(wp(C2,Q)¬E)P\equiv(\operatorname{wp}(C_{1},Q)\wedge E)\vee(\operatorname{wp}(C_{2},Q)\wedge\neg E)

(2):

{wp(C1,Q)}C1{Q}\vdash\{\operatorname{wp}(C_{1},Q)\}~{}C_{1}~{}\{Q\} by induction

(3):

PEwp(C1,Q)P\wedge E\Rightarrow\operatorname{wp}(C_{1},Q) by logic

(4):

{PE}C1{Q}\vdash\{P\wedge E\}~{}C_{1}~{}\{Q\} by OX-Cons using (2) and (3)

(5):

{wp(C2,Q)}C2{Q}\vdash\{\operatorname{wp}(C_{2},Q)\}~{}C_{2}~{}\{Q\} by induction

(6):

P¬Ewp(C2,Q)P\wedge\neg E\Rightarrow\operatorname{wp}(C_{2},Q) by logic

(7):

{P¬E}C2{Q}\vdash\{P\wedge\neg E\}~{}C_{2}~{}\{Q\} by OX-Cons using (5) and (6)

(8):

{P}ifEthenC1elseC2{Q}\vdash\{P\}~{}\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}~{}\{Q\} by OX-If using (4) and (7)

(9):

Pwp(ifEthenC1elseC2,Q)P\equiv\operatorname{wp}(\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2},Q) by (1) and definition of wp\operatorname{wp}

(10):

{wp(ifEthenC1elseC2)}ifEthenC1elseC2{Q}\vdash\{\operatorname{wp}(\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})\}\\ \textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}\ \{Q\} by (8) and (9) ∎

Lemma 0.

If Pwp(C,Q)P\Rightarrow\operatorname{wp}(C,Q) then {P}C{Q}\vdash\{P\}~{}C~{}\{Q\}.

Proof.

By Lemma 2, {wp(C,Q)}C{Q}\vdash\{\operatorname{wp}(C,Q)\}~{}C~{}\{Q\}, thus by OX-Cons {P}C{Q}\vdash\{P\}~{}C~{}\{Q\}. ∎

Theorem 4.

Pwp(C,Q){P}C{Q}P\Rightarrow\operatorname{wp}(C,Q)\iff\vdash\{P\}~{}C~{}\{Q\}.

Proof.

\Longrightarrow: Lemma 3; \Longleftarrow: Lemma 1. ∎

C.2. Strongest postconditions

Lemma 0.

If {P}C{Q}\vdash\{P\}~{}C~{}\{Q\} then sp(P,C)Q\operatorname{sp}(P,C)\Rightarrow Q.

Proof.

By induction on {P}C{Q}\vdash\{P\}~{}C~{}\{Q\}:

OX-Skip::

Then PQP\equiv Q and CskipC\equiv\textnormal{{skip}}, thus
sp(P,skip)PQ\operatorname{sp}(P,\textnormal{{skip}})\equiv P\equiv Q.

OX-Assign::

Then PQ[x/E]P\equiv Q[x/E] and Cx:-EC\equiv x\coloneq E. Assuming vfv(Q)v\notin\operatorname{fv}(Q):

sp(Q[x/E],x:-E)\displaystyle\operatorname{sp}(Q[x/E],x\coloneq E)
v(x=E[x/v]Q[x/E][x/v])\displaystyle\quad\equiv\exists v(x=E[x/v]\wedge Q[x/E][x/v]) defn
v(x=E[x/v]Q[x/E[x/v]))\displaystyle\quad\equiv\exists v(x=E[x/v]\wedge Q[x/E[x/v])) substitution
v(x=E[x/v]Q[x/x])\displaystyle\quad\equiv\exists v(x=E[x/v]\wedge Q[x/x]) subst. equality
v(x=E[x/v]Q)\displaystyle\quad\equiv\exists v(x=E[x/v]\wedge Q) redundant
Q\displaystyle\quad\Rightarrow Q vfv(Q)\displaystyle v\notin\operatorname{fv}(Q)
OX-Seq::
(1):

CC1;C2C\equiv C_{1};C_{2} for some C1,C2C_{1},C_{2} by inversion

(2):

{P}C1{R}\vdash\{P\}~{}C_{1}~{}\{R\} for some RR by inversion

(3):

{R}C2{Q}\vdash\{R\}~{}C_{2}~{}\{Q\} by inversion

(4):

sp(P,C1)R\operatorname{sp}(P,C_{1})\Rightarrow R by induction using (2)

(5):

sp(sp(P,C1),C2)sp(R,C2)\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2})\Rightarrow\operatorname{sp}(R,C_{2}) by Lemma 4 using (4)

(6):

sp(R,C2)Q\operatorname{sp}(R,C_{2})\Rightarrow Q by induction using (3)

(7):

sp(sp(P,C1),C2)Q\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2})\Rightarrow Q by (5) and (6)

(8):

sp(P,C)sp(P,C1;C2)Q\operatorname{sp}(P,C)\equiv\operatorname{sp}(P,C_{1};C_{2})\Rightarrow Q by (1), (7), and definition of sp\operatorname{sp}

OX-If::
(1):

CifEthenC1elseC2C\equiv\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2} for some E,C1,C2E,C_{1},C_{2} by inversion

(2):

{EP}C1{Q}\vdash\{E\wedge P\}~{}C_{1}~{}\{Q\} by inversion

(3):

{¬EP}C2{Q}\vdash\{\neg E\wedge P\}~{}C_{2}~{}\{Q\} by inversion

(4):

sp(EP,C1)Q\operatorname{sp}(E\wedge P,C_{1})\Rightarrow Q by induction using (2)

(5):

sp(¬EP,C2)Q\operatorname{sp}(\neg E\wedge P,C_{2})\Rightarrow Q by induction using (3)

(6):

sp(EP,C1)sp(¬EP,C2)Q\operatorname{sp}(E\wedge P,C_{1})\vee\operatorname{sp}(\neg E\wedge P,C_{2})\Rightarrow Q by logic using (4) and (5)

(7):

sp(P,ifEthenC1elseC2)Q\operatorname{sp}(P,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})\Rightarrow Q by (6) and definition of sp\operatorname{sp}

OX-Cons::
(1):

PPP\Rightarrow P^{\prime} for some PP^{\prime} by inversion

(2):

QQQ^{\prime}\Rightarrow Q for some QQ^{\prime} by inversion

(3):

{P}C{Q}\vdash\{P^{\prime}\}~{}C~{}\{Q^{\prime}\} by inversion

(4):

sp(P,C)sp(P,C)\operatorname{sp}(P,C)\Rightarrow\operatorname{sp}(P^{\prime},C) by Lemma 4 and (1)

(5):

sp(P,C)Q\operatorname{sp}(P^{\prime},C)\Rightarrow Q^{\prime} by induction using (3)

(6):

sp(P,C)Q\operatorname{sp}(P,C)\Rightarrow Q by (4), (5), and (2) ∎

Lemma 0.

{P}C{sp(P,C)}\vdash\{P\}~{}C~{}\{\operatorname{sp}(P,C)\}

Proof.

By induction on CC:

skip::
(1):

{P}skip{P}\vdash\{P\}~{}\textnormal{{skip}}~{}\{P\} by OX-Skip

(2):

Psp(P,skip)P\equiv\operatorname{sp}(P,\textnormal{{skip}}) by OX-Skip

(3):

{P}skip{sp(P,skip)}\vdash\{P\}~{}\textnormal{{skip}}~{}\{\operatorname{sp}(P,\textnormal{{skip}})\} by OX-Cons using (1) and (2)

x:-Ex\coloneq E::
(1):

Let Qv(x=E[x/v]P[x/v])Q\equiv\exists v(x=E[x/v]\wedge P[x/v]) where vfv(P)v\notin\operatorname{fv}(P).

(2):

{Q[E/x]}x:-E{Q}\vdash\{Q[E/x]\}~{}x\coloneq E~{}\{Q\} by OX-Assign

(3):

Pv(E=E[x/v]P[x/v])Q[x/E]P\Rightarrow\exists v(E=E[x/v]\wedge P[x/v])\equiv Q[x/E] by logic (witnessed by letting v=xv=x).

(4):

{P}x:-E{Q}\vdash\{P\}~{}x\coloneq E~{}\{Q\} by OX-Cons using (2) and (3)

(5):

{P}x:-E{sp(P,x:-E)}\vdash\{P\}~{}x\coloneq E~{}\{\operatorname{sp}(P,x\coloneq E)\} by (1), (4), and definition of sp\operatorname{sp}

C1;C2C_{1};C_{2}::
(1):

{P}C1{sp(P,C1)}\vdash\{P\}~{}C_{1}~{}\{\operatorname{sp}(P,C_{1})\} by induction

(2):

{sp(P,C1)}C2{sp(sp(P,C1),C2)}\vdash\{\operatorname{sp}(P,C_{1})\}~{}C_{2}~{}\{\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2})\} by induction

(3):

{P}C1;C2{sp(sp(P,C1),C2)}\vdash\{P\}~{}C_{1};C_{2}~{}\{\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2})\} by OX-Seq using (1) and (2)

(4):

sp(P,C1;C2)sp(sp(P,C1),C2)\operatorname{sp}(P,C_{1};C_{2})\equiv\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2}) by definition

(5):

{P}C1;C2{sp(P,C1;C2)}\vdash\{P\}~{}C_{1};C_{2}~{}\{\operatorname{sp}(P,C_{1};C_{2})\} by (3) and (4)

ifEthenC1elseC2\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}::
(1):

{PE}C1{sp(PE,C1)}\vdash\{P\wedge E\}~{}C_{1}~{}\{\operatorname{sp}(P\wedge E,C_{1})\} by induction

(2):

{P¬E}C2{sp(P¬E,C2)}\vdash\{P\wedge\neg E\}~{}C_{2}~{}\{\operatorname{sp}(P\wedge\neg E,C_{2})\} by induction

(3):

{PE}C1{sp(PE,C1)sp(P¬E,C2)}\vdash\{P\wedge E\}~{}C_{1}~{}\{\operatorname{sp}(P\wedge E,C_{1})\vee\operatorname{sp}(P\wedge\neg E,C_{2})\} by OX-Cons using (1)

(4):

{P¬E}C2{sp(PE,C1)sp(P¬E,C2)}\vdash\{P\wedge\neg E\}~{}C_{2}~{}\{\operatorname{sp}(P\wedge E,C_{1})\vee\operatorname{sp}(P\wedge\neg E,C_{2})\} by OX-Cons using (2)

(5):

{P}ifEthenC1elseC2{sp(PE,C1)sp(P¬E,C2)}\vdash\{P\}~{}\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}~{}\{\operatorname{sp}(P\wedge E,C_{1})\vee\operatorname{sp}(P\wedge\neg E,C_{2})\} by OX-If using (3) and (4)

(6):

{P}ifEthenC1elseC2{sp(P,ifEthenC1elseC2)}\vdash\{P\}~{}\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}\\ \{\operatorname{sp}(P,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})\} by (5) and definition of sp\operatorname{sp}

Lemma 0.

If sp(P,C)Q\operatorname{sp}(P,C)\Rightarrow Q then {P}C{Q}\vdash\{P\}~{}C~{}\{Q\}.

Proof.

Immediate from Lemma 6, applying OX-Cons. ∎

Theorem 8.

sp(P,C)Q{P}C{Q}\operatorname{sp}(P,C)\Rightarrow Q\iff\vdash\{P\}~{}C~{}\{Q\}

Proof.

\Longrightarrow: Lemma 7; \Longleftarrow: Lemma 5

Appendix D Incorrectness logic

Incorrectness logic triples are denoted [P]C[Q]\vdash[P]~{}C~{}[Q]. Deductions in incorrectness logic are defined as follows: {mathpar} \inferrule[UX-Skip] ⊢[P]  skip  [P] \inferrule[UX-Seq] ⊢[P]  C_1  [Q]
⊢[Q]  C_2  [R] ⊢[P]  C_1; C_2  [R] \inferrule[UX-Assign] ⊢[P]  x :-E  [∃v (x = E[x/v] ∧P[x/v])] \inferrule[UX-IfThen] ⊢[E ∧P]  C_1  [Q] ⊢[E ∧P]  if E then C_1 else C_2  [Q] \inferrule[UX-IfElse] ⊢[¬E ∧P]  C_2  [Q] ⊢[¬E ∧P]  if E then C_1 else C_2  [Q]
\inferrule[UX-Cons] P’ ⇒P

⊢[P’]  C  [Q’]

Q ⇒Q’ ⊢[P]  C  [Q] \inferrule[UX-Disj] ⊢[P_1]  C  [Q_1]

⊢[P_2]  C  [Q_2] ⊢[P_1 ∨P_2]  C  [Q_1 ∨Q_2]

D.1. Strongest postconditions

Lemma 0.

[P]C[sp(P,C)]\vdash[P]~{}C~{}[\operatorname{sp}(P,C)]

Proof.

By induction on CC:

skip::

By UX-Skip [P]skip[P]\vdash[P]~{}\textnormal{{skip}}~{}[P] and Psp(P,skip)P\equiv\operatorname{sp}(P,\textnormal{{skip}}) by definition.

x:-Ex\coloneq E::
(1):

[P]x:-E[v(x=E[x/v]P[x/v])]\vdash[P]~{}x\coloneq E~{}[\exists v(x=E[x/v]\wedge P[x/v])] by UX-Assign

(2):

sp(P,x:-E)v(x=E[x/v]P[x/v])\operatorname{sp}(P,x\coloneq E)\equiv\exists v(x=E[x/v]\wedge P[x/v]) by definition of sp\operatorname{sp}

(3):

[P]x:-E[sp(P,x:-E)]\vdash[P]~{}x\coloneq E~{}[sp(P,x\coloneq E)] by (1) and (2)

C1;C2C_{1};C_{2}::
(1):

[P]C1[sp(P,C1)]\vdash[P]~{}C_{1}~{}[\operatorname{sp}(P,C_{1})] by induction

(2):

[sp(P,C1)]C2[sp(sp(P,C1),C2)]\vdash[\operatorname{sp}(P,C_{1})]~{}C_{2}~{}[\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2})] by induction

(3):

[P]C1;C2[sp(sp(P,C1),C2)]\vdash[P]~{}C_{1};C_{2}~{}[\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2})] by UX-Seq using (1), (2)

(4):

sp(P,C1;C2)sp(sp(P,C1),C2)\operatorname{sp}(P,C_{1};C_{2})\equiv\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2}) by definition

(5):

[P]C1;C2[sp(P,C1;C2)]\vdash[P]~{}C_{1};C_{2}~{}[\operatorname{sp}(P,C_{1};C_{2})] by (3), (4)

ifEthenC1elseC2\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}::
(1):

[EP]C1[sp(EP,C1)]\vdash[E\wedge P]~{}C_{1}~{}[\operatorname{sp}(E\wedge P,C_{1})] by induction

(2):

[¬EP]C2[sp(¬EP,C2)]\vdash[\neg E\wedge P]~{}C_{2}~{}[\operatorname{sp}(\neg E\wedge P,C_{2})] by induction

(3):

[EP]ifEthenC1elseC2[sp(EP,C1)]\vdash[E\wedge P]~{}\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}~{}[\operatorname{sp}(E\wedge P,C_{1})] by UX-IfThen using (1)

(4):

[¬EP]ifEthenC1elseC2[sp(¬EP,C2)]\vdash[\neg E\wedge P]~{}\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}~{}[\operatorname{sp}(\neg E\wedge P,C_{2})] by UX-IfElse using (2)

(5):

[P]ifEthenC1elseC2[sp(EP,C1)sp(¬EP,C2)]\vdash[P]~{}\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}~{}[\operatorname{sp}(E\wedge P,C_{1})\vee\operatorname{sp}(\neg E\wedge P,C_{2})] by UX-Disj using (3) and (4)

(6):

sp(P,ifEthenC1elseC2)sp(EP,C1)sp(¬EP,C2)\operatorname{sp}(P,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})\equiv\operatorname{sp}(E\wedge P,C_{1})\vee\operatorname{sp}(\neg E\wedge P,C_{2}) by definition

(7):

[P]ifEthenC1elseC2[sp(P,ifEthenC1elseC2)]\vdash[P]~{}\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}\\ {}[\operatorname{sp}(P,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})] by (5) and (6) ∎

Lemma 0.

If Qsp(P,C)Q\Rightarrow\operatorname{sp}(P,C) then [P]C[Q]\vdash[P]~{}C~{}[Q].

Proof.

Immediate from Lemma 1 and UX-Cons. ∎

Lemma 0.

If [P]C[Q]\vdash[P]~{}C~{}[Q] then Qsp(P,C)Q\Rightarrow\operatorname{sp}(P,C).

Proof.

By induction on the derivation [P]C[Q]\vdash[P]~{}C~{}[Q]:

UX-Skip::
(1):

QPQ\equiv P by inversion

(2):

sp(P,skip)P\operatorname{sp}(P,\textnormal{{skip}})\equiv P by definition

(3):

Qsp(P,skip)Q\equiv\operatorname{sp}(P,\textnormal{{skip}}) by (1) and (2)

UX-Assign::
(1):

Qv(x=E[x/v]P[x/v])Q\equiv\exists v(x=E[x/v]\wedge P[x/v]) by inversion

(2):

sp(P,x:-E)v(x=E[x/v]P[x/v])\operatorname{sp}(P,x\coloneq E)\equiv\exists v(x=E[x/v]\wedge P[x/v]) by definition

(3):

Qsp(P,x:-E)Q\equiv\operatorname{sp}(P,x\coloneq E) by (1) and (2)

UX-Seq::
(1):

[P]C1[R]\vdash[P]~{}C_{1}~{}[R] for some RR by inversion

(2):

[R]C2[Q]\vdash[R]~{}C_{2}~{}[Q] by inversion

(3):

Qsp(R,C2)Q\Rightarrow\operatorname{sp}(R,C_{2}) by induction using (2)

(4):

Rsp(P,C1)R\Rightarrow\operatorname{sp}(P,C_{1}) by induction using (1)

(5):

sp(R,C2)sp(sp(P,C1),C2)\operatorname{sp}(R,C_{2})\Rightarrow\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2}) by Lemma 4 using (4)

(6):
Q\displaystyle Q sp(R,C2)\displaystyle\Rightarrow\operatorname{sp}(R,C_{2}) (3)
sp(sp(P,C1),C2)\displaystyle\Rightarrow\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2}) (5)
sp(P,C1;C2)\displaystyle\equiv\operatorname{sp}(P,C_{1};C_{2}) defn sp\operatorname{sp}
UX-IfThen::
(1):

CifEthenC1elseC2C\equiv\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2} for some E,C1,C2E,C_{1},C_{2} by inversion

(2):

PEPP\equiv E\wedge P^{\prime} for some PP^{\prime} by inversion

(3):

[EP]C1[Q]\vdash[E\wedge P^{\prime}]~{}C_{1}~{}[Q] by inversion

(4):

Qsp(EP,C1)Q\Rightarrow\operatorname{sp}(E\wedge P^{\prime},C_{1}) by induction using (3)

(5):

sp(EP,C1)sp(EP,ifEthenC1elseC2)\operatorname{sp}(E\wedge P^{\prime},C_{1})\equiv\operatorname{sp}(E\wedge P^{\prime},\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}) by Lemma 11

(6):
Q\displaystyle Q sp(EP,C1)\displaystyle\Rightarrow\operatorname{sp}(E\wedge P^{\prime},C_{1}) (4)
sp(EP,ifEthenC1elseC2)\displaystyle\equiv\operatorname{sp}(E\wedge P^{\prime},\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}) (5)
sp(P,ifEthenC1elseC2)\displaystyle\equiv\operatorname{sp}(P,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}) (2)
UX-IfElse::
(1):

CifEthenC1elseC2C\equiv\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2} for some E,C1,C2E,C_{1},C_{2} by inversion

(2):

P¬EPP\equiv\neg E\wedge P^{\prime} for some PP^{\prime} by inversion

(3):

[¬EP]C2[Q]\vdash[\neg E\wedge P^{\prime}]~{}C_{2}~{}[Q] by inversion

(4):

Qsp(¬EP,C2)Q\Rightarrow\operatorname{sp}(\neg E\wedge P^{\prime},C_{2}) by induction using (3)

(5):

sp(¬EP,C2)sp(¬EP,ifEthenC1elseC2)\operatorname{sp}(\neg E\wedge P^{\prime},C_{2})\equiv\operatorname{sp}(\neg E\wedge P^{\prime},\\ \textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}) by Lemma 11

(6):
Q\displaystyle Q sp(¬EP,C2)\displaystyle\Rightarrow\operatorname{sp}(\neg E\wedge P^{\prime},C_{2}) (4)
sp(¬EP,ifEthenC1elseC2)\displaystyle\equiv\operatorname{sp}(\neg E\wedge P^{\prime},\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}) (5)
sp(P,ifEthenC1elseC2)\displaystyle\equiv\operatorname{sp}(P,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}) (2)
UX-Cons::
(1):

[P]C[Q]\vdash[P^{\prime}]~{}C~{}[Q^{\prime}] for some P,QP^{\prime},Q^{\prime} by inversion

(2):

PPP^{\prime}\Rightarrow P by inversion

(3):

QQQ\Rightarrow Q^{\prime} by inversion

(4):

Qsp(P,C)Q^{\prime}\Rightarrow\operatorname{sp}(P^{\prime},C) by induction using (1)

(5):

sp(P,C)sp(P,C)\operatorname{sp}(P^{\prime},C)\Rightarrow\operatorname{sp}(P,C) by Lemma 4 using (2)

(6):

QQsp(P,C)sp(P,C)Q\Rightarrow Q^{\prime}\Rightarrow\operatorname{sp}(P^{\prime},C)\Rightarrow\operatorname{sp}(P,C) by (3), (4), and (5)

UX-Disj::
(1):

[P1]C[Q1]\vdash[P_{1}]~{}C~{}[Q_{1}] for some P1,Q1P_{1},Q_{1} by inversion

(2):

[P2]C[Q2]\vdash[P_{2}]~{}C~{}[Q_{2}] for some P2,Q2P_{2},Q_{2} by inversion

(3):

PP1P2P\equiv P_{1}\vee P_{2} by inversion

(4):

QQ1Q2Q\equiv Q_{1}\vee Q_{2} by inversion

(5):

Q1sp(P1,C)Q_{1}\Rightarrow\operatorname{sp}(P_{1},C) by induction using (1)

(6):

Q2sp(P2,C)Q_{2}\Rightarrow\operatorname{sp}(P_{2},C) by induction using (2)

(7):

Q1Q2sp(P1,C)sp(P2,C)Q_{1}\vee Q_{2}\Rightarrow\operatorname{sp}(P_{1},C)\vee\operatorname{sp}(P_{2},C) by logic using (5) and (6)

(8):
Q\displaystyle Q Q1Q2\displaystyle\equiv Q_{1}\vee Q_{2} (4)
sp(P1,C)sp(P2,C)\displaystyle\Rightarrow\operatorname{sp}(P_{1},C)\vee\operatorname{sp}(P_{2},C) (7)
sp(P1P2,C)\displaystyle\equiv\operatorname{sp}(P_{1}\vee P_{2},C)
sp(P,C)\displaystyle\equiv\operatorname{sp}(P,C) (3)
Theorem 4.

[P]C[Q]Qsp(P,C)\vdash[P]~{}C~{}[Q]\iff Q\Rightarrow\operatorname{sp}(P,C)

Proof.

\Longrightarrow: Lemma 3; \Longleftarrow: Lemma 2. ∎

D.2. Satisfiability

Lemma 0.

If []C[Q]\vdash[\bot]~{}C~{}[Q] then QQ\equiv\bot.

Proof.
(1):

Qsp(,C)Q\Rightarrow\operatorname{sp}(\bot,C) by Theorem 8

(2):

sp(,C)\operatorname{sp}(\bot,C)\equiv\bot by Lemma 5

(3):

QQ\Rightarrow\bot logic using (1) and (2)

(4):

QQ\equiv\bot by logic using (3) ∎

Lemma 0.

If [P]C[Q]\vdash[P]~{}C~{}[Q] and QSatFormulaQ\in\textsc{SatFormula} then PSatFormulaP\in\textsc{SatFormula}.

Proof.

Assume [P]C[Q]\vdash[P]~{}C~{}[Q], then we prove the contrapositive; that is, PSatFormulaQSatFormulaP\notin\textsc{SatFormula}\implies Q\notin\textsc{SatFormula}.

Assuming PSatFormulaP\notin\textsc{SatFormula}, since PFormulaP\in\textsc{Formula} we get PP\equiv\bot, and thus []C[Q]\vdash[\bot]~{}C~{}[Q] by assumption. Then by Lemma 5, QQ\equiv\bot thus QSatFormulaQ\notin\textsc{SatFormula}. ∎

Lemma 0.

If [P]C[Q]\vdash[P]~{}C~{}[Q] and QSatFormulaQ\in\textsc{SatFormula} then Pwp(P,C)SatFormulaP\wedge\operatorname{wp}(P,C)\in\textsc{SatFormula}.

Proof.
(1):

[P]C[Q]\vdash[P]~{}C~{}[Q] by assumption

(2):

QSatFormulaQ\in\textsc{SatFormula} by assumption

(3):

Qsp(P,C)Q\Rightarrow\operatorname{sp}(P,C) by Lemma 3 using (1)

(4):

sp(Pwp(P,C),C)Q\operatorname{sp}(P\wedge\operatorname{wp}(P,C),C)\equiv Q by Lemma 14 using (3)

(5):

[Pwp(P,C)]C[Q]\vdash[P\wedge\operatorname{wp}(P,C)]~{}C~{}[Q] by Lemma 2 using (4)

(6):

Pwp(C,Q)SatFormulaP\wedge\operatorname{wp}(C,Q)\in\textsc{SatFormula} by Lemma 6 using (2) and (5) ∎

Appendix E Exact logic

Exact logic triples are denoted by (P)C(Q)\vdash(P)~{}C~{}(Q). Deductions in exact logic are characterized by the following rules: {mathpar} \inferrule[EX-Skip] ⊢(⊤)   skip   (⊤) \inferrule[EX-Assign] x ∉fv(E’) ⊢(x = E’)   x :-E   (x = E[x/E’])
\inferrule[EX-IfThen] ⊢(P ∧E)   C_1   (Q) ⊢(P ∧E)   if E then C_1 else C_2   (Q) \inferrule[EX-Seq] ⊢(P)   C_1   (R)

⊢(R)   C_2   (Q) ⊢(P)   C_1; C_2   (Q)
\inferrule[EX-IfElse] ⊢(P ∧¬E)   C_2   (Q) ⊢(P ∧¬E)   if E then C_1 else C_2   (Q) \inferrule[EX-Frame] mod(C) ∩fv(R) = ∅

⊢(P)   C   (Q) ⊢(P ∧R)   C   (Q ∧R)
\inferrule[EX-Exists] ⊢(P)   C   (Q)

x ∉fv(C) ⊢(∃x  P)   C   (∃x  Q) \inferrule[EX-Disj] ⊢(P_1)   C   (Q_1)

⊢(P_2)   C   (Q_2) ⊢(P_1 ∨P_2)   C   (Q_1 ∨Q_2)

Note: We drop the equivalence rule, since it is immediately valid by our characterization of formulas by equivalence classes.

E.1. Strongest postconditions

Lemma 0.

(P)C(sp(P,C))\vdash(P)~{}C~{}(\operatorname{sp}(P,C))

Proof.

By induction on CC:

skip::

By EX-Skip (P)skip(P)\vdash(P)~{}\textnormal{{skip}}~{}(P) and Psp(P,skip)P\equiv\operatorname{sp}(P,\textnormal{{skip}}) by definition.

Cx:-EC\equiv x\coloneq E::
(1):

Py(x=yP)P\equiv\exists y(x=y\wedge P^{\prime}) for some PP^{\prime} since y(x=y)\exists y(x=y) is a tautology. WLOG assume xfv(P)x\notin\operatorname{fv}(P^{\prime}) (instances of xx can be replaced with yy).

(2):

(x=y)x:-E(x=E[x/y])\vdash(x=y)~{}x\coloneq E~{}(x=E[x/y]) by EX-Assign.

(3):

(x=yP)x:-E(x=E[x/y]P)\vdash(x=y\wedge P^{\prime})~{}x\coloneq E~{}(x=E[x/y]\wedge P^{\prime}) by EX-Frame and (2).

(4):

(y(x=yP))x:-E(y(x=E[x/y]P))\vdash(\exists y(x=y\wedge P^{\prime}))~{}x\coloneq E~{}(\exists y(x=E[x/y]\wedge P^{\prime})) by EX-Exists and (3).

(5):
sp(P,x:-E)\displaystyle\operatorname{sp}(P,x\coloneq E) v(x=E[x/v]P[x/v])\displaystyle\equiv\exists v(x=E[x/v]\wedge P[x/v]) defn
v(x=E[x/v](y(v=yP)))\displaystyle\equiv\exists v(x=E[x/v]\wedge(\exists y(v=y\wedge P^{\prime}))) (1)
y(x=E[x/y]P)\displaystyle\equiv\exists y(x=E[x/y]\wedge P^{\prime}) logic
(6):

(P)x:-E(sp(P,x:-E))\vdash(P)~{}x\coloneq E~{}(\operatorname{sp}(P,x\coloneq E)) by (1), (4), and (5).

C1;C2C_{1};C_{2}::
(1):

(P)C(sp(P,C1))\vdash(P)~{}C~{}(\operatorname{sp}(P,C_{1})) by induction

(2):

(sp(P,C1))C2(sp(sp(P,C1),C2))\vdash(\operatorname{sp}(P,C_{1}))~{}C_{2}~{}(\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2})) by induction

(3):

(P)C1;C2(sp(sp(P,C1),C2))\vdash(P)~{}C_{1};C_{2}~{}(\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2})) by EX-Seq using (1), (2)

(4):

sp(sp(P,C1),C2)sp(P,C1;C2)\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2})\equiv\operatorname{sp}(P,C_{1};C_{2}) by definition

(5):

(P)C1;C2(sp(P,C1;C2))\vdash(P)~{}C_{1};C_{2}~{}(\operatorname{sp}(P,C_{1};C_{2})) by (3) and (4)

ifEthenC1elseC2\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}::
(1):

(PE)C1(sp(PE,C1))\vdash(P\wedge E)~{}C_{1}~{}(\operatorname{sp}(P\wedge E,C_{1})) by induction

(2):

(P¬E)C2(sp(P¬E,C2))\vdash(P\wedge\neg E)~{}C_{2}~{}(\operatorname{sp}(P\wedge\neg E,C_{2})) by induction

(3):

(PE)ifEthenC1elseC2(sp(PE,C1))\vdash(P\wedge E)~{}\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}~{}(\operatorname{sp}(P\wedge E,C_{1})) by EX-IfThen using (1)

(4):

(P¬E)ifEthenC1elseC2(sp(P¬E,C2))\vdash(P\wedge\neg E)~{}\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}~{}(\operatorname{sp}(P\wedge\neg E,C_{2})) by EX-IfElse using (2)

(5):

((PE)(P¬E))ifEthenC1elseC2(sp(PE,C1)sp(P¬E,C2))\vdash((P\wedge E)\vee(P\wedge\neg E))~{}\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}\\ (\operatorname{sp}(P\wedge E,C_{1})\vee\operatorname{sp}(P\wedge\neg E,C_{2})) by EX-Disj using (3), (4)

(6):

(PE)(P¬E)P(P\wedge E)\vee(P\wedge\neg E)\equiv P by logic

(7):

sp(PE,C1)sp(P¬E,C2)sp(P,ifEthenC1elseC2)\operatorname{sp}(P\wedge E,C_{1})\vee\operatorname{sp}(P\wedge\neg E,C_{2})\equiv\\ \operatorname{sp}(P,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}) by definition

(8):

(P)ifEthenC1elseC2(sp(P,ifEthenC1elseC2))\vdash(P)~{}\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}\\ (\operatorname{sp}(P,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2})) by (5), (6), and (7) ∎

Lemma 0.

If (P)C(Q)\vdash(P)~{}C~{}(Q) then Qsp(P,C)Q\equiv\operatorname{sp}(P,C).

Proof.

By induction on the derivation (P)C(Q)\vdash(P)~{}C~{}(Q):

EX-Skip::

By inversion CskipC\equiv\textnormal{{skip}} and QPQ\equiv P, and by definition sp(P,skip)P\operatorname{sp}(P,\textnormal{{skip}})\equiv P. Thus sp(P,C)sp(P,skip)PQ\operatorname{sp}(P,C)\equiv\operatorname{sp}(P,\textnormal{{skip}})\\ \equiv P\equiv Q.

EX-Assign::
(1):

Cx:-EC\equiv x\coloneq E for some x,Ex,E by inversion

(2):

Px=EP\equiv x=E^{\prime} for some EE^{\prime} by inversion

(3):

Qx=E[x/E]Q\equiv x=E[x/E^{\prime}] by inversion

(4):

xfv(E)x\notin\operatorname{fv}(E^{\prime}) by inversion

(5):
sp(P,C)\displaystyle\operatorname{sp}(P,C)
sp(x=E,x:-E)\displaystyle\quad\equiv\operatorname{sp}(x=E^{\prime},x\coloneq E) (1), (2)
v(x=E[x/v](x=E)[x/v])\displaystyle\quad\equiv\exists v(x=E[x/v]\wedge(x=E^{\prime})[x/v]) defn sp\operatorname{sp}
v(x=E[x/v]v=E)\displaystyle\quad\equiv\exists v(x=E[x/v]\wedge v=E^{\prime}) (4)
v(x=E[x/E]v=E)\displaystyle\quad\equiv\exists v(x=E[x/E^{\prime}]\wedge v=E^{\prime}) logic
x=E[x/E]vv=E\displaystyle\quad\equiv x=E[x/E^{\prime}]\wedge\exists v\,v=E^{\prime} (4)
x=E[x/E]\displaystyle\quad\equiv x=E[x/E^{\prime}] logic
Q\displaystyle\quad\equiv Q (3)
EX-IfThen::
(1):

CifEthenC1elseC2C\equiv\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2} by inversion

(2):

PPEP\equiv P^{\prime}\wedge E for some PP^{\prime} by inversion

(3):

(PE)C1(Q)\vdash(P^{\prime}\wedge E)~{}C_{1}~{}(Q) by inversion

(4):

Qsp(PE,C1)Q\equiv\operatorname{sp}(P^{\prime}\wedge E,C_{1}) by induction using (3)

(5):
sp(P,C)\displaystyle\operatorname{sp}(P,C)
sp(PE,ifEthenC1elseC2)\displaystyle\quad\equiv\operatorname{sp}(P^{\prime}\wedge E,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}) (1), (2)
sp(PE,C1)\displaystyle\quad\equiv\operatorname{sp}(P^{\prime}\wedge E,C_{1})
Q\displaystyle\quad\equiv Q (4)
EX-IfElse::
(1):

CifEthenC1elseC2C\equiv\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2} by inversion

(2):

PP¬EP\equiv P^{\prime}\wedge\neg E for some PP^{\prime} by inversion

(3):

(P¬E)C2(Q)\vdash(P^{\prime}\wedge\neg E)~{}C_{2}~{}(Q) by inversion

(4):

Qsp(P¬E,C2)Q\equiv\operatorname{sp}(P^{\prime}\wedge\neg E,C_{2}) by induction using (3)

(5):
sp(P,C)\displaystyle\operatorname{sp}(P,C)
sp(P¬E,ifEthenC1elseC2)\displaystyle\quad\equiv\operatorname{sp}(P^{\prime}\wedge\neg E,\textnormal{{if}}\ E\ \textnormal{{then}}\ C_{1}\ \textnormal{{else}}\ C_{2}) (1), (2)
sp(P¬E,C2)\displaystyle\quad\equiv\operatorname{sp}(P^{\prime}\wedge\neg E,C_{2})
Q\displaystyle\quad\equiv Q (4)
EX-Seq::
(1):

CC1;C2C\equiv C_{1};C_{2} for some C1,C2C_{1},C_{2} by inversion

(2):

(P)C1(R)\vdash(P)~{}C_{1}~{}(R) for some RR by inversion

(3):

(R)C2(Q)\vdash(R)~{}C_{2}~{}(Q) by inversion

(4):

Rsp(P,C1)R\equiv\operatorname{sp}(P,C_{1}) by induction using (2)

(5):

Qsp(R,C2)Q\equiv\operatorname{sp}(R,C_{2}) by induction using (3)

(6):
sp(P,C)\displaystyle\operatorname{sp}(P,C) sp(P,C1;C2)\displaystyle\equiv\operatorname{sp}(P,C_{1};C_{2}) (1)
sp(sp(P,C1),C2)\displaystyle\equiv\operatorname{sp}(\operatorname{sp}(P,C_{1}),C_{2}) defn sp\operatorname{sp}
sp(R,C2)\displaystyle\equiv\operatorname{sp}(R,C_{2}) (4)
Q\displaystyle\equiv Q (5)
EX-Exists::
(1):

PxPP\equiv\exists x\,P^{\prime} for some PP^{\prime} by inversion

(2):

QxQQ\equiv\exists x\,Q^{\prime} for some QQ^{\prime} by inversion

(3):

(P)C(Q)\vdash(P^{\prime})~{}C~{}(Q^{\prime}) by inversion

(4):

xfv(C)x\notin\operatorname{fv}(C) by inversion

(5):

Qsp(P,C)Q^{\prime}\equiv\operatorname{sp}(P^{\prime},C) by induction using (3)

(6):
sp(P,C)\displaystyle\operatorname{sp}(P,C) sp(xP,C)\displaystyle\equiv\operatorname{sp}(\exists x\,P^{\prime},C) (2)
xsp(P,C)\displaystyle\equiv\exists x\,\operatorname{sp}(P^{\prime},C)
xQ\displaystyle\equiv\exists x\,Q^{\prime} (5)
Q\displaystyle\equiv Q (2)
EX-Disj::
(1):

PP1P2P\equiv P_{1}\vee P_{2} for some P1,P2P_{1},P_{2} by inversion

(2):

QQ1Q2Q\equiv Q_{1}\vee Q_{2} for some Q1,Q2Q_{1},Q_{2} by inversion

(3):

(P1)C(Q1)\vdash(P_{1})~{}C~{}(Q_{1}) by inversion

(4):

(P2)C(Q2)\vdash(P_{2})~{}C~{}(Q_{2}) by inversion

(5):

Q1sp(P1,C)Q_{1}\equiv\operatorname{sp}(P_{1},C) by induction using (3)

(6):

Q2sp(P2,C)Q_{2}\equiv\operatorname{sp}(P_{2},C) by induction using (4)

(7):
sp(P,C)\displaystyle\operatorname{sp}(P,C) sp(P1P2,C)\displaystyle\equiv\operatorname{sp}(P_{1}\vee P_{2},C) (1)
sp(P1,C)sp(P2,C)\displaystyle\equiv\operatorname{sp}(P_{1},C)\vee\operatorname{sp}(P_{2},C)
Q1Q2\displaystyle\equiv Q_{1}\vee Q_{2} (5), (6)
Q\displaystyle\equiv Q (2)
EX-Frame::
(1):

PPRP\equiv P^{\prime}\wedge R for some P,RP^{\prime},R by inversion

(2):

QQRQ\equiv Q^{\prime}\wedge R for some QQ^{\prime} by inversion

(3):

mod(C)fv(R)=\operatorname{mod}(C)\cap\operatorname{fv}(R)=\emptyset by inversion

(4):

(P)C(Q)\vdash(P^{\prime})~{}C~{}(Q^{\prime}) by inversion

(5):

sp(P,C)Q\operatorname{sp}(P^{\prime},C)\equiv Q by induction using (4)

(6):

sp(PR,C)Rsp(P,C)\operatorname{sp}(P^{\prime}\wedge R,C)\equiv R\wedge\operatorname{sp}(P^{\prime},C) by Lemma 10 using (3)

(7):
sp(P,C)\displaystyle\operatorname{sp}(P,C) sp(PR,C)\displaystyle\equiv\operatorname{sp}(P^{\prime}\wedge R,C) (1)
Rsp(P,C)\displaystyle\equiv R\wedge\operatorname{sp}(P^{\prime},C) (6)
RQ\displaystyle\equiv R\wedge Q^{\prime} (5)
Q\displaystyle\equiv Q (2)
Theorem 3.

(P)C(Q)Qsp(P,C)\vdash(P)~{}C~{}(Q)\iff Q\equiv\operatorname{sp}(P,C)

Proof.

\Longrightarrow: Lemma 2; \Longleftarrow: Lemma 1

E.2. Gradual exact logic

Valid triples in gradual exact logic are denoted ~(P~)C(Q~)\mathrel{\widetilde{\vdash}}(\widetilde{P})~{}C~{}(\widetilde{Q}).

Definition 0.

The concretization γ:F~ormula𝒫(Formula)\gamma:\widetilde{\textsc{F}}\textsc{ormula}\to\\ \mathcal{P}(\textsc{Formula}) maps a gradual formula to the set of all formulas it can represent:

γ(P)\displaystyle\gamma(P) :-{P}\displaystyle\coloneq\{P\}
γ(?P)\displaystyle\gamma(\textnormal{{?}}\wedge P) :-{PSatFormulaPP}\displaystyle\coloneq\{P^{\prime}\in\textsc{SatFormula}\mid P^{\prime}\Rightarrow P\}
Definition 0.

Deductions in gradual exact logic directly are lifted deductions in exact logic:

~(P~)C(Q~)def(P)C(Q)\displaystyle\mathrel{\widetilde{\vdash}}(\widetilde{P})~{}C~{}(\widetilde{Q})\mathrel{\stackrel{{\scriptstyle\text{def}}}{{\iff}}}\vdash(P)~{}C~{}(Q)
for some Pγ(P~)P\in\gamma(\widetilde{P}) and Qγ(Q~)Q\in\gamma(\widetilde{Q})
Theorem 6.

For PSatFormulaP\in\textsc{SatFormula},

~(P)C(?Q){P}C{Q}.\mathrel{\widetilde{\vdash}}(P)~{}C~{}(\textnormal{{?}}\wedge Q)\iff\vdash\{P\}~{}C~{}\{Q\}.

That is, except for the vacuous case where PP\equiv\bot, gradualizing the postcondition exactly characterizes deductions in Hoare logic.

Proof.

\Longrightarrow:

(1):

~(P)C(?Q)\mathrel{\widetilde{\vdash}}(P)~{}C~{}(\textnormal{{?}}\wedge Q) by assumptino

(2):

(P)C(Q)\vdash(P)~{}C~{}(Q^{\prime}) for some Qγ(?Q)Q^{\prime}\in\gamma(\textnormal{{?}}\wedge Q) by (1)

(3):

sp(P,C)Q\operatorname{sp}(P,C)\equiv Q^{\prime} by Lemma 2 using (2)

(4):

QQQ^{\prime}\Rightarrow Q by definition of γ\gamma and (2)

(5):

sp(P,C)Q\operatorname{sp}(P,C)\Rightarrow Q by (3) and (4)

(6):

{P}C{Q}\vdash\{P\}~{}C~{}\{Q\} by Lemma 7 using (5)

\Longleftarrow:

(1):

{P}C{Q}\vdash\{P\}~{}C~{}\{Q\} by assumption

(2):

PSatFormulaP\in\textsc{SatFormula} by assumption

(3):

sp(P,C)Q\operatorname{sp}(P,C)\Rightarrow Q by Lemma 5 using (1)

(4):

sp(P,C)SatFormula\operatorname{sp}(P,C)\in\textsc{SatFormula} by Lemma 7 using (2)

(5):

sp(P,C)γ(?Q)\operatorname{sp}(P,C)\in\gamma(\textnormal{{?}}\wedge Q) by definition of γ\gamma using (3) and (4)

(6):

(P)C(sp(P,C))\vdash(P)~{}C~{}(\operatorname{sp}(P,C)) by Lemma 1

(7):

~(P)C(?Q)\mathrel{\widetilde{\vdash}}(P)~{}C~{}(\textnormal{{?}}\wedge Q) by definition using (5) and (6) ∎

Theorem 7.

If QSatFormulaQ\in\textsc{SatFormula},

~(?P)C(Q)[P]C[Q]\mathrel{\widetilde{\vdash}}(\textnormal{{?}}\wedge P)~{}C~{}(Q)\iff\vdash[P]~{}C~{}[Q]

That is, except in the vacuous case where QQ\equiv\bot, gradualizing the precondition exactly characterizes deductions in incorrectness logic.

Proof.

\Longrightarrow:

(1):

~(?P)C(Q)\mathrel{\widetilde{\vdash}}(\textnormal{{?}}\wedge P)~{}C~{}(Q) by assumption

(2):

(P)C(Q)\vdash(P^{\prime})~{}C~{}(Q) for some Pγ(?P)P^{\prime}\in\gamma(\textnormal{{?}}\wedge P) by definition using (1)

(3):

PPP^{\prime}\Rightarrow P by definition of γ\gamma using (2)

(4):

Qsp(P,C)Q\equiv\operatorname{sp}(P^{\prime},C) by Lemma 2 using (2)

(5):

sp(P,C)sp(P,C)\operatorname{sp}(P^{\prime},C)\Rightarrow\operatorname{sp}(P,C) by Lemma 4 using (3)

(6):

Qsp(P,C)Q\Rightarrow\operatorname{sp}(P,C) by (4) and (5)

(7):

[P]C[Q]\vdash[P]~{}C~{}[Q] by Theorem 8 using (6)

\Longleftarrow:

(1):

[P]C[Q]\vdash[P]~{}C~{}[Q] by assumption

(2):

QSatFormulaQ\in\textsc{SatFormula} by assumption

(3):

Pwp(C,Q)SatFormulaP\wedge\operatorname{wp}(C,Q)\in\textsc{SatFormula} by Lemma 7 using (1) and (2)

(4):

Pwp(C,Q)PP\wedge\operatorname{wp}(C,Q)\Rightarrow P by logic

(5):

Pwp(C,Q)γ(?P)P\wedge\operatorname{wp}(C,Q)\in\gamma(\textnormal{{?}}\wedge P) by definition of γ\gamma using (3) and (4)

(6):

Qsp(P,C)Q\Rightarrow\operatorname{sp}(P,C) by Theorem 8 using (1)

(7):

sp(Pwp(C,Q),C)Q\operatorname{sp}(P\wedge\operatorname{wp}(C,Q),C)\equiv Q by Lemma 14 using (6)

(8):

(Pwp(C,Q))C(Q)\vdash(P\wedge\operatorname{wp}(C,Q))~{}C~{}(Q) by Theorem 3 using (7)

(9):

~(?P)C(Q)\mathrel{\widetilde{\vdash}}(\textnormal{{?}}\wedge P)~{}C~{}(Q) by (5) and (8) ∎