13.07.2015 Views

Nested Hoare Triples and Frame Rules for Higher-order Store

Nested Hoare Triples and Frame Rules for Higher-order Store

Nested Hoare Triples and Frame Rules for Higher-order Store

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Nested</strong> <strong>Hoare</strong> <strong>Triples</strong> <strong>and</strong> <strong>Frame</strong> <strong>Rules</strong> <strong>for</strong><strong>Higher</strong>-<strong>order</strong> <strong>Store</strong>Jan Schwinghammer 1⋆ , Lars Birkedal 2 , Bernhard Reus 3 , <strong>and</strong> Hongseok Yang 41 Saarl<strong>and</strong> University, Germany3 University of Sussex, Brighton, UK2 IT University of Copenhagen, Denmark4 Queen Mary University of London, UKAbstract. Separation logic is a <strong>Hoare</strong>-style logic <strong>for</strong> reasoning aboutprograms with heap-allocated mutable data structures. As a step towardextending separation logic to high-level languages with ML-style general(higher-<strong>order</strong>) storage, we investigate the compatibility of nested <strong>Hoare</strong>triples with several variations of higher-<strong>order</strong> frame rules.The interaction of nested triples <strong>and</strong> frame rules can be subtle, <strong>and</strong> theinclusion of certain frame rules is in fact unsound. A particular combinationof rules can be shown consistent by means of a Kripke model whereworlds live in a recursively defined ultrametric space. The resulting logicallows us to elegantly prove programs involving stored code. In particular,it leads to natural specifications <strong>and</strong> proofs of invariants required<strong>for</strong> dealing with recursion through the store.Keywords. <strong>Higher</strong>-<strong>order</strong> store, <strong>Hoare</strong> logic, separation logic, semantics.1 IntroductionMany programming languages permit not only the storage of first-<strong>order</strong> data, butalso <strong>for</strong>ms of higher-<strong>order</strong> store. Examples are code pointers in C, <strong>and</strong> ML-likegeneral references. It is there<strong>for</strong>e important to have modular reasoning principles<strong>for</strong> these language features. Separation logic is an effective <strong>for</strong>malism <strong>for</strong> modularreasoning about pointer programs, in low-level C-like programming languages<strong>and</strong>, more recently, also in higher-level languages [13, 6, 9]. However, its assertionsare usually limited to talk about first-<strong>order</strong> data.In previous work, we have begun the study of separation logic <strong>for</strong> languageswith higher-<strong>order</strong> store [2, 12]. A challenge in this research is the combination ofproof rules from separation logic <strong>for</strong> <strong>and</strong> modular reasoning, <strong>and</strong> proof rules <strong>for</strong>code stored on the heap. Ideally, a program logic <strong>for</strong> higher-<strong>order</strong> store providessufficiently expressive proof rules that, e.g., can deal with recursion through thestore, <strong>and</strong> at the same time interact well with (higher-<strong>order</strong>) frame rules, whichenable modular program verification.Our earlier work shows that separation logic is consistent with higher-<strong>order</strong>store. However, the <strong>for</strong>mulation of [2, 12] has a shortcoming: code is treated like⋆ Corresponding author. Email:jan@ps.uni-sb.de. Address: Programming SystemsLab, Universität des Saarl<strong>and</strong>es, Campus E1 3, 66123 Saarbrücken, Germany.


any other data in that assertions can only mention concrete comm<strong>and</strong>s. Formodular reasoning, however, it is clearly desirable to abstract from particularcode <strong>and</strong> instead (partially) specify its behaviour. For example, when verifyingmutually recursive procedures on the heap, one would like to consider each procedurein isolation, relying on properties but not the implementations of theothers. The recursion rule in [2, 12] does not achieve this. A second, <strong>and</strong> lessobvious consequence of lacking behavioural specifications <strong>for</strong> code in assertionsis that one cannot take full advantage of the frame rules of separation logic. Forinstance, the language in [2] can simulate higher-<strong>order</strong> procedures by passingarguments through the heap, but the available (higher-<strong>order</strong>) frame rules arenot useful here because an appropriate specification <strong>for</strong> this encoding is missing.In this article, we address these shortcomings by investigating a program logicin which stored code can be specified using <strong>Hoare</strong> triples, i.e., an assertion languagewith nested triples. This is an obvious idea, but the combination of nestedtriples <strong>and</strong> frame rules turns out to be tricky: the most natural combinationturns out to be unsound.The main technical contributions of this paper are there<strong>for</strong>e: (1) the observationthat certain “deep” frame rules can be unsound, (2) the suggestion of a“good” combination of nested <strong>Hoare</strong> triples <strong>and</strong> frame rules, <strong>and</strong> (3) the verificationof those by means of an elegant Kripke model, where the worlds arethemselves world-dependent sets of heaps. The worlds <strong>for</strong>m a complete metricspace <strong>and</strong> the (denotation of the) tensor ⊗, needed to generically express higher<strong>order</strong>frame rules, is contractive; as a consequence, our logic permits recursivelydefined assertions.After introducing the syntax of language <strong>and</strong> assertions in Section 2 we discusssome unsound combinations of rules in Section 3, which also contains thesuggested set of rules <strong>for</strong> our logic. The soundness of the logic is then shown inSection 4.2 Syntax of Programs <strong>and</strong> AssertionsWe consider a simple imperative programming language extended with operations<strong>for</strong> stored code <strong>and</strong> heap manipulation. The syntax of the language isshown in Fig. 1. The expressions in the language are integer expressions, variables,<strong>and</strong> the quote expression ‘C’ <strong>for</strong> representing an unevaluated comm<strong>and</strong>C. The integer or code value denoted by expression e 1 can be stored in a heapcell e 0 using [e 0 ]:=e 1 , <strong>and</strong> this stored value can later be looked up <strong>and</strong> boundto the (immutable) variable y by let y=[e 0 ] in D. In case the value stored in celle 0 is code ‘C’, we can run (or “evaluate”) this code by executing eval [e 0 ]. Ourlanguage also provides constructs <strong>for</strong> allocating <strong>and</strong> disposing heap cells such ase 0 above. We point out that, as in ML, all variables x, y, z in our language areimmutable, so that once they are bound to a value, their values do not change.This property of the language lets us avoid side conditions on variables whenstudying frame rules. Finally, we do not include while loops in our language,since they can be expressed by stored code (using L<strong>and</strong>in’s knot).


d ∈ Exp ::= 0 | −1 | 1 | . . . | d 1+d 2 | . . . | x integer expressions, variable| ‘C’ quote (comm<strong>and</strong> as expression)C ∈ Com ::= [e 1]:=e 2 | let y=[e] in C | eval [e] assignment, lookup, unquote| let x=new (e 1, . . . , e n) in C | free e allocation, disposal| skip | C 1;C 2 | if (e 1=e 2) then C 1 else C 2 no op, sequencing, conditionalP, Q ∈ Assn ::= false | true | P ∨ Q | P ∧ Q | P ⇒ Q intuitionistic-logic connectives| ∀x.P | ∃x.P | int(e) | e 1 = e 2 | e 1 ≤ e 2 quantifiers, atomic <strong>for</strong>mulas| e 1 ↦→ e 2 | emp | P ∗ Q separating connectives| {P }e{Q} | P ⊗ Q | . . . <strong>Hoare</strong> triple, invariant extensionFig. 1. Syntax of expressions, comm<strong>and</strong>s <strong>and</strong> assertionsP ◦ R def= (P ⊗ R) ∗ R{P }e{Q} ⊗R ⇔{P ◦ R}e{Q ◦ R} (κx.P ) ⊗ R ⇔ κx.(P ⊗ R) (κ ∈ {∀, ∃}, x /∈ fv(R))(P ⊗ R) ⊗ R ′ ⇔ P ⊗ (R ◦ R ′ ) (P ⊕ Q) ⊗ R ⇔ (P ⊗ R) ⊕ (Q ⊗ R) (⊕ ∈ {⇒, ∧, ∨, ∗})P ⊗ R ⇔ P (P is one of true, false, emp, e=e ′ , e↦→e ′ <strong>and</strong> int(e))Fig. 2. Axioms <strong>for</strong> distributing − ⊗ ROur assertion language is st<strong>and</strong>ard first-<strong>order</strong> intuitionistic logic, extendedwith separating connectives emp, ∗, the points-to predicate ↦→ [13], <strong>and</strong> withrecursively defined assertions. The syntax of assertions appears in Fig. 1. Eachassertion describes a property of states, which consist of an immutable stack <strong>and</strong>a mutable heap. Formula emp means that the heap component of the state isempty, <strong>and</strong> P ∗ Q means that the heap component can be split into two, onesatisfying P <strong>and</strong> the other satisfying Q, both evaluated w.r.t. the same stack.The points-to predicate e 0 ↦→ e 1 states that the heap component consists of onlyone cell e 0 whose contents is (some approximation of) e 1 .One interesting aspect of our assertion language is that it includes <strong>Hoare</strong>triples {P }e{Q} <strong>and</strong> invariant extensions P ⊗ Q; previous work [4, 2] does nottreat them as assertions, but as so-called specifications, which <strong>for</strong>m a differentsyntactic category. Intuitively, {P }e{Q} means that e denotes code satisfying{P } {Q}, <strong>and</strong> P ⊗ Q denotes a modification of P where all the pre- <strong>and</strong>post-conditions of triples inside P are ∗-extended with Q. For instance, theassertion (∃k. (1 ↦→ k) ∧{emp}k{emp}) ⊗ (2↦→0) is equivalent to (∃k. (1 ↦→ k) ∧{2↦→0}k{2↦→0}). This assertion says that cell 1 is the only cell in the heap <strong>and</strong>it stores code k that satisfies the triple {2↦→0} {2↦→0}. This intuition of the ⊗operator can also be seen in the set of axioms in Fig. 2, which let us distribute⊗ through all the constructs of the assertion language.Note that since triples are assertions, they can appear in pre- <strong>and</strong> postconditionsof triples. This nested use of triples is useful in reasoning, becauseit allows one to specify stored code behaviourally, in terms of properties thatit satisfies. Another important consequence of having these new constructs as


assertions is that they allow us to study proof rules <strong>for</strong> exploiting locality ofstored code systematically, as we will describe shortly.The last case . . . in Fig. 1 represents pre-defined assertions, including recursivelydefined ones. In particular, it contains all recursively defined assertionsof the <strong>for</strong>m R = P ⊗ R, where R does not appear in P . These assertions arealways well-defined (because ⊗ is “contractive” in its second argument, as shownin Lemma 4), <strong>and</strong> they let us reason about self-applying stored code, withoutusing specialized rules [2]. We will say more about the use of recursively definedpredicates <strong>and</strong> their existence in Sections 3 <strong>and</strong> 4. 1We shall make use of two abbreviations. The first is P ◦ R, which st<strong>and</strong>s <strong>for</strong>(P ⊗ R) ∗ R (already used in Fig. 2). This abbreviation is often used to add aninvariant R to a <strong>Hoare</strong> triple{P }e{Q}, so as to obtain{P ◦ R}e{Q ◦ R}. We use ◦instead of ∗ here to extend not only P by R but also ensure, via ⊗, that all <strong>Hoare</strong>triples nested inside P preserve R as an invariant. The ◦ operator has been introducedin [11], where it is credited to Paul-André Melliès <strong>and</strong> Nicolas Tabareau.defThe second abbreviation is <strong>for</strong> the “↦→” operator: e 1 ↦→ P [e 2 ] = e 1 ↦→ e 2 ∧ P [e 2 ]def<strong>and</strong> e 1 ↦→ P [ ] = ∃x. e 1 ↦→ P [x]. Here x is a fresh (logic) variable <strong>and</strong> P [·] is anassertion with an expression hole, such as{Q} · {R}, int(·), · = e or · ≤ e.3 Proof <strong>Rules</strong> <strong>for</strong> <strong>Higher</strong>-<strong>order</strong> <strong>Store</strong>In our <strong>for</strong>mal setting, reasoning about programs is done by deriving the judgementΓ ⊢ P , where P is an assertion expressing properties of programs <strong>and</strong> Γis a list of variables containing all the free variables in P . For instance, to provethat comm<strong>and</strong> C stores at cell 1 the code that initializes cell 10 to 0, 2 we needto derive Γ ⊢ {1 ↦→ }‘C’{1 ↦→ {10 ↦→ } {10 ↦→ 0}}. In this section, we describeinference rules <strong>and</strong> axioms <strong>for</strong> assertions that let one efficiently reason aboutprograms. We focus on those related to higher-<strong>order</strong> store.St<strong>and</strong>ard proof rules The proof rules include the st<strong>and</strong>ard proof rules <strong>for</strong>intuitionistic logic <strong>and</strong> the logic of bunched implications [7] (not repeated here).Moreover, the proof rules include variations of st<strong>and</strong>ard separation logic proofrules, see Fig. 3. 3 The figure neither includes the rule <strong>for</strong> executing stored codewith eval [e] nor the frame rule <strong>for</strong> adding invariants to triples; the reason <strong>for</strong>this omission is that these two rules raise nontrivial issues in the presence ofhigher-<strong>order</strong> store <strong>and</strong> nested triples, as we will now discuss.1 More generally, we may need to solve mutually recursive assertions 〈R 1, . . . , R n〉 =〈P 1 ⊗(R 1 ∗. . .∗R n), . . . , P n ⊗(R 1 ∗. . .∗R n)〉 in <strong>order</strong> to deal with mutually recursivestored procedures. For brevity we omit <strong>for</strong>mal syntax <strong>for</strong> such; see Theorem 11 <strong>for</strong>the semantic existence proof.2 One concrete example of such C is [1]:=‘[10]:=0’.3 The Update, Free <strong>and</strong> Skip rules in the figure are not the usual small axioms inseparation logic, since they contain assertion P describing the unchanged part. Sincewe have the st<strong>and</strong>ard frame rule <strong>for</strong> ∗, we could have used small axioms instead here.But we chose not to do this, because the current non-small axioms make it easier tofollow our discussions on frame rules <strong>and</strong> higher-<strong>order</strong> store in the next subsection.


DerefΓ, x ⊢ {P ∗ e ↦→ x}‘C’{Q}Update(x∉fv(e, Q))Γ ⊢ {∃x.P ∗ e ↦→ x}‘let x=[e] in C’{Q}NewΓ, x ⊢ {P ∗ x ↦→ e}‘C’{Q}Free(x∉fv(P, e, Q))Γ ⊢ {P }‘let x=new e in C’{Q}SkipΓ ⊢{P }‘skip’{P }IfΓ ⊢{P ∧ e 0=e 1}‘C’{Q}SeqΓ ⊢{P }‘C’{R}Γ ⊢{P ∧ e 0≠e 1}‘D’{Q}Γ ⊢{P }‘if (e 0=e 1) then C else D’{Q}Γ ⊢ {e ↦→ ∗ P }‘[e] := e 0’{e ↦→ e 0 ∗ P }Γ ⊢{e ↦→ ∗ P }‘free(e)’{P }Γ ⊢{R}‘D’{Q}Γ ⊢{P }‘C; D’{Q}Fig. 3. Proof rules from separation logicConseqΓ ⊢ P ′ ⇒ P Γ ⊢ Q ⇒ Q ′Γ ⊢ {P }e{Q} ⇒{P ′ }e{Q ′ }<strong>Frame</strong> rule <strong>for</strong> higher-<strong>order</strong> store The frame rule is the most importantrule in separation logic, <strong>and</strong> it <strong>for</strong>malizes the intuition of local reasoning, whereproofs focus on the footprints of the programs we verify. Developing a similarrule in our setting is challenging, because nested triples allow <strong>for</strong> several choicesregarding the shape of the rule. Moreover, the recursive nature of the higher<strong>order</strong>store muddies the water <strong>and</strong> it is difficult to see which choices actuallymake sense (i.e., do not lead to inconsistency).To see this problem more clearly, consider the rules below:Γ ⊢{P }e{Q}Γ ⊢{P R}e{Q R} <strong>and</strong>Γ ⊢{P }e{Q} ⇒{P R}e{Q R}<strong>for</strong> ∈ {∗, ◦}.Note that we have four choices, depending on whether we use = ∗ or = ◦<strong>and</strong> on whether we have an inference rule or an axiom. If we choose ∗ <strong>for</strong> , weobtain shallow frame rules that add R to the outermost triple{P }e{Q} only; theydo not add R in nested triples appearing in pre-condition P <strong>and</strong> post-conditionQ. On the other h<strong>and</strong>, if we choose ◦ <strong>for</strong> , since (A ◦ R) = (A ⊗ R ∗ R), weobtain deep frame rules that add the invariant R not just to the outermost triplebut also to all the nested triples in P <strong>and</strong> Q.The distinction between inference rule <strong>and</strong> axiom has some bearing on wherethe frame rule can be applied. With the axiom version, we can apply the framerule not just to valid triples, but also to nested triples appearing in pre- or postconditions.With the inference rule version, however, we cannot add invariantsto (or remove from) nested triples.Ideally, we would like to have the axiom versions of the frame rules <strong>for</strong> both∗ <strong>and</strong> ◦. Un<strong>for</strong>tunately, this is not possible <strong>for</strong> ◦. Adding the axiom version <strong>for</strong>◦ makes our logic unsound. The source of the problem is that with the axiomversion <strong>for</strong> ◦, one can add invariants selectively to some, but not necessarily all,nested triples. This flexibility can be abused to derive incorrect conclusions.


Concretely, with the axiom version <strong>for</strong> ◦ we can make the following derivation:Γ ⊢{P ◦ S}e{Q ◦ S}Γ ⊢{P }e{Q} ⊗ S ⊗-Dist Γ ⊢{P }e{Q} ⇒{P ◦ R}e{Q ◦ R} <strong>Frame</strong>Γ ⊢{P }e{Q} ⊗ S ⇒{P ◦ R}e{Q ◦ R} ⊗ S ⊗-MonoΓ ⊢{P ◦ R}e{Q ◦ R} ⊗ SΓ ⊢{(P ◦ R) ◦ S}e{(Q ◦ R) ◦ S} ⊗-DistModusPonHere we use the distribution axioms <strong>for</strong> ⊗ in Fig. 2 <strong>and</strong> the monotonicity of− ⊗ R. This derivation means that when adding R to nested triples, we can skipthe triples in the S part of the pre- <strong>and</strong> post-conditions of{P ◦ S}e{Q ◦ S}. Thisflexibility leads to the unsoundness:Proposition 1. Adding the axiom version of the frame rule <strong>for</strong> ◦ renders ourlogic unsound.Proof. Let R be the predicate defined by R = (3 ↦→ {1↦→ } {1↦→ }) ⊗ R. Then,we can derive the triple:(†)k ⊢ {2 ↦→ {1↦→ } {1↦→ } ◦ R}k{2 ↦→ ◦ R}k ⊢ {`2 ↦→ {1↦→ } {1↦→ } ◦ 1 ↦→ ´ ◦ R}k{`2 ↦→ ◦ 1 ↦→ ´ ◦ R}k ⊢ {2 ↦→ ‘free(−1)’ ∗ 1 ↦→ ∗ R}k{2 ↦→ ∗ 1 ↦→ ∗ 3 ↦→ {1↦→ ∗ R} {1↦→ ∗ R}}Here the first step uses the derivation above <strong>for</strong> adding invariants selectively,<strong>and</strong> the last step uses the Consequence axiom with the below two implications:2 ↦→ {1↦→ } {1↦→ } ◦ 1↦→ ◦ R ⇐⇒ 2 ↦→ {1↦→ ∗ 1↦→ ∗ R} {1↦→ ∗ 1↦→ ∗ R} ∗ 1↦→ ∗ R⇐⇒ 2 ↦→ {false} {false} ∗ 1↦→ ∗ R⇐= 2 ↦→‘free(−1)’ ∗ 1↦→ ∗ R2 ↦→ ◦ 1↦→ ◦ R ⇐⇒ 2 ↦→ ∗ 1↦→ ∗ R ⇐⇒ 2 ↦→ ∗ 1↦→ ∗ ((3 ↦→ {1↦→ } {1↦→ }) ⊗ R)⇐⇒ 2 ↦→ ∗ 1↦→ ∗ 3 ↦→ {1↦→ ∗ R} {1↦→ ∗ R}.Consider C ≡ let x=[2] in [3]:=x. When P [y] ≡ {1↦→ }y{1↦→ } ⊗ R,⊢ {2↦→P [ ] ∗ 3↦→P [ ]}‘C’{2↦→P [ ] ∗ 3↦→P [ ]}⊢ {2 ↦→ {1↦→ } {1↦→ } ◦ R}‘C’{2 ↦→ ◦ R}Now we instantiate k in (†) with C, discharge the premise of the resulting derivationwith the above derivation <strong>for</strong> C, <strong>and</strong> obtain⊢ {2 ↦→ ‘free(−1)’ ∗ 1 ↦→⊢ {2 ↦→ ‘free(−1)’ ∗ 1 ↦→∗ R}‘C’{2 ↦→ ∗ 1 ↦→ ∗ 3 ↦→ {1↦→ ∗ R} {1↦→ ∗ R}}∗ R}‘C; free(2)’{1 ↦→ ∗ 3 ↦→ {1↦→ ∗ R} {1↦→ ∗ R}}Here the second step uses the rules Free <strong>and</strong> Seq in Fig. 3. But the postconditionof the conclusion here is equivalent to 1↦→ ∗ R by the definition of R<strong>and</strong> the distribution axioms <strong>for</strong> ⊗. Thus, as our rule <strong>for</strong> eval will show later, weshould be able to conclude that⊢{2 ↦→ ‘free(−1)’ ∗ 1↦→ ∗ R}‘C; free(2); eval [3]’{1↦→ ∗ 3 ↦→ {1↦→ ∗ R} {1↦→ ∗ R}}


However, since −1 is not even an address, the program (C; free(2); eval [3]) alwaysfaults, contradicting the requirement of separation logic that proved programsrun without faulting.⊓⊔Notice that in the derivation above it is essential that R is a recursively definedassertion, otherwise we would not obtain that 2 <strong>and</strong> 3 point to code satisfyingthe same P .Fortunately, the second best choice leads to a consistent proof system:Proposition 2. Both the inference rule version of the frame rule <strong>for</strong> ◦ <strong>and</strong> theaxiom version <strong>for</strong> ∗ are sound in our semantics, which will be given in Section 4.In fact, the semantics also validates the following more general version of therule <strong>for</strong> ◦:Γ ⊢ PΓ ⊢ P ⊗ RRule <strong>for</strong> executing stored code An important <strong>and</strong> challenging part of thedesign of a program logic <strong>for</strong> higher-<strong>order</strong> store is the design of a proof rule <strong>for</strong>eval [e], the comm<strong>and</strong> that executes code stored at e. Indeed, the rule shouldovercome two challenges directly related to the recursive nature of higher-<strong>order</strong>store: (1) implicit recursion through the store (i.e., L<strong>and</strong>in’s knot), <strong>and</strong> (2) extensionalspecifications of stored code.These two challenges are addressed, using the expressiveness of our assertionlanguage, by the following rule <strong>for</strong> eval [e]:EvalΓ, k ⊢ R[k] ⇒{P ∗ e ↦→ R[ ]}k{Q}Γ ⊢{P ∗ e ↦→ R[ ]}‘eval [e]’{Q}This rule states that in <strong>order</strong> to prove {P ∗ e ↦→ R[ ]}‘eval [e]’{Q} <strong>for</strong> executingstored code in [e] under the assumption that e points to arbitrary code k (expressedby the which is an abbreviation <strong>for</strong> ∃k.e ↦→ R[k]), it suffices to showthat the specification R[k] implies that k itself fulfils triple{P ∗ e ↦→ R[ ]}k{Q}.In the above rule we do not make any assumptions about what code e actuallypoints to, as long as it fulfils the specification R. It may even be updated betweenrecursive calls. However, <strong>for</strong> recursion through the store, R must be recursivelydefined as it needs to maintain itself as an invariant of the code in e.The Eval rule crucially relies on the expressiveness of our assertion language,especially the presence of nested triples <strong>and</strong> recursive assertions. In our previouswork, we did not consider nested triples. As a result, we had to reason explicitlywith stored code, rather than properties of the code, as illustrated by one of ourold rules <strong>for</strong> eval [2]:OldEvalΓ ⊢{P }‘eval [e]’{Q} ⇒ {P }‘C’{Q}Γ ⊢{P ∗ e ↦→ ‘C’}‘eval [e]’{Q ∗ e ↦→ ‘C’}


EvalNonRec1Γ ⊢{P ∗ e ↦→ ∀y. {P } {Q}}‘eval [e]’{Q ∗ x ↦→ ∀y. {P } {Q}}EvalNonRecUpdΓ ⊢{P ∗ e ↦→ ∀y. {P ∗ e ↦→ } {Q}}‘eval [e]’{Q}EvalRec(where R = (e ↦→ ∀y. {P } {Q} ∗ P0) ⊗ R)Γ ⊢{P ◦ R}‘eval [e]’{Q ◦ R}Fig. 4. Derived rules from EvalHere the actual code C is specified explicitly in the pre- <strong>and</strong> post-conditions ofthe triple. In both rules the intuition is that the premise states that the body ofthe recursive procedure fulfils the triple, under the assumption that the recursivecall does so as well. In the Eval rule this is done without direct reference to thecode itself, but rather via a k satisfying R. The soundness proof of OldEvalproceeded along the lines of Pitts’ method <strong>for</strong> establishing relational propertiesof domains [10]. On the other h<strong>and</strong>, Eval relies on the availability of recursiveassertions, the existence of which is guaranteed by Banach’s fixpoint theorem.From the Eval rule one can easily derive the axioms of Fig. 4. The first twoaxioms are <strong>for</strong> non-recursive calls. This can be seen from the fact that in thepre-condition of the nested triples e does not appear at all or does not have aspecification, respectively. Only the third axiom EvalRec allows <strong>for</strong> recursivecalls. The idea of this axiom is that one assumes that the code in [e] fulfills therequired triple provided the code that e points to at call-time fulfils the triple aswell. Let us look at the actual derivation of EvalRec to make this evident. Wewrite S[k] ≡ ∀y.{P ◦ R}k{Q ◦ R} such that <strong>for</strong> the original R of rule EvalRecwe have R ⇔ (e ↦→ S[ ] ∗ (P 0 ⊗ R)). Note that Γ contains the variables y whichmay appear freely in P <strong>and</strong> Q.FOLΓ, k ⊢ (∀y.{P ◦ R}k{Q ◦ R}) ⇒{P ◦ R}k{Q ◦ R}ConseqΓ, k ⊢ S[k] ⇒{(P ⊗ R) ∗ (P 0 ⊗ R) ∗ e ↦→ S[ ]}k{Q ◦ R}EvalΓ ⊢{(P ⊗ R) ∗ (P 0 ⊗ R) ∗ e ↦→ S[ ]}‘eval [e]’{Q ◦ R}ConseqΓ ⊢{P ◦ R}‘eval [e]’{Q ◦ R}In the derivation tree above, the axiom used at the top is simply a first-<strong>order</strong> axiom<strong>for</strong> ∀ elimination. The quantified variables y are substituted by the variableswith the same name from the context. After an application of the EvalRec rulethose variables y can then be substituted further.The use of recursive specification R = (e ↦→ ∀y.{P } {Q}∗P 0 )⊗R is essentialhere as it allows us to unroll the definition so that the Eval rule can be applied.Note that in the logic of [5], which also uses nested triples but features neithera specification logic nor expresses any frame rules or axioms, such recursivespecifications are avoided. This is possible under the assumption that code doesnot change during recursion. One can then express the recursive R above as


⊗-<strong>Frame</strong>Γ ⊢ PΓ ⊢ P ⊗ R∗-<strong>Frame</strong>Γ ⊢{P }e{Q} ⇒{P ∗ R}e{Q ∗ R}EvalΓ, k ⊢ R[k] ⇒{P ∗ e ↦→ R[ ]}k{Q}Γ ⊢{P ∗ e ↦→ R[ ]}‘eval [e]’{Q}Fig. 5. Proof rules specific to higher-<strong>order</strong> storefollows (we can omit the P 0 now, as this is only needed <strong>for</strong> mutually recursivelydefined triples):e ↦→{e ↦→ k ∗ P }k{e ↦→ k ∗ Q}.The question however remains how the assertion can be proved <strong>for</strong> some concrete‘C’ in [e]. In loc.cit. this is done by an induction on some appropriate argument,as total correctness is considered only. Note that our old OldEval can be viewedas a fixpoint induction rule <strong>for</strong> proving such specifications, if one quantifiesaway the concrete appearances of ‘C’. In any case, our new Eval is obviouslyelegant to use, <strong>and</strong> it does not only allow <strong>for</strong> recursion through the store butalso disentangles the reasoning from the concrete code stored in the heap.Be<strong>for</strong>e finishing this section, we summarize a particular choice of a proofruleset from the current <strong>and</strong> previous subsections in Fig. 5. They will be provedsound in Section 4.4 Semantics of <strong>Nested</strong> <strong>Triples</strong>This section develops a model <strong>for</strong> the programming language <strong>and</strong> logic we havepresented. The semantics of programs, given in the next subsection using anuntyped domain-theoretic model, is st<strong>and</strong>ard. The following semantics of thelogic is, however, unusual; it is a possible world semantics where the worldslive in a recursively defined metric space. Finally, we discuss the existence ofrecursively defined assertions, which have been used in the previous sections.Semantics of expressions <strong>and</strong> comm<strong>and</strong>s The interpretation of the programminglanguage is given in the category Cppo ⊥ of pointed cpos <strong>and</strong> strictcontinuous functions, <strong>and</strong> is the same as in our previous work [2]. That is, comm<strong>and</strong>sdenote strict continuous functions C η∈ Heap ⊸ T err (Heap) whereHeap = Rec(Val) Val = Integers ⊥ ⊕ Com ⊥ Com = Heap ⊸ T err (Heap) (1)In these equations, T err (D) = D⊕{error} ⊥ denotes the error monad, <strong>and</strong> Rec(D)denotes records with entries from D <strong>and</strong> labelled by positive natural numbers. 4We use some evident record notations, such as {|l 1 =d 1 , . . . , l n =d n |} <strong>for</strong> the recordmapping label l i to d i , <strong>and</strong> dom(r) <strong>for</strong> the set of labels of a record r. Thedisjointness predicate r#r ′ on records holds if r <strong>and</strong> r ′ are not ⊥ <strong>and</strong> havedisjoint domains, <strong>and</strong> a continuous partial combining operation r·r ′ is defined byr · r ′ def= if r#r ′ then r ∪ r ′ else (if r=⊥ ∨ r ′ =⊥ then ⊥ else undefined).4 Formally, Rec(D) = `P N⊆ fin Nats(N→D + ↓ )´where (N→D ⊥↓) is the cpo of mapsfrom the finite address set N to the cpo D ↓ = D−{⊥} of non-bottom elements of D.


skip ηh def= hC 1;C 2 ηh def= if C 1 ηh ∈ {⊥, error} then C 1 ηh else C 2 η(C 1 ηh)if e=e ′ then C 1 else C 2η h def= if {e 1 η, e 2 η} ⊆ Com ⊥ then ⊥else if (e η= e ′ ) then C1 η η h else C2 η hlet x=new e 1, ..., e n in C ηh def= let l = min{l | ∀l ′ . (l≤l ′


<strong>and</strong> satisfies the strong triangle inequality d(x, y) ≤ max{d(x, z), d(z, y)}, <strong>for</strong>all x, y, z ∈ X. An (ultra-)metric space is complete if every Cauchy sequencehas a limit. A function f : X 1 → X 2 between metric spaces (X 1 , d 1 ), (X 2 , d 2 ) isnon-expansive if <strong>for</strong> all x, y ∈ X 1 , d 2 (f(x), f(y)) ≤ d 1 (x, y). It is contractive if<strong>for</strong> some 0 < δ < 1, d 2 (f(x), f(y)) ≤ δ · d 1 (x, y) <strong>for</strong> all x, y ∈ X 1 .The complete, 1-bounded ultrametric spaces <strong>and</strong> non-expansive functionsbetween them <strong>for</strong>m a Cartesian closed category CBUlt. Products in CBUlt aregiven by the set-theoretic product where the distance is the maximum of thecomponentwise distances, <strong>and</strong> exponentials are given by the non-expansive functionsequipped with the sup-metric. A functor F : CBUlt op × CBUlt −→ CBUltis locally non-expansive if d(F (f, g), F (f ′ , g ′ )) ≤ max{d(f, f ′ ), d(g, g ′ )} <strong>for</strong> allnon-expansive f, f ′ , g, g ′ , <strong>and</strong> it is locally contractive if d(F (f, g), F (f ′ , g ′ )) ≤ δ ·max{d(f, f ′ ), d(g, g ′ )} <strong>for</strong> some δ < 1. By multiplication of the distances of (X, d)with a shrinking factor δ < 1 one obtains a new ultrametric space, δ · (X, d) =(X, d ′ ) where d ′ (x, y) = δ · d(x, y). By shrinking, a locally non-expansive functorF yields a locally contractive functor (δ · F )(X 1 , X 2 ) = δ · (F (X 1 , X 2 )).The set UAdm of uni<strong>for</strong>m admissible subsets of Heap becomes a complete, 1-bounded ultrametric space when equipped with the following distance function:d(p, q) = defif (p≠q) then (2 −max{i∈ω | p [i]=q [i] } ) else 0. Note that d is well-defined:first, because π 0 = ⊥ <strong>and</strong> ⊥ ∈ p <strong>for</strong> all p ∈ UAdm the set {i ∈ ω | p [i] = q [i] }is non-empty; second, this set is finite, because p ≠ q implies p [i] ≠ q [i] <strong>for</strong> allsufficiently large i by the uni<strong>for</strong>mity of p, q <strong>and</strong> using ⊔ n∈ω π n = id Heap .Theorem 3. There exists an ultrametric space W <strong>and</strong> an isomorphism ι from· (W → UAdm) to W in CBUlt.12Proof. By an application of America & Rutten’s general existence theorem <strong>for</strong>fixed points of locally contractive functors on complete ultrametric spaces [1].See also [3] <strong>for</strong> details of a similar recent application.⊓⊔We write Pred <strong>for</strong> 1 2 · (W → UAdm) <strong>and</strong> ι−1 : W ∼ = Pred <strong>for</strong> the inverse to ι.For an ultrametric space (X, d) <strong>and</strong> n ∈ ω we use the notation x = n y tomean that d(x, y) ≤ 2 −n . By the ultrametric inequality, each = n is an equivalencerelation on X [3]. Since all non-zero distances in UAdm are of the <strong>for</strong>m 2 −n <strong>for</strong>some n ∈ ω, this is also the case <strong>for</strong> the distance function on W . There<strong>for</strong>e, toshow that a map is non-expansive it suffices to show that f(x) = n f(y) wheneverx = n y. The definition of Pred has the following consequence: <strong>for</strong> p, q ∈ Pred,p = n q iff p(w) n−1= q(w) <strong>for</strong> all w ∈ W . This fact is used repeatedly in our proofs.For p, q ∈ UAdm, the separating conjunction p ∗ q is defined as usual, byh ∈ p∗q ⇔ def∃h 1 , h 2 . h = h 1 ·h 2 ∧h 1 ∈ p∧h 2 ∈ q. This operation is lifted to nonexpansivefunctions p 1 , p 2 ∈ Pred pointwise, by (p 1 ∗ p 2 )(w) = p 1 (w) ∗ p 2 (w).This is well-defined, <strong>and</strong> moreover determines a non-expansive operation on thespace Pred. The corresponding unit <strong>for</strong> the lifted ∗ is the non-expansive functionemp = λw.{{||}, ⊥} (i.e., p ∗ emp = emp ∗ p = p, <strong>for</strong> all p). We let emp = ι(emp).Lemma 4. There exists a non-expansive map ◦ : W × W → W <strong>and</strong> a map⊗ : Pred × W → Pred that is non-expansive in its first <strong>and</strong> contractive in its


second argument, satisfying q ◦ r = ι(ι −1 (q) ⊗ r ∗ ι −1 (r)) <strong>and</strong> p ⊗ r = λw.p(r ◦ w)<strong>for</strong> all p ∈ Pred <strong>and</strong> q, r ∈ W .Proof. The idea of the proof is that the defining equations of both operationsgive rise to contractive maps, which have (unique) fixed points by Banach’s fixedpoint theorem. The detailed proof is given in Appendix A.1.⊓⊔Lemma 5. (W, ◦, emp) is a monoid in CBUlt. Moreover, ⊗ is an action of thismonoid on Pred.Proof. First, emp is a left-unit <strong>for</strong> ◦, emp◦q = ι((λw.ι −1 (emp)(q◦w))∗ι −1 (q)) =ι(ι −1 (q)) = q. Using this, one shows that it is also a right-unit <strong>for</strong> ◦. Next, oneshows by induction that <strong>for</strong> all n ∈ ω, ◦ is associative up to distance 2 −n , fromwhich associativity follows. By the 1-boundedness of W the base case is clear.For the inductive step n > 0, by definition of the distance function on Pred itsuffices to show that <strong>for</strong> all w ∈ W , ι −1 ((p ◦ q) ◦ r)(w) n−1= ι −1 (p ◦ (q ◦ r))(w).This equation follows from the definition of ◦ <strong>and</strong> the inductive hypothesis.That ⊗ <strong>for</strong>ms an action of W on Pred follows from these properties of ◦. First,p ⊗ emp = λw.p(emp ◦ w) = p since emp is a unit <strong>for</strong> ◦. Next, (p ⊗ q) ⊗ r =λw.p(q ◦ (r ◦ w)) = λw.p((q ◦ r) ◦ w) = p ⊗ (q ◦ r) by the associativity of ◦.The full proof is given as Lemma 14 in Appendix A.1.⊓⊔Semantics of triples <strong>and</strong> assertions Since assertions appear in the pre<strong>and</strong>post-conditions of <strong>Hoare</strong> triples, <strong>and</strong> triples can be nested inside assertions,the interpretation of assertions <strong>and</strong> triples must be defined simultaneously. Toachieve this, we first define a notion of semantic triple.Definition 6 (Semantic triple). A semantic <strong>Hoare</strong> triple consists of predicatesp, q ∈ Pred <strong>and</strong> a strict continuous function c ∈ Heap ⊸ T err (Heap),written {p}c{q}. For w ∈ W , a semantic triple {p}c{q} is <strong>for</strong>ced by w, denotedw |={p}c{q}, if <strong>for</strong> all r ∈ UAdm <strong>and</strong> all h ∈ Heap:h ∈ p(w) ∗ ι −1 (w)(emp) ∗ r ⇒ c(h) ∈ Ad(q(w) ∗ ι −1 (w)(emp) ∗ r),where Ad(r) denotes the least downward closed <strong>and</strong> admissible set of heaps containingr. A semantic triple is valid, written |= {p}c{q}, if w |= {p}c{q} <strong>for</strong>all w ∈ W . We extend semantic triples from Com = Heap ⊸ T err (Heap) to alld ∈ Val, by w |={p}d{q} iff d = c <strong>for</strong> some comm<strong>and</strong> c ∈ Com <strong>and</strong> w |={p}c{q}.Thus, semantic triples bake in the first-<strong>order</strong> frame property (by conjoiningr), <strong>and</strong> “close” the “open” recursion (by applying the world w, on which thetriple implicitly depends, to emp). The admissible downward closure that isapplied to the entire post-condition is in line with a partial correctness interpretationof triples. In particular, it entails that {c ∈ Com | w |= {p}c{q}}is an admissible <strong>and</strong> downward closed subset of Com. Finally, semantic triplesare non-expansive, in the sense that if w n =w ′ <strong>for</strong> n>0 <strong>and</strong> w |={p}c{q}, then


true ηw = HeapP ∧ Q ηw = P ηw ∩ Q ηwfalse ηw = {⊥}P ∨ Q ηw = P ηw ∪ Q ηwemp ηw = {{||} , ⊥}P ∗ Q ηw = P ηw ∗ Q ηwe 1 ↦→ e 2 ηw = {h | h ⊑ {|e 1 η= e 2 η|}} P ⊗ Q ηw = (P η⊗ ι(Q η))we 1 = e 2 ηw = {h | h ≠ ⊥ ⇒ e 1 η= e 2 η} ∀x.P ηw = T d∈Val P η[x:=d] w∃x.P ηw = {h | ∀n ∈ ω. π n(h) ∈ S d∈Val P η[x:=d] w}P ⇒ Q ηw = {h | ∀n ∈ ω. π n(h) ∈ P ηw implies π n(h) ∈ Q ηw}{P }e{Q} ηw = {h | w |={P η} e η{Q η}}∪ {π n(h) | n > 0 ⇒ w |={P η}π n−1; e η; π n−1{Q η}}Fig. 7. Semantics of assertionsw ′ |={p}c ′ {q} <strong>for</strong> c ′ = defπ n−1 ◦ c ◦ π n−1 . This observation plays a key role in thefollowing definition of the semantics of nested triples.Assertions are interpreted as elements P η∈ Pred. Note that (UAdm, ⊆) is acomplete Heyting BI algebra. Using the pointwise extension of the operations ofthis algebra to the set of non-expansive functions W → UAdm, we also obtain acomplete Heyting BI algebra on Pred = 1 2 · (W → UAdm) which soundly modelsthe intuitionistic predicate BI part of the assertion logic. Moreover, the monoidaction of W on Pred serves to model the invariant extension of the assertionlogic. This interpretation of assertions is spelled out in detail in Fig. 7.The interpretation of a triple {P }e{Q} is a union of two sets, <strong>and</strong> deservessome comment. The first set in this union is the expected interpretation, <strong>and</strong>states that if the triple holds in the current world w, then this set equals thetop element Heap. But, considering this first set alone, we would not be able toprove that {P }e{Q} ηis a non-expansive function from W to UAdm, since itlacks sufficient “approximation in<strong>for</strong>mation.” Guided by the non-expansivenessof semantic triples mentioned above (<strong>and</strong> proved in Lemma 17 in the Appendix),the second set in the interpretation adds this in<strong>for</strong>mation. Basically, it states thatif a triple is “approximately valid” (meaning that it holds <strong>for</strong> π n ; c; π n rather thanc), then the assertion is “approximately true” (i.e., it contains Heap [n+1] ). Asimilar approach has been taken in [3] to <strong>for</strong>ce non-expansiveness <strong>for</strong> a referencetype constructor <strong>for</strong> ML-style references.We can recover the (approximate) validity of a nested triple from its interpretation:If h is a compact element of Heap, then the least r <strong>for</strong> which π r (h) = his the rank of h. Now, if h ∈ {P }e{Q} ηw <strong>for</strong> some h of rank r, then theinterpretation indeed yields w |={P η}π r−1 ; e η; π r−1 {Q η}.Soundness of the proof rules We prove soundness of the proof rules listed inSections 2 <strong>and</strong> 3. We first consider the distribution axioms <strong>for</strong> − ⊗ R in Fig. 2.Lemma 7 (⊗-Dist, 1). The axiom (P ⊗ Q) ⊗ R ⇔ P ⊗ (Q ◦ R) is valid.Proof. This is an instance of the fact that ⊗ is a monoid action: by definition ofthe semantics, the equivalence of both sides follows from Lemma 5. ⊓⊔


Lemma 8 (⊗-Dist, 2). The axiom {P }e{Q} ⊗ R ⇔{P ◦ R}e{Q ◦ R} is valid.Proof. The statement follows from the following claim: <strong>for</strong> all p, q, r ∈ Pred,strict continuous c : Heap ⊸ T err (Heap) <strong>and</strong> all w ∈ W , ι(r) ◦ w |={p}c{q} iffw |={p ⊗ ι(r) ∗ r}c{q ⊗ ι(r) ∗ r}. The proof of this claim uses the equation(p ⊗ ι(r) ∗ r)(w) ∗ ι −1 (w)(emp) = p(ι(r) ◦ w) ∗ ι −1 (ι(r) ◦ w)(emp),which is a consequence of the definitions of ⊗ <strong>and</strong> ◦.⊓⊔The proofs of the remaining distribution axioms are more direct: the interpretationof the logical connectives is defined pointwise, <strong>and</strong> emp <strong>and</strong> (e 1 ↦→ e 2 )are constant. The full proofs can be found in Appendix A.2.Next, we consider the rules <strong>for</strong> higher-<strong>order</strong> store given in Fig. 5.Lemma 9 (⊗-<strong>Frame</strong>). The ⊗-<strong>Frame</strong> rule is sound: if h ∈ p(w) <strong>for</strong> all h ∈Heap <strong>and</strong> w ∈ W , then h ∈ (p⊗ι(r))(w) <strong>for</strong> all h ∈ Heap, w ∈ W <strong>and</strong> r ∈ Pred.Proof. Assume ∀h.∀w.h ∈ p(w). Let r ∈ Pred, w ∈ W <strong>and</strong> h ∈ Heap. We show thath ∈ (p ⊗ ι(r))(w). Note that (p ⊗ ι(r))(w) = p(ι(r) ◦ w) by the definition of ⊗. So,<strong>for</strong> w ′ = defι(r) ◦ w, the assumption shows that h ∈ p(w ′ ) = (p ⊗ ι(r))(w). ⊓⊔Lemma 10 (∗-<strong>Frame</strong>).{P }e{Q} ⇒{P ∗ R}e{Q ∗ R} is valid <strong>for</strong> all P, Q, R, e.Proof. We show that <strong>for</strong> all w ∈ W , p, q, r ∈ Pred <strong>and</strong> c ∈ Com, if w |={p}c{q},then w |={p ∗ r}c{q ∗ r}. This implies the lemma as follows. If k is the rank ofπ n (h) <strong>and</strong> π n (h) ∈ {P }e{Q} w, then w |={P η}c{Q η} <strong>for</strong> c = π k−1 ; e η; π k−1 .This lets us conclude w |= {P ∗ R η}c{Q ∗ R η}, which in turn implies thatπ n (h) belongs to {P ∗ R}e{Q ∗ R} w.Assume w |={p}c{q}. We must show that w |={p ∗ r}c{q ∗ r}. Let r ′ ∈ UAdm<strong>and</strong> assume h ∈ (p ∗ r)(w) ∗ ι −1 (w)(emp) ∗ r ′ = p(w) ∗ ι −1 (w)(emp) ∗ (r(w) ∗ r ′ ).Since w |={p}c{q}, it follows that c(h) ∈ Ad(q(w) ∗ ι −1 (w)(emp) ∗ (r(w) ∗ r ′ )) =Ad((q ∗ r)(w) ∗ ι −1 (w)(emp) ∗ r ′ ), which establishes w |={p ∗ r}c{q ∗ r}. ⊓⊔The other rules in Section 3 (i.e., those in Fig. 3 <strong>and</strong> rule Eval) are provedsound in Appendix A.3.Semantics of recursive assertions The following general fixed point theoremis a consequence of Banach’s fixed point theorem, <strong>and</strong> it allows us to introducerecursively defined assertions in the logic, as used in previous sections.Theorem 11 (Mutually recursive predicates). Let I be a set <strong>and</strong> supposethat, <strong>for</strong> each i ∈ I, F i : Pred I → Pred is a contractive function. Then thereexists a unique p = (p i ) i∈I ∈ Pred I such that F i (p) = p i , <strong>for</strong> all i ∈ I.Note that this theorem is sufficiently general to permit the mutual recursivedefinition of even infinite families of predicates.As established in Lemma 4, ⊗ is contractive in its right-h<strong>and</strong> argument.Thus, <strong>for</strong> fixed P <strong>and</strong> η, the map F (r) = P η⊗ ι(r) on Pred is contractive


<strong>and</strong> has a unique fixed point, r. Given an equation R = P ⊗ R we take r as theinterpretation of R, <strong>and</strong> note that indeed R η= r = P η⊗ ι(r) = P ⊗ R η.Along the same lines, we can interpret mutually recursive assertions: R 1 = P 1 ⊗(R 1 ∗ . . . ∗ R n ), . . . , R n = P n ⊗ (R 1 ∗ . . . ∗ R n ). Using the non-expansivenessof ∗ as an operation on Pred, these equations give rise to contractive functionsF i (r 1 , . . . , r n ) = P i η⊗ ι(r 1 ∗ . . . ∗ r n ). We then define R i ηas r i , where r =(r i ) i=1..n is the unique element of Pred n satisfying r i = F i (r) that exists byTheorem 11.Acknowledgements We would like to thank Kristian Støvring <strong>and</strong> JacobThamsborg <strong>for</strong> helpful discussions. Kristian suggested that ⊗ is a monoid action.References1. P. America <strong>and</strong> J. J. M. M. Rutten. Solving reflexive domain equations in acategory of complete metric spaces. J. Comput. Syst. Sci., 39(3):343–375, 1989.2. L. Birkedal, B. Reus, J. Schwinghammer, <strong>and</strong> H. Yang. A simple model of separationlogic <strong>for</strong> higher-<strong>order</strong> store. In ICALP’08, pages 348–360, 2008.3. L. Birkedal, K. Støvring, <strong>and</strong> J. Thamsborg. Realizability semantics of parametricpolymorphism, general references, <strong>and</strong> recursive types. In FOSSACS’09, pages456–470, 2009.4. L. Birkedal, N. Torp-Smith, <strong>and</strong> H. Yang. Semantics of separation-logic typing<strong>and</strong> higher-<strong>order</strong> frame rules <strong>for</strong> algol-like languages. Logical Methods in Comput.Sci., 2(5:1), 2006.5. K. Honda, N. Yoshida, <strong>and</strong> M. Berger. An observationally complete program logic<strong>for</strong> imperative higher-<strong>order</strong> functions. In LICS’05, pages 270–279, 2005.6. N. Krishnaswami, L. Birkedal, J. Aldrich, <strong>and</strong> J. Reynolds. Idealized ML <strong>and</strong> ItsSeparation Logic. Available at http://www.cs.cmu.edu/˜neelk/, 2007.7. P. W. O’Hearn <strong>and</strong> D. J. Pym. The logic of bunched implications. Bulletin ofSymbolic Logic, 5(2):215–244, June 1999.8. P. W. O’Hearn, H. Yang, <strong>and</strong> J. C. Reynolds. Separation <strong>and</strong> in<strong>for</strong>mation hiding.In POPL’04, pages 268–280, 2004.9. M. Parkinson <strong>and</strong> G. Biermann. Separation logic, abstraction <strong>and</strong> inheritance. InPOPL’08, pages 75–86, 2008.10. A. M. Pitts. Relational properties of domains. Inf. Comput., 127:66–90, 1996.11. F. Pottier. Hiding local state in direct style: a higher-<strong>order</strong> anti-frame rule. InLICS’08, pages 331–340, 2008.12. B. Reus <strong>and</strong> J. Schwinghammer. Separation logic <strong>for</strong> higher-<strong>order</strong> store. In CSL’06,pages 575–590, 2006.13. J. C. Reynolds. Separation logic: A logic <strong>for</strong> shared mutable data structures. InLICS’02, pages 55–74, 2002.14. M. B. Smyth <strong>and</strong> G. D. Plotkin. The category-theoretic solution of recursivedomain equations. SIAM J. Comput., 11(4):761–783, 1982.15. T. Streicher. Domain-theoretic Foundations of Functional Programming. WorldScientific, 2006.


AProofsThis section contains the proofs omitted from the main part of the paper.A.1 Interpretation of assertionsLemma 12 (Separating conjunction). Separating conjunction is well-defined:if p, q ∈ Pred then so is p ∗ q. Moreover, it is a non-expansive operation on thespace Pred.Proof. We first show that separating conjunction on UAdm is well-defined, i.e.,if p, q ∈ UAdm then so is p ∗ q. Since ⊥ ∈ p <strong>and</strong> ⊥ ∈ q, <strong>and</strong> ⊥ = ⊥ · ⊥, wehave ⊥ ∈ p ∗ q. If h 0 ⊑ h 1 ⊑ . . . is a chain in p ∗ q with lub h ≠ ⊥, thenh i ≠ ⊥ <strong>for</strong> almost all i <strong>and</strong> there are heaps h ′ i , h′′ ih ′ isuch that h i = h ′ i · h′′ i <strong>and</strong>∈ q. Since the <strong>order</strong> on Heap is defined pointwise (<strong>for</strong> heaps with∈ p <strong>and</strong> h′′ iequal domain), there must be a subsequence (h ik ) k such that h ′ i 1⊑ h ′ i 2⊑ . . .is a chain in p <strong>and</strong> h ′′i 1⊑ h ′′i 2⊑ . . . is a chain in q. Thus, also their lubs h ′<strong>and</strong> h ′′ satisfy h ′ ∈ p <strong>and</strong> h ′′ ∈ q. It follows that h = h ′ · h ′′ ∈ p ∗ q, <strong>and</strong>there<strong>for</strong>e p ∗ q is admissible. To see that p ∗ q is uni<strong>for</strong>m, suppose h ∈ p ∗ q <strong>and</strong>let n ∈ ω. By definition, h = h 1 · h 2 <strong>for</strong> heaps h 1 , h 2 such that h 1 ∈ p <strong>and</strong> h 2 ∈ q.By uni<strong>for</strong>mity of p <strong>and</strong> q, this gives π n (h 1 ) ∈ p <strong>and</strong> π n (h 2 ) ∈ q, from whichπ n (h) = π n (h 1 ) · π n (h 2 ) ∈ p ∗ q follows.It remains to show that <strong>for</strong> p, q ∈ Pred, p ∗ q is a non-expansive function.Suppose w, w ′ ∈ W such that w n = w ′ , <strong>and</strong> suppose π n (h) ∈ (p ∗ q)(w) =p(w) ∗ q(w). We must show that π n (h) ∈ (p ∗ q)(w ′ ). By definition of ∗ on UAdmthere exist h 1 ∈ p(w) <strong>and</strong> h 2 ∈ q(w) such that π n (h) = h 1 · h 2 . By uni<strong>for</strong>mity,we also have π n (h 1 ) ∈ p(w) <strong>and</strong> π n (h 2 ) ∈ q(w). Since we assumed w n = w ′ , thisyields π n (h 1 · h 2 ) = π n (h 1 ) · π n (h 2 ) ∈ p(w ′ ) ∗ q(w ′ ) = (p ∗ q)(w ′ ). Finally, sinceπ n (h) = π n (π n (h)) = π n (h 1 · h 2 ) the first statement follows.To see that separating conjunction is non-expansive, assume that p n = p ′<strong>and</strong> q n = q ′ <strong>for</strong> arbitrary p, p ′ , q, q ′ ∈ Pred. We must show that p ∗ q n = p ′ ∗ q ′ .Since Pred = 1 n−12 · (W → UAdm) we can equivalently show that p(w) ∗ q(w) =p ′ (w) ∗ q ′ (w) <strong>for</strong> all w ∈ W . This follows from the assumption that p = n p ′ <strong>and</strong>q = n q ′ <strong>and</strong> the fact that π n−1 (h) = π n−1 (h 1 ) · π n−1 (h 2 ) whenever h = h 1 · h 2 . ⊓⊔Lemma 13 (Existence of maps ◦ <strong>and</strong> ⊗). There exists a non-expansive map◦ : W × W → W <strong>and</strong> a map ⊗ : Pred × W → Pred that is non-expansive in itsfirst <strong>and</strong> contractive in its second argument, satisfyingq ◦ r = ι(ι −1 (q) ⊗ r ∗ ι −1 (r)) <strong>and</strong> p ⊗ r = λw.p(r ◦ w)<strong>for</strong> all p ∈ Pred <strong>and</strong> q, r ∈ W .Proof. The idea of the proof is that the defining equations of both operationsgive rise to contractive maps, which have (unique) fixed points by the Banach


fixed point theorem. More precisely, consider the endofunction · on the (nonexpansive-function)space W × W → W , defined <strong>for</strong> all p, q ∈ W byp ◦ q = ι((λw.ι −1 (p)(q ◦ w)) ∗ ι −1 (q)).Note that ◦ is a non-expansive function: if p n = p ′ <strong>and</strong> q n = q ′ then q ◦ w n = q ′ ◦ win W <strong>for</strong> all w ∈ W <strong>and</strong> ι −1 (p) n = ι −1 (p ′ ) <strong>and</strong> ι −1 (q) n = ι −1 (q ′ ) in Pred. Since ∗is non-expansive (Lemma 12),(λw.ι −1 (p)(q ◦ w)) ∗ ι −1 (q) n−1= (λw.ι −1 (p ′ )(q ′ ◦ w)) ∗ ι −1 (q ′ )holds in W → UAdm, so that p ◦ q = n p ′ ◦ q ′ in W .nWe show that · is contractive. Assume ◦ 1 = ◦2 in W × W → W ; we mustn+1show that ◦ 1 = ◦ 2 . Let p, q ∈ W . Then by the sup-metric on W × W → W itsuffices to prove that p ◦ 1 q n+1= p ◦ 2 q holds in W , or equivalently, that(λw.ι −1 (p)(q ◦ 1 w)) ∗ ι −1 (q) n = (λw.ι −1 (p)(q ◦ 2 w)) ∗ ι −1 (q)holds in W → UAdm. By the non-expansiveness of ∗ (Lemma 12) <strong>and</strong> the supmetricon W → UAdm, this follows since q ◦ 1 w = n q ◦ 2 w <strong>for</strong> all w ∈ W by thenassumption that ◦ 1 = ◦2 , <strong>and</strong> hence ι −1 (p)(q ◦ 1 w) = n ι −1 (p)(q ◦ 2 w).By contractiveness of · <strong>and</strong> the Banach fixed point theorem, there exists aunique non-expansive map ◦ satisfying p◦q = p ◦ q = ι(λw.ι −1 (p)(q◦w)∗ι −1 (q)).We can now define the operation ⊗ : Pred × W → Pred by p ⊗ r = λw.p(r ◦ w)<strong>for</strong> all p ∈ Pred <strong>and</strong> r ∈ W , from which the required equivalences follow. Finally,we note that if p = n p ′ <strong>and</strong> r = m r ′ then p ⊗ r = k p ′ ⊗ r ′ <strong>for</strong> k = min{n, m + 1},i.e., the operation is non-expansive in its first component <strong>and</strong> contractive in itssecond. To see this, suppose p = n p ′ in Pred <strong>and</strong> r = m r ′ in W . Without lossof generality we may assume n > 0, so that p n−1= p ′ holds in W → UAdm.By non-expansiveness of ◦ it follows that r ◦ w = m r ′ ◦ w <strong>for</strong> all w, <strong>and</strong> there<strong>for</strong>eλw.p(r◦w) min{n−1,m}= λw.p ′ (r ′ ◦w) in W → UAdm. Hence p⊗r min{n,m+1}= p ′ ⊗r ′in Pred as required.⊓⊔Lemma 14. (W, ◦, emp) is a monoid in CBUlt. Moreover, ⊗ is an action ofthis monoid on Pred.Proof. We check that W is a monoid. First, emp is a left-unit <strong>for</strong> ◦: if q ∈ Wthenemp ◦ q = ι(λw.ι −1 (emp)(q ◦ w) ∗ ι −1 (q)) = ι(ι −1 (q)) = qIt is also a right-unit <strong>for</strong> ◦:p ◦ emp = ι(λw.ι −1 (p)(emp ◦ w) ∗ ι −1 (emp)) = ι(λw.ι −1 (p)(w) ∗ emp) = p<strong>for</strong> all p ∈ W . By induction on n we show that <strong>for</strong> all n ∈ ω, ◦ is associative upto distance 2 −n from which associativity follows. By 1-boundedness of W it is


clear that (p ◦ q) ◦ r = 0 p ◦ (q ◦ r) holds <strong>for</strong> all p, q, r ∈ W . For the inductive stepn > 0, by definition of the distance function on Pred it suffices to show that <strong>for</strong>all w ∈ W , ι −1 ((p ◦ q) ◦ r)(w) n−1= ι −1 (p ◦ (q ◦ r))(w). Suppose w ∈ W , thenι −1 ((p ◦ q) ◦ r)(w) = ι −1 (p ◦ q)(r ◦ w) ∗ ι −1 (r)(w)= ι −1 (p)(q ◦ (r ◦ w)) ∗ ι −1 (q)(r ◦ w) ∗ ι −1 (r)(w)= ι −1 (p)(q ◦ (r ◦ w)) ∗ ι −1 (q ◦ r)(w)n−1= ι −1 (p)((q ◦ r) ◦ w) ∗ ι −1 (q ◦ r)(w)= ι −1 (p ◦ (q ◦ r))(w)using the induction hypothesis in the fourth equation.That ⊗ <strong>for</strong>ms an action of W on Pred follows from these properties of ◦:first, p ⊗ emp = λw.p(emp ◦ w) = p since emp is a unit <strong>for</strong> ◦, <strong>and</strong>(p ⊗ q) ⊗ r = λw.p(q ◦ (r ◦ w)) = λw.p((q ◦ r) ◦ w) = p ⊗ (q ◦ r)by the associativity of ◦.⊓⊔Since there is a closure applied to the post-condition of semantic triples, butno similar closure used in the pre-condition, it is not immediately clear how tocompose comm<strong>and</strong>s. The following characterisation permits the proof <strong>for</strong> therule of sequential composition.Lemma 15 (Closure). If f:D ⊸ D ′ is a strict continuous function, q ⊆ D ′ isan admissible <strong>and</strong> downwards closed subset of D ′ , <strong>and</strong> p ⊆ D is an arbitrarysubset of D, then f(p) ⊆ q implies f(Ad(p)) ⊆ q.Proof. Since f is continuous, the pre-image f −1 (q) of q is admissible <strong>and</strong> downwardclosed. From the assumption that f(p) ⊆ q it follows that p ⊆ f −1 (q), <strong>and</strong>thus Ad(p) ⊆ f −1 (q) as the <strong>for</strong>mer is by definition the least admissible <strong>and</strong> downwardclosed subset of D containing p. Thus, if h ∈ Ad(p) then f(h) ∈ q. ⊓⊔Lemma 16 (Admissibility <strong>and</strong> downward closure of semantic triples).Assume w ∈ W <strong>and</strong> p, q ∈ Pred. Then the set {c ∈ Com | w |= {p}c{q}} is anadmissible <strong>and</strong> downward closed subset of Com.Proof. Both properties follow from the definition of w |={p}c{q}, specificallyfrom the respective properties of the closure applied in the post-condition. ⊓⊔Lemma 17 (Non-expansiveness of semantic triples). Assume w, w ′ ∈ W<strong>and</strong> let n>0 s.t. w n =w ′ . If w |={p}c{q}, then w ′ |={p}c ′ {q} <strong>for</strong> c ′ def= π n−1 ◦ c ◦ π n−1 .Proof. Assume w = n w ′ , <strong>and</strong> let p, q ∈ Pred <strong>and</strong> c : Heap ⊸ T err (Heap) suchthat w |={p}c{q}. To show that w ′ |={p}c ′ {q} <strong>for</strong> c ′ (h) = defπ n−1 (c(π n−1 (h))), letr ∈ UAdm <strong>and</strong> h ∈ Heap such that h ∈ p(w ′ ) ∗ ι −1 (w ′ )(emp) ∗ r. We must provethat c ′ (h) ∈ Ad(q(w ′ ) ∗ ι −1 (w ′ )(emp) ∗ r).


Since w = n w ′ , we have ι −1 (w ′ )(emp) n−1= ι −1 (w)(emp). Hence, by thenon-expansiveness of p <strong>and</strong> the compatibility of ∗ with projections, we haveπ n−1 (h) ∈ p(w) ∗ ι −1 (w)(emp) ∗ r. By the assumption that w |= {p}c{q}, thisyields c(π n−1 (h)) ∈ Ad(q(w)∗ι −1 (w)(emp)∗r). Using the non-expansiveness of q,the uni<strong>for</strong>mity of r, <strong>and</strong> the fact that ι −1 (w)(emp) n−1= ι −1 (w ′ )(emp) again, weknow that π n−1 (h ′ ) ∈ q(w ′ )∗ι −1 (w ′ )(emp)∗r whenever h ′ ∈ q(w)∗ι −1 (w)(emp)∗r. Thus π n−1 (c(π n−1 (h))) ∈ Ad(q(w ′ ) ∗ ι −1 (w ′ )(emp) ∗ r) by Lemma 15 <strong>and</strong> thecontinuity of π n−1 . This was to show.⊓⊔Lemma 18 (Heyting BI algebra). Let I = {{||}, ⊥}. Then (UAdm, ⊆, ∗, I)is a complete BI algebra. That is, (UAdm, ⊆) is a residuated complete Heytingalgebra with a (monotone) commutative monoid structure (UAdm, ∗, I) <strong>and</strong> the∗ operator also has the corresponding residuation operator.Proof. Since admissibility <strong>and</strong> uni<strong>for</strong>mity are preserved by arbitrary intersections,UAdm is a complete lattice, with meets given by set-theoretic intersection,least element {⊥} <strong>and</strong> greatest element Heap. Binary joins are given byset-theoretic union, <strong>and</strong> arbitrary joins by ⊔ i p i = ⋂ {p ∈ UAdm | p ⊇ ⋃ i p i}.The join is described more explicitly as ⊔ i p i = {h | ∀n ∈ ω. π n (h) ∈ ⋃ i p i}.First, note that the right h<strong>and</strong> side r = def{h | ∀n ∈ ω. π n (h) ∈ ⋃ i p i} is anelement of UAdm: r is uni<strong>for</strong>m, i.e., h ∈ r implies π m (h) ∈ r <strong>for</strong> all m ∈ ω, sinceπ n · π m = π min{n,m} . To show that r is also admissible suppose h 0 ⊑ h 1 ⊑ . . . isa chain in r, <strong>and</strong> let h be the lub of this chain. We must show that π n (h) ∈ ⋃ i p i<strong>for</strong> all n ∈ ω. By compactness, π n (h) ⊑ h k ⊑ h <strong>for</strong> some k, <strong>and</strong> hence π n (h) =π n (h k ) ∈ ⋃ p i using the idempotency of π n <strong>and</strong> the fact that h k ∈ r. To seethe inclusion r ⊆ ⊔ i p i, note that <strong>for</strong> all h, if π n (h) ∈ ⋃ i p i ⊆ p <strong>for</strong> all n ∈ ω<strong>and</strong> some arbitrary p ∈ UAdm, then also h = ⊔ n π n (h) ∈ p by admissibility, <strong>and</strong>hence h ∈ ⊔ i p i follows. For the other inclusion, we claim that the right h<strong>and</strong>side r = def{h | ∀n ∈ ω. π n (h) ∈ ⋃ i p i} is one of the elements appearing in theintersection; from this claim it is immediate that r ⊇ ⊔ i p i. The claim followssince r ⊇ ⋃ i p i by the uni<strong>for</strong>mity of the p i ’s.The implication of this complete lattice UAdm is described by p ⇒ q def={h | ∀n ∈ ω. if π n (h) ∈ p then π n (h) ∈ q}: Using π n · π m = π min{n,m} it is easyto see that p ⇒ q is uni<strong>for</strong>m. Admissibility follows analogously to the case ofjoins: if h 0 ⊑ h 1 ⊑ . . . is a chain in p ⇒ q with lub h, <strong>and</strong> if n ∈ ω is such thatπ n (h) ∈ p then we must show that π n (h) ∈ q. Since π n (h) ⊑ h is compact, thereis some k such that π n (h) ⊑ h k ⊑ h, <strong>and</strong> thus the required π n (h) = π n (h k ) ∈ qfollows from h k ∈ p ⇒ q. Next, to see that p ⇒ q is indeed the implicationin UAdm, first note that we have p ∩ (p ⇒ q) ⊆ q, using the uni<strong>for</strong>mity of p<strong>and</strong> the admissibility of q. If p ∩ r ⊆ q <strong>for</strong> some r ∈ UAdm, <strong>and</strong> h ∈ r <strong>and</strong>π n (h) ∈ p <strong>for</strong> some n ∈ ω, then the uni<strong>for</strong>mity of r yields π n (h) ∈ q. Thus weobtain p ∩ r ⊆ q ⇔ r ⊆ p ⇒ q.That ∗ is an operation on UAdm is established in the proof of Lemma 12. Itis easy to check that ∗ is commutative <strong>and</strong> associative <strong>and</strong> that it is monotone,i.e., if p ⊆ p ′ <strong>and</strong> q ⊆ q ′ then p ∗ q ⊆ p ′ ∗ q ′ . Moreover, we have I ∈ UAdm, <strong>and</strong>


the fact that p∗I = p = I ∗p follows from the definition of the heap combinationh · h ′ . Finally, the separating implication is given byp −∗ q = {h | ∀n ∈ ω.∀h ′ ∈ p. if π n (h)#h ′ then π n (h) · h ′ ∈ q}.One can check that p −∗ q is uni<strong>for</strong>m <strong>and</strong> admissible, <strong>and</strong> that it satisfies p ∗ r ⊆q ⇔ r ⊆ p −∗ q.⊓⊔Lemma 19 (Well-definedness). The interpretation in Fig. 7 is well-defined:each P ηdenotes a non-expansive function from W to UAdm.Proof. The proof is by induction on the structure of P .– Cases false, true <strong>and</strong> emp. By Lemma 18, we have that P ηw ∈ UAdm <strong>for</strong>all w ∈ W . In all three cases, P ηis constant, <strong>and</strong> thus non-expansive.– Cases P 1 ∧ P 2 , P 1 ∨ P 2 <strong>and</strong> ∀x.P 1 . By Lemma 18, we have that P ηw ∈UAdm <strong>for</strong> all w ∈ W . To prove non-expansiveness of P 1 ∧ P 2 η, assumep, q : W → UAdm are non-expansive, w n = w ′ , <strong>and</strong> let h ∈ (p ∩ q)(w) =p(w)∩q(w). By the non-expansiveness of p <strong>and</strong> q we have π n (h) ∈ (p∩q)(w ′ ).Using a symmetric argument, this shows (p ∩ q)(w) n = (p ∩ q)(w ′ ), i.e., thenon-expansiveness of p ∩ q. Non-expansiveness of P 1 ∧ P 2 η<strong>and</strong> ∀x.P 1 ηis analogous, by showing that p ∪ q <strong>and</strong> ∩ i p i are non-expansive <strong>for</strong> all nonexpansivep, q, p i : W → UAdm.– Case P 1 ⇒ P 2 . By Lemma 18, we have that P ηw ∈ UAdm <strong>for</strong> all w ∈ W .To prove non-expansiveness of P 1 ⇒ P 2 η, assume p, q : W → UAdm arenon-expansive, w n = w ′ , <strong>and</strong> let h ∈ (p(w) ⇒ q(w)). It suffices to show thatπ n (h) ∈ (p(w ′ ) ⇒ q(w ′ )). So let m ∈ ω <strong>and</strong> assume π m (π n (h)) ∈ p(w ′ ). Thusalso π m (π n (h)) ∈ p(w), <strong>and</strong> there<strong>for</strong>e π m (π n (h)) ∈ q(w) by the assumptionthat h ∈ (p ⇒ q)(w). Consequently, also π m (π n (h)) ∈ q(w ′ ), <strong>and</strong> we haveproved that π n (h) ∈ (p(w ′ ) ⇒ q(w ′ )), i.e., that p ⇒ q is non-expansive.– Case ∃x.P ′ . By Lemma 18, we have that P ηw ∈ UAdm <strong>for</strong> all w ∈ W . Toprove non-expansiveness of ∃x.P ′ η, assume that <strong>for</strong> i ∈ I, p i : W → UAdmare non-expansive, w n = w ′ , <strong>and</strong> choose h such that π m (h) ∈ ⋃ i p i(w) <strong>for</strong>all m ∈ ω. We must show that π m (π n (h)) ∈ ⋃ i p i(w ′ ) <strong>for</strong> all m ∈ ω. Byassumption, there exists i such that π m (h) ∈ p i (w), so that π n (π m (h)) ∈p i (w ′ ) by the non-expansiveness of p i . This proves π m (π n (h)) = π n (π m (h)) ∈⋃i p i(w ′ ).– Case P 1 ∗ P 2 <strong>and</strong> P 1 ⊗ P 2 . These cases have already been considered inLemmas 12 <strong>and</strong> 13.– Case e 1 ↦→ e 2 . Since e 1 ↦→ e 2 ηis a constant function, it is non-expansive.That its codomain is UAdm follows from the admissibility <strong>and</strong> downwardclosure of {d ∈ Val | d ⊑ d ′ }.– Case e 1 = e 2 . Since e 1 = e 2 ηis a constant function, it is non-expansive.That its codomain is UAdm follows since it either equals Heap or {⊥}.– Case{P 1 }e{Q 1 }. We first prove the non-expansiveness of {P 1 }e{Q 1 } η. Assumethat w n = w ′ , <strong>and</strong> let h ∈ {P }e{Q} ηw. We must show that π n (h) ∈


{P }e{Q} ηw ′ . We distinguish two cases. First, if h is not finite, i.e., h ≠π n (h) <strong>for</strong> all n, then the assumption h ∈ {P }e{Q} ηw implies that w |={P 1 η} e η{Q 1 η}. By Lemma 17 we havew ′ |={P 1 η}π n−1 ; e η; π n−1 {Q 1 η}<strong>and</strong> there<strong>for</strong>e π n (h) ∈ {P }e{Q} ηw ′ .In the second case, h has finite rank k, say. By assumption we haveBy Lemma 17 we obtainw |={P 1 η}π k−1 ; e η; π k−1 {Q 1 η},w ′ |={P 1 η}π m−1 ; e η; π m−1 {Q 1 η},<strong>for</strong> m = min{k, n}, <strong>and</strong> there<strong>for</strong>e π m (h) ∈ {P }e{Q} ηw ′ . The result followssince π m (h) = π n (h) (using π k (h) = h).Since w = n w <strong>for</strong> all n, the uni<strong>for</strong>mity of {P 1 }e{Q 1 } ηw follows as a specialcase from non-expansiveness. That ⊥ ∈ {P 1 }e{Q 1 } ηw is immediate fromthe definition. Finally, to see the closure under least upper bounds, let h 0 ⊑h 1 ⊑ . . . be a chain in {P 1 }e{Q 1 } ηw, <strong>and</strong> let h be its lub. Then, eitherthere exists a bound n <strong>for</strong> the ranks of all h i , which implies that h = h i<strong>for</strong> some i so that h ∈ {P 1 }e{Q 1 } ηw follows trivially. Otherwise, <strong>for</strong> eachn ∈ ω there exists an index i n such that h in is of rank n. Thus from theassumption <strong>and</strong> the fact that h in = π n (h in ) we getw |={P 1 η}π n−1 ; e η; π n−1 {Q 1 η}<strong>for</strong> all n ∈ ω. By the admissibility of semantic triples in the comm<strong>and</strong> position(Lemma 16), it follows that the triple is <strong>for</strong>ced at w also <strong>for</strong> the lub⊔n π n; e η; π n . Since ⊔ n π n = id, we obtain w |={P 1 η} e η{Q 1 η} fromthis. There<strong>for</strong>e h ∈ {P 1 }e{Q 1 } ηw holds.⊓⊔A.2 Soundness of the distribution axiomsLemma 20 (⊗-Dist, 1). The axiom (P ⊗ Q) ⊗ R ⇔ P ⊗ (Q ◦ R) is valid.Proof. By definition, the semantics of the left-h<strong>and</strong> side (P ⊗ Q) ⊗ R is givenby (P η⊗ ι(Q η)) ⊗ ι(R η). This is the same as P η⊗ (ι(Q η) ◦ ι(R η)) byLemma 5. By definition of ◦, we can write this equivalently as P η⊗ ι(Q η⊗ι(R η) ∗ R η), which is the semantics of P ⊗ (Q ⊗ R ∗ R) = P ⊗ (Q ◦ R). ⊓⊔Lemma 21 (⊗-Dist, 2). The axiom{P }e{Q} ⊗ R ⇔{P ◦ R}e{Q ◦ R} is valid.Proof. The statement follows from the following claim: <strong>for</strong> all p, q, r ∈ Pred,strict continuous c : Heap ⊸ T err (Heap) <strong>and</strong> all w ∈ W ,ι(r) ◦ w |={p}c{q} ⇔ w |={p ⊗ ι(r) ∗ r}c{q ⊗ ι(r) ∗ r}.


The proof of this claim makes use of the following equation:(p ⊗ ι(r) ∗ r)(w) ∗ ι −1 (w)(emp) = p(ι(r) ◦ w) ∗ ι −1 (ι(r) ◦ w)(emp),<strong>and</strong> analogously with q in place of p. To obtain this equation, note that byexp<strong>and</strong>ing the definition of ⊗ we have:(p ⊗ ι(r) ∗ r)(w) ∗ ι −1 (w)(emp) = (p ⊗ ι(r))(w) ∗ r(w) ∗ ι −1 (w)(emp)= p(ι(r) ◦ w) ∗ r(w ◦ emp) ∗ ι −1 (w)(emp)= p(ι(r) ◦ w) ∗ (r ⊗ w)(emp) ∗ ι −1 (w)(emp)= p(ι(r) ◦ w) ∗ (r ⊗ w ∗ ι −1 (w))(emp)= p(ι(r) ◦ w) ∗ ι −1 (ι(r) ◦ w)(emp)i.e., the equation is a direct consequence of the definitions of ⊗ <strong>and</strong> ◦.Lemma 22 (⊗-distribution, 3). The axioms (P ⊕Q)⊗R ⇔ (P ⊗R)⊕(Q⊗R)<strong>for</strong> ⊕ ∈ {⇒, ∧, ∨, ∗} are valid.Proof. We consider the case of separating conjunction. This case follows, since<strong>for</strong> all p, q, r ∈ Pred <strong>and</strong> w ∈ W we have((p ∗ q) ⊗ ι(r))(w) = (p ∗ q)(ι(r) ◦ w)= p(ι(r) ◦ w) ∗ q(ι(r) ◦ w)= (p ⊗ ι(r))(w) ∗ (q ⊗ ι(r))(w)= (p ⊗ ι(r) ∗ q ⊗ ι(r))(w).Note that these equations only depended on the fact that ∗ on Pred is definedpointwise. The cases <strong>for</strong> ⇒, ∧ <strong>and</strong> ∨ follow completely analogously. ⊓⊔Lemma 23 (⊗-distribution, 4). The axioms (κx.P ) ⊗ R ⇔ κx.(P ⊗ R) <strong>for</strong>κ ∈ {∀, ∃} <strong>and</strong> x /∈ fv(R) are valid.Proof. We consider the case of universal quantification. Let r = defR η, <strong>and</strong> notethat r = R η[x:=d]<strong>for</strong> any d ∈ Val, since x /∈ fv(R). The validity of the axiomfollows since(∀x.P ) ⊗ R ηw = ∀x.P η(ι(r) ◦ w)= ⋂P η[x:=d](ι(r) ◦ w)d∈Val= ⋂d∈Val(P η[x:=d]⊗ ι(R η[x:=d]))(w)= ∀x.(P ⊗ R) ηw.The case of existential quantification is analogous.Lemma 24 (⊗-distribution, 5). The following axioms are all valid:false⊗R ⇔ false, true⊗R ⇔ true, emp⊗R ⇔ emp, (e 1 ↦→ e 2 )⊗R ⇔ e 1 ↦→ e 2 .Proof. For all P , P ⊗ R ηw = P η(ι(R η) ◦ w) by definition of ⊗. Sincefalse, true, emp <strong>and</strong> e 1 ↦→ e 2 are all constant, the claim follows. ⊓⊔⊓⊔⊓⊔


A.3 Soundness of st<strong>and</strong>ard rules from separation logicThe following lemmas show that the usual rules of separation logic, expressedusing triples containing quoted comm<strong>and</strong>s as shown in Fig. 3, are sound.Lemma 25 (Skip). The axiom {P }‘skip’{P } is valid.Proof. This follows from the fact that skip ηh = h <strong>for</strong> all h ∈ Heap, <strong>and</strong> thatAd(·) is a closure operation.⊓⊔Lemma 26 (Conditional). If{P ∧ e 0 =e 1 }‘C’{Q} <strong>and</strong>{P ∧ e 0 ≠e 1 }‘D’{Q} areboth valid, then so is {P }‘if (e 0 =e 1 ) then C else D’{Q}.Proof. Let w ∈ W <strong>and</strong> r ∈ UAdm <strong>and</strong> suppose h ∈ P ηw ∗ ι −1 (w)(emp) ∗ r.From the semantics of the conditional, we can assume without loss of generalitythat e 0 η<strong>and</strong> e 1 ηare not both in Com ⊥ . We must show thatc(h) ∈ Ad(Q ηw ∗ ι −1 (w)(emp) ∗ r),where c(h) = if (e 0 η= e 1 η) then C ηh else D ηh. Depending on whethere 0 η= e 1 ηor not, we have e 0 =e 1 ηw = Heap or e 0 ≠e 1 ηw = Heap. There<strong>for</strong>e,the statement follows from either the first or the second assumed triple. ⊓⊔Lemma 27 (Sequencing). If {P }‘C’{R} <strong>and</strong> {R}‘D’{Q} are valid, then so is{P }‘C; D’{Q}.Proof. Let η ∈ Env <strong>and</strong> fix w ∈ W . Let r ∈ UAdm <strong>and</strong> assume h ∈ P η(w) ∗ι −1 (w)(emp)∗r. We must show that C; D ηh ∈ Ad(Q η(w)∗ι −1 (w)(emp)∗r).First note that C ηh ∈ Ad(R η(w) ∗ ι −1 (w)(emp) ∗ r), by the assumptionthat {P }‘C’{R} is valid. In particular, C ηh ≠ error. Moreover, in the casewhere C ηh = ⊥ we also have C; D ηh = ⊥ by the semantics of sequentialcomposition, so that the admissibility of Ad(Q η(w) ∗ ι −1 (w)(emp) ∗ r) givesthe result.Thus, we can assume that C; D ηh = D η(C ηh). From the assumptionthat{R}‘D’{Q} is valid it follows that D ηmaps R η(w)∗ι −1 (w)(emp)∗r intoAd(Q η(w) ∗ ι −1 (w)(emp) ∗ r). Since C ηh ∈ Ad(R η(w) ∗ ι −1 (w)(emp) ∗ r)we obtain D η(C ηh) ∈ Ad(Q η(w) ∗ ι −1 (w)(emp) ∗ r) by Lemma 15. ⊓⊔Lemma 28 (Update). The axiom {e ↦→ ∗ P }‘[e] := e 0 ’{e ↦→ e 0 ∗ P } is valid.Proof. By Lemma 10, it suffices to prove the validity of{e ↦→ }‘[e] := e 0 ’{e ↦→ e 0 }.Let η ∈ Env, p = e ↦→ η, q = e ↦→ e 0 η<strong>and</strong> c = [e] := e 0 η. We will show thatw |={p}c{q} holds <strong>for</strong> all w ∈ W .Let w ∈ W <strong>and</strong> r ∈ UAdm, <strong>and</strong> suppose h ∈ p(w)∗ι −1 (w)(emp)∗r. We mayassume that h ≠ ⊥, <strong>for</strong> otherwise c(h) = ⊥ ∈ q(w) ∗ ι −1 (w)(emp) ∗ r is immediate.Thus, h = h ′·h ′′ such that h ′ ∈ p(w) <strong>and</strong> h ′′ ∈ ι −1 (w)(emp)∗r. In particular,


since h ′ ∈ p(w) = e ↦→ ηw, we obtain that e η∈ dom(h ′ ) ⊆ dom(h). There<strong>for</strong>e,from the semantics of the assignment comm<strong>and</strong>, c(h) = h[e η↦→ e 1 η].But this heap is the same as {|e η= e 1 η|} · h ′′ , <strong>and</strong> there<strong>for</strong>e c(h) ∈ q(w) ∗ι −1 (w)(emp) ∗ r ⊆ Ad(q(w) ∗ ι −1 (w)(emp) ∗ r).⊓⊔Lemma 29 (Free). The axiom {e ↦→∗ P }‘free(e)’{P } is valid.Proof. By Lemma 10, it suffices to prove the validity of{e ↦→ }‘free(e)’{emp}.Let η ∈ Env, p = e ↦→ η, q = emp η<strong>and</strong> c = free(e) η. We will prove thatw |={p}c{q} holds <strong>for</strong> all w ∈ W .Let w ∈ W , let r ∈ UAdm <strong>and</strong> suppose h ∈ p(w) ∗ ι −1 (w)(emp) ∗ r. Sinceq(w) is the unit <strong>for</strong> ∗ <strong>and</strong> Ad(·) is a closure operation, we must only showc(h) ∈ ι −1 (w)(emp) ∗ r. We may assume that h ≠ ⊥, <strong>for</strong> otherwise c(h) =⊥ ∈ ι −1 (w)(emp) ∗ r is immediate. Thus, h = h ′ · h ′′ such that h ′ ∈ p(w) <strong>and</strong>h ′′ ∈ ι −1 (w)(emp)∗r. In particular, since h ′ ∈ p(w) = e ↦→ ηw, we obtain that{e η} = dom(h ′ ) ⊆ dom(h). There<strong>for</strong>e, from the semantics of the deallocationcomm<strong>and</strong>, c(h) = h ′′ . It follows that c(h) ∈ ι −1 (w)(emp) ∗ r.⊓⊔Lemma 30 (Deref). If {P ∗ e ↦→ x}‘C’{Q} is valid <strong>and</strong> x is not free in e <strong>and</strong>Q, then {∃x.P ∗ e ↦→ x}‘let x=[e] in C’{Q} is also valid.Proof. Assume that {P ∗ e ↦→ x}‘C’{Q} is valid, <strong>and</strong> pick η ∈ Env. Let c =let x=[e] in C η. We will show that w |={∃x.P ∗ e ↦→ x η}c{Q η} <strong>for</strong> all w ∈ W .Let w ∈ W , r ∈ UAdm <strong>and</strong> h ∈ ∃x.P ∗ e ↦→ x η(w) ∗ ι −1 (w)(emp) ∗ r. Wemust show that c(h) ∈ Ad(Q η(w) ∗ ι −1 (w)(emp) ∗ r). By definition there areheaps h ′ , h ′′ such h = h ′·h ′′ <strong>and</strong> h ′ ∈ ∃x.P ∗ e ↦→ x η(w) <strong>and</strong> h ′′ ∈ ι −1 (w)(emp)∗r. By definition this means that∀n. ∃d n ∈ Val. π n (h ′ ) ∈ P ∗ e ↦→ x η[x:=dn] (w).Let us write η n <strong>for</strong> η[x := d n ]. In the remainder of the proof, we will prove that∀n. c(π n (h)) ∈ Ad(Q η∗ ι −1 (w)(emp) ∗ r),because then, by admissibility <strong>and</strong> the continuity of c, we obtain the requiredc(h) ∈ Ad(Q η∗ ι −1 (w)(emp) ∗ r).Without loss of generality we can assume that π n (h) ≠ ⊥, so that π n (h ′ ) ≠ ⊥as well. Then, since x /∈ fv(e), we have in particular e η∈ dom(π n (h ′ )) ⊆dom(h) <strong>and</strong> π n (h ′ )(e η) ⊑ d n . Using the monotonicity of comm<strong>and</strong>s with respectto the environment, this givesc(π n (h)) = C η[x:=πn(h ′ )(e η )] (π n(h)) ⊑ C ηn(π n (h))By uni<strong>for</strong>mity of ι −1 (w)(emp)∗r, we have π n (h) ∈ P ∗ e ↦→ x ηn∗ι −1 (w)(emp)∗r, so that the assumption gives usc(π n (h)) ⊑ C ηn(π n (h)) ∈ Ad(Q ηn∗ ι −1 (w)(emp) ∗ r).


Since Ad(p ′ ) is a downward-closed set <strong>for</strong> every p ′ , the above <strong>for</strong>mula impliesthat c(π n (h)) belongs to the set on the right h<strong>and</strong> side. Furthermore, sincex /∈ fv(Q), we have Q ηn= Q η. The combination of these two facts give thedesired c(π n (h)) ∈ Ad(Q η∗ ι −1 (w)(emp) ∗ r).⊓⊔Lemma 31 (New). If {P ∗ x ↦→ e}‘C’{Q} is valid <strong>and</strong> x is not free in P , Q <strong>and</strong>e, then {P }‘let x=new e in C’{Q} is valid.Proof. Let w ∈ W , η ∈ Env, r ∈ UAdm. Suppose h ∈ P η(w)∗ι −1 (w)(emp)∗r.We must show that c(h) ∈ Ad(Q η(w)∗ι −1 (w)(emp)∗r). Consider the followingenvironment η ′ <strong>and</strong> heap h ′ :η ′ = defη[x := l] h ′ = defh · {|l = e η|}where l is the least natural number not contained in dom(h). Since x is not freein e <strong>and</strong> P , we have e η= e η ′ <strong>and</strong> P η= P η ′. Thus by the assumption onh we obtain:h ′ ∈ P ∗ x ↦→ e η ′ w ∗ ι −1 (w)(emp) ∗ r.Then the assumption that{P ∗ x ↦→ e}‘C’{Q} is valid implies:C η ′ h ′ ∈ Ad(Q η ′ (w) ∗ ι −1 (w)(emp) ∗ r).Using the fact that let x=new e in C η(h) = C η ′ h ′ <strong>and</strong> since Q η ′ = Q η,this proves the statement.⊓⊔Up until now, we have proved the soundness of all the rules in Fig. 3, exceptthe rule of Consequence. The soundness proof of this Consequence ruleis slightly different from those of the others, because Consequence is an axiominvolving an implication between triples, whereas the other rules are inferencerules <strong>for</strong> trans<strong>for</strong>ming valid <strong>Hoare</strong> triples. Due to the pointwise interpretationof implication <strong>and</strong> the inclusion of the approximations in the interpretation oftriples, this <strong>for</strong>m of the Consequence rule could be potentially problematic. Ourproof of Consequence overcomes this potential problem, exploiting the fact thatthe rule is parametric in the comm<strong>and</strong>: it is the same comm<strong>and</strong> that appearsin all the triples of the rule. To illustrate this proof technique, in addition tothe soundness of Consequence, we will prove two other rules that also involveimplications between triples.Lemma 32 (Consequence). If P ′ ⇒ P <strong>and</strong> Q ⇒ Q ′ are valid, then so is{P }e{Q} ⇒{P ′ }e{Q ′ }.Proof. Let η ∈ Env <strong>and</strong> fix w ∈ W <strong>and</strong> n > 0. Let p = P η, p ′ = P ′ η,q = Q η<strong>and</strong> q ′ = Q ′ η, <strong>and</strong> assume that π n (h) ∈ {P }e{Q} ηw. We mustshow that π n (h) ∈ {P ′ }e{Q ′ } ηw.Let k denote the rank of π n (h). Without loss of generality, we can assumek > 0. Let c be π k−1 ; e η; π k−1 . Then the assumption yields w |={p}c{q}, <strong>and</strong> it


A.4 RecursionTheorem 35 (Mutually recursive predicates). Suppose that <strong>for</strong> each i ∈I, F i : Pred I → Pred is a contractive function. Then there exists a uniquep = (p i ) i∈I ∈ Pred I such that F i (p) = p i <strong>for</strong> all i ∈ I.Proof. Recall that Pred I is an ultrametric space, with distance function d(p, q) =sup i d Pred (p i , q i ) <strong>for</strong> p = (p i ) i∈I <strong>and</strong> q = (q i ) i∈I in Pred I . By assumption, eachF i is contractive. In particular, since all distances in Pred are of the <strong>for</strong>m 2 −n ,we have d Pred (F i (p), F i (q)) ≤ 1 2 · d(p, q). Then F : PredI → Pred I given byF (p) = (F i (p)) i∈I is a contractive endofunction on this ultrametric space, whichby the Banach fixed point theorem has a unique fixed point p, satisfying p i =(F (p)) i = F i (p). ⊓⊔

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!